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/>.
21 #include "exec/helper-proto.h"
23 #define DECNUMDIGITS 34
24 #include "libdecnumber/decContext.h"
25 #include "libdecnumber/decNumber.h"
26 #include "libdecnumber/dpd/decimal32.h"
27 #include "libdecnumber/dpd/decimal64.h"
28 #include "libdecnumber/dpd/decimal128.h"
30 #if defined(HOST_WORDS_BIGENDIAN)
40 uint64_t t64
[2], a64
[2], b64
[2];
46 static void dfp_prepare_rounding_mode(decContext
*context
, uint64_t fpscr
)
50 switch ((fpscr
>> 32) & 0x7) {
52 rnd
= DEC_ROUND_HALF_EVEN
;
58 rnd
= DEC_ROUND_CEILING
;
61 rnd
= DEC_ROUND_FLOOR
;
64 rnd
= DEC_ROUND_HALF_UP
;
67 rnd
= DEC_ROUND_HALF_DOWN
;
76 g_assert_not_reached();
79 decContextSetRounding(context
, rnd
);
82 static void dfp_set_round_mode_from_immediate(uint8_t r
, uint8_t rmc
,
89 rnd
= DEC_ROUND_HALF_EVEN
;
95 rnd
= DEC_ROUND_HALF_UP
;
97 case 3: /* use FPSCR rounding mode */
100 assert(0); /* cannot get here */
102 } else { /* r == 1 */
105 rnd
= DEC_ROUND_CEILING
;
108 rnd
= DEC_ROUND_FLOOR
;
114 rnd
= DEC_ROUND_HALF_DOWN
;
117 assert(0); /* cannot get here */
120 decContextSetRounding(&dfp
->context
, rnd
);
123 static void dfp_prepare_decimal64(struct PPC_DFP
*dfp
, uint64_t *a
,
124 uint64_t *b
, CPUPPCState
*env
)
126 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL64
);
127 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
132 decimal64ToNumber((decimal64
*)dfp
->a64
, &dfp
->a
);
135 decNumberZero(&dfp
->a
);
140 decimal64ToNumber((decimal64
*)dfp
->b64
, &dfp
->b
);
143 decNumberZero(&dfp
->b
);
147 static void dfp_prepare_decimal128(struct PPC_DFP
*dfp
, uint64_t *a
,
148 uint64_t *b
, CPUPPCState
*env
)
150 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL128
);
151 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
155 dfp
->a64
[0] = a
[HI_IDX
];
156 dfp
->a64
[1] = a
[LO_IDX
];
157 decimal128ToNumber((decimal128
*)dfp
->a64
, &dfp
->a
);
159 dfp
->a64
[0] = dfp
->a64
[1] = 0;
160 decNumberZero(&dfp
->a
);
164 dfp
->b64
[0] = b
[HI_IDX
];
165 dfp
->b64
[1] = b
[LO_IDX
];
166 decimal128ToNumber((decimal128
*)dfp
->b64
, &dfp
->b
);
168 dfp
->b64
[0] = dfp
->b64
[1] = 0;
169 decNumberZero(&dfp
->b
);
173 static void dfp_set_FPSCR_flag(struct PPC_DFP
*dfp
, uint64_t flag
,
176 dfp
->env
->fpscr
|= (flag
| FP_FX
);
177 if (dfp
->env
->fpscr
& enabled
) {
178 dfp
->env
->fpscr
|= FP_FEX
;
182 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP
*dfp
,
188 switch (decNumberClass(&dfp
->t
, context
)) {
195 case DEC_CLASS_NEG_INF
:
198 case DEC_CLASS_NEG_NORMAL
:
201 case DEC_CLASS_NEG_SUBNORMAL
:
204 case DEC_CLASS_NEG_ZERO
:
207 case DEC_CLASS_POS_ZERO
:
210 case DEC_CLASS_POS_SUBNORMAL
:
213 case DEC_CLASS_POS_NORMAL
:
216 case DEC_CLASS_POS_INF
:
220 assert(0); /* should never get here */
222 dfp
->env
->fpscr
&= ~(0x1F << 12);
223 dfp
->env
->fpscr
|= (fprf
<< 12);
226 static void dfp_set_FPRF_from_FRT(struct PPC_DFP
*dfp
)
228 dfp_set_FPRF_from_FRT_with_context(dfp
, &dfp
->context
);
231 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP
*dfp
)
233 decContext shortContext
;
234 decContextDefault(&shortContext
, DEC_INIT_DECIMAL32
);
235 dfp_set_FPRF_from_FRT_with_context(dfp
, &shortContext
);
238 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP
*dfp
)
240 decContext longContext
;
241 decContextDefault(&longContext
, DEC_INIT_DECIMAL64
);
242 dfp_set_FPRF_from_FRT_with_context(dfp
, &longContext
);
245 static void dfp_check_for_OX(struct PPC_DFP
*dfp
)
247 if (dfp
->context
.status
& DEC_Overflow
) {
248 dfp_set_FPSCR_flag(dfp
, FP_OX
, FP_OE
);
252 static void dfp_check_for_UX(struct PPC_DFP
*dfp
)
254 if (dfp
->context
.status
& DEC_Underflow
) {
255 dfp_set_FPSCR_flag(dfp
, FP_UX
, FP_UE
);
259 static void dfp_check_for_XX(struct PPC_DFP
*dfp
)
261 if (dfp
->context
.status
& DEC_Inexact
) {
262 dfp_set_FPSCR_flag(dfp
, FP_XX
| FP_FI
, FP_XE
);
266 static void dfp_check_for_ZX(struct PPC_DFP
*dfp
)
268 if (dfp
->context
.status
& DEC_Division_by_zero
) {
269 dfp_set_FPSCR_flag(dfp
, FP_ZX
, FP_ZE
);
273 static void dfp_check_for_VXSNAN(struct PPC_DFP
*dfp
)
275 if (dfp
->context
.status
& DEC_Invalid_operation
) {
276 if (decNumberIsSNaN(&dfp
->a
) || decNumberIsSNaN(&dfp
->b
)) {
277 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
282 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP
*dfp
)
284 if (decNumberIsSNaN(&dfp
->t
)) {
285 dfp
->t
.bits
&= ~DECSNAN
;
286 dfp
->t
.bits
|= DECNAN
;
287 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
291 static void dfp_check_for_VXISI(struct PPC_DFP
*dfp
, int testForSameSign
)
293 if (dfp
->context
.status
& DEC_Invalid_operation
) {
294 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
295 int same
= decNumberClass(&dfp
->a
, &dfp
->context
) ==
296 decNumberClass(&dfp
->b
, &dfp
->context
);
297 if ((same
&& testForSameSign
) || (!same
&& !testForSameSign
)) {
298 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXISI
, FP_VE
);
304 static void dfp_check_for_VXISI_add(struct PPC_DFP
*dfp
)
306 dfp_check_for_VXISI(dfp
, 0);
309 static void dfp_check_for_VXISI_subtract(struct PPC_DFP
*dfp
)
311 dfp_check_for_VXISI(dfp
, 1);
314 static void dfp_check_for_VXIMZ(struct PPC_DFP
*dfp
)
316 if (dfp
->context
.status
& DEC_Invalid_operation
) {
317 if ((decNumberIsInfinite(&dfp
->a
) && decNumberIsZero(&dfp
->b
)) ||
318 (decNumberIsInfinite(&dfp
->b
) && decNumberIsZero(&dfp
->a
))) {
319 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIMZ
, FP_VE
);
324 static void dfp_check_for_VXZDZ(struct PPC_DFP
*dfp
)
326 if (dfp
->context
.status
& DEC_Division_undefined
) {
327 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXZDZ
, FP_VE
);
331 static void dfp_check_for_VXIDI(struct PPC_DFP
*dfp
)
333 if (dfp
->context
.status
& DEC_Invalid_operation
) {
334 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
335 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIDI
, FP_VE
);
340 static void dfp_check_for_VXVC(struct PPC_DFP
*dfp
)
342 if (decNumberIsNaN(&dfp
->a
) || decNumberIsNaN(&dfp
->b
)) {
343 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXVC
, FP_VE
);
347 static void dfp_check_for_VXCVI(struct PPC_DFP
*dfp
)
349 if ((dfp
->context
.status
& DEC_Invalid_operation
) &&
350 (!decNumberIsSNaN(&dfp
->a
)) &&
351 (!decNumberIsSNaN(&dfp
->b
))) {
352 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
356 static void dfp_set_CRBF_from_T(struct PPC_DFP
*dfp
)
358 if (decNumberIsNaN(&dfp
->t
)) {
360 } else if (decNumberIsZero(&dfp
->t
)) {
362 } else if (decNumberIsNegative(&dfp
->t
)) {
369 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP
*dfp
)
371 dfp
->env
->fpscr
&= ~(0xF << 12);
372 dfp
->env
->fpscr
|= (dfp
->crbf
<< 12);
375 static inline void dfp_makeQNaN(decNumber
*dn
)
377 dn
->bits
&= ~DECSPECIAL
;
381 static inline int dfp_get_digit(decNumber
*dn
, int n
)
383 assert(DECDPUN
== 3);
384 int unit
= n
/ DECDPUN
;
385 int dig
= n
% DECDPUN
;
388 return dn
->lsu
[unit
] % 10;
390 return (dn
->lsu
[unit
] / 10) % 10;
392 return dn
->lsu
[unit
] / 100;
394 g_assert_not_reached();
397 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
398 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
400 struct PPC_DFP dfp; \
401 dfp_prepare_decimal##size(&dfp, a, b, env); \
402 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
403 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
407 } else if (size == 128) { \
408 t[0] = dfp.t64[HI_IDX]; \
409 t[1] = dfp.t64[LO_IDX]; \
413 static void ADD_PPs(struct PPC_DFP
*dfp
)
415 dfp_set_FPRF_from_FRT(dfp
);
416 dfp_check_for_OX(dfp
);
417 dfp_check_for_UX(dfp
);
418 dfp_check_for_XX(dfp
);
419 dfp_check_for_VXSNAN(dfp
);
420 dfp_check_for_VXISI_add(dfp
);
423 DFP_HELPER_TAB(dadd
, decNumberAdd
, ADD_PPs
, 64)
424 DFP_HELPER_TAB(daddq
, decNumberAdd
, ADD_PPs
, 128)
426 static void SUB_PPs(struct PPC_DFP
*dfp
)
428 dfp_set_FPRF_from_FRT(dfp
);
429 dfp_check_for_OX(dfp
);
430 dfp_check_for_UX(dfp
);
431 dfp_check_for_XX(dfp
);
432 dfp_check_for_VXSNAN(dfp
);
433 dfp_check_for_VXISI_subtract(dfp
);
436 DFP_HELPER_TAB(dsub
, decNumberSubtract
, SUB_PPs
, 64)
437 DFP_HELPER_TAB(dsubq
, decNumberSubtract
, SUB_PPs
, 128)
439 static void MUL_PPs(struct PPC_DFP
*dfp
)
441 dfp_set_FPRF_from_FRT(dfp
);
442 dfp_check_for_OX(dfp
);
443 dfp_check_for_UX(dfp
);
444 dfp_check_for_XX(dfp
);
445 dfp_check_for_VXSNAN(dfp
);
446 dfp_check_for_VXIMZ(dfp
);
449 DFP_HELPER_TAB(dmul
, decNumberMultiply
, MUL_PPs
, 64)
450 DFP_HELPER_TAB(dmulq
, decNumberMultiply
, MUL_PPs
, 128)
452 static void DIV_PPs(struct PPC_DFP
*dfp
)
454 dfp_set_FPRF_from_FRT(dfp
);
455 dfp_check_for_OX(dfp
);
456 dfp_check_for_UX(dfp
);
457 dfp_check_for_ZX(dfp
);
458 dfp_check_for_XX(dfp
);
459 dfp_check_for_VXSNAN(dfp
);
460 dfp_check_for_VXZDZ(dfp
);
461 dfp_check_for_VXIDI(dfp
);
464 DFP_HELPER_TAB(ddiv
, decNumberDivide
, DIV_PPs
, 64)
465 DFP_HELPER_TAB(ddivq
, decNumberDivide
, DIV_PPs
, 128)
467 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \
468 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
470 struct PPC_DFP dfp; \
471 dfp_prepare_decimal##size(&dfp, a, b, env); \
472 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
473 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
478 static void CMPU_PPs(struct PPC_DFP
*dfp
)
480 dfp_set_CRBF_from_T(dfp
);
481 dfp_set_FPCC_from_CRBF(dfp
);
482 dfp_check_for_VXSNAN(dfp
);
485 DFP_HELPER_BF_AB(dcmpu
, decNumberCompare
, CMPU_PPs
, 64)
486 DFP_HELPER_BF_AB(dcmpuq
, decNumberCompare
, CMPU_PPs
, 128)
488 static void CMPO_PPs(struct PPC_DFP
*dfp
)
490 dfp_set_CRBF_from_T(dfp
);
491 dfp_set_FPCC_from_CRBF(dfp
);
492 dfp_check_for_VXSNAN(dfp
);
493 dfp_check_for_VXVC(dfp
);
496 DFP_HELPER_BF_AB(dcmpo
, decNumberCompare
, CMPO_PPs
, 64)
497 DFP_HELPER_BF_AB(dcmpoq
, decNumberCompare
, CMPO_PPs
, 128)
499 #define DFP_HELPER_TSTDC(op, size) \
500 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \
502 struct PPC_DFP dfp; \
505 dfp_prepare_decimal##size(&dfp, a, 0, env); \
507 match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \
508 match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
509 match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \
510 match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \
511 match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \
512 match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \
514 if (decNumberIsNegative(&dfp.a)) { \
515 dfp.crbf = match ? 0xA : 0x8; \
517 dfp.crbf = match ? 0x2 : 0x0; \
520 dfp_set_FPCC_from_CRBF(&dfp); \
524 DFP_HELPER_TSTDC(dtstdc
, 64)
525 DFP_HELPER_TSTDC(dtstdcq
, 128)
527 #define DFP_HELPER_TSTDG(op, size) \
528 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \
530 struct PPC_DFP dfp; \
531 int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \
532 is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \
535 dfp_prepare_decimal##size(&dfp, a, 0, env); \
537 if ((size) == 64) { \
542 } else if ((size) == 128) { \
549 is_negative = decNumberIsNegative(&dfp.a); \
550 is_zero = decNumberIsZero(&dfp.a); \
551 is_extreme_exp = (dfp.a.exponent == maxexp) || \
552 (dfp.a.exponent == minexp); \
553 is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \
554 is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \
555 leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \
556 (dfp.a.lsu[nzero_idx] != 0); \
559 match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \
560 match |= (dcm & 0x10) && is_zero && is_extreme_exp; \
561 match |= (dcm & 0x08) && \
562 (is_subnormal || (is_normal && is_extreme_exp)); \
563 match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \
564 !leftmost_is_nonzero; \
565 match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \
566 leftmost_is_nonzero; \
567 match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \
570 dfp.crbf = match ? 0xA : 0x8; \
572 dfp.crbf = match ? 0x2 : 0x0; \
575 dfp_set_FPCC_from_CRBF(&dfp); \
579 DFP_HELPER_TSTDG(dtstdg
, 64)
580 DFP_HELPER_TSTDG(dtstdgq
, 128)
582 #define DFP_HELPER_TSTEX(op, size) \
583 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
585 struct PPC_DFP dfp; \
586 int expa, expb, a_is_special, b_is_special; \
588 dfp_prepare_decimal##size(&dfp, a, b, env); \
590 expa = dfp.a.exponent; \
591 expb = dfp.b.exponent; \
592 a_is_special = decNumberIsSpecial(&dfp.a); \
593 b_is_special = decNumberIsSpecial(&dfp.b); \
595 if (a_is_special || b_is_special) { \
596 int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
597 int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
598 dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \
599 } else if (expa < expb) { \
601 } else if (expa > expb) { \
607 dfp_set_FPCC_from_CRBF(&dfp); \
611 DFP_HELPER_TSTEX(dtstex
, 64)
612 DFP_HELPER_TSTEX(dtstexq
, 128)
614 #define DFP_HELPER_TSTSF(op, size) \
615 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
617 struct PPC_DFP dfp; \
620 dfp_prepare_decimal##size(&dfp, 0, b, env); \
624 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
626 } else if (k == 0) { \
628 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
629 /* Zero has no sig digits */ \
632 unsigned nsd = dfp.b.digits; \
635 } else if (k > nsd) { \
642 dfp_set_FPCC_from_CRBF(&dfp); \
646 DFP_HELPER_TSTSF(dtstsf
, 64)
647 DFP_HELPER_TSTSF(dtstsfq
, 128)
649 static void QUA_PPs(struct PPC_DFP
*dfp
)
651 dfp_set_FPRF_from_FRT(dfp
);
652 dfp_check_for_XX(dfp
);
653 dfp_check_for_VXSNAN(dfp
);
654 dfp_check_for_VXCVI(dfp
);
657 static void dfp_quantize(uint8_t rmc
, struct PPC_DFP
*dfp
)
659 dfp_set_round_mode_from_immediate(0, rmc
, dfp
);
660 decNumberQuantize(&dfp
->t
, &dfp
->b
, &dfp
->a
, &dfp
->context
);
661 if (decNumberIsSNaN(&dfp
->a
)) {
663 dfp_makeQNaN(&dfp
->t
);
664 } else if (decNumberIsSNaN(&dfp
->b
)) {
666 dfp_makeQNaN(&dfp
->t
);
667 } else if (decNumberIsQNaN(&dfp
->a
)) {
669 } else if (decNumberIsQNaN(&dfp
->b
)) {
674 #define DFP_HELPER_QUAI(op, size) \
675 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
676 uint32_t te, uint32_t rmc) \
678 struct PPC_DFP dfp; \
680 dfp_prepare_decimal##size(&dfp, 0, b, env); \
682 decNumberFromUInt32(&dfp.a, 1); \
683 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
685 dfp_quantize(rmc, &dfp); \
686 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
692 } else if (size == 128) { \
693 t[0] = dfp.t64[HI_IDX]; \
694 t[1] = dfp.t64[LO_IDX]; \
698 DFP_HELPER_QUAI(dquai
, 64)
699 DFP_HELPER_QUAI(dquaiq
, 128)
701 #define DFP_HELPER_QUA(op, size) \
702 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
703 uint64_t *b, uint32_t rmc) \
705 struct PPC_DFP dfp; \
707 dfp_prepare_decimal##size(&dfp, a, b, env); \
709 dfp_quantize(rmc, &dfp); \
710 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
716 } else if (size == 128) { \
717 t[0] = dfp.t64[HI_IDX]; \
718 t[1] = dfp.t64[LO_IDX]; \
722 DFP_HELPER_QUA(dqua
, 64)
723 DFP_HELPER_QUA(dquaq
, 128)
725 static void _dfp_reround(uint8_t rmc
, int32_t ref_sig
, int32_t xmax
,
728 int msd_orig
, msd_rslt
;
730 if (unlikely((ref_sig
== 0) || (dfp
->b
.digits
<= ref_sig
))) {
732 if (decNumberIsSNaN(&dfp
->b
)) {
733 dfp_makeQNaN(&dfp
->t
);
734 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FPSCR_VE
);
739 /* Reround is equivalent to quantizing b with 1**E(n) where */
740 /* n = exp(b) + numDigits(b) - reference_significance. */
742 decNumberFromUInt32(&dfp
->a
, 1);
743 dfp
->a
.exponent
= dfp
->b
.exponent
+ dfp
->b
.digits
- ref_sig
;
745 if (unlikely(dfp
->a
.exponent
> xmax
)) {
747 dfp
->t
.bits
&= ~DECNEG
;
748 dfp_makeQNaN(&dfp
->t
);
749 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FPSCR_VE
);
753 dfp_quantize(rmc
, dfp
);
755 msd_orig
= dfp_get_digit(&dfp
->b
, dfp
->b
.digits
-1);
756 msd_rslt
= dfp_get_digit(&dfp
->t
, dfp
->t
.digits
-1);
758 /* If the quantization resulted in rounding up to the next magnitude, */
759 /* then we need to shift the significand and adjust the exponent. */
761 if (unlikely((msd_orig
== 9) && (msd_rslt
== 1))) {
765 decNumberFromInt32(&negone
, -1);
766 decNumberShift(&dfp
->t
, &dfp
->t
, &negone
, &dfp
->context
);
769 if (unlikely(dfp
->t
.exponent
> xmax
)) {
770 dfp_makeQNaN(&dfp
->t
);
772 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
773 /* Inhibit XX in this case */
774 decContextClearStatus(&dfp
->context
, DEC_Inexact
);
779 #define DFP_HELPER_RRND(op, size) \
780 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
781 uint64_t *b, uint32_t rmc) \
783 struct PPC_DFP dfp; \
784 int32_t ref_sig = *a & 0x3F; \
785 int32_t xmax = ((size) == 64) ? 369 : 6111; \
787 dfp_prepare_decimal##size(&dfp, 0, b, env); \
789 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
790 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
796 } else if (size == 128) { \
797 t[0] = dfp.t64[HI_IDX]; \
798 t[1] = dfp.t64[LO_IDX]; \
802 DFP_HELPER_RRND(drrnd
, 64)
803 DFP_HELPER_RRND(drrndq
, 128)
805 #define DFP_HELPER_RINT(op, postprocs, size) \
806 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
807 uint32_t r, uint32_t rmc) \
809 struct PPC_DFP dfp; \
811 dfp_prepare_decimal##size(&dfp, 0, b, env); \
813 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
814 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
815 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
820 } else if (size == 128) { \
821 t[0] = dfp.t64[HI_IDX]; \
822 t[1] = dfp.t64[LO_IDX]; \
826 static void RINTX_PPs(struct PPC_DFP
*dfp
)
828 dfp_set_FPRF_from_FRT(dfp
);
829 dfp_check_for_XX(dfp
);
830 dfp_check_for_VXSNAN(dfp
);
833 DFP_HELPER_RINT(drintx
, RINTX_PPs
, 64)
834 DFP_HELPER_RINT(drintxq
, RINTX_PPs
, 128)
836 static void RINTN_PPs(struct PPC_DFP
*dfp
)
838 dfp_set_FPRF_from_FRT(dfp
);
839 dfp_check_for_VXSNAN(dfp
);
842 DFP_HELPER_RINT(drintn
, RINTN_PPs
, 64)
843 DFP_HELPER_RINT(drintnq
, RINTN_PPs
, 128)
845 void helper_dctdp(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
848 uint32_t b_short
= *b
;
849 dfp_prepare_decimal64(&dfp
, 0, 0, env
);
850 decimal32ToNumber((decimal32
*)&b_short
, &dfp
.t
);
851 decimal64FromNumber((decimal64
*)t
, &dfp
.t
, &dfp
.context
);
852 dfp_set_FPRF_from_FRT(&dfp
);
855 void helper_dctqpq(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
858 dfp_prepare_decimal128(&dfp
, 0, 0, env
);
859 decimal64ToNumber((decimal64
*)b
, &dfp
.t
);
861 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
862 dfp_set_FPRF_from_FRT(&dfp
);
864 decimal128FromNumber((decimal128
*)&dfp
.t64
, &dfp
.t
, &dfp
.context
);
865 t
[0] = dfp
.t64
[HI_IDX
];
866 t
[1] = dfp
.t64
[LO_IDX
];
869 void helper_drsp(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
872 uint32_t t_short
= 0;
873 dfp_prepare_decimal64(&dfp
, 0, b
, env
);
874 decimal32FromNumber((decimal32
*)&t_short
, &dfp
.b
, &dfp
.context
);
875 decimal32ToNumber((decimal32
*)&t_short
, &dfp
.t
);
877 dfp_set_FPRF_from_FRT_short(&dfp
);
878 dfp_check_for_OX(&dfp
);
879 dfp_check_for_UX(&dfp
);
880 dfp_check_for_XX(&dfp
);
885 void helper_drdpq(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
888 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
889 decimal64FromNumber((decimal64
*)&dfp
.t64
, &dfp
.b
, &dfp
.context
);
890 decimal64ToNumber((decimal64
*)&dfp
.t64
, &dfp
.t
);
892 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
893 dfp_set_FPRF_from_FRT_long(&dfp
);
894 dfp_check_for_OX(&dfp
);
895 dfp_check_for_UX(&dfp
);
896 dfp_check_for_XX(&dfp
);
898 decimal64FromNumber((decimal64
*)dfp
.t64
, &dfp
.t
, &dfp
.context
);
903 #define DFP_HELPER_CFFIX(op, size) \
904 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
906 struct PPC_DFP dfp; \
907 dfp_prepare_decimal##size(&dfp, 0, b, env); \
908 decNumberFromInt64(&dfp.t, (int64_t)(*b)); \
909 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
914 } else if (size == 128) { \
915 t[0] = dfp.t64[HI_IDX]; \
916 t[1] = dfp.t64[LO_IDX]; \
920 static void CFFIX_PPs(struct PPC_DFP
*dfp
)
922 dfp_set_FPRF_from_FRT(dfp
);
923 dfp_check_for_XX(dfp
);
926 DFP_HELPER_CFFIX(dcffix
, 64)
927 DFP_HELPER_CFFIX(dcffixq
, 128)
929 #define DFP_HELPER_CTFIX(op, size) \
930 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
932 struct PPC_DFP dfp; \
933 dfp_prepare_decimal##size(&dfp, 0, b, env); \
935 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
936 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
937 if (decNumberIsInfinite(&dfp.b)) { \
938 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
940 dfp.t64[0] = INT64_MIN; \
941 if (decNumberIsSNaN(&dfp.b)) { \
942 invalid_flags |= FP_VXSNAN; \
945 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
946 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
949 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
950 dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
951 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
952 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
953 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
955 dfp_check_for_XX(&dfp); \
962 DFP_HELPER_CTFIX(dctfix
, 64)
963 DFP_HELPER_CTFIX(dctfixq
, 128)
965 static inline void dfp_set_bcd_digit_64(uint64_t *t
, uint8_t digit
,
968 *t
|= ((uint64_t)(digit
& 0xF) << (n
<< 2));
971 static inline void dfp_set_bcd_digit_128(uint64_t *t
, uint8_t digit
,
974 t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] |=
975 ((uint64_t)(digit
& 0xF) << ((n
& 15) << 2));
978 static inline void dfp_set_sign_64(uint64_t *t
, uint8_t sgn
)
984 static inline void dfp_set_sign_128(uint64_t *t
, uint8_t sgn
)
987 t
[HI_IDX
] |= (t
[LO_IDX
] >> 60);
989 t
[LO_IDX
] |= (sgn
& 0xF);
992 #define DFP_HELPER_DEDPD(op, size) \
993 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t sp) \
995 struct PPC_DFP dfp; \
996 uint8_t digits[34]; \
999 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1001 decNumberGetBCD(&dfp.b, digits); \
1002 dfp.t64[0] = dfp.t64[1] = 0; \
1005 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1006 dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i); \
1012 if (decNumberIsNegative(&dfp.b)) { \
1015 sgn = ((sp & 1) ? 0xF : 0xC); \
1017 dfp_set_sign_##size(dfp.t64, sgn); \
1021 t[0] = dfp.t64[0]; \
1022 } else if (size == 128) { \
1023 t[0] = dfp.t64[HI_IDX]; \
1024 t[1] = dfp.t64[LO_IDX]; \
1028 DFP_HELPER_DEDPD(ddedpd
, 64)
1029 DFP_HELPER_DEDPD(ddedpdq
, 128)
1031 static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t
, unsigned n
)
1033 return *t
>> ((n
<< 2) & 63) & 15;
1036 static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t
, unsigned n
)
1038 return t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] >> ((n
<< 2) & 63) & 15;
1041 #define DFP_HELPER_ENBCD(op, size) \
1042 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t s) \
1044 struct PPC_DFP dfp; \
1045 uint8_t digits[32]; \
1046 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1048 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1050 decNumberZero(&dfp.t); \
1053 uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1054 switch (sgnNibble) { \
1066 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1071 while (offset < (size)/4) { \
1073 digits[(size)/4-n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1074 if (digits[(size)/4-n] > 10) { \
1075 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1078 nonzero |= (digits[(size)/4-n] > 0); \
1083 decNumberSetBCD(&dfp.t, digits+((size)/4)-n, n); \
1087 dfp.t.bits |= DECNEG; \
1089 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1091 dfp_set_FPRF_from_FRT(&dfp); \
1092 if ((size) == 64) { \
1093 t[0] = dfp.t64[0]; \
1094 } else if ((size) == 128) { \
1095 t[0] = dfp.t64[HI_IDX]; \
1096 t[1] = dfp.t64[LO_IDX]; \
1100 DFP_HELPER_ENBCD(denbcd
, 64)
1101 DFP_HELPER_ENBCD(denbcdq
, 128)
1103 #define DFP_HELPER_XEX(op, size) \
1104 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
1106 struct PPC_DFP dfp; \
1108 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1110 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1111 if (decNumberIsInfinite(&dfp.b)) { \
1113 } else if (decNumberIsSNaN(&dfp.b)) { \
1115 } else if (decNumberIsQNaN(&dfp.b)) { \
1121 if ((size) == 64) { \
1122 *t = dfp.b.exponent + 398; \
1123 } else if ((size) == 128) { \
1124 *t = dfp.b.exponent + 6176; \
1131 DFP_HELPER_XEX(dxex
, 64)
1132 DFP_HELPER_XEX(dxexq
, 128)
1134 static void dfp_set_raw_exp_64(uint64_t *t
, uint64_t raw
)
1136 *t
&= 0x8003ffffffffffffULL
;
1137 *t
|= (raw
<< (63-13));
1140 static void dfp_set_raw_exp_128(uint64_t *t
, uint64_t raw
)
1142 t
[HI_IDX
] &= 0x80003fffffffffffULL
;
1143 t
[HI_IDX
] |= (raw
<< (63-17));
1146 #define DFP_HELPER_IEX(op, size) \
1147 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
1149 struct PPC_DFP dfp; \
1150 uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \
1152 int64_t exp = *((int64_t *)a); \
1154 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1156 if ((size) == 64) { \
1158 raw_qnan = 0x1F00; \
1159 raw_snan = 0x1F80; \
1162 } else if ((size) == 128) { \
1164 raw_qnan = 0x1f000; \
1165 raw_snan = 0x1f800; \
1166 raw_inf = 0x1e000; \
1172 if (unlikely((exp < 0) || (exp > max_exp))) { \
1173 dfp.t64[0] = dfp.b64[0]; \
1174 dfp.t64[1] = dfp.b64[1]; \
1176 dfp_set_raw_exp_##size(dfp.t64, raw_inf); \
1177 } else if (exp == -3) { \
1178 dfp_set_raw_exp_##size(dfp.t64, raw_snan); \
1180 dfp_set_raw_exp_##size(dfp.t64, raw_qnan); \
1184 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1185 dfp.t.bits &= ~DECSPECIAL; \
1187 dfp.t.exponent = exp - bias; \
1188 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1192 t[0] = dfp.t64[0]; \
1193 } else if (size == 128) { \
1194 t[0] = dfp.t64[HI_IDX]; \
1195 t[1] = dfp.t64[LO_IDX]; \
1199 DFP_HELPER_IEX(diex
, 64)
1200 DFP_HELPER_IEX(diexq
, 128)
1202 static void dfp_clear_lmd_from_g5msb(uint64_t *t
)
1205 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1206 /* from the left-most decimal digit (LMD) and the biased exponent. */
1207 /* This routine clears the LMD bits while preserving the exponent */
1208 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1209 /* Finite Numbers" in the Power ISA for additional details. */
1211 uint64_t g5msb
= (*t
>> 58) & 0x1F;
1213 if ((g5msb
>> 3) < 3) { /* LMD in [0-7] ? */
1214 *t
&= ~(7ULL << 58);
1216 switch (g5msb
& 7) {
1237 *t
&= ~(0x1fULL
<< 58);
1238 *t
|= (g5msb
<< 58);
1242 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1243 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
1246 struct PPC_DFP dfp; \
1247 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1249 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1251 if (sh <= max_digits) { \
1254 unsigned special = dfp.a.bits & DECSPECIAL; \
1257 decNumberFromUInt32(&shd, sh); \
1259 decNumberFromInt32(&shd, -((int32_t)sh)); \
1262 dfp.a.bits &= ~DECSPECIAL; \
1263 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1265 dfp.t.bits |= special; \
1266 if (special && (dfp.t.digits >= max_digits)) { \
1267 dfp.t.digits = max_digits - 1; \
1270 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1273 if ((size) == 64) { \
1274 dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL; \
1275 dfp_clear_lmd_from_g5msb(dfp.t64); \
1277 dfp.t64[HI_IDX] = dfp.a64[HI_IDX] & \
1278 0xFFFFC00000000000ULL; \
1279 dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX); \
1280 dfp.t64[LO_IDX] = 0; \
1284 if ((size) == 64) { \
1285 t[0] = dfp.t64[0]; \
1287 t[0] = dfp.t64[HI_IDX]; \
1288 t[1] = dfp.t64[LO_IDX]; \
1292 DFP_HELPER_SHIFT(dscli
, 64, 1)
1293 DFP_HELPER_SHIFT(dscliq
, 128, 1)
1294 DFP_HELPER_SHIFT(dscri
, 64, 0)
1295 DFP_HELPER_SHIFT(dscriq
, 128, 0)