Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81723 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27882 invoked from network); 3 Feb 2015 16:09:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2015 16:09:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.200 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.200 imap1-2.ox.privateemail.com Received: from [192.64.116.200] ([192.64.116.200:52702] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 03/42-20608-B92F0D45 for ; Tue, 03 Feb 2015 11:08:59 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 187FEB0008E; Tue, 3 Feb 2015 11:08:57 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id PwqdILprAN8x; Tue, 3 Feb 2015 11:08:56 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 27800B00085; Tue, 3 Feb 2015 11:08:56 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) In-Reply-To: Date: Tue, 3 Feb 2015 16:08:54 +0000 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <699DCF46-4DBC-4312-961F-31D36C078999@ajf.me> References: <1D8DC03D-0E2C-494C-9ED2-244334F57D8A@ajf.me> <6E8210CD-4BA3-4694-8482-4803F562D77C@ajf.me> <1BEEF374-D8BC-4FE2-8CE0-5EBB1AC4CF04@ajf.me> <3B265E0A-7479-456A-B134-5CA607C68EFF@ajf.me> <1470D29A-2D95-4E03-BEBC-CE1D9AD2FB16@ajf.me> To: Leigh X-Mailer: Apple Mail (2.2070.6) Subject: Re: [PHP-DEV] Zero-fill right shift. From: ajf@ajf.me (Andrea Faulds) > On 3 Feb 2015, at 15:12, Leigh wrote: >=20 > On 3 February 2015 at 15:02, Andrea Faulds wrote: >> Why would it be promoted?! The high bit is the 63rd bit. It fits = within a long. >>=20 >=20 > I'm assuming your bigint implementation would want to respect signage. >=20 > When does it promote? 63rd to preserve signage? >=20 > 4611686018427387904 // 1 << 62 - int > 9223372036854775808 // 1 << 63 - bigint > 18446744073709551616 // 1 << 64 - bigint >=20 > Or 64th to for complete madness? >=20 > 4611686018427387904 // 1 << 62 - int > -9223372036854775808 // 1 << 63 - int > 18446744073709551616 // 1 << 64 - bigint The specific code can be found here: = https://github.com/TazeTSchnitzel/php-src/blob/b91a80879ca3ba269bd239d9c82= 0003c83d0dbc1/Zend/zend_operators.c#L2265 The algorithm is fairly simple: if the number of bits in the long plus = the number of bits to shift is greater than the number of bits in a = zend_long, then it promotes. Thus: $ sapi/cli/php -r '$x =3D 1 << 62; debug_zval_dump($x);' long(4611686018427387904) $ sapi/cli/php -r '$x =3D 1 << 63; debug_zval_dump($x);' bigint(9223372036854775808) refcount(2) $ sapi/cli/php -r '$x =3D 1 << 64; debug_zval_dump($x);' bigint(18446744073709551616) refcount(2) I tried some negative numbers, but I=E2=80=99ve noticed that any left = shift on a negative integer promotes - that=E2=80=99s probably because = clz is including the high bit... that should probably be fixed, mostly = likely by doing clz on its absolute value in that case and factoring in = the high bit. -- Andrea Faulds http://ajf.me/