unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Jozef Chraplewski <jozef@applicake.com>
Cc: 13031@debbugs.gnu.org
Subject: bug#13031: large numbers
Date: Wed, 05 Dec 2012 11:20:41 -0500	[thread overview]
Message-ID: <87a9tsv5ba.fsf@tines.lan> (raw)
In-Reply-To: <87ip8ivflv.fsf@tines.lan> (Mark H. Weaver's message of "Mon, 03 Dec 2012 19:13:48 -0500")

I wrote:
> I guess that on your system, (* 65536 65536) evaluates to 0.
> Is that right?

I should have mentioned that if you're on a 64-bit system, then it may
instead be the case that (* (expt 2 32) (expt 2 32)) evaluates to 0.
Same bug either way, and the rest of my previous email still applies.

    Thanks,
      Mark


> If so, I believe the problem is caused by an aggressive optimization in
> recent versions of Clang, which breaks Guile's logic for detecting
> overflow when multiplying two fixnums.
>
> Currently, Guile computes kk = xx * yy and checks for overflow by
> verifying that kk / xx == yy.
>
> I believe that Clang is optimizing out the check, because recent C
> standards permit C implementations to assume that signed integer
> arithmetic will never overflow.  For details, see:
> http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
>
> One solution is to compile with the "-fwrapv" option, which should
> disable the optimization.
>
> Another solution is to apply the following patch.
>
> Jozef, would you be willing to test this patch and tell me if it fixes
> the problem?
>
>    Many thanks,
>       Mark
>
>
>
> diff --git a/libguile/numbers.c b/libguile/numbers.c
> index 52e227f..66c95db 100644
> --- a/libguile/numbers.c
> +++ b/libguile/numbers.c
> @@ -7640,10 +7640,16 @@ scm_product (SCM x, SCM y)
>        if (SCM_LIKELY (SCM_I_INUMP (y)))
>  	{
>  	  scm_t_inum yy = SCM_I_INUM (y);
> -	  scm_t_inum kk = xx * yy;
> -	  SCM k = SCM_I_MAKINUM (kk);
> -	  if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
> -	    return k;
> +#if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
> +          scm_t_int64 kk = xx * (scm_t_int64) yy;
> +          if (SCM_FIXABLE (kk))
> +            return SCM_I_MAKINUM (kk);
> +#else
> +          scm_t_inum axx = (xx > 0) ? xx : -xx;
> +          scm_t_inum ayy = (yy > 0) ? yy : -yy;
> +          if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
> +            return SCM_I_MAKINUM (xx * yy);
> +#endif
>  	  else
>  	    {
>  	      SCM result = scm_i_inum2big (xx);





  reply	other threads:[~2012-12-05 16:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-29  8:42 bug#13031: large numbers Jozef Chraplewski
2012-11-29 19:08 ` Noah Lavine
2012-11-29 19:15 ` Stefan Israelsson Tampe
2012-11-29 22:11 ` Ludovic Courtès
2012-11-30 11:54   ` Jozef Chraplewski
2012-11-30 16:27     ` Ludovic Courtès
2012-11-30 17:33     ` Mark H Weaver
2012-12-01  5:22 ` Mark H Weaver
2012-12-03 16:35   ` Jozef Chraplewski
2012-12-04  0:13     ` Mark H Weaver
2012-12-05 16:20       ` Mark H Weaver [this message]
2012-12-07 10:40         ` Jozef Chraplewski
2012-12-07 17:06           ` Mark H Weaver

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/guile/

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

  git send-email \
    --in-reply-to=87a9tsv5ba.fsf@tines.lan \
    --to=mhw@netris.org \
    --cc=13031@debbugs.gnu.org \
    --cc=jozef@applicake.com \
    /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.
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).