-
Notifications
You must be signed in to change notification settings - Fork 109
/
check_p_sincos.c
205 lines (159 loc) · 4.83 KB
/
check_p_sincos.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* p_sincos has a different signature from all other math functions in the
PAL. It has 1 input vector and 2 output vectors. As a result, it needs
a distinct test infrastructure. */
#include <utest.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include <pal.h>
/* TODO: This relative path include is fragile */
#include "../../src/base/pal_base_private.h"
#include <common.h>
#include "simple.h"
#define GOLD_PATH XSTRING(gold/FUNCTION.gold.h)
#include GOLD_PATH
PTYPE *ai, *resCos, *resSin, *refCos, *refSin;
struct gold *gold = builtin_gold;
size_t gold_size = ARRAY_SIZE(builtin_gold);
/* For detecting erroneous overwrites */
#define OUTPUT_END_MARKER ((PTYPE)60189537703610376.0)
bool compare(PTYPE x, PTYPE y)
{
PTYPE err;
if (fabs(x - y) <= EPSILON_MAX)
return true;
if (fabs(x) > fabs(y))
err = fabs((x - y) / x);
else
err = fabs((x - y) / y);
return err <= EPSILON_RELMAX;
}
#ifdef __epiphany__
void *simple_calloc(size_t nmemb, size_t size)
{
void *p;
uint64_t *q;
size_t i;
/* Find program break */
p = sbrk(0);
if (!p)
return NULL;
/* Align by double-word */
if (!sbrk((8 - ((uintptr_t) p & 7)) & 7))
return NULL;
/* Calculate total size (assume no overflow) */
size *= nmemb;
/* Allocate in even double-words */
size = (size + 7) & ~7;
p = sbrk(size);
if (!p)
return NULL;
/* Set to zero */
for (i = 0, q = p; i < size; i += 8, q++)
*q = 0;
return p;
}
#define simple_free(p)
#else
#define simple_calloc calloc
#define simple_free free
#endif
int setup(struct ut_suite *suite)
{
size_t i;
(void) suite;
ai = simple_calloc(gold_size, sizeof(PTYPE));
refCos = simple_calloc(gold_size, sizeof(PTYPE));
refSin = simple_calloc(gold_size, sizeof(PTYPE));
/* Allocate one extra element for res and add end marker so overwrites can
* be detected */
resCos = simple_calloc(gold_size + 1, sizeof(PTYPE));
resCos[gold_size] = OUTPUT_END_MARKER;
resSin = simple_calloc(gold_size + 1, sizeof(PTYPE));
resSin[gold_size] = OUTPUT_END_MARKER;
for (i = 0; i < gold_size; i++) {
ai[i] = gold[i].ai;
}
return 0;
}
int teardown(struct ut_suite *suite)
{
(void) suite;
simple_free(ai);
simple_free(resCos);
simple_free(resSin);
simple_free(refCos);
simple_free(refSin);
return 0;
}
int tc_against_gold_e(struct ut_suite *suite, struct ut_tcase *tcase)
{
/* Run FUNCTION against gold input here so results are available
* for all test cases. */
FUNCTION(ai, resSin, resCos, gold_size);
return 0;
}
int tc_against_gold_v(struct ut_suite *suite, struct ut_tcase *tcase)
{
size_t i;
for (i = 0; i < gold_size; i++) {
ut_assert_msg(compare(resSin[i], gold[i].bi),
"p_sincos(%f): sin: %f != %f",
ai[i], resSin[i], gold[i].bi);
ut_assert_msg(compare(resCos[i], gold[i].gold),
"p_sincos(%f): cos: %f != %f",
ai[i], resCos[i], gold[i].gold);
}
ut_assert_msg(resSin[i] == OUTPUT_END_MARKER,
"Output end marker was overwritten");
ut_assert_msg(resCos[i] == OUTPUT_END_MARKER,
"Output end marker was overwritten");
return 0;
}
int tc_against_ref_v(struct ut_suite *suite, struct ut_tcase *tcase)
{
size_t i;
for (i = 0; i < gold_size; i++) {
ut_assert_msg(compare(resSin[i], sinf(gold[i].ai)),
"p_sincos_f32(%f): sin: %f != %f",
ai[i], resSin[i], sinf(gold[i].ai));
ut_assert_msg(compare(resCos[i], cosf(gold[i].ai)),
"p_sincos_f32(%f): cos: %f != %f",
ai[i], resCos[i], cosf(gold[i].ai));
}
return 0;
}
DECLARE_UT_TCASE(tc_against_gold, tc_against_gold_e, tc_against_gold_v, NULL);
DECLARE_UT_TCASE(tc_against_ref, NULL, tc_against_ref_v, NULL);
DECLARE_UT_TCASE_LIST(tcases, &tc_against_gold, &tc_against_ref);
#define FUNCTION_SUITE XCONCAT2(FUNCTION,_suite)
DECLARE_UT_SUITE(FUNCTION_SUITE, setup, teardown, false, tcases, NULL);
#ifdef __epiphany__
struct status {
uint32_t done;
uint32_t _pad1;
uint32_t returncode;
uint32_t _pad2;
} __attribute__((packed));
volatile struct status *epiphany_status = (struct status *) 0x8f200000;
volatile char *epiphany_results = (char *) 0x8f300000;
#endif
int main(int argc, char *argv[])
{
int ret;
char buf[1024] = { 0 };
struct ut_suite *suite;
suite = &FUNCTION_SUITE;
ret = ut_run(suite);
ut_report(buf, ARRAY_SIZE(buf), suite, true);
#ifdef __epiphany__
memcpy((void *) epiphany_results, buf, sizeof(buf));
epiphany_status->returncode = ret;
epiphany_status->done = 1;
#else
printf("%s", buf);
#endif
return ret;
}