summaryrefslogtreecommitdiff
path: root/utests/compiler_convert_uchar_sat.cpp
diff options
context:
space:
mode:
authorHomer Hsing <homer.xing@intel.com>2013-01-30 14:05:31 +0800
committerZhigang Gong <zhigang.gong@linux.intel.com>2013-04-10 14:52:32 +0800
commitb3e9efd931ce0aa76cdf1480d1b685a5bcae695f (patch)
tree7ed56109e2ff87ba204656bb055da1d0a74f4e45 /utests/compiler_convert_uchar_sat.cpp
parente4325621280ce9d3150e28602aba0fa563857e0e (diff)
downloadbeignet-b3e9efd931ce0aa76cdf1480d1b685a5bcae695f.tar.gz
Add convert_uchar_sat and test case
'convert_uchar_sat' converts float to uchar saturately. 'convert_uchar_sat' simply calls add_sat. by 'convert_uchar_sat' function we don't need to clamp(value, 0, 255). we also add a test case. Signed-off-by: Homer Hsing <homer.xing@intel.com> Reviewed-by: Lu Guanqun <guanqun.lu@intel.com>
Diffstat (limited to 'utests/compiler_convert_uchar_sat.cpp')
-rw-r--r--utests/compiler_convert_uchar_sat.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/utests/compiler_convert_uchar_sat.cpp b/utests/compiler_convert_uchar_sat.cpp
new file mode 100644
index 00000000..da000417
--- /dev/null
+++ b/utests/compiler_convert_uchar_sat.cpp
@@ -0,0 +1,44 @@
+#include "utest_helper.hpp"
+
+static void cpu(int global_id, float *src, int *dst) {
+ float f = src[global_id];
+ dst[global_id] = f > 255 ? 255 : f < 0 ? 0 : f;
+}
+
+void compiler_convert_uchar_sat(void)
+{
+ const size_t n = 16;
+ float cpu_src[16];
+ int cpu_dst[16];
+
+ // Setup kernel and buffers
+ OCL_CREATE_KERNEL("compiler_convert_uchar_sat");
+ OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(float), NULL);
+ OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(int), NULL);
+ OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
+ OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
+ globals[0] = 16;
+ locals[0] = 16;
+
+ // Run random tests
+ for (uint32_t pass = 0; pass < 8; ++pass) {
+ OCL_MAP_BUFFER(0);
+ for (int32_t i = 0; i < (int32_t) n; ++i)
+ cpu_src[i] = ((float*)buf_data[0])[i] = (rand() & 1023) / 2;
+ OCL_UNMAP_BUFFER(0);
+
+ // Run the kernel on GPU
+ OCL_NDRANGE(1);
+
+ // Run on CPU
+ for (int32_t i = 0; i < (int32_t) n; ++i) cpu(i, cpu_src, cpu_dst);
+
+ // Compare
+ OCL_MAP_BUFFER(1);
+ for (int32_t i = 0; i < (int32_t) n; ++i)
+ OCL_ASSERT(((int *)buf_data[1])[i] == cpu_dst[i]);
+ OCL_UNMAP_BUFFER(1);
+ }
+}
+
+MAKE_UTEST_FROM_FUNCTION(compiler_convert_uchar_sat);