Adventures in 64-bit PHP: 32-bit Left Shift on a 64-bit Machine
My team member was looking for source code that can parse Excel files and found PHP-ExcelReader. It worked fine on her computer, so she ported it to production. After running it for a few times the server became totally unresponsive.
I went over to take a look at the server and the mouse pointer wouldn’t even move. So I had to do a hard reboot. I ran tail -n 300 /var/log/messages (it’s a SUSE LINUX Enterprise Server 9 for AMD64 & Intel EM64T) and it showed that the server was running out of memory before it hung. I checked the Apache httpd log file and it reported that a script had timed out. Finally, I scanned the PHP log file. Aha – it gave the script filename and line number.
The script in question was oleread.inc, an include file for PHP-ExcelReader. And the line reported by the PHP log contained a left shift operator (<<).
Could it be that the left shift operator gives different results between the 32-bit and 64-bit platforms? I tried Googling and found one guy having the same problem. He didn’t have a solution, but at least he confirmed my suspicions. I spent some time looking for a function that would simulate 32-bit left shift on a 64-bit machine but my search was fruitless. So I decided to roll my own.
I had to go back to basics – I’d forgotten all my first year CS stuff on binary numbers. Fortunately I found a nicely-written tutorial explaining how it all works. Armed with this knowledge I proceeded to write a 32-bit left shift function, which will be the subject of my next post.
And only after I had written this function did I find a patch for PHP-ExcelReader to enable it to run on 64 bits …
One Response to “Adventures in 64-bit PHP: 32-bit Left Shift on a 64-bit Machine”
1 Julien 26 November 2009 @ 11:40 pm
Excellent, thanks !
Comments: