1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
package Data::IEEE754;
use strict;
use warnings;
use utf8;
our $VERSION = '0.02';
use Config;
use Exporter qw( import );
our @EXPORT_OK = qw(
pack_double_be
pack_float_be
unpack_double_be
unpack_float_be
);
# This code is all copied from Data::MessagePack::PP by Makamaka
# Hannyaharamitu, and was then tweaked by Dave Rolsky. Blame Dave for the
# bugs.
#
# Perl 5.10 introduced the ">" and "<" modifiers for pack which can be used to
# force a specific endianness.
if ( $] < 5.010 ) {
my $bo_is_le = ( $Config{byteorder} =~ /^1234/ );
if ($bo_is_le) {
*pack_float_be = sub {
return pack( 'N1', unpack( 'V1', pack( 'f', $_[0] ) ) );
};
*pack_double_be = sub {
my @v = unpack( 'V2', pack( 'd', $_[0] ) );
return pack( 'N2', @v[ 1, 0 ] );
};
*unpack_float_be = sub {
my @v = unpack( 'v2', $_[0] );
return unpack( 'f', pack( 'n2', @v[ 1, 0 ] ) );
};
*unpack_double_be = sub {
my @v = unpack( 'V2', $_[0] );
return unpack( 'd', pack( 'N2', @v[ 1, 0 ] ) );
};
}
else { # big endian
*pack_float_be = sub {
return pack 'f', $_[0];
};
*pack_double_be = sub {
return pack 'd', $_[0];
};
*unpack_float_be
= sub { return unpack( 'f', $_[0] ); };
*unpack_double_be
= sub { return unpack( 'd', $_[0] ); };
}
}
else {
*pack_float_be = sub {
return pack 'f>', $_[0];
};
*pack_double_be = sub {
return pack 'd>', $_[0];
};
*unpack_float_be = sub {
return unpack( 'f>', $_[0] );
};
*unpack_double_be = sub {
return unpack( 'd>', $_[0] );
};
}
1;
# ABSTRACT: Pack and unpack big-endian IEEE754 floats and doubles
__END__
=pod
=encoding UTF-8
=head1 NAME
Data::IEEE754 - Pack and unpack big-endian IEEE754 floats and doubles
=head1 VERSION
version 0.02
=head1 SYNOPSIS
use Data::IEEE754 qw( pack_double_be unpack_double_be );
my $packed = pack_double_be(3.14);
my $double = unpack_double_be($packed);
=head1 DESCRIPTION
This module provides some simple convenience functions for packing and
unpacking IEEE 754 floats and doubles.
If you can require Perl 5.10 or greater then this module is pointless. Just
use the C<< d> >> and C<< f> >> pack formats instead!
Currently this module only implements big-endian order. Patches to add
little-endian order subroutines are welcome.
=head1 EXPORTS
This module optionally exports the following four functions:
=over 4
=item * pack_float_be($number)
=item * pack_double_be($number)
=item * unpack_float_be($binary)
=item * unpack_double_be($binary)
=back
=head1 CREDITS
The code in this module is more or less copied and pasted from
L<Data::MessagePack>'s C<Data::MessagePack::PP> module. That module was
written by Makamaka Hannyaharamitu. The code was then tweaked by Dave Rolsky,
so blame him for the bugs.
=head1 SUPPORT
Please submit bugs to the CPAN RT system at
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-IEEE754 or via email at
bug-data-ieee754@rt.cpan.org.
Bugs may be submitted through L<https://github.com/maxmind/Data-IEEE754/issues>.
=head1 AUTHOR
Dave Rolsky <autarch@urth.org>
=head1 CONTRIBUTORS
=for stopwords Dave Rolsky Greg Oschwald
=over 4
=item *
Dave Rolsky <drolsky@maxmind.com>
=item *
Greg Oschwald <goschwald@maxmind.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by MaxMind, Inc.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=cut
|