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"
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 */
32 #define HELPER_LOG(x...) qemu_log(x)
34 #define HELPER_LOG(x...)
37 #define RET128(F) (env->retxl = F.low, F.high)
39 uint8_t s390_softfloat_exc_to_ieee(unsigned int exc
)
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;
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
;
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
;
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
,
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
:
122 case float_relation_less
:
124 case float_relation_greater
:
126 case float_relation_unordered
:
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
)) {
138 } else if (float32_is_zero(v
)) {
140 } else if (float32_is_neg(v
)) {
147 uint32_t set_cc_nz_f64(float64 v
)
149 if (float64_is_any_nan(v
)) {
151 } else if (float64_is_zero(v
)) {
153 } else if (float64_is_neg(v
)) {
160 uint32_t set_cc_nz_f128(float128 v
)
162 if (float128_is_any_nan(v
)) {
164 } else if (float128_is_zero(v
)) {
166 } else if (float128_is_neg(v
)) {
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());
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());
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
),
207 handle_exceptions(env
, false, GETPC());
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());
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());
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
),
234 handle_exceptions(env
, false, GETPC());
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());
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());
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
),
261 handle_exceptions(env
, false, GETPC());
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());
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());
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());
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
),
297 handle_exceptions(env
, false, GETPC());
301 /* 128/64-bit FP multiplication */
302 uint64_t HELPER(mxdb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
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());
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());
319 /* convert 128-bit float to 64-bit float */
320 uint64_t HELPER(ldxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
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());
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());
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());
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());
358 /* convert 128-bit float to 32-bit float */
359 uint64_t HELPER(lexb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
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());
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
),
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
;
406 /* round to nearest with ties away from 0 */
407 set_float_rounding_mode(float_round_ties_away
, &env
->fpu_status
);
410 /* round to prepare for shorter precision */
411 set_float_rounding_mode(float_round_to_odd
, &env
->fpu_status
);
414 /* round to nearest with ties to even */
415 set_float_rounding_mode(float_round_nearest_even
, &env
->fpu_status
);
419 set_float_rounding_mode(float_round_to_zero
, &env
->fpu_status
);
423 set_float_rounding_mode(float_round_up
, &env
->fpu_status
);
427 set_float_rounding_mode(float_round_down
, &env
->fpu_status
);
430 g_assert_not_reached();
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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
));
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());
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());
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());
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());
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());
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());
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());
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());
664 /* round to integer 128-bit */
665 uint64_t HELPER(fixb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
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
),
672 s390_restore_bfp_rounding_mode(env
, old_mode
);
673 handle_exceptions(env
, xxc_from_m34(m34
), GETPC());
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
),
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());
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());
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
,
728 handle_exceptions(env
, false, GETPC());
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
,
738 handle_exceptions(env
, false, GETPC());
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());
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());
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());
814 static const int fpc_to_rnd
[8] = {
815 float_round_nearest_even
,
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. */
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
;
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
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);
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
);