# ========== Copyright Header Begin ==========================================
# OpenSPARC T2 Processor File: BigIntSupport.pm
# Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
# 4150 Network Circle, Santa Clara, California 95054, U.S.A.
# * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# For the avoidance of doubt, and except that if any non-GPL license
# choice is available it will apply instead, Sun elects to use only
# the General Public License version 2 (GPLv2) at this time for any
# software where a choice of GPL license versions is made
# available with the language indicating that GPLv2 or any later version
# may be used, or where a choice of which version of the GPL is applied is
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
# CA 95054 USA or visit www.sun.com if you need additional information or
# ========== Copyright Header End ============================================
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use BigIntSupport ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
our %EXPORT_TAGS = ( 'all' => [ qw(
our @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
# Preloaded methods go here.
$hexstring = 0 unless defined $hexstring;
$hexstring =~ s/^0[xX]//;
my $bignum = Math
::BigInt
->new(0);
while(length $hexstring) {
my $tail = substr $hexstring, -8, 8, "";
my $tailval = hex($tail);
$bignum += Math
::BigInt
->new($tailval) << $shift;
return sprintf("%x", $bignum);
my $low = $bignum & 0xffffffff;
$string = sprintf("%08x", $low) . $string;
$string = '0' if $string eq '';
if($format =~ /^\s*\%?(0)?(\d+)?[lL]*([xd])\s*/) {
my ($lead, $width, $base) = ($1, $2, $3);
$width = 0 unless defined $width;
$base = '' unless defined $base;
$string = bigint2hex
($bigint);
my $strlen = length($string);
my $diff = $width - $strlen;
my $char = defined($lead)?
'0' : ' ';
my $leading = $char x
$diff;
$string = "$leading$string";
croak
"Bad format string: $format";
# Below is stub documentation for your module. You better edit it!
BigIntSupport - Perl extension for interfacing between Math::BigInt values and text strings.
# Create Math::BigInt from an arbitrary-sized hex number
my $bigint1 = hex2bigint("0xdeadbeefcafebabe01234567");
my $bigint2 = hex2bigint("0xffffffff");
my $string = bigint2hex($bigint1);
print "Result is 0x$string\n";
This provides some interface functions for the Math::BigInt library,
which is part of the perl standard libary. Basically, Math::BigInt is
a module for arbitrary-sized integers. It overloads all the standard
math operators for use with Math::BigInt objects, but it does not
overload either the hex() function or sprintf "%x", so it is difficult
to use Math::BigInt objects for large hex numbers. This module is a
solution to that problem.
The following functions are exported:
=item hex2bigint($hex_string)
Interprets $hex_string as a hex number (whether or not a leading 0x is
present). Returns a Math::BigInt object representing the number. To
convert decimal numbers to Math::BigInt objects, use
Math::BigInt->new($decimal_string).
=item bigint2hex($bigint)
Formats the given Math::BigInt object as a hexidecimal string and
returns it. The string has no leading zeros or 0x.
=item bigint_sprintf($bigint, $format)
Takes a Math::BigInt and a printf-style format string and returns a
string with the appropriately-formatted string. The format string can
contain only %x or %d, and may optionally include a width
specification, which may include a leading zero. In other words,
"%016x" is legal but "The answer is %d" is not. A leading "%" is
Math::BigInt(3), perl(1).