summaryrefslogtreecommitdiffstats
path: root/qemu/block/throttle-groups.c
blob: 4920e094950a66b8d39278e76f03632becbd22fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * QEMU block throttling group infrastructure
 *
 * Copyright (C) Nodalink, EURL. 2014
 * Copyright (C) Igalia, S.L. 2015
 *
 * Authors:
 *   Benoît Canet <benoit.canet@nodalink.com>
 *   Alberto Garcia <berto@igalia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 or
 * (at your option) version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "block/throttle-groups.h"
#include "qemu/queue.h"
#include "qemu/thread.h"
#include "sysemu/qtest.h"

/* The ThrottleGroup structure (with its ThrottleState) is shared
 * among different BlockDriverState and it's independent from
 * AioContext, so in order to use it from different threads it needs
 * its own locking.
 *
 * This locking is however handled internally in this file, so it's
 * transparent to outside users.
 *
 * The whole ThrottleGroup structure is private and invisible to
 * outside users, that only use it through its ThrottleState.
 *
 * In addition to the ThrottleGroup structure, BlockDriverState has
 * fields that need to be accessed by other members of the group and
 * therefore also need to be protected by this lock. Once a BDS is
 * registered in a group those fields can be accessed by other threads
 * any time.
 *
 * Again, all this is handled internally and is mostly transparent to
 * the outside. The 'throttle_timers' field however has an additional
 * constraint because it may be temporarily invalid (see for example
 * bdrv_set_aio_context()). Therefore in this file a thread will
 * access some other BDS's timers only after verifying that that BDS
 * has throttled requests in the queue.
 */
typedef struct ThrottleGroup {
    char *name; /* This is constant during the lifetime of the group */

    QemuMutex lock; /* This lock protects the following four fields */
    ThrottleState ts;
    QLIST_HEAD(, BlockDriverState) head;
    BlockDriverState *tokens[2];
    bool any_timer_armed[2];

    /* These two are protected by the global throttle_groups_lock */
    unsigned refcount;
    QTAILQ_ENTRY(ThrottleGroup) list;
} ThrottleGroup;

static QemuMutex throttle_groups_lock;
static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
    QTAILQ_HEAD_INITIALIZER(throttle_groups);

/* Increments the reference count of a ThrottleGroup given its name.
 *
 * If no ThrottleGroup is found with the given name a new one is
 * created.
 *
 * @name: the name of the ThrottleGroup
 * @ret:  the ThrottleState member of the ThrottleGroup
 */
ThrottleState *throttle_group_incref(const char *name)
{
    ThrottleGroup *tg = NULL;
    ThrottleGroup *iter;

    qemu_mutex_lock(&throttle_groups_lock);

    /* Look for an existing group with that name */
    QTAILQ_FOREACH(iter, &throttle_groups, list) {
        if (!strcmp(name, iter->name)) {
            tg = iter;
            break;
        }
    }

    /* Create a new one if not found */
    if (!tg) {
        tg = g_new0(ThrottleGroup, 1);
        tg->name = g_strdup(name);
        qemu_mutex_init(&tg->lock);
        throttle_init(&tg->ts);
        QLIST_INIT(&tg->head);

        QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
    }

    tg->refcount++;

    qemu_mutex_unlock(&throttle_groups_lock);

    return &tg->ts;
}

/* Decrease the reference count of a ThrottleGroup.
 *
 * When the reference count reaches zero the ThrottleGroup is
 * destroyed.
 *
 * @ts:  The ThrottleGroup to unref, given by its ThrottleState member
 */
void throttle_group_unref(ThrottleState *ts)
{
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);

    qemu_mutex_lock(&throttle_groups_lock);
    if (--tg->refcount == 0) {
        QTAILQ_REMOVE(&throttle_groups, tg, list);
        qemu_mutex_destroy(&tg->lock);
        g_free(tg->name);
        g_free(tg);
    }
    qemu_mutex_unlock(&throttle_groups_lock);
}

/* Get the name from a BlockDriverState's ThrottleGroup. The name (and
 * the pointer) is guaranteed to remain constant during the lifetime
 * of the group.
 *
 * @bs:   a BlockDriverState that is member of a throttling group
 * @ret:  the name of the group.
 */
const char *throttle_group_get_name(BlockDriverState *bs)
{
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
    return tg->name;
}

/* Return the next BlockDriverState in the round-robin sequence,
 * simulating a circular list.
 *
 * This assumes that tg->lock is held.
 *
 * @bs:  the current BlockDriverState
 * @ret: the next BlockDriverState in the sequence
 */
static BlockDriverState *throttle_group_next_bs(BlockDriverState *bs)
{
    ThrottleState *ts = bs->throttle_state;
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    BlockDriverState *next = QLIST_NEXT(bs, round_robin);

    if (!next) {
        return QLIST_FIRST(&tg->head);
    }

    return next;
}

/* Return the next BlockDriverState in the round-robin sequence with
 * pending I/O requests.
 *
 * This assumes that tg->lock is held.
 *
 * @bs:        the current BlockDriverState
 * @is_write:  the type of operation (read/write)
 * @ret:       the next BlockDriverState with pending requests, or bs
 *             if there is none.
 */
static BlockDriverState *next_throttle_token(BlockDriverState *bs,
                                             bool is_write)
{
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
    BlockDriverState *token, *start;

    start = token = tg->tokens[is_write];

    /* get next bs round in round robin style */
    token = throttle_group_next_bs(token);
    while (token != start && !token->pending_reqs[is_write]) {
        token = throttle_group_next_bs(token);
    }

    /* If no IO are queued for scheduling on the next round robin token
     * then decide the token is the current bs because chances are
     * the current bs get the current request queued.
     */
    if (token == start && !token->pending_reqs[is_write]) {
        token = bs;
    }

    return token;
}

