Re: reliably test integer size for portable bit-fiddling?
by BrowserUk (Patriarch) on Dec 08, 2014 at 20:00 UTC
|
C:\test>perl -MConfig -E"say $Config{ ivsize }"
8
C:\test>\perl32\bin\perl -MConfig -le"print $Config{ ivsize }"
4
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
|
root@raspberry2:/home/helmut# time perl -MConfig -E 'print $Config{ivs
+ize};'
8
real 0m0.177s
user 0m0.160s
sys 0m0.010s
root@raspberry2:/home/helmut# time perl -e 'print length(pack('j', -1
+))*8;'
64
real 0m0.028s
user 0m0.020s
sys 0m0.000s
| [reply] [d/l] |
|
Config is a core module, so is always available.
| [reply] |
|
C:\test>\perl32\bin\perl -e"print length(pack('j', -1))*8;"
32
C:\test>perl -e"print length(pack('j', -1))*8;"
64
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Config is part of Perl, and is Pure Perl.
| [reply] |
Re: reliably test integer size for portable bit-fiddling?
by Perlbotics (Archbishop) on Dec 08, 2014 at 19:57 UTC
|
I think so. Perhaps, it is also an option for you to ask perl itself with the help of core module Config?
perl -e 'use Config; print $Config{use64bitint} eq "define" ? "yes, "
+ : "not ", "64bit perl\n"'
Result (here):
yes, 64bit perl
| [reply] [d/l] [select] |
|
$ perl -V:'use64bit(int|all)|[iu]vsize'
ivsize='8';
use64bitall='define';
use64bitint='define';
uvsize='8';
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
Re: reliably test integer size for portable bit-fiddling? (~0)
by tye (Sage) on Dec 09, 2014 at 00:04 UTC
|
log(~0)/log(2)
| [reply] [d/l] |
|
| [reply] |
|
$ perl -E " say log(~0)/log(2) "
31.9999999996641
$ perl -E " say int 0.999+log(~0)/log(2) "
32
| [reply] [d/l] |
|
|
log(~0)/log(2)
Hmm, I tried a similar idea (get the index of the highest bit), but it failed. Now I remember:
helmut@w3:~$ perl -e 'print log(-1)/log(2),"\n";'
Can't take log of -1 at -e line 1.
Thx, this is extremely fast and IMHO reliable, because it tests the capabilty directly.
Helmut "wollmers" Wollmersdorfer | [reply] [d/l] [select] |
|
I figure for base2 stuff length is better than log (no need to worry about rounding/transcendental crap), e.g.,
4*length sprintf'%x',~0
| [reply] [d/l] [select] |
|
say length sprintf "%b", ~0;;
64
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: reliably test integer size for portable bit-fiddling?
by Tux (Canon) on Dec 08, 2014 at 21:34 UTC
|
The j format is still architecture depending, as the others already showed. If your perl has 64bit ints (even if the poiters are just 32bit), does q (or Q) work for you? (for portability, do not forget < or >). That won't work on 32-bit-only perl builds.
$ perl -MDP -we'DHexDump pack "Q", 0x12345678'
0000 78 56 34 12 00 00 00 00 xV4.....
$ perl -MDP -we'DHexDump pack "Q>", 0x12345678'
0000 00 00 00 00 12 34 56 78 .....4Vx
$ perl -MDP -we'DHexDump pack "Q<", 0x12345678'
0000 78 56 34 12 00 00 00 00 xV4.....
$ perl -V:uvsize
uvsize='8';
other perl
$ perl -V:uvsize
uvsize='4';
$ perl -wle'print length pack "Q", 3'
Invalid type 'Q' in pack at -e line 1.
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
|
I had written once use constant CAN_PACK_QUADS => !! eval { my $f = pack 'q'; 1 }; cause it dies when it can't
| [reply] [d/l] |
Re: reliably test integer size for portable bit-fiddling?
by DrHyde (Prior) on Dec 09, 2014 at 11:56 UTC
|
The normal way to figure out how big your ints are is this:
use constant MAXINT => ~0;
which is quite a bit easier to read. | [reply] [d/l] |
Re: reliably test integer size for portable bit-fiddling?
by danaj (Friar) on Dec 14, 2014 at 06:28 UTC
|
I find: ~0 > 4294967295 to work and be simple/fast/understandable. Easier to follow at a glance than your pack, no time spent loading Config, no log() needed. If you use it a lot, make it a constant, e.g.
use constant MAXBITS => (~0 > 4294967295) ? 64 : 32;
A downside is that it assumes a 32-/64-bit world so isn't ready for 128-bit UVs if they every come. If you're already using the Config module or think it's more clear and don't care about the small load time, then I'd use that ($Config{uvsize}).
A warning on using ~0: don't use it in a place where bigint, bignum, or integer may be loaded, because it will be -1. This shouldn't be an issue inside your module, but something to be aware of.
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |