-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_opt.wat
79 lines (67 loc) · 1.71 KB
/
string_opt.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
;; -----------------------------------------------------------------------------
;; ### string_opt.wat
;; Optimized versions of strcat, strcpy and strlen
;; By: anachan01h
;; -----------------------------------------------------------------------------
(import "js" "memory" (memory 1))
;; -----------------------------------------------------------------------------
;; char *strcat(char *dest, char *src)
;; -----------------------------------------------------------------------------
;; char *strcpy(char *dest, char *src)
;; -----------------------------------------------------------------------------
;; int strlen(char *str)
(func $strlen (export "strlen") (param $str i32) (result i32)
(local $ptr i32)
(local $dword i32)
local.get $str
local.set $ptr
block $break
loop $iter
local.get $ptr
i32.load
local.tee $dword
local.get $ptr
i32.const 4
i32.add
local.set $ptr
i32.const 0x01010101
i32.sub
i32.const 0xFFFFFFFF
local.get $dword
i32.xor
i32.const 0x80808080
i32.and
i32.and
i32.eqz
br_if $iter
local.get $ptr
i32.const 4
i32.sub
local.tee $ptr
i32.load8_u
i32.eqz
br_if $break
local.get $ptr
i32.const 1
i32.add
local.tee $ptr
i32.load8_u
i32.eqz
br_if $break
local.get $ptr
i32.const 1
i32.add
local.tee $ptr
i32.load8_u
i32.eqz
br_if $break
local.get $ptr
i32.const 1
i32.add
local.set $ptr
end
end
local.get $ptr
local.get $str
i32.sub
)