/* Check if the next I/O request for a BlockDriverState needs to be
 * throttled or not. If there's no timer set in this group, set one
 * and update the token accordingly.
 *
 * This assumes that tg->lock is held.
 *
 * @bs:         the current BlockDriverState
 * @is_write:   the type of operation (read/write)
 * @ret:        whether the I/O request needs to be throttled or not
 */
static bool throttle_group_schedule_timer(BlockDriverState *bs,
                                          bool is_write)
{
    ThrottleState *ts = bs->throttle_state;
    ThrottleTimers *tt = &bs->throttle_timers;
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    bool must_wait;

    /* Check if any of the timers in this group is already armed */
    if (tg->any_timer_armed[is_write]) {
        return true;
    }

    must_wait = throttle_schedule_timer(ts, tt, is_write);

    /* If a timer just got armed, set bs as the current token */
    if (must_wait) {
        tg->tokens[is_write] = bs;
        tg->any_timer_armed[is_write] = true;
    }

    return must_wait;
}

/* Look for the next pending I/O request and schedule it.
 *
 * This assumes that tg->lock is held.
 *
 * @bs:        the current BlockDriverState
 * @is_write:  the type of operation (read/write)
 */
static void schedule_next_request(BlockDriverState *bs, bool is_write)
{
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
    bool must_wait;
    BlockDriverState *token;

    /* Check if there's any pending request to schedule next */
    token = next_throttle_token(bs, is_write);
    if (!token->pending_reqs[is_write]) {
        return;
    }

    /* Set a timer for the request if it needs to be throttled */
    must_wait = throttle_group_schedule_timer(token, is_write);

    /* If it doesn't have to wait, queue it for immediate execution */
    if (!must_wait) {
        /* Give preference to requests from the current bs */
        if (qemu_in_coroutine() &&
            qemu_co_queue_next(&bs->throttled_reqs[is_write])) {
            token = bs;
        } else {
            ThrottleTimers *tt = &token->throttle_timers;
            int64_t now = qemu_clock_get_ns(tt->clock_type);
            timer_mod(tt->timers[is_write], now + 1);
            tg->any_timer_armed[is_write] = true;
        }
        tg->tokens[is_write] = token;
    }
}

/* Check if an I/O request needs to be throttled, wait and set a timer
 * if necessary, and schedule the next request using a round robin
 * algorithm.
 *
 * @bs:        the current BlockDriverState
 * @bytes:     the number of bytes for this I/O
 * @is_write:  the type of operation (read/write)
 */
void coroutine_fn throttle_group_co_io_limits_intercept(BlockDriverState *bs,
                                                        unsigned int bytes,
                                                        bool is_write)
{
    bool must_wait;
    BlockDriverState *token;

    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
    qemu_mutex_lock(&tg->lock);

    /* First we check if this I/O has to be throttled. */
    token = next_throttle_token(bs, is_write);
    must_wait = throttle_group_schedule_timer(token, is_write);

    /* Wait if there's a timer set or queued requests of this type */
    if (must_wait || bs->pending_reqs[is_write]) {
        bs->pending_reqs[is_write]++;
        qemu_mutex_unlock(&tg->lock);
        qemu_co_queue_wait(&bs->throttled_reqs[is_write]);
        qemu_mutex_lock(&tg->lock);
        bs->pending_reqs[is_write]--;
    }

    /* The I/O will be executed, so do the accounting */
    throttle_account(bs->throttle_state, is_write, bytes);

    /* Schedule the next request */
    schedule_next_request(bs, is_write);

    qemu_mutex_unlock(&tg->lock);
}

/* Update the throttle configuration for a particular group. Similar
 * to throttle_config(), but guarantees atomicity within the
 * throttling group.
 *
 * @bs:  a BlockDriverState that is member of the group
 * @cfg: the configuration to set
 */
void throttle_group_config(BlockDriverState *bs, ThrottleConfig *cfg)
{
    ThrottleTimers *tt = &bs->throttle_timers;
    ThrottleState *ts = bs->throttle_state;
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    qemu_mutex_lock(&tg->lock);
    /* throttle_config() cancels the timers */
    if (timer_pending(tt->timers[0])) {
        tg->any_timer_armed[0] = false;
    }
    if (timer_pending(tt->timers[1])) {
        tg->any_timer_armed[1] = false;
    }
    throttle_config(ts, tt, cfg);
    qemu_mutex_unlock(&tg->lock);
}

/* Get the throttle configuration from a particular group. Similar to
 * throttle_get_config(), but guarantees atomicity within the
 * throttling group.
 *
 * @bs:  a BlockDriverState that is member of the group
 * @cfg: the configuration will be written here
 */
void throttle_group_get_config(BlockDriverState *bs, ThrottleConfig *cfg)
{
    ThrottleState *ts = bs->throttle_state;
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    qemu_mutex_lock(&tg->lock);
    throttle_get_config(ts, cfg);
    qemu_mutex_unlock(&tg->lock);
}

/* ThrottleTimers callback. This wakes up a request that was waiting
 * because it had been throttled.
 *
 * @bs:        the BlockDriverState whose request had been throttled
 * @is_write:  the type of operation (read/write)
 */
static void timer_cb(BlockDriverState *bs, bool is_write)
{
    ThrottleState *ts = bs->throttle_state;
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    bool empty_queue;

    /* The timer has just been fired, so we can update the flag */
    qemu_mutex_lock(&tg->lock);
    tg->any_timer_armed[is_write] = false;
    qemu_mutex_unlock(&tg->lock);

    /* Run the request that was waiting for this timer */
    empty_queue = !qemu_co_enter_next(&bs->throttled_reqs[is_write]);

    /* If the request queue was empty then we have to take care of
     * scheduling the next one */
    if (empty_queue) {
        qemu_mutex_lock(&tg->lock);
        schedule_next_request(bs, is_write);
        qemu_mutex_unlock(&tg->lock);
    }
}

