Repository: dotnet/runtime
File: src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.BitDecrement.cs (C#)

Code before:
                    Vector128<uint> isPosInf = Vector128.Equals(bits, Vector128.Create(0x7F80_0000u)); // PositiveInfinityBits
                    Vector128<uint> isPosZero = Vector128.Equals(bits, Vector128.Create(0x0000_0000u)); // PositiveZeroBits
                    Vector128<uint> isNegInf = Vector128.Equals(bits, Vector128.Create(0xFF80_0000u)); // NegativeInfinityBits

                    // Determine if negative (sign bit set)
                    Vector128<uint> isNegative = Vector128.Equals(bits & Vector128.Create(0x8000_0000u), Vector128.Create(0x8000_0000u));

                    // Compute bit incremented/decremented results
                    Vector128<uint> incremented = bits + Vector128<uint>.One;
                    Vector128<uint> decremented = bits - Vector128<uint>.One;

                    // Select based on sign: negative -> increment, positive -> decrement
                    Vector128<uint> result = Vector128.ConditionalSelect(isNegative, incremented, decremented);

                    // Handle special cases
                    // +0.0 -> -Epsilon
                    result = Vector128.ConditionalSelect(isPosZero, Vector128.Create(BitConverter.SingleToUInt32Bits(-float.Epsilon)), result);

                    // +Infinity -> MaxValue
                    result = Vector128.ConditionalSelect(isPosInf, Vector128.Create(BitConverter.SingleToUInt32Bits(float.MaxValue)), result);

                    // NaN -> NaN (return original), -Infinity -> -Infinity (return original)
                    Vector128<uint> preserveOriginal = isNaN | isNegInf;
                    result = Vector128.ConditionalSelect(preserveOriginal, bits, result);

                    return result.AsSingle().As<float, T>();
                }
                else if (typeof(T) == typeof(double))
                {
                    Vector128<double> xDouble = x.AsDouble();
                    Vector128<ulong> bits = xDouble.AsUInt64();

                    // Create masks for special cases
                    Vector128<ulong> isNaN = ~Vector128.Equals(xDouble, xDouble).AsUInt64();
                    Vector128<ulong> isPosInf = Vector128.Equals(bits, Vector128.Create(0x7FF0_0000_0000_0000ul)); // PositiveInfinityBits
                    Vector128<ulong> isPosZero = Vector128.Equals(bits, Vector128.Create(0x0000_0000_0000_0000ul)); // PositiveZeroBits
                    Vector128<ulong> isNegInf = Vector128.Equals(bits, Vector128.Create(0xFFF0_0000_0000_0000ul)); // NegativeInfinityBits

                    // Determine if negative (sign bit set)
                    Vector128<ulong> isNegative = Vector128.Equals(bits & Vector128.Create(0x8000_0000_0000_0000ul), Vector128.Create(0x8000_0000_0000_0000ul));

                    // Compute bit incremented/decremented results
                    Vector128<ulong> incremented = bits + Vector128<ulong>.One;
                    Vector128<ulong> decremented = bits - Vector128<ulong>.One;

                    // Select based on sign: negative -> increment, positive -> decrement
                    Vector128<ulong> result = Vector128.ConditionalSelect(isNegative, incremented, decremented);

                    // Handle special cases
                    // +0.0 -> -Epsilon
                    result = Vector128.ConditionalSelect(isPosZero, Vector128.Create(BitConverter.DoubleToUInt64Bits(-double.Epsilon)), result);

Review comment (suggestion):
@copilot, is this following the exact same logic as https://github.com/dotnet/runtime/blob/b19edc085666acaf215ff60edce24cada71e2f93/src/libraries/System.Private.CoreLib/src/System/MathF.cs#L84-L114 ? I ask because some of the comparisons look different, e.g. this is comparing against positive zero and returning -Epsilon, whereas MathF.BitIncrement compares against -0 and returns +Epsilon.

Please double check that everything for float is the same as for MathF.BitIncrement and for double the same as Math.BitIncrement (except that these SIMD implementations are branchless). Similarly for BitDecrement.

Code after:
                        Vector128.IsNegative(xFloat).AsUInt32(),
                        bits + Vector128<uint>.One,
                        bits - Vector128<uint>.One);

                    // Handle special cases with a single conditional select
                    Vector128<uint> isPositiveZero = Vector128.IsZero(xFloat).AsUInt32();
                    Vector128<uint> specialValue = Vector128.Create(BitConverter.SingleToUInt32Bits(-float.Epsilon)) & isPositiveZero;

                    Vector128<uint> isNaNOrNegInf = (Vector128.IsNaN(xFloat) | Vector128.IsNegativeInfinity(xFloat)).AsUInt32();
                    specialValue |= bits & isNaNOrNegInf;

                    Vector128<uint> specialMask = isPositiveZero | isNaNOrNegInf;
                    return Vector128.ConditionalSelect(specialMask, specialValue, result).AsSingle().As<float, T>();
                }

                if (typeof(T) == typeof(double))
                {
                    Vector128<double> xDouble = x.AsDouble();
                    Vector128<ulong> bits = xDouble.AsUInt64();

                    // General case: negative -> increment, positive -> decrement
                    Vector128<ulong> result = Vector128.ConditionalSelect(
                        Vector128.IsNegative(xDouble).AsUInt64(),
                        bits + Vector128<ulong>.One,
                        bits - Vector128<ulong>.One);

                    // Handle special cases with a single conditional select
                    Vector128<ulong> isPositiveZero = Vector128.IsZero(xDouble).AsUInt64();
                    Vector128<ulong> specialValue = Vector128.Create(BitConverter.DoubleToUInt64Bits(-double.Epsilon)) & isPositiveZero;

                    Vector128<ulong> isNaNOrNegInf = (Vector128.IsNaN(xDouble) | Vector128.IsNegativeInfinity(xDouble)).AsUInt64();
                    specialValue |= bits & isNaNOrNegInf;

                    Vector128<ulong> specialMask = isPositiveZero | isNaNOrNegInf;
                    return Vector128.ConditionalSelect(specialMask, specialValue, result).AsDouble().As<double, T>();
                }

                // Fallback for unsupported types - should not be reached since Vectorizable returns false
                throw new NotSupportedException();
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Vector256<T> Invoke(Vector256<T> x)
            {
                if (typeof(T) == typeof(float))
                {
                    Vector256<float> xFloat = x.AsSingle();
                    Vector256<uint> bits = xFloat.AsUInt32();

                    // General case: negative -> increment, positive -> decrement
                    Vector256<uint> result = Vector256.ConditionalSelect(