Skip to content

Commit 21ab780

Browse files
xtqqczzeEgorBo
andauthored
Remove unsafe bool casts (dotnet#111024)
Co-authored-by: Egor Bogatov <[email protected]>
1 parent 02fd751 commit 21ab780

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/libraries/System.Private.CoreLib/src/System/Buffers/Text/FormattingHelpers.CountDigits.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static int CountDigits(ulong value)
2525
Debug.Assert(log2ToPow10.Length == 64);
2626

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

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

5858
// Return the number of digits based on the power of 10, shifted by 1
5959
// if it falls below the threshold.
60-
bool lessThan = value < powerOf10;
61-
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
60+
int index = (int)elementOffset;
61+
return index - (value < powerOf10 ? 1 : 0);
6262
}
6363

6464
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/libraries/System.Private.CoreLib/src/System/Half.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ public static explicit operator Half(float value)
746746
// Extract sign bit
747747
uint sign = (bitValue & float.SignMask) >> 16;
748748
// Detecting NaN (~0u if a is not NaN)
749-
uint realMask = (uint)(Unsafe.BitCast<bool, sbyte>(float.IsNaN(value)) - 1);
749+
uint realMask = float.IsNaN(value) ? 0u : ~0u;
750750
// Clear sign bit
751751
value = float.Abs(value);
752752
// Rectify values that are Infinity in Half. (float.Min now emits vminps instruction if one of two arguments is a constant)
@@ -1075,17 +1075,15 @@ public static explicit operator float(Half value)
10751075
// Extract exponent bits of value (BiasedExponent is not for here as it performs unnecessary shift)
10761076
uint offsetExponent = bitValueInProcess & HalfExponentMask;
10771077
// ~0u when value is subnormal, 0 otherwise
1078-
uint subnormalMask = (uint)-Unsafe.BitCast<bool, byte>(offsetExponent == 0u);
1079-
// ~0u when value is either Infinity or NaN, 0 otherwise
1080-
int infinityOrNaNMask = Unsafe.BitCast<bool, byte>(offsetExponent == HalfExponentMask);
1078+
uint subnormalMask = offsetExponent == 0u ? ~0u : 0u;
10811079
// 0x3880_0000u if value is subnormal, 0 otherwise
10821080
uint maskedExponentLowerBound = subnormalMask & ExponentLowerBound;
10831081
// 0x3880_0000u if value is subnormal, 0x3800_0000u otherwise
10841082
uint offsetMaskedExponentLowerBound = ExponentOffset | maskedExponentLowerBound;
10851083
// Match the position of the boundary of exponent bits and fraction bits with IEEE 754 Binary32(Single)
10861084
bitValueInProcess <<= 13;
10871085
// Double the offsetMaskedExponentLowerBound if value is either Infinity or NaN
1088-
offsetMaskedExponentLowerBound <<= infinityOrNaNMask;
1086+
offsetMaskedExponentLowerBound <<= offsetExponent == HalfExponentMask ? 1 : 0;
10891087
// Extract exponent bits and fraction bits of value
10901088
bitValueInProcess &= HalfToSingleBitsMask;
10911089
// Adjust exponent to match the range of exponent

0 commit comments

Comments
 (0)