Skip to content

Commit

Permalink
Put everything in one file
Browse files Browse the repository at this point in the history
`wasm-link` is deprecated and it's not possible to support multi-value
functions.

And `--relocatable` option is broken so we can't link wasm files with
`wasm-ld` too.

See: WebAssembly/wabt#1133
  • Loading branch information
caasi committed Jul 4, 2020
1 parent 9d00deb commit 102942a
Show file tree
Hide file tree
Showing 2 changed files with 330 additions and 14 deletions.
327 changes: 327 additions & 0 deletions color.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
(module
(func $fmod
(param $x f64)
(param $y f64)
(result f64)
get_local $x
get_local $y
get_local $x
get_local $y
f64.div
f64.trunc
f64.mul
f64.sub
)
(export "fmod" (func $fmod))

(func
(export "rgb2hsv")
(param $r f32)
(param $g f32)
(param $b f32)
(result f32 f32 f32)
(local $M f32)
(local $m f32)
(local $rr f32)
(local $gg f32)
(local $bb f32)
(local $c f32)
(local $t f32)
(local $h f32)
(local $hh f32)
(local $s f32)
(local $v f32)
get_local $r
f32.const 255.0
f32.div
set_local $rr
get_local $g
f32.const 255.0
f32.div
set_local $gg
get_local $b
f32.const 255.0
f32.div
set_local $bb
;; find max and min
get_local $rr
get_local $gg
get_local $bb
f32.max
f32.max
set_local $M
get_local $rr
get_local $gg
get_local $bb
f32.min
f32.min
set_local $m
;; delta
get_local $M
get_local $m
f32.sub
set_local $c
block
;; $c is 0.0
f32.const 0.0
get_local $c
f32.eq
if
get_local $c
set_local $hh
br 1
end
;; $M is $rr
get_local $M
get_local $rr
f32.eq
if
get_local $gg
get_local $bb
f32.sub
get_local $c
f32.div
f64.promote/f32
f64.const 6.0
call $fmod
f32.demote/f64
set_local $hh
br 1
end
;; $M is $gg
get_local $M
get_local $gg
f32.eq
if
get_local $bb
get_local $rr
f32.sub
get_local $c
f32.div
f32.const 2.0
f32.add
set_local $hh
br 1
end
get_local $rr
get_local $gg
f32.sub
get_local $c
f32.div
f32.const 4.0
f32.add
set_local $hh
end
get_local $hh
f32.const 60.0
f32.mul
set_local $h
get_local $M
set_local $v
get_local $v
f32.const 0.0
f32.eq
if
f32.const 0.0
set_local $s
else
get_local $c
get_local $v
f32.div
set_local $s
end
;; return
get_local $h
get_local $s
get_local $v
)

(func
(export "hsv2rgb")
(param $h f32)
(param $s f32)
(param $v f32)
(result f32 f32 f32)
(local $r f32)
(local $g f32)
(local $b f32)
(local $c f32)
(local $m f32)
(local $hh f32)
(local $x f32)
get_local $s
get_local $v
f32.mul
set_local $c
get_local $v
get_local $c
f32.sub
set_local $m
get_local $h
f32.const 60.0
f32.div
set_local $hh
;; x = c * (1 - abs(hh % 2 - 1))
get_local $c
f32.const 1.0
;; modulo
get_local $hh
f64.promote/f32
f64.const 2.0
call $fmod
f32.demote/f64
;; end of modulo
f32.const 1.0
f32.sub
f32.abs
f32.sub
f32.mul
set_local $x
block
get_local $hh
f32.const 1.0
f32.lt
if
get_local $c
set_local $r
get_local $x
set_local $g
f32.const 0.0
set_local $b
br 1
end
get_local $hh
f32.const 2.0
f32.lt
if
get_local $x
set_local $r
get_local $c
set_local $g
f32.const 0.0
set_local $b
br 1
end
get_local $hh
f32.const 3.0
f32.lt
if
f32.const 0.0
set_local $r
get_local $c
set_local $g
get_local $x
set_local $b
br 1
end
get_local $hh
f32.const 4.0
f32.lt
if
f32.const 0.0
set_local $r
get_local $x
set_local $g
get_local $c
set_local $b
br 1
end
get_local $hh
f32.const 5.0
f32.lt
if
get_local $x
set_local $r
f32.const 0.0
set_local $g
get_local $c
set_local $b
br 1
end
get_local $c
set_local $r
f32.const 0.0
set_local $g
get_local $x
set_local $b
end
get_local $m
get_local $r
f32.add
f32.const 255.0
f32.mul
set_local $r
get_local $m
get_local $g
f32.add
f32.const 255.0
f32.mul
set_local $g
get_local $m
get_local $b
f32.add
f32.const 255.0
f32.mul
set_local $b
;; return
get_local $r
get_local $g
get_local $b
)
)

