From: Tom Tromey Date: Sat, 7 Jul 2018 05:26:13 +0000 (-0600) Subject: Make 1+ and 1- handle bignums X-Git-Tag: emacs-27.0.90~4598^2~27 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=0d868917efb46400cf7dd57a1cdbba7404f322a7;p=emacs.git 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. --- 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