gpu.js
gpu.js copied to clipboard
Default fixIntegerDivisionAccuracy = true breaks negative division on Mac M1 Max

What is wrong?
When dividing a negative number by a negative number, the result is rounded to an integer. For example: -1045000 / -2042500 = 0.
Where does it happen?
Using node.js in Chrome 99.0.4844.27 on my Macbook Pro M1 Max, macOS 12.2.1. I am guessing it has to do with the M1 processor.
How do we replicate the issue?
Fails when running in GPU:
gpu = new window.GPU();
testGPU = gpu.createKernel(function() {
return [-1045000 / -2042500, 1045000 / 2042500]
}, {fixIntegerDivisionAccuracy: false }).setOutput([1])
testGPU()[0]; // incorrectly outputs [0, 0.5116279125213623]
Works when running in CPU:
let cpu = new window.GPU({ mode: "cpu" })
testCPU = cpu.createKernel(function() {
return [-1045000 / -2042500, 1045000 / 2042500]
}, { }).setOutput([1])
testCPU()[0]; // correctly outputs [0.5116279125213623, 0.5116279125213623]
Work-around appears to be to disable fixIntegerDivisionAccuracy:
gpu = new window.GPU();
testGPU = gpu.createKernel(function() {
return [-1045000 / -2042500, 1045000 / 2042500]
}, {fixIntegerDivisionAccuracy: false }).setOutput([1])
testGPU()[0]; // correctly outputs [0.5116279125213623, 0.5116279125213623]
How important is this (1-5)?
- The rounding makes it hard to track down. So pretty bad, given the default setup will fail to do basic math correctly. But assuming
fixIntegerDivisionAccuracyis not needed to fix other things on the M1, this could be an easy fix.
Expected behavior (i.e. solution)
See my example in the CPU and workaround above.
Other Comments
I tested a few different divisions, and it looks like it is rounding to the nearest integer when numerator and denominator are both negative. (As opposed to just setting the result to 0.) Fails with simple integers too, like -7 / -3.