This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Do C++ signed types have modulo semantics?
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Cc: Michael Veksler <VEKSLER at il dot ibm dot com>, Daniel Berlin <dberlin at dberlin dot org>,Mark Mitchell <mark at codesourcery dot com>, Paul Koning <pkoning at equallogic dot com>,gcc mailing list <gcc at gcc dot gnu dot org>
- Date: Tue, 28 Jun 2005 14:27:02 +0100
- Subject: Re: Do C++ signed types have modulo semantics?
- References: <OF0E4366CA.5D54868B-ON4325702E.0036D351-4325702E.003973C5@il.ibm.com> <m3k6keeibo.fsf@uniton.integrable-solutions.net>
Gabriel Dos Reis wrote:
Michael Veksler <VEKSLER@il.ibm.com> writes:
[...]
| The code is not very simple, and different codes will get optimized
| differently.
| The user will have to learn how to write this piece of code differently for
| each
| processor to have best results.
|
| int wrap_sum(int a, int b)
| {
| if ( (a<0) != (b<0))
| return a+b; // Different sign, no overflow possible.
| unsigned sum = (unsigned) a + b;
| if (sum <= MAX_INT)
| return sum;
| sum -= MIN_INT;
| if (sum > MAX_INT) // can be eliminated for 2's complement
| abort(); // oops
| return (int)sum + MIN_INT;
| }
|
| It does not look too good optimization-wise.
Thanks for providing this example. This is the sort of thing I
faced. With pipeline process, it does harm.
Please explain why
int wrap_sum (int a, int b)
{
return (int) ((unsigned)a + (unsigned)b));
}
is unacceptable (provided we're on the standard 2's complement machine where the
mapping between negative ints and unsigned is *implementation defined* to be the
sane mapping, which I might point out you've already assumed in the wrap_sum I
quoted).
nathan
and whoever silently removed gcc@gcc.gnu.org, shame on you
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk