wasm-arrays
wasm-arrays copied to clipboard
Improve bit shifting
I was reading your article https://becominghuman.ai/passing-and-returning-webassembly-array-parameters-a0f572c65d97
I noticed this code:
` const buf = Module._malloc(typedArray.length * typedArray.BYTES_PER_ELEMENT)
switch (heapIn) {
case "HEAP8": case "HEAPU8":
Module[heapIn].set(typedArray, buf)
break
case "HEAP16": case "HEAPU16":
Module[heapIn].set(typedArray, buf >> 1)
break
case "HEAP32": case "HEAPU32": case "HEAPF32":
Module[heapIn].set(typedArray, buf >> 2)
break
case "HEAPF64":
Module[heapIn].set(typedArray, buf >> 3)
break
}`
Can't this be simplified to:
` const buf = Module._malloc(typedArray.length * typedArray.BYTES_PER_ELEMENT)
Module[heapIn].set(typedArray, buf * typedArray.BYTES_PER_ELEMENT)`
Since it looks like the bit shifting is the same as multiplying with the number of bytes per element.
Ah, interesting. Perhaps this would work! I haven't tested this however, have you, with success?
No, I didn't compile your code, I was just delving into web assembly and stumbled upon your blog and code.