fixing use of an undefined behavior that was breaking compiles occasionally

This commit is contained in:
JeffBezanson 2009-07-29 02:32:23 +00:00
parent ecfd81148f
commit 08787a01cd
3 changed files with 17 additions and 10 deletions

View File

@ -12,7 +12,7 @@ FLAGS = -falign-functions -Wall -Wextra -Wno-strict-aliasing -I$(LLTDIR) $(CFLAG
LIBS = $(LLT) -lm LIBS = $(LLT) -lm
DEBUGFLAGS = -g -DDEBUG $(FLAGS) DEBUGFLAGS = -g -DDEBUG $(FLAGS)
SHIPFLAGS = -O2 -DNDEBUG -march=native $(FLAGS) SHIPFLAGS = -O3 -DNDEBUG $(FLAGS)
default: release test default: release test

View File

@ -281,7 +281,10 @@ value_t equal(value_t a, value_t b)
static uptrint_t bounded_hash(value_t a, int bound, int *oob) static uptrint_t bounded_hash(value_t a, int bound, int *oob)
{ {
*oob = 0; *oob = 0;
union {
double d; double d;
int64_t i64;
} u;
numerictype_t nt; numerictype_t nt;
size_t i, len; size_t i, len;
cvalue_t *cv; cvalue_t *cv;
@ -292,8 +295,8 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
switch(tg) { switch(tg) {
case TAG_NUM : case TAG_NUM :
case TAG_NUM1: case TAG_NUM1:
d = numval(a); u.d = (double)numval(a);
return doublehash(*(int64_t*)&d); return doublehash(u.i64);
case TAG_FUNCTION: case TAG_FUNCTION:
if (uintval(a) > N_BUILTINS) if (uintval(a) > N_BUILTINS)
return bounded_hash(((function_t*)ptr(a))->bcode, bound, oob); return bounded_hash(((function_t*)ptr(a))->bcode, bound, oob);
@ -304,8 +307,8 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
cp = (cprim_t*)ptr(a); cp = (cprim_t*)ptr(a);
data = cp_data(cp); data = cp_data(cp);
nt = cp_numtype(cp); nt = cp_numtype(cp);
d = conv_to_double(data, nt); u.d = conv_to_double(data, nt);
return doublehash(*(int64_t*)&d); return doublehash(u.i64);
case TAG_CVALUE: case TAG_CVALUE:
cv = (cvalue_t*)ptr(a); cv = (cvalue_t*)ptr(a);
data = cv_data(cv); data = cv_data(cv);

View File

@ -52,12 +52,14 @@ extern int64_t llabs(int64_t j);
int dbl_equals(double a, double b) int dbl_equals(double a, double b)
{ {
union { double d; int64_t i; } ua;
union { double d; int64_t i; } ub;
int64_t aint, bint; int64_t aint, bint;
if (a == b) if (a == b)
return 1; return 1;
aint = *(int64_t*)&a; ua.d = a; aint = ua.i;
bint = *(int64_t*)&b; ub.d = b; bint = ub.i;
if (aint < 0) if (aint < 0)
aint = BIT63 - aint; aint = BIT63 - aint;
if (bint < 0) if (bint < 0)
@ -72,12 +74,14 @@ int dbl_equals(double a, double b)
int flt_equals(float a, float b) int flt_equals(float a, float b)
{ {
union { float f; int32_t i; } ua;
union { float f; int32_t i; } ub;
int32_t aint, bint; int32_t aint, bint;
if (a == b) if (a == b)
return 1; return 1;
aint = *(int32_t*)&a; ua.f = a; aint = ua.i;
bint = *(int32_t*)&b; ub.f = b; bint = ub.i;
if (aint < 0) if (aint < 0)
aint = BIT31 - aint; aint = BIT31 - aint;
if (bint < 0) if (bint < 0)