From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: bug#32463: 27.0.50; (logior -1) => 4611686018427387903 Date: Sun, 19 Aug 2018 03:52:55 -0700 Organization: UCLA Computer Science Department Message-ID: <66c3b06d-bf67-8f61-4b13-1debf0668010@cs.ucla.edu> References: <5230a57b-5896-606d-f157-2e547710b6e8@cs.ucla.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------7320B30EED029E4B6B51CAC8" X-Trace: blaine.gmane.org 1534675933 12217 195.159.176.226 (19 Aug 2018 10:52:13 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 19 Aug 2018 10:52:13 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cc: 32463@debbugs.gnu.org To: Andy Moreton Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Aug 19 12:52:09 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frLJY-00031Y-V8 for geb-bug-gnu-emacs@m.gmane.org; Sun, 19 Aug 2018 12:52:09 +0200 Original-Received: from localhost ([::1]:42372 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frLLd-0002NF-QV for geb-bug-gnu-emacs@m.gmane.org; Sun, 19 Aug 2018 06:54:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43265) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1frLLT-0002N4-Oq for bug-gnu-emacs@gnu.org; Sun, 19 Aug 2018 06:54:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1frLLO-0006TS-AG for bug-gnu-emacs@gnu.org; Sun, 19 Aug 2018 06:54:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49394) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1frLLN-0006TI-Ub for bug-gnu-emacs@gnu.org; Sun, 19 Aug 2018 06:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1frLLN-0001Mw-Qu for bug-gnu-emacs@gnu.org; Sun, 19 Aug 2018 06:54:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Paul Eggert Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 19 Aug 2018 10:54:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 32463 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 32463-submit@debbugs.gnu.org id=B32463.15346759864986 (code B ref 32463); Sun, 19 Aug 2018 10:54:01 +0000 Original-Received: (at 32463) by debbugs.gnu.org; 19 Aug 2018 10:53:06 +0000 Original-Received: from localhost ([127.0.0.1]:54412 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frLKU-0001IM-8H for submit@debbugs.gnu.org; Sun, 19 Aug 2018 06:53:06 -0400 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:58002) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1frLKS-0001HK-1v for 32463@debbugs.gnu.org; Sun, 19 Aug 2018 06:53:04 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id EAF3716081D; Sun, 19 Aug 2018 03:52:57 -0700 (PDT) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id Ve21TKXzgGxc; Sun, 19 Aug 2018 03:52:56 -0700 (PDT) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id A767E16087B; Sun, 19 Aug 2018 03:52:56 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 3IMvZaEb4_v1; Sun, 19 Aug 2018 03:52:56 -0700 (PDT) Original-Received: from [192.168.1.9] (unknown [47.154.30.119]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 574A216081D; Sun, 19 Aug 2018 03:52:56 -0700 (PDT) In-Reply-To: <5230a57b-5896-606d-f157-2e547710b6e8@cs.ucla.edu> Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:149590 Archived-At: This is a multi-part message in MIME format. --------------7320B30EED029E4B6B51CAC8 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable >> a) A bug in bignumcompare for 64bit Windows. Patch is here: >> http://lists.gnu.org/archive/html/emacs-devel/2018-08/msg00487.html >> ... >> b) fmod_float has a bug: >> http://lists.gnu.org/archive/html/emacs-devel/2018-08/msg00442.html >> >> c) Extend Fexpt to support bignums. Patch is here: >> http://lists.gnu.org/archive/html/emacs-devel/2018-08/msg00503.html >> >> d) Extend Fceiling, Ffloor, Fround and Ftruncate to support bignums by >> =C2=A0=C2=A0 updating rounding_driver. I worked on these and installed patches to master that should do (a), (b)= , and=20 (c). For (d) I wrote the attached patch, and plan to test it a bit more b= efore=20 installing, as it's the hairiest. --------------7320B30EED029E4B6B51CAC8 Content-Type: text/x-patch; name="rounding.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rounding.diff" diff --git a/src/floatfns.c b/src/floatfns.c index 54d068c29e..e06657219d 100644 --- a/src/floatfns.c +++ b/src/floatfns.c @@ -379,10 +379,10 @@ This is the same as the exponent of a float. */) static Lisp_Object rounding_driver (Lisp_Object arg, Lisp_Object divisor, double (*double_round) (double), - EMACS_INT (*int_round2) (EMACS_INT, EMACS_INT), + void (*int_divide) (mpz_t, mpz_t const, mpz_t const), const char *name) { - CHECK_FIXNUM_OR_FLOAT (arg); + CHECK_NUMBER (arg); double d; if (NILP (divisor)) @@ -393,12 +393,25 @@ rounding_driver (Lisp_Object arg, Lisp_Object divisor, } else { - CHECK_FIXNUM_OR_FLOAT (divisor); + CHECK_NUMBER (divisor); if (!FLOATP (arg) && !FLOATP (divisor)) { - if (XFIXNUM (divisor) == 0) + if (EQ (divisor, make_fixnum (0))) xsignal0 (Qarith_error); - return make_fixnum (int_round2 (XFIXNUM (arg), XFIXNUM (divisor))); + mpz_t d, q; + mpz_init (d); + mpz_init (q); + int_divide (q, + (FIXNUMP (arg) + ? (mpz_set_intmax (q, XFIXNUM (arg)), q) + : XBIGNUM (arg)->value), + (FIXNUMP (divisor) + ? (mpz_set_intmax (d, XFIXNUM (divisor)), d) + : XBIGNUM (arg)->value)); + Lisp_Object result = make_number (q); + mpz_clear (d); + mpz_clear (q); + return result; } double f1 = FLOATP (arg) ? XFLOAT_DATA (arg) : XFIXNUM (arg); @@ -422,37 +435,39 @@ rounding_driver (Lisp_Object arg, Lisp_Object divisor, xsignal2 (Qrange_error, build_string (name), arg); } -static EMACS_INT -ceiling2 (EMACS_INT i1, EMACS_INT i2) -{ - return i1 / i2 + ((i1 % i2 != 0) & ((i1 < 0) == (i2 < 0))); -} - -static EMACS_INT -floor2 (EMACS_INT i1, EMACS_INT i2) -{ - return i1 / i2 - ((i1 % i2 != 0) & ((i1 < 0) != (i2 < 0))); -} - -static EMACS_INT -truncate2 (EMACS_INT i1, EMACS_INT i2) -{ - return i1 / i2; -} - -static EMACS_INT -round2 (EMACS_INT i1, EMACS_INT i2) -{ - /* The C language's division operator 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 C's quotient is - odd. */ - EMACS_INT q = i1 / i2; - EMACS_INT r = i1 % i2; - EMACS_INT abs_r = eabs (r); - EMACS_INT abs_r1 = eabs (i2) - abs_r; - return q + (abs_r + (q & 1) <= abs_r1 ? 0 : (i2 ^ r) < 0 ? -1 : 1); +static void +rounddiv_q (mpz_t q, mpz_t const n, mpz_t const 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. + + If we were using EMACS_INT arithmetic instead of bignums, + the following code could look something like this: + + q = n / d; + r = n % d; + neg_d = d < 0; + neg_r = r < 0; + r = eabs (r); + abs_r1 = eabs (d) - r; + if (abs_r1 < r + (q & 1)) + q += neg_d == neg_r ? 1 : -1; */ + + mpz_t r, abs_r1; + mpz_init (r); + mpz_init (abs_r1); + mpz_tdiv_qr (q, r, n, d); + bool neg_d = mpz_sgn (d) < 0; + bool neg_r = mpz_sgn (r) < 0; + mpz_abs (r, r); + mpz_abs (abs_r1, d); + mpz_sub (abs_r1, abs_r1, r); + if (mpz_cmp (abs_r1, r) < (mpz_odd_p (q) != 0)) + (neg_d == neg_r ? mpz_add_ui : mpz_sub_ui) (q, q, 1); + mpz_clear (r); + mpz_clear (abs_r1); } /* The code uses emacs_rint, so that it works to undefine HAVE_RINT @@ -483,7 +498,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, ceiling2, "ceiling"); + return rounding_driver (arg, divisor, ceil, mpz_cdiv_q, "ceiling"); } DEFUN ("floor", Ffloor, Sfloor, 1, 2, 0, @@ -492,7 +507,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, floor2, "floor"); + return rounding_driver (arg, divisor, floor, mpz_fdiv_q, "floor"); } DEFUN ("round", Fround, Sround, 1, 2, 0, @@ -505,7 +520,7 @@ 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, round2, "round"); + return rounding_driver (arg, divisor, emacs_rint, rounddiv_q, "round"); } DEFUN ("truncate", Ftruncate, Struncate, 1, 2, 0, @@ -514,7 +529,7 @@ Rounds ARG toward zero. With optional DIVISOR, truncate ARG/DIVISOR. */) (Lisp_Object arg, Lisp_Object divisor) { - return rounding_driver (arg, divisor, trunc, truncate2, "truncate"); + return rounding_driver (arg, divisor, trunc, mpz_tdiv_q, "truncate"); } --------------7320B30EED029E4B6B51CAC8--