Wasm Reference
  • Welcome
  • Nodejs WAT Compiler
  • Types
    • Functions
    • Globals
    • Linear Memory
    • Tables
    • Numbers
    • Text
  • Structures
    • IF
    • LOOP
  • Algorithms
    • Hello World
    • Estimate Pi
  • Maths Helper Algorithms
    • Pseudorandom Number Generator
    • Factorial
    • Dist
    • Map
    • Fract
    • RemF or ModF
    • Round
    • Clamp
    • Mix
    • Pow
    • Approx Sin
    • Approx Cos
    • Approx Tan
    • ToRad and ToDeg
Powered by GitBook
On this page

Was this helpful?

  1. Maths Helper Algorithms

Pow

Returns x to the power of y.

(module
    (func $pow (export "pow") (param $x i32) (param $y i32) (result i32)
    (local $out i32)
    (local $index i32)
    i32.const 1
    set_local $out
    i32.const 1
    set_local $index

    get_local $y
    (i32.const 0)
    i32.eq
    if $i0
      i32.const 1
      return
    end
    

    (block $b0
      (loop $l0
        (i32.mul 
          (get_local $out)
          (get_local $x)
        )
        set_local $out

        (i32.add 
          (get_local $index)
          (i32.const 1)
        )
        tee_local $index
        get_local $y
        i32.gt
        br_if 1

        br 0
      )
    )

    get_local $out
  )
)
PreviousMixNextApprox Sin

Last updated 5 years ago

Was this helpful?