lib9: hide uvlong/double conversions to placate gcc (John Gosset)

This commit is contained in:
Russ Cox 2008-05-10 13:35:54 -04:00
parent ada24b4005
commit 66f6e2b651

View file

@ -6,6 +6,7 @@
*/ */
#include "plan9.h" #include "plan9.h"
#include <assert.h>
#include "fmt.h" #include "fmt.h"
#include "fmtdef.h" #include "fmtdef.h"
@ -13,31 +14,43 @@ static uvlong uvnan = ((uvlong)0x7FF00000<<32)|0x00000001;
static uvlong uvinf = ((uvlong)0x7FF00000<<32)|0x00000000; static uvlong uvinf = ((uvlong)0x7FF00000<<32)|0x00000000;
static uvlong uvneginf = ((uvlong)0xFFF00000<<32)|0x00000000; static uvlong uvneginf = ((uvlong)0xFFF00000<<32)|0x00000000;
/* gcc sees through the obvious casts. */
static uvlong
d2u(double d)
{
union {
uvlong v;
double d;
} u;
assert(sizeof(u.d) == sizeof(u.v));
u.d = d;
return u.v;
}
static double
u2d(uvlong v)
{
union {
uvlong v;
double d;
} u;
assert(sizeof(u.d) == sizeof(u.v));
u.v = v;
return u.d;
}
double double
__NaN(void) __NaN(void)
{ {
uvlong *p; return u2d(uvnan);
/* gcc complains about "return *(double*)&uvnan;" */
p = &uvnan;
return *(double*)p;
} }
int int
__isNaN(double d) __isNaN(double d)
{ {
/*
* Used to just say x = *(uvlong*)&d,
* but gcc miscompiles that!
*/
union {
uvlong i;
double f;
} u;
uvlong x; uvlong x;
u.f = d; x = d2u(d);
x = u.i;
/* IEEE 754: exponent bits 0x7FF and non-zero mantissa */ /* IEEE 754: exponent bits 0x7FF and non-zero mantissa */
return (x&uvinf) == uvinf && (x&~uvneginf) != 0; return (x&uvinf) == uvinf && (x&~uvneginf) != 0;
} }
@ -45,23 +58,15 @@ __isNaN(double d)
double double
__Inf(int sign) __Inf(int sign)
{ {
uvlong *p; return u2d(sign < 0 ? uvneginf : uvinf);
if(sign < 0)
p = &uvinf;
else
p = &uvneginf;
return *(double*)p;
} }
int int
__isInf(double d, int sign) __isInf(double d, int sign)
{ {
uvlong x; uvlong x;
double *p;
x = d2u(d);
p = &d;
x = *(uvlong*)p;
if(sign == 0) if(sign == 0)
return x==uvinf || x==uvneginf; return x==uvinf || x==uvneginf;
else if(sign > 0) else if(sign > 0)