-
Notifications
You must be signed in to change notification settings - Fork 191
Hash functions #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hash functions #554
Changes from all commits
f731dd5
b8f84f7
4147dbf
1c31148
a38867d
d8b2fde
7be16f3
00e622a
3489f00
cbbf0b2
1134094
74716d1
52621af
26f1272
387bff8
2a83793
bb0ba66
df5d865
ff62a21
c68388b
dda8997
a68c1f5
5d3e281
8cd021d
38e7cae
29ae7cb
3cf3bea
2395e93
bd41342
ab89f1f
13ef4bd
fb8394e
c2cb605
7087ccc
57664f7
43bca7f
b7f4fc8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU) | |
endif() | ||
add_compile_options(-fimplicit-none) | ||
add_compile_options(-ffree-line-length-132) | ||
add_compile_options(-fno-range-check) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in the documentation about this -- if it isn't needed for gfortran 9+ it might be better not to have this -- otherwise if it is needed, we should consider whether it needs documenting in the README (as it is easy to compile stdlib with user-specified flags, which could miss this). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is required for version 9. |
||
add_compile_options(-Wall) | ||
add_compile_options(-Wextra) | ||
add_compile_options(-Wimplicit-procedure) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Fortran stdlib Makefile | ||
|
||
FC ?= gfortran | ||
FFLAGS ?= -Wall -Wextra -Wimplicit-interface -fPIC -g -fcheck=all | ||
FFLAGS ?= -Wall -Wextra -Wimplicit-interface -fPIC -g -fcheck=all -fno-range-check | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per previous comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my previous comments. |
||
FYPPFLAGS ?= | ||
|
||
export FC | ||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
!!------------------------------------------------------------------------------ | ||
!! `FNV_1_HASH` and `FNV_1A_Hash` are translations to Fortran 2008 of the | ||
!! `FNV-1` and `FNV-1a` hash functions of Glenn Fowler, Landon Curt Noll, | ||
!! and Phong Vo, that has been released into the public domain. Permission | ||
!! has been granted, by Landon Curt Noll, for the use of these algorithms | ||
!! in the Fortran Standard Library. A description of these functions is | ||
!! available at https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function. | ||
!!------------------------------------------------------------------------------ | ||
|
||
!#! Integer kinds to be considered during templating | ||
#:set INT_KINDS = ["int16", "int32", "int64"] | ||
|
||
submodule(stdlib_32_bit_hash_functions) stdlib_32_bit_fnv_hashes | ||
!! An implementation of the FNV hashes 1 and 1a of Glenn Fowler, Landon Curt | ||
!! Noll, and Kiem-Phong-Vo, | ||
!! https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function | ||
implicit none | ||
|
||
integer(int_hash), parameter :: & | ||
offset_basis = int( z'811C9DC5', int_hash ), & | ||
prime = int( z'01000193', int_hash ) | ||
|
||
contains | ||
|
||
pure module function int8_fnv_1( key ) result(hash_code) | ||
!! The original FNV-1 8-bit key algorithm. | ||
integer(int8), intent(in) :: key(:) | ||
integer(int_hash) :: hash_code | ||
|
||
integer(int64) :: i | ||
|
||
hash_code = offset_basis | ||
do i=1_int64, size(key, kind=int64) | ||
hash_code = hash_code * prime | ||
if ( little_endian ) then | ||
hash_code = ieor( hash_code, & | ||
transfer( [key(i), 0_int8, 0_int8, 0_int8], & | ||
0_int_hash ) ) | ||
else | ||
hash_code = ieor( hash_code, & | ||
transfer( [0_int8, 0_int8, 0_int8, key(i)], & | ||
0_int_hash ) ) | ||
end if | ||
end do | ||
|
||
end function int8_fnv_1 | ||
|
||
|
||
#:for k1 in INT_KINDS | ||
pure module function ${k1}$_fnv_1( key ) result(hash_code) | ||
! A ${k1}$ array key wrapper for the FNV-1 algorithm. | ||
integer(${k1}$), intent(in) :: key(:) | ||
integer(int_hash) :: hash_code | ||
|
||
hash_code = int8_fnv_1( transfer( key, 0_int8, & | ||
bytes_${k1}$* & | ||
size( key, kind=int64 ) ) ) | ||
|
||
end function ${k1}$_fnv_1 | ||
|
||
#:endfor | ||
|
||
|
||
pure module function character_fnv_1( key ) result(hash_code) | ||
! A default character key wrapper for the FNV-1 algorithm. | ||
character(*), intent(in) :: key | ||
integer(int_hash) :: hash_code | ||
|
||
hash_code = int8_fnv_1( transfer( key, & | ||
0_int8, & | ||
bytes_char* & | ||
len(key, kind=int64) ) ) | ||
|
||
end function character_fnv_1 | ||
|
||
|
||
pure module function int8_fnv_1a( key ) result(hash_code) | ||
!! The original FNV-1a 8-bit key algorithm. | ||
integer(int8), intent(in) :: key(:) | ||
integer(int_hash) :: hash_code | ||
|
||
integer(int64) :: i | ||
|
||
hash_code = offset_basis | ||
do i=1_int64, size(key, kind=int64) | ||
if ( little_endian ) then | ||
hash_code = ieor( hash_code, & | ||
transfer( [key(i), 0_int8, 0_int8, 0_int8], & | ||
0_int_hash ) ) | ||
else | ||
hash_code = ieor( hash_code, & | ||
transfer( [0_int8, 0_int8, 0_int8, key(i)], & | ||
0_int_hash ) ) | ||
end if | ||
hash_code = hash_code * prime | ||
end do | ||
|
||
end function int8_fnv_1a | ||
|
||
|
||
#:for k1 in INT_KINDS | ||
pure module function ${k1}$_fnv_1a( key ) result(hash_code) | ||
! A ${k1}$ array key wrapper for the FNV-1a algorithm. | ||
integer(${k1}$), intent(in) :: key(:) | ||
integer(int_hash) :: hash_code | ||
|
||
hash_code = int8_fnv_1a( transfer( key, 0_int8, & | ||
bytes_${k1}$* & | ||
size(key, kind=int64)) ) | ||
|
||
end function ${k1}$_fnv_1a | ||
|
||
#:endfor | ||
|
||
pure module function character_fnv_1a( key ) result(hash_code) | ||
! A default character key wrapper for the FNV-1 algorithm. | ||
character(*), intent(in) :: key | ||
integer(int_hash) :: hash_code | ||
|
||
hash_code = int8_fnv_1a( transfer( key, 0_int8, & | ||
(bits_char/bits_int8)* & | ||
len(key, kind=int64) ) ) | ||
|
||
end function character_fnv_1a | ||
|
||
end submodule stdlib_32_bit_fnv_hashes |
Uh oh!
There was an error while loading. Please reload this page.