diff --git a/test_conformance/basic/CMakeLists.txt b/test_conformance/basic/CMakeLists.txt index fe070a6b..df778a68 100644 --- a/test_conformance/basic/CMakeLists.txt +++ b/test_conformance/basic/CMakeLists.txt @@ -2,7 +2,7 @@ set(MODULE_NAME BASIC) set(${MODULE_NAME}_SOURCES main.cpp - test_fpmath_float.cpp test_fpmath_float2.cpp test_fpmath_float4.cpp + test_fpmath_float.cpp test_intmath.cpp test_hiloeo.cpp test_local.cpp test_pointercast.cpp test_if.cpp test_loop.cpp diff --git a/test_conformance/basic/test_fpmath_float.cpp b/test_conformance/basic/test_fpmath_float.cpp index 60d509b0..fced0f4e 100644 --- a/test_conformance/basic/test_fpmath_float.cpp +++ b/test_conformance/basic/test_fpmath_float.cpp @@ -1,6 +1,6 @@ // // Copyright (c) 2017 The Khronos Group Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -22,245 +22,175 @@ #include #include "harness/rounding_mode.h" +#include +#include +#include +#include + #include "procs.h" -static const char *fpadd_kernel_code = -"__kernel void test_fpadd(__global float *srcA, __global float *srcB, __global float *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -static const char *fpsub_kernel_code = -"__kernel void test_fpsub(__global float *srcA, __global float *srcB, __global float *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -static const char *fpmul_kernel_code = -"__kernel void test_fpmul(__global float *srcA, __global float *srcB, __global float *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - - -static int -verify_fpadd(float *inptrA, float *inptrB, float *outptr, int n) +struct TestDef { - float r; - int i; + const char op; + std::function ref; +}; - for (i=0; i (&input)[2], std::vector &output, + const TestDef &test) +{ + + auto &inA = input[0]; + auto &inB = input[1]; + for (int i = 0; i < output.size(); i++) { - r = inptrA[i] + inptrB[i]; - if (r != outptr[i]) + float r = test.ref(inA[i], inB[i]); + if (r != output[i]) { - log_error("FP_ADD float test failed\n"); + log_error("FP '%c' float test failed\n", test.op); return -1; } } - log_info("FP_ADD float test passed\n"); + log_info("FP '%c' float test passed\n", test.op); return 0; } -static int -verify_fpsub(float *inptrA, float *inptrB, float *outptr, int n) -{ - float r; - int i; - for (i=0; i (&input)[2]) +{ + RandomSeed seed(gRandomSeed); + + auto random_generator = [&seed]() { + return get_random_float(-MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31), + MAKE_HEX_FLOAT(0x1.0p31f, 0x1, 31), seed); + }; + + for (auto &v : input) { - r = inptrA[i] - inptrB[i]; - if (r != outptr[i]) - { - log_error("FP_SUB float test failed\n"); - return -1; - } + std::generate(v.begin(), v.end(), random_generator); } - - log_info("FP_SUB float test passed\n"); - return 0; } -static int -verify_fpmul(float *inptrA, float *inptrB, float *outptr, int n) +template +int test_fpmath(cl_device_id device, cl_context context, cl_command_queue queue, + int num_elements, const std::string type_str, + const TestDef &test) { - float r; - int i; + clMemWrapper streams[3]; + clProgramWrapper program; + clKernelWrapper kernel; - for (i=0; i inputs[]{ std::vector(N * num_elements), + std::vector(N * num_elements) }; + std::vector output = std::vector(N * num_elements); - streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err); - test_error( err, "clCreateBuffer failed."); - streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err); - test_error( err, "clCreateBuffer failed."); - streams[2] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err); - test_error( err, "clCreateBuffer failed."); - streams[3] = clCreateBuffer(context, CL_MEM_READ_WRITE, length, NULL, &err); - test_error( err, "clCreateBuffer failed."); + generate_random_inputs(inputs); - p = input_ptr[0]; - for (i=0; i(num_elements) }; + err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, threads, NULL, 0, NULL, + NULL); + test_error(err, "clEnqueueNDRangeKernel failed."); + + err = clEnqueueReadBuffer(queue, streams[2], CL_TRUE, 0, length, + output.data(), 0, NULL, NULL); + test_error(err, "clEnqueueReadBuffer failed."); + + if (isRTZ) set_round(kRoundTowardZero, kfloat); + + err = verify_fp(inputs, output, test); + + if (isRTZ) set_round(oldMode, kfloat); return err; } +template +int test_fpmath_common(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements, + const std::string type_str) +{ + TestDef tests[] = { { '+', std::plus() }, + { '-', std::minus() }, + { '*', std::multiplies() } }; + int err = TEST_PASS; + + for (const auto &test : tests) + { + err |= test_fpmath(device, context, queue, num_elements, type_str, + test); + } + + return err; +} + +int test_fpmath_float(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_fpmath_common<1>(device, context, queue, num_elements, "float"); +} + +int test_fpmath_float2(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_fpmath_common<2>(device, context, queue, num_elements, + "float2"); +} + +int test_fpmath_float4(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_fpmath_common<4>(device, context, queue, num_elements, + "float4"); +} diff --git a/test_conformance/basic/test_fpmath_float2.cpp b/test_conformance/basic/test_fpmath_float2.cpp deleted file mode 100644 index 1881b4be..00000000 --- a/test_conformance/basic/test_fpmath_float2.cpp +++ /dev/null @@ -1,266 +0,0 @@ -// -// Copyright (c) 2017 The Khronos Group Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -#include "harness/compat.h" - -#include -#include -#include -#include -#include -#include "harness/rounding_mode.h" - - -#include "procs.h" - -const char *fpadd2_kernel_code = -"__kernel void test_fpadd2(__global float2 *srcA, __global float2 *srcB, __global float2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *fpsub2_kernel_code = -"__kernel void test_fpsub2(__global float2 *srcA, __global float2 *srcB, __global float2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *fpmul2_kernel_code = -"__kernel void test_fpmul2(__global float2 *srcA, __global float2 *srcB, __global float2 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - - -int -verify_fpadd2(float *inptrA, float *inptrB, float *outptr, int n) -{ - float r; - int i; - - for (i=0; i -#include -#include -#include -#include - - -#include "procs.h" -#include "harness/rounding_mode.h" - -const char *fpadd4_kernel_code = -"__kernel void test_fpadd4(__global float4 *srcA, __global float4 *srcB, __global float4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] + srcB[tid];\n" -"}\n"; - -const char *fpsub4_kernel_code = -"__kernel void test_fpsub4(__global float4 *srcA, __global float4 *srcB, __global float4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] - srcB[tid];\n" -"}\n"; - -const char *fpmul4_kernel_code = -"__kernel void test_fpmul4(__global float4 *srcA, __global float4 *srcB, __global float4 *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = srcA[tid] * srcB[tid];\n" -"}\n"; - - -int -verify_fpadd4(float *inptrA, float *inptrB, float *outptr, int n) -{ - float r; - int i; - - for (i=0; i