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

Factorial

(module
    (func $fact (export "fact") (param $in f32) (result f32)
    
    (local $out f32)
    f32.const 1
    set_local $out

    get_local $in
    (f32.const 0)
    f32.eq
    if $i0
      f32.const 1
      return
    end
    
    get_local $in
    (f32.const 1)
    f32.eq
    if $i0
      f32.const 1
      return
    end

    get_local $in
    (f32.const 2)
    f32.eq
    if $i0
      f32.const 2
      return
    end

    (block $b0
      (loop $l0
        (f32.mul 
          (get_local $out)
          (get_local $in)
        )
        set_local $out

        (f32.sub 
          (get_local $in)
          (f32.const 1)
        )
        tee_local $in
        (f32.const 2)
        f32.eq
        br_if 1

        br 0
      )
    )
    get_local $out
  ))
PreviousPseudorandom Number GeneratorNextDist

Last updated 5 years ago

Was this helpful?