diff --git a/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt b/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt index b7d20f2640..156c2c9f1d 100644 --- a/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt +++ b/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt @@ -1,6 +1,6 @@ // --------------------------------------------------------------------------------------- // ILGPU Algorithms -// Copyright (c) 2023 ILGPU Project +// Copyright (c) 2023-2024 ILGPU Project // www.ilgpu.net // // File: VectorTypes.tt/VectorTypes.cs @@ -11,6 +11,7 @@ <#@ template debug="false" hostspecific="false" language="C#" #> <#@ include file="../TypeInformation.ttinclude"#> +<#@ include file="../FixedPrecision/FixedIntConfig.ttinclude"#> <#@ assembly name="System.Core" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> @@ -19,7 +20,11 @@ // Please note that this code does not support FP16 at the moment because ILGPU.Half does // not support the INumberBase and INumber interfaces and will be considered // obsolete in the future anyway. -var allTypes = IntTypes.Concat(FloatTypes.Skip(1)); +var fixedPrecisionTypes = FixedPrecisionIntTypes + .SelectMany(t => t.ToBasicTypeInformation()).ToHashSet(); +var allTypes = IntTypes + .Concat(FloatTypes.Skip(1)) + .Concat(fixedPrecisionTypes); var typesByRawName = allTypes.ToDictionary(t => t.Type); var accumulationTypes = new Dictionary() { @@ -62,6 +67,7 @@ string GetTypeName(TypeInformation type, int vectorLength) return $"{baseTypeName}{postFix}x{vectorLength}"; } #> +using ILGPU.Algorithms.FixedPrecision; using ILGPU.Algorithms.Random; using ILGPU.Runtime; using System; @@ -81,6 +87,7 @@ namespace ILGPU.Algorithms.Vectors <# foreach (var type in allTypes) { #> <# var accumulations = accumulationTypes.TryGetValue(type.Name, out var accTypes) ? accTypes : Array.Empty(); #> +<# var propPostfix = fixedPrecisionTypes.Contains(type) ? ".RawValue" : "" ; #> <# foreach (var vectorLength in vectorLengths) { #> <# var typeName = GetTypeName(type, vectorLength); #> /// @@ -103,7 +110,7 @@ namespace ILGPU.Algorithms.Vectors /// The offset of the <#= vectorItemNames[i] #> field in bytes. /// public static readonly int Offset<#= vectorItemNames[i] #> = - sizeof(<#= type.Type #>) * <#= i #>; + Interop.SizeOf<<#= type.Type #>>() * <#= i #>; /// /// The offset of the <#= vectorItemNames[i] #> field in bytes. @@ -120,7 +127,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Returns the radix of the underlying value. /// - public static int Radix => 10; + public static int Radix => 2; /// /// Returns an invalid value (min [signed types], max value [unsigned] or NaN). @@ -143,7 +150,8 @@ namespace ILGPU.Algorithms.Vectors /// /// Returns the value one. /// - public static <#= typeName #> One => FromScalar(<#= type.FormatNumber("1") #>); + public static <#= typeName #> One => + FromScalar((<#= type.Type #>)<#= type.FormatNumber("1") #>); /// /// Returns the value zero. @@ -161,12 +169,13 @@ namespace ILGPU.Algorithms.Vectors /// The first value. /// The second value. /// The min value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> Min( <#= typeName #> first, <#= typeName #> second) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) - .Select(t => $"({type.Type})Math.Min(first.{t}, second.{t})")) #>); + .Select(t => $"({type.Type})Math.Min(first.{t}{propPostfix}, second.{t}{propPostfix})")) #>); /// /// Computes the max value of both. @@ -174,12 +183,13 @@ namespace ILGPU.Algorithms.Vectors /// The first value. /// The second value. /// The max value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> Max( <#= typeName #> first, <#= typeName #> second) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) - .Select(t => $"({type.Type})Math.Max(first.{t}, second.{t})")) #>); + .Select(t => $"({type.Type})Math.Max(first.{t}{propPostfix}, second.{t}{propPostfix})")) #>); /// /// Clamps the given value. @@ -307,6 +317,7 @@ namespace ILGPU.Algorithms.Vectors /// /// The target memory address. /// The current value to add. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void AtomicAdd(ref <#= typeName #> target, <#= typeName #> value) { ref var elementRef = ref Unsafe.As<<#= typeName #>, <#= type.Type #>>( @@ -662,13 +673,14 @@ namespace ILGPU.Algorithms.Vectors /// /// Returns the absolute value. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> Abs(<#= typeName #> value) => <# if (type.IsUnsignedInt) { #> value; <# } else { #> new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) - .Select(t => $"Math.Abs(value.{t})")) #>); + .Select(t => $"({type.Type})Math.Abs(value.{t}{propPostfix})")) #>); <# } #> /// @@ -682,11 +694,13 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator --(<#= typeName #> value) => value - One; /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator /(<#= typeName #> left, <#= typeName #> right) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) @@ -695,11 +709,13 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator ++(<#= typeName #> value) => value + One; /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator *(<#= typeName #> left, <#= typeName #> right) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) @@ -708,6 +724,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator -(<#= typeName #> left, <#= typeName #> right) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) @@ -717,6 +734,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Not supported operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] static <#= typeName #> IUnaryNegationOperators<<#= typeName #>, <#= typeName #>>. operator -(<#= typeName #> value) => value; <# } else { #> @@ -726,17 +744,19 @@ namespace ILGPU.Algorithms.Vectors public static <#= typeName #> operator -(<#= typeName #> value) => new <#= typeName #>(<#= string.Join(", ", vectorItemNames .Take(vectorLength) - .Select(t => $"({type.Type})-value.{t}")) #>); + .Select(t => $"({type.Type})(-value.{t})")) #>); <# } #> /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> operator +(<#= typeName #> value) => value; /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> MaxMagnitude(<#= typeName #> x, <#= typeName #> y) => <# if (type.IsUnsignedInt) { #> Max(x, y); @@ -749,6 +769,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> MaxMagnitudeNumber(<#= typeName #> x, <#= typeName #> y) => <# if (type.IsInt) { #> MaxMagnitude(x, y); @@ -761,6 +782,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> MinMagnitude(<#= typeName #> x, <#= typeName #> y) => <# if (type.IsUnsignedInt) { #> Min(x, y); @@ -773,6 +795,7 @@ namespace ILGPU.Algorithms.Vectors /// /// Performs the specified operation. /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static <#= typeName #> MinMagnitudeNumber(<#= typeName #> x, <#= typeName #> y) => <# if (type.IsInt) { #> MinMagnitude(x, y);