]> git.unchartedbackwaters.co.uk Git - francis/libjit.git/commitdiff
Test cases and bug fixes for mathematical operations.
authorRhys Weatherley <rweather@southern-storm.com.au>
Wed, 12 May 2004 06:58:45 +0000 (06:58 +0000)
committerRhys Weatherley <rweather@southern-storm.com.au>
Wed, 12 May 2004 06:58:45 +0000 (06:58 +0000)
ChangeLog
dpas/dpas-parser.y
jit/jit-interp.cpp
tests/Makefile.am
tests/math.pas [new file with mode: 0644]

index db7f6c2dcfb6e4530453268c467e7413c5a1620f..9ab07a1604095c5bf98001b8af7a28cae1bb0ebb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -32,6 +32,9 @@
        * dpas/dpas-builtin.c, jit/jit-dump.c, jit/jit-value.c: add builtins
        for mathematical operations.
 
+       * dpas/dpas-parser.y, jit/jit-interp.cpp, tests/Makefile.am,
+       tests/math.pas: test cases and bug fixes for mathematical operations.
+
 2004-05-11  Rhys Weatherley  <rweather@southern-storm.com.au>
 
        * include/jit/jit-insn.h, jit/jit-insn.c, jit/jit-interp.cpp,
index 91f68f12c851cff3f44ea29fff19df615e621060..959d9bea2384bfcc46d4ee6a0d4bb0ed534ecfcd 100644 (file)
@@ -2548,6 +2548,21 @@ Factor
                                                (0, 0, dpas_sem_get_type($1), dpas_sem_get_value($1),
                                                 $3.exprs, $3.len);
                                }
