2 * PowerPC Decimal Floating Point (DPF) emulation helpers for QEMU.
4 * Copyright (c) 2014 IBM Corporation.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "exec/helper-proto.h"
23 #define DECNUMDIGITS 34
24 #include "libdecnumber/decContext.h"
25 #include "libdecnumber/decNumber.h"
26 #include "libdecnumber/dpd/decimal32.h"
27 #include "libdecnumber/dpd/decimal64.h"
28 #include "libdecnumber/dpd/decimal128.h"
30 #if defined(HOST_WORDS_BIGENDIAN)
40 uint64_t t64[2], a64[2], b64[2];
46 static void dfp_prepare_rounding_mode(decContext *context, uint64_t fpscr)
50 switch ((fpscr >> 32) & 0x7) {
52 rnd = DEC_ROUND_HALF_EVEN;
58 rnd = DEC_ROUND_CEILING;
61 rnd = DEC_ROUND_FLOOR;
64 rnd = DEC_ROUND_HALF_UP;
67 rnd = DEC_ROUND_HALF_DOWN;
76 g_assert_not_reached();
79 decContextSetRounding(context, rnd);
82 static void dfp_prepare_decimal64(struct PPC_DFP *dfp, uint64_t *a,
83 uint64_t *b, CPUPPCState *env)
85 decContextDefault(&dfp->context, DEC_INIT_DECIMAL64);
86 dfp_prepare_rounding_mode(&dfp->context, env->fpscr);
91 decimal64ToNumber((decimal64 *)dfp->a64, &dfp->a);
94 decNumberZero(&dfp->a);
99 decimal64ToNumber((decimal64 *)dfp->b64, &dfp->b);
102 decNumberZero(&dfp->b);
106 static void dfp_prepare_decimal128(struct PPC_DFP *dfp, uint64_t *a,
107 uint64_t *b, CPUPPCState *env)
109 decContextDefault(&dfp->context, DEC_INIT_DECIMAL128);
110 dfp_prepare_rounding_mode(&dfp->context, env->fpscr);
114 dfp->a64[0] = a[HI_IDX];
115 dfp->a64[1] = a[LO_IDX];
116 decimal128ToNumber((decimal128 *)dfp->a64, &dfp->a);
118 dfp->a64[0] = dfp->a64[1] = 0;
119 decNumberZero(&dfp->a);
123 dfp->b64[0] = b[HI_IDX];
124 dfp->b64[1] = b[LO_IDX];
125 decimal128ToNumber((decimal128 *)dfp->b64, &dfp->b);
127 dfp->b64[0] = dfp->b64[1] = 0;
128 decNumberZero(&dfp->b);
132 #define FP_FX (1ull << FPSCR_FX)
133 #define FP_FEX (1ull << FPSCR_FEX)
134 #define FP_OX (1ull << FPSCR_OX)
135 #define FP_OE (1ull << FPSCR_OE)
136 #define FP_UX (1ull << FPSCR_UX)
137 #define FP_UE (1ull << FPSCR_UE)
138 #define FP_XX (1ull << FPSCR_XX)
139 #define FP_XE (1ull << FPSCR_XE)
140 #define FP_ZX (1ull << FPSCR_ZX)
141 #define FP_ZE (1ull << FPSCR_ZE)
142 #define FP_VX (1ull << FPSCR_VX)
143 #define FP_VXSNAN (1ull << FPSCR_VXSNAN)
144 #define FP_VXISI (1ull << FPSCR_VXISI)
145 #define FP_VXIMZ (1ull << FPSCR_VXIMZ)
146 #define FP_VXZDZ (1ull << FPSCR_VXZDZ)
147 #define FP_VXIDI (1ull << FPSCR_VXIDI)
148 #define FP_VXVC (1ull << FPSCR_VXVC)
149 #define FP_VXCVI (1ull << FPSCR_VXCVI)
150 #define FP_VE (1ull << FPSCR_VE)
151 #define FP_FI (1ull << FPSCR_FI)
153 static void dfp_set_FPSCR_flag(struct PPC_DFP *dfp, uint64_t flag,
156 dfp->env->fpscr |= (flag | FP_FX);
157 if (dfp->env->fpscr & enabled) {
158 dfp->env->fpscr |= FP_FEX;
162 static void dfp_set_FPRF_from_FRT_with_context(struct PPC_DFP *dfp,
168 switch (decNumberClass(&dfp->t, context)) {
175 case DEC_CLASS_NEG_INF:
178 case DEC_CLASS_NEG_NORMAL:
181 case DEC_CLASS_NEG_SUBNORMAL:
184 case DEC_CLASS_NEG_ZERO:
187 case DEC_CLASS_POS_ZERO:
190 case DEC_CLASS_POS_SUBNORMAL:
193 case DEC_CLASS_POS_NORMAL:
196 case DEC_CLASS_POS_INF:
200 assert(0); /* should never get here */
202 dfp->env->fpscr &= ~(0x1F << 12);
203 dfp->env->fpscr |= (fprf << 12);
206 static void dfp_set_FPRF_from_FRT(struct PPC_DFP *dfp)
208 dfp_set_FPRF_from_FRT_with_context(dfp, &dfp->context);
211 static void dfp_check_for_OX(struct PPC_DFP *dfp)
213 if (dfp->context.status & DEC_Overflow) {
214 dfp_set_FPSCR_flag(dfp, FP_OX, FP_OE);
218 static void dfp_check_for_UX(struct PPC_DFP *dfp)
220 if (dfp->context.status & DEC_Underflow) {
221 dfp_set_FPSCR_flag(dfp, FP_UX, FP_UE);
225 static void dfp_check_for_XX(struct PPC_DFP *dfp)
227 if (dfp->context.status & DEC_Inexact) {
228 dfp_set_FPSCR_flag(dfp, FP_XX | FP_FI, FP_XE);
232 static void dfp_check_for_VXSNAN(struct PPC_DFP *dfp)
234 if (dfp->context.status & DEC_Invalid_operation) {
235 if (decNumberIsSNaN(&dfp->a) || decNumberIsSNaN(&dfp->b)) {
236 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FP_VE);
241 static void dfp_check_for_VXISI(struct PPC_DFP *dfp, int testForSameSign)
243 if (dfp->context.status & DEC_Invalid_operation) {
244 if (decNumberIsInfinite(&dfp->a) && decNumberIsInfinite(&dfp->b)) {
245 int same = decNumberClass(&dfp->a, &dfp->context) ==
246 decNumberClass(&dfp->b, &dfp->context);
247 if ((same && testForSameSign) || (!same && !testForSameSign)) {
248 dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXISI, FP_VE);
254 static void dfp_check_for_VXISI_add(struct PPC_DFP *dfp)
256 dfp_check_for_VXISI(dfp, 0);
259 #define DFP_HELPER_TAB(op, dnop, postprocs, size) \
260 void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \
262 struct PPC_DFP dfp; \
263 dfp_prepare_decimal##size(&dfp, a, b, env); \
264 dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \
265 decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \
269 } else if (size == 128) { \
270 t[0] = dfp.t64[HI_IDX]; \
271 t[1] = dfp.t64[LO_IDX]; \
275 static void ADD_PPs(struct PPC_DFP *dfp)
277 dfp_set_FPRF_from_FRT(dfp);
278 dfp_check_for_OX(dfp);
279 dfp_check_for_UX(dfp);
280 dfp_check_for_XX(dfp);
281 dfp_check_for_VXSNAN(dfp);
282 dfp_check_for_VXISI_add(dfp);
285 DFP_HELPER_TAB(dadd, decNumberAdd, ADD_PPs, 64)
286 DFP_HELPER_TAB(daddq, decNumberAdd, ADD_PPs, 128)