]>
git.proxmox.com Git - qemu.git/blob - target-s390x/fpu_helper.c
ff3b435e893834b8ad294ef0bd5f1b1be605f92b
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 /* convert 32-bit int to 64-bit float */
138 void HELPER(cdfbr
)(CPUS390XState
*env
, uint32_t f1
, int32_t v2
)
140 HELPER_LOG("%s: converting %d to f%d\n", __func__
, v2
, f1
);
141 env
->fregs
[f1
].d
= int32_to_float64(v2
, &env
->fpu_status
);
144 /* convert 32-bit int to 128-bit float */
145 void HELPER(cxfbr
)(CPUS390XState
*env
, uint32_t f1
, int32_t v2
)
149 v1
.q
= int32_to_float128(v2
, &env
->fpu_status
);
150 env
->fregs
[f1
].ll
= v1
.ll
.upper
;
151 env
->fregs
[f1
+ 2].ll
= v1
.ll
.lower
;
154 /* convert 64-bit int to 32-bit float */
155 void HELPER(cegbr
)(CPUS390XState
*env
, uint32_t f1
, int64_t v2
)
157 HELPER_LOG("%s: converting %ld to f%d\n", __func__
, v2
, f1
);
158 env
->fregs
[f1
].l
.upper
= int64_to_float32(v2
, &env
->fpu_status
);
161 /* convert 64-bit int to 64-bit float */
162 void HELPER(cdgbr
)(CPUS390XState
*env
, uint32_t f1
, int64_t v2
)
164 HELPER_LOG("%s: converting %ld to f%d\n", __func__
, v2
, f1
);
165 env
->fregs
[f1
].d
= int64_to_float64(v2
, &env
->fpu_status
);
168 /* convert 64-bit int to 128-bit float */
169 void HELPER(cxgbr
)(CPUS390XState
*env
, uint32_t f1
, int64_t v2
)
173 x1
.q
= int64_to_float128(v2
, &env
->fpu_status
);
174 HELPER_LOG("%s: converted %ld to 0x%lx and 0x%lx\n", __func__
, v2
,
175 x1
.ll
.upper
, x1
.ll
.lower
);
176 env
->fregs
[f1
].ll
= x1
.ll
.upper
;
177 env
->fregs
[f1
+ 2].ll
= x1
.ll
.lower
;
180 /* convert 32-bit int to 32-bit float */
181 void HELPER(cefbr
)(CPUS390XState
*env
, uint32_t f1
, int32_t v2
)
183 env
->fregs
[f1
].l
.upper
= int32_to_float32(v2
, &env
->fpu_status
);
184 HELPER_LOG("%s: converting %d to 0x%d in f%d\n", __func__
, v2
,
185 env
->fregs
[f1
].l
.upper
, f1
);
188 /* 32-bit FP addition */
189 uint64_t HELPER(aeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
191 float32 ret
= float32_add(f1
, f2
, &env
->fpu_status
);
192 handle_exceptions(env
, GETPC());
196 /* 64-bit FP addition */
197 uint64_t HELPER(adb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
199 float64 ret
= float64_add(f1
, f2
, &env
->fpu_status
);
200 handle_exceptions(env
, GETPC());
204 /* 128-bit FP addition */
205 uint64_t HELPER(axb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
206 uint64_t bh
, uint64_t bl
)
208 float128 ret
= float128_add(make_float128(ah
, al
),
209 make_float128(bh
, bl
),
211 handle_exceptions(env
, GETPC());
215 /* 32-bit FP subtraction */
216 uint64_t HELPER(seb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
218 float32 ret
= float32_sub(f1
, f2
, &env
->fpu_status
);
219 handle_exceptions(env
, GETPC());
223 /* 64-bit FP subtraction */
224 uint64_t HELPER(sdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
226 float64 ret
= float64_sub(f1
, f2
, &env
->fpu_status
);
227 handle_exceptions(env
, GETPC());
231 /* 128-bit FP subtraction */
232 uint64_t HELPER(sxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
233 uint64_t bh
, uint64_t bl
)
235 float128 ret
= float128_sub(make_float128(ah
, al
),
236 make_float128(bh
, bl
),
238 handle_exceptions(env
, GETPC());
242 /* 32-bit FP division */
243 uint64_t HELPER(deb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
245 float32 ret
= float32_div(f1
, f2
, &env
->fpu_status
);
246 handle_exceptions(env
, GETPC());
250 /* 64-bit FP division */
251 uint64_t HELPER(ddb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
253 float64 ret
= float64_div(f1
, f2
, &env
->fpu_status
);
254 handle_exceptions(env
, GETPC());
258 /* 128-bit FP division */
259 uint64_t HELPER(dxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
260 uint64_t bh
, uint64_t bl
)
262 float128 ret
= float128_div(make_float128(ah
, al
),
263 make_float128(bh
, bl
),
265 handle_exceptions(env
, GETPC());
269 /* 32-bit FP multiplication */
270 uint64_t HELPER(meeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
272 float32 ret
= float32_mul(f1
, f2
, &env
->fpu_status
);
273 handle_exceptions(env
, GETPC());
277 /* 64-bit FP multiplication */
278 uint64_t HELPER(mdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
280 float64 ret
= float64_mul(f1
, f2
, &env
->fpu_status
);
281 handle_exceptions(env
, GETPC());
285 /* 64/32-bit FP multiplication */
286 uint64_t HELPER(mdeb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
288 float64 ret
= float32_to_float64(f2
, &env
->fpu_status
);
289 ret
= float64_mul(f1
, ret
, &env
->fpu_status
);
290 handle_exceptions(env
, GETPC());
294 /* 128-bit FP multiplication */
295 uint64_t HELPER(mxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
296 uint64_t bh
, uint64_t bl
)
298 float128 ret
= float128_mul(make_float128(ah
, al
),
299 make_float128(bh
, bl
),
301 handle_exceptions(env
, GETPC());
305 /* 128/64-bit FP multiplication */
306 uint64_t HELPER(mxdb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
309 float128 ret
= float64_to_float128(f2
, &env
->fpu_status
);
310 ret
= float128_mul(make_float128(ah
, al
), ret
, &env
->fpu_status
);
311 handle_exceptions(env
, GETPC());
315 /* convert 32-bit float to 64-bit float */
316 uint64_t HELPER(ldeb
)(CPUS390XState
*env
, uint64_t f2
)
318 float64 ret
= float32_to_float64(f2
, &env
->fpu_status
);
319 handle_exceptions(env
, GETPC());
323 /* convert 128-bit float to 64-bit float */
324 uint64_t HELPER(ldxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
326 float64 ret
= float128_to_float64(make_float128(ah
, al
), &env
->fpu_status
);
327 handle_exceptions(env
, GETPC());
331 /* convert 64-bit float to 128-bit float */
332 uint64_t HELPER(lxdb
)(CPUS390XState
*env
, uint64_t f2
)
334 float128 ret
= float64_to_float128(f2
, &env
->fpu_status
);
335 handle_exceptions(env
, GETPC());
339 /* convert 32-bit float to 128-bit float */
340 uint64_t HELPER(lxeb
)(CPUS390XState
*env
, uint64_t f2
)
342 float128 ret
= float32_to_float128(f2
, &env
->fpu_status
);
343 handle_exceptions(env
, GETPC());
347 /* convert 64-bit float to 32-bit float */
348 uint64_t HELPER(ledb
)(CPUS390XState
*env
, uint64_t f2
)
350 float32 ret
= float64_to_float32(f2
, &env
->fpu_status
);
351 handle_exceptions(env
, GETPC());
355 /* convert 128-bit float to 32-bit float */
356 uint64_t HELPER(lexb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
358 float32 ret
= float128_to_float32(make_float128(ah
, al
), &env
->fpu_status
);
359 handle_exceptions(env
, GETPC());
363 /* 32-bit FP compare */
364 uint32_t HELPER(ceb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
366 int cmp
= float32_compare_quiet(f1
, f2
, &env
->fpu_status
);
367 handle_exceptions(env
, GETPC());
368 return float_comp_to_cc(env
, cmp
);
371 /* 64-bit FP compare */
372 uint32_t HELPER(cdb
)(CPUS390XState
*env
, uint64_t f1
, uint64_t f2
)
374 int cmp
= float64_compare_quiet(f1
, f2
, &env
->fpu_status
);
375 handle_exceptions(env
, GETPC());
376 return float_comp_to_cc(env
, cmp
);
379 /* 128-bit FP compare */
380 uint32_t HELPER(cxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
381 uint64_t bh
, uint64_t bl
)
383 int cmp
= float128_compare_quiet(make_float128(ah
, al
),
384 make_float128(bh
, bl
),
386 handle_exceptions(env
, GETPC());
387 return float_comp_to_cc(env
, cmp
);
390 static int swap_round_mode(CPUS390XState
*env
, int m3
)
392 int ret
= env
->fpu_status
.float_rounding_mode
;
398 /* biased round no nearest */
400 /* round to nearest */
401 set_float_rounding_mode(float_round_nearest_even
, &env
->fpu_status
);
405 set_float_rounding_mode(float_round_to_zero
, &env
->fpu_status
);
409 set_float_rounding_mode(float_round_up
, &env
->fpu_status
);
413 set_float_rounding_mode(float_round_down
, &env
->fpu_status
);
419 /* convert 32-bit float to 64-bit int */
420 uint64_t HELPER(cgeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
422 int hold
= swap_round_mode(env
, m3
);
423 int64_t ret
= float32_to_int64(v2
, &env
->fpu_status
);
424 set_float_rounding_mode(hold
, &env
->fpu_status
);
425 handle_exceptions(env
, GETPC());
429 /* convert 64-bit float to 64-bit int */
430 uint64_t HELPER(cgdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
432 int hold
= swap_round_mode(env
, m3
);
433 int64_t ret
= float64_to_int64(v2
, &env
->fpu_status
);
434 set_float_rounding_mode(hold
, &env
->fpu_status
);
435 handle_exceptions(env
, GETPC());
439 /* convert 128-bit float to 64-bit int */
440 uint64_t HELPER(cgxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
442 int hold
= swap_round_mode(env
, m3
);
443 float128 v2
= make_float128(h
, l
);
444 int64_t ret
= float128_to_int64(v2
, &env
->fpu_status
);
445 set_float_rounding_mode(hold
, &env
->fpu_status
);
446 handle_exceptions(env
, GETPC());
450 /* convert 32-bit float to 32-bit int */
451 uint64_t HELPER(cfeb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
453 int hold
= swap_round_mode(env
, m3
);
454 int32_t ret
= float32_to_int32(v2
, &env
->fpu_status
);
455 set_float_rounding_mode(hold
, &env
->fpu_status
);
456 handle_exceptions(env
, GETPC());
460 /* convert 64-bit float to 32-bit int */
461 uint64_t HELPER(cfdb
)(CPUS390XState
*env
, uint64_t v2
, uint32_t m3
)
463 int hold
= swap_round_mode(env
, m3
);
464 int32_t ret
= float64_to_int32(v2
, &env
->fpu_status
);
465 set_float_rounding_mode(hold
, &env
->fpu_status
);
466 handle_exceptions(env
, GETPC());
470 /* convert 128-bit float to 32-bit int */
471 uint64_t HELPER(cfxb
)(CPUS390XState
*env
, uint64_t h
, uint64_t l
, uint32_t m3
)
473 int hold
= swap_round_mode(env
, m3
);
474 float128 v2
= make_float128(h
, l
);
475 int32_t ret
= float128_to_int32(v2
, &env
->fpu_status
);
476 set_float_rounding_mode(hold
, &env
->fpu_status
);
477 handle_exceptions(env
, GETPC());
481 /* 32-bit FP multiply and add */
482 uint64_t HELPER(maeb
)(CPUS390XState
*env
, uint64_t f1
,
483 uint64_t f2
, uint64_t f3
)
485 float32 ret
= float32_muladd(f2
, f3
, f1
, 0, &env
->fpu_status
);
486 handle_exceptions(env
, GETPC());
490 /* 64-bit FP multiply and add */
491 uint64_t HELPER(madb
)(CPUS390XState
*env
, uint64_t f1
,
492 uint64_t f2
, uint64_t f3
)
494 float64 ret
= float64_muladd(f2
, f3
, f1
, 0, &env
->fpu_status
);
495 handle_exceptions(env
, GETPC());
499 /* 32-bit FP multiply and subtract */
500 uint64_t HELPER(mseb
)(CPUS390XState
*env
, uint64_t f1
,
501 uint64_t f2
, uint64_t f3
)
503 float32 ret
= float32_muladd(f2
, f3
, f1
, float_muladd_negate_c
,
505 handle_exceptions(env
, GETPC());
509 /* 64-bit FP multiply and subtract */
510 uint64_t HELPER(msdb
)(CPUS390XState
*env
, uint64_t f1
,
511 uint64_t f2
, uint64_t f3
)
513 float64 ret
= float64_muladd(f2
, f3
, f1
, float_muladd_negate_c
,
515 handle_exceptions(env
, GETPC());
519 /* test data class 32-bit */
520 uint32_t HELPER(tceb
)(uint64_t f1
, uint64_t m2
)
523 int neg
= float32_is_neg(v1
);
526 if ((float32_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
527 (float32_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
528 (float32_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
529 (float32_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
531 } else if (m2
& (1 << (9-neg
))) {
532 /* assume normalized number */
535 /* FIXME: denormalized? */
539 /* test data class 64-bit */
540 uint32_t HELPER(tcdb
)(uint64_t v1
, uint64_t m2
)
542 int neg
= float64_is_neg(v1
);
545 if ((float64_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
546 (float64_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
547 (float64_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
548 (float64_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
550 } else if (m2
& (1 << (9-neg
))) {
551 /* assume normalized number */
554 /* FIXME: denormalized? */
558 /* test data class 128-bit */
559 uint32_t HELPER(tcxb
)(uint64_t ah
, uint64_t al
, uint64_t m2
)
561 float128 v1
= make_float128(ah
, al
);
562 int neg
= float128_is_neg(v1
);
565 if ((float128_is_zero(v1
) && (m2
& (1 << (11-neg
)))) ||
566 (float128_is_infinity(v1
) && (m2
& (1 << (5-neg
)))) ||
567 (float128_is_any_nan(v1
) && (m2
& (1 << (3-neg
)))) ||
568 (float128_is_signaling_nan(v1
) && (m2
& (1 << (1-neg
))))) {
570 } else if (m2
& (1 << (9-neg
))) {
571 /* assume normalized number */
574 /* FIXME: denormalized? */
578 /* square root 32-bit */
579 uint64_t HELPER(sqeb
)(CPUS390XState
*env
, uint64_t f2
)
581 float32 ret
= float32_sqrt(f2
, &env
->fpu_status
);
582 handle_exceptions(env
, GETPC());
586 /* square root 64-bit */
587 uint64_t HELPER(sqdb
)(CPUS390XState
*env
, uint64_t f2
)
589 float64 ret
= float64_sqrt(f2
, &env
->fpu_status
);
590 handle_exceptions(env
, GETPC());
594 /* square root 128-bit */
595 uint64_t HELPER(sqxb
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
)
597 float128 ret
= float128_sqrt(make_float128(ah
, al
), &env
->fpu_status
);
598 handle_exceptions(env
, GETPC());