Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 288bd46

Browse files
benaadamsjkotas
authored andcommitted
Improve performance for Math.Abs (#15823)
* Improve perf for Math.Abs * Inline Math.Abs
1 parent 8266cbc commit 288bd46

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

src/mscorlib/shared/System/Math.cs

Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,61 @@ public static partial class Math
3636
1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15
3737
};
3838

39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3940
public static short Abs(short value)
4041
{
41-
return (value >= 0) ? value : AbsHelper(value);
42+
if (value < 0)
43+
{
44+
value = (short)-value;
45+
if (value < 0)
46+
{
47+
ThrowAbsOverflow();
48+
}
49+
}
50+
return value;
4251
}
4352

53+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4454
public static int Abs(int value)
4555
{
46-
return (value >= 0) ? value : AbsHelper(value);
56+
if (value < 0)
57+
{
58+
value = -value;
59+
if (value < 0)
60+
{
61+
ThrowAbsOverflow();
62+
}
63+
}
64+
return value;
4765
}
4866

67+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4968
public static long Abs(long value)
5069
{
51-
return (value >= 0) ? value : AbsHelper(value);
70+
if (value < 0)
71+
{
72+
value = -value;
73+
if (value < 0)
74+
{
75+
ThrowAbsOverflow();
76+
}
77+
}
78+
return value;
5279
}
5380

81+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5482
[CLSCompliant(false)]
5583
public static sbyte Abs(sbyte value)
5684
{
57-
return (value >= 0) ? value : AbsHelper(value);
85+
if (value < 0)
86+
{
87+
value = (sbyte)-value;
88+
if (value < 0)
89+
{
90+
ThrowAbsOverflow();
91+
}
92+
}
93+
return value;
5894
}
5995

6096
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -63,6 +99,12 @@ public static decimal Abs(decimal value)
6399
return decimal.Abs(value);
64100
}
65101

102+
[StackTraceHidden]
103+
private static void ThrowAbsOverflow()
104+
{
105+
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
106+
}
107+
66108
public static long BigMul(int a, int b)
67109
{
68110
return ((long)a) * b;
@@ -758,54 +800,6 @@ public static unsafe double Truncate(double d)
758800
return d;
759801
}
760802

761-
private static short AbsHelper(short value)
762-
{
763-
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");
764-
765-
if (value == short.MinValue)
766-
{
767-
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
768-
}
769-
770-
return ((short)(-value));
771-
}
772-
773-
private static int AbsHelper(int value)
774-
{
775-
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");
776-
777-
if (value == int.MinValue)
778-
{
779-
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
780-
}
781-
782-
return -value;
783-
}
784-
785-
private static long AbsHelper(long value)
786-
{
787-
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");
788-
789-
if (value == long.MinValue)
790-
{
791-
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
792-
}
793-
794-
return -value;
795-
}
796-
797-
private static sbyte AbsHelper(sbyte value)
798-
{
799-
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");
800-
801-
if (value == sbyte.MinValue)
802-
{
803-
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
804-
}
805-
806-
return ((sbyte)(-value));
807-
}
808-
809803
private static unsafe double copysign(double x, double y)
810804
{
811805
var xbits = BitConverter.DoubleToInt64Bits(x);

0 commit comments

Comments
 (0)