rewrite AlmostEqualUlps() to avoid compiler warnings concerning `strict-aliasing'

This commit is contained in:
Henrik Forstén 2013-06-24 22:01:40 +02:00 committed by rlar
parent 97d99067a6
commit 9c04f01991
1 changed files with 9 additions and 2 deletions

View File

@ -24,6 +24,11 @@ bool AlmostEqualUlps(double A, double B, int maxUlps)
{
int64_t aInt, bInt, intDiff;
union {
double d;
int64_t i;
} uA, uB;
if (A == B)
return TRUE;
@ -34,12 +39,14 @@ bool AlmostEqualUlps(double A, double B, int maxUlps)
/* default NAN won't compare as equal to anything. */
assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
aInt = *(int64_t*)&A;
uA.d = A;
aInt = uA.i;
/* Make aInt lexicographically ordered as a twos-complement int */
if (aInt < 0)
aInt = int64_min - aInt;
bInt = *(int64_t*)&B;
uB.d = B;
bInt = uB.i;
/* Make bInt lexicographically ordered as a twos-complement int */
if (bInt < 0)
bInt = int64_min - bInt;