NAME

Math::Int64 - Manipulate 64 bits integers in Perl

SYNOPSIS

use Math::Int64 qw(int64 uint64);

my $i = int64(1);
my $j = $i << 40;
print($i + $j * 1000000);

my $k = uint64("12345678901234567890");

DESCRIPTION

This module adds support for 64 bit integers, signed and unsigned, to Perl.

Exportable functions

Die on overflow

The lexical pragma Math::Int64::die_on_overflow configures the module to throw an error when some operation results in integer overflow.

For instance:

use Math::Int64 qw(uint64);
use Math::Int64::die_on_overflow;

my $zero = uint64(0);
say ($zero - 1);                 # dies as -1 falls outside
                                 # the uint64_t range

no Math::Int64::die_on_overflow; # deactivates lexical pragma
say ($zero - 1);                 # no error is detected here!

The pragma can also be activated as follows:

use Math::Int64 ':die_on_overflow';

Once this pragma is used, several Math::Int64 operations may become slower. Deactivating the pragma will not make them fast again.

On Perl 5.8.x, as lexical pragmas support is not available, the pragma die_on_overflow pragma is global and can not be deactivated.

Fallback to native 64bit support if available

If the lexical pragma Math::Int64::native_if_available is used in your program and the version of perl in use has native support for 64bit integers, the functions imported from the module that create 64bit integers (i.e. uint64, int64, string_to_int64, native_to_int64, etc.) will return regular perl scalars.

For instance:

use Math::Int64 qw(int64);

$a = int64(34); # always returns an object of the class Math::Int64

use Math::Int64::native_if_available;
$a = int64(34); # returns a regular scalar on perls compiled with
                # 64bit support

This feature is not enabled by default because the semantics for perl scalars and for 64 bit integers as implemented in this module are not identical.

Perl is prone to coerce integers into floats while this module keeps then always as 64bit integers. Specifically, the division operation and overflows are the most problematic cases. Also, when using native integers, the signed/unsigned division blurs.

Besides that, in most situations it is safe to use the native fallback.

As happens with the die_on_overflow pragma, on Perl 5.8.x it is global.

The pragma can also be activated as follows:

use Math::Int64 ':native_if_available';

Transparent conversion of objects to int64/uint64

When in some operation involving int64/uint64 numbers, a blessed object is passed as an operand, the module would try to coerce the object into an int64/uint64 number calling the methods as_int64/as_uint64 respectively.

If the corresponding method is not implemented, the object will be stringified and then parsed as a base 10 number.

Storable integration

Objects of classes Math::Int64 and Math::UInt64 implement the STORABLE_freeze and STORABLE_thaw methods for a transparent integration with Storable.

C API

This module provides a native C API that can be used to create and read Math::Int64 int64 and uint64 SVs from your own XS modules.

In order to use it you need to follow these steps:

For instance:

--- Foo64.xs ---------

 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
 #include "ppport.h"

 /* #define MATH_INT64_NATIVE_IF_AVAILABLE */
 #include "math_int64.h"

 MODULE = Foo64                PACKAGE = Foo64
 BOOT:
     PERL_MATH_INT64_LOAD_OR_CROAK;

 int64_t
 some_int64()
 CODE:
     RETVAL = -42;
 OUTPUT:
     RETVAL


 --- Makefile.PL -----

 use ExtUtils::MakeMaker;
 WriteMakefile( NAME         => 'Foo64',
                VERSION_FROM => 'lib/Foo64.pm',
                OBJECT       => '$(O_FILES)' );

If the macro MATH_INT64_NATIVE_IF_AVAILABLE is defined before including perl_math_int64.h and the perl interpreter is compiled with native 64bit integer support, IVs will be used to represent 64bit integers instead of the object representation provided by Math::Int64.

These are the C macros available from Math::Int64 C API:

If you require any other function available through the C API don't hesitate to ask for it!

BUGS AND SUPPORT

The Storable integration feature is experimental.

The C API feature is experimental.

This module requires int64 support from the C compiler.

In order to report bugs you can send me and email to the address that appears below or use the CPAN RT bug tracking system available at http://rt.cpan.org.

The source for the development version of the module is hosted at GitHub: https://github.com/salva/p5-Math-Int64.

My wishlist

If you like this module and you're feeling generous, take a look at my Amazon Wish List: http://amzn.com/w/1WU1P6IR5QZ42

SEE ALSO

The C API usage sample module Math::Int64::C_API::Sample.

Other modules providing support for larger integers or numbers are Math::BigInt, Math::BigRat and Math::Big, Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.

COPYRIGHT AND LICENSE

Copyright © 2007, 2009, 2011-2014 by Salvador Fandiño (sfandino@yahoo.com)

Copyright © 2014 by Dave Rolsky (autarch@urth.org)

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.