xref: /qemu/target/ppc/dfp_helper.c (revision 474c2e931d51bad00c20c65d6fe478a9391e5e2a)
1 /*
2  *  PowerPC Decimal Floating Point (DPF) emulation helpers for QEMU.
3  *
4  *  Copyright (c) 2014 IBM Corporation.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 
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"
30 
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
38 
39 static void get_dfp64(uint64_t *dst, ppc_fprp_t *dfp)
40 {
41     dst[0] = dfp->VsrD(0);
42 }
43 
44 static void get_dfp128(uint64_t *dst, ppc_fprp_t *dfp)
45 {
46     dst[HI_IDX] = dfp[0].VsrD(0);
47     dst[LO_IDX] = dfp[1].VsrD(0);
48 }
49 
50 static void set_dfp64(ppc_fprp_t *dfp, uint64_t *src)
51 {
52     dfp->VsrD(0) = src[0];
53 }
54 
55 static void set_dfp128(ppc_fprp_t *dfp, uint64_t *src)
56 {
57     dfp[0].VsrD(0) = src[HI_IDX];
58     dfp[1].VsrD(0) = src[LO_IDX];
59 }
60 
61 struct PPC_DFP {
62     CPUPPCState *env;
63     uint64_t t64[2], a64[2], b64[2];
64     decNumber t, a, b;
65     decContext context;
66     uint8_t crbf;
67 };
68 
69 static void dfp_prepare_rounding_mode(decContext *context, uint64_t fpscr)
70 {
71     enum rounding rnd;
72 
73     switch ((fpscr & FP_DRN) >> FPSCR_DRN0) {
74     case 0:
75         rnd = DEC_ROUND_HALF_EVEN;
76         break;
77     case 1:
78         rnd = DEC_ROUND_DOWN;
79         break;
80     case 2:
81          rnd = DEC_ROUND_CEILING;
82          break;
83     case 3:
84          rnd = DEC_ROUND_FLOOR;
85          break;
86     case 4:
87          rnd = DEC_ROUND_HALF_UP;
88          break;
89     case 5:
90          rnd = DEC_ROUND_HALF_DOWN;
91          break;
92     case 6:
93          rnd = DEC_ROUND_UP;
94          break;
95     case 7:
96          rnd = DEC_ROUND_05UP;
97          break;
98     default:
99         g_assert_not_reached();
100     }
101 
102     decContextSetRounding(context, rnd);
103 }
104 
105 static void dfp_set_round_mode_from_immediate(uint8_t r, uint8_t rmc,
106                                                   struct PPC_DFP *dfp)
107 {
108     enum rounding rnd;
109     if (r == 0) {
110         switch (rmc & 3) {
111         case 0:
112             rnd = DEC_ROUND_HALF_EVEN;
113             break;
114         case 1:
115             rnd = DEC_ROUND_DOWN;
116             break;
117         case 2:
118             rnd = DEC_ROUND_HALF_UP;
119             break;
120         case 3: /* use FPSCR rounding mode */
121             return;
122         default:
123             assert(0); /* cannot get here */
124         }
125     } else { /* r == 1 */
126         switch (rmc & 3) {
127         case 0:
128             rnd = DEC_ROUND_CEILING;
129             break;
130         case 1:
131             rnd = DEC_ROUND_FLOOR;
132             break;
133         case 2:
134             rnd = DEC_ROUND_UP;
135             break;
136         case 3:
137             rnd = DEC_ROUND_HALF_DOWN;
138             break;
139         default:
140             assert(0); /* cannot get here */
141         }
142     }
143     decContextSetRounding(&dfp->context, rnd);
144 }
145 
146 static void dfp_prepare_decimal64(struct PPC_DFP *dfp, ppc_fprp_t *a,
147                                   ppc_fprp_t *b, CPUPPCState *env)
148 {
149     decContextDefault(&dfp->context, DEC_INIT_DECIMAL64);
150     dfp_prepare_rounding_mode(&dfp->context, env->fpscr);
151     dfp->env = env;
152 
153     if (a) {
154         get_dfp64(dfp->a64, a);
155         decimal64ToNumber((decimal64 *)dfp->a64, &dfp->a);
156     } else {
157         dfp->a64[0] = 0;
158         decNumberZero(&dfp->a);
159     }
160 
161     if (b) {
162         get_dfp64(dfp->b64, b);
163         decimal64ToNumber((decimal64 *)dfp->b64, &dfp->b);
164     } else {
165         dfp->b64[0] = 0;
166         decNumberZero(&dfp->b);
167     }
168 }
169 
170 static void dfp_prepare_decimal128(struct PPC_DFP *dfp, ppc_fprp_t *a,
171                                    ppc_fprp_t *b, CPUPPCState *env)
172 {
173     decContextDefault(&dfp->context, DEC_INIT_DECIMAL128);
174     dfp_prepare_rounding_mode(&dfp->context, env->fpscr);
175     dfp->env = env;
176 
177     if (a) {
178         get_dfp128(dfp->a64, a);
179         decimal128ToNumber((decimal128 *)dfp->a64, &dfp->a);
180     } else {
181         dfp->a64[0] = dfp->a64[1] = 0;
182         decNumberZero(&dfp->a);
183     }
184 
185     if (b) {
186         get_dfp128(dfp->b64, b);
187         decimal128ToNumber((decimal128 *)dfp->b64, &dfp->b);
188     } else {
189         dfp->b64[0] = dfp->b64[1] = 0;
190         decNumberZero(&dfp->b);
191     }
192 }
193 
194 static void dfp_finalize_decimal64(struct PPC_DFP *dfp)
195 {
196     decimal64FromNumber((decimal64 *)&dfp->t64, &dfp->t, &dfp->context);
197 }
198 
199 static void dfp_finalize_decimal128(struct PPC_DFP *dfp)
200 {
201     decimal128FromNumber((decimal128 *)&dfp->t64, &dfp->t, &dfp->context);
202 }
203 
204 static void dfp_set_FPSCR_flag(struct PPC_DFP *dfp, uint64_t flag,
205                 uint64_t enabled)
206 {
207     dfp->env->fpscr |= (flag | FP_FX);
208     if (dfp->env->fpscr & enabled) {
209         dfp->env->fpscr |= FP_FEX;
210     }
211 }
212 
213 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP *dfp,
214                 decContext *context)
215 {
216     uint64_t fprf = 0;
217 
218     /* construct FPRF */
219     switch (decNumberClass(&dfp->t, context)) {
220     case DEC_CLASS_SNAN:
221         fprf = 0x01;
222         break;
223     case DEC_CLASS_QNAN:
224         fprf = 0x11;
225         break;
226     case DEC_CLASS_NEG_INF:
227         fprf = 0x09;
228         break;
229     case DEC_CLASS_NEG_NORMAL:
230         fprf = 0x08;
231         break;
232     case DEC_CLASS_NEG_SUBNORMAL:
233         fprf = 0x18;
234         break;
235     case DEC_CLASS_NEG_ZERO:
236         fprf = 0x12;
237         break;
238     case DEC_CLASS_POS_ZERO:
239         fprf = 0x02;
240         break;
241     case DEC_CLASS_POS_SUBNORMAL:
242         fprf = 0x14;
243         break;
244     case DEC_CLASS_POS_NORMAL:
245         fprf = 0x04;
246         break;
247     case DEC_CLASS_POS_INF:
248         fprf = 0x05;
249         break;
250     default:
251         assert(0); /* should never get here */
252     }
253     dfp->env->fpscr &= ~FP_FPRF;
254     dfp->env->fpscr |= (fprf << FPSCR_FPRF);
255 }
256 
257 static void dfp_set_FPRF_from_FRT(struct PPC_DFP *dfp)
258 {
259     dfp_set_FPRF_from_FRT_with_context(dfp, &dfp->context);
260 }
261 
262 static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP *dfp)
263 {
264     decContext shortContext;
265     decContextDefault(&shortContext, DEC_INIT_DECIMAL32);
266     dfp_set_FPRF_from_FRT_with_context(dfp, &shortContext);
267 }
268 
269 static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP *dfp)
270 {
271     decContext longContext;
272     decContextDefault(&longContext, DEC_INIT_DECIMAL64);
273     dfp_set_FPRF_from_FRT_with_context(dfp, &longContext);
274 }
275 
276 static void dfp_check_for_OX(struct PPC_DFP *dfp)
277 {
278     if (dfp->context.status & DEC_Overflow) {
279         dfp_set_FPSCR_flag(dfp, FP_OX, FP_OE);
280     }
281 }
282 
283 static void dfp_check_for_UX(struct PPC_DFP *dfp)
284 {
285     if (dfp->context.status & DEC_Underflow) {
286         dfp_set_FPSCR_flag(dfp, FP_UX, FP_UE);
287     }
288 }
289 
290 static void dfp_check_for_XX(struct PPC_DFP *dfp)
291 {
292     if (dfp->context.status & DEC_Inexact) {
293         dfp_set_FPSCR_flag(dfp, FP_XX | FP_FI, FP_XE);
294     }
295 }
296 
297 static void dfp_check_for_ZX(struct PPC_DFP *dfp)
298 {
299     if (dfp->context.status & DEC_Division_by_zero) {
300         dfp_set_FPSCR_flag(dfp, FP_ZX, FP_ZE);
301     }
302 }
303 
304 static void dfp_check_for_VXSNAN(struct PPC_DFP *dfp)
305 {
306     if (dfp->context.status & DEC_Invalid_operation) {
307         if (decNumberIsSNaN(&dfp->a) || decNumberIsSNaN(&dfp->b)) {
308             dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FP_VE);
309         }
310     }
311 }
312 
313 static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP *dfp)
314 {
315     if (decNumberIsSNaN(&dfp->t)) {
316         dfp->t.bits &= ~DECSNAN;
317         dfp->t.bits |= DECNAN;
318         dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FP_VE);
319     }
320 }
321 
322 static void dfp_check_for_VXISI(struct PPC_DFP *dfp, int testForSameSign)
323 {
324     if (dfp->context.status & DEC_Invalid_operation) {
325         if (decNumberIsInfinite(&dfp->a) && decNumberIsInfinite(&dfp->b)) {
326             int same = decNumberClass(&dfp->a, &dfp->context) ==
327                        decNumberClass(&dfp->b, &dfp->context);
328             if ((same && testForSameSign) || (!same && !testForSameSign)) {
329                 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXISI, FP_VE);
330             }
331         }
332     }
333 }
334 
335 static void dfp_check_for_VXISI_add(struct PPC_DFP *dfp)
336 {
337     dfp_check_for_VXISI(dfp, 0);
338 }
339 
340 static void dfp_check_for_VXISI_subtract(struct PPC_DFP *dfp)
341 {
342     dfp_check_for_VXISI(dfp, 1);
343 }
344 
345 static void dfp_check_for_VXIMZ(struct PPC_DFP *dfp)
346 {
347     if (dfp->context.status & DEC_Invalid_operation) {
348         if ((decNumberIsInfinite(&dfp->a) && decNumberIsZero(&dfp->b)) ||
349             (decNumberIsInfinite(&dfp->b) && decNumberIsZero(&dfp->a))) {
350             dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXIMZ, FP_VE);
351         }
352     }
353 }
354 
355 static void dfp_check_for_VXZDZ(struct PPC_DFP *dfp)
356 {
357     if (dfp->context.status & DEC_Division_undefined) {
358         dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXZDZ, FP_VE);
359     }
360 }
361 
362 static void dfp_check_for_VXIDI(struct PPC_DFP *dfp)
363 {
364     if (dfp->context.status & DEC_Invalid_operation) {
365         if (decNumberIsInfinite(&dfp->a) && decNumberIsInfinite(&dfp->b)) {
366             dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXIDI, FP_VE);
367         }
368     }
369 }
370 
371 static void dfp_check_for_VXVC(struct PPC_DFP *dfp)
372 {
373     if (decNumberIsNaN(&dfp->a) || decNumberIsNaN(&dfp->b)) {
374         dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXVC, FP_VE);
375     }
376 }
377 
378 static void dfp_check_for_VXCVI(struct PPC_DFP *dfp)
379 {
380     if ((dfp->context.status & DEC_Invalid_operation) &&
381         (!decNumberIsSNaN(&dfp->a)) &&
382         (!decNumberIsSNaN(&dfp->b))) {
383         dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FP_VE);
384     }
385 }
386 
387 static void dfp_set_CRBF_from_T(struct PPC_DFP *dfp)
388 {
389     if (decNumberIsNaN(&dfp->t)) {
390         dfp->crbf = 1;
391     } else if (decNumberIsZero(&dfp->t)) {
392         dfp->crbf = 2;
393     } else if (decNumberIsNegative(&dfp->t)) {
394         dfp->crbf = 8;
395     } else {
396         dfp->crbf = 4;
397     }
398 }
399 
400 static void dfp_set_FPCC_from_CRBF(struct PPC_DFP *dfp)
401 {
402     dfp->env->fpscr &= ~FP_FPCC;
403     dfp->env->fpscr |= (dfp->crbf << FPSCR_FPCC);
404 }
405 
406 static inline void dfp_makeQNaN(decNumber *dn)
407 {
408     dn->bits &= ~DECSPECIAL;
409     dn->bits |= DECNAN;
410 }
411 
412 static inline int dfp_get_digit(decNumber *dn, int n)
413 {
414     assert(DECDPUN == 3);
415     int unit = n / DECDPUN;
416     int dig = n % DECDPUN;
417     switch (dig) {
418     case 0:
419         return dn->lsu[unit] % 10;
420     case 1:
421         return (dn->lsu[unit] / 10) % 10;
422     case 2:
423         return dn->lsu[unit] / 100;
424     }
425     g_assert_not_reached();
426 }
427 
428 #define DFP_HELPER_TAB(op, dnop, postprocs, size)                              \
429 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a,               \
430                  ppc_fprp_t *b)                                                \
431 {                                                                              \
432     struct PPC_DFP dfp;                                                        \
433     dfp_prepare_decimal##size(&dfp, a, b, env);                                \
434     dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context);                                \
435     dfp_finalize_decimal##size(&dfp);                                          \
436     postprocs(&dfp);                                                           \
437     if (size == 64) {                                                          \
438         set_dfp64(t, dfp.t64);                                                 \
439     } else if (size == 128) {                                                  \
440         set_dfp128(t, dfp.t64);                                                \
441     }                                                                          \
442 }
443 
444 static void ADD_PPs(struct PPC_DFP *dfp)
445 {
446     dfp_set_FPRF_from_FRT(dfp);
447     dfp_check_for_OX(dfp);
448     dfp_check_for_UX(dfp);
449     dfp_check_for_XX(dfp);
450     dfp_check_for_VXSNAN(dfp);
451     dfp_check_for_VXISI_add(dfp);
452 }
453 
454 DFP_HELPER_TAB(dadd, decNumberAdd, ADD_PPs, 64)
455 DFP_HELPER_TAB(daddq, decNumberAdd, ADD_PPs, 128)
456 
457 static void SUB_PPs(struct PPC_DFP *dfp)
458 {
459     dfp_set_FPRF_from_FRT(dfp);
460     dfp_check_for_OX(dfp);
461     dfp_check_for_UX(dfp);
462     dfp_check_for_XX(dfp);
463     dfp_check_for_VXSNAN(dfp);
464     dfp_check_for_VXISI_subtract(dfp);
465 }
466 
467 DFP_HELPER_TAB(dsub, decNumberSubtract, SUB_PPs, 64)
468 DFP_HELPER_TAB(dsubq, decNumberSubtract, SUB_PPs, 128)
469 
470 static void MUL_PPs(struct PPC_DFP *dfp)
471 {
472     dfp_set_FPRF_from_FRT(dfp);
473     dfp_check_for_OX(dfp);
474     dfp_check_for_UX(dfp);
475     dfp_check_for_XX(dfp);
476     dfp_check_for_VXSNAN(dfp);
477     dfp_check_for_VXIMZ(dfp);
478 }
479 
480 DFP_HELPER_TAB(dmul, decNumberMultiply, MUL_PPs, 64)
481 DFP_HELPER_TAB(dmulq, decNumberMultiply, MUL_PPs, 128)
482 
483 static void DIV_PPs(struct PPC_DFP *dfp)
484 {
485     dfp_set_FPRF_from_FRT(dfp);
486     dfp_check_for_OX(dfp);
487     dfp_check_for_UX(dfp);
488     dfp_check_for_ZX(dfp);
489     dfp_check_for_XX(dfp);
490     dfp_check_for_VXSNAN(dfp);
491     dfp_check_for_VXZDZ(dfp);
492     dfp_check_for_VXIDI(dfp);
493 }
494 
495 DFP_HELPER_TAB(ddiv, decNumberDivide, DIV_PPs, 64)
496 DFP_HELPER_TAB(ddivq, decNumberDivide, DIV_PPs, 128)
497 
498 #define DFP_HELPER_BF_AB(op, dnop, postprocs, size)                            \
499 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b)           \
500 {                                                                              \
501     struct PPC_DFP dfp;                                                        \
502     dfp_prepare_decimal##size(&dfp, a, b, env);                                \
503     dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context);                                \
504     dfp_finalize_decimal##size(&dfp);                                          \
505     postprocs(&dfp);                                                           \
506     return dfp.crbf;                                                           \
507 }
508 
509 static void CMPU_PPs(struct PPC_DFP *dfp)
510 {
511     dfp_set_CRBF_from_T(dfp);
512     dfp_set_FPCC_from_CRBF(dfp);
513     dfp_check_for_VXSNAN(dfp);
514 }
515 
516 DFP_HELPER_BF_AB(dcmpu, decNumberCompare, CMPU_PPs, 64)
517 DFP_HELPER_BF_AB(dcmpuq, decNumberCompare, CMPU_PPs, 128)
518 
519 static void CMPO_PPs(struct PPC_DFP *dfp)
520 {
521     dfp_set_CRBF_from_T(dfp);
522     dfp_set_FPCC_from_CRBF(dfp);
523     dfp_check_for_VXSNAN(dfp);
524     dfp_check_for_VXVC(dfp);
525 }
526 
527 DFP_HELPER_BF_AB(dcmpo, decNumberCompare, CMPO_PPs, 64)
528 DFP_HELPER_BF_AB(dcmpoq, decNumberCompare, CMPO_PPs, 128)
529 
530 #define DFP_HELPER_TSTDC(op, size)                                       \
531 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm)      \
532 {                                                                        \
533     struct PPC_DFP dfp;                                                  \
534     int match = 0;                                                       \
535                                                                          \
536     dfp_prepare_decimal##size(&dfp, a, 0, env);                          \
537                                                                          \
538     match |= (dcm & 0x20) && decNumberIsZero(&dfp.a);                    \
539     match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \
540     match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context);    \
541     match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a);                \
542     match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a);                    \
543     match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a);                    \
544                                                                          \
545     if (decNumberIsNegative(&dfp.a)) {                                   \
546         dfp.crbf = match ? 0xA : 0x8;                                    \
547     } else {                                                             \
548         dfp.crbf = match ? 0x2 : 0x0;                                    \
549     }                                                                    \
550                                                                          \
551     dfp_set_FPCC_from_CRBF(&dfp);                                        \
552     return dfp.crbf;                                                     \
553 }
554 
555 DFP_HELPER_TSTDC(dtstdc, 64)
556 DFP_HELPER_TSTDC(dtstdcq, 128)
557 
558 #define DFP_HELPER_TSTDG(op, size)                                       \
559 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, uint32_t dcm)      \
560 {                                                                        \
561     struct PPC_DFP dfp;                                                  \
562     int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero,   \
563         is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero,    \
564         match;                                                           \
565                                                                          \
566     dfp_prepare_decimal##size(&dfp, a, 0, env);                          \
567                                                                          \
568     if ((size) == 64) {                                                  \
569         minexp = -398;                                                   \
570         maxexp = 369;                                                    \
571         nzero_digits = 16;                                               \
572         nzero_idx = 5;                                                   \
573     } else if ((size) == 128) {                                          \
574         minexp = -6176;                                                  \
575         maxexp = 6111;                                                   \
576         nzero_digits = 34;                                               \
577         nzero_idx = 11;                                                  \
578     }                                                                    \
579                                                                          \
580     is_negative = decNumberIsNegative(&dfp.a);                           \
581     is_zero = decNumberIsZero(&dfp.a);                                   \
582     is_extreme_exp = (dfp.a.exponent == maxexp) ||                       \
583                      (dfp.a.exponent == minexp);                         \
584     is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context);           \
585     is_normal = decNumberIsNormal(&dfp.a, &dfp.context);                 \
586     leftmost_is_nonzero = (dfp.a.digits == nzero_digits) &&              \
587                           (dfp.a.lsu[nzero_idx] != 0);                   \
588     match = 0;                                                           \
589                                                                          \
590     match |= (dcm & 0x20) && is_zero && !is_extreme_exp;                 \
591     match |= (dcm & 0x10) && is_zero && is_extreme_exp;                  \
592     match |= (dcm & 0x08) &&                                             \
593              (is_subnormal || (is_normal && is_extreme_exp));            \
594     match |= (dcm & 0x04) && is_normal && !is_extreme_exp &&             \
595              !leftmost_is_nonzero;                                       \
596     match |= (dcm & 0x02) && is_normal && !is_extreme_exp &&             \
597              leftmost_is_nonzero;                                        \
598     match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a);                 \
599                                                                          \
600     if (is_negative) {                                                   \
601         dfp.crbf = match ? 0xA : 0x8;                                    \
602     } else {                                                             \
603         dfp.crbf = match ? 0x2 : 0x0;                                    \
604     }                                                                    \
605                                                                          \
606     dfp_set_FPCC_from_CRBF(&dfp);                                        \
607     return dfp.crbf;                                                     \
608 }
609 
610 DFP_HELPER_TSTDG(dtstdg, 64)
611 DFP_HELPER_TSTDG(dtstdgq, 128)
612 
613 #define DFP_HELPER_TSTEX(op, size)                                       \
614 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b)     \
615 {                                                                        \
616     struct PPC_DFP dfp;                                                  \
617     int expa, expb, a_is_special, b_is_special;                          \
618                                                                          \
619     dfp_prepare_decimal##size(&dfp, a, b, env);                          \
620                                                                          \
621     expa = dfp.a.exponent;                                               \
622     expb = dfp.b.exponent;                                               \
623     a_is_special = decNumberIsSpecial(&dfp.a);                           \
624     b_is_special = decNumberIsSpecial(&dfp.b);                           \
625                                                                          \
626     if (a_is_special || b_is_special) {                                  \
627         int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \
628         int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \
629         dfp.crbf = (atype ^ btype) ? 0x1 : 0x2;                          \
630     } else if (expa < expb) {                                            \
631         dfp.crbf = 0x8;                                                  \
632     } else if (expa > expb) {                                            \
633         dfp.crbf = 0x4;                                                  \
634     } else {                                                             \
635         dfp.crbf = 0x2;                                                  \
636     }                                                                    \
637                                                                          \
638     dfp_set_FPCC_from_CRBF(&dfp);                                        \
639     return dfp.crbf;                                                     \
640 }
641 
642 DFP_HELPER_TSTEX(dtstex, 64)
643 DFP_HELPER_TSTEX(dtstexq, 128)
644 
645 #define DFP_HELPER_TSTSF(op, size)                                       \
646 uint32_t helper_##op(CPUPPCState *env, ppc_fprp_t *a, ppc_fprp_t *b)     \
647 {                                                                        \
648     struct PPC_DFP dfp;                                                  \
649     unsigned k;                                                          \
650     uint64_t a64;                                                        \
651                                                                          \
652     dfp_prepare_decimal##size(&dfp, 0, b, env);                          \
653                                                                          \
654     get_dfp64(&a64, a);                                                  \
655     k = a64 & 0x3F;                                                      \
656                                                                          \
657     if (unlikely(decNumberIsSpecial(&dfp.b))) {                          \
658         dfp.crbf = 1;                                                    \
659     } else if (k == 0) {                                                 \
660         dfp.crbf = 4;                                                    \
661     } else if (unlikely(decNumberIsZero(&dfp.b))) {                      \
662         /* Zero has no sig digits */                                     \
663         dfp.crbf = 4;                                                    \
664     } else {                                                             \
665         unsigned nsd = dfp.b.digits;                                     \
666         if (k < nsd) {                                                   \
667             dfp.crbf = 8;                                                \
668         } else if (k > nsd) {                                            \
669             dfp.crbf = 4;                                                \
670         } else {                                                         \
671             dfp.crbf = 2;                                                \
672         }                                                                \
673     }                                                                    \
674                                                                          \
675     dfp_set_FPCC_from_CRBF(&dfp);                                        \
676     return dfp.crbf;                                                     \
677 }
678 
679 DFP_HELPER_TSTSF(dtstsf, 64)
680 DFP_HELPER_TSTSF(dtstsfq, 128)
681 
682 #define DFP_HELPER_TSTSFI(op, size)                                     \
683 uint32_t helper_##op(CPUPPCState *env, uint32_t a, ppc_fprp_t *b)       \
684 {                                                                       \
685     struct PPC_DFP dfp;                                                 \
686     unsigned uim;                                                       \
687                                                                         \
688     dfp_prepare_decimal##size(&dfp, 0, b, env);                         \
689                                                                         \
690     uim = a & 0x3F;                                                     \
691                                                                         \
692     if (unlikely(decNumberIsSpecial(&dfp.b))) {                         \
693         dfp.crbf = 1;                                                   \
694     } else if (uim == 0) {                                              \
695         dfp.crbf = 4;                                                   \
696     } else if (unlikely(decNumberIsZero(&dfp.b))) {                     \
697         /* Zero has no sig digits */                                    \
698         dfp.crbf = 4;                                                   \
699     } else {                                                            \
700         unsigned nsd = dfp.b.digits;                                    \
701         if (uim < nsd) {                                                \
702             dfp.crbf = 8;                                               \
703         } else if (uim > nsd) {                                         \
704             dfp.crbf = 4;                                               \
705         } else {                                                        \
706             dfp.crbf = 2;                                               \
707         }                                                               \
708     }                                                                   \
709                                                                         \
710     dfp_set_FPCC_from_CRBF(&dfp);                                       \
711     return dfp.crbf;                                                    \
712 }
713 
714 DFP_HELPER_TSTSFI(dtstsfi, 64)
715 DFP_HELPER_TSTSFI(dtstsfiq, 128)
716 
717 static void QUA_PPs(struct PPC_DFP *dfp)
718 {
719     dfp_set_FPRF_from_FRT(dfp);
720     dfp_check_for_XX(dfp);
721     dfp_check_for_VXSNAN(dfp);
722     dfp_check_for_VXCVI(dfp);
723 }
724 
725 static void dfp_quantize(uint8_t rmc, struct PPC_DFP *dfp)
726 {
727     dfp_set_round_mode_from_immediate(0, rmc, dfp);
728     decNumberQuantize(&dfp->t, &dfp->b, &dfp->a, &dfp->context);
729     if (decNumberIsSNaN(&dfp->a)) {
730         dfp->t = dfp->a;
731         dfp_makeQNaN(&dfp->t);
732     } else if (decNumberIsSNaN(&dfp->b)) {
733         dfp->t = dfp->b;
734         dfp_makeQNaN(&dfp->t);
735     } else if (decNumberIsQNaN(&dfp->a)) {
736         dfp->t = dfp->a;
737     } else if (decNumberIsQNaN(&dfp->b)) {
738         dfp->t = dfp->b;
739     }
740 }
741 
742 #define DFP_HELPER_QUAI(op, size)                                       \
743 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b,        \
744                  uint32_t te, uint32_t rmc)                             \
745 {                                                                       \
746     struct PPC_DFP dfp;                                                 \
747                                                                         \
748     dfp_prepare_decimal##size(&dfp, 0, b, env);                         \
749                                                                         \
750     decNumberFromUInt32(&dfp.a, 1);                                     \
751     dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3);                 \
752                                                                         \
753     dfp_quantize(rmc, &dfp);                                            \
754     dfp_finalize_decimal##size(&dfp);                                   \
755     QUA_PPs(&dfp);                                                      \
756                                                                         \
757     if (size == 64) {                                                   \
758         set_dfp64(t, dfp.t64);                                          \
759     } else if (size == 128) {                                           \
760         set_dfp128(t, dfp.t64);                                         \
761     }                                                                   \
762 }
763 
764 DFP_HELPER_QUAI(dquai, 64)
765 DFP_HELPER_QUAI(dquaiq, 128)
766 
767 #define DFP_HELPER_QUA(op, size)                                        \
768 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a,        \
769                  ppc_fprp_t *b, uint32_t rmc)                           \
770 {                                                                       \
771     struct PPC_DFP dfp;                                                 \
772                                                                         \
773     dfp_prepare_decimal##size(&dfp, a, b, env);                         \
774                                                                         \
775     dfp_quantize(rmc, &dfp);                                            \
776     dfp_finalize_decimal##size(&dfp);                                   \
777     QUA_PPs(&dfp);                                                      \
778                                                                         \
779     if (size == 64) {                                                   \
780         set_dfp64(t, dfp.t64);                                          \
781     } else if (size == 128) {                                           \
782         set_dfp128(t, dfp.t64);                                         \
783     }                                                                   \
784 }
785 
786 DFP_HELPER_QUA(dqua, 64)
787 DFP_HELPER_QUA(dquaq, 128)
788 
789 static void _dfp_reround(uint8_t rmc, int32_t ref_sig, int32_t xmax,
790                              struct PPC_DFP *dfp)
791 {
792     int msd_orig, msd_rslt;
793 
794     if (unlikely((ref_sig == 0) || (dfp->b.digits <= ref_sig))) {
795         dfp->t = dfp->b;
796         if (decNumberIsSNaN(&dfp->b)) {
797             dfp_makeQNaN(&dfp->t);
798             dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FPSCR_VE);
799         }
800         return;
801     }
802 
803     /* Reround is equivalent to quantizing b with 1**E(n) where */
804     /* n = exp(b) + numDigits(b) - reference_significance.      */
805 
806     decNumberFromUInt32(&dfp->a, 1);
807     dfp->a.exponent = dfp->b.exponent + dfp->b.digits - ref_sig;
808 
809     if (unlikely(dfp->a.exponent > xmax)) {
810         dfp->t.digits = 0;
811         dfp->t.bits &= ~DECNEG;
812         dfp_makeQNaN(&dfp->t);
813         dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FPSCR_VE);
814         return;
815     }
816 
817     dfp_quantize(rmc, dfp);
818 
819     msd_orig = dfp_get_digit(&dfp->b, dfp->b.digits-1);
820     msd_rslt = dfp_get_digit(&dfp->t, dfp->t.digits-1);
821 
822     /* If the quantization resulted in rounding up to the next magnitude, */
823     /* then we need to shift the significand and adjust the exponent.     */
824 
825     if (unlikely((msd_orig == 9) && (msd_rslt == 1))) {
826 
827         decNumber negone;
828 
829         decNumberFromInt32(&negone, -1);
830         decNumberShift(&dfp->t, &dfp->t, &negone, &dfp->context);
831         dfp->t.exponent++;
832 
833         if (unlikely(dfp->t.exponent > xmax)) {
834             dfp_makeQNaN(&dfp->t);
835             dfp->t.digits = 0;
836             dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FP_VE);
837             /* Inhibit XX in this case */
838             decContextClearStatus(&dfp->context, DEC_Inexact);
839         }
840     }
841 }
842 
843 #define DFP_HELPER_RRND(op, size)                                       \
844 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a,        \
845                  ppc_fprp_t *b, uint32_t rmc)                           \
846 {                                                                       \
847     struct PPC_DFP dfp;                                                 \
848     uint64_t a64;                                                       \
849     int32_t ref_sig;                                                    \
850     int32_t xmax = ((size) == 64) ? 369 : 6111;                         \
851                                                                         \
852     dfp_prepare_decimal##size(&dfp, 0, b, env);                         \
853                                                                         \
854     get_dfp64(&a64, a);                                                 \
855     ref_sig = a64 & 0x3f;                                               \
856                                                                         \
857     _dfp_reround(rmc, ref_sig, xmax, &dfp);                             \
858     dfp_finalize_decimal##size(&dfp);                                   \
859     QUA_PPs(&dfp);                                                      \
860                                                                         \
861     if (size == 64) {                                                   \
862         set_dfp64(t, dfp.t64);                                          \
863     } else if (size == 128) {                                           \
864         set_dfp128(t, dfp.t64);                                         \
865     }                                                                   \
866 }
867 
868 DFP_HELPER_RRND(drrnd, 64)
869 DFP_HELPER_RRND(drrndq, 128)
870 
871 #define DFP_HELPER_RINT(op, postprocs, size)                                   \
872 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b,               \
873              uint32_t r, uint32_t rmc)                                         \
874 {                                                                              \
875     struct PPC_DFP dfp;                                                        \
876                                                                                \
877     dfp_prepare_decimal##size(&dfp, 0, b, env);                                \
878                                                                                \
879     dfp_set_round_mode_from_immediate(r, rmc, &dfp);                           \
880     decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context);                    \
881     dfp_finalize_decimal##size(&dfp);                                          \
882     postprocs(&dfp);                                                           \
883                                                                                \
884     if (size == 64) {                                                          \
885         set_dfp64(t, dfp.t64);                                                 \
886     } else if (size == 128) {                                                  \
887         set_dfp128(t, dfp.t64);                                                \
888     }                                                                          \
889 }
890 
891 static void RINTX_PPs(struct PPC_DFP *dfp)
892 {
893     dfp_set_FPRF_from_FRT(dfp);
894     dfp_check_for_XX(dfp);
895     dfp_check_for_VXSNAN(dfp);
896 }
897 
898 DFP_HELPER_RINT(drintx, RINTX_PPs, 64)
899 DFP_HELPER_RINT(drintxq, RINTX_PPs, 128)
900 
901 static void RINTN_PPs(struct PPC_DFP *dfp)
902 {
903     dfp_set_FPRF_from_FRT(dfp);
904     dfp_check_for_VXSNAN(dfp);
905 }
906 
907 DFP_HELPER_RINT(drintn, RINTN_PPs, 64)
908 DFP_HELPER_RINT(drintnq, RINTN_PPs, 128)
909 
910 void helper_dctdp(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)
911 {
912     struct PPC_DFP dfp;
913     uint64_t b64;
914     uint32_t b_short;
915 
916     get_dfp64(&b64, b);
917     b_short = (uint32_t)b64;
918 
919     dfp_prepare_decimal64(&dfp, 0, 0, env);
920     decimal32ToNumber((decimal32 *)&b_short, &dfp.t);
921     dfp_finalize_decimal64(&dfp);
922     set_dfp64(t, dfp.t64);
923     dfp_set_FPRF_from_FRT(&dfp);
924 }
925 
926 void helper_dctqpq(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)
927 {
928     struct PPC_DFP dfp;
929     uint64_t b64;
930     dfp_prepare_decimal128(&dfp, 0, 0, env);
931     get_dfp64(&b64, b);
932     decimal64ToNumber((decimal64 *)&b64, &dfp.t);
933 
934     dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp);
935     dfp_set_FPRF_from_FRT(&dfp);
936 
937     dfp_finalize_decimal128(&dfp);
938     set_dfp128(t, dfp.t64);
939 }
940 
941 void helper_drsp(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)
942 {
943     struct PPC_DFP dfp;
944     uint32_t t_short = 0;
945     uint64_t t64;
946     dfp_prepare_decimal64(&dfp, 0, b, env);
947     decimal32FromNumber((decimal32 *)&t_short, &dfp.b, &dfp.context);
948     decimal32ToNumber((decimal32 *)&t_short, &dfp.t);
949 
950     dfp_set_FPRF_from_FRT_short(&dfp);
951     dfp_check_for_OX(&dfp);
952     dfp_check_for_UX(&dfp);
953     dfp_check_for_XX(&dfp);
954 
955     t64 = (uint64_t)t_short;
956     set_dfp64(t, &t64);
957 }
958 
959 void helper_drdpq(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)
960 {
961     struct PPC_DFP dfp;
962     dfp_prepare_decimal128(&dfp, 0, b, env);
963     decimal64FromNumber((decimal64 *)&dfp.t64, &dfp.b, &dfp.context);
964     decimal64ToNumber((decimal64 *)&dfp.t64, &dfp.t);
965 
966     dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp);
967     dfp_set_FPRF_from_FRT_long(&dfp);
968     dfp_check_for_OX(&dfp);
969     dfp_check_for_UX(&dfp);
970     dfp_check_for_XX(&dfp);
971 
972     dfp.t64[0] = dfp.t64[1] = 0;
973     dfp_finalize_decimal64(&dfp);
974     set_dfp128(t, dfp.t64);
975 }
976 
977 #define DFP_HELPER_CFFIX(op, size)                                             \
978 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)               \
979 {                                                                              \
980     struct PPC_DFP dfp;                                                        \
981     uint64_t b64;                                                              \
982     dfp_prepare_decimal##size(&dfp, 0, b, env);                                \
983     get_dfp64(&b64, b);                                                        \
984     decNumberFromInt64(&dfp.t, (int64_t)b64);                                  \
985     dfp_finalize_decimal##size(&dfp);                                          \
986     CFFIX_PPs(&dfp);                                                           \
987                                                                                \
988     if (size == 64) {                                                          \
989         set_dfp64(t, dfp.t64);                                                 \
990     } else if (size == 128) {                                                  \
991         set_dfp128(t, dfp.t64);                                                \
992     }                                                                          \
993 }
994 
995 static void CFFIX_PPs(struct PPC_DFP *dfp)
996 {
997     dfp_set_FPRF_from_FRT(dfp);
998     dfp_check_for_XX(dfp);
999 }
1000 
1001 DFP_HELPER_CFFIX(dcffix, 64)
1002 DFP_HELPER_CFFIX(dcffixq, 128)
1003 
1004 #define DFP_HELPER_CTFIX(op, size)                                            \
1005 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b)              \
1006 {                                                                             \
1007     struct PPC_DFP dfp;                                                       \
1008     dfp_prepare_decimal##size(&dfp, 0, b, env);                               \
1009                                                                               \
1010     if (unlikely(decNumberIsSpecial(&dfp.b))) {                               \
1011         uint64_t invalid_flags = FP_VX | FP_VXCVI;                            \
1012         if (decNumberIsInfinite(&dfp.b)) {                                    \
1013             dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
1014         } else { /* NaN */                                                    \
1015             dfp.t64[0] = INT64_MIN;                                           \
1016             if (decNumberIsSNaN(&dfp.b)) {                                    \
1017                 invalid_flags |= FP_VXSNAN;                                   \
1018             }                                                                 \
1019         }                                                                     \
1020         dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE);                       \
1021     } else if (unlikely(decNumberIsZero(&dfp.b))) {                           \
1022         dfp.t64[0] = 0;                                                       \
1023     } else {                                                                  \
1024         decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context);               \
1025         dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context);          \
1026         if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) {      \
1027             dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \
1028             dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE);                \
1029         } else {                                                              \
1030             dfp_check_for_XX(&dfp);                                           \
1031         }                                                                     \
1032     }                                                                         \
1033                                                                               \
1034     set_dfp64(t, dfp.t64);                                                    \
1035 }
1036 
1037 DFP_HELPER_CTFIX(dctfix, 64)
1038 DFP_HELPER_CTFIX(dctfixq, 128)
1039 
1040 static inline void dfp_set_bcd_digit_64(uint64_t *t, uint8_t digit,
1041                                             unsigned n)
1042 {
1043     *t |= ((uint64_t)(digit & 0xF) << (n << 2));
1044 }
1045 
1046 static inline void dfp_set_bcd_digit_128(uint64_t *t, uint8_t digit,
1047                                              unsigned n)
1048 {
1049     t[(n & 0x10) ? HI_IDX : LO_IDX] |=
1050         ((uint64_t)(digit & 0xF) << ((n & 15) << 2));
1051 }
1052 
1053 static inline void dfp_set_sign_64(uint64_t *t, uint8_t sgn)
1054 {
1055     *t <<= 4;
1056     *t |= (sgn & 0xF);
1057 }
1058 
1059 static inline void dfp_set_sign_128(uint64_t *t, uint8_t sgn)
1060 {
1061     t[HI_IDX] <<= 4;
1062     t[HI_IDX] |= (t[LO_IDX] >> 60);
1063     t[LO_IDX] <<= 4;
1064     t[LO_IDX] |= (sgn & 0xF);
1065 }
1066 
1067 #define DFP_HELPER_DEDPD(op, size)                                        \
1068 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b,          \
1069                  uint32_t sp)                                             \
1070 {                                                                         \
1071     struct PPC_DFP dfp;                                                   \
1072     uint8_t digits[34];                                                   \
1073     int i, N;                                                             \
1074                                                                           \
1075     dfp_prepare_decimal##size(&dfp, 0, b, env);                           \
1076                                                                           \
1077     decNumberGetBCD(&dfp.b, digits);                                      \
1078     dfp.t64[0] = dfp.t64[1] = 0;                                          \
1079     N = dfp.b.digits;                                                     \
1080                                                                           \
1081     for (i = 0; (i < N) && (i < (size)/4); i++) {                         \
1082         dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i);              \
1083     }                                                                     \
1084                                                                           \
1085     if (sp & 2) {                                                         \
1086         uint8_t sgn;                                                      \
1087                                                                           \
1088         if (decNumberIsNegative(&dfp.b)) {                                \
1089             sgn = 0xD;                                                    \
1090         } else {                                                          \
1091             sgn = ((sp & 1) ? 0xF : 0xC);                                 \
1092         }                                                                 \
1093         dfp_set_sign_##size(dfp.t64, sgn);                                \
1094     }                                                                     \
1095                                                                           \
1096     if (size == 64) {                                                     \
1097         set_dfp64(t, dfp.t64);                                            \
1098     } else if (size == 128) {                                             \
1099         set_dfp128(t, dfp.t64);                                           \
1100     }                                                                     \
1101 }
1102 
1103 DFP_HELPER_DEDPD(ddedpd, 64)
1104 DFP_HELPER_DEDPD(ddedpdq, 128)
1105 
1106 static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t, unsigned n)
1107 {
1108     return *t >> ((n << 2) & 63) & 15;
1109 }
1110 
1111 static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t, unsigned n)
1112 {
1113     return t[(n & 0x10) ? HI_IDX : LO_IDX] >> ((n << 2) & 63) & 15;
1114 }
1115 
1116 #define DFP_HELPER_ENBCD(op, size)                                           \
1117 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b,             \
1118                  uint32_t s)                                                 \
1119 {                                                                            \
1120     struct PPC_DFP dfp;                                                      \
1121     uint8_t digits[32];                                                      \
1122     int n = 0, offset = 0, sgn = 0, nonzero = 0;                             \
1123                                                                              \
1124     dfp_prepare_decimal##size(&dfp, 0, b, env);                              \
1125                                                                              \
1126     decNumberZero(&dfp.t);                                                   \
1127                                                                              \
1128     if (s) {                                                                 \
1129         uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++);     \
1130         switch (sgnNibble) {                                                 \
1131         case 0xD:                                                            \
1132         case 0xB:                                                            \
1133             sgn = 1;                                                         \
1134             break;                                                           \
1135         case 0xC:                                                            \
1136         case 0xF:                                                            \
1137         case 0xA:                                                            \
1138         case 0xE:                                                            \
1139             sgn = 0;                                                         \
1140             break;                                                           \
1141         default:                                                             \
1142             dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE);            \
1143             return;                                                          \
1144         }                                                                    \
1145         }                                                                    \
1146                                                                              \
1147     while (offset < (size) / 4) {                                            \
1148         n++;                                                                 \
1149         digits[(size) / 4 - n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \
1150         if (digits[(size) / 4 - n] > 10) {                                   \
1151             dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE);            \
1152             return;                                                          \
1153         } else {                                                             \
1154             nonzero |= (digits[(size) / 4 - n] > 0);                         \
1155         }                                                                    \
1156     }                                                                        \
1157                                                                              \
1158     if (nonzero) {                                                           \
1159         decNumberSetBCD(&dfp.t, digits + ((size) / 4) - n, n);               \
1160     }                                                                        \
1161                                                                              \
1162     if (s && sgn)  {                                                         \
1163         dfp.t.bits |= DECNEG;                                                \
1164     }                                                                        \
1165     dfp_finalize_decimal##size(&dfp);                                        \
1166     dfp_set_FPRF_from_FRT(&dfp);                                             \
1167     if ((size) == 64) {                                                      \
1168         set_dfp64(t, dfp.t64);                                               \
1169     } else if ((size) == 128) {                                              \
1170         set_dfp128(t, dfp.t64);                                              \
1171     }                                                                        \
1172 }
1173 
1174 DFP_HELPER_ENBCD(denbcd, 64)
1175 DFP_HELPER_ENBCD(denbcdq, 128)
1176 
1177 #define DFP_HELPER_XEX(op, size)                               \
1178 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *b) \
1179 {                                                              \
1180     struct PPC_DFP dfp;                                        \
1181     uint64_t t64;                                              \
1182                                                                \
1183     dfp_prepare_decimal##size(&dfp, 0, b, env);                \
1184                                                                \
1185     if (unlikely(decNumberIsSpecial(&dfp.b))) {                \
1186         if (decNumberIsInfinite(&dfp.b)) {                     \
1187             t64 = -1;                                          \
1188         } else if (decNumberIsSNaN(&dfp.b)) {                  \
1189             t64 = -3;                                          \
1190         } else if (decNumberIsQNaN(&dfp.b)) {                  \
1191             t64 = -2;                                          \
1192         } else {                                               \
1193             assert(0);                                         \
1194         }                                                      \
1195         set_dfp64(t, &t64);                                    \
1196     } else {                                                   \
1197         if ((size) == 64) {                                    \
1198             t64 = dfp.b.exponent + 398;                        \
1199         } else if ((size) == 128) {                            \
1200             t64 = dfp.b.exponent + 6176;                       \
1201         } else {                                               \
1202             assert(0);                                         \
1203         }                                                      \
1204         set_dfp64(t, &t64);                                    \
1205     }                                                          \
1206 }
1207 
1208 DFP_HELPER_XEX(dxex, 64)
1209 DFP_HELPER_XEX(dxexq, 128)
1210 
1211 static void dfp_set_raw_exp_64(uint64_t *t, uint64_t raw)
1212 {
1213     *t &= 0x8003ffffffffffffULL;
1214     *t |= (raw << (63 - 13));
1215 }
1216 
1217 static void dfp_set_raw_exp_128(uint64_t *t, uint64_t raw)
1218 {
1219     t[HI_IDX] &= 0x80003fffffffffffULL;
1220     t[HI_IDX] |= (raw << (63 - 17));
1221 }
1222 
1223 #define DFP_HELPER_IEX(op, size)                                          \
1224 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a,          \
1225                  ppc_fprp_t *b)                                           \
1226 {                                                                         \
1227     struct PPC_DFP dfp;                                                   \
1228     uint64_t raw_qnan, raw_snan, raw_inf, max_exp, a64;                   \
1229     int bias;                                                             \
1230     int64_t exp;                                                          \
1231                                                                           \
1232     get_dfp64(&a64, a);                                                   \
1233     exp = (int64_t)a64;                                                   \
1234     dfp_prepare_decimal##size(&dfp, 0, b, env);                           \
1235                                                                           \
1236     if ((size) == 64) {                                                   \
1237         max_exp = 767;                                                    \
1238         raw_qnan = 0x1F00;                                                \
1239         raw_snan = 0x1F80;                                                \
1240         raw_inf = 0x1E00;                                                 \
1241         bias = 398;                                                       \
1242     } else if ((size) == 128) {                                           \
1243         max_exp = 12287;                                                  \
1244         raw_qnan = 0x1f000;                                               \
1245         raw_snan = 0x1f800;                                               \
1246         raw_inf = 0x1e000;                                                \
1247         bias = 6176;                                                      \
1248     } else {                                                              \
1249         assert(0);                                                        \
1250     }                                                                     \
1251                                                                           \
1252     if (unlikely((exp < 0) || (exp > max_exp))) {                         \
1253         dfp.t64[0] = dfp.b64[0];                                          \
1254         dfp.t64[1] = dfp.b64[1];                                          \
1255         if (exp == -1) {                                                  \
1256             dfp_set_raw_exp_##size(dfp.t64, raw_inf);                     \
1257         } else if (exp == -3) {                                           \
1258             dfp_set_raw_exp_##size(dfp.t64, raw_snan);                    \
1259         } else {                                                          \
1260             dfp_set_raw_exp_##size(dfp.t64, raw_qnan);                    \
1261         }                                                                 \
1262     } else {                                                              \
1263         dfp.t = dfp.b;                                                    \
1264         if (unlikely(decNumberIsSpecial(&dfp.t))) {                       \
1265             dfp.t.bits &= ~DECSPECIAL;                                    \
1266         }                                                                 \
1267         dfp.t.exponent = exp - bias;                                      \
1268         dfp_finalize_decimal##size(&dfp);                                 \
1269     }                                                                     \
1270     if (size == 64) {                                                     \
1271         set_dfp64(t, dfp.t64);                                            \
1272     } else if (size == 128) {                                             \
1273         set_dfp128(t, dfp.t64);                                           \
1274     }                                                                     \
1275 }
1276 
1277 DFP_HELPER_IEX(diex, 64)
1278 DFP_HELPER_IEX(diexq, 128)
1279 
1280 static void dfp_clear_lmd_from_g5msb(uint64_t *t)
1281 {
1282 
1283     /* The most significant 5 bits of the PowerPC DFP format combine bits  */
1284     /* from the left-most decimal digit (LMD) and the biased exponent.     */
1285     /* This  routine clears the LMD bits while preserving the exponent     */
1286     /*  bits.  See "Figure 80: Encoding of bits 0:4 of the G field for     */
1287     /*  Finite Numbers" in the Power ISA for additional details.           */
1288 
1289     uint64_t g5msb = (*t >> 58) & 0x1F;
1290 
1291     if ((g5msb >> 3) < 3) { /* LMD in [0-7] ? */
1292        *t &= ~(7ULL << 58);
1293     } else {
1294        switch (g5msb & 7) {
1295        case 0:
1296        case 1:
1297            g5msb = 0;
1298            break;
1299        case 2:
1300        case 3:
1301            g5msb = 0x8;
1302            break;
1303        case 4:
1304        case 5:
1305            g5msb = 0x10;
1306            break;
1307        case 6:
1308            g5msb = 0x1E;
1309            break;
1310        case 7:
1311            g5msb = 0x1F;
1312            break;
1313        }
1314 
1315         *t &= ~(0x1fULL << 58);
1316         *t |= (g5msb << 58);
1317     }
1318 }
1319 
1320 #define DFP_HELPER_SHIFT(op, size, shift_left)                      \
1321 void helper_##op(CPUPPCState *env, ppc_fprp_t *t, ppc_fprp_t *a,    \
1322                  uint32_t sh)                                       \
1323 {                                                                   \
1324     struct PPC_DFP dfp;                                             \
1325     unsigned max_digits = ((size) == 64) ? 16 : 34;                 \
1326                                                                     \
1327     dfp_prepare_decimal##size(&dfp, a, 0, env);                     \
1328                                                                     \
1329     if (sh <= max_digits) {                                         \
1330                                                                     \
1331         decNumber shd;                                              \
1332         unsigned special = dfp.a.bits & DECSPECIAL;                 \
1333                                                                     \
1334         if (shift_left) {                                           \
1335             decNumberFromUInt32(&shd, sh);                          \
1336         } else {                                                    \
1337             decNumberFromInt32(&shd, -((int32_t)sh));               \
1338         }                                                           \
1339                                                                     \
1340         dfp.a.bits &= ~DECSPECIAL;                                  \
1341         decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context);         \
1342                                                                     \
1343         dfp.t.bits |= special;                                      \
1344         if (special && (dfp.t.digits >= max_digits)) {              \
1345             dfp.t.digits = max_digits - 1;                          \
1346         }                                                           \
1347                                                                     \
1348         dfp_finalize_decimal##size(&dfp);                           \
1349     } else {                                                        \
1350         if ((size) == 64) {                                         \
1351             dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL;        \
1352             dfp_clear_lmd_from_g5msb(dfp.t64);                      \
1353         } else {                                                    \
1354             dfp.t64[HI_IDX] = dfp.a64[HI_IDX] &                     \
1355                               0xFFFFC00000000000ULL;                \
1356             dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX);             \
1357             dfp.t64[LO_IDX] = 0;                                    \
1358         }                                                           \
1359     }                                                               \
1360                                                                     \
1361     if ((size) == 64) {                                             \
1362         set_dfp64(t, dfp.t64);                                      \
1363     } else {                                                        \
1364         set_dfp128(t, dfp.t64);                                     \
1365     }                                                               \
1366 }
1367 
1368 DFP_HELPER_SHIFT(dscli, 64, 1)
1369 DFP_HELPER_SHIFT(dscliq, 128, 1)
1370 DFP_HELPER_SHIFT(dscri, 64, 0)
1371 DFP_HELPER_SHIFT(dscriq, 128, 0)
1372