[Python-3000-checkins] r59457 - in python/branches/py3k: Include/floatobject.h Lib/test/test_float.py Makefile.pre.in Misc/NEWS Objects/doubledigits.c Objects/floatobject.c PCbuild/pythoncore.vcproj PCbuild8/pythoncore/pythoncore.vcproj PCbuild9/pythoncore.vcproj

Christian Heimes lists at cheimes.de
Tue Dec 11 03:57:06 CET 2007


Neil Schemenauer wrote:
> Basically, write a little C program that exits with 0 or 1 depending
> on double endian-ness.

Here is a medium sized C program. It can probably be coded in a fraction
of the size but this way everybody can understand it. In the case it's
too long for your taste just check if the first or last bit of an
(unsigned int64*)&((double)-1) is 1.

#define BIAS 1023
typedef struct {
	unsigned int  sign :  1;
	unsigned int   exp : 11;
	unsigned int    m1 :  4;
	unsigned int    m2 : 16;
	unsigned int    m3 : 16;
	unsigned int    m4 : 16;
} be_ieee_dbl;

typedef struct {
	unsigned int    m4 : 16;
	unsigned int    m3 : 16;
	unsigned int    m2 : 16;
	unsigned int    m1 :  4;
	unsigned int   exp : 11;
	unsigned int  sign :  1;
} le_ieee_dbl;

/* 1 = little endian IEEE double
 * 2 = big endian IEEE double
 * 0 = error
 */
int main(void)
{
	double dbl = -9.;
	le_ieee_dbl *le = (le_ieee_dbl*)&dbl;
	be_ieee_dbl *be = (be_ieee_dbl*)&dbl;
	if (le->sign == 1 && le->exp-BIAS == 3 && le->m1 == 2 &&
	    le->m2 == 0 && le->m3 == 0 && le->m4 == 0) {
		return 1;
	}
        if (be->sign == 1 && be->exp-BIAS == 3 && be->m1 == 2 &&
	    be->m2 == 0 && be->m3 == 0 && be->m4 == 0) {
		return 2;
	}
	return 0;
}



More information about the Python-3000-checkins mailing list