-
Notifications
You must be signed in to change notification settings - Fork 173
/
stdlib_hash_32bit.fypp
273 lines (221 loc) · 9.35 KB
/
stdlib_hash_32bit.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
#! Integer kinds to be considered during templating
#:set INT_KINDS = ["int8", "int16", "int32", "int64"]
module stdlib_hash_32bit
use, intrinsic :: iso_fortran_env, only : &
character_storage_size
use stdlib_kinds, only: &
dp, &
int8, &
int16, &
int32, &
int64
implicit none
private
integer, parameter, public :: &
int_hash = int32
!! The number of bits in the output hash
! pow32_over_phi is the odd integer that most closely approximates 2**32/phi,
! where phi is the golden ratio 1.618...
integer(int32), parameter :: &
pow32_over_phi = int( z'9E3779B9', int32 )
! The number of bits used by each integer type
integer, parameter :: &
! Should be 8
bits_int8 = bit_size(0_int8), &
! Should be 16
bits_int16 = bit_size(0_int16), &
! Should be 32
bits_int32 = bit_size(0_int32), &
! Should be 64
bits_int64 = bit_size(0_int64)
integer, parameter :: &
! Should be 1
bytes_int8 = bits_int8/bits_int8, &
! Should be 2
bytes_int16 = bits_int16/bits_int8, &
! Should be 4
bytes_int32 = bits_int32/bits_int8, &
! Should be 8
bytes_int64 = bits_int64/bits_int8
integer, parameter :: &
bits_char = character_storage_size, &
bytes_char = bits_char/bits_int8
! Dealing with different endians
logical, parameter, public :: &
little_endian = ( 1 == transfer([1_int8, 0_int8], 0_int16) )
public :: &
fibonacci_hash, &
fnv_1_hash, &
fnv_1a_hash, &
new_nmhash32_seed, &
new_nmhash32x_seed, &
new_water_hash_seed,&
nmhash32, &
nmhash32x, &
odd_random_integer, &
universal_mult_hash,&
water_hash
interface fnv_1_hash
!! Version: experimental
!!
!! FNV_1 interfaces
!! ([Specification](../page/specs/stdlib_hash_procedures.html#fnv_1_hash-calculates-a-hash-code-from-a-key))
#:for k1 in INT_KINDS
pure module function ${k1}$_fnv_1( key ) result(hash_code)
!! FNV_1 hash function for rank 1 array keys of kind ${k1}$
integer(${k1}$), intent(in) :: key(:)
integer(int_hash) :: hash_code
end function ${k1}$_fnv_1
#:endfor
elemental module function character_fnv_1( key ) result(hash_code)
!! FNV_1 hash function for default character string keys
character(*), intent(in) :: key
integer(int_hash) :: hash_code
end function character_fnv_1
end interface fnv_1_hash
interface fnv_1a_hash
!! Version: experimental
!!
!! FNV_1A interfaces
!! ([Specification](../page/specs/stdlib_hash_procedures.html#fnv_1a_hash-calculates-a-hash-code-from-a-key))
#:for k1 in INT_KINDS
pure module function ${k1}$_fnv_1a( key ) result(hash_value)
!! FNV_1A hash function for rank 1 array keys of kind ${k1}$
integer(${k1}$), intent(in) :: key(:)
integer(int_hash) :: hash_value
end function ${k1}$_fnv_1a
#:endfor
elemental module function character_fnv_1a( key ) result(hash_value)
!! FNV_1A hash function for default character string keys
character(*), intent(in) :: key
integer(int_hash) :: hash_value
end function character_fnv_1a
end interface fnv_1a_hash
interface nmhash32
!! Version: experimental
!!
!! NMHASH32 interfaces
!! ([Specification](../page/specs/stdlib_hash_procedures.html#nmhash32-calculates-a-hash-code-from-a-key-and-a-seed))
#:for k1 in INT_KINDS
pure module function ${k1}$_nmhash32( key, seed ) &
result(hash_value)
!! NMHASH32 hash function for rank 1 array keys of kind ${k1}$
integer(${k1}$), intent(in) :: key(0:)
integer(int32), intent(in) :: seed
integer(int32) :: hash_value
end function ${k1}$_nmhash32
#:endfor
elemental module function character_nmhash32( key, seed ) &
result(hash_value)
!! NMHASH32 hash function for default character string keys
character(*), intent(in) :: key
integer(int32), intent(in) :: seed
integer(int32) :: hash_value
end function character_nmhash32
end interface nmhash32
interface nmhash32x
!! Version: experimental
!!
!! NMHASH32X interfaces
!! ([Specification](file:///home/jvandenp/stdlib/API-doc/page/specs/stdlib_hash_procedures.html#nmhash32x-calculates-a-hash-code-from-a-key-and-a-seed))
#:for k1 in INT_KINDS
pure module function ${k1}$_nmhash32x( key, seed ) &
result(hash_value)
!! NMHASH32 hash function for rank 1 array keys of kind ${k1}$
integer(${k1}$), intent(in) :: key(0:)
integer(int32), intent(in) :: seed
integer(int32) :: hash_value
end function ${k1}$_nmhash32x
#:endfor
elemental module function character_nmhash32x( key, seed ) &
result(hash_value)
!! NMHASH32 hash function for default character string keys
character(*), intent(in) :: key
integer(int32), intent(in) :: seed
integer(int32) :: hash_value
end function character_nmhash32x
end interface nmhash32x
interface water_hash
!! Version: experimental
!!
!! WATER_HASH interfaces
!! ([Specification](../page/specs/stdlib_hash_procedures.html#water_hash-calculates-a-hash-code-from-a-key-and-a-seed))
#:for k1 in INT_KINDS
pure module function ${k1}$_water_hash( key, seed ) &
result(hash_code)
!! WATER HASH function for rank 1 array keys of kind ${k1}$
integer(${k1}$), intent(in) :: key(0:)
integer(int64), intent(in) :: seed
integer(int_hash) :: hash_code
end function ${k1}$_water_hash
#:endfor
elemental module function character_water_hash( key, seed ) &
result(hash_code)
!! WATER hash function for default character string keys
character(*), intent(in) :: key
integer(int64), intent(in) :: seed
integer(int_hash) :: hash_code
end function character_water_hash
end interface water_hash
interface new_water_hash_seed
!! Version: experimental
!!
!! ([Specification](file:///home/jvandenp/stdlib/API-doc/page/specs/stdlib_hash_procedures.html#new_water_hash_seed-returns-a-valid-input-seed-for-water_hash))
module subroutine new_water_hash_seed( seed )
integer(int64), intent(inout) :: seed
end subroutine new_water_hash_seed
end interface new_water_hash_seed
interface new_nmhash32_seed
!! Version: experimental
!!
!! ([Specification](../page/specs/stdlib_hash_procedures.html#new_nmhash32_seed-returns-a-valid-input-seed-for-nmhash32)
module subroutine new_nmhash32_seed( seed )
integer(int32), intent(inout) :: seed
end subroutine new_nmhash32_seed
end interface new_nmhash32_seed
interface new_nmhash32x_seed
!! Version: experimental
!!
!! ([Specification](../page/specs/stdlib_hash_procedures.html#new_nmhash32x_seed-returns-a-valid-input-seed-for-nmhash32x))
module subroutine new_nmhash32x_seed( seed )
integer(int32), intent(inout) :: seed
end subroutine new_nmhash32x_seed
end interface new_nmhash32x_seed
contains
elemental function fibonacci_hash( key, nbits ) result( sample )
!! Version: experimental
!!
!! Maps the 32 bit integer `key` to an unsigned integer value with only `nbits`
!! bits where `nbits` is less than 32
!! ([Specification](../page/specs/stdlib_hash_procedures.html#fibonacci_hash-maps-an-integer-to-a-smaller-number-of-bits))
integer(int32), intent(in) :: key
integer, intent(in) :: nbits
integer(int32) :: sample
sample = ishft( key*pow32_over_phi, -32 + nbits )
end function fibonacci_hash
elemental function universal_mult_hash( key, seed, nbits ) result( sample )
!! Version: experimental
!!
!! Uses the "random" odd 32 bit integer `seed` to map the 32 bit integer `key` to
!! an unsigned integer value with only `nbits` bits where `nbits` is less than 32
!! ([Specification](../page/specs/stdlib_hash_procedures.html#universal_mult_hash-maps-an-integer-to-a-smaller-number-of-bits))
integer(int32), intent(in) :: key
integer(int32), intent(in) :: seed
integer, intent(in) :: nbits
integer(int32) :: sample
sample = ishft( key*seed, -32 + nbits )
end function universal_mult_hash
subroutine odd_random_integer( harvest )
!! Version: experimental
!!
!! Returns a 32 bit pseudo random integer, `harvest`, distributed uniformly over
!! the odd integers of the `int32` kind.
!! ([Specification](../page/specs/stdlib_hash_procedures.html#odd_random_integer-returns-an-odd-integer))
integer(int32), intent(out) :: harvest
real(dp) :: sample
call random_number( sample )
harvest = int( floor( sample * 2_int64**32, int64 ) - 2_int64**31, &
int32 )
harvest = ishft( harvest, 1 ) + 1_int32
end subroutine odd_random_integer
end module stdlib_hash_32bit