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/helper-proto.h"
26 #include "fpu/softfloat.h"
27
28 /* #define DEBUG_HELPER */
29 #ifdef DEBUG_HELPER
30 #define HELPER_LOG(x...) qemu_log(x)
31 #else
32 #define HELPER_LOG(x...)
33 #endif
34
RET128(float128 f)35 static inline Int128 RET128(float128 f)
36 {
37 return int128_make128(f.low, f.high);
38 }
39
ARG128(Int128 i)40 static inline float128 ARG128(Int128 i)
41 {
42 return make_float128(int128_gethi(i), int128_getlo(i));
43 }
44
s390_softfloat_exc_to_ieee(unsigned int exc)45 uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
46 {
47 uint8_t s390_exc = 0;
48
49 s390_exc |= (exc & float_flag_invalid) ? S390_IEEE_MASK_INVALID : 0;
50 s390_exc |= (exc & float_flag_divbyzero) ? S390_IEEE_MASK_DIVBYZERO : 0;
51 s390_exc |= (exc & float_flag_overflow) ? S390_IEEE_MASK_OVERFLOW : 0;
52 s390_exc |= (exc & float_flag_underflow) ? S390_IEEE_MASK_UNDERFLOW : 0;
53 s390_exc |= (exc & (float_flag_inexact | float_flag_invalid_cvti)) ?
54 S390_IEEE_MASK_INEXACT : 0;
55
56 return s390_exc;
57 }
58
59 /* Should be called after any operation that may raise IEEE exceptions. */
handle_exceptions(CPUS390XState * env,bool XxC,uintptr_t retaddr)60 static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
61 {
62 unsigned s390_exc, qemu_exc;
63
64 /* Get the exceptions raised by the current operation. Reset the
65 fpu_status contents so that the next operation has a clean slate. */
66 qemu_exc = env->fpu_status.float_exception_flags;
67 if (qemu_exc == 0) {
68 return;
69 }
70 env->fpu_status.float_exception_flags = 0;
71 s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
72
73 /*
74 * IEEE-Underflow exception recognition exists if a tininess condition
75 * (underflow) exists and
76 * - The mask bit in the FPC is zero and the result is inexact
77 * - The mask bit in the FPC is one
78 * So tininess conditions that are not inexact don't trigger any
79 * underflow action in case the mask bit is not one.
80 */
81 if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
82 !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
83 s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
84 }
85
86 /*
87 * FIXME:
88 * 1. Right now, all inexact conditions are indicated as
89 * "truncated" (0) and never as "incremented" (1) in the DXC.
90 * 2. Only traps due to invalid/divbyzero are suppressing. Other traps
91 * are completing, meaning the target register has to be written!
92 * This, however will mean that we have to write the register before
93 * triggering the trap - impossible right now.
94 */
95
96 /*
97 * invalid/divbyzero cannot coexist with other conditions.
98 * overflow/underflow however can coexist with inexact, we have to
99 * handle it separately.
100 */
101 if (s390_exc & ~S390_IEEE_MASK_INEXACT) {
102 if (s390_exc & ~S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
103 /* trap condition - inexact reported along */
104 tcg_s390_data_exception(env, s390_exc, retaddr);
105 }
106 /* nontrap condition - inexact handled differently */
107 env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
108 }
109
110 /* inexact handling */
111 if (s390_exc & S390_IEEE_MASK_INEXACT && !XxC) {
112 /* trap condition - overflow/underflow _not_ reported along */
113 if (s390_exc & S390_IEEE_MASK_INEXACT & env->fpc >> 24) {
114 tcg_s390_data_exception(env, s390_exc & S390_IEEE_MASK_INEXACT,
115 retaddr);
116 }
117 /* nontrap condition */
118 env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
119 }
120 }
121
float_comp_to_cc(CPUS390XState * env,FloatRelation float_compare)122 int float_comp_to_cc(CPUS390XState *env, FloatRelation float_compare)
123 {
124 switch (float_compare) {
125 case float_relation_equal:
126 return 0;
127 case float_relation_less:
128 return 1;
129 case float_relation_greater:
130 return 2;
131 case float_relation_unordered:
132 return 3;
133 default:
134 cpu_abort(env_cpu(env), "unknown return value for float compare\n");
135 }
136 }
137
138 /* condition codes for unary FP ops */
set_cc_nz_f32(float32 v)139 uint32_t set_cc_nz_f32(float32 v)
140 {
141 if (float32_is_any_nan(v)) {
142 return 3;
143 } else if (float32_is_zero(v)) {
144 return 0;
145 } else if (float32_is_neg(v)) {
146 return 1;
147 } else {
148 return 2;
149 }
150 }
151
set_cc_nz_f64(float64 v)152 uint32_t set_cc_nz_f64(float64 v)
153 {
154 if (float64_is_any_nan(v)) {
155 return 3;
156 } else if (float64_is_zero(v)) {
157 return 0;
158 } else if (float64_is_neg(v)) {
159 return 1;
160 } else {
161 return 2;
162 }
163 }
164
set_cc_nz_f128(float128 v)165 uint32_t set_cc_nz_f128(float128 v)
166 {
167 if (float128_is_any_nan(v)) {
168 return 3;
169 } else if (float128_is_zero(v)) {
170 return 0;
171 } else if (float128_is_neg(v)) {
172 return 1;
173 } else {
174 return 2;
175 }
176 }
177
178 /* condition codes for FP to integer conversion ops */
set_cc_conv_f32(float32 v,float_status * stat)179 static uint32_t set_cc_conv_f32(float32 v, float_status *stat)
180 {
181 if (stat->float_exception_flags & float_flag_invalid) {
182 return 3;
183 } else {
184 return set_cc_nz_f32(v);
185 }
186 }
187
set_cc_conv_f64(float64 v,float_status * stat)188 static uint32_t set_cc_conv_f64(float64 v, float_status *stat)
189 {
190 if (stat->float_exception_flags & float_flag_invalid) {
191 return 3;
192 } else {
193 return set_cc_nz_f64(v);
194 }
195 }
196
set_cc_conv_f128(float128 v,float_status * stat)197 static uint32_t set_cc_conv_f128(float128 v, float_status *stat)
198 {
199 if (stat->float_exception_flags & float_flag_invalid) {
200 return 3;
201 } else {
202 return set_cc_nz_f128(v);
203 }
204 }
205
round_from_m34(uint32_t m34)206 static inline uint8_t round_from_m34(uint32_t m34)
207 {
208 return extract32(m34, 0, 4);
209 }
210
xxc_from_m34(uint32_t m34)211 static inline bool xxc_from_m34(uint32_t m34)
212 {
213 /* XxC is bit 1 of m4 */
214 return extract32(m34, 4 + 3 - 1, 1);
215 }
216
217 /* 32-bit FP addition */
HELPER(aeb)218 uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
219 {
220 float32 ret = float32_add(f1, f2, &env->fpu_status);
221 handle_exceptions(env, false, GETPC());
222 return ret;
223 }
224
225 /* 64-bit FP addition */
HELPER(adb)226 uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
227 {
228 float64 ret = float64_add(f1, f2, &env->fpu_status);
229 handle_exceptions(env, false, GETPC());
230 return ret;
231 }
232
233 /* 128-bit FP addition */
HELPER(axb)234 Int128 HELPER(axb)(CPUS390XState *env, Int128 a, Int128 b)
235 {
236 float128 ret = float128_add(ARG128(a), ARG128(b), &env->fpu_status);
237 handle_exceptions(env, false, GETPC());
238 return RET128(ret);
239 }
240
241 /* 32-bit FP subtraction */
HELPER(seb)242 uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
243 {
244 float32 ret = float32_sub(f1, f2, &env->fpu_status);
245 handle_exceptions(env, false, GETPC());
246 return ret;
247 }
248
249 /* 64-bit FP subtraction */
HELPER(sdb)250 uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
251 {
252 float64 ret = float64_sub(f1, f2, &env->fpu_status);
253 handle_exceptions(env, false, GETPC());
254 return ret;
255 }
256
257 /* 128-bit FP subtraction */
HELPER(sxb)258 Int128 HELPER(sxb)(CPUS390XState *env, Int128 a, Int128 b)
259 {
260 float128 ret = float128_sub(ARG128(a), ARG128(b), &env->fpu_status);
261 handle_exceptions(env, false, GETPC());
262 return RET128(ret);
263 }
264
265 /* 32-bit FP division */
HELPER(deb)266 uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
267 {
268 float32 ret = float32_div(f1, f2, &env->fpu_status);
269 handle_exceptions(env, false, GETPC());
270 return ret;
271 }
272
273 /* 64-bit FP division */
HELPER(ddb)274 uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
275 {
276 float64 ret = float64_div(f1, f2, &env->fpu_status);
277 handle_exceptions(env, false, GETPC());
278 return ret;
279 }
280
281 /* 128-bit FP division */
HELPER(dxb)282 Int128 HELPER(dxb)(CPUS390XState *env, Int128 a, Int128 b)
283 {
284 float128 ret = float128_div(ARG128(a), ARG128(b), &env->fpu_status);
285 handle_exceptions(env, false, GETPC());
286 return RET128(ret);
287 }
288
289 /* 32-bit FP multiplication */
HELPER(meeb)290 uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
291 {
292 float32 ret = float32_mul(f1, f2, &env->fpu_status);
293 handle_exceptions(env, false, GETPC());
294 return ret;
295 }
296
297 /* 64-bit FP multiplication */
HELPER(mdb)298 uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
299 {
300 float64 ret = float64_mul(f1, f2, &env->fpu_status);
301 handle_exceptions(env, false, GETPC());
302 return ret;
303 }
304
305 /* 64/32-bit FP multiplication */
HELPER(mdeb)306 uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
307 {
308 float64 f1_64 = float32_to_float64(f1, &env->fpu_status);
309 float64 ret = float32_to_float64(f2, &env->fpu_status);
310 ret = float64_mul(f1_64, ret, &env->fpu_status);
311 handle_exceptions(env, false, GETPC());
312 return ret;
313 }
314
315 /* 128-bit FP multiplication */
HELPER(mxb)316 Int128 HELPER(mxb)(CPUS390XState *env, Int128 a, Int128 b)
317 {
318 float128 ret = float128_mul(ARG128(a), ARG128(b), &env->fpu_status);
319 handle_exceptions(env, false, GETPC());
320 return RET128(ret);
321 }
322
323 /* 128/64-bit FP multiplication */
HELPER(mxdb)324 Int128 HELPER(mxdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
325 {
326 float128 f1_128 = float64_to_float128(f1, &env->fpu_status);
327 float128 ret = float64_to_float128(f2, &env->fpu_status);
328 ret = float128_mul(f1_128, ret, &env->fpu_status);
329 handle_exceptions(env, false, GETPC());
330 return RET128(ret);
331 }
332
333 /* convert 32-bit float to 64-bit float */
HELPER(ldeb)334 uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
335 {
336 float64 ret = float32_to_float64(f2, &env->fpu_status);
337 handle_exceptions(env, false, GETPC());
338 return ret;
339 }
340
341 /* convert 128-bit float to 64-bit float */
HELPER(ldxb)342 uint64_t HELPER(ldxb)(CPUS390XState *env, Int128 a, uint32_t m34)
343 {
344 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
345 float64 ret = float128_to_float64(ARG128(a), &env->fpu_status);
346
347 s390_restore_bfp_rounding_mode(env, old_mode);
348 handle_exceptions(env, xxc_from_m34(m34), GETPC());
349 return ret;
350 }
351
352 /* convert 64-bit float to 128-bit float */
HELPER(lxdb)353 Int128 HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
354 {
355 float128 ret = float64_to_float128(f2, &env->fpu_status);
356 handle_exceptions(env, false, GETPC());
357 return RET128(ret);
358 }
359
360 /* convert 32-bit float to 128-bit float */
HELPER(lxeb)361 Int128 HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
362 {
363 float128 ret = float32_to_float128(f2, &env->fpu_status);
364 handle_exceptions(env, false, GETPC());
365 return RET128(ret);
366 }
367
368 /* convert 64-bit float to 32-bit float */
HELPER(ledb)369 uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
370 {
371 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
372 float32 ret = float64_to_float32(f2, &env->fpu_status);
373
374 s390_restore_bfp_rounding_mode(env, old_mode);
375 handle_exceptions(env, xxc_from_m34(m34), GETPC());
376 return ret;
377 }
378
379 /* convert 128-bit float to 32-bit float */
HELPER(lexb)380 uint64_t HELPER(lexb)(CPUS390XState *env, Int128 a, uint32_t m34)
381 {
382 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
383 float32 ret = float128_to_float32(ARG128(a), &env->fpu_status);
384
385 s390_restore_bfp_rounding_mode(env, old_mode);
386 handle_exceptions(env, xxc_from_m34(m34), GETPC());
387 return ret;
388 }
389
390 /* 32-bit FP compare */
HELPER(ceb)391 uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
392 {
393 FloatRelation cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
394 handle_exceptions(env, false, GETPC());
395 return float_comp_to_cc(env, cmp);
396 }
397
398 /* 64-bit FP compare */
HELPER(cdb)399 uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
400 {
401 FloatRelation cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
402 handle_exceptions(env, false, GETPC());
403 return float_comp_to_cc(env, cmp);
404 }
405
406 /* 128-bit FP compare */
HELPER(cxb)407 uint32_t HELPER(cxb)(CPUS390XState *env, Int128 a, Int128 b)
408 {
409 FloatRelation cmp = float128_compare_quiet(ARG128(a), ARG128(b),
410 &env->fpu_status);
411 handle_exceptions(env, false, GETPC());
412 return float_comp_to_cc(env, cmp);
413 }
414
s390_swap_bfp_rounding_mode(CPUS390XState * env,int m3)415 int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
416 {
417 int ret = env->fpu_status.float_rounding_mode;
418
419 switch (m3) {
420 case 0:
421 /* current mode */
422 break;
423 case 1:
424 /* round to nearest with ties away from 0 */
425 set_float_rounding_mode(float_round_ties_away, &env->fpu_status);
426 break;
427 case 3:
428 /* round to prepare for shorter precision */
429 set_float_rounding_mode(float_round_to_odd, &env->fpu_status);
430 break;
431 case 4:
432 /* round to nearest with ties to even */
433 set_float_rounding_mode(float_round_nearest_even, &env->fpu_status);
434 break;
435 case 5:
436 /* round to zero */
437 set_float_rounding_mode(float_round_to_zero, &env->fpu_status);
438 break;
439 case 6:
440 /* round to +inf */
441 set_float_rounding_mode(float_round_up, &env->fpu_status);
442 break;
443 case 7:
444 /* round to -inf */
445 set_float_rounding_mode(float_round_down, &env->fpu_status);
446 break;
447 default:
448 g_assert_not_reached();
449 }
450 return ret;
451 }
452
s390_restore_bfp_rounding_mode(CPUS390XState * env,int old_mode)453 void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
454 {
455 set_float_rounding_mode(old_mode, &env->fpu_status);
456 }
457
458 /* convert 64-bit int to 32-bit float */
HELPER(cegb)459 uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
460 {
461 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
462 float32 ret = int64_to_float32(v2, &env->fpu_status);
463
464 s390_restore_bfp_rounding_mode(env, old_mode);
465 handle_exceptions(env, xxc_from_m34(m34), GETPC());
466 return ret;
467 }
468
469 /* convert 64-bit int to 64-bit float */
HELPER(cdgb)470 uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
471 {
472 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
473 float64 ret = int64_to_float64(v2, &env->fpu_status);
474
475 s390_restore_bfp_rounding_mode(env, old_mode);
476 handle_exceptions(env, xxc_from_m34(m34), GETPC());
477 return ret;
478 }
479
480 /* convert 64-bit int to 128-bit float */
HELPER(cxgb)481 Int128 HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
482 {
483 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
484 float128 ret = int64_to_float128(v2, &env->fpu_status);
485
486 s390_restore_bfp_rounding_mode(env, old_mode);
487 handle_exceptions(env, xxc_from_m34(m34), GETPC());
488 return RET128(ret);
489 }
490
491 /* convert 64-bit uint to 32-bit float */
HELPER(celgb)492 uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
493 {
494 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
495 float32 ret = uint64_to_float32(v2, &env->fpu_status);
496
497 s390_restore_bfp_rounding_mode(env, old_mode);
498 handle_exceptions(env, xxc_from_m34(m34), GETPC());
499 return ret;
500 }
501
502 /* convert 64-bit uint to 64-bit float */
HELPER(cdlgb)503 uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
504 {
505 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
506 float64 ret = uint64_to_float64(v2, &env->fpu_status);
507
508 s390_restore_bfp_rounding_mode(env, old_mode);
509 handle_exceptions(env, xxc_from_m34(m34), GETPC());
510 return ret;
511 }
512
513 /* convert 64-bit uint to 128-bit float */
HELPER(cxlgb)514 Int128 HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
515 {
516 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
517 float128 ret = uint64_to_float128(v2, &env->fpu_status);
518
519 s390_restore_bfp_rounding_mode(env, old_mode);
520 handle_exceptions(env, xxc_from_m34(m34), GETPC());
521 return RET128(ret);
522 }
523
524 /* convert 32-bit float to 64-bit int */
HELPER(cgeb)525 uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
526 {
527 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
528 int64_t ret = float32_to_int64(v2, &env->fpu_status);
529 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
530
531 s390_restore_bfp_rounding_mode(env, old_mode);
532 handle_exceptions(env, xxc_from_m34(m34), GETPC());
533 env->cc_op = cc;
534 if (float32_is_any_nan(v2)) {
535 return INT64_MIN;
536 }
537 return ret;
538 }
539
540 /* convert 64-bit float to 64-bit int */
HELPER(cgdb)541 uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
542 {
543 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
544 int64_t ret = float64_to_int64(v2, &env->fpu_status);
545 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
546
547 s390_restore_bfp_rounding_mode(env, old_mode);
548 handle_exceptions(env, xxc_from_m34(m34), GETPC());
549 env->cc_op = cc;
550 if (float64_is_any_nan(v2)) {
551 return INT64_MIN;
552 }
553 return ret;
554 }
555
556 /* convert 128-bit float to 64-bit int */
HELPER(cgxb)557 uint64_t HELPER(cgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
558 {
559 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
560 float128 v2 = ARG128(i2);
561 int64_t ret = float128_to_int64(v2, &env->fpu_status);
562 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
563
564 s390_restore_bfp_rounding_mode(env, old_mode);
565 handle_exceptions(env, xxc_from_m34(m34), GETPC());
566 env->cc_op = cc;
567 if (float128_is_any_nan(v2)) {
568 return INT64_MIN;
569 }
570 return ret;
571 }
572
573 /* convert 32-bit float to 32-bit int */
HELPER(cfeb)574 uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
575 {
576 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
577 int32_t ret = float32_to_int32(v2, &env->fpu_status);
578 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
579
580 s390_restore_bfp_rounding_mode(env, old_mode);
581 handle_exceptions(env, xxc_from_m34(m34), GETPC());
582 env->cc_op = cc;
583 if (float32_is_any_nan(v2)) {
584 return INT32_MIN;
585 }
586 return ret;
587 }
588
589 /* convert 64-bit float to 32-bit int */
HELPER(cfdb)590 uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
591 {
592 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
593 int32_t ret = float64_to_int32(v2, &env->fpu_status);
594 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
595
596 s390_restore_bfp_rounding_mode(env, old_mode);
597 handle_exceptions(env, xxc_from_m34(m34), GETPC());
598 env->cc_op = cc;
599 if (float64_is_any_nan(v2)) {
600 return INT32_MIN;
601 }
602 return ret;
603 }
604
605 /* convert 128-bit float to 32-bit int */
HELPER(cfxb)606 uint64_t HELPER(cfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
607 {
608 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
609 float128 v2 = ARG128(i2);
610 int32_t ret = float128_to_int32(v2, &env->fpu_status);
611 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
612
613 s390_restore_bfp_rounding_mode(env, old_mode);
614 handle_exceptions(env, xxc_from_m34(m34), GETPC());
615 env->cc_op = cc;
616 if (float128_is_any_nan(v2)) {
617 return INT32_MIN;
618 }
619 return ret;
620 }
621
622 /* convert 32-bit float to 64-bit uint */
HELPER(clgeb)623 uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
624 {
625 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
626 uint64_t ret = float32_to_uint64(v2, &env->fpu_status);
627 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
628
629 s390_restore_bfp_rounding_mode(env, old_mode);
630 handle_exceptions(env, xxc_from_m34(m34), GETPC());
631 env->cc_op = cc;
632 if (float32_is_any_nan(v2)) {
633 return 0;
634 }
635 return ret;
636 }
637
638 /* convert 64-bit float to 64-bit uint */
HELPER(clgdb)639 uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
640 {
641 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
642 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
643 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
644
645 s390_restore_bfp_rounding_mode(env, old_mode);
646 handle_exceptions(env, xxc_from_m34(m34), GETPC());
647 env->cc_op = cc;
648 if (float64_is_any_nan(v2)) {
649 return 0;
650 }
651 return ret;
652 }
653
654 /* convert 128-bit float to 64-bit uint */
HELPER(clgxb)655 uint64_t HELPER(clgxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
656 {
657 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
658 float128 v2 = ARG128(i2);
659 uint64_t ret = float128_to_uint64(v2, &env->fpu_status);
660 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
661
662 s390_restore_bfp_rounding_mode(env, old_mode);
663 handle_exceptions(env, xxc_from_m34(m34), GETPC());
664 env->cc_op = cc;
665 if (float128_is_any_nan(v2)) {
666 return 0;
667 }
668 return ret;
669 }
670
671 /* convert 32-bit float to 32-bit uint */
HELPER(clfeb)672 uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
673 {
674 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
675 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
676 uint32_t cc = set_cc_conv_f32(v2, &env->fpu_status);
677
678 s390_restore_bfp_rounding_mode(env, old_mode);
679 handle_exceptions(env, xxc_from_m34(m34), GETPC());
680 env->cc_op = cc;
681 if (float32_is_any_nan(v2)) {
682 return 0;
683 }
684 return ret;
685 }
686
687 /* convert 64-bit float to 32-bit uint */
HELPER(clfdb)688 uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
689 {
690 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
691 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
692 uint32_t cc = set_cc_conv_f64(v2, &env->fpu_status);
693
694 s390_restore_bfp_rounding_mode(env, old_mode);
695 handle_exceptions(env, xxc_from_m34(m34), GETPC());
696 env->cc_op = cc;
697 if (float64_is_any_nan(v2)) {
698 return 0;
699 }
700 return ret;
701 }
702
703 /* convert 128-bit float to 32-bit uint */
HELPER(clfxb)704 uint64_t HELPER(clfxb)(CPUS390XState *env, Int128 i2, uint32_t m34)
705 {
706 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
707 float128 v2 = ARG128(i2);
708 uint32_t ret = float128_to_uint32(v2, &env->fpu_status);
709 uint32_t cc = set_cc_conv_f128(v2, &env->fpu_status);
710
711 s390_restore_bfp_rounding_mode(env, old_mode);
712 handle_exceptions(env, xxc_from_m34(m34), GETPC());
713 env->cc_op = cc;
714 if (float128_is_any_nan(v2)) {
715 return 0;
716 }
717 return ret;
718 }
719
720 /* round to integer 32-bit */
HELPER(fieb)721 uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
722 {
723 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
724 float32 ret = float32_round_to_int(f2, &env->fpu_status);
725
726 s390_restore_bfp_rounding_mode(env, old_mode);
727 handle_exceptions(env, xxc_from_m34(m34), GETPC());
728 return ret;
729 }
730
731 /* round to integer 64-bit */
HELPER(fidb)732 uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
733 {
734 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
735 float64 ret = float64_round_to_int(f2, &env->fpu_status);
736
737 s390_restore_bfp_rounding_mode(env, old_mode);
738 handle_exceptions(env, xxc_from_m34(m34), GETPC());
739 return ret;
740 }
741
742 /* round to integer 128-bit */
HELPER(fixb)743 Int128 HELPER(fixb)(CPUS390XState *env, Int128 a, uint32_t m34)
744 {
745 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
746 float128 ret = float128_round_to_int(ARG128(a), &env->fpu_status);
747
748 s390_restore_bfp_rounding_mode(env, old_mode);
749 handle_exceptions(env, xxc_from_m34(m34), GETPC());
750 return RET128(ret);
751 }
752
753 /* 32-bit FP compare and signal */
HELPER(keb)754 uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
755 {
756 FloatRelation cmp = float32_compare(f1, f2, &env->fpu_status);
757 handle_exceptions(env, false, GETPC());
758 return float_comp_to_cc(env, cmp);
759 }
760
761 /* 64-bit FP compare and signal */
HELPER(kdb)762 uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
763 {
764 FloatRelation cmp = float64_compare(f1, f2, &env->fpu_status);
765 handle_exceptions(env, false, GETPC());
766 return float_comp_to_cc(env, cmp);
767 }
768
769 /* 128-bit FP compare and signal */
HELPER(kxb)770 uint32_t HELPER(kxb)(CPUS390XState *env, Int128 a, Int128 b)
771 {
772 FloatRelation cmp = float128_compare(ARG128(a), ARG128(b),
773 &env->fpu_status);
774 handle_exceptions(env, false, GETPC());
775 return float_comp_to_cc(env, cmp);
776 }
777
778 /* 32-bit FP multiply and add */
HELPER(maeb)779 uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
780 uint64_t f2, uint64_t f3)
781 {
782 float32 ret = float32_muladd(f3, f2, f1, 0, &env->fpu_status);
783 handle_exceptions(env, false, GETPC());
784 return ret;
785 }
786
787 /* 64-bit FP multiply and add */
HELPER(madb)788 uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
789 uint64_t f2, uint64_t f3)
790 {
791 float64 ret = float64_muladd(f3, f2, f1, 0, &env->fpu_status);
792 handle_exceptions(env, false, GETPC());
793 return ret;
794 }
795
796 /* 32-bit FP multiply and subtract */
HELPER(mseb)797 uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
798 uint64_t f2, uint64_t f3)
799 {
800 float32 ret = float32_muladd(f3, f2, f1, float_muladd_negate_c,
801 &env->fpu_status);
802 handle_exceptions(env, false, GETPC());
803 return ret;
804 }
805
806 /* 64-bit FP multiply and subtract */
HELPER(msdb)807 uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
808 uint64_t f2, uint64_t f3)
809 {
810 float64 ret = float64_muladd(f3, f2, f1, float_muladd_negate_c,
811 &env->fpu_status);
812 handle_exceptions(env, false, GETPC());
813 return ret;
814 }
815
816 /* The rightmost bit has the number 11. */
dcmask(int bit,bool neg)817 static inline uint16_t dcmask(int bit, bool neg)
818 {
819 return 1 << (11 - bit - neg);
820 }
821
822 #define DEF_FLOAT_DCMASK(_TYPE) \
823 uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1) \
824 { \
825 const bool neg = _TYPE##_is_neg(f1); \
826 \
827 /* Sorted by most common cases - only one class is possible */ \
828 if (_TYPE##_is_normal(f1)) { \
829 return dcmask(2, neg); \
830 } else if (_TYPE##_is_zero(f1)) { \
831 return dcmask(0, neg); \
832 } else if (_TYPE##_is_denormal(f1)) { \
833 return dcmask(4, neg); \
834 } else if (_TYPE##_is_infinity(f1)) { \
835 return dcmask(6, neg); \
836 } else if (_TYPE##_is_quiet_nan(f1, &env->fpu_status)) { \
837 return dcmask(8, neg); \
838 } \
839 /* signaling nan, as last remaining case */ \
840 return dcmask(10, neg); \
841 }
842 DEF_FLOAT_DCMASK(float32)
DEF_FLOAT_DCMASK(float64)843 DEF_FLOAT_DCMASK(float64)
844 DEF_FLOAT_DCMASK(float128)
845
846 /* test data class 32-bit */
847 uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
848 {
849 return (m2 & float32_dcmask(env, f1)) != 0;
850 }
851
852 /* test data class 64-bit */
HELPER(tcdb)853 uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
854 {
855 return (m2 & float64_dcmask(env, v1)) != 0;
856 }
857
858 /* test data class 128-bit */
HELPER(tcxb)859 uint32_t HELPER(tcxb)(CPUS390XState *env, Int128 a, uint64_t m2)
860 {
861 return (m2 & float128_dcmask(env, ARG128(a))) != 0;
862 }
863
864 /* square root 32-bit */
HELPER(sqeb)865 uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
866 {
867 float32 ret = float32_sqrt(f2, &env->fpu_status);
868 handle_exceptions(env, false, GETPC());
869 return ret;
870 }
871
872 /* square root 64-bit */
HELPER(sqdb)873 uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
874 {
875 float64 ret = float64_sqrt(f2, &env->fpu_status);
876 handle_exceptions(env, false, GETPC());
877 return ret;
878 }
879
880 /* square root 128-bit */
HELPER(sqxb)881 Int128 HELPER(sqxb)(CPUS390XState *env, Int128 a)
882 {
883 float128 ret = float128_sqrt(ARG128(a), &env->fpu_status);
884 handle_exceptions(env, false, GETPC());
885 return RET128(ret);
886 }
887
888 static const int fpc_to_rnd[8] = {
889 float_round_nearest_even,
890 float_round_to_zero,
891 float_round_up,
892 float_round_down,
893 -1,
894 -1,
895 -1,
896 float_round_to_odd,
897 };
898
899 /* set fpc */
HELPER(sfpc)900 void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
901 {
902 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
903 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
904 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
905 }
906
907 /* Install everything in the main FPC. */
908 env->fpc = fpc;
909
910 /* Install the rounding mode in the shadow fpu_status. */
911 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
912 }
913
914 /* set fpc and signal */
HELPER(sfas)915 void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
916 {
917 uint32_t signalling = env->fpc;
918 uint32_t s390_exc;
919
920 if (fpc_to_rnd[fpc & 0x7] == -1 || fpc & 0x03030088u ||
921 (!s390_has_feat(S390_FEAT_FLOATING_POINT_EXT) && fpc & 0x4)) {
922 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
923 }
924
925 /*
926 * FPC is set to the FPC operand with a bitwise OR of the signalling
927 * flags.
928 */
929 env->fpc = fpc | (signalling & 0x00ff0000);
930 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
931
932 /*
933 * If any signaling flag is enabled in the new FPC mask, a
934 * simulated-iee-exception exception occurs.
935 */
936 s390_exc = (signalling >> 16) & (fpc >> 24);
937 if (s390_exc) {
938 if (s390_exc & S390_IEEE_MASK_INVALID) {
939 s390_exc = S390_IEEE_MASK_INVALID;
940 } else if (s390_exc & S390_IEEE_MASK_DIVBYZERO) {
941 s390_exc = S390_IEEE_MASK_DIVBYZERO;
942 } else if (s390_exc & S390_IEEE_MASK_OVERFLOW) {
943 s390_exc &= (S390_IEEE_MASK_OVERFLOW | S390_IEEE_MASK_INEXACT);
944 } else if (s390_exc & S390_IEEE_MASK_UNDERFLOW) {
945 s390_exc &= (S390_IEEE_MASK_UNDERFLOW | S390_IEEE_MASK_INEXACT);
946 } else if (s390_exc & S390_IEEE_MASK_INEXACT) {
947 s390_exc = S390_IEEE_MASK_INEXACT;
948 } else if (s390_exc & S390_IEEE_MASK_QUANTUM) {
949 s390_exc = S390_IEEE_MASK_QUANTUM;
950 }
951 tcg_s390_data_exception(env, s390_exc | 3, GETPC());
952 }
953 }
954
955 /* set bfp rounding mode */
HELPER(srnm)956 void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
957 {
958 if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
959 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
960 }
961
962 env->fpc = deposit32(env->fpc, 0, 3, rnd);
963 set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);
964 }
965