diff options
author | Luo Xionghu <xionghu.luo@intel.com> | 2015-01-28 11:49:51 +0800 |
---|---|---|
committer | Zhigang Gong <zhigang.gong@intel.com> | 2015-01-28 12:44:58 +0800 |
commit | 600d23b04e8be609fa5dcdc6ffcc0e383799cd40 (patch) | |
tree | 521561c264e02b4aef492f4cf0946f7b2f7b87c4 /utests/compiler_clz.cpp | |
parent | d0ade586e3cdbf2c8f1923872c93b3b6b07b897b (diff) | |
download | beignet-600d23b04e8be609fa5dcdc6ffcc0e383799cd40.tar.gz |
fix clz utest issue.
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@intel.com>
Reviewed-by: Zhigang Gong <zhigang.gong@linux.intel.com>
Diffstat (limited to 'utests/compiler_clz.cpp')
-rw-r--r-- | utests/compiler_clz.cpp | 129 |
1 files changed, 103 insertions, 26 deletions
diff --git a/utests/compiler_clz.cpp b/utests/compiler_clz.cpp index 901e19b5..9116608c 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) |