2 * PowerPC Decimal Floating Point (DPF) emulation helpers for QEMU.
4 * Copyright (c) 2014 IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/helper-proto.h"
24 #define DECNUMDIGITS 34
25 #include "libdecnumber/decContext.h"
26 #include "libdecnumber/decNumber.h"
27 #include "libdecnumber/dpd/decimal32.h"
28 #include "libdecnumber/dpd/decimal64.h"
29 #include "libdecnumber/dpd/decimal128.h"
32 static void get_dfp64(ppc_vsr_t
*dst
, ppc_fprp_t
*dfp
)
34 dst
->VsrD(1) = dfp
->VsrD(0);
37 static void get_dfp128(ppc_vsr_t
*dst
, ppc_fprp_t
*dfp
)
39 dst
->VsrD(0) = dfp
[0].VsrD(0);
40 dst
->VsrD(1) = dfp
[1].VsrD(0);
43 static void set_dfp64(ppc_fprp_t
*dfp
, ppc_vsr_t
*src
)
45 dfp
->VsrD(0) = src
->VsrD(1);
48 static void set_dfp128(ppc_fprp_t
*dfp
, ppc_vsr_t
*src
)
50 dfp
[0].VsrD(0) = src
->VsrD(0);
51 dfp
[1].VsrD(0) = src
->VsrD(1);
62 static void dfp_prepare_rounding_mode(decContext
*context
, uint64_t fpscr
)
66 switch ((fpscr
& FP_DRN
) >> FPSCR_DRN0
) {
68 rnd
= DEC_ROUND_HALF_EVEN
;
74 rnd
= DEC_ROUND_CEILING
;
77 rnd
= DEC_ROUND_FLOOR
;
80 rnd
= DEC_ROUND_HALF_UP
;
83 rnd
= DEC_ROUND_HALF_DOWN
;
92 g_assert_not_reached();
95 decContextSetRounding(context
, rnd
);
98 static void dfp_set_round_mode_from_immediate(uint8_t r
, uint8_t rmc
,
105 rnd
= DEC_ROUND_HALF_EVEN
;
108 rnd
= DEC_ROUND_DOWN
;
111 rnd
= DEC_ROUND_HALF_UP
;
113 case 3: /* use FPSCR rounding mode */
116 assert(0); /* cannot get here */
118 } else { /* r == 1 */
121 rnd
= DEC_ROUND_CEILING
;
124 rnd
= DEC_ROUND_FLOOR
;
130 rnd
= DEC_ROUND_HALF_DOWN
;
133 assert(0); /* cannot get here */
136 decContextSetRounding(&dfp
->context
, rnd
);
139 static void dfp_prepare_decimal64(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
140 ppc_fprp_t
*b
, CPUPPCState
*env
)
142 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL64
);
143 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
147 get_dfp64(&dfp
->va
, a
);
148 decimal64ToNumber((decimal64
*)&dfp
->va
.VsrD(1), &dfp
->a
);
151 decNumberZero(&dfp
->a
);
155 get_dfp64(&dfp
->vb
, b
);
156 decimal64ToNumber((decimal64
*)&dfp
->vb
.VsrD(1), &dfp
->b
);
159 decNumberZero(&dfp
->b
);
163 static void dfp_prepare_decimal128(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
164 ppc_fprp_t
*b
, CPUPPCState
*env
)
166 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL128
);
167 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
171 get_dfp128(&dfp
->va
, a
);
172 decimal128ToNumber((decimal128
*)&dfp
->va
, &dfp
->a
);
174 dfp
->va
.VsrD(0) = dfp
->va
.VsrD(1) = 0;
175 decNumberZero(&dfp
->a
);
179 get_dfp128(&dfp
->vb
, b
);
180 decimal128ToNumber((decimal128
*)&dfp
->vb
, &dfp
->b
);
182 dfp
->vb
.VsrD(0) = dfp
->vb
.VsrD(1) = 0;
183 decNumberZero(&dfp
->b
);
187 static void dfp_finalize_decimal64(struct PPC_DFP
*dfp
)
189 decimal64FromNumber((decimal64
*)&dfp
->vt
.VsrD(1), &dfp
->t
, &dfp
->context
);
192 static void dfp_finalize_decimal128(struct PPC_DFP
*dfp
)
194 decimal128FromNumber((decimal128
*)&dfp
->vt
, &dfp
->t
, &dfp
->context
);
197 static void dfp_set_FPSCR_flag(struct PPC_DFP
*dfp
, uint64_t flag
,
200 dfp
->env
->fpscr
|= (flag
| FP_FX
);
201 if (dfp
->env
->fpscr
& enabled
) {
202 dfp
->env
->fpscr
|= FP_FEX
;
206 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP
*dfp
,
212 switch (decNumberClass(&dfp
->t
, context
)) {
219 case DEC_CLASS_NEG_INF
:
222 case DEC_CLASS_NEG_NORMAL
:
225 case DEC_CLASS_NEG_SUBNORMAL
:
228 case DEC_CLASS_NEG_ZERO
:
231 case DEC_CLASS_POS_ZERO
:
234 case DEC_CLASS_POS_SUBNORMAL
:
237 case DEC_CLASS_POS_NORMAL
:
240 case DEC_CLASS_POS_INF
:
244 assert(0); /* should never get here */
246 dfp
->env
->fpscr
&= ~FP_FPRF
;
247 dfp
->env
->fpscr
|= (fprf
<< FPSCR_FPRF
);
250 static void dfp_set_FPRF_from_FRT(struct PPC_DFP
*dfp
)
252 dfp_set_FPRF_from_FRT_with_context(dfp
, &dfp
->context
);
255 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP
*dfp
)
257 decContext shortContext
;
258 decContextDefault(&shortContext
, DEC_INIT_DECIMAL32
);
259 dfp_set_FPRF_from_FRT_with_context(dfp
, &shortContext
);
262 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP
*dfp
)
264 decContext longContext
;
265 decContextDefault(&longContext
, DEC_INIT_DECIMAL64
);
266 dfp_set_FPRF_from_FRT_with_context(dfp
, &longContext
);
269 static void dfp_check_for_OX(struct PPC_DFP
*dfp
)
271 if (dfp
->context
.status
& DEC_Overflow
) {
272 dfp_set_FPSCR_flag(dfp
, FP_OX
, FP_OE
);
276 static void dfp_check_for_UX(struct PPC_DFP
*dfp
)
278 if (dfp
->context
.status
& DEC_Underflow
) {
279 dfp_set_FPSCR_flag(dfp
, FP_UX
, FP_UE
);
283 static void dfp_check_for_XX(struct PPC_DFP
*dfp
)
285 if (dfp
->context
.status
& DEC_Inexact
) {
286 dfp_set_FPSCR_flag(dfp
, FP_XX
| FP_FI
, FP_XE
);
290 static void dfp_check_for_ZX(struct PPC_DFP
*dfp
)
292 if (dfp
->context
.status
& DEC_Division_by_zero
) {
293 dfp_set_FPSCR_flag(dfp
, FP_ZX
, FP_ZE
);
297 static void dfp_check_for_VXSNAN(struct PPC_DFP
*dfp
)
299 if (dfp
->context
.status
& DEC_Invalid_operation
) {
300 if (decNumberIsSNaN(&dfp
->a
) || decNumberIsSNaN(&dfp
->b
)) {
301 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
306 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP
*dfp
)
308 if (decNumberIsSNaN(&dfp
->t
)) {
309 dfp
->t
.bits
&= ~DECSNAN
;
310 dfp
->t
.bits
|= DECNAN
;
311 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
315 static void dfp_check_for_VXISI(struct PPC_DFP
*dfp
, int testForSameSign
)
317 if (dfp
->context
.status
& DEC_Invalid_operation
) {
318 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
319 int same
= decNumberClass(&dfp
->a
, &dfp
->context
) ==
320 decNumberClass(&dfp
->b
, &dfp
->context
);
321 if ((same
&& testForSameSign
) || (!same
&& !testForSameSign
)) {
322 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXISI
, FP_VE
);
328 static void dfp_check_for_VXISI_add(struct PPC_DFP
*dfp
)
330 dfp_check_for_VXISI(dfp
, 0);
333 static void dfp_check_for_VXISI_subtract(struct PPC_DFP
*dfp
)
335 dfp_check_for_VXISI(dfp
, 1);
338 static void dfp_check_for_VXIMZ(struct PPC_DFP
*dfp
)
340 if (dfp
->context
.status
& DEC_Invalid_operation
) {
341 if ((decNumberIsInfinite(&dfp
->a
) && decNumberIsZero(&dfp
->b
)) ||
342 (decNumberIsInfinite(&dfp
->b
) && decNumberIsZero(&dfp
->a
))) {
343 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIMZ
, FP_VE
);
348 static void dfp_check_for_VXZDZ(struct PPC_DFP
*dfp
)
350 if (dfp
->context
.status
& DEC_Division_undefined
) {
351 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXZDZ
, FP_VE
);
355 static void dfp_check_for_VXIDI(struct PPC_DFP
*dfp
)
357 if (dfp
->context
.status
& DEC_Invalid_operation
) {
358 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
359 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIDI
, FP_VE
);
364 static void dfp_check_for_VXVC(struct PPC_DFP
*dfp
)
366 if (decNumberIsNaN(&dfp
->a
) || decNumberIsNaN(&dfp
->b
)) {
367 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXVC
, FP_VE
);
371 static void dfp_check_for_VXCVI(struct PPC_DFP
*dfp
)
373 if ((dfp
->context
.status
& DEC_Invalid_operation
) &&
374 (!decNumberIsSNaN(&dfp
->a
)) &&
375 (!decNumberIsSNaN(&dfp
->b
))) {
376 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
380 static void dfp_set_CRBF_from_T(struct PPC_DFP
*dfp
)
382 if (decNumberIsNaN(&dfp
->t
)) {
384 } else if (decNumberIsZero(&dfp
->t
)) {
386 } else if (decNumberIsNegative(&dfp
->t
)) {
393 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP
*dfp
)
395 dfp
->env
->fpscr
&= ~FP_FPCC
;
396 dfp
->env
->fpscr
|= (dfp
->crbf
<< FPSCR_FPCC
);
399 static inline void dfp_makeQNaN(decNumber
*dn
)
401 dn
->bits
&= ~DECSPECIAL
;
405 static inline int dfp_get_digit(decNumber
*dn
, int n
)
407 assert(DECDPUN
== 3);
408 int unit
= n
/ DECDPUN
;
409 int dig
= n
% DECDPUN
;
412 return dn
->lsu
[unit
] % 10;
414 return (dn
->lsu
[unit
] / 10) % 10;
416 return dn
->lsu
[unit
] / 100;
418 g_assert_not_reached();
421 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
422 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
425 struct PPC_DFP dfp; \
426 dfp_prepare_decimal##size(&dfp, a, b, env); \
427 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
428 dfp_finalize_decimal##size(&dfp); \
430 set_dfp##size(t, &dfp.vt); \
433 static void ADD_PPs(struct PPC_DFP
*dfp
)
435 dfp_set_FPRF_from_FRT(dfp
);
436 dfp_check_for_OX(dfp
);
437 dfp_check_for_UX(dfp
);
438 dfp_check_for_XX(dfp
);
439 dfp_check_for_VXSNAN(dfp
);
440 dfp_check_for_VXISI_add(dfp
);
443 DFP_HELPER_TAB(dadd
, decNumberAdd
, ADD_PPs
, 64)
444 DFP_HELPER_TAB(daddq
, decNumberAdd
, ADD_PPs
, 128)
446 static void SUB_PPs(struct PPC_DFP
*dfp
)
448 dfp_set_FPRF_from_FRT(dfp
);
449 dfp_check_for_OX(dfp
);
450 dfp_check_for_UX(dfp
);
451 dfp_check_for_XX(dfp
);
452 dfp_check_for_VXSNAN(dfp
);
453 dfp_check_for_VXISI_subtract(dfp
);
456 DFP_HELPER_TAB(dsub
, decNumberSubtract
, SUB_PPs
, 64)
457 DFP_HELPER_TAB(dsubq
, decNumberSubtract
, SUB_PPs
, 128)
459 static void MUL_PPs(struct PPC_DFP
*dfp
)
461 dfp_set_FPRF_from_FRT(dfp
);
462 dfp_check_for_OX(dfp
);
463 dfp_check_for_UX(dfp
);
464 dfp_check_for_XX(dfp
);
465 dfp_check_for_VXSNAN(dfp
);
466 dfp_check_for_VXIMZ(dfp
);
469 DFP_HELPER_TAB(dmul
, decNumberMultiply
, MUL_PPs
, 64)
470 DFP_HELPER_TAB(dmulq
, decNumberMultiply
, MUL_PPs
, 128)
472 static void DIV_PPs(struct PPC_DFP
*dfp
)
474 dfp_set_FPRF_from_FRT(dfp
);
475 dfp_check_for_OX(dfp
);
476 dfp_check_for_UX(dfp
);
477 dfp_check_for_ZX(dfp
);
478 dfp_check_for_XX(dfp
);
479 dfp_check_for_VXSNAN(dfp
);
480 dfp_check_for_VXZDZ(dfp
);
481 dfp_check_for_VXIDI(dfp
);
484 DFP_HELPER_TAB(ddiv
, decNumberDivide
, DIV_PPs
, 64)
485 DFP_HELPER_TAB(ddivq
, decNumberDivide
, DIV_PPs
, 128)
487 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \
488 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
490 struct PPC_DFP dfp; \
491 dfp_prepare_decimal##size(&dfp, a, b, env); \
492 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
493 dfp_finalize_decimal##size(&dfp); \
498 static void CMPU_PPs(struct PPC_DFP
*dfp
)
500 dfp_set_CRBF_from_T(dfp
);
501 dfp_set_FPCC_from_CRBF(dfp
);
502 dfp_check_for_VXSNAN(dfp
);
505 DFP_HELPER_BF_AB(dcmpu
, decNumberCompare
, CMPU_PPs
, 64)
506 DFP_HELPER_BF_AB(dcmpuq
, decNumberCompare
, CMPU_PPs
, 128)
508 static void CMPO_PPs(struct PPC_DFP
*dfp
)
510 dfp_set_CRBF_from_T(dfp
);
511 dfp_set_FPCC_from_CRBF(dfp
);
512 dfp_check_for_VXSNAN(dfp
);
513 dfp_check_for_VXVC(dfp
);
516 DFP_HELPER_BF_AB(dcmpo
, decNumberCompare
, CMPO_PPs
, 64)
517 DFP_HELPER_BF_AB(dcmpoq
, decNumberCompare
, CMPO_PPs
, 128)
519 #define DFP_HELPER_TSTDC(op, size) \
520 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
522 struct PPC_DFP dfp; \
525 dfp_prepare_decimal##size(&dfp, a, 0, env); \
527 match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \
528 match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
529 match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \
530 match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \
531 match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \
532 match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \
534 if (decNumberIsNegative(&dfp.a)) { \
535 dfp.crbf = match ? 0xA : 0x8; \
537 dfp.crbf = match ? 0x2 : 0x0; \
540 dfp_set_FPCC_from_CRBF(&dfp); \
544 DFP_HELPER_TSTDC(dtstdc
, 64)
545 DFP_HELPER_TSTDC(dtstdcq
, 128)
547 #define DFP_HELPER_TSTDG(op, size) \
548 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
550 struct PPC_DFP dfp; \
551 int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \
552 is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \
555 dfp_prepare_decimal##size(&dfp, a, 0, env); \
557 if ((size) == 64) { \
562 } else if ((size) == 128) { \
569 is_negative = decNumberIsNegative(&dfp.a); \
570 is_zero = decNumberIsZero(&dfp.a); \
571 is_extreme_exp = (dfp.a.exponent == maxexp) || \
572 (dfp.a.exponent == minexp); \
573 is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \
574 is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \
575 leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \
576 (dfp.a.lsu[nzero_idx] != 0); \
579 match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \
580 match |= (dcm & 0x10) && is_zero && is_extreme_exp; \
581 match |= (dcm & 0x08) && \
582 (is_subnormal || (is_normal && is_extreme_exp)); \
583 match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \
584 !leftmost_is_nonzero; \
585 match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \
586 leftmost_is_nonzero; \
587 match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \
590 dfp.crbf = match ? 0xA : 0x8; \
592 dfp.crbf = match ? 0x2 : 0x0; \
595 dfp_set_FPCC_from_CRBF(&dfp); \
599 DFP_HELPER_TSTDG(dtstdg
, 64)
600 DFP_HELPER_TSTDG(dtstdgq
, 128)
602 #define DFP_HELPER_TSTEX(op, size) \
603 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
605 struct PPC_DFP dfp; \
606 int expa, expb, a_is_special, b_is_special; \
608 dfp_prepare_decimal##size(&dfp, a, b, env); \
610 expa = dfp.a.exponent; \
611 expb = dfp.b.exponent; \
612 a_is_special = decNumberIsSpecial(&dfp.a); \
613 b_is_special = decNumberIsSpecial(&dfp.b); \
615 if (a_is_special || b_is_special) { \
616 int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
617 int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
618 dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \
619 } else if (expa < expb) { \
621 } else if (expa > expb) { \
627 dfp_set_FPCC_from_CRBF(&dfp); \
631 DFP_HELPER_TSTEX(dtstex
, 64)
632 DFP_HELPER_TSTEX(dtstexq
, 128)
634 #define DFP_HELPER_TSTSF(op, size) \
635 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
637 struct PPC_DFP dfp; \
641 dfp_prepare_decimal##size(&dfp, 0, b, env); \
644 k = va.VsrD(1) & 0x3F; \
646 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
648 } else if (k == 0) { \
650 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
651 /* Zero has no sig digits */ \
654 unsigned nsd = dfp.b.digits; \
657 } else if (k > nsd) { \
664 dfp_set_FPCC_from_CRBF(&dfp); \
668 DFP_HELPER_TSTSF(dtstsf
, 64)
669 DFP_HELPER_TSTSF(dtstsfq
, 128)
671 #define DFP_HELPER_TSTSFI(op, size) \
672 uint32_t helper_##op(CPUPPCState *env, uint32_t a, ppc_fprp_t *b) \
674 struct PPC_DFP dfp; \
677 dfp_prepare_decimal##size(&dfp, 0, b, env); \
681 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
683 } else if (uim == 0) { \
685 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
686 /* Zero has no sig digits */ \
689 unsigned nsd = dfp.b.digits; \
692 } else if (uim > nsd) { \
699 dfp_set_FPCC_from_CRBF(&dfp); \
703 DFP_HELPER_TSTSFI(dtstsfi
, 64)
704 DFP_HELPER_TSTSFI(dtstsfiq
, 128)
706 static void QUA_PPs(struct PPC_DFP
*dfp
)
708 dfp_set_FPRF_from_FRT(dfp
);
709 dfp_check_for_XX(dfp
);
710 dfp_check_for_VXSNAN(dfp
);
711 dfp_check_for_VXCVI(dfp
);
714 static void dfp_quantize(uint8_t rmc
, struct PPC_DFP
*dfp
)
716 dfp_set_round_mode_from_immediate(0, rmc
, dfp
);
717 decNumberQuantize(&dfp
->t
, &dfp
->b
, &dfp
->a
, &dfp
->context
);
718 if (decNumberIsSNaN(&dfp
->a
)) {
720 dfp_makeQNaN(&dfp
->t
);
721 } else if (decNumberIsSNaN(&dfp
->b
)) {
723 dfp_makeQNaN(&dfp
->t
);
724 } else if (decNumberIsQNaN(&dfp
->a
)) {
726 } else if (decNumberIsQNaN(&dfp
->b
)) {
731 #define DFP_HELPER_QUAI(op, size) \
732 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
733 uint32_t te, uint32_t rmc) \
735 struct PPC_DFP dfp; \
737 dfp_prepare_decimal##size(&dfp, 0, b, env); \
739 decNumberFromUInt32(&dfp.a, 1); \
740 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
742 dfp_quantize(rmc, &dfp); \
743 dfp_finalize_decimal##size(&dfp); \
746 set_dfp##size(t, &dfp.vt); \
749 DFP_HELPER_QUAI(dquai
, 64)
750 DFP_HELPER_QUAI(dquaiq
, 128)
752 #define DFP_HELPER_QUA(op, size) \
753 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
754 ppc_fprp_t *b, uint32_t rmc) \
756 struct PPC_DFP dfp; \
758 dfp_prepare_decimal##size(&dfp, a, b, env); \
760 dfp_quantize(rmc, &dfp); \
761 dfp_finalize_decimal##size(&dfp); \
764 set_dfp##size(t, &dfp.vt); \
767 DFP_HELPER_QUA(dqua
, 64)
768 DFP_HELPER_QUA(dquaq
, 128)
770 static void _dfp_reround(uint8_t rmc
, int32_t ref_sig
, int32_t xmax
,
773 int msd_orig
, msd_rslt
;
775 if (unlikely((ref_sig
== 0) || (dfp
->b
.digits
<= ref_sig
))) {
777 if (decNumberIsSNaN(&dfp
->b
)) {
778 dfp_makeQNaN(&dfp
->t
);
779 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FPSCR_VE
);
784 /* Reround is equivalent to quantizing b with 1**E(n) where */
785 /* n = exp(b) + numDigits(b) - reference_significance. */
787 decNumberFromUInt32(&dfp
->a
, 1);
788 dfp
->a
.exponent
= dfp
->b
.exponent
+ dfp
->b
.digits
- ref_sig
;
790 if (unlikely(dfp
->a
.exponent
> xmax
)) {
792 dfp
->t
.bits
&= ~DECNEG
;
793 dfp_makeQNaN(&dfp
->t
);
794 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FPSCR_VE
);
798 dfp_quantize(rmc
, dfp
);
800 msd_orig
= dfp_get_digit(&dfp
->b
, dfp
->b
.digits
-1);
801 msd_rslt
= dfp_get_digit(&dfp
->t
, dfp
->t
.digits
-1);
803 /* If the quantization resulted in rounding up to the next magnitude, */
804 /* then we need to shift the significand and adjust the exponent. */
806 if (unlikely((msd_orig
== 9) && (msd_rslt
== 1))) {
810 decNumberFromInt32(&negone
, -1);
811 decNumberShift(&dfp
->t
, &dfp
->t
, &negone
, &dfp
->context
);
814 if (unlikely(dfp
->t
.exponent
> xmax
)) {
815 dfp_makeQNaN(&dfp
->t
);
817 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
818 /* Inhibit XX in this case */
819 decContextClearStatus(&dfp
->context
, DEC_Inexact
);
824 #define DFP_HELPER_RRND(op, size) \
825 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
826 ppc_fprp_t *b, uint32_t rmc) \
828 struct PPC_DFP dfp; \
831 int32_t xmax = ((size) == 64) ? 369 : 6111; \
833 dfp_prepare_decimal##size(&dfp, 0, b, env); \
836 ref_sig = va.VsrD(1) & 0x3f; \
838 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
839 dfp_finalize_decimal##size(&dfp); \
842 set_dfp##size(t, &dfp.vt); \
845 DFP_HELPER_RRND(drrnd
, 64)
846 DFP_HELPER_RRND(drrndq
, 128)
848 #define DFP_HELPER_RINT(op, postprocs, size) \
849 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
850 uint32_t r, uint32_t rmc) \
852 struct PPC_DFP dfp; \
854 dfp_prepare_decimal##size(&dfp, 0, b, env); \
856 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
857 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
858 dfp_finalize_decimal##size(&dfp); \
861 set_dfp##size(t, &dfp.vt); \
864 static void RINTX_PPs(struct PPC_DFP
*dfp
)
866 dfp_set_FPRF_from_FRT(dfp
);
867 dfp_check_for_XX(dfp
);
868 dfp_check_for_VXSNAN(dfp
);
871 DFP_HELPER_RINT(drintx
, RINTX_PPs
, 64)
872 DFP_HELPER_RINT(drintxq
, RINTX_PPs
, 128)
874 static void RINTN_PPs(struct PPC_DFP
*dfp
)
876 dfp_set_FPRF_from_FRT(dfp
);
877 dfp_check_for_VXSNAN(dfp
);
880 DFP_HELPER_RINT(drintn
, RINTN_PPs
, 64)
881 DFP_HELPER_RINT(drintnq
, RINTN_PPs
, 128)
883 void helper_dctdp(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
890 b_short
= (uint32_t)vb
.VsrD(1);
892 dfp_prepare_decimal64(&dfp
, 0, 0, env
);
893 decimal32ToNumber((decimal32
*)&b_short
, &dfp
.t
);
894 dfp_finalize_decimal64(&dfp
);
895 set_dfp64(t
, &dfp
.vt
);
896 dfp_set_FPRF_from_FRT(&dfp
);
899 void helper_dctqpq(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
903 dfp_prepare_decimal128(&dfp
, 0, 0, env
);
905 decimal64ToNumber((decimal64
*)&vb
.VsrD(1), &dfp
.t
);
907 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
908 dfp_set_FPRF_from_FRT(&dfp
);
910 dfp_finalize_decimal128(&dfp
);
911 set_dfp128(t
, &dfp
.vt
);
914 void helper_drsp(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
917 uint32_t t_short
= 0;
919 dfp_prepare_decimal64(&dfp
, 0, b
, env
);
920 decimal32FromNumber((decimal32
*)&t_short
, &dfp
.b
, &dfp
.context
);
921 decimal32ToNumber((decimal32
*)&t_short
, &dfp
.t
);
923 dfp_set_FPRF_from_FRT_short(&dfp
);
924 dfp_check_for_OX(&dfp
);
925 dfp_check_for_UX(&dfp
);
926 dfp_check_for_XX(&dfp
);
928 vt
.VsrD(1) = (uint64_t)t_short
;
932 void helper_drdpq(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
935 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
936 decimal64FromNumber((decimal64
*)&dfp
.vt
.VsrD(1), &dfp
.b
, &dfp
.context
);
937 decimal64ToNumber((decimal64
*)&dfp
.vt
.VsrD(1), &dfp
.t
);
939 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
940 dfp_set_FPRF_from_FRT_long(&dfp
);
941 dfp_check_for_OX(&dfp
);
942 dfp_check_for_UX(&dfp
);
943 dfp_check_for_XX(&dfp
);
945 dfp
.vt
.VsrD(0) = dfp
.vt
.VsrD(1) = 0;
946 dfp_finalize_decimal64(&dfp
);
947 set_dfp128(t
, &dfp
.vt
);
950 #define DFP_HELPER_CFFIX(op, size) \
951 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
953 struct PPC_DFP dfp; \
955 dfp_prepare_decimal##size(&dfp, 0, b, env); \
957 decNumberFromInt64(&dfp.t, (int64_t)vb.VsrD(1)); \
958 dfp_finalize_decimal##size(&dfp); \
961 set_dfp##size(t, &dfp.vt); \
964 static void CFFIX_PPs(struct PPC_DFP
*dfp
)
966 dfp_set_FPRF_from_FRT(dfp
);
967 dfp_check_for_XX(dfp
);
970 DFP_HELPER_CFFIX(dcffix
, 64)
971 DFP_HELPER_CFFIX(dcffixq
, 128)
973 #define DFP_HELPER_CTFIX(op, size) \
974 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
976 struct PPC_DFP dfp; \
977 dfp_prepare_decimal##size(&dfp, 0, b, env); \
979 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
980 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
981 if (decNumberIsInfinite(&dfp.b)) { \
982 dfp.vt.VsrD(1) = decNumberIsNegative(&dfp.b) ? INT64_MIN : \
985 dfp.vt.VsrD(1) = INT64_MIN; \
986 if (decNumberIsSNaN(&dfp.b)) { \
987 invalid_flags |= FP_VXSNAN; \
990 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
991 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
992 dfp.vt.VsrD(1) = 0; \
994 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
995 dfp.vt.VsrD(1) = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
996 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
997 dfp.vt.VsrD(1) = decNumberIsNegative(&dfp.b) ? INT64_MIN : \
999 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
1001 dfp_check_for_XX(&dfp); \
1005 set_dfp64(t, &dfp.vt); \
1008 DFP_HELPER_CTFIX(dctfix
, 64)
1009 DFP_HELPER_CTFIX(dctfixq
, 128)
1011 static inline void dfp_set_bcd_digit_64(ppc_vsr_t
*t
, uint8_t digit
,
1014 t
->VsrD(1) |= ((uint64_t)(digit
& 0xF) << (n
<< 2));
1017 static inline void dfp_set_bcd_digit_128(ppc_vsr_t
*t
, uint8_t digit
,
1020 t
->VsrD((n
& 0x10) ? 0 : 1) |=
1021 ((uint64_t)(digit
& 0xF) << ((n
& 15) << 2));
1024 static inline void dfp_set_sign_64(ppc_vsr_t
*t
, uint8_t sgn
)
1027 t
->VsrD(1) |= (sgn
& 0xF);
1030 static inline void dfp_set_sign_128(ppc_vsr_t
*t
, uint8_t sgn
)
1033 t
->VsrD(0) |= (t
->VsrD(1) >> 60);
1035 t
->VsrD(1) |= (sgn
& 0xF);
1038 #define DFP_HELPER_DEDPD(op, size) \
1039 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1042 struct PPC_DFP dfp; \
1043 uint8_t digits[34]; \
1046 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1048 decNumberGetBCD(&dfp.b, digits); \
1049 dfp.vt.VsrD(0) = dfp.vt.VsrD(1) = 0; \
1052 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1053 dfp_set_bcd_digit_##size(&dfp.vt, digits[N - i - 1], i); \
1059 if (decNumberIsNegative(&dfp.b)) { \
1062 sgn = ((sp & 1) ? 0xF : 0xC); \
1064 dfp_set_sign_##size(&dfp.vt, sgn); \
1067 set_dfp##size(t, &dfp.vt); \
1070 DFP_HELPER_DEDPD(ddedpd
, 64)
1071 DFP_HELPER_DEDPD(ddedpdq
, 128)
1073 static inline uint8_t dfp_get_bcd_digit_64(ppc_vsr_t
*t
, unsigned n
)
1075 return t
->VsrD(1) >> ((n
<< 2) & 63) & 15;
1078 static inline uint8_t dfp_get_bcd_digit_128(ppc_vsr_t
*t
, unsigned n
)
1080 return t
->VsrD((n
& 0x10) ? 0 : 1) >> ((n
<< 2) & 63) & 15;
1083 #define DFP_HELPER_ENBCD(op, size) \
1084 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1087 struct PPC_DFP dfp; \
1088 uint8_t digits[32]; \
1089 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1091 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1093 decNumberZero(&dfp.t); \
1096 uint8_t sgnNibble = dfp_get_bcd_digit_##size(&dfp.vb, offset++); \
1097 switch (sgnNibble) { \
1109 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1114 while (offset < (size) / 4) { \
1116 digits[(size) / 4 - n] = dfp_get_bcd_digit_##size(&dfp.vb, \
1118 if (digits[(size) / 4 - n] > 10) { \
1119 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1122 nonzero |= (digits[(size) / 4 - n] > 0); \
1127 decNumberSetBCD(&dfp.t, digits + ((size) / 4) - n, n); \
1131 dfp.t.bits |= DECNEG; \
1133 dfp_finalize_decimal##size(&dfp); \
1134 dfp_set_FPRF_from_FRT(&dfp); \
1135 set_dfp##size(t, &dfp.vt); \
1138 DFP_HELPER_ENBCD(denbcd
, 64)
1139 DFP_HELPER_ENBCD(denbcdq
, 128)
1141 #define DFP_HELPER_XEX(op, size) \
1142 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
1144 struct PPC_DFP dfp; \
1147 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1149 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1150 if (decNumberIsInfinite(&dfp.b)) { \
1152 } else if (decNumberIsSNaN(&dfp.b)) { \
1154 } else if (decNumberIsQNaN(&dfp.b)) { \
1159 set_dfp64(t, &vt); \
1161 if ((size) == 64) { \
1162 vt.VsrD(1) = dfp.b.exponent + 398; \
1163 } else if ((size) == 128) { \
1164 vt.VsrD(1) = dfp.b.exponent + 6176; \
1168 set_dfp64(t, &vt); \
1172 DFP_HELPER_XEX(dxex
, 64)
1173 DFP_HELPER_XEX(dxexq
, 128)
1175 static void dfp_set_raw_exp_64(ppc_vsr_t
*t
, uint64_t raw
)
1177 t
->VsrD(1) &= 0x8003ffffffffffffULL
;
1178 t
->VsrD(1) |= (raw
<< (63 - 13));
1181 static void dfp_set_raw_exp_128(ppc_vsr_t
*t
, uint64_t raw
)
1183 t
->VsrD(0) &= 0x80003fffffffffffULL
;
1184 t
->VsrD(0) |= (raw
<< (63 - 17));
1187 #define DFP_HELPER_IEX(op, size) \
1188 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1191 struct PPC_DFP dfp; \
1192 uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \
1197 get_dfp64(&va, a); \
1198 exp = (int64_t)va.VsrD(1); \
1199 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1201 if ((size) == 64) { \
1203 raw_qnan = 0x1F00; \
1204 raw_snan = 0x1F80; \
1207 } else if ((size) == 128) { \
1209 raw_qnan = 0x1f000; \
1210 raw_snan = 0x1f800; \
1211 raw_inf = 0x1e000; \
1217 if (unlikely((exp < 0) || (exp > max_exp))) { \
1218 dfp.vt.VsrD(0) = dfp.vb.VsrD(0); \
1219 dfp.vt.VsrD(1) = dfp.vb.VsrD(1); \
1221 dfp_set_raw_exp_##size(&dfp.vt, raw_inf); \
1222 } else if (exp == -3) { \
1223 dfp_set_raw_exp_##size(&dfp.vt, raw_snan); \
1225 dfp_set_raw_exp_##size(&dfp.vt, raw_qnan); \
1229 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1230 dfp.t.bits &= ~DECSPECIAL; \
1232 dfp.t.exponent = exp - bias; \
1233 dfp_finalize_decimal##size(&dfp); \
1235 set_dfp##size(t, &dfp.vt); \
1238 DFP_HELPER_IEX(diex
, 64)
1239 DFP_HELPER_IEX(diexq
, 128)
1241 static void dfp_clear_lmd_from_g5msb(uint64_t *t
)
1244 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1245 /* from the left-most decimal digit (LMD) and the biased exponent. */
1246 /* This routine clears the LMD bits while preserving the exponent */
1247 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1248 /* Finite Numbers" in the Power ISA for additional details. */
1250 uint64_t g5msb
= (*t
>> 58) & 0x1F;
1252 if ((g5msb
>> 3) < 3) { /* LMD in [0-7] ? */
1253 *t
&= ~(7ULL << 58);
1255 switch (g5msb
& 7) {
1276 *t
&= ~(0x1fULL
<< 58);
1277 *t
|= (g5msb
<< 58);
1281 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1282 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1285 struct PPC_DFP dfp; \
1286 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1288 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1290 if (sh <= max_digits) { \
1293 unsigned special = dfp.a.bits & DECSPECIAL; \
1296 decNumberFromUInt32(&shd, sh); \
1298 decNumberFromInt32(&shd, -((int32_t)sh)); \
1301 dfp.a.bits &= ~DECSPECIAL; \
1302 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1304 dfp.t.bits |= special; \
1305 if (special && (dfp.t.digits >= max_digits)) { \
1306 dfp.t.digits = max_digits - 1; \
1309 dfp_finalize_decimal##size(&dfp); \
1311 if ((size) == 64) { \
1312 dfp.vt.VsrD(1) = dfp.va.VsrD(1) & \
1313 0xFFFC000000000000ULL; \
1314 dfp_clear_lmd_from_g5msb(&dfp.vt.VsrD(1)); \
1316 dfp.vt.VsrD(0) = dfp.va.VsrD(0) & \
1317 0xFFFFC00000000000ULL; \
1318 dfp_clear_lmd_from_g5msb(&dfp.vt.VsrD(0)); \
1319 dfp.vt.VsrD(1) = 0; \
1323 set_dfp##size(t, &dfp.vt); \
1326 DFP_HELPER_SHIFT(dscli
, 64, 1)
1327 DFP_HELPER_SHIFT(dscliq
, 128, 1)
1328 DFP_HELPER_SHIFT(dscri
, 64, 0)
1329 DFP_HELPER_SHIFT(dscriq
, 128, 0)