static void read_timer_cb(void *opaque)
{
    timer_cb(opaque, false);
}

static void write_timer_cb(void *opaque)
{
    timer_cb(opaque, true);
}

/* Register a BlockDriverState in the throttling group, also
 * initializing its timers and updating its throttle_state pointer to
 * point to it. If a throttling group with that name does not exist
 * yet, it will be created.
 *
 * @bs:        the BlockDriverState to insert
 * @groupname: the name of the group
 */
void throttle_group_register_bs(BlockDriverState *bs, const char *groupname)
{
    int i;
    ThrottleState *ts = throttle_group_incref(groupname);
    ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
    int clock_type = QEMU_CLOCK_REALTIME;

    if (qtest_enabled()) {
        /* For testing block IO throttling only */
        clock_type = QEMU_CLOCK_VIRTUAL;
    }

    bs->throttle_state = ts;

    qemu_mutex_lock(&tg->lock);
    /* If the ThrottleGroup is new set this BlockDriverState as the token */
    for (i = 0; i < 2; i++) {
        if (!tg->tokens[i]) {
            tg->tokens[i] = bs;
        }
    }

    QLIST_INSERT_HEAD(&tg->head, bs, round_robin);

    throttle_timers_init(&bs->throttle_timers,
                         bdrv_get_aio_context(bs),
                         clock_type,
                         read_timer_cb,
                         write_timer_cb,
                         bs);

    qemu_mutex_unlock(&tg->lock);
}

/* Unregister a BlockDriverState from its group, removing it from the
 * list, destroying the timers and setting the throttle_state pointer
 * to NULL.
 *
 * The BlockDriverState must not have pending throttled requests, so
 * the caller has to drain them first.
 *
 * The group will be destroyed if it's empty after this operation.
 *
 * @bs: the BlockDriverState to remove
 */
void throttle_group_unregister_bs(BlockDriverState *bs)
{
    ThrottleGroup *tg = container_of(bs->throttle_state, ThrottleGroup, ts);
    int i;

    assert(bs->pending_reqs[0] == 0 && bs->pending_reqs[1] == 0);
    assert(qemu_co_queue_empty(&bs->throttled_reqs[0]));
    assert(qemu_co_queue_empty(&bs->throttled_reqs[1]));

    qemu_mutex_lock(&tg->lock);
    for (i = 0; i < 2; i++) {
        if (tg->tokens[i] == bs) {
            BlockDriverState *token = throttle_group_next_bs(bs);
            /* Take care of the case where this is the last bs in the group */
            if (token == bs) {
                token = NULL;
            }
            tg->tokens[i] = token;
        }
    }

    /* remove the current bs from the list */
    QLIST_REMOVE(bs, round_robin);
    throttle_timers_destroy(&bs->throttle_timers);
    qemu_mutex_unlock(&tg->lock);

    throttle_group_unref(&tg->ts);
    bs->throttle_state = NULL;
}

static void throttle_groups_init(void)
{
    qemu_mutex_init(&throttle_groups_lock);
}

