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

Commit

Permalink
Improve performance for Math.Abs (dotnet/coreclr#15823)
Browse files Browse the repository at this point in the history
* Improve perf for Math.Abs

* Inline Math.Abs

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
  • Loading branch information
benaadams authored and dotnet-bot committed Jan 13, 2018
1 parent fcde3e4 commit f587c19
Showing 1 changed file with 46 additions and 52 deletions.
98 changes: 46 additions & 52 deletions src/Common/src/CoreLib/System/Math.cs
Expand Up @@ -36,25 +36,61 @@ public static partial class Math
1E9, 1E10, 1E11, 1E12, 1E13, 1E14, 1E15
};

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short Abs(short value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = (short)-value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Abs(int value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = -value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Abs(long value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = -value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
[CLSCompliant(false)]
public static sbyte Abs(sbyte value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = (sbyte)-value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

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

[StackTraceHidden]
private static void ThrowAbsOverflow()
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

public static long BigMul(int a, int b)
{
return ((long)a) * b;
Expand Down Expand Up @@ -758,54 +800,6 @@ public static unsafe double Truncate(double d)
return d;
}

private static short AbsHelper(short value)
{
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");

if (value == short.MinValue)
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

return ((short)(-value));
}

private static int AbsHelper(int value)
{
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");

if (value == int.MinValue)
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

return -value;
}

private static long AbsHelper(long value)
{
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");

if (value == long.MinValue)
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

return -value;
}

private static sbyte AbsHelper(sbyte value)
{
Debug.Assert(value < 0, "AbsHelper should only be called for negative values! (workaround for JIT inlining)");

if (value == sbyte.MinValue)
{
throw new OverflowException(SR.Overflow_NegateTwosCompNum);
}

return ((sbyte)(-value));
}

private static unsafe double copysign(double x, double y)
{
var xbits = BitConverter.DoubleToInt64Bits(x);
Expand Down

0 comments on commit f587c19

Please sign in to comment.