File: mod128.pl

package info (click to toggle)
libmath-int128-perl 0.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 604 kB
  • sloc: perl: 2,614; ansic: 340; makefile: 3
file content (151 lines) | stat: -rw-r--r-- 4,539 bytes parent folder | download
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
#! perl -slw
use strict;
use Benchmark qw[ cmpthese ];
use Math::Int128 qw[ uint128 uint128_to_hex :op ];
use Math::GMPz qw[ Rmpz_init_set_str ];


sub FNV_1_128 {
    my $s = shift;
    my $h = uint128( '144066263297769815596495629667062367629' );
    my $p = uint128( '309485009821345068724781371' );
    $h *= $p, $h ^= $_ for unpack 'C*', $s;
    return $h;
}

sub FNV_1a_128 {
    my $s = shift;
    my $h = uint128( '144066263297769815596495629667062367629' );
    my $p = uint128( '309485009821345068724781371' );
    $h ^= $_, $h *= $p for unpack 'C*', $s;
    return $h;
}

our $h = uint128();
our $p = uint128();

sub FNV_1_128_pa {
    my $s = shift;
    uint128_set($h, '144066263297769815596495629667062367629' );
    uint128_set($p, '309485009821345068724781371' );
    $h *= $p, $h ^= $_ for unpack 'C*', $s;
    return $h;
}

sub FNV_1a_128_pa {
    my $s = shift;
    uint128_set($h, '144066263297769815596495629667062367629' );
    uint128_set($p, '309485009821345068724781371' );
    $h ^= $_, $h *= $p for unpack 'C*', $s;
    return $h;
}

sub FNV_1_128_gmpz {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    my $m = Math::GMPz->new( 1 ) << 128;
    $h *= $p, $h ^= $_ for unpack 'C*', $s;
    return $h % $m;
}

sub FNV_1a_128_gmpz {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    my $m = Math::GMPz->new( 1 ) << 128;
    $h ^= $_, $h *= $p for unpack 'C*', $s;
    return $h % $m;
}

sub FNV_1_128_gmpz2 {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    my $m = Math::GMPz->new( 1 ) << 128;
    $h *= $p, $h ^= $_, $h %= $m for unpack 'C*', $s;
    return $h;
}

sub FNV_1a_128_gmpz2 {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    my $m = Math::GMPz->new( 1 ) << 128;
    $h ^= $_, $h *= $p, $h %= $m for unpack 'C*', $s;
    return $h;
}

my $mod128_mask = (Math::GMPz->new( 1 ) << 128) - 1;

sub FNV_1_128_gmpz3 {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    $h *= $p, $h &= $mod128_mask, $h ^= $_ for unpack 'C*', $s;
    return $h;
}

sub FNV_1a_128_gmpz3 {
    my $s = shift;
    my $h = Rmpz_init_set_str('144066263297769815596495629667062367629', 10);
    my $p = Rmpz_init_set_str('309485009821345068724781371', 10);
    $h ^= $_, $h *= $p, $h &= $mod128_mask for unpack 'C*', $s;
    return $h;
}

sub FNV_1_128_gmpz4 {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    $h *= $p, $h ^= $_ for unpack 'C*', $s;
    return $h;
}

sub FNV_1a_128_gmpz4 {
    my $s = shift;
    my $h = Math::GMPz->new( '144066263297769815596495629667062367629' );
    my $p = Math::GMPz->new( '309485009821345068724781371' );
    $h ^= $_, $h *= $p for unpack 'C*', $s;
    return $h;
}


our $text = do{ local( @ARGV, $/ ) = $0; <> };
print length $text;

cmpthese -1, {
    int128 => q[
        my $fnv1  = uint128_to_hex( FNV_1_128( $text ) );
        my $fnv1a = uint128_to_hex( FNV_1a_128( $text ) );
    ],
    int128_pa => q[
        my $fnv1  = uint128_to_hex( FNV_1_128_pa( $text ) );
        my $fnv1a = uint128_to_hex( FNV_1a_128_pa( $text ) );
    ],
    GMPz => q[
        my $fnv1  = Math::GMPz::Rmpz_get_str( FNV_1_128_gmpz( $text ), 16 );
        my $fnv1a = Math::GMPz::Rmpz_get_str( FNV_1a_128_gmpz( $text ), 16 );
    ],
    GMPz2 => q[
        my $fnv1  = Math::GMPz::Rmpz_get_str( FNV_1_128_gmpz2( $text ), 16 );
        my $fnv1a = Math::GMPz::Rmpz_get_str( FNV_1a_128_gmpz2( $text ), 16 );
    ],
    GMPz3 => q[
        my $fnv1  = Math::GMPz::Rmpz_get_str( FNV_1_128_gmpz3( $text ), 16 );
        my $fnv1a = Math::GMPz::Rmpz_get_str( FNV_1a_128_gmpz3( $text ), 16 );
    ],
    GMPz4 => q[
        my $fnv1  = Math::GMPz::Rmpz_get_str( FNV_1_128_gmpz3( $text ), 16 );
        my $fnv1a = Math::GMPz::Rmpz_get_str( FNV_1a_128_gmpz3( $text ), 16 );
    ],
};

__END__
C:\test>FNV128.pl
2455
         Rate   GMPz  GMPz2 int128
GMPz   14.1/s     --   -77%   -95%
GMPz2  61.4/s   334%     --   -78%
int128  274/s  1840%   347%     --