You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
signed integer division: unspecified rounding mode for the quotient; and overflow #1774
Consider x/y where x and y are signed integers.
What is the result when the ratio x/y is not an integer.
I think it's common to say it's implementation-defined.
This is the case:
all the way up through C++11.
SPIR-V (and Vulkan does not further qualify)
Signed integer overflow can occur for:
INT_MIN / -1
where INT_MIN is the largest-magnitude negative number. This occurs because signed integers use twos-complement.
MSL 3.1 "Scalar and vector operators" doesn't say what happens for arguments of opposite signs. Since MSL is based on C++14, I think it gets (from cppreference)
The quotient is truncated towards zero (fractional part is discarded).
Doesn't say specifically what happens for integer division of opposite signs
Gives only examples with positive sign
For modulus it says:
The modulus operator returns the remainder of a division. This produces different results when using integers and floating-point numbers. Integer remainders that are fractional will be truncated.
And then:
The % operator is defined only in cases where either both sides are positive or both sides are negative
DN: Polyfill more straightforward than I expected. It’s more instructions now, but people probably don’t care?
MM: Probably best to be portable first. Maybe less portable a la fastmath later
KN: Devs probably won’t love that they get 3 instructions for one “instruction”. So maybe we can allow operator% only for unsigned, since that’s “perfect”, but mod(int,int) that’s portable.
DM: Do people care wrt how expensive division is?
MM: Ints uncommon in shaders, too. And division on ints are doubly uncommon. Perhaps even so uncommon that it’s fine for it to be 3 instructions.
JG: Ints will be more common in compute shaders, though.
Consensus:
Require portability even if it emits extra instructions
Activity
kvark commentedon May 28, 2021
What about MSL and HLSL. Isn't it defined there?
dneto0 commentedon Jun 1, 2021
MSL 3.1 "Scalar and vector operators" doesn't say what happens for arguments of opposite signs. Since MSL is based on C++14, I think it gets (from cppreference)
HLSL doc (https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators ):
For modulus it says:
And then:
dneto0 commentedon Jun 1, 2021
Something concrete to look at:
kdashg commentedon Jun 1, 2021
WGSL meeting minutes 2021-06-01
dneto0 commentedon Jun 3, 2021
Polyfilling signed integer division
Unsigned division
For integers x, y, with 0 <= x and 0 < y, there are unique integers
q (the quotient) and r (the remainder) such that:
The quotient is taken to be "the" integer division of x by y.
Signed division
If either x or y is negative, and b is non-zero, there are two natural choices
for q:
The trend in C-family languages is to use truncation, i.e.
q_rtz.Computing
q_rtzin in terms of unsigned integer division is done as follows:where sign(w) is 1 when x < 0 and -1 when w > 0.
Then define the signed remainder from:
that is:
Algorithm 1 in pseudo-code
Table of examples
edit: Fix the select condition in the code sample.
dneto0 commentedon Jun 3, 2021
I think @alan-baker had a more concise way to compute the corresponding modulus:
I think that's
kvark commentedon Jun 3, 2021
Looks quite doable, thank you for investigating this!
kdashg commentedon Jun 8, 2021
WGSL meeting minutes 2021-06-08
dneto0 commentedon Jun 10, 2021
Decision from 2021-06-08
a = q * b + requation.We didn't talk about division by zero or the INT_MIN / -1 case.
sdiv.wgsl: detailed semantics of integer division
wgsl: detailed semantics of integer division and remander
wgsl: detailed semantics of integer division and remander
dneto0 commentedon Jun 10, 2021
Whoops. The pseudocode for the sdiv and smod algorithms should use
abs(u32(.))instead ofu32(.)constructors directly.dneto0 commentedon Jun 10, 2021
And
absis slightly more expensive thanu32construction, but probably still much cheaper than the division.dneto0 commentedon Jun 10, 2021
So, restating:
dneto0 commentedon Jun 10, 2021
What happens for INT_MIN / -1.
INT_MIN in i32 is 0x10000000.
Taking the rule abs(x) = -x when x <0, then abs(INT_MIN), with two's complement is:
So u32(abs(INTMIN)) = 0x10000000
Divided by u32(abs(-1)) = 1 yields:
q_abs = 0x1000 0000
And (INT_MIN < 0) == (-1 <0) is true, so the result of the polyfill for sdiv is:
So the polyfill result INT_MIN = sdiv(INT_MIN, -1).
We have a choice:
dneto0 commentedon Jun 10, 2021
I've updated #1830 to take option 2 for the overflow case: define the result by rule.
wgsl: detailed semantics of integer division and remander
wgsl: detailed semantics of integer division and remander
wgsl: detailed semantics of integer division and remainder (#1830)
Rename `make*F32IntervalCase` to `make*ToF32IntervalCase` (gpuweb#1775)