From 9eb8e57dd474dd387951abb2959034f0a0812393 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Tue, 11 Dec 2007 23:16:59 +0000 Subject: [PATCH] support floating point math --- Robust/src/ClassLibrary/Math.java | 72 +++++++++++-------------------- Robust/src/Main/Main.java | 1 + Robust/src/Runtime/math.c | 43 ++++++++++++++++++ Robust/src/Runtime/runtime.h | 2 + Robust/src/buildscript | 1 + 5 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 Robust/src/Runtime/math.c diff --git a/Robust/src/ClassLibrary/Math.java b/Robust/src/ClassLibrary/Math.java index 789ccb4f..e55c0cf5 100644 --- a/Robust/src/ClassLibrary/Math.java +++ b/Robust/src/ClassLibrary/Math.java @@ -1,52 +1,28 @@ public class Math { - //public static final double PI=3.14159265358979323846; - - public double fabs(double x) { - if (x < 0) { - return -x; - } else { - return x; - } - } - - public double sin(double rad) { - double app; - double diff; - int inc = 1; - double PI=3.14159265358979323846; - - while( rad > 2*PI ) rad -= 2*PI; - while( rad < -2*PI ) rad += 2*PI; - app = diff = rad; - diff = (diff * (-(rad*rad))) / ((2.0 * inc) * (2.0 * inc + 1.0)); - app = app + diff; - inc++; - while( fabs(diff) >= 0.00001 ) { - diff = (diff * (-(rad*rad))) / ((2.0 * inc) * (2.0 * inc + 1.0)); - app = app + diff; - inc++; - } - return app; + //public static final double PI=3.14159265358979323846; + + public static double fabs(double x) { + if (x < 0) { + return -x; + } else { + return x; } + } + + public static float abs(float a) { + if (a<0) + return -a; + else return a; + } - public double cos(double rad) { - double app; - double diff; - int inc = 1; - double PI=3.14159265358979323846; - - rad += PI/2.0; - while( rad > 2*PI ) rad -= 2*PI; - while( rad < -2*PI ) rad += 2*PI; - app = diff = rad; - diff = (diff * (-(rad*rad))) / ((2.0 * inc) * (2.0 * inc + 1.0)); - app = app + diff; - inc++; - while( fabs(diff) >= 0.00001 ) { - diff = (diff * (-(rad*rad))) / ((2.0 * inc) * (2.0 * inc + 1.0)); - app = app + diff; - inc++; - } - return app; - } + public static native double sin(double a); + public static native double cos(double a); + public static native double asin(double a); + public static native double acos(double a); + public static native double tan(double a); + public static native double atan(double a); + public static native double exp(double a); + public static native double sqrt(double a); + public static native double log(double a); + public static native double pow(double a, double b); } diff --git a/Robust/src/Main/Main.java b/Robust/src/Main/Main.java index 706dca58..784a95c4 100644 --- a/Robust/src/Main/Main.java +++ b/Robust/src/Main/Main.java @@ -119,6 +119,7 @@ public class Main { readSourceFile(state, ClassLibraryPrefix+"OutputStream.java"); readSourceFile(state, ClassLibraryPrefix+"FileOutputStream.java"); readSourceFile(state, ClassLibraryPrefix+"File.java"); + readSourceFile(state, ClassLibraryPrefix+"Math.java"); readSourceFile(state, ClassLibraryPrefix+"InetAddress.java"); readSourceFile(state, ClassLibraryPrefix+"SocketInputStream.java"); readSourceFile(state, ClassLibraryPrefix+"SocketOutputStream.java"); diff --git a/Robust/src/Runtime/math.c b/Robust/src/Runtime/math.c new file mode 100644 index 00000000..8baaf3fb --- /dev/null +++ b/Robust/src/Runtime/math.c @@ -0,0 +1,43 @@ +#include "runtime.h" +#include "math.h" +#include "structdefs.h" + +double CALL11(___Math______cos____D, double ___a___, double ___a___) { + return cos(___a___); +} + +double CALL11(___Math______sin____D, double ___a___, double ___a___) { + return sin(___a___); +} + +double CALL11(___Math______tan____D, double ___a___, double ___a___) { + return tan(___a___); +} + +double CALL11(___Math______acos____D, double ___a___, double ___a___) { + return acos(___a___); +} + +double CALL11(___Math______asin____D, double ___a___, double ___a___) { + return asin(___a___); +} + +double CALL11(___Math______atan____D, double ___a___, double ___a___) { + return atan(___a___); +} + +double CALL11(___Math______log____D, double ___a___, double ___a___) { + return log(___a___); +} + +double CALL11(___Math______exp____D, double ___a___, double ___a___) { + return exp(___a___); +} + +double CALL11(___Math______sqrt____D, double ___a___, double ___a___) { + return sqrt(___a___); +} + +double CALL22(___Math______pow____D_D, double ___a___, double ___b___, double ___a___, double ___b___) { + return pow(___a___,___b___); +} diff --git a/Robust/src/Runtime/runtime.h b/Robust/src/Runtime/runtime.h index fdef2c2f..7158c14f 100644 --- a/Robust/src/Runtime/runtime.h +++ b/Robust/src/Runtime/runtime.h @@ -57,6 +57,7 @@ void createstartupobject(); #define CALL02(name, alt1, alt2) name(struct name ## _params * ___params___) #define CALL11(name,rest, alt) name(struct name ## _params * ___params___, rest) #define CALL12(name,rest, alt1, alt2) name(struct name ## _params * ___params___, rest) +#define CALL22(name, rest, rest2, alt1, alt2) name(struct name ## _params * ___params___, rest, rest2) #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(struct name ## _params * ___params___, rest, rest2) #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2) #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(struct name ## _params * ___params___, rest, rest2, rest3) @@ -67,6 +68,7 @@ void createstartupobject(); #define CALL02(name, alt1, alt2) name(alt1, alt2) #define CALL11(name,rest, alt) name(alt) #define CALL12(name,rest, alt1, alt2) name(alt1, alt2) +#define CALL22(name, rest, rest2, alt1, alt2) name(alt1, alt2) #define CALL23(name, rest, rest2, alt1, alt2, alt3) name(alt1, alt2, alt3) #define CALL24(name, rest, rest2, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4) #define CALL34(name, rest, rest2, rest3, alt1, alt2, alt3, alt4) name(alt1, alt2, alt3, alt4) diff --git a/Robust/src/buildscript b/Robust/src/buildscript index fde52d79..c2422ae2 100755 --- a/Robust/src/buildscript +++ b/Robust/src/buildscript @@ -209,6 +209,7 @@ $ROBUSTROOT/Runtime/file.c $ROBUSTROOT/Runtime/Queue.c \ $ROBUSTROOT/Runtime/SimpleHash.c $ROBUSTROOT/Runtime/option.c \ $ROBUSTROOT/Runtime/ObjectHash.c \ $ROBUSTROOT/Runtime/garbage.c $ROBUSTROOT/Runtime/socket.c \ +$ROBUSTROOT/Runtime/math.c \ $ROBUSTROOT/Runtime/GenericHashtable.c $ROBUSTROOT/Runtime/object.c" if $DSMFLAG -- 2.34.1