#30810 [Opn->Fbk]: discrepancy of bitwise operations between php4.3.9 and php5.0.2

From: Date: Thu, 06 Jan 2005 14:25:26 +0000
Subject: #30810 [Opn->Fbk]: discrepancy of bitwise operations between php4.3.9 and php5.0.2
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-71524@lists.php.net to get a copy of this message
 ID:               30810
 Updated by:       derick@php.net
 Reported By:      swoo at gvlabs dot com
-Status:           Open
+Status:           Feedback
 Bug Type:         Variables related
 Operating System: winxp
 PHP Version:      5.0.2
 New Comment:

Please try using this CVS snapshot:

  http://snaps.php.net/php5-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.0-win32-latest.zip


Previous Comments:
------------------------------------------------------------------------

[2005-01-06 15:04:13] rs at esoft dot dk

Seems to be fixed in 5.0.3

------------------------------------------------------------------------

[2004-11-17 04:51:05] swoo at gvlabs dot com

Description:
------------
This problem was discovered when I was testing some working code in
preparation for migrating from php4 to php5.
The example is a fragment of the logic used to read an ieee 754 number
from an excel spreadsheet. When you run the code in php4 it yields the
correct interpretation.  However, with php5 it appears that the bitwise
mask and shift when setting $fexp fails with the code that works with
php4.
Also, the php5 resolution of the number is slightly higher.
I recoded the example with the logic that works with php5.
Was there a change in the ieee library or a change to the casting of a
floating point number for bit manipulation in php5? I have not tested
this on any other platform.



Reproduce code:
---------------
<?php  // example of the problem - run with php4 then run with php5

	//25f2d18aa9d26dc0
	$binarydata = pack("CCCCCCCC", 0x25, 0xf2, 0xd1, 0x8a, 0xa9, 0xd2,
0x6d, 0xc0);
	
	// php5 ans: -238.598699007
	// php4 ans: -238.583196077
	
	echo ieee($binarydata, false);
	echo "\n";
	echo ieee($binarydata, true);

function ieee($ieee, $php4test){
	
	if($php4test){
		echo "php4 test ";
	} else {
		echo "php5 test ";
	}
	
	if($php4test){ 	
	
		// php 4.3.9 Built: Sep 21 2004 14:04:26
		// Zend Engine v1.3.0
		
		$num_lo =
16777216*ord($ieee[3])+(ord($ieee[0])|(ord($ieee[1])<<8)|(ord($ieee[2])<<16));
		$num_hi =
16777216*ord($ieee[7])+(ord($ieee[4])|(ord($ieee[5])<<8)|(ord($ieee[6])<<16));
	
		$fexp = (($num_hi & 0x7ff00000) >> 20) - 1023;   // FAILS IN PHP5
		$val = 1+(($num_hi & 0x000fffff)+$num_lo/4294967296)/1048576;
	
	} else {
	
		// php 5.0.2 (cli) build: Sep 24 2004 01:25:41
		// Zend Engine v2.0.2
	
		$num_lo =
(pow(2,24)*ord($ieee[3]))+(ord($ieee[0])|(ord($ieee[1])<<8)|(ord($ieee[2])<<16));
		$num_hi = (pow(2,24)*ord($ieee[7]) &
0x7f)+(ord($ieee[4])|(ord($ieee[5])<<8)|((ord($ieee[6]) & 0x1f)<<16));
	
		$b1 = (ord($ieee[7]) & 0x7f);  // strip the sign bit from the high
byte
		$b2 = (ord($ieee[6]) >> 4);  // get byte leftmost bits 
		$b3 =($b1 << 4) + ($b2);  // shift 4 bits and add low 4 bits
		$fexp = $b3  - 1023;  // exponent
		$val = 1+ ($num_hi + ($num_lo/pow(2,32)))/pow(2,20);

	}

	if( ($fexp==1024) || ($fexp==-1023) )
		return (float)0;

	if( $fexp > 0 ) {
		for( $i=0; $i<$fexp; $i++ )
			$val *= 2;
	} else {
		for( $i=0; $i<abs($fexp); $i++ )
			$val /= 2;
	}
	
	if($php4test){
		if( $num_hi & 0x80000000 ) $val = -$val;
	} else {
		if(ord($ieee[7]) & 0x80) $val = -$val;  // check the high byte's high
bit
	}	

	return (float)$val;
}

?>

Expected result:
----------------
(when run with php4)
php5 test -238.583196077
php4 test -238.583196077

(when run with php5)
php5 test -238.598699007
php4 test -238.598699007

Actual result:
--------------
(when run with php4)
php5 test -238.583196077
php4 test -238.583196077

(when run with php5)
php5 test -238.598699007
php4 test 0


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=30810&edit=1


Thread (4 messages)

« previous php.bugs (#71524) next »