1 | /* $Id: testmath.c 6517 2008-01-25 17:33:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Testcase for the no-crt math stuff.
|
---|
4 | */
|
---|
5 |
|
---|
6 |
|
---|
7 | /*******************************************************************************
|
---|
8 | * Header Files *
|
---|
9 | *******************************************************************************/
|
---|
10 | #ifndef MATHTEST_STANDALONE
|
---|
11 | # include <iprt/assert.h>
|
---|
12 | # include <math.h>
|
---|
13 | # undef printf
|
---|
14 | # define printf AssertMsg2
|
---|
15 | #else
|
---|
16 | # include <stdio.h>
|
---|
17 | # include <math.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #if defined(__GNUC__) && ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3) || (__GNUC__ > 4))
|
---|
21 | /* gcc starting with version 4.3 uses the MPFR library which results in more accurate results */
|
---|
22 | # define SIN180 -0.8011526357338304777463731115L
|
---|
23 | #else
|
---|
24 | # define SIN180 -0.801152635733830477871L
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | static void bitch(const char *pszWhat, const long double *plrdResult, const long double *plrdExpected)
|
---|
28 | {
|
---|
29 | const unsigned char *pach1 = (const unsigned char *)plrdResult;
|
---|
30 | const unsigned char *pach2 = (const unsigned char *)plrdExpected;
|
---|
31 | #ifndef MATHTEST_STANDALONE
|
---|
32 | printf("error: %s - %d instead of %d\n", pszWhat, (int)(*plrdResult * 100000), (int)(*plrdExpected * 100000));
|
---|
33 | #else
|
---|
34 | printf("error: %s - %.25f instead of %.25f\n", pszWhat, (double)*plrdResult, (double)*plrdExpected);
|
---|
35 | #endif
|
---|
36 | printf(" %02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x\n", pach1[0], pach1[1], pach1[2], pach1[3], pach1[4], pach1[5], pach1[6], pach1[7], pach1[8], pach1[9]);
|
---|
37 | printf(" %02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x\n", pach2[0], pach2[1], pach2[2], pach2[3], pach2[4], pach2[5], pach2[6], pach2[7], pach2[8], pach2[9]);
|
---|
38 | }
|
---|
39 |
|
---|
40 | static void bitchll(const char *pszWhat, long long llResult, long long llExpected)
|
---|
41 | {
|
---|
42 | #if defined(__MINGW32__) && !defined(Assert)
|
---|
43 | printf("error: %s - %I64d instead of %I64d\n", pszWhat, llResult, llExpected);
|
---|
44 | #else
|
---|
45 | printf("error: %s - %lld instead of %lld\n", pszWhat, llResult, llExpected);
|
---|
46 | #endif
|
---|
47 | }
|
---|
48 |
|
---|
49 | static void bitchl(const char *pszWhat, long lResult, long lExpected)
|
---|
50 | {
|
---|
51 | printf("error: %s - %ld instead of %ld\n", pszWhat, lResult, lExpected);
|
---|
52 | }
|
---|
53 |
|
---|
54 | extern int testsin(void)
|
---|
55 | {
|
---|
56 | return sinl(180.0L) == SIN180;
|
---|
57 | }
|
---|
58 |
|
---|
59 | extern int testremainder(void)
|
---|
60 | {
|
---|
61 | static double s_rd1 = 2.5;
|
---|
62 | static double s_rd2 = 2.0;
|
---|
63 | static double s_rd3 = 0.5;
|
---|
64 | return remainder(s_rd1, s_rd2) == s_rd3;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static __inline__ void set_cw(unsigned cw)
|
---|
68 | {
|
---|
69 | __asm __volatile("fldcw %0" : : "m" (cw));
|
---|
70 | }
|
---|
71 |
|
---|
72 | static __inline__ unsigned get_cw(void)
|
---|
73 | {
|
---|
74 | unsigned cw;
|
---|
75 | __asm __volatile("fstcw %0" : : "m" (cw));
|
---|
76 | return cw & 0xffff;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static long double check_lrd(const long double lrd, const unsigned long long ull, const unsigned short us)
|
---|
80 | {
|
---|
81 | static volatile long double lrd2;
|
---|
82 | lrd2 = lrd;
|
---|
83 | if ( *(unsigned long long *)&lrd2 != ull
|
---|
84 | || ((unsigned short *)&lrd2)[4] != us)
|
---|
85 | {
|
---|
86 | #if defined(__MINGW32__) && !defined(Assert)
|
---|
87 | printf("%I64x:%04x instead of %I64x:%04x\n", *(unsigned long long *)&lrd2, ((unsigned short *)&lrd2)[4], ull, us);
|
---|
88 | #else
|
---|
89 | printf("%llx:%04x instead of %llx:%04x\n", *(unsigned long long *)&lrd2, ((unsigned short *)&lrd2)[4], ull, us);
|
---|
90 | #endif
|
---|
91 | __asm__("int3\n");
|
---|
92 | }
|
---|
93 | return lrd;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static long double make_lrd(const unsigned long long ull, const unsigned short us)
|
---|
98 | {
|
---|
99 | union
|
---|
100 | {
|
---|
101 | long double lrd;
|
---|
102 | struct
|
---|
103 | {
|
---|
104 | unsigned long long ull;
|
---|
105 | unsigned short us;
|
---|
106 | } i;
|
---|
107 | } u;
|
---|
108 |
|
---|
109 | u.i.ull = ull;
|
---|
110 | u.i.us = us;
|
---|
111 | return u.lrd;
|
---|
112 | }
|
---|
113 |
|
---|
114 | static long double check_lrd_cw(const long double lrd, const unsigned long long ull, const unsigned short us, const unsigned cw)
|
---|
115 | {
|
---|
116 | set_cw(cw);
|
---|
117 | if (cw != get_cw())
|
---|
118 | {
|
---|
119 | printf("get_cw() -> %#x expected %#x\n", get_cw(), cw);
|
---|
120 | __asm__("int3\n");
|
---|
121 | }
|
---|
122 | return check_lrd(lrd, ull, us);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static long double make_lrd_cw(unsigned long long ull, unsigned short us, unsigned cw)
|
---|
126 | {
|
---|
127 | set_cw(cw);
|
---|
128 | return check_lrd_cw(make_lrd(ull, us), ull, us, cw);
|
---|
129 | }
|
---|
130 |
|
---|
131 | extern int testmath(void)
|
---|
132 | {
|
---|
133 | unsigned cErrors = 0;
|
---|
134 | long double lrdResult;
|
---|
135 | long double lrdExpect;
|
---|
136 | long double lrd;
|
---|
137 | #define CHECK(operation, expect) \
|
---|
138 | do { \
|
---|
139 | lrdExpect = expect; \
|
---|
140 | lrdResult = operation; \
|
---|
141 | if (lrdResult != lrdExpect) \
|
---|
142 | { \
|
---|
143 | bitch(#operation, &lrdResult, &lrdExpect); \
|
---|
144 | cErrors++; \
|
---|
145 | } \
|
---|
146 | } while (0)
|
---|
147 |
|
---|
148 | long long llResult;
|
---|
149 | long long llExpect;
|
---|
150 | #define CHECKLL(operation, expect) \
|
---|
151 | do { \
|
---|
152 | llExpect = expect; \
|
---|
153 | llResult = operation; \
|
---|
154 | if (llResult != llExpect) \
|
---|
155 | { \
|
---|
156 | bitchll(#operation, llResult, llExpect); \
|
---|
157 | cErrors++; \
|
---|
158 | } \
|
---|
159 | } while (0)
|
---|
160 |
|
---|
161 | long lResult;
|
---|
162 | long lExpect;
|
---|
163 | #define CHECKL(operation, expect) \
|
---|
164 | do { \
|
---|
165 | lExpect = expect; \
|
---|
166 | lResult = operation; \
|
---|
167 | if (lResult != lExpect) \
|
---|
168 | { \
|
---|
169 | bitchl(#operation, lResult, lExpect); \
|
---|
170 | cErrors++; \
|
---|
171 | } \
|
---|
172 | } while (0)
|
---|
173 |
|
---|
174 | CHECK(atan2l(1.0L, 1.0L), 0.785398163397448309603L);
|
---|
175 | CHECK(atan2l(2.3L, 3.3L), 0.608689307327411694890L);
|
---|
176 |
|
---|
177 | CHECK(ceill(1.9L), 2.0L);
|
---|
178 | CHECK(ceill(4.5L), 5.0L);
|
---|
179 | CHECK(ceill(3.3L), 4.0L);
|
---|
180 | CHECK(ceill(6.1L), 7.0L);
|
---|
181 |
|
---|
182 | CHECK(floorl(1.9L), 1.0L);
|
---|
183 | CHECK(floorl(4.5L), 4.0L);
|
---|
184 | CHECK(floorl(7.3L), 7.0L);
|
---|
185 | CHECK(floorl(1234.1L), 1234.0L);
|
---|
186 | CHECK(floor(1233.1), 1233.0);
|
---|
187 | CHECK(floor(1239.98989898), 1239.0);
|
---|
188 | CHECK(floorf(9999.999), 9999.0);
|
---|
189 |
|
---|
190 | CHECK(ldexpl(1.0L, 1), 2.0L);
|
---|
191 | CHECK(ldexpl(1.0L, 10), 1024.0L);
|
---|
192 | CHECK(ldexpl(2.25L, 10), 2304.0L);
|
---|
193 |
|
---|
194 | CHECKLL(llrintl(1.0L), 1);
|
---|
195 | CHECKLL(llrintl(1.3L), 1);
|
---|
196 | CHECKLL(llrintl(1.5L), 2);
|
---|
197 | CHECKLL(llrintl(1.9L), 2);
|
---|
198 | CHECKLL(llrintf(123.34), 123);
|
---|
199 | CHECKLL(llrintf(-123.50), -124);
|
---|
200 | CHECKLL(llrint(42.42), 42);
|
---|
201 | CHECKLL(llrint(-2147483648.12343), -2147483648LL);
|
---|
202 | #if !defined(RT_ARCH_AMD64)
|
---|
203 | CHECKLL(lrint(-21474836499.12343), -2147483648LL);
|
---|
204 | CHECKLL(lrint(-2147483649932412.12343), -2147483648LL);
|
---|
205 | #else
|
---|
206 | CHECKLL(lrint(-21474836499.12343), -21474836499L);
|
---|
207 | CHECKLL(lrint(-2147483649932412.12343), -2147483649932412L);
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | // __asm__("int3");
|
---|
211 | CHECKL(lrintl(make_lrd_cw(000000000000000000ULL,000000,0x027f)), 0L);
|
---|
212 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x027f)), 0L);
|
---|
213 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x027f)), 0L);
|
---|
214 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x067f)), 0L);
|
---|
215 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x067f)), 0L);
|
---|
216 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x0a7f)), 1L);
|
---|
217 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x0a7f)), 1L);
|
---|
218 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x0e7f)), 0L);
|
---|
219 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x3ffe,0x0e7f)), 0L);
|
---|
220 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x027f)), 0L);
|
---|
221 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x027f)), 0L);
|
---|
222 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x067f)), -1L);
|
---|
223 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x067f)), -1L);
|
---|
224 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x0a7f)), 0L);
|
---|
225 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x0a7f)), 0L);
|
---|
226 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x0e7f)), 0L);
|
---|
227 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0xbffe,0x0e7f)), 0L);
|
---|
228 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x027f)), 0L);
|
---|
229 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x027f)), 0L);
|
---|
230 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x067f)), 0L);
|
---|
231 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x067f)), 0L);
|
---|
232 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x0a7f)), 1L);
|
---|
233 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x0a7f)), 1L);
|
---|
234 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x0e7f)), 0L);
|
---|
235 | CHECKL(lrintl(make_lrd_cw(0x9249249249249000ULL,0x3ffc,0x0e7f)), 0L);
|
---|
236 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x027f)), 0L);
|
---|
237 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x027f)), 0L);
|
---|
238 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x067f)), -1L);
|
---|
239 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x067f)), -1L);
|
---|
240 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x0a7f)), 0L);
|
---|
241 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x0a7f)), 0L);
|
---|
242 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x0e7f)), 0L);
|
---|
243 | CHECKL(lrintl(make_lrd_cw(0xe38e38e38e38e000ULL,0xbffb,0x0e7f)), 0L);
|
---|
244 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x027f)), 32768L);
|
---|
245 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x027f)), 32768L);
|
---|
246 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x067f)), 32768L);
|
---|
247 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x067f)), 32768L);
|
---|
248 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x0a7f)), 32768L);
|
---|
249 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x0a7f)), 32768L);
|
---|
250 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x0e7f)), 32768L);
|
---|
251 | CHECKL(lrintl(make_lrd_cw(0x8000000000000000ULL,0x400e,0x0e7f)), 32768L);
|
---|
252 | #if !defined(RT_ARCH_AMD64)
|
---|
253 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x027f)), (long)-2147483648L);
|
---|
254 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x027f)), (long)-2147483648L);
|
---|
255 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x067f)), (long)-2147483648L);
|
---|
256 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x067f)), (long)-2147483648L);
|
---|
257 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x0a7f)), (long)-2147483648L);
|
---|
258 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x0a7f)), (long)-2147483648L);
|
---|
259 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x0e7f)), (long)-2147483648L);
|
---|
260 | CHECKL(lrintl(make_lrd_cw(0xad78ebc5ac620000ULL,0xc041,0x0e7f)), (long)-2147483648L);
|
---|
261 | #endif
|
---|
262 | set_cw(0x27f);
|
---|
263 |
|
---|
264 | CHECK(logl(2.7182818284590452353602874713526625L), 1.0);
|
---|
265 |
|
---|
266 | CHECK(remainderl(1.0L, 1.0L), 0.0);
|
---|
267 | CHECK(remainderl(1.0L, 1.5L), -0.5);
|
---|
268 | CHECK(remainderl(42.0L, 34.25L), 7.75);
|
---|
269 | CHECK(remainderf(43.0, 34.25), 8.75);
|
---|
270 | CHECK(remainder(44.25, 34.25), 10.00);
|
---|
271 | double rd1 = 44.25;
|
---|
272 | double rd2 = 34.25;
|
---|
273 | CHECK(remainder(rd1, rd2), 10.00);
|
---|
274 | CHECK(remainder(2.5, 2.0), 0.5);
|
---|
275 | CHECK(remainder(2.5, 2.0), 0.5);
|
---|
276 | CHECK(remainder(2.5, 2.0), 0.5);
|
---|
277 | CHECKLL(testremainder(), 1);
|
---|
278 |
|
---|
279 |
|
---|
280 | CHECK(rintl(1.0L), 1.0);
|
---|
281 | CHECK(rintl(1.4L), 1.0);
|
---|
282 | CHECK(rintl(1.3L), 1.0);
|
---|
283 | CHECK(rintl(0.9L), 1.0);
|
---|
284 | CHECK(rintl(3123.1232L), 3123.0);
|
---|
285 | CHECK(rint(3985.13454), 3985.0);
|
---|
286 | CHECK(rintf(9999.999), 10000.0);
|
---|
287 |
|
---|
288 | CHECK(sinl(1.0L), 0.84147098480789650664L);
|
---|
289 | lrd = 180.0L;
|
---|
290 | CHECK(sinl(lrd), -0.801152635733830477871L);
|
---|
291 | CHECK(sinl(180.0L), SIN180);
|
---|
292 | CHECKLL(testsin(), 1);
|
---|
293 |
|
---|
294 | CHECK(sqrtl(1.0L), 1.0);
|
---|
295 | CHECK(sqrtl(4.0L), 2.0);
|
---|
296 | CHECK(sqrtl(1525225.0L), 1235.0);
|
---|
297 |
|
---|
298 | CHECK(tanl(0.0L), 0.0);
|
---|
299 | CHECK(tanl(0.7853981633974483096156608458198757L), 1.0);
|
---|
300 |
|
---|
301 | CHECK(powl(0.0, 0.0), 1.0);
|
---|
302 | CHECK(powl(2.0, 2.0), 4.0);
|
---|
303 | CHECK(powl(3.0, 3.0), 27.0);
|
---|
304 |
|
---|
305 | return cErrors;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
310 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
311 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
312 | #if 0
|
---|
313 |
|
---|
314 | #define floatx_to_int32 floatx80_to_int32
|
---|
315 | #define floatx_to_int64 floatx80_to_int64
|
---|
316 | #define floatx_to_int32_round_to_zero floatx80_to_int32_round_to_zero
|
---|
317 | #define floatx_to_int64_round_to_zero floatx80_to_int64_round_to_zero
|
---|
318 | #define floatx_abs floatx80_abs
|
---|
319 | #define floatx_chs floatx80_chs
|
---|
320 | #define floatx_round_to_int(foo, bar) floatx80_round_to_int(foo, NULL)
|
---|
321 | #define floatx_compare floatx80_compare
|
---|
322 | #define floatx_compare_quiet floatx80_compare_quiet
|
---|
323 | #undef sin
|
---|
324 | #undef cos
|
---|
325 | #undef sqrt
|
---|
326 | #undef pow
|
---|
327 | #undef log
|
---|
328 | #undef tan
|
---|
329 | #undef atan2
|
---|
330 | #undef floor
|
---|
331 | #undef ceil
|
---|
332 | #undef ldexp
|
---|
333 | #define sin sinl
|
---|
334 | #define cos cosl
|
---|
335 | #define sqrt sqrtl
|
---|
336 | #define pow powl
|
---|
337 | #define log logl
|
---|
338 | #define tan tanl
|
---|
339 | #define atan2 atan2l
|
---|
340 | #define floor floorl
|
---|
341 | #define ceil ceill
|
---|
342 | #define ldexp ldexpl
|
---|
343 |
|
---|
344 |
|
---|
345 | typedef long double CPU86_LDouble;
|
---|
346 |
|
---|
347 | typedef union {
|
---|
348 | long double d;
|
---|
349 | struct {
|
---|
350 | unsigned long long lower;
|
---|
351 | unsigned short upper;
|
---|
352 | } l;
|
---|
353 | } CPU86_LDoubleU;
|
---|
354 |
|
---|
355 | /* the following deal with x86 long double-precision numbers */
|
---|
356 | #define MAXEXPD 0x7fff
|
---|
357 | #define EXPBIAS 16383
|
---|
358 | #define EXPD(fp) (fp.l.upper & 0x7fff)
|
---|
359 | #define SIGND(fp) ((fp.l.upper) & 0x8000)
|
---|
360 | #define MANTD(fp) (fp.l.lower)
|
---|
361 | #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
|
---|
362 |
|
---|
363 | typedef long double floatx80;
|
---|
364 | #define STATUS_PARAM , void *pv
|
---|
365 |
|
---|
366 | static floatx80 floatx80_round_to_int( floatx80 a STATUS_PARAM)
|
---|
367 | {
|
---|
368 | return rintl(a);
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 |
|
---|
373 | struct myenv
|
---|
374 | {
|
---|
375 | unsigned int fpstt; /* top of stack index */
|
---|
376 | unsigned int fpus;
|
---|
377 | unsigned int fpuc;
|
---|
378 | unsigned char fptags[8]; /* 0 = valid, 1 = empty */
|
---|
379 | union {
|
---|
380 | #ifdef USE_X86LDOUBLE
|
---|
381 | CPU86_LDouble d __attribute__((aligned(16)));
|
---|
382 | #else
|
---|
383 | CPU86_LDouble d;
|
---|
384 | #endif
|
---|
385 | } fpregs[8];
|
---|
386 |
|
---|
387 | } my_env, env_org, env_res, *env = &my_env;
|
---|
388 |
|
---|
389 |
|
---|
390 | #define ST0 (env->fpregs[env->fpstt].d)
|
---|
391 | #define ST(n) (env->fpregs[(env->fpstt + (n)) & 7].d)
|
---|
392 | #define ST1 ST(1)
|
---|
393 | #define MAXTAN 9223372036854775808.0
|
---|
394 |
|
---|
395 |
|
---|
396 | static inline void fpush(void)
|
---|
397 | {
|
---|
398 | env->fpstt = (env->fpstt - 1) & 7;
|
---|
399 | env->fptags[env->fpstt] = 0; /* validate stack entry */
|
---|
400 | }
|
---|
401 |
|
---|
402 | static inline void fpop(void)
|
---|
403 | {
|
---|
404 | env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
|
---|
405 | env->fpstt = (env->fpstt + 1) & 7;
|
---|
406 | }
|
---|
407 |
|
---|
408 | static void helper_f2xm1(void)
|
---|
409 | {
|
---|
410 | ST0 = pow(2.0,ST0) - 1.0;
|
---|
411 | }
|
---|
412 |
|
---|
413 | static void helper_fyl2x(void)
|
---|
414 | {
|
---|
415 | CPU86_LDouble fptemp;
|
---|
416 |
|
---|
417 | fptemp = ST0;
|
---|
418 | if (fptemp>0.0){
|
---|
419 | fptemp = log(fptemp)/log(2.0); /* log2(ST) */
|
---|
420 | ST1 *= fptemp;
|
---|
421 | fpop();
|
---|
422 | } else {
|
---|
423 | env->fpus &= (~0x4700);
|
---|
424 | env->fpus |= 0x400;
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | static void helper_fptan(void)
|
---|
429 | {
|
---|
430 | CPU86_LDouble fptemp;
|
---|
431 |
|
---|
432 | fptemp = ST0;
|
---|
433 | if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
|
---|
434 | env->fpus |= 0x400;
|
---|
435 | } else {
|
---|
436 | ST0 = tan(fptemp);
|
---|
437 | fpush();
|
---|
438 | ST0 = 1.0;
|
---|
439 | env->fpus &= (~0x400); /* C2 <-- 0 */
|
---|
440 | /* the above code is for |arg| < 2**52 only */
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | static void helper_fpatan(void)
|
---|
445 | {
|
---|
446 | CPU86_LDouble fptemp, fpsrcop;
|
---|
447 |
|
---|
448 | fpsrcop = ST1;
|
---|
449 | fptemp = ST0;
|
---|
450 | ST1 = atan2(fpsrcop,fptemp);
|
---|
451 | fpop();
|
---|
452 | }
|
---|
453 |
|
---|
454 | static void helper_fxtract(void)
|
---|
455 | {
|
---|
456 | CPU86_LDoubleU temp;
|
---|
457 | unsigned int expdif;
|
---|
458 |
|
---|
459 | temp.d = ST0;
|
---|
460 | expdif = EXPD(temp) - EXPBIAS;
|
---|
461 | /*DP exponent bias*/
|
---|
462 | ST0 = expdif;
|
---|
463 | fpush();
|
---|
464 | BIASEXPONENT(temp);
|
---|
465 | ST0 = temp.d;
|
---|
466 | }
|
---|
467 |
|
---|
468 | static void helper_fprem1(void)
|
---|
469 | {
|
---|
470 | CPU86_LDouble dblq, fpsrcop, fptemp;
|
---|
471 | CPU86_LDoubleU fpsrcop1, fptemp1;
|
---|
472 | int expdif;
|
---|
473 | int q;
|
---|
474 |
|
---|
475 | fpsrcop = ST0;
|
---|
476 | fptemp = ST1;
|
---|
477 | fpsrcop1.d = fpsrcop;
|
---|
478 | fptemp1.d = fptemp;
|
---|
479 | expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
|
---|
480 | if (expdif < 53) {
|
---|
481 | dblq = fpsrcop / fptemp;
|
---|
482 | dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
|
---|
483 | ST0 = fpsrcop - fptemp*dblq;
|
---|
484 | q = (int)dblq; /* cutting off top bits is assumed here */
|
---|
485 | env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
|
---|
486 | /* (C0,C1,C3) <-- (q2,q1,q0) */
|
---|
487 | env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
|
---|
488 | env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
|
---|
489 | env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
|
---|
490 | } else {
|
---|
491 | env->fpus |= 0x400; /* C2 <-- 1 */
|
---|
492 | fptemp = pow(2.0, expdif-50);
|
---|
493 | fpsrcop = (ST0 / ST1) / fptemp;
|
---|
494 | /* fpsrcop = integer obtained by rounding to the nearest */
|
---|
495 | fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
|
---|
496 | floor(fpsrcop): ceil(fpsrcop);
|
---|
497 | ST0 -= (ST1 * fpsrcop * fptemp);
|
---|
498 | }
|
---|
499 | }
|
---|
500 |
|
---|
501 | static void helper_fprem(void)
|
---|
502 | {
|
---|
503 | #if 0
|
---|
504 | LogFlow(("helper_fprem: ST0=%.*Vhxs ST1=%.*Vhxs fpus=%#x\n", sizeof(ST0), &ST0, sizeof(ST1), &ST1, env->fpus));
|
---|
505 |
|
---|
506 | __asm__ __volatile__("fldt (%2)\n"
|
---|
507 | "fldt (%1)\n"
|
---|
508 | "fprem \n"
|
---|
509 | "fnstsw (%0)\n"
|
---|
510 | "fstpt (%1)\n"
|
---|
511 | "fstpt (%2)\n"
|
---|
512 | : : "r" (&env->fpus), "r" (&ST0), "r" (&ST1) : "memory");
|
---|
513 |
|
---|
514 | LogFlow(("helper_fprem: -> ST0=%.*Vhxs fpus=%#x c\n", sizeof(ST0), &ST0, env->fpus));
|
---|
515 | #else
|
---|
516 | CPU86_LDouble dblq, fpsrcop, fptemp;
|
---|
517 | CPU86_LDoubleU fpsrcop1, fptemp1;
|
---|
518 | int expdif;
|
---|
519 | int q;
|
---|
520 |
|
---|
521 | fpsrcop = ST0;
|
---|
522 | fptemp = ST1;
|
---|
523 | fpsrcop1.d = fpsrcop;
|
---|
524 | fptemp1.d = fptemp;
|
---|
525 |
|
---|
526 | expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
|
---|
527 | if ( expdif < 53 ) {
|
---|
528 | dblq = fpsrcop / fptemp;
|
---|
529 | dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
|
---|
530 | ST0 = fpsrcop - fptemp*dblq;
|
---|
531 | q = (int)dblq; /* cutting off top bits is assumed here */
|
---|
532 | env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
|
---|
533 | /* (C0,C1,C3) <-- (q2,q1,q0) */
|
---|
534 | env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
|
---|
535 | env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
|
---|
536 | env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
|
---|
537 | } else {
|
---|
538 | env->fpus |= 0x400; /* C2 <-- 1 */
|
---|
539 | fptemp = pow(2.0, expdif-50);
|
---|
540 | fpsrcop = (ST0 / ST1) / fptemp;
|
---|
541 | /* fpsrcop = integer obtained by chopping */
|
---|
542 | fpsrcop = (fpsrcop < 0.0)?
|
---|
543 | -(floor(fabs(fpsrcop))): floor(fpsrcop);
|
---|
544 | ST0 -= (ST1 * fpsrcop * fptemp);
|
---|
545 | }
|
---|
546 | #endif
|
---|
547 | }
|
---|
548 |
|
---|
549 | static void helper_fyl2xp1(void)
|
---|
550 | {
|
---|
551 | CPU86_LDouble fptemp;
|
---|
552 |
|
---|
553 | fptemp = ST0;
|
---|
554 | if ((fptemp+1.0)>0.0) {
|
---|
555 | fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
|
---|
556 | ST1 *= fptemp;
|
---|
557 | fpop();
|
---|
558 | } else {
|
---|
559 | env->fpus &= (~0x4700);
|
---|
560 | env->fpus |= 0x400;
|
---|
561 | }
|
---|
562 | }
|
---|
563 |
|
---|
564 | static void helper_fsqrt(void)
|
---|
565 | {
|
---|
566 | CPU86_LDouble fptemp;
|
---|
567 |
|
---|
568 | fptemp = ST0;
|
---|
569 | if (fptemp<0.0) {
|
---|
570 | env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
|
---|
571 | env->fpus |= 0x400;
|
---|
572 | }
|
---|
573 | ST0 = sqrt(fptemp);
|
---|
574 | }
|
---|
575 |
|
---|
576 | static void helper_fsincos(void)
|
---|
577 | {
|
---|
578 | CPU86_LDouble fptemp;
|
---|
579 |
|
---|
580 | fptemp = ST0;
|
---|
581 | if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
|
---|
582 | env->fpus |= 0x400;
|
---|
583 | } else {
|
---|
584 | ST0 = sin(fptemp);
|
---|
585 | fpush();
|
---|
586 | ST0 = cos(fptemp);
|
---|
587 | env->fpus &= (~0x400); /* C2 <-- 0 */
|
---|
588 | /* the above code is for |arg| < 2**63 only */
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | static void helper_frndint(void)
|
---|
593 | {
|
---|
594 | ST0 = floatx_round_to_int(ST0, &env->fp_status);
|
---|
595 | }
|
---|
596 |
|
---|
597 | static void helper_fscale(void)
|
---|
598 | {
|
---|
599 | ST0 = ldexp (ST0, (int)(ST1));
|
---|
600 | }
|
---|
601 |
|
---|
602 | static void helper_fsin(void)
|
---|
603 | {
|
---|
604 | CPU86_LDouble fptemp;
|
---|
605 |
|
---|
606 | fptemp = ST0;
|
---|
607 | if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
|
---|
608 | env->fpus |= 0x400;
|
---|
609 | } else {
|
---|
610 | ST0 = sin(fptemp);
|
---|
611 | env->fpus &= (~0x400); /* C2 <-- 0 */
|
---|
612 | /* the above code is for |arg| < 2**53 only */
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | static void helper_fcos(void)
|
---|
617 | {
|
---|
618 | CPU86_LDouble fptemp;
|
---|
619 |
|
---|
620 | fptemp = ST0;
|
---|
621 | if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
|
---|
622 | env->fpus |= 0x400;
|
---|
623 | } else {
|
---|
624 | ST0 = cos(fptemp);
|
---|
625 | env->fpus &= (~0x400); /* C2 <-- 0 */
|
---|
626 | /* the above code is for |arg5 < 2**63 only */
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | static void helper_fxam_ST0(void)
|
---|
631 | {
|
---|
632 | CPU86_LDoubleU temp;
|
---|
633 | int expdif;
|
---|
634 |
|
---|
635 | temp.d = ST0;
|
---|
636 |
|
---|
637 | env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
|
---|
638 | if (SIGND(temp))
|
---|
639 | env->fpus |= 0x200; /* C1 <-- 1 */
|
---|
640 |
|
---|
641 | /* XXX: test fptags too */
|
---|
642 | expdif = EXPD(temp);
|
---|
643 | if (expdif == MAXEXPD) {
|
---|
644 | #ifdef USE_X86LDOUBLE
|
---|
645 | if (MANTD(temp) == 0x8000000000000000ULL)
|
---|
646 | #else
|
---|
647 | if (MANTD(temp) == 0)
|
---|
648 | #endif
|
---|
649 | env->fpus |= 0x500 /*Infinity*/;
|
---|
650 | else
|
---|
651 | env->fpus |= 0x100 /*NaN*/;
|
---|
652 | } else if (expdif == 0) {
|
---|
653 | if (MANTD(temp) == 0)
|
---|
654 | env->fpus |= 0x4000 /*Zero*/;
|
---|
655 | else
|
---|
656 | env->fpus |= 0x4400 /*Denormal*/;
|
---|
657 | } else {
|
---|
658 | env->fpus |= 0x400;
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 |
|
---|
663 | void check_env(void)
|
---|
664 | {
|
---|
665 | int i;
|
---|
666 | for (i = 0; i < 8; i++)
|
---|
667 | {
|
---|
668 | CPU86_LDoubleU my, res;
|
---|
669 | my.d = env->fpregs[i].d;
|
---|
670 | res.d = env_res.fpregs[i].d;
|
---|
671 |
|
---|
672 | if ( my.l.lower != res.l.lower
|
---|
673 | || my.l.upper != res.l.upper)
|
---|
674 | printf("register %i: %#018llx:%#06x\n"
|
---|
675 | " expected %#018llx:%#06x\n",
|
---|
676 | i,
|
---|
677 | my.l.lower, my.l.upper,
|
---|
678 | res.l.lower, res.l.upper);
|
---|
679 | }
|
---|
680 | for (i = 0; i < 8; i++)
|
---|
681 | if (env->fptags[i] != env_res.fptags[i])
|
---|
682 | printf("tag %i: %d != %d\n", i, env->fptags[i], env_res.fptags[i]);
|
---|
683 | if (env->fpstt != env_res.fpstt)
|
---|
684 | printf("fpstt: %#06x != %#06x\n", env->fpstt, env_res.fpstt);
|
---|
685 | if (env->fpuc != env_res.fpuc)
|
---|
686 | printf("fpuc: %#06x != %#06x\n", env->fpuc, env_res.fpuc);
|
---|
687 | if (env->fpus != env_res.fpus)
|
---|
688 | printf("fpus: %#06x != %#06x\n", env->fpus, env_res.fpus);
|
---|
689 | }
|
---|
690 | #endif /* not used. */
|
---|
691 |
|
---|
692 | #if 0 /* insert this into helper.c */
|
---|
693 | /* FPU helpers */
|
---|
694 | CPU86_LDoubleU my_st[8];
|
---|
695 | unsigned int my_fpstt;
|
---|
696 | unsigned int my_fpus;
|
---|
697 | unsigned int my_fpuc;
|
---|
698 | unsigned char my_fptags[8];
|
---|
699 |
|
---|
700 | void hlp_fpu_enter(void)
|
---|
701 | {
|
---|
702 | int i;
|
---|
703 | for (i = 0; i < 8; i++)
|
---|
704 | my_st[i].d = env->fpregs[i].d;
|
---|
705 | my_fpstt = env->fpstt;
|
---|
706 | my_fpus = env->fpus;
|
---|
707 | my_fpuc = env->fpuc;
|
---|
708 | memcpy(&my_fptags, &env->fptags, sizeof(my_fptags));
|
---|
709 | }
|
---|
710 |
|
---|
711 | void hlp_fpu_leave(const char *psz)
|
---|
712 | {
|
---|
713 | int i;
|
---|
714 | Log(("/*code*/ \n"));
|
---|
715 | for (i = 0; i < 8; i++)
|
---|
716 | Log(("/*code*/ *(unsigned long long *)&env_org.fpregs[%d] = %#018llxULL; ((unsigned short *)&env_org.fpregs[%d])[4] = %#06x; env_org.fptags[%d]=%d;\n",
|
---|
717 | i, my_st[i].l.lower, i, my_st[i].l.upper, i, my_fptags[i]));
|
---|
718 | Log(("/*code*/ env_org.fpstt=%#x;\n", my_fpstt));
|
---|
719 | Log(("/*code*/ env_org.fpus=%#x;\n", my_fpus));
|
---|
720 | Log(("/*code*/ env_org.fpuc=%#x;\n", my_fpuc));
|
---|
721 | for (i = 0; i < 8; i++)
|
---|
722 | {
|
---|
723 | CPU86_LDoubleU u;
|
---|
724 | u.d = env->fpregs[i].d;
|
---|
725 | Log(("/*code*/ *(unsigned long long *)&env_res.fpregs[%d] = %#018llxULL; ((unsigned short *)&env_res.fpregs[%d])[4] = %#06x; env_res.fptags[%d]=%d;\n",
|
---|
726 | i, u.l.lower, i, u.l.upper, i, env->fptags[i]));
|
---|
727 | }
|
---|
728 | Log(("/*code*/ env_res.fpstt=%#x;\n", env->fpstt));
|
---|
729 | Log(("/*code*/ env_res.fpus=%#x;\n", env->fpus));
|
---|
730 | Log(("/*code*/ env_res.fpuc=%#x;\n", env->fpuc));
|
---|
731 |
|
---|
732 | Log(("/*code*/ my_env = env_org;\n"));
|
---|
733 | Log(("/*code*/ %s();\n", psz));
|
---|
734 | Log(("/*code*/ check_env();\n"));
|
---|
735 | }
|
---|
736 | #endif /* helper.c */
|
---|
737 |
|
---|
738 | extern void testmath2(void )
|
---|
739 | {
|
---|
740 | #if 0
|
---|
741 | #include "/tmp/code.h"
|
---|
742 | #endif
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
747 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
748 | /////////////////////////////////////////////////////////////////////////////////////////
|
---|
749 |
|
---|
750 | #ifdef MATHTEST_STANDALONE
|
---|
751 |
|
---|
752 | void test_fops(double a, double b)
|
---|
753 | {
|
---|
754 | printf("a=%f b=%f a+b=%f\n", a, b, a + b);
|
---|
755 | printf("a=%f b=%f a-b=%f\n", a, b, a - b);
|
---|
756 | printf("a=%f b=%f a*b=%f\n", a, b, a * b);
|
---|
757 | printf("a=%f b=%f a/b=%f\n", a, b, a / b);
|
---|
758 | printf("a=%f b=%f fmod(a, b)=%f\n", a, b, (double)fmod(a, b));
|
---|
759 | printf("a=%f sqrt(a)=%f\n", a, (double)sqrtl(a));
|
---|
760 | printf("a=%f sin(a)=%f\n", a, (double)sinl(a));
|
---|
761 | printf("a=%f cos(a)=%f\n", a, (double)cos(a));
|
---|
762 | printf("a=%f tan(a)=%f\n", a, (double)tanl(a));
|
---|
763 | printf("a=%f log(a)=%f\n", a, (double)log(a));
|
---|
764 | printf("a=%f exp(a)=%f\n", a, (double)exp(a));
|
---|
765 | printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
|
---|
766 | /* just to test some op combining */
|
---|
767 | printf("a=%f asin(sinl(a))=%f\n", a, (double)asin(sinl(a)));
|
---|
768 | printf("a=%f acos(cos(a))=%f\n", a, (double)acos(cos(a)));
|
---|
769 | printf("a=%f atan(tanl(a))=%f\n", a, (double)atan(tanl(a)));
|
---|
770 | }
|
---|
771 |
|
---|
772 | int main()
|
---|
773 | {
|
---|
774 | unsigned cErrors = testmath();
|
---|
775 |
|
---|
776 | testmath2();
|
---|
777 | test_fops(2, 3);
|
---|
778 | test_fops(1.4, -5);
|
---|
779 |
|
---|
780 | printf("cErrors=%d\n", cErrors);
|
---|
781 | return cErrors;
|
---|
782 | }
|
---|
783 | #endif
|
---|
784 |
|
---|