diff options
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | arith.c | 15 | ||||
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | mpi-patches/mpi-to-double | 58 | ||||
-rw-r--r-- | mpi-patches/series | 1 |
6 files changed, 89 insertions, 0 deletions
@@ -1,5 +1,18 @@ 2012-03-20 Kaz Kylheku <kaz@kylheku.com> + * arith.c (flo_int): New function. + + * eval.c (eval_init): flo-int registered as intrinsic. + + * lib.h (flo_int): Declared. + + * mpi-patches/series: Added mpi-to-double to patch stack. + (mp_to_double): New MPI function. + + * mpi-patches/mpi-to-double: New file. + +2012-03-20 Kaz Kylheku <kaz@kylheku.com> + * arith.c (plus): Optimization: use num_fast when result is in the fixnum range. Implemented FLNUM cases, except for adding a FLNUM @@ -1157,6 +1157,21 @@ val int_flo(val f) } } +val flo_int(val i) +{ + if (fixnump(i)) + return flo(c_num(i)); + + { + double d; + type_check(i, BGNUM); + if (mp_to_double(mp(i), &d) != MP_OKAY) + uw_throwf(error_s, lit("flo-int: bignum to float conversion failed"), + nao); + return flo(d); + } +} + void arith_init(void) { mp_init(&NUM_MAX_MP); @@ -2292,6 +2292,7 @@ void eval_init(void) reg_fun(intern(lit("int-str"), user_package), func_n2o(int_str, 1)); reg_fun(intern(lit("flo-str"), user_package), func_n1(flo_str)); reg_fun(intern(lit("int-flo"), user_package), func_n1(int_flo)); + reg_fun(intern(lit("flo-int"), user_package), func_n1(flo_int)); reg_fun(intern(lit("chrp"), user_package), func_n1(chrp)); reg_fun(intern(lit("chr-isalnum"), user_package), func_n1(chr_isalnum)); reg_fun(intern(lit("chr-isalpha"), user_package), func_n1(chr_isalpha)); @@ -454,6 +454,7 @@ val string_lt(val astr, val bstr); val int_str(val str, val base); val flo_str(val str); val int_flo(val f); +val flo_int(val i); val chrp(val chr); wchar_t c_chr(val chr); val chr_isalnum(val ch); diff --git a/mpi-patches/mpi-to-double b/mpi-patches/mpi-to-double new file mode 100644 index 00000000..653d612c --- /dev/null +++ b/mpi-patches/mpi-to-double @@ -0,0 +1,58 @@ +Index: mpi-1.8.6/mpi.c +=================================================================== +--- mpi-1.8.6.orig/mpi.c 2012-03-20 20:23:46.604727758 -0700 ++++ mpi-1.8.6/mpi.c 2012-03-20 20:37:28.514792258 -0700 +@@ -14,6 +14,7 @@ + #include <stdlib.h> + #include <string.h> + #include <ctype.h> ++#include <math.h> + + typedef unsigned char mem_t; + extern mem_t *chk_malloc(size_t size); +@@ -2329,6 +2330,29 @@ + + /* }}} */ + ++mp_err mp_to_double(mp_int *mp, double *d) ++{ ++ int ix; ++ mp_size used = USED(mp); ++ mp_digit *dp = DIGITS(mp); ++ static double mult; ++ double out = dp[used - 1]; ++ ++ if (!mult) ++ mult = pow(2.0, MP_DIGIT_BIT); ++ ++ for (ix = (int) used - 2; ix >= 0; ix++) { ++ out = out * mult; ++ out += (double) dp[ix]; ++ } ++ ++ if (SIGN(mp) == MP_NEG) ++ out = -out; ++ ++ *d = out; ++ return MP_OKAY; ++} ++ + /*------------------------------------------------------------------------*/ + /* {{{ mp_print(mp, ofp) */ + +Index: mpi-1.8.6/mpi.h +=================================================================== +--- mpi-1.8.6.orig/mpi.h 2012-03-20 20:23:39.184556258 -0700 ++++ mpi-1.8.6/mpi.h 2012-03-20 20:25:30.018865508 -0700 +@@ -187,6 +187,11 @@ + #endif /* end MP_NUMTH */ + + /*------------------------------------------------------------------------*/ ++/* Conversions */ ++ ++mp_err mp_to_double(mp_int *mp, double *d); ++ ++/*------------------------------------------------------------------------*/ + /* Input and output */ + + #if MP_IOFUNC diff --git a/mpi-patches/series b/mpi-patches/series index 0181c920..c880ab60 100644 --- a/mpi-patches/series +++ b/mpi-patches/series @@ -12,3 +12,4 @@ fix-bad-shifts bit-search-optimizations shrink-mpi-int faster-square-root +mpi-to-double |