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

Improve performance for Math.Abs #15823

Merged
merged 4 commits into from Jan 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 42 additions & 52 deletions src/mscorlib/shared/System/Math.cs
Expand Up @@ -38,23 +38,55 @@ public static partial class Math

public static short Abs(short value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = (short)-value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

public static int Abs(int value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = -value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

public static long Abs(long value)
{
return (value >= 0) ? value : AbsHelper(value);
if (value < 0)
{
value = -value;
if (value < 0)
{
ThrowAbsOverflow();
}
}
return value;
}

[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 +95,12 @@ public static decimal Abs(decimal value)
return decimal.Abs(value);
}

[StackTraceHidden]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do, prevent inlining?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stops it showing up in exception stack trace #14652 so it looks like the throw was in-place rather than off a throw helper function

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 +796,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