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.1 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);
54 static void set_dfp128_to_avr(ppc_avr_t
*dst
, ppc_vsr_t
*src
)
67 static void dfp_prepare_rounding_mode(decContext
*context
, uint64_t fpscr
)
71 switch ((fpscr
& FP_DRN
) >> FPSCR_DRN0
) {
73 rnd
= DEC_ROUND_HALF_EVEN
;
79 rnd
= DEC_ROUND_CEILING
;
82 rnd
= DEC_ROUND_FLOOR
;
85 rnd
= DEC_ROUND_HALF_UP
;
88 rnd
= DEC_ROUND_HALF_DOWN
;
97 g_assert_not_reached();
100 decContextSetRounding(context
, rnd
);
103 static void dfp_set_round_mode_from_immediate(uint8_t r
, uint8_t rmc
,
110 rnd
= DEC_ROUND_HALF_EVEN
;
113 rnd
= DEC_ROUND_DOWN
;
116 rnd
= DEC_ROUND_HALF_UP
;
118 case 3: /* use FPSCR rounding mode */
121 assert(0); /* cannot get here */
123 } else { /* r == 1 */
126 rnd
= DEC_ROUND_CEILING
;
129 rnd
= DEC_ROUND_FLOOR
;
135 rnd
= DEC_ROUND_HALF_DOWN
;
138 assert(0); /* cannot get here */
141 decContextSetRounding(&dfp
->context
, rnd
);
144 static void dfp_prepare_decimal64(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
145 ppc_fprp_t
*b
, CPUPPCState
*env
)
147 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL64
);
148 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
152 get_dfp64(&dfp
->va
, a
);
153 decimal64ToNumber((decimal64
*)&dfp
->va
.VsrD(1), &dfp
->a
);
156 decNumberZero(&dfp
->a
);
160 get_dfp64(&dfp
->vb
, b
);
161 decimal64ToNumber((decimal64
*)&dfp
->vb
.VsrD(1), &dfp
->b
);
164 decNumberZero(&dfp
->b
);
168 static void dfp_prepare_decimal128(struct PPC_DFP
*dfp
, ppc_fprp_t
*a
,
169 ppc_fprp_t
*b
, CPUPPCState
*env
)
171 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL128
);
172 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
176 get_dfp128(&dfp
->va
, a
);
177 decimal128ToNumber((decimal128
*)&dfp
->va
, &dfp
->a
);
179 dfp
->va
.VsrD(0) = dfp
->va
.VsrD(1) = 0;
180 decNumberZero(&dfp
->a
);
184 get_dfp128(&dfp
->vb
, b
);
185 decimal128ToNumber((decimal128
*)&dfp
->vb
, &dfp
->b
);
187 dfp
->vb
.VsrD(0) = dfp
->vb
.VsrD(1) = 0;
188 decNumberZero(&dfp
->b
);
192 static void dfp_finalize_decimal64(struct PPC_DFP
*dfp
)
194 decimal64FromNumber((decimal64
*)&dfp
->vt
.VsrD(1), &dfp
->t
, &dfp
->context
);
197 static void dfp_finalize_decimal128(struct PPC_DFP
*dfp
)
199 decimal128FromNumber((decimal128
*)&dfp
->vt
, &dfp
->t
, &dfp
->context
);
202 static void dfp_set_FPSCR_flag(struct PPC_DFP
*dfp
, uint64_t flag
,
205 dfp
->env
->fpscr
|= (flag
| FP_FX
);
206 if (dfp
->env
->fpscr
& enabled
) {
207 dfp
->env
->fpscr
|= FP_FEX
;
211 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP
*dfp
,
217 switch (decNumberClass(&dfp
->t
, context
)) {
224 case DEC_CLASS_NEG_INF
:
227 case DEC_CLASS_NEG_NORMAL
:
230 case DEC_CLASS_NEG_SUBNORMAL
:
233 case DEC_CLASS_NEG_ZERO
:
236 case DEC_CLASS_POS_ZERO
:
239 case DEC_CLASS_POS_SUBNORMAL
:
242 case DEC_CLASS_POS_NORMAL
:
245 case DEC_CLASS_POS_INF
:
249 assert(0); /* should never get here */
251 dfp
->env
->fpscr
&= ~FP_FPRF
;
252 dfp
->env
->fpscr
|= (fprf
<< FPSCR_FPRF
);
255 static void dfp_set_FPRF_from_FRT(struct PPC_DFP
*dfp
)
257 dfp_set_FPRF_from_FRT_with_context(dfp
, &dfp
->context
);
260 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP
*dfp
)
262 decContext shortContext
;
263 decContextDefault(&shortContext
, DEC_INIT_DECIMAL32
);
264 dfp_set_FPRF_from_FRT_with_context(dfp
, &shortContext
);
267 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP
*dfp
)
269 decContext longContext
;
270 decContextDefault(&longContext
, DEC_INIT_DECIMAL64
);
271 dfp_set_FPRF_from_FRT_with_context(dfp
, &longContext
);
274 static void dfp_check_for_OX(struct PPC_DFP
*dfp
)
276 if (dfp
->context
.status
& DEC_Overflow
) {
277 dfp_set_FPSCR_flag(dfp
, FP_OX
, FP_OE
);
281 static void dfp_check_for_UX(struct PPC_DFP
*dfp
)
283 if (dfp
->context
.status
& DEC_Underflow
) {
284 dfp_set_FPSCR_flag(dfp
, FP_UX
, FP_UE
);
288 static void dfp_check_for_XX(struct PPC_DFP
*dfp
)
290 if (dfp
->context
.status
& DEC_Inexact
) {
291 dfp_set_FPSCR_flag(dfp
, FP_XX
| FP_FI
, FP_XE
);
295 static void dfp_check_for_ZX(struct PPC_DFP
*dfp
)
297 if (dfp
->context
.status
& DEC_Division_by_zero
) {
298 dfp_set_FPSCR_flag(dfp
, FP_ZX
, FP_ZE
);
302 static void dfp_check_for_VXSNAN(struct PPC_DFP
*dfp
)
304 if (dfp
->context
.status
& DEC_Invalid_operation
) {
305 if (decNumberIsSNaN(&dfp
->a
) || decNumberIsSNaN(&dfp
->b
)) {
306 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
311 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP
*dfp
)
313 if (decNumberIsSNaN(&dfp
->t
)) {
314 dfp
->t
.bits
&= ~DECSNAN
;
315 dfp
->t
.bits
|= DECNAN
;
316 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
320 static void dfp_check_for_VXISI(struct PPC_DFP
*dfp
, int testForSameSign
)
322 if (dfp
->context
.status
& DEC_Invalid_operation
) {
323 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
324 int same
= decNumberClass(&dfp
->a
, &dfp
->context
) ==
325 decNumberClass(&dfp
->b
, &dfp
->context
);
326 if ((same
&& testForSameSign
) || (!same
&& !testForSameSign
)) {
327 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXISI
, FP_VE
);
333 static void dfp_check_for_VXISI_add(struct PPC_DFP
*dfp
)
335 dfp_check_for_VXISI(dfp
, 0);
338 static void dfp_check_for_VXISI_subtract(struct PPC_DFP
*dfp
)
340 dfp_check_for_VXISI(dfp
, 1);
343 static void dfp_check_for_VXIMZ(struct PPC_DFP
*dfp
)
345 if (dfp
->context
.status
& DEC_Invalid_operation
) {
346 if ((decNumberIsInfinite(&dfp
->a
) && decNumberIsZero(&dfp
->b
)) ||
347 (decNumberIsInfinite(&dfp
->b
) && decNumberIsZero(&dfp
->a
))) {
348 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIMZ
, FP_VE
);
353 static void dfp_check_for_VXZDZ(struct PPC_DFP
*dfp
)
355 if (dfp
->context
.status
& DEC_Division_undefined
) {
356 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXZDZ
, FP_VE
);
360 static void dfp_check_for_VXIDI(struct PPC_DFP
*dfp
)
362 if (dfp
->context
.status
& DEC_Invalid_operation
) {
363 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
364 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIDI
, FP_VE
);
369 static void dfp_check_for_VXVC(struct PPC_DFP
*dfp
)
371 if (decNumberIsNaN(&dfp
->a
) || decNumberIsNaN(&dfp
->b
)) {
372 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXVC
, FP_VE
);
376 static void dfp_check_for_VXCVI(struct PPC_DFP
*dfp
)
378 if ((dfp
->context
.status
& DEC_Invalid_operation
) &&
379 (!decNumberIsSNaN(&dfp
->a
)) &&
380 (!decNumberIsSNaN(&dfp
->b
))) {
381 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
385 static void dfp_set_CRBF_from_T(struct PPC_DFP
*dfp
)
387 if (decNumberIsNaN(&dfp
->t
)) {
389 } else if (decNumberIsZero(&dfp
->t
)) {
391 } else if (decNumberIsNegative(&dfp
->t
)) {
398 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP
*dfp
)
400 dfp
->env
->fpscr
&= ~FP_FPCC
;
401 dfp
->env
->fpscr
|= (dfp
->crbf
<< FPSCR_FPCC
);
404 static inline void dfp_makeQNaN(decNumber
*dn
)
406 dn
->bits
&= ~DECSPECIAL
;
410 static inline int dfp_get_digit(decNumber
*dn
, int n
)
412 assert(DECDPUN
== 3);
413 int unit
= n
/ DECDPUN
;
414 int dig
= n
% DECDPUN
;
417 return dn
->lsu
[unit
] % 10;
419 return (dn
->lsu
[unit
] / 10) % 10;
421 return dn
->lsu
[unit
] / 100;
423 g_assert_not_reached();
426 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
427 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
430 struct PPC_DFP dfp; \
431 dfp_prepare_decimal##size(&dfp, a, b, env); \
432 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
433 dfp_finalize_decimal##size(&dfp); \
435 set_dfp##size(t, &dfp.vt); \
438 static void ADD_PPs(struct PPC_DFP
*dfp
)
440 dfp_set_FPRF_from_FRT(dfp
);
441 dfp_check_for_OX(dfp
);
442 dfp_check_for_UX(dfp
);
443 dfp_check_for_XX(dfp
);
444 dfp_check_for_VXSNAN(dfp
);
445 dfp_check_for_VXISI_add(dfp
);
448 DFP_HELPER_TAB(DADD
, decNumberAdd
, ADD_PPs
, 64)
449 DFP_HELPER_TAB(DADDQ
, decNumberAdd
, ADD_PPs
, 128)
451 static void SUB_PPs(struct PPC_DFP
*dfp
)
453 dfp_set_FPRF_from_FRT(dfp
);
454 dfp_check_for_OX(dfp
);
455 dfp_check_for_UX(dfp
);
456 dfp_check_for_XX(dfp
);
457 dfp_check_for_VXSNAN(dfp
);
458 dfp_check_for_VXISI_subtract(dfp
);
461 DFP_HELPER_TAB(DSUB
, decNumberSubtract
, SUB_PPs
, 64)
462 DFP_HELPER_TAB(DSUBQ
, decNumberSubtract
, SUB_PPs
, 128)
464 static void MUL_PPs(struct PPC_DFP
*dfp
)
466 dfp_set_FPRF_from_FRT(dfp
);
467 dfp_check_for_OX(dfp
);
468 dfp_check_for_UX(dfp
);
469 dfp_check_for_XX(dfp
);
470 dfp_check_for_VXSNAN(dfp
);
471 dfp_check_for_VXIMZ(dfp
);
474 DFP_HELPER_TAB(DMUL
, decNumberMultiply
, MUL_PPs
, 64)
475 DFP_HELPER_TAB(DMULQ
, decNumberMultiply
, MUL_PPs
, 128)
477 static void DIV_PPs(struct PPC_DFP
*dfp
)
479 dfp_set_FPRF_from_FRT(dfp
);
480 dfp_check_for_OX(dfp
);
481 dfp_check_for_UX(dfp
);
482 dfp_check_for_ZX(dfp
);
483 dfp_check_for_XX(dfp
);
484 dfp_check_for_VXSNAN(dfp
);
485 dfp_check_for_VXZDZ(dfp
);
486 dfp_check_for_VXIDI(dfp
);
489 DFP_HELPER_TAB(DDIV
, decNumberDivide
, DIV_PPs
, 64)
490 DFP_HELPER_TAB(DDIVQ
, decNumberDivide
, DIV_PPs
, 128)
492 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \
493 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
495 struct PPC_DFP dfp; \
496 dfp_prepare_decimal##size(&dfp, a, b, env); \
497 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
498 dfp_finalize_decimal##size(&dfp); \
503 static void CMPU_PPs(struct PPC_DFP
*dfp
)
505 dfp_set_CRBF_from_T(dfp
);
506 dfp_set_FPCC_from_CRBF(dfp
);
507 dfp_check_for_VXSNAN(dfp
);
510 DFP_HELPER_BF_AB(DCMPU
, decNumberCompare
, CMPU_PPs
, 64)
511 DFP_HELPER_BF_AB(DCMPUQ
, decNumberCompare
, CMPU_PPs
, 128)
513 static void CMPO_PPs(struct PPC_DFP
*dfp
)
515 dfp_set_CRBF_from_T(dfp
);
516 dfp_set_FPCC_from_CRBF(dfp
);
517 dfp_check_for_VXSNAN(dfp
);
518 dfp_check_for_VXVC(dfp
);
521 DFP_HELPER_BF_AB(DCMPO
, decNumberCompare
, CMPO_PPs
, 64)
522 DFP_HELPER_BF_AB(DCMPOQ
, decNumberCompare
, CMPO_PPs
, 128)
524 #define DFP_HELPER_TSTDC(op, size) \
525 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
527 struct PPC_DFP dfp; \
530 dfp_prepare_decimal##size(&dfp, a, 0, env); \
532 match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \
533 match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
534 match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \
535 match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \
536 match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \
537 match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \
539 if (decNumberIsNegative(&dfp.a)) { \
540 dfp.crbf = match ? 0xA : 0x8; \
542 dfp.crbf = match ? 0x2 : 0x0; \
545 dfp_set_FPCC_from_CRBF(&dfp); \
549 DFP_HELPER_TSTDC(DTSTDC
, 64)
550 DFP_HELPER_TSTDC(DTSTDCQ
, 128)
552 #define DFP_HELPER_TSTDG(op, size) \
553 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm) \
555 struct PPC_DFP dfp; \
556 int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \
557 is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \
560 dfp_prepare_decimal##size(&dfp, a, 0, env); \
562 if ((size) == 64) { \
567 } else if ((size) == 128) { \
574 is_negative = decNumberIsNegative(&dfp.a); \
575 is_zero = decNumberIsZero(&dfp.a); \
576 is_extreme_exp = (dfp.a.exponent == maxexp) || \
577 (dfp.a.exponent == minexp); \
578 is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \
579 is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \
580 leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \
581 (dfp.a.lsu[nzero_idx] != 0); \
584 match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \
585 match |= (dcm & 0x10) && is_zero && is_extreme_exp; \
586 match |= (dcm & 0x08) && \
587 (is_subnormal || (is_normal && is_extreme_exp)); \
588 match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \
589 !leftmost_is_nonzero; \
590 match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \
591 leftmost_is_nonzero; \
592 match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \
595 dfp.crbf = match ? 0xA : 0x8; \
597 dfp.crbf = match ? 0x2 : 0x0; \
600 dfp_set_FPCC_from_CRBF(&dfp); \
604 DFP_HELPER_TSTDG(DTSTDG
, 64)
605 DFP_HELPER_TSTDG(DTSTDGQ
, 128)
607 #define DFP_HELPER_TSTEX(op, size) \
608 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
610 struct PPC_DFP dfp; \
611 int expa, expb, a_is_special, b_is_special; \
613 dfp_prepare_decimal##size(&dfp, a, b, env); \
615 expa = dfp.a.exponent; \
616 expb = dfp.b.exponent; \
617 a_is_special = decNumberIsSpecial(&dfp.a); \
618 b_is_special = decNumberIsSpecial(&dfp.b); \
620 if (a_is_special || b_is_special) { \
621 int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
622 int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
623 dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \
624 } else if (expa < expb) { \
626 } else if (expa > expb) { \
632 dfp_set_FPCC_from_CRBF(&dfp); \
636 DFP_HELPER_TSTEX(DTSTEX
, 64)
637 DFP_HELPER_TSTEX(DTSTEXQ
, 128)
639 #define DFP_HELPER_TSTSF(op, size) \
640 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b) \
642 struct PPC_DFP dfp; \
646 dfp_prepare_decimal##size(&dfp, 0, b, env); \
649 k = va.VsrD(1) & 0x3F; \
651 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
653 } else if (k == 0) { \
655 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
656 /* Zero has no sig digits */ \
659 unsigned nsd = dfp.b.digits; \
662 } else if (k > nsd) { \
669 dfp_set_FPCC_from_CRBF(&dfp); \
673 DFP_HELPER_TSTSF(DTSTSF
, 64)
674 DFP_HELPER_TSTSF(DTSTSFQ
, 128)
676 #define DFP_HELPER_TSTSFI(op, size) \
677 uint32_t helper_##op(CPUPPCState *env, uint32_t a, ppc_fprp_t *b) \
679 struct PPC_DFP dfp; \
682 dfp_prepare_decimal##size(&dfp, 0, b, env); \
686 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
688 } else if (uim == 0) { \
690 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
691 /* Zero has no sig digits */ \
694 unsigned nsd = dfp.b.digits; \
697 } else if (uim > nsd) { \
704 dfp_set_FPCC_from_CRBF(&dfp); \
708 DFP_HELPER_TSTSFI(DTSTSFI
, 64)
709 DFP_HELPER_TSTSFI(DTSTSFIQ
, 128)
711 static void QUA_PPs(struct PPC_DFP
*dfp
)
713 dfp_set_FPRF_from_FRT(dfp
);
714 dfp_check_for_XX(dfp
);
715 dfp_check_for_VXSNAN(dfp
);
716 dfp_check_for_VXCVI(dfp
);
719 static void dfp_quantize(uint8_t rmc
, struct PPC_DFP
*dfp
)
721 dfp_set_round_mode_from_immediate(0, rmc
, dfp
);
722 decNumberQuantize(&dfp
->t
, &dfp
->b
, &dfp
->a
, &dfp
->context
);
723 if (decNumberIsSNaN(&dfp
->a
)) {
725 dfp_makeQNaN(&dfp
->t
);
726 } else if (decNumberIsSNaN(&dfp
->b
)) {
728 dfp_makeQNaN(&dfp
->t
);
729 } else if (decNumberIsQNaN(&dfp
->a
)) {
731 } else if (decNumberIsQNaN(&dfp
->b
)) {
736 #define DFP_HELPER_QUAI(op, size) \
737 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
738 uint32_t te, uint32_t rmc) \
740 struct PPC_DFP dfp; \
742 dfp_prepare_decimal##size(&dfp, 0, b, env); \
744 decNumberFromUInt32(&dfp.a, 1); \
745 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
747 dfp_quantize(rmc, &dfp); \
748 dfp_finalize_decimal##size(&dfp); \
751 set_dfp##size(t, &dfp.vt); \
754 DFP_HELPER_QUAI(DQUAI
, 64)
755 DFP_HELPER_QUAI(DQUAIQ
, 128)
757 #define DFP_HELPER_QUA(op, size) \
758 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
759 ppc_fprp_t *b, uint32_t rmc) \
761 struct PPC_DFP dfp; \
763 dfp_prepare_decimal##size(&dfp, a, b, env); \
765 dfp_quantize(rmc, &dfp); \
766 dfp_finalize_decimal##size(&dfp); \
769 set_dfp##size(t, &dfp.vt); \
772 DFP_HELPER_QUA(DQUA
, 64)
773 DFP_HELPER_QUA(DQUAQ
, 128)
775 static void _dfp_reround(uint8_t rmc
, int32_t ref_sig
, int32_t xmax
,
778 int msd_orig
, msd_rslt
;
780 if (unlikely((ref_sig
== 0) || (dfp
->b
.digits
<= ref_sig
))) {
782 if (decNumberIsSNaN(&dfp
->b
)) {
783 dfp_makeQNaN(&dfp
->t
);
784 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FPSCR_VE
);
789 /* Reround is equivalent to quantizing b with 1**E(n) where */
790 /* n = exp(b) + numDigits(b) - reference_significance. */
792 decNumberFromUInt32(&dfp
->a
, 1);
793 dfp
->a
.exponent
= dfp
->b
.exponent
+ dfp
->b
.digits
- ref_sig
;
795 if (unlikely(dfp
->a
.exponent
> xmax
)) {
797 dfp
->t
.bits
&= ~DECNEG
;
798 dfp_makeQNaN(&dfp
->t
);
799 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FPSCR_VE
);
803 dfp_quantize(rmc
, dfp
);
805 msd_orig
= dfp_get_digit(&dfp
->b
, dfp
->b
.digits
-1);
806 msd_rslt
= dfp_get_digit(&dfp
->t
, dfp
->t
.digits
-1);
808 /* If the quantization resulted in rounding up to the next magnitude, */
809 /* then we need to shift the significand and adjust the exponent. */
811 if (unlikely((msd_orig
== 9) && (msd_rslt
== 1))) {
815 decNumberFromInt32(&negone
, -1);
816 decNumberShift(&dfp
->t
, &dfp
->t
, &negone
, &dfp
->context
);
819 if (unlikely(dfp
->t
.exponent
> xmax
)) {
820 dfp_makeQNaN(&dfp
->t
);
822 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
823 /* Inhibit XX in this case */
824 decContextClearStatus(&dfp
->context
, DEC_Inexact
);
829 #define DFP_HELPER_RRND(op, size) \
830 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
831 ppc_fprp_t *b, uint32_t rmc) \
833 struct PPC_DFP dfp; \
836 int32_t xmax = ((size) == 64) ? 369 : 6111; \
838 dfp_prepare_decimal##size(&dfp, 0, b, env); \
841 ref_sig = va.VsrD(1) & 0x3f; \
843 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
844 dfp_finalize_decimal##size(&dfp); \
847 set_dfp##size(t, &dfp.vt); \
850 DFP_HELPER_RRND(DRRND
, 64)
851 DFP_HELPER_RRND(DRRNDQ
, 128)
853 #define DFP_HELPER_RINT(op, postprocs, size) \
854 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
855 uint32_t r, uint32_t rmc) \
857 struct PPC_DFP dfp; \
859 dfp_prepare_decimal##size(&dfp, 0, b, env); \
861 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
862 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
863 dfp_finalize_decimal##size(&dfp); \
866 set_dfp##size(t, &dfp.vt); \
869 static void RINTX_PPs(struct PPC_DFP
*dfp
)
871 dfp_set_FPRF_from_FRT(dfp
);
872 dfp_check_for_XX(dfp
);
873 dfp_check_for_VXSNAN(dfp
);
876 DFP_HELPER_RINT(DRINTX
, RINTX_PPs
, 64)
877 DFP_HELPER_RINT(DRINTXQ
, RINTX_PPs
, 128)
879 static void RINTN_PPs(struct PPC_DFP
*dfp
)
881 dfp_set_FPRF_from_FRT(dfp
);
882 dfp_check_for_VXSNAN(dfp
);
885 DFP_HELPER_RINT(DRINTN
, RINTN_PPs
, 64)
886 DFP_HELPER_RINT(DRINTNQ
, RINTN_PPs
, 128)
888 void helper_DCTDP(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
895 b_short
= (uint32_t)vb
.VsrD(1);
897 dfp_prepare_decimal64(&dfp
, 0, 0, env
);
898 decimal32ToNumber((decimal32
*)&b_short
, &dfp
.t
);
899 dfp_finalize_decimal64(&dfp
);
900 set_dfp64(t
, &dfp
.vt
);
901 dfp_set_FPRF_from_FRT(&dfp
);
904 void helper_DCTQPQ(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
908 dfp_prepare_decimal128(&dfp
, 0, 0, env
);
910 decimal64ToNumber((decimal64
*)&vb
.VsrD(1), &dfp
.t
);
912 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
913 dfp_set_FPRF_from_FRT(&dfp
);
915 dfp_finalize_decimal128(&dfp
);
916 set_dfp128(t
, &dfp
.vt
);
919 void helper_DRSP(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
922 uint32_t t_short
= 0;
924 dfp_prepare_decimal64(&dfp
, 0, b
, env
);
925 decimal32FromNumber((decimal32
*)&t_short
, &dfp
.b
, &dfp
.context
);
926 decimal32ToNumber((decimal32
*)&t_short
, &dfp
.t
);
928 dfp_set_FPRF_from_FRT_short(&dfp
);
929 dfp_check_for_OX(&dfp
);
930 dfp_check_for_UX(&dfp
);
931 dfp_check_for_XX(&dfp
);
933 vt
.VsrD(1) = (uint64_t)t_short
;
937 void helper_DRDPQ(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_fprp_t
*b
)
940 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
941 decimal64FromNumber((decimal64
*)&dfp
.vt
.VsrD(1), &dfp
.b
, &dfp
.context
);
942 decimal64ToNumber((decimal64
*)&dfp
.vt
.VsrD(1), &dfp
.t
);
944 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
945 dfp_set_FPRF_from_FRT_long(&dfp
);
946 dfp_check_for_OX(&dfp
);
947 dfp_check_for_UX(&dfp
);
948 dfp_check_for_XX(&dfp
);
950 dfp
.vt
.VsrD(0) = dfp
.vt
.VsrD(1) = 0;
951 dfp_finalize_decimal64(&dfp
);
952 set_dfp128(t
, &dfp
.vt
);
955 #define DFP_HELPER_CFFIX(op, size) \
956 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
958 struct PPC_DFP dfp; \
960 dfp_prepare_decimal##size(&dfp, 0, b, env); \
962 decNumberFromInt64(&dfp.t, (int64_t)vb.VsrD(1)); \
963 dfp_finalize_decimal##size(&dfp); \
966 set_dfp##size(t, &dfp.vt); \
969 static void CFFIX_PPs(struct PPC_DFP
*dfp
)
971 dfp_set_FPRF_from_FRT(dfp
);
972 dfp_check_for_XX(dfp
);
975 DFP_HELPER_CFFIX(DCFFIX
, 64)
976 DFP_HELPER_CFFIX(DCFFIXQ
, 128)
978 void helper_DCFFIXQQ(CPUPPCState
*env
, ppc_fprp_t
*t
, ppc_avr_t
*b
)
982 dfp_prepare_decimal128(&dfp
, NULL
, NULL
, env
);
983 decNumberFromInt128(&dfp
.t
, (uint64_t)b
->VsrD(1), (int64_t)b
->VsrD(0));
984 dfp_finalize_decimal128(&dfp
);
987 set_dfp128(t
, &dfp
.vt
);
990 #define DFP_HELPER_CTFIX(op, size) \
991 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
993 struct PPC_DFP dfp; \
994 dfp_prepare_decimal##size(&dfp, 0, b, env); \
996 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
997 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
998 if (decNumberIsInfinite(&dfp.b)) { \
999 dfp.vt.VsrD(1) = decNumberIsNegative(&dfp.b) ? INT64_MIN : \
1001 } else { /* NaN */ \
1002 dfp.vt.VsrD(1) = INT64_MIN; \
1003 if (decNumberIsSNaN(&dfp.b)) { \
1004 invalid_flags |= FP_VXSNAN; \
1007 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
1008 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
1009 dfp.vt.VsrD(1) = 0; \
1011 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
1012 dfp.vt.VsrD(1) = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
1013 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
1014 dfp.vt.VsrD(1) = decNumberIsNegative(&dfp.b) ? INT64_MIN : \
1016 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
1018 dfp_check_for_XX(&dfp); \
1022 set_dfp64(t, &dfp.vt); \
1025 DFP_HELPER_CTFIX(DCTFIX
, 64)
1026 DFP_HELPER_CTFIX(DCTFIXQ
, 128)
1028 void helper_DCTFIXQQ(CPUPPCState
*env
, ppc_avr_t
*t
, ppc_fprp_t
*b
)
1031 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
1033 if (unlikely(decNumberIsSpecial(&dfp
.b
))) {
1034 uint64_t invalid_flags
= FP_VX
| FP_VXCVI
;
1035 if (decNumberIsInfinite(&dfp
.b
)) {
1036 if (decNumberIsNegative(&dfp
.b
)) {
1037 dfp
.vt
.VsrD(0) = INT64_MIN
;
1040 dfp
.vt
.VsrD(0) = INT64_MAX
;
1041 dfp
.vt
.VsrD(1) = UINT64_MAX
;
1044 dfp
.vt
.VsrD(0) = INT64_MIN
;
1046 if (decNumberIsSNaN(&dfp
.b
)) {
1047 invalid_flags
|= FP_VXSNAN
;
1050 dfp_set_FPSCR_flag(&dfp
, invalid_flags
, FP_VE
);
1051 } else if (unlikely(decNumberIsZero(&dfp
.b
))) {
1055 decNumberToIntegralExact(&dfp
.b
, &dfp
.b
, &dfp
.context
);
1056 decNumberIntegralToInt128(&dfp
.b
, &dfp
.context
,
1057 &dfp
.vt
.VsrD(1), &dfp
.vt
.VsrD(0));
1058 if (decContextTestStatus(&dfp
.context
, DEC_Invalid_operation
)) {
1059 if (decNumberIsNegative(&dfp
.b
)) {
1060 dfp
.vt
.VsrD(0) = INT64_MIN
;
1063 dfp
.vt
.VsrD(0) = INT64_MAX
;
1064 dfp
.vt
.VsrD(1) = UINT64_MAX
;
1066 dfp_set_FPSCR_flag(&dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
1068 dfp_check_for_XX(&dfp
);
1072 set_dfp128_to_avr(t
, &dfp
.vt
);
1075 static inline void dfp_set_bcd_digit_64(ppc_vsr_t
*t
, uint8_t digit
,
1078 t
->VsrD(1) |= ((uint64_t)(digit
& 0xF) << (n
<< 2));
1081 static inline void dfp_set_bcd_digit_128(ppc_vsr_t
*t
, uint8_t digit
,
1084 t
->VsrD((n
& 0x10) ? 0 : 1) |=
1085 ((uint64_t)(digit
& 0xF) << ((n
& 15) << 2));
1088 static inline void dfp_set_sign_64(ppc_vsr_t
*t
, uint8_t sgn
)
1091 t
->VsrD(1) |= (sgn
& 0xF);
1094 static inline void dfp_set_sign_128(ppc_vsr_t
*t
, uint8_t sgn
)
1097 t
->VsrD(0) |= (t
->VsrD(1) >> 60);
1099 t
->VsrD(1) |= (sgn
& 0xF);
1102 #define DFP_HELPER_DEDPD(op, size) \
1103 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1106 struct PPC_DFP dfp; \
1107 uint8_t digits[34]; \
1110 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1112 decNumberGetBCD(&dfp.b, digits); \
1113 dfp.vt.VsrD(0) = dfp.vt.VsrD(1) = 0; \
1116 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1117 dfp_set_bcd_digit_##size(&dfp.vt, digits[N - i - 1], i); \
1123 if (decNumberIsNegative(&dfp.b)) { \
1126 sgn = ((sp & 1) ? 0xF : 0xC); \
1128 dfp_set_sign_##size(&dfp.vt, sgn); \
1131 set_dfp##size(t, &dfp.vt); \
1134 DFP_HELPER_DEDPD(DDEDPD
, 64)
1135 DFP_HELPER_DEDPD(DDEDPDQ
, 128)
1137 static inline uint8_t dfp_get_bcd_digit_64(ppc_vsr_t
*t
, unsigned n
)
1139 return t
->VsrD(1) >> ((n
<< 2) & 63) & 15;
1142 static inline uint8_t dfp_get_bcd_digit_128(ppc_vsr_t
*t
, unsigned n
)
1144 return t
->VsrD((n
& 0x10) ? 0 : 1) >> ((n
<< 2) & 63) & 15;
1147 #define DFP_HELPER_ENBCD(op, size) \
1148 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b, \
1151 struct PPC_DFP dfp; \
1152 uint8_t digits[32]; \
1153 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1155 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1157 decNumberZero(&dfp.t); \
1160 uint8_t sgnNibble = dfp_get_bcd_digit_##size(&dfp.vb, offset++); \
1161 switch (sgnNibble) { \
1173 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1178 while (offset < (size) / 4) { \
1180 digits[(size) / 4 - n] = dfp_get_bcd_digit_##size(&dfp.vb, \
1182 if (digits[(size) / 4 - n] > 10) { \
1183 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1186 nonzero |= (digits[(size) / 4 - n] > 0); \
1191 decNumberSetBCD(&dfp.t, digits + ((size) / 4) - n, n); \
1195 dfp.t.bits |= DECNEG; \
1197 dfp_finalize_decimal##size(&dfp); \
1198 dfp_set_FPRF_from_FRT(&dfp); \
1199 set_dfp##size(t, &dfp.vt); \
1202 DFP_HELPER_ENBCD(DENBCD
, 64)
1203 DFP_HELPER_ENBCD(DENBCDQ
, 128)
1205 #define DFP_HELPER_XEX(op, size) \
1206 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
1208 struct PPC_DFP dfp; \
1211 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1213 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1214 if (decNumberIsInfinite(&dfp.b)) { \
1216 } else if (decNumberIsSNaN(&dfp.b)) { \
1218 } else if (decNumberIsQNaN(&dfp.b)) { \
1223 set_dfp64(t, &vt); \
1225 if ((size) == 64) { \
1226 vt.VsrD(1) = dfp.b.exponent + 398; \
1227 } else if ((size) == 128) { \
1228 vt.VsrD(1) = dfp.b.exponent + 6176; \
1232 set_dfp64(t, &vt); \
1236 DFP_HELPER_XEX(DXEX
, 64)
1237 DFP_HELPER_XEX(DXEXQ
, 128)
1239 static void dfp_set_raw_exp_64(ppc_vsr_t
*t
, uint64_t raw
)
1241 t
->VsrD(1) &= 0x8003ffffffffffffULL
;
1242 t
->VsrD(1) |= (raw
<< (63 - 13));
1245 static void dfp_set_raw_exp_128(ppc_vsr_t
*t
, uint64_t raw
)
1247 t
->VsrD(0) &= 0x80003fffffffffffULL
;
1248 t
->VsrD(0) |= (raw
<< (63 - 17));
1251 #define DFP_HELPER_IEX(op, size) \
1252 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1255 struct PPC_DFP dfp; \
1256 uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \
1261 get_dfp64(&va, a); \
1262 exp = (int64_t)va.VsrD(1); \
1263 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1265 if ((size) == 64) { \
1267 raw_qnan = 0x1F00; \
1268 raw_snan = 0x1F80; \
1271 } else if ((size) == 128) { \
1273 raw_qnan = 0x1f000; \
1274 raw_snan = 0x1f800; \
1275 raw_inf = 0x1e000; \
1281 if (unlikely((exp < 0) || (exp > max_exp))) { \
1282 dfp.vt.VsrD(0) = dfp.vb.VsrD(0); \
1283 dfp.vt.VsrD(1) = dfp.vb.VsrD(1); \
1285 dfp_set_raw_exp_##size(&dfp.vt, raw_inf); \
1286 } else if (exp == -3) { \
1287 dfp_set_raw_exp_##size(&dfp.vt, raw_snan); \
1289 dfp_set_raw_exp_##size(&dfp.vt, raw_qnan); \
1293 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1294 dfp.t.bits &= ~DECSPECIAL; \
1296 dfp.t.exponent = exp - bias; \
1297 dfp_finalize_decimal##size(&dfp); \
1299 set_dfp##size(t, &dfp.vt); \
1302 DFP_HELPER_IEX(DIEX
, 64)
1303 DFP_HELPER_IEX(DIEXQ
, 128)
1305 static void dfp_clear_lmd_from_g5msb(uint64_t *t
)
1308 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1309 /* from the left-most decimal digit (LMD) and the biased exponent. */
1310 /* This routine clears the LMD bits while preserving the exponent */
1311 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1312 /* Finite Numbers" in the Power ISA for additional details. */
1314 uint64_t g5msb
= (*t
>> 58) & 0x1F;
1316 if ((g5msb
>> 3) < 3) { /* LMD in [0-7] ? */
1317 *t
&= ~(7ULL << 58);
1319 switch (g5msb
& 7) {
1340 *t
&= ~(0x1fULL
<< 58);
1341 *t
|= (g5msb
<< 58);
1345 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1346 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a, \
1349 struct PPC_DFP dfp; \
1350 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1352 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1354 if (sh <= max_digits) { \
1357 unsigned special = dfp.a.bits & DECSPECIAL; \
1360 decNumberFromUInt32(&shd, sh); \
1362 decNumberFromInt32(&shd, -((int32_t)sh)); \
1365 dfp.a.bits &= ~DECSPECIAL; \
1366 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1368 dfp.t.bits |= special; \
1369 if (special && (dfp.t.digits >= max_digits)) { \
1370 dfp.t.digits = max_digits - 1; \
1373 dfp_finalize_decimal##size(&dfp); \
1375 if ((size) == 64) { \
1376 dfp.vt.VsrD(1) = dfp.va.VsrD(1) & \
1377 0xFFFC000000000000ULL; \
1378 dfp_clear_lmd_from_g5msb(&dfp.vt.VsrD(1)); \
1380 dfp.vt.VsrD(0) = dfp.va.VsrD(0) & \
1381 0xFFFFC00000000000ULL; \
1382 dfp_clear_lmd_from_g5msb(&dfp.vt.VsrD(0)); \
1383 dfp.vt.VsrD(1) = 0; \
1387 set_dfp##size(t, &dfp.vt); \
1390 DFP_HELPER_SHIFT(DSCLI
, 64, 1)
1391 DFP_HELPER_SHIFT(DSCLIQ
, 128, 1)
1392 DFP_HELPER_SHIFT(DSCRI
, 64, 0)
1393 DFP_HELPER_SHIFT(DSCRIQ
, 128, 0)