diff --git a/std/assembly/dataview.ts b/std/assembly/dataview.ts index 87fe3ce816..b25274f074 100644 --- a/std/assembly/dataview.ts +++ b/std/assembly/dataview.ts @@ -57,7 +57,7 @@ export class DataView { (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); var result: i16 = load(this.dataStart + byteOffset); - return littleEndian ? result : bswap(result); + return littleEndian ? result : bswap(result); } getInt32(byteOffset: i32, littleEndian: bool = false): i32 { @@ -65,7 +65,7 @@ export class DataView { (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); var result: i32 = load(this.dataStart + byteOffset); - return littleEndian ? result : bswap(result); + return littleEndian ? result : bswap(result); } getUint8(byteOffset: i32): u8 { @@ -114,14 +114,14 @@ export class DataView { if ( (byteOffset >>> 31) | i32(byteOffset + 2 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); + store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setInt32(byteOffset: i32, value: i32, littleEndian: bool = false): void { if ( (byteOffset >>> 31) | i32(byteOffset + 4 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); + store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setUint8(byteOffset: i32, value: u8): void { @@ -150,7 +150,7 @@ export class DataView { (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); var result: i64 = load(this.dataStart + byteOffset); - return littleEndian ? result : bswap(result); + return littleEndian ? result : bswap(result); } getUint64(byteOffset: i32, littleEndian: bool = false): u64 { @@ -165,7 +165,7 @@ export class DataView { if ( (byteOffset >>> 31) | i32(byteOffset + 8 > this.byteLength) ) throw new RangeError(E_INDEXOUTOFRANGE); - store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); + store(this.dataStart + byteOffset, littleEndian ? value : bswap(value)); } setUint64(byteOffset: i32, value: u64, littleEndian: bool = false): void { diff --git a/std/assembly/polyfills.ts b/std/assembly/polyfills.ts index 87b7bc4db4..a17582ea4b 100644 --- a/std/assembly/polyfills.ts +++ b/std/assembly/polyfills.ts @@ -1,12 +1,12 @@ export function bswap(value: T): T { if (isInteger()) { 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) { @@ -28,9 +28,9 @@ export function bswap(value: T): T { export function bswap16(value: T): T { if (isInteger() && sizeof() <= 4) { if (sizeof() == 2) { - return ((value << 8) | ((value >> 8) & 0x00FF)); + return ((value << 8) | ((value >>> 8) & 0x00FF)); } else if (sizeof() == 4) { - return (((value << 8) & 0xFF00) | ((value >> 8) & 0x00FF) | (value & 0xFFFF0000)); + return (((value << 8) & 0xFF00) | ((value >>> 8) & 0x00FF) | (value & 0xFFFF0000)); } return value; } diff --git a/tests/compiler/std/dataview.optimized.wat b/tests/compiler/std/dataview.optimized.wat index e348453f22..ef01115839 100644 --- a/tests/compiler/std/dataview.optimized.wat +++ b/tests/compiler/std/dataview.optimized.wat @@ -4,8 +4,8 @@ (type $none_=>_none (func)) (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) (type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32))) - (type $i32_=>_i32 (func (param i32) (result i32))) (type $i32_=>_none (func (param i32))) + (type $i32_=>_i32 (func (param i32) (result i32))) (type $i32_i64_i32_=>_none (func (param i32 i64 i32))) (type $i32_i32_=>_i64 (func (param i32 i32) (result i64))) (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) @@ -1939,17 +1939,15 @@ i32.add i32.load8_s ) - (func $~lib/polyfills/bswap (param $0 i32) (result i32) + (func $~lib/polyfills/bswap (param $0 i32) (result i32) local.get $0 - i32.const 16 + i32.const 8 i32.shl - i32.const 24 - i32.shr_s - i32.const 255 - i32.and local.get $0 + i32.const 65535 + i32.and i32.const 8 - i32.shl + i32.shr_u i32.or ) (func $~lib/dataview/DataView#getInt16 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) @@ -1982,7 +1980,7 @@ local.get $0 else local.get $0 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end ) (func $~lib/dataview/DataView#getInt32 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) @@ -2063,17 +2061,6 @@ i32.add i32.load8_u ) - (func $~lib/polyfills/bswap (param $0 i32) (result i32) - local.get $0 - i32.const 8 - i32.shl - local.get $0 - i32.const 65535 - i32.and - i32.const 8 - i32.shr_u - i32.or - ) (func $~lib/dataview/DataView#getUint16 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) local.get $1 i32.const 31 @@ -2242,7 +2229,7 @@ local.get $1 else local.get $1 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end i32.store16 ) diff --git a/tests/compiler/std/dataview.untouched.wat b/tests/compiler/std/dataview.untouched.wat index 56ef316bc5..02400d5588 100644 --- a/tests/compiler/std/dataview.untouched.wat +++ b/tests/compiler/std/dataview.untouched.wat @@ -9,11 +9,11 @@ (type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32))) (type $i32_i32_i64_i32_=>_none (func (param i32 i32 i64 i32))) (type $i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32) (result i32))) - (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i32_i32_=>_i64 (func (param i32 i32 i32) (result i64))) (type $i32_i32_f32_i32_=>_none (func (param i32 i32 f32 i32))) (type $i32_i32_f64_i32_=>_none (func (param i32 i32 f64 i32))) (type $none_=>_i32 (func (result i32))) + (type $i64_=>_i64 (func (param i64) (result i64))) (type $i32_i32_i32_=>_f32 (func (param i32 i32 i32) (result f32))) (type $i32_i32_i32_=>_f64 (func (param i32 i32 i32) (result f64))) (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) @@ -2672,7 +2672,7 @@ i32.add i32.load8_s ) - (func $~lib/polyfills/bswap (param $0 i32) (result i32) + (func $~lib/polyfills/bswap (param $0 i32) (result i32) i32.const 1 drop i32.const 2 @@ -2685,14 +2685,12 @@ i32.and i32.shl local.get $0 - i32.const 16 - i32.shl - i32.const 16 - i32.shr_s + i32.const 65535 + i32.and i32.const 8 i32.const 15 i32.and - i32.shr_s + i32.shr_u i32.const 255 i32.and i32.or @@ -2729,33 +2727,9 @@ local.get $3 else local.get $3 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end ) - (func $~lib/polyfills/bswap (param $0 i32) (result i32) - i32.const 1 - drop - i32.const 4 - i32.const 2 - i32.eq - drop - i32.const 4 - i32.const 4 - i32.eq - drop - local.get $0 - i32.const -16711936 - i32.and - i32.const 8 - i32.rotl - local.get $0 - i32.const 16711935 - i32.and - i32.const 8 - i32.rotr - i32.or - return - ) (func $~lib/dataview/DataView#getInt32 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) (local $3 i32) local.get $1 @@ -2787,62 +2761,9 @@ local.get $3 else local.get $3 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end ) - (func $~lib/polyfills/bswap (param $0 i64) (result i64) - (local $1 i64) - (local $2 i64) - (local $3 i64) - i32.const 1 - drop - i32.const 8 - i32.const 2 - i32.eq - drop - i32.const 8 - i32.const 4 - i32.eq - drop - i32.const 8 - i32.const 8 - i32.eq - drop - local.get $0 - i64.const 8 - i64.shr_u - i64.const 71777214294589695 - i64.and - local.set $1 - local.get $0 - i64.const 71777214294589695 - i64.and - i64.const 8 - i64.shl - local.set $2 - local.get $1 - local.get $2 - i64.or - local.set $3 - local.get $3 - i64.const 16 - i64.shr_u - i64.const 281470681808895 - i64.and - local.set $1 - local.get $3 - i64.const 281470681808895 - i64.and - i64.const 16 - i64.shl - local.set $2 - local.get $1 - local.get $2 - i64.or - i64.const 32 - i64.rotr - return - ) (func $~lib/dataview/DataView#getInt64 (param $0 i32) (param $1 i32) (param $2 i32) (result i64) (local $3 i64) local.get $1 @@ -2874,7 +2795,7 @@ local.get $3 else local.get $3 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end ) (func $~lib/dataview/DataView#getUint8 (param $0 i32) (param $1 i32) (result i32) @@ -2896,30 +2817,6 @@ i32.add i32.load8_u ) - (func $~lib/polyfills/bswap (param $0 i32) (result i32) - i32.const 1 - drop - i32.const 2 - i32.const 2 - i32.eq - drop - local.get $0 - i32.const 8 - i32.const 15 - i32.and - i32.shl - local.get $0 - i32.const 65535 - i32.and - i32.const 8 - i32.const 15 - i32.and - i32.shr_u - i32.const 255 - i32.and - i32.or - return - ) (func $~lib/dataview/DataView#getUint16 (param $0 i32) (param $1 i32) (param $2 i32) (result i32) (local $3 i32) local.get $1 @@ -3146,7 +3043,7 @@ local.get $2 else local.get $2 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end i32.store16 ) @@ -3178,7 +3075,7 @@ local.get $2 else local.get $2 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end i32.store ) @@ -3210,7 +3107,7 @@ local.get $2 else local.get $2 - call $~lib/polyfills/bswap + call $~lib/polyfills/bswap end i64.store ) diff --git a/tests/compiler/std/polyfills.untouched.wat b/tests/compiler/std/polyfills.untouched.wat index e6ee88e0c2..c8b6c53be2 100644 --- a/tests/compiler/std/polyfills.untouched.wat +++ b/tests/compiler/std/polyfills.untouched.wat @@ -92,7 +92,7 @@ i32.const 8 i32.const 15 i32.and - i32.shr_s + i32.shr_u i32.const 255 i32.and i32.or @@ -400,7 +400,7 @@ i32.const 8 i32.const 15 i32.and - i32.shr_s + i32.shr_u i32.const 255 i32.and i32.or @@ -466,7 +466,7 @@ i32.and local.get $0 i32.const 8 - i32.shr_s + i32.shr_u i32.const 255 i32.and i32.or