From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Andy Moreton Newsgroups: gmane.emacs.devel Subject: Re: Merging bignum to master Date: Tue, 14 Aug 2018 01:21:51 +0100 Message-ID: <86r2j13ihs.fsf@gmail.com> References: <877ekwu1mn.fsf@tromey.com> <87o9e8oaot.fsf@tcd.ie> <87a7prk0h6.fsf@tromey.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1534206035 5548 195.159.176.226 (14 Aug 2018 00:20:35 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 14 Aug 2018 00:20:35 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1.50 (windows-nt) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Aug 14 02:20:31 2018 Return-path: Envelope-to: ged-emacs-devel@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 1fpN4Y-0001Ia-8S for ged-emacs-devel@m.gmane.org; Tue, 14 Aug 2018 02:20:30 +0200 Original-Received: from localhost ([::1]:41888 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fpN6c-0002ki-R7 for ged-emacs-devel@m.gmane.org; Mon, 13 Aug 2018 20:22:38 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50943) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fpN64-0002kR-6R for emacs-devel@gnu.org; Mon, 13 Aug 2018 20:22:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fpN61-0006zB-1F for emacs-devel@gnu.org; Mon, 13 Aug 2018 20:22:04 -0400 Original-Received: from [195.159.176.226] (port=55160 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fpN60-0006xB-Pd for emacs-devel@gnu.org; Mon, 13 Aug 2018 20:22:00 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1fpN3s-0000Vu-9g for emacs-devel@gnu.org; Tue, 14 Aug 2018 02:19:48 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 77 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:RC34HVG2BdRpml6KCJi9GVJ8z6k= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:228512 Archived-At: On Sun 12 Aug 2018, Tom Tromey wrote: >>>>>> "Basil" == Basil L Contovounesios writes: > > Basil> It'd be nice if expt could be updated to handle bignums: > > What do you think of this? > > Tom > > diff --git a/src/floatfns.c b/src/floatfns.c > index bbf7df4db3..c55418a35c 100644 > --- a/src/floatfns.c > +++ b/src/floatfns.c > @@ -206,25 +206,28 @@ DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0, > { > CHECK_FIXNUM_OR_FLOAT (arg1); > CHECK_FIXNUM_OR_FLOAT (arg2); > - if (FIXNUMP (arg1) /* common lisp spec */ > + if (INTEGERP (arg1) /* common lisp spec */ > && FIXNUMP (arg2) /* don't promote, if both are ints, and */ > && XFIXNUM (arg2) >= 0) /* we are sure the result is not fractional */ > { /* this can be improved by pre-calculating */ > - EMACS_INT y; /* some binary powers of x then accumulating */ > - EMACS_UINT acc, x; /* Unsigned so that overflow is well defined. */ You have dropped some of the commentary that explains how it is trying to give similar behaviour to Common Lisp. See the description at: > Lisp_Object val; > + mpz_t x, *xp, r; > > - x = XFIXNUM (arg1); > - y = XFIXNUM (arg2); > - acc = (y & 1 ? x : 1); > - > - while ((y >>= 1) != 0) > + if (BIGNUMP (arg1)) > + xp = &XBIGNUM (arg1)->value; > + else > { > - x *= x; > - if (y & 1) > - acc *= x; > + mpz_init_set_si (x, XFIXNUM (arg1)); This doesn't work for systems where sizeof (EMACS_INT) > sizeof (long). > + xp = &x; > } > - XSETINT (val, acc); > + > + mpz_init (r); > + mpz_pow_ui (r, *xp, XFIXNUM (arg2)); Likewise. > + > + val = make_number (r); > + mpz_clear (r); > + if (xp == &x) > + mpz_clear (x); > return val; > } > return make_float (pow (XFLOATINT (arg1), XFLOATINT (arg2))); > diff --git a/test/src/floatfns-tests.el b/test/src/floatfns-tests.el > index 7714c05d60..4fab032ecb 100644 > --- a/test/src/floatfns-tests.el > +++ b/test/src/floatfns-tests.el > @@ -46,4 +46,8 @@ > (should (= (+ (logb most-positive-fixnum) 1) > (logb (+ most-positive-fixnum 1))))) > > +(ert-deftest bignum-expt () > + (should (= (expt 10 100) > + (apply #'* (make-list 100 10))))) > + > (provide 'floatfns-tests)