Skip to content

wgsl: OpXMod -> OpXRem for integers - #2187

Closed
munrocket wants to merge 1 commit into
gpuweb:mainfrom
munrocket:main
Closed

munrocket wants to merge 1 commit into
gpuweb:mainfrom
munrocket:main

Conversation

@munrocket

@munrocket munrocket commented Oct 15, 2021 •

Copy link
Copy Markdown
Contributor

Should we change OpSMod -> OpSRem and OpUMod -> OpURem for integers?

Motivation:

  1. Making WGSL more consistent. Currently we have different conventions for floats and int's.
  2. C/Javascript using OpXRem for integers. Spir-V require non-negative numbers so OpSRem===OpSMod there.
  3. You not removing OpSMod operation according to Remove % for signed integral #1763 (comment)

Conclusion:

  • OpUMod and OpURem is literally the same so just editorial change.

  • OpSRem not defined in Spir-V for non-negative numbers so basically the same too with exception.
    If you going to emulate it. OpSRem looks simple x - y * (x / y), but proper emulation for OpSMod looks wierd: if (y < 0) { x = -x; y = -y; }; r = (x >= 0) ? x / y : ((x + 1) / x) - 1; r = x - y * r;. I believe that all shader languages uses the same rounding in division? Because otherwise we will have problems with emulation and integer division consistency between backends.

Little bit related issue: #1696, #1763.
This issue not related to float fmod (which is much more useful for webgl users and frequently requested)

@w3cbot

w3cbot commented Oct 15, 2021

Copy link
Copy Markdown

munrocket marked as non substantive for IPR from ash-nazg.

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (a28e7cd):
WebGPU | IDL
WGSL
Explainer

@kvark kvark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we've already agreed on this, just never made it into the text.

@dneto0

dneto0 commented Oct 19, 2021

Copy link
Copy Markdown
Contributor

See also #1830 which was an attempt at updating the semantics of these operations. On some backends, that would mean polyfilling when needed.

@kdashg

kdashg commented Oct 20, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-10-19
  • MM: Can we discuss in terms of semantics rather than SPIR-V opcodes.
  • DM: Closest thing to previous agreement in #1830
  • BC: Agree we should discuss direct semantics.
  • Resolved that we should include the algorithm instead of just the spir-v opcode
  • AB: I think one place this is getting stuck is how to deal with negatives, since spir-v and hlsl have restrictions on what can or can’t be negative. We have to decide how that works.
  • MM: Should probably just polyfill
  • DN: I think there was another issue that got held up on figuring out how “dynamic error” should be defined
  • DN to split things into separate sections
  • MM: Our position is that these functions should work with signed negative numbers.
  • MM: Making behavior match between ints and floats also seems good
  • JG: +1

@kainino0x kainino0x added the wgsl WebGPU Shading Language Issues label Oct 25, 2021
@ben-clayton

Copy link
Copy Markdown
Contributor

#2450 just reminded me of this PR. We should get this landed or added to the next meeting agenda.

Comment thread wgsl/index.bs
<td>|e1| : |T|<br>|e2| : |T|<br>|T| is [UNSIGNEDINTEGRAL]
<td>|e1| `%` |e2| : |T|
<td>Unsigned integer remainder. [=Component-wise=] when |T| is a vector. (OpUMod)
<td>Unsigned integer remainder. [=Component-wise=] when |T| is a vector. (OpURem)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpURem isn't a SPIR-V opcode. There would be no difference from OpURem.

@dneto0

dneto0 commented Jan 19, 2022

Copy link
Copy Markdown
Contributor

Discussed in the 2022-01-18 meeting

Alan points out there is no OpURem. There is only OpUMod, and it would be the same anyway.

@dneto0 dneto0 closed this Jan 19, 2022
@dneto0

dneto0 commented Jan 19, 2022

Copy link
Copy Markdown
Contributor

Landed via #2518

@kdashg

kdashg commented Jan 22, 2022

Copy link
Copy Markdown
Contributor

details>

WGSL meeting minutes 2022-01-18

  • (This is what we asked for, right?)
  • David (offline): This aspect is already covered by PR #1830 “detailed semantics of integer division and remainder”.
    • That PR does:
      • Defines all the cases for negative divisor and dividend
      • Handles overflow case for division ( MIN_INT / -1 ).
      • Says division by 0 is a “dynamic error”
    • Further discussion suggested a polyfill that used ‘select’ to avoid divide by zero.
    • PR 1830 now specifies definite result for integer division and % with zero.
  • Dzmitry on Matrix chat said
    • “I know integer division is slow already, and we can make them a tiny bit slower
    • but if I have to use it for some reason, I'd be very pissed off by those 2 extra conditions on every division.”
  • For now, take 2187, then revisit 1830.

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.

8 participants