NAME

uinteger - like "use integer", but unsigned

SYNOPSIS

use uinteger;
print 1 - 2; # print a large number

DESCRIPTION

Rewrites add, subtract, multiply and unary negation in use uinteger context to perform the operation as if the number was an unsigned integer (a Perl UV).

Negative numbers are treated as their 2's complement representation.

Most bitops already treat their arguments as unsigned outside of use integer so this doesn't need to cover those.

PERFORMANCE

The raw OPs are about as fast as under use integer;, but two things lead to slightly slower performance:

  • The target lexical optimization isn't performed for custom ops. Code like:

    $z = $c + $d;

    initially generates an op tree something like:

    sassign
      padtmp = add(stack values)
        padsv($c)
        padsv($d)
      padsv($z)

    which, when the target variable is lexical, is then optimized to:

    $z = add(stack values)
      padsv($c)
      padsv($d)

    but that optimization isn't done for custom operators. For code that does a lot of intermediate stores to lexicals this can make a significant difference. This may change.

  • SvUV(), the macro used to fetch an unsigned integer from an SV only directly accesses the value when the value isn't representable as an IV, ie. when the value is above IV_MAX.

    This means that fetching an IV is typically a little faster.

If you want performant unsigned integer (or integer or floating point) math you should probably be using XS.

AUTHOR

Tony Cook <tony@develop-help.com>