Intrinsics1 min read

atomic_xor

Atomically performs a bitwise XOR operation.

Reading Time
1 min
Word Count
143
Sections
12
Try It Live

Test atomic_xor in a live shader

Open the playground, start from a visual preset, and wire atomic_xor into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo Pending

This intrinsic does not yet have a self-contained interactive preview. Compute-only and external-resource intrinsics need a different demo path than the current fragment and 3D showcases.

The atomic_xor function atomically performs a bitwise XOR between the current value and the input, storing the result. Returns the original value.

Signature

bwsl
atomic_xor(dest, value)

Parameters

ParameterDescription
destInteger lvalue to modify atomically
valueInteger value to XOR with

Return Value

Returns the original value at dest before the operation.

Example

bwsl
pipeline ToggleFlags {
pass "Main" {
compute "Main" [64, 1, 1] {
// Toggle specific bits atomically
int toggleMask = 1 << bitToToggle;
int previous = atomic_xor(toggleState[0], toggleMask);
// Check if bit was set before
bool wasSet = (previous & toggleMask) != 0;
}
}
}

Common Use Cases

Toggle Bits

bwsl
// Toggle a flag
int flagBit = 1 << flagIndex;
atomic_xor(flags, flagBit);

Checksum/Parity

bwsl
// XOR together for parity check
atomic_xor(parityAccum, dataValue);

Random Number Contribution

bwsl
// Contribute to shared RNG state
atomic_xor(rngState, threadSeed);

Debug Visualization

bwsl
// Toggle debug bit for touched pixels
atomic_xor(debugFlags[pixelIndex], 0x1);

Difference Detection

bwsl
// XOR to find differing bits
int diff = atomic_xor(accumulated, currentValue);
// diff has 1s where values differ

Toggle Property

XOR is self-inverse: XORing the same value twice returns to the original. This makes it useful for toggles.

Parity Accumulation

XORing all values together gives parity information - useful for error detection in parallel algorithms.

Compiled Output

When compiled to GLSL:

glsl
atomicXor(dest, value)

When compiled to HLSL:

hlsl
InterlockedXor(dest, value, originalValue)

When compiled to SPIR-V:

Uses OpAtomicXor instruction.

See Also