From 2b068dd8e2394bc4cb63695d72931122457f86e3 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 12:22:30 +0300 Subject: [PATCH 1/6] improve bswap --- std/assembly/polyfills.ts | 37 +++++---- tests/compiler/std/array.untouched.wat | 4 + tests/compiler/std/dataview.untouched.wat | 12 +++ tests/compiler/std/polyfills.untouched.wat | 86 ++++++++++++--------- tests/compiler/std/typedarray.untouched.wat | 4 + 5 files changed, 91 insertions(+), 52 deletions(-) diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index a17582ea4b..272f7ac0b6 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -1,17 +1,20 @@ export function bswap(value: T): T { if (isInteger()) { + if (sizeof() == 1) { + return value; + } if (sizeof() == 2) { - return ((value << 8) | ((value >>> 8) & 0x00FF)); + return ((value << 8) | ((value >>> 8) & 0x00FF)); } if (sizeof() == 4) { return ( - rotl(value & 0xFF00FF00, 8) | - rotr(value & 0x00FF00FF, 8) + rotl(value & 0xFF00FF00, 8) | + rotr(value & 0x00FF00FF, 8) ); } if (sizeof() == 8) { - let a = (value >> 8) & 0x00FF00FF00FF00FF; - let b = (value & 0x00FF00FF00FF00FF) << 8; + let a = (value >>> 8) & 0x00FF00FF00FF00FF; + let b = (value & 0x00FF00FF00FF00FF) << 8; let v = a | b; a = (v >> 16) & 0x0000FFFF0000FFFF; @@ -19,21 +22,25 @@ export function bswap(value: T): T { return rotr(a | b, 32); } - return value; } - assert(false); - return value; + ERROR("Unsupported generic type"); } export function bswap16(value: T): T { - if (isInteger() && sizeof() <= 4) { + if (isInteger()) { + if (sizeof() == 1) { + return value; + } if (sizeof() == 2) { - return ((value << 8) | ((value >>> 8) & 0x00FF)); - } else if (sizeof() == 4) { - return (((value << 8) & 0xFF00) | ((value >>> 8) & 0x00FF) | (value & 0xFFFF0000)); + return ((value << 8) | ((value >>> 8) & 0x00FF)); + } + if (sizeof() == 4) { + return ( + ((value << 8) & 0xFF00) | + ((value >>> 8) & 0x00FF) | + (value & 0xFFFF0000) + ); } - return value; } - assert(false); - return value; + ERROR("Unsupported generic type"); } diff --git a/tests/compiler/std/array.untouched.wat b/tests/compiler/std/array.untouched.wat index 1d376833f6..c2c0f7c8d6 100644 --- a/tests/compiler/std/array.untouched.wat +++ b/tests/compiler/std/array.untouched.wat @@ -5179,6 +5179,10 @@ i32.const 1 drop i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 i32.const 2 i32.eq drop diff --git a/tests/compiler/std/dataview.untouched.wat b/tests/compiler/std/dataview.untouched.wat index 23b9b1b8e5..7be3b16bc9 100644 --- a/tests/compiler/std/dataview.untouched.wat +++ b/tests/compiler/std/dataview.untouched.wat @@ -2498,6 +2498,10 @@ i32.const 1 drop i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 i32.const 2 i32.eq drop @@ -2561,6 +2565,10 @@ i32.const 1 drop i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 i32.const 2 i32.eq drop @@ -2666,6 +2674,10 @@ i32.const 1 drop i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 i32.const 2 i32.eq drop diff --git a/tests/compiler/std/polyfills.untouched.wat b/tests/compiler/std/polyfills.untouched.wat index 044d17a884..3140d1ea86 100644 --- a/tests/compiler/std/polyfills.untouched.wat +++ b/tests/compiler/std/polyfills.untouched.wat @@ -17,15 +17,7 @@ i32.const 1 drop i32.const 1 - i32.const 2 - i32.eq - drop - i32.const 1 - i32.const 4 - i32.eq - drop i32.const 1 - i32.const 8 i32.eq drop local.get $0 @@ -35,15 +27,7 @@ i32.const 1 drop i32.const 1 - i32.const 2 - i32.eq - drop - i32.const 1 - i32.const 4 - i32.eq - drop i32.const 1 - i32.const 8 i32.eq drop local.get $0 @@ -53,6 +37,10 @@ i32.const 1 drop i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 i32.const 2 i32.eq drop @@ -77,6 +65,10 @@ i32.const 1 drop i32.const 2 + i32.const 1 + i32.eq + drop + i32.const 2 i32.const 2 i32.eq drop @@ -100,6 +92,10 @@ i32.const 1 drop i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 i32.const 2 i32.eq drop @@ -124,6 +120,10 @@ i32.const 1 drop i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 i32.const 2 i32.eq drop @@ -151,6 +151,10 @@ i32.const 1 drop i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 i32.const 2 i32.eq drop @@ -204,6 +208,10 @@ i32.const 1 drop i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 i32.const 2 i32.eq drop @@ -233,7 +241,7 @@ local.set $3 local.get $3 i64.const 16 - i64.shr_u + i64.shr_s i64.const 281470681808895 i64.and local.set $1 @@ -254,6 +262,10 @@ i32.const 1 drop i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 i32.const 2 i32.eq drop @@ -278,6 +290,10 @@ i32.const 1 drop i32.const 4 + i32.const 1 + i32.eq + drop + i32.const 4 i32.const 2 i32.eq drop @@ -300,15 +316,9 @@ ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) i32.const 1 - i32.const 4 - i32.le_u drop i32.const 1 - i32.const 2 - i32.eq - drop i32.const 1 - i32.const 4 i32.eq drop local.get $0 @@ -316,24 +326,20 @@ ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) i32.const 1 - i32.const 4 - i32.le_u drop i32.const 1 - i32.const 2 - i32.eq - drop i32.const 1 - i32.const 4 i32.eq drop local.get $0 return ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) + i32.const 1 + drop i32.const 2 - i32.const 4 - i32.le_u + i32.const 1 + i32.eq drop i32.const 2 i32.const 2 @@ -357,9 +363,11 @@ return ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) + i32.const 1 + drop i32.const 2 - i32.const 4 - i32.le_u + i32.const 1 + i32.eq drop i32.const 2 i32.const 2 @@ -382,9 +390,11 @@ return ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) + i32.const 1 + drop i32.const 4 - i32.const 4 - i32.le_u + i32.const 1 + i32.eq drop i32.const 4 i32.const 2 @@ -412,9 +422,11 @@ return ) (func $~lib/polyfills/bswap16 (param $0 i32) (result i32) + i32.const 1 + drop i32.const 4 - i32.const 4 - i32.le_u + i32.const 1 + i32.eq drop i32.const 4 i32.const 2 diff --git a/tests/compiler/std/typedarray.untouched.wat b/tests/compiler/std/typedarray.untouched.wat index 4af3dd3e29..bf2c7a5a80 100644 --- a/tests/compiler/std/typedarray.untouched.wat +++ b/tests/compiler/std/typedarray.untouched.wat @@ -13253,6 +13253,10 @@ i32.const 1 drop i32.const 8 + i32.const 1 + i32.eq + drop + i32.const 8 i32.const 2 i32.eq drop From 86ca700dceea232d93f44c8dde00f40d49229de6 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 12:26:00 +0300 Subject: [PATCH 2/6] fix --- std/assembly/polyfills.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 272f7ac0b6..4eba792aa7 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -17,7 +17,7 @@ export function bswap(value: T): T { let b = (value & 0x00FF00FF00FF00FF) << 8; let v = a | b; - a = (v >> 16) & 0x0000FFFF0000FFFF; + a = (v >>> 16) & 0x0000FFFF0000FFFF; b = (v & 0x0000FFFF0000FFFF) << 16; return rotr(a | b, 32); From 010da62f29e1a83a51c41a222d4faae9255ea31c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 12:34:21 +0300 Subject: [PATCH 3/6] upd fixture --- tests/compiler/std/polyfills.untouched.wat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/compiler/std/polyfills.untouched.wat b/tests/compiler/std/polyfills.untouched.wat index 3140d1ea86..4792acfe20 100644 --- a/tests/compiler/std/polyfills.untouched.wat +++ b/tests/compiler/std/polyfills.untouched.wat @@ -241,7 +241,7 @@ local.set $3 local.get $3 i64.const 16 - i64.shr_s + i64.shr_u i64.const 281470681808895 i64.and local.set $1 From 5821266c169e7eac05601505153498e02e8cc54d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 18:59:51 +0300 Subject: [PATCH 4/6] refactoring --- std/assembly/polyfills.ts | 10 +++++----- tests/compiler/std/polyfills.untouched.wat | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 4eba792aa7..95e38e374a 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -4,7 +4,7 @@ export function bswap(value: T): T { return value; } if (sizeof() == 2) { - return ((value << 8) | ((value >>> 8) & 0x00FF)); + return (value << 8 | (value >>> 8 & 0xFF)); } if (sizeof() == 4) { return ( @@ -20,7 +20,7 @@ export function bswap(value: T): T { a = (v >>> 16) & 0x0000FFFF0000FFFF; b = (v & 0x0000FFFF0000FFFF) << 16; - return rotr(a | b, 32); + return rotr(a | b, 32); } } ERROR("Unsupported generic type"); @@ -32,12 +32,12 @@ export function bswap16(value: T): T { return value; } if (sizeof() == 2) { - return ((value << 8) | ((value >>> 8) & 0x00FF)); + return (value << 8 | (value >>> 8 & 0xFF)); } if (sizeof() == 4) { return ( - ((value << 8) & 0xFF00) | - ((value >>> 8) & 0x00FF) | + (((value & 0xFF) << 8)) | + ((value >>> 8) & 0xFF) | (value & 0xFFFF0000) ); } diff --git a/tests/compiler/std/polyfills.untouched.wat b/tests/compiler/std/polyfills.untouched.wat index 4792acfe20..4f517d0290 100644 --- a/tests/compiler/std/polyfills.untouched.wat +++ b/tests/compiler/std/polyfills.untouched.wat @@ -405,10 +405,10 @@ i32.eq drop local.get $0 + i32.const 255 + i32.and i32.const 8 i32.shl - i32.const 65280 - i32.and local.get $0 i32.const 8 i32.shr_u @@ -437,10 +437,10 @@ i32.eq drop local.get $0 + i32.const 255 + i32.and i32.const 8 i32.shl - i32.const 65280 - i32.and local.get $0 i32.const 8 i32.shr_u From 6201cdcfca74f6755201fd194eee5352a4f630c4 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 19:50:21 +0300 Subject: [PATCH 5/6] better --- std/assembly/polyfills.ts | 18 +++++++++--------- tests/compiler/std/dataview.untouched.wat | 2 -- tests/compiler/std/polyfills.untouched.wat | 14 ++++---------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 95e38e374a..66c7298b0c 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -4,17 +4,17 @@ export function bswap(value: T): T { return value; } if (sizeof() == 2) { - return (value << 8 | (value >>> 8 & 0xFF)); + return (value << 8 | (value >> 8)); } if (sizeof() == 4) { return ( - rotl(value & 0xFF00FF00, 8) | - rotr(value & 0x00FF00FF, 8) + rotl(value & 0xFF00FF00, 8) | + rotr(value & 0x00FF00FF, 8) ); } if (sizeof() == 8) { - let a = (value >>> 8) & 0x00FF00FF00FF00FF; - let b = (value & 0x00FF00FF00FF00FF) << 8; + let a = (value >> 8) & 0x00FF00FF00FF00FF; + let b = (value & 0x00FF00FF00FF00FF) << 8; let v = a | b; a = (v >>> 16) & 0x0000FFFF0000FFFF; @@ -32,13 +32,13 @@ export function bswap16(value: T): T { return value; } if (sizeof() == 2) { - return (value << 8 | (value >>> 8 & 0xFF)); + return (value << 8 | (value >>> 8)); } if (sizeof() == 4) { return ( - (((value & 0xFF) << 8)) | - ((value >>> 8) & 0xFF) | - (value & 0xFFFF0000) + (((value & 0xFF) << 8)) | + ((value >> 8) & 0xFF) | + (value & 0xFFFF0000) ); } } diff --git a/tests/compiler/std/dataview.untouched.wat b/tests/compiler/std/dataview.untouched.wat index 7be3b16bc9..c7a83d2dd4 100644 --- a/tests/compiler/std/dataview.untouched.wat +++ b/tests/compiler/std/dataview.untouched.wat @@ -2693,8 +2693,6 @@ i32.const 15 i32.and i32.shr_u - i32.const 255 - i32.and i32.or return ) diff --git a/tests/compiler/std/polyfills.untouched.wat b/tests/compiler/std/polyfills.untouched.wat index 4f517d0290..0833eb66e6 100644 --- a/tests/compiler/std/polyfills.untouched.wat +++ b/tests/compiler/std/polyfills.untouched.wat @@ -56,8 +56,6 @@ i32.const 15 i32.and i32.shr_u - i32.const 255 - i32.and i32.or return ) @@ -78,13 +76,12 @@ i32.and i32.shl local.get $0 - i32.extend16_s + i32.const 65535 + i32.and i32.const 8 i32.const 15 i32.and i32.shr_u - i32.const 255 - i32.and i32.or return ) @@ -357,8 +354,6 @@ i32.const 15 i32.and i32.shr_u - i32.const 255 - i32.and i32.or return ) @@ -379,13 +374,12 @@ i32.and i32.shl local.get $0 - i32.extend16_s + i32.const 65535 + i32.and i32.const 8 i32.const 15 i32.and i32.shr_u - i32.const 255 - i32.and i32.or return ) From 01a04f7e59f3ce0d1be04ebec29550e5e779b1b2 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 18 Sep 2021 19:51:33 +0300 Subject: [PATCH 6/6] refactor --- std/assembly/polyfills.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 66c7298b0c..3a4295caa2 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -32,7 +32,7 @@ export function bswap16(value: T): T { return value; } if (sizeof() == 2) { - return (value << 8 | (value >>> 8)); + return (value << 8 | (value >> 8)); } if (sizeof() == 4) { return (