Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:22529 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 60377 invoked by uid 1010); 20 Mar 2006 08:22:29 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 60359 invoked from network); 20 Mar 2006 08:22:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Mar 2006 08:22:29 -0000 X-Host-Fingerprint: 210.55.31.88 mail.album.co.nz Linux 2.5 (sometimes 2.4) (4) Received: from ([210.55.31.88:48717] helo=www.album.co.nz) by pb1.pair.com (ecelerity 2.0 beta r(6323M)) with SMTP id D9/3E-55982-3466E144 for ; Mon, 20 Mar 2006 03:22:28 -0500 Received: by www.album.co.nz (Postfix, from userid 1003) id 8B01ED5F8D; Mon, 20 Mar 2006 20:22:23 +1200 (NZST) X-Spam-Checker-Version: SpamAssassin 3.1.0-gr0 (2005-09-13) on www.album.co.nz X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,UPPERCASE_25_50 autolearn=ham version=3.1.0-gr0 Received: from [192.168.0.9] (222-152-187-158.jetstream.xtra.co.nz [222.152.187.158]) by www.album.co.nz (Postfix) with ESMTP id 000F1D5F8A; Mon, 20 Mar 2006 20:22:21 +1200 (NZST) Message-ID: <441E663D.3050003@album.co.nz> Date: Mon, 20 Mar 2006 20:22:21 +1200 Organization: Album Limited User-Agent: Mail/News 1.5 (X11/20060319) MIME-Version: 1.0 To: Bruce CC: internals@lists.php.net References: <33a75ab80603191653tca7f454i8baa71015b5b0373@mail.gmail.com> In-Reply-To: <33a75ab80603191653tca7f454i8baa71015b5b0373@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] IntVal(float) Returns Different Values From: jasper@album.co.nz (Jasper Bryant-Greene) You want the php-general mailing list. This list is for discussing development of the PHP language and runtime... Jasper Bruce wrote: > I am having trouble porting some code originally written in Borland Delphi > to PHP. The Delphi code expects certain behavior on integer overflows that I > can only duplicate on some PHP systems. For example: > > $BB = -2181087916; > $AA = (int)$BB; > $AA = intval($BB); > > On some systems, $AA will be int(-2147483648), however, on most systems, $AA > will be int(2113879380), which is the same value truncated at 32 bits. It is > this latter behavior that I need to properly port the Delphi code. > > Can someone suggest a way to do this that is consistent on all platforms? > For reference, I am attaching the Delphi code and my PHP port. > > Thanks... > > --Bruce > > {quick (block) mixer routine} > procedure MixBlock(const Matrix : T128bit; var Block; Encrypt : Boolean); > > const > CKeyBox : array [False..True, 0..3, 0..2] of LongInt = > (((0, 3, 1), (2, 1, 3), (1, 0, 2), (3, 2, 0)), > ((3, 2, 0), (1, 0, 2), (2, 1, 3), (0, 3, 1))); > > var > Blocks : array [0..1] of LongInt absolute Block; > Work : LongInt; > Right : LongInt; > Left : LongInt; > R : LongInt; > AA, BB : LongInt; > CC, DD : LongInt; > > begin > Right := Blocks[0]; > Left := Blocks[1]; > > for R := 0 to 3 do begin > {transform the right side} > AA := Right; > BB := Matrix[CKeyBox[Encrypt, R, 0]]; > CC := Matrix[CKeyBox[Encrypt, R, 1]]; > DD := Matrix[CKeyBox[Encrypt, R, 2]]; > > AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 7); > BB := BB + AA; AA := AA + BB; BB := BB xor (BB shl 13); > CC := CC + BB; BB := BB + CC; CC := CC xor (CC shr 17); > DD := DD + CC; CC := CC + DD; DD := DD xor (DD shl 9); > > AA := AA + DD; DD := DD + AA; AA := AA xor (AA shr 3); > BB := BB + AA; BB := BB xor (BB shl 7); > CC := CC + BB; > CC := CC xor (DD shr 15); > DD := DD + CC; > DD := DD xor (DD shl 11); > > Work := Left xor DD; > Left := Right; > Right := Work; > end; > > Blocks[0] := Left; > Blocks[1] := Right; > end; > > Here is my PHP port: > > function zeroFill($a, $b) > { > $z = hexdec(80000000); > if ($z & $a) > { > $a >>= 1; > $a &= (~ $z); > $a |= 0x40000000; > $a >>= ($b-1); > } > else > { > $a >>= $b; > } > return $a; > } > > function MixBlock($AKey, &$ACode, $Encrypt) > { > $CKeyBox = array(array(3, 2, 0), array(1, 0, 2), array(2, 1, 3), array(0, > 3, 1)); > > $Right = $ACode[0]; > $Left = $ACode[1]; > > for ($R=0; $R<=3; $R++) > { > $AA = $Right; > if ($Encrypt) > { > $BB = $AKey[$CKeyBox[$R][0]]; > $CC = $AKey[$CKeyBox[$R][1]]; > $DD = $AKey[$CKeyBox[$R][2]]; > } > else > { > $BB = $AKey[$CKeyBox[3-$R][0]]; > $CC = $AKey[$CKeyBox[3-$R][1]]; > $DD = $AKey[$CKeyBox[3-$R][2]]; > } > > $AA = (int)$AA + (int)$DD; > $DD = (int)$DD + (int)$AA; > $AA = (int)$AA ^ zeroFill($AA, 7); > $BB = (int)$BB + (int)$AA; > $AA = (int)$AA + (int)$BB; > > $BB = (int)$BB ^ ((int)$BB << 13); > $CC = (int)$CC + (int)$BB; $BB = (int)$BB + (int)$CC; > $CC = (int)$CC ^ zeroFill($CC, 17); > $DD = (int)$DD + (int)$CC; $CC = (int)$CC + (int)$DD; > $DD = (int)$DD ^ ((int)$DD << 9); > > $AA = (int)$AA + (int)$DD; $DD = (int)$DD + (int)$AA; > $AA = (int)$AA ^ zeroFill($AA, 3); > $BB = (int)$BB + (int)$AA; > $BB = (int)$BB ^ (int)((int)$BB << 7); > $CC = (int)$CC + (int)$BB; > $CC = (int)$CC ^ zeroFill($DD, 15); > $DD = (int)$DD + (int)$CC; > $DD = (int)$DD ^ (int)((int)$DD << 11); > > $Work = $Left ^ $DD; > $Left = $Right; > $Right = $Work; > } > > $ACode[0] = $Left; > $ACode[1] = $Right; > } > ?> >