From 600d23b04e8be609fa5dcdc6ffcc0e383799cd40 Mon Sep 17 00:00:00 2001 From: Luo Xionghu Date: Wed, 28 Jan 2015 11:49:51 +0800 Subject: 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 Reviewed-by: Zhigang Gong --- utests/compiler_clz.cpp | 129 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 103 insertions(+), 26 deletions(-) (limited to 'utests/compiler_clz.cpp') 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 -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 +T get_max(); + +#define DEF_TEMPLATE_MAX(TYPE, NAME) \ +template <> \ +TYPE get_max() \ +{ \ + static TYPE max = CL_##NAME##_MAX; \ + return max; \ +} \ + \ +template <> \ +u##TYPE get_max() \ +{ \ + 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 +T get_min(); + +#define DEF_TEMPLATE_MIN(TYPE, NAME) \ +template <> \ +TYPE get_min() \ +{ \ + static TYPE min = CL_##NAME##_MIN; \ + return min; \ +} \ + \ +template <> \ +u##TYPE get_min() \ +{ \ + 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 -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 min = get_min(); 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(# kernel);\ + test(# 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) -- cgit v1.2.1