-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib_32_bit_water_hashes.fypp
282 lines (246 loc) · 12.3 KB
/
stdlib_32_bit_water_hashes.fypp
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
!!------------------------------------------------------------------------------
!! `WATER_HASH` is a translation to Fortran 2008 of the `waterhash` algorithm
!! of Tommy Ettinger. Tommy Ettinger's original C++ code, `waterhash.h`, is
!! available at the URL: https://github.com/tommyettinger/waterhash under the
!! `unlicense`, https://github.com/tommyettinger/waterhash/blob/master/LICENSE.
!! "`waterhash` is a variant on Wang Yi's `wyhash`, with 32 bit output,
!! using at most 64 bit arithmetic. `wyhash` is available at the URL:
!! `https://github.com/wangyi-fudan/wyhash` also under the unlicense:
!! `https://github.com/wangyi-fudan/wyhash/blob/master/LICENSE`.
!! Original Author: Wang Yi <godspeed_china@yeah.net>
!! Waterhash Variant Author: Tommy Ettinger <tommy.ettinger@gmail.com>
!!
!! The `unlicense` reads as follows:
!! This is free and unencumbered software released into the public domain.
!!
!! Anyone is free to copy, modify, publish, use, compile, sell, or
!! distribute this software, either in source code form or as a compiled
!! binary, for any purpose, commercial or non-commercial, and by any
!! means.
!!
!! In jurisdictions that recognize copyright laws, the author or authors
!! of this software dedicate any and all copyright interest in the
!! software to the public domain. We make this dedication for the benefit
!! of the public at large and to the detriment of our heirs and
!! successors. We intend this dedication to be an overt act of
!! relinquishment in perpetuity of all present and future rights to this
!! software under copyright law.
!!
!! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
!! EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
!! MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
!! IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
!! OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
!! ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
!! OTHER DEALINGS IN THE SOFTWARE.
!!
!! For more information, please refer to <http://unlicense.org>
!!
!! `WATER_HASH` is distributed as part of the `stdlib_32_bit_hash_functions.f90`
!! module and its `stdlib_32_bit_water_hashes.f90` submodule with the Fortran
!! Standard Library at URL: https://github.com/fortran-lang/stdlib.
!! The Fortran Standard Library, including this code, is distributed under the
!! MIT License as described in the `LICENSE` file distributed with the library.
!! `WATER_HASH` differs from `waterhash.h` not only in its use of Fortran,
!! but also in its use of signed two's complement arithmetic in contrast to
!! the unsigned arithmetic of Ettinger and Wang Yi, and in making some of the
!! uses of `TRANSFER` endian dependent, in an attempt to make the quality of
!! the hash endian independent. The use of signed arithmetic may change with
!! the planned introduction of the unsigned BITS datatype in what is currently
!! known as Fortran 202X.
!!
!! To be useful this code must be processed by a processor that implements two
!! Fortran 2008 extensions to Fortran 2003: submodules, and 64 bit (`INT64`)
!! integers. The processor must also use two's complement integers
!! (all Fortran 95+ processors use two's complement arithmetic) with
!! wrap around overflow at runtime and for BOZ constants. The latest releases
!! of the following processors are known to implement the required Fortran
!! 2008 extensions and default to runtime wrap around overflow: FLANG,
!! gfortran, ifort, and NAG Fortran. Older versions of gfortran will require
!! the compiler flag, `-fno-range-check`, to ensure wrap around semantics
!! for BOZ constants, and only versions of the NAG compiler starting with
!! version 17, have implemented submodules. The latest releases of Cray
!! Fortran and IBM Fortran are known to implement the Fortran 2008 extensions,
!! but whether they also implement wrap around overflow is unknown.
!!
!! This implementation has only been tested on little endian processors. It
!! will generate different hashes on big endian processors, but they are
!! believed to be of comparable quality to those generated for little endian
!! processors.
!!
!! No version of this hash is suitable as a cryptographic hash.
!!------------------------------------------------------------------------------
#! Integer kinds to be considered during templating
#:set INT_KINDS = ["int16", "int32", "int64"]
submodule(stdlib_32_bit_hash_functions) stdlib_32_bit_water_hashes
implicit none
contains
pure module function int8_water_hash( key, seed ) result(hash_code)
integer(int32) :: hash_code
integer(int8), intent(in) :: key(0:)
integer(int64), intent(in) :: seed
integer(int32) :: dummy(2)
integer(int64) :: h
integer(int64) :: i
integer(int64) :: len
integer(int64), parameter :: &
waterp0 = int(z'a0761d65', kind=int64), &
waterp1 = int(z'e7037ed1', kind=int64), &
waterp2 = int(z'8ebc6af1', kind=int64), &
waterp3 = int(z'589965cd', kind=int64), &
waterp4 = int(z'1d8e4e27', kind=int64), &
waterp5 = int(z'eb44accb', kind=int64)
len = size(key, kind=int64)
h = seed
do i = 0_int64, len-16, 16
h = watermum(watermum(ieor(waterr32(key(i:)),waterp1), &
ieor(waterr32(key(i+4:)),waterp2)) + h, &
watermum(ieor(waterr32(key(i+8:)),waterp3), &
ieor(waterr32(key(i+12:)),waterp4)))
end do
h = h + waterp5
select case( iand(len, 15_int64) )
case(1)
h = watermum(ieor(waterp2, h), &
ieor(waterr08(key(i:)), waterp1))
case(2)
h = watermum(ieor(waterp3, h), &
ieor(waterr16(key(i:)), waterp4))
case(3)
h = watermum(ieor(waterr16(key(i:)), h), &
ieor(waterr08(key(i+2:)), waterp2))
case(4)
h = watermum(ieor(waterr16(key(i:)), h), &
ieor(waterr16(key(i+2:)), waterp3))
case(5)
h = watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr08(key(i+4:)), waterp1))
case(6)
h = watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr16(key(i+4:)), waterp1))
case(7)
h = watermum(ieor(waterr32(key(i:)), h), &
ieor(ior(ishft(waterr16(key(i+4:)), 8), &
waterr08(key(i+6:))), waterp1))
case(8)
h = watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp0))
case(9)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(ieor(h, waterp4), &
ieor(waterr08(key(i+8:)), waterp3)))
case(10)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(h, ieor(waterr16(key(i+8:)), waterp3)))
case(11)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(h, &
ieor(ior(ishft(waterr16(key(i+8:)),8), &
waterr08(key(i+10:))), &
waterp3)))
case(12)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(ieor(h, waterr32(key(i+8:))), &
waterp4))
case(13)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(ieor(h, waterr32(key(i+8:))), &
ieor(waterr08(key(i+12:)), waterp4)))
case(14)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(ieor(h, waterr32(key(i+8:))), &
ieor(waterr16(key(i+12:)), waterp4)))
case(15)
h = ieor(watermum(ieor(waterr32(key(i:)), h), &
ieor(waterr32(key(i+4:)), waterp2)), &
watermum(ieor(h, waterr32(key(i+8:))), &
ieor(ior(ishft(waterr16(key(i+12:)),8), &
waterr08(key(i+14:))), &
waterp4)))
end select
h = ieor( h, ishft(h,16) ) * ieor( len, waterp0 )
h = h - ishft( seed, -32 )
dummy(1:2) = transfer(h, dummy, 2)
if (little_endian) then
hash_code = dummy(1)
else
hash_code = dummy(2)
end if
contains
pure function watermum( a, b ) result(r)
integer(int64) :: r
integer(int64), intent(in) :: a, b
r = a * b
r = r - ishft(r, -32)
end function watermum
pure function waterr08( p ) result(v)
integer(int64) :: v
integer(int8), intent(in) :: p(:)
if (little_endian) then
v = transfer( [ p(1), 0_int8, 0_int8, 0_int8, &
0_int8, 0_int8, 0_int8, 0_int8 ], v )
else
v = transfer( [ 0_int8, 0_int8, 0_int8, 0_int8, &
0_int8, 0_int8, 0_int8, p(1) ], v )
end if
end function waterr08
pure function waterr16( p ) result(v)
integer(int64) :: v
integer(int8), intent(in) :: p(:)
if (little_endian) then
v = transfer( [ p(1), p(2), 0_int8, 0_int8, &
0_int8, 0_int8, 0_int8, 0_int8 ], v )
else
v = transfer( [ 0_int8, 0_int8, 0_int8, 0_int8, &
0_int8, 0_int8, p(2), p(1) ], v )
end if
end function waterr16
pure function waterr32( p ) result(v)
integer(int64) :: v
integer(int8), intent(in) :: p(:)
if (little_endian) then
v = transfer( [ p(1), p(2), p(3), p(4), &
0_int8, 0_int8, 0_int8, 0_int8 ], v )
else
v = transfer( [ 0_int8, 0_int8, 0_int8, 0_int8, &
p(4), p(3), p(2), p(3) ], v )
end if
end function waterr32
end function int8_water_hash
#:for k1 in INT_KINDS
pure module function ${k1}$_water_hash( key, seed ) result(hash_code)
integer(${k1}$), intent(in) :: key(:)
integer(int64), intent(in) :: seed
integer(int_hash) :: hash_code
hash_code = int8_water_hash( transfer( key, 0_int8, &
bytes_${k1}$*size(key, kind=int64) ), seed)
end function ${k1}$_water_hash
#:endfor
pure module function character_water_hash( key, seed ) result(hash_code)
character(*), intent(in) :: key
integer(int64), intent(in) :: seed
integer(int_hash) :: hash_code
hash_code = int8_water_hash( transfer( key, 0_int8, &
bytes_char*len(key, kind=int64) ), seed)
end function character_water_hash
module subroutine new_water_hash_seed( seed )
integer(int64), intent(inout) :: seed
integer(int64) :: old_seed
real(dp) :: sample(2)
integer(int32) :: part(2)
old_seed = seed
find_seed:do
call random_number( sample )
part = int( floor( sample * 2_int64**32, int64 ) - 2_int64**31, &
int32 )
seed = transfer( part, seed )
if ( seed /= old_seed ) return
end do find_seed
end subroutine new_water_hash_seed
end submodule stdlib_32_bit_water_hashes