This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: signed is undefined and has been since 1992 (in GCC)
- From: Michael Veksler <VEKSLER at il dot ibm dot com>
- To: Robert Dewar <dewar at adacore dot com>
- Cc: "'Andrew Haley'" <aph at redhat dot com>, Dave Korn <dave dot korn at artimi dot com>, Olivier Galibert <galibert at pobox dot com>, "'gcc mailing list'" <gcc at gcc dot gnu dot org>, "'Gabriel Dos Reis'" <gdr at integrable-solutions dot net>, "'Andrew Pinski'" <pinskia at physics dot uc dot edu>
- Date: Wed, 29 Jun 2005 00:53:33 +0300
- Subject: Re: signed is undefined and has been since 1992 (in GCC)
Robert Dewar wrote on 28/06/2005 22:20:56:
> I am puzzled, why would *ANYONE* who knows C use int
> rather than unsigned if they want wrap around semantics?
>
int saturated_mul(int a, int b)
{
int ret= a*b;
if (a == 0 || ret % a == 0)
return ret;
else if ( (a<0) == (b<0) ) // same sign
return MAX_INT;
else
return MIN_INT;
}
instead of the bizzare:
int saturated_mul(int a, int b)
{
// (unsigned)a*b does not give easy ways to
// find signed overflow on negatives
if (a==0 || b==0)
return 0;
bool positive = ( (a<0) == (b<0) );
if (positive)
{
unsigned u_a=a, u_b=b;
if (a < 0)
{
u_a *= -1;
u_b *= -1;
}
unsigned ret= u_a*u_b;
if (ret % u_a == 0)
return ret;
else
return MAX_INT;
}
else
{
// negative
even more bizzare code;
}
It would be really great the standard could
make my life easier. I need to code to standard,
not to gcc, so extending gcc may not help....
Michael