4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #define QT0 (env->qt0)
24 #define QT1 (env->qt1)
26 static void check_ieee_exceptions(CPUSPARCState
*env
)
30 status
= get_float_exception_flags(&env
->fp_status
);
32 /* Copy IEEE 754 flags into FSR */
33 if (status
& float_flag_invalid
) {
36 if (status
& float_flag_overflow
) {
39 if (status
& float_flag_underflow
) {
42 if (status
& float_flag_divbyzero
) {
45 if (status
& float_flag_inexact
) {
49 if ((env
->fsr
& FSR_CEXC_MASK
) & ((env
->fsr
& FSR_TEM_MASK
) >> 23)) {
50 /* Unmasked exception, generate a trap */
51 env
->fsr
|= FSR_FTT_IEEE_EXCP
;
52 helper_raise_exception(env
, TT_FP_EXCP
);
54 /* Accumulate exceptions */
55 env
->fsr
|= (env
->fsr
& FSR_CEXC_MASK
) << 5;
60 static inline void clear_float_exceptions(CPUSPARCState
*env
)
62 set_float_exception_flags(0, &env
->fp_status
);
65 #define F_HELPER(name, p) void helper_f##name##p(CPUSPARCState *env)
67 #define F_BINOP(name) \
68 float32 helper_f ## name ## s (CPUSPARCState *env, float32 src1, \
72 clear_float_exceptions(env); \
73 ret = float32_ ## name (src1, src2, &env->fp_status); \
74 check_ieee_exceptions(env); \
77 float64 helper_f ## name ## d (CPUSPARCState * env, float64 src1,\
81 clear_float_exceptions(env); \
82 ret = float64_ ## name (src1, src2, &env->fp_status); \
83 check_ieee_exceptions(env); \
88 clear_float_exceptions(env); \
89 QT0 = float128_ ## name (QT0, QT1, &env->fp_status); \
90 check_ieee_exceptions(env); \
99 float64
helper_fsmuld(CPUSPARCState
*env
, float32 src1
, float32 src2
)
102 clear_float_exceptions(env
);
103 ret
= float64_mul(float32_to_float64(src1
, &env
->fp_status
),
104 float32_to_float64(src2
, &env
->fp_status
),
106 check_ieee_exceptions(env
);
110 void helper_fdmulq(CPUSPARCState
*env
, float64 src1
, float64 src2
)
112 clear_float_exceptions(env
);
113 QT0
= float128_mul(float64_to_float128(src1
, &env
->fp_status
),
114 float64_to_float128(src2
, &env
->fp_status
),
116 check_ieee_exceptions(env
);
119 float32
helper_fnegs(float32 src
)
121 return float32_chs(src
);
124 #ifdef TARGET_SPARC64
125 float64
helper_fnegd(float64 src
)
127 return float64_chs(src
);
132 QT0
= float128_chs(QT1
);
136 /* Integer to float conversion. */
137 float32
helper_fitos(CPUSPARCState
*env
, int32_t src
)
139 /* Inexact error possible converting int to float. */
141 clear_float_exceptions(env
);
142 ret
= int32_to_float32(src
, &env
->fp_status
);
143 check_ieee_exceptions(env
);
147 float64
helper_fitod(CPUSPARCState
*env
, int32_t src
)
149 /* No possible exceptions converting int to double. */
150 return int32_to_float64(src
, &env
->fp_status
);
153 void helper_fitoq(CPUSPARCState
*env
, int32_t src
)
155 /* No possible exceptions converting int to long double. */
156 QT0
= int32_to_float128(src
, &env
->fp_status
);
159 #ifdef TARGET_SPARC64
160 float32
helper_fxtos(CPUSPARCState
*env
, int64_t src
)
163 clear_float_exceptions(env
);
164 ret
= int64_to_float32(src
, &env
->fp_status
);
165 check_ieee_exceptions(env
);
169 float64
helper_fxtod(CPUSPARCState
*env
, int64_t src
)
172 clear_float_exceptions(env
);
173 ret
= int64_to_float64(src
, &env
->fp_status
);
174 check_ieee_exceptions(env
);
178 void helper_fxtoq(CPUSPARCState
*env
, int64_t src
)
180 /* No possible exceptions converting long long to long double. */
181 QT0
= int64_to_float128(src
, &env
->fp_status
);
186 /* floating point conversion */
187 float32
helper_fdtos(CPUSPARCState
*env
, float64 src
)
190 clear_float_exceptions(env
);
191 ret
= float64_to_float32(src
, &env
->fp_status
);
192 check_ieee_exceptions(env
);
196 float64
helper_fstod(CPUSPARCState
*env
, float32 src
)
199 clear_float_exceptions(env
);
200 ret
= float32_to_float64(src
, &env
->fp_status
);
201 check_ieee_exceptions(env
);
205 float32
helper_fqtos(CPUSPARCState
*env
)
208 clear_float_exceptions(env
);
209 ret
= float128_to_float32(QT1
, &env
->fp_status
);
210 check_ieee_exceptions(env
);
214 void helper_fstoq(CPUSPARCState
*env
, float32 src
)
216 clear_float_exceptions(env
);
217 QT0
= float32_to_float128(src
, &env
->fp_status
);
218 check_ieee_exceptions(env
);
221 float64
helper_fqtod(CPUSPARCState
*env
)
224 clear_float_exceptions(env
);
225 ret
= float128_to_float64(QT1
, &env
->fp_status
);
226 check_ieee_exceptions(env
);
230 void helper_fdtoq(CPUSPARCState
*env
, float64 src
)
232 clear_float_exceptions(env
);
233 QT0
= float64_to_float128(src
, &env
->fp_status
);
234 check_ieee_exceptions(env
);
237 /* Float to integer conversion. */
238 int32_t helper_fstoi(CPUSPARCState
*env
, float32 src
)
241 clear_float_exceptions(env
);
242 ret
= float32_to_int32_round_to_zero(src
, &env
->fp_status
);
243 check_ieee_exceptions(env
);
247 int32_t helper_fdtoi(CPUSPARCState
*env
, float64 src
)
250 clear_float_exceptions(env
);
251 ret
= float64_to_int32_round_to_zero(src
, &env
->fp_status
);
252 check_ieee_exceptions(env
);
256 int32_t helper_fqtoi(CPUSPARCState
*env
)
259 clear_float_exceptions(env
);
260 ret
= float128_to_int32_round_to_zero(QT1
, &env
->fp_status
);
261 check_ieee_exceptions(env
);
265 #ifdef TARGET_SPARC64
266 int64_t helper_fstox(CPUSPARCState
*env
, float32 src
)
269 clear_float_exceptions(env
);
270 ret
= float32_to_int64_round_to_zero(src
, &env
->fp_status
);
271 check_ieee_exceptions(env
);
275 int64_t helper_fdtox(CPUSPARCState
*env
, float64 src
)
278 clear_float_exceptions(env
);
279 ret
= float64_to_int64_round_to_zero(src
, &env
->fp_status
);
280 check_ieee_exceptions(env
);
284 int64_t helper_fqtox(CPUSPARCState
*env
)
287 clear_float_exceptions(env
);
288 ret
= float128_to_int64_round_to_zero(QT1
, &env
->fp_status
);
289 check_ieee_exceptions(env
);
294 float32
helper_fabss(float32 src
)
296 return float32_abs(src
);
299 #ifdef TARGET_SPARC64
300 float64
helper_fabsd(float64 src
)
302 return float64_abs(src
);
305 void helper_fabsq(CPUSPARCState
*env
)
307 QT0
= float128_abs(QT1
);
311 float32
helper_fsqrts(CPUSPARCState
*env
, float32 src
)
314 clear_float_exceptions(env
);
315 ret
= float32_sqrt(src
, &env
->fp_status
);
316 check_ieee_exceptions(env
);
320 float64
helper_fsqrtd(CPUSPARCState
*env
, float64 src
)
323 clear_float_exceptions(env
);
324 ret
= float64_sqrt(src
, &env
->fp_status
);
325 check_ieee_exceptions(env
);
329 void helper_fsqrtq(CPUSPARCState
*env
)
331 clear_float_exceptions(env
);
332 QT0
= float128_sqrt(QT1
, &env
->fp_status
);
333 check_ieee_exceptions(env
);
336 #define GEN_FCMP(name, size, reg1, reg2, FS, E) \
337 void glue(helper_, name) (CPUSPARCState *env) \
339 env->fsr &= FSR_FTT_NMASK; \
340 if (E && (glue(size, _is_any_nan)(reg1) || \
341 glue(size, _is_any_nan)(reg2)) && \
342 (env->fsr & FSR_NVM)) { \
343 env->fsr |= FSR_NVC; \
344 env->fsr |= FSR_FTT_IEEE_EXCP; \
345 helper_raise_exception(env, TT_FP_EXCP); \
347 switch (glue(size, _compare) (reg1, reg2, &env->fp_status)) { \
348 case float_relation_unordered: \
349 if ((env->fsr & FSR_NVM)) { \
350 env->fsr |= FSR_NVC; \
351 env->fsr |= FSR_FTT_IEEE_EXCP; \
352 helper_raise_exception(env, TT_FP_EXCP); \
354 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
355 env->fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
356 env->fsr |= FSR_NVA; \
359 case float_relation_less: \
360 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
361 env->fsr |= FSR_FCC0 << FS; \
363 case float_relation_greater: \
364 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
365 env->fsr |= FSR_FCC1 << FS; \
368 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
372 #define GEN_FCMP_T(name, size, FS, E) \
373 void glue(helper_, name)(CPUSPARCState *env, size src1, size src2) \
375 env->fsr &= FSR_FTT_NMASK; \
376 if (E && (glue(size, _is_any_nan)(src1) || \
377 glue(size, _is_any_nan)(src2)) && \
378 (env->fsr & FSR_NVM)) { \
379 env->fsr |= FSR_NVC; \
380 env->fsr |= FSR_FTT_IEEE_EXCP; \
381 helper_raise_exception(env, TT_FP_EXCP); \
383 switch (glue(size, _compare) (src1, src2, &env->fp_status)) { \
384 case float_relation_unordered: \
385 if ((env->fsr & FSR_NVM)) { \
386 env->fsr |= FSR_NVC; \
387 env->fsr |= FSR_FTT_IEEE_EXCP; \
388 helper_raise_exception(env, TT_FP_EXCP); \
390 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
391 env->fsr |= (FSR_FCC1 | FSR_FCC0) << FS; \
392 env->fsr |= FSR_NVA; \
395 case float_relation_less: \
396 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
397 env->fsr |= FSR_FCC0 << FS; \
399 case float_relation_greater: \
400 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
401 env->fsr |= FSR_FCC1 << FS; \
404 env->fsr &= ~((FSR_FCC1 | FSR_FCC0) << FS); \
409 GEN_FCMP_T(fcmps
, float32
, 0, 0);
410 GEN_FCMP_T(fcmpd
, float64
, 0, 0);
412 GEN_FCMP_T(fcmpes
, float32
, 0, 1);
413 GEN_FCMP_T(fcmped
, float64
, 0, 1);
415 GEN_FCMP(fcmpq
, float128
, QT0
, QT1
, 0, 0);
416 GEN_FCMP(fcmpeq
, float128
, QT0
, QT1
, 0, 1);
418 #ifdef TARGET_SPARC64
419 GEN_FCMP_T(fcmps_fcc1
, float32
, 22, 0);
420 GEN_FCMP_T(fcmpd_fcc1
, float64
, 22, 0);
421 GEN_FCMP(fcmpq_fcc1
, float128
, QT0
, QT1
, 22, 0);
423 GEN_FCMP_T(fcmps_fcc2
, float32
, 24, 0);
424 GEN_FCMP_T(fcmpd_fcc2
, float64
, 24, 0);
425 GEN_FCMP(fcmpq_fcc2
, float128
, QT0
, QT1
, 24, 0);
427 GEN_FCMP_T(fcmps_fcc3
, float32
, 26, 0);
428 GEN_FCMP_T(fcmpd_fcc3
, float64
, 26, 0);
429 GEN_FCMP(fcmpq_fcc3
, float128
, QT0
, QT1
, 26, 0);
431 GEN_FCMP_T(fcmpes_fcc1
, float32
, 22, 1);
432 GEN_FCMP_T(fcmped_fcc1
, float64
, 22, 1);
433 GEN_FCMP(fcmpeq_fcc1
, float128
, QT0
, QT1
, 22, 1);
435 GEN_FCMP_T(fcmpes_fcc2
, float32
, 24, 1);
436 GEN_FCMP_T(fcmped_fcc2
, float64
, 24, 1);
437 GEN_FCMP(fcmpeq_fcc2
, float128
, QT0
, QT1
, 24, 1);
439 GEN_FCMP_T(fcmpes_fcc3
, float32
, 26, 1);
440 GEN_FCMP_T(fcmped_fcc3
, float64
, 26, 1);
441 GEN_FCMP(fcmpeq_fcc3
, float128
, QT0
, QT1
, 26, 1);
446 static inline void set_fsr(CPUSPARCState
*env
)
450 switch (env
->fsr
& FSR_RD_MASK
) {
452 rnd_mode
= float_round_nearest_even
;
456 rnd_mode
= float_round_to_zero
;
459 rnd_mode
= float_round_up
;
462 rnd_mode
= float_round_down
;
465 set_float_rounding_mode(rnd_mode
, &env
->fp_status
);
468 void helper_ldfsr(CPUSPARCState
*env
, uint32_t new_fsr
)
470 env
->fsr
= (new_fsr
& FSR_LDFSR_MASK
) | (env
->fsr
& FSR_LDFSR_OLDMASK
);
474 #ifdef TARGET_SPARC64
475 void helper_ldxfsr(CPUSPARCState
*env
, uint64_t new_fsr
)
477 env
->fsr
= (new_fsr
& FSR_LDXFSR_MASK
) | (env
->fsr
& FSR_LDXFSR_OLDMASK
);