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/>.
22 #include "exec/cpu_ldst.h"
23 #include "exec/helper-proto.h"
25 /* #define DEBUG_HELPER */
27 #define HELPER_LOG(x...) qemu_log(x)
29 #define HELPER_LOG(x...)
32 #define RET128(F) (env->retxl = F.low, F.high)
34 #define convert_bit(mask, from, to) \
36 ? (mask / (from / to)) & to \
37 : (mask & from) * (to / from))
39 static void ieee_exception(CPUS390XState *env, uint32_t dxc, uintptr_t retaddr)
41 /* Install the DXC code. */
42 env->fpc = (env->fpc & ~0xff00) | (dxc << 8);
44 runtime_exception(env, PGM_DATA, retaddr);
47 /* Should be called after any operation that may raise IEEE exceptions. */
48 static void handle_exceptions(CPUS390XState *env, uintptr_t retaddr)
50 unsigned s390_exc, qemu_exc;
52 /* Get the exceptions raised by the current operation. Reset the
53 fpu_status contents so that the next operation has a clean slate. */
54 qemu_exc = env->fpu_status.float_exception_flags;
58 env->fpu_status.float_exception_flags = 0;
60 /* Convert softfloat exception bits to s390 exception bits. */
62 s390_exc |= convert_bit(qemu_exc, float_flag_invalid, 0x80);
63 s390_exc |= convert_bit(qemu_exc, float_flag_divbyzero, 0x40);
64 s390_exc |= convert_bit(qemu_exc, float_flag_overflow, 0x20);
65 s390_exc |= convert_bit(qemu_exc, float_flag_underflow, 0x10);
66 s390_exc |= convert_bit(qemu_exc, float_flag_inexact, 0x08);
68 /* Install the exceptions that we raised. */
69 env->fpc |= s390_exc << 16;
71 /* Send signals for enabled exceptions. */
72 s390_exc &= env->fpc >> 24;
74 ieee_exception(env, s390_exc, retaddr);
78 static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
80 S390CPU *cpu = s390_env_get_cpu(env);
82 switch (float_compare) {
83 case float_relation_equal:
85 case float_relation_less:
87 case float_relation_greater:
89 case float_relation_unordered:
92 cpu_abort(CPU(cpu), "unknown return value for float compare\n");
96 /* condition codes for unary FP ops */
97 uint32_t set_cc_nz_f32(float32 v)
99 if (float32_is_any_nan(v)) {
101 } else if (float32_is_zero(v)) {
103 } else if (float32_is_neg(v)) {
110 uint32_t set_cc_nz_f64(float64 v)
112 if (float64_is_any_nan(v)) {
114 } else if (float64_is_zero(v)) {
116 } else if (float64_is_neg(v)) {
123 uint32_t set_cc_nz_f128(float128 v)
125 if (float128_is_any_nan(v)) {
127 } else if (float128_is_zero(v)) {
129 } else if (float128_is_neg(v)) {
136 /* 32-bit FP addition */
137 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
139 float32 ret = float32_add(f1, f2, &env->fpu_status);
140 handle_exceptions(env, GETPC());
144 /* 64-bit FP addition */
145 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
147 float64 ret = float64_add(f1, f2, &env->fpu_status);
148 handle_exceptions(env, GETPC());
152 /* 128-bit FP addition */
153 uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
154 uint64_t bh, uint64_t bl)
156 float128 ret = float128_add(make_float128(ah, al),
157 make_float128(bh, bl),
159 handle_exceptions(env, GETPC());
163 /* 32-bit FP subtraction */
164 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
166 float32 ret = float32_sub(f1, f2, &env->fpu_status);
167 handle_exceptions(env, GETPC());
171 /* 64-bit FP subtraction */
172 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
174 float64 ret = float64_sub(f1, f2, &env->fpu_status);
175 handle_exceptions(env, GETPC());
179 /* 128-bit FP subtraction */
180 uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
181 uint64_t bh, uint64_t bl)
183 float128 ret = float128_sub(make_float128(ah, al),
184 make_float128(bh, bl),
186 handle_exceptions(env, GETPC());
190 /* 32-bit FP division */
191 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
193 float32 ret = float32_div(f1, f2, &env->fpu_status);
194 handle_exceptions(env, GETPC());
198 /* 64-bit FP division */
199 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
201 float64 ret = float64_div(f1, f2, &env->fpu_status);
202 handle_exceptions(env, GETPC());
206 /* 128-bit FP division */
207 uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
208 uint64_t bh, uint64_t bl)
210 float128 ret = float128_div(make_float128(ah, al),
211 make_float128(bh, bl),
213 handle_exceptions(env, GETPC());
217 /* 32-bit FP multiplication */
218 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
220 float32 ret = float32_mul(f1, f2, &env->fpu_status);
221 handle_exceptions(env, GETPC());
225 /* 64-bit FP multiplication */
226 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
228 float64 ret = float64_mul(f1, f2, &env->fpu_status);
229 handle_exceptions(env, GETPC());
233 /* 64/32-bit FP multiplication */
234 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
236 float64 ret = float32_to_float64(f2, &env->fpu_status);
237 ret = float64_mul(f1, ret, &env->fpu_status);
238 handle_exceptions(env, GETPC());
242 /* 128-bit FP multiplication */
243 uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
244 uint64_t bh, uint64_t bl)
246 float128 ret = float128_mul(make_float128(ah, al),
247 make_float128(bh, bl),
249 handle_exceptions(env, GETPC());
253 /* 128/64-bit FP multiplication */
254 uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
257 float128 ret = float64_to_float128(f2, &env->fpu_status);
258 ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
259 handle_exceptions(env, GETPC());
263 /* convert 32-bit float to 64-bit float */
264 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
266 float64 ret = float32_to_float64(f2, &env->fpu_status);
267 handle_exceptions(env, GETPC());
268 return float64_maybe_silence_nan(ret);
271 /* convert 128-bit float to 64-bit float */
272 uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
274 float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
275 handle_exceptions(env, GETPC());
276 return float64_maybe_silence_nan(ret);
279 /* convert 64-bit float to 128-bit float */
280 uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
282 float128 ret = float64_to_float128(f2, &env->fpu_status);
283 handle_exceptions(env, GETPC());
284 return RET128(float128_maybe_silence_nan(ret));
287 /* convert 32-bit float to 128-bit float */
288 uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
290 float128 ret = float32_to_float128(f2, &env->fpu_status);
291 handle_exceptions(env, GETPC());
292 return RET128(float128_maybe_silence_nan(ret));
295 /* convert 64-bit float to 32-bit float */
296 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2)
298 float32 ret = float64_to_float32(f2, &env->fpu_status);
299 handle_exceptions(env, GETPC());
300 return float32_maybe_silence_nan(ret);
303 /* convert 128-bit float to 32-bit float */
304 uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al)
306 float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
307 handle_exceptions(env, GETPC());
308 return float32_maybe_silence_nan(ret);
311 /* 32-bit FP compare */
312 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
314 int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
315 handle_exceptions(env, GETPC());
316 return float_comp_to_cc(env, cmp);
319 /* 64-bit FP compare */
320 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
322 int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
323 handle_exceptions(env, GETPC());
324 return float_comp_to_cc(env, cmp);
327 /* 128-bit FP compare */
328 uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
329 uint64_t bh, uint64_t bl)
331 int cmp = float128_compare_quiet(make_float128(ah, al),
332 make_float128(bh, bl),
334 handle_exceptions(env, GETPC());
335 return float_comp_to_cc(env, cmp);
338 static int swap_round_mode(CPUS390XState *env, int m3)
340 int ret = env->fpu_status.float_rounding_mode;
346 /* biased round no nearest */
348 /* round to nearest */
349 set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
353 set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
357 set_float_rounding_mode(float_round_up, &env->fpu_status);
361 set_float_rounding_mode(float_round_down, &env->fpu_status);
367 /* convert 64-bit int to 32-bit float */
368 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m3)
370 int hold = swap_round_mode(env, m3);
371 float32 ret = int64_to_float32(v2, &env->fpu_status);
372 set_float_rounding_mode(hold, &env->fpu_status);
373 handle_exceptions(env, GETPC());
377 /* convert 64-bit int to 64-bit float */
378 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
380 int hold = swap_round_mode(env, m3);
381 float64 ret = int64_to_float64(v2, &env->fpu_status);
382 set_float_rounding_mode(hold, &env->fpu_status);
383 handle_exceptions(env, GETPC());
387 /* convert 64-bit int to 128-bit float */
388 uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m3)
390 int hold = swap_round_mode(env, m3);
391 float128 ret = int64_to_float128(v2, &env->fpu_status);
392 set_float_rounding_mode(hold, &env->fpu_status);
393 handle_exceptions(env, GETPC());
397 /* convert 64-bit uint to 32-bit float */
398 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
400 int hold = swap_round_mode(env, m3);
401 float32 ret = uint64_to_float32(v2, &env->fpu_status);
402 set_float_rounding_mode(hold, &env->fpu_status);
403 handle_exceptions(env, GETPC());
407 /* convert 64-bit uint to 64-bit float */
408 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
410 int hold = swap_round_mode(env, m3);
411 float64 ret = uint64_to_float64(v2, &env->fpu_status);
412 set_float_rounding_mode(hold, &env->fpu_status);
413 handle_exceptions(env, GETPC());
417 /* convert 64-bit uint to 128-bit float */
418 uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
420 int hold = swap_round_mode(env, m3);
421 float128 ret = uint64_to_float128(v2, &env->fpu_status);
422 set_float_rounding_mode(hold, &env->fpu_status);
423 handle_exceptions(env, GETPC());
427 /* convert 32-bit float to 64-bit int */
428 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
430 int hold = swap_round_mode(env, m3);
431 int64_t ret = float32_to_int64(v2, &env->fpu_status);
432 set_float_rounding_mode(hold, &env->fpu_status);
433 handle_exceptions(env, GETPC());
437 /* convert 64-bit float to 64-bit int */
438 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
440 int hold = swap_round_mode(env, m3);
441 int64_t ret = float64_to_int64(v2, &env->fpu_status);
442 set_float_rounding_mode(hold, &env->fpu_status);
443 handle_exceptions(env, GETPC());
447 /* convert 128-bit float to 64-bit int */
448 uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
450 int hold = swap_round_mode(env, m3);
451 float128 v2 = make_float128(h, l);
452 int64_t ret = float128_to_int64(v2, &env->fpu_status);
453 set_float_rounding_mode(hold, &env->fpu_status);
454 handle_exceptions(env, GETPC());
458 /* convert 32-bit float to 32-bit int */
459 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
461 int hold = swap_round_mode(env, m3);
462 int32_t ret = float32_to_int32(v2, &env->fpu_status);
463 set_float_rounding_mode(hold, &env->fpu_status);
464 handle_exceptions(env, GETPC());
468 /* convert 64-bit float to 32-bit int */
469 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
471 int hold = swap_round_mode(env, m3);
472 int32_t ret = float64_to_int32(v2, &env->fpu_status);
473 set_float_rounding_mode(hold, &env->fpu_status);
474 handle_exceptions(env, GETPC());
478 /* convert 128-bit float to 32-bit int */
479 uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
481 int hold = swap_round_mode(env, m3);
482 float128 v2 = make_float128(h, l);
483 int32_t ret = float128_to_int32(v2, &env->fpu_status);
484 set_float_rounding_mode(hold, &env->fpu_status);
485 handle_exceptions(env, GETPC());
489 /* convert 32-bit float to 64-bit uint */
490 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
492 int hold = swap_round_mode(env, m3);
494 v2 = float32_to_float64(v2, &env->fpu_status);
495 ret = float64_to_uint64(v2, &env->fpu_status);
496 set_float_rounding_mode(hold, &env->fpu_status);
497 handle_exceptions(env, GETPC());
501 /* convert 64-bit float to 64-bit uint */
502 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
504 int hold = swap_round_mode(env, m3);
505 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
506 set_float_rounding_mode(hold, &env->fpu_status);
507 handle_exceptions(env, GETPC());
511 /* convert 128-bit float to 64-bit uint */
512 uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
514 int hold = swap_round_mode(env, m3);
515 float128 v2 = make_float128(h, l);
516 /* ??? Not 100% correct. */
517 uint64_t ret = float128_to_int64(v2, &env->fpu_status);
518 set_float_rounding_mode(hold, &env->fpu_status);
519 handle_exceptions(env, GETPC());
523 /* convert 32-bit float to 32-bit uint */
524 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
526 int hold = swap_round_mode(env, m3);
527 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
528 set_float_rounding_mode(hold, &env->fpu_status);
529 handle_exceptions(env, GETPC());
533 /* convert 64-bit float to 32-bit uint */
534 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m3)
536 int hold = swap_round_mode(env, m3);
537 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
538 set_float_rounding_mode(hold, &env->fpu_status);
539 handle_exceptions(env, GETPC());
543 /* convert 128-bit float to 32-bit uint */
544 uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m3)
546 int hold = swap_round_mode(env, m3);
547 float128 v2 = make_float128(h, l);
548 /* Not 100% correct. */
549 uint32_t ret = float128_to_int64(v2, &env->fpu_status);
550 set_float_rounding_mode(hold, &env->fpu_status);
551 handle_exceptions(env, GETPC());
555 /* round to integer 32-bit */
556 uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m3)
558 int hold = swap_round_mode(env, m3);
559 float32 ret = float32_round_to_int(f2, &env->fpu_status);
560 set_float_rounding_mode(hold, &env->fpu_status);
561 handle_exceptions(env, GETPC());
565 /* round to integer 64-bit */
566 uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m3)
568 int hold = swap_round_mode(env, m3);
569 float64 ret = float64_round_to_int(f2, &env->fpu_status);
570 set_float_rounding_mode(hold, &env->fpu_status);
571 handle_exceptions(env, GETPC());
575 /* round to integer 128-bit */
576 uint64_t HELPER(fixb)(CPUS390XState *env, uint64_t ah, uint64_t al, uint32_t m3)
578 int hold = swap_round_mode(env, m3);
579 float128 ret = float128_round_to_int(make_float128(ah, al),
581 set_float_rounding_mode(hold, &env->fpu_status);
582 handle_exceptions(env, GETPC());
586 /* 32-bit FP multiply and add */
587 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
588 uint64_t f2, uint64_t f3)
590 float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
591 handle_exceptions(env, GETPC());
595 /* 64-bit FP multiply and add */
596 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
597 uint64_t f2, uint64_t f3)
599 float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
600 handle_exceptions(env, GETPC());
604 /* 32-bit FP multiply and subtract */
605 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
606 uint64_t f2, uint64_t f3)
608 float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
610 handle_exceptions(env, GETPC());
614 /* 64-bit FP multiply and subtract */
615 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
616 uint64_t f2, uint64_t f3)
618 float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
620 handle_exceptions(env, GETPC());
624 /* test data class 32-bit */
625 uint32_t HELPER(tceb)(uint64_t f1, uint64_t m2)
628 int neg = float32_is_neg(v1);
631 if ((float32_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
632 (float32_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
633 (float32_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
634 (float32_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
636 } else if (m2 & (1 << (9-neg))) {
637 /* assume normalized number */
640 /* FIXME: denormalized? */
644 /* test data class 64-bit */
645 uint32_t HELPER(tcdb)(uint64_t v1, uint64_t m2)
647 int neg = float64_is_neg(v1);
650 if ((float64_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
651 (float64_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
652 (float64_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
653 (float64_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
655 } else if (m2 & (1 << (9-neg))) {
656 /* assume normalized number */
659 /* FIXME: denormalized? */
663 /* test data class 128-bit */
664 uint32_t HELPER(tcxb)(uint64_t ah, uint64_t al, uint64_t m2)
666 float128 v1 = make_float128(ah, al);
667 int neg = float128_is_neg(v1);
670 if ((float128_is_zero(v1) && (m2 & (1 << (11-neg)))) ||
671 (float128_is_infinity(v1) && (m2 & (1 << (5-neg)))) ||
672 (float128_is_any_nan(v1) && (m2 & (1 << (3-neg)))) ||
673 (float128_is_signaling_nan(v1) && (m2 & (1 << (1-neg))))) {
675 } else if (m2 & (1 << (9-neg))) {
676 /* assume normalized number */
679 /* FIXME: denormalized? */
683 /* square root 32-bit */
684 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
686 float32 ret = float32_sqrt(f2, &env->fpu_status);
687 handle_exceptions(env, GETPC());
691 /* square root 64-bit */
692 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
694 float64 ret = float64_sqrt(f2, &env->fpu_status);
695 handle_exceptions(env, GETPC());
699 /* square root 128-bit */
700 uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
702 float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
703 handle_exceptions(env, GETPC());
707 static const int fpc_to_rnd[4] = {
708 float_round_nearest_even,
715 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
717 /* Install everything in the main FPC. */
720 /* Install the rounding mode in the shadow fpu_status. */
721 set_float_rounding_mode(fpc_to_rnd[fpc & 3], &env->fpu_status);
724 /* set fpc and signal */
725 void HELPER(sfas)(CPUS390XState *env, uint64_t val)
727 uint32_t signalling = env->fpc;
728 uint32_t source = val;
731 /* The contents of the source operand are placed in the FPC register;
732 then the flags in the FPC register are set to the logical OR of the
733 signalling flags and the source flags. */
734 env->fpc = source | (signalling & 0x00ff0000);
735 set_float_rounding_mode(fpc_to_rnd[source & 3], &env->fpu_status);
737 /* If any signalling flag is 1 and the corresponding source mask
738 is also 1, a simulated-iee-exception trap occurs. */
739 s390_exc = (signalling >> 16) & (source >> 24);
741 ieee_exception(env, s390_exc | 3, GETPC());