This is the mail archive of the
`gcc@gcc.gnu.org`
mailing list for the GCC project.

Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|

Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |

Other format: | [Raw text] |

*From*: Ulf Magnusson <ulfalizer at gmail dot com>*To*: Miles Bader <miles at gnu dot org>*Cc*: Pedro Pedruzzi <pedro dot pedruzzi at gmail dot com>, gcc at gcc dot gnu dot org*Date*: Thu, 6 Oct 2011 12:52:29 +0200*Subject*: Re: Option to make unsigned->signed conversion always well-defined?*References*: <CAFkk2KSn2Us=W2ER396pA+cbgW1HaTJWb-mLPiJVv4Xk0QJYuA@mail.gmail.com> <4E8CE072.8010109@gmail.com> <CAFkk2KQOAZQzORhRA87UUqcbiJmfd7sQbAcmk+EcfhJ7kmxgSQ@mail.gmail.com> <CAFkk2KQNOi2UoPPEWU+69uh0Ntno9J8Jyw7YRb3n2Zhge416DQ@mail.gmail.com> <buo62k2biev.fsf@dhlpc061.dev.necel.com>

On Thu, Oct 6, 2011 at 11:04 AM, Miles Bader <miles@gnu.org> wrote: > Ulf Magnusson <ulfalizer@gmail.com> writes: >> Might as well do >> >> bool overflowbit(unsigned int a, unsigned int b) { >> ? ? const unsigned int sum = a + b; >> ? ? return (a ^ b) & ~(a ^ sum) & 0x80; >> } >> >> But still not very good output compared to other approaches as expected. > > How about: > > ? bool overflowbit2(unsigned int a, unsigned int b) > ? { > ? ? ? const unsigned int sum = a + b; > ? ? ? return ~(a ^ b) & sum & 0x80; > ? } > > ? > > I thinnnnk it has the same results as your function... > [I just made a table of all 8 possibilities, and checked!] > > -miles > > -- > Circus, n. A place where horses, ponies and elephants are permitted to see > men, women and children acting the fool. > Ops, should have been return ~(a ^ b) & (a ^ sum) & 0x80 ~(a ^ b) gives 1 in the sign bit position if the signs are the same, and (a ^ sum) gives 1 if it's different in the sum. A clearer way of writing it (that also generates suboptimal code) is bool overflow(unsigned int a, unsigned int b) { const unsigned asign = a & 0x80; const unsigned bsign = b & 0x80; const unsigned sumsign = (a + b) & 0x80; return (asign == bsign) && (asign != sumsign); } Seems bit-fiddling isn't the way to go. Maybe I should take this to gnu-help as it isn't really development-related. /Ulf

**Follow-Ups**:**Re: Option to make unsigned->signed conversion always well-defined?***From:*Pedro Pedruzzi

**References**:**Option to make unsigned->signed conversion always well-defined?***From:*Ulf Magnusson

**Re: Option to make unsigned->signed conversion always well-defined?***From:*Pedro Pedruzzi

**Re: Option to make unsigned->signed conversion always well-defined?***From:*Ulf Magnusson

**Re: Option to make unsigned->signed conversion always well-defined?***From:*Ulf Magnusson

**Re: Option to make unsigned->signed conversion always well-defined?***From:*Miles Bader

Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|

Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |