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)
41 uint64_t t64
[2], a64
[2], b64
[2];
47 static void dfp_prepare_rounding_mode(decContext
*context
, uint64_t fpscr
)
51 switch ((fpscr
>> 32) & 0x7) {
53 rnd
= DEC_ROUND_HALF_EVEN
;
59 rnd
= DEC_ROUND_CEILING
;
62 rnd
= DEC_ROUND_FLOOR
;
65 rnd
= DEC_ROUND_HALF_UP
;
68 rnd
= DEC_ROUND_HALF_DOWN
;
77 g_assert_not_reached();
80 decContextSetRounding(context
, rnd
);
83 static void dfp_set_round_mode_from_immediate(uint8_t r
, uint8_t rmc
,
90 rnd
= DEC_ROUND_HALF_EVEN
;
96 rnd
= DEC_ROUND_HALF_UP
;
98 case 3: /* use FPSCR rounding mode */
101 assert(0); /* cannot get here */
103 } else { /* r == 1 */
106 rnd
= DEC_ROUND_CEILING
;
109 rnd
= DEC_ROUND_FLOOR
;
115 rnd
= DEC_ROUND_HALF_DOWN
;
118 assert(0); /* cannot get here */
121 decContextSetRounding(&dfp
->context
, rnd
);
124 static void dfp_prepare_decimal64(struct PPC_DFP
*dfp
, uint64_t *a
,
125 uint64_t *b
, CPUPPCState
*env
)
127 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL64
);
128 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
133 decimal64ToNumber((decimal64
*)dfp
->a64
, &dfp
->a
);
136 decNumberZero(&dfp
->a
);
141 decimal64ToNumber((decimal64
*)dfp
->b64
, &dfp
->b
);
144 decNumberZero(&dfp
->b
);
148 static void dfp_prepare_decimal128(struct PPC_DFP
*dfp
, uint64_t *a
,
149 uint64_t *b
, CPUPPCState
*env
)
151 decContextDefault(&dfp
->context
, DEC_INIT_DECIMAL128
);
152 dfp_prepare_rounding_mode(&dfp
->context
, env
->fpscr
);
156 dfp
->a64
[0] = a
[HI_IDX
];
157 dfp
->a64
[1] = a
[LO_IDX
];
158 decimal128ToNumber((decimal128
*)dfp
->a64
, &dfp
->a
);
160 dfp
->a64
[0] = dfp
->a64
[1] = 0;
161 decNumberZero(&dfp
->a
);
165 dfp
->b64
[0] = b
[HI_IDX
];
166 dfp
->b64
[1] = b
[LO_IDX
];
167 decimal128ToNumber((decimal128
*)dfp
->b64
, &dfp
->b
);
169 dfp
->b64
[0] = dfp
->b64
[1] = 0;
170 decNumberZero(&dfp
->b
);
174 static void dfp_set_FPSCR_flag(struct PPC_DFP
*dfp
, uint64_t flag
,
177 dfp
->env
->fpscr
|= (flag
| FP_FX
);
178 if (dfp
->env
->fpscr
& enabled
) {
179 dfp
->env
->fpscr
|= FP_FEX
;
183 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP
*dfp
,
189 switch (decNumberClass(&dfp
->t
, context
)) {
196 case DEC_CLASS_NEG_INF
:
199 case DEC_CLASS_NEG_NORMAL
:
202 case DEC_CLASS_NEG_SUBNORMAL
:
205 case DEC_CLASS_NEG_ZERO
:
208 case DEC_CLASS_POS_ZERO
:
211 case DEC_CLASS_POS_SUBNORMAL
:
214 case DEC_CLASS_POS_NORMAL
:
217 case DEC_CLASS_POS_INF
:
221 assert(0); /* should never get here */
223 dfp
->env
->fpscr
&= ~(0x1F << 12);
224 dfp
->env
->fpscr
|= (fprf
<< 12);
227 static void dfp_set_FPRF_from_FRT(struct PPC_DFP
*dfp
)
229 dfp_set_FPRF_from_FRT_with_context(dfp
, &dfp
->context
);
232 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP
*dfp
)
234 decContext shortContext
;
235 decContextDefault(&shortContext
, DEC_INIT_DECIMAL32
);
236 dfp_set_FPRF_from_FRT_with_context(dfp
, &shortContext
);
239 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP
*dfp
)
241 decContext longContext
;
242 decContextDefault(&longContext
, DEC_INIT_DECIMAL64
);
243 dfp_set_FPRF_from_FRT_with_context(dfp
, &longContext
);
246 static void dfp_check_for_OX(struct PPC_DFP
*dfp
)
248 if (dfp
->context
.status
& DEC_Overflow
) {
249 dfp_set_FPSCR_flag(dfp
, FP_OX
, FP_OE
);
253 static void dfp_check_for_UX(struct PPC_DFP
*dfp
)
255 if (dfp
->context
.status
& DEC_Underflow
) {
256 dfp_set_FPSCR_flag(dfp
, FP_UX
, FP_UE
);
260 static void dfp_check_for_XX(struct PPC_DFP
*dfp
)
262 if (dfp
->context
.status
& DEC_Inexact
) {
263 dfp_set_FPSCR_flag(dfp
, FP_XX
| FP_FI
, FP_XE
);
267 static void dfp_check_for_ZX(struct PPC_DFP
*dfp
)
269 if (dfp
->context
.status
& DEC_Division_by_zero
) {
270 dfp_set_FPSCR_flag(dfp
, FP_ZX
, FP_ZE
);
274 static void dfp_check_for_VXSNAN(struct PPC_DFP
*dfp
)
276 if (dfp
->context
.status
& DEC_Invalid_operation
) {
277 if (decNumberIsSNaN(&dfp
->a
) || decNumberIsSNaN(&dfp
->b
)) {
278 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
283 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP
*dfp
)
285 if (decNumberIsSNaN(&dfp
->t
)) {
286 dfp
->t
.bits
&= ~DECSNAN
;
287 dfp
->t
.bits
|= DECNAN
;
288 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FP_VE
);
292 static void dfp_check_for_VXISI(struct PPC_DFP
*dfp
, int testForSameSign
)
294 if (dfp
->context
.status
& DEC_Invalid_operation
) {
295 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
296 int same
= decNumberClass(&dfp
->a
, &dfp
->context
) ==
297 decNumberClass(&dfp
->b
, &dfp
->context
);
298 if ((same
&& testForSameSign
) || (!same
&& !testForSameSign
)) {
299 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXISI
, FP_VE
);
305 static void dfp_check_for_VXISI_add(struct PPC_DFP
*dfp
)
307 dfp_check_for_VXISI(dfp
, 0);
310 static void dfp_check_for_VXISI_subtract(struct PPC_DFP
*dfp
)
312 dfp_check_for_VXISI(dfp
, 1);
315 static void dfp_check_for_VXIMZ(struct PPC_DFP
*dfp
)
317 if (dfp
->context
.status
& DEC_Invalid_operation
) {
318 if ((decNumberIsInfinite(&dfp
->a
) && decNumberIsZero(&dfp
->b
)) ||
319 (decNumberIsInfinite(&dfp
->b
) && decNumberIsZero(&dfp
->a
))) {
320 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIMZ
, FP_VE
);
325 static void dfp_check_for_VXZDZ(struct PPC_DFP
*dfp
)
327 if (dfp
->context
.status
& DEC_Division_undefined
) {
328 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXZDZ
, FP_VE
);
332 static void dfp_check_for_VXIDI(struct PPC_DFP
*dfp
)
334 if (dfp
->context
.status
& DEC_Invalid_operation
) {
335 if (decNumberIsInfinite(&dfp
->a
) && decNumberIsInfinite(&dfp
->b
)) {
336 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXIDI
, FP_VE
);
341 static void dfp_check_for_VXVC(struct PPC_DFP
*dfp
)
343 if (decNumberIsNaN(&dfp
->a
) || decNumberIsNaN(&dfp
->b
)) {
344 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXVC
, FP_VE
);
348 static void dfp_check_for_VXCVI(struct PPC_DFP
*dfp
)
350 if ((dfp
->context
.status
& DEC_Invalid_operation
) &&
351 (!decNumberIsSNaN(&dfp
->a
)) &&
352 (!decNumberIsSNaN(&dfp
->b
))) {
353 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
357 static void dfp_set_CRBF_from_T(struct PPC_DFP
*dfp
)
359 if (decNumberIsNaN(&dfp
->t
)) {
361 } else if (decNumberIsZero(&dfp
->t
)) {
363 } else if (decNumberIsNegative(&dfp
->t
)) {
370 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP
*dfp
)
372 dfp
->env
->fpscr
&= ~(0xF << 12);
373 dfp
->env
->fpscr
|= (dfp
->crbf
<< 12);
376 static inline void dfp_makeQNaN(decNumber
*dn
)
378 dn
->bits
&= ~DECSPECIAL
;
382 static inline int dfp_get_digit(decNumber
*dn
, int n
)
384 assert(DECDPUN
== 3);
385 int unit
= n
/ DECDPUN
;
386 int dig
= n
% DECDPUN
;
389 return dn
->lsu
[unit
] % 10;
391 return (dn
->lsu
[unit
] / 10) % 10;
393 return dn
->lsu
[unit
] / 100;
395 g_assert_not_reached();
398 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
399 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
401 struct PPC_DFP dfp; \
402 dfp_prepare_decimal##size(&dfp, a, b, env); \
403 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
404 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
408 } else if (size == 128) { \
409 t[0] = dfp.t64[HI_IDX]; \
410 t[1] = dfp.t64[LO_IDX]; \
414 static void ADD_PPs(struct PPC_DFP
*dfp
)
416 dfp_set_FPRF_from_FRT(dfp
);
417 dfp_check_for_OX(dfp
);
418 dfp_check_for_UX(dfp
);
419 dfp_check_for_XX(dfp
);
420 dfp_check_for_VXSNAN(dfp
);
421 dfp_check_for_VXISI_add(dfp
);
424 DFP_HELPER_TAB(dadd
, decNumberAdd
, ADD_PPs
, 64)
425 DFP_HELPER_TAB(daddq
, decNumberAdd
, ADD_PPs
, 128)
427 static void SUB_PPs(struct PPC_DFP
*dfp
)
429 dfp_set_FPRF_from_FRT(dfp
);
430 dfp_check_for_OX(dfp
);
431 dfp_check_for_UX(dfp
);
432 dfp_check_for_XX(dfp
);
433 dfp_check_for_VXSNAN(dfp
);
434 dfp_check_for_VXISI_subtract(dfp
);
437 DFP_HELPER_TAB(dsub
, decNumberSubtract
, SUB_PPs
, 64)
438 DFP_HELPER_TAB(dsubq
, decNumberSubtract
, SUB_PPs
, 128)
440 static void MUL_PPs(struct PPC_DFP
*dfp
)
442 dfp_set_FPRF_from_FRT(dfp
);
443 dfp_check_for_OX(dfp
);
444 dfp_check_for_UX(dfp
);
445 dfp_check_for_XX(dfp
);
446 dfp_check_for_VXSNAN(dfp
);
447 dfp_check_for_VXIMZ(dfp
);
450 DFP_HELPER_TAB(dmul
, decNumberMultiply
, MUL_PPs
, 64)
451 DFP_HELPER_TAB(dmulq
, decNumberMultiply
, MUL_PPs
, 128)
453 static void DIV_PPs(struct PPC_DFP
*dfp
)
455 dfp_set_FPRF_from_FRT(dfp
);
456 dfp_check_for_OX(dfp
);
457 dfp_check_for_UX(dfp
);
458 dfp_check_for_ZX(dfp
);
459 dfp_check_for_XX(dfp
);
460 dfp_check_for_VXSNAN(dfp
);
461 dfp_check_for_VXZDZ(dfp
);
462 dfp_check_for_VXIDI(dfp
);
465 DFP_HELPER_TAB(ddiv
, decNumberDivide
, DIV_PPs
, 64)
466 DFP_HELPER_TAB(ddivq
, decNumberDivide
, DIV_PPs
, 128)
468 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \
469 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
471 struct PPC_DFP dfp; \
472 dfp_prepare_decimal##size(&dfp, a, b, env); \
473 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
474 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
479 static void CMPU_PPs(struct PPC_DFP
*dfp
)
481 dfp_set_CRBF_from_T(dfp
);
482 dfp_set_FPCC_from_CRBF(dfp
);
483 dfp_check_for_VXSNAN(dfp
);
486 DFP_HELPER_BF_AB(dcmpu
, decNumberCompare
, CMPU_PPs
, 64)
487 DFP_HELPER_BF_AB(dcmpuq
, decNumberCompare
, CMPU_PPs
, 128)
489 static void CMPO_PPs(struct PPC_DFP
*dfp
)
491 dfp_set_CRBF_from_T(dfp
);
492 dfp_set_FPCC_from_CRBF(dfp
);
493 dfp_check_for_VXSNAN(dfp
);
494 dfp_check_for_VXVC(dfp
);
497 DFP_HELPER_BF_AB(dcmpo
, decNumberCompare
, CMPO_PPs
, 64)
498 DFP_HELPER_BF_AB(dcmpoq
, decNumberCompare
, CMPO_PPs
, 128)
500 #define DFP_HELPER_TSTDC(op, size) \
501 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \
503 struct PPC_DFP dfp; \
506 dfp_prepare_decimal##size(&dfp, a, 0, env); \
508 match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \
509 match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
510 match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \
511 match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \
512 match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \
513 match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \
515 if (decNumberIsNegative(&dfp.a)) { \
516 dfp.crbf = match ? 0xA : 0x8; \
518 dfp.crbf = match ? 0x2 : 0x0; \
521 dfp_set_FPCC_from_CRBF(&dfp); \
525 DFP_HELPER_TSTDC(dtstdc
, 64)
526 DFP_HELPER_TSTDC(dtstdcq
, 128)
528 #define DFP_HELPER_TSTDG(op, size) \
529 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \
531 struct PPC_DFP dfp; \
532 int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \
533 is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \
536 dfp_prepare_decimal##size(&dfp, a, 0, env); \
538 if ((size) == 64) { \
543 } else if ((size) == 128) { \
550 is_negative = decNumberIsNegative(&dfp.a); \
551 is_zero = decNumberIsZero(&dfp.a); \
552 is_extreme_exp = (dfp.a.exponent == maxexp) || \
553 (dfp.a.exponent == minexp); \
554 is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \
555 is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \
556 leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \
557 (dfp.a.lsu[nzero_idx] != 0); \
560 match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \
561 match |= (dcm & 0x10) && is_zero && is_extreme_exp; \
562 match |= (dcm & 0x08) && \
563 (is_subnormal || (is_normal && is_extreme_exp)); \
564 match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \
565 !leftmost_is_nonzero; \
566 match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \
567 leftmost_is_nonzero; \
568 match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \
571 dfp.crbf = match ? 0xA : 0x8; \
573 dfp.crbf = match ? 0x2 : 0x0; \
576 dfp_set_FPCC_from_CRBF(&dfp); \
580 DFP_HELPER_TSTDG(dtstdg
, 64)
581 DFP_HELPER_TSTDG(dtstdgq
, 128)
583 #define DFP_HELPER_TSTEX(op, size) \
584 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
586 struct PPC_DFP dfp; \
587 int expa, expb, a_is_special, b_is_special; \
589 dfp_prepare_decimal##size(&dfp, a, b, env); \
591 expa = dfp.a.exponent; \
592 expb = dfp.b.exponent; \
593 a_is_special = decNumberIsSpecial(&dfp.a); \
594 b_is_special = decNumberIsSpecial(&dfp.b); \
596 if (a_is_special || b_is_special) { \
597 int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
598 int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
599 dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \
600 } else if (expa < expb) { \
602 } else if (expa > expb) { \
608 dfp_set_FPCC_from_CRBF(&dfp); \
612 DFP_HELPER_TSTEX(dtstex
, 64)
613 DFP_HELPER_TSTEX(dtstexq
, 128)
615 #define DFP_HELPER_TSTSF(op, size) \
616 uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \
618 struct PPC_DFP dfp; \
621 dfp_prepare_decimal##size(&dfp, 0, b, env); \
625 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
627 } else if (k == 0) { \
629 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
630 /* Zero has no sig digits */ \
633 unsigned nsd = dfp.b.digits; \
636 } else if (k > nsd) { \
643 dfp_set_FPCC_from_CRBF(&dfp); \
647 DFP_HELPER_TSTSF(dtstsf
, 64)
648 DFP_HELPER_TSTSF(dtstsfq
, 128)
650 #define DFP_HELPER_TSTSFI(op, size) \
651 uint32_t helper_##op(CPUPPCState *env, uint32_t a, uint64_t *b) \
653 struct PPC_DFP dfp; \
656 dfp_prepare_decimal##size(&dfp, 0, b, env); \
660 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
662 } else if (uim == 0) { \
664 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
665 /* Zero has no sig digits */ \
668 unsigned nsd = dfp.b.digits; \
671 } else if (uim > nsd) { \
678 dfp_set_FPCC_from_CRBF(&dfp); \
682 DFP_HELPER_TSTSFI(dtstsfi
, 64)
683 DFP_HELPER_TSTSFI(dtstsfiq
, 128)
685 static void QUA_PPs(struct PPC_DFP
*dfp
)
687 dfp_set_FPRF_from_FRT(dfp
);
688 dfp_check_for_XX(dfp
);
689 dfp_check_for_VXSNAN(dfp
);
690 dfp_check_for_VXCVI(dfp
);
693 static void dfp_quantize(uint8_t rmc
, struct PPC_DFP
*dfp
)
695 dfp_set_round_mode_from_immediate(0, rmc
, dfp
);
696 decNumberQuantize(&dfp
->t
, &dfp
->b
, &dfp
->a
, &dfp
->context
);
697 if (decNumberIsSNaN(&dfp
->a
)) {
699 dfp_makeQNaN(&dfp
->t
);
700 } else if (decNumberIsSNaN(&dfp
->b
)) {
702 dfp_makeQNaN(&dfp
->t
);
703 } else if (decNumberIsQNaN(&dfp
->a
)) {
705 } else if (decNumberIsQNaN(&dfp
->b
)) {
710 #define DFP_HELPER_QUAI(op, size) \
711 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
712 uint32_t te, uint32_t rmc) \
714 struct PPC_DFP dfp; \
716 dfp_prepare_decimal##size(&dfp, 0, b, env); \
718 decNumberFromUInt32(&dfp.a, 1); \
719 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
721 dfp_quantize(rmc, &dfp); \
722 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
728 } else if (size == 128) { \
729 t[0] = dfp.t64[HI_IDX]; \
730 t[1] = dfp.t64[LO_IDX]; \
734 DFP_HELPER_QUAI(dquai
, 64)
735 DFP_HELPER_QUAI(dquaiq
, 128)
737 #define DFP_HELPER_QUA(op, size) \
738 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
739 uint64_t *b, uint32_t rmc) \
741 struct PPC_DFP dfp; \
743 dfp_prepare_decimal##size(&dfp, a, b, env); \
745 dfp_quantize(rmc, &dfp); \
746 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
752 } else if (size == 128) { \
753 t[0] = dfp.t64[HI_IDX]; \
754 t[1] = dfp.t64[LO_IDX]; \
758 DFP_HELPER_QUA(dqua
, 64)
759 DFP_HELPER_QUA(dquaq
, 128)
761 static void _dfp_reround(uint8_t rmc
, int32_t ref_sig
, int32_t xmax
,
764 int msd_orig
, msd_rslt
;
766 if (unlikely((ref_sig
== 0) || (dfp
->b
.digits
<= ref_sig
))) {
768 if (decNumberIsSNaN(&dfp
->b
)) {
769 dfp_makeQNaN(&dfp
->t
);
770 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXSNAN
, FPSCR_VE
);
775 /* Reround is equivalent to quantizing b with 1**E(n) where */
776 /* n = exp(b) + numDigits(b) - reference_significance. */
778 decNumberFromUInt32(&dfp
->a
, 1);
779 dfp
->a
.exponent
= dfp
->b
.exponent
+ dfp
->b
.digits
- ref_sig
;
781 if (unlikely(dfp
->a
.exponent
> xmax
)) {
783 dfp
->t
.bits
&= ~DECNEG
;
784 dfp_makeQNaN(&dfp
->t
);
785 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FPSCR_VE
);
789 dfp_quantize(rmc
, dfp
);
791 msd_orig
= dfp_get_digit(&dfp
->b
, dfp
->b
.digits
-1);
792 msd_rslt
= dfp_get_digit(&dfp
->t
, dfp
->t
.digits
-1);
794 /* If the quantization resulted in rounding up to the next magnitude, */
795 /* then we need to shift the significand and adjust the exponent. */
797 if (unlikely((msd_orig
== 9) && (msd_rslt
== 1))) {
801 decNumberFromInt32(&negone
, -1);
802 decNumberShift(&dfp
->t
, &dfp
->t
, &negone
, &dfp
->context
);
805 if (unlikely(dfp
->t
.exponent
> xmax
)) {
806 dfp_makeQNaN(&dfp
->t
);
808 dfp_set_FPSCR_flag(dfp
, FP_VX
| FP_VXCVI
, FP_VE
);
809 /* Inhibit XX in this case */
810 decContextClearStatus(&dfp
->context
, DEC_Inexact
);
815 #define DFP_HELPER_RRND(op, size) \
816 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
817 uint64_t *b, uint32_t rmc) \
819 struct PPC_DFP dfp; \
820 int32_t ref_sig = *a & 0x3F; \
821 int32_t xmax = ((size) == 64) ? 369 : 6111; \
823 dfp_prepare_decimal##size(&dfp, 0, b, env); \
825 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
826 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
832 } else if (size == 128) { \
833 t[0] = dfp.t64[HI_IDX]; \
834 t[1] = dfp.t64[LO_IDX]; \
838 DFP_HELPER_RRND(drrnd
, 64)
839 DFP_HELPER_RRND(drrndq
, 128)
841 #define DFP_HELPER_RINT(op, postprocs, size) \
842 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
843 uint32_t r, uint32_t rmc) \
845 struct PPC_DFP dfp; \
847 dfp_prepare_decimal##size(&dfp, 0, b, env); \
849 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
850 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
851 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
856 } else if (size == 128) { \
857 t[0] = dfp.t64[HI_IDX]; \
858 t[1] = dfp.t64[LO_IDX]; \
862 static void RINTX_PPs(struct PPC_DFP
*dfp
)
864 dfp_set_FPRF_from_FRT(dfp
);
865 dfp_check_for_XX(dfp
);
866 dfp_check_for_VXSNAN(dfp
);
869 DFP_HELPER_RINT(drintx
, RINTX_PPs
, 64)
870 DFP_HELPER_RINT(drintxq
, RINTX_PPs
, 128)
872 static void RINTN_PPs(struct PPC_DFP
*dfp
)
874 dfp_set_FPRF_from_FRT(dfp
);
875 dfp_check_for_VXSNAN(dfp
);
878 DFP_HELPER_RINT(drintn
, RINTN_PPs
, 64)
879 DFP_HELPER_RINT(drintnq
, RINTN_PPs
, 128)
881 void helper_dctdp(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
884 uint32_t b_short
= *b
;
885 dfp_prepare_decimal64(&dfp
, 0, 0, env
);
886 decimal32ToNumber((decimal32
*)&b_short
, &dfp
.t
);
887 decimal64FromNumber((decimal64
*)t
, &dfp
.t
, &dfp
.context
);
888 dfp_set_FPRF_from_FRT(&dfp
);
891 void helper_dctqpq(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
894 dfp_prepare_decimal128(&dfp
, 0, 0, env
);
895 decimal64ToNumber((decimal64
*)b
, &dfp
.t
);
897 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
898 dfp_set_FPRF_from_FRT(&dfp
);
900 decimal128FromNumber((decimal128
*)&dfp
.t64
, &dfp
.t
, &dfp
.context
);
901 t
[0] = dfp
.t64
[HI_IDX
];
902 t
[1] = dfp
.t64
[LO_IDX
];
905 void helper_drsp(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
908 uint32_t t_short
= 0;
909 dfp_prepare_decimal64(&dfp
, 0, b
, env
);
910 decimal32FromNumber((decimal32
*)&t_short
, &dfp
.b
, &dfp
.context
);
911 decimal32ToNumber((decimal32
*)&t_short
, &dfp
.t
);
913 dfp_set_FPRF_from_FRT_short(&dfp
);
914 dfp_check_for_OX(&dfp
);
915 dfp_check_for_UX(&dfp
);
916 dfp_check_for_XX(&dfp
);
921 void helper_drdpq(CPUPPCState
*env
, uint64_t *t
, uint64_t *b
)
924 dfp_prepare_decimal128(&dfp
, 0, b
, env
);
925 decimal64FromNumber((decimal64
*)&dfp
.t64
, &dfp
.b
, &dfp
.context
);
926 decimal64ToNumber((decimal64
*)&dfp
.t64
, &dfp
.t
);
928 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp
);
929 dfp_set_FPRF_from_FRT_long(&dfp
);
930 dfp_check_for_OX(&dfp
);
931 dfp_check_for_UX(&dfp
);
932 dfp_check_for_XX(&dfp
);
934 decimal64FromNumber((decimal64
*)dfp
.t64
, &dfp
.t
, &dfp
.context
);
939 #define DFP_HELPER_CFFIX(op, size) \
940 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
942 struct PPC_DFP dfp; \
943 dfp_prepare_decimal##size(&dfp, 0, b, env); \
944 decNumberFromInt64(&dfp.t, (int64_t)(*b)); \
945 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
950 } else if (size == 128) { \
951 t[0] = dfp.t64[HI_IDX]; \
952 t[1] = dfp.t64[LO_IDX]; \
956 static void CFFIX_PPs(struct PPC_DFP
*dfp
)
958 dfp_set_FPRF_from_FRT(dfp
);
959 dfp_check_for_XX(dfp
);
962 DFP_HELPER_CFFIX(dcffix
, 64)
963 DFP_HELPER_CFFIX(dcffixq
, 128)
965 #define DFP_HELPER_CTFIX(op, size) \
966 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
968 struct PPC_DFP dfp; \
969 dfp_prepare_decimal##size(&dfp, 0, b, env); \
971 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
972 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
973 if (decNumberIsInfinite(&dfp.b)) { \
974 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
976 dfp.t64[0] = INT64_MIN; \
977 if (decNumberIsSNaN(&dfp.b)) { \
978 invalid_flags |= FP_VXSNAN; \
981 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
982 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
985 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
986 dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
987 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
988 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
989 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
991 dfp_check_for_XX(&dfp); \
998 DFP_HELPER_CTFIX(dctfix
, 64)
999 DFP_HELPER_CTFIX(dctfixq
, 128)
1001 static inline void dfp_set_bcd_digit_64(uint64_t *t
, uint8_t digit
,
1004 *t
|= ((uint64_t)(digit
& 0xF) << (n
<< 2));
1007 static inline void dfp_set_bcd_digit_128(uint64_t *t
, uint8_t digit
,
1010 t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] |=
1011 ((uint64_t)(digit
& 0xF) << ((n
& 15) << 2));
1014 static inline void dfp_set_sign_64(uint64_t *t
, uint8_t sgn
)
1020 static inline void dfp_set_sign_128(uint64_t *t
, uint8_t sgn
)
1023 t
[HI_IDX
] |= (t
[LO_IDX
] >> 60);
1025 t
[LO_IDX
] |= (sgn
& 0xF);
1028 #define DFP_HELPER_DEDPD(op, size) \
1029 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t sp) \
1031 struct PPC_DFP dfp; \
1032 uint8_t digits[34]; \
1035 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1037 decNumberGetBCD(&dfp.b, digits); \
1038 dfp.t64[0] = dfp.t64[1] = 0; \
1041 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1042 dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i); \
1048 if (decNumberIsNegative(&dfp.b)) { \
1051 sgn = ((sp & 1) ? 0xF : 0xC); \
1053 dfp_set_sign_##size(dfp.t64, sgn); \
1057 t[0] = dfp.t64[0]; \
1058 } else if (size == 128) { \
1059 t[0] = dfp.t64[HI_IDX]; \
1060 t[1] = dfp.t64[LO_IDX]; \
1064 DFP_HELPER_DEDPD(ddedpd
, 64)
1065 DFP_HELPER_DEDPD(ddedpdq
, 128)
1067 static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t
, unsigned n
)
1069 return *t
>> ((n
<< 2) & 63) & 15;
1072 static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t
, unsigned n
)
1074 return t
[(n
& 0x10) ? HI_IDX
: LO_IDX
] >> ((n
<< 2) & 63) & 15;
1077 #define DFP_HELPER_ENBCD(op, size) \
1078 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t s) \
1080 struct PPC_DFP dfp; \
1081 uint8_t digits[32]; \
1082 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1084 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1086 decNumberZero(&dfp.t); \
1089 uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1090 switch (sgnNibble) { \
1102 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1107 while (offset < (size)/4) { \
1109 digits[(size)/4-n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1110 if (digits[(size)/4-n] > 10) { \
1111 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1114 nonzero |= (digits[(size)/4-n] > 0); \
1119 decNumberSetBCD(&dfp.t, digits+((size)/4)-n, n); \
1123 dfp.t.bits |= DECNEG; \
1125 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1127 dfp_set_FPRF_from_FRT(&dfp); \
1128 if ((size) == 64) { \
1129 t[0] = dfp.t64[0]; \
1130 } else if ((size) == 128) { \
1131 t[0] = dfp.t64[HI_IDX]; \
1132 t[1] = dfp.t64[LO_IDX]; \
1136 DFP_HELPER_ENBCD(denbcd
, 64)
1137 DFP_HELPER_ENBCD(denbcdq
, 128)
1139 #define DFP_HELPER_XEX(op, size) \
1140 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
1142 struct PPC_DFP dfp; \
1144 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1146 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1147 if (decNumberIsInfinite(&dfp.b)) { \
1149 } else if (decNumberIsSNaN(&dfp.b)) { \
1151 } else if (decNumberIsQNaN(&dfp.b)) { \
1157 if ((size) == 64) { \
1158 *t = dfp.b.exponent + 398; \
1159 } else if ((size) == 128) { \
1160 *t = dfp.b.exponent + 6176; \
1167 DFP_HELPER_XEX(dxex
, 64)
1168 DFP_HELPER_XEX(dxexq
, 128)
1170 static void dfp_set_raw_exp_64(uint64_t *t
, uint64_t raw
)
1172 *t
&= 0x8003ffffffffffffULL
;
1173 *t
|= (raw
<< (63-13));
1176 static void dfp_set_raw_exp_128(uint64_t *t
, uint64_t raw
)
1178 t
[HI_IDX
] &= 0x80003fffffffffffULL
;
1179 t
[HI_IDX
] |= (raw
<< (63-17));
1182 #define DFP_HELPER_IEX(op, size) \
1183 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
1185 struct PPC_DFP dfp; \
1186 uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \
1188 int64_t exp = *((int64_t *)a); \
1190 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1192 if ((size) == 64) { \
1194 raw_qnan = 0x1F00; \
1195 raw_snan = 0x1F80; \
1198 } else if ((size) == 128) { \
1200 raw_qnan = 0x1f000; \
1201 raw_snan = 0x1f800; \
1202 raw_inf = 0x1e000; \
1208 if (unlikely((exp < 0) || (exp > max_exp))) { \
1209 dfp.t64[0] = dfp.b64[0]; \
1210 dfp.t64[1] = dfp.b64[1]; \
1212 dfp_set_raw_exp_##size(dfp.t64, raw_inf); \
1213 } else if (exp == -3) { \
1214 dfp_set_raw_exp_##size(dfp.t64, raw_snan); \
1216 dfp_set_raw_exp_##size(dfp.t64, raw_qnan); \
1220 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1221 dfp.t.bits &= ~DECSPECIAL; \
1223 dfp.t.exponent = exp - bias; \
1224 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1228 t[0] = dfp.t64[0]; \
1229 } else if (size == 128) { \
1230 t[0] = dfp.t64[HI_IDX]; \
1231 t[1] = dfp.t64[LO_IDX]; \
1235 DFP_HELPER_IEX(diex
, 64)
1236 DFP_HELPER_IEX(diexq
, 128)
1238 static void dfp_clear_lmd_from_g5msb(uint64_t *t
)
1241 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1242 /* from the left-most decimal digit (LMD) and the biased exponent. */
1243 /* This routine clears the LMD bits while preserving the exponent */
1244 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1245 /* Finite Numbers" in the Power ISA for additional details. */
1247 uint64_t g5msb
= (*t
>> 58) & 0x1F;
1249 if ((g5msb
>> 3) < 3) { /* LMD in [0-7] ? */
1250 *t
&= ~(7ULL << 58);
1252 switch (g5msb
& 7) {
1273 *t
&= ~(0x1fULL
<< 58);
1274 *t
|= (g5msb
<< 58);
1278 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1279 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
1282 struct PPC_DFP dfp; \
1283 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1285 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1287 if (sh <= max_digits) { \
1290 unsigned special = dfp.a.bits & DECSPECIAL; \
1293 decNumberFromUInt32(&shd, sh); \
1295 decNumberFromInt32(&shd, -((int32_t)sh)); \
1298 dfp.a.bits &= ~DECSPECIAL; \
1299 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1301 dfp.t.bits |= special; \
1302 if (special && (dfp.t.digits >= max_digits)) { \
1303 dfp.t.digits = max_digits - 1; \
1306 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1309 if ((size) == 64) { \
1310 dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL; \
1311 dfp_clear_lmd_from_g5msb(dfp.t64); \
1313 dfp.t64[HI_IDX] = dfp.a64[HI_IDX] & \
1314 0xFFFFC00000000000ULL; \
1315 dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX); \
1316 dfp.t64[LO_IDX] = 0; \
1320 if ((size) == 64) { \
1321 t[0] = dfp.t64[0]; \
1323 t[0] = dfp.t64[HI_IDX]; \
1324 t[1] = dfp.t64[LO_IDX]; \
1328 DFP_HELPER_SHIFT(dscli
, 64, 1)
1329 DFP_HELPER_SHIFT(dscliq
, 128, 1)
1330 DFP_HELPER_SHIFT(dscri
, 64, 0)
1331 DFP_HELPER_SHIFT(dscriq
, 128, 0)