-
-
Notifications
You must be signed in to change notification settings - Fork 672
Add bswap post MVP polyfills #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
eb9421c
3f55503
853ca8f
4be02c5
cd48b90
76e156f
a7de3e0
7fde983
1da2359
2fe5c20
dd30363
128da24
70e3e62
0c411d4
1cbf4c0
728a55d
1c87f0b
f70dc25
80b3e28
702b55f
06196c0
a6878af
de93e90
0ac2bfc
97fe21b
ff83396
f3fa3de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export function bswap<T>(value: T): T { | ||
if (sizeof<T>() == 1) { | ||
return value; | ||
} else if (sizeof<T>() == 2) { | ||
return bswap16<T>(value); | ||
} else if (sizeof<T>() == 4) { | ||
return <T>( | ||
rotl<u32>(<u32>value & 0xFF00FF00, 8) | | ||
rotr<u32>(<u32>value & 0x00FF00FF, 8) | ||
); | ||
} else if (sizeof<T>() == 8) { | ||
var a: u64 = (<u64>value >> 8) & 0x00FF00FF00FF00FF; | ||
var b: u64 = (<u64>value & 0x00FF00FF00FF00FF) << 8; | ||
var v: u64 = a | b; | ||
|
||
a = (v >> 16) & 0x0000FFFF0000FFFF; | ||
b = (v & 0x0000FFFF0000FFFF) << 16; | ||
|
||
return <T>rotr<u64>(a | b, 32); | ||
} | ||
throw new TypeError("Unsupported type"); | ||
} | ||
|
||
export function bswap16<T>(value: T): T { | ||
if (sizeof<T>() == 1) { | ||
return value; | ||
} else if (sizeof<T>() == 2 || sizeof<T>() == 4) { | ||
return <T>(((value << 8) & <T>0xFF00) | ((value >> 8) & <T>0x00FF) | (value & <T>0xFFFF0000)); | ||
} | ||
throw new TypeError("Unsupported type"); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,11 @@ declare function store<T = u8>(ptr: usize, value: T, constantOffset?: usize): vo | |
/** Emits an unreachable operation that results in a runtime error when executed. */ | ||
declare function unreachable(): any; // sic | ||
|
||
/** [Polyfill] Performs the sign-agnostic reverse bytes **/ | ||
declare function bswap<T = i8 | u8 | i16 | u16 | i32 | u32 | isize | usize>(value: T): T; | ||
/** [Polyfill] Performs the sign-agnostic reverse bytes only for last 16-bit **/ | ||
declare function bswap16<T = i8 | u8 | i16 | u16 | i32 | u32>(value: T): T; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do these actually exist in the portable JS library (portable.js)? If not, you can just skip them in portable.d.ts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll implement this in portable.js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that in portable, you have to decide for let's say i32 arguments only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
/** 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.*/ | ||
declare function changetype<T>(value: any): T; | ||
/** Traps if the specified value is not true-ish, otherwise returns the value. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In stdlib I am trying to use assertions instead of errors, just because errors add static string data to memory while assertions do not when turned off.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, just an assertion without a string message, but a comment behind that shows up when using source maps, that is, unless we are implementing an existing JS interface that actually is specified to throw an error on specific occasions, of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed