Skip to content

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

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f731dd5
Hash functions for the Fotran Standard Library
wclodius2 Oct 19, 2021
b8f84f7
Updated doc/specs/index.md file
wclodius2 Oct 19, 2021
4147dbf
Added a compiler option
wclodius2 Oct 20, 2021
1c31148
subject line
wclodius2 Oct 20, 2021
a38867d
Fixed Makefiles
wclodius2 Oct 20, 2021
d8b2fde
Fixed Makefile.manual
wclodius2 Oct 20, 2021
7be16f3
Made corrections in the comments andthe code
wclodius2 Nov 4, 2021
00e622a
Merge https://github.com/fortran-lang/stdlib into hash_functions
wclodius2 Nov 6, 2021
3489f00
Changed discussion of keys and stdlib_32_bit_hash_functions,md
wclodius2 Nov 6, 2021
cbbf0b2
Added files to test the validity of the hash functions in libstdlib.a.
wclodius2 Nov 12, 2021
1134094
Changed README.mmd to READMME.md
wclodius2 Nov 12, 2021
74716d1
Deleted the README.mmd file
wclodius2 Nov 12, 2021
52621af
Changed filename from test_hash_validity.f90 to hash_validity_test.f90
wclodius2 Nov 12, 2021
26f1272
Removed test_hash_validity.f90
wclodius2 Nov 12, 2021
387bff8
Modified Makefile.validation and README.mb.
wclodius2 Nov 13, 2021
2a83793
Update ../CMMakeLists.tst
wclodius2 Nov 13, 2021
bb0ba66
Updating changes in mode.
wclodius2 Nov 13, 2021
df5d865
Update mode
wclodius2 Nov 13, 2021
ff62a21
Trying to get two where I can rebase
wclodius2 Nov 13, 2021
c68388b
Getting to where I can merge
wclodius2 Nov 13, 2021
dda8997
Getting to rebase
wclodius2 Nov 13, 2021
a68c1f5
getting to rebase
wclodius2 Nov 13, 2021
5d3e281
Getting ready for rebase
wclodius2 Nov 13, 2021
8cd021d
Getting ready for rebase
wclodius2 Nov 13, 2021
38e7cae
Getting ready for rebase
wclodius2 Nov 13, 2021
29ae7cb
Reset the executable bits
wclodius2 Nov 13, 2021
3cf3bea
Corrected bit permissions.
wclodius2 Nov 13, 2021
2395e93
Changed file permissions to non-executable.
wclodius2 Nov 13, 2021
bd41342
Corrected executable mode
wclodius2 Nov 13, 2021
ab89f1f
Corrected file permissions.
wclodius2 Nov 13, 2021
13ef4bd
Corrected executable permissions
wclodius2 Nov 13, 2021
fb8394e
Corrected executable mode.
wclodius2 Nov 13, 2021
c2cb605
Corrected executable bit.
wclodius2 Nov 13, 2021
7087ccc
Corrected executable bit.
wclodius2 Nov 13, 2021
57664f7
Corrected executable bit.
wclodius2 Nov 13, 2021
43bca7f
Corrected executable bit.
wclodius2 Nov 13, 2021
b7f4fc8
Corrected executable bit.
wclodius2 Nov 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.manual
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per previous comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my previous comments.

FYPPFLAGS ?=

export FC
Expand Down
2 changes: 2 additions & 0 deletions doc/specs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This is and index/directory of the specifications (specs) for each new module/fe
- [ascii](./stdlib_ascii.html) - Procedures for handling ASCII characters
- [bitsets](./stdlib_bitsets.html) - Bitset data types and procedures
- [error](./stdlib_error.html) - Catching and handling errors
- [hash\_functions](./stdlib_has_functions.html) - Hashing integer
vectors or character strings
- [IO](./stdlib_io.html) - Input/output helper & convenience
- [kinds](./stdlib_kinds.html) - Kind parameters
- [linalg](./stdlib_linalg.html) - Linear Algebra
Expand Down
1,697 changes: 1,697 additions & 0 deletions doc/specs/stdlib_hash_functions.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

# Create a list of the files to be preprocessed
set(fppFiles
stdlib_32_bit_fnv_hashes.fypp
stdlib_32_bit_hash_functions.fypp
stdlib_32_bit_nmhashes.fypp
stdlib_32_bit_water_hashes.fypp
stdlib_64_bit_fnv_hashes.fypp
stdlib_64_bit_hash_functions.fypp
stdlib_64_bit_pengy_hashes.fypp
stdlib_64_bit_spookyv2_hashes.fypp
stdlib_ascii.fypp
stdlib_bitsets.fypp
stdlib_bitsets_64.fypp
Expand Down
24 changes: 24 additions & 0 deletions src/Makefile.manual
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
SRCFYPP = \
stdlib_32_bit_fnv_hashes.fypp \
stdlib_32_bit_hash_functions.fypp \
stdlib_32_bit_nmhashes.fypp \
stdlib_32_bit_water_hashes.fypp \
stdlib_64_bit_fnv_hashes.fypp \
stdlib_64_bit_hash_functions.fypp \
stdlib_64_bit_pengy_hashes.fypp \
stdlib_64_bit_spookyv2_hashes.fypp \
stdlib_ascii.fypp \
stdlib_bitsets_64.fypp \
stdlib_bitsets_large.fypp \
Expand Down Expand Up @@ -74,6 +82,22 @@ $(SRCGEN): %.f90: %.fypp common.fypp

# Fortran module dependencies
f18estop.o: stdlib_error.o
stdlib_32_bit_fnv_hashes.o: \
stdlib_32_bit_hash_functions.o
stdlib_32_bit_hash_functions.o: \
stdlib_kinds.o
stdlib_32_bit_nmhashes.o: \
stdlib_32_bit_hash_functions.o
stdlib_32_bit_water_hashes.o: \
stdlib_32_bit_hash_functions.o
stdlib_64_bit_fnv_hashes.o: \
stdlib_64_bit_hash_functions.o
stdlib_64_bit_hash_functions.o: \
stdlib_kinds.o
stdlib_64_bit_pengy_hashes.o: \
stdlib_64_bit_hash_functions.o
stdlib_64_bit_spookyv2_hashes.o: \
stdlib_64_bit_hash_functions.o
stdlib_ascii.o: stdlib_kinds.o
stdlib_bitsets.o: stdlib_kinds.o
stdlib_bitsets_64.o: stdlib_bitsets.o
Expand Down
126 changes: 126 additions & 0 deletions src/stdlib_32_bit_fnv_hashes.fypp
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
Loading