Re: Type-punning / casting problem
Hi James, thanks for your reply.
James Kanze wrote:
On Sep 14, 9:02 pm, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
I have got confused by the rules about what won't work, what will work,
and what might work, when casting.
Does anyone know what the rules actually are?
The rules are that if the variable is declared as a float, the
only way you can access it is as a float, or as an array of char
or unsigned char. Anything else is unsigned behavior.
Thanks.
In practice, most compilers allow more; I think that g++
explicitly guarantees type punning with a union, and not a few
compilers guarantee it when a cast is used. Still, if you
really want to use htonl, the "correct" solution is to memcpy
the bytes of the float into a uint32_t, and call the function on
that. (Technically, even that can fail, but in practice, you're
probably safe.)
Right, I'll try that:
static inline float htonfloat(float f) {
uint32_t i;
memcpy(&i,&f,4);
i = htonl(i);
float r;
memcpy(&r,&i,4);
return r;
}
template <>
inline const char* encode_arg<float>(float& f) {
f = htonfloat(f);
return reinterpret_cast<const char*>(&f);
}
....it works! hurray!
(Yes, I could do it with just one memcpy, but I think they're optimised
away anyway.)
The "correct" way to stream a float, of course, is to extract
the exponent, sign and mantissa using functions like frexp and
ldexp. I've experimented with doing so; it's less complex than
it sounds, and at least on a Sun Sparc under Solaris, it's not
outrageously expensive in runtime either (which, I'll admit,
surprised me). My current code for writing a float, for
example, looks something like:
bool isNeg = source < 0 ;
if ( isNeg ) {
source = - source ;
}
int exp ;
if ( source == 0.0 ) {
exp = 0 ;
} else {
source = ldexp( frexp( source, &exp ), 24 ) ;
exp += 126 ;
}
unsigned long mant = source ;
dest.put( (isNeg ? 0x80 : 0x00) | exp >> 1 ) ;
dest.put( ((exp << 7) & 0x80) | ((mant >> 16) & 0x7F) ) ;
dest.put( mant >> 8 ) ;
dest.put( mant ) ;
Hmm. Interesting, but I think I prefer what I have above.
Regards,
Phil.