10099c1b48c0d0016a6eb4d3b67e7b3ffab1211d
[ghc.git] / testsuite / tests / codeGen / should_compile / cg008.hs
1 {-# LANGUAGE MagicHash, BangPatterns #-}
2 {-# OPTIONS_GHC -O0 #-}
3
4 -- Variant of cgrun066; compilation as a module is different.
5
6 module Cg008 (hashStr) where
7
8 import Foreign.C
9 import Data.Word
10 import Foreign.Ptr
11 import GHC.Exts
12
13 import Control.Exception
14
15 hashStr :: Ptr Word8 -> Int -> Int
16 hashStr (Ptr a#) (I# len#) = loop 0# 0#
17 where
18 loop h n | n GHC.Exts.==# len# = I# h
19 | otherwise = loop h2 (n GHC.Exts.+# 1#)
20 where !c = ord# (indexCharOffAddr# a# n)
21 !h2 = (c GHC.Exts.+# (h GHC.Exts.*# 128#)) `remInt#` 4091#