mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
wasm: add a webassembly compiler backend, based on using binaryen (#17368)
This commit is contained in:
57
examples/wasm/functions.v
Normal file
57
examples/wasm/functions.v
Normal file
@ -0,0 +1,57 @@
|
||||
// `pub` functions will be exported.
|
||||
//
|
||||
// ```
|
||||
// v -b wasm -no-builtin functions.v
|
||||
// wasmer functions.wasm -i main.powi <a> <b>
|
||||
// wasmer functions.wasm -i main.gcd <a> <b>
|
||||
// ```
|
||||
|
||||
pub fn gcd(a_ i64, b_ i64) i64 {
|
||||
mut a := a_
|
||||
mut b := b_
|
||||
if a < 0 {
|
||||
a = -a
|
||||
}
|
||||
if b < 0 {
|
||||
b = -b
|
||||
}
|
||||
for b != 0 {
|
||||
a %= b
|
||||
if a == 0 {
|
||||
return b
|
||||
}
|
||||
b %= a
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
pub fn powi(a i64, b i64) i64 {
|
||||
mut b_ := b
|
||||
mut p := a
|
||||
mut v := i64(1)
|
||||
|
||||
if b_ < 0 { // exponent < 0
|
||||
if a == 0 {
|
||||
return -1 // division by 0
|
||||
}
|
||||
return if a * a != 1 {
|
||||
0
|
||||
} else {
|
||||
if (b_ & 1) > 0 {
|
||||
a
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for b_ > 0 {
|
||||
if b_ & 1 > 0 {
|
||||
v *= p
|
||||
}
|
||||
p *= p
|
||||
b_ >>= 1
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
Reference in New Issue
Block a user