king: new mug
@@ -5,6 +5,7 @@ module Urbit.Noun.Mug where
import ClassyPrelude
import Data.Bits
+import Data.ByteString.Builder
import Urbit.Atom
import Data.Hash.Murmur (murmur3)
@@ -13,14 +14,7 @@ type Mug = Word32
{-# INLINE mugBS #-}
mugBS :: ByteString -> Word32
-mugBS = go 0xcafebabe
- where
- go seed buf =
- let haz = murmur3 seed buf
- ham = shiftR haz 31 `xor` (haz .&. 0x7fff_ffff)
- in if ham == 0
- then go (seed + 1) buf
- else ham
+mugBS = mum 0xcafe_babe 0x7fff
-- XX is there a way to do this without copy?
{-# INLINE mugAtom #-}
@@ -29,4 +23,16 @@ mugAtom = mugBS . atomBytes
{-# INLINE mugBoth #-}
mugBoth :: Word32 -> Word32 -> Word32
-mugBoth m n = mugAtom $ fromIntegral $ m `xor` 0x7fff_ffff `xor` n
+mugBoth m n = mum 0xdead_beef 0xfffe
+ $ toStrict $ toLazyByteString (word32LE m <> word32LE n)
+
+mum :: Word32 -> Word32 -> ByteString -> Word32
+mum syd fal key = go syd 0
+ where
+ go syd 8 = fal
+ go syd i =
+ let haz = murmur3 syd key
+ ham = shiftR haz 31 `xor` (haz .&. 0x7fff_ffff)
+ in if ham /= 0
+ then ham
+ else go (syd + 1) (i + 1)
\ No newline at end of file
