Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:65756 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57454 invoked from network); 10 Feb 2013 14:58:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Feb 2013 14:58:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:38141] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8C/76-20900-1A5B7115 for ; Sun, 10 Feb 2013 09:58:42 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id B57BF70003FA; Sun, 10 Feb 2013 14:58:38 +0000 (WET) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 0sIOP66s2HpD; Sun, 10 Feb 2013 14:58:38 +0000 (WET) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 2C28470003F7; Sun, 10 Feb 2013 14:58:37 +0000 (WET) Received: from damnation.nl.lo.geleia.net (unknown [IPv6:2001:470:94a2:4:21d:baff:feee:cc0b]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id A360B20071B4; Sun, 10 Feb 2013 14:58:33 +0000 (WET) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Remi Collet" , "Stas Malyshev" , "Gustavo Lopes" Cc: "PHP Internals" References: <511666DE.8080809@fedoraproject.org> <51172819.4000806@sugarcrm.com> <51174790.8040805@fedoraproject.org> <511751C5.1020906@fedoraproject.org> <51175BDD.9040705@sugarcrm.com> Date: Sun, 10 Feb 2013 15:58:28 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_I=2ES=2ET=2E?= Message-ID: In-Reply-To: User-Agent: Opera Mail/12.12 (Linux) Subject: Re: [PHP-DEV] double val to long val conversion issue From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Sun, 10 Feb 2013 12:00:01 +0100, Gustavo Lopes wrote: > My concern is that we may also have different behavior when we go > outside the unsigned long range. > > As to whether ppc64 or x86_64 is correct when casting (double)LONG_MAX > to a signed long, the answer is this that since the double is outside > long range, the behavior is undefined. If we want more predictability, > we have to do the conversion ourselves and probably take a small > performance hit. If we want to eliminate the undefined behavior, we should do (on archs with 64-bit longs): (long)(unsigned long)(fmod(d, pow(2., 64.))) Can you test this program on ppc64: #include #include long convert(double d) { return (long)(unsigned long)(fmod(d, pow(2., 64.))); } int main(int argc, char *argv[]) { double d; if (argc > 1 && sscanf(argv[1], "%lg", &d)) { printf("%ld %ld\n", convert(d), (long)(unsigned long)d); } return 0; } I get this: $ gcc -O3 -lm conv.c && ./a.out 9223372036854775808 -9223372036854775808 -9223372036854775808 $ gcc -O3 -lm conv.c && ./a.out 4e21 -2943463994972700672 0 $ gcc -O3 -lm conv.c && ./a.out 4e19 3106511852580896768 0 Which seems correct when verified with mathematica: In[9]:= c = Function[o, o // N // SetPrecision[#, \[Infinity]] & // Mod[#, 2^64] & // Piecewise[{{#, # < 2^63}}, -2^64 + #] &]; In[10]:= c /@ {2^63, 4*^21, 4*^19} Out[10]= {-9223372036854775808, -2943463994972700672, \ 3106511852580896768} The cast to long from unsigned long is still implementation-defined behavior, but all twos complement archs should work like this (i.e. don't change the representation). In any case, it's not undefined behavior. -- Gustavo Lopes