[llvm-commits] CVS: llvm/include/llvm/Support/MathExtras.h

Reid Spencer reid at x10sys.com
Wed Aug 17 16:33:05 PDT 2005


On Wed, 2005-08-17 at 17:46 -0500, Chris Lattner wrote:
> On Wed, 17 Aug 2005, Reid Spencer wrote:
> >> +// BitsToFloat - This function takes a 32-bit integer and returns the bit
> >> +// equivalent float.
> >> +inline float BitsToFloat(unsigned Bits) {
> >> +  union {
> >> +    unsigned I;
> >> +    float F;
> >> +  } T;
> >> +  T.I = Bits;
> >> +  return T.F;
> >> +}
> >
> > This one I'm not so sure about. "unsigned" could be 32 or 64-bits
> > depending on the platform. float is probably 32-bits on all platforms we
> > care about. If unsigned is 64-bits and big-endian, we would get the
> > wrong value for the float as it would be effectively shifted into the
> > high order bits. Truncation could occur too.
> >
> > Shouldn't this use uin32_t instead of "unsigned" ?
> 
> He's not making things any worse than they were before, but I don't see 
> any harm in changing it to uint32_t.  

True, he's not, but I agree that changing it to uint32_t would both do
no harm and potentially avoid problems down the road.

> LLVM has many assumptions that 
> int/unsigned are 32-bits. 

In many of those cases, the range of possible values makes the size of
unsigned irrelevant. Not so in this case.

>  Are you aware of any platform (that matters) where int is not 32-bits?

It generally depends on the compiler in use since the standard
recommends a size but does not prohibit a compiler from specifying an
"implementation dependent" size. One of the reasons we don't get
problems in this area is because we use GCC nearly everywhere and its
sizes are consistent from platform to platform. HOWEVER, we are also
currently supporting aCC on HP-UX and the opportunity for supporting
other compilers in the future exists. Of particular not, I believe this
code will break on a 64-bit HP-UX machine compiled with aCC where "int"
is 64-bits

Reid
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20050817/3e4fbea5/attachment.sig>


More information about the llvm-commits mailing list