Files
wgpu/naga/tests/out/hlsl/empty-global-name.hlsl
T
Jamie Nicol 88f6e9b8f8 [naga hlsl-out] Avoid undefined behaviour for signed integer addition, subtraction, and multiplication
Though not explicitly specified one way or the other, we have been
informed by DirectX engineers that signed integer overflow may be
undefined behaviour in some cases. To avoid this, we therefore bitcast
signed operands to unsigned prior to performing addition, subtraction,
or multiplication, then bitcast the result back to signed. As signed
types are represented as two's complement, this gives the correct
result whilst avoid any potential undefined behaviour.

Unfortunately HLSL's bitcast functions asint() and asuint() only work
for the 32-bit int and uint types. We therefore only apply this
workaround for 32-bit signed arithmetic. Support for other bit widths
could be added in the future, but extra care must be taken when
converting from unsigned to signed to avoid undefined or
implemented-defined behaviour.
2025-02-14 15:17:40 -08:00

19 lines
273 B
HLSL

struct type_1 {
int member;
};
RWByteAddressBuffer unnamed : register(u0);
void function()
{
int _e3 = asint(unnamed.Load(0));
unnamed.Store(0, asuint(asint(asuint(_e3) + asuint(int(1)))));
return;
}
[numthreads(1, 1, 1)]
void main()
{
function();
}