Skip to content

Commit de1c4b3

Browse files
MaxGraeydcodeIO
authored andcommitted
Add bswap/bswap16 post MVP polyfills (AssemblyScript#34)
1 parent 3163389 commit de1c4b3

File tree

8 files changed

+3301
-0
lines changed

8 files changed

+3301
-0
lines changed

std/assembly.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ declare function free_memory(ptr: usize): void;
175175
/** Emits an unreachable operation that results in a runtime error when executed. Both a statement and an expression of any type. */
176176
declare function unreachable(): any; // sic
177177

178+
/** [Polyfill] Performs the sign-agnostic reverse bytes **/
179+
declare function bswap<T = i8 | u8 | i16 | u16 | i32 | u32 | i64 | u64 | isize | usize>(value: T): T;
180+
/** [Polyfill] Performs the sign-agnostic reverse bytes only for last 16-bit **/
181+
declare function bswap16<T = i8 | u8 | i16 | u16 | i32 | u32>(value: T): T;
182+
178183
/** NaN (not a number) as a 32-bit or 64-bit float depending on context. */
179184
declare const NaN: f32 | f64;
180185
/** Positive infinity as a 32-bit or 64-bit float depending on context. */

std/assembly/error.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ export class Error {
1212
export class RangeError extends Error {
1313
name: string = "RangeError";
1414
}
15+
16+
export class TypeError extends Error {
17+
name: string = "TypeError";
18+
}

std/assembly/polyfills.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export function bswap<T>(value: T): T {
2+
assert(sizeof<T>() == 1 || sizeof<T>() == 2 || sizeof<T>() == 4 || sizeof<T>() == 8);
3+
4+
if (sizeof<T>() == 2) {
5+
return bswap16<T>(value);
6+
} else if (sizeof<T>() == 4) {
7+
return <T>(
8+
rotl<u32>(<u32>value & 0xFF00FF00, 8) |
9+
rotr<u32>(<u32>value & 0x00FF00FF, 8)
10+
);
11+
} else if (sizeof<T>() == 8) {
12+
var a: u64 = (<u64>value >> 8) & 0x00FF00FF00FF00FF;
13+
var b: u64 = (<u64>value & 0x00FF00FF00FF00FF) << 8;
14+
var v: u64 = a | b;
15+
16+
a = (v >> 16) & 0x0000FFFF0000FFFF;
17+
b = (v & 0x0000FFFF0000FFFF) << 16;
18+
19+
return <T>rotr<u64>(a | b, 32);
20+
}
21+
return value;
22+
}
23+
24+
export function bswap16<T>(value: T): T {
25+
assert(sizeof<T>() == 1 || sizeof<T>() == 2 || sizeof<T>() == 4);
26+
27+
if (sizeof<T>() == 2 || sizeof<T>() == 4) {
28+
return <T>(((value << 8) & <T>0xFF00) | ((value >> 8) & <T>0x00FF) | (value & <T>0xFFFF0000));
29+
}
30+
return value;
31+
}

std/portable.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ declare function store<T = u8>(ptr: usize, value: T, constantOffset?: usize): vo
129129
/** Emits an unreachable operation that results in a runtime error when executed. */
130130
declare function unreachable(): any; // sic
131131

132+
/** [Polyfill] Performs the sign-agnostic reverse bytes **/
133+
declare function bswap<T = i32 | u32 | isize | usize>(value: T): T;
134+
/** [Polyfill] Performs the sign-agnostic reverse bytes only for last 16-bit **/
135+
declare function bswap16<T = i16 | u16 | i32 | u32>(value: T): T;
136+
132137
/** Changes the type of any value of `usize` kind to another one of `usize` kind. Useful for casting class instances to their pointer values and vice-versa. Beware that this is unsafe.*/
133138
declare function changetype<T>(value: any): T;
134139
/** Traps if the specified value is not true-ish, otherwise returns the value. */

std/portable.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ globalScope["select"] = function select(ifTrue, ifFalse, condition) { return con
7171
globalScope["sqrt"] = Math.sqrt;
7272
globalScope["trunc"] = Math.trunc;
7373

74+
globalScope["bswap"] = function bswap(value) {
75+
var a = value >> 8 & 0x00FF00FF;
76+
var b = (value & 0x00FF00FF) << 8;
77+
value = a | b;
78+
a = value >> 16 & 0x0000FFFF;
79+
b = (value & 0x0000FFFF) << 16;
80+
return a | b;
81+
}
82+
83+
globalScope["bswap16"] = function bswap16(value) {
84+
return ((value << 8) & 0xFF00) | ((value >> 8) & 0x00FF) | (value & 0xFFFF0000);
85+
}
86+
7487
function UnreachableError() {
7588
if (Error.captureStackTrace)
7689
Error.captureStackTrace(this, UnreachableError);

0 commit comments

Comments
 (0)