Skip to content

Commit

Permalink
Complemented test_decorate saturation test to support cl_khr_fp16 ext…
Browse files Browse the repository at this point in the history
…ension (issue #142)
  • Loading branch information
shajder committed Jun 19, 2023
1 parent dec1e62 commit e09c504
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
OpCapability Linkage
OpCapability Kernel
OpCapability Int8
OpCapability Float16
OpMemoryModel Physical32 OpenCL
OpEntryPoint Kernel %1 "decorate_saturated_conversion_char" %gl_GlobalInvocationID
OpName %res "res"
Expand All @@ -23,22 +24,22 @@
%void = OpTypeVoid
%uchar = OpTypeInt 8 0
%_ptr_CrossWorkgroup_uchar = OpTypePointer CrossWorkgroup %uchar
%float = OpTypeFloat 32
%_ptr_CrossWorkgroup_float = OpTypePointer CrossWorkgroup %float
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_float
%half = OpTypeFloat 16
%_ptr_CrossWorkgroup_half = OpTypePointer CrossWorkgroup %half
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_half
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%1 = OpFunction %void None %16
%res = OpFunctionParameter %_ptr_CrossWorkgroup_uchar
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%entry = OpLabel
%17 = OpLoad %v3uint %gl_GlobalInvocationID Aligned 0
%18 = OpCompositeExtract %uint %17 0
%19 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %lhs %18
%20 = OpLoad %float %19 Aligned 4
%21 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %rhs %18
%22 = OpLoad %float %21 Aligned 4
%23 = OpFMul %float %20 %22
%19 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %lhs %18
%20 = OpLoad %half %19 Aligned 2
%21 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %rhs %18
%22 = OpLoad %half %21 Aligned 2
%23 = OpFMul %half %20 %22
%7 = OpConvertFToS %uchar %23
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_uchar %res %18
OpStore %24 %7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
OpCapability Kernel
OpCapability Int64
OpCapability Int8
OpCapability Float16
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %1 "decorate_saturated_conversion_char" %gl_GlobalInvocationID
OpName %res "res"
Expand All @@ -24,25 +25,25 @@
%void = OpTypeVoid
%uchar = OpTypeInt 8 0
%_ptr_CrossWorkgroup_uchar = OpTypePointer CrossWorkgroup %uchar
%float = OpTypeFloat 32
%_ptr_CrossWorkgroup_float = OpTypePointer CrossWorkgroup %float
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_float
%half = OpTypeFloat 16
%_ptr_CrossWorkgroup_half = OpTypePointer CrossWorkgroup %half
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_half
%ulong_32 = OpConstant %ulong 32
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3ulong Input
%1 = OpFunction %void None %16
%res = OpFunctionParameter %_ptr_CrossWorkgroup_uchar
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%entry = OpLabel
%18 = OpLoad %v3ulong %gl_GlobalInvocationID Aligned 0
%19 = OpCompositeExtract %ulong %18 0
%20 = OpShiftLeftLogical %ulong %19 %ulong_32
%21 = OpShiftRightArithmetic %ulong %20 %ulong_32
%22 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %lhs %21
%23 = OpLoad %float %22 Aligned 4
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %rhs %21
%25 = OpLoad %float %24 Aligned 4
%26 = OpFMul %float %23 %25
%22 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %lhs %21
%23 = OpLoad %half %22 Aligned 2
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %rhs %21
%25 = OpLoad %half %24 Aligned 2
%26 = OpFMul %half %23 %25
%7 = OpConvertFToS %uchar %26
%27 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_uchar %res %21
OpStore %27 %7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
OpCapability Linkage
OpCapability Kernel
OpCapability Int8
OpCapability Float16
OpMemoryModel Physical32 OpenCL
OpEntryPoint Kernel %1 "decorate_saturated_conversion_uchar" %gl_GlobalInvocationID
OpName %res "res"
Expand All @@ -23,22 +24,22 @@
%void = OpTypeVoid
%uchar = OpTypeInt 8 0
%_ptr_CrossWorkgroup_uchar = OpTypePointer CrossWorkgroup %uchar
%float = OpTypeFloat 32
%_ptr_CrossWorkgroup_float = OpTypePointer CrossWorkgroup %float
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_float
%half = OpTypeFloat 16
%_ptr_CrossWorkgroup_half = OpTypePointer CrossWorkgroup %half
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_half
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%1 = OpFunction %void None %16
%res = OpFunctionParameter %_ptr_CrossWorkgroup_uchar
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%entry = OpLabel
%17 = OpLoad %v3uint %gl_GlobalInvocationID Aligned 0
%18 = OpCompositeExtract %uint %17 0
%19 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %lhs %18
%20 = OpLoad %float %19 Aligned 4
%21 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %rhs %18
%22 = OpLoad %float %21 Aligned 4
%23 = OpFMul %float %20 %22
%19 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %lhs %18
%20 = OpLoad %half %19 Aligned 2
%21 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %rhs %18
%22 = OpLoad %half %21 Aligned 2
%23 = OpFMul %half %20 %22
%7 = OpConvertFToU %uchar %23
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_uchar %res %18
OpStore %24 %7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
OpCapability Kernel
OpCapability Int64
OpCapability Int8
OpCapability Float16
OpMemoryModel Physical64 OpenCL
OpEntryPoint Kernel %1 "decorate_saturated_conversion_uchar" %gl_GlobalInvocationID
OpName %res "res"
Expand All @@ -24,25 +25,25 @@
%void = OpTypeVoid
%uchar = OpTypeInt 8 0
%_ptr_CrossWorkgroup_uchar = OpTypePointer CrossWorkgroup %uchar
%float = OpTypeFloat 32
%_ptr_CrossWorkgroup_float = OpTypePointer CrossWorkgroup %float
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_float %_ptr_CrossWorkgroup_float
%half = OpTypeFloat 16
%_ptr_CrossWorkgroup_half = OpTypePointer CrossWorkgroup %half
%16 = OpTypeFunction %void %_ptr_CrossWorkgroup_uchar %_ptr_CrossWorkgroup_half %_ptr_CrossWorkgroup_half
%ulong_32 = OpConstant %ulong 32
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3ulong Input
%1 = OpFunction %void None %16
%res = OpFunctionParameter %_ptr_CrossWorkgroup_uchar
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_float
%lhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%rhs = OpFunctionParameter %_ptr_CrossWorkgroup_half
%entry = OpLabel
%18 = OpLoad %v3ulong %gl_GlobalInvocationID Aligned 0
%19 = OpCompositeExtract %ulong %18 0
%20 = OpShiftLeftLogical %ulong %19 %ulong_32
%21 = OpShiftRightArithmetic %ulong %20 %ulong_32
%22 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %lhs %21
%23 = OpLoad %float %22 Aligned 4
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_float %rhs %21
%25 = OpLoad %float %24 Aligned 4
%26 = OpFMul %float %23 %25
%22 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %lhs %21
%23 = OpLoad %half %22 Aligned 2
%24 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_half %rhs %21
%25 = OpLoad %half %24 Aligned 2
%26 = OpFMul %half %23 %25
%7 = OpConvertFToU %uchar %26
%27 = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_uchar %res %21
OpStore %27 %7
Expand Down
75 changes: 60 additions & 15 deletions test_conformance/spirv_new/test_decorate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ or Khronos Conformance Test Source License Agreement as executed between Khronos
#define isnan std::isnan
#endif

#define HALF_NAN 0x7e00

long double reference_remainderl(long double x, long double y);
int gIsInRTZMode = 0;
int gDeviceILogb0 = 1;
int gDeviceILogbNaN = 1;
int gCheckTininessBeforeRounding = 1;

static int inline is_half_nan(cl_half half) { return (half & 0x7fff) > 0x7c00; }

static int verify_results(cl_device_id deviceID,
cl_context context,
Expand Down Expand Up @@ -212,14 +215,39 @@ int verify_saturated_results(cl_device_id deviceID,
Tl range = (Tl)(hiVal) - (Tl)(loVal);

RandomSeed seed(gRandomSeed);
for (int i = 0; i < num; i++) {
h_lhs[i] = genrand<Ti>(seed) * range;
Tl val = (genrand<Tl>(seed) % hiVal);
// randomly set some values on rhs to NaN
if (val * 20 < hiVal) {
h_rhs[i] = NAN;
} else {
h_rhs[i] = (Ti)(val);
if (std::is_same<Ti, cl_half>::value)
{
for (int i = 0; i < num; i++)
{
h_lhs[i] =
cl_half_from_float(genrand<float>(seed) * range, CL_HALF_RTE);
Tl val = (genrand<Tl>(seed) % hiVal);
// randomly set some values on rhs to NaN
if (val * 20 < hiVal)
{
h_rhs[i] = HALF_NAN;
}
else
{
h_rhs[i] = (Ti)(val);
}
}
}
else
{
for (int i = 0; i < num; i++)
{
h_lhs[i] = genrand<Ti>(seed) * range;
Tl val = (genrand<Tl>(seed) % hiVal);
// randomly set some values on rhs to NaN
if (val * 20 < hiVal)
{
h_rhs[i] = NAN;
}
else
{
h_rhs[i] = (Ti)(val);
}
}
}

Expand Down Expand Up @@ -256,11 +284,28 @@ int verify_saturated_results(cl_device_id deviceID,
SPIRV_CHECK_ERROR(err, "Failed to read to output");

for (int i = 0; i < num; i++) {
Tl ival = (Tl)(h_lhs[i] * h_rhs[i]);
To val = (To)std::min<Ti>(std::max<Ti>(ival, loVal), hiVal);

if (isnan(h_rhs[i])) {
val = 0;
To val;
if (std::is_same<Ti, cl_half>::value)
{
Tl ival =
(Tl)(cl_half_to_float(h_lhs[i]) * cl_half_to_float(h_rhs[i]));
val = (To)std::min<float>(std::max<float>(ival, loVal), hiVal);

if (is_half_nan(h_rhs[i]))
{
val = 0;
}
}
else
{
Tl ival = (Tl)(h_lhs[i] * h_rhs[i]);
val = (To)std::min<Ti>(std::max<Ti>(ival, loVal), hiVal);

if (isnan(h_rhs[i]))
{
val = 0;
}
}

if (val != h_res[i]) {
Expand Down Expand Up @@ -303,10 +348,10 @@ int test_saturate_full(cl_device_id deviceID,
(deviceID, context, queue, \
"decorate_saturated_conversion_" #To, \
#Ti #Tl #To); \
} \
}

TEST_SATURATED_CONVERSION(float, int, char)
TEST_SATURATED_CONVERSION(float, uint, uchar)
TEST_SATURATED_CONVERSION(half, short, char)
TEST_SATURATED_CONVERSION(half, ushort, uchar)
TEST_SATURATED_CONVERSION(float, int, short)
TEST_SATURATED_CONVERSION(float, uint, ushort)
TEST_SATURATED_CONVERSION(double, long, int)
Expand Down

0 comments on commit e09c504

Please sign in to comment.