block_init(throttle_groups_init);
span> (fprf << 12); } static void dfp_set_FPRF_from_FRT(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT_with_context(dfp, &dfp->context); } static void dfp_set_FPRF_from_FRT_short(struct PPC_DFP *dfp) { decContext shortContext; decContextDefault(&shortContext, DEC_INIT_DECIMAL32); dfp_set_FPRF_from_FRT_with_context(dfp, &shortContext); } static void dfp_set_FPRF_from_FRT_long(struct PPC_DFP *dfp) { decContext longContext; decContextDefault(&longContext, DEC_INIT_DECIMAL64); dfp_set_FPRF_from_FRT_with_context(dfp, &longContext); } static void dfp_check_for_OX(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Overflow) { dfp_set_FPSCR_flag(dfp, FP_OX, FP_OE); } } static void dfp_check_for_UX(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Underflow) { dfp_set_FPSCR_flag(dfp, FP_UX, FP_UE); } } static void dfp_check_for_XX(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Inexact) { dfp_set_FPSCR_flag(dfp, FP_XX | FP_FI, FP_XE); } } static void dfp_check_for_ZX(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Division_by_zero) { dfp_set_FPSCR_flag(dfp, FP_ZX, FP_ZE); } } static void dfp_check_for_VXSNAN(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Invalid_operation) { if (decNumberIsSNaN(&dfp->a) || decNumberIsSNaN(&dfp->b)) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FP_VE); } } } static void dfp_check_for_VXSNAN_and_convert_to_QNaN(struct PPC_DFP *dfp) { if (decNumberIsSNaN(&dfp->t)) { dfp->t.bits &= ~DECSNAN; dfp->t.bits |= DECNAN; dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FP_VE); } } static void dfp_check_for_VXISI(struct PPC_DFP *dfp, int testForSameSign) { if (dfp->context.status & DEC_Invalid_operation) { if (decNumberIsInfinite(&dfp->a) && decNumberIsInfinite(&dfp->b)) { int same = decNumberClass(&dfp->a, &dfp->context) == decNumberClass(&dfp->b, &dfp->context); if ((same && testForSameSign) || (!same && !testForSameSign)) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXISI, FP_VE); } } } } static void dfp_check_for_VXISI_add(struct PPC_DFP *dfp) { dfp_check_for_VXISI(dfp, 0); } static void dfp_check_for_VXISI_subtract(struct PPC_DFP *dfp) { dfp_check_for_VXISI(dfp, 1); } static void dfp_check_for_VXIMZ(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Invalid_operation) { if ((decNumberIsInfinite(&dfp->a) && decNumberIsZero(&dfp->b)) || (decNumberIsInfinite(&dfp->b) && decNumberIsZero(&dfp->a))) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXIMZ, FP_VE); } } } static void dfp_check_for_VXZDZ(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Division_undefined) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXZDZ, FP_VE); } } static void dfp_check_for_VXIDI(struct PPC_DFP *dfp) { if (dfp->context.status & DEC_Invalid_operation) { if (decNumberIsInfinite(&dfp->a) && decNumberIsInfinite(&dfp->b)) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXIDI, FP_VE); } } } static void dfp_check_for_VXVC(struct PPC_DFP *dfp) { if (decNumberIsNaN(&dfp->a) || decNumberIsNaN(&dfp->b)) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXVC, FP_VE); } } static void dfp_check_for_VXCVI(struct PPC_DFP *dfp) { if ((dfp->context.status & DEC_Invalid_operation) && (!decNumberIsSNaN(&dfp->a)) && (!decNumberIsSNaN(&dfp->b))) { dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FP_VE); } } static void dfp_set_CRBF_from_T(struct PPC_DFP *dfp) { if (decNumberIsNaN(&dfp->t)) { dfp->crbf = 1; } else if (decNumberIsZero(&dfp->t)) { dfp->crbf = 2; } else if (decNumberIsNegative(&dfp->t)) { dfp->crbf = 8; } else { dfp->crbf = 4; } } static void dfp_set_FPCC_from_CRBF(struct PPC_DFP *dfp) { dfp->env->fpscr &= ~(0xF << 12); dfp->env->fpscr |= (dfp->crbf << 12); } static inline void dfp_makeQNaN(decNumber *dn) { dn->bits &= ~DECSPECIAL; dn->bits |= DECNAN; } static inline int dfp_get_digit(decNumber *dn, int n) { assert(DECDPUN == 3); int unit = n / DECDPUN; int dig = n % DECDPUN; switch (dig) { case 0: return dn->lsu[unit] % 10; case 1: return (dn->lsu[unit] / 10) % 10; case 2: return dn->lsu[unit] / 100; } g_assert_not_reached(); } #define DFP_HELPER_TAB(op, dnop, postprocs, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \ { \ struct PPC_DFP dfp; \ dfp_prepare_decimal##size(&dfp, a, b, env); \ dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \ postprocs(&dfp); \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } static void ADD_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_OX(dfp); dfp_check_for_UX(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXISI_add(dfp); } DFP_HELPER_TAB(dadd, decNumberAdd, ADD_PPs, 64) DFP_HELPER_TAB(daddq, decNumberAdd, ADD_PPs, 128) static void SUB_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_OX(dfp); dfp_check_for_UX(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXISI_subtract(dfp); } DFP_HELPER_TAB(dsub, decNumberSubtract, SUB_PPs, 64) DFP_HELPER_TAB(dsubq, decNumberSubtract, SUB_PPs, 128) static void MUL_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_OX(dfp); dfp_check_for_UX(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXIMZ(dfp); } DFP_HELPER_TAB(dmul, decNumberMultiply, MUL_PPs, 64) DFP_HELPER_TAB(dmulq, decNumberMultiply, MUL_PPs, 128) static void DIV_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_OX(dfp); dfp_check_for_UX(dfp); dfp_check_for_ZX(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXZDZ(dfp); dfp_check_for_VXIDI(dfp); } DFP_HELPER_TAB(ddiv, decNumberDivide, DIV_PPs, 64) DFP_HELPER_TAB(ddivq, decNumberDivide, DIV_PPs, 128) #define DFP_HELPER_BF_AB(op, dnop, postprocs, size) \ uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \ { \ struct PPC_DFP dfp; \ dfp_prepare_decimal##size(&dfp, a, b, env); \ dnop(&dfp.t, &dfp.a, &dfp.b, &dfp.context); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \ postprocs(&dfp); \ return dfp.crbf; \ } static void CMPU_PPs(struct PPC_DFP *dfp) { dfp_set_CRBF_from_T(dfp); dfp_set_FPCC_from_CRBF(dfp); dfp_check_for_VXSNAN(dfp); } DFP_HELPER_BF_AB(dcmpu, decNumberCompare, CMPU_PPs, 64) DFP_HELPER_BF_AB(dcmpuq, decNumberCompare, CMPU_PPs, 128) static void CMPO_PPs(struct PPC_DFP *dfp) { dfp_set_CRBF_from_T(dfp); dfp_set_FPCC_from_CRBF(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXVC(dfp); } DFP_HELPER_BF_AB(dcmpo, decNumberCompare, CMPO_PPs, 64) DFP_HELPER_BF_AB(dcmpoq, decNumberCompare, CMPO_PPs, 128) #define DFP_HELPER_TSTDC(op, size) \ uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \ { \ struct PPC_DFP dfp; \ int match = 0; \ \ dfp_prepare_decimal##size(&dfp, a, 0, env); \ \ match |= (dcm & 0x20) && decNumberIsZero(&dfp.a); \ match |= (dcm & 0x10) && decNumberIsSubnormal(&dfp.a, &dfp.context); \ match |= (dcm & 0x08) && decNumberIsNormal(&dfp.a, &dfp.context); \ match |= (dcm & 0x04) && decNumberIsInfinite(&dfp.a); \ match |= (dcm & 0x02) && decNumberIsQNaN(&dfp.a); \ match |= (dcm & 0x01) && decNumberIsSNaN(&dfp.a); \ \ if (decNumberIsNegative(&dfp.a)) { \ dfp.crbf = match ? 0xA : 0x8; \ } else { \ dfp.crbf = match ? 0x2 : 0x0; \ } \ \ dfp_set_FPCC_from_CRBF(&dfp); \ return dfp.crbf; \ } DFP_HELPER_TSTDC(dtstdc, 64) DFP_HELPER_TSTDC(dtstdcq, 128) #define DFP_HELPER_TSTDG(op, size) \ uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint32_t dcm) \ { \ struct PPC_DFP dfp; \ int minexp, maxexp, nzero_digits, nzero_idx, is_negative, is_zero, \ is_extreme_exp, is_subnormal, is_normal, leftmost_is_nonzero, \ match; \ \ dfp_prepare_decimal##size(&dfp, a, 0, env); \ \ if ((size) == 64) { \ minexp = -398; \ maxexp = 369; \ nzero_digits = 16; \ nzero_idx = 5; \ } else if ((size) == 128) { \ minexp = -6176; \ maxexp = 6111; \ nzero_digits = 34; \ nzero_idx = 11; \ } \ \ is_negative = decNumberIsNegative(&dfp.a); \ is_zero = decNumberIsZero(&dfp.a); \ is_extreme_exp = (dfp.a.exponent == maxexp) || \ (dfp.a.exponent == minexp); \ is_subnormal = decNumberIsSubnormal(&dfp.a, &dfp.context); \ is_normal = decNumberIsNormal(&dfp.a, &dfp.context); \ leftmost_is_nonzero = (dfp.a.digits == nzero_digits) && \ (dfp.a.lsu[nzero_idx] != 0); \ match = 0; \ \ match |= (dcm & 0x20) && is_zero && !is_extreme_exp; \ match |= (dcm & 0x10) && is_zero && is_extreme_exp; \ match |= (dcm & 0x08) && \ (is_subnormal || (is_normal && is_extreme_exp)); \ match |= (dcm & 0x04) && is_normal && !is_extreme_exp && \ !leftmost_is_nonzero; \ match |= (dcm & 0x02) && is_normal && !is_extreme_exp && \ leftmost_is_nonzero; \ match |= (dcm & 0x01) && decNumberIsSpecial(&dfp.a); \ \ if (is_negative) { \ dfp.crbf = match ? 0xA : 0x8; \ } else { \ dfp.crbf = match ? 0x2 : 0x0; \ } \ \ dfp_set_FPCC_from_CRBF(&dfp); \ return dfp.crbf; \ } DFP_HELPER_TSTDG(dtstdg, 64) DFP_HELPER_TSTDG(dtstdgq, 128) #define DFP_HELPER_TSTEX(op, size) \ uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \ { \ struct PPC_DFP dfp; \ int expa, expb, a_is_special, b_is_special; \ \ dfp_prepare_decimal##size(&dfp, a, b, env); \ \ expa = dfp.a.exponent; \ expb = dfp.b.exponent; \ a_is_special = decNumberIsSpecial(&dfp.a); \ b_is_special = decNumberIsSpecial(&dfp.b); \ \ if (a_is_special || b_is_special) { \ int atype = a_is_special ? (decNumberIsNaN(&dfp.a) ? 4 : 2) : 1; \ int btype = b_is_special ? (decNumberIsNaN(&dfp.b) ? 4 : 2) : 1; \ dfp.crbf = (atype ^ btype) ? 0x1 : 0x2; \ } else if (expa < expb) { \ dfp.crbf = 0x8; \ } else if (expa > expb) { \ dfp.crbf = 0x4; \ } else { \ dfp.crbf = 0x2; \ } \ \ dfp_set_FPCC_from_CRBF(&dfp); \ return dfp.crbf; \ } DFP_HELPER_TSTEX(dtstex, 64) DFP_HELPER_TSTEX(dtstexq, 128) #define DFP_HELPER_TSTSF(op, size) \ uint32_t helper_##op(CPUPPCState *env, uint64_t *a, uint64_t *b) \ { \ struct PPC_DFP dfp; \ unsigned k; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ k = *a & 0x3F; \ \ if (unlikely(decNumberIsSpecial(&dfp.b))) { \ dfp.crbf = 1; \ } else if (k == 0) { \ dfp.crbf = 4; \ } else if (unlikely(decNumberIsZero(&dfp.b))) { \ /* Zero has no sig digits */ \ dfp.crbf = 4; \ } else { \ unsigned nsd = dfp.b.digits; \ if (k < nsd) { \ dfp.crbf = 8; \ } else if (k > nsd) { \ dfp.crbf = 4; \ } else { \ dfp.crbf = 2; \ } \ } \ \ dfp_set_FPCC_from_CRBF(&dfp); \ return dfp.crbf; \ } DFP_HELPER_TSTSF(dtstsf, 64) DFP_HELPER_TSTSF(dtstsfq, 128) static void QUA_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); dfp_check_for_VXCVI(dfp); } static void dfp_quantize(uint8_t rmc, struct PPC_DFP *dfp) { dfp_set_round_mode_from_immediate(0, rmc, dfp); decNumberQuantize(&dfp->t, &dfp->b, &dfp->a, &dfp->context); if (decNumberIsSNaN(&dfp->a)) { dfp->t = dfp->a; dfp_makeQNaN(&dfp->t); } else if (decNumberIsSNaN(&dfp->b)) { dfp->t = dfp->b; dfp_makeQNaN(&dfp->t); } else if (decNumberIsQNaN(&dfp->a)) { dfp->t = dfp->a; } else if (decNumberIsQNaN(&dfp->b)) { dfp->t = dfp->b; } } #define DFP_HELPER_QUAI(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \ uint32_t te, uint32_t rmc) \ { \ struct PPC_DFP dfp; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ decNumberFromUInt32(&dfp.a, 1); \ dfp.a.exponent = (int32_t)((int8_t)(te << 3) >> 3); \ \ dfp_quantize(rmc, &dfp); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ QUA_PPs(&dfp); \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_QUAI(dquai, 64) DFP_HELPER_QUAI(dquaiq, 128) #define DFP_HELPER_QUA(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \ uint64_t *b, uint32_t rmc) \ { \ struct PPC_DFP dfp; \ \ dfp_prepare_decimal##size(&dfp, a, b, env); \ \ dfp_quantize(rmc, &dfp); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ QUA_PPs(&dfp); \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_QUA(dqua, 64) DFP_HELPER_QUA(dquaq, 128) static void _dfp_reround(uint8_t rmc, int32_t ref_sig, int32_t xmax, struct PPC_DFP *dfp) { int msd_orig, msd_rslt; if (unlikely((ref_sig == 0) || (dfp->b.digits <= ref_sig))) { dfp->t = dfp->b; if (decNumberIsSNaN(&dfp->b)) { dfp_makeQNaN(&dfp->t); dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXSNAN, FPSCR_VE); } return; } /* Reround is equivalent to quantizing b with 1**E(n) where */ /* n = exp(b) + numDigits(b) - reference_significance. */ decNumberFromUInt32(&dfp->a, 1); dfp->a.exponent = dfp->b.exponent + dfp->b.digits - ref_sig; if (unlikely(dfp->a.exponent > xmax)) { dfp->t.digits = 0; dfp->t.bits &= ~DECNEG; dfp_makeQNaN(&dfp->t); dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FPSCR_VE); return; } dfp_quantize(rmc, dfp); msd_orig = dfp_get_digit(&dfp->b, dfp->b.digits-1); msd_rslt = dfp_get_digit(&dfp->t, dfp->t.digits-1); /* If the quantization resulted in rounding up to the next magnitude, */ /* then we need to shift the significand and adjust the exponent. */ if (unlikely((msd_orig == 9) && (msd_rslt == 1))) { decNumber negone; decNumberFromInt32(&negone, -1); decNumberShift(&dfp->t, &dfp->t, &negone, &dfp->context); dfp->t.exponent++; if (unlikely(dfp->t.exponent > xmax)) { dfp_makeQNaN(&dfp->t); dfp->t.digits = 0; dfp_set_FPSCR_flag(dfp, FP_VX | FP_VXCVI, FP_VE); /* Inhibit XX in this case */ decContextClearStatus(&dfp->context, DEC_Inexact); } } } #define DFP_HELPER_RRND(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \ uint64_t *b, uint32_t rmc) \ { \ struct PPC_DFP dfp; \ int32_t ref_sig = *a & 0x3F; \ int32_t xmax = ((size) == 64) ? 369 : 6111; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ _dfp_reround(rmc, ref_sig, xmax, &dfp); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ QUA_PPs(&dfp); \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_RRND(drrnd, 64) DFP_HELPER_RRND(drrndq, 128) #define DFP_HELPER_RINT(op, postprocs, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, \ uint32_t r, uint32_t rmc) \ { \ struct PPC_DFP dfp; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ dfp_set_round_mode_from_immediate(r, rmc, &dfp); \ decNumberToIntegralExact(&dfp.t, &dfp.b, &dfp.context); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \ postprocs(&dfp); \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } static void RINTX_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_XX(dfp); dfp_check_for_VXSNAN(dfp); } DFP_HELPER_RINT(drintx, RINTX_PPs, 64) DFP_HELPER_RINT(drintxq, RINTX_PPs, 128) static void RINTN_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_VXSNAN(dfp); } DFP_HELPER_RINT(drintn, RINTN_PPs, 64) DFP_HELPER_RINT(drintnq, RINTN_PPs, 128) void helper_dctdp(CPUPPCState *env, uint64_t *t, uint64_t *b) { struct PPC_DFP dfp; uint32_t b_short = *b; dfp_prepare_decimal64(&dfp, 0, 0, env); decimal32ToNumber((decimal32 *)&b_short, &dfp.t); decimal64FromNumber((decimal64 *)t, &dfp.t, &dfp.context); dfp_set_FPRF_from_FRT(&dfp); } void helper_dctqpq(CPUPPCState *env, uint64_t *t, uint64_t *b) { struct PPC_DFP dfp; dfp_prepare_decimal128(&dfp, 0, 0, env); decimal64ToNumber((decimal64 *)b, &dfp.t); dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp); dfp_set_FPRF_from_FRT(&dfp); decimal128FromNumber((decimal128 *)&dfp.t64, &dfp.t, &dfp.context); t[0] = dfp.t64[HI_IDX]; t[1] = dfp.t64[LO_IDX]; } void helper_drsp(CPUPPCState *env, uint64_t *t, uint64_t *b) { struct PPC_DFP dfp; uint32_t t_short = 0; dfp_prepare_decimal64(&dfp, 0, b, env); decimal32FromNumber((decimal32 *)&t_short, &dfp.b, &dfp.context); decimal32ToNumber((decimal32 *)&t_short, &dfp.t); dfp_set_FPRF_from_FRT_short(&dfp); dfp_check_for_OX(&dfp); dfp_check_for_UX(&dfp); dfp_check_for_XX(&dfp); *t = t_short; } void helper_drdpq(CPUPPCState *env, uint64_t *t, uint64_t *b) { struct PPC_DFP dfp; dfp_prepare_decimal128(&dfp, 0, b, env); decimal64FromNumber((decimal64 *)&dfp.t64, &dfp.b, &dfp.context); decimal64ToNumber((decimal64 *)&dfp.t64, &dfp.t); dfp_check_for_VXSNAN_and_convert_to_QNaN(&dfp); dfp_set_FPRF_from_FRT_long(&dfp); dfp_check_for_OX(&dfp); dfp_check_for_UX(&dfp); dfp_check_for_XX(&dfp); decimal64FromNumber((decimal64 *)dfp.t64, &dfp.t, &dfp.context); t[0] = dfp.t64[0]; t[1] = 0; } #define DFP_HELPER_CFFIX(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \ { \ struct PPC_DFP dfp; \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ decNumberFromInt64(&dfp.t, (int64_t)(*b)); \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, &dfp.context); \ CFFIX_PPs(&dfp); \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } static void CFFIX_PPs(struct PPC_DFP *dfp) { dfp_set_FPRF_from_FRT(dfp); dfp_check_for_XX(dfp); } DFP_HELPER_CFFIX(dcffix, 64) DFP_HELPER_CFFIX(dcffixq, 128) #define DFP_HELPER_CTFIX(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \ { \ struct PPC_DFP dfp; \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ if (unlikely(decNumberIsSpecial(&dfp.b))) { \ uint64_t invalid_flags = FP_VX | FP_VXCVI; \ if (decNumberIsInfinite(&dfp.b)) { \ dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \ } else { /* NaN */ \ dfp.t64[0] = INT64_MIN; \ if (decNumberIsSNaN(&dfp.b)) { \ invalid_flags |= FP_VXSNAN; \ } \ } \ dfp_set_FPSCR_flag(&dfp, invalid_flags, FP_VE); \ } else if (unlikely(decNumberIsZero(&dfp.b))) { \ dfp.t64[0] = 0; \ } else { \ decNumberToIntegralExact(&dfp.b, &dfp.b, &dfp.context); \ dfp.t64[0] = decNumberIntegralToInt64(&dfp.b, &dfp.context); \ if (decContextTestStatus(&dfp.context, DEC_Invalid_operation)) { \ dfp.t64[0] = decNumberIsNegative(&dfp.b) ? INT64_MIN : INT64_MAX; \ dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FP_VE); \ } else { \ dfp_check_for_XX(&dfp); \ } \ } \ \ *t = dfp.t64[0]; \ } DFP_HELPER_CTFIX(dctfix, 64) DFP_HELPER_CTFIX(dctfixq, 128) static inline void dfp_set_bcd_digit_64(uint64_t *t, uint8_t digit, unsigned n) { *t |= ((uint64_t)(digit & 0xF) << (n << 2)); } static inline void dfp_set_bcd_digit_128(uint64_t *t, uint8_t digit, unsigned n) { t[(n & 0x10) ? HI_IDX : LO_IDX] |= ((uint64_t)(digit & 0xF) << ((n & 15) << 2)); } static inline void dfp_set_sign_64(uint64_t *t, uint8_t sgn) { *t <<= 4; *t |= (sgn & 0xF); } static inline void dfp_set_sign_128(uint64_t *t, uint8_t sgn) { t[HI_IDX] <<= 4; t[HI_IDX] |= (t[LO_IDX] >> 60); t[LO_IDX] <<= 4; t[LO_IDX] |= (sgn & 0xF); } #define DFP_HELPER_DEDPD(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t sp) \ { \ struct PPC_DFP dfp; \ uint8_t digits[34]; \ int i, N; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ decNumberGetBCD(&dfp.b, digits); \ dfp.t64[0] = dfp.t64[1] = 0; \ N = dfp.b.digits; \ \ for (i = 0; (i < N) && (i < (size)/4); i++) { \ dfp_set_bcd_digit_##size(dfp.t64, digits[N-i-1], i); \ } \ \ if (sp & 2) { \ uint8_t sgn; \ \ if (decNumberIsNegative(&dfp.b)) { \ sgn = 0xD; \ } else { \ sgn = ((sp & 1) ? 0xF : 0xC); \ } \ dfp_set_sign_##size(dfp.t64, sgn); \ } \ \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_DEDPD(ddedpd, 64) DFP_HELPER_DEDPD(ddedpdq, 128) static inline uint8_t dfp_get_bcd_digit_64(uint64_t *t, unsigned n) { return *t >> ((n << 2) & 63) & 15; } static inline uint8_t dfp_get_bcd_digit_128(uint64_t *t, unsigned n) { return t[(n & 0x10) ? HI_IDX : LO_IDX] >> ((n << 2) & 63) & 15; } #define DFP_HELPER_ENBCD(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b, uint32_t s) \ { \ struct PPC_DFP dfp; \ uint8_t digits[32]; \ int n = 0, offset = 0, sgn = 0, nonzero = 0; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ decNumberZero(&dfp.t); \ \ if (s) { \ uint8_t sgnNibble = dfp_get_bcd_digit_##size(dfp.b64, offset++); \ switch (sgnNibble) { \ case 0xD: \ case 0xB: \ sgn = 1; \ break; \ case 0xC: \ case 0xF: \ case 0xA: \ case 0xE: \ sgn = 0; \ break; \ default: \ dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \ return; \ } \ } \ \ while (offset < (size)/4) { \ n++; \ digits[(size)/4-n] = dfp_get_bcd_digit_##size(dfp.b64, offset++); \ if (digits[(size)/4-n] > 10) { \ dfp_set_FPSCR_flag(&dfp, FP_VX | FP_VXCVI, FPSCR_VE); \ return; \ } else { \ nonzero |= (digits[(size)/4-n] > 0); \ } \ } \ \ if (nonzero) { \ decNumberSetBCD(&dfp.t, digits+((size)/4)-n, n); \ } \ \ if (s && sgn) { \ dfp.t.bits |= DECNEG; \ } \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ dfp_set_FPRF_from_FRT(&dfp); \ if ((size) == 64) { \ t[0] = dfp.t64[0]; \ } else if ((size) == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_ENBCD(denbcd, 64) DFP_HELPER_ENBCD(denbcdq, 128) #define DFP_HELPER_XEX(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *b) \ { \ struct PPC_DFP dfp; \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ if (unlikely(decNumberIsSpecial(&dfp.b))) { \ if (decNumberIsInfinite(&dfp.b)) { \ *t = -1; \ } else if (decNumberIsSNaN(&dfp.b)) { \ *t = -3; \ } else if (decNumberIsQNaN(&dfp.b)) { \ *t = -2; \ } else { \ assert(0); \ } \ } else { \ if ((size) == 64) { \ *t = dfp.b.exponent + 398; \ } else if ((size) == 128) { \ *t = dfp.b.exponent + 6176; \ } else { \ assert(0); \ } \ } \ } DFP_HELPER_XEX(dxex, 64) DFP_HELPER_XEX(dxexq, 128) static void dfp_set_raw_exp_64(uint64_t *t, uint64_t raw) { *t &= 0x8003ffffffffffffULL; *t |= (raw << (63-13)); } static void dfp_set_raw_exp_128(uint64_t *t, uint64_t raw) { t[HI_IDX] &= 0x80003fffffffffffULL; t[HI_IDX] |= (raw << (63-17)); } #define DFP_HELPER_IEX(op, size) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, uint64_t *b) \ { \ struct PPC_DFP dfp; \ uint64_t raw_qnan, raw_snan, raw_inf, max_exp; \ int bias; \ int64_t exp = *((int64_t *)a); \ \ dfp_prepare_decimal##size(&dfp, 0, b, env); \ \ if ((size) == 64) { \ max_exp = 767; \ raw_qnan = 0x1F00; \ raw_snan = 0x1F80; \ raw_inf = 0x1E00; \ bias = 398; \ } else if ((size) == 128) { \ max_exp = 12287; \ raw_qnan = 0x1f000; \ raw_snan = 0x1f800; \ raw_inf = 0x1e000; \ bias = 6176; \ } else { \ assert(0); \ } \ \ if (unlikely((exp < 0) || (exp > max_exp))) { \ dfp.t64[0] = dfp.b64[0]; \ dfp.t64[1] = dfp.b64[1]; \ if (exp == -1) { \ dfp_set_raw_exp_##size(dfp.t64, raw_inf); \ } else if (exp == -3) { \ dfp_set_raw_exp_##size(dfp.t64, raw_snan); \ } else { \ dfp_set_raw_exp_##size(dfp.t64, raw_qnan); \ } \ } else { \ dfp.t = dfp.b; \ if (unlikely(decNumberIsSpecial(&dfp.t))) { \ dfp.t.bits &= ~DECSPECIAL; \ } \ dfp.t.exponent = exp - bias; \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ } \ if (size == 64) { \ t[0] = dfp.t64[0]; \ } else if (size == 128) { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_IEX(diex, 64) DFP_HELPER_IEX(diexq, 128) static void dfp_clear_lmd_from_g5msb(uint64_t *t) { /* The most significant 5 bits of the PowerPC DFP format combine bits */ /* from the left-most decimal digit (LMD) and the biased exponent. */ /* This routine clears the LMD bits while preserving the exponent */ /* bits. See "Figure 80: Encoding of bits 0:4 of the G field for */ /* Finite Numbers" in the Power ISA for additional details. */ uint64_t g5msb = (*t >> 58) & 0x1F; if ((g5msb >> 3) < 3) { /* LMD in [0-7] ? */ *t &= ~(7ULL << 58); } else { switch (g5msb & 7) { case 0: case 1: g5msb = 0; break; case 2: case 3: g5msb = 0x8; break; case 4: case 5: g5msb = 0x10; break; case 6: g5msb = 0x1E; break; case 7: g5msb = 0x1F; break; } *t &= ~(0x1fULL << 58); *t |= (g5msb << 58); } } #define DFP_HELPER_SHIFT(op, size, shift_left) \ void helper_##op(CPUPPCState *env, uint64_t *t, uint64_t *a, \ uint32_t sh) \ { \ struct PPC_DFP dfp; \ unsigned max_digits = ((size) == 64) ? 16 : 34; \ \ dfp_prepare_decimal##size(&dfp, a, 0, env); \ \ if (sh <= max_digits) { \ \ decNumber shd; \ unsigned special = dfp.a.bits & DECSPECIAL; \ \ if (shift_left) { \ decNumberFromUInt32(&shd, sh); \ } else { \ decNumberFromInt32(&shd, -((int32_t)sh)); \ } \ \ dfp.a.bits &= ~DECSPECIAL; \ decNumberShift(&dfp.t, &dfp.a, &shd, &dfp.context); \ \ dfp.t.bits |= special; \ if (special && (dfp.t.digits >= max_digits)) { \ dfp.t.digits = max_digits - 1; \ } \ \ decimal##size##FromNumber((decimal##size *)dfp.t64, &dfp.t, \ &dfp.context); \ } else { \ if ((size) == 64) { \ dfp.t64[0] = dfp.a64[0] & 0xFFFC000000000000ULL; \ dfp_clear_lmd_from_g5msb(dfp.t64); \ } else { \ dfp.t64[HI_IDX] = dfp.a64[HI_IDX] & \ 0xFFFFC00000000000ULL; \ dfp_clear_lmd_from_g5msb(dfp.t64 + HI_IDX); \ dfp.t64[LO_IDX] = 0; \ } \ } \ \ if ((size) == 64) { \ t[0] = dfp.t64[0]; \ } else { \ t[0] = dfp.t64[HI_IDX]; \ t[1] = dfp.t64[LO_IDX]; \ } \ } DFP_HELPER_SHIFT(dscli, 64, 1) DFP_HELPER_SHIFT(dscliq, 128, 1) DFP_HELPER_SHIFT(dscri, 64, 0) DFP_HELPER_SHIFT(dscriq, 128, 0)