|
NAMEData::Integer - details of the native integer data typeSYNOPSISuse Data::Integer qw(natint_bits); $n = natint_bits; # and other constants; see text use Data::Integer qw(nint sint uint nint_is_sint nint_is_uint); $ni = nint($ni); $si = sint($si); $ui = uint($ui); if(nint_is_sint($ni)) { ... if(nint_is_uint($ni)) { ... use Data::Integer qw( nint_sgn sint_sgn uint_sgn nint_abs sint_abs uint_abs nint_cmp sint_cmp uint_cmp nint_min sint_min uint_min nint_max sint_max uint_max nint_neg sint_neg uint_neg nint_add sint_add uint_add nint_sub sint_sub uint_sub); $sn = nint_sgn($ni); $sn = sint_sgn($si); $sn = uint_sgn($ui); $ni = nint_abs($ni); $si = sint_abs($si); $ui = uint_abs($ui); @sorted_nints = sort { nint_cmp($a, $b) } @nints; @sorted_sints = sort { sint_cmp($a, $b) } @sints; @sorted_uints = sort { uint_cmp($a, $b) } @uints; $ni = nint_min($na, $nb); $si = sint_min($sa, $sb); $ui = uint_min($ua, $ub); $ni = nint_max($na, $nb); $si = sint_max($sa, $sb); $ui = uint_max($ua, $ub); $ni = nint_neg($ni); $si = sint_neg($si); $ui = uint_neg($ui); $ni = nint_add($na, $nb); $si = sint_add($sa, $sb); $ui = uint_add($ua, $ub); $ni = nint_sub($na, $nb); $si = sint_sub($sa, $sb); $ui = uint_sub($ua, $ub); use Data::Integer qw( sint_shl uint_shl sint_shr uint_shr sint_rol uint_rol sint_ror uint_ror); $si = sint_shl($si, $dist); $ui = uint_shl($ui, $dist); $si = sint_shr($si, $dist); $ui = uint_shr($ui, $dist); $si = sint_rol($si, $dist); $ui = uint_rol($ui, $dist); $si = sint_ror($si, $dist); $ui = uint_ror($ui, $dist); use Data::Integer qw( nint_bits_as_sint nint_bits_as_uint sint_bits_as_uint uint_bits_as_sint); $si = nint_bits_as_sint($ni); $ui = nint_bits_as_uint($ni); $ui = sint_bits_as_uint($si); $si = uint_bits_as_sint($ui); use Data::Integer qw( sint_not uint_not sint_and uint_and sint_nand uint_nand sint_andn uint_andn sint_or uint_or sint_nor uint_nor sint_orn uint_orn sint_xor uint_xor sint_nxor uint_nxor sint_mux uint_mux); $si = sint_not($si); $ui = uint_not($ui); $si = sint_and($sa, $sb); $ui = uint_and($ua, $ub); $si = sint_nand($sa, $sb); $ui = uint_nand($ua, $ub); $si = sint_andn($sa, $sb); $ui = uint_andn($ua, $ub); $si = sint_or($sa, $sb); $ui = uint_or($ua, $ub); $si = sint_nor($sa, $sb); $ui = uint_nor($ua, $ub); $si = sint_orn($sa, $sb); $ui = uint_orn($ua, $ub); $si = sint_xor($sa, $sb); $ui = uint_xor($ua, $ub); $si = sint_nxor($sa, $sb); $ui = uint_nxor($ua, $ub); $si = sint_mux($sa, $sb, $sc); $ui = uint_mux($ua, $ub, $uc); use Data::Integer qw( sint_madd uint_madd sint_msub uint_msub sint_cadd uint_cadd sint_csub uint_csub sint_sadd uint_sadd sint_ssub uint_ssub); $si = sint_madd($sa, $sb); $ui = uint_madd($ua, $ub); $si = sint_msub($sa, $sb); $ui = uint_msub($ua, $ub); ($carry, $si) = sint_cadd($sa, $sb, $carry); ($carry, $ui) = uint_cadd($ua, $ub, $carry); ($carry, $si) = sint_csub($sa, $sb, $carry); ($carry, $ui) = uint_csub($ua, $ub, $carry); $si = sint_sadd($sa, $sb); $ui = uint_sadd($ua, $ub); $si = sint_ssub($sa, $sb); $ui = uint_ssub($ua, $ub); use Data::Integer qw(natint_hex hex_natint); print natint_hex($value); $value = hex_natint($string); DESCRIPTIONThis module is about the native integer numerical data type. A native integer is one of the types of datum that can appear in the numeric part of a Perl scalar. This module supplies constants describing the native integer type.There are actually two native integer representations: signed and unsigned. Both are handled by this module. NATIVE INTEGERSEach native integer format represents a value using binary place value, with some fixed number of bits. The number of bits is the same for both signed and unsigned representations. In each case the least-significant bit has the value 1, the next 2, the next 4, and so on. In the unsigned representation, this pattern continues up to and including the most-significant bit, which for a 32-bit machine therefore has the value 2^31 (2147483648). The unsigned format cannot represent any negative numbers.In the signed format, the most-significant bit is exceptional, having the negation of the value that it does in the unsigned format. Thus on a 32-bit machine this has the value -2^31 (-2147483648). Values with this bit set are negative, and those with it clear are non-negative; this bit is also known as the "sign bit". It is usual in machine arithmetic to use one of these formats at a time, for example to add two signed numbers yielding a signed result. However, Perl has a trick: a scalar with a native integer value contains an additional flag bit which indicates whether the signed or unsigned format is being used. It is therefore possible to mix signed and unsigned numbers in arithmetic, at some extra expense. CONSTANTSEach of the extreme-value constants has two names, a short one and a long one. The short names are more convenient to use, but the long names are clearer in a context where other similar constants exist.Due to the risks of Perl changing the behaviour of a native integer value that has been involved in floating point arithmetic (see "BUGS"), the extreme-value constants are actually non-constant functions that always return a fresh copy of the appropriate value. The returned value is always a pure native integer value, unsullied by floating point or string operations.
FUNCTIONSEach "nint_", "sint_", or "uint_" function operates on one of the three integer formats. "nint_" functions operate on Perl's union of signed and unsigned; "sint_" functions operate on signed integers; and "uint_" functions operate on unsigned integers. Except where indicated otherwise, the function returns a value of its primary type.Parameters A, B, and C, where present, must be numbers of the appropriate type: specifically, with a numerical value that can be represented in that type. If there are multiple flavours of zero, due to floating point funkiness, all zeroes are treated the same. Parameters with other names have other requirements, explained with each function. The functions attempt to detect unsuitable arguments, and "die" if an invalid argument is detected, but they can't notice some kinds of incorrect argument. Generally, it is the caller's responsibility to provide a sane numerical argument, and supplying an invalid argument will cause mayhem. Only the numeric value of plain scalar arguments is used; the string value is completely ignored, so dualvars are not a problem. Canonicalisation and classificationThese are basic glue functions.
ArithmeticThese functions operate on numerical values rather than just bit patterns. They will all "die" if the true numerical result doesn't fit into the result format, rather than give a wrong answer.
Bit shiftingThese functions all operate on the bit patterns representing integers, mostly ignoring the numerical values represented. In most cases the results for particular numerical arguments are influenced by the word size, because that determines where a bit being left-shifted will drop off the end of the word and where a bit will be shifted in during a rightward shift.With the exception of rightward shifts (see below), each pair of functions performs exactly the same operations on the bit sequences. There inevitably can't be any functions here that operate on Perl's union of signed and unsigned; you must choose, by which function you call, which type the result is to be tagged as.
Format conversionThese functions convert between the various native integer formats by reinterpreting the bit patterns used to represent the integers. The bit pattern remains unchanged; its meaning changes, and so the numerical value changes. Perl scalars preserve the numerical value, rather than just the bit pattern, so from the Perl point of view these are functions that change numbers into other numbers.
Bitwise operationsThese functions all operate on the bit patterns representing integers, completely ignoring the numerical values represented. They are mostly not influenced by the word size, in the sense that they will produce the same numerical result for the same numerical arguments regardless of word size. However, a few are affected by the word size: those on unsigned operands that return a non-zero result if given zero arguments.Each pair of functions performs exactly the same operations on the bit sequences. There inevitably can't be any functions here that operate on Perl's union of signed and unsigned; you must choose, by which function you call, which type the result is to be tagged as.
Machine arithmeticThese functions perform arithmetic operations that are inherently influenced by the word size. They always produce a well-defined output if given valid inputs. There inevitably can't be any functions here that operate on Perl's union of signed and unsigned; you must choose, by which function you call, which type the result is to be tagged as.
String conversion
BUGSIn Perl 5.6, when a native integer scalar is used in any arithmetic other than specifically integer arithmetic, it gets partially transformed into a floating point scalar. Even if its numerical value can be represented exactly in floating point, so that floating point arithmetic uses the correct numerical value, some operations are affected by the floatness. In particular, the stringification of the scalar doesn't necessarily represent its exact value if it is tagged as floating point.Because of this transforming behaviour, if you need to stringify a native integer it is best to ensure that it doesn't get used in any non-integer arithmetic first. If an integer scalar must be used in standard Perl arithmetic, it may be copied first and the copy operated upon to avoid causing side effects on the original. If an integer scalar might have already been transformed, it can be cleaned by passing it through the canonicalisation function "nint". The functions in this module all avoid modifying their arguments, and always return pristine integers. Perl 5.8+ still internally modifies integer scalars in the same circumstances, but seems to have corrected all the misbehaviour that resulted from it. Also in Perl 5.6, default Perl arithmetic doesn't necessarily work correctly on native integers. (This is part of the motivation for the myriad arithmetic functions in this module.) Default arithmetic here is strictly floating point, so if there are native integers that cannot be exactly represented in floating point then the arithmetic will approximate the values before operating on them. Perl 5.8+ attempts to use native integer operations where possible in its default arithmetic, but as of Perl 5.8.8 it doesn't always succeed. For reliable integer arithmetic, integer operations must still be requested explicitly. SEE ALSOData::Float, Scalar::Number, perlnumber(1)AUTHORAndrew Main (Zefram) <zefram@fysh.org>COPYRIGHTCopyright (C) 2007, 2010, 2015, 2017 Andrew Main (Zefram) <zefram@fysh.org>LICENSEThis module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |