Repository navigation
wgsl: detailed semantics of integer division and remainder - #1830
Conversation
|
This is a bit mealy-mouthed about divide by zero and the overflow case. |
99f1465 to
edd6207
Compare
| to perform more operations than when computing an unsigned division. | ||
| Use unsigned division when both operands are known to have the same sign. | ||
|
|
||
| Produces a [=dynamic error=] when |e2| evaluates to zero. |
There was a problem hiding this comment.
so we can say "produces a dynamic error if e1 is the most negative value" as well?
There was a problem hiding this comment.
So that's something we can debate. If the polyfill is used then no exception is required. Let's discuss this next week.
|
Metal Shading Language spec in section 3.1:
|
WGSL meeting minutes 2021-06-15
|
|
We can avoid undefined behaviour on unsigned integer division-by-zero if you replace with a / clamp(b,1,b); |
Is this a fancy way to write |
|
From 2021-08-03, @jdashg suggested defining: x/ 0 = x |
|
To put the idea more concretely, if we want x/0 = x, then you have to do the select on the divisor to avoid the 0 case: It's incorrect to do the select on the division result because you'd still invoke the undefined behaviour in underlying languages. That is, this is the wrong solution: select(x/y, x, y==0) // still does the bad case. |
|
Are there alternatives here? I.e. could you think of a different polyfill that is perhaps more efficient than a |
I think conditional select is pretty cheap. To confirm that, I found that it's a native instruction in:
|
WGSL meeting minutes 2021-08-03
|
WGSL meeting minutes 2021-10-26
|
|
Also would fix #2187 which requests changing the semantics from SPIR-V OpSMod to OpSRem. If I read correctly, that is already included in this proposal. |
a801d88 to
da9ef30
Compare
|
Only did a rebase |
|
The group wanted to avoid dynamic error on divide by zero. So now the PR specifies definite answers for divide by zero and % by zero. |
| <!-- | ||
| where Divisor = select(e2, 1, e2==0) | ||
| where MINIT = most negative value in |T| | ||
| select( truncate(e1/Divisor), MININT , (MININT==e1) && (Divisor == -1)) |
There was a problem hiding this comment.
That's a bit unfortunate. It's a complex condition per element of division that's meant to handle a case that is extremely rare (unlike, say, division by zero).
Adds definition for `truncate` For divide by zero and signed integer division overlow, say they produce a "dynamic error". Fixes: gpuweb#1774
This pins down the result for both division and remainder.
These are no longer "dynamic errors".
For integer division: e1 / 0 = e1
For integers, e1 % 0 = 0
The % case is somewhat arbitrary, but it makes this true:
e1 = (e1 / 0) + (e1 % 0)
Another way of formulating the signed integer cases is to forcibly
use a divisor of 1 in the edge cases:
where MINIT = most negative value in |T|
where Divisor = select(e2, 1, (e2==0) | ((e1 == MININT) & (e2 == -1)))
then
"e1 / e2" = truncate(e1 / Divisor)
"e1 % e2" = e1 - truncate(e1/Divisor) * Divisor
The unsigned integer case is similar but doesn't have (MININT,-1) case.
79b616f to
52563eb
Compare
|
Discussed in 2022-01-25 meeting Agreed to land as proposed, after fixing merge conflict. |
|
In the meeting @kdashg suggested we make sure that the / and % formulations pick the quotient(divisor) in the same way. I checked, and the proposed rules do pick them the same way. In fact, both edge case conditions end up selecting the divisor the same way: So there is only one select required, but its condition is a combination of equality checks. That seems nicer than having nested selects. |
WGSL meeting minutes 2022-01-25
|
Adds definition for
truncateFor divide by zero and signed integer division overlow, say they
produce a "dynamic error".
Fixes: #1774
Update 2022-01-18: Group wants to avoid dynamic error on divide by zero. So now the PR specifies definite answers for divide by zero and % by zero.