+                               else if(dpas_sem_is_type($1) && $3.len == 1 &&
+                                               dpas_sem_is_rvalue($3.exprs[0]))
+                               {
+                                       /* Cast a value to a new type */
+                                       jit_value_t conv;
+                                       conv = jit_insn_convert
+                                               (dpas_current_function(),
+                                                dpas_sem_get_value(dpas_lvalue_to_rvalue($3.exprs[0])),
+                                                dpas_sem_get_type($1), 0);
+                                       if(!conv)
+                                       {
+                                               dpas_out_of_memory();
+                                       }
+                                       dpas_sem_set_rvalue($$, dpas_sem_get_type($1), conv);
+                               }
                                else
                                {
                                        if(!dpas_sem_is_error($1))
index 7f18793bb7cac1674fb09a782e9d0b56839a5c52..ed2b60cacf807660b49750d31123c1b5df26f0c5 100644 (file)
@@ -4010,7 +4010,7 @@ void _jit_run_function(jit_function_interp *func, jit_item *args,
                VMCASE(JIT_OP_LDARG_NFLOAT):
                {
                        /* Load a native float argument onto the stack */
-                       VM_STK_NFLOATP = *VM_ARG(jit_float64);
+                       VM_STK_NFLOATP = *VM_ARG(jit_nfloat);
                        VM_MODIFY_PC_AND_STACK(2, -1);
                }
                VMBREAK;
@@ -4170,7 +4170,7 @@ void _jit_run_function(jit_function_interp *func, jit_item *args,
                VMCASE(JIT_OP_LDLOC_NFLOAT):
                {
                        /* Load a native float local onto the stack */
-                       VM_STK_NFLOATP = *VM_LOC(jit_float64);
+                       VM_STK_NFLOATP = *VM_LOC(jit_nfloat);
                        VM_MODIFY_PC_AND_STACK(2, -1);
                }
                VMBREAK;
index c8e1bbdd0899e02d04072b2e5ee3c515da6e9eca..bdb7ad5e1ade0677ab14eeba3908f7b1fd0a0cd8 100644 (file)
@@ -1,3 +1,4 @@
 
-TESTS = coerce.pas
+TESTS = coerce.pas \
+               math.pas
 TESTS_ENVIRONMENT = $(top_builddir)/dpas/dpas
diff --git a/tests/math.pas b/tests/math.pas
new file mode 100644 (file)
index 0000000..efefdaa
--- /dev/null
@@ -0,0 +1,110 @@
+(*
+ * math.pas - Test mathematical operators.
+ *
+ * Copyright (C) 2004  Southern Storm Software, Pty Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *)
+
+program coerce;
+
+const
+       pi = 3.14159265358979323846;
+
+var
+       failed: Boolean;
+
+procedure run(msg: String; value: Boolean);
+begin
+       Write(msg);
+       Write(" ... ");
+       if value then begin
+               WriteLn("ok");
+       end else begin
+               WriteLn("failed");
+               failed := True;
+       end;
+end;
+
+procedure runf(msg: String; value, expected, delta: ShortReal);
+begin
+       run(msg, Abs(value - expected) <= delta);
+end;
+
+procedure rund(msg: String; value, expected, delta: Real);
+begin
+       run(msg, Abs(value - expected) <= delta);
+end;
+
+procedure runn(msg: String; value, expected, delta: LongReal);
+begin
+       run(msg, Abs(value - expected) <= delta);
+end;
+
+procedure run_tests;
+var
+       b: Byte;
+       s: ShortInt;
+       f: ShortReal;
+       d: Real;
+       n: LongReal;
+begin
+       { Initialize some values of odd type sizes }
+       b := 3;
+       s := 67;
+       f := 24.0;
+       d := 56.0;
+       n := 123.5;
+
+       { Test coercion rules for mathematical operators }
+       run("math_coerce_byte", SameType(LongReal, Sin(b)));
+       run("math_coerce_short", SameType(LongReal, Cos(s)));
+       run("math_coerce_int", SameType(LongReal, 3 pow 5));
+       run("math_coerce_uint", SameType(LongReal, Atan2(3, 080000005H)));
+       run("math_coerce_long", SameType(LongReal, Round(0100000000H)));
+       run("math_coerce_ulong", SameType(LongReal, Log(08000000000000000H)));
+       run("math_coerce_float32", SameType(ShortReal, Log10(f)));
+       run("math_coerce_float64", SameType(Real, Atan(d)));
+       run("math_coerce_nfloat", SameType(LongReal, Abs(n)));
+
+       { Test that the operators give sane answers }
+       runf("math_sin_0", Sin(ShortReal(0.0)), ShortReal(0.0), 0.00001);
+       runf("math_sin_pi_2", Sin(ShortReal(pi / 2)), ShortReal(1.0), 0.00001);
+       f := ShortReal(pi);
+       runf("math_sin_pi", Sin(f), ShortReal(0.0), 0.00001);
+       runf("math_cos_0", Cos(ShortReal(0.0)), ShortReal(1.0), 0.00001);
+       runf("math_sqrt_1", Sqrt(ShortReal(1.0)), ShortReal(1.0), 0.00001);
+       runf("math_sqrt_2", Sqrt(ShortReal(2.0)), ShortReal(1.4142), 0.0001);
+       f := Sqrt(ShortReal(-1.0));
+       run("math_sqrt_m1", IsNaN(f));
+       rund("math_ceil_1.5", Ceil(Real(1.5)), Real(2.0), 0.00001);
+       rund("math_ceil_m1.5", Ceil(Real(-1.5)), Real(-1.0), 0.00001);
+       rund("math_floor_1.5", Floor(Real(1.5)), Real(1.0), 0.00001);
+       rund("math_floor_m1.5", Floor(Real(-1.5)), Real(-2.0), 0.00001);
+       rund("math_rint_1.5", Rint(Real(1.5)), Real(2.0), 0.00001);
+       rund("math_rint_2.5", Rint(Real(2.5)), Real(2.0), 0.00001);
+       rund("math_round_1.5", Round(Real(1.5)), Real(2.0), 0.00001);
+       rund("math_round_2.5", Round(Real(2.5)), Real(3.0), 0.00001);
+       runn("math_max_1_6", Max(1.0, 6.0), 6.0, 0.00001);
+       runn("math_min_1_6", Min(1.0, 6.0), 1.0, 0.00001);
+end;
+
+begin
+       failed := False;
+       run_tests;
+       if failed then begin
+               Terminate(1);
+       end;
+end.