summaryrefslogtreecommitdiffstats
path: root/lib.h
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2011-12-09 22:25:51 -0800
committerKaz Kylheku <kaz@kylheku.com>2011-12-09 22:25:51 -0800
commit236a20e92316535bc75dde63d51431875e253bfb (patch)
tree6ec4fb84a27cb311027495db9d3c34b791fe207e /lib.h
parentb1088a2502cba1a61b862f708489c8d4baa722fe (diff)
downloadtxr-236a20e92316535bc75dde63d51431875e253bfb.tar.gz
txr-236a20e92316535bc75dde63d51431875e253bfb.tar.bz2
txr-236a20e92316535bc75dde63d51431875e253bfb.zip
Bignum support, here we go!
Bignums, based on Michael Fromberger's MPI library, are integrated into the input syntax, stream output, equality testing, the garbage collector, and hashing. The plus operation handles transitions between fixnums and bignums. Other operations are still fixnum only. * Makefile (CFLAGS): Add mpi directory to include file search. (OBJS): Include new arith.o module and all of MPI_OBJS. (MPI_OBJS, MPI_OBJS_BASE): New variables. * configure (mpi_version, have_quilt, have_patch): New variables. Script detects whether patch and quilt are available. Unpacks mpi library, applies patches. Detects 128 bit integer type. Records more information in config.h about the sizes of types. * dep.mk: Updated. * depend.txr: Make work with paths that have directory components. * eval.c (eval_init): Rename of nump to fixnump. * gc.c (finalize, mark_obj): Handle BGNUM case. * hash.c: (hash_c_str): Changed to return unsigned long instead of long. (equal_hash): Handle BGNUM case. (eql_hash): Handle bignums with equal-hash, but other objects as eq. * lib.c (num_s): Variable renamed to fixnum_s. (bignum_s): New symbol variable. (code2type): Follow rename of num_s. Handle BGNUM case. (typeof): Follow rename of num_s. (eql): Handle bignums using equal, and other types using eq. (equal): Handle BGNUM case. (chk_calloc): New function. (c_num): Wording change in error message: is not a fixnum. (nump): Renamed to fixnump. (bignump): New function. (plus): Function removed, reimplemented in arith.c. (int_str): Handle integers which are too large for wcstol using bignum conversion. Base 0 is no longer passed to wcstol but converted to 10 because the special semantics for 0 would be inconsistent for bignums. (obj_init): Follow rename of num_s. Initialize bignum_s.
Diffstat (limited to 'lib.h')
-rw-r--r--lib.h24
1 files changed, 20 insertions, 4 deletions
diff --git a/lib.h b/lib.h
index 019ffb73..9c4fe339 100644
--- a/lib.h
+++ b/lib.h
@@ -24,6 +24,8 @@
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
+#include "mpi.h"
+
typedef int_ptr_t cnum;
#define TAG_SHIFT 2
@@ -37,7 +39,8 @@ typedef int_ptr_t cnum;
typedef enum type {
NUM = TAG_NUM, CHR = TAG_CHR, LIT = TAG_LIT, CONS,
- STR, SYM, PKG, FUN, VEC, LCONS, LSTR, COBJ, ENV
+ STR, SYM, PKG, FUN, VEC, LCONS, LSTR, COBJ, ENV,
+ BGNUM
} type_t;
typedef enum functype
@@ -183,6 +186,11 @@ struct env {
val up_env;
};
+struct bignum {
+ type_t type;
+ mp_int mp;
+};
+
union obj {
struct any t;
struct cons c;
@@ -195,6 +203,7 @@ union obj {
struct lazy_string ls;
struct cobj co;
struct env e;
+ struct bignum bn;
};
INLINE cnum tag(val obj) { return ((cnum) obj) & TAG_MASK; }
@@ -245,6 +254,11 @@ INLINE val num_fast(cnum n)
return (val) ((n << TAG_SHIFT) | TAG_NUM);
}
+INLINE mp_int *mp(val bign)
+{
+ return &bign->bn.mp;
+}
+
INLINE val chr(wchar_t ch)
{
return (val) (((cnum) ch << TAG_SHIFT) | TAG_CHR);
@@ -259,9 +273,9 @@ INLINE val chr(wchar_t ch)
#define lit(strlit) lit_noex(strlit)
extern val keyword_package, system_package, user_package;
-extern val null, t, cons_s, str_s, chr_s, num_s, sym_s, pkg_s, fun_s, vec_s;
+extern val null, t, cons_s, str_s, chr_s, fixnum_s, sym_s, pkg_s, fun_s, vec_s;
extern val stream_s, hash_s, hash_iter_s, lcons_s, lstr_s, cobj_s, cptr_s;
-extern val env_s;
+extern val env_s, bignum_s;
extern val var_s, expr_s, regex_s, chset_s, set_s, cset_s, wild_s, oneplus_s;
extern val nongreedy_s, compiled_regex_s;
extern val quote_s, qquote_s, unquote_s, splice_s;
@@ -328,6 +342,7 @@ val none_satisfy(val list, val pred, val key);
val eql(val left, val right);
val equal(val left, val right);
mem_t *chk_malloc(size_t size);
+mem_t *chk_calloc(size_t n, size_t size);
mem_t *chk_realloc(mem_t *, size_t size);
wchar_t *chk_strdup(const wchar_t *str);
val cons(val car, val cdr);
@@ -346,7 +361,8 @@ val proper_plist_to_alist(val list);
val improper_plist_to_alist(val list, val boolean_keys);
val num(cnum val);
cnum c_num(val num);
-val nump(val num);
+val fixnump(val num);
+val bignump(val num);
val plus(val anum, val bnum);
val plusv(val nlist);
val minus(val anum, val bnum);