Skip to content

Commit b0cad7e

Browse files
aykevldeadprogram
authored andcommitted
runtime: add support for math intrinsics where supported
In particular, add support for a few math intrinsics for WebAssembly, but add a few intrinsics to other systems as well at the same time. Some may be missing still but will be easy to add if needed. This increases the performance of one example by 50% to 100% depending on the browser: the bottleneck was the inefficient sqrt implementation.
1 parent 00cc486 commit b0cad7e

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

src/runtime/math.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ func math_Cbrt(x float64) float64 { return math_cbrt(x) }
5656
func math_cbrt(x float64) float64
5757

5858
//go:linkname math_Ceil math.Ceil
59-
func math_Ceil(x float64) float64 { return math_ceil(x) }
59+
func math_Ceil(x float64) float64 {
60+
if GOARCH == "arm64" || GOARCH == "wasm" {
61+
return llvm_ceil(x)
62+
}
63+
return math_ceil(x)
64+
}
65+
66+
//go:export llvm.ceil.f64
67+
func llvm_ceil(x float64) float64
6068

6169
//go:linkname math_ceil math.ceil
6270
func math_ceil(x float64) float64
@@ -104,7 +112,15 @@ func math_Exp2(x float64) float64 { return math_exp2(x) }
104112
func math_exp2(x float64) float64
105113

106114
//go:linkname math_Floor math.Floor
107-
func math_Floor(x float64) float64 { return math_floor(x) }
115+
func math_Floor(x float64) float64 {
116+
if GOARCH == "arm64" || GOARCH == "wasm" {
117+
return llvm_floor(x)
118+
}
119+
return math_floor(x)
120+
}
121+
122+
//go:export llvm.floor.f64
123+
func llvm_floor(x float64) float64
108124

109125
//go:linkname math_floor math.floor
110126
func math_floor(x float64) float64
@@ -152,13 +168,29 @@ func math_Log2(x float64) float64 { return math_log2(x) }
152168
func math_log2(x float64) float64
153169

154170
//go:linkname math_Max math.Max
155-
func math_Max(x, y float64) float64 { return math_max(x, y) }
171+
func math_Max(x, y float64) float64 {
172+
if GOARCH == "arm64" || GOARCH == "wasm" {
173+
return llvm_maximum(x, y)
174+
}
175+
return math_max(x, y)
176+
}
177+
178+
//go:export llvm.maximum.f64
179+
func llvm_maximum(x, y float64) float64
156180

157181
//go:linkname math_max math.max
158182
func math_max(x, y float64) float64
159183

160184
//go:linkname math_Min math.Min
161-
func math_Min(x, y float64) float64 { return math_min(x, y) }
185+
func math_Min(x, y float64) float64 {
186+
if GOARCH == "arm64" || GOARCH == "wasm" {
187+
return llvm_minimum(x, y)
188+
}
189+
return math_min(x, y)
190+
}
191+
192+
//go:export llvm.minimum.f64
193+
func llvm_minimum(x, y float64) float64
162194

163195
//go:linkname math_min math.min
164196
func math_min(x, y float64) float64
@@ -200,7 +232,15 @@ func math_Sinh(x float64) float64 { return math_sinh(x) }
200232
func math_sinh(x float64) float64
201233

202234
//go:linkname math_Sqrt math.Sqrt
203-
func math_Sqrt(x float64) float64 { return math_sqrt(x) }
235+
func math_Sqrt(x float64) float64 {
236+
if GOARCH == "x86" || GOARCH == "amd64" || GOARCH == "wasm" {
237+
return llvm_sqrt(x)
238+
}
239+
return math_sqrt(x)
240+
}
241+
242+
//go:export llvm.sqrt.f64
243+
func llvm_sqrt(x float64) float64
204244

205245
//go:linkname math_sqrt math.sqrt
206246
func math_sqrt(x float64) float64
@@ -218,7 +258,15 @@ func math_Tanh(x float64) float64 { return math_tanh(x) }
218258
func math_tanh(x float64) float64
219259

220260
//go:linkname math_Trunc math.Trunc
221-
func math_Trunc(x float64) float64 { return math_trunc(x) }
261+
func math_Trunc(x float64) float64 {
262+
if GOARCH == "arm64" || GOARCH == "wasm" {
263+
return llvm_trunc(x)
264+
}
265+
return math_trunc(x)
266+
}
267+
268+
//go:export llvm.trunc.f64
269+
func llvm_trunc(x float64) float64
222270

223271
//go:linkname math_trunc math.trunc
224272
func math_trunc(x float64) float64

0 commit comments

Comments
 (0)