From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.devel Subject: Re: Using the GNU GMP Library for Bignums in Emacs Date: Wed, 1 Aug 2018 00:57:09 -0700 Organization: UCLA Computer Science Department Message-ID: <94d69bf6-d186-3607-5b1d-9dee4a08ace1@cs.ucla.edu> References: <29f933ac-a6bf-8742-66a7-0a9d6d3e5a88@disroot.org> <83bmecy6fx.fsf@gnu.org> <0d3175d8-d996-651e-b221-71978bde3a65@cs.ucla.edu> <87tvpdnzgy.fsf@tromey.com> <4c2a814f-c254-29e5-39cf-11b5f2e5c9c8@cs.ucla.edu> <49d8ba62-c9a5-9203-d882-8e900b441ff3@cs.ucla.edu> <8e0320d9-e0d0-2b57-57cc-2df4399f133c@cs.ucla.edu> <87lgaio7xd.fsf@tromey.com> <877em1cb0i.fsf@tromey.com> <765767b2-d2e5-a9a6-f724-d58ecf4847bb@cs.ucla.edu> <1452F858-C7FD-4AEB-BB85-D874692F918F@raeburn.org> <5b3be3f4-d438-166a-863a-587c880584c8@cs.ucla.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------8BCA8F2B49317F2F2A3EB87E" X-Trace: blaine.gmane.org 1533110124 10030 195.159.176.226 (1 Aug 2018 07:55:24 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 1 Aug 2018 07:55:24 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cc: Tom Tromey , Stefan Monnier , Richard Stallman , emacs-devel@gnu.org To: Ken Raeburn Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Aug 01 09:55:19 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 1fklyX-0002SM-TI for ged-emacs-devel@m.gmane.org; Wed, 01 Aug 2018 09:55:18 +0200 Original-Received: from localhost ([::1]:33885 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fkm0e-0007fd-F3 for ged-emacs-devel@m.gmane.org; Wed, 01 Aug 2018 03:57:28 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49690) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fkm0U-0007fL-BU for emacs-devel@gnu.org; Wed, 01 Aug 2018 03:57:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fkm0R-0001zB-No for emacs-devel@gnu.org; Wed, 01 Aug 2018 03:57:18 -0400 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:58446) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fkm0R-0001yz-9h; Wed, 01 Aug 2018 03:57:15 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id C3763160222; Wed, 1 Aug 2018 00:57:13 -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 jq_Dam2MXlLD; Wed, 1 Aug 2018 00:57:10 -0700 (PDT) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 471861605B1; Wed, 1 Aug 2018 00:57:10 -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 N2Lajd-wkVc5; Wed, 1 Aug 2018 00:57:10 -0700 (PDT) Original-Received: from [192.168.1.9] (unknown [47.154.30.119]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id BE47A160222; Wed, 1 Aug 2018 00:57:09 -0700 (PDT) Openpgp: preference=signencrypt Autocrypt: addr=eggert@cs.ucla.edu; prefer-encrypt=mutual; keydata= xsFNBEyAcmQBEADAAyH2xoTu7ppG5D3a8FMZEon74dCvc4+q1XA2J2tBy2pwaTqfhpxxdGA9 Jj50UJ3PD4bSUEgN8tLZ0san47l5XTAFLi2456ciSl5m8sKaHlGdt9XmAAtmXqeZVIYX/UFS 96fDzf4xhEmm/y7LbYEPQdUdxu47xA5KhTYp5bltF3WYDz1Ygd7gx07Auwp7iw7eNvnoDTAl KAl8KYDZzbDNCQGEbpY3efZIvPdeI+FWQN4W+kghy+P6au6PrIIhYraeua7XDdb2LS1en3Ss mE3QjqfRqI/A2ue8JMwsvXe/WK38Ezs6x74iTaqI3AFH6ilAhDqpMnd/msSESNFt76DiO1ZK QMr9amVPknjfPmJISqdhgB1DlEdw34sROf6V8mZw0xfqT6PKE46LcFefzs0kbg4GORf8vjG2 Sf1tk5eU8MBiyN/bZ03bKNjNYMpODDQQwuP84kYLkX2wBxxMAhBxwbDVZudzxDZJ1C2VXujC OJVxq2kljBM9ETYuUGqd75AW2LXrLw6+MuIsHFAYAgRr7+KcwDgBAfwhPBYX34nSSiHlmLC+ KaHLeCLF5ZI2vKm3HEeCTtlOg7xZEONgwzL+fdKo+D6SoC8RRxJKs8a3sVfI4t6CnrQzvJbB n6gxdgCu5i29J1QCYrCYvql2UyFPAK+do99/1jOXT4m2836j1wARAQABzSBQYXVsIEVnZ2Vy dCA8ZWdnZXJ0QGNzLnVjbGEuZWR1PsLBfgQTAQIAKAUCTIByZAIbAwUJEswDAAYLCQgHAwIG FQgCCQoLBBYCAwECH In-Reply-To: <5b3be3f4-d438-166a-863a-587c880584c8@cs.ucla.edu> Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 131.179.128.68 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:228075 Archived-At: This is a multi-part message in MIME format. --------------8BCA8F2B49317F2F2A3EB87E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On July 22 I wrote: > I see three ways to go here. > > 1. We change eql, memql, sxhash-eql, etc. to treat all NaNs alike, so that there > is just one NaN value from the Lisp point of view. We also change 'format', > 'copysign' etc. to ignore a NaN's sign, and look through any other use of > floating-point values to make sure that NaN signs are ignored. > > 2. We change eql, memql, sxhash-eql, etc. so that only the sign (not the > significand) of a NaN is looked at, so that there are just two NaN values from > the Lisp point of view. > > 3. We alter 'read', 'format' etc. to read and generate NaN significands. For > example, (format "%s" NaN) could return "0.1e+NaN" if the significand's > low-order bit was set. > > (1) sounds too drastic, as the sign of a NaN can be useful in some cases and > Emacs has long provided for obtaining the sign of a NaN. Although either (2) or > (3) would be OK, I'm inclined to go for (3) as I expect it would be a bit > cleaner and more useful. No further comment on this, and I went with (3) and installed into master the attached patch to do something along those lines on platforms that have ieee754.h (mostly GNUish platforms, I expect). Other platforms are unaffected. --------------8BCA8F2B49317F2F2A3EB87E Content-Type: text/x-patch; name="0001-Read-and-print-NaN-significand-if-ieee754.h.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-Read-and-print-NaN-significand-if-ieee754.h.patch" >From 6e37d2fd05bea373c472af1c6e80238ace5e1c94 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 1 Aug 2018 00:49:39 -0700 Subject: [PATCH] Read and print NaN significand if * configure.ac: Check for ieee754.h. * doc/lispref/numbers.texi (Float Basics): Document that NaN string representation digits are machine-dependent. * etc/NEWS: Mention the change. * src/lread.c, src/print.c [HAVE_IEEE754_H]: Include ieee754.h. * src/lread.c (string_to_number) [HAVE_IEEE754_H]: * src/print.c (float_to_string) [HAVE_IEEE754_H]: Read and print NaN significand. --- configure.ac | 1 + doc/lispref/numbers.texi | 2 +- etc/NEWS | 3 +++ src/lread.c | 11 +++++++++++ src/print.c | 11 +++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index b691867..dbdcce7 100644 --- a/configure.ac +++ b/configure.ac @@ -1668,6 +1668,7 @@ AC_DEFUN dnl checks for header files AC_CHECK_HEADERS_ONCE( + ieee754.h linux/fs.h malloc.h sys/systeminfo.h diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi index 14d5059..a3317c9 100644 --- a/doc/lispref/numbers.texi +++ b/doc/lispref/numbers.texi @@ -241,7 +241,7 @@ Float Basics NaNs carry a sign and a significand, and non-numeric functions treat two NaNs as equal when their signs and significands agree. Significands of NaNs are -machine-dependent and are not directly visible to Emacs Lisp. +machine-dependent, as are the digits in their string representation. When NaNs and signed zeros are involved, non-numeric functions like @code{eql}, @code{equal}, @code{sxhash-eql}, @code{sxhash-equal} and diff --git a/etc/NEWS b/etc/NEWS index f1ea835..9e7a765 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -880,6 +880,9 @@ Formerly, some of these functions ignored signs and significands of NaNs. Now, all these functions treat NaN signs and significands as significant. For example, (eql 0.0e+NaN -0.0e+NaN) now returns nil because the two NaNs have different signs; formerly it returned t. +Also, on platforms that have Emacs now reads and prints +NaN significands; e.g., if X is a NaN, (format "%s" X) now returns +"0.0e+NaN", "1.0e+NaN", etc., depending on X's significand. +++ ** The function 'make-string' accepts an additional optional argument. diff --git a/src/lread.c b/src/lread.c index 50fc6ef..290b0f6 100644 --- a/src/lread.c +++ b/src/lread.c @@ -72,6 +72,10 @@ along with GNU Emacs. If not, see . */ #define file_tell ftell #endif +#if HAVE_IEEE754_H +# include +#endif + /* The objects or placeholders read with the #n=object form. A hash table maps a number to either a placeholder (while the @@ -3757,8 +3761,15 @@ string_to_number (char const *string, int base, int flags) { state |= E_EXP; cp += 3; +#if HAVE_IEEE754_H + union ieee754_double u + = { .ieee_nan = { .exponent = -1, .quiet_nan = 1, + .mantissa0 = n >> 31 >> 1, .mantissa1 = n }}; + value = u.d; +#else /* NAN is a "positive" NaN on all known Emacs hosts. */ value = NAN; +#endif } else cp = ecp; diff --git a/src/print.c b/src/print.c index da6ec1a..add2160 100644 --- a/src/print.c +++ b/src/print.c @@ -40,6 +40,10 @@ along with GNU Emacs. If not, see . */ #include #include +#if HAVE_IEEE754_H +# include +#endif + #ifdef WINDOWSNT # include /* for F_DUPFD_CLOEXEC */ #endif @@ -1011,6 +1015,12 @@ float_to_string (char *buf, double data) } if (isnan (data)) { +#if HAVE_IEEE754_H + union ieee754_double u = { .d = data }; + uprintmax_t hi = u.ieee_nan.mantissa0; + return sprintf (buf, &"-%"pMu".0e+NaN"[!u.ieee_nan.negative], + (hi << 31 << 1) + u.ieee_nan.mantissa1); +#else /* Prepend "-" if the NaN's sign bit is negative. The sign bit of a double is the bit that is 1 in -0.0. */ static char const NaN_string[] = "0.0e+NaN"; @@ -1029,6 +1039,7 @@ float_to_string (char *buf, double data) strcpy (buf + negative, NaN_string); return negative + sizeof NaN_string - 1; +#endif } if (NILP (Vfloat_output_format) -- 2.7.4 --------------8BCA8F2B49317F2F2A3EB87E--