Skip to content

Instantly share code, notes, and snippets.

@JoelJaeschke
Created January 8, 2021 22:51
Show Gist options
  • Save JoelJaeschke/ba4de9b4991dbbb873d3b75a126a7118 to your computer and use it in GitHub Desktop.
Save JoelJaeschke/ba4de9b4991dbbb873d3b75a126a7118 to your computer and use it in GitHub Desktop.
Just a simple numerical integration in WebAssembly. Why? Why not!
(module
(func $f (param $x f32) (result f32)
;;function f(x) = x^2
get_local $x
get_local $x
f32.mul
)
(func (export "integrate") (param $ss f32) (param $start f32) (param $end f32) (result f32)
(local $sum f32)
(local $idx f32)
f32.const 0
set_local $sum
get_local $start
set_local $idx
loop $integration
;; evaluate f at the current index
get_local $idx
call $f
;; add the result to the running sum
get_local $ss
f32.mul
get_local $sum
f32.add
set_local $sum
;; increment the index
get_local $ss
get_local $idx
f32.add
set_local $idx
;; break if we reached the end of the range
get_local $end
get_local $idx
f32.gt
br_if $integration
end
get_local $sum)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment