help needed: function not returning right val on *SOME* PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

help needed: function not returning right val on *SOME* PHP

Post by shokk »

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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You shouldn't need us to see all of the code in the first place... Just the snippet where there is a problem.

If you know where the calculations are incorrect, why don't you just post up that part?
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

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 "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZA2: " . $a . "<br />";
        }
        return $a;
}
which is called from here:

Code: Select all

function mix($a,$b,$c) {
    $a -= $b;
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A: " . $a . "<br />";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: " . $b . "<br />";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: " . $c . "<br />";
    $a -= $c;
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A: " . $a . "<br />";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: " . $b . "<br />";
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C: " . $c . "<br />";
    $a ^= (zeroFill($c,13));
    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A: " . $a . "<br />";

[...]

    return array($a,$b,$c);
}
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]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Couldn't those numbers be affected by PHP on 32 bit vs 64 bit systems?
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

That's what I expected, but my system and the other that I've been debugging are both 32-bit.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are the ones seeing the irregularity on 64bit systems?
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

I'll have him ask his hosting service.
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

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

Code: Select all

return $a;
is not returning that same value properly for the function.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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:
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).
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.
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just out of curiosity, have you tried removing the parentheses when setting $a?

Code: Select all

<?php
$a ^= (zeroFill($c,13));
?>
to

Code: Select all

<?php
$a ^= zeroFill($c,13);
?>
shokk
Forum Newbie
Posts: 8
Joined: Wed Oct 22, 2003 5:35 pm

Post by shokk »

Well, it worked for me both before and after that. I'll check with the other guys to see if it helped.
Post Reply