Skip to content

Remove % for signed integral - #1763

Closed
dneto0 wants to merge 1 commit into
gpuweb:mainfrom
dneto0:signed-integer-mod
Closed

dneto0 wants to merge 1 commit into
gpuweb:mainfrom
dneto0:signed-integer-mod

Conversation

@dneto0

@dneto0 dneto0 commented May 21, 2021

Copy link
Copy Markdown
Contributor

In Vulkan, OpSMod is undefined if either operand is negative. The spec
explicit says it's no more functional than OpUMod.
https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#spirvenv-precision-operation

@dneto0 dneto0 added the wgsl WebGPU Shading Language Issues label May 21, 2021
@dneto0 dneto0 added this to the MVP milestone May 21, 2021
@dneto0 dneto0 mentioned this pull request May 21, 2021
@dneto0
dneto0 requested review from alan-baker, kvark and litherum May 21, 2021 21:18
@kvark

kvark commented May 25, 2021

Copy link
Copy Markdown
Contributor

Counter proposal: just polyfill this on SPIR-V with x - y * (x / y), which matches OpFRem from #1696, and is consistent with floating-point % that we have.

@dneto0

dneto0 commented May 25, 2021

Copy link
Copy Markdown
Contributor Author

Counter proposal: just polyfill this on SPIR-V with x - y * (x / y), which matches OpFRem from #1696, and is consistent with floating-point % that we have.

You need to watch out for INT_MIN / -1, which overflows signed integer.

@kvark

kvark commented May 26, 2021

Copy link
Copy Markdown
Contributor

Right. I imagine we could just put a note into spec saying that under specific conditions this computation falls under the umbrella of integer overflow scenarios.

@dneto0

dneto0 commented May 27, 2021

Copy link
Copy Markdown
Contributor Author

x - y * (x / y)

Now I have to ask about how signed integer division works.

cpprefrence says:

  • Up to and including C++11, the quotient rounding direction is implementation-defined
  • After C++11, the quotient is truncated (toward zero, i.e. drop the fractional part)
  • If a/b is representable in the result type, then (a/b)*b + a%b == a

It seems to me important that the identity should hold even when a/b is not an integer. Otherwise the important things like the Euclidean algorithm breaks down.
(I think its still the case that the magnitude of a%b is always smaller than the divisor b, which is what Euclid's algorithm needs in order to make progress.)

I think the quotient-rounding direction of integer division is orthogonal.

@kdashg

kdashg commented Jun 1, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-05-25
  • Undefined in spir-v and in glsl for negatives
  • DN: Please mak the polyfill without control flow. (use select)
  • DN:DM: What about overflows generally? INT_MIN / -1? (overflows signed integer)
  • JG to file an issue for that
  • DM to try making a spir-v polyfill

@dneto0

dneto0 commented Jun 1, 2021

Copy link
Copy Markdown
Contributor Author

Counter proposal: just polyfill this on SPIR-V with x - y * (x / y)

#1774 tracks questions about signed integer division, particularly for opposite signed operands.

@dneto0
dneto0 marked this pull request as draft June 1, 2021 16:52
@kdashg

kdashg commented Jun 8, 2021

Copy link
Copy Markdown
Contributor

@kvark

kvark commented Jun 9, 2021

Copy link
Copy Markdown
Contributor

@dneto0 I think we agreed to not remove it on the call yesterday, so this PR can be closed.

@kvark kvark closed this Jun 9, 2021
ben-clayton pushed a commit to ben-clayton/gpuweb that referenced this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants