From 1ecbc5285930546c2e48b82042f82f91f798c678 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 9 Oct 2023 09:48:59 -0400 Subject: [PATCH] Remove a few unnecessary `unsafe` keyword uses in TensorPrimitives --- .../Numerics/Tensors/TensorPrimitives.cs | 10 +++++----- .../Tensors/TensorPrimitives.netcore.cs | 20 +++++++++---------- .../Tensors/TensorPrimitives.netstandard.cs | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs index 4b4a11bfaa84ca..9ee72d7735d201 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs @@ -45,7 +45,7 @@ public static void Abs(ReadOnlySpan x, Span destination) => /// If either of the element-wise input values is equal to , the resulting element-wise value is also NaN. /// /// - public static unsafe void Add(ReadOnlySpan x, ReadOnlySpan y, Span destination) => + public static void Add(ReadOnlySpan x, ReadOnlySpan y, Span destination) => InvokeSpanSpanIntoSpan(x, y, destination); /// Computes the element-wise addition of single-precision floating-point numbers in the specified tensors. @@ -326,7 +326,7 @@ public static void Exp(ReadOnlySpan x, Span destination) => /// operating systems or architectures. /// /// - public static unsafe int IndexOfMax(ReadOnlySpan x) + public static int IndexOfMax(ReadOnlySpan x) { int result = -1; @@ -376,7 +376,7 @@ public static unsafe int IndexOfMax(ReadOnlySpan x) /// operating systems or architectures. /// /// - public static unsafe int IndexOfMaxMagnitude(ReadOnlySpan x) + public static int IndexOfMaxMagnitude(ReadOnlySpan x) { int result = -1; @@ -429,7 +429,7 @@ public static unsafe int IndexOfMaxMagnitude(ReadOnlySpan x) /// operating systems or architectures. /// /// - public static unsafe int IndexOfMin(ReadOnlySpan x) + public static int IndexOfMin(ReadOnlySpan x) { int result = -1; @@ -479,7 +479,7 @@ public static unsafe int IndexOfMin(ReadOnlySpan x) /// operating systems or architectures. /// /// - public static unsafe int IndexOfMinMagnitude(ReadOnlySpan x) + public static int IndexOfMinMagnitude(ReadOnlySpan x) { int result = -1; diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs index db7fce0da92483..89dc5e91559968 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netcore.cs @@ -1210,7 +1210,7 @@ private static float MinMaxCore(ReadOnlySpan x) /// Performs an element-wise operation on and writes the results to . /// Specifies the operation to perform on each element loaded from . - private static unsafe void InvokeSpanIntoSpan( + private static void InvokeSpanIntoSpan( ReadOnlySpan x, Span destination) where TUnaryOperator : struct, IUnaryOperator { @@ -1326,7 +1326,7 @@ private static unsafe void InvokeSpanIntoSpan( /// /// Specifies the operation to perform on the pair-wise elements loaded from and . /// - private static unsafe void InvokeSpanSpanIntoSpan( + private static void InvokeSpanSpanIntoSpan( ReadOnlySpan x, ReadOnlySpan y, Span destination) where TBinaryOperator : struct, IBinaryOperator { @@ -1456,7 +1456,7 @@ private static unsafe void InvokeSpanSpanIntoSpan( /// /// Specifies the operation to perform on each element loaded from with . /// - private static unsafe void InvokeSpanScalarIntoSpan( + private static void InvokeSpanScalarIntoSpan( ReadOnlySpan x, float y, Span destination) where TBinaryOperator : struct, IBinaryOperator => InvokeSpanScalarIntoSpan(x, y, destination); @@ -1472,7 +1472,7 @@ private static unsafe void InvokeSpanScalarIntoSpan( /// /// Specifies the operation to perform on the transformed value from with . /// - private static unsafe void InvokeSpanScalarIntoSpan( + private static void InvokeSpanScalarIntoSpan( ReadOnlySpan x, float y, Span destination) where TTransformOperator : struct, IUnaryOperator where TBinaryOperator : struct, IBinaryOperator @@ -1603,7 +1603,7 @@ private static unsafe void InvokeSpanScalarIntoSpan, , /// and . /// - private static unsafe void InvokeSpanSpanSpanIntoSpan( + private static void InvokeSpanSpanSpanIntoSpan( ReadOnlySpan x, ReadOnlySpan y, ReadOnlySpan z, Span destination) where TTernaryOperator : struct, ITernaryOperator { @@ -1743,7 +1743,7 @@ private static unsafe void InvokeSpanSpanSpanIntoSpan( /// Specifies the operation to perform on the pair-wise elements loaded from and /// with . /// - private static unsafe void InvokeSpanSpanScalarIntoSpan( + private static void InvokeSpanSpanScalarIntoSpan( ReadOnlySpan x, ReadOnlySpan y, float z, Span destination) where TTernaryOperator : struct, ITernaryOperator { @@ -1887,7 +1887,7 @@ private static unsafe void InvokeSpanSpanScalarIntoSpan( /// Specifies the operation to perform on the pair-wise element loaded from , with , /// and the element loaded from . /// - private static unsafe void InvokeSpanScalarSpanIntoSpan( + private static void InvokeSpanScalarSpanIntoSpan( ReadOnlySpan x, float y, ReadOnlySpan z, Span destination) where TTernaryOperator : struct, ITernaryOperator { @@ -2142,7 +2142,7 @@ private static float GetFirstNaN(Vector512 vector) /// and zero for all other elements. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe Vector128 CreateRemainderMaskSingleVector128(int count) => + private static Vector128 CreateRemainderMaskSingleVector128(int count) => Vector128.LoadUnsafe( ref Unsafe.As(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x16)), (uint)((count * 16) + 12)); // last four floats in the row @@ -2152,7 +2152,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16 /// and zero for all other elements. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe Vector256 CreateRemainderMaskSingleVector256(int count) => + private static Vector256 CreateRemainderMaskSingleVector256(int count) => Vector256.LoadUnsafe( ref Unsafe.As(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x16)), (uint)((count * 16) + 8)); // last eight floats in the row @@ -2163,7 +2163,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16 /// and zero for all other elements. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static unsafe Vector512 CreateRemainderMaskSingleVector512(int count) => + private static Vector512 CreateRemainderMaskSingleVector512(int count) => Vector512.LoadUnsafe( ref Unsafe.As(ref MemoryMarshal.GetReference(RemainderUInt32Mask_16x16)), (uint)(count * 16)); // all sixteen floats in the row diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs index 15d0a8aaf72928..631d9354351a87 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.netstandard.cs @@ -750,7 +750,7 @@ ref Unsafe.As>( private static unsafe bool IsNegative(float f) => *(int*)&f < 0; /// Gets whether each specified is negative. - private static unsafe Vector IsNegative(Vector f) => + private static Vector IsNegative(Vector f) => (Vector)Vector.LessThan((Vector)f, Vector.Zero); /// Gets the base 2 logarithm of . @@ -760,7 +760,7 @@ private static unsafe Vector IsNegative(Vector f) => /// Gets a vector mask that will be all-ones-set for the last elements /// and zero for all other elements. /// - private static unsafe Vector CreateRemainderMaskSingleVector(int count) + private static Vector CreateRemainderMaskSingleVector(int count) { Debug.Assert(Vector.Count is 4 or 8 or 16);