mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Add bswap/bswap16 post MVP polyfills (#34)
This commit is contained in:
707
tests/compiler/std/polyfills.optimized.wat
Normal file
707
tests/compiler/std/polyfills.optimized.wat
Normal file
@ -0,0 +1,707 @@
|
||||
(module
|
||||
(type $iiiiv (func (param i32 i32 i32 i32)))
|
||||
(type $ii (func (param i32) (result i32)))
|
||||
(type $II (func (param i64) (result i64)))
|
||||
(type $v (func))
|
||||
(import "env" "abort" (func $abort (param i32 i32 i32 i32)))
|
||||
(memory $0 1)
|
||||
(data (i32.const 4) "\12\00\00\00(\00l\00i\00b\00)\00/\00p\00o\00l\00y\00f\00i\00l\00l\00s\00.\00t\00s")
|
||||
(data (i32.const 44) "\10\00\00\00s\00t\00d\00/\00p\00o\00l\00y\00f\00i\00l\00l\00s\00.\00t\00s")
|
||||
(export "memory" (memory $0))
|
||||
(start $start)
|
||||
(func "$(lib)/polyfills/bswap16<u16>" (; 1 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 1)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.const 1)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
(return
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.and
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 65280)
|
||||
)
|
||||
(i32.and
|
||||
(i32.shr_u
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get_local $0)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap<u16>" (; 2 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 1)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(call "$(lib)/polyfills/bswap16<u16>"
|
||||
(get_local $0)
|
||||
)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap16<i16>" (; 3 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 1)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.const 1)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
(return
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 24)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const -256)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const 0)
|
||||
)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
(i32.const 16)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get_local $0)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap<i16>" (; 4 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 1)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(call "$(lib)/polyfills/bswap16<i16>"
|
||||
(get_local $0)
|
||||
)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap16<u32>" (; 5 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 0)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.const 0)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
(return
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.and
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 65280)
|
||||
)
|
||||
(i32.and
|
||||
(i32.shr_u
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const -65536)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get_local $0)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap<u32>" (; 6 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 0)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(i32.or
|
||||
(i32.rotl
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const -16711936)
|
||||
)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.rotr
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const 16711935)
|
||||
)
|
||||
(i32.const 8)
|
||||
)
|
||||
)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap16<i32>" (; 7 ;) (type $ii) (param $0 i32) (result i32)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 0)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 25)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.const 0)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
(return
|
||||
(i32.or
|
||||
(i32.or
|
||||
(i32.and
|
||||
(i32.shl
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 65280)
|
||||
)
|
||||
(i32.and
|
||||
(i32.shr_s
|
||||
(get_local $0)
|
||||
(i32.const 8)
|
||||
)
|
||||
(i32.const 255)
|
||||
)
|
||||
)
|
||||
(i32.and
|
||||
(get_local $0)
|
||||
(i32.const -65536)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(get_local $0)
|
||||
)
|
||||
(func "$(lib)/polyfills/bswap<u64>" (; 8 ;) (type $II) (param $0 i64) (result i64)
|
||||
(local $1 i32)
|
||||
(if
|
||||
(i32.eqz
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(if (result i32)
|
||||
(tee_local $1
|
||||
(i32.and
|
||||
(i32.const 0)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 0)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(get_local $1)
|
||||
(i32.const 1)
|
||||
)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 4)
|
||||
(i32.const 2)
|
||||
(i32.const 2)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(i64.rotr
|
||||
(i64.or
|
||||
(i64.and
|
||||
(i64.shr_u
|
||||
(tee_local $0
|
||||
(i64.or
|
||||
(i64.and
|
||||
(i64.shr_u
|
||||
(get_local $0)
|
||||
(i64.const 8)
|
||||
)
|
||||
(i64.const 71777214294589695)
|
||||
)
|
||||
(i64.shl
|
||||
(i64.and
|
||||
(get_local $0)
|
||||
(i64.const 71777214294589695)
|
||||
)
|
||||
(i64.const 8)
|
||||
)
|
||||
)
|
||||
)
|
||||
(i64.const 16)
|
||||
)
|
||||
(i64.const 281470681808895)
|
||||
)
|
||||
(i64.shl
|
||||
(i64.and
|
||||
(get_local $0)
|
||||
(i64.const 281470681808895)
|
||||
)
|
||||
(i64.const 16)
|
||||
)
|
||||
)
|
||||
(i64.const 32)
|
||||
)
|
||||
)
|
||||
(func $start (; 9 ;) (type $v)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<u16>"
|
||||
(i32.const 43707)
|
||||
)
|
||||
(i32.const 48042)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 4)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<i16>"
|
||||
(i32.const -21829)
|
||||
)
|
||||
(i32.const -17494)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 5)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<u32>"
|
||||
(i32.const -1430532899)
|
||||
)
|
||||
(i32.const -573785174)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 8)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<u32>"
|
||||
(i32.const -1430532899)
|
||||
)
|
||||
(i32.const -573785174)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 9)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i64.ne
|
||||
(call "$(lib)/polyfills/bswap<u64>"
|
||||
(i64.const 4822679907192029)
|
||||
)
|
||||
(i64.const -2464388556401798912)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 12)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i64.ne
|
||||
(call "$(lib)/polyfills/bswap<u64>"
|
||||
(i64.const 4822679907192029)
|
||||
)
|
||||
(i64.const -2464388556401798912)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 13)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<u32>"
|
||||
(i32.const -1430532899)
|
||||
)
|
||||
(i32.const -573785174)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 16)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap<u32>"
|
||||
(i32.const -1430532899)
|
||||
)
|
||||
(i32.const -573785174)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 17)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap16<u16>"
|
||||
(i32.const 43707)
|
||||
)
|
||||
(i32.const 48042)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 20)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap16<i16>"
|
||||
(i32.const -21829)
|
||||
)
|
||||
(i32.const -17494)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 21)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap16<u32>"
|
||||
(i32.const -7820613)
|
||||
)
|
||||
(i32.const -7816278)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 24)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
(if
|
||||
(i32.ne
|
||||
(call "$(lib)/polyfills/bswap16<i32>"
|
||||
(i32.const -7820613)
|
||||
)
|
||||
(i32.const -7816278)
|
||||
)
|
||||
(block
|
||||
(call $abort
|
||||
(i32.const 0)
|
||||
(i32.const 44)
|
||||
(i32.const 25)
|
||||
(i32.const 0)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
25
tests/compiler/std/polyfills.ts
Normal file
25
tests/compiler/std/polyfills.ts
Normal file
@ -0,0 +1,25 @@
|
||||
// bswap / bswap16 tests
|
||||
|
||||
// check bswap<T> for i16/u16
|
||||
assert(bswap<u16>(<u16>0xaabb) == <u16>0xbbaa);
|
||||
assert(bswap<i16>(<i16>0xaabb) == <i16>0xbbaa);
|
||||
|
||||
// check bswap<T> for i32/u32
|
||||
assert(bswap<u32>(<u32>0xaabbccdd) == <u32>0xddccbbaa);
|
||||
assert(bswap<i32>(<i32>0xaabbccdd) == <i32>0xddccbbaa);
|
||||
|
||||
// check bswap<T> for i64/u64
|
||||
assert(bswap<u64>(<u64>0x00112233aabbccdd) == <u64>0xddccbbaa33221100);
|
||||
assert(bswap<i64>(<i64>0x00112233aabbccdd) == <i64>0xddccbbaa33221100);
|
||||
|
||||
// check bswap<T> for i32/u32
|
||||
assert(bswap<usize>(<usize>0xaabbccdd) == <usize>0xddccbbaa);
|
||||
assert(bswap<isize>(<isize>0xaabbccdd) == <isize>0xddccbbaa);
|
||||
|
||||
// check bswap16<T> for i16/u16
|
||||
assert(bswap16<u16>(<u16>0xaabb) == <u16>0xbbaa);
|
||||
assert(bswap16<i16>(<i16>0xaabb) == <i16>0xbbaa);
|
||||
|
||||
// check bswap16<T> for i32/u32
|
||||
assert(bswap16<u32>(<u32>0xff88aabb) == <u32>0xff88bbaa);
|
||||
assert(bswap16<i32>(<i32>0xff88aabb) == <i32>0xff88bbaa);
|
2511
tests/compiler/std/polyfills.untouched.wat
Normal file
2511
tests/compiler/std/polyfills.untouched.wat
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user