]>
git.proxmox.com Git - qemu.git/blob - target-s390x/fpu_helper.c
6a7654183352faaf4d884380962b808f2fb7d0e5
2 * S/390 FPU helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #if !defined(CONFIG_USER_ONLY)
25 #include "exec/softmmu_exec.h"
28 /* #define DEBUG_HELPER */
30 #define HELPER_LOG(x...) qemu_log(x)
32 #define HELPER_LOG(x...)
35 #define RET128(F) (env->retxl = F.low, F.high)
37 #define convert_bit(mask, from, to) \
39 ? (mask / (from / to)) & to \
40 : (mask & from) * (to / from))
42 static void ieee_exception(CPUS390XState
*env
, uint32_t dxc
, uintptr_t retaddr
)
44 /* Install the DXC code. */
45 env
->fpc
= (env
->fpc
& ~0xff00) | (dxc
<< 8);
47 runtime_exception(env
, PGM_DATA
, retaddr
);
50 /* Should be called after any operation that may raise IEEE exceptions. */
51 static void handle_exceptions(CPUS390XState
*env
, uintptr_t retaddr
)
53 unsigned s390_exc
, qemu_exc
;
55 /* Get the exceptions raised by the current operation. Reset the
56 fpu_status contents so that the next operation has a clean slate. */
57 qemu_exc
= env
->fpu_status
.float_exception_flags
;
61 env
->fpu_status
.float_exception_flags
= 0;
63 /* Convert softfloat exception bits to s390 exception bits. */
65 s390_exc
|= convert_bit(qemu_exc
, float_flag_invalid
, 0x80);
66 s390_exc
|= convert_bit(qemu_exc
, float_flag_divbyzero
, 0x40);
67 s390_exc
|= convert_bit(qemu_exc
, float_flag_overflow
, 0x20);
68 s390_exc
|= convert_bit(qemu_exc
, float_flag_underflow
, 0x10);
69 s390_exc
|= convert_bit(qemu_exc
, float_flag_inexact
, 0x08);
71 /* Install the exceptions that we raised. */
72 env
->fpc
|= s390_exc
<< 16;
74 /* Send signals for enabled exceptions. */
75 s390_exc
&= env
->fpc
>> 24;
77 ieee_exception(env
, s390_exc
, retaddr
);
81 static inline int float_comp_to_cc(CPUS390XState
*env
, int float_compare
)
83 switch (float_compare
) {
84 case float_relation_equal
:
86 case float_relation_less
:
88 case float_relation_greater
:
90 case float_relation_unordered
:
93 cpu_abort(env
, "unknown return value for float compare\n");
97 /* condition codes for unary FP ops */
98 uint32_t set_cc_nz_f32(float32 v
)
100 if (float32_is_any_nan(v
)) {
102 } else if (float32_is_zero(v
)) {
104 } else if (float32_is_neg(v
)) {
111 uint32_t set_cc_nz_f64(float64 v
)
113 if (float64_is_any_nan(v
)) {
115 } else if (float64_is_zero(v
)) {
117 } else if (float64_is_neg(v
)) {
124 uint32_t set_cc_nz_f128(float128 v
)
126 if (float128_is_any_nan(v
)) {
128 } else if (float128_is_zero(v
)) {
130 } else if (float128_is_neg(v
)) {
137 /* 32-bit FP addition */
138 uint64_t HELPER(aeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
140 float32 ret
= float32_add(f1
, f2
, &env
->fpu_status
);
141 handle_exceptions(env
, GETPC());
145 /* 64-bit FP addition */
146 uint64_t HELPER(adb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
148 float64 ret
= float64_add(f1
, f2
, &env
->fpu_status
);
149 handle_exceptions(env
, GETPC());
153 /* 128-bit FP addition */
154 uint64_t HELPER(axb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
155 uint64_t bh
, uint64_t bl
)
157 float128 ret
= float128_add(make_float128(ah
, al
),
158 make_float128(bh
, bl
),
160 handle_exceptions(env
, GETPC());
164 /* 32-bit FP subtraction */
165 uint64_t HELPER(seb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
167 float32 ret
= float32_sub(f1
, f2
, &env
->fpu_status
);
168 handle_exceptions(env
, GETPC());
172 /* 64-bit FP subtraction */
173 uint64_t HELPER(sdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
175 float64 ret
= float64_sub(f1
, f2
, &env
->fpu_status
);
176 handle_exceptions(env
, GETPC());
180 /* 128-bit FP subtraction */
181 uint64_t HELPER(sxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
182 uint64_t bh
, uint64_t bl
)
184 float128 ret
= float128_sub(make_float128(ah
, al
),
185 make_float128(bh
, bl
),
187 handle_exceptions(env
, GETPC());
191 /* 32-bit FP division */
192 uint64_t HELPER(deb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
194 float32 ret
= float32_div(f1
, f2
, &env
->fpu_status
);
195 handle_exceptions(env
, GETPC());
199 /* 64-bit FP division */
200 uint64_t HELPER(ddb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
202 float64 ret
= float64_div(f1
, f2
, &env
->fpu_status
);
203 handle_exceptions(env
, GETPC());
207 /* 128-bit FP division */
208 uint64_t HELPER(dxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
209 uint64_t bh
, uint64_t bl
)
211 float128 ret
= float128_div(make_float128(ah
, al
),
212 make_float128(bh
, bl
),
214 handle_exceptions(env
, GETPC());
218 /* 32-bit FP multiplication */
219 uint64_t HELPER(meeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
221 float32 ret
= float32_mul(f1
, f2
, &env
->fpu_status
);
222 handle_exceptions(env
, GETPC());
226 /* 64-bit FP multiplication */
227 uint64_t HELPER(mdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
229 float64 ret
= float64_mul(f1
, f2
, &env
->fpu_status
);
230 handle_exceptions(env
, GETPC());
234 /* 64/32-bit FP multiplication */
235 uint64_t HELPER(mdeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
237 float64 ret
= float32_to_float64(f2
, &env
->fpu_status
);
238 ret
= float64_mul(f1
, ret
, &env
->fpu_status
);
239 handle_exceptions(env
, GETPC());
243 /* 128-bit FP multiplication */
244 uint64_t HELPER(mxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
245 uint64_t bh
, uint64_t bl
)
247 float128 ret
= float128_mul(make_float128(ah
, al
),
248 make_float128(bh
, bl
),
250 handle_exceptions(env
, GETPC());
254 /* 128/64-bit FP multiplication */
255 uint64_t HELPER(mxdb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
258 float128 ret
= float64_to_float128(f2
, &env
->fpu_status
);
259 ret
= float128_mul(make_float128(ah
, al
), ret
, &env
->fpu_status
);
260 handle_exceptions(env
, GETPC());
264 /* convert 32-bit float to 64-bit float */
265 uint64_t HELPER(ldeb
)(CPUS390XState
*env
, uint64_t f2
)
267 float64 ret
= float32_to_float64(f2
, &env
->fpu_status
);
268 handle_exceptions(env
, GETPC());
272 /* convert 128-bit float to 64-bit float */
273 uint64_t HELPER(ldxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
275 float64 ret
= float128_to_float64(make_float128(ah
, al
), &env
->fpu_status
);
276 handle_exceptions(env
, GETPC());
280 /* convert 64-bit float to 128-bit float */
281 uint64_t HELPER(lxdb
)(CPUS390XState
*env
, uint64_t f2
)
283 float128 ret
= float64_to_float128(f2
, &env
->fpu_status
);
284 handle_exceptions(env
, GETPC());
288 /* convert 32-bit float to 128-bit float */
289 uint64_t HELPER(lxeb
)(CPUS390XState
*env
, uint64_t f2
)
291 float128 ret
= float32_to_float128(f2
, &env
->fpu_status
);
292 handle_exceptions(env
, GETPC());
296 /* convert 64-bit float to 32-bit float */
297 uint64_t HELPER(ledb
)(CPUS390XState
*env
, uint64_t f2
)
299 float32 ret
= float64_to_float32(f2
, &env
->fpu_status
);
300 handle_exceptions(env
, GETPC());
304 /* convert 128-bit float to 32-bit float */
305 uint64_t HELPER(lexb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
307 float32 ret
= float128_to_float32(make_float128(ah
, al
), &env
->fpu_status
);
308 handle_exceptions(env
, GETPC());
312 /* 32-bit FP compare */
313 uint32_t HELPER(ceb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
315 int cmp
= float32_compare_quiet(f1
, f2
, &env
->fpu_status
);
316 handle_exceptions(env
, GETPC());
317 return float_comp_to_cc(env
, cmp
);
320 /* 64-bit FP compare */
321 uint32_t HELPER(cdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
323 int cmp
= float64_compare_quiet(f1
, f2
, &env
->fpu_status
);
324 handle_exceptions(env
, GETPC());
325 return float_comp_to_cc(env
, cmp
);
328 /* 128-bit FP compare */
329 uint32_t HELPER(cxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
330 uint64_t bh
, uint64_t bl
)
332 int cmp
= float128_compare_quiet(make_float128(ah
, al
),
333 make_float128(bh
, bl
),
335 handle_exceptions(env
, GETPC());
336 return float_comp_to_cc(env
, cmp
);
339 static int swap_round_mode(CPUS390XState
*env
, int m3
)
341 int ret
= env
->fpu_status
.float_rounding_mode
;
347 /* biased round no nearest */
349 /* round to nearest */
350 set_float_rounding_mode(float_round_nearest_even
, &env
->fpu_status
);
354 set_float_rounding_mode(float_round_to_zero
, &env
->fpu_status
);
358 set_float_rounding_mode(float_round_up
, &env
->fpu_status
);
362 set_float_rounding_mode(float_round_down
, &env
->fpu_status
);
368 /* convert 64-bit int to 32-bit float */
369 uint64_t HELPER(cegb
)(CPUS390XState
*env
, int64_t v2
, uint32_t m3
)
371 int hold
= swap_round_mode(env
, m3
);
372 float32 ret
= int64_to_float32(v2
, &env
->fpu_status
);
373 set_float_rounding_mode(hold
, &env
->fpu_status
);
374 handle_exceptions(env
, GETPC());
378 /* convert 64-bit int to 64-bit float */
379 uint64_t HELPER(cdgb
)(CPUS390XState
*env
, int64_t v2
, uint32_t m3
)
381 int hold
= swap_round_mode(env
, m3
);
382 float64 ret
= int64_to_float64(v2
, &env
->fpu_status
);
383 set_float_rounding_mode(hold
, &env
->fpu_status
);
384 handle_exceptions(env
, GETPC());
388 /* convert 64-bit int to 128-bit float */
389 uint64_t HELPER(cxgb
)(CPUS390XState
*env
, int64_t v2
, uint32_t m3
)
391 int hold
= swap_round_mode(env
, m3
);
392 float128 ret
= int64_to_float128(v2
, &env
->fpu_status
);
393 set_float_rounding_mode(hold
, &env
->fpu_status
);
394 handle_exceptions(env
, GETPC());
398 /* convert 32-bit float to 64-bit int */
399 uint64_t HELPER(cgeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
401 int hold
= swap_round_mode(env
, m3
);
402 int64_t ret
= float32_to_int64(v2
, &env
->fpu_status
);
403 set_float_rounding_mode(hold
, &env
->fpu_status
);
404 handle_exceptions(env
, GETPC());
408 /* convert 64-bit float to 64-bit int */
409 uint64_t HELPER(cgdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
411 int hold
= swap_round_mode(env
, m3
);
412 int64_t ret
= float64_to_int64(v2
, &env
->fpu_status
);
413 set_float_rounding_mode(hold
, &env
->fpu_status
);
414 handle_exceptions(env
, GETPC());
418 /* convert 128-bit float to 64-bit int */
419 uint64_t HELPER(cgxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
421 int hold
= swap_round_mode(env
, m3
);
422 float128 v2
= make_float128(h
, l
);
423 int64_t ret
= float128_to_int64(v2
, &env
->fpu_status
);
424 set_float_rounding_mode(hold
, &env
->fpu_status
);
425 handle_exceptions(env
, GETPC());
429 /* convert 32-bit float to 32-bit int */
430 uint64_t HELPER(cfeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
432 int hold
= swap_round_mode(env
, m3
);
433 int32_t ret
= float32_to_int32(v2
, &env
->fpu_status
);
434 set_float_rounding_mode(hold
, &env
->fpu_status
);
435 handle_exceptions(env
, GETPC());
439 /* convert 64-bit float to 32-bit int */
440 uint64_t HELPER(cfdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
442 int hold
= swap_round_mode(env
, m3
);
443 int32_t ret
= float64_to_int32(v2
, &env
->fpu_status
);
444 set_float_rounding_mode(hold
, &env
->fpu_status
);
445 handle_exceptions(env
, GETPC());
449 /* convert 128-bit float to 32-bit int */
450 uint64_t HELPER(cfxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
452 int hold
= swap_round_mode(env
, m3
);
453 float128 v2
= make_float128(h
, l
);
454 int32_t ret
= float128_to_int32(v2
, &env
->fpu_status
);
455 set_float_rounding_mode(hold
, &env
->fpu_status
);
456 handle_exceptions(env
, GETPC());
460 /* convert 32-bit float to 64-bit uint */
461 uint64_t HELPER(clgeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
463 int hold
= swap_round_mode(env
, m3
);
465 v2
= float32_to_float64(v2
, &env
->fpu_status
);
466 ret
= float64_to_uint64(v2
, &env
->fpu_status
);
467 set_float_rounding_mode(hold
, &env
->fpu_status
);
468 handle_exceptions(env
, GETPC());
472 /* convert 64-bit float to 64-bit uint */
473 uint64_t HELPER(clgdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
475 int hold
= swap_round_mode(env
, m3
);
476 uint64_t ret
= float64_to_uint64(v2
, &env
->fpu_status
);
477 set_float_rounding_mode(hold
, &env
->fpu_status
);
478 handle_exceptions(env
, GETPC());
482 /* convert 128-bit float to 64-bit uint */
483 uint64_t HELPER(clgxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
485 int hold
= swap_round_mode(env
, m3
);
486 float128 v2
= make_float128(h
, l
);
487 /* ??? Not 100% correct. */
488 uint64_t ret
= float128_to_int64(v2
, &env
->fpu_status
);
489 set_float_rounding_mode(hold
, &env
->fpu_status
);
490 handle_exceptions(env
, GETPC());
494 /* convert 32-bit float to 32-bit uint */
495 uint64_t HELPER(clfeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
497 int hold
= swap_round_mode(env
, m3
);
498 uint32_t ret
= float32_to_uint32(v2
, &env
->fpu_status
);
499 set_float_rounding_mode(hold
, &env
->fpu_status
);
500 handle_exceptions(env
, GETPC());
504 /* convert 64-bit float to 32-bit uint */
505 uint64_t HELPER(clfdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
507 int hold
= swap_round_mode(env
, m3
);
508 uint32_t ret
= float64_to_uint32(v2
, &env
->fpu_status
);
509 set_float_rounding_mode(hold
, &env
->fpu_status
);
510 handle_exceptions(env
, GETPC());
514 /* convert 128-bit float to 32-bit uint */
515 uint64_t HELPER(clfxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
517 int hold
= swap_round_mode(env
, m3
);
518 float128 v2
= make_float128(h
, l
);
519 /* Not 100% correct. */
520 uint32_t ret
= float128_to_int64(v2
, &env
->fpu_status
);
521 set_float_rounding_mode(hold
, &env
->fpu_status
);
522 handle_exceptions(env
, GETPC());
526 /* 32-bit FP multiply and add */
527 uint64_t HELPER(maeb
)(CPUS390XState
*env
, uint64_t f1
,
528 uint64_t f2
, uint64_t f3
)
530 float32 ret
= float32_muladd(f2
, f3
, f1
, 0, &env
->fpu_status
);
531 handle_exceptions(env
, GETPC());
535 /* 64-bit FP multiply and add */
536 uint64_t HELPER(madb
)(CPUS390XState
*env
, uint64_t f1
,
537 uint64_t f2
, uint64_t f3
)
539 float64 ret
= float64_muladd(f2
, f3
, f1
, 0, &env
->fpu_status
);
540 handle_exceptions(env
, GETPC());
544 /* 32-bit FP multiply and subtract */
545 uint64_t HELPER(mseb
)(CPUS390XState
*env
, uint64_t f1
,
546 uint64_t f2
, uint64_t f3
)
548 float32 ret
= float32_muladd(f2
, f3
, f1
, float_muladd_negate_c
,
550 handle_exceptions(env
, GETPC());
554 /* 64-bit FP multiply and subtract */
555 uint64_t HELPER(msdb
)(CPUS390XState
*env
, uint64_t f1
,
556 uint64_t f2
, uint64_t f3
)
558 float64 ret
= float64_muladd(f2
, f3
, f1
, float_muladd_negate_c
,
560 handle_exceptions(env
, GETPC());
564 /* test data class 32-bit */
565 uint32_t HELPER(tceb
)(uint64_t f1
, uint64_t m2
)
568 int neg
= float32_is_neg(v1
);
571 if ((float32_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
572 (float32_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
573 (float32_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
574 (float32_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
576 } else if (m2
& (1 << (9-neg
))) {
577 /* assume normalized number */
580 /* FIXME: denormalized? */
584 /* test data class 64-bit */
585 uint32_t HELPER(tcdb
)(uint64_t v1
, uint64_t m2
)
587 int neg
= float64_is_neg(v1
);
590 if ((float64_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
591 (float64_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
592 (float64_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
593 (float64_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
595 } else if (m2
& (1 << (9-neg
))) {
596 /* assume normalized number */
599 /* FIXME: denormalized? */
603 /* test data class 128-bit */
604 uint32_t HELPER(tcxb
)(uint64_t ah
, uint64_t al
, uint64_t m2
)
606 float128 v1
= make_float128(ah
, al
);
607 int neg
= float128_is_neg(v1
);
610 if ((float128_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
611 (float128_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
612 (float128_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
613 (float128_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
615 } else if (m2
& (1 << (9-neg
))) {
616 /* assume normalized number */
619 /* FIXME: denormalized? */
623 /* square root 32-bit */
624 uint64_t HELPER(sqeb
)(CPUS390XState
*env
, uint64_t f2
)
626 float32 ret
= float32_sqrt(f2
, &env
->fpu_status
);
627 handle_exceptions(env
, GETPC());
631 /* square root 64-bit */
632 uint64_t HELPER(sqdb
)(CPUS390XState
*env
, uint64_t f2
)
634 float64 ret
= float64_sqrt(f2
, &env
->fpu_status
);
635 handle_exceptions(env
, GETPC());
639 /* square root 128-bit */
640 uint64_t HELPER(sqxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
642 float128 ret
= float128_sqrt(make_float128(ah
, al
), &env
->fpu_status
);
643 handle_exceptions(env
, GETPC());
648 void HELPER(sfpc
)(CPUS390XState
*env
, uint64_t fpc
)
650 static const int rnd
[4] = {
651 float_round_nearest_even
,
657 /* Install everything in the main FPC. */
660 /* Install the rounding mode in the shadow fpu_status. */
661 set_float_rounding_mode(rnd
[fpc
& 3], &env
->fpu_status
);