Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static int CountDigits(ulong value)
Debug.Assert(log2ToPow10.Length == 64);

// TODO: Replace with log2ToPow10[BitOperations.Log2(value)] once https://github.com/dotnet/runtime/issues/79257 is fixed
uint index = Unsafe.Add(ref MemoryMarshal.GetReference(log2ToPow10), BitOperations.Log2(value));
nint elementOffset = Unsafe.Add(ref MemoryMarshal.GetReference(log2ToPow10), BitOperations.Log2(value));

// Read the associated power of 10.
ReadOnlySpan<ulong> powersOf10 =
Expand All @@ -52,13 +52,13 @@ public static int CountDigits(ulong value)
1000000000000000000,
10000000000000000000,
];
Debug.Assert((index + 1) <= powersOf10.Length);
ulong powerOf10 = Unsafe.Add(ref MemoryMarshal.GetReference(powersOf10), index);
Debug.Assert((elementOffset + 1) <= powersOf10.Length);
ulong powerOf10 = Unsafe.Add(ref MemoryMarshal.GetReference(powersOf10), elementOffset);

// Return the number of digits based on the power of 10, shifted by 1
// if it falls below the threshold.
bool lessThan = value < powerOf10;
return (int)(index - Unsafe.As<bool, byte>(ref lessThan)); // while arbitrary bools may be non-0/1, comparison operators are expected to return 0/1
int index = (int)elementOffset;
return index - (value < powerOf10 ? 1 : 0);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
8 changes: 3 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/Half.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ public static explicit operator Half(float value)
// Extract sign bit
uint sign = (bitValue & float.SignMask) >> 16;
// Detecting NaN (~0u if a is not NaN)
uint realMask = (uint)(Unsafe.BitCast<bool, sbyte>(float.IsNaN(value)) - 1);
uint realMask = float.IsNaN(value) ? 0u : ~0u;
// Clear sign bit
value = float.Abs(value);
// Rectify values that are Infinity in Half. (float.Min now emits vminps instruction if one of two arguments is a constant)
Expand Down Expand Up @@ -1075,17 +1075,15 @@ public static explicit operator float(Half value)
// Extract exponent bits of value (BiasedExponent is not for here as it performs unnecessary shift)
uint offsetExponent = bitValueInProcess & HalfExponentMask;
// ~0u when value is subnormal, 0 otherwise
uint subnormalMask = (uint)-Unsafe.BitCast<bool, byte>(offsetExponent == 0u);
// ~0u when value is either Infinity or NaN, 0 otherwise
int infinityOrNaNMask = Unsafe.BitCast<bool, byte>(offsetExponent == HalfExponentMask);
uint subnormalMask = offsetExponent == 0u ? ~0u : 0u;
// 0x3880_0000u if value is subnormal, 0 otherwise
uint maskedExponentLowerBound = subnormalMask & ExponentLowerBound;
// 0x3880_0000u if value is subnormal, 0x3800_0000u otherwise
uint offsetMaskedExponentLowerBound = ExponentOffset | maskedExponentLowerBound;
// Match the position of the boundary of exponent bits and fraction bits with IEEE 754 Binary32(Single)
bitValueInProcess <<= 13;
// Double the offsetMaskedExponentLowerBound if value is either Infinity or NaN
offsetMaskedExponentLowerBound <<= infinityOrNaNMask;
offsetMaskedExponentLowerBound <<= offsetExponent == HalfExponentMask ? 1 : 0;
// Extract exponent bits and fraction bits of value
bitValueInProcess &= HalfToSingleBitsMask;
// Adjust exponent to match the range of exponent
Expand Down
Loading