help needed: function not returning right val on *SOME* PHP
Moderator: General Moderators
help needed: function not returning right val on *SOME* PHP
I have code that you can see at http://www.shokk.com/downloads/debug.pht which is supposed to produce the output seen at http://www.shokk.com/mint/pepper/ernieo ... .shokk.com but which produces the output seen at http://www.rourkem.com/mint/pepper/erni ... .shokk.com on some other systems.
The code has verbose output for various checkpoints in the code to figure out where the calcs are not being done right.
You'll notice that after the point labeled ZA2:, the values are not the same. At the ZA2: point, the last value in a function has been calculated and is returned to whatever called it. The A: point right after that is what was received from the function call and on some installs the returned value is different. There are no randomly calculated values - just some basic arithmetic and bitwise shifts. I've looked at phpinfo() output on both systems, and even 32-bit vs 64-bit as reasons why this might happen, but can't track this down. I'm hoping someone can glance at this and see right off what my mistake is, or can point out some bug I'm triggering.
I did not post the code here because it's a little lengthy, but please let me know if that is not the right forum etiquette and I will post it directly here.
The code has verbose output for various checkpoints in the code to figure out where the calcs are not being done right.
You'll notice that after the point labeled ZA2:, the values are not the same. At the ZA2: point, the last value in a function has been calculated and is returned to whatever called it. The A: point right after that is what was received from the function call and on some installs the returned value is different. There are no randomly calculated values - just some basic arithmetic and bitwise shifts. I've looked at phpinfo() output on both systems, and even 32-bit vs 64-bit as reasons why this might happen, but can't track this down. I'm hoping someone can glance at this and see right off what my mistake is, or can point out some bug I'm triggering.
I did not post the code here because it's a little lengthy, but please let me know if that is not the right forum etiquette and I will post it directly here.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
feyd | Please use
which is called from here:
and right after the zeroFill call we print $a again. Simple stuff and it works on my server. But others that I have given the code to report that the numbers being calculated from the debug.pht code at the link above do not come out right after the zeroFill function. The expected value of A after the ZA2 point is -1612449673, as you can see at http://www.shokk.com/mint/pepper/ernieo ... .shokk.com but the ones getting it wrong are coming up as -2147300576 as can be seen at http://www.rourkem.com/mint/pepper/erni ... .shokk.com
Note that zeroFill (incorrectly named) is really just an 8-bit unsigned shift right.
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
OK, so this is the code that gets called. The ZA2 output can be seen below.Code: Select all
function zeroFill($a, $b)
{
$z = hexdec(80000000);
if ($z & $a) # if a is negative
{
$a = ($a>>1); # arith shift right
$a &= (~$z); # flip all bits
$a |= 0x40000000; # take off the signed bit
$a = ($a>>($b-1));
}
else # a is positive - just do a shift
{
$a = ($a>>$b);
echo " ZA2: " . $a . "<br />";
}
return $a;
}Code: Select all
function mix($a,$b,$c) {
$a -= $b;
echo " A: " . $a . "<br />";
echo " B: " . $b . "<br />";
echo " C: " . $c . "<br />";
$a -= $c;
echo " A: " . $a . "<br />";
echo " B: " . $b . "<br />";
echo " C: " . $c . "<br />";
$a ^= (zeroFill($c,13));
echo " A: " . $a . "<br />";
[...]
return array($a,$b,$c);
}Note that zeroFill (incorrectly named) is really just an 8-bit unsigned shift right.
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Just getting back to this issue. Why would 32-bit vs 64-bit have anything to do it? The right value is being calculated for $a within the function, but the
is not returning that same value properly for the function.
Code: Select all
return $a;- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
The value of numbers in PHP is dependent upon the type of system that PHP is running on. 64bit systems can produce much larger numbers that 32bit systems.
Read the manual on floating point numbers, particularly the note that states:
Read the manual on floating point numbers, particularly the note that states:
There is also more information in the manual on integers and their platform dependency as well. Additionally, this manual page mentions 'integer overflow' and what happens to numbers that get too big. Also, read the user comments on the integer manual page for a lot of good information from other developers as it relates to numbers and their sizes on different platforms.The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format).
Understood, but what I'm saying is that the correct value is being generated for $a within the function. It is just not being returned back from the function call for some people. It is in the return from the function that the $a is not correct. Is the return from a function a boundary where 32-bit and 64-bit systems might differ, even if the actual calculation is generating the correct value?
Are numbers like -112900818 and 5794697687 generally to big for 32-bit systems to properly handle in PHP? Seems unlikely since my own 32-bit system is handling it OK. I'm still trying to get feedback from others that are seeing the issue.
Are numbers like -112900818 and 5794697687 generally to big for 32-bit systems to properly handle in PHP? Seems unlikely since my own 32-bit system is handling it OK. I'm still trying to get feedback from others that are seeing the issue.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Just out of curiosity, have you tried removing the parentheses when setting $a?
to
Code: Select all
<?php
$a ^= (zeroFill($c,13));
?>Code: Select all
<?php
$a ^= zeroFill($c,13);
?>