-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdjb2.wat
84 lines (71 loc) · 2.09 KB
/
djb2.wat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(module
;; 0-64 reserved for param block
(memory (export "memory") 10 1000)
(func (export "djb2_init") (param $ptr i32)
;; setup param block (expect memory to be cleared)
(i32.store (get_local $ptr) (i32.const 5381))
)
(func (export "djb2_update") (param $ptr i32) (param $input i32) (param $input_end i32)
(local $hash i32)
(set_local $hash (i32.load (get_local $ptr)))
(block $end
(loop $start
(br_if $end (i32.eq (get_local $input) (get_local $input_end)))
(set_local $hash
(i32.add
(get_local $hash)
(i32.add
(i32.load8_u (get_local $input))
(i32.shl (get_local $hash) (i32.const 5))
)
)
)
(set_local $input (i32.add (get_local $input) (i32.const 1)))
(br $start)
)
)
(i32.store (get_local $ptr) (get_local $hash))
)
(func (export "djb2") (param $input i32) (param $input_end i32) (result i32)
(local $hash i32)
(set_local $hash (i32.const 5381))
(block $end
(loop $start
(br_if $end (i32.eq (get_local $input) (get_local $input_end)))
(set_local $hash
(i32.add
(get_local $hash)
(i32.add
(i32.load8_u (get_local $input))
(i32.shl (get_local $hash) (i32.const 5))
)
)
)
(set_local $input (i32.add (get_local $input) (i32.const 1)))
(br $start)
)
)
(get_local $hash)
)
(func (export "djb2_xor") (param $input i32) (param $input_end i32) (result i32)
(local $hash i32)
(set_local $hash (i32.const 5381))
(block $end
(loop $start
(br_if $end (i32.eq (get_local $input) (get_local $input_end)))
(set_local $hash
(i32.add
(get_local $hash)
(i32.xor
(i32.load8_u (get_local $input))
(i32.shl (get_local $hash) (i32.const 5))
)
)
)
(set_local $input (i32.add (get_local $input) (i32.const 1)))
(br $start)
)
)
(get_local $hash)
)
)