Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Lesson Learned: not all 32b perls are created equal

by pryrt (Abbot)
on Sep 30, 2017 at 15:22 UTC ( [id://1200426]=perlmeditation: print w/replies, xml ) Need Help??

Given the pretty CPAN Testers Christmas Tree, especially in the *BSD columns, I learned that not all 32-bit perls are created equal. After some more debug, I was able to show that it was those 32-bit perls with $Config{ivsize}==4 (32bit integers; aka perl -V:ivsize) that were failing, but those with $Config{ivsize}==8 (64bit integers in 32bit perl) would pass.

I had assumed (without looking) that the default ivsize on the 32bit perl was 4 bytes (32 bits), so thought that I had already tested and verified my IV weren't overflowing (or, if they were, they were promoting to NV). After seeing the problem, I discovered that when using left-shift, it would go from IV to NV... but it didn't. However, *=2 did promote the way I expected:

<berrybrew use perl-5.12.3_32> C:\Users\Peter>perl -MDevel::Peek -lE "$iv=1<<30; for(1..3) { Dump $iv +; $iv<<=1; }" SV = IV(0x68fc28) at 0x68fc2c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1073741824 SV = IV(0x68fc28) at 0x68fc2c REFCNT = 1 FLAGS = (IOK,pIOK,IsUV) UV = 2147483648 SV = IV(0x68fc28) at 0x68fc2c REFCNT = 1 FLAGS = (IOK,pIOK) IV = 0 <berrybrew use perl-5.12.3_32> C:\Users\Peter>perl -MDevel::Peek -lE "$iv=1<<30; for(1..3) { Dump $iv +; $iv*=2; }" SV = IV(0x24dfbe0) at 0x24dfbe4 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 1073741824 SV = IV(0x24dfbe0) at 0x24dfbe4 REFCNT = 1 FLAGS = (IOK,pIOK,IsUV) UV = 2147483648 SV = PVNV(0x7ea464) at 0x24dfbe4 REFCNT = 1 FLAGS = (NOK,pNOK) IV = -2147483648 NV = 4294967296 PV = 0 <berrybrew use perl-5.12.3_32> C:\Users\Peter>perl -V:ivsize ivsize='4';

Conclusion: when doing a testing suite across versions, it's not always enough to just have a "32bit perl"; especially if you are dependent on integer sizes, also check that your test suite includes multiple ivsize values.

Replies are listed 'Best First'.
Re: Lesson Learned: not all 32b perls are created equal
by ikegami (Patriarch) on Sep 30, 2017 at 21:43 UTC

    C doesn't provide operators to perform bitwise operations on floats (because processors don't provide those either), and Perl doesn't go out of its way to add support for that.

    It did recently add support for hexadecimal floats, though!

    $ perl -e'CORE::say sprintf "%a", 9/10' 0x1.ccccccccccccdp-1 $ perl -e'CORE::say 0x1.ccccccccccccdp-1' 0.9

      It was a case of subconsciously expecting DWIM, and since it passed my 100%-coverage tests with 32bit perl, I thought it did DWIM

      Yes, I saw the %a soon after releasing that project... defonitely a welcome addition. Some day, I might have my module use the builtin on modern perls, but I'll have to see whether it has all the features I want...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://1200426]
Approved by Athanasius
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found