summaryrefslogtreecommitdiff
path: root/utests/compiler_basic_arithmetic.cpp
blob: ba05de03176d2e8d5e78f5f36d8a3d42dc6ce290 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "utest_helper.hpp"

enum eTestOP {
  TEST_OP_ADD =0,
  TEST_OP_SUB,
  TEST_OP_MUL,
  TEST_OP_DIV,
  TEST_OP_REM
};

template <typename T, eTestOP op>
static void test_exec(const char* kernel_name)
{
  const size_t n = 160;

  // Setup kernel and buffers
  OCL_CREATE_KERNEL_FROM_FILE("compiler_basic_arithmetic", kernel_name);
  buf_data[0] = (T*) malloc(sizeof(T) * n);
  buf_data[1] = (T*) malloc(sizeof(T) * n);
  for (uint32_t i = 0; i < n; ++i) ((T*)buf_data[0])[i] = (T) rand();
  for (uint32_t i = 0; i < n; ++i) ((T*)buf_data[1])[i] = (T) rand();
  if(op == TEST_OP_DIV || op == TEST_OP_REM) {
    for (uint32_t i = 0; i < n; ++i) {
      if(((T*)buf_data[1])[i] == 0)
       ((T*)buf_data[1])[i] = (T) 1;
    }
  }
  OCL_CREATE_BUFFER(buf[0], CL_MEM_COPY_HOST_PTR, n * sizeof(T), buf_data[0]);
  OCL_CREATE_BUFFER(buf[1], CL_MEM_COPY_HOST_PTR, n * sizeof(T), buf_data[1]);
  OCL_CREATE_BUFFER(buf[2], 0, n * sizeof(T), NULL);

  // Run the kernel
  OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]);
  OCL_SET_ARG(1, sizeof(cl_mem), &buf[1]);
  OCL_SET_ARG(2, sizeof(cl_mem), &buf[2]);
  globals[0] = n;
  locals[0] = 16;
  OCL_NDRANGE(1);

  // Check result
  OCL_MAP_BUFFER(2);
  if(op == TEST_OP_SUB) {
    for (uint32_t i = 0; i < n; ++i)
      OCL_ASSERT(((T*)buf_data[2])[i] == (T)(((T*)buf_data[0])[i] - ((T*)buf_data[1])[i]));
  } else if(op == TEST_OP_ADD) {
    for (uint32_t i = 0; i < n; ++i)
      OCL_ASSERT(((T*)buf_data[2])[i] == (T)(((T*)buf_data[0])[i] + ((T*)buf_data[1])[i]));
  } else if(op == TEST_OP_MUL) {
    for (uint32_t i = 0; i < n; ++i)
      OCL_ASSERT(((T*)buf_data[2])[i] == (T)(((T*)buf_data[0])[i] * ((T*)buf_data[1])[i]));
  } else if(op == TEST_OP_DIV) {
    for (uint32_t i = 0; i < n; ++i)
      OCL_ASSERT(((T*)buf_data[2])[i] == (T)(((T*)buf_data[0])[i] / ((T*)buf_data[1])[i]));
  } else {
    for (uint32_t i = 0; i < n; ++i)
      OCL_ASSERT(((T*)buf_data[2])[i] == (T)(((T*)buf_data[0])[i] % ((T*)buf_data[1])[i]));
  }
  free(buf_data[0]);
  free(buf_data[1]);
  buf_data[0] = buf_data[1] = NULL;
}

#define DECL_TEST_SUB(type, alias, keep_program) \
static void compiler_sub_ ##alias(void)\
{\
  test_exec<type, TEST_OP_SUB>("compiler_sub_" # alias);\
}\
MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_sub_ ## alias, keep_program)

#define DECL_TEST_ADD(type, alias, keep_program) \
static void compiler_add_ ##alias(void)\
{\
  test_exec<type, TEST_OP_ADD>("compiler_add_" # alias);\
}\
MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_add_ ## alias, keep_program)

#define DECL_TEST_MUL(type, alias, keep_program) \
static void compiler_mul_ ##alias(void)\
{\
  test_exec<type, TEST_OP_MUL>("compiler_mul_" # alias);\
}\
MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_mul_ ## alias, keep_program)

#define DECL_TEST_DIV(type, alias, keep_program) \
static void compiler_div_ ##alias(void)\
{\
  test_exec<type, TEST_OP_DIV>("compiler_div_" # alias);\
}\
MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_div_ ## alias, keep_program)

#define DECL_TEST_REM(type, alias, keep_program) \
static void compiler_rem_ ##alias(void)\
{\
  test_exec<type, TEST_OP_REM>("compiler_rem_" # alias);\
}\
MAKE_UTEST_FROM_FUNCTION_KEEP_PROGRAM(compiler_rem_ ## alias, keep_program)

#define _DECL_TEST_FOR_ALL_TYPE(op, keep_program) \
DECL_TEST_##op(int8_t, char, true) \
DECL_TEST_##op(uint8_t, uchar, true) \
DECL_TEST_##op(int16_t, short, true) \
DECL_TEST_##op(uint16_t, ushort, true) \
DECL_TEST_##op(int32_t, int, true) \
DECL_TEST_##op(uint32_t, uint, keep_program)

#define DECL_TEST_FOR_ALL_TYPE(op) _DECL_TEST_FOR_ALL_TYPE(op, true)

#define DECL_TEST_FOR_ALL_TYPE_END(op) _DECL_TEST_FOR_ALL_TYPE(op, false)

DECL_TEST_FOR_ALL_TYPE(SUB)
DECL_TEST_FOR_ALL_TYPE(ADD)
DECL_TEST_FOR_ALL_TYPE(MUL)
DECL_TEST_FOR_ALL_TYPE(DIV)
DECL_TEST_FOR_ALL_TYPE_END(REM)
#undef DECL_TEST_FOR_ALL_TYPE