xref: /openbmc/qemu/target/s390x/tcg/fpu_helper.c (revision beb1a917)
1 /*
2  *  S/390 FPU helper routines
3  *
4  *  Copyright (c) 2009 Ulrich Hecht
5  *  Copyright (c) 2009 Alexander Graf
6  *
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.
11  *
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.
16  *
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/>.
19  */
20 
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"
29 
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
36 
37 static inline Int128 RET128(float128 f)
38 {
39     return int128_make128(f.low, f.high);
40 }
41 
42 static inline float128 ARG128(Int128 i)
43 {
44     return make_float128(int128_gethi(i), int128_getlo(i));
45 }
46 
47 uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
48 {
49     uint8_t s390_exc = 0;
50 
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;
57 
58     return s390_exc;
59 }
60 
61 /* Should be called after any operation that may raise IEEE exceptions.  */
62 static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
63 {
64     unsigned s390_exc, qemu_exc;
65 
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;
71     }
72     env->fpu_status.float_exception_flags = 0;
73     s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
74 
75     /*
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.
82      */
83     if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
84         !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
85         s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
86     }
87 
88     /*
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.
96      */
97 
98     /*
99      * invalid/divbyzero cannot coexist with other conditions.
100      * overflow/underflow however can coexist with inexact, we have to
101      * handle it separately.
102      */
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);
107         }
108         /* nontrap condition - inexact handled differently */
109         env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
110     }
111 
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);
118         }
119         /* nontrap condition */
120         env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
121     }
122 }
123 
124 int float_comp_to_cc(CPUS390XState *env, FloatRelation float_compare)
125 {
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");
137     }
138 }
139 
140 /* condition codes for unary FP ops */
141 uint32_t set_cc_nz_f32(float32 v)
142 {
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;
151     }
152 }
153 
154 uint32_t set_cc_nz_f64(float64 v)
155 {
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;
164     }
165 }
166 
167 uint32_t set_cc_nz_f128(float128 v)
168 {
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;
177     }
178 }
179 
180 /* condition codes for FP to integer conversion ops */
181 static uint32_t set_cc_conv_f32(float32 v, float_status *stat)
182 {
183     if (stat->float_exception_flags & float_flag_invalid) {
184         return 3;
185     } else {
186         return set_cc_nz_f32(v);
187     }
188 }
189 
190 static uint32_t set_cc_conv_f64(float64 v, float_status *stat)
191 {
192     if (stat->float_exception_flags & float_flag_invalid) {
193         return 3;
194     } else {
195         return set_cc_nz_f64(v);
196     }
197 }
198 
199 static uint32_t set_cc_conv_f128(float128 v, float_status *stat)
200 {
201     if (stat->float_exception_flags & float_flag_invalid) {
202         return 3;
203     } else {
204         return set_cc_nz_f128(v);
205     }
206 }
207 
208 static inline uint8_t round_from_m34(uint32_t m34)
209 {
210     return extract32(m34, 0, 4);
211 }
212 
213 static inline bool xxc_from_m34(uint32_t m34)
214 {
215     /* XxC is bit 1 of m4 */
216     return extract32(m34, 4 + 3 - 1, 1);
217 }
218 
219 /* 32-bit FP addition */
220 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
221 {
222     float32 ret = float32_add(f1, f2, &env->fpu_status);
223     handle_exceptions(env, false, GETPC());
224     return ret;
225 }
226 
227 /* 64-bit FP addition */
228 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
229 {
230     float64 ret = float64_add(f1, f2, &env->fpu_status);
231     handle_exceptions(env, false, GETPC());
232     return ret;
233 }
234 
235 /* 128-bit FP addition */
236 Int128 HELPER(axb)(CPUS390XState *env, Int128 a, Int128 b)
237 {
238     float128 ret = float128_add(ARG128(a), ARG128(b), &env->fpu_status);
239     handle_exceptions(env, false, GETPC());
240     return RET128(ret);
241 }
242 
243 /* 32-bit FP subtraction */
244 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
245 {
246     float32 ret = float32_sub(f1, f2, &env->fpu_status);
247     handle_exceptions(env, false, GETPC());
248     return ret;
249 }
250 
251 /* 64-bit FP subtraction */
252 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
253 {
254     float64 ret = float64_sub(f1, f2, &env->fpu_status);
255     handle_exceptions(env, false, GETPC());
256     return ret;
257 }
258 
259 /* 128-bit FP subtraction */
260 Int128 HELPER(sxb)(CPUS390XState *env, Int128 a, Int128 b)
261 {
262     float128 ret = float128_sub(ARG128(a), ARG128(b), &env->fpu_status);
263     handle_exceptions(env, false, GETPC());
264     return RET128(ret);
265 }
266 
267 /* 32-bit FP division */
268 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
269 {
270     float32 ret = float32_div(f1, f2, &env->fpu_status);
271     handle_exceptions(env, false, GETPC());
272     return ret;
273 }
274 
275 /* 64-bit FP division */
276 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
277 {
278     float64 ret = float64_div(f1, f2, &env->fpu_status);
279     handle_exceptions(env, false, GETPC());
280     return ret;
281 }
282 
283 /* 128-bit FP division */
284 Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b)
285 {
286     float128 ret = float128_div(ARG128(a), ARG128(b), &env->fpu_status);
287     handle_exceptions(env, false, GETPC());
288     return RET128(ret);
289 }
290 
291 /* 32-bit FP multiplication */
292 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
293 {
294     float32 ret = float32_mul(f1, f2, &env->fpu_status);
295     handle_exceptions(env, false, GETPC());
296     return ret;
297 }
298 
299 /* 64-bit FP multiplication */
300 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
301 {
302     float64 ret = float64_mul(f1, f2, &env->fpu_status);
303     handle_exceptions(env, false, GETPC());
304     return ret;
305 }
306 
307 /* 64/32-bit FP multiplication */
308 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
309 {
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;
315 }
316 
317 /* 128-bit FP multiplication */
318 Int128 HELPER(mxb)(CPUS390XState *env, Int128 a, Int128 b)
319 {
320     float128 ret = float128_mul(ARG128(a), ARG128(b), &env->fpu_status);
321     handle_exceptions(env, false, GETPC());
322     return RET128(ret);
323 }
324 
325 /* 128/64-bit FP multiplication */
326 Int128 HELPER(mxdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
327 {
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);
333 }
334 
335 /* convert 32-bit float to 64-bit float */
336 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
337 {
338     float64 ret = float32_to_float64(f2, &env->fpu_status);
339     handle_exceptions(env, false, GETPC());
340     return ret;
341 }
342 
343 /* convert 128-bit float to 64-bit float */
344 uint64_t HELPER(ldxb)(CPUS390XState *env, Int128 a, uint32_t m34)
345 {
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);
348 
349     s390_restore_bfp_rounding_mode(env, old_mode);
350     handle_exceptions(env, xxc_from_m34(m34), GETPC());
351     return ret;
352 }
353 
354 /* convert 64-bit float to 128-bit float */
355 Int128 HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
356 {
357     float128 ret = float64_to_float128(f2, &env->fpu_status);
358     handle_exceptions(env, false, GETPC());
359     return RET128(ret);
360 }
361 
362 /* convert 32-bit float to 128-bit float */
363 Int128 HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
364 {
365     float128 ret = float32_to_float128(f2, &env->fpu_status);
366     handle_exceptions(env, false, GETPC());
367     return RET128(ret);
368 }
369 
370 /* convert 64-bit float to 32-bit float */
371 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
372 {
373     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
374     float32 ret = float64_to_float32(f2, &env->fpu_status);
375 
376     s390_restore_bfp_rounding_mode(env, old_mode);
377     handle_exceptions(env, xxc_from_m34(m34), GETPC());
378     return ret;
379 }
380 
381 /* convert 128-bit float to 32-bit float */
382 uint64_t HELPER(lexb)(CPUS390XState *env, Int128 a, uint32_t m34)
383 {
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);
386 
387     s390_restore_bfp_rounding_mode(env, old_mode);
388     handle_exceptions(env, xxc_from_m34(m34), GETPC());
389     return ret;
390 }
391 
392 /* 32-bit FP compare */
393 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
394 {
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);
398 }
399 
400 /* 64-bit FP compare */
401 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
402 {
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);
406 }
407 
408 /* 128-bit FP compare */
409 uint32_t HELPER(cxb)(CPUS390XState *env, Int128 a, Int128 b)
410 {
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);
415 }
416 
417 int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
418 {
419     int ret = env->fpu_status.float_rounding_mode;
420 
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();
451     }
452     return ret;
453 }
454 
455 void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
456 {
457     set_float_rounding_mode(old_mode, &env->fpu_status);
458 }
459 
460 /* convert 64-bit int to 32-bit float */
461 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
462 {
463     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
464     float32 ret = int64_to_float32(v2, &env->fpu_status);
465 
466     s390_restore_bfp_rounding_mode(env, old_mode);
467     handle_exceptions(env, xxc_from_m34(m34), GETPC());
468     return ret;
469 }
470 
471 /* convert 64-bit int to 64-bit float */
472 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
473 {
474     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
475     float64 ret = int64_to_float64(v2, &env->fpu_status);
476 
477     s390_restore_bfp_rounding_mode(env, old_mode);
478     handle_exceptions(env, xxc_from_m34(m34), GETPC());
479     return ret;
480 }
481 
482 /* convert 64-bit int to 128-bit float */
483 Int128 HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
484 {
485     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
486     float128 ret = int64_to_float128(v2, &env->fpu_status);
487 
488     s390_restore_bfp_rounding_mode(env, old_mode);
489     handle_exceptions(env, xxc_from_m34(m34), GETPC());
490     return RET128(ret);
491 }
492 
493 /* convert 64-bit uint to 32-bit float */
494 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
495 {
496     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
497     float32 ret = uint64_to_float32(v2, &env->fpu_status);
498 
499     s390_restore_bfp_rounding_mode(env, old_mode);
500     handle_exceptions(env, xxc_from_m34(m34), GETPC());
501     return ret;
502 }
503 
504 /* convert 64-bit uint to 64-bit float */
505 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
506 {
507     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
508     float64 ret = uint64_to_float64(v2, &env->fpu_status);
509 
510     s390_restore_bfp_rounding_mode(env, old_mode);
511     handle_exceptions(env, xxc_from_m34(m34), GETPC());
512     return ret;
513 }
514 
515 /* convert 64-bit uint to 128-bit float */
516 Int128 HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
517 {
518     int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
519     float128 ret = uint64_to_float128(v2, &env->fpu_status);
520 
521     s390_restore_bfp_rounding_mode(env, old_mode);
522     handle_exceptions(env, xxc_from_m34(m34), GETPC());
523     return RET128(ret);
524 }
525 
526 /* convert 32-bit float to 64-bit int */
527 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
528 {
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);
532 
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;
538     }
539     return ret;
540 }
541 
542 /* convert 64-bit float to 64-bit int */
543 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
544 {
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);
548 
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;
554     }
555     return ret;
556 }
557 
558 /* convert 128-bit float to 64-bit int */
559 uint64_t HELPER(cgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
560 {
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);
565 
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;
571     }
572     return ret;
573 }
574 
575 /* convert 32-bit float to 32-bit int */
576 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
577 {
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);
581 
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;
587     }
588     return ret;
589 }
590 
591 /* convert 64-bit float to 32-bit int */
592 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
593 {
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);
597 
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;
603     }
604     return ret;
605 }
606 
607 /* convert 128-bit float to 32-bit int */
608 uint64_t HELPER(cfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
609 {
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);
614 
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;
620     }
621     return ret;
622 }
623 
624 /* convert 32-bit float to 64-bit uint */
625 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
626 {
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);
630 
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;
636     }
637     return ret;
638 }
639 
640 /* convert 64-bit float to 64-bit uint */
641 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
642 {
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);
646 
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;
652     }
653     return ret;
654 }
655 
656 /* convert 128-bit float to 64-bit uint */
657 uint64_t HELPER(clgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
658 {
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);
663 
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;
669     }
670     return ret;
671 }
672 
673 /* convert 32-bit float to 32-bit uint */
674 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
675 {
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);
679 
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;
685     }
686     return ret;
687 }
688 
689 /* convert 64-bit float to 32-bit uint */
690 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
691 {
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);
695 
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;
701     }
702     return ret;
703 }
704 
705 /* convert 128-bit float to 32-bit uint */
706 uint64_t HELPER(clfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
707 {
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);
712 
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;
718     }
719     return ret;
720 }
721 
722 /* round to integer 32-bit */
723 uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
724 {
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);
727 
728     s390_restore_bfp_rounding_mode(env, old_mode);
729     handle_exceptions(env, xxc_from_m34(m34), GETPC());
730     return ret;
731 }
732 
733 /* round to integer 64-bit */
734 uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
735 {
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);
738 
739     s390_restore_bfp_rounding_mode(env, old_mode);
740     handle_exceptions(env, xxc_from_m34(m34), GETPC());
741     return ret;
742 }
743 
744 /* round to integer 128-bit */
745 Int128 HELPER(fixb)(CPUS390XState *env, Int128 a, uint32_t m34)
746 {
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);
749 
750     s390_restore_bfp_rounding_mode(env, old_mode);
751     handle_exceptions(env, xxc_from_m34(m34), GETPC());
752     return RET128(ret);
753 }
754 
755 /* 32-bit FP compare and signal */
756 uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
757 {
758     FloatRelation cmp = float32_compare(f1, f2, &env->fpu_status);
759     handle_exceptions(env, false, GETPC());
760     return float_comp_to_cc(env, cmp);
761 }
762 
763 /* 64-bit FP compare and signal */
764 uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
765 {
766     FloatRelation cmp = float64_compare(f1, f2, &env->fpu_status);
767     handle_exceptions(env, false, GETPC());
768     return float_comp_to_cc(env, cmp);
769 }
770 
771 /* 128-bit FP compare and signal */
772 uint32_t HELPER(kxb)(CPUS390XState *env, Int128 a, Int128 b)
773 {
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);
778 }
779 
780 /* 32-bit FP multiply and add */
781 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
782                       uint64_t f2, uint64_t f3)
783 {
784     float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
785     handle_exceptions(env, false, GETPC());
786     return ret;
787 }
788 
789 /* 64-bit FP multiply and add */
790 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
791                       uint64_t f2, uint64_t f3)
792 {
793     float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
794     handle_exceptions(env, false, GETPC());
795     return ret;
796 }
797 
798 /* 32-bit FP multiply and subtract */
799 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
800                       uint64_t f2, uint64_t f3)
801 {
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;
806 }
807 
808 /* 64-bit FP multiply and subtract */
809 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
810                       uint64_t f2, uint64_t f3)
811 {
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;
816 }
817 
818 /* The rightmost bit has the number 11. */
819 static inline uint16_t dcmask(int bit, bool neg)
820 {
821     return 1 << (11 - bit - neg);
822 }
823 
824 #define DEF_FLOAT_DCMASK(_TYPE) \
825 uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1)              \
826 {                                                                  \
827     const bool neg = _TYPE##_is_neg(f1);                           \
828                                                                    \
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);                                     \
840     }                                                              \
841     /* signaling nan, as last remaining case */                    \
842     return dcmask(10, neg);                                        \
843 }
844 DEF_FLOAT_DCMASK(float32)
845 DEF_FLOAT_DCMASK(float64)
846 DEF_FLOAT_DCMASK(float128)
847 
848 /* test data class 32-bit */
849 uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
850 {
851     return (m2 & float32_dcmask(env, f1)) != 0;
852 }
853 
854 /* test data class 64-bit */
855 uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
856 {
857     return (m2 & float64_dcmask(env, v1)) != 0;
858 }
859 
860 /* test data class 128-bit */
861 uint32_t HELPER(tcxb)(CPUS390XState *env, Int128 a, uint64_t m2)
862 {
863     return (m2 & float128_dcmask(env, ARG128(a))) != 0;
864 }
865 
866 /* square root 32-bit */
867 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
868 {
869     float32 ret = float32_sqrt(f2, &env->fpu_status);
870     handle_exceptions(env, false, GETPC());
871     return ret;
872 }
873 
874 /* square root 64-bit */
875 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
876 {
877     float64 ret = float64_sqrt(f2, &env->fpu_status);
878     handle_exceptions(env, false, GETPC());
879     return ret;
880 }
881 
882 /* square root 128-bit */
883 Int128 HELPER(sqxb)(CPUS390XState *env, Int128 a)
884 {
885     float128 ret = float128_sqrt(ARG128(a), &env->fpu_status);
886     handle_exceptions(env, false, GETPC());
887     return RET128(ret);
888 }
889 
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,
895     -1,
896     -1,
897     -1,
898     float_round_to_odd,
899 };
900 
901 /* set fpc */
902 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
903 {
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());
907     }
908 
909     /* Install everything in the main FPC.  */
910     env->fpc = fpc;
911 
912     /* Install the rounding mode in the shadow fpu_status.  */
913     set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
914 }
915 
916 /* set fpc and signal */
917 void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
918 {
919     uint32_t signalling = env->fpc;
920     uint32_t s390_exc;
921 
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());
925     }
926 
927     /*
928      * FPC is set to the FPC operand with a bitwise OR of the signalling
929      * flags.
930      */
931     env->fpc = fpc | (signalling & 0x00ff0000);
932     set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
933 
934     /*
935      * If any signaling flag is enabled in the new FPC mask, a
936      * simulated-iee-exception exception occurs.
937      */
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;
952         }
953         tcg_s390_data_exception(env, s390_exc | 3, GETPC());
954     }
955 }
956 
957 /* set bfp rounding mode */
958 void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
959 {
960     if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
961         tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
962     }
963 
964     env->fpc = deposit32(env->fpc, 0, 3, rnd);
965     set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);
966 }
967