Import Upstream version 20180207
[hcoop/debian/mlton.git] / benchmark / tests / checksum.sml
CommitLineData
7f918cf1
CE
1(* Author: sweeks@sweeks.com
2 * This code is based on the following paper.
3 * The Performance of FoxNet 2.0
4 * Herb Derby
5 * CMU-CS-99-137
6 * June 1999
7 *)
8
9fun checkOne (new, ac) =
10 let open Word32
11 in ac + >> (new, 0w16) + andb (new, 0wxFFFF)
12 end
13
14fun fold f b (buf, first, last) =
15 let
16 fun loop (i, ac) =
17 if i > last
18 then ac
19 else loop (i + 1,
20 f (Word32.fromLarge (PackWord32Little.subArr (buf, i)),
21 ac))
22 in
23 loop (first, b)
24 end
25
26fun checksum buf = fold checkOne 0w0 buf
27
28structure Main =
29 struct
30 fun doit n =
31 let
32 val first = 0
33 val size = 10000000
34 val buf = Word8Array.array (size, 0w0)
35 val bytesPerWord = 4
36 val last = size div bytesPerWord - 1
37 val rec loop =
38 fn 0 => ()
39 | n =>
40 let val w = checksum (buf, first, last)
41 val _ = if w <> 0w0 then raise Fail "bug" else ()
42 in loop (n - 1)
43 end
44 in loop n
45 end
46 end