From fe69004a3798e896cf7349149c6c37ec58676b45 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 20 Mar 2012 20:59:12 -0700 Subject: * 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. --- ChangeLog | 13 +++++++++++ arith.c | 15 ++++++++++++ eval.c | 1 + lib.h | 1 + mpi-patches/mpi-to-double | 58 +++++++++++++++++++++++++++++++++++++++++++++++ mpi-patches/series | 1 + 6 files changed, 89 insertions(+) create mode 100644 mpi-patches/mpi-to-double diff --git a/ChangeLog b/ChangeLog index 21b6aa76..78f6910d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2012-03-20 Kaz Kylheku + + * 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 * arith.c (plus): Optimization: use num_fast when diff --git a/arith.c b/arith.c index 5c17048d..769da0d0 100644 --- a/arith.c +++ b/arith.c @@ -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); diff --git a/eval.c b/eval.c index c56919a7..e185acb0 100644 --- a/eval.c +++ b/eval.c @@ -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)); diff --git a/lib.h b/lib.h index 211d27de..df9c2592 100644 --- a/lib.h +++ b/lib.h @@ -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 + #include + #include ++#include + + 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 -- cgit v1.2.3