|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64142 dval to lval different behavior on ppc64
Submitted: 2013-02-04 14:15 UTC Modified: 2013-02-05 14:58 UTC
From: Assigned: remi
Status: Closed Package: *General Issues
PHP Version: 5.4.11 OS: GNU/Linux
Private report: No CVE-ID:
 [2013-02-04 14:15 UTC]
zend_dval_to_lval have different result on x86_64 and ppc64 (and probably other arch)

This cause some test failure:
Test & operator : 64bit long tests [tests/lang/operators/bitwiseAnd_basiclong_64bit.phpt]
Test ~N operator : 64bit long tests [tests/lang/operators/bitwiseNot_basiclong_64bit.phpt]
Test | operator : 64bit long tests [tests/lang/operators/bitwiseOr_basiclong_64bit.phpt]
Test ^ operator : 64bit long tests [tests/lang/operators/bitwiseXor_basiclong_64bit.phpt]
Test % operator : 64bit long tests [tests/lang/operators/modulus_basiclong_64bit.phpt]
Test decbin function : 64bit long tests [ext/standard/tests/math/decbin_basiclong_64bit.phpt]
Test dechex function : 64bit long tests [ext/standard/tests/math/dechex_basiclong_64bit.phpt]
Test decoct function : 64bit long tests [ext/standard/tests/math/decoct_basiclong_64bit.phpt]
Test chunk_split() function : usage variations - unexpected values for 'chunklen' argument(Bug#42796) [ext/standard/tests/strings/chunk_split_variation2.phpt]

Test script:
php -r 'printf("%ld\n", 0x7fffffffffffffff);'
php -r 'printf("%ld\n", 0x7fffffffffffffff+1);'

Expected result:

Actual result:


0001-Fixed-bug-64142-dval-to-lval-different-behavior-on-p.patch (last revision 2013-02-04 14:30 UTC) by

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-04 14:15 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: remi
 [2013-02-04 14:30 UTC]
The following patch has been added/updated:

Patch Name: 0001-Fixed-bug-64142-dval-to-lval-different-behavior-on-p.patch
Revision:   1359988217
 [2013-02-04 14:32 UTC]
It seems I cannot commit this fix (You have insufficient Karma!)

No regression detection on x86_64 (no failed test)
On ppc64 we now have only 16 failed tests (instead of 25)
 [2013-02-04 14:32 UTC]
-Status: Assigned +Status: Open -Assigned To: remi +Assigned To:
 [2013-02-05 01:16 UTC]
You should be able to commit it now
 [2013-02-05 01:16 UTC]
-Assigned To: +Assigned To: remi
 [2013-02-05 07:19 UTC]
-Status: Assigned +Status: Closed
 [2013-02-05 07:19 UTC]
Automatic comment on behalf of remi
Log: Fixed bug #64142 (dval to lval different behavior on ppc64)
 [2013-02-05 11:05 UTC]
The commit message says:
> On x86_64:
> (long)(double)9223372036854775807+1 = -9223372036854775808
> On ppc64
> (long)(double)9223372036854775807-1 = 9223372036854775807

How much is (long)(double)0x7fffffffffffffffL in that arch? Is it 0x7ffffffffffffffeL ?

The closest doubles near 0x7fffffffffffffff are 0x8000000000000000 and 0x7ffffffffffffffe. So basically ppp64 rounds down while x64 rounds up? If that's the case, there's nothing to "fix" in the code; you'll presumably get this behavior in a lot of other places, not just when casting LONG_MAX to double.

But in any case, I don't see how that is relevant to the patch. The patch changes a (long)dval cast to (long)(unsigned long)dval cast, which is a double-to-long cast, not a long-to-double cast.
 [2013-02-05 12:32 UTC]
> (long)(double)9223372036854775807-1 = 9223372036854775807

Ah sorry, I read that wrong. It can't obviously be rounding down, because the result seems larger; you're subtracting one here when you're adding 1 on top... The part about the closes doubles is wrong too, they're already spaced by 1024 by then.

In any case, your explanation in the commit seems both irrelevant and wrong. AS to the wrong part, in x86_64:

    double dd = 9223372036854775807;
    printf("%ld\n", ((long)dd) + 1);



So the -9223372036854775808 result is just an artifact of the compiler doing away with the casts completely.
 [2013-02-05 12:37 UTC]
BTW, that's with optimization turned off. If I pass -O3 to gcc , it's still able to figure out it doesn't need to cast anything and the output is -9223372036854775808.

In any case, we should get to the bottom of this. I very much doubt the patch is wrong, but if we figure out the underling cause we may find out the problem is larger.
 [2013-02-05 14:58 UTC]
@cataphract see my latest message on php-cvs ML.

Really strange. C Test (with -O2) give the same result on x86_64 and ppc64 and seems to prove the fix is not needed.

No I need to understand why it improves the PHP result.

I can keep the test builder for some time, so will investigate on this...
 [2013-02-05 14:58 UTC]
-Status: Closed +Status: Assigned
 [2013-02-11 08:14 UTC]
-Status: Assigned +Status: Closed
 [2013-02-11 08:14 UTC]
Automatic comment on behalf of remi
Log: Fixed bug #64142 (dval to lval different behavior on ppc64)
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC