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: Thu, 16 Aug 2018 00:41:32 +0100 Message-ID: <86sh3fgpub.fsf@gmail.com> References: <877ekwu1mn.fsf@tromey.com> <87o9e8oaot.fsf@tcd.ie> <87a7prk0h6.fsf@tromey.com> <86r2j13ihs.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1534376421 24005 195.159.176.226 (15 Aug 2018 23:40:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 15 Aug 2018 23:40:21 +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 Thu Aug 16 01:40:17 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 1fq5Oi-00069f-0I for ged-emacs-devel@m.gmane.org; Thu, 16 Aug 2018 01:40:16 +0200 Original-Received: from localhost ([::1]:52672 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fq5Qo-00044x-9C for ged-emacs-devel@m.gmane.org; Wed, 15 Aug 2018 19:42:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:45096) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fq5QB-00044c-Ec for emacs-devel@gnu.org; Wed, 15 Aug 2018 19:41:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fq5Q7-000062-CL for emacs-devel@gnu.org; Wed, 15 Aug 2018 19:41:47 -0400 Original-Received: from [195.159.176.226] (port=41173 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fq5Q5-0008Ug-QH for emacs-devel@gnu.org; Wed, 15 Aug 2018 19:41:42 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1fq5Nv-0005Fx-2S for emacs-devel@gnu.org; Thu, 16 Aug 2018 01:39:27 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 83 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:4nItM+fVRAOOodkB7kwmhtpGD+A= 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:228574 Archived-At: On Tue 14 Aug 2018, Andy Moreton wrote: > 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 and Basil, please try the following patch for expt. I have given it some light sanity testing on Windows 64bit. AndyM diff --git a/src/floatfns.c b/src/floatfns.c index bbf7df4db3..0e7e3090e8 100644 --- a/src/floatfns.c +++ b/src/floatfns.c @@ -204,29 +204,38 @@ DEFUN ("expt", Fexpt, Sexpt, 2, 2, 0, doc: /* Return the exponential ARG1 ** ARG2. */) (Lisp_Object arg1, Lisp_Object arg2) { - CHECK_FIXNUM_OR_FLOAT (arg1); + CHECK_NUMBER (arg1); CHECK_FIXNUM_OR_FLOAT (arg2); - if (FIXNUMP (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. */ - Lisp_Object val; - - x = XFIXNUM (arg1); - y = XFIXNUM (arg2); - acc = (y & 1 ? x : 1); - - while ((y >>= 1) != 0) - { - x *= x; - if (y & 1) - acc *= x; - } - XSETINT (val, acc); - return val; + + 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 */ + && (sizeof (EMACS_INT) <= sizeof (long) + || (long) XFIXNUM (arg2) == XFIXNUM (arg2))) + { + Lisp_Object res; + mpz_t val; + + mpz_init (val); + if (BIGNUMP (arg1)) + { + mpz_pow_ui (val, XBIGNUM (arg1)->value, XFIXNUM (arg2)); + } + else if (XFIXNUM (arg1) >= 0) + { + mpz_ui_pow_ui (val, XFIXNUM (arg1), XFIXNUM (arg2)); + } + else + { + mpz_ui_pow_ui (val, -XFIXNUM (arg1), XFIXNUM (arg2)); + if (XFIXNUM (arg2) & 1) + mpz_neg (val, val); + } + res = make_number (val); + mpz_clear (val); + return res; } + return make_float (pow (XFLOATINT (arg1), XFLOATINT (arg2))); }