wgsl: support extended integer arithmetic for add, subtract, multiply
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.
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.
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.
ANGLE polyfills these methods when translating HLSL to GLSL. The translations can be found here and here.
This was in post-MVP, should it be post-V1 or V1?
This was in post-MVP, should it be post-V1 or V1?
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.
WGSL meeting minutes 2022-01-25
- DN: Google happy post-V1
- RM: ok either way
- Post V1