;; test fmod
(assert_return (invoke "fmod" (f64.const 1.0) (f64.const 1.0)) (f64.const 0.0))
(assert_return (invoke "fmod" (f64.const 2.5) (f64.const 1.0)) (f64.const 0.5))

;; test rgb2hsv
(assert_return
(invoke "rgb2hsv" (f32.const 0.0) (f32.const 0.0) (f32.const 0.0))
(f32.const 0.0) (f32.const 0.0) (f32.const 0.0)
)
(assert_return
(invoke "rgb2hsv" (f32.const 255.0) (f32.const 255.0) (f32.const 255.0))
(f32.const 0.0) (f32.const 0.0) (f32.const 1.0)
)
(assert_return
(invoke "rgb2hsv" (f32.const 255.0) (f32.const 0.0) (f32.const 0.0))
(f32.const 0.0) (f32.const 1.0) (f32.const 1.0)
)
(assert_return
(invoke "rgb2hsv" (f32.const 0.0) (f32.const 255.0) (f32.const 0.0))
(f32.const 120.0) (f32.const 1.0) (f32.const 1.0)
)
(assert_return
(invoke "rgb2hsv" (f32.const 0.0) (f32.const 0.0) (f32.const 255.0))
(f32.const 240.0) (f32.const 1.0) (f32.const 1.0)
)
(assert_return
(invoke "rgb2hsv" (f32.const 191.25) (f32.const 191.25) (f32.const 191.25))
(f32.const 0.0) (f32.const 0.0) (f32.const 0.75)
)

;; test hsv2rgb
(assert_return
(invoke "hsv2rgb" (f32.const 0.0) (f32.const 0.0) (f32.const 0.0))
(f32.const 0.0) (f32.const 0.0) (f32.const 0.0)
)
(assert_return
(invoke "hsv2rgb" (f32.const 0.0) (f32.const 0.0) (f32.const 1.0))
(f32.const 255.0) (f32.const 255.0) (f32.const 255.0)
)
(assert_return
(invoke "hsv2rgb" (f32.const 0.0) (f32.const 1.0) (f32.const 1.0))
(f32.const 255.0) (f32.const 0.0) (f32.const 0.0)
)
(assert_return
(invoke "hsv2rgb" (f32.const 120.0) (f32.const 1.0) (f32.const 1.0))
(f32.const 0.0) (f32.const 255.0) (f32.const 0.0)
)
(assert_return
(invoke "hsv2rgb" (f32.const 240.0) (f32.const 1.0) (f32.const 1.0))
(f32.const 0.0) (f32.const 0.0) (f32.const 255.0)
)
(assert_return
(invoke "hsv2rgb" (f32.const 0.0) (f32.const 0.0) (f32.const 0.75))
(f32.const 191.25) (f32.const 191.25) (f32.const 191.25)
)
17 changes: 3 additions & 14 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
#!/bin/sh
# build and test math functions
wast2json ./math.wast -o ./math.json
spectest-interp ./math.json

# rgb2hsv
wast2json ./rgb2hsv.wast -o ./rgb2hsv.json
# link and overwrite the output wasm
wasm-link ./math.0.wasm ./rgb2hsv.0.wasm -o ./rgb2hsv.0.wasm
spectest-interp ./rgb2hsv.json

# hsv2rgb
wast2json ./hsv2rgb.wast -o ./hsv2rgb.json
wasm-link ./math.0.wasm ./hsv2rgb.0.wasm -o ./hsv2rgb.0.wasm
spectest-interp ./hsv2rgb.json
# build and test functions
wast2json ./color.wast -o ./color.json
spectest-interp ./color.json

0 comments on commit 102942a

Please sign in to comment.