-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Remove unsafe code from CborHelpers, Crc32, and BlobUtilities #126269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
19e3d13
406a185
032535a
2ae30d3
8071377
bc5870b
86719d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.CompilerServices; | ||
|
|
@@ -53,7 +53,7 @@ public static float HalfToFloat(ushort value) | |
| { | ||
| if (sig == 0) | ||
| { | ||
| return CborHelpers.UInt32BitsToSingle(sign ? FloatSignMask : 0); // Positive / Negative zero | ||
| return BitConverter.UInt32BitsToSingle(sign ? FloatSignMask : 0); // Positive / Negative zero | ||
| } | ||
| (exp, sig) = NormSubnormalF16Sig(sig); | ||
| exp -= 1; | ||
|
|
@@ -62,7 +62,7 @@ public static float HalfToFloat(ushort value) | |
| return CreateSingle(sign, (byte)(exp + 0x70), sig << 13); | ||
|
|
||
| static float CreateSingle(bool sign, byte exp, uint sig) | ||
| => CborHelpers.Int32BitsToSingle((int)(((sign ? 1U : 0U) << FloatSignShift) + ((uint)exp << FloatExponentShift) + sig)); | ||
| => BitConverter.Int32BitsToSingle((int)(((sign ? 1U : 0U) << FloatSignShift) + ((uint)exp << FloatExponentShift) + sig)); | ||
| } | ||
|
Comment on lines
56
to
66
|
||
|
|
||
| public static bool HalfIsNaN(ushort value) | ||
|
|
@@ -119,7 +119,7 @@ private static float CreateSingleNaN(bool sign, ulong significand) | |
| uint signInt = (sign ? 1U : 0U) << FloatSignShift; | ||
| uint sigInt = (uint)(significand >> 41); | ||
|
|
||
| return CborHelpers.UInt32BitsToSingle(signInt | NaNBits | sigInt); | ||
| return BitConverter.UInt32BitsToSingle(signInt | NaNBits | sigInt); | ||
| } | ||
| #endregion | ||
|
|
||
|
|
@@ -128,7 +128,7 @@ public static ushort FloatToHalf(float value) | |
| { | ||
| const int SingleMaxExponent = 0xFF; | ||
|
|
||
| uint floatInt = CborHelpers.SingleToUInt32Bits(value); | ||
| uint floatInt = BitConverter.SingleToUInt32Bits(value); | ||
| bool sign = (floatInt & FloatSignMask) >> FloatSignShift != 0; | ||
| int exp = (int)(floatInt & FloatExponentMask) >> FloatExponentShift; | ||
| uint sig = floatInt & FloatSignificandMask; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers.Binary; | ||
|
|
@@ -27,7 +27,7 @@ private void WriteHalf(ushort value) | |
| } | ||
| else | ||
| { | ||
| CborHelpers.WriteHalfBigEndian(_buffer.AsSpan(_offset), value); | ||
| BinaryPrimitives.WriteUInt16BigEndian(_buffer.AsSpan(_offset), value); | ||
| } | ||
| _offset += sizeof(ushort); | ||
| AdvanceDataItemCounters(); | ||
|
|
@@ -37,7 +37,7 @@ private void WriteHalf(ushort value) | |
| internal static bool TryConvertSingleToHalf(float value, out ushort result) | ||
| { | ||
| result = HalfHelpers.FloatToHalf(value); | ||
| return float.IsNaN(value) || CborHelpers.SingleToInt32Bits(HalfHelpers.HalfToFloat(result)) == CborHelpers.SingleToInt32Bits(value); | ||
| return float.IsNaN(value) || BitConverter.SingleToInt32Bits(HalfHelpers.HalfToFloat(result)) == BitConverter.SingleToInt32Bits(value); | ||
| } | ||
|
Comment on lines
39
to
41
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not mean to add a bunch of ifdefs to the common file.
I meant to:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I did have that in mind, in my workflow I just ask AI to address the feedback and then change it. It sometimes proactively pushes it before I change it, but I assumed it's fine since the PR is in draft 🙂 Still, I appreciate the early feedback!