Created
September 12, 2023 06:29
-
-
Save DDR0/411f341c8161fcdf28ae2b73cc2ecd3e to your computer and use it in GitHub Desktop.
pshufb for javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Javascript/ECMAScript adaptation of the Intel pshufb instruction. I think. | |
//Does not work in-place. No zeroing. CC BY-NC-SA 2023 DDR0. | |
//Example: | |
/* | |
pshufb.num(0x0a0b0c0d, | |
0x01010303).toString(16).padStart(8, '0'); | |
returns: '0c0c0a0a' | |
*/ | |
export const pshufb = Object.freeze({ | |
__proto__: null, | |
///JS number implementation, 32 bits. | |
num: (num, mask) => 0 | |
| ((num >>> ((mask >>> 0 & 0xFF)*8) & 0xFF) << 0) | |
| ((num >>> ((mask >>> 8 & 0xFF)*8) & 0xFF) << 8) | |
| ((num >>> ((mask >>> 16 & 0xFF)*8) & 0xFF) << 16) | |
| ((num >>> ((mask >>> 24 & 0xFF)*8) & 0xFF) << 24), | |
///JS BigInt implementation, covers the powers from 64 to 2048 bits. | |
big: (num, mask) => { | |
let out = 0n, byt = 0n | |
while (!((!mask) && (byt == 64 || byt == 128 || byt == 256 || byt == 512 || byt == 1024 || byt == 2048))) { | |
out |= ((num >> ((mask & 255n)*8n) & 255n) << byt) | |
byt += 8n | |
mask >>= 8n | |
} | |
return out | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment