]> Git Repo - qemu.git/blame - target/s390x/fpu_helper.c
s390x/tcg: Introduce tcg_s390_vector_exception()
[qemu.git] / target / s390x / fpu_helper.c
CommitLineData
e72ca652
BS
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
41c6a6dd 10 * version 2.1 of the License, or (at your option) any later version.
e72ca652
BS
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
9615495a 21#include "qemu/osdep.h"
e72ca652 22#include "cpu.h"
4e58b838 23#include "internal.h"
bbf6ea3b 24#include "tcg_s390x.h"
63c91552 25#include "exec/exec-all.h"
f08b6170 26#include "exec/cpu_ldst.h"
2ef6175a 27#include "exec/helper-proto.h"
24f91e81 28#include "fpu/softfloat.h"
e72ca652 29
e72ca652
BS
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
587626f8
RH
37#define RET128(F) (env->retxl = F.low, F.high)
38
4b70fc54
DH
39uint8_t s390_softfloat_exc_to_ieee(unsigned int exc)
40{
41 uint8_t s390_exc = 0;
42
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;
48
49 return s390_exc;
50}
587626f8 51
587626f8 52/* Should be called after any operation that may raise IEEE exceptions. */
cf97f9ff 53static void handle_exceptions(CPUS390XState *env, bool XxC, uintptr_t retaddr)
587626f8
RH
54{
55 unsigned s390_exc, qemu_exc;
56
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;
62 }
63 env->fpu_status.float_exception_flags = 0;
4b70fc54 64 s390_exc = s390_softfloat_exc_to_ieee(qemu_exc);
587626f8 65
6d6ad1d1
DH
66 /*
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.
73 */
74 if (!(s390_exc & S390_IEEE_MASK_INEXACT) &&
75 !((env->fpc >> 24) & S390_IEEE_MASK_UNDERFLOW)) {
76 s390_exc &= ~S390_IEEE_MASK_UNDERFLOW;
77 }
78
fcb9e9f2
DH
79 /*
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.
87 */
587626f8 88
fcb9e9f2
DH
89 /*
90 * invalid/divbyzero cannot coexist with other conditions.
91 * overflow/underflow however can coexist with inexact, we have to
92 * handle it separatly.
93 */
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);
98 }
99 /* nontrap condition - inexact handled differently */
100 env->fpc |= (s390_exc & ~S390_IEEE_MASK_INEXACT) << 16;
101 }
102
103 /* inexact handling */
cf97f9ff 104 if (s390_exc & S390_IEEE_MASK_INEXACT && !XxC) {
fcb9e9f2
DH
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);
109 }
110 /* nontrap condition */
111 env->fpc |= (s390_exc & S390_IEEE_MASK_INEXACT) << 16;
587626f8
RH
112 }
113}
114
449c0d70 115static inline int float_comp_to_cc(CPUS390XState *env, int float_compare)
e72ca652 116{
a47dddd7
AF
117 S390CPU *cpu = s390_env_get_cpu(env);
118
e72ca652
BS
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:
a47dddd7 129 cpu_abort(CPU(cpu), "unknown return value for float compare\n");
e72ca652
BS
130 }
131}
132
e72ca652
BS
133/* condition codes for unary FP ops */
134uint32_t set_cc_nz_f32(float32 v)
135{
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;
144 }
145}
146
147uint32_t set_cc_nz_f64(float64 v)
148{
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;
157 }
158}
159
587626f8 160uint32_t set_cc_nz_f128(float128 v)
e72ca652
BS
161{
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;
170 }
171}
172
dce0a58f
DH
173static inline uint8_t round_from_m34(uint32_t m34)
174{
175 return extract32(m34, 0, 4);
176}
177
178static inline bool xxc_from_m34(uint32_t m34)
179{
180 /* XxC is bit 1 of m4 */
181 return extract32(m34, 4 + 3 - 1, 1);
182}
183
587626f8
RH
184/* 32-bit FP addition */
185uint64_t HELPER(aeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 186{
587626f8 187 float32 ret = float32_add(f1, f2, &env->fpu_status);
cf97f9ff 188 handle_exceptions(env, false, GETPC());
587626f8 189 return ret;
e72ca652
BS
190}
191
587626f8
RH
192/* 64-bit FP addition */
193uint64_t HELPER(adb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 194{
587626f8 195 float64 ret = float64_add(f1, f2, &env->fpu_status);
cf97f9ff 196 handle_exceptions(env, false, GETPC());
587626f8
RH
197 return ret;
198}
e72ca652 199
587626f8
RH
200/* 128-bit FP addition */
201uint64_t HELPER(axb)(CPUS390XState *env, uint64_t ah, uint64_t al,
202 uint64_t bh, uint64_t bl)
203{
204 float128 ret = float128_add(make_float128(ah, al),
205 make_float128(bh, bl),
206 &env->fpu_status);
cf97f9ff 207 handle_exceptions(env, false, GETPC());
587626f8 208 return RET128(ret);
e72ca652
BS
209}
210
1a800a2d
RH
211/* 32-bit FP subtraction */
212uint64_t HELPER(seb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 213{
1a800a2d 214 float32 ret = float32_sub(f1, f2, &env->fpu_status);
cf97f9ff 215 handle_exceptions(env, false, GETPC());
1a800a2d 216 return ret;
e72ca652
BS
217}
218
1a800a2d
RH
219/* 64-bit FP subtraction */
220uint64_t HELPER(sdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 221{
1a800a2d 222 float64 ret = float64_sub(f1, f2, &env->fpu_status);
cf97f9ff 223 handle_exceptions(env, false, GETPC());
1a800a2d
RH
224 return ret;
225}
e72ca652 226
1a800a2d
RH
227/* 128-bit FP subtraction */
228uint64_t HELPER(sxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
229 uint64_t bh, uint64_t bl)
230{
231 float128 ret = float128_sub(make_float128(ah, al),
232 make_float128(bh, bl),
233 &env->fpu_status);
cf97f9ff 234 handle_exceptions(env, false, GETPC());
1a800a2d 235 return RET128(ret);
e72ca652
BS
236}
237
f08a5c31
RH
238/* 32-bit FP division */
239uint64_t HELPER(deb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 240{
f08a5c31 241 float32 ret = float32_div(f1, f2, &env->fpu_status);
cf97f9ff 242 handle_exceptions(env, false, GETPC());
f08a5c31 243 return ret;
e72ca652
BS
244}
245
f08a5c31
RH
246/* 64-bit FP division */
247uint64_t HELPER(ddb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 248{
f08a5c31 249 float64 ret = float64_div(f1, f2, &env->fpu_status);
cf97f9ff 250 handle_exceptions(env, false, GETPC());
f08a5c31
RH
251 return ret;
252}
e72ca652 253
f08a5c31
RH
254/* 128-bit FP division */
255uint64_t HELPER(dxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
256 uint64_t bh, uint64_t bl)
257{
258 float128 ret = float128_div(make_float128(ah, al),
259 make_float128(bh, bl),
260 &env->fpu_status);
cf97f9ff 261 handle_exceptions(env, false, GETPC());
f08a5c31 262 return RET128(ret);
e72ca652
BS
263}
264
83b00736
RH
265/* 32-bit FP multiplication */
266uint64_t HELPER(meeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 267{
83b00736 268 float32 ret = float32_mul(f1, f2, &env->fpu_status);
cf97f9ff 269 handle_exceptions(env, false, GETPC());
83b00736 270 return ret;
e72ca652
BS
271}
272
83b00736
RH
273/* 64-bit FP multiplication */
274uint64_t HELPER(mdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 275{
83b00736 276 float64 ret = float64_mul(f1, f2, &env->fpu_status);
cf97f9ff 277 handle_exceptions(env, false, GETPC());
83b00736
RH
278 return ret;
279}
e72ca652 280
83b00736
RH
281/* 64/32-bit FP multiplication */
282uint64_t HELPER(mdeb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
283{
284 float64 ret = float32_to_float64(f2, &env->fpu_status);
285 ret = float64_mul(f1, ret, &env->fpu_status);
cf97f9ff 286 handle_exceptions(env, false, GETPC());
83b00736
RH
287 return ret;
288}
289
290/* 128-bit FP multiplication */
291uint64_t HELPER(mxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
292 uint64_t bh, uint64_t bl)
293{
294 float128 ret = float128_mul(make_float128(ah, al),
295 make_float128(bh, bl),
296 &env->fpu_status);
cf97f9ff 297 handle_exceptions(env, false, GETPC());
83b00736
RH
298 return RET128(ret);
299}
300
301/* 128/64-bit FP multiplication */
302uint64_t HELPER(mxdb)(CPUS390XState *env, uint64_t ah, uint64_t al,
303 uint64_t f2)
304{
305 float128 ret = float64_to_float128(f2, &env->fpu_status);
306 ret = float128_mul(make_float128(ah, al), ret, &env->fpu_status);
cf97f9ff 307 handle_exceptions(env, false, GETPC());
83b00736 308 return RET128(ret);
e72ca652
BS
309}
310
311/* convert 32-bit float to 64-bit float */
587626f8 312uint64_t HELPER(ldeb)(CPUS390XState *env, uint64_t f2)
e72ca652 313{
587626f8 314 float64 ret = float32_to_float64(f2, &env->fpu_status);
cf97f9ff 315 handle_exceptions(env, false, GETPC());
d0cfecb5 316 return ret;
e72ca652
BS
317}
318
319/* convert 128-bit float to 64-bit float */
bdcfcd44
DH
320uint64_t HELPER(ldxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
321 uint32_t m34)
e72ca652 322{
bdcfcd44 323 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
587626f8 324 float64 ret = float128_to_float64(make_float128(ah, al), &env->fpu_status);
bdcfcd44
DH
325
326 s390_restore_bfp_rounding_mode(env, old_mode);
327 handle_exceptions(env, xxc_from_m34(m34), GETPC());
d0cfecb5 328 return ret;
e72ca652
BS
329}
330
331/* convert 64-bit float to 128-bit float */
587626f8 332uint64_t HELPER(lxdb)(CPUS390XState *env, uint64_t f2)
e72ca652 333{
587626f8 334 float128 ret = float64_to_float128(f2, &env->fpu_status);
cf97f9ff 335 handle_exceptions(env, false, GETPC());
d0cfecb5 336 return RET128(ret);
587626f8 337}
e72ca652 338
587626f8
RH
339/* convert 32-bit float to 128-bit float */
340uint64_t HELPER(lxeb)(CPUS390XState *env, uint64_t f2)
341{
342 float128 ret = float32_to_float128(f2, &env->fpu_status);
cf97f9ff 343 handle_exceptions(env, false, GETPC());
d0cfecb5 344 return RET128(ret);
e72ca652
BS
345}
346
347/* convert 64-bit float to 32-bit float */
bdcfcd44 348uint64_t HELPER(ledb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
e72ca652 349{
bdcfcd44 350 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
587626f8 351 float32 ret = float64_to_float32(f2, &env->fpu_status);
bdcfcd44
DH
352
353 s390_restore_bfp_rounding_mode(env, old_mode);
354 handle_exceptions(env, xxc_from_m34(m34), GETPC());
d0cfecb5 355 return ret;
e72ca652
BS
356}
357
358/* convert 128-bit float to 32-bit float */
bdcfcd44
DH
359uint64_t HELPER(lexb)(CPUS390XState *env, uint64_t ah, uint64_t al,
360 uint32_t m34)
e72ca652 361{
bdcfcd44 362 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
587626f8 363 float32 ret = float128_to_float32(make_float128(ah, al), &env->fpu_status);
bdcfcd44
DH
364
365 s390_restore_bfp_rounding_mode(env, old_mode);
366 handle_exceptions(env, xxc_from_m34(m34), GETPC());
d0cfecb5 367 return ret;
e72ca652
BS
368}
369
587626f8
RH
370/* 32-bit FP compare */
371uint32_t HELPER(ceb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 372{
587626f8 373 int cmp = float32_compare_quiet(f1, f2, &env->fpu_status);
cf97f9ff 374 handle_exceptions(env, false, GETPC());
587626f8 375 return float_comp_to_cc(env, cmp);
e72ca652
BS
376}
377
587626f8
RH
378/* 64-bit FP compare */
379uint32_t HELPER(cdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
e72ca652 380{
587626f8 381 int cmp = float64_compare_quiet(f1, f2, &env->fpu_status);
cf97f9ff 382 handle_exceptions(env, false, GETPC());
587626f8 383 return float_comp_to_cc(env, cmp);
e72ca652
BS
384}
385
587626f8
RH
386/* 128-bit FP compare */
387uint32_t HELPER(cxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
388 uint64_t bh, uint64_t bl)
e72ca652 389{
587626f8
RH
390 int cmp = float128_compare_quiet(make_float128(ah, al),
391 make_float128(bh, bl),
392 &env->fpu_status);
cf97f9ff 393 handle_exceptions(env, false, GETPC());
587626f8 394 return float_comp_to_cc(env, cmp);
e72ca652
BS
395}
396
c0ee7015 397int s390_swap_bfp_rounding_mode(CPUS390XState *env, int m3)
e72ca652 398{
68c8bd93 399 int ret = env->fpu_status.float_rounding_mode;
b12b103e 400
e72ca652
BS
401 switch (m3) {
402 case 0:
403 /* current mode */
404 break;
405 case 1:
b12b103e
DH
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;
e72ca652 413 case 4:
b12b103e 414 /* round to nearest with ties to even */
e72ca652
BS
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;
b12b103e
DH
429 default:
430 g_assert_not_reached();
e72ca652 431 }
68c8bd93 432 return ret;
e72ca652
BS
433}
434
c0ee7015
DH
435void s390_restore_bfp_rounding_mode(CPUS390XState *env, int old_mode)
436{
437 set_float_rounding_mode(old_mode, &env->fpu_status);
438}
439
683bb9a8 440/* convert 64-bit int to 32-bit float */
dce0a58f 441uint64_t HELPER(cegb)(CPUS390XState *env, int64_t v2, uint32_t m34)
683bb9a8 442{
dce0a58f 443 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
683bb9a8 444 float32 ret = int64_to_float32(v2, &env->fpu_status);
c0ee7015
DH
445
446 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 447 handle_exceptions(env, xxc_from_m34(m34), GETPC());
683bb9a8
RH
448 return ret;
449}
450
451/* convert 64-bit int to 64-bit float */
dce0a58f 452uint64_t HELPER(cdgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
683bb9a8 453{
dce0a58f 454 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
683bb9a8 455 float64 ret = int64_to_float64(v2, &env->fpu_status);
c0ee7015
DH
456
457 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 458 handle_exceptions(env, xxc_from_m34(m34), GETPC());
683bb9a8
RH
459 return ret;
460}
461
462/* convert 64-bit int to 128-bit float */
dce0a58f 463uint64_t HELPER(cxgb)(CPUS390XState *env, int64_t v2, uint32_t m34)
683bb9a8 464{
dce0a58f 465 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
683bb9a8 466 float128 ret = int64_to_float128(v2, &env->fpu_status);
c0ee7015
DH
467
468 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 469 handle_exceptions(env, xxc_from_m34(m34), GETPC());
2112bf1b
RH
470 return RET128(ret);
471}
472
473/* convert 64-bit uint to 32-bit float */
dce0a58f 474uint64_t HELPER(celgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
2112bf1b 475{
dce0a58f 476 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
2112bf1b 477 float32 ret = uint64_to_float32(v2, &env->fpu_status);
c0ee7015
DH
478
479 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 480 handle_exceptions(env, xxc_from_m34(m34), GETPC());
2112bf1b
RH
481 return ret;
482}
483
484/* convert 64-bit uint to 64-bit float */
dce0a58f 485uint64_t HELPER(cdlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
2112bf1b 486{
dce0a58f 487 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
2112bf1b 488 float64 ret = uint64_to_float64(v2, &env->fpu_status);
c0ee7015
DH
489
490 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 491 handle_exceptions(env, xxc_from_m34(m34), GETPC());
2112bf1b
RH
492 return ret;
493}
494
495/* convert 64-bit uint to 128-bit float */
dce0a58f 496uint64_t HELPER(cxlgb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
2112bf1b 497{
dce0a58f 498 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
d2d9feac 499 float128 ret = uint64_to_float128(v2, &env->fpu_status);
c0ee7015
DH
500
501 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 502 handle_exceptions(env, xxc_from_m34(m34), GETPC());
683bb9a8
RH
503 return RET128(ret);
504}
505
e72ca652 506/* convert 32-bit float to 64-bit int */
dce0a58f 507uint64_t HELPER(cgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
e72ca652 508{
dce0a58f 509 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93 510 int64_t ret = float32_to_int64(v2, &env->fpu_status);
c0ee7015
DH
511
512 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 513 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 514 return ret;
e72ca652
BS
515}
516
517/* convert 64-bit float to 64-bit int */
dce0a58f 518uint64_t HELPER(cgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
e72ca652 519{
dce0a58f 520 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93 521 int64_t ret = float64_to_int64(v2, &env->fpu_status);
c0ee7015
DH
522
523 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 524 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 525 return ret;
e72ca652
BS
526}
527
528/* convert 128-bit float to 64-bit int */
dce0a58f 529uint64_t HELPER(cgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
e72ca652 530{
dce0a58f 531 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93
RH
532 float128 v2 = make_float128(h, l);
533 int64_t ret = float128_to_int64(v2, &env->fpu_status);
c0ee7015
DH
534
535 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 536 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 537 return ret;
e72ca652
BS
538}
539
540/* convert 32-bit float to 32-bit int */
dce0a58f 541uint64_t HELPER(cfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
e72ca652 542{
dce0a58f 543 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93 544 int32_t ret = float32_to_int32(v2, &env->fpu_status);
c0ee7015
DH
545
546 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 547 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 548 return ret;
e72ca652
BS
549}
550
551/* convert 64-bit float to 32-bit int */
dce0a58f 552uint64_t HELPER(cfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
e72ca652 553{
dce0a58f 554 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93 555 int32_t ret = float64_to_int32(v2, &env->fpu_status);
c0ee7015
DH
556
557 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 558 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 559 return ret;
e72ca652
BS
560}
561
562/* convert 128-bit float to 32-bit int */
dce0a58f 563uint64_t HELPER(cfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
e72ca652 564{
dce0a58f 565 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
68c8bd93
RH
566 float128 v2 = make_float128(h, l);
567 int32_t ret = float128_to_int32(v2, &env->fpu_status);
c0ee7015
DH
568
569 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 570 handle_exceptions(env, xxc_from_m34(m34), GETPC());
68c8bd93 571 return ret;
e72ca652
BS
572}
573
6ac1b45f 574/* convert 32-bit float to 64-bit uint */
dce0a58f 575uint64_t HELPER(clgeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
6ac1b45f 576{
dce0a58f 577 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
6ac1b45f 578 uint64_t ret;
c0ee7015 579
6ac1b45f
RH
580 v2 = float32_to_float64(v2, &env->fpu_status);
581 ret = float64_to_uint64(v2, &env->fpu_status);
c0ee7015 582 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 583 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
584 return ret;
585}
586
587/* convert 64-bit float to 64-bit uint */
dce0a58f 588uint64_t HELPER(clgdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
6ac1b45f 589{
dce0a58f 590 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
6ac1b45f 591 uint64_t ret = float64_to_uint64(v2, &env->fpu_status);
c0ee7015
DH
592
593 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 594 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
595 return ret;
596}
597
598/* convert 128-bit float to 64-bit uint */
dce0a58f 599uint64_t HELPER(clgxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
6ac1b45f 600{
dce0a58f 601 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
3af471f9 602 uint64_t ret = float128_to_uint64(make_float128(h, l), &env->fpu_status);
c0ee7015
DH
603
604 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 605 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
606 return ret;
607}
608
609/* convert 32-bit float to 32-bit uint */
dce0a58f 610uint64_t HELPER(clfeb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
6ac1b45f 611{
dce0a58f 612 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
6ac1b45f 613 uint32_t ret = float32_to_uint32(v2, &env->fpu_status);
c0ee7015
DH
614
615 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 616 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
617 return ret;
618}
619
620/* convert 64-bit float to 32-bit uint */
dce0a58f 621uint64_t HELPER(clfdb)(CPUS390XState *env, uint64_t v2, uint32_t m34)
6ac1b45f 622{
dce0a58f 623 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
6ac1b45f 624 uint32_t ret = float64_to_uint32(v2, &env->fpu_status);
c0ee7015
DH
625
626 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 627 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
628 return ret;
629}
630
631/* convert 128-bit float to 32-bit uint */
dce0a58f 632uint64_t HELPER(clfxb)(CPUS390XState *env, uint64_t h, uint64_t l, uint32_t m34)
6ac1b45f 633{
dce0a58f 634 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
3af471f9 635 uint32_t ret = float128_to_uint32(make_float128(h, l), &env->fpu_status);
c0ee7015
DH
636
637 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 638 handle_exceptions(env, xxc_from_m34(m34), GETPC());
6ac1b45f
RH
639 return ret;
640}
641
ed0bcece 642/* round to integer 32-bit */
dce0a58f 643uint64_t HELPER(fieb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
ed0bcece 644{
dce0a58f 645 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
ed0bcece 646 float32 ret = float32_round_to_int(f2, &env->fpu_status);
c0ee7015
DH
647
648 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 649 handle_exceptions(env, xxc_from_m34(m34), GETPC());
ed0bcece
AJ
650 return ret;
651}
652
653/* round to integer 64-bit */
dce0a58f 654uint64_t HELPER(fidb)(CPUS390XState *env, uint64_t f2, uint32_t m34)
ed0bcece 655{
dce0a58f 656 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
ed0bcece 657 float64 ret = float64_round_to_int(f2, &env->fpu_status);
c0ee7015
DH
658
659 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 660 handle_exceptions(env, xxc_from_m34(m34), GETPC());
ed0bcece
AJ
661 return ret;
662}
663
664/* round to integer 128-bit */
dce0a58f
DH
665uint64_t HELPER(fixb)(CPUS390XState *env, uint64_t ah, uint64_t al,
666 uint32_t m34)
ed0bcece 667{
dce0a58f 668 int old_mode = s390_swap_bfp_rounding_mode(env, round_from_m34(m34));
ed0bcece
AJ
669 float128 ret = float128_round_to_int(make_float128(ah, al),
670 &env->fpu_status);
cf97f9ff 671
c0ee7015 672 s390_restore_bfp_rounding_mode(env, old_mode);
dce0a58f 673 handle_exceptions(env, xxc_from_m34(m34), GETPC());
ed0bcece
AJ
674 return RET128(ret);
675}
676
9c8be598
AJ
677/* 32-bit FP compare and signal */
678uint32_t HELPER(keb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
679{
680 int cmp = float32_compare(f1, f2, &env->fpu_status);
cf97f9ff 681 handle_exceptions(env, false, GETPC());
9c8be598
AJ
682 return float_comp_to_cc(env, cmp);
683}
684
685/* 64-bit FP compare and signal */
686uint32_t HELPER(kdb)(CPUS390XState *env, uint64_t f1, uint64_t f2)
687{
688 int cmp = float64_compare(f1, f2, &env->fpu_status);
cf97f9ff 689 handle_exceptions(env, false, GETPC());
9c8be598
AJ
690 return float_comp_to_cc(env, cmp);
691}
692
693/* 128-bit FP compare and signal */
694uint32_t HELPER(kxb)(CPUS390XState *env, uint64_t ah, uint64_t al,
695 uint64_t bh, uint64_t bl)
696{
697 int cmp = float128_compare(make_float128(ah, al),
698 make_float128(bh, bl),
699 &env->fpu_status);
cf97f9ff 700 handle_exceptions(env, false, GETPC());
9c8be598
AJ
701 return float_comp_to_cc(env, cmp);
702}
703
722bfec3
RH
704/* 32-bit FP multiply and add */
705uint64_t HELPER(maeb)(CPUS390XState *env, uint64_t f1,
706 uint64_t f2, uint64_t f3)
e72ca652 707{
722bfec3 708 float32 ret = float32_muladd(f2, f3, f1, 0, &env->fpu_status);
cf97f9ff 709 handle_exceptions(env, false, GETPC());
722bfec3 710 return ret;
e72ca652
BS
711}
712
722bfec3
RH
713/* 64-bit FP multiply and add */
714uint64_t HELPER(madb)(CPUS390XState *env, uint64_t f1,
715 uint64_t f2, uint64_t f3)
e72ca652 716{
722bfec3 717 float64 ret = float64_muladd(f2, f3, f1, 0, &env->fpu_status);
cf97f9ff 718 handle_exceptions(env, false, GETPC());
722bfec3 719 return ret;
e72ca652
BS
720}
721
722bfec3
RH
722/* 32-bit FP multiply and subtract */
723uint64_t HELPER(mseb)(CPUS390XState *env, uint64_t f1,
724 uint64_t f2, uint64_t f3)
e72ca652 725{
722bfec3
RH
726 float32 ret = float32_muladd(f2, f3, f1, float_muladd_negate_c,
727 &env->fpu_status);
cf97f9ff 728 handle_exceptions(env, false, GETPC());
722bfec3 729 return ret;
e72ca652
BS
730}
731
722bfec3
RH
732/* 64-bit FP multiply and subtract */
733uint64_t HELPER(msdb)(CPUS390XState *env, uint64_t f1,
734 uint64_t f2, uint64_t f3)
e72ca652 735{
722bfec3
RH
736 float64 ret = float64_muladd(f2, f3, f1, float_muladd_negate_c,
737 &env->fpu_status);
cf97f9ff 738 handle_exceptions(env, false, GETPC());
722bfec3 739 return ret;
e72ca652
BS
740}
741
fc7cc951
DH
742/* The rightmost bit has the number 11. */
743static inline uint16_t dcmask(int bit, bool neg)
744{
745 return 1 << (11 - bit - neg);
746}
747
748#define DEF_FLOAT_DCMASK(_TYPE) \
749static uint16_t _TYPE##_dcmask(CPUS390XState *env, _TYPE f1) \
750{ \
751 const bool neg = _TYPE##_is_neg(f1); \
752 \
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); \
764 } \
765 /* signaling nan, as last remaining case */ \
766 return dcmask(10, neg); \
767}
768DEF_FLOAT_DCMASK(float32)
769DEF_FLOAT_DCMASK(float64)
770DEF_FLOAT_DCMASK(float128)
771
e72ca652 772/* test data class 32-bit */
af39bc8c 773uint32_t HELPER(tceb)(CPUS390XState *env, uint64_t f1, uint64_t m2)
e72ca652 774{
fc7cc951 775 return (m2 & float32_dcmask(env, f1)) != 0;
e72ca652
BS
776}
777
778/* test data class 64-bit */
af39bc8c 779uint32_t HELPER(tcdb)(CPUS390XState *env, uint64_t v1, uint64_t m2)
e72ca652 780{
fc7cc951 781 return (m2 & float64_dcmask(env, v1)) != 0;
e72ca652
BS
782}
783
784/* test data class 128-bit */
fc7cc951
DH
785uint32_t HELPER(tcxb)(CPUS390XState *env, uint64_t ah, uint64_t al, uint64_t m2)
786{
787 return (m2 & float128_dcmask(env, make_float128(ah, al))) != 0;
e72ca652
BS
788}
789
16d7b2a4
RH
790/* square root 32-bit */
791uint64_t HELPER(sqeb)(CPUS390XState *env, uint64_t f2)
e72ca652 792{
16d7b2a4 793 float32 ret = float32_sqrt(f2, &env->fpu_status);
cf97f9ff 794 handle_exceptions(env, false, GETPC());
16d7b2a4
RH
795 return ret;
796}
797
798/* square root 64-bit */
799uint64_t HELPER(sqdb)(CPUS390XState *env, uint64_t f2)
800{
801 float64 ret = float64_sqrt(f2, &env->fpu_status);
cf97f9ff 802 handle_exceptions(env, false, GETPC());
16d7b2a4
RH
803 return ret;
804}
805
806/* square root 128-bit */
807uint64_t HELPER(sqxb)(CPUS390XState *env, uint64_t ah, uint64_t al)
808{
809 float128 ret = float128_sqrt(make_float128(ah, al), &env->fpu_status);
cf97f9ff 810 handle_exceptions(env, false, GETPC());
16d7b2a4 811 return RET128(ret);
e72ca652 812}
8379bfdb 813
2aea83c6 814static const int fpc_to_rnd[8] = {
411edc22
RH
815 float_round_nearest_even,
816 float_round_to_zero,
817 float_round_up,
2aea83c6
DH
818 float_round_down,
819 -1,
820 -1,
821 -1,
822 float_round_to_odd,
411edc22
RH
823};
824
8379bfdb
RH
825/* set fpc */
826void HELPER(sfpc)(CPUS390XState *env, uint64_t fpc)
827{
2aea83c6
DH
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());
831 }
832
8379bfdb
RH
833 /* Install everything in the main FPC. */
834 env->fpc = fpc;
835
836 /* Install the rounding mode in the shadow fpu_status. */
2aea83c6 837 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
411edc22
RH
838}
839
840/* set fpc and signal */
f66a0ecf 841void HELPER(sfas)(CPUS390XState *env, uint64_t fpc)
411edc22
RH
842{
843 uint32_t signalling = env->fpc;
411edc22
RH
844 uint32_t s390_exc;
845
2aea83c6
DH
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());
849 }
850
f66a0ecf
DH
851 /*
852 * FPC is set to the FPC operand with a bitwise OR of the signalling
853 * flags.
854 */
855 env->fpc = fpc | (signalling & 0x00ff0000);
2aea83c6 856 set_float_rounding_mode(fpc_to_rnd[fpc & 0x7], &env->fpu_status);
411edc22 857
f66a0ecf
DH
858 /*
859 * If any signaling flag is enabled in the new FPC mask, a
860 * simulated-iee-exception exception occurs.
861 */
862 s390_exc = (signalling >> 16) & (fpc >> 24);
411edc22 863 if (s390_exc) {
8772bbe4
DH
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;
876 }
bbf6ea3b 877 tcg_s390_data_exception(env, s390_exc | 3, GETPC());
411edc22 878 }
8379bfdb 879}
b9c737f5
DH
880
881/* set bfp rounding mode */
882void HELPER(srnm)(CPUS390XState *env, uint64_t rnd)
883{
884 if (rnd > 0x7 || fpc_to_rnd[rnd & 0x7] == -1) {
885 s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, GETPC());
886 }
887
888 env->fpc = deposit32(env->fpc, 0, 3, rnd);
889 set_float_rounding_mode(fpc_to_rnd[rnd & 0x7], &env->fpu_status);
890}
This page took 0.520981 seconds and 4 git commands to generate.