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

Code before:
                ushort bits = (ushort)x;

                // Check if not finite: NaN or Infinity
                if ((~bits & PositiveInfinityBits) == 0)
                {
                    // NaN returns NaN, -Infinity returns -Infinity, +Infinity returns MaxValue
                    return bits == PositiveInfinityBits ? (short)MaxValueBits : x;
                }

                // +0.0 returns -Epsilon
                if (bits == PositiveZeroBits)
                {
                    return unchecked((short)NegativeEpsilonBits);
                }

                // Negative values need to be incremented, positive values need to be decremented
                return (short)(bits < SignMask ? bits - 1 : bits + 1);
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Vector128<short> Invoke(Vector128<short> x)
            {
                Vector128<ushort> bits = x.AsUInt16();

                // General case: negative -> increment, positive -> decrement
                Vector128<ushort> isNegative = Vector128.ShiftRightArithmetic(x, 15).AsUInt16();
                Vector128<ushort> result = Vector128.ConditionalSelect(
                    isNegative,
                    bits + Vector128<ushort>.One,
                    bits - Vector128<ushort>.One);

                // Handle special cases with a single conditional select
                Vector128<ushort> isPositiveZero = Vector128.Equals(bits, Vector128<ushort>.Zero);
                Vector128<ushort> specialValue = Vector128.Create(NegativeEpsilonBits) & isPositiveZero;

                // NaN: (bits & 0x7FFF) > 0x7C00 (both positive and negative NaN)
                // -Infinity: bits == 0xFC00
                Vector128<ushort> absValue = bits & Vector128.Create((ushort)0x7FFF);
                Vector128<ushort> isNaN = Vector128.GreaterThan(absValue, Vector128.Create(PositiveInfinityBits));
                Vector128<ushort> isNegInf = Vector128.Equals(bits, Vector128.Create(NegativeInfinityBits));
                Vector128<ushort> isNaNOrNegInf = isNaN | isNegInf;
                specialValue |= bits & isNaNOrNegInf;

                Vector128<ushort> specialMask = isPositiveZero | isNaNOrNegInf;
                return Vector128.ConditionalSelect(specialMask, specialValue, result).AsInt16();
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Vector256<short> Invoke(Vector256<short> x)
            {
                Vector256<ushort> bits = x.AsUInt16();

Review comment (suggestion):
nit:
```suggestion
                Vector128<ushort> isNegative = Vector128.IsNegative(x).AsUInt16();
```

This will do a proper comparison and allow other optimizations to light up, it's also clearer as to intent.

Code after:
                Vector128<ushort> bits = x.AsUInt16();

                // General case: negative -> increment, positive -> decrement
                Vector128<ushort> isNegative = Vector128.IsNegative(x).AsUInt16();
                Vector128<ushort> result = Vector128.ConditionalSelect(
                    isNegative,
                    bits + Vector128<ushort>.One,
                    bits - Vector128<ushort>.One);

                // Handle special cases with a single conditional select
                Vector128<ushort> isPositiveZero = Vector128.IsZero(bits);
                Vector128<ushort> specialValue = Vector128.Create(NegativeEpsilonBits) & isPositiveZero;

                // NaN: (bits & 0x7FFF) > 0x7C00 (both positive and negative NaN)
                // -Infinity: bits == 0xFC00
                Vector128<ushort> absValue = bits & Vector128.Create((ushort)0x7FFF);
                Vector128<ushort> isNaN = Vector128.GreaterThan(absValue, Vector128.Create(PositiveInfinityBits));
                Vector128<ushort> isNegInf = Vector128.Equals(bits, Vector128.Create(NegativeInfinityBits));
                Vector128<ushort> isNaNOrNegInf = isNaN | isNegInf;
                specialValue |= bits & isNaNOrNegInf;

                Vector128<ushort> specialMask = isPositiveZero | isNaNOrNegInf;
                return Vector128.ConditionalSelect(specialMask, specialValue, result).AsInt16();
            }

            [MethodImpl(MethodImplOptions.AggressiveInlining)]
            public static Vector256<short> Invoke(Vector256<short> x)
            {
                Vector256<ushort> bits = x.AsUInt16();

                // General case: negative -> increment, positive -> decrement
                Vector256<ushort> isNegative = Vector256.IsNegative(x).AsUInt16();
                Vector256<ushort> result = Vector256.ConditionalSelect(
                    isNegative,
                    bits + Vector256<ushort>.One,
                    bits - Vector256<ushort>.One);

                // Handle special cases with a single conditional select
                Vector256<ushort> isPositiveZero = Vector256.IsZero(bits);
                Vector256<ushort> specialValue = Vector256.Create(NegativeEpsilonBits) & isPositiveZero;

                // NaN: (bits & 0x7FFF) > 0x7C00 (both positive and negative NaN)
                // -Infinity: bits == 0xFC00
                Vector256<ushort> absValue = bits & Vector256.Create((ushort)0x7FFF);
                Vector256<ushort> isNaN = Vector256.GreaterThan(absValue, Vector256.Create(PositiveInfinityBits));
                Vector256<ushort> isNegInf = Vector256.Equals(bits, Vector256.Create(NegativeInfinityBits));
                Vector256<ushort> isNaNOrNegInf = isNaN | isNegInf;
                specialValue |= bits & isNaNOrNegInf;

                Vector256<ushort> specialMask = isPositiveZero | isNaNOrNegInf;
                return Vector256.ConditionalSelect(specialMask, specialValue, result).AsInt16();