* support/regression/tests/float_single.c: added regression test. Some testpoints...
[fw/sdcc] / support / regression / tests / float_single.c
1 /** Test of float functions with a single float argument
2
3    func: SINF, SINHF, ASINF, COSF, COSHF, ACOSF, TANF, TANHF, ATANF, SQRTF, EXPF, LOGF, LOG10F, FLOORF, CEILF, FABSF
4 */
5
6 #include <testfwk.h>
7 #include <math.h>
8
9 #if 0
10 #   include <stdio.h>
11 #   define DEBUG(x) x      /* only for "make test-host" */
12 #else
13 #   define DEBUG(x)
14 #endif
15
16 #define {func} (1)
17
18 #define TOLERANCE (1e-5)
19
20 #ifdef SDCC_mcs51
21 #   define STORAGE __code
22 #else
23 #   define STORAGE
24 #endif
25
26 #ifdef SDCC
27 #   ifndef REENTRANT
28 #       define REENTRANT reentrant
29 #   endif
30 #else
31 #   define REENTRANT
32 #endif
33
34
35 /* now exceptions for targets/functions which would not pass */
36 #if defined(SDCC_ds390)
37 #   define ACOSF_DISABLED (1)
38 #endif
39
40 #if defined(SDCC_hc08)
41 #   define ACOSF_DISABLED (1)
42 #   define EXPF_DISABLED  (1)
43 #   define SINHF_DISABLED (1)
44 #   define TANF_DISABLED  (1)
45 #endif
46
47 #if defined(SDCC_mcs51) && defined(SDCC_USE_XSTACK)
48 #   define SINF_DISABLED  (1)
49 #   define ACOSF_DISABLED (1)
50 #   define COSF_DISABLED  (1)
51 #endif
52
53 #if defined(SDCC_z80)
54 #   define ACOSF_DISABLED (1)
55 #   define ASINF_DISABLED (1)
56 #   define ATANF_DISABLED (1)
57 #endif
58
59
60 static float dummy(float a)
61 {
62     return a;
63 }
64
65 typedef float (*float_test_func)(float) REENTRANT;
66
67 /* the table with functions, their argument, expected result, tolerance.
68    For most 8-bitters each testpoint uses 14 bytes so we could have a few:) */
69 struct
70 {
71     float_test_func f;
72     float arg, result, tolerance;
73 }
74 static const STORAGE testpoint[] =
75 {
76
77     #if SINF
78     #   if {func}_DISABLED
79     #       warning {func} disabled
80     { dummy,      0.0,                    0.0,            TOLERANCE },
81     #   else
82     { sinf,       1.0,                    0.841470984,    TOLERANCE },
83     { sinf,      -2.0,                   -0.909297426,    TOLERANCE },
84     { sinf,       3.14159265/6.0,         0.5,            TOLERANCE },
85     { sinf,       3.14159265+2.0,        -0.909297426,    TOLERANCE },
86     #   endif
87     #endif
88     #if SINHF
89     #   if {func}_DISABLED
90     #       warning {func} disabled
91     { dummy,      0.0,                    0.0,            TOLERANCE },
92     #   else
93     { sinhf,      1.0,                    1.175201193,    TOLERANCE },
94     { sinhf,     -2.0,                   -3.626860407,    TOLERANCE },
95     { sinhf,      3.14159265/6.0,         0.547853473,    TOLERANCE },
96     { sinhf,      3.14159265+2.0,        85.49101383,     TOLERANCE },
97     #   endif
98     #endif
99     #if ASINF
100     #   if {func}_DISABLED
101     #       warning {func} disabled
102     { dummy,      0.0,                    0.0,            TOLERANCE },
103     #   else
104     { asinf,      0.5,                    3.14159265/6.0, TOLERANCE },
105     { asinf,      0.99749498,             1.5,            TOLERANCE },
106     { asinf,     -0.84147098,            -1.0,            TOLERANCE },
107     { asinf,      0.29552020,             0.3,            TOLERANCE },
108     #   endif
109     #endif
110
111
112     #if COSF
113     #   if {func}_DISABLED
114     #       warning {func} disabled
115     { dummy,      0.0,                    0.0,            TOLERANCE },
116     #   else
117     { cosf,       1.0,                    0.540302305,    TOLERANCE },
118     { cosf,       2.0,                   -0.416146836,    TOLERANCE },
119     { cosf,       3.14159265/3.0,         0.5,            TOLERANCE },
120     { cosf,       3.14159265+2.0,        +0.416146836,    TOLERANCE },
121     #    endif
122     #endif
123     #if COSHF
124     { coshf,      1.0,                    1.543080634,    TOLERANCE },
125     { coshf,      2.0,                    3.7621956910,   TOLERANCE },
126     { coshf,      3.0,                   10.06766199,     TOLERANCE },
127     { coshf,      4.0,                   27.30823283,     TOLERANCE },
128     #endif
129     #if ACOSF
130     #   if {func}_DISABLED
131     #       warning {func} disabled
132     { dummy,      0.0,                    0.0,            TOLERANCE },
133     #   else
134     { acosf,      0.1,                    1.470628905,    TOLERANCE },
135     { acosf,      0.5,                    1.047197551,    TOLERANCE },
136     { acosf,     -0.9,                    2.690565841,    TOLERANCE },
137     { acosf,      0.999,                  0.044725087,    TOLERANCE },
138     #   endif
139     #endif
140
141
142     #if TANF
143     #   if {func}_DISABLED
144     #       warning {func} disabled
145     { dummy,      0.0,                    0.0,            TOLERANCE },
146     #   else
147     { tanf,       1.0,                    1.557407724,    TOLERANCE },
148     { tanf,       2.0,                   -2.18503986,     TOLERANCE },
149     { tanf,       -1.5,                 -14.10141994,     TOLERANCE },
150     #   endif
151     #endif
152     #if TANHF
153     { tanhf,      1.0,                    0.761594155,    TOLERANCE },
154     { tanhf,      3.800201167,            0.999,          TOLERANCE },
155     { tanhf,      1.5,                    0.905148253,    TOLERANCE },
156     #endif
157     #if ATANF
158     #   if {func}_DISABLED
159     #       warning {func} disabled
160     { dummy,      0.0,                    0.0,            TOLERANCE },
161     #   else
162     { atanf,      1.0,                    0.7853981633,   TOLERANCE },
163     { atanf,      -2.0,                  -1.1071487177,   TOLERANCE },
164     { atanf,      42.0,                   1.5469913006,   TOLERANCE },
165     #   endif
166     #endif
167
168
169     #if SQRTF
170     { sqrtf,      5.0,                    2.23606801,     TOLERANCE },
171     { sqrtf,      1522756.0,           1234.0,            TOLERANCE },
172     { sqrtf,      10000.0,              100.0,            TOLERANCE },
173     #endif
174
175
176     #if EXPF
177     #   if {func}_DISABLED
178     #       warning {func} disabled
179     { dummy,      0.0,                    0.0,            TOLERANCE },
180     #   else
181     { expf,       2.30258509,            10.0,            TOLERANCE },
182     { expf,       4.82028150,           124.0,            TOLERANCE },
183     { expf,      10.0,                22026.46579,        TOLERANCE },
184     #   endif
185     #endif
186     #if LOGF
187     { logf,       2.06115362e-9,        -20.0,            TOLERANCE },
188     { logf,      10.0,                    2.30258509,     TOLERANCE },
189     { logf,     124.0,                    4.82028150,     TOLERANCE },
190     { logf,   22026.46579,               10.0,            TOLERANCE },
191     #endif
192     #if LOG10F
193     { log10f,     0.000001,              -6.0,            TOLERANCE },
194     { log10f,     3.16227766e-4,         -3.5,            TOLERANCE },
195     { log10f,     3.16227766,             0.5,            TOLERANCE },
196     { log10f,    10.0,                    1.0,            TOLERANCE },
197     { log10f,    1E6,                     6.0,            TOLERANCE },
198     #endif
199
200
201     #if FLOORF
202     #   if {func}_DISABLED
203     #       warning {func} disabled
204     { dummy,      0.0,                    0.0,            TOLERANCE },
205     #   else
206     { floorf,     1.0+0.000001,           1.0,            TOLERANCE },
207     { floorf,     1.0-0.000001,           0.0,            TOLERANCE },
208     //{ floorf,   1.0-0.000001,          -0.0,            TOLERANCE }, /* oops */
209     //{ dummy,    0.0,                   -0.0,            TOLERANCE }, /* oops */
210     { floorf,    -1.0+0.000001,          -1.0,            TOLERANCE },
211     { floorf,    -1.0-0.000001,          -2.0,            TOLERANCE },
212     #   endif
213     #endif
214
215
216     #if CEILF
217     { ceilf,      1.0+0.000001,           2.0,            TOLERANCE },
218     { ceilf,      1.0-0.000001,           1.0,            TOLERANCE },
219     { ceilf,     -1.0+0.000001,           0.0,            TOLERANCE },
220     { ceilf,     -1.0-0.000001,          -1.0,            TOLERANCE },
221     #endif
222
223
224     #if FABSF
225     { fabsf,     -1.0e-20,                1.0e-20,        TOLERANCE },
226     { fabsf,      9999999,                9999999,        TOLERANCE },
227     #endif
228
229 };
230
231
232 void
233 testFloat(void)
234 {
235     unsigned char i;
236     float result, rel_error;
237
238     for( i = 0; i < sizeof testpoint / sizeof testpoint[0]; i++ ) {
239
240         result = testpoint[i].f(testpoint[i].arg);
241
242         rel_error = testpoint[i].result ? result/testpoint[i].result - 1.0 : 0.0;
243
244         DEBUG(printf ("Test No: %d f(%f) = %f should: %f rel_error: %f %s\n",
245                       i, testpoint[i].arg, result, testpoint[i].result, rel_error,
246                       (fabsf(rel_error) < testpoint[i].tolerance) ? "Ok" : "Fail");)
247
248         ASSERT(fabsf(rel_error) < testpoint[i].tolerance);
249     }
250 }