-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add managed implementation of Math/MathF.Abs #63881
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,9 +49,9 @@ public static partial class Math | |
|
||
private const double SCALEB_C3 = 9007199254740992; // 0x1p53 | ||
|
||
private const int ILogB_NaN = 0x7fffffff; | ||
private const int ILogB_NaN = 0x7FFFFFFF; | ||
|
||
private const int ILogB_Zero = (-1 - 0x7fffffff); | ||
private const int ILogB_Zero = (-1 - 0x7FFFFFFF); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static short Abs(short value) | ||
|
@@ -133,6 +133,26 @@ public static decimal Abs(decimal value) | |
return decimal.Abs(value); | ||
} | ||
|
||
[Intrinsic] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static double Abs(double value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes overall look correct to me. However, I'd like to better understand the need for this change as I wouldn't expect this to provide any diffs for existing platforms or even make new platform bring up easier. On RyuJIT I would expect that Mono JIT, Mono LLVM, and NativeAOT are doing relatively the same, but if not I would expect a different PR rather than one updating the managed implementation here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this PR in the same spirit as ILogB change which went in earlier for .NET 7. My understanding was that we generally want to reduce number of FCalls, especially when it has zero cost.
Do you mean it will rather make new platform bring up harder? Hard for me to imagine how, since this managed implementation is using a few BitConverter methods, which are also used by existing methods in Math.cs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mono JIT (mini), Interp and LLVM all handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, I was just indicating that, as it appears to me, this is effectively a zero-diff PR (at least for expected real world use-cases). That is, this PR doesn't make the code easier to read or maintain, it doesn't make new platform bring up easier, etc. The one place it will impact is |
||
{ | ||
const ulong mask = 0x7FFFFFFFFFFFFFFF; | ||
ulong raw = BitConverter.DoubleToUInt64Bits(value); | ||
|
||
return BitConverter.UInt64BitsToDouble(raw & mask); | ||
} | ||
|
||
[Intrinsic] | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static float Abs(float value) | ||
{ | ||
const uint mask = 0x7FFFFFFF; | ||
uint raw = BitConverter.SingleToUInt32Bits(value); | ||
|
||
return BitConverter.UInt32BitsToSingle(raw & mask); | ||
} | ||
|
||
[DoesNotReturn] | ||
[StackTraceHidden] | ||
private static void ThrowAbsOverflow() | ||
|
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.
Nit: I have noticed that there is unused
#define _copysignf (float)_copysign
a few lines above. It is left-over from times when CopySign was FCall. You delete it while you are on it.