Skip to content

Commit

Permalink
issue 3632 - modify float is float to do a bitwise compare
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jun 26, 2011
1 parent 9b50184 commit 0d93cf4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/constfold.c
Expand Up @@ -866,8 +866,27 @@ Expression *Identity(enum TOK op, Type *type, Expression *e1, Expression *e2)
}
else
{
return Equal((op == TOKidentity) ? TOKequal : TOKnotequal,
type, e1, e2);
if (e1->type->isreal())
{
real_t v1 = e1->toReal();
real_t v2 = e2->toReal();
cmp = !memcmp(&v1, &v2, sizeof(real_t));
}
else if (e1->type->isimaginary())
{
real_t v1 = e1->toImaginary();
real_t v2 = e2->toImaginary();
cmp = !memcmp(&v1, &v2, sizeof(real_t));
}
else if (e1->type->iscomplex())
{
complex_t v1 = e1->toComplex();
complex_t v2 = e2->toComplex();
cmp = !memcmp(&v1, &v2, sizeof(complex_t));
}
else
return Equal((op == TOKidentity) ? TOKequal : TOKnotequal,
type, e1, e2);
}
if (op == TOKnotidentity)
cmp ^= 1;
Expand Down
2 changes: 1 addition & 1 deletion src/e2ir.c
Expand Up @@ -2444,7 +2444,7 @@ elem *IdentityExp::toElem(IRState *irs)

//printf("IdentityExp::toElem() %s\n", toChars());

if (t1->ty == Tstruct)
if (t1->ty == Tstruct || t1->isfloating())
{ // Do bit compare of struct's
elem *es1;
elem *es2;
Expand Down
18 changes: 18 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -3011,6 +3011,23 @@ void test153()
static assert(!__traits(isStaticFunction, bar));
}

/***************************************************/
// 3632


void test154() {
float f;
assert(f is float.init);
double d;
assert(d is double.init);
real r;
assert(r is real.init);

assert(float.nan is float.nan);
assert(double.nan is double.nan);
assert(real.nan is real.nan);
}

/***************************************************/

int main()
Expand Down Expand Up @@ -3168,6 +3185,7 @@ int main()
test151();
test152();
test153();
test154();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 0d93cf4

Please sign in to comment.