Type-punning

Sergei Organov osv@javad.com
Fri Jun 22 20:09:00 GMT 2007


Chris Lattner <clattner@apple.com> writes:

> On Jun 22, 2007, at 1:41 AM, Sergei Organov wrote:
>
>> Herman Geza <hg211@hszk.bme.hu> writes:
>>
>> [...]
>>
>>> What is the correct way to do this:
>>>
>>> void setNaN(float &v) {
>>> 	reinterpret_cast<int&>(v) = 0x7f800001;
>>> }
>>>
>>> without a type-prunning warning?  I cannot use the union trick here
>>
>> Why? Won't the following work?
>>
>> void setNaN(float &v) {
>>   union { float f; int i; } t;
>>   t.i = 0x7f800001;
>>   v = t.f;
>> }
>
> __builtin_nan isn't sufficient?

... and there a also

 -- Function: double nan (const char *TAGP)
 -- Function: float nanf (const char *TAGP)
 -- Function: long double nanl (const char *TAGP)
     The `nan' function returns a representation of NaN, provided that
     NaN is supported by the target platform.  `nan
     ("N-CHAR-SEQUENCE")' is equivalent to `strtod
     ("NAN(N-CHAR-SEQUENCE)")'.

     The argument TAGP is used in an unspecified manner.  On IEEE 754
     systems, there are many representations of NaN, and TAGP selects
     one.  On other systems it may do nothing.

in GNU libc, and the manual says they are C99 standard functions.

-- Sergei.



More information about the Gcc mailing list