migration/colo.h: Remove obsolete codes
[qemu/kevin.git] / target / s390x / fpu_helper.c
blob1be68bafea30b637bfde9b9993dba9818f3e53d5
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 "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 #define RET128(F) (env->retxl = F.low, F.high)
39 uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
41 uint8_t s390_exc = 0;
43 s390_exc |= (exc & float_flag_invalid) ? S390_IEEE_MASK_INVALID : 0;
44 s390_exc |= (exc & float_flag_divbyzero) ? S390_IEEE_MASK_DIVBYZERO : 0;
45 s390_exc |= (exc & float_flag_overflow) ? S390_IEEE_MASK_OVERFLOW : 0;
46 s390_exc |= (exc & float_flag_underflow) ? S390_IEEE_MASK_UNDERFLOW : 0;
47 s390_exc |= (exc & float_flag_inexact) ? S390_IEEE_MASK_INEXACT : 0;
49 return s390_exc;
52 /* Should be called after any operation that may raise IEEE exceptions. */
53 static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
55 unsigned s390_exc, qemu_exc;
57 /* Get the exceptions raised by the current operation. Reset the
58 fpu_status contents so that the next operation has a clean slate. */
59 qemu_exc = env->fpu_status.float_exception_flags;
60 if (qemu_exc == 0) {
61 return;
63 env->fpu_status.float_exception_flags = 0;
64 s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
67 * IEEE-Underflow exception recognition exists if a tininess condition
68 * (underflow) exists and
69 * - The mask bit in the FPC is zero and the result is inexact
70 * - The mask bit in the FPC is one
71 * So tininess conditions that are not inexact don't trigger any
72 * underflow action in case the mask bit is not one.
74 if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
75 !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
76 s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
80 * FIXME:
81 * 1. Right now, all inexact conditions are inidicated as
82 * "truncated" (0) and never as "incremented" (1) in the DXC.
83 * 2. Only traps due to invalid/divbyzero are suppressing. Other traps
84 * are completing, meaning the target register has to be written!
85 * This, however will mean that we have to write the register before
86 * triggering the trap - impossible right now.
90 * invalid/divbyzero cannot coexist with other conditions.
91 * overflow/underflow however can coexist with inexact, we have to
92 * handle it separatly.
94 if (s390_exc & ~S390_IEEE_MASK_INEXACT) {
95 if (s390_exc & ~S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
96 /* trap condition - inexact reported along */
97 tcg_s390_data_exception(env, s390_exc, retaddr);
99 /* nontrap condition - inexact handled differently */
100 env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
103 /* inexact handling */
104 if (s390_exc & S390_IEEE_MASK_INEXACT && !XxC) {
105 /* trap condition - overflow/underflow _not_ reported along */
106 if (s390_exc & S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
107 tcg_s390_data_exception(env, s390_exc & S390_IEEE_MASK_INEXACT,
108 retaddr);
110 /* nontrap condition */
111 env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
115 static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
117 S390CPU *cpu = s390_env_get_cpu(env);
119 switch (float_compare) {
120 case float_relation_equal:
121 return 0;
122 case float_relation_less:
123 return 1;
124 case float_relation_greater:
125 return 2;
126 case float_relation_unordered:
127 return 3;
128 default:
129 cpu_abort(CPU(cpu), "unknown return value for float compare\n");
133 /* condition codes for unary FP ops */
134 uint32_t set_cc_nz_f32(float32 v)
136 if (float32_is_any_nan(v)) {
137 return 3;
138 } else if (float32_is_zero(v)) {
139 return 0;
140 } else if (float32_is_neg(v)) {
141 return 1;
142 } else {
143 return 2;
147 uint32_t set_cc_nz_f64(float64 v)
149 if (float64_is_any_nan(v)) {
150 return 3;
151 } else if (float64_is_zero(v)) {
152 return 0;
153 } else if (float64_is_neg(v)) {
154 return 1;
155 } else {
156 return 2;
160 uint32_t set_cc_nz_f128(float128 v)
162 if (float128_is_any_nan(v)) {
163 return 3;
164 } else if (float128_is_zero(v)) {
165 return 0;
166 } else if (float128_is_neg(v)) {
167 return 1;
168 } else {
169 return 2;
173 static inline uint8_t round_from_m34(uint32_t m34)
175 return extract32(m34, 0, 4);
178 static inline bool xxc_from_m34(uint32_t m34)
180 /* XxC is bit 1 of m4 */
181 return extract32(m34, 4 + 3 - 1, 1);
184 /* 32-bit FP addition */
185 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
187 float32 ret = float32_add(f1, f2, &env->fpu_status);
188 handle_exceptions(env, false, GETPC());
189 return ret;
192 /* 64-bit FP addition */
193 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
195 float64 ret = float64_add(f1, f2, &env->fpu_status);
196 handle_exceptions(env, false, GETPC());
197 return ret;
200 /* 128-bit FP addition */
201 uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
202 uint64_t bh, uint64_t bl)
204 float128 ret = float128_add(make_float128(ah, al),
205 make_float128(bh, bl),
206 &env->fpu_status);
207 handle_exceptions(env, false, GETPC());
208 return RET128(ret);
211 /* 32-bit FP subtraction */
212 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
214 float32 ret = float32_sub(f1, f2, &env->fpu_status);
215 handle_exceptions(env, false, GETPC());
216 return ret;
219 /* 64-bit FP subtraction */
220 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
222 float64 ret = float64_sub(f1, f2, &env->fpu_status);
223 handle_exceptions(env, false, GETPC());
224 return ret;
227 /* 128-bit FP subtraction */
228 uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
229 uint64_t bh, uint64_t bl)
231 float128 ret = float128_sub(make_float128(ah, al),
232 make_float128(bh, bl),
233 &env->fpu_status);
234 handle_exceptions(env, false, GETPC());
235 return RET128(ret);
238 /* 32-bit FP division */
239 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
241 float32 ret = float32_div(f1, f2, &env->fpu_status);
242 handle_exceptions(env, false, GETPC());
243 return ret;
246 /* 64-bit FP division */
247 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
249 float64 ret = float64_div(f1, f2, &env->fpu_status);
250 handle_exceptions(env, false, GETPC());
251 return ret;
254 /* 128-bit FP division */
255 uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
256 uint64_t bh, uint64_t bl)
258 float128 ret = float128_div(make_float128(ah, al),
259 make_float128(bh, bl),
260 &env->fpu_status);
261 handle_exceptions(env, false, GETPC());
262 return RET128(ret);
265 /* 32-bit FP multiplication */
266 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
268 float32 ret = float32_mul(f1, f2, &env->fpu_status);
269 handle_exceptions(env, false, GETPC());
270 return ret;
273 /* 64-bit FP multiplication */
274 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
276 float64 ret = float64_mul(f1, f2, &env->fpu_status);
277 handle_exceptions(env, false, GETPC());
278 return ret;
281 /* 64/32-bit FP multiplication */
282 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
284 float64 ret = float32_to_float64(f2, &env->fpu_status);
285 ret = float64_mul(f1, ret, &env->fpu_status);
286 handle_exceptions(env, false, GETPC());
287 return ret;
290 /* 128-bit FP multiplication */
291 uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
292 uint64_t bh, uint64_t bl)
294 float128 ret = float128_mul(make_float128(ah, al),
295 make_float128(bh, bl),
296 &env->fpu_status);
297 handle_exceptions(env, false, GETPC());
298 return RET128(ret);
301 /* 128/64-bit FP multiplication */
302 uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
303 uint64_t f2)
305 float128 ret = float64_to_float128(f2, &env->fpu_status);
306 ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
307 handle_exceptions(env, false, GETPC());
308 return RET128(ret);
311 /* convert 32-bit float to 64-bit float */
312 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
314 float64 ret = float32_to_float64(f2, &env->fpu_status);
315 handle_exceptions(env, false, GETPC());
316 return ret;
319 /* convert 128-bit float to 64-bit float */
320 uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
321 uint32_t m34)
323 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
324 float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
326 s390_restore_bfp_rounding_mode(env, old_mode);
327 handle_exceptions(env, xxc_from_m34(m34), GETPC());
328 return ret;
331 /* convert 64-bit float to 128-bit float */
332 uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
334 float128 ret = float64_to_float128(f2, &env->fpu_status);
335 handle_exceptions(env, false, GETPC());
336 return RET128(ret);
339 /* convert 32-bit float to 128-bit float */
340 uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
342 float128 ret = float32_to_float128(f2, &env->fpu_status);
343 handle_exceptions(env, false, GETPC());
344 return RET128(ret);
347 /* convert 64-bit float to 32-bit float */
348 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
350 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
351 float32 ret = float64_to_float32(f2, &env->fpu_status);
353 s390_restore_bfp_rounding_mode(env, old_mode);
354 handle_exceptions(env, xxc_from_m34(m34), GETPC());
355 return ret;
358 /* convert 128-bit float to 32-bit float */
359 uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al,
360 uint32_t m34)
362 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
363 float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
365 s390_restore_bfp_rounding_mode(env, old_mode);
366 handle_exceptions(env, xxc_from_m34(m34), GETPC());
367 return ret;
370 /* 32-bit FP compare */
371 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
373 int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
374 handle_exceptions(env, false, GETPC());
375 return float_comp_to_cc(env, cmp);
378 /* 64-bit FP compare */
379 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
381 int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
382 handle_exceptions(env, false, GETPC());
383 return float_comp_to_cc(env, cmp);
386 /* 128-bit FP compare */
387 uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
388 uint64_t bh, uint64_t bl)
390 int cmp = float128_compare_quiet(make_float128(ah, al),
391 make_float128(bh, bl),
392 &env->fpu_status);
393 handle_exceptions(env, false, GETPC());
394 return float_comp_to_cc(env, cmp);
397 int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
399 int ret = env->fpu_status.float_rounding_mode;
401 switch (m3) {
402 case 0:
403 /* current mode */
404 break;
405 case 1:
406 /* round to nearest with ties away from 0 */
407 set_float_rounding_mode(float_round_ties_away, &env->fpu_status);
408 break;
409 case 3:
410 /* round to prepare for shorter precision */
411 set_float_rounding_mode(float_round_to_odd, &env->fpu_status);
412 break;
413 case 4:
414 /* round to nearest with ties to even */
415 set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
416 break;
417 case 5:
418 /* round to zero */
419 set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
420 break;
421 case 6:
422 /* round to +inf */
423 set_float_rounding_mode(float_round_up, &env->fpu_status);
424 break;
425 case 7:
426 /* round to -inf */
427 set_float_rounding_mode(float_round_down, &env->fpu_status);
428 break;
429 default:
430 g_assert_not_reached();
432 return ret;
435 void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
437 set_float_rounding_mode(old_mode, &env->fpu_status);
440 /* convert 64-bit int to 32-bit float */
441 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
443 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
444 float32 ret = int64_to_float32(v2, &env->fpu_status);
446 s390_restore_bfp_rounding_mode(env, old_mode);
447 handle_exceptions(env, xxc_from_m34(m34), GETPC());
448 return ret;
451 /* convert 64-bit int to 64-bit float */
452 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
454 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
455 float64 ret = int64_to_float64(v2, &env->fpu_status);
457 s390_restore_bfp_rounding_mode(env, old_mode);
458 handle_exceptions(env, xxc_from_m34(m34), GETPC());
459 return ret;
462 /* convert 64-bit int to 128-bit float */
463 uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
465 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
466 float128 ret = int64_to_float128(v2, &env->fpu_status);
468 s390_restore_bfp_rounding_mode(env, old_mode);
469 handle_exceptions(env, xxc_from_m34(m34), GETPC());
470 return RET128(ret);
473 /* convert 64-bit uint to 32-bit float */
474 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
476 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
477 float32 ret = uint64_to_float32(v2, &env->fpu_status);
479 s390_restore_bfp_rounding_mode(env, old_mode);
480 handle_exceptions(env, xxc_from_m34(m34), GETPC());
481 return ret;
484 /* convert 64-bit uint to 64-bit float */
485 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
487 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
488 float64 ret = uint64_to_float64(v2, &env->fpu_status);
490 s390_restore_bfp_rounding_mode(env, old_mode);
491 handle_exceptions(env, xxc_from_m34(m34), GETPC());
492 return ret;
495 /* convert 64-bit uint to 128-bit float */
496 uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
498 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
499 float128 ret = uint64_to_float128(v2, &env->fpu_status);
501 s390_restore_bfp_rounding_mode(env, old_mode);
502 handle_exceptions(env, xxc_from_m34(m34), GETPC());
503 return RET128(ret);
506 /* convert 32-bit float to 64-bit int */
507 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
509 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
510 int64_t ret = float32_to_int64(v2, &env->fpu_status);
512 s390_restore_bfp_rounding_mode(env, old_mode);
513 handle_exceptions(env, xxc_from_m34(m34), GETPC());
514 return ret;
517 /* convert 64-bit float to 64-bit int */
518 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
520 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
521 int64_t ret = float64_to_int64(v2, &env->fpu_status);
523 s390_restore_bfp_rounding_mode(env, old_mode);
524 handle_exceptions(env, xxc_from_m34(m34), GETPC());
525 return ret;
528 /* convert 128-bit float to 64-bit int */
529 uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
531 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
532 float128 v2 = make_float128(h, l);
533 int64_t ret = float128_to_int64(v2, &env->fpu_status);
535 s390_restore_bfp_rounding_mode(env, old_mode);
536 handle_exceptions(env, xxc_from_m34(m34), GETPC());
537 return ret;
540 /* convert 32-bit float to 32-bit int */
541 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
543 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
544 int32_t ret = float32_to_int32(v2, &env->fpu_status);
546 s390_restore_bfp_rounding_mode(env, old_mode);
547 handle_exceptions(env, xxc_from_m34(m34), GETPC());
548 return ret;
551 /* convert 64-bit float to 32-bit int */
552 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
554 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
555 int32_t ret = float64_to_int32(v2, &env->fpu_status);
557 s390_restore_bfp_rounding_mode(env, old_mode);
558 handle_exceptions(env, xxc_from_m34(m34), GETPC());
559 return ret;
562 /* convert 128-bit float to 32-bit int */
563 uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
565 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
566 float128 v2 = make_float128(h, l);
567 int32_t ret = float128_to_int32(v2, &env->fpu_status);
569 s390_restore_bfp_rounding_mode(env, old_mode);
570 handle_exceptions(env, xxc_from_m34(m34), GETPC());
571 return ret;
574 /* convert 32-bit float to 64-bit uint */
575 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
577 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
578 uint64_t ret;
580 v2 = float32_to_float64(v2, &env->fpu_status);
581 ret = float64_to_uint64(v2, &env->fpu_status);
582 s390_restore_bfp_rounding_mode(env, old_mode);
583 handle_exceptions(env, xxc_from_m34(m34), GETPC());
584 return ret;
587 /* convert 64-bit float to 64-bit uint */
588 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
590 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
591 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
593 s390_restore_bfp_rounding_mode(env, old_mode);
594 handle_exceptions(env, xxc_from_m34(m34), GETPC());
595 return ret;
598 /* convert 128-bit float to 64-bit uint */
599 uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
601 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
602 uint64_t ret = float128_to_uint64(make_float128(h, l), &env->fpu_status);
604 s390_restore_bfp_rounding_mode(env, old_mode);
605 handle_exceptions(env, xxc_from_m34(m34), GETPC());
606 return ret;
609 /* convert 32-bit float to 32-bit uint */
610 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
612 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
613 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
615 s390_restore_bfp_rounding_mode(env, old_mode);
616 handle_exceptions(env, xxc_from_m34(m34), GETPC());
617 return ret;
620 /* convert 64-bit float to 32-bit uint */
621 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
623 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
624 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
626 s390_restore_bfp_rounding_mode(env, old_mode);
627 handle_exceptions(env, xxc_from_m34(m34), GETPC());
628 return ret;
631 /* convert 128-bit float to 32-bit uint */
632 uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
634 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
635 uint32_t ret = float128_to_uint32(make_float128(h, l), &env->fpu_status);
637 s390_restore_bfp_rounding_mode(env, old_mode);
638 handle_exceptions(env, xxc_from_m34(m34), GETPC());
639 return ret;
642 /* round to integer 32-bit */
643 uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
645 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
646 float32 ret = float32_round_to_int(f2, &env->fpu_status);
648 s390_restore_bfp_rounding_mode(env, old_mode);
649 handle_exceptions(env, xxc_from_m34(m34), GETPC());
650 return ret;
653 /* round to integer 64-bit */
654 uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
656 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
657 float64 ret = float64_round_to_int(f2, &env->fpu_status);
659 s390_restore_bfp_rounding_mode(env, old_mode);
660 handle_exceptions(env, xxc_from_m34(m34), GETPC());
661 return ret;
664 /* round to integer 128-bit */
665 uint64_t HELPER(fixb)(CPUS390XState *env, uint64_t ah, uint64_t al,
666 uint32_t m34)
668 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
669 float128 ret = float128_round_to_int(make_float128(ah, al),
670 &env->fpu_status);
672 s390_restore_bfp_rounding_mode(env, old_mode);
673 handle_exceptions(env, xxc_from_m34(m34), GETPC());
674 return RET128(ret);
677 /* 32-bit FP compare and signal */
678 uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
680 int cmp = float32_compare(f1, f2, &env->fpu_status);
681 handle_exceptions(env, false, GETPC());
682 return float_comp_to_cc(env, cmp);
685 /* 64-bit FP compare and signal */
686 uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
688 int cmp = float64_compare(f1, f2, &env->fpu_status);
689 handle_exceptions(env, false, GETPC());
690 return float_comp_to_cc(env, cmp);
693 /* 128-bit FP compare and signal */
694 uint32_t HELPER(kxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
695 uint64_t bh, uint64_t bl)
697 int cmp = float128_compare(make_float128(ah, al),
698 make_float128(bh, bl),
699 &env->fpu_status);
700 handle_exceptions(env, false, GETPC());
701 return float_comp_to_cc(env, cmp);
704 /* 32-bit FP multiply and add */
705 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
706 uint64_t f2, uint64_t f3)
708 float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
709 handle_exceptions(env, false, GETPC());
710 return ret;
713 /* 64-bit FP multiply and add */
714 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
715 uint64_t f2, uint64_t f3)
717 float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
718 handle_exceptions(env, false, GETPC());
719 return ret;
722 /* 32-bit FP multiply and subtract */
723 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
724 uint64_t f2, uint64_t f3)
726 float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
727 &env->fpu_status);
728 handle_exceptions(env, false, GETPC());
729 return ret;
732 /* 64-bit FP multiply and subtract */
733 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
734 uint64_t f2, uint64_t f3)
736 float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
737 &env->fpu_status);
738 handle_exceptions(env, false, GETPC());
739 return ret;
742 /* The rightmost bit has the number 11. */
743 static inline uint16_t dcmask(int bit, bool neg)
745 return 1 << (11 - bit - neg);
748 #define DEF_FLOAT_DCMASK(_TYPE) \
749 static uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1) \
751 const bool neg = _TYPE##_is_neg(f1); \
753 /* Sorted by most common cases - only one class is possible */ \
754 if (_TYPE##_is_normal(f1)) { \
755 return dcmask(2, neg); \
756 } else if (_TYPE##_is_zero(f1)) { \
757 return dcmask(0, neg); \
758 } else if (_TYPE##_is_denormal(f1)) { \
759 return dcmask(4, neg); \
760 } else if (_TYPE##_is_infinity(f1)) { \
761 return dcmask(6, neg); \
762 } else if (_TYPE##_is_quiet_nan(f1, &env->fpu_status)) { \
763 return dcmask(8, neg); \
765 /* signaling nan, as last remaining case */ \
766 return dcmask(10, neg); \
768 DEF_FLOAT_DCMASK(float32)
769 DEF_FLOAT_DCMASK(float64)
770 DEF_FLOAT_DCMASK(float128)
772 /* test data class 32-bit */
773 uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
775 return (m2 & float32_dcmask(env, f1)) != 0;
778 /* test data class 64-bit */
779 uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
781 return (m2 & float64_dcmask(env, v1)) != 0;
784 /* test data class 128-bit */
785 uint32_t HELPER(tcxb)(CPUS390XState *env, uint64_t ah, uint64_t al, uint64_t m2)
787 return (m2 & float128_dcmask(env, make_float128(ah, al))) != 0;
790 /* square root 32-bit */
791 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
793 float32 ret = float32_sqrt(f2, &env->fpu_status);
794 handle_exceptions(env, false, GETPC());
795 return ret;
798 /* square root 64-bit */
799 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
801 float64 ret = float64_sqrt(f2, &env->fpu_status);
802 handle_exceptions(env, false, GETPC());
803 return ret;
806 /* square root 128-bit */
807 uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
809 float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
810 handle_exceptions(env, false, GETPC());
811 return RET128(ret);
814 static const int fpc_to_rnd[8] = {
815 float_round_nearest_even,
816 float_round_to_zero,
817 float_round_up,
818 float_round_down,
822 float_round_to_odd,
825 /* set fpc */
826 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
828 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
829 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
830 s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
833 /* Install everything in the main FPC. */
834 env->fpc = fpc;
836 /* Install the rounding mode in the shadow fpu_status. */
837 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
840 /* set fpc and signal */
841 void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
843 uint32_t signalling = env->fpc;
844 uint32_t s390_exc;
846 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
847 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
848 s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
852 * FPC is set to the FPC operand with a bitwise OR of the signalling
853 * flags.
855 env->fpc = fpc | (signalling & 0x00ff0000);
856 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
859 * If any signaling flag is enabled in the new FPC mask, a
860 * simulated-iee-exception exception occurs.
862 s390_exc = (signalling >> 16) & (fpc >> 24);
863 if (s390_exc) {
864 if (s390_exc & S390_IEEE_MASK_INVALID) {
865 s390_exc = S390_IEEE_MASK_INVALID;
866 } else if (s390_exc & S390_IEEE_MASK_DIVBYZERO) {
867 s390_exc = S390_IEEE_MASK_DIVBYZERO;
868 } else if (s390_exc & S390_IEEE_MASK_OVERFLOW) {
869 s390_exc &= (S390_IEEE_MASK_OVERFLOW | S390_IEEE_MASK_INEXACT);
870 } else if (s390_exc & S390_IEEE_MASK_UNDERFLOW) {
871 s390_exc &= (S390_IEEE_MASK_UNDERFLOW | S390_IEEE_MASK_INEXACT);
872 } else if (s390_exc & S390_IEEE_MASK_INEXACT) {
873 s390_exc = S390_IEEE_MASK_INEXACT;
874 } else if (s390_exc & S390_IEEE_MASK_QUANTUM) {
875 s390_exc = S390_IEEE_MASK_QUANTUM;
877 tcg_s390_data_exception(env, s390_exc | 3, GETPC());
881 /* set bfp rounding mode */
882 void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
884 if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
885 s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
888 env->fpc = deposit32(env->fpc, 0, 3, rnd);
889 set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);