From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <3e1162e60712190715q3f50d601sd88b1e0c6c74afc8@mail.gmail.com> Date: Wed, 19 Dec 2007 07:15:09 -0800 From: "David Leimbach" To: 9fans@cse.psu.edu MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_15451_9062593.1198077309064" Subject: [9fans] fun and scary evil C code Topicbox-Message-UUID: 1ca47a20-ead3-11e9-9d60-3106f5b1d025 ------=_Part_15451_9062593.1198077309064 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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. ------=_Part_15451_9062593.1198077309064 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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.

------=_Part_15451_9062593.1198077309064--