[Beignet] [patch v4 3/3] fix clz utest issue.

xionghu.luo at intel.com xionghu.luo at intel.com
Tue Jan 27 19:49:51 PST 2015


From: Luo Xionghu <xionghu.luo at intel.com>

should use clz function instead of __builtin_clz.
add zero input check.

v2: add signed type test. remove redundant case.
v3: remove printf.

Signed-off-by: Luo Xionghu <xionghu.luo at intel.com>
---
 kernels/compiler_clz.cl       |   8 ++-
 kernels/compiler_clz_int.cl   |   5 --
 kernels/compiler_clz_short.cl |   5 --
 utests/CMakeLists.txt         |   2 -
 utests/compiler_clz.cpp       | 129 +++++++++++++++++++++++++++++++++---------
 utests/compiler_clz_int.cpp   |  31 ----------
 utests/compiler_clz_short.cpp |  31 ----------
 7 files changed, 109 insertions(+), 102 deletions(-)
 delete mode 100644 kernels/compiler_clz_int.cl
 delete mode 100644 kernels/compiler_clz_short.cl
 delete mode 100644 utests/compiler_clz_int.cpp
 delete mode 100644 utests/compiler_clz_short.cpp

diff --git a/kernels/compiler_clz.cl b/kernels/compiler_clz.cl
index 7ab6261..4b06178 100644
--- a/kernels/compiler_clz.cl
+++ b/kernels/compiler_clz.cl
@@ -3,10 +3,14 @@
 {                                                \
   __global TYPE* A = &src[get_global_id(0)];    \
   __global TYPE* B = &dst[get_global_id(0)];    \
-  *B =  __builtin_clz(*A);   \
+  *B =  clz(*A);   \
 }
 
-COMPILER_CLZ(uint)
 COMPILER_CLZ(ulong)
+COMPILER_CLZ(uint)
 COMPILER_CLZ(ushort)
 COMPILER_CLZ(uchar)
