2 * S/390 FPU helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #if !defined(CONFIG_USER_ONLY)
25 #include "exec/softmmu_exec.h"
28 /* #define DEBUG_HELPER */
30 #define HELPER_LOG(x...) qemu_log(x)
32 #define HELPER_LOG(x...)
35 #define RET128(F) (env->retxl = F.low, F.high)
37 #define convert_bit(mask, from, to) \
39 ? (mask / (from / to)) & to \
40 : (mask & from) * (to / from))
42 static void ieee_exception(CPUS390XState *env, uint32_t dxc, uintptr_t retaddr)
44 /* Install the DXC code. */
45 env->fpc = (env->fpc & ~0xff00) | (dxc << 8);
47 runtime_exception(env, PGM_DATA, retaddr);
50 /* Should be called after any operation that may raise IEEE exceptions. */
51 static void handle_exceptions(CPUS390XState *env, uintptr_t retaddr)
53 unsigned s390_exc, qemu_exc;
55 /* Get the exceptions raised by the current operation. Reset the
56 fpu_status contents so that the next operation has a clean slate. */
57 qemu_exc = env->fpu_status.float_exception_flags;
61 env->fpu_status.float_exception_flags = 0;
63 /* Convert softfloat exception bits to s390 exception bits. */
65 s390_exc |= convert_bit(qemu_exc, float_flag_invalid, 0x80);
66 s390_exc |= convert_bit(qemu_exc, float_flag_divbyzero, 0x40);
67 s390_exc |= convert_bit(qemu_exc, float_flag_overflow, 0x20);
68 s390_exc |= convert_bit(qemu_exc, float_flag_underflow, 0x10);
69 s390_exc |= convert_bit(qemu_exc, float_flag_inexact, 0x08);
71 /* Install the exceptions that we raised. */
72 env->fpc |= s390_exc << 16;
74 /* Send signals for enabled exceptions. */
75 s390_exc &= env->fpc >> 24;
77 ieee_exception(env, s390_exc, retaddr);
81 static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
83 S390CPU *cpu = s390_env_get_cpu(env);
85 switch (float_compare) {
86 case float_relation_equal:
88 case float_relation_less:
90 case float_relation_greater:
92 case float_relation_unordered:
95 cpu_abort(CPU(cpu), "unknown return value for float compare\n");
99 /* condition codes for unary FP ops */
100 uint32_t set_cc_nz_f32(float32 v)
102 if (float32_is_any_nan(v)) {
104 } else if (float32_is_zero(v)) {
106 } else if (float32_is_neg(v)) {
113 uint32_t set_cc_nz_f64(float64 v)
115 if (float64_is_any_nan(v)) {
117 } else if (float64_is_zero(v)) {
119 } else if (float64_is_neg(v)) {
126 uint32_t set_cc_nz_f128(float128 v)
128 if (float128_is_any_nan(v)) {
130 } else if (float128_is_zero(v)) {
132 } else if (float128_is_neg(v)) {
139 /* 32-bit FP addition */
140 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
142 float32 ret = float32_add(f1, f2, &env->fpu_status);
143 handle_exceptions(env, GETPC());
147 /* 64-bit FP addition */
148 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
150 float64 ret = float64_add(f1, f2, &env->fpu_status);
151 handle_exceptions(env, GETPC());
155 /* 128-bit FP addition */
156 uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
157 uint64_t bh, uint64_t bl)
159 float128 ret = float128_add(make_float128(ah, al),
160 make_float128(bh, bl),
162 handle_exceptions(env, GETPC());
166 /* 32-bit FP subtraction */
167 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
169 float32 ret = float32_sub(f1, f2, &env->fpu_status);
170 handle_exceptions(env, GETPC());
174 /* 64-bit FP subtraction */
175 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
177 float64 ret = float64_sub(f1, f2, &env->fpu_status);
178 handle_exceptions(env, GETPC());
182 /* 128-bit FP subtraction */
183 uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
184 uint64_t bh, uint64_t bl)
186 float128 ret = float128_sub(make_float128(ah, al),
187 make_float128(bh, bl),
189 handle_exceptions(env, GETPC());
193 /* 32-bit FP division */
194 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
196 float32 ret = float32_div(f1, f2, &env->fpu_status);
197 handle_exceptions(env, GETPC());
201 /* 64-bit FP division */
202 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
204 float64 ret = float64_div(f1, f2, &env->fpu_status);
205 handle_exceptions(env, GETPC());
209 /* 128-bit FP division */
210 uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
211 uint64_t bh, uint64_t bl)
213 float128 ret = float128_div(make_float128(ah, al),
214 make_float128(bh, bl),
216 handle_exceptions(env, GETPC());
220 /* 32-bit FP multiplication */
221 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
223 float32 ret = float32_mul(f1, f2, &env->fpu_status);
224 handle_exceptions(env, GETPC());
228 /* 64-bit FP multiplication */
229 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
231 float64 ret = float64_mul(f1, f2, &env->fpu_status);
232 handle_exceptions(env, GETPC());
236 /* 64/32-bit FP multiplication */
237 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
239 float64 ret = float32_to_float64(f2, &env->fpu_status);
240 ret = float64_mul(f1, ret, &env->fpu_status);
241 handle_exceptions(env, GETPC());
245 /* 128-bit FP multiplication */
246 uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
247 uint64_t bh, uint64_t bl)
249 float128 ret = float128_mul(make_float128(ah, al),
250 make_float128(bh, bl),
252 handle_exceptions(env, GETPC());
256 /* 128/64-bit FP multiplication */
257 uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
260 float128 ret = float64_to_float128(f2, &env->fpu_status);
261 ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
262 handle_exceptions(env, GETPC());
266 /* convert 32-bit float to 64-bit float */
267 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
269 float64 ret = float32_to_float64(f2, &env->fpu_status);
270 handle_exceptions(env, GETPC());
274 /* convert 128-bit float to 64-bit float */
275 uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
277 float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
278 handle_exceptions(env, GETPC());
282 /* convert 64-bit float to 128-bit float */
283 uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
285 float128 ret = float64_to_float128(f2, &env->fpu_status);
286 handle_exceptions(env, GETPC());
290 /* convert 32-bit float to 128-bit float */
291 uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
293 float128 ret = float32_to_float128(f2, &env->fpu_status);
294 handle_exceptions(env, GETPC());
298 /* convert 64-bit float to 32-bit float */
299 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2)
301 float32 ret = float64_to_float32(f2, &env->fpu_status);
302 handle_exceptions(env, GETPC());
306 /* convert 128-bit float to 32-bit float */
307 uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al)
309 float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
310 handle_exceptions(env, GETPC());
314 /* 32-bit FP compare */
315 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
317 int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
318 handle_exceptions(env, GETPC());
319 return float_comp_to_cc(env, cmp);
322 /* 64-bit FP compare */
323 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
325 int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
326 handle_exceptions(env, GETPC());
327 return float_comp_to_cc(env, cmp);
330 /* 128-bit FP compare */
331 uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
332 uint64_t bh, uint64_t bl)
334 int cmp = float128_compare_quiet(make_float128(ah, al),
335 make_float128(bh, bl),
337 handle_exceptions(env, GETPC());
338 return float_comp_to_cc(env, cmp);
341 static int swap_round_mode(CPUS390XState *env, int m3)
343 int ret = env->fpu_status.float_rounding_mode;
349 /* biased round no nearest */
351 /* round to nearest */
352 set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
356 set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
360 set_float_rounding_mode(float_round_up, &env->fpu_status);
364 set_float_rounding_mode(float_round_down, &env->fpu_status);
370 /* convert 64-bit int to 32-bit float */
371 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m3)
373 int hold = swap_round_mode(env, m3);
374 float32 ret = int64_to_float32(v2, &env->fpu_status);
375 set_float_rounding_mode(hold, &env->fpu_status);
376 handle_exceptions(env, GETPC());
380 /* convert 64-bit int to 64-bit float */
381 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
383 int hold = swap_round_mode(env, m3);
384 float64 ret = int64_to_float64(v2, &env->fpu_status);
385 set_float_rounding_mode(hold, &env->fpu_status);
386 handle_exceptions(env, GETPC());
390 /* convert 64-bit int to 128-bit float */
391 uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
393 int hold = swap_round_mode(env, m3);
394 float128 ret = int64_to_float128(v2, &env->fpu_status);
395 set_float_rounding_mode(hold, &env->fpu_status);
396 handle_exceptions(env, GETPC());
400 /* convert 64-bit uint to 32-bit float */
401 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
403 int hold = swap_round_mode(env, m3);
404 float32 ret = uint64_to_float32(v2, &env->fpu_status);
405 set_float_rounding_mode(hold, &env->fpu_status);
406 handle_exceptions(env, GETPC());
410 /* convert 64-bit uint to 64-bit float */
411 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
413 int hold = swap_round_mode(env, m3);
414 float64 ret = uint64_to_float64(v2, &env->fpu_status);
415 set_float_rounding_mode(hold, &env->fpu_status);
416 handle_exceptions(env, GETPC());
420 /* convert 64-bit uint to 128-bit float */
421 uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
423 int hold = swap_round_mode(env, m3);
424 float128 ret = uint64_to_float128(v2, &env->fpu_status);
425 set_float_rounding_mode(hold, &env->fpu_status);
426 handle_exceptions(env, GETPC());
430 /* convert 32-bit float to 64-bit int */
431 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
433 int hold = swap_round_mode(env, m3);
434 int64_t ret = float32_to_int64(v2, &env->fpu_status);
435 set_float_rounding_mode(hold, &env->fpu_status);
436 handle_exceptions(env, GETPC());
440 /* convert 64-bit float to 64-bit int */
441 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
443 int hold = swap_round_mode(env, m3);
444 int64_t ret = float64_to_int64(v2, &env->fpu_status);
445 set_float_rounding_mode(hold, &env->fpu_status);
446 handle_exceptions(env, GETPC());
450 /* convert 128-bit float to 64-bit int */
451 uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
453 int hold = swap_round_mode(env, m3);
454 float128 v2 = make_float128(h, l);
455 int64_t ret = float128_to_int64(v2, &env->fpu_status);
456 set_float_rounding_mode(hold, &env->fpu_status);
457 handle_exceptions(env, GETPC());
461 /* convert 32-bit float to 32-bit int */
462 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
464 int hold = swap_round_mode(env, m3);
465 int32_t ret = float32_to_int32(v2, &env->fpu_status);
466 set_float_rounding_mode(hold, &env->fpu_status);
467 handle_exceptions(env, GETPC());
471 /* convert 64-bit float to 32-bit int */
472 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
474 int hold = swap_round_mode(env, m3);
475 int32_t ret = float64_to_int32(v2, &env->fpu_status);
476 set_float_rounding_mode(hold, &env->fpu_status);
477 handle_exceptions(env, GETPC());
481 /* convert 128-bit float to 32-bit int */
482 uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
484 int hold = swap_round_mode(env, m3);
485 float128 v2 = make_float128(h, l);
486 int32_t ret = float128_to_int32(v2, &env->fpu_status);
487 set_float_rounding_mode(hold, &env->fpu_status);
488 handle_exceptions(env, GETPC());
492 /* convert 32-bit float to 64-bit uint */
493 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
495 int hold = swap_round_mode(env, m3);
497 v2 = float32_to_float64(v2, &env->fpu_status);
498 ret = float64_to_uint64(v2, &env->fpu_status);
499 set_float_rounding_mode(hold, &env->fpu_status);
500 handle_exceptions(env, GETPC());
504 /* convert 64-bit float to 64-bit uint */
505 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
507 int hold = swap_round_mode(env, m3);
508 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
509 set_float_rounding_mode(hold, &env->fpu_status);
510 handle_exceptions(env, GETPC());
514 /* convert 128-bit float to 64-bit uint */
515 uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
517 int hold = swap_round_mode(env, m3);
518 float128 v2 = make_float128(h, l);
519 /* ??? Not 100% correct. */
520 uint64_t ret = float128_to_int64(v2, &env->fpu_status);
521 set_float_rounding_mode(hold, &env->fpu_status);
522 handle_exceptions(env, GETPC());
526 /* convert 32-bit float to 32-bit uint */
527 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
529 int hold = swap_round_mode(env, m3);
530 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
531 set_float_rounding_mode(hold, &env->fpu_status);
532 handle_exceptions(env, GETPC());
536 /* convert 64-bit float to 32-bit uint */
537 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
539 int hold = swap_round_mode(env, m3);
540 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
541 set_float_rounding_mode(hold, &env->fpu_status);
542 handle_exceptions(env, GETPC());
546 /* convert 128-bit float to 32-bit uint */
547 uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
549 int hold = swap_round_mode(env, m3);
550 float128 v2 = make_float128(h, l);
551 /* Not 100% correct. */
552 uint32_t ret = float128_to_int64(v2, &env->fpu_status);
553 set_float_rounding_mode(hold, &env->fpu_status);
554 handle_exceptions(env, GETPC());
558 /* 32-bit FP multiply and add */
559 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
560 uint64_t f2, uint64_t f3)
562 float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
563 handle_exceptions(env, GETPC());
567 /* 64-bit FP multiply and add */
568 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
569 uint64_t f2, uint64_t f3)
571 float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
572 handle_exceptions(env, GETPC());
576 /* 32-bit FP multiply and subtract */
577 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
578 uint64_t f2, uint64_t f3)
580 float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
582 handle_exceptions(env, GETPC());
586 /* 64-bit FP multiply and subtract */
587 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
588 uint64_t f2, uint64_t f3)
590 float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
592 handle_exceptions(env, GETPC());
596 /* test data class 32-bit */
597 uint32_t HELPER(tceb)(uint64_t f1, uint64_t m2)
600 int neg = float32_is_neg(v1);
603 if ((float32_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
604 (float32_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
605 (float32_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
606 (float32_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
608 } else if (m2 & (1 << (9-neg))) {
609 /* assume normalized number */
612 /* FIXME: denormalized? */
616 /* test data class 64-bit */
617 uint32_t HELPER(tcdb)(uint64_t v1, uint64_t m2)
619 int neg = float64_is_neg(v1);
622 if ((float64_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
623 (float64_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
624 (float64_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
625 (float64_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
627 } else if (m2 & (1 << (9-neg))) {
628 /* assume normalized number */
631 /* FIXME: denormalized? */
635 /* test data class 128-bit */
636 uint32_t HELPER(tcxb)(uint64_t ah, uint64_t al, uint64_t m2)
638 float128 v1 = make_float128(ah, al);
639 int neg = float128_is_neg(v1);
642 if ((float128_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
643 (float128_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
644 (float128_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
645 (float128_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
647 } else if (m2 & (1 << (9-neg))) {
648 /* assume normalized number */
651 /* FIXME: denormalized? */
655 /* square root 32-bit */
656 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
658 float32 ret = float32_sqrt(f2, &env->fpu_status);
659 handle_exceptions(env, GETPC());
663 /* square root 64-bit */
664 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
666 float64 ret = float64_sqrt(f2, &env->fpu_status);
667 handle_exceptions(env, GETPC());
671 /* square root 128-bit */
672 uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
674 float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
675 handle_exceptions(env, GETPC());
679 static const int fpc_to_rnd[4] = {
680 float_round_nearest_even,
687 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
689 /* Install everything in the main FPC. */
692 /* Install the rounding mode in the shadow fpu_status. */
693 set_float_rounding_mode(fpc_to_rnd[fpc & 3], &env->fpu_status);
696 /* set fpc and signal */
697 void HELPER(sfas)(CPUS390XState *env, uint64_t val)
699 uint32_t signalling = env->fpc;
700 uint32_t source = val;
703 /* The contents of the source operand are placed in the FPC register;
704 then the flags in the FPC register are set to the logical OR of the
705 signalling flags and the source flags. */
706 env->fpc = source | (signalling & 0x00ff0000);
707 set_float_rounding_mode(fpc_to_rnd[source & 3], &env->fpu_status);
709 /* If any signalling flag is 1 and the corresponding source mask
710 is also 1, a simulated-iee-exception trap occurs. */
711 s390_exc = (signalling >> 16) & (source >> 24);
713 ieee_exception(env, s390_exc | 3, GETPC());