mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-20 06:21:13 +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.
218 lines
8.3 KiB
Plaintext
218 lines
8.3 KiB
Plaintext
// language: metal2.3
|
|
#include <metal_stdlib>
|
|
#include <simd/simd.h>
|
|
|
|
using metal::uint;
|
|
|
|
struct UniformCompatible {
|
|
uint val_u32_;
|
|
int val_i32_;
|
|
float val_f32_;
|
|
char _pad3[4];
|
|
ulong val_u64_;
|
|
char _pad4[8];
|
|
metal::ulong2 val_u64_2_;
|
|
char _pad5[16];
|
|
metal::ulong3 val_u64_3_;
|
|
metal::ulong4 val_u64_4_;
|
|
long val_i64_;
|
|
char _pad8[8];
|
|
metal::long2 val_i64_2_;
|
|
metal::long3 val_i64_3_;
|
|
metal::long4 val_i64_4_;
|
|
ulong final_value;
|
|
};
|
|
struct type_11 {
|
|
ulong inner[2];
|
|
};
|
|
struct type_12 {
|
|
long inner[2];
|
|
};
|
|
struct StorageCompatible {
|
|
type_11 val_u64_array_2_;
|
|
type_12 val_i64_array_2_;
|
|
};
|
|
constant ulong constant_variable = 20uL;
|
|
|
|
long naga_abs(long val) {
|
|
return metal::select(as_type<long>(-as_type<ulong>(val)), val, val >= 0);
|
|
}
|
|
|
|
long int64_function(
|
|
long x,
|
|
constant UniformCompatible& input_uniform,
|
|
device UniformCompatible const& input_storage,
|
|
device StorageCompatible const& input_arrays,
|
|
device UniformCompatible& output,
|
|
device StorageCompatible& output_arrays
|
|
) {
|
|
long val = 20L;
|
|
long _e8 = val;
|
|
val = as_type<long>(as_type<ulong>(_e8) + as_type<ulong>(as_type<long>(as_type<ulong>(as_type<long>(as_type<ulong>(31L) - as_type<ulong>(1002003004005006L))) + as_type<ulong>(-9223372036854775807L))));
|
|
long _e10 = val;
|
|
long _e13 = val;
|
|
val = as_type<long>(as_type<ulong>(_e13) + as_type<ulong>(as_type<long>(as_type<ulong>(_e10) + as_type<ulong>(5L))));
|
|
uint _e17 = input_uniform.val_u32_;
|
|
long _e18 = val;
|
|
long _e22 = val;
|
|
val = as_type<long>(as_type<ulong>(_e22) + as_type<ulong>(static_cast<long>(_e17 + static_cast<uint>(_e18))));
|
|
int _e26 = input_uniform.val_i32_;
|
|
long _e27 = val;
|
|
long _e31 = val;
|
|
val = as_type<long>(as_type<ulong>(_e31) + as_type<ulong>(static_cast<long>(as_type<int>(as_type<uint>(_e26) + as_type<uint>(static_cast<int>(_e27))))));
|
|
float _e35 = input_uniform.val_f32_;
|
|
long _e36 = val;
|
|
long _e40 = val;
|
|
val = as_type<long>(as_type<ulong>(_e40) + as_type<ulong>(static_cast<long>(_e35 + static_cast<float>(_e36))));
|
|
long _e44 = input_uniform.val_i64_;
|
|
long _e47 = val;
|
|
val = as_type<long>(as_type<ulong>(_e47) + as_type<ulong>(metal::long3(_e44).z));
|
|
ulong _e51 = input_uniform.val_u64_;
|
|
long _e53 = val;
|
|
val = as_type<long>(as_type<ulong>(_e53) + as_type<ulong>(as_type<long>(_e51)));
|
|
metal::ulong2 _e57 = input_uniform.val_u64_2_;
|
|
long _e60 = val;
|
|
val = as_type<long>(as_type<ulong>(_e60) + as_type<ulong>(as_type<metal::long2>(_e57).y));
|
|
metal::ulong3 _e64 = input_uniform.val_u64_3_;
|
|
long _e67 = val;
|
|
val = as_type<long>(as_type<ulong>(_e67) + as_type<ulong>(as_type<metal::long3>(_e64).z));
|
|
metal::ulong4 _e71 = input_uniform.val_u64_4_;
|
|
long _e74 = val;
|
|
val = as_type<long>(as_type<ulong>(_e74) + as_type<ulong>(as_type<metal::long4>(_e71).w));
|
|
long _e80 = input_uniform.val_i64_;
|
|
long _e83 = input_storage.val_i64_;
|
|
output.val_i64_ = as_type<long>(as_type<ulong>(_e80) + as_type<ulong>(_e83));
|
|
metal::long2 _e89 = input_uniform.val_i64_2_;
|
|
metal::long2 _e92 = input_storage.val_i64_2_;
|
|
output.val_i64_2_ = as_type<metal::long2>(as_type<metal::ulong2>(_e89) + as_type<metal::ulong2>(_e92));
|
|
metal::long3 _e98 = input_uniform.val_i64_3_;
|
|
metal::long3 _e101 = input_storage.val_i64_3_;
|
|
output.val_i64_3_ = as_type<metal::long3>(as_type<metal::ulong3>(_e98) + as_type<metal::ulong3>(_e101));
|
|
metal::long4 _e107 = input_uniform.val_i64_4_;
|
|
metal::long4 _e110 = input_storage.val_i64_4_;
|
|
output.val_i64_4_ = as_type<metal::long4>(as_type<metal::ulong4>(_e107) + as_type<metal::ulong4>(_e110));
|
|
type_12 _e116 = input_arrays.val_i64_array_2_;
|
|
output_arrays.val_i64_array_2_ = _e116;
|
|
long _e117 = val;
|
|
long _e119 = val;
|
|
val = as_type<long>(as_type<ulong>(_e119) + as_type<ulong>(naga_abs(_e117)));
|
|
long _e121 = val;
|
|
long _e122 = val;
|
|
long _e123 = val;
|
|
long _e125 = val;
|
|
val = as_type<long>(as_type<ulong>(_e125) + as_type<ulong>(metal::clamp(_e121, _e122, _e123)));
|
|
long _e127 = val;
|
|
metal::long2 _e128 = metal::long2(_e127);
|
|
long _e129 = val;
|
|
metal::long2 _e130 = metal::long2(_e129);
|
|
long _e132 = val;
|
|
val = as_type<long>(as_type<ulong>(_e132) + as_type<ulong>(( + _e128.x * _e130.x + _e128.y * _e130.y)));
|
|
long _e134 = val;
|
|
long _e135 = val;
|
|
long _e137 = val;
|
|
val = as_type<long>(as_type<ulong>(_e137) + as_type<ulong>(metal::max(_e134, _e135)));
|
|
long _e139 = val;
|
|
long _e140 = val;
|
|
long _e142 = val;
|
|
val = as_type<long>(as_type<ulong>(_e142) + as_type<ulong>(metal::min(_e139, _e140)));
|
|
long _e144 = val;
|
|
long _e146 = val;
|
|
val = as_type<long>(as_type<ulong>(_e146) + as_type<ulong>(metal::select(metal::select(long(-1), long(1), (_e144 > 0)), long(0), (_e144 == 0))));
|
|
long _e148 = val;
|
|
return _e148;
|
|
}
|
|
|
|
ulong uint64_function(
|
|
ulong x_1,
|
|
constant UniformCompatible& input_uniform,
|
|
device UniformCompatible const& input_storage,
|
|
device StorageCompatible const& input_arrays,
|
|
device UniformCompatible& output,
|
|
device StorageCompatible& output_arrays
|
|
) {
|
|
ulong val_1 = 20uL;
|
|
ulong _e8 = val_1;
|
|
val_1 = _e8 + ((31uL + 18446744073709551615uL) - 18446744073709551615uL);
|
|
ulong _e10 = val_1;
|
|
ulong _e13 = val_1;
|
|
val_1 = _e13 + (_e10 + 5uL);
|
|
uint _e17 = input_uniform.val_u32_;
|
|
ulong _e18 = val_1;
|
|
ulong _e22 = val_1;
|
|
val_1 = _e22 + static_cast<ulong>(_e17 + static_cast<uint>(_e18));
|
|
int _e26 = input_uniform.val_i32_;
|
|
ulong _e27 = val_1;
|
|
ulong _e31 = val_1;
|
|
val_1 = _e31 + static_cast<ulong>(as_type<int>(as_type<uint>(_e26) + as_type<uint>(static_cast<int>(_e27))));
|
|
float _e35 = input_uniform.val_f32_;
|
|
ulong _e36 = val_1;
|
|
ulong _e40 = val_1;
|
|
val_1 = _e40 + static_cast<ulong>(_e35 + static_cast<float>(_e36));
|
|
ulong _e44 = input_uniform.val_u64_;
|
|
ulong _e47 = val_1;
|
|
val_1 = _e47 + metal::ulong3(_e44).z;
|
|
long _e51 = input_uniform.val_i64_;
|
|
ulong _e53 = val_1;
|
|
val_1 = _e53 + as_type<ulong>(_e51);
|
|
metal::long2 _e57 = input_uniform.val_i64_2_;
|
|
ulong _e60 = val_1;
|
|
val_1 = _e60 + as_type<metal::ulong2>(_e57).y;
|
|
metal::long3 _e64 = input_uniform.val_i64_3_;
|
|
ulong _e67 = val_1;
|
|
val_1 = _e67 + as_type<metal::ulong3>(_e64).z;
|
|
metal::long4 _e71 = input_uniform.val_i64_4_;
|
|
ulong _e74 = val_1;
|
|
val_1 = _e74 + as_type<metal::ulong4>(_e71).w;
|
|
ulong _e80 = input_uniform.val_u64_;
|
|
ulong _e83 = input_storage.val_u64_;
|
|
output.val_u64_ = _e80 + _e83;
|
|
metal::ulong2 _e89 = input_uniform.val_u64_2_;
|
|
metal::ulong2 _e92 = input_storage.val_u64_2_;
|
|
output.val_u64_2_ = _e89 + _e92;
|
|
metal::ulong3 _e98 = input_uniform.val_u64_3_;
|
|
metal::ulong3 _e101 = input_storage.val_u64_3_;
|
|
output.val_u64_3_ = _e98 + _e101;
|
|
metal::ulong4 _e107 = input_uniform.val_u64_4_;
|
|
metal::ulong4 _e110 = input_storage.val_u64_4_;
|
|
output.val_u64_4_ = _e107 + _e110;
|
|
type_11 _e116 = input_arrays.val_u64_array_2_;
|
|
output_arrays.val_u64_array_2_ = _e116;
|
|
ulong _e117 = val_1;
|
|
ulong _e119 = val_1;
|
|
val_1 = _e119 + metal::abs(_e117);
|
|
ulong _e121 = val_1;
|
|
ulong _e122 = val_1;
|
|
ulong _e123 = val_1;
|
|
ulong _e125 = val_1;
|
|
val_1 = _e125 + metal::clamp(_e121, _e122, _e123);
|
|
ulong _e127 = val_1;
|
|
metal::ulong2 _e128 = metal::ulong2(_e127);
|
|
ulong _e129 = val_1;
|
|
metal::ulong2 _e130 = metal::ulong2(_e129);
|
|
ulong _e132 = val_1;
|
|
val_1 = _e132 + ( + _e128.x * _e130.x + _e128.y * _e130.y);
|
|
ulong _e134 = val_1;
|
|
ulong _e135 = val_1;
|
|
ulong _e137 = val_1;
|
|
val_1 = _e137 + metal::max(_e134, _e135);
|
|
ulong _e139 = val_1;
|
|
ulong _e140 = val_1;
|
|
ulong _e142 = val_1;
|
|
val_1 = _e142 + metal::min(_e139, _e140);
|
|
ulong _e144 = val_1;
|
|
return _e144;
|
|
}
|
|
|
|
kernel void main_(
|
|
constant UniformCompatible& input_uniform [[user(fake0)]]
|
|
, device UniformCompatible const& input_storage [[user(fake0)]]
|
|
, device StorageCompatible const& input_arrays [[user(fake0)]]
|
|
, device UniformCompatible& output [[user(fake0)]]
|
|
, device StorageCompatible& output_arrays [[user(fake0)]]
|
|
) {
|
|
ulong _e3 = uint64_function(67uL, input_uniform, input_storage, input_arrays, output, output_arrays);
|
|
long _e5 = int64_function(60L, input_uniform, input_storage, input_arrays, output, output_arrays);
|
|
output.final_value = _e3 + as_type<ulong>(_e5);
|
|
return;
|
|
}
|