From: Tom Tromey Date: Sat, 7 Jul 2018 20:53:23 +0000 (-0600) Subject: Provide new functions to create bignums X-Git-Tag: emacs-27.0.90~4598^2~37 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=b2f3f4ee29ba8510d3cad8025d9ce2c2014b1b7f;p=emacs.git Provide new functions to create bignums * src/alloc.c (make_bignum_str, make_number): New functions. * src/lisp.h (make_bignum_str, make_number): Declare. --- diff --git a/src/alloc.c b/src/alloc.c index 8ebf3e05d69..b775948fd96 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -3779,6 +3779,51 @@ build_marker (struct buffer *buf, ptrdiff_t charpos, ptrdiff_t bytepos) return obj; } + + +Lisp_Object +make_bignum_str (const char *num, int base) +{ + Lisp_Object obj; + struct Lisp_Bignum *b; + int check; + + obj = allocate_misc (Lisp_Misc_Bignum); + b = XBIGNUM (obj); + mpz_init (b->value); + check = mpz_set_str (b->value, num, base); + eassert (check == 0); + return obj; +} + +/* Given an mpz_t, make a number. This may return a bignum or a + fixnum depending on VALUE. */ + +Lisp_Object +make_number (mpz_t value) +{ + Lisp_Object obj; + struct Lisp_Bignum *b; + + if (mpz_fits_slong_p (value)) + { + long l = mpz_get_si (value); + if (!FIXNUM_OVERFLOW_P (l)) + { + XSETINT (obj, l); + return obj; + } + } + + obj = allocate_misc (Lisp_Misc_Bignum); + b = XBIGNUM (obj); + /* We could mpz_init + mpz_swap here, to avoid a copy, but the + resulting API seemed possibly confusing. */ + mpz_init_set (b->value, value); + + return obj; +} + /* Return a newly created vector or string with specified arguments as elements. If all the arguments are characters that can fit diff --git a/src/lisp.h b/src/lisp.h index 37e43b0c5a1..6a3db24949a 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3643,6 +3643,9 @@ extern Lisp_Object list5 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object, enum constype {CONSTYPE_HEAP, CONSTYPE_PURE}; extern Lisp_Object listn (enum constype, ptrdiff_t, Lisp_Object, ...); +extern Lisp_Object make_bignum_str (const char *num, int base); +extern Lisp_Object make_number (mpz_t value); + /* Build a frequently used 2/3/4-integer lists. */ INLINE Lisp_Object