9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] fun and scary evil C code
@ 2007-12-19 15:15 David Leimbach
  2007-12-19 20:37 ` Russ Cox
  0 siblings, 1 reply; 11+ messages in thread
From: David Leimbach @ 2007-12-19 15:15 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 1037 bytes --]

I was amused by this:
http://www.steike.com/code/useless/evil-c/

I particularly liked the "internalEndianMagic".

I see this in some XML libs, as well as GMP and other open sourced code.

http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2005-12/msg00198.html


From:
https://svn.r-project.org/R/trunk/src/extra/trio/trionan.c

/*
 * Endian-agnostic indexing macro.
 *
 * The value of internalEndianMagic, when converted into a 64-bit
 * integer, becomes 0x0706050403020100 (we could have used a 64-bit
 * integer value instead of a double, but not all platforms supports
 * that type). The value is automatically encoded with the correct
 * endianess by the compiler, which means that we can support any
 * kind of endianess. The individual bytes are then used as an index
 * for the IEEE 754 bit-patterns and masks.
 */
#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
#endif

pretty weird stuff.

[-- Attachment #2: Type: text/html, Size: 2084 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: [9fans] fun and scary evil C code
@ 2007-12-20 16:29 Russ Cox
  0 siblings, 0 replies; 11+ messages in thread
From: Russ Cox @ 2007-12-20 16:29 UTC (permalink / raw)
  To: 9fans

> Does C99 or any other C mandate the actual memory layout
> of floats and doubles or the exact conversion of constant  
> representations?
> I'm fairly sure they somehow mandate IEEE 754 properties,
> but do they actually say that floats and doubles have to be stored  
> exactly that way in 4 or 8 bytes?
> 
> Even if we assume sizeof(double) == 8,
> what if my implementation is perverse and interleaves the exponent  
> bits amongst the mantissa bits?
> Where is this disallowed in the standard(s)?

It doesn't matter what the standard says; it matters what implementations do.
C implementations are going to provide what the underlying
hardware does, and almost all hardware does IEEE 754.

> as long as the conversion you're after has an exact ieee representation,
> i can't see how two compliant implementations could come up with
> differing representations.  (two different numbers can't have the same
> ieee representation, except -0 and +0.)  the conversion process doesn't
> need any floating point itself and the only interpolation comes when
> numbers don't have exact representations.

Historically, it has not always been true that decimal <-> binary
conversion of doubles has been precise enough to replicate a 
specific bit pattern.  While conversion doesn't require floating
point, it is often done in floating point anyway for convenience.

Russ


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-12-20 16:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-19 15:15 [9fans] fun and scary evil C code David Leimbach
2007-12-19 20:37 ` Russ Cox
2007-12-19 22:32   ` Robert William Fuller
2007-12-19 22:38     ` erik quanstrom
2007-12-19 22:51   ` Bakul Shah
2007-12-20  0:18   ` dave.l
2007-12-20  0:33     ` dave.l
2007-12-20  0:51       ` erik quanstrom
2007-12-20  9:29         ` roger peppe
2007-12-20 13:02           ` erik quanstrom
2007-12-20 16:29 Russ Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).