From 08787a01cdd4103d1e5efa34b661b524651ec538 Mon Sep 17 00:00:00 2001 From: JeffBezanson Date: Wed, 29 Jul 2009 02:32:23 +0000 Subject: [PATCH] fixing use of an undefined behavior that was breaking compiles occasionally --- femtolisp/Makefile | 2 +- femtolisp/equal.c | 13 ++++++++----- llt/fp.c | 12 ++++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/femtolisp/Makefile b/femtolisp/Makefile index 76eaea0..dfa0ea1 100644 --- a/femtolisp/Makefile +++ b/femtolisp/Makefile @@ -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 diff --git a/femtolisp/equal.c b/femtolisp/equal.c index 00cfa2d..ef92f53 100644 --- a/femtolisp/equal.c +++ b/femtolisp/equal.c @@ -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); diff --git a/llt/fp.c b/llt/fp.c index ffa1da9..12731fc 100644 --- a/llt/fp.c +++ b/llt/fp.c @@ -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)