Skip to content

Use Unsafe.BitCast for Int128UInt128 operators #104506

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

Merged
merged 1 commit into from
Jul 9, 2024
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
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public static explicit operator checked ulong(Int128 value)
/// <param name="value">The value to convert.</param>
/// <returns><paramref name="value" /> converted to a <see cref="UInt128" />.</returns>
[CLSCompliant(false)]
public static explicit operator UInt128(Int128 value) => new UInt128(value._upper, value._lower);
public static explicit operator UInt128(Int128 value) => Unsafe.BitCast<Int128, UInt128>(value);

/// <summary>Explicitly converts a 128-bit signed integer to a <see cref="UInt128" /> value, throwing an overflow exception for any values that fall outside the representable range.</summary>
/// <param name="value">The value to convert.</param>
Expand All @@ -438,7 +438,7 @@ public static explicit operator checked UInt128(Int128 value)
{
ThrowHelper.ThrowOverflowException();
}
return new UInt128(value._upper, value._lower);
return Unsafe.BitCast<Int128, UInt128>(value);
}

/// <summary>Explicitly converts a 128-bit signed integer to a <see cref="UIntPtr" /> value.</summary>
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/UInt128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static explicit operator checked long(UInt128 value)
/// <param name="value">The value to convert.</param>
/// <returns><paramref name="value" /> converted to a <see cref="Int128" />.</returns>
[CLSCompliant(false)]
public static explicit operator Int128(UInt128 value) => new Int128(value._upper, value._lower);
public static explicit operator Int128(UInt128 value) => Unsafe.BitCast<UInt128, Int128>(value);

/// <summary>Explicitly converts a 128-bit unsigned integer to a <see cref="Int128" /> value, throwing an overflow exception for any values that fall outside the representable range.</summary>
/// <param name="value">The value to convert.</param>
Expand All @@ -357,7 +357,7 @@ public static explicit operator checked Int128(UInt128 value)
{
ThrowHelper.ThrowOverflowException();
}
return new Int128(value._upper, value._lower);
return Unsafe.BitCast<UInt128, Int128>(value);
}

/// <summary>Explicitly converts a 128-bit unsigned integer to a <see cref="IntPtr" /> value.</summary>
Expand Down
Loading