1#include "shared.rsh"
2
3// Testing math library
4
5volatile float f1;
6volatile float2 f2;
7volatile float3 f3;
8volatile float4 f4;
9
10volatile int i1;
11volatile int2 i2;
12volatile int3 i3;
13volatile int4 i4;
14
15volatile uint ui1;
16volatile uint2 ui2;
17volatile uint3 ui3;
18volatile uint4 ui4;
19
20volatile short s1;
21volatile short2 s2;
22volatile short3 s3;
23volatile short4 s4;
24
25volatile ushort us1;
26volatile ushort2 us2;
27volatile ushort3 us3;
28volatile ushort4 us4;
29
30volatile char c1;
31volatile char2 c2;
32volatile char3 c3;
33volatile char4 c4;
34
35volatile uchar uc1;
36volatile uchar2 uc2;
37volatile uchar3 uc3;
38volatile uchar4 uc4;
39
40#define DECL_INT(prefix)            \
41volatile char prefix##_c_1 = 1;     \
42volatile char2 prefix##_c_2 = 1;    \
43volatile char3 prefix##_c_3 = 1;    \
44volatile char4 prefix##_c_4 = 1;    \
45volatile uchar prefix##_uc_1 = 1;   \
46volatile uchar2 prefix##_uc_2 = 1;  \
47volatile uchar3 prefix##_uc_3 = 1;  \
48volatile uchar4 prefix##_uc_4 = 1;  \
49volatile short prefix##_s_1 = 1;    \
50volatile short2 prefix##_s_2 = 1;   \
51volatile short3 prefix##_s_3 = 1;   \
52volatile short4 prefix##_s_4 = 1;   \
53volatile ushort prefix##_us_1 = 1;  \
54volatile ushort2 prefix##_us_2 = 1; \
55volatile ushort3 prefix##_us_3 = 1; \
56volatile ushort4 prefix##_us_4 = 1; \
57volatile int prefix##_i_1 = 1;      \
58volatile int2 prefix##_i_2 = 1;     \
59volatile int3 prefix##_i_3 = 1;     \
60volatile int4 prefix##_i_4 = 1;     \
61volatile uint prefix##_ui_1 = 1;    \
62volatile uint2 prefix##_ui_2 = 1;   \
63volatile uint3 prefix##_ui_3 = 1;   \
64volatile uint4 prefix##_ui_4 = 1;   \
65volatile long prefix##_l_1 = 1;     \
66volatile ulong prefix##_ul_1 = 1;
67
68DECL_INT(res)
69DECL_INT(src1)
70DECL_INT(src2)
71
72#define TEST_INT_OP_TYPE(op, type)                      \
73rsDebug("Testing " #op " for " #type "3", i++);         \
74res_##type##_3 = src1_##type##_3 op src2_##type##_3;    \
75
76#define TEST_INT_OP(op)                     \
77TEST_INT_OP_TYPE(op, c)                     \
78TEST_INT_OP_TYPE(op, uc)                    \
79
80#define TEST_XN_FUNC_YN(typeout, fnc, typein)   \
81    res_##typeout##_1 = fnc(src1_##typein##_1); \
82    res_##typeout##_2 = fnc(src1_##typein##_2); \
83    res_##typeout##_3 = fnc(src1_##typein##_3); \
84    res_##typeout##_4 = fnc(src1_##typein##_4);
85
86#define TEST_XN_FUNC_XN_XN(type, fnc)                       \
87    res_##type##_1 = fnc(src1_##type##_1, src2_##type##_1); \
88    res_##type##_2 = fnc(src1_##type##_2, src2_##type##_2); \
89    res_##type##_3 = fnc(src1_##type##_3, src2_##type##_3); \
90    res_##type##_4 = fnc(src1_##type##_4, src2_##type##_4);
91
92#define TEST_X_FUNC_X_X_X(type, fnc)    \
93    res_##type##_1 = fnc(src1_##type##_1, src2_##type##_1, src2_##type##_1);
94
95#define TEST_IN_FUNC_IN(fnc)        \
96    rsDebug("Testing " #fnc, 0);    \
97    TEST_XN_FUNC_YN(uc, fnc, uc)    \
98    TEST_XN_FUNC_YN(c, fnc, c)      \
99    TEST_XN_FUNC_YN(us, fnc, us)    \
100    TEST_XN_FUNC_YN(s, fnc, s)      \
101    TEST_XN_FUNC_YN(ui, fnc, ui)    \
102    TEST_XN_FUNC_YN(i, fnc, i)
103
104#define TEST_UIN_FUNC_IN(fnc)       \
105    rsDebug("Testing " #fnc, 0);    \
106    TEST_XN_FUNC_YN(uc, fnc, c)     \
107    TEST_XN_FUNC_YN(us, fnc, s)     \
108    TEST_XN_FUNC_YN(ui, fnc, i)     \
109
110#define TEST_IN_FUNC_IN_IN(fnc)     \
111    rsDebug("Testing " #fnc, 0);    \
112    TEST_XN_FUNC_XN_XN(uc, fnc)     \
113    TEST_XN_FUNC_XN_XN(c, fnc)      \
114    TEST_XN_FUNC_XN_XN(us, fnc)     \
115    TEST_XN_FUNC_XN_XN(s, fnc)      \
116    TEST_XN_FUNC_XN_XN(ui, fnc)     \
117    TEST_XN_FUNC_XN_XN(i, fnc)
118
119#define TEST_I_FUNC_I_I_I(fnc)      \
120    rsDebug("Testing " #fnc, 0);    \
121    TEST_X_FUNC_X_X_X(uc, fnc)      \
122    TEST_X_FUNC_X_X_X(c, fnc)       \
123    TEST_X_FUNC_X_X_X(us, fnc)      \
124    TEST_X_FUNC_X_X_X(s, fnc)       \
125    TEST_X_FUNC_X_X_X(ui, fnc)      \
126    TEST_X_FUNC_X_X_X(i, fnc)
127
128#define TEST_FN_FUNC_FN(fnc)        \
129    rsDebug("Testing " #fnc, 0);    \
130    f1 = fnc(f1);                   \
131    f2 = fnc(f2);                   \
132    f3 = fnc(f3);                   \
133    f4 = fnc(f4);
134
135#define TEST_FN_FUNC_FN_PFN(fnc)    \
136    rsDebug("Testing " #fnc, 0);    \
137    f1 = fnc(f1, (float*) &f1);     \
138    f2 = fnc(f2, (float2*) &f2);    \
139    f3 = fnc(f3, (float3*) &f3);    \
140    f4 = fnc(f4, (float4*) &f4);
141
142#define TEST_FN_FUNC_FN_FN(fnc)     \
143    rsDebug("Testing " #fnc, 0);    \
144    f1 = fnc(f1, f1);               \
145    f2 = fnc(f2, f2);               \
146    f3 = fnc(f3, f3);               \
147    f4 = fnc(f4, f4);
148
149#define TEST_F34_FUNC_F34_F34(fnc)  \
150    rsDebug("Testing " #fnc, 0);    \
151    f3 = fnc(f3, f3);               \
152    f4 = fnc(f4, f4);
153
154#define TEST_FN_FUNC_FN_F(fnc)      \
155    rsDebug("Testing " #fnc, 0);    \
156    f1 = fnc(f1, f1);               \
157    f2 = fnc(f2, f1);               \
158    f3 = fnc(f3, f1);               \
159    f4 = fnc(f4, f1);
160
161#define TEST_F_FUNC_FN(fnc)         \
162    rsDebug("Testing " #fnc, 0);    \
163    f1 = fnc(f1);                   \
164    f1 = fnc(f2);                   \
165    f1 = fnc(f3);                   \
166    f1 = fnc(f4);
167
168#define TEST_F_FUNC_FN_FN(fnc)      \
169    rsDebug("Testing " #fnc, 0);    \
170    f1 = fnc(f1, f1);               \
171    f1 = fnc(f2, f2);               \
172    f1 = fnc(f3, f3);               \
173    f1 = fnc(f4, f4);
174
175#define TEST_FN_FUNC_FN_IN(fnc)     \
176    rsDebug("Testing " #fnc, 0);    \
177    f1 = fnc(f1, i1);               \
178    f2 = fnc(f2, i2);               \
179    f3 = fnc(f3, i3);               \
180    f4 = fnc(f4, i4);
181
182#define TEST_FN_FUNC_FN_I(fnc)      \
183    rsDebug("Testing " #fnc, 0);    \
184    f1 = fnc(f1, i1);               \
185    f2 = fnc(f2, i1);               \
186    f3 = fnc(f3, i1);               \
187    f4 = fnc(f4, i1);
188
189#define TEST_FN_FUNC_FN_FN_FN(fnc)  \
190    rsDebug("Testing " #fnc, 0);    \
191    f1 = fnc(f1, f1, f1);           \
192    f2 = fnc(f2, f2, f2);           \
193    f3 = fnc(f3, f3, f3);           \
194    f4 = fnc(f4, f4, f4);
195
196#define TEST_FN_FUNC_FN_FN_F(fnc)   \
197    rsDebug("Testing " #fnc, 0);    \
198    f1 = fnc(f1, f1, f1);           \
199    f2 = fnc(f2, f1, f1);           \
200    f3 = fnc(f3, f1, f1);           \
201    f4 = fnc(f4, f1, f1);
202
203#define TEST_FN_FUNC_FN_PIN(fnc)    \
204    rsDebug("Testing " #fnc, 0);    \
205    f1 = fnc(f1, (int*) &i1);       \
206    f2 = fnc(f2, (int2*) &i2);      \
207    f3 = fnc(f3, (int3*) &i3);      \
208    f4 = fnc(f4, (int4*) &i4);
209
210#define TEST_FN_FUNC_FN_FN_PIN(fnc) \
211    rsDebug("Testing " #fnc, 0);    \
212    f1 = fnc(f1, f1, (int*) &i1);   \
213    f2 = fnc(f2, f2, (int2*) &i2);  \
214    f3 = fnc(f3, f3, (int3*) &i3);  \
215    f4 = fnc(f4, f4, (int4*) &i4);
216
217#define TEST_IN_FUNC_FN(fnc)        \
218    rsDebug("Testing " #fnc, 0);    \
219    i1 = fnc(f1);                   \
220    i2 = fnc(f2);                   \
221    i3 = fnc(f3);                   \
222    i4 = fnc(f4);
223
224static bool test_fp_math(uint32_t index) {
225    bool failed = false;
226    start();
227
228    TEST_FN_FUNC_FN(acos);
229    TEST_FN_FUNC_FN(acosh);
230    TEST_FN_FUNC_FN(acospi);
231    TEST_FN_FUNC_FN(asin);
232    TEST_FN_FUNC_FN(asinh);
233    TEST_FN_FUNC_FN(asinpi);
234    TEST_FN_FUNC_FN(atan);
235    TEST_FN_FUNC_FN_FN(atan2);
236    TEST_FN_FUNC_FN(atanh);
237    TEST_FN_FUNC_FN(atanpi);
238    TEST_FN_FUNC_FN_FN(atan2pi);
239    TEST_FN_FUNC_FN(cbrt);
240    TEST_FN_FUNC_FN(ceil);
241    TEST_FN_FUNC_FN_FN_FN(clamp);
242    TEST_FN_FUNC_FN_FN_F(clamp);
243    TEST_FN_FUNC_FN_FN(copysign);
244    TEST_FN_FUNC_FN(cos);
245    TEST_FN_FUNC_FN(cosh);
246    TEST_FN_FUNC_FN(cospi);
247    TEST_F34_FUNC_F34_F34(cross);
248    TEST_FN_FUNC_FN(degrees);
249    TEST_F_FUNC_FN_FN(distance);
250    TEST_F_FUNC_FN_FN(dot);
251    TEST_FN_FUNC_FN(erfc);
252    TEST_FN_FUNC_FN(erf);
253    TEST_FN_FUNC_FN(exp);
254    TEST_FN_FUNC_FN(exp2);
255    TEST_FN_FUNC_FN(exp10);
256    TEST_FN_FUNC_FN(expm1);
257    TEST_FN_FUNC_FN(fabs);
258    TEST_FN_FUNC_FN_FN(fdim);
259    TEST_FN_FUNC_FN(floor);
260    TEST_FN_FUNC_FN_FN_FN(fma);
261    TEST_FN_FUNC_FN_FN(fmax);
262    TEST_FN_FUNC_FN_F(fmax);
263    TEST_FN_FUNC_FN_FN(fmin);
264    TEST_FN_FUNC_FN_F(fmin);
265    TEST_FN_FUNC_FN_FN(fmod);
266    TEST_FN_FUNC_FN_PFN(fract);
267    TEST_FN_FUNC_FN_PIN(frexp);
268    TEST_FN_FUNC_FN_FN(hypot);
269    TEST_IN_FUNC_FN(ilogb);
270    TEST_FN_FUNC_FN_IN(ldexp);
271    TEST_FN_FUNC_FN_I(ldexp);
272    TEST_F_FUNC_FN(length);
273    TEST_FN_FUNC_FN(lgamma);
274    TEST_FN_FUNC_FN_PIN(lgamma);
275    TEST_FN_FUNC_FN(log);
276    TEST_FN_FUNC_FN(log2);
277    TEST_FN_FUNC_FN(log10);
278    TEST_FN_FUNC_FN(log1p);
279    TEST_FN_FUNC_FN(logb);
280    TEST_FN_FUNC_FN_FN_FN(mad);
281    TEST_FN_FUNC_FN_FN(max);
282    TEST_FN_FUNC_FN_F(max);
283    TEST_FN_FUNC_FN_FN(min);
284    TEST_FN_FUNC_FN_F(min);
285    TEST_FN_FUNC_FN_FN_FN(mix);
286    TEST_FN_FUNC_FN_FN_F(mix);
287    TEST_FN_FUNC_FN_PFN(modf);
288    // nan
289    TEST_FN_FUNC_FN_FN(nextafter);
290    TEST_FN_FUNC_FN(normalize);
291    TEST_FN_FUNC_FN_FN(pow);
292    TEST_FN_FUNC_FN_IN(pown);
293    TEST_FN_FUNC_FN_FN(powr);
294    TEST_FN_FUNC_FN(radians);
295    TEST_FN_FUNC_FN_FN(remainder);
296    TEST_FN_FUNC_FN_FN_PIN(remquo);
297    TEST_FN_FUNC_FN(rint);
298    TEST_FN_FUNC_FN_IN(rootn);
299    TEST_FN_FUNC_FN(round);
300    TEST_FN_FUNC_FN(rsqrt);
301    TEST_FN_FUNC_FN(sign);
302    TEST_FN_FUNC_FN(sin);
303    TEST_FN_FUNC_FN_PFN(sincos);
304    TEST_FN_FUNC_FN(sinh);
305    TEST_FN_FUNC_FN(sinpi);
306    TEST_FN_FUNC_FN(sqrt);
307    TEST_FN_FUNC_FN_FN(step);
308    TEST_FN_FUNC_FN_F(step);
309    TEST_FN_FUNC_FN(tan);
310    TEST_FN_FUNC_FN(tanh);
311    TEST_FN_FUNC_FN(tanpi);
312    TEST_FN_FUNC_FN(tgamma);
313    TEST_FN_FUNC_FN(trunc);
314
315    float time = end(index);
316
317    if (failed) {
318        rsDebug("test_fp_math FAILED", time);
319    }
320    else {
321        rsDebug("test_fp_math PASSED", time);
322    }
323
324    return failed;
325}
326
327static bool test_int_math(uint32_t index) {
328    bool failed = false;
329    start();
330
331    TEST_UIN_FUNC_IN(abs);
332    TEST_IN_FUNC_IN(clz);
333    TEST_IN_FUNC_IN_IN(min);
334    TEST_IN_FUNC_IN_IN(max);
335    TEST_I_FUNC_I_I_I(rsClamp);
336
337    float time = end(index);
338
339    if (failed) {
340        rsDebug("test_int_math FAILED", time);
341    }
342    else {
343        rsDebug("test_int_math PASSED", time);
344    }
345
346    return failed;
347}
348
349static bool test_basic_operators() {
350    bool failed = false;
351    int i = 0;
352
353    TEST_INT_OP(+);
354    TEST_INT_OP(-);
355    TEST_INT_OP(*);
356    TEST_INT_OP(/);
357    TEST_INT_OP(%);
358    TEST_INT_OP(<<);
359    TEST_INT_OP(>>);
360
361    if (failed) {
362        rsDebug("test_basic_operators FAILED", 0);
363    }
364    else {
365        rsDebug("test_basic_operators PASSED", 0);
366    }
367
368    return failed;
369}
370
371#define TEST_CVT(to, from, type)                        \
372rsDebug("Testing convert from " #from " to " #to, 0);   \
373to##1 = from##1;                                        \
374to##2 = convert_##type##2(from##2);                     \
375to##3 = convert_##type##3(from##3);                     \
376to##4 = convert_##type##4(from##4);
377
378#define TEST_CVT_MATRIX(to, type)   \
379TEST_CVT(to, c, type);              \
380TEST_CVT(to, uc, type);             \
381TEST_CVT(to, s, type);              \
382TEST_CVT(to, us, type);             \
383TEST_CVT(to, i, type);              \
384TEST_CVT(to, ui, type);             \
385TEST_CVT(to, f, type);              \
386
387static bool test_convert() {
388    bool failed = false;
389
390    TEST_CVT_MATRIX(c, char);
391    TEST_CVT_MATRIX(uc, uchar);
392    TEST_CVT_MATRIX(s, short);
393    TEST_CVT_MATRIX(us, ushort);
394    TEST_CVT_MATRIX(i, int);
395    TEST_CVT_MATRIX(ui, uint);
396    TEST_CVT_MATRIX(f, float);
397
398    if (failed) {
399        rsDebug("test_convert FAILED", 0);
400    }
401    else {
402        rsDebug("test_convert PASSED", 0);
403    }
404
405    return failed;
406}
407
408void math_test(uint32_t index, int test_num) {
409    bool failed = false;
410    rsDebug("Here ", 1);
411    res_uc_3 = src1_uc_3 / src2_uc_3;
412    rsDebug("Here ", 2);
413    failed |= test_basic_operators();
414    rsDebug("Here ", 3);
415
416    if (failed) {
417        rsSendToClientBlocking(RS_MSG_TEST_FAILED);
418    }
419    else {
420        rsSendToClientBlocking(RS_MSG_TEST_PASSED);
421    }
422}
423
424