This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Patch for locale/programs/3level.h


> From: Bruno Haible <haible@ilog.fr>
> Date: Mon, 6 Aug 2001 22:25:39 +0200 (CEST)
> 
> > We noticed a problem with ~0 nesting in 3level.h.  The following looks
> > more sane to me.
> >
> > -      if (lookup1 != ~((uint32_t) 0))
> > +      if (lookup1 != (uint32_t) ~0)
> 
> The values are not the same....
> The first value is definitely the intended one.

It's true that they're not the same, but surely the second value is
what is intended here.

Assuming a twos-complement platform, (~0) evaluates to -1, and hence
((uint32_t) ~0) evaluates to 0xFFFFFFFF regardless of int width.

Conversly, ~((uint32_t) 0) does not always evaluate to 0xFFFFFFF.  For
example, if int is 64 bits, it evaluates to -1, which is
0xFFFFFFFFFFFFFFFF (again, assuming twos-complement).

Here's a better patch:

-      if (lookup1 != ~((uint32_t) 0))
+      if (lookup1 != (uint32_t) -1)

(uint32_t) -1 is the usual way to write an all-one's pattern.
It has the somewhat-theoretical advantage of being portable to
ones-complement and to signed-magnitude hosts, whereas (uint32_t) ~0
is not portable to such hosts.  It has the more important advantage of
being the usual way to write an all-ones unsigned value, which means
it's easier for other people to understand the code.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]