Skip to content

Commit

Permalink
Merge branch 'master' into removewarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaTy committed Mar 5, 2022
2 parents 3573104 + 6439b5b commit 70ae0c0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 195 deletions.
12 changes: 12 additions & 0 deletions Tests/Cosmos.TestRunner.TestController/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public static void AreEqual(long expected, long actual, string message, [CallerF
IsTrue(xResult, message, file, line);
}

public static void AreEqual(double expected, double actual, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
var xResult = Math.Abs(expected - actual) < 0.0001;
if (!xResult)
{
TestController.Debugger.Send($"Expected value: '{expected}' " + BitConverter.ToString(BitConverter.GetBytes(expected)));
TestController.Debugger.Send($"Actual value: '{actual}' " + BitConverter.ToString(BitConverter.GetBytes(expected)));
TestController.Debugger.Send($"Diff: {xResult}");
}
IsTrue(xResult, message, file, line);
}

public static void AreEqual(string[] expected, string[] actual, string message, [CallerFilePath] string file = null, [CallerLineNumber] int line = 0)
{
if(expected.Length != actual.Length)
Expand Down
16 changes: 5 additions & 11 deletions Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/MathTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;

using Cosmos.Debug.Kernel;
using Cosmos.TestRunner;

namespace Cosmos.Compiler.Tests.Bcl.System
Expand Down Expand Up @@ -159,9 +159,9 @@ public static void Execute()

result = Math.Cos(Math.PI);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -1), "Cos gives correct answer for PI");

result = Math.Cos(Math.PI / 2);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 6.12323399573677E-17), "Cos gives correct answer for PI / 2");
Assert.AreEqual(0.0, result, "Cos gives correct answer for PI / 2");

result = Math.Cos(Math.PI / 3);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.5), "Cos gives correct answer for PI / 3");
Expand Down Expand Up @@ -235,10 +235,10 @@ public static void Execute()
Assert.IsTrue(double.IsNaN(result), "Sin works with -INF");

result = Math.Sin(Math.PI);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.22464679914735E-16), "Sin gives correct answer for PI");
Assert.AreEqual(0.0, result, "Sin gives correct answer for PI");

result = Math.Sin(Math.PI / 2);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1), "Sin gives correct answer for PI / 2");
Assert.AreEqual(1.0, result, "Sin gives correct answer for PI / 2");

result = Math.Sin(Math.PI / 3);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 0.866025403784439), "Sin gives correct answer for PI / 3");
Expand Down Expand Up @@ -416,12 +416,6 @@ public static void Execute()
result = Math.Tan(-0.5);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, -0.54630248984379), "Tan works with negative doubles");

result = Math.Tan(Math.PI);
Assert.IsTrue(result <= -.22464679914735E-16, "Tan gives matching result for Pi but mathematically inaccurate result. " + result);

result = Math.Tan(Math.PI / 2);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, 1.63312393531954E+16), "Tan gives result matching normal Math function but incorrect in mathematical sense");

result = Math.Tan(Math.PI / 3);
Assert.IsTrue(EqualityHelper.DoublesAreEqual(result, Math.Sqrt(3)), "Tan gives correct value for PI / 3");

Expand Down
34 changes: 34 additions & 0 deletions source/Cosmos.Core_Plugs/MathImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ public static double Round(double d)
{
throw new NotImplementedException();
}

[PlugMethod(Assembler = typeof(MathCosASM))]
public static double Cos(double d)
{
throw new NotImplementedException();
}

[PlugMethod(Assembler = typeof(MathSinASM))]
public static double Sin(double d)
{
throw new NotImplementedException();
}
}

class MathRoundASM : AssemblerMethod
Expand All @@ -29,4 +41,26 @@ public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
XS.FPU.FloatStoreAndPop(ESP, isIndirect: true, size: RegisterSize.Long64);
}
}

class MathCosASM : AssemblerMethod
{
public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
{
XS.FPU.FloatLoad(EBP, destinationIsIndirect: true, displacement: 8, size: RegisterSize.Long64);
XS.FPU.FloatCosine();
XS.Sub(ESP, 8);
XS.FPU.FloatStoreAndPop(ESP, isIndirect: true, size: RegisterSize.Long64);
}
}

class MathSinASM : AssemblerMethod
{
public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
{
XS.FPU.FloatLoad(EBP, destinationIsIndirect: true, displacement: 8, size: RegisterSize.Long64);
XS.FPU.FloatSine();
XS.Sub(ESP, 8);
XS.FPU.FloatStoreAndPop(ESP, isIndirect: true, size: RegisterSize.Long64);
}
}
}
186 changes: 2 additions & 184 deletions source/Cosmos.System2_Plugs/System/MathImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,105 +510,6 @@ public static double Ceiling(double x)

#endregion Ceiling

#region Cos

public static double Cos(double x)
{
double[] y = new double[2];
double z = 0.0;
int n, ix;

/* High word of x. */
ix = HighWord(x);

/* |x| ~< pi/4 */
ix &= 0x7fffffff;
if (ix <= 0x3fe921fb)
{
return _cos(x, z);
}

/* cos(Inf or NaN) is NaN */
else if (ix >= 0x7ff00000)
{
return x - x;
}

/* argument reduction needed */
else
{
n = __ieee754_rem_pio2(x, y);
switch (n & 3)
{
case 0:
return _cos(y[0], y[1]);

case 1:
return -_sin(y[0], y[1], 1);

case 2:
return -_cos(y[0], y[1]);

default:
return _sin(y[0], y[1], 1);
}
}
}

private static double _cos(double x, double y)
{
const double one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
double a, hz, z, r, qx = 0;
int ix;
ix = HighWord(x) & 0x7fffffff; /* ix = |x|'s high word*/
if (ix < 0x3e400000)
{ /* if x < 2**27 */
if (((int)x) == 0)
{
return one; /* generate inexact */
}
}
z = x * x;
r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
if (ix < 0x3FD33333) /* if |x| < 0.3 */
{
return one - (0.5 * z - (z * r - x * y));
}
else
{
if (ix > 0x3fe90000)
{ /* x > 0.78125 */
qx = 0.28125;
}
else
{
//__HI(qx) = ix - 0x00200000; /* x/4 */
var bqx = BitConverter.GetBytes(BitConverter.DoubleToInt64Bits(qx));
var bvh = BitConverter.GetBytes(ix - 0x00200000);
for (int i = 0; i < 4; i++)
{
bqx[i + (BitConverter.IsLittleEndian ? 4 : 0)] = bvh[i];
}
//__LO(qx) = 0;
for (int i = 0; i < 4; i++)
{
bqx[i + (BitConverter.IsLittleEndian ? 4 : 0)] = 0;
}
}
hz = 0.5 * z - qx;
a = one - qx;
return a - (hz - (z * r - x * y));
}
}

#endregion Cos

#region Cosh

public static double Cosh(double x)
Expand Down Expand Up @@ -1130,86 +1031,6 @@ public static double Pow(double b, double e)

#endregion Pow

#region Sin

public static double Sin(double x)
{
double[] y = new double[2];
double z = 0.0;
int n, ix;

/* High word of x. */
ix = HighWord(x);

/* |x| ~< pi/4 */
ix &= 0x7fffffff;
if (ix <= 0x3fe921fb)
{
return _sin(x, z, 0);
}

/* sin(Inf or NaN) is NaN */
else if (ix >= 0x7ff00000)
{
return x - x;
}

/* argument reduction needed */
else
{
n = __ieee754_rem_pio2(x, y);
switch (n & 3)
{
case 0:
return _sin(y[0], y[1], 1);

case 1:
return _cos(y[0], y[1]);

case 2:
return -_sin(y[0], y[1], 1);

default:
return -_cos(y[0], y[1]);
}
}
}

private static double _sin(double x, double y, int iy)
{
const double half = 5.00000000000000000000e-01; /* 0x3FE00000; 0x00000000 */
const double S1 = -1.66666666666666324348e-01; /* 0xBFC55555; 0x55555549 */
const double S2 = 8.33333333332248946124e-03; /* 0x3F811111; 0x1110F8A6 */
const double S3 = -1.98412698298579493134e-04; /* 0xBF2A01A0; 0x19C161D5 */
const double S4 = 2.75573137070700676789e-06; /* 0x3EC71DE3; 0x57B1FE7D */
const double S5 = -2.50507602534068634195e-08; /* 0xBE5AE5E6; 0x8A2B9CEB */
const double S6 = 1.58969099521155010221e-10; /* 0x3DE5D93A; 0x5ACFD57C */

double z, r, v;
int ix;
ix = HighWord(x) & 0x7fffffff; /* high word of x */
if (ix < 0x3e400000) /* |x| < 2**-27 */
{
if ((int)x == 0)
{
return x;
}
} /* generate inexact */
z = x * x;
v = z * x;
r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
if (iy == 0)
{
return x + v * (S1 + z * r);
}
else
{
return x - ((z * (half * y - v * r) - y) - v * S1);
}
}

#endregion Sin

#region Sinh

public static double Sinh(double x)
Expand Down Expand Up @@ -1402,11 +1223,8 @@ public static double Sqrt(double x)

public static double Tan(double x)
{
if (Double.IsNegativeInfinity(x) || Double.IsInfinity(x))
{
return Double.NaN;
}
return Sin(x) / Cos(x);
if (double.IsNegativeInfinity(x) || double.IsInfinity(x)) return double.NaN;
return Math.Sin(x) / Math.Cos(x);
}

#endregion Tan
Expand Down

0 comments on commit 70ae0c0

Please sign in to comment.