virtio-blk: multiqueue batch notify
[qemu/kevin.git] / target-ppc / dfp_helper.c
blobdb0ede698bcedc5b999a44435438a23e89cd34bf
1 /*
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"
21 #include "cpu.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)
32 #define HI_IDX 0
33 #define LO_IDX 1
34 #else
35 #define HI_IDX 1
36 #define LO_IDX 0
37 #endif
39 struct PPC_DFP {
40 CPUPPCState *env;
41 uint64_t t64[2], a64[2], b64[2];
42 decNumber t, a, b;
43 decContext context;
44 uint8_t crbf;
47 static void dfp_prepare_rounding_mode(decContext *context, uint64_t fpscr)
49 enum rounding rnd;
51 switch ((fpscr >> 32) & 0x7) {
52 case 0:
53 rnd = DEC_ROUND_HALF_EVEN;
54 break;
55 case 1:
56 rnd = DEC_ROUND_DOWN;
57 break;
58 case 2:
59 rnd = DEC_ROUND_CEILING;
60 break;
61 case 3:
62 rnd = DEC_ROUND_FLOOR;
63 break;
64 case 4:
65 rnd = DEC_ROUND_HALF_UP;
66 break;
67 case 5:
68 rnd = DEC_ROUND_HALF_DOWN;
69 break;
70 case 6:
71 rnd = DEC_ROUND_UP;
72 break;
73 case 7:
74 rnd = DEC_ROUND_05UP;
75 break;
76 default:
77 g_assert_not_reached();
80 decContextSetRounding(context, rnd);
83 static void dfp_set_round_mode_from_immediate(uint8_t r, uint8_t rmc,
84 struct PPC_DFP *dfp)
86 enum rounding rnd;
87 if (r == 0) {
88 switch (rmc & 3) {
89 case 0:
90 rnd = DEC_ROUND_HALF_EVEN;
91 break;
92 case 1:
93 rnd = DEC_ROUND_DOWN;
94 break;
95 case 2:
96 rnd = DEC_ROUND_HALF_UP;
97 break;
98 case 3: /* use FPSCR rounding mode */
99 return;
100 default:
101 assert(0); /* cannot get here */
103 } else { /* r == 1 */
104 switch (rmc & 3) {
105 case 0:
106 rnd = DEC_ROUND_CEILING;
107 break;
108 case 1:
109 rnd = DEC_ROUND_FLOOR;
110 break;
111 case 2:
112 rnd = DEC_ROUND_UP;
113 break;
114 case 3:
115 rnd = DEC_ROUND_HALF_DOWN;
116 break;
117 default:
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);
129 dfp->env = env;
131 if (a) {
132 dfp->a64[0] = *a;
133 decimal64ToNumber((decimal64 *)dfp->a64, &dfp->a);
134 } else {
135 dfp->a64[0] = 0;
136 decNumberZero(&dfp->a);
139 if (b) {
140 dfp->b64[0] = *b;
141 decimal64ToNumber((decimal64 *)dfp->b64, &dfp->b);
142 } else {
143 dfp->b64[0] = 0;
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);
153 dfp->env = env;
155 if (a) {
156 dfp->a64[0] = a[HI_IDX];
157 dfp->a64[1] = a[LO_IDX];
158 decimal128ToNumber((decimal128 *)dfp->a64, &dfp->a);
159 } else {
160 dfp->a64[0] = dfp->a64[1] = 0;
161 decNumberZero(&dfp->a);
164 if (b) {
165 dfp->b64[0] = b[HI_IDX];
166 dfp->b64[1] = b[LO_IDX];
167 decimal128ToNumber((decimal128 *)dfp->b64, &dfp->b);
168 } else {
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,
175 uint64_t enabled)
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,
184 decContext *context)
186 uint64_t fprf = 0;
188 /* construct FPRF */
189 switch (decNumberClass(&dfp->t, context)) {
190 case DEC_CLASS_SNAN:
191 fprf = 0x01;
192 break;
193 case DEC_CLASS_QNAN:
194 fprf = 0x11;
195 break;
196 case DEC_CLASS_NEG_INF:
197 fprf = 0x09;
198 break;
199 case DEC_CLASS_NEG_NORMAL:
200 fprf = 0x08;
201 break;
202 case DEC_CLASS_NEG_SUBNORMAL:
203 fprf = 0x18;
204 break;
205 case DEC_CLASS_NEG_ZERO:
206 fprf = 0x12;
207 break;
208 case DEC_CLASS_POS_ZERO:
209 fprf = 0x02;
210 break;
211 case DEC_CLASS_POS_SUBNORMAL:
212 fprf = 0x14;
213 break;
214 case DEC_CLASS_POS_NORMAL:
215 fprf = 0x04;
216 break;
217 case DEC_CLASS_POS_INF:
218 fprf = 0x05;
219 break;
220 default:
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)) {
360 dfp->crbf = 1;
361 } else if (decNumberIsZero(&dfp->t)) {
362 dfp->crbf = 2;
363 } else if (decNumberIsNegative(&dfp->t)) {
364 dfp->crbf = 8;
365 } else {
366 dfp->crbf = 4;
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;
379 dn->bits |= DECNAN;
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;
387 switch (dig) {
388 case 0:
389 return dn->lsu[unit] % 10;
390 case 1:
391 return (dn->lsu[unit] / 10) % 10;
392 case 2:
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); \
405 postprocs(&dfp); \
406 if (size == 64) { \
407 t[0] = dfp.t64[0]; \
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); \
475 postprocs(&dfp); \
476 return dfp.crbf; \
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; \
504 int match = 0; \
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; \
517 } else { \
518 dfp.crbf = match ? 0x2 : 0x0; \
521 dfp_set_FPCC_from_CRBF(&dfp); \
522 return dfp.crbf; \
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, \
534 match; \
536 dfp_prepare_decimal##size(&dfp, a, 0, env); \
538 if ((size) == 64) { \
539 minexp = -398; \
540 maxexp = 369; \
541 nzero_digits = 16; \
542 nzero_idx = 5; \
543 } else if ((size) == 128) { \
544 minexp = -6176; \
545 maxexp = 6111; \
546 nzero_digits = 34; \
547 nzero_idx = 11; \
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); \
558 match = 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); \
570 if (is_negative) { \
571 dfp.crbf = match ? 0xA : 0x8; \
572 } else { \
573 dfp.crbf = match ? 0x2 : 0x0; \
576 dfp_set_FPCC_from_CRBF(&dfp); \
577 return dfp.crbf; \
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) { \
601 dfp.crbf = 0x8; \
602 } else if (expa > expb) { \
603 dfp.crbf = 0x4; \
604 } else { \
605 dfp.crbf = 0x2; \
608 dfp_set_FPCC_from_CRBF(&dfp); \
609 return dfp.crbf; \
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; \
619 unsigned k; \
621 dfp_prepare_decimal##size(&dfp, 0, b, env); \
623 k = *a & 0x3F; \
625 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
626 dfp.crbf = 1; \
627 } else if (k == 0) { \
628 dfp.crbf = 4; \
629 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
630 /* Zero has no sig digits */ \
631 dfp.crbf = 4; \
632 } else { \
633 unsigned nsd = dfp.b.digits; \
634 if (k < nsd) { \
635 dfp.crbf = 8; \
636 } else if (k > nsd) { \
637 dfp.crbf = 4; \
638 } else { \
639 dfp.crbf = 2; \
643 dfp_set_FPCC_from_CRBF(&dfp); \
644 return dfp.crbf; \
647 DFP_HELPER_TSTSF(dtstsf, 64)
648 DFP_HELPER_TSTSF(dtstsfq, 128)
650 static void QUA_PPs(struct PPC_DFP *dfp)
652 dfp_set_FPRF_from_FRT(dfp);
653 dfp_check_for_XX(dfp);
654 dfp_check_for_VXSNAN(dfp);
655 dfp_check_for_VXCVI(dfp);
658 static void dfp_quantize(uint8_t rmc, struct PPC_DFP *dfp)
660 dfp_set_round_mode_from_immediate(0, rmc, dfp);
661 decNumberQuantize(&dfp->t, &dfp->b, &dfp->a, &dfp->context);
662 if (decNumberIsSNaN(&dfp->a)) {
663 dfp->t = dfp->a;
664 dfp_makeQNaN(&dfp->t);
665 } else if (decNumberIsSNaN(&dfp->b)) {
666 dfp->t = dfp->b;
667 dfp_makeQNaN(&dfp->t);
668 } else if (decNumberIsQNaN(&dfp->a)) {
669 dfp->t = dfp->a;
670 } else if (decNumberIsQNaN(&dfp->b)) {
671 dfp->t = dfp->b;
675 #define DFP_HELPER_QUAI(op, size) \
676 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
677 uint32_t te, uint32_t rmc) \
679 struct PPC_DFP dfp; \
681 dfp_prepare_decimal##size(&dfp, 0, b, env); \
683 decNumberFromUInt32(&dfp.a, 1); \
684 dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \
686 dfp_quantize(rmc, &dfp); \
687 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
688 &dfp.context); \
689 QUA_PPs(&dfp); \
691 if (size == 64) { \
692 t[0] = dfp.t64[0]; \
693 } else if (size == 128) { \
694 t[0] = dfp.t64[HI_IDX]; \
695 t[1] = dfp.t64[LO_IDX]; \
699 DFP_HELPER_QUAI(dquai, 64)
700 DFP_HELPER_QUAI(dquaiq, 128)
702 #define DFP_HELPER_QUA(op, size) \
703 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
704 uint64_t *b, uint32_t rmc) \
706 struct PPC_DFP dfp; \
708 dfp_prepare_decimal##size(&dfp, a, b, env); \
710 dfp_quantize(rmc, &dfp); \
711 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
712 &dfp.context); \
713 QUA_PPs(&dfp); \
715 if (size == 64) { \
716 t[0] = dfp.t64[0]; \
717 } else if (size == 128) { \
718 t[0] = dfp.t64[HI_IDX]; \
719 t[1] = dfp.t64[LO_IDX]; \
723 DFP_HELPER_QUA(dqua, 64)
724 DFP_HELPER_QUA(dquaq, 128)
726 static void _dfp_reround(uint8_t rmc, int32_t ref_sig, int32_t xmax,
727 struct PPC_DFP *dfp)
729 int msd_orig, msd_rslt;
731 if (unlikely((ref_sig == 0) || (dfp->b.digits <= ref_sig))) {
732 dfp->t = dfp->b;
733 if (decNumberIsSNaN(&dfp->b)) {
734 dfp_makeQNaN(&dfp->t);
735 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FPSCR_VE);
737 return;
740 /* Reround is equivalent to quantizing b with 1**E(n) where */
741 /* n = exp(b) + numDigits(b) - reference_significance. */
743 decNumberFromUInt32(&dfp->a, 1);
744 dfp->a.exponent = dfp->b.exponent + dfp->b.digits - ref_sig;
746 if (unlikely(dfp->a.exponent > xmax)) {
747 dfp->t.digits = 0;
748 dfp->t.bits &= ~DECNEG;
749 dfp_makeQNaN(&dfp->t);
750 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FPSCR_VE);
751 return;
754 dfp_quantize(rmc, dfp);
756 msd_orig = dfp_get_digit(&dfp->b, dfp->b.digits-1);
757 msd_rslt = dfp_get_digit(&dfp->t, dfp->t.digits-1);
759 /* If the quantization resulted in rounding up to the next magnitude, */
760 /* then we need to shift the significand and adjust the exponent. */
762 if (unlikely((msd_orig == 9) && (msd_rslt == 1))) {
764 decNumber negone;
766 decNumberFromInt32(&negone, -1);
767 decNumberShift(&dfp->t, &dfp->t, &negone, &dfp->context);
768 dfp->t.exponent++;
770 if (unlikely(dfp->t.exponent > xmax)) {
771 dfp_makeQNaN(&dfp->t);
772 dfp->t.digits = 0;
773 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FP_VE);
774 /* Inhibit XX in this case */
775 decContextClearStatus(&dfp->context, DEC_Inexact);
780 #define DFP_HELPER_RRND(op, size) \
781 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
782 uint64_t *b, uint32_t rmc) \
784 struct PPC_DFP dfp; \
785 int32_t ref_sig = *a & 0x3F; \
786 int32_t xmax = ((size) == 64) ? 369 : 6111; \
788 dfp_prepare_decimal##size(&dfp, 0, b, env); \
790 _dfp_reround(rmc, ref_sig, xmax, &dfp); \
791 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
792 &dfp.context); \
793 QUA_PPs(&dfp); \
795 if (size == 64) { \
796 t[0] = dfp.t64[0]; \
797 } else if (size == 128) { \
798 t[0] = dfp.t64[HI_IDX]; \
799 t[1] = dfp.t64[LO_IDX]; \
803 DFP_HELPER_RRND(drrnd, 64)
804 DFP_HELPER_RRND(drrndq, 128)
806 #define DFP_HELPER_RINT(op, postprocs, size) \
807 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \
808 uint32_t r, uint32_t rmc) \
810 struct PPC_DFP dfp; \
812 dfp_prepare_decimal##size(&dfp, 0, b, env); \
814 dfp_set_round_mode_from_immediate(r, rmc, &dfp); \
815 decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \
816 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
817 postprocs(&dfp); \
819 if (size == 64) { \
820 t[0] = dfp.t64[0]; \
821 } else if (size == 128) { \
822 t[0] = dfp.t64[HI_IDX]; \
823 t[1] = dfp.t64[LO_IDX]; \
827 static void RINTX_PPs(struct PPC_DFP *dfp)
829 dfp_set_FPRF_from_FRT(dfp);
830 dfp_check_for_XX(dfp);
831 dfp_check_for_VXSNAN(dfp);
834 DFP_HELPER_RINT(drintx, RINTX_PPs, 64)
835 DFP_HELPER_RINT(drintxq, RINTX_PPs, 128)
837 static void RINTN_PPs(struct PPC_DFP *dfp)
839 dfp_set_FPRF_from_FRT(dfp);
840 dfp_check_for_VXSNAN(dfp);
843 DFP_HELPER_RINT(drintn, RINTN_PPs, 64)
844 DFP_HELPER_RINT(drintnq, RINTN_PPs, 128)
846 void helper_dctdp(CPUPPCState *env, uint64_t *t, uint64_t *b)
848 struct PPC_DFP dfp;
849 uint32_t b_short = *b;
850 dfp_prepare_decimal64(&dfp, 0, 0, env);
851 decimal32ToNumber((decimal32 *)&b_short, &dfp.t);
852 decimal64FromNumber((decimal64 *)t, &dfp.t, &dfp.context);
853 dfp_set_FPRF_from_FRT(&dfp);
856 void helper_dctqpq(CPUPPCState *env, uint64_t *t, uint64_t *b)
858 struct PPC_DFP dfp;
859 dfp_prepare_decimal128(&dfp, 0, 0, env);
860 decimal64ToNumber((decimal64 *)b, &dfp.t);
862 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp);
863 dfp_set_FPRF_from_FRT(&dfp);
865 decimal128FromNumber((decimal128 *)&dfp.t64, &dfp.t, &dfp.context);
866 t[0] = dfp.t64[HI_IDX];
867 t[1] = dfp.t64[LO_IDX];
870 void helper_drsp(CPUPPCState *env, uint64_t *t, uint64_t *b)
872 struct PPC_DFP dfp;
873 uint32_t t_short = 0;
874 dfp_prepare_decimal64(&dfp, 0, b, env);
875 decimal32FromNumber((decimal32 *)&t_short, &dfp.b, &dfp.context);
876 decimal32ToNumber((decimal32 *)&t_short, &dfp.t);
878 dfp_set_FPRF_from_FRT_short(&dfp);
879 dfp_check_for_OX(&dfp);
880 dfp_check_for_UX(&dfp);
881 dfp_check_for_XX(&dfp);
883 *t = t_short;
886 void helper_drdpq(CPUPPCState *env, uint64_t *t, uint64_t *b)
888 struct PPC_DFP dfp;
889 dfp_prepare_decimal128(&dfp, 0, b, env);
890 decimal64FromNumber((decimal64 *)&dfp.t64, &dfp.b, &dfp.context);
891 decimal64ToNumber((decimal64 *)&dfp.t64, &dfp.t);
893 dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp);
894 dfp_set_FPRF_from_FRT_long(&dfp);
895 dfp_check_for_OX(&dfp);
896 dfp_check_for_UX(&dfp);
897 dfp_check_for_XX(&dfp);
899 decimal64FromNumber((decimal64 *)dfp.t64, &dfp.t, &dfp.context);
900 t[0] = dfp.t64[0];
901 t[1] = 0;
904 #define DFP_HELPER_CFFIX(op, size) \
905 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
907 struct PPC_DFP dfp; \
908 dfp_prepare_decimal##size(&dfp, 0, b, env); \
909 decNumberFromInt64(&dfp.t, (int64_t)(*b)); \
910 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
911 CFFIX_PPs(&dfp); \
913 if (size == 64) { \
914 t[0] = dfp.t64[0]; \
915 } else if (size == 128) { \
916 t[0] = dfp.t64[HI_IDX]; \
917 t[1] = dfp.t64[LO_IDX]; \
921 static void CFFIX_PPs(struct PPC_DFP *dfp)
923 dfp_set_FPRF_from_FRT(dfp);
924 dfp_check_for_XX(dfp);
927 DFP_HELPER_CFFIX(dcffix, 64)
928 DFP_HELPER_CFFIX(dcffixq, 128)
930 #define DFP_HELPER_CTFIX(op, size) \
931 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
933 struct PPC_DFP dfp; \
934 dfp_prepare_decimal##size(&dfp, 0, b, env); \
936 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
937 uint64_t invalid_flags = FP_VX | FP_VXCVI; \
938 if (decNumberIsInfinite(&dfp.b)) { \
939 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
940 } else { /* NaN */ \
941 dfp.t64[0] = INT64_MIN; \
942 if (decNumberIsSNaN(&dfp.b)) { \
943 invalid_flags |= FP_VXSNAN; \
946 dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \
947 } else if (unlikely(decNumberIsZero(&dfp.b))) { \
948 dfp.t64[0] = 0; \
949 } else { \
950 decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \
951 dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context); \
952 if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \
953 dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
954 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \
955 } else { \
956 dfp_check_for_XX(&dfp); \
960 *t = dfp.t64[0]; \
963 DFP_HELPER_CTFIX(dctfix, 64)
964 DFP_HELPER_CTFIX(dctfixq, 128)
966 static inline void dfp_set_bcd_digit_64(uint64_t *t, uint8_t digit,
967 unsigned n)
969 *t |= ((uint64_t)(digit & 0xF) << (n << 2));
972 static inline void dfp_set_bcd_digit_128(uint64_t *t, uint8_t digit,
973 unsigned n)
975 t[(n & 0x10) ? HI_IDX : LO_IDX] |=
976 ((uint64_t)(digit & 0xF) << ((n & 15) << 2));
979 static inline void dfp_set_sign_64(uint64_t *t, uint8_t sgn)
981 *t <<= 4;
982 *t |= (sgn & 0xF);
985 static inline void dfp_set_sign_128(uint64_t *t, uint8_t sgn)
987 t[HI_IDX] <<= 4;
988 t[HI_IDX] |= (t[LO_IDX] >> 60);
989 t[LO_IDX] <<= 4;
990 t[LO_IDX] |= (sgn & 0xF);
993 #define DFP_HELPER_DEDPD(op, size) \
994 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t sp) \
996 struct PPC_DFP dfp; \
997 uint8_t digits[34]; \
998 int i, N; \
1000 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1002 decNumberGetBCD(&dfp.b, digits); \
1003 dfp.t64[0] = dfp.t64[1] = 0; \
1004 N = dfp.b.digits; \
1006 for (i = 0; (i < N) && (i < (size)/4); i++) { \
1007 dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i); \
1010 if (sp & 2) { \
1011 uint8_t sgn; \
1013 if (decNumberIsNegative(&dfp.b)) { \
1014 sgn = 0xD; \
1015 } else { \
1016 sgn = ((sp & 1) ? 0xF : 0xC); \
1018 dfp_set_sign_##size(dfp.t64, sgn); \
1021 if (size == 64) { \
1022 t[0] = dfp.t64[0]; \
1023 } else if (size == 128) { \
1024 t[0] = dfp.t64[HI_IDX]; \
1025 t[1] = dfp.t64[LO_IDX]; \
1029 DFP_HELPER_DEDPD(ddedpd, 64)
1030 DFP_HELPER_DEDPD(ddedpdq, 128)
1032 static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t, unsigned n)
1034 return *t >> ((n << 2) & 63) & 15;
1037 static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t, unsigned n)
1039 return t[(n & 0x10) ? HI_IDX : LO_IDX] >> ((n << 2) & 63) & 15;
1042 #define DFP_HELPER_ENBCD(op, size) \
1043 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t s) \
1045 struct PPC_DFP dfp; \
1046 uint8_t digits[32]; \
1047 int n = 0, offset = 0, sgn = 0, nonzero = 0; \
1049 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1051 decNumberZero(&dfp.t); \
1053 if (s) { \
1054 uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1055 switch (sgnNibble) { \
1056 case 0xD: \
1057 case 0xB: \
1058 sgn = 1; \
1059 break; \
1060 case 0xC: \
1061 case 0xF: \
1062 case 0xA: \
1063 case 0xE: \
1064 sgn = 0; \
1065 break; \
1066 default: \
1067 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1068 return; \
1072 while (offset < (size)/4) { \
1073 n++; \
1074 digits[(size)/4-n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1075 if (digits[(size)/4-n] > 10) { \
1076 dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \
1077 return; \
1078 } else { \
1079 nonzero |= (digits[(size)/4-n] > 0); \
1083 if (nonzero) { \
1084 decNumberSetBCD(&dfp.t, digits+((size)/4)-n, n); \
1087 if (s && sgn) { \
1088 dfp.t.bits |= DECNEG; \
1090 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1091 &dfp.context); \
1092 dfp_set_FPRF_from_FRT(&dfp); \
1093 if ((size) == 64) { \
1094 t[0] = dfp.t64[0]; \
1095 } else if ((size) == 128) { \
1096 t[0] = dfp.t64[HI_IDX]; \
1097 t[1] = dfp.t64[LO_IDX]; \
1101 DFP_HELPER_ENBCD(denbcd, 64)
1102 DFP_HELPER_ENBCD(denbcdq, 128)
1104 #define DFP_HELPER_XEX(op, size) \
1105 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \
1107 struct PPC_DFP dfp; \
1109 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1111 if (unlikely(decNumberIsSpecial(&dfp.b))) { \
1112 if (decNumberIsInfinite(&dfp.b)) { \
1113 *t = -1; \
1114 } else if (decNumberIsSNaN(&dfp.b)) { \
1115 *t = -3; \
1116 } else if (decNumberIsQNaN(&dfp.b)) { \
1117 *t = -2; \
1118 } else { \
1119 assert(0); \
1121 } else { \
1122 if ((size) == 64) { \
1123 *t = dfp.b.exponent + 398; \
1124 } else if ((size) == 128) { \
1125 *t = dfp.b.exponent + 6176; \
1126 } else { \
1127 assert(0); \
1132 DFP_HELPER_XEX(dxex, 64)
1133 DFP_HELPER_XEX(dxexq, 128)
1135 static void dfp_set_raw_exp_64(uint64_t *t, uint64_t raw)
1137 *t &= 0x8003ffffffffffffULL;
1138 *t |= (raw << (63-13));
1141 static void dfp_set_raw_exp_128(uint64_t *t, uint64_t raw)
1143 t[HI_IDX] &= 0x80003fffffffffffULL;
1144 t[HI_IDX] |= (raw << (63-17));
1147 #define DFP_HELPER_IEX(op, size) \
1148 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
1150 struct PPC_DFP dfp; \
1151 uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \
1152 int bias; \
1153 int64_t exp = *((int64_t *)a); \
1155 dfp_prepare_decimal##size(&dfp, 0, b, env); \
1157 if ((size) == 64) { \
1158 max_exp = 767; \
1159 raw_qnan = 0x1F00; \
1160 raw_snan = 0x1F80; \
1161 raw_inf = 0x1E00; \
1162 bias = 398; \
1163 } else if ((size) == 128) { \
1164 max_exp = 12287; \
1165 raw_qnan = 0x1f000; \
1166 raw_snan = 0x1f800; \
1167 raw_inf = 0x1e000; \
1168 bias = 6176; \
1169 } else { \
1170 assert(0); \
1173 if (unlikely((exp < 0) || (exp > max_exp))) { \
1174 dfp.t64[0] = dfp.b64[0]; \
1175 dfp.t64[1] = dfp.b64[1]; \
1176 if (exp == -1) { \
1177 dfp_set_raw_exp_##size(dfp.t64, raw_inf); \
1178 } else if (exp == -3) { \
1179 dfp_set_raw_exp_##size(dfp.t64, raw_snan); \
1180 } else { \
1181 dfp_set_raw_exp_##size(dfp.t64, raw_qnan); \
1183 } else { \
1184 dfp.t = dfp.b; \
1185 if (unlikely(decNumberIsSpecial(&dfp.t))) { \
1186 dfp.t.bits &= ~DECSPECIAL; \
1188 dfp.t.exponent = exp - bias; \
1189 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1190 &dfp.context); \
1192 if (size == 64) { \
1193 t[0] = dfp.t64[0]; \
1194 } else if (size == 128) { \
1195 t[0] = dfp.t64[HI_IDX]; \
1196 t[1] = dfp.t64[LO_IDX]; \
1200 DFP_HELPER_IEX(diex, 64)
1201 DFP_HELPER_IEX(diexq, 128)
1203 static void dfp_clear_lmd_from_g5msb(uint64_t *t)
1206 /* The most significant 5 bits of the PowerPC DFP format combine bits */
1207 /* from the left-most decimal digit (LMD) and the biased exponent. */
1208 /* This routine clears the LMD bits while preserving the exponent */
1209 /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */
1210 /* Finite Numbers" in the Power ISA for additional details. */
1212 uint64_t g5msb = (*t >> 58) & 0x1F;
1214 if ((g5msb >> 3) < 3) { /* LMD in [0-7] ? */
1215 *t &= ~(7ULL << 58);
1216 } else {
1217 switch (g5msb & 7) {
1218 case 0:
1219 case 1:
1220 g5msb = 0;
1221 break;
1222 case 2:
1223 case 3:
1224 g5msb = 0x8;
1225 break;
1226 case 4:
1227 case 5:
1228 g5msb = 0x10;
1229 break;
1230 case 6:
1231 g5msb = 0x1E;
1232 break;
1233 case 7:
1234 g5msb = 0x1F;
1235 break;
1238 *t &= ~(0x1fULL << 58);
1239 *t |= (g5msb << 58);
1243 #define DFP_HELPER_SHIFT(op, size, shift_left) \
1244 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \
1245 uint32_t sh) \
1247 struct PPC_DFP dfp; \
1248 unsigned max_digits = ((size) == 64) ? 16 : 34; \
1250 dfp_prepare_decimal##size(&dfp, a, 0, env); \
1252 if (sh <= max_digits) { \
1254 decNumber shd; \
1255 unsigned special = dfp.a.bits & DECSPECIAL; \
1257 if (shift_left) { \
1258 decNumberFromUInt32(&shd, sh); \
1259 } else { \
1260 decNumberFromInt32(&shd, -((int32_t)sh)); \
1263 dfp.a.bits &= ~DECSPECIAL; \
1264 decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \
1266 dfp.t.bits |= special; \
1267 if (special && (dfp.t.digits >= max_digits)) { \
1268 dfp.t.digits = max_digits - 1; \
1271 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \
1272 &dfp.context); \
1273 } else { \
1274 if ((size) == 64) { \
1275 dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL; \
1276 dfp_clear_lmd_from_g5msb(dfp.t64); \
1277 } else { \
1278 dfp.t64[HI_IDX] = dfp.a64[HI_IDX] & \
1279 0xFFFFC00000000000ULL; \
1280 dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX); \
1281 dfp.t64[LO_IDX] = 0; \
1285 if ((size) == 64) { \
1286 t[0] = dfp.t64[0]; \
1287 } else { \
1288 t[0] = dfp.t64[HI_IDX]; \
1289 t[1] = dfp.t64[LO_IDX]; \
1293 DFP_HELPER_SHIFT(dscli, 64, 1)
1294 DFP_HELPER_SHIFT(dscliq, 128, 1)
1295 DFP_HELPER_SHIFT(dscri, 64, 0)
1296 DFP_HELPER_SHIFT(dscriq, 128, 0)