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

Code before:
                {
                    Vector128<double> xDouble = x.AsDouble();
                    Vector128<ulong> bits = xDouble.AsUInt64();

                    // Select based on sign: negative -> increment, positive -> decrement
                    Vector128<ulong> isNegative = Vector128.Equals(bits & Vector128.Create(0x8000_0000_0000_0000ul), Vector128.Create(0x8000_0000_0000_0000ul));
                    Vector128<ulong> result = Vector128.ConditionalSelect(
                        isNegative,
                        bits + Vector128<ulong>.One,
                        bits - Vector128<ulong>.One);

                    // Handle special cases
                    // +0.0 -> -Epsilon
                    Vector128<ulong> isPosZero = Vector128.Equals(bits, Vector128.Create(0x0000_0000_0000_0000ul)); // PositiveZeroBits
                    result = Vector128.ConditionalSelect(isPosZero, Vector128.Create(BitConverter.DoubleToUInt64Bits(-double.Epsilon)), result);

                    // +Infinity -> MaxValue
                    Vector128<ulong> isPosInf = Vector128.Equals(bits, Vector128.Create(0x7FF0_0000_0000_0000ul)); // PositiveInfinityBits
                    result = Vector128.ConditionalSelect(isPosInf, Vector128.Create(BitConverter.DoubleToUInt64Bits(double.MaxValue)), result);

                    // NaN -> NaN (return original), -Infinity -> -Infinity (return original)
                    Vector128<ulong> isNaN = ~Vector128.Equals(xDouble, xDouble).AsUInt64();
                    Vector128<ulong> isNegInf = Vector128.Equals(bits, Vector128.Create(0xFFF0_0000_0000_0000ul)); // NegativeInfinityBits
                    result = Vector128.ConditionalSelect(isNaN | isNegInf, bits, result);

                    return 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();

                    // Select based on sign: negative -> increment, positive -> decrement
                    Vector256<uint> isNegative = Vector256.Equals(bits & Vector256.Create(0x8000_0000u), Vector256.Create(0x8000_0000u));
                    Vector256<uint> result = Vector256.ConditionalSelect(
                        isNegative,
                        bits + Vector256<uint>.One,
                        bits - Vector256<uint>.One);

                    // Handle special cases
                    // +0.0 -> -Epsilon
                    Vector256<uint> isPosZero = Vector256.Equals(bits, Vector256.Create(0x0000_0000u)); // PositiveZeroBits
                    result = Vector256.ConditionalSelect(isPosZero, Vector256.Create(BitConverter.SingleToUInt32Bits(-float.Epsilon)), result);

Review comment (performance):
So in general, this is quite expensive and doing a lot of potentially unnecessary work.

This could be simplified one of a few ways:
1. We could presume that most inputs are "normal" and therefore have a `if (!Vector128.AllWhereAllBitsSet(Vector128.IsNormal(x))) { }` check. This would have a branch but take away the bulk of the extra computation required to handle infinities, nans, subnormals, zero, etc
2. Similar to one, but we say most inptus are "finite" and therefore have a `if(!Vector128.AllWhereAllBitsSet(Vector128.IsFinite(x))) { }` check (this avoids special handling for infinities and nans on the common path).
3. We change the logic here to only do a two total conditional selects. This requires the general `isNegative` check for `bits++` vs `bits--` and separately tracking a `specialMask` and `specialSelect`

For 3, this ends up working because none of special checks "conflict" with eachother. That is, given a `Vector128.IsNaN` check and a `Vector128.IsPositiveInfinity` check, you will not have any elements equal to eachother and so you can simply `bitwise-or` the masks together:
```csharp
Vector128<ulong> result = Vector128.ConditionalSelect(
    Vector128.IsNegative(xDouble).AsUInt64(),
    bits + Vector128<ulong>.One,
    bits - Vector128<ulong>.One
);

Vector128<ulong> isPositiveZero = Vector128.IsZero(x.AsUInt64());
Vector128<ulong> specialValue = Vector128.Create(-double.Epsilon).AsUInt64() & isPositiveZero;

Vector128<ulong> isNaNOrNegInf = (Vector128.IsNaN(xDouble) | Vector128.IsNegativeInfinity(xDouble)).AsUInt64();
specialValue |= (bits & isNaNOrNegInf);

return Vector128.ConditionalSelect(
    isPositiveZero | isNaNOrNegInf, 
    specialValue,
    result
).As<ulong, T>();
```

This ends up helping reduce pipeline pressure and the overall number of checks done. It also helps make it clear what checks are being done, rather than having to intuit or rely on code comments describing what the bit patterns are doing.

Code after:
                    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(
                        Vector256.IsNegative(xFloat).AsUInt32(),
                        bits + Vector256<uint>.One,
                        bits - Vector256<uint>.One);

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

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

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

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

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

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