s390x: spelling fixes
[qemu/ar7.git] / target / s390x / tcg / fpu_helper.c
blobc329b31261789a651c565497e7ebcfcd4c6b59d9
1 /*
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.1 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/>.
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "s390x-internal.h"
24 #include "tcg_s390x.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
28 #include "fpu/softfloat.h"
30 /* #define DEBUG_HELPER */
31 #ifdef DEBUG_HELPER
32 #define HELPER_LOG(x...) qemu_log(x)
33 #else
34 #define HELPER_LOG(x...)
35 #endif
37 static inline Int128 RET128(float128 f)
39 return int128_make128(f.low, f.high);
42 static inline float128 ARG128(Int128 i)
44 return make_float128(int128_gethi(i), int128_getlo(i));
47 uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
49 uint8_t s390_exc = 0;
51 s390_exc |= (exc & float_flag_invalid) ? S390_IEEE_MASK_INVALID : 0;
52 s390_exc |= (exc & float_flag_divbyzero) ? S390_IEEE_MASK_DIVBYZERO : 0;
53 s390_exc |= (exc & float_flag_overflow) ? S390_IEEE_MASK_OVERFLOW : 0;
54 s390_exc |= (exc & float_flag_underflow) ? S390_IEEE_MASK_UNDERFLOW : 0;
55 s390_exc |= (exc & (float_flag_inexact | float_flag_invalid_cvti)) ?
56 S390_IEEE_MASK_INEXACT : 0;
58 return s390_exc;
61 /* Should be called after any operation that may raise IEEE exceptions. */
62 static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
64 unsigned s390_exc, qemu_exc;
66 /* Get the exceptions raised by the current operation. Reset the
67 fpu_status contents so that the next operation has a clean slate. */
68 qemu_exc = env->fpu_status.float_exception_flags;
69 if (qemu_exc == 0) {
70 return;
72 env->fpu_status.float_exception_flags = 0;
73 s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
76 * IEEE-Underflow exception recognition exists if a tininess condition
77 * (underflow) exists and
78 * - The mask bit in the FPC is zero and the result is inexact
79 * - The mask bit in the FPC is one
80 * So tininess conditions that are not inexact don't trigger any
81 * underflow action in case the mask bit is not one.
83 if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
84 !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
85 s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
89 * FIXME:
90 * 1. Right now, all inexact conditions are indicated as
91 * "truncated" (0) and never as "incremented" (1) in the DXC.
92 * 2. Only traps due to invalid/divbyzero are suppressing. Other traps
93 * are completing, meaning the target register has to be written!
94 * This, however will mean that we have to write the register before
95 * triggering the trap - impossible right now.
99 * invalid/divbyzero cannot coexist with other conditions.
100 * overflow/underflow however can coexist with inexact, we have to
101 * handle it separately.
103 if (s390_exc & ~S390_IEEE_MASK_INEXACT) {
104 if (s390_exc & ~S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
105 /* trap condition - inexact reported along */
106 tcg_s390_data_exception(env, s390_exc, retaddr);
108 /* nontrap condition - inexact handled differently */
109 env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
112 /* inexact handling */
113 if (s390_exc & S390_IEEE_MASK_INEXACT && !XxC) {
114 /* trap condition - overflow/underflow _not_ reported along */
115 if (s390_exc & S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
116 tcg_s390_data_exception(env, s390_exc & S390_IEEE_MASK_INEXACT,
117 retaddr);
119 /* nontrap condition */
120 env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
124 int float_comp_to_cc(CPUS390XState *env, FloatRelation float_compare)
126 switch (float_compare) {
127 case float_relation_equal:
128 return 0;
129 case float_relation_less:
130 return 1;
131 case float_relation_greater:
132 return 2;
133 case float_relation_unordered:
134 return 3;
135 default:
136 cpu_abort(env_cpu(env), "unknown return value for float compare\n");
140 /* condition codes for unary FP ops */
141 uint32_t set_cc_nz_f32(float32 v)
143 if (float32_is_any_nan(v)) {
144 return 3;
145 } else if (float32_is_zero(v)) {
146 return 0;
147 } else if (float32_is_neg(v)) {
148 return 1;
149 } else {
150 return 2;
154 uint32_t set_cc_nz_f64(float64 v)
156 if (float64_is_any_nan(v)) {
157 return 3;
158 } else if (float64_is_zero(v)) {
159 return 0;
160 } else if (float64_is_neg(v)) {
161 return 1;
162 } else {
163 return 2;
167 uint32_t set_cc_nz_f128(float128 v)
169 if (float128_is_any_nan(v)) {
170 return 3;
171 } else if (float128_is_zero(v)) {
172 return 0;
173 } else if (float128_is_neg(v)) {
174 return 1;
175 } else {
176 return 2;
180 /* condition codes for FP to integer conversion ops */
181 static uint32_t set_cc_conv_f32(float32 v, float_status *stat)
183 if (stat->float_exception_flags & float_flag_invalid) {
184 return 3;
185 } else {
186 return set_cc_nz_f32(v);
190 static uint32_t set_cc_conv_f64(float64 v, float_status *stat)
192 if (stat->float_exception_flags & float_flag_invalid) {
193 return 3;
194 } else {
195 return set_cc_nz_f64(v);
199 static uint32_t set_cc_conv_f128(float128 v, float_status *stat)
201 if (stat->float_exception_flags & float_flag_invalid) {
202 return 3;
203 } else {
204 return set_cc_nz_f128(v);
208 static inline uint8_t round_from_m34(uint32_t m34)
210 return extract32(m34, 0, 4);
213 static inline bool xxc_from_m34(uint32_t m34)
215 /* XxC is bit 1 of m4 */
216 return extract32(m34, 4 + 3 - 1, 1);
219 /* 32-bit FP addition */
220 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
222 float32 ret = float32_add(f1, f2, &env->fpu_status);
223 handle_exceptions(env, false, GETPC());
224 return ret;
227 /* 64-bit FP addition */
228 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
230 float64 ret = float64_add(f1, f2, &env->fpu_status);
231 handle_exceptions(env, false, GETPC());
232 return ret;
235 /* 128-bit FP addition */
236 Int128 HELPER(axb)(CPUS390XState *env, Int128 a, Int128 b)
238 float128 ret = float128_add(ARG128(a), ARG128(b), &env->fpu_status);
239 handle_exceptions(env, false, GETPC());
240 return RET128(ret);
243 /* 32-bit FP subtraction */
244 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
246 float32 ret = float32_sub(f1, f2, &env->fpu_status);
247 handle_exceptions(env, false, GETPC());
248 return ret;
251 /* 64-bit FP subtraction */
252 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
254 float64 ret = float64_sub(f1, f2, &env->fpu_status);
255 handle_exceptions(env, false, GETPC());
256 return ret;
259 /* 128-bit FP subtraction */
260 Int128 HELPER(sxb)(CPUS390XState *env, Int128 a, Int128 b)
262 float128 ret = float128_sub(ARG128(a), ARG128(b), &env->fpu_status);
263 handle_exceptions(env, false, GETPC());
264 return RET128(ret);
267 /* 32-bit FP division */
268 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
270 float32 ret = float32_div(f1, f2, &env->fpu_status);
271 handle_exceptions(env, false, GETPC());
272 return ret;
275 /* 64-bit FP division */
276 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
278 float64 ret = float64_div(f1, f2, &env->fpu_status);
279 handle_exceptions(env, false, GETPC());
280 return ret;
283 /* 128-bit FP division */
284 Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b)
286 float128 ret = float128_div(ARG128(a), ARG128(b), &env->fpu_status);
287 handle_exceptions(env, false, GETPC());
288 return RET128(ret);
291 /* 32-bit FP multiplication */
292 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
294 float32 ret = float32_mul(f1, f2, &env->fpu_status);
295 handle_exceptions(env, false, GETPC());
296 return ret;
299 /* 64-bit FP multiplication */
300 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
302 float64 ret = float64_mul(f1, f2, &env->fpu_status);
303 handle_exceptions(env, false, GETPC());
304 return ret;
307 /* 64/32-bit FP multiplication */
308 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
310 float64 f1_64 = float32_to_float64(f1, &env->fpu_status);
311 float64 ret = float32_to_float64(f2, &env->fpu_status);
312 ret = float64_mul(f1_64, ret, &env->fpu_status);
313 handle_exceptions(env, false, GETPC());
314 return ret;
317 /* 128-bit FP multiplication */
318 Int128 HELPER(mxb)(CPUS390XState *env, Int128 a, Int128 b)
320 float128 ret = float128_mul(ARG128(a), ARG128(b), &env->fpu_status);
321 handle_exceptions(env, false, GETPC());
322 return RET128(ret);
325 /* 128/64-bit FP multiplication */
326 Int128 HELPER(mxdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
328 float128 f1_128 = float64_to_float128(f1, &env->fpu_status);
329 float128 ret = float64_to_float128(f2, &env->fpu_status);
330 ret = float128_mul(f1_128, ret, &env->fpu_status);
331 handle_exceptions(env, false, GETPC());
332 return RET128(ret);
335 /* convert 32-bit float to 64-bit float */
336 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
338 float64 ret = float32_to_float64(f2, &env->fpu_status);
339 handle_exceptions(env, false, GETPC());
340 return ret;
343 /* convert 128-bit float to 64-bit float */
344 uint64_t HELPER(ldxb)(CPUS390XState *env, Int128 a, uint32_t m34)
346 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
347 float64 ret = float128_to_float64(ARG128(a), &env->fpu_status);
349 s390_restore_bfp_rounding_mode(env, old_mode);
350 handle_exceptions(env, xxc_from_m34(m34), GETPC());
351 return ret;
354 /* convert 64-bit float to 128-bit float */
355 Int128 HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
357 float128 ret = float64_to_float128(f2, &env->fpu_status);
358 handle_exceptions(env, false, GETPC());
359 return RET128(ret);
362 /* convert 32-bit float to 128-bit float */
363 Int128 HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
365 float128 ret = float32_to_float128(f2, &env->fpu_status);
366 handle_exceptions(env, false, GETPC());
367 return RET128(ret);
370 /* convert 64-bit float to 32-bit float */
371 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
373 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
374 float32 ret = float64_to_float32(f2, &env->fpu_status);
376 s390_restore_bfp_rounding_mode(env, old_mode);
377 handle_exceptions(env, xxc_from_m34(m34), GETPC());
378 return ret;
381 /* convert 128-bit float to 32-bit float */
382 uint64_t HELPER(lexb)(CPUS390XState *env, Int128 a, uint32_t m34)
384 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
385 float32 ret = float128_to_float32(ARG128(a), &env->fpu_status);
387 s390_restore_bfp_rounding_mode(env, old_mode);
388 handle_exceptions(env, xxc_from_m34(m34), GETPC());
389 return ret;
392 /* 32-bit FP compare */
393 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
395 FloatRelation cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
396 handle_exceptions(env, false, GETPC());
397 return float_comp_to_cc(env, cmp);
400 /* 64-bit FP compare */
401 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
403 FloatRelation cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
404 handle_exceptions(env, false, GETPC());
405 return float_comp_to_cc(env, cmp);
408 /* 128-bit FP compare */
409 uint32_t HELPER(cxb)(CPUS390XState *env, Int128 a, Int128 b)
411 FloatRelation cmp = float128_compare_quiet(ARG128(a), ARG128(b),
412 &env->fpu_status);
413 handle_exceptions(env, false, GETPC());
414 return float_comp_to_cc(env, cmp);
417 int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
419 int ret = env->fpu_status.float_rounding_mode;
421 switch (m3) {
422 case 0:
423 /* current mode */
424 break;
425 case 1:
426 /* round to nearest with ties away from 0 */
427 set_float_rounding_mode(float_round_ties_away, &env->fpu_status);
428 break;
429 case 3:
430 /* round to prepare for shorter precision */
431 set_float_rounding_mode(float_round_to_odd, &env->fpu_status);
432 break;
433 case 4:
434 /* round to nearest with ties to even */
435 set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
436 break;
437 case 5:
438 /* round to zero */
439 set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
440 break;
441 case 6:
442 /* round to +inf */
443 set_float_rounding_mode(float_round_up, &env->fpu_status);
444 break;
445 case 7:
446 /* round to -inf */
447 set_float_rounding_mode(float_round_down, &env->fpu_status);
448 break;
449 default:
450 g_assert_not_reached();
452 return ret;
455 void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
457 set_float_rounding_mode(old_mode, &env->fpu_status);
460 /* convert 64-bit int to 32-bit float */
461 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
463 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
464 float32 ret = int64_to_float32(v2, &env->fpu_status);
466 s390_restore_bfp_rounding_mode(env, old_mode);
467 handle_exceptions(env, xxc_from_m34(m34), GETPC());
468 return ret;
471 /* convert 64-bit int to 64-bit float */
472 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
474 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
475 float64 ret = int64_to_float64(v2, &env->fpu_status);
477 s390_restore_bfp_rounding_mode(env, old_mode);
478 handle_exceptions(env, xxc_from_m34(m34), GETPC());
479 return ret;
482 /* convert 64-bit int to 128-bit float */
483 Int128 HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
485 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
486 float128 ret = int64_to_float128(v2, &env->fpu_status);
488 s390_restore_bfp_rounding_mode(env, old_mode);
489 handle_exceptions(env, xxc_from_m34(m34), GETPC());
490 return RET128(ret);
493 /* convert 64-bit uint to 32-bit float */
494 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
496 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
497 float32 ret = uint64_to_float32(v2, &env->fpu_status);
499 s390_restore_bfp_rounding_mode(env, old_mode);
500 handle_exceptions(env, xxc_from_m34(m34), GETPC());
501 return ret;
504 /* convert 64-bit uint to 64-bit float */
505 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
507 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
508 float64 ret = uint64_to_float64(v2, &env->fpu_status);
510 s390_restore_bfp_rounding_mode(env, old_mode);
511 handle_exceptions(env, xxc_from_m34(m34), GETPC());
512 return ret;
515 /* convert 64-bit uint to 128-bit float */
516 Int128 HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
518 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
519 float128 ret = uint64_to_float128(v2, &env->fpu_status);
521 s390_restore_bfp_rounding_mode(env, old_mode);
522 handle_exceptions(env, xxc_from_m34(m34), GETPC());
523 return RET128(ret);
526 /* convert 32-bit float to 64-bit int */
527 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
529 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
530 int64_t ret = float32_to_int64(v2, &env->fpu_status);
531 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
533 s390_restore_bfp_rounding_mode(env, old_mode);
534 handle_exceptions(env, xxc_from_m34(m34), GETPC());
535 env->cc_op = cc;
536 if (float32_is_any_nan(v2)) {
537 return INT64_MIN;
539 return ret;
542 /* convert 64-bit float to 64-bit int */
543 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
545 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
546 int64_t ret = float64_to_int64(v2, &env->fpu_status);
547 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
549 s390_restore_bfp_rounding_mode(env, old_mode);
550 handle_exceptions(env, xxc_from_m34(m34), GETPC());
551 env->cc_op = cc;
552 if (float64_is_any_nan(v2)) {
553 return INT64_MIN;
555 return ret;
558 /* convert 128-bit float to 64-bit int */
559 uint64_t HELPER(cgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
561 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
562 float128 v2 = ARG128(i2);
563 int64_t ret = float128_to_int64(v2, &env->fpu_status);
564 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
566 s390_restore_bfp_rounding_mode(env, old_mode);
567 handle_exceptions(env, xxc_from_m34(m34), GETPC());
568 env->cc_op = cc;
569 if (float128_is_any_nan(v2)) {
570 return INT64_MIN;
572 return ret;
575 /* convert 32-bit float to 32-bit int */
576 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
578 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
579 int32_t ret = float32_to_int32(v2, &env->fpu_status);
580 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
582 s390_restore_bfp_rounding_mode(env, old_mode);
583 handle_exceptions(env, xxc_from_m34(m34), GETPC());
584 env->cc_op = cc;
585 if (float32_is_any_nan(v2)) {
586 return INT32_MIN;
588 return ret;
591 /* convert 64-bit float to 32-bit int */
592 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
594 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
595 int32_t ret = float64_to_int32(v2, &env->fpu_status);
596 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
598 s390_restore_bfp_rounding_mode(env, old_mode);
599 handle_exceptions(env, xxc_from_m34(m34), GETPC());
600 env->cc_op = cc;
601 if (float64_is_any_nan(v2)) {
602 return INT32_MIN;
604 return ret;
607 /* convert 128-bit float to 32-bit int */
608 uint64_t HELPER(cfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
610 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
611 float128 v2 = ARG128(i2);
612 int32_t ret = float128_to_int32(v2, &env->fpu_status);
613 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
615 s390_restore_bfp_rounding_mode(env, old_mode);
616 handle_exceptions(env, xxc_from_m34(m34), GETPC());
617 env->cc_op = cc;
618 if (float128_is_any_nan(v2)) {
619 return INT32_MIN;
621 return ret;
624 /* convert 32-bit float to 64-bit uint */
625 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
627 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
628 uint64_t ret = float32_to_uint64(v2, &env->fpu_status);
629 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
631 s390_restore_bfp_rounding_mode(env, old_mode);
632 handle_exceptions(env, xxc_from_m34(m34), GETPC());
633 env->cc_op = cc;
634 if (float32_is_any_nan(v2)) {
635 return 0;
637 return ret;
640 /* convert 64-bit float to 64-bit uint */
641 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
643 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
644 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
645 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
647 s390_restore_bfp_rounding_mode(env, old_mode);
648 handle_exceptions(env, xxc_from_m34(m34), GETPC());
649 env->cc_op = cc;
650 if (float64_is_any_nan(v2)) {
651 return 0;
653 return ret;
656 /* convert 128-bit float to 64-bit uint */
657 uint64_t HELPER(clgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
659 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
660 float128 v2 = ARG128(i2);
661 uint64_t ret = float128_to_uint64(v2, &env->fpu_status);
662 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
664 s390_restore_bfp_rounding_mode(env, old_mode);
665 handle_exceptions(env, xxc_from_m34(m34), GETPC());
666 env->cc_op = cc;
667 if (float128_is_any_nan(v2)) {
668 return 0;
670 return ret;
673 /* convert 32-bit float to 32-bit uint */
674 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
676 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
677 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
678 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
680 s390_restore_bfp_rounding_mode(env, old_mode);
681 handle_exceptions(env, xxc_from_m34(m34), GETPC());
682 env->cc_op = cc;
683 if (float32_is_any_nan(v2)) {
684 return 0;
686 return ret;
689 /* convert 64-bit float to 32-bit uint */
690 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
692 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
693 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
694 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
696 s390_restore_bfp_rounding_mode(env, old_mode);
697 handle_exceptions(env, xxc_from_m34(m34), GETPC());
698 env->cc_op = cc;
699 if (float64_is_any_nan(v2)) {
700 return 0;
702 return ret;
705 /* convert 128-bit float to 32-bit uint */
706 uint64_t HELPER(clfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
708 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
709 float128 v2 = ARG128(i2);
710 uint32_t ret = float128_to_uint32(v2, &env->fpu_status);
711 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
713 s390_restore_bfp_rounding_mode(env, old_mode);
714 handle_exceptions(env, xxc_from_m34(m34), GETPC());
715 env->cc_op = cc;
716 if (float128_is_any_nan(v2)) {
717 return 0;
719 return ret;
722 /* round to integer 32-bit */
723 uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
725 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
726 float32 ret = float32_round_to_int(f2, &env->fpu_status);
728 s390_restore_bfp_rounding_mode(env, old_mode);
729 handle_exceptions(env, xxc_from_m34(m34), GETPC());
730 return ret;
733 /* round to integer 64-bit */
734 uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
736 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
737 float64 ret = float64_round_to_int(f2, &env->fpu_status);
739 s390_restore_bfp_rounding_mode(env, old_mode);
740 handle_exceptions(env, xxc_from_m34(m34), GETPC());
741 return ret;
744 /* round to integer 128-bit */
745 Int128 HELPER(fixb)(CPUS390XState *env, Int128 a, uint32_t m34)
747 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
748 float128 ret = float128_round_to_int(ARG128(a), &env->fpu_status);
750 s390_restore_bfp_rounding_mode(env, old_mode);
751 handle_exceptions(env, xxc_from_m34(m34), GETPC());
752 return RET128(ret);
755 /* 32-bit FP compare and signal */
756 uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
758 FloatRelation cmp = float32_compare(f1, f2, &env->fpu_status);
759 handle_exceptions(env, false, GETPC());
760 return float_comp_to_cc(env, cmp);
763 /* 64-bit FP compare and signal */
764 uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
766 FloatRelation cmp = float64_compare(f1, f2, &env->fpu_status);
767 handle_exceptions(env, false, GETPC());
768 return float_comp_to_cc(env, cmp);
771 /* 128-bit FP compare and signal */
772 uint32_t HELPER(kxb)(CPUS390XState *env, Int128 a, Int128 b)
774 FloatRelation cmp = float128_compare(ARG128(a), ARG128(b),
775 &env->fpu_status);
776 handle_exceptions(env, false, GETPC());
777 return float_comp_to_cc(env, cmp);
780 /* 32-bit FP multiply and add */
781 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
782 uint64_t f2, uint64_t f3)
784 float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
785 handle_exceptions(env, false, GETPC());
786 return ret;
789 /* 64-bit FP multiply and add */
790 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
791 uint64_t f2, uint64_t f3)
793 float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
794 handle_exceptions(env, false, GETPC());
795 return ret;
798 /* 32-bit FP multiply and subtract */
799 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
800 uint64_t f2, uint64_t f3)
802 float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
803 &env->fpu_status);
804 handle_exceptions(env, false, GETPC());
805 return ret;
808 /* 64-bit FP multiply and subtract */
809 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
810 uint64_t f2, uint64_t f3)
812 float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
813 &env->fpu_status);
814 handle_exceptions(env, false, GETPC());
815 return ret;
818 /* The rightmost bit has the number 11. */
819 static inline uint16_t dcmask(int bit, bool neg)
821 return 1 << (11 - bit - neg);
824 #define DEF_FLOAT_DCMASK(_TYPE) \
825 uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1) \
827 const bool neg = _TYPE##_is_neg(f1); \
829 /* Sorted by most common cases - only one class is possible */ \
830 if (_TYPE##_is_normal(f1)) { \
831 return dcmask(2, neg); \
832 } else if (_TYPE##_is_zero(f1)) { \
833 return dcmask(0, neg); \
834 } else if (_TYPE##_is_denormal(f1)) { \
835 return dcmask(4, neg); \
836 } else if (_TYPE##_is_infinity(f1)) { \
837 return dcmask(6, neg); \
838 } else if (_TYPE##_is_quiet_nan(f1, &env->fpu_status)) { \
839 return dcmask(8, neg); \
841 /* signaling nan, as last remaining case */ \
842 return dcmask(10, neg); \
844 DEF_FLOAT_DCMASK(float32)
845 DEF_FLOAT_DCMASK(float64)
846 DEF_FLOAT_DCMASK(float128)
848 /* test data class 32-bit */
849 uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
851 return (m2 & float32_dcmask(env, f1)) != 0;
854 /* test data class 64-bit */
855 uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
857 return (m2 & float64_dcmask(env, v1)) != 0;
860 /* test data class 128-bit */
861 uint32_t HELPER(tcxb)(CPUS390XState *env, Int128 a, uint64_t m2)
863 return (m2 & float128_dcmask(env, ARG128(a))) != 0;
866 /* square root 32-bit */
867 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
869 float32 ret = float32_sqrt(f2, &env->fpu_status);
870 handle_exceptions(env, false, GETPC());
871 return ret;
874 /* square root 64-bit */
875 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
877 float64 ret = float64_sqrt(f2, &env->fpu_status);
878 handle_exceptions(env, false, GETPC());
879 return ret;
882 /* square root 128-bit */
883 Int128 HELPER(sqxb)(CPUS390XState *env, Int128 a)
885 float128 ret = float128_sqrt(ARG128(a), &env->fpu_status);
886 handle_exceptions(env, false, GETPC());
887 return RET128(ret);
890 static const int fpc_to_rnd[8] = {
891 float_round_nearest_even,
892 float_round_to_zero,
893 float_round_up,
894 float_round_down,
898 float_round_to_odd,
901 /* set fpc */
902 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
904 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
905 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
906 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
909 /* Install everything in the main FPC. */
910 env->fpc = fpc;
912 /* Install the rounding mode in the shadow fpu_status. */
913 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
916 /* set fpc and signal */
917 void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
919 uint32_t signalling = env->fpc;
920 uint32_t s390_exc;
922 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
923 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
924 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
928 * FPC is set to the FPC operand with a bitwise OR of the signalling
929 * flags.
931 env->fpc = fpc | (signalling & 0x00ff0000);
932 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
935 * If any signaling flag is enabled in the new FPC mask, a
936 * simulated-iee-exception exception occurs.
938 s390_exc = (signalling >> 16) & (fpc >> 24);
939 if (s390_exc) {
940 if (s390_exc & S390_IEEE_MASK_INVALID) {
941 s390_exc = S390_IEEE_MASK_INVALID;
942 } else if (s390_exc & S390_IEEE_MASK_DIVBYZERO) {
943 s390_exc = S390_IEEE_MASK_DIVBYZERO;
944 } else if (s390_exc & S390_IEEE_MASK_OVERFLOW) {
945 s390_exc &= (S390_IEEE_MASK_OVERFLOW | S390_IEEE_MASK_INEXACT);
946 } else if (s390_exc & S390_IEEE_MASK_UNDERFLOW) {
947 s390_exc &= (S390_IEEE_MASK_UNDERFLOW | S390_IEEE_MASK_INEXACT);
948 } else if (s390_exc & S390_IEEE_MASK_INEXACT) {
949 s390_exc = S390_IEEE_MASK_INEXACT;
950 } else if (s390_exc & S390_IEEE_MASK_QUANTUM) {
951 s390_exc = S390_IEEE_MASK_QUANTUM;
953 tcg_s390_data_exception(env, s390_exc | 3, GETPC());
957 /* set bfp rounding mode */
958 void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
960 if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
961 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
964 env->fpc = deposit32(env->fpc, 0, 3, rnd);
965 set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);