+COMPILER_CLZ(long)
+COMPILER_CLZ(int)
+COMPILER_CLZ(short)
+COMPILER_CLZ(char)
diff --git a/kernels/compiler_clz_int.cl b/kernels/compiler_clz_int.cl
deleted file mode 100644
index 0f17f86..0000000
--- a/kernels/compiler_clz_int.cl
+++ /dev/null
@@ -1,5 +0,0 @@
-kernel void compiler_clz_int(global int *src, global int *dst) {
-  int i = get_global_id(0);
-  dst[i] = clz(src[i]);
-}
-
diff --git a/kernels/compiler_clz_short.cl b/kernels/compiler_clz_short.cl
deleted file mode 100644
index 1ecf7a9..0000000
--- a/kernels/compiler_clz_short.cl
+++ /dev/null
@@ -1,5 +0,0 @@
-kernel void compiler_clz_short(global short *src, global short *dst) {
-  int i = get_global_id(0);
-  dst[i] = clz(src[i]);
-}
-
diff --git a/utests/CMakeLists.txt b/utests/CMakeLists.txt
index f8fb9c6..eaba27d 100644
--- a/utests/CMakeLists.txt
+++ b/utests/CMakeLists.txt
@@ -49,8 +49,6 @@ set (utests_sources
   compiler_array4.cpp
   compiler_byte_scatter.cpp
   compiler_ceil.cpp
-  compiler_clz_short.cpp
-  compiler_clz_int.cpp
   compiler_popcount.cpp
   compiler_convert_uchar_sat.cpp
   compiler_copy_buffer.cpp
diff --git a/utests/compiler_clz.cpp b/utests/compiler_clz.cpp
index 901e19b..9116608 100644
--- a/utests/compiler_clz.cpp
+++ b/utests/compiler_clz.cpp
@@ -2,18 +2,54 @@
 
 namespace {
 
-template <typename U>
-U get_max()
-{
-  int shift_bit = sizeof(U)*8;
-  U u_max = 0;
-  for (int i = 0; i < shift_bit; i++)
-    u_max |= 1<<(shift_bit-i-1);
-  return u_max;
+template<typename T>
+T get_max();
+
+#define DEF_TEMPLATE_MAX(TYPE, NAME)                                \
+template <>                                                         \
+TYPE get_max<TYPE>()                                                \
+{                                                                   \
+  static TYPE max = CL_##NAME##_MAX;                                \
+  return max;                                                       \
+}                                                                   \
+                                                                    \
+template <>                                                         \
+u##TYPE get_max<u##TYPE>()                                          \
+{                                                                   \
+  static u##TYPE max = CL_U##NAME##_MAX;                            \
+  return max;                                                       \
+}
+
+DEF_TEMPLATE_MAX(int8_t, CHAR)
+DEF_TEMPLATE_MAX(int16_t, SHRT)
+DEF_TEMPLATE_MAX(int32_t, INT)
+DEF_TEMPLATE_MAX(int64_t, LONG)
+
+template<typename T>
+T get_min();
+
+#define DEF_TEMPLATE_MIN(TYPE, NAME)                                \
+template <>                                                         \
+TYPE get_min<TYPE>()                                                \
+{                                                                   \
+  static TYPE min = CL_##NAME##_MIN;                                \
+  return min;                                                       \
+}                                                                   \
+                                                                    \
+template <>                                                         \
+u##TYPE get_min<u##TYPE>()                                          \
+{                                                                   \
+  static u##TYPE min = 0;                                           \
+  return min;                                                       \
 }
 
+DEF_TEMPLATE_MIN(int8_t, CHAR)
+DEF_TEMPLATE_MIN(int16_t, SHRT)
+DEF_TEMPLATE_MIN(int32_t, INT)
+DEF_TEMPLATE_MIN(int64_t, LONG)
+
 template<typename U>
-void test(const char *kernel_name)
+void test(const char *kernel_name, int s_type)
 {
   const size_t n = 64;
 
@@ -25,28 +61,65 @@ void test(const char *kernel_name)
   OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
 
   U max = get_max<U>();
+  U min = get_min<U>();
 
   OCL_MAP_BUFFER(0);
   for (uint32_t i = 0; i < n; ++i) {
       ((U*)buf_data[0])[i] = max >> i;
+      if(i == sizeof(U)*8)
+        ((U*)buf_data[0])[i] = min;
   }
+
   OCL_UNMAP_BUFFER(0);
 
   globals[0] = n;
   locals[0] = 16;
   OCL_NDRANGE(1);
   OCL_MAP_BUFFER(1);
-  for (uint32_t i = 0; i < n; ++i) {
-    if(sizeof(U) == 1 && i < 8 )
-      OCL_ASSERT(((U*)buf_data[1])[i] == (i+24) );
-    else if(sizeof(U) == 2 && i < 16 )
-      OCL_ASSERT(((U*)buf_data[1])[i] == (i+16) );
-    else if(sizeof(U) == 4 && i < 32 )
-      OCL_ASSERT(((U*)buf_data[1])[i] == i );
-    else if(sizeof(U) == 8 && i < 32 )
-      OCL_ASSERT(((U*)buf_data[1])[i] == 0 );
-    else if(sizeof(U) == 8 && i > 31)
-      OCL_ASSERT(((U*)buf_data[1])[i] == (i-32) );
+  // for unsigned type.
+  if(s_type == 0)
+  {
+    for (uint32_t i = 0; i < n; ++i) {
+      if(sizeof(U) == 1 && i < 8 )
+        OCL_ASSERT(((U*)buf_data[1])[i] == i );
+      else if(sizeof(U) == 2 && i < 16 )
+        OCL_ASSERT(((U*)buf_data[1])[i] == i );
+      else if(sizeof(U) == 4 && i < 32 )
+        OCL_ASSERT(((U*)buf_data[1])[i] == i );
+      else if(sizeof(U) == 8 && i < 64 )
+        OCL_ASSERT(((U*)buf_data[1])[i] == i );
+    }
+  }
+  else  // signed type
+  {
+    for (uint32_t i = 0; i < n; ++i) {
+      if(sizeof(U) == 1)
+      {
+        if( i < 8 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == i+1 );
+        else if( i == 8 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == 0 );
+      }
+      else if(sizeof(U) == 2)
+      {
+        if( i < 16 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == i+1 );
+        else if( i == 16 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == 0 );
+      }
+      else if(sizeof(U) == 4)
+      {
+        if( i < 32 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == i+1 );
+        else if( i == 32 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == 0 );
+      }
+      else if(sizeof(U) == 8)
+      {
+        if( i < 63 )
+          OCL_ASSERT(((U*)buf_data[1])[i] == i+1 );
+      }
+    }
   }
   OCL_UNMAP_BUFFER(1);
 
@@ -54,14 +127,18 @@ void test(const char *kernel_name)
 
 }
 
-#define compiler_clz(type, kernel) \
+#define compiler_clz(type, kernel, s_type)\
 static void compiler_clz_ ##type(void)\
 {\
-  test<type>(# kernel);\
+  test<type>(# kernel, s_type);\
 }\
 MAKE_UTEST_FROM_FUNCTION(compiler_clz_ ## type);
 
-compiler_clz(uint64_t, compiler_clz_ulong)
-compiler_clz(uint32_t, compiler_clz_uint)
-compiler_clz(uint16_t, compiler_clz_ushort)
-compiler_clz(uint8_t, compiler_clz_uchar)
+compiler_clz(uint64_t, compiler_clz_ulong, 0)
+compiler_clz(uint32_t, compiler_clz_uint, 0)
+compiler_clz(uint16_t, compiler_clz_ushort, 0)
+compiler_clz(uint8_t, compiler_clz_uchar, 0)
+compiler_clz(int64_t, compiler_clz_long, 1)
+compiler_clz(int32_t, compiler_clz_int, 1)
+compiler_clz(int16_t, compiler_clz_short, 1)
+compiler_clz(int8_t, compiler_clz_char, 1)
diff --git a/utests/compiler_clz_int.cpp b/utests/compiler_clz_int.cpp
deleted file mode 100644
index c12cfc6..0000000
--- a/utests/compiler_clz_int.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "utest_helper.hpp"
-
-void compiler_clz_int(void)
-{
-  const int n = 32;
-
-  // Setup kernel and buffers
-  OCL_CREATE_KERNEL("compiler_clz_int");
-  OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(int), 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] = n;
-  locals[0] = 16;
-
-  OCL_MAP_BUFFER(0);
-  ((int*)buf_data[0])[0] = 0;
-  for (int32_t i = 1; i < (int32_t) n; ++i)
-    ((int*)buf_data[0])[i] = 0xffffffffu >> i;
-  OCL_UNMAP_BUFFER(0);
-
-  OCL_NDRANGE(1);
-
-  OCL_MAP_BUFFER(1);
-  OCL_ASSERT(((int*)buf_data[1])[0] == 32);
-  for (int i = 1; i < n; ++i)
-    OCL_ASSERT(((int*)buf_data[1])[i] == i);
-  OCL_UNMAP_BUFFER(1);
-}
-
-MAKE_UTEST_FROM_FUNCTION(compiler_clz_int);
diff --git a/utests/compiler_clz_short.cpp b/utests/compiler_clz_short.cpp
deleted file mode 100644
index eb3a370..0000000
--- a/utests/compiler_clz_short.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "utest_helper.hpp"
-
-void compiler_clz_short(void)
-{
-  const size_t n = 16;
-
-  // Setup kernel and buffers
-  OCL_CREATE_KERNEL("compiler_clz_short");
-  OCL_CREATE_BUFFER(buf[0], 0, n * sizeof(short), NULL);
-  OCL_CREATE_BUFFER(buf[1], 0, n * sizeof(short), NULL);
-  OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
-  OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
-  globals[0] = n;
-  locals[0] = 16;
-
-  OCL_MAP_BUFFER(0);
-  ((short*)buf_data[0])[0] = 0;
-  for (int32_t i = 1; i < (int32_t) n; ++i)
-    ((short*)buf_data[0])[i] = 0xffffu >> i;
-  OCL_UNMAP_BUFFER(0);
-
-  OCL_NDRANGE(1);
-
-  OCL_MAP_BUFFER(1);
-  OCL_ASSERT(((short*)buf_data[1])[0] == 16);
-  for (unsigned i = 1; i < (unsigned) n; ++i)
-    OCL_ASSERT(((short*)buf_data[1])[i] == (short)i);
-  OCL_UNMAP_BUFFER(1);
-}
-
-MAKE_UTEST_FROM_FUNCTION(compiler_clz_short);
-- 
1.9.1



More information about the Beignet mailing list