gpuweb icon indicating copy to clipboard operation
gpuweb copied to clipboard

wgsl: support extended integer arithmetic for add, subtract, multiply

Open dneto0 opened this issue 4 years ago • 7 comments

SPIR-V has:

  • add-with-carry OpIAddCarry
  • sub-with-borrow OpISubBorrow
  • unsigned multiply (1 word * 1 word produces 2 words)
  • signed multiply (1 word * 1 word produces 2 words)

MSL has

  • hadd(x,y) Returns (x + y) >> 1. The intermediate sum does not modulo overflow.
  • rhadd(x,y) Returns (x + y + 1) >> 1. The intermediate sum does not modulo overflow.
  • mulhi(x,y) Returns the top word of x * y. (Presumably you do x*y to get the bottom bits)
  • madhi(x,y,c) return mulhi(x,y)+c.

I haven't looked at HLSL yet.


update: Fixed typo in expansion of madhi.

dneto0 avatar Mar 26 '21 04:03 dneto0

The SPIR-V opcodes OpIAddCarry and OpISubBorrow both interpret the inputs as unsigned integers only. So support is only partially available. For example, 2 + (-1) would yield a carry.

alan-baker avatar Mar 29 '21 18:03 alan-baker

WGSL meeting minutes 2021-05-11
  • AB: should be post-MVP
  • MM: not clear what HLSL offers for this
  • TR: mul_high is exposed in DXIL, but not in HLSL
  • MM: what is our rule for this: will all implementations that rely on HLSL go straight to DXIL, or do we want compatibility with HLSL the language.
  • DN: DXIL path is at least one year from now for us, so we want support in HLSL itself
  • GR: also not clear that DXIL-only operations are as widely tested.
  • MM: so we should only implement things which are supported by HLSL.

kdashg avatar May 17 '21 18:05 kdashg

ANGLE polyfills these methods when translating HLSL to GLSL. The translations can be found here and here.

Kangz avatar Sep 17 '21 14:09 Kangz

This was in post-MVP, should it be post-V1 or V1?

Kangz avatar Jan 22 '22 10:01 Kangz

This was in post-MVP, should it be post-V1 or V1?

Kangz avatar Jan 22 '22 10:01 Kangz

I think there's a typo in the OP:

madhi(x,y,c) return madhi(x,y)+c.

should be

madhi(x,y,c) return mulhi(x, y) + c.

litherum avatar Jan 25 '22 17:01 litherum

WGSL meeting minutes 2022-01-25
  • DN: Google happy post-V1
  • RM: ok either way
  • Post V1

kdashg avatar Jan 26 '22 18:01 kdashg