8000 Merge pull request #20 from howjmay/refactor-tests · plctlab/numpy@b95b690 · GitHub
[go: up one dir, main page]

Skip to content

Commit b95b690

Browse files
authored
Merge pull request numpy#20 from howjmay/refactor-tests
test: Refactor the test framework
2 parents 9cd5b99 + 0904223 commit b95b690

File tree

1 file changed

+49
-62
lines changed

1 file changed

+49
-62
lines changed

tests/impl.cpp

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,30 @@
3333
//
3434
// Functions with "test_" prefix will be called in run_single_test.
3535
namespace NEON2RVV {
36-
// Forward declaration
36+
static float ranf(float low, float high) {
37+
float rand_float = (float)rand() / (float)RAND_MAX;
38+
return rand_float * (high - low) + low;
39+
}
40+
41+
#if defined(__riscv_v_elen)
42+
#define REGISTER_SIZE __riscv_v_elen
43+
#elif defined(__aarch64__)
44+
#define REGISTER_SIZE 128
45+
#endif
46+
3747
class NEON2RVV_TEST_IMPL : public NEON2RVV_TEST {
3848
public:
39-
NEON2RVV_TEST_IMPL(void);
40-
result_t load_test_float_pointers(uint32_t i);
41-
result_t load_test_int_pointers(uint32_t i);
42-
result_t run_single_test(INSTRUCTION_TEST test, uint32_t i);
43-
49+
NEON2RVV_TEST_IMPL(void) {
50+
test_cases_float_pointer1 = (float *)platform_aligned_alloc(REGISTER_SIZE);
51+
test_cases_float_pointer2 = (float *)platform_aligned_alloc(REGISTER_SIZE);
52+
test_cases_int_pointer1 = (int32_t *)platform_aligned_alloc(REGISTER_SIZE);
53+
test_cases_int_pointer2 = (int32_t *)platform_aligned_alloc(REGISTER_SIZE);
54+
srand(0);
55+
for (uint32_t i = 0; i < MAX_TEST_VALUE; i++) {
56+
test_cases_floats[i] = ranf(-100000, 100000);
57+
test_cases_ints[i] = (int32_t)ranf(-100000, 100000);
58+
}
59+
}
4460
float *test_cases_float_pointer1;
4561
float *test_cases_float_pointer2;
4662
int32_t *test_cases_int_pointer1;
@@ -54,18 +70,30 @@ class NEON2RVV_TEST_IMPL : public NEON2RVV_TEST {
5470
platform_aligned_free(test_cases_int_pointer1);
5571
platform_aligned_free(test_cases_int_pointer2);
5672
}
73+
74+
void load_test_float_pointers(uint32_t iter) {
75+
for (int i = 0; i < 4; i++) {
76+
test_cases_float_pointer1[i] = test_cases_floats[iter + i];
77+
test_cases_float_pointer2[i + 4] = test_cases_floats[iter + i + 4];
78+
}
79+
}
80+
void load_test_int_pointers(uint32_t iter) {
81+
for (int i = 0; i < 4; i++) {
82+
test_cases_int_pointer1[i] = test_cases_ints[iter + i];
83+
test_cases_int_pointer2[i + 4] = test_cases_ints[iter + i + 4];
84+
}
85+
}
86+
result_t run_single_test(INSTRUCTION_TEST test, uint32_t iter);
87+
5788
virtual void release(void) { delete this; }
5889
virtual result_t run_test(INSTRUCTION_TEST test) {
5990
result_t ret = TEST_SUCCESS;
6091

6192
// Test a whole bunch of values
6293
for (uint32_t i = 0; i < (MAX_TEST_VALUE - 8); i++) {
63-
ret = load_test_float_pointers(i); // Load some random float values
64-
if (ret == TEST_FAIL)
65-
break; // load test float failed??
66-
ret = load_test_int_pointers(i); // load some random int values
67-
if (ret == TEST_FAIL)
68-
break; // load test float failed??
94+
load_test_float_pointers(i); // Load some random float values
95+
load_test_int_pointers(i); // load some random int values
96+
6997
// If we are testing the reciprocal, then invert the input data
7098
// (easier for debugging)
7199
if (test == it_vrecps_f32 || test == it_vrecpsq_f32 || test == it_vrecpe_f32 || test == it_vrecpe_u32 ||
@@ -224,6 +252,11 @@ class NEON2RVV_TEST_IMPL : public NEON2RVV_TEST {
224252
}
225253
};
226254

255+
NEON2RVV_TEST *NEON2RVV_TEST::create(void) {
256+
NEON2RVV_TEST_IMPL *st = new NEON2RVV_TEST_IMPL;
257+
return static_cast<NEON2RVV_TEST *>(st);
258+
}
259+
227260
const char *instruction_string[] = {
228261
#define _(x) #x,
229262
INTRIN_LIST
@@ -295,13 +328,6 @@ static inline double bankers_rounding(double val) {
295328
return ret;
296329
}
297330

298-
static float ranf(void) {
299-
uint32_t ir = rand() & 0x7FFF;
300-
return (float)ir * (1.0f / 32768.0f);
301-
}
302-
303-
static float ranf(float low, float high) { return ranf() * (high - low) + low; }
304-
305331
result_t test_vadd_s8(const NEON2RVV_TEST_IMPL &impl, uint32_t iter) {
306332
const int8_t *_a = (int8_t *)impl.test_cases_int_pointer1;
307333
const int8_t *_b = (int8_t *)impl.test_cases_int_pointer2;
@@ -4317,47 +4343,13 @@ result_t test_vsudotq_laneq_s32(const NEON2RVV_TEST_IMPL &impl, uint32_t iter) {
43174343
// Dummy function to match the case label in run_single_test.
43184344
result_t test_last(const NEON2RVV_TEST_IMPL &impl, uint32_t iter) { return TEST_SUCCESS; }
43194345

4320-
#if defined(__riscv_v_elen)
4321-
#define REGISTER_SIZE __riscv_v_elen
4322-
#elif defined(__aarch64__)
4323-
#define REGISTER_SIZE 128
4324-
#endif
4325-
4326-
NEON2RVV_TEST_IMPL::NEON2RVV_TEST_IMPL(void) {
4327-
test_cases_float_pointer1 = (float *)platform_aligned_alloc(REGISTER_SIZE);
4328-
test_cases_float_pointer2 = (float *)platform_aligned_alloc(REGISTER_SIZE);
4329-
test_cases_int_pointer1 = (int32_t *)platform_aligned_alloc(REGISTER_SIZE);
4330-
test_cases_int_pointer2 = (int32_t *)platform_aligned_alloc(REGISTER_SIZE);
4331-
srand(0);
4332-
for (uint32_t i = 0; i < MAX_TEST_VALUE; i++) {
4333-
test_cases_floats[i] = ranf(-100000, 100000);
4334-
test_cases_ints[i] = (int32_t)ranf(-100000, 100000);
4335-
}
4336-
}
4337-
4338-
result_t NEON2RVV_TEST_IMPL::load_test_float_pointers(uint32_t i) {
4339-
for (int i = 0; i < 4; i++) {
4340-
test_cases_float_pointer1[i] = test_cases_floats[i];
4341-
test_cases_float_pointer2[i + 4] = test_cases_floats[i + 4];
4342-
}
4343-
return TEST_SUCCESS;
4344-
}
4345-
4346-
result_t NEON2RVV_TEST_IMPL::load_test_int_pointers(uint32_t i) {
4347-
for (int i = 0; i < 4; i++) {
4348-
test_cases_int_pointer1[i] = test_cases_ints[i];
4349-
test_cases_int_pointer2[i + 4] = test_cases_ints[i + 4];
4350-
}
4351-
return TEST_SUCCESS;
4352-
}
4353-
4354-
result_t NEON2RVV_TEST_IMPL::run_single_test(INSTRUCTION_TEST test, uint32_t i) {
4346+
result_t NEON2RVV_TEST_IMPL::run_single_test(INSTRUCTION_TEST test, uint32_t iter) {
43554347
result_t ret = TEST_SUCCESS;
43564348

43574349
switch (test) {
4358-
#define _(x) \
4359-
case it_##x: \
4360-
ret = test_##x(*this, i); \
4350+
#define _(x) \
4351+
case it_##x: \
4352+
ret = test_##x(*this, iter); \
43614353
break;
43624354
INTRIN_LIST
43634355
#undef _
@@ -4366,9 +4358,4 @@ result_t NEON2RVV_TEST_IMPL::run_single_test(INSTRUCTION_TEST test, uint32_t i)
43664358
return ret;
43674359
}
43684360

4369-
NEON2RVV_TEST *NEON2RVV_TEST::create(void) {
4370-
NEON2RVV_TEST_IMPL *st = new NEON2RVV_TEST_IMPL;
4371-
return static_cast<NEON2RVV_TEST *>(st);
4372-
}
4373-
43744361
} // namespace NEON2RVV

0 commit comments

Comments
 (0)
0