blob: aa1c621d712eb6d50ff1fd65256fc2dfdb76b2c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{-# LANGUAGE MagicHash, BangPatterns #-}
{-# OPTIONS_GHC -O0 #-}
import Foreign.C
import Data.Word
import Foreign.Ptr
import GHC.Exts
import Control.Exception
hashStr :: Ptr Word8 -> Int -> Int
hashStr (Ptr a#) (I# len#) = loop 0# 0#
where
loop h n | n GHC.Exts.==# len# = I# h
| otherwise = loop h2 (n GHC.Exts.+# 1#)
where !c = ord# (indexCharOffAddr# a# n)
!h2 = (c GHC.Exts.+# (h GHC.Exts.*# 128#)) `remInt#` 4091#
-- Infinite loops with new code generator + C-- optimizations
main = do
withCStringLen "ff" $ \(ptr, l) -> do
print (hashStr (castPtr ptr) l)
|