-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhaming_distance_simd.wat
67 lines (61 loc) · 2.1 KB
/
haming_distance_simd.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
(module
(import "env" "log" (func $log (param i32))) ;; Import a logging function
(memory (export "memory") 1) ;; Define and export memory
(func $hamming_distance (param $ptrA i32) (param $ptrB i32) (param $len i32) (result i32)
(local $dist i32)
(local.set $dist (i32.const 0)) ;; Initialize distance
(block $exit ;; Define a breakable block for the loop
(loop $loop ;; Start loop with a label
(if (i32.eqz (local.get $len)) ;; Break if length is 0
(br $exit)
)
;; Log the current pointer
(call $log (local.get $ptrA))
;; XOR the vectors and calculate popcount
(local.set $dist
(i32.add
(local.get $dist)
(i32.add
(i32.popcnt (i32x4.extract_lane 0
(v128.xor
(v128.load (local.get $ptrA))
(v128.load (local.get $ptrB))
)
))
(i32.add
(i32.popcnt (i32x4.extract_lane 1
(v128.xor
(v128.load (local.get $ptrA))
(v128.load (local.get $ptrB))
)
))
(i32.add
(i32.popcnt (i32x4.extract_lane 2
(v128.xor
(v128.load (local.get $ptrA))
(v128.load (local.get $ptrB))
)
))
(i32.popcnt (i32x4.extract_lane 3
(v128.xor
(v128.load (local.get $ptrA))
(v128.load (local.get $ptrB))
)
))
)
)
)
)
)
;; Advance pointers by 16 bytes (128 bits)
(local.set $ptrA (i32.add (local.get $ptrA) (i32.const 16)))
(local.set $ptrB (i32.add (local.get $ptrB) (i32.const 16)))
(local.set $len (i32.sub (local.get $len) (i32.const 16)))
;; Continue loop
(br $loop)
)
)
(local.get $dist) ;; Return the distance
)
(export "hamming_distance" (func $hamming_distance))
)