Reimplemented hash function in C to avoid overflow trap.

scr-persistent-collective
Salvatore Filippone 6 years ago
parent dc73b5d9ab
commit c7312ab54c

@ -23,7 +23,7 @@ UTIL_MODS = aux/psb_string_mod.o desc/psb_desc_const_mod.o desc/psb_indx_map_mod
aux/psb_ip_reord_mod.o\ aux/psb_ip_reord_mod.o\
aux/psb_i_sort_mod.o aux/psb_s_sort_mod.o aux/psb_d_sort_mod.o \ aux/psb_i_sort_mod.o aux/psb_s_sort_mod.o aux/psb_d_sort_mod.o \
aux/psb_c_sort_mod.o aux/psb_z_sort_mod.o \ aux/psb_c_sort_mod.o aux/psb_z_sort_mod.o \
psb_check_mod.o aux/psb_hash_mod.o\ psb_check_mod.o aux/psb_hash_mod.o aux/hashval.o\
serial/psb_i_base_vect_mod.o serial/psb_i_vect_mod.o\ serial/psb_i_base_vect_mod.o serial/psb_i_vect_mod.o\
serial/psb_d_base_vect_mod.o serial/psb_d_vect_mod.o\ serial/psb_d_base_vect_mod.o serial/psb_d_vect_mod.o\
serial/psb_s_base_vect_mod.o serial/psb_s_vect_mod.o\ serial/psb_s_base_vect_mod.o serial/psb_s_vect_mod.o\

@ -9,7 +9,7 @@
#define H64MASK 0x7FFFFFFFFFFFFFFF #define H64MASK 0x7FFFFFFFFFFFFFFF
#define BMASK 0xFF #define BMASK 0xFF
int32_t psb_chashval_32(int32_t inkey) int32_t psb_c_hashval_32(int32_t inkey)
{ {
uint32_t key, val, i; uint32_t key, val, i;
key = inkey; key = inkey;
@ -22,7 +22,7 @@ int32_t psb_chashval_32(int32_t inkey)
return(val); return(val);
} }
int64_t psb_chashval_64(int64_t inkey) int64_t psb_c_hashval_64(int64_t inkey)
{ {
uint64_t key, val, i; uint64_t key, val, i;
key = inkey; key = inkey;
@ -35,7 +35,7 @@ int64_t psb_chashval_64(int64_t inkey)
return(val); return(val);
} }
int32_t psb_chashval_64_32(int64_t inkey) int32_t psb_c_hashval_64_32(int64_t inkey)
{ {
uint32_t key, val, i; uint32_t key, val, i;
key = inkey; key = inkey;

@ -33,7 +33,7 @@
! !
module psb_hash_mod module psb_hash_mod
use psb_const_mod use psb_const_mod
use iso_c_binding
!> \class psb_hash_mod !> \class psb_hash_mod
!! \brief Simple hash module for storing integer keys. !! \brief Simple hash module for storing integer keys.
!! !!
@ -66,6 +66,8 @@ module psb_hash_mod
integer(psb_ipk_), parameter :: HashDuplicate = 123, HashOK=0, HashOutOfMemory=-512,& integer(psb_ipk_), parameter :: HashDuplicate = 123, HashOK=0, HashOutOfMemory=-512,&
& HashFreeEntry = -1, HashNotFound = -256 & HashFreeEntry = -1, HashNotFound = -256
integer, parameter, private :: psb_c_int = c_int32_t
interface psb_hash_init interface psb_hash_init
module procedure psb_hash_init_v, psb_hash_init_n module procedure psb_hash_init_v, psb_hash_init_n
end interface end interface
@ -87,31 +89,17 @@ module psb_hash_mod
module procedure HashFree module procedure HashFree
end interface end interface
contains interface psb_hashval
function psb_c_hashval_32(key) bind(c) result(res)
import psb_c_int
implicit none
integer(psb_c_int), value :: key
integer(psb_c_int) :: res
end function psb_c_hashval_32
end interface psb_hashval
! contains
! This is based on the djb2 hashing algorithm
! see e.g. http://www.cse.yorku.ca/~oz/hash.html
!
function hashval(key) result(val)
integer(psb_ipk_), intent(in) :: key
integer(psb_ipk_), parameter :: ival=5381, mask=huge(ival)
integer(psb_ipk_) :: key_, val, i
key_ = key
val = ival
do i=1, psb_sizeof_int
val = val * 33 + iand(key_,255)
key_ = ishft(key_,-8)
end do
val = val + ishft(val,-5)
val = iand(val,mask)
end function hashval
function psb_Sizeof_hash_type(hash) result(val) function psb_Sizeof_hash_type(hash) result(val)
type(psb_hash_type) :: hash type(psb_hash_type) :: hash
integer(psb_long_int_k_) :: val integer(psb_long_int_k_) :: val
@ -296,7 +284,7 @@ contains
hsize = hash%hsize hsize = hash%hsize
hmask = hash%hmask hmask = hash%hmask
hk = iand(hashval(key),hmask) hk = iand(psb_hashval(key),hmask)
if (hk == 0) then if (hk == 0) then
hd = 1 hd = 1
else else
@ -359,7 +347,7 @@ contains
end if end if
hsize = hash%hsize hsize = hash%hsize
hmask = hash%hmask hmask = hash%hmask
hk = iand(hashval(key),hmask) hk = iand(psb_hashval(key),hmask)
if (hk == 0) then if (hk == 0) then
hd = 1 hd = 1
else else

@ -598,7 +598,8 @@ contains
if (info >=0) then if (info >=0) then
if (nxt == lip) then if (nxt == lip) then
ncol = max(nxt,ncol) ncol = max(nxt,ncol)
call psb_ensure_size(ncol,idxmap%loc_to_glob,info,pad=-ione,addsz=laddsz) call psb_ensure_size(ncol,idxmap%loc_to_glob,info,&
& pad=-ione,addsz=laddsz)
if (info /= psb_success_) then if (info /= psb_success_) then
info=1 info=1
ch_err='psb_ensure_size' ch_err='psb_ensure_size'

Loading…
Cancel
Save