From 3e8898ffeb1478c96c440a158db2e7d662b26a30 Mon Sep 17 00:00:00 2001 From: John Kesapides <46718829+JohnKesapidesARM@users.noreply.github.com> Date: Wed, 24 May 2023 16:55:25 +0100 Subject: [PATCH] Deduplicate test_basic int2float/float2int (#1537) Merge int2float,float2int. Signed-off-by: John Kesapides --- test_conformance/basic/CMakeLists.txt | 2 +- test_conformance/basic/test_int2float.cpp | 191 +++++++++++----------- 2 files changed, 95 insertions(+), 98 deletions(-) diff --git a/test_conformance/basic/CMakeLists.txt b/test_conformance/basic/CMakeLists.txt index dde3311d..adf24bd8 100644 --- a/test_conformance/basic/CMakeLists.txt +++ b/test_conformance/basic/CMakeLists.txt @@ -11,7 +11,7 @@ set(${MODULE_NAME}_SOURCES test_multireadimageonefmt.cpp test_multireadimagemultifmt.cpp test_imagedim.cpp test_vloadstore.cpp - test_int2float.cpp test_float2int.cpp + test_int2float.cpp test_createkernelsinprogram.cpp test_hostptr.cpp test_explicit_s2v.cpp diff --git a/test_conformance/basic/test_int2float.cpp b/test_conformance/basic/test_int2float.cpp index 3a8458c9..c5afc244 100644 --- a/test_conformance/basic/test_int2float.cpp +++ b/test_conformance/basic/test_int2float.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 @@ -21,123 +21,120 @@ #include #include +#include +#include #include "procs.h" -const char *int2float_kernel_code = -"__kernel void test_int2float(__global int *src, __global float *dst)\n" -"{\n" -" int tid = get_global_id(0);\n" -"\n" -" dst[tid] = (float)src[tid];\n" -"\n" -"}\n"; - - -int -verify_int2float(cl_int *inptr, cl_float *outptr, int n) +namespace { +const char *int2float_kernel_code = R"( +__kernel void test_X2Y(__global TYPE_X *src, __global TYPE_Y *dst) { - int i; + int tid = get_global_id(0); - for (i=0; i const char *Type2str() { return ""; } +template <> const char *Type2str() { return "int"; } +template <> const char *Type2str() { return "float"; } + +template void generate_random_inputs(std::vector &v) +{ + 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); + }; + + std::generate(v.begin(), v.end(), random_generator); } -int -test_int2float(cl_device_id device, cl_context context, cl_command_queue queue, int num_elements) +template bool equal_value(Tx a, Ty b) { - cl_mem streams[2]; - cl_int *input_ptr; - cl_float *output_ptr; - cl_program program; - cl_kernel kernel; - size_t threads[1]; - int err; - int i; - MTdata d; + return a == (Tx)b; +} + +template +int verify_X2Y(std::vector input, std::vector output, + const char *test_name) +{ + + if (!std::equal(output.begin(), output.end(), input.begin(), + equal_value)) + { + log_error("%s test failed\n", test_name); + return -1; + } + + log_info("%s test passed\n", test_name); + return 0; +} +template +int test_X2Y(cl_device_id device, cl_context context, cl_command_queue queue, + int num_elements, const char *test_name) +{ + clMemWrapper streams[2]; + clProgramWrapper program; + clKernelWrapper kernel; + int err; + + + std::vector input(num_elements); + std::vector output(num_elements); - input_ptr = (cl_int*)malloc(sizeof(cl_int) * num_elements); - output_ptr = (cl_float*)malloc(sizeof(cl_float) * num_elements); streams[0] = clCreateBuffer(context, CL_MEM_READ_WRITE, - sizeof(cl_int) * num_elements, NULL, NULL); - if (!streams[0]) - { - log_error("clCreateBuffer failed\n"); - return -1; - } + sizeof(Tx) * num_elements, nullptr, &err); + test_error(err, "clCreateBuffer failed."); streams[1] = clCreateBuffer(context, CL_MEM_READ_WRITE, - sizeof(cl_float) * num_elements, NULL, NULL); - if (!streams[1]) - { - log_error("clCreateBuffer failed\n"); - return -1; - } + sizeof(Ty) * num_elements, nullptr, &err); + test_error(err, "clCreateBuffer failed."); - d = init_genrand( gRandomSeed ); - for (i=0; i(device, context, queue, num_elements, + "INT2FLOAT"); +} +int test_float2int(cl_device_id device, cl_context context, + cl_command_queue queue, int num_elements) +{ + return test_X2Y(device, context, queue, num_elements, + "FLOAT2INT"); +}