/// <summary> /// A simple 1D kernel using math functions. /// The <see cref="IntrinsicMath"/> class contains intrinsic math functions that - /// in contrast to the default .Net Math class - work on both floats and doubles. Note that /// the /// <see cref="IntrinsicMath"/> class is supported on all accelerators. /// The CompileUnitFlags.FastMath flag can be used during the creation of the compile unit /// to enable fast math intrinsics. /// Note that the full power of math functions on all accelerators is available via the /// Algorithms library (see ILGPU.Algorithms.Math sample). /// </summary> /// <param name="index">The current thread index.</param> /// <param name="dataView">The view pointing to our memory buffer.</param> /// <param name="constant">A uniform constant.</param> static void MathKernel( Index1D index, // The global thread index (1D in this case) ArrayView <float> singleView, // A view of floats to store float results from GPUMath ArrayView <double> doubleView, // A view of doubles to store double results from GPUMath ArrayView <double> doubleView2) // A view of doubles to store double results from .Net Math { // Note the different returns type of GPUMath.Sqrt and Math.Sqrt. singleView[index] = IntrinsicMath.Abs(index); doubleView[index] = IntrinsicMath.Clamp((double)(int)index, 0.0, 12.0); // Note that use can safely use functions from the Math class as long as they have a counterpart // in the IntrinsicMath class. doubleView2[index] = Math.Min(0.2, index); }
/// <summary> /// Returns true if the given integer is a power of two. /// </summary> /// <param name="value">The integer value.</param> /// <returns>True, if the given integer is a power of two.</returns> public static bool IsPowerOf2(long value) => value != long.MinValue && IsPowerOf2((ulong)IntrinsicMath.Abs(value));
private static float Distance(Point a, Point b, Point p) { return(IntrinsicMath.Abs((p.Y - a.Y) * (b.X - a.X) - (b.Y - a.Y) * (p.X - a.X))); }
public static long Abs(long value) => IntrinsicMath.Abs(value);
public static short Abs(short value) => IntrinsicMath.Abs(value);
public static int Abs(int value) => IntrinsicMath.Abs(value);
public static sbyte Abs(sbyte value) => IntrinsicMath.Abs(value);
public static float Abs(float value) => IntrinsicMath.Abs(value);
public static double Abs(double value) => IntrinsicMath.Abs(value);
/// <summary cref="UnorderedTransformation.PerformTransformation(Method.Builder)"/> protected override bool PerformTransformation(Method.Builder builder) { var scope = builder.CreateScope(); var cfg = scope.CreateCFG(); var ifInfos = IfInfos.Create(cfg); bool converted = false; foreach (IfInfo ifInfo in ifInfos) { // Check for an extremely simple if block to convert if (!ifInfo.IsSimpleIf()) { continue; } // Check size constraints int ifBlockSize = ifInfo.IfBlock.Count; int elseBlockSize = ifInfo.ElseBlock.Count; int blockSizeDiff = IntrinsicMath.Abs(ifBlockSize - elseBlockSize); if (ifBlockSize > MaxBlockSize || elseBlockSize > MaxBlockSize || blockSizeDiff > DefaultMaxSizeDifference) { continue; } // Check for side effects if (ifInfo.HasSideEffects()) { continue; } // Convert the current if block var variableInfo = ifInfo.ResolveVariableInfo(); var blockBuilder = builder[ifInfo.EntryBlock]; blockBuilder.MergeBlock(ifInfo.IfBlock); blockBuilder.MergeBlock(ifInfo.ElseBlock); blockBuilder.MergeBlock(ifInfo.ExitBlock); // Convert all phi values var condition = ifInfo.Condition; foreach (var variableEntry in variableInfo) { var variable = variableEntry.Value; blockBuilder.SetupInsertPosition(variableEntry.Key); var predicate = blockBuilder.CreatePredicate( condition, variable.TrueValue, variable.FalseValue); // Replace the phi node variableEntry.Key.Replace(predicate); blockBuilder.Remove(variableEntry.Key); } converted = true; } return(converted); }
/// <summary> /// Returns true if the given integer is a power of two. /// </summary> /// <param name="value">The integer value.</param> /// <returns>True, if the given integer is a power of two.</returns> public static bool IsPowerOf2(long value) => value == long.MinValue ? false : IsPowerOf2((ulong)IntrinsicMath.Abs(value));