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
DEBUGFLAGS = -g -DDEBUG $(FLAGS)
SHIPFLAGS = -O2 -DNDEBUG -march=native $(FLAGS)
SHIPFLAGS = -O3 -DNDEBUG $(FLAGS)
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)
{
*oob = 0;
double d;
union {
double d;
int64_t i64;
} u;
numerictype_t nt;
size_t i, len;
cvalue_t *cv;
@ -292,8 +295,8 @@ static uptrint_t bounded_hash(value_t a, int bound, int *oob)
switch(tg) {
case TAG_NUM :
case TAG_NUM1:
d = numval(a);
return doublehash(*(int64_t*)&d);
u.d = (double)numval(a);
return doublehash(u.i64);
case TAG_FUNCTION:
if (uintval(a) > N_BUILTINS)
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);
data = cp_data(cp);
nt = cp_numtype(cp);
d = conv_to_double(data, nt);
return doublehash(*(int64_t*)&d);
u.d = conv_to_double(data, nt);
return doublehash(u.i64);
case TAG_CVALUE:
cv = (cvalue_t*)ptr(a);
data = cv_data(cv);

View File

@ -52,12 +52,14 @@ extern int64_t llabs(int64_t j);
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;
if (a == b)
return 1;
aint = *(int64_t*)&a;
bint = *(int64_t*)&b;
ua.d = a; aint = ua.i;
ub.d = b; bint = ub.i;
if (aint < 0)
aint = BIT63 - aint;
if (bint < 0)
@ -72,12 +74,14 @@ int dbl_equals(double a, double 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;
if (a == b)
return 1;
aint = *(int32_t*)&a;
bint = *(int32_t*)&b;
ua.f = a; aint = ua.i;
ub.f = b; bint = ub.i;
if (aint < 0)
aint = BIT31 - aint;
if (bint < 0)