1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - fpu.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2010 Ari64 *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 #define M64P_FPU_INLINE static __inline
29 typedef enum { FE_TONEAREST = 0, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD } eRoundType;
30 static void fesetround(eRoundType RoundType)
32 static const unsigned int msRound[4] = { _RC_NEAR, _RC_CHOP, _RC_UP, _RC_DOWN };
33 unsigned int oldX87, oldSSE2;
34 __control87_2(msRound[RoundType], _MCW_RC, &oldX87, &oldSSE2);
36 static __inline double round(double x) { return floor(x + 0.5); }
37 static __inline float roundf(float x) { return (float) floor(x + 0.5); }
38 static __inline double trunc(double x) { return (double) (int) x; }
39 static __inline float truncf(float x) { return (float) (int) x; }
42 #define M64P_FPU_INLINE static inline
47 M64P_FPU_INLINE void set_rounding(void)
49 switch(rounding_mode) {
51 fesetround(FE_TONEAREST);
54 fesetround(FE_TOWARDZERO);
57 fesetround(FE_UPWARD);
60 fesetround(FE_DOWNWARD);
65 M64P_FPU_INLINE void cvt_s_w(int *source,float *dest)
68 *dest = (float) *source;
70 M64P_FPU_INLINE void cvt_d_w(int *source,double *dest)
73 *dest = (double) *source;
75 M64P_FPU_INLINE void cvt_s_l(long long *source,float *dest)
78 *dest = (float) *source;
80 M64P_FPU_INLINE void cvt_d_l(long long *source,double *dest)
83 *dest = (double) *source;
85 M64P_FPU_INLINE void cvt_d_s(float *source,double *dest)
88 *dest = (double) *source;
90 M64P_FPU_INLINE void cvt_s_d(double *source,float *dest)
93 *dest = (float) *source;
96 M64P_FPU_INLINE void round_l_s(float *source,long long *dest)
98 *dest = (long long) roundf(*source);
100 M64P_FPU_INLINE void round_w_s(float *source,int *dest)
102 *dest = (int) roundf(*source);
104 M64P_FPU_INLINE void trunc_l_s(float *source,long long *dest)
106 *dest = (long long) truncf(*source);
108 M64P_FPU_INLINE void trunc_w_s(float *source,int *dest)
110 *dest = (int) truncf(*source);
112 M64P_FPU_INLINE void ceil_l_s(float *source,long long *dest)
114 *dest = (long long) ceilf(*source);
116 M64P_FPU_INLINE void ceil_w_s(float *source,int *dest)
118 *dest = (int) ceilf(*source);
120 M64P_FPU_INLINE void floor_l_s(float *source,long long *dest)
122 *dest = (long long) floorf(*source);
124 M64P_FPU_INLINE void floor_w_s(float *source,int *dest)
126 *dest = (int) floorf(*source);
129 M64P_FPU_INLINE void round_l_d(double *source,long long *dest)
131 *dest = (long long) round(*source);
133 M64P_FPU_INLINE void round_w_d(double *source,int *dest)
135 *dest = (int) round(*source);
137 M64P_FPU_INLINE void trunc_l_d(double *source,long long *dest)
139 *dest = (long long) trunc(*source);
141 M64P_FPU_INLINE void trunc_w_d(double *source,int *dest)
143 *dest = (int) trunc(*source);
145 M64P_FPU_INLINE void ceil_l_d(double *source,long long *dest)
147 *dest = (long long) ceil(*source);
149 M64P_FPU_INLINE void ceil_w_d(double *source,int *dest)
151 *dest = (int) ceil(*source);
153 M64P_FPU_INLINE void floor_l_d(double *source,long long *dest)
155 *dest = (long long) floor(*source);
157 M64P_FPU_INLINE void floor_w_d(double *source,int *dest)
159 *dest = (int) floor(*source);
162 M64P_FPU_INLINE void cvt_w_s(float *source,int *dest)
167 case 0: round_w_s(source,dest);return;
168 case 1: trunc_w_s(source,dest);return;
169 case 2: ceil_w_s(source,dest);return;
170 case 3: floor_w_s(source,dest);return;
173 M64P_FPU_INLINE void cvt_w_d(double *source,int *dest)
178 case 0: round_w_d(source,dest);return;
179 case 1: trunc_w_d(source,dest);return;
180 case 2: ceil_w_d(source,dest);return;
181 case 3: floor_w_d(source,dest);return;
184 M64P_FPU_INLINE void cvt_l_s(float *source,long long *dest)
189 case 0: round_l_s(source,dest);return;
190 case 1: trunc_l_s(source,dest);return;
191 case 2: ceil_l_s(source,dest);return;
192 case 3: floor_l_s(source,dest);return;
195 M64P_FPU_INLINE void cvt_l_d(double *source,long long *dest)
200 case 0: round_l_d(source,dest);return;
201 case 1: trunc_l_d(source,dest);return;
202 case 2: ceil_l_d(source,dest);return;
203 case 3: floor_l_d(source,dest);return;
207 M64P_FPU_INLINE void c_f_s()
211 M64P_FPU_INLINE void c_un_s(float *source,float *target)
213 FCR31=(isnan(*source) || isnan(*target)) ? FCR31|0x800000 : FCR31&~0x800000;
216 M64P_FPU_INLINE void c_eq_s(float *source,float *target)
218 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
219 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
221 M64P_FPU_INLINE void c_ueq_s(float *source,float *target)
223 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
224 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
227 M64P_FPU_INLINE void c_olt_s(float *source,float *target)
229 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
230 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
232 M64P_FPU_INLINE void c_ult_s(float *source,float *target)
234 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
235 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
238 M64P_FPU_INLINE void c_ole_s(float *source,float *target)
240 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
241 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
243 M64P_FPU_INLINE void c_ule_s(float *source,float *target)
245 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
246 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
249 M64P_FPU_INLINE void c_sf_s(float *source,float *target)
251 //if (isnan(*source) || isnan(*target)) // FIXME - exception
254 M64P_FPU_INLINE void c_ngle_s(float *source,float *target)
256 //if (isnan(*source) || isnan(*target)) // FIXME - exception
260 M64P_FPU_INLINE void c_seq_s(float *source,float *target)
262 //if (isnan(*source) || isnan(*target)) // FIXME - exception
263 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
265 M64P_FPU_INLINE void c_ngl_s(float *source,float *target)
267 //if (isnan(*source) || isnan(*target)) // FIXME - exception
268 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
271 M64P_FPU_INLINE void c_lt_s(float *source,float *target)
273 //if (isnan(*source) || isnan(*target)) // FIXME - exception
274 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
276 M64P_FPU_INLINE void c_nge_s(float *source,float *target)
278 //if (isnan(*source) || isnan(*target)) // FIXME - exception
279 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
282 M64P_FPU_INLINE void c_le_s(float *source,float *target)
284 //if (isnan(*source) || isnan(*target)) // FIXME - exception
285 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
287 M64P_FPU_INLINE void c_ngt_s(float *source,float *target)
289 //if (isnan(*source) || isnan(*target)) // FIXME - exception
290 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
293 M64P_FPU_INLINE void c_f_d()
297 M64P_FPU_INLINE void c_un_d(double *source,double *target)
299 FCR31=(isnan(*source) || isnan(*target)) ? FCR31|0x800000 : FCR31&~0x800000;
302 M64P_FPU_INLINE void c_eq_d(double *source,double *target)
304 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
305 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
307 M64P_FPU_INLINE void c_ueq_d(double *source,double *target)
309 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
310 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
313 M64P_FPU_INLINE void c_olt_d(double *source,double *target)
315 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
316 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
318 M64P_FPU_INLINE void c_ult_d(double *source,double *target)
320 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
321 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
324 M64P_FPU_INLINE void c_ole_d(double *source,double *target)
326 if (isnan(*source) || isnan(*target)) {FCR31&=~0x800000;return;}
327 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
329 M64P_FPU_INLINE void c_ule_d(double *source,double *target)
331 if (isnan(*source) || isnan(*target)) {FCR31|=0x800000;return;}
332 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
335 M64P_FPU_INLINE void c_sf_d(double *source,double *target)
337 //if (isnan(*source) || isnan(*target)) // FIXME - exception
340 M64P_FPU_INLINE void c_ngle_d(double *source,double *target)
342 //if (isnan(*source) || isnan(*target)) // FIXME - exception
346 M64P_FPU_INLINE void c_seq_d(double *source,double *target)
348 //if (isnan(*source) || isnan(*target)) // FIXME - exception
349 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
351 M64P_FPU_INLINE void c_ngl_d(double *source,double *target)
353 //if (isnan(*source) || isnan(*target)) // FIXME - exception
354 FCR31 = *source==*target ? FCR31|0x800000 : FCR31&~0x800000;
357 M64P_FPU_INLINE void c_lt_d(double *source,double *target)
359 //if (isnan(*source) || isnan(*target)) // FIXME - exception
360 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
362 M64P_FPU_INLINE void c_nge_d(double *source,double *target)
364 //if (isnan(*source) || isnan(*target)) // FIXME - exception
365 FCR31 = *source<*target ? FCR31|0x800000 : FCR31&~0x800000;
368 M64P_FPU_INLINE void c_le_d(double *source,double *target)
370 //if (isnan(*source) || isnan(*target)) // FIXME - exception
371 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
373 M64P_FPU_INLINE void c_ngt_d(double *source,double *target)
375 //if (isnan(*source) || isnan(*target)) // FIXME - exception
376 FCR31 = *source<=*target ? FCR31|0x800000 : FCR31&~0x800000;
380 M64P_FPU_INLINE void add_s(float *source1,float *source2,float *target)
383 *target=(*source1)+(*source2);
385 M64P_FPU_INLINE void sub_s(float *source1,float *source2,float *target)
388 *target=(*source1)-(*source2);
390 M64P_FPU_INLINE void mul_s(float *source1,float *source2,float *target)
393 *target=(*source1)*(*source2);
395 M64P_FPU_INLINE void div_s(float *source1,float *source2,float *target)
398 *target=(*source1)/(*source2);
400 M64P_FPU_INLINE void sqrt_s(float *source,float *target)
403 *target=sqrtf(*source);
405 M64P_FPU_INLINE void abs_s(float *source,float *target)
408 *target=fabsf(*source);
410 M64P_FPU_INLINE void mov_s(float *source,float *target)
415 M64P_FPU_INLINE void neg_s(float *source,float *target)
420 M64P_FPU_INLINE void add_d(double *source1,double *source2,double *target)
423 *target=(*source1)+(*source2);
425 M64P_FPU_INLINE void sub_d(double *source1,double *source2,double *target)
428 *target=(*source1)-(*source2);
430 M64P_FPU_INLINE void mul_d(double *source1,double *source2,double *target)
433 *target=(*source1)*(*source2);
435 M64P_FPU_INLINE void div_d(double *source1,double *source2,double *target)
438 *target=(*source1)/(*source2);
440 M64P_FPU_INLINE void sqrt_d(double *source,double *target)
443 *target=sqrt(*source);
445 M64P_FPU_INLINE void abs_d(double *source,double *target)
448 *target=fabs(*source);
450 M64P_FPU_INLINE void mov_d(double *source,double *target)
455 M64P_FPU_INLINE void neg_d(double *source,double *target)