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"
31 #if defined(HOST_WORDS_BIGENDIAN)
39 static void get_dfp64(uint64_t *dst
, ppc_fprp_t
*dfp
)
41 dst
[0] = dfp
->VsrD(0);
44 static void get_dfp128(uint64_t *dst
, ppc_fprp_t
*dfp
)
46 dst
[HI_IDX
] = dfp
[0].VsrD(0);
47 dst
[LO_IDX
] = dfp
[1].VsrD(0);
50 static void set_dfp64(ppc_fprp_t
*dfp
, uint64_t *src
)
52 dfp
->VsrD(0) = src
[0];
55 static void set_dfp128(ppc_fprp_t
*dfp
, uint64_t *src
)
57 dfp
[0].VsrD(0) = src
[HI_IDX
];
58 dfp
[1].VsrD(0) = src
[LO_IDX
];
63 uint64_t t64
[2], a64
[2], b64
[2];
69 static void dfp_prepare_rounding_mode(decContext
*context
, uint64_t fpscr
)
73 switch ((fpscr
& FP_DRN
) >> FPSCR_DRN0
) {
75 rnd
= DEC_ROUND_HALF_EVEN
;
81 rnd
= DEC_ROUND_CEILING
;
84 rnd
= DEC_ROUND_FLOOR
;
87 rnd
= DEC_ROUND_HALF_UP
;
90 rnd
= DEC_ROUND_HALF_DOWN
;
99 g_assert_not_reached();
102 decContextSetRounding(context
, rnd
);
105 static void dfp_set_round_mode_from_immediate(uint8_t r
, uint8_t rmc
,
112 rnd
= DEC_ROUND_HALF_EVEN
;
115 rnd
= DEC_ROUND_DOWN
;
118 rnd
= DEC_ROUND_HALF_UP
;
120 case 3: /* use FPSCR rounding mode */
123 assert(0); /* cannot get here */
125 } else { /* r == 1 */
128 rnd
= DEC_ROUND_CEILING
;
131 rnd
= DEC_ROUND_FLOOR
;
137 rnd
= DEC_ROUND_HALF_DOWN
;
140 assert(0); /* cannot get here */
143 decContextSetRounding(&dfp
->context
, rnd
);
146 static void dfp_prepare_decimal64(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
147 ppc_fprp_t
*b
, CPUPPCState
*env
)
149 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL64
);
150 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
154 get_dfp64(dfp
->a64
, a
);
155 decimal64ToNumber((decimal64
*)dfp
->a64
, &dfp
->a
);
158 decNumberZero(&dfp
->a
);
162 get_dfp64(dfp
->b64
, b
);
163 decimal64ToNumber((decimal64
*)dfp
->b64
, &dfp
->b
);
166 decNumberZero(&dfp
->b
);
170 static void dfp_prepare_decimal128(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
171 ppc_fprp_t
*b
, CPUPPCState
*env
)
173 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL128
);
174 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
178 get_dfp128(dfp
->a64
, a
);
179 decimal128ToNumber((decimal128
*)dfp
->a64
, &dfp
->a
);
181 dfp
->a64
[0] = dfp
->a64
[1] = 0;
182 decNumberZero(&dfp
->a
);
186 get_dfp128(dfp
->b64
, b
);
187 decimal128ToNumber((decimal128
*)dfp
->b64
, &dfp
->b
);
189 dfp
->b64
[0] = dfp
->b64
[1] = 0;
190 decNumberZero(&dfp
->b
);
194 static void dfp_finalize_decimal64(struct PPC_DFP
*dfp
)
196 decimal64FromNumber((decimal64
*)&dfp
->t64
, &dfp
->t
, &dfp
->context
);
199 static void dfp_finalize_decimal128(struct PPC_DFP
*dfp
)
201 decimal128FromNumber((decimal128
*)&dfp
->t64
, &dfp
->t
, &dfp
->context
);
204 static void dfp_set_FPSCR_flag(struct PPC_DFP
*dfp
, uint64_t flag
,
207 dfp
->env
->fpscr
|= (flag
| FP_FX
);
208 if (dfp
->env
->fpscr
& enabled
) {
209 dfp
->env
->fpscr
|= FP_FEX
;
213 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP
*dfp
,
219 switch (decNumberClass(&dfp
->t
, context
)) {
226 case DEC_CLASS_NEG_INF
:
229 case DEC_CLASS_NEG_NORMAL
:
232 case DEC_CLASS_NEG_SUBNORMAL
:
235 case DEC_CLASS_NEG_ZERO
:
238 case DEC_CLASS_POS_ZERO
:
241 case DEC_CLASS_POS_SUBNORMAL
:
244 case DEC_CLASS_POS_NORMAL
:
247 case DEC_CLASS_POS_INF
:
251 assert(0); /* should never get here */
253 dfp
->env
->fpscr
&= ~FP_FPRF
;
254 dfp
->env
->fpscr
|= (fprf
<< FPSCR_FPRF
);
257 static void dfp_set_FPRF_from_FRT(struct PPC_DFP
*dfp
)
259 dfp_set_FPRF_from_FRT_with_context(dfp
, &dfp
->context
);
262 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP
*dfp
)
264 decContext shortContext
;
265 decContextDefault(&shortContext
, DEC_INIT_DECIMAL32
);
266 dfp_set_FPRF_from_FRT_with_context(dfp
, &shortContext
);
269 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP
*dfp
)
271 decContext longContext
;
272 decContextDefault(&longContext
, DEC_INIT_DECIMAL64
);
273 dfp_set_FPRF_from_FRT_with_context(dfp
, &longContext
);
276 static void dfp_check_for_OX(struct PPC_DFP
*dfp
)
278 if (dfp
->context
.status
& DEC_Overflow
) {
279 dfp_set_FPSCR_flag(dfp
, FP_OX
, FP_OE
);
283 static void dfp_check_for_UX(struct PPC_DFP
*dfp
)
285 if (dfp
->context
.status
& DEC_Underflow
) {
286 dfp_set_FPSCR_flag(dfp
, FP_UX
, FP_UE
);
290 static void dfp_check_for_XX(struct PPC_DFP
*dfp
)
292 if (dfp
->context
.status
& DEC_Inexact
) {
293 dfp_set_FPSCR_flag(dfp
, FP_XX
| FP_FI
, FP_XE
);
297 static void dfp_check_for_ZX(struct PPC_DFP
*dfp
)
299 if (dfp
->context
.status
& DEC_Division_by_zero
) {
300 dfp_set_FPSCR_flag(dfp
, FP_ZX
, FP_ZE
);
304 static void dfp_check_for_VXSNAN(struct PPC_DFP
*dfp
)
306 if (dfp
->context
.status
& DEC_Invalid_operation
) {
307 if (decNumberIsSNaN(&dfp
->a
) || decNumberIsSNaN(&dfp
->b
)) {
308 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
313 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP
*dfp
)
315 if (decNumberIsSNaN(&dfp
->t
)) {
316 dfp
->t
.bits
&= ~DECSNAN
;
317 dfp
->t
.bits
|= DECNAN
;
318 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
322 static void dfp_check_for_VXISI(struct PPC_DFP
*dfp
, int testForSameSign
)
324 if (dfp
->context
.status
& DEC_Invalid_operation
) {
325 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
326 int same
= decNumberClass(&dfp
->a
, &dfp
->context
) ==
327 decNumberClass(&dfp
->b
, &dfp
->context
);
328 if ((same
&& testForSameSign
) || (!same
&& !testForSameSign
)) {
329 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXISI
, FP_VE
);
335 static void dfp_check_for_VXISI_add(struct PPC_DFP
*dfp
)
337 dfp_check_for_VXISI(dfp
, 0);
340 static void dfp_check_for_VXISI_subtract(struct PPC_DFP
*dfp
)
342 dfp_check_for_VXISI(dfp
, 1);
345 static void dfp_check_for_VXIMZ(struct PPC_DFP
*dfp
)
347 if (dfp
->context
.status
& DEC_Invalid_operation
) {
348 if ((decNumberIsInfinite(&dfp
->a
) && decNumberIsZero(&dfp
->b
)) ||
349 (decNumberIsInfinite(&dfp
->b
) && decNumberIsZero(&dfp
->a
))) {
350 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIMZ
, FP_VE
);
355 static void dfp_check_for_VXZDZ(struct PPC_DFP
*dfp
)
357 if (dfp
->context
.status
& DEC_Division_undefined
) {
358 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXZDZ
, FP_VE
);
362 static void dfp_check_for_VXIDI(struct PPC_DFP
*dfp
)
364 if (dfp
->context
.status
& DEC_Invalid_operation
) {
365 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
366 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIDI
, FP_VE
);
371 static void dfp_check_for_VXVC(struct PPC_DFP
*dfp
)
373 if (decNumberIsNaN(&dfp
->a
) || decNumberIsNaN(&dfp
->b
)) {
374 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXVC
, FP_VE
);
378 static void dfp_check_for_VXCVI(struct PPC_DFP
*dfp
)
380 if ((dfp
->context
.status
& DEC_Invalid_operation
) &&
381 (!decNumberIsSNaN(&dfp
->a
)) &&
382 (!decNumberIsSNaN(&dfp
->b
))) {
383 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
387 static void dfp_set_CRBF_from_T(struct PPC_DFP
*dfp
)
389 if (decNumberIsNaN(&dfp
->t
)) {
391 } else if (decNumberIsZero(&dfp
->t
)) {
393 } else if (decNumberIsNegative(&dfp
->t
)) {
400 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP
*dfp
)
402 dfp
->env
->fpscr
&= ~FP_FPCC
;
403 dfp
->env
->fpscr
|= (dfp
->crbf
<< FPSCR_FPCC
);
406 static inline void dfp_makeQNaN(decNumber
*dn
)
408 dn
->bits
&= ~DECSPECIAL
;
412 static inline int dfp_get_digit(decNumber
*dn
, int n
)
414 assert(DECDPUN
== 3);
415 int unit
= n
/ DECDPUN
;
416 int dig
= n
% DECDPUN
;
419 return dn
->lsu
[unit
] % 10;
421 return (dn
->lsu
[unit
] / 10) % 10;
423 return dn
->lsu
[unit
] / 100;
425 g_assert_not_reached();
428 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
429 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
432 struct PPC_DFP dfp; \
433 dfp_prepare_decimal##size(&dfp, a, b, env); \
434 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
435 dfp_finalize_decimal##size(&dfp); \
438 set_dfp64(t, dfp.t64); \
439 } else if (size == 128) { \
440 set_dfp128(t, dfp.t64); \
444 static void ADD_PPs(struct PPC_DFP
*dfp
)
446 dfp_set_FPRF_from_FRT(dfp
);
447 dfp_check_for_OX(dfp
);
448 dfp_check_for_UX(dfp
);
449 dfp_check_for_XX(dfp
);
450 dfp_check_for_VXSNAN(dfp
);
451 dfp_check_for_VXISI_add(dfp
);
454 DFP_HELPER_TAB(dadd
, decNumberAdd
, ADD_PPs
, 64)
455 DFP_HELPER_TAB(daddq
, decNumberAdd
, ADD_PPs
, 128)
457 static void SUB_PPs(struct PPC_DFP
*dfp
)
459 dfp_set_FPRF_from_FRT(dfp
);
460 dfp_check_for_OX(dfp
);
461 dfp_check_for_UX(dfp
);
462 dfp_check_for_XX(dfp
);
463 dfp_check_for_VXSNAN(dfp
);
464 dfp_check_for_VXISI_subtract(dfp
);
467 DFP_HELPER_TAB(dsub
, decNumberSubtract
, SUB_PPs
, 64)
468 DFP_HELPER_TAB(dsubq
, decNumberSubtract
, SUB_PPs
, 128)
470 static void MUL_PPs(struct PPC_DFP
*dfp
)
472 dfp_set_FPRF_from_FRT(dfp
);
473 dfp_check_for_OX(dfp
);
474 dfp_check_for_UX(dfp
);
475 dfp_check_for_XX(dfp
);
476 dfp_check_for_VXSNAN(dfp
);
477 dfp_check_for_VXIMZ(dfp
);
480 DFP_HELPER_TAB(dmul
, decNumberMultiply
, MUL_PPs
, 64)
481 DFP_HELPER_TAB(dmulq
, decNumberMultiply
, MUL_PPs
, 128)
483 static void DIV_PPs(struct PPC_DFP
*dfp
)
485 dfp_set_FPRF_from_FRT(dfp
);
486 dfp_check_for_OX(dfp
);
487 dfp_check_for_UX(dfp
);
488 dfp_check_for_ZX(dfp
);
489 dfp_check_for_XX(dfp
);
490 dfp_check_for_VXSNAN(dfp
);
491 dfp_check_for_VXZDZ(dfp
);
492 dfp_check_for_VXIDI(dfp
);
495 DFP_HELPER_TAB(ddiv
, decNumberDivide
, DIV_PPs
, 64)
496 DFP_HELPER_TAB(ddivq
, decNumberDivide
, DIV_PPs
, 128)
498 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \
499 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
501 struct PPC_DFP dfp; \
502 dfp_prepare_decimal##size(&dfp, a, b, env); \
503 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
504 dfp_finalize_decimal##size(&dfp); \
509 static void CMPU_PPs(struct PPC_DFP
*dfp
)
511 dfp_set_CRBF_from_T(dfp
);
512 dfp_set_FPCC_from_CRBF(dfp
);
513 dfp_check_for_VXSNAN(dfp
);
516 DFP_HELPER_BF_AB(dcmpu
, decNumberCompare
, CMPU_PPs
, 64)
517 DFP_HELPER_BF_AB(dcmpuq
, decNumberCompare
, CMPU_PPs
, 128)
519 static void CMPO_PPs(struct PPC_DFP
*dfp
)
521 dfp_set_CRBF_from_T(dfp
);
522 dfp_set_FPCC_from_CRBF(dfp
);
523 dfp_check_for_VXSNAN(dfp
);
524 dfp_check_for_VXVC(dfp
);
527 DFP_HELPER_BF_AB(dcmpo
, decNumberCompare
, CMPO_PPs
, 64)
528 DFP_HELPER_BF_AB(dcmpoq
, decNumberCompare
, CMPO_PPs
, 128)
530 #define DFP_HELPER_TSTDC(op, size) \
531 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
533 struct PPC_DFP dfp; \
536 dfp_prepare_decimal##size(&dfp, a, 0, env); \
538 match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \
539 match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
540 match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \
541 match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \
542 match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \
543 match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \
545 if (decNumberIsNegative(&dfp.a)) { \
546 dfp.crbf = match ? 0xA : 0x8; \
548 dfp.crbf = match ? 0x2 : 0x0; \
551 dfp_set_FPCC_from_CRBF(&dfp); \
555 DFP_HELPER_TSTDC(dtstdc
, 64)
556 DFP_HELPER_TSTDC(dtstdcq
, 128)
558 #define DFP_HELPER_TSTDG(op, size) \
559 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
561 struct PPC_DFP dfp; \
562 int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \
563 is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \
566 dfp_prepare_decimal##size(&dfp, a, 0, env); \
568 if ((size) == 64) { \
573 } else if ((size) == 128) { \
580 is_negative = decNumberIsNegative(&dfp.a); \
581 is_zero = decNumberIsZero(&dfp.a); \
582 is_extreme_exp = (dfp.a.exponent == maxexp) || \
583 (dfp.a.exponent == minexp); \
584 is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \
585 is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \
586 leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \
587 (dfp.a.lsu[nzero_idx] != 0); \
590 match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \
591 match |= (dcm & 0x10) && is_zero && is_extreme_exp; \
592 match |= (dcm & 0x08) && \
593 (is_subnormal || (is_normal && is_extreme_exp)); \
594 match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \
595 !leftmost_is_nonzero; \
596 match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \
597 leftmost_is_nonzero; \
598 match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \
601 dfp.crbf = match ? 0xA : 0x8; \
603 dfp.crbf = match ? 0x2 : 0x0; \
606 dfp_set_FPCC_from_CRBF(&dfp); \
610 DFP_HELPER_TSTDG(dtstdg
, 64)
611 DFP_HELPER_TSTDG(dtstdgq
, 128)
613 #define DFP_HELPER_TSTEX(op, size) \
614 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
616 struct PPC_DFP dfp; \
617 int expa, expb, a_is_special, b_is_special; \
619 dfp_prepare_decimal##size(&dfp, a, b, env); \
621 expa = dfp.a.exponent; \
622 expb = dfp.b.exponent; \
623 a_is_special = decNumberIsSpecial(&dfp.a); \
624 b_is_special = decNumberIsSpecial(&dfp.b); \
626 if (a_is_special || b_is_special) { \
627 int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
628 int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
629 dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \
630 } else if (expa < expb) { \
632 } else if (expa > expb) { \
638 dfp_set_FPCC_from_CRBF(&dfp); \
642 DFP_HELPER_TSTEX(dtstex
, 64)
643 DFP_HELPER_TSTEX(dtstexq
, 128)
645 #define DFP_HELPER_TSTSF(op, size) \
646 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
648 struct PPC_DFP dfp; \
652 dfp_prepare_decimal##size(&dfp, 0, b, env); \
654 get_dfp64(&a64, a); \
657 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
659 } else if (k == 0) { \
661 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
662 /* Zero has no sig digits */ \
665 unsigned nsd = dfp.b.digits; \
668 } else if (k > nsd) { \
675 dfp_set_FPCC_from_CRBF(&dfp); \
679 DFP_HELPER_TSTSF(dtstsf
, 64)
680 DFP_HELPER_TSTSF(dtstsfq
, 128)
682 #define DFP_HELPER_TSTSFI(op, size) \
683 uint32_t helper_##op(CPUPPCState *env, uint32_t a, ppc_fprp_t *b) \
685 struct PPC_DFP dfp; \
688 dfp_prepare_decimal##size(&dfp, 0, b, env); \
692 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
694 } else if (uim == 0) { \
696 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
697 /* Zero has no sig digits */ \
700 unsigned nsd = dfp.b.digits; \
703 } else if (uim > nsd) { \
710 dfp_set_FPCC_from_CRBF(&dfp); \
714 DFP_HELPER_TSTSFI(dtstsfi
, 64)
715 DFP_HELPER_TSTSFI(dtstsfiq
, 128)
717 static void QUA_PPs(struct PPC_DFP
*dfp
)
719 dfp_set_FPRF_from_FRT(dfp
);
720 dfp_check_for_XX(dfp
);
721 dfp_check_for_VXSNAN(dfp
);
722 dfp_check_for_VXCVI(dfp
);
725 static void dfp_quantize(uint8_t rmc
, struct PPC_DFP
*dfp
)
727 dfp_set_round_mode_from_immediate(0, rmc
, dfp
);
728 decNumberQuantize(&dfp
->t
, &dfp
->b
, &dfp
->a
, &dfp
->context
);
729 if (decNumberIsSNaN(&dfp
->a
)) {
731 dfp_makeQNaN(&dfp
->t
);
732 } else if (decNumberIsSNaN(&dfp
->b
)) {
734 dfp_makeQNaN(&dfp
->t
);
735 } else if (decNumberIsQNaN(&dfp
->a
)) {
737 } else if (decNumberIsQNaN(&dfp
->b
)) {
742 #define DFP_HELPER_QUAI(op, size) \
743 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
744 uint32_t te, uint32_t rmc) \
746 struct PPC_DFP dfp; \
748 dfp_prepare_decimal##size(&dfp, 0, b, env); \
750 decNumberFromUInt32(&dfp.a, 1); \
751 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
753 dfp_quantize(rmc, &dfp); \
754 dfp_finalize_decimal##size(&dfp); \
758 set_dfp64(t, dfp.t64); \
759 } else if (size == 128) { \
760 set_dfp128(t, dfp.t64); \
764 DFP_HELPER_QUAI(dquai
, 64)
765 DFP_HELPER_QUAI(dquaiq
, 128)
767 #define DFP_HELPER_QUA(op, size) \
768 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
769 ppc_fprp_t *b, uint32_t rmc) \
771 struct PPC_DFP dfp; \
773 dfp_prepare_decimal##size(&dfp, a, b, env); \
775 dfp_quantize(rmc, &dfp); \
776 dfp_finalize_decimal##size(&dfp); \
780 set_dfp64(t, dfp.t64); \
781 } else if (size == 128) { \
782 set_dfp128(t, dfp.t64); \
786 DFP_HELPER_QUA(dqua
, 64)
787 DFP_HELPER_QUA(dquaq
, 128)
789 static void _dfp_reround(uint8_t rmc
, int32_t ref_sig
, int32_t xmax
,
792 int msd_orig
, msd_rslt
;
794 if (unlikely((ref_sig
== 0) || (dfp
->b
.digits
<= ref_sig
))) {
796 if (decNumberIsSNaN(&dfp
->b
)) {
797 dfp_makeQNaN(&dfp
->t
);
798 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FPSCR_VE
);
803 /* Reround is equivalent to quantizing b with 1**E(n) where */
804 /* n = exp(b) + numDigits(b) - reference_significance. */
806 decNumberFromUInt32(&dfp
->a
, 1);
807 dfp
->a
.exponent
= dfp
->b
.exponent
+ dfp
->b
.digits
- ref_sig
;
809 if (unlikely(dfp
->a
.exponent
> xmax
)) {
811 dfp
->t
.bits
&= ~DECNEG
;
812 dfp_makeQNaN(&dfp
->t
);
813 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FPSCR_VE
);
817 dfp_quantize(rmc
, dfp
);
819 msd_orig
= dfp_get_digit(&dfp
->b
, dfp
->b
.digits
-1);
820 msd_rslt
= dfp_get_digit(&dfp
->t
, dfp
->t
.digits
-1);
822 /* If the quantization resulted in rounding up to the next magnitude, */
823 /* then we need to shift the significand and adjust the exponent. */
825 if (unlikely((msd_orig
== 9) && (msd_rslt
== 1))) {
829 decNumberFromInt32(&negone
, -1);
830 decNumberShift(&dfp
->t
, &dfp
->t
, &negone
, &dfp
->context
);
833 if (unlikely(dfp
->t
.exponent
> xmax
)) {
834 dfp_makeQNaN(&dfp
->t
);
836 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
837 /* Inhibit XX in this case */
838 decContextClearStatus(&dfp
->context
, DEC_Inexact
);
843 #define DFP_HELPER_RRND(op, size) \
844 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
845 ppc_fprp_t *b, uint32_t rmc) \
847 struct PPC_DFP dfp; \
850 int32_t xmax = ((size) == 64) ? 369 : 6111; \
852 dfp_prepare_decimal##size(&dfp, 0, b, env); \
854 get_dfp64(&a64, a); \
855 ref_sig = a64 & 0x3f; \
857 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
858 dfp_finalize_decimal##size(&dfp); \
862 set_dfp64(t, dfp.t64); \
863 } else if (size == 128) { \
864 set_dfp128(t, dfp.t64); \
868 DFP_HELPER_RRND(drrnd
, 64)
869 DFP_HELPER_RRND(drrndq
, 128)
871 #define DFP_HELPER_RINT(op, postprocs, size) \
872 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
873 uint32_t r, uint32_t rmc) \
875 struct PPC_DFP dfp; \
877 dfp_prepare_decimal##size(&dfp, 0, b, env); \
879 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
880 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
881 dfp_finalize_decimal##size(&dfp); \
885 set_dfp64(t, dfp.t64); \
886 } else if (size == 128) { \
887 set_dfp128(t, dfp.t64); \
891 static void RINTX_PPs(struct PPC_DFP
*dfp
)
893 dfp_set_FPRF_from_FRT(dfp
);
894 dfp_check_for_XX(dfp
);
895 dfp_check_for_VXSNAN(dfp
);
898 DFP_HELPER_RINT(drintx
, RINTX_PPs
, 64)
899 DFP_HELPER_RINT(drintxq
, RINTX_PPs
, 128)
901 static void RINTN_PPs(struct PPC_DFP
*dfp
)
903 dfp_set_FPRF_from_FRT(dfp
);
904 dfp_check_for_VXSNAN(dfp
);
907 DFP_HELPER_RINT(drintn
, RINTN_PPs
, 64)
908 DFP_HELPER_RINT(drintnq
, RINTN_PPs
, 128)
910 void helper_dctdp(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
917 b_short
= (uint32_t)b64
;
919 dfp_prepare_decimal64(&dfp
, 0, 0, env
);
920 decimal32ToNumber((decimal32
*)&b_short
, &dfp
.t
);
921 dfp_finalize_decimal64(&dfp
);
922 set_dfp64(t
, dfp
.t64
);
923 dfp_set_FPRF_from_FRT(&dfp
);
926 void helper_dctqpq(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
930 dfp_prepare_decimal128(&dfp
, 0, 0, env
);
932 decimal64ToNumber((decimal64
*)&b64
, &dfp
.t
);
934 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
935 dfp_set_FPRF_from_FRT(&dfp
);
937 dfp_finalize_decimal128(&dfp
);
938 set_dfp128(t
, dfp
.t64
);
941 void helper_drsp(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
944 uint32_t t_short
= 0;
946 dfp_prepare_decimal64(&dfp
, 0, b
, env
);
947 decimal32FromNumber((decimal32
*)&t_short
, &dfp
.b
, &dfp
.context
);
948 decimal32ToNumber((decimal32
*)&t_short
, &dfp
.t
);
950 dfp_set_FPRF_from_FRT_short(&dfp
);
951 dfp_check_for_OX(&dfp
);
952 dfp_check_for_UX(&dfp
);
953 dfp_check_for_XX(&dfp
);
955 t64
= (uint64_t)t_short
;
959 void helper_drdpq(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
962 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
963 decimal64FromNumber((decimal64
*)&dfp
.t64
, &dfp
.b
, &dfp
.context
);
964 decimal64ToNumber((decimal64
*)&dfp
.t64
, &dfp
.t
);
966 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
967 dfp_set_FPRF_from_FRT_long(&dfp
);
968 dfp_check_for_OX(&dfp
);
969 dfp_check_for_UX(&dfp
);
970 dfp_check_for_XX(&dfp
);
972 dfp
.t64
[0] = dfp
.t64
[1] = 0;
973 dfp_finalize_decimal64(&dfp
);
974 set_dfp128(t
, dfp
.t64
);
977 #define DFP_HELPER_CFFIX(op, size) \
978 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
980 struct PPC_DFP dfp; \
982 dfp_prepare_decimal##size(&dfp, 0, b, env); \
983 get_dfp64(&b64, b); \
984 decNumberFromInt64(&dfp.t, (int64_t)b64); \
985 dfp_finalize_decimal##size(&dfp); \
989 set_dfp64(t, dfp.t64); \
990 } else if (size == 128) { \
991 set_dfp128(t, dfp.t64); \
995 static void CFFIX_PPs(struct PPC_DFP
*dfp
)
997 dfp_set_FPRF_from_FRT(dfp
);
998 dfp_check_for_XX(dfp
);
1001 DFP_HELPER_CFFIX(dcffix
, 64)
1002 DFP_HELPER_CFFIX(dcffixq
, 128)
1004 #define DFP_HELPER_CTFIX(op, size) \
1005 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
1007 struct PPC_DFP dfp; \
1008 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1010 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1011 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
1012 if (decNumberIsInfinite(&dfp.b)) { \
1013 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
1014 } else { /* NaN */ \
1015 dfp.t64[0] = INT64_MIN; \
1016 if (decNumberIsSNaN(&dfp.b)) { \
1017 invalid_flags |= FP_VXSNAN; \
1020 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
1021 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
1024 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
1025 dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
1026 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
1027 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
1028 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
1030 dfp_check_for_XX(&dfp); \
1034 set_dfp64(t, dfp.t64); \
1037 DFP_HELPER_CTFIX(dctfix
, 64)
1038 DFP_HELPER_CTFIX(dctfixq
, 128)
1040 static inline void dfp_set_bcd_digit_64(uint64_t *t
, uint8_t digit
,
1043 *t
|= ((uint64_t)(digit
& 0xF) << (n
<< 2));
1046 static inline void dfp_set_bcd_digit_128(uint64_t *t
, uint8_t digit
,
1049 t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] |=
1050 ((uint64_t)(digit
& 0xF) << ((n
& 15) << 2));
1053 static inline void dfp_set_sign_64(uint64_t *t
, uint8_t sgn
)
1059 static inline void dfp_set_sign_128(uint64_t *t
, uint8_t sgn
)
1062 t
[HI_IDX
] |= (t
[LO_IDX
] >> 60);
1064 t
[LO_IDX
] |= (sgn
& 0xF);
1067 #define DFP_HELPER_DEDPD(op, size) \
1068 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1071 struct PPC_DFP dfp; \
1072 uint8_t digits[34]; \
1075 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1077 decNumberGetBCD(&dfp.b, digits); \
1078 dfp.t64[0] = dfp.t64[1] = 0; \
1081 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1082 dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i); \
1088 if (decNumberIsNegative(&dfp.b)) { \
1091 sgn = ((sp & 1) ? 0xF : 0xC); \
1093 dfp_set_sign_##size(dfp.t64, sgn); \
1097 set_dfp64(t, dfp.t64); \
1098 } else if (size == 128) { \
1099 set_dfp128(t, dfp.t64); \
1103 DFP_HELPER_DEDPD(ddedpd
, 64)
1104 DFP_HELPER_DEDPD(ddedpdq
, 128)
1106 static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t
, unsigned n
)
1108 return *t
>> ((n
<< 2) & 63) & 15;
1111 static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t
, unsigned n
)
1113 return t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] >> ((n
<< 2) & 63) & 15;
1116 #define DFP_HELPER_ENBCD(op, size) \
1117 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1120 struct PPC_DFP dfp; \
1121 uint8_t digits[32]; \
1122 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1124 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1126 decNumberZero(&dfp.t); \
1129 uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1130 switch (sgnNibble) { \
1142 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1147 while (offset < (size) / 4) { \
1149 digits[(size) / 4 - n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1150 if (digits[(size) / 4 - n] > 10) { \
1151 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1154 nonzero |= (digits[(size) / 4 - n] > 0); \
1159 decNumberSetBCD(&dfp.t, digits + ((size) / 4) - n, n); \
1163 dfp.t.bits |= DECNEG; \
1165 dfp_finalize_decimal##size(&dfp); \
1166 dfp_set_FPRF_from_FRT(&dfp); \
1167 if ((size) == 64) { \
1168 set_dfp64(t, dfp.t64); \
1169 } else if ((size) == 128) { \
1170 set_dfp128(t, dfp.t64); \
1174 DFP_HELPER_ENBCD(denbcd
, 64)
1175 DFP_HELPER_ENBCD(denbcdq
, 128)
1177 #define DFP_HELPER_XEX(op, size) \
1178 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
1180 struct PPC_DFP dfp; \
1183 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1185 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1186 if (decNumberIsInfinite(&dfp.b)) { \
1188 } else if (decNumberIsSNaN(&dfp.b)) { \
1190 } else if (decNumberIsQNaN(&dfp.b)) { \
1195 set_dfp64(t, &t64); \
1197 if ((size) == 64) { \
1198 t64 = dfp.b.exponent + 398; \
1199 } else if ((size) == 128) { \
1200 t64 = dfp.b.exponent + 6176; \
1204 set_dfp64(t, &t64); \
1208 DFP_HELPER_XEX(dxex
, 64)
1209 DFP_HELPER_XEX(dxexq
, 128)
1211 static void dfp_set_raw_exp_64(uint64_t *t
, uint64_t raw
)
1213 *t
&= 0x8003ffffffffffffULL
;
1214 *t
|= (raw
<< (63 - 13));
1217 static void dfp_set_raw_exp_128(uint64_t *t
, uint64_t raw
)
1219 t
[HI_IDX
] &= 0x80003fffffffffffULL
;
1220 t
[HI_IDX
] |= (raw
<< (63 - 17));
1223 #define DFP_HELPER_IEX(op, size) \
1224 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1227 struct PPC_DFP dfp; \
1228 uint64_t raw_qnan, raw_snan, raw_inf, max_exp, a64; \
1232 get_dfp64(&a64, a); \
1233 exp = (int64_t)a64; \
1234 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1236 if ((size) == 64) { \
1238 raw_qnan = 0x1F00; \
1239 raw_snan = 0x1F80; \
1242 } else if ((size) == 128) { \
1244 raw_qnan = 0x1f000; \
1245 raw_snan = 0x1f800; \
1246 raw_inf = 0x1e000; \
1252 if (unlikely((exp < 0) || (exp > max_exp))) { \
1253 dfp.t64[0] = dfp.b64[0]; \
1254 dfp.t64[1] = dfp.b64[1]; \
1256 dfp_set_raw_exp_##size(dfp.t64, raw_inf); \
1257 } else if (exp == -3) { \
1258 dfp_set_raw_exp_##size(dfp.t64, raw_snan); \
1260 dfp_set_raw_exp_##size(dfp.t64, raw_qnan); \
1264 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1265 dfp.t.bits &= ~DECSPECIAL; \
1267 dfp.t.exponent = exp - bias; \
1268 dfp_finalize_decimal##size(&dfp); \
1271 set_dfp64(t, dfp.t64); \
1272 } else if (size == 128) { \
1273 set_dfp128(t, dfp.t64); \
1277 DFP_HELPER_IEX(diex
, 64)
1278 DFP_HELPER_IEX(diexq
, 128)
1280 static void dfp_clear_lmd_from_g5msb(uint64_t *t
)
1283 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1284 /* from the left-most decimal digit (LMD) and the biased exponent. */
1285 /* This routine clears the LMD bits while preserving the exponent */
1286 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1287 /* Finite Numbers" in the Power ISA for additional details. */
1289 uint64_t g5msb
= (*t
>> 58) & 0x1F;
1291 if ((g5msb
>> 3) < 3) { /* LMD in [0-7] ? */
1292 *t
&= ~(7ULL << 58);
1294 switch (g5msb
& 7) {
1315 *t
&= ~(0x1fULL
<< 58);
1316 *t
|= (g5msb
<< 58);
1320 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1321 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1324 struct PPC_DFP dfp; \
1325 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1327 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1329 if (sh <= max_digits) { \
1332 unsigned special = dfp.a.bits & DECSPECIAL; \
1335 decNumberFromUInt32(&shd, sh); \
1337 decNumberFromInt32(&shd, -((int32_t)sh)); \
1340 dfp.a.bits &= ~DECSPECIAL; \
1341 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1343 dfp.t.bits |= special; \
1344 if (special && (dfp.t.digits >= max_digits)) { \
1345 dfp.t.digits = max_digits - 1; \
1348 dfp_finalize_decimal##size(&dfp); \
1350 if ((size) == 64) { \
1351 dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL; \
1352 dfp_clear_lmd_from_g5msb(dfp.t64); \
1354 dfp.t64[HI_IDX] = dfp.a64[HI_IDX] & \
1355 0xFFFFC00000000000ULL; \
1356 dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX); \
1357 dfp.t64[LO_IDX] = 0; \
1361 if ((size) == 64) { \
1362 set_dfp64(t, dfp.t64); \
1364 set_dfp128(t, dfp.t64); \
1368 DFP_HELPER_SHIFT(dscli
, 64, 1)
1369 DFP_HELPER_SHIFT(dscliq
, 128, 1)
1370 DFP_HELPER_SHIFT(dscri
, 64, 0)
1371 DFP_HELPER_SHIFT(dscriq
, 128, 0)