Skip to content

Commit

Permalink
Make the C benchmark fairer, by not specialising c or d
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Sep 30, 2012
1 parent 881b0e0 commit 9227325
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
6 changes: 3 additions & 3 deletions benchmarks/Benchmarks.hs
Expand Up @@ -11,7 +11,7 @@ import Foreign.ForeignPtr
import GHC.Exts
import GHC.ST (ST(..))
import Data.Word
import Foreign.C.Types (CSize(..))
import Foreign.C.Types (CInt(..), CSize(..))
import Foreign.Ptr
import Data.ByteString.Internal
import qualified Data.ByteString as B
Expand Down Expand Up @@ -54,7 +54,7 @@ main = do
hsSipHash = HS.hash (HS.SipKey k0 k1)
cSipHash (PS fp off len) =
inlinePerformIO . withForeignPtr fp $ \ptr ->
return $! c_siphash k0 k1 (ptr `plusPtr` off) (fromIntegral len)
return $! c_siphash 2 4 k0 k1 (ptr `plusPtr` off) (fromIntegral len)

withForeignPtr fp5 $ \ p5 ->
withForeignPtr fp8 $ \ p8 ->
Expand Down Expand Up @@ -131,4 +131,4 @@ new (I# n#) = unBA (runST $ ST $ \s1 ->
(# s3, ba #) -> (# s3, BA ba #))

foreign import ccall unsafe "siphash" c_siphash
:: Word64 -> Word64 -> Ptr Word8 -> CSize -> Word64
:: CInt -> CInt -> Word64 -> Word64 -> Ptr Word8 -> CSize -> Word64
26 changes: 21 additions & 5 deletions benchmarks/cbits/siphash.c
Expand Up @@ -14,20 +14,26 @@ typedef uint8_t u8;
v2 += v1; v1=ROTL(v1,17); v1 ^= v2; v2=ROTL(v2,32); \
} while(0)

u64 siphash(u64 k0, u64 k1, const u8 *str, size_t len)
u64 siphash(int c, int d, u64 k0, u64 k1, const u8 *str, size_t len)
{
u64 v0 = 0x736f6d6570736575ull ^ k0;
u64 v1 = 0x646f72616e646f6dull ^ k1;
u64 v2 = 0x6c7967656e657261ull ^ k0;
u64 v3 = 0x7465646279746573ull ^ k1;
u64 b = ((u64) len) << 56;
const u8 *end, *p;
int i;

for (p = str, end = str + (len & ~7); p < end; p += 8) {
u64 m = *(u64 *) p;
v3 ^= m;
SIPROUND;
SIPROUND;
if (c == 2) {
SIPROUND;
SIPROUND;
} else {
for (i = 0; i < c; i++)
SIPROUND;
}
v0 ^= m;
}

Expand All @@ -42,15 +48,25 @@ u64 siphash(u64 k0, u64 k1, const u8 *str, size_t len)
}

v3 ^= b;
SIPROUND;
SIPROUND;
if (c == 2) {
SIPROUND;
SIPROUND;
} else {
for (i = 0; i < c; i++)
SIPROUND;
}
v0 ^= b;

v2 ^= 0xff;
if (d == 4) {
SIPROUND;
SIPROUND;
SIPROUND;
SIPROUND;
} else {
for (i = 0; i < d; i++)
SIPROUND;
}
b = v0 ^ v1 ^ v2 ^ v3;
return b;
}

0 comments on commit 9227325

Please sign in to comment.