{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Cuckoo.Internal.HashFunctions
(
saltedFnv1aPtr
, saltedFnv1aStorable
, saltedFnv1aByteString
, saltedSipHashPtr
, saltedSipHashStorable
, saltedSipHashByteString
, sipHashInternal
) where
import qualified Data.ByteString as B
import Data.Hash.FNV1
import Data.Hash.SipHash
import Foreign
import Data.Cuckoo.Internal
saltedFnv1aPtr
:: Int
-> Ptr Word8
-> Int
-> Word64
saltedFnv1aPtr :: Int -> Ptr Word8 -> Int -> Word64
saltedFnv1aPtr Int
s Ptr Word8
p Int
ps = (Ptr Word8 -> Int -> Word64 -> IO Word64)
-> Ptr Word8 -> Int -> Word64 -> Word64
forall b.
(Ptr Word8 -> Int -> b -> IO b) -> Ptr Word8 -> Int -> b -> b
hashPtr_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ Ptr Word8
p Int
ps
(Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aPtr #-}
saltedFnv1aStorable
:: Storable a
=> Int
-> a
-> Word64
saltedFnv1aStorable :: Int -> a -> Word64
saltedFnv1aStorable Int
s a
x = (Ptr Word8 -> Int -> Word64 -> IO Word64) -> a -> Word64 -> Word64
forall a b.
Storable a =>
(Ptr Word8 -> Int -> b -> IO b) -> a -> b -> b
hashStorable_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ a
x
(Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aStorable #-}
saltedFnv1aByteString
:: Int
-> B.ByteString
-> Word64
saltedFnv1aByteString :: Int -> ByteString -> Word64
saltedFnv1aByteString Int
s ByteString
b = (Ptr Word8 -> Int -> Word64 -> IO Word64)
-> ByteString -> Word64 -> Word64
forall b. (Ptr Word8 -> Int -> b -> IO b) -> ByteString -> b -> b
hashByteString_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ ByteString
b
(Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aByteString #-}
saltedSipHashPtr
:: Int
-> Ptr Word8
-> Int
-> Word64
saltedSipHashPtr :: Int -> Ptr Word8 -> Int -> Word64
saltedSipHashPtr Int
s Ptr Word8
ptr Int
l = (Ptr Word8 -> Int -> IO Word64) -> Ptr Word8 -> Int -> Word64
forall b. (Ptr Word8 -> Int -> IO b) -> Ptr Word8 -> Int -> b
hashPtr (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
1043639) Ptr Word8
ptr Int
l
{-# INLINE saltedSipHashPtr #-}
saltedSipHashStorable
:: Storable a
=> Int
-> a
-> Word64
saltedSipHashStorable :: Int -> a -> Word64
saltedSipHashStorable Int
s = (Ptr Word8 -> Int -> IO Word64) -> a -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
914279)
{-# INLINE saltedSipHashStorable #-}
saltedSipHashByteString
:: Int
-> B.ByteString
-> Word64
saltedSipHashByteString :: Int -> ByteString -> Word64
saltedSipHashByteString Int
s = (Ptr Word8 -> Int -> IO Word64) -> ByteString -> Word64
forall b. (Ptr Word8 -> Int -> IO b) -> ByteString -> b
hashByteString (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
914279)
{-# INLINE saltedSipHashByteString #-}
sipHashInternal :: Storable a => Int -> a -> Word64
sipHashInternal :: Int -> a -> Word64
sipHashInternal Int
s = (Ptr Word8 -> Int -> IO Word64) -> a -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 Word64
994559 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
713243))
{-# INLINE sipHashInternal #-}