mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 22:11:01 +01:00
Adds infrastructure to the MSL backend for emitting helper functions, based upon the existing HLSL backend equivalent. Emit helper functions for MathFunction::Abs and UnaryOperator::Negate with a signed integer scalar or vector operand. And for BinaryOperator::Divide and BinaryOperator::Modulo with signed or unsigned integer scalar or vector operands. Abs and Negate are written to avoid signed integer overflow when the operand equals INT_MIN. This is achieved by bitcasting the value to unsigned and using the negation operator, then bitcasting the result back to signed. Division and Modulo avoid undefined bevaviour for INT_MIN / -1 and divide-by-zero by using 1 for the divisor instead. Additionally we avoid undefined behaviour when using the modulo operator when either or both operands are negative by using the equation from the WGSL spec, using division, subtraction and multiplication, rather than MSL's modulus operator. Lastly, as the usage of the wrapper function for unary integer negation makes the negation_avoids_prefix_decrement() testcase less interesting, we extend it to additionally test negating floats.
68 lines
1.4 KiB
Plaintext
68 lines
1.4 KiB
Plaintext
// language: metal1.0
|
|
#include <metal_stdlib>
|
|
#include <simd/simd.h>
|
|
|
|
using metal::uint;
|
|
|
|
struct _mslBufferSizes {
|
|
uint size0;
|
|
};
|
|
|
|
typedef uint type_1[1];
|
|
struct PrimeIndices {
|
|
type_1 data;
|
|
};
|
|
|
|
uint naga_mod(uint lhs, uint rhs) {
|
|
return lhs % metal::select(rhs, 1u, rhs == 0u);
|
|
}
|
|
|
|
uint naga_div(uint lhs, uint rhs) {
|
|
return lhs / metal::select(rhs, 1u, rhs == 0u);
|
|
}
|
|
|
|
uint collatz_iterations(
|
|
uint n_base
|
|
) {
|
|
uint n = {};
|
|
uint i = 0u;
|
|
n = n_base;
|
|
uint2 loop_bound = uint2(0u);
|
|
while(true) {
|
|
if (metal::all(loop_bound == uint2(4294967295u))) { break; }
|
|
loop_bound += uint2(loop_bound.y == 4294967295u, 1u);
|
|
uint _e4 = n;
|
|
if (_e4 > 1u) {
|
|
} else {
|
|
break;
|
|
}
|
|
{
|
|
uint _e7 = n;
|
|
if (naga_mod(_e7, 2u) == 0u) {
|
|
uint _e12 = n;
|
|
n = naga_div(_e12, 2u);
|
|
} else {
|
|
uint _e16 = n;
|
|
n = (3u * _e16) + 1u;
|
|
}
|
|
uint _e20 = i;
|
|
i = _e20 + 1u;
|
|
}
|
|
}
|
|
uint _e23 = i;
|
|
return _e23;
|
|
}
|
|
|
|
struct main_Input {
|
|
};
|
|
kernel void main_(
|
|
metal::uint3 global_id [[thread_position_in_grid]]
|
|
, device PrimeIndices& v_indices [[user(fake0)]]
|
|
, constant _mslBufferSizes& _buffer_sizes [[user(fake0)]]
|
|
) {
|
|
uint _e9 = v_indices.data[global_id.x];
|
|
uint _e10 = collatz_iterations(_e9);
|
|
v_indices.data[global_id.x] = _e10;
|
|
return;
|
|
}
|