>From baa6ae8724fd4cd7631164a89bf8eed4ff79cfc0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 5 Sep 2018 00:21:02 -0700 Subject: [PATCH] Improve (round FIXNUM FIXNUM) performance * src/floatfns.c (rounding_driver): New arg fixnum_divide. All callers changed. (ceiling2, floor2, truncate2, round2): New functions. Not that new, actually; these are essentially taken from Emacs 26. (Fceiling, Ffloor, Fround, Ftruncate): Use them. --- src/floatfns.c | 74 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/src/floatfns.c b/src/floatfns.c index 13ab7b0359..dc7236353c 100644 --- a/src/floatfns.c +++ b/src/floatfns.c @@ -339,6 +339,7 @@ static Lisp_Object rounding_driver (Lisp_Object arg, Lisp_Object divisor, double (*double_round) (double), void (*int_divide) (mpz_t, mpz_t const, mpz_t const), + EMACS_INT (*fixnum_divide) (EMACS_INT, EMACS_INT), const char *name) { CHECK_NUMBER (arg); @@ -357,8 +358,14 @@ rounding_driver (Lisp_Object arg, Lisp_Object divisor, { /* Divide as integers. Converting to double might lose info, even for fixnums; also see the FIXME below. */ - if (EQ (divisor, make_fixnum (0))) - xsignal0 (Qarith_error); + if (FIXNUMP (divisor)) + { + if (XFIXNUM (divisor) == 0) + xsignal0 (Qarith_error); + if (FIXNUMP (arg)) + return make_int (fixnum_divide (XFIXNUM (arg), + XFIXNUM (divisor))); + } int_divide (mpz[0], *bignum_integer (&mpz[0], arg), *bignum_integer (&mpz[1], divisor)); @@ -387,26 +394,47 @@ rounding_driver (Lisp_Object arg, Lisp_Object divisor, return double_to_bignum (dr); } -static void -rounddiv_q (mpz_t q, mpz_t const n, mpz_t const d) +static EMACS_INT +ceiling2 (EMACS_INT n, EMACS_INT d) { - /* mpz_tdiv_qr gives us one remainder R, but we want the remainder - R1 on the other side of 0 if R1 is closer to 0 than R is; because - we want to round to even, we also want R1 if R and R1 are the - same distance from 0 and if the quotient is odd. + return n / d + ((n % d != 0) & ((n < 0) == (d < 0))); +} - If we were using EMACS_INT arithmetic instead of bignums, - the following code could look something like this: +static EMACS_INT +floor2 (EMACS_INT n, EMACS_INT d) +{ + return n / d - ((n % d != 0) & ((n < 0) != (d < 0))); +} - q = n / d; - r = n % d; - neg_d = d < 0; - neg_r = r < 0; - abs_r = eabs (r); - abs_r1 = eabs (d) - abs_r; - if (abs_r1 < abs_r + (q & 1)) - q += neg_d == neg_r ? 1 : -1; */ +static EMACS_INT +truncate2 (EMACS_INT n, EMACS_INT d) +{ + return n / d; +} +static EMACS_INT +round2 (EMACS_INT n, EMACS_INT d) +{ + /* The C language's division operator gives us the remainder R + corresponding to truncated division, but we want the remainder R1 + on the other side of 0 if R1 is closer to 0 than R is; because we + want to round to even, we also want R1 if R and R1 are the same + distance from 0 and if the truncated quotient is odd. */ + EMACS_INT q = n / d; + EMACS_INT r = n % d; + bool neg_d = d < 0; + bool neg_r = r < 0; + EMACS_INT abs_r = eabs (r); + EMACS_INT abs_r1 = eabs (d) - abs_r; + if (abs_r1 < abs_r + (q & 1)) + q += neg_d == neg_r ? 1 : -1; + return q; +} + +static void +rounddiv_q (mpz_t q, mpz_t const n, mpz_t const d) +{ + /* Mimic the source code of round2, using mpz_t instead of EMACS_INT. */ mpz_t *r = &mpz[2], *abs_r = r, *abs_r1 = &mpz[3]; mpz_tdiv_qr (q, *r, n, d); bool neg_d = mpz_sgn (d) < 0; @@ -446,7 +474,7 @@ This rounds the value towards +inf. With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR. */) (Lisp_Object arg, Lisp_Object divisor) { - return rounding_driver (arg, divisor, ceil, mpz_cdiv_q, "ceiling"); + return rounding_driver (arg, divisor, ceil, mpz_cdiv_q, ceiling2, "ceiling"); } DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0, @@ -455,7 +483,7 @@ This rounds the value towards -inf. With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR. */) (Lisp_Object arg, Lisp_Object divisor) { - return rounding_driver (arg, divisor, floor, mpz_fdiv_q, "floor"); + return rounding_driver (arg, divisor, floor, mpz_fdiv_q, floor2, "floor"); } DEFUN ("round", Fround, Sround, 1, 2, 0, @@ -468,7 +496,8 @@ your machine. For example, (round 2.5) can return 3 on some systems, but 2 on others. */) (Lisp_Object arg, Lisp_Object divisor) { - return rounding_driver (arg, divisor, emacs_rint, rounddiv_q, "round"); + return rounding_driver (arg, divisor, emacs_rint, rounddiv_q, round2, + "round"); } /* Since rounding_driver truncates anyway, no need to call 'trunc'. */ @@ -484,7 +513,8 @@ Rounds ARG toward zero. With optional DIVISOR, truncate ARG/DIVISOR. */) (Lisp_Object arg, Lisp_Object divisor) { - return rounding_driver (arg, divisor, identity, mpz_tdiv_q, "truncate"); + return rounding_driver (arg, divisor, identity, mpz_tdiv_q, truncate2, + "truncate"); } -- 2.17.1