From 0d868917efb46400cf7dd57a1cdbba7404f322a7 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Fri, 6 Jul 2018 23:26:13 -0600 Subject: [PATCH] Make 1+ and 1- handle bignums * src/data.c (Fadd1, Fsub1): Handle bignums. * test/src/data-tests.el (data-tests-1+, data-tests-1-): New tests. --- src/data.c | 48 ++++++++++++++++++++++++++++++++++++++---- test/src/data-tests.el | 8 +++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/data.c b/src/data.c index 18b572de977..d7175683c89 100644 --- a/src/data.c +++ b/src/data.c @@ -3239,12 +3239,32 @@ DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0, Markers are converted to integers. */) (register Lisp_Object number) { - CHECK_FIXNUM_OR_FLOAT_COERCE_MARKER (number); + CHECK_NUMBER_COERCE_MARKER (number); if (FLOATP (number)) return (make_float (1.0 + XFLOAT_DATA (number))); - XSETINT (number, XINT (number) + 1); + if (BIGNUMP (number)) + { + mpz_t num; + mpz_init (num); + mpz_add_ui (num, XBIGNUM (number)->value, 1); + number = make_number (num); + mpz_clear (num); + } + else + { + eassume (FIXNUMP (number)); + if (XINT (number) < MOST_POSITIVE_FIXNUM) + XSETINT (number, XINT (number) + 1); + else + { + mpz_t num; + mpz_init_set_si (num, XINT (number) + 1); + number = make_number (num); + mpz_clear (num); + } + } return number; } @@ -3253,12 +3273,32 @@ DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0, Markers are converted to integers. */) (register Lisp_Object number) { - CHECK_FIXNUM_OR_FLOAT_COERCE_MARKER (number); + CHECK_NUMBER_COERCE_MARKER (number); if (FLOATP (number)) return (make_float (-1.0 + XFLOAT_DATA (number))); - XSETINT (number, XINT (number) - 1); + if (BIGNUMP (number)) + { + mpz_t num; + mpz_init (num); + mpz_sub_ui (num, XBIGNUM (number)->value, 1); + number = make_number (num); + mpz_clear (num); + } + else + { + eassume (FIXNUMP (number)); + if (XINT (number) > MOST_POSITIVE_FIXNUM) + XSETINT (number, XINT (number) - 1); + else + { + mpz_t num; + mpz_init_set_si (num, XINT (number) - 1); + number = make_number (num); + mpz_clear (num); + } + } return number; } diff --git a/test/src/data-tests.el b/test/src/data-tests.el index 1143028a126..dd6ce196f96 100644 --- a/test/src/data-tests.el +++ b/test/src/data-tests.el @@ -579,4 +579,12 @@ comparing the subr with a much slower lisp implementation." (v (read s))) (should (equal (number-to-string v) s)))) +(ert-deftest data-tests-1+ () + (should (> (1+ most-positive-fixnum) most-positive-fixnum)) + (should (fixnump (1+ (1- most-negative-fixnum))))) + +(ert-deftest data-tests-1- () + (should (< (1- most-negative-fixnum) most-negative-fixnum)) + (should (fixnump (1- (1+ most-positive-fixnum))))) + ;;; data-tests.el ends here -- 2.39.2