public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "marc.glisse at normalesup dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/47913] [C++0x] improve ratio_add to overflow less often
Date: Wed, 02 Mar 2011 10:00:00 -0000	[thread overview]
Message-ID: <bug-47913-4-oDqdpPUomm@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-47913-4@http.gcc.gnu.org/bugzilla/>

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47913

--- Comment #7 from Marc Glisse <marc.glisse at normalesup dot org> 2011-03-02 09:59:52 UTC ---
(In reply to comment #6)
> 1- Please make sure the code is minimally documented (are the comments in
> longlong.h enough?)

Ok, I wasn't sure it was worth it if the code was unlikely to ever make it.

> 2- I see stuff like __builtin_clzll(__d) on __d a uintmax_t, I'm not sure it's
> always ok, on any 32-bit and 64-bit target.

Do you have a better alternative in mind? Should I reimplement it using
templates? It shouldn't be too hard, but I would check on the gcc ML first.

> More generally - I'm asking to Marc the mathematician
> here, not Mark the libstdc++ contributor - do we have a clear characterization
> of which specific overflows can be avoided?

All of those where both the input and the output are legal std::ratio.

> Are we *really* sure the
> boost::rational implementation is equivalent to GCC and weaker than what you
> are proposing: the first time I looked into it I remember seeing a
> normalization happening earlier toward the end, per the last two lines of that
> comment:
> 
>  // Which proves that instead of normalizing the result, it is better to
>  // divide num and den by gcd((a*d1 + c*b1), g)

Boost isn't exactly equivalent to gcc. Making a mix of their ratio and rational
(and possibly extrapolating a bit), they avoid some overflows of the numerator
by factoring out the gcd of the numerators, and they avoid all overflows of the
denominator by reducing the numerator with the gcd of the denominators so they
can compute directly the right denominator. They still fail on 2 types of
numerator overflow, either when the numerator is too large before
simplification with the gcd, or when the 2 products are large but their sum is
small. The example at the end of the attached file shows the second case.


  parent reply	other threads:[~2011-03-02 10:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-27 13:23 [Bug libstdc++/47913] New: " marc.glisse at normalesup dot org
2011-02-27 14:13 ` [Bug libstdc++/47913] " paolo.carlini at oracle dot com
2011-02-27 19:33 ` marc.glisse at normalesup dot org
2011-02-27 23:38 ` paolo.carlini at oracle dot com
2011-02-28  1:33 ` paolo.carlini at oracle dot com
2011-03-01 22:16 ` marc.glisse at normalesup dot org
2011-03-01 23:00 ` paolo.carlini at oracle dot com
2011-03-02 10:00 ` marc.glisse at normalesup dot org [this message]
2011-03-02 10:59 ` paolo.carlini at oracle dot com
2011-03-02 11:50 ` marc.glisse at normalesup dot org
2011-03-02 11:54 ` marc.glisse at normalesup dot org
2011-03-02 11:59 ` paolo.carlini at oracle dot com
2011-03-02 12:07 ` paolo.carlini at oracle dot com
2011-03-02 14:05 ` marc.glisse at normalesup dot org
2011-03-02 14:15 ` paolo.carlini at oracle dot com
2011-03-02 14:58 ` paolo at gcc dot gnu.org
2011-03-02 14:59 ` paolo.carlini at oracle dot com
2011-03-02 20:50 ` marc.glisse at normalesup dot org
2011-03-02 23:21 ` paolo.carlini at oracle dot com
2011-03-03  6:41 ` marc.glisse at normalesup dot org
2011-03-03  9:51 ` paolo.carlini at oracle dot com
2011-05-04 16:35 ` marc.glisse at normalesup dot org
2011-05-04 17:05 ` paolo.carlini at oracle dot com
2011-05-04 18:08 ` paolo.carlini at oracle dot com
2011-05-04 23:28 ` paolo at gcc dot gnu.org
2011-05-04 23:36 ` paolo.carlini at oracle dot com

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

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

  git send-email \
    --in-reply-to=bug-47913-4-oDqdpPUomm@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.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.
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).