Skip to content

Commit

Permalink
Improve performance for overflow checking
Browse files Browse the repository at this point in the history
  • Loading branch information
leppie committed Sep 17, 2019
1 parent 57a4ab7 commit e764375
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions IronScheme/IronScheme/Runtime/Numbers.cs
Expand Up @@ -186,44 +186,48 @@ static bool IsExact(object obj)
public static object FxPlusInternal(int a, int b)
{
long r = (long)a + b;
if (r > int.MaxValue || r < int.MinValue)
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject((int)r);
return RuntimeHelpers.Int32ToObject(rr);
}

[Builtin("fx-internal", AllowConstantFold = true)]
public static object FxMinusInternal(int a, int b)
{
long r = (long)a - b;
if (r > int.MaxValue || r < int.MinValue)
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject((int)r);
return RuntimeHelpers.Int32ToObject(rr);
}

[Builtin("fx*internal", AllowConstantFold = true)]
public static object FxMultiplyInternal(int a, int b)
{
long r = (long)a * b;
if (r > int.MaxValue || r < int.MinValue)
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject((int)r);
return RuntimeHelpers.Int32ToObject(rr);
}

[Builtin("fxarithmetic-shift-left-internal", AllowConstantFold = true)]
public static object FxShiftLeftInternal(int a, int b)
{
long r = (long)a << b;
if (r > int.MaxValue || r < int.MinValue)
int rr = (int)r;
if (r != rr)
{
return FALSE;
}
return RuntimeHelpers.Int32ToObject((int)r);
return RuntimeHelpers.Int32ToObject(rr);
}

enum NumberClass
Expand Down Expand Up @@ -370,13 +374,14 @@ public static object Add(object first, object second)
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) + ConvertToInteger(second);
if (result > int.MaxValue || result < int.MinValue)
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject((int)result);
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
Expand Down Expand Up @@ -410,13 +415,14 @@ public static object Subtract(object first, object second)
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) - ConvertToInteger(second);
if (result > int.MaxValue || result < int.MinValue)
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject((int)result);
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
Expand Down Expand Up @@ -450,13 +456,14 @@ public static object Multiply(object first, object second)
case NumberClass.Integer:
{
long result = (long)ConvertToInteger(first) * ConvertToInteger(second);
if (result > int.MaxValue || result < int.MinValue)
int iresult = (int)result;
if (result != iresult)
{
return (BigInteger)result;
}
else
{
return RuntimeHelpers.Int32ToObject((int)result);
return RuntimeHelpers.Int32ToObject(iresult);
}
}
case NumberClass.BigInteger:
Expand Down Expand Up @@ -526,9 +533,7 @@ public static object Divide(object first, object second)
}

#endregion




static double SafeConvert(object obj)
{
try
Expand Down

0 comments on commit e764375

Please sign in to comment.