unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Toru TSUNEYOSHI <t_tuneyosi@hotmail.com>
To: eliz@gnu.org
Cc: emacs-devel@gnu.org
Subject: Re: macro FIXNUM_OVERFLOW_P in lisp.h is valid ?
Date: Sun, 25 Oct 2009 03:45:39 +0900	[thread overview]
Message-ID: <BLU0-SMTP841B3A880E0081831C2D57E2BC0@phx.gbl> (raw)
In-Reply-To: <83my3heylo.fsf@gnu.org>

[-- Attachment #1: Type: Text/Plain, Size: 1425 bytes --]

Thanks for your checking.

From: Eli Zaretskii <eliz@gnu.org>

> OK, but please make your patch compare against MOST_POSITIVE_FIXNUM,
> instead of using INT_MAX or LONG_MAX.  Also, if you know that the
> value does not overflow an EMACS_INT, you can simply call make_number,
> instead of make_fixnum_or_float.

I revised the patch as you wrote.
Would you like to check it?

;; supplement
;; check list (with results on 32-bit platforms)

(string-to-number "268435455")		; 2^28 - 1
=>		   268435455
(string-to-number "268435456")		; 2^28
=>		   268435456.0
(string-to-number "268435457")		; 2^28 + 1
=>		   268435457.0
(string-to-number "-268435456")		; - 2^28
=>		   -268435456
(string-to-number "-268435457")		; - 2^28 - 1
=>		   -268435457.0
(string-to-number "536870911")		; 2^29 - 1
=>		   536870911.0
(string-to-number "1073741822")		; 2^30 - 1
=>		   1073741822.0
(string-to-number "2147483647")		; 2^31 - 1
=>		   2147483647.0
(string-to-number "1152921504606846975") ; 2^60 - 1
=>		  1.1529215046068467e+018
(string-to-number "1152921504606846976") ; 2^60
=>		  1.1529215046068467e+018
(string-to-number "1152921504606846977") ; 2^60 + 1
=>		  1.1529215046068467e+018
(string-to-number "-1152921504606846976") ; - 2^60
=>		  -1.1529215046068467e+018
(string-to-number "-1152921504606846977") ; - 2^60 - 1
=>		  -1.1529215046068467e+018
(string-to-number "9223372036854775807") ; 2^63 - 1
=>		  9.223372036854778e+018

[-- Attachment #2: data.c.diff --]
[-- Type: Text/X-Patch, Size: 1055 bytes --]

--- data.c.orig	2009-06-21 13:38:14.000000000 +0900
+++ data.c	2009-10-25 03:35:54.414697700 +0900
@@ -2392,17 +2392,43 @@
     val = make_float (sign * atof (p));
   else
     {
-      double v = 0;
+      unsigned char *old_p = p;
+      EMACS_INT most_fixnum, chknum, v = 0;
+      int overflow = 0;		/* most_fixnum overflow */
 
-      while (1)
+      most_fixnum = sign > 0 ? MOST_POSITIVE_FIXNUM : MOST_NEGATIVE_FIXNUM * -1;
+      chknum = most_fixnum / b;
+
+      while (v >= 0)
 	{
 	  int digit = digit_to_number (*p++, b);
 	  if (digit < 0)
 	    break;
+	  if (chknum < v / b)
+	    {
+	      overflow = 1;
+	      break;
+	    }
 	  v = v * b + digit;
 	}
 
-      val = make_fixnum_or_float (sign * v);
+      if (!overflow && v <= most_fixnum && v >= 0)
+	val = make_number (sign * v);
+      else
+	{
+	  double w = 0;
+	  p = old_p;
+
+	  while (1)
+	    {
+	      int digit = digit_to_number (*p++, b);
+	      if (digit < 0)
+		break;
+	      w = w * b + digit;
+	    }
+
+	  val = make_float (sign * w);
+	}
     }
 
   return val;

  reply	other threads:[~2009-10-24 18:45 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-23 17:51 macro FIXNUM_OVERFLOW_P in lisp.h is valid ? Toru TSUNEYOSHI
2009-10-23 19:33 ` Eli Zaretskii
2009-10-24  1:50   ` Toru TSUNEYOSHI
2009-10-24  9:05     ` Eli Zaretskii
2009-10-24 11:59       ` Toru TSUNEYOSHI
2009-10-24 13:00         ` Eli Zaretskii
2009-10-24 18:45           ` Toru TSUNEYOSHI [this message]
2009-10-24 15:14         ` Andreas Schwab
2009-10-24 18:39           ` Stefan Monnier
2009-10-26 14:48           ` Toru TSUNEYOSHI
     [not found]   ` <20091024.105033.100383844.t_tuneyosi@hotmail.com>
2009-10-24  6:07     ` Toru TSUNEYOSHI
     [not found]     ` <20091024.150744.186061320.t_tuneyosi@hotmail.com>
2009-10-24  7:46       ` Toru TSUNEYOSHI
2009-10-24 10:11         ` Eli Zaretskii
2009-10-23 20:57 ` Andreas Schwab
2009-10-23 22:02   ` Eli Zaretskii
2009-10-24  1:05     ` Stefan Monnier
2009-10-24  9:15       ` Eli Zaretskii
2009-10-24  9:40         ` Andreas Schwab
2009-10-24 10:16           ` Eli Zaretskii
2009-10-24 10:01         ` Andreas Schwab
2009-10-23 21:14 ` Stefan Monnier
2009-10-25  8:51   ` Toru TSUNEYOSHI
     [not found]   ` <20091025.175131.55657724.t_tuneyosi@hotmail.com>
2009-10-25 11:30     ` Toru TSUNEYOSHI

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BLU0-SMTP841B3A880E0081831C2D57E2BC0@phx.gbl \
    --to=t_tuneyosi@hotmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).