Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
8417 lines (7429 sloc) 226 KB
/*
* Copyright (c) 2008-2016 Stefan Krah. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "mpdecimal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include "basearith.h"
#include "bits.h"
#include "convolute.h"
#include "crt.h"
#include "mpalloc.h"
#include "typearith.h"
#include "umodarith.h"
#ifdef PPRO
#if defined(_MSC_VER)
#include <float.h>
#pragma float_control(precise, on)
#pragma fenv_access(on)
#elif !defined(__OpenBSD__) && !defined(__NetBSD__)
/* C99 */
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
#endif
#endif
/* Disable warning that is part of -Wextra since gcc 7.0. */
#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && __GNUC__ >= 7
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif
#if defined(_MSC_VER)
#define ALWAYS_INLINE __forceinline
#elif defined(LEGACY_COMPILER)
#define ALWAYS_INLINE
#undef inline
#define inline
#else
#ifdef TEST_COVERAGE
#define ALWAYS_INLINE
#else
#define ALWAYS_INLINE inline __attribute__ ((always_inline))
#endif
#endif
#define MPD_NEWTONDIV_CUTOFF 1024L
#define MPD_NEW_STATIC(name, flags, exp, digits, len) \
mpd_uint_t name##_data[MPD_MINALLOC_MAX]; \
mpd_t name = {flags|MPD_STATIC|MPD_STATIC_DATA, exp, digits, \
len, MPD_MINALLOC_MAX, name##_data}
#define MPD_NEW_CONST(name, flags, exp, digits, len, alloc, initval) \
mpd_uint_t name##_data[alloc] = {initval}; \
mpd_t name = {flags|MPD_STATIC|MPD_CONST_DATA, exp, digits, \
len, alloc, name##_data}
#define MPD_NEW_SHARED(name, a) \
mpd_t name = {(a->flags&~MPD_DATAFLAGS)|MPD_STATIC|MPD_SHARED_DATA, \
a->exp, a->digits, a->len, a->alloc, a->data}
static mpd_uint_t data_one[1] = {1};
static mpd_uint_t data_zero[1] = {0};
static const mpd_t one = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_one};
static const mpd_t minus_one = {MPD_NEG|MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1,
data_one};
static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
static inline void _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx,
uint32_t *status);
static void _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a,
mpd_ssize_t exp);
static inline mpd_ssize_t _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size);
static int _mpd_cmp_abs(const mpd_t *a, const mpd_t *b);
static void _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status);
static inline void _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status);
static void _mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a,
const mpd_t *b, uint32_t *status);
static inline void _mpd_qpow_uint(mpd_t *result, const mpd_t *base,
mpd_uint_t exp, uint8_t resultsign,
const mpd_context_t *ctx, uint32_t *status);
static mpd_uint_t mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n);
/******************************************************************************/
/* Version */
/******************************************************************************/
const char *
mpd_version(void)
{
return MPD_VERSION;
}
/******************************************************************************/
/* Performance critical inline functions */
/******************************************************************************/
#ifdef CONFIG_64
/* Digits in a word, primarily useful for the most significant word. */
ALWAYS_INLINE int
mpd_word_digits(mpd_uint_t word)
{
if (word < mpd_pow10[9]) {
if (word < mpd_pow10[4]) {
if (word < mpd_pow10[2]) {
return (word < mpd_pow10[1]) ? 1 : 2;
}
return (word < mpd_pow10[3]) ? 3 : 4;
}
if (word < mpd_pow10[6]) {
return (word < mpd_pow10[5]) ? 5 : 6;
}
if (word < mpd_pow10[8]) {
return (word < mpd_pow10[7]) ? 7 : 8;
}
return 9;
}
if (word < mpd_pow10[14]) {
if (word < mpd_pow10[11]) {
return (word < mpd_pow10[10]) ? 10 : 11;
}
if (word < mpd_pow10[13]) {
return (word < mpd_pow10[12]) ? 12 : 13;
}
return 14;
}
if (word < mpd_pow10[18]) {
if (word < mpd_pow10[16]) {
return (word < mpd_pow10[15]) ? 15 : 16;
}
return (word < mpd_pow10[17]) ? 17 : 18;
}
return (word < mpd_pow10[19]) ? 19 : 20;
}
#else
ALWAYS_INLINE int
mpd_word_digits(mpd_uint_t word)
{
if (word < mpd_pow10[4]) {
if (word < mpd_pow10[2]) {
return (word < mpd_pow10[1]) ? 1 : 2;
}
return (word < mpd_pow10[3]) ? 3 : 4;
}
if (word < mpd_pow10[6]) {
return (word < mpd_pow10[5]) ? 5 : 6;
}
if (word < mpd_pow10[8]) {
return (word < mpd_pow10[7]) ? 7 : 8;
}
return (word < mpd_pow10[9]) ? 9 : 10;
}
#endif
/* Adjusted exponent */
ALWAYS_INLINE mpd_ssize_t
mpd_adjexp(const mpd_t *dec)
{
return (dec->exp + dec->digits) - 1;
}
/* Etiny */
ALWAYS_INLINE mpd_ssize_t
mpd_etiny(const mpd_context_t *ctx)
{
return ctx->emin - (ctx->prec - 1);
}
/* Etop: used for folding down in IEEE clamping */
ALWAYS_INLINE mpd_ssize_t
mpd_etop(const mpd_context_t *ctx)
{
return ctx->emax - (ctx->prec - 1);
}
/* Most significant word */
ALWAYS_INLINE mpd_uint_t
mpd_msword(const mpd_t *dec)
{
assert(dec->len > 0);
return dec->data[dec->len-1];
}
/* Most significant digit of a word */
inline mpd_uint_t
mpd_msd(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
return word / mpd_pow10[n-1];
}
/* Least significant digit of a word */
ALWAYS_INLINE mpd_uint_t
mpd_lsd(mpd_uint_t word)
{
return word % 10;
}
/* Coefficient size needed to store 'digits' */
ALWAYS_INLINE mpd_ssize_t
mpd_digits_to_size(mpd_ssize_t digits)
{
mpd_ssize_t q, r;
_mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
return (r == 0) ? q : q+1;
}
/* Number of digits in the exponent. Not defined for MPD_SSIZE_MIN. */
inline int
mpd_exp_digits(mpd_ssize_t exp)
{
exp = (exp < 0) ? -exp : exp;
return mpd_word_digits(exp);
}
/* Canonical */
ALWAYS_INLINE int
mpd_iscanonical(const mpd_t *dec UNUSED)
{
return 1;
}
/* Finite */
ALWAYS_INLINE int
mpd_isfinite(const mpd_t *dec)
{
return !(dec->flags & MPD_SPECIAL);
}
/* Infinite */
ALWAYS_INLINE int
mpd_isinfinite(const mpd_t *dec)
{
return dec->flags & MPD_INF;
}
/* NaN */
ALWAYS_INLINE int
mpd_isnan(const mpd_t *dec)
{
return dec->flags & (MPD_NAN|MPD_SNAN);
}
/* Negative */
ALWAYS_INLINE int
mpd_isnegative(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* Positive */
ALWAYS_INLINE int
mpd_ispositive(const mpd_t *dec)
{
return !(dec->flags & MPD_NEG);
}
/* qNaN */
ALWAYS_INLINE int
mpd_isqnan(const mpd_t *dec)
{
return dec->flags & MPD_NAN;
}
/* Signed */
ALWAYS_INLINE int
mpd_issigned(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* sNaN */
ALWAYS_INLINE int
mpd_issnan(const mpd_t *dec)
{
return dec->flags & MPD_SNAN;
}
/* Special */
ALWAYS_INLINE int
mpd_isspecial(const mpd_t *dec)
{
return dec->flags & MPD_SPECIAL;
}
/* Zero */
ALWAYS_INLINE int
mpd_iszero(const mpd_t *dec)
{
return !mpd_isspecial(dec) && mpd_msword(dec) == 0;
}
/* Test for zero when specials have been ruled out already */
ALWAYS_INLINE int
mpd_iszerocoeff(const mpd_t *dec)
{
return mpd_msword(dec) == 0;
}
/* Normal */
inline int
mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx)
{
if (mpd_isspecial(dec)) return 0;
if (mpd_iszerocoeff(dec)) return 0;
return mpd_adjexp(dec) >= ctx->emin;
}
/* Subnormal */
inline int
mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx)
{
if (mpd_isspecial(dec)) return 0;
if (mpd_iszerocoeff(dec)) return 0;
return mpd_adjexp(dec) < ctx->emin;
}
/* Odd word */
ALWAYS_INLINE int
mpd_isoddword(mpd_uint_t word)
{
return word & 1;
}
/* Odd coefficient */
ALWAYS_INLINE int
mpd_isoddcoeff(const mpd_t *dec)
{
return mpd_isoddword(dec->data[0]);
}
/* 0 if dec is positive, 1 if dec is negative */
ALWAYS_INLINE uint8_t
mpd_sign(const mpd_t *dec)
{
return dec->flags & MPD_NEG;
}
/* 1 if dec is positive, -1 if dec is negative */
ALWAYS_INLINE int
mpd_arith_sign(const mpd_t *dec)
{
return 1 - 2 * mpd_isnegative(dec);
}
/* Radix */
ALWAYS_INLINE long
mpd_radix(void)
{
return 10;
}
/* Dynamic decimal */
ALWAYS_INLINE int
mpd_isdynamic(const mpd_t *dec)
{
return !(dec->flags & MPD_STATIC);
}
/* Static decimal */
ALWAYS_INLINE int
mpd_isstatic(const mpd_t *dec)
{
return dec->flags & MPD_STATIC;
}
/* Data of decimal is dynamic */
ALWAYS_INLINE int
mpd_isdynamic_data(const mpd_t *dec)
{
return !(dec->flags & MPD_DATAFLAGS);
}
/* Data of decimal is static */
ALWAYS_INLINE int
mpd_isstatic_data(const mpd_t *dec)
{
return dec->flags & MPD_STATIC_DATA;
}
/* Data of decimal is shared */
ALWAYS_INLINE int
mpd_isshared_data(const mpd_t *dec)
{
return dec->flags & MPD_SHARED_DATA;
}
/* Data of decimal is const */
ALWAYS_INLINE int
mpd_isconst_data(const mpd_t *dec)
{
return dec->flags & MPD_CONST_DATA;
}
/******************************************************************************/
/* Inline memory handling */
/******************************************************************************/
/* Fill destination with zeros */
ALWAYS_INLINE void
mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len)
{
mpd_size_t i;
for (i = 0; i < len; i++) {
dest[i] = 0;
}
}
/* Free a decimal */
ALWAYS_INLINE void
mpd_del(mpd_t *dec)
{
if (mpd_isdynamic_data(dec)) {
mpd_free(dec->data);
}
if (mpd_isdynamic(dec)) {
mpd_free(dec);
}
}
/*
* Resize the coefficient. Existing data up to 'nwords' is left untouched.
* Return 1 on success, 0 otherwise.
*
* Input invariant: MPD_MINALLOC <= result->alloc.
*
* Case nwords == result->alloc:
* 'result' is unchanged. Return 1.
*
* Case nwords > result->alloc:
* Case realloc success:
* The value of 'result' does not change. Return 1.
* Case realloc failure:
* 'result' is NaN, status is updated with MPD_Malloc_error. Return 0.
*
* Case nwords < result->alloc:
* Case is_static_data or realloc failure [1]:
* 'result' is unchanged. Return 1.
* Case realloc success:
* The value of result is undefined (expected). Return 1.
*
*
* [1] In that case the old (now oversized) area is still valid.
*/
ALWAYS_INLINE int
mpd_qresize(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
assert(MPD_MINALLOC <= result->alloc);
nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords;
if (nwords == result->alloc) {
return 1;
}
if (mpd_isstatic_data(result)) {
if (nwords > result->alloc) {
return mpd_switch_to_dyn(result, nwords, status);
}
return 1;
}
return mpd_realloc_dyn(result, nwords, status);
}
/* Same as mpd_qresize, but the complete coefficient (including the old
* memory area!) is initialized to zero. */
ALWAYS_INLINE int
mpd_qresize_zero(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
assert(MPD_MINALLOC <= result->alloc);
nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords;
if (nwords != result->alloc) {
if (mpd_isstatic_data(result)) {
if (nwords > result->alloc) {
return mpd_switch_to_dyn_zero(result, nwords, status);
}
}
else if (!mpd_realloc_dyn(result, nwords, status)) {
return 0;
}
}
mpd_uint_zero(result->data, nwords);
return 1;
}
/*
* Reduce memory size for the coefficient to MPD_MINALLOC. In theory,
* realloc may fail even when reducing the memory size. But in that case
* the old memory area is always big enough, so checking for MPD_Malloc_error
* is not imperative.
*/
ALWAYS_INLINE void
mpd_minalloc(mpd_t *result)
{
assert(!mpd_isconst_data(result)); /* illegal operation for a const */
assert(!mpd_isshared_data(result)); /* illegal operation for a shared */
if (!mpd_isstatic_data(result) && result->alloc > MPD_MINALLOC) {
uint8_t err = 0;
result->data = mpd_realloc(result->data, MPD_MINALLOC,
sizeof *result->data, &err);
if (!err) {
result->alloc = MPD_MINALLOC;
}
}
}
int
mpd_resize(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx)
{
uint32_t status = 0;
if (!mpd_qresize(result, nwords, &status)) {
mpd_addstatus_raise(ctx, status);
return 0;
}
return 1;
}
int
mpd_resize_zero(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx)
{
uint32_t status = 0;
if (!mpd_qresize_zero(result, nwords, &status)) {
mpd_addstatus_raise(ctx, status);
return 0;
}
return 1;
}
/******************************************************************************/
/* Set attributes of a decimal */
/******************************************************************************/
/* Set digits. Assumption: result->len is initialized and > 0. */
inline void
mpd_setdigits(mpd_t *result)
{
mpd_ssize_t wdigits = mpd_word_digits(mpd_msword(result));
result->digits = wdigits + (result->len-1) * MPD_RDIGITS;
}
/* Set sign */
ALWAYS_INLINE void
mpd_set_sign(mpd_t *result, uint8_t sign)
{
result->flags &= ~MPD_NEG;
result->flags |= sign;
}
/* Copy sign from another decimal */
ALWAYS_INLINE void
mpd_signcpy(mpd_t *result, const mpd_t *a)
{
uint8_t sign = a->flags&MPD_NEG;
result->flags &= ~MPD_NEG;
result->flags |= sign;
}
/* Set infinity */
ALWAYS_INLINE void
mpd_set_infinity(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_INF;
}
/* Set qNaN */
ALWAYS_INLINE void
mpd_set_qnan(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_NAN;
}
/* Set sNaN */
ALWAYS_INLINE void
mpd_set_snan(mpd_t *result)
{
result->flags &= ~MPD_SPECIAL;
result->flags |= MPD_SNAN;
}
/* Set to negative */
ALWAYS_INLINE void
mpd_set_negative(mpd_t *result)
{
result->flags |= MPD_NEG;
}
/* Set to positive */
ALWAYS_INLINE void
mpd_set_positive(mpd_t *result)
{
result->flags &= ~MPD_NEG;
}
/* Set to dynamic */
ALWAYS_INLINE void
mpd_set_dynamic(mpd_t *result)
{
result->flags &= ~MPD_STATIC;
}
/* Set to static */
ALWAYS_INLINE void
mpd_set_static(mpd_t *result)
{
result->flags |= MPD_STATIC;
}
/* Set data to dynamic */
ALWAYS_INLINE void
mpd_set_dynamic_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
}
/* Set data to static */
ALWAYS_INLINE void
mpd_set_static_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_STATIC_DATA;
}
/* Set data to shared */
ALWAYS_INLINE void
mpd_set_shared_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_SHARED_DATA;
}
/* Set data to const */
ALWAYS_INLINE void
mpd_set_const_data(mpd_t *result)
{
result->flags &= ~MPD_DATAFLAGS;
result->flags |= MPD_CONST_DATA;
}
/* Clear flags, preserving memory attributes. */
ALWAYS_INLINE void
mpd_clear_flags(mpd_t *result)
{
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
}
/* Set flags, preserving memory attributes. */
ALWAYS_INLINE void
mpd_set_flags(mpd_t *result, uint8_t flags)
{
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
result->flags |= flags;
}
/* Copy flags, preserving memory attributes of result. */
ALWAYS_INLINE void
mpd_copy_flags(mpd_t *result, const mpd_t *a)
{
uint8_t aflags = a->flags;
result->flags &= (MPD_STATIC|MPD_DATAFLAGS);
result->flags |= (aflags & ~(MPD_STATIC|MPD_DATAFLAGS));
}
/* Initialize a workcontext from ctx. Set traps, flags and newtrap to 0. */
static inline void
mpd_workcontext(mpd_context_t *workctx, const mpd_context_t *ctx)
{
workctx->prec = ctx->prec;
workctx->emax = ctx->emax;
workctx->emin = ctx->emin;
workctx->round = ctx->round;
workctx->traps = 0;
workctx->status = 0;
workctx->newtrap = 0;
workctx->clamp = ctx->clamp;
workctx->allcr = ctx->allcr;
}
/******************************************************************************/
/* Getting and setting parts of decimals */
/******************************************************************************/
/* Flip the sign of a decimal */
static inline void
_mpd_negate(mpd_t *dec)
{
dec->flags ^= MPD_NEG;
}
/* Set coefficient to zero */
void
mpd_zerocoeff(mpd_t *result)
{
mpd_minalloc(result);
result->digits = 1;
result->len = 1;
result->data[0] = 0;
}
/* Set the coefficient to all nines. */
void
mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
{
mpd_ssize_t len, r;
_mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (!mpd_qresize(result, len, status)) {
return;
}
result->len = len;
result->digits = ctx->prec;
--len;
if (r > 0) {
result->data[len--] = mpd_pow10[r]-1;
}
for (; len >= 0; --len) {
result->data[len] = MPD_RADIX-1;
}
}
/*
* Cut off the most significant digits so that the rest fits in ctx->prec.
* Cannot fail.
*/
static void
_mpd_cap(mpd_t *result, const mpd_context_t *ctx)
{
uint32_t dummy;
mpd_ssize_t len, r;
if (result->len > 0 && result->digits > ctx->prec) {
_mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (r != 0) {
result->data[len-1] %= mpd_pow10[r];
}
len = _mpd_real_size(result->data, len);
/* resize to fewer words cannot fail */
mpd_qresize(result, len, &dummy);
result->len = len;
mpd_setdigits(result);
}
if (mpd_iszero(result)) {
_settriple(result, mpd_sign(result), 0, result->exp);
}
}
/*
* Cut off the most significant digits of a NaN payload so that the rest
* fits in ctx->prec - ctx->clamp. Cannot fail.
*/
static void
_mpd_fix_nan(mpd_t *result, const mpd_context_t *ctx)
{
uint32_t dummy;
mpd_ssize_t prec;
mpd_ssize_t len, r;
prec = ctx->prec - ctx->clamp;
if (result->len > 0 && result->digits > prec) {
if (prec == 0) {
mpd_minalloc(result);
result->len = result->digits = 0;
}
else {
_mpd_idiv_word(&len, &r, prec, MPD_RDIGITS);
len = (r == 0) ? len : len+1;
if (r != 0) {
result->data[len-1] %= mpd_pow10[r];
}
len = _mpd_real_size(result->data, len);
/* resize to fewer words cannot fail */
mpd_qresize(result, len, &dummy);
result->len = len;
mpd_setdigits(result);
if (mpd_iszerocoeff(result)) {
/* NaN0 is not a valid representation */
result->len = result->digits = 0;
}
}
}
}
/*
* Get n most significant digits from a decimal, where 0 < n <= MPD_UINT_DIGITS.
* Assumes MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for 32 and 64 bit
* machines.
*
* The result of the operation will be in lo. If the operation is impossible,
* hi will be nonzero. This is used to indicate an error.
*/
static inline void
_mpd_get_msdigits(mpd_uint_t *hi, mpd_uint_t *lo, const mpd_t *dec,
unsigned int n)
{
mpd_uint_t r, tmp;
assert(0 < n && n <= MPD_RDIGITS+1);
_mpd_div_word(&tmp, &r, dec->digits, MPD_RDIGITS);
r = (r == 0) ? MPD_RDIGITS : r; /* digits in the most significant word */
*hi = 0;
*lo = dec->data[dec->len-1];
if (n <= r) {
*lo /= mpd_pow10[r-n];
}
else if (dec->len > 1) {
/* at this point 1 <= r < n <= MPD_RDIGITS+1 */
_mpd_mul_words(hi, lo, *lo, mpd_pow10[n-r]);
tmp = dec->data[dec->len-2] / mpd_pow10[MPD_RDIGITS-(n-r)];
*lo = *lo + tmp;
if (*lo < tmp) (*hi)++;
}
}
/******************************************************************************/
/* Gathering information about a decimal */
/******************************************************************************/
/* The real size of the coefficient without leading zero words. */
static inline mpd_ssize_t
_mpd_real_size(mpd_uint_t *data, mpd_ssize_t size)
{
while (size > 1 && data[size-1] == 0) {
size--;
}
return size;
}
/* Return number of trailing zeros. No errors are possible. */
mpd_ssize_t
mpd_trail_zeros(const mpd_t *dec)
{
mpd_uint_t word;
mpd_ssize_t i, tz = 0;
for (i=0; i < dec->len; ++i) {
if (dec->data[i] != 0) {
word = dec->data[i];
tz = i * MPD_RDIGITS;
while (word % 10 == 0) {
word /= 10;
tz++;
}
break;
}
}
return tz;
}
/* Integer: Undefined for specials */
static int
_mpd_isint(const mpd_t *dec)
{
mpd_ssize_t tz;
if (mpd_iszerocoeff(dec)) {
return 1;
}
tz = mpd_trail_zeros(dec);
return (dec->exp + tz >= 0);
}
/* Integer */
int
mpd_isinteger(const mpd_t *dec)
{
if (mpd_isspecial(dec)) {
return 0;
}
return _mpd_isint(dec);
}
/* Word is a power of 10 */
static int
mpd_word_ispow10(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
if (word == mpd_pow10[n-1]) {
return 1;
}
return 0;
}
/* Coefficient is a power of 10 */
static int
mpd_coeff_ispow10(const mpd_t *dec)
{
if (mpd_word_ispow10(mpd_msword(dec))) {
if (_mpd_isallzero(dec->data, dec->len-1)) {
return 1;
}
}
return 0;
}
/* All digits of a word are nines */
static int
mpd_word_isallnine(mpd_uint_t word)
{
int n;
n = mpd_word_digits(word);
if (word == mpd_pow10[n]-1) {
return 1;
}
return 0;
}
/* All digits of the coefficient are nines */
static int
mpd_coeff_isallnine(const mpd_t *dec)
{
if (mpd_word_isallnine(mpd_msword(dec))) {
if (_mpd_isallnine(dec->data, dec->len-1)) {
return 1;
}
}
return 0;
}
/* Odd decimal: Undefined for non-integers! */
int
mpd_isodd(const mpd_t *dec)
{
mpd_uint_t q, r;
assert(mpd_isinteger(dec));
if (mpd_iszerocoeff(dec)) return 0;
if (dec->exp < 0) {
_mpd_div_word(&q, &r, -dec->exp, MPD_RDIGITS);
q = dec->data[q] / mpd_pow10[r];
return mpd_isoddword(q);
}
return dec->exp == 0 && mpd_isoddword(dec->data[0]);
}
/* Even: Undefined for non-integers! */
int
mpd_iseven(const mpd_t *dec)
{
return !mpd_isodd(dec);
}
/******************************************************************************/
/* Getting and setting decimals */
/******************************************************************************/
/* Internal function: Set a static decimal from a triple, no error checking. */
static void
_ssettriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
{
mpd_set_flags(result, sign);
result->exp = exp;
_mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
result->len = (result->data[1] == 0) ? 1 : 2;
mpd_setdigits(result);
}
/* Internal function: Set a decimal from a triple, no error checking. */
static void
_settriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp)
{
mpd_minalloc(result);
mpd_set_flags(result, sign);
result->exp = exp;
_mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX);
result->len = (result->data[1] == 0) ? 1 : 2;
mpd_setdigits(result);
}
/* Set a special number from a triple */
void
mpd_setspecial(mpd_t *result, uint8_t sign, uint8_t type)
{
mpd_minalloc(result);
result->flags &= ~(MPD_NEG|MPD_SPECIAL);
result->flags |= (sign|type);
result->exp = result->digits = result->len = 0;
}
/* Set result of NaN with an error status */
void
mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status)
{
mpd_minalloc(result);
mpd_set_qnan(result);
mpd_set_positive(result);
result->exp = result->digits = result->len = 0;
*status |= flags;
}
/* quietly set a static decimal from an mpd_ssize_t */
void
mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_uint_t u;
uint8_t sign = MPD_POS;
if (a < 0) {
if (a == MPD_SSIZE_MIN) {
u = (mpd_uint_t)MPD_SSIZE_MAX +
(-(MPD_SSIZE_MIN+MPD_SSIZE_MAX));
}
else {
u = -a;
}
sign = MPD_NEG;
}
else {
u = a;
}
_ssettriple(result, sign, u, 0);
mpd_qfinalize(result, ctx, status);
}
/* quietly set a static decimal from an mpd_uint_t */
void
mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
uint32_t *status)
{
_ssettriple(result, MPD_POS, a, 0);
mpd_qfinalize(result, ctx, status);
}
/* quietly set a static decimal from an int32_t */
void
mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qsset_ssize(result, a, ctx, status);
}
/* quietly set a static decimal from a uint32_t */
void
mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qsset_uint(result, a, ctx, status);
}
#ifdef CONFIG_64
/* quietly set a static decimal from an int64_t */
void
mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qsset_ssize(result, a, ctx, status);
}
/* quietly set a static decimal from a uint64_t */
void
mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qsset_uint(result, a, ctx, status);
}
#endif
/* quietly set a decimal from an mpd_ssize_t */
void
mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_minalloc(result);
mpd_qsset_ssize(result, a, ctx, status);
}
/* quietly set a decimal from an mpd_uint_t */
void
mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx,
uint32_t *status)
{
_settriple(result, MPD_POS, a, 0);
mpd_qfinalize(result, ctx, status);
}
/* quietly set a decimal from an int32_t */
void
mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qset_ssize(result, a, ctx, status);
}
/* quietly set a decimal from a uint32_t */
void
mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_qset_uint(result, a, ctx, status);
}
#if defined(CONFIG_32) && !defined(LEGACY_COMPILER)
/* set a decimal from a uint64_t */
static void
_c32setu64(mpd_t *result, uint64_t u, uint8_t sign, uint32_t *status)
{
mpd_uint_t w[3];
uint64_t q;
int i, len;
len = 0;
do {
q = u / MPD_RADIX;
w[len] = (mpd_uint_t)(u - q * MPD_RADIX);
u = q; len++;
} while (u != 0);
if (!mpd_qresize(result, len, status)) {
return;
}
for (i = 0; i < len; i++) {
result->data[i] = w[i];
}
mpd_set_sign(result, sign);
result->exp = 0;
result->len = len;
mpd_setdigits(result);
}
static void
_c32_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
_c32setu64(result, a, MPD_POS, status);
mpd_qfinalize(result, ctx, status);
}
/* set a decimal from an int64_t */
static void
_c32_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
uint64_t u;
uint8_t sign = MPD_POS;
if (a < 0) {
if (a == INT64_MIN) {
u = (uint64_t)INT64_MAX + (-(INT64_MIN+INT64_MAX));
}
else {
u = -a;
}
sign = MPD_NEG;
}
else {
u = a;
}
_c32setu64(result, u, sign, status);
mpd_qfinalize(result, ctx, status);
}
#endif /* CONFIG_32 && !LEGACY_COMPILER */
#ifndef LEGACY_COMPILER
/* quietly set a decimal from an int64_t */
void
mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
#ifdef CONFIG_64
mpd_qset_ssize(result, a, ctx, status);
#else
_c32_qset_i64(result, a, ctx, status);
#endif
}
/* quietly set a decimal from a uint64_t */
void
mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx,
uint32_t *status)
{
#ifdef CONFIG_64
mpd_qset_uint(result, a, ctx, status);
#else
_c32_qset_u64(result, a, ctx, status);
#endif
}
#endif /* !LEGACY_COMPILER */
/*
* Quietly get an mpd_uint_t from a decimal. Assumes
* MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for
* 32 and 64 bit machines.
*
* If the operation is impossible, MPD_Invalid_operation is set.
*/
static mpd_uint_t
_mpd_qget_uint(int use_sign, const mpd_t *a, uint32_t *status)
{
mpd_t tmp;
mpd_uint_t tmp_data[2];
mpd_uint_t lo, hi;
if (mpd_isspecial(a)) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
if (mpd_iszero(a)) {
return 0;
}
if (use_sign && mpd_isnegative(a)) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
if (a->digits+a->exp > MPD_RDIGITS+1) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
if (a->exp < 0) {
if (!_mpd_isint(a)) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
/* At this point a->digits+a->exp <= MPD_RDIGITS+1,
* so the shift fits. */
tmp.data = tmp_data;
tmp.flags = MPD_STATIC|MPD_STATIC_DATA;
tmp.alloc = 2;
mpd_qsshiftr(&tmp, a, -a->exp);
tmp.exp = 0;
a = &tmp;
}
_mpd_get_msdigits(&hi, &lo, a, MPD_RDIGITS+1);
if (hi) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
if (a->exp > 0) {
_mpd_mul_words(&hi, &lo, lo, mpd_pow10[a->exp]);
if (hi) {
*status |= MPD_Invalid_operation;
return MPD_UINT_MAX;
}
}
return lo;
}
/*
* Sets Invalid_operation for:
* - specials
* - negative numbers (except negative zero)
* - non-integers
* - overflow
*/
mpd_uint_t
mpd_qget_uint(const mpd_t *a, uint32_t *status)
{
return _mpd_qget_uint(1, a, status);
}
/* Same as above, but gets the absolute value, i.e. the sign is ignored. */
mpd_uint_t
mpd_qabs_uint(const mpd_t *a, uint32_t *status)
{
return _mpd_qget_uint(0, a, status);
}
/* quietly get an mpd_ssize_t from a decimal */
mpd_ssize_t
mpd_qget_ssize(const mpd_t *a, uint32_t *status)
{
mpd_uint_t u;
int isneg;
u = mpd_qabs_uint(a, status);
if (*status&MPD_Invalid_operation) {
return MPD_SSIZE_MAX;
}
isneg = mpd_isnegative(a);
if (u <= MPD_SSIZE_MAX) {
return isneg ? -((mpd_ssize_t)u) : (mpd_ssize_t)u;
}
else if (isneg && u+(MPD_SSIZE_MIN+MPD_SSIZE_MAX) == MPD_SSIZE_MAX) {
return MPD_SSIZE_MIN;
}
*status |= MPD_Invalid_operation;
return MPD_SSIZE_MAX;
}
#if defined(CONFIG_32) && !defined(LEGACY_COMPILER)
/*
* Quietly get a uint64_t from a decimal. If the operation is impossible,
* MPD_Invalid_operation is set.
*/
static uint64_t
_c32_qget_u64(int use_sign, const mpd_t *a, uint32_t *status)
{
MPD_NEW_STATIC(tmp,0,0,20,3);
mpd_context_t maxcontext;
uint64_t ret;
tmp_data[0] = 709551615;
tmp_data[1] = 446744073;
tmp_data[2] = 18;
if (mpd_isspecial(a)) {
*status |= MPD_Invalid_operation;
return UINT64_MAX;
}
if (mpd_iszero(a)) {
return 0;
}
if (use_sign && mpd_isnegative(a)) {
*status |= MPD_Invalid_operation;
return UINT64_MAX;
}
if (!_mpd_isint(a)) {
*status |= MPD_Invalid_operation;
return UINT64_MAX;
}
if (_mpd_cmp_abs(a, &tmp) > 0) {
*status |= MPD_Invalid_operation;
return UINT64_MAX;
}
mpd_maxcontext(&maxcontext);
mpd_qrescale(&tmp, a, 0, &maxcontext, &maxcontext.status);
maxcontext.status &= ~MPD_Rounded;
if (maxcontext.status != 0) {
*status |= (maxcontext.status|MPD_Invalid_operation); /* GCOV_NOT_REACHED */
return UINT64_MAX; /* GCOV_NOT_REACHED */
}
ret = 0;
switch (tmp.len) {
case 3:
ret += (uint64_t)tmp_data[2] * 1000000000000000000ULL;
case 2:
ret += (uint64_t)tmp_data[1] * 1000000000ULL;
case 1:
ret += tmp_data[0];
break;
default:
abort(); /* GCOV_NOT_REACHED */
}
return ret;
}
static int64_t
_c32_qget_i64(const mpd_t *a, uint32_t *status)
{
uint64_t u;
int isneg;
u = _c32_qget_u64(0, a, status);
if (*status&MPD_Invalid_operation) {
return INT64_MAX;
}
isneg = mpd_isnegative(a);
if (u <= INT64_MAX) {
return isneg ? -((int64_t)u) : (int64_t)u;
}
else if (isneg && u+(INT64_MIN+INT64_MAX) == INT64_MAX) {
return INT64_MIN;
}
*status |= MPD_Invalid_operation;
return INT64_MAX;
}
#endif /* CONFIG_32 && !LEGACY_COMPILER */
#ifdef CONFIG_64
/* quietly get a uint64_t from a decimal */
uint64_t
mpd_qget_u64(const mpd_t *a, uint32_t *status)
{
return mpd_qget_uint(a, status);
}
/* quietly get an int64_t from a decimal */
int64_t
mpd_qget_i64(const mpd_t *a, uint32_t *status)
{
return mpd_qget_ssize(a, status);
}
/* quietly get a uint32_t from a decimal */
uint32_t
mpd_qget_u32(const mpd_t *a, uint32_t *status)
{
uint64_t x = mpd_qget_uint(a, status);
if (*status&MPD_Invalid_operation) {
return UINT32_MAX;
}
if (x > UINT32_MAX) {
*status |= MPD_Invalid_operation;
return UINT32_MAX;
}
return (uint32_t)x;
}
/* quietly get an int32_t from a decimal */
int32_t
mpd_qget_i32(const mpd_t *a, uint32_t *status)
{
int64_t x = mpd_qget_ssize(a, status);
if (*status&MPD_Invalid_operation) {
return INT32_MAX;
}
if (x < INT32_MIN || x > INT32_MAX) {
*status |= MPD_Invalid_operation;
return INT32_MAX;
}
return (int32_t)x;
}
#else
#ifndef LEGACY_COMPILER
/* quietly get a uint64_t from a decimal */
uint64_t
mpd_qget_u64(const mpd_t *a, uint32_t *status)
{
return _c32_qget_u64(1, a, status);
}
/* quietly get an int64_t from a decimal */
int64_t
mpd_qget_i64(const mpd_t *a, uint32_t *status)
{
return _c32_qget_i64(a, status);
}
#endif
/* quietly get a uint32_t from a decimal */
uint32_t
mpd_qget_u32(const mpd_t *a, uint32_t *status)
{
return mpd_qget_uint(a, status);
}
/* quietly get an int32_t from a decimal */
int32_t
mpd_qget_i32(const mpd_t *a, uint32_t *status)
{
return mpd_qget_ssize(a, status);
}
#endif
/******************************************************************************/
/* Filtering input of functions, finalizing output of functions */
/******************************************************************************/
/*
* Check if the operand is NaN, copy to result and return 1 if this is
* the case. Copying can fail since NaNs are allowed to have a payload that
* does not fit in MPD_MINALLOC.
*/
int
mpd_qcheck_nan(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
uint32_t *status)
{
if (mpd_isnan(a)) {
*status |= mpd_issnan(a) ? MPD_Invalid_operation : 0;
mpd_qcopy(result, a, status);
mpd_set_qnan(result);
_mpd_fix_nan(result, ctx);
return 1;
}
return 0;
}
/*
* Check if either operand is NaN, copy to result and return 1 if this
* is the case. Copying can fail since NaNs are allowed to have a payload
* that does not fit in MPD_MINALLOC.
*/
int
mpd_qcheck_nans(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
if ((a->flags|b->flags)&(MPD_NAN|MPD_SNAN)) {
const mpd_t *choice = b;
if (mpd_issnan(a)) {
choice = a;
*status |= MPD_Invalid_operation;
}
else if (mpd_issnan(b)) {
*status |= MPD_Invalid_operation;
}
else if (mpd_isqnan(a)) {
choice = a;
}
mpd_qcopy(result, choice, status);
mpd_set_qnan(result);
_mpd_fix_nan(result, ctx);
return 1;
}
return 0;
}
/*
* Check if one of the operands is NaN, copy to result and return 1 if this
* is the case. Copying can fail since NaNs are allowed to have a payload
* that does not fit in MPD_MINALLOC.
*/
static int
mpd_qcheck_3nans(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c,
const mpd_context_t *ctx, uint32_t *status)
{
if ((a->flags|b->flags|c->flags)&(MPD_NAN|MPD_SNAN)) {
const mpd_t *choice = c;
if (mpd_issnan(a)) {
choice = a;
*status |= MPD_Invalid_operation;
}
else if (mpd_issnan(b)) {
choice = b;
*status |= MPD_Invalid_operation;
}
else if (mpd_issnan(c)) {
*status |= MPD_Invalid_operation;
}
else if (mpd_isqnan(a)) {
choice = a;
}
else if (mpd_isqnan(b)) {
choice = b;
}
mpd_qcopy(result, choice, status);
mpd_set_qnan(result);
_mpd_fix_nan(result, ctx);
return 1;
}
return 0;
}
/* Check if rounding digit 'rnd' leads to an increment. */
static inline int
_mpd_rnd_incr(const mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx)
{
int ld;
switch (ctx->round) {
case MPD_ROUND_DOWN: case MPD_ROUND_TRUNC:
return 0;
case MPD_ROUND_HALF_UP:
return (rnd >= 5);
case MPD_ROUND_HALF_EVEN:
return (rnd > 5) || ((rnd == 5) && mpd_isoddcoeff(dec));
case MPD_ROUND_CEILING:
return !(rnd == 0 || mpd_isnegative(dec));
case MPD_ROUND_FLOOR:
return !(rnd == 0 || mpd_ispositive(dec));
case MPD_ROUND_HALF_DOWN:
return (rnd > 5);
case MPD_ROUND_UP:
return !(rnd == 0);
case MPD_ROUND_05UP:
ld = (int)mpd_lsd(dec->data[0]);
return (!(rnd == 0) && (ld == 0 || ld == 5));
default:
/* Without a valid context, further results will be undefined. */
return 0; /* GCOV_NOT_REACHED */
}
}
/*
* Apply rounding to a decimal that has been right-shifted into a full
* precision decimal. If an increment leads to an overflow of the precision,
* adjust the coefficient and the exponent and check the new exponent for
* overflow.
*/
static inline void
_mpd_apply_round(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
uint32_t *status)
{
if (_mpd_rnd_incr(dec, rnd, ctx)) {
/* We have a number with exactly ctx->prec digits. The increment
* can only lead to an overflow if the decimal is all nines. In
* that case, the result is a power of ten with prec+1 digits.
*
* If the precision is a multiple of MPD_RDIGITS, this situation is
* detected by _mpd_baseincr returning a carry.
* If the precision is not a multiple of MPD_RDIGITS, we have to
* check if the result has one digit too many.
*/
mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
if (carry) {
dec->data[dec->len-1] = mpd_pow10[MPD_RDIGITS-1];
dec->exp += 1;
_mpd_check_exp(dec, ctx, status);
return;
}
mpd_setdigits(dec);
if (dec->digits > ctx->prec) {
mpd_qshiftr_inplace(dec, 1);
dec->exp += 1;
dec->digits = ctx->prec;
_mpd_check_exp(dec, ctx, status);
}
}
}
/*
* Apply rounding to a decimal. Allow overflow of the precision.
*/
static inline void
_mpd_apply_round_excess(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
uint32_t *status)
{
if (_mpd_rnd_incr(dec, rnd, ctx)) {
mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
if (carry) {
if (!mpd_qresize(dec, dec->len+1, status)) {
return;
}
dec->data[dec->len] = 1;
dec->len += 1;
}
mpd_setdigits(dec);
}
}
/*
* Apply rounding to a decimal that has been right-shifted into a decimal
* with full precision or less. Return failure if an increment would
* overflow the precision.
*/
static inline int
_mpd_apply_round_fit(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx,
uint32_t *status)
{
if (_mpd_rnd_incr(dec, rnd, ctx)) {
mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len);
if (carry) {
if (!mpd_qresize(dec, dec->len+1, status)) {
return 0;
}
dec->data[dec->len] = 1;
dec->len += 1;
}
mpd_setdigits(dec);
if (dec->digits > ctx->prec) {
mpd_seterror(dec, MPD_Invalid_operation, status);
return 0;
}
}
return 1;
}
/* Check a normal number for overflow, underflow, clamping. If the operand
is modified, it will be zero, special or (sub)normal with a coefficient
that fits into the current context precision. */
static inline void
_mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
{
mpd_ssize_t adjexp, etiny, shift;
int rnd;
adjexp = mpd_adjexp(dec);
if (adjexp > ctx->emax) {
if (mpd_iszerocoeff(dec)) {
dec->exp = ctx->emax;
if (ctx->clamp) {
dec->exp -= (ctx->prec-1);
}
mpd_zerocoeff(dec);
*status |= MPD_Clamped;
return;
}
switch (ctx->round) {
case MPD_ROUND_HALF_UP: case MPD_ROUND_HALF_EVEN:
case MPD_ROUND_HALF_DOWN: case MPD_ROUND_UP:
case MPD_ROUND_TRUNC:
mpd_setspecial(dec, mpd_sign(dec), MPD_INF);
break;
case MPD_ROUND_DOWN: case MPD_ROUND_05UP:
mpd_qmaxcoeff(dec, ctx, status);
dec->exp = ctx->emax - ctx->prec + 1;
break;
case MPD_ROUND_CEILING:
if (mpd_isnegative(dec)) {
mpd_qmaxcoeff(dec, ctx, status);
dec->exp = ctx->emax - ctx->prec + 1;
}
else {
mpd_setspecial(dec, MPD_POS, MPD_INF);
}
break;
case MPD_ROUND_FLOOR:
if (mpd_ispositive(dec)) {
mpd_qmaxcoeff(dec, ctx, status);
dec->exp = ctx->emax - ctx->prec + 1;
}
else {
mpd_setspecial(dec, MPD_NEG, MPD_INF);
}
break;
default: /* debug */
abort(); /* GCOV_NOT_REACHED */
}
*status |= MPD_Overflow|MPD_Inexact|MPD_Rounded;
} /* fold down */
else if (ctx->clamp && dec->exp > mpd_etop(ctx)) {
/* At this point adjexp=exp+digits-1 <= emax and exp > etop=emax-prec+1:
* (1) shift = exp -emax+prec-1 > 0
* (2) digits+shift = exp+digits-1 - emax + prec <= prec */
shift = dec->exp - mpd_etop(ctx);
if (!mpd_qshiftl(dec, dec, shift, status)) {
return;
}
dec->exp -= shift;
*status |= MPD_Clamped;
if (!mpd_iszerocoeff(dec) && adjexp < ctx->emin) {
/* Underflow is impossible, since exp < etiny=emin-prec+1
* and exp > etop=emax-prec+1 would imply emax < emin. */
*status |= MPD_Subnormal;
}
}
else if (adjexp < ctx->emin) {
etiny = mpd_etiny(ctx);
if (mpd_iszerocoeff(dec)) {
if (dec->exp < etiny) {
dec->exp = etiny;
mpd_zerocoeff(dec);
*status |= MPD_Clamped;
}
return;
}
*status |= MPD_Subnormal;
if (dec->exp < etiny) {
/* At this point adjexp=exp+digits-1 < emin and exp < etiny=emin-prec+1:
* (1) shift = emin-prec+1 - exp > 0
* (2) digits-shift = exp+digits-1 - emin + prec < prec */
shift = etiny - dec->exp;
rnd = (int)mpd_qshiftr_inplace(dec, shift);
dec->exp = etiny;
/* We always have a spare digit in case of an increment. */
_mpd_apply_round_excess(dec, rnd, ctx, status);
*status |= MPD_Rounded;
if (rnd) {
*status |= (MPD_Inexact|MPD_Underflow);
if (mpd_iszerocoeff(dec)) {
mpd_zerocoeff(dec);
*status |= MPD_Clamped;
}
}
}
/* Case exp >= etiny=emin-prec+1:
* (1) adjexp=exp+digits-1 < emin
* (2) digits < emin-exp+1 <= prec */
}
}
/* Transcendental functions do not always set Underflow reliably,
* since they only use as much precision as is necessary for correct
* rounding. If a result like 1.0000000000e-101 is finalized, there
* is no rounding digit that would trigger Underflow. But we can
* assume Inexact, so a short check suffices. */
static inline void
mpd_check_underflow(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
{
if (mpd_adjexp(dec) < ctx->emin && !mpd_iszero(dec) &&
dec->exp < mpd_etiny(ctx)) {
*status |= MPD_Underflow;
}
}
/* Check if a normal number must be rounded after the exponent has been checked. */
static inline void
_mpd_check_round(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status)
{
mpd_uint_t rnd;
mpd_ssize_t shift;
/* must handle specials: _mpd_check_exp() can produce infinities or NaNs */
if (mpd_isspecial(dec)) {
return;
}
if (dec->digits > ctx->prec) {
shift = dec->digits - ctx->prec;
rnd = mpd_qshiftr_inplace(dec, shift);
dec->exp += shift;
_mpd_apply_round(dec, rnd, ctx, status);
*status |= MPD_Rounded;
if (rnd) {
*status |= MPD_Inexact;
}
}
}
/* Finalize all operations. */
void
mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status)
{
if (mpd_isspecial(result)) {
if (mpd_isnan(result)) {
_mpd_fix_nan(result, ctx);
}
return;
}
_mpd_check_exp(result, ctx, status);
_mpd_check_round(result, ctx, status);
}
/******************************************************************************/
/* Copying */
/******************************************************************************/
/* Internal function: Copy a decimal, share data with src: USE WITH CARE! */
static inline void
_mpd_copy_shared(mpd_t *dest, const mpd_t *src)
{
dest->flags = src->flags;
dest->exp = src->exp;
dest->digits = src->digits;
dest->len = src->len;
dest->alloc = src->alloc;
dest->data = src->data;
mpd_set_shared_data(dest);
}
/*
* Copy a decimal. In case of an error, status is set to MPD_Malloc_error.
*/
int
mpd_qcopy(mpd_t *result, const mpd_t *a, uint32_t *status)
{
if (result == a) return 1;
if (!mpd_qresize(result, a->len, status)) {
return 0;
}
mpd_copy_flags(result, a);
result->exp = a->exp;
result->digits = a->digits;
result->len = a->len;
memcpy(result->data, a->data, a->len * (sizeof *result->data));
return 1;
}
/*
* Copy to a decimal with a static buffer. The caller has to make sure that
* the buffer is big enough. Cannot fail.
*/
static void
mpd_qcopy_static(mpd_t *result, const mpd_t *a)
{
if (result == a) return;
memcpy(result->data, a->data, a->len * (sizeof *result->data));
mpd_copy_flags(result, a);
result->exp = a->exp;
result->digits = a->digits;
result->len = a->len;
}
/*
* Return a newly allocated copy of the operand. In case of an error,
* status is set to MPD_Malloc_error and the return value is NULL.
*/
mpd_t *
mpd_qncopy(const mpd_t *a)
{
mpd_t *result;
if ((result = mpd_qnew_size(a->len)) == NULL) {
return NULL;
}
memcpy(result->data, a->data, a->len * (sizeof *result->data));
mpd_copy_flags(result, a);
result->exp = a->exp;
result->digits = a->digits;
result->len = a->len;
return result;
}
/*
* Copy a decimal and set the sign to positive. In case of an error, the
* status is set to MPD_Malloc_error.
*/
int
mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status)
{
if (!mpd_qcopy(result, a, status)) {
return 0;
}
mpd_set_positive(result);
return 1;
}
/*
* Copy a decimal and negate the sign. In case of an error, the
* status is set to MPD_Malloc_error.
*/
int
mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status)
{
if (!mpd_qcopy(result, a, status)) {
return 0;
}
_mpd_negate(result);
return 1;
}
/*
* Copy a decimal, setting the sign of the first operand to the sign of the
* second operand. In case of an error, the status is set to MPD_Malloc_error.
*/
int
mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status)
{
uint8_t sign_b = mpd_sign(b); /* result may equal b! */
if (!mpd_qcopy(result, a, status)) {
return 0;
}
mpd_set_sign(result, sign_b);
return 1;
}
/******************************************************************************/
/* Comparisons */
/******************************************************************************/
/*
* For all functions that compare two operands and return an int the usual
* convention applies to the return value:
*
* -1 if op1 < op2
* 0 if op1 == op2
* 1 if op1 > op2
*
* INT_MAX for error
*/
/* Convenience macro. If a and b are not equal, return from the calling
* function with the correct comparison value. */
#define CMP_EQUAL_OR_RETURN(a, b) \
if (a != b) { \
if (a < b) { \
return -1; \
} \
return 1; \
}
/*
* Compare the data of big and small. This function does the equivalent
* of first shifting small to the left and then comparing the data of
* big and small, except that no allocation for the left shift is needed.
*/
static int
_mpd_basecmp(mpd_uint_t *big, mpd_uint_t *small, mpd_size_t n, mpd_size_t m,
mpd_size_t shift)
{
#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
/* spurious uninitialized warnings */
mpd_uint_t l=l, lprev=lprev, h=h;
#else
mpd_uint_t l, lprev, h;
#endif
mpd_uint_t q, r;
mpd_uint_t ph, x;
assert(m > 0 && n >= m && shift > 0);
_mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS);
if (r != 0) {
ph = mpd_pow10[r];
--m; --n;
_mpd_divmod_pow10(&h, &lprev, small[m--], MPD_RDIGITS-r);
if (h != 0) {
CMP_EQUAL_OR_RETURN(big[n], h)
--n;
}
for (; m != MPD_SIZE_MAX; m--,n--) {
_mpd_divmod_pow10(&h, &l, small[m], MPD_RDIGITS-r);
x = ph * lprev + h;
CMP_EQUAL_OR_RETURN(big[n], x)
lprev = l;
}
x = ph * lprev;
CMP_EQUAL_OR_RETURN(big[q], x)
}
else {
while (--m != MPD_SIZE_MAX) {
CMP_EQUAL_OR_RETURN(big[m+q], small[m])
}
}
return !_mpd_isallzero(big, q);
}
/* Compare two decimals with the same adjusted exponent. */
static int
_mpd_cmp_same_adjexp(const mpd_t *a, const mpd_t *b)
{
mpd_ssize_t shift, i;
if (a->exp != b->exp) {
/* Cannot wrap: a->exp + a->digits = b->exp + b->digits, so
* a->exp - b->exp = b->digits - a->digits. */
shift = a->exp - b->exp;
if (shift > 0) {
return -1 * _mpd_basecmp(b->data, a->data, b->len, a->len, shift);
}
else {
return _mpd_basecmp(a->data, b->data, a->len, b->len, -shift);
}
}
/*
* At this point adjexp(a) == adjexp(b) and a->exp == b->exp,
* so a->digits == b->digits, therefore a->len == b->len.
*/
for (i = a->len-1; i >= 0; --i) {
CMP_EQUAL_OR_RETURN(a->data[i], b->data[i])
}
return 0;
}
/* Compare two numerical values. */
static int
_mpd_cmp(const mpd_t *a, const mpd_t *b)
{
mpd_ssize_t adjexp_a, adjexp_b;
/* equal pointers */
if (a == b) {
return 0;
}
/* infinities */
if (mpd_isinfinite(a)) {
if (mpd_isinfinite(b)) {
return mpd_isnegative(b) - mpd_isnegative(a);
}
return mpd_arith_sign(a);
}
if (mpd_isinfinite(b)) {
return -mpd_arith_sign(b);
}
/* zeros */
if (mpd_iszerocoeff(a)) {
if (mpd_iszerocoeff(b)) {
return 0;
}
return -mpd_arith_sign(b);
}
if (mpd_iszerocoeff(b)) {
return mpd_arith_sign(a);
}
/* different signs */
if (mpd_sign(a) != mpd_sign(b)) {
return mpd_sign(b) - mpd_sign(a);
}
/* different adjusted exponents */
adjexp_a = mpd_adjexp(a);
adjexp_b = mpd_adjexp(b);
if (adjexp_a != adjexp_b) {
if (adjexp_a < adjexp_b) {
return -1 * mpd_arith_sign(a);
}
return mpd_arith_sign(a);
}
/* same adjusted exponents */
return _mpd_cmp_same_adjexp(a, b) * mpd_arith_sign(a);
}
/* Compare the absolutes of two numerical values. */
static int
_mpd_cmp_abs(const mpd_t *a, const mpd_t *b)
{
mpd_ssize_t adjexp_a, adjexp_b;
/* equal pointers */
if (a == b) {
return 0;
}
/* infinities */
if (mpd_isinfinite(a)) {
if (mpd_isinfinite(b)) {
return 0;
}
return 1;
}
if (mpd_isinfinite(b)) {
return -1;
}
/* zeros */
if (mpd_iszerocoeff(a)) {
if (mpd_iszerocoeff(b)) {
return 0;
}
return -1;
}
if (mpd_iszerocoeff(b)) {
return 1;
}
/* different adjusted exponents */
adjexp_a = mpd_adjexp(a);
adjexp_b = mpd_adjexp(b);
if (adjexp_a != adjexp_b) {
if (adjexp_a < adjexp_b) {
return -1;
}
return 1;
}
/* same adjusted exponents */
return _mpd_cmp_same_adjexp(a, b);
}
/* Compare two values and return an integer result. */
int
mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status)
{
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_isnan(a) || mpd_isnan(b)) {
*status |= MPD_Invalid_operation;
return INT_MAX;
}
}
return _mpd_cmp(a, b);
}
/*
* Compare a and b, convert the usual integer result to a decimal and
* store it in 'result'. For convenience, the integer result of the comparison
* is returned. Comparisons involving NaNs return NaN/INT_MAX.
*/
int
mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
int c;
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return INT_MAX;
}
}
c = _mpd_cmp(a, b);
_settriple(result, (c < 0), (c != 0), 0);
return c;
}
/* Same as mpd_compare(), but signal for all NaNs, i.e. also for quiet NaNs. */
int
mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
int c;
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
*status |= MPD_Invalid_operation;
return INT_MAX;
}
}
c = _mpd_cmp(a, b);
_settriple(result, (c < 0), (c != 0), 0);
return c;
}
/* Compare the operands using a total order. */
int
mpd_cmp_total(const mpd_t *a, const mpd_t *b)
{
mpd_t aa, bb;
int nan_a, nan_b;
int c;
if (mpd_sign(a) != mpd_sign(b)) {
return mpd_sign(b) - mpd_sign(a);
}
if (mpd_isnan(a)) {
c = 1;
if (mpd_isnan(b)) {
nan_a = (mpd_isqnan(a)) ? 1 : 0;
nan_b = (mpd_isqnan(b)) ? 1 : 0;
if (nan_b == nan_a) {
if (a->len > 0 && b->len > 0) {
_mpd_copy_shared(&aa, a);
_mpd_copy_shared(&bb, b);
aa.exp = bb.exp = 0;
/* compare payload */
c = _mpd_cmp_abs(&aa, &bb);
}
else {
c = (a->len > 0) - (b->len > 0);
}
}
else {
c = nan_a - nan_b;
}
}
}
else if (mpd_isnan(b)) {
c = -1;
}
else {
c = _mpd_cmp_abs(a, b);
if (c == 0 && a->exp != b->exp) {
c = (a->exp < b->exp) ? -1 : 1;
}
}
return c * mpd_arith_sign(a);
}
/*
* Compare a and b according to a total order, convert the usual integer result
* to a decimal and store it in 'result'. For convenience, the integer result
* of the comparison is returned.
*/
int
mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b)
{
int c;
c = mpd_cmp_total(a, b);
_settriple(result, (c < 0), (c != 0), 0);
return c;
}
/* Compare the magnitude of the operands using a total order. */
int
mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b)
{
mpd_t aa, bb;
_mpd_copy_shared(&aa, a);
_mpd_copy_shared(&bb, b);
mpd_set_positive(&aa);
mpd_set_positive(&bb);
return mpd_cmp_total(&aa, &bb);
}
/*
* Compare the magnitude of a and b according to a total order, convert the
* the usual integer result to a decimal and store it in 'result'.
* For convenience, the integer result of the comparison is returned.
*/
int
mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b)
{
int c;
c = mpd_cmp_total_mag(a, b);
_settriple(result, (c < 0), (c != 0), 0);
return c;
}
/* Determine an ordering for operands that are numerically equal. */
static inline int
_mpd_cmp_numequal(const mpd_t *a, const mpd_t *b)
{
int sign_a, sign_b;
int c;
sign_a = mpd_sign(a);
sign_b = mpd_sign(b);
if (sign_a != sign_b) {
c = sign_b - sign_a;
}
else {
c = (a->exp < b->exp) ? -1 : 1;
c *= mpd_arith_sign(a);
}
return c;
}
/******************************************************************************/
/* Shifting the coefficient */
/******************************************************************************/
/*
* Shift the coefficient of the operand to the left, no check for specials.
* Both operands may be the same pointer. If the result length has to be
* increased, mpd_qresize() might fail with MPD_Malloc_error.
*/
int
mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
{
mpd_ssize_t size;
assert(!mpd_isspecial(a));
assert(n >= 0);
if (mpd_iszerocoeff(a) || n == 0) {
return mpd_qcopy(result, a, status);
}
size = mpd_digits_to_size(a->digits+n);
if (!mpd_qresize(result, size, status)) {
return 0; /* result is NaN */
}
_mpd_baseshiftl(result->data, a->data, size, a->len, n);
mpd_copy_flags(result, a);
result->exp = a->exp;
result->digits = a->digits+n;
result->len = size;
return 1;
}
/* Determine the rounding indicator if all digits of the coefficient are shifted
* out of the picture. */
static mpd_uint_t
_mpd_get_rnd(const mpd_uint_t *data, mpd_ssize_t len, int use_msd)
{
mpd_uint_t rnd = 0, rest = 0, word;
word = data[len-1];
/* special treatment for the most significant digit if shift == digits */
if (use_msd) {
_mpd_divmod_pow10(&rnd, &rest, word, mpd_word_digits(word)-1);
if (len > 1 && rest == 0) {
rest = !_mpd_isallzero(data, len-1);
}
}
else {
rest = !_mpd_isallzero(data, len);
}
return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd;
}
/*
* Same as mpd_qshiftr(), but 'result' is an mpd_t with a static coefficient.
* It is the caller's responsibility to ensure that the coefficient is big
* enough. The function cannot fail.
*/
static mpd_uint_t
mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n)
{
mpd_uint_t rnd;
mpd_ssize_t size;
assert(!mpd_isspecial(a));
assert(n >= 0);
if (mpd_iszerocoeff(a) || n == 0) {
mpd_qcopy_static(result, a);
return 0;
}
if (n >= a->digits) {
rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
mpd_zerocoeff(result);
}
else {
result->digits = a->digits-n;
size = mpd_digits_to_size(result->digits);
rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
result->len = size;
}
mpd_copy_flags(result, a);
result->exp = a->exp;
return rnd;
}
/*
* Inplace shift of the coefficient to the right, no check for specials.
* Returns the rounding indicator for mpd_rnd_incr().
* The function cannot fail.
*/
mpd_uint_t
mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n)
{
uint32_t dummy;
mpd_uint_t rnd;
mpd_ssize_t size;
assert(!mpd_isspecial(result));
assert(n >= 0);
if (mpd_iszerocoeff(result) || n == 0) {
return 0;
}
if (n >= result->digits) {
rnd = _mpd_get_rnd(result->data, result->len, (n==result->digits));
mpd_zerocoeff(result);
}
else {
rnd = _mpd_baseshiftr(result->data, result->data, result->len, n);
result->digits -= n;
size = mpd_digits_to_size(result->digits);
/* reducing the size cannot fail */
mpd_qresize(result, size, &dummy);
result->len = size;
}
return rnd;
}
/*
* Shift the coefficient of the operand to the right, no check for specials.
* Both operands may be the same pointer. Returns the rounding indicator to
* be used by mpd_rnd_incr(). If the result length has to be increased,
* mpd_qcopy() or mpd_qresize() might fail with MPD_Malloc_error. In those
* cases, MPD_UINT_MAX is returned.
*/
mpd_uint_t
mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status)
{
mpd_uint_t rnd;
mpd_ssize_t size;
assert(!mpd_isspecial(a));
assert(n >= 0);
if (mpd_iszerocoeff(a) || n == 0) {
if (!mpd_qcopy(result, a, status)) {
return MPD_UINT_MAX;
}
return 0;
}
if (n >= a->digits) {
rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits));
mpd_zerocoeff(result);
}
else {
result->digits = a->digits-n;
size = mpd_digits_to_size(result->digits);
if (result == a) {
rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
/* reducing the size cannot fail */
mpd_qresize(result, size, status);
}
else {
if (!mpd_qresize(result, size, status)) {
return MPD_UINT_MAX;
}
rnd = _mpd_baseshiftr(result->data, a->data, a->len, n);
}
result->len = size;
}
mpd_copy_flags(result, a);
result->exp = a->exp;
return rnd;
}
/******************************************************************************/
/* Miscellaneous operations */
/******************************************************************************/
/* Logical And */
void
mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
const mpd_t *big = a, *small = b;
mpd_uint_t x, y, z, xbit, ybit;
int k, mswdigits;
mpd_ssize_t i;
if (mpd_isspecial(a) || mpd_isspecial(b) ||
mpd_isnegative(a) || mpd_isnegative(b) ||
a->exp != 0 || b->exp != 0) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (b->digits > a->digits) {
big = b;
small = a;
}
if (!mpd_qresize(result, big->len, status)) {
return;
}
/* full words */
for (i = 0; i < small->len-1; i++) {
x = small->data[i];
y = big->data[i];
z = 0;
for (k = 0; k < MPD_RDIGITS; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit&ybit) ? mpd_pow10[k] : 0;
}
result->data[i] = z;
}
/* most significant word of small */
x = small->data[i];
y = big->data[i];
z = 0;
mswdigits = mpd_word_digits(x);
for (k = 0; k < mswdigits; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit&ybit) ? mpd_pow10[k] : 0;
}
result->data[i++] = z;
/* scan the rest of y for digits > 1 */
for (; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
}
/* scan the rest of big for digits > 1 */
for (; i < big->len; i++) {
y = big->data[i];
for (k = 0; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
}
}
mpd_clear_flags(result);
result->exp = 0;
result->len = _mpd_real_size(result->data, small->len);
mpd_qresize(result, result->len, status);
mpd_setdigits(result);
_mpd_cap(result, ctx);
return;
invalid_operation:
mpd_seterror(result, MPD_Invalid_operation, status);
}
/* Class of an operand. Returns a pointer to the constant name. */
const char *
mpd_class(const mpd_t *a, const mpd_context_t *ctx)
{
if (mpd_isnan(a)) {
if (mpd_isqnan(a))
return "NaN";
else
return "sNaN";
}
else if (mpd_ispositive(a)) {
if (mpd_isinfinite(a))
return "+Infinity";
else if (mpd_iszero(a))
return "+Zero";
else if (mpd_isnormal(a, ctx))
return "+Normal";
else
return "+Subnormal";
}
else {
if (mpd_isinfinite(a))
return "-Infinity";
else if (mpd_iszero(a))
return "-Zero";
else if (mpd_isnormal(a, ctx))
return "-Normal";
else
return "-Subnormal";
}
}
/* Logical Xor */
void
mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
uint32_t *status)
{
mpd_uint_t x, z, xbit;
mpd_ssize_t i, digits, len;
mpd_ssize_t q, r;
int k;
if (mpd_isspecial(a) || mpd_isnegative(a) || a->exp != 0) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
digits = (a->digits < ctx->prec) ? ctx->prec : a->digits;
_mpd_idiv_word(&q, &r, digits, MPD_RDIGITS);
len = (r == 0) ? q : q+1;
if (!mpd_qresize(result, len, status)) {
return;
}
for (i = 0; i < len; i++) {
x = (i < a->len) ? a->data[i] : 0;
z = 0;
for (k = 0; k < MPD_RDIGITS; k++) {
xbit = x % 10;
x /= 10;
if (xbit > 1) {
goto invalid_operation;
}
z += !xbit ? mpd_pow10[k] : 0;
}
result->data[i] = z;
}
mpd_clear_flags(result);
result->exp = 0;
result->len = _mpd_real_size(result->data, len);
mpd_qresize(result, result->len, status);
mpd_setdigits(result);
_mpd_cap(result, ctx);
return;
invalid_operation:
mpd_seterror(result, MPD_Invalid_operation, status);
}
/* Exponent of the magnitude of the most significant digit of the operand. */
void
mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
uint32_t *status)
{
if (mpd_isspecial(a)) {
if (mpd_qcheck_nan(result, a, ctx, status)) {
return;
}
mpd_setspecial(result, MPD_POS, MPD_INF);
}
else if (mpd_iszerocoeff(a)) {
mpd_setspecial(result, MPD_NEG, MPD_INF);
*status |= MPD_Division_by_zero;
}
else {
mpd_qset_ssize(result, mpd_adjexp(a), ctx, status);
}
}
/* Logical Or */
void
mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
const mpd_t *big = a, *small = b;
mpd_uint_t x, y, z, xbit, ybit;
int k, mswdigits;
mpd_ssize_t i;
if (mpd_isspecial(a) || mpd_isspecial(b) ||
mpd_isnegative(a) || mpd_isnegative(b) ||
a->exp != 0 || b->exp != 0) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (b->digits > a->digits) {
big = b;
small = a;
}
if (!mpd_qresize(result, big->len, status)) {
return;
}
/* full words */
for (i = 0; i < small->len-1; i++) {
x = small->data[i];
y = big->data[i];
z = 0;
for (k = 0; k < MPD_RDIGITS; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit|ybit) ? mpd_pow10[k] : 0;
}
result->data[i] = z;
}
/* most significant word of small */
x = small->data[i];
y = big->data[i];
z = 0;
mswdigits = mpd_word_digits(x);
for (k = 0; k < mswdigits; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit|ybit) ? mpd_pow10[k] : 0;
}
/* scan for digits > 1 and copy the rest of y */
for (; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
z += ybit*mpd_pow10[k];
}
result->data[i++] = z;
/* scan for digits > 1 and copy the rest of big */
for (; i < big->len; i++) {
y = big->data[i];
for (k = 0; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
}
result->data[i] = big->data[i];
}
mpd_clear_flags(result);
result->exp = 0;
result->len = _mpd_real_size(result->data, big->len);
mpd_qresize(result, result->len, status);
mpd_setdigits(result);
_mpd_cap(result, ctx);
return;
invalid_operation:
mpd_seterror(result, MPD_Invalid_operation, status);
}
/*
* Rotate the coefficient of 'a' by 'b' digits. 'b' must be an integer with
* exponent 0.
*/
void
mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
uint32_t workstatus = 0;
MPD_NEW_STATIC(tmp,0,0,0,0);
MPD_NEW_STATIC(big,0,0,0,0);
MPD_NEW_STATIC(small,0,0,0,0);
mpd_ssize_t n, lshift, rshift;
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return;
}
}
if (b->exp != 0 || mpd_isinfinite(b)) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
n = mpd_qget_ssize(b, &workstatus);
if (workstatus&MPD_Invalid_operation) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (n > ctx->prec || n < -ctx->prec) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (mpd_isinfinite(a)) {
mpd_qcopy(result, a, status);
return;
}
if (n >= 0) {
lshift = n;
rshift = ctx->prec-n;
}
else {
lshift = ctx->prec+n;
rshift = -n;
}
if (a->digits > ctx->prec) {
if (!mpd_qcopy(&tmp, a, status)) {
mpd_seterror(result, MPD_Malloc_error, status);
goto finish;
}
_mpd_cap(&tmp, ctx);
a = &tmp;
}
if (!mpd_qshiftl(&big, a, lshift, status)) {
mpd_seterror(result, MPD_Malloc_error, status);
goto finish;
}
_mpd_cap(&big, ctx);
if (mpd_qshiftr(&small, a, rshift, status) == MPD_UINT_MAX) {
mpd_seterror(result, MPD_Malloc_error, status);
goto finish;
}
_mpd_qadd(result, &big, &small, ctx, status);
finish:
mpd_del(&tmp);
mpd_del(&big);
mpd_del(&small);
}
/*
* b must be an integer with exponent 0 and in the range +-2*(emax + prec).
* XXX: In my opinion +-(2*emax + prec) would be more sensible.
* The result is a with the value of b added to its exponent.
*/
void
mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
uint32_t workstatus = 0;
mpd_uint_t n, maxjump;
#ifndef LEGACY_COMPILER
int64_t exp;
#else
mpd_uint_t x;
int x_sign, n_sign;
mpd_ssize_t exp;
#endif
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return;
}
}
if (b->exp != 0 || mpd_isinfinite(b)) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
n = mpd_qabs_uint(b, &workstatus);
/* the spec demands this */
maxjump = 2 * (mpd_uint_t)(ctx->emax + ctx->prec);
if (n > maxjump || workstatus&MPD_Invalid_operation) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (mpd_isinfinite(a)) {
mpd_qcopy(result, a, status);
return;
}
#ifndef LEGACY_COMPILER
exp = a->exp + (int64_t)n * mpd_arith_sign(b);
exp = (exp > MPD_EXP_INF) ? MPD_EXP_INF : exp;
exp = (exp < MPD_EXP_CLAMP) ? MPD_EXP_CLAMP : exp;
#else
x = (a->exp < 0) ? -a->exp : a->exp;
x_sign = (a->exp < 0) ? 1 : 0;
n_sign = mpd_isnegative(b) ? 1 : 0;
if (x_sign == n_sign) {
x = x + n;
if (x < n) x = MPD_UINT_MAX;
}
else {
x_sign = (x >= n) ? x_sign : n_sign;
x = (x >= n) ? x - n : n - x;
}
if (!x_sign && x > MPD_EXP_INF) x = MPD_EXP_INF;
if (x_sign && x > -MPD_EXP_CLAMP) x = -MPD_EXP_CLAMP;
exp = x_sign ? -((mpd_ssize_t)x) : (mpd_ssize_t)x;
#endif
mpd_qcopy(result, a, status);
result->exp = (mpd_ssize_t)exp;
mpd_qfinalize(result, ctx, status);
}
/*
* Shift the coefficient by n digits, positive n is a left shift. In the case
* of a left shift, the result is decapitated to fit the context precision. If
* you don't want that, use mpd_shiftl().
*/
void
mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx,
uint32_t *status)
{
if (mpd_isspecial(a)) {
if (mpd_qcheck_nan(result, a, ctx, status)) {
return;
}
mpd_qcopy(result, a, status);
return;
}
if (n >= 0 && n <= ctx->prec) {
mpd_qshiftl(result, a, n, status);
_mpd_cap(result, ctx);
}
else if (n < 0 && n >= -ctx->prec) {
if (!mpd_qcopy(result, a, status)) {
return;
}
_mpd_cap(result, ctx);
mpd_qshiftr_inplace(result, -n);
}
else {
mpd_seterror(result, MPD_Invalid_operation, status);
}
}
/*
* Same as mpd_shiftn(), but the shift is specified by the decimal b, which
* must be an integer with a zero exponent. Infinities remain infinities.
*/
void
mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx,
uint32_t *status)
{
uint32_t workstatus = 0;
mpd_ssize_t n;
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return;
}
}
if (b->exp != 0 || mpd_isinfinite(b)) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
n = mpd_qget_ssize(b, &workstatus);
if (workstatus&MPD_Invalid_operation) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (n > ctx->prec || n < -ctx->prec) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (mpd_isinfinite(a)) {
mpd_qcopy(result, a, status);
return;
}
if (n >= 0) {
mpd_qshiftl(result, a, n, status);
_mpd_cap(result, ctx);
}
else {
if (!mpd_qcopy(result, a, status)) {
return;
}
_mpd_cap(result, ctx);
mpd_qshiftr_inplace(result, -n);
}
}
/* Logical Xor */
void
mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
const mpd_t *big = a, *small = b;
mpd_uint_t x, y, z, xbit, ybit;
int k, mswdigits;
mpd_ssize_t i;
if (mpd_isspecial(a) || mpd_isspecial(b) ||
mpd_isnegative(a) || mpd_isnegative(b) ||
a->exp != 0 || b->exp != 0) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
if (b->digits > a->digits) {
big = b;
small = a;
}
if (!mpd_qresize(result, big->len, status)) {
return;
}
/* full words */
for (i = 0; i < small->len-1; i++) {
x = small->data[i];
y = big->data[i];
z = 0;
for (k = 0; k < MPD_RDIGITS; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit^ybit) ? mpd_pow10[k] : 0;
}
result->data[i] = z;
}
/* most significant word of small */
x = small->data[i];
y = big->data[i];
z = 0;
mswdigits = mpd_word_digits(x);
for (k = 0; k < mswdigits; k++) {
xbit = x % 10;
x /= 10;
ybit = y % 10;
y /= 10;
if (xbit > 1 || ybit > 1) {
goto invalid_operation;
}
z += (xbit^ybit) ? mpd_pow10[k] : 0;
}
/* scan for digits > 1 and copy the rest of y */
for (; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
z += ybit*mpd_pow10[k];
}
result->data[i++] = z;
/* scan for digits > 1 and copy the rest of big */
for (; i < big->len; i++) {
y = big->data[i];
for (k = 0; k < MPD_RDIGITS; k++) {
ybit = y % 10;
y /= 10;
if (ybit > 1) {
goto invalid_operation;
}
}
result->data[i] = big->data[i];
}
mpd_clear_flags(result);
result->exp = 0;
result->len = _mpd_real_size(result->data, big->len);
mpd_qresize(result, result->len, status);
mpd_setdigits(result);
_mpd_cap(result, ctx);
return;
invalid_operation:
mpd_seterror(result, MPD_Invalid_operation, status);
}
/******************************************************************************/
/* Arithmetic operations */
/******************************************************************************/
/*
* The absolute value of a. If a is negative, the result is the same
* as the result of the minus operation. Otherwise, the result is the
* result of the plus operation.
*/
void
mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx,
uint32_t *status)
{
if (mpd_isspecial(a)) {
if (mpd_qcheck_nan(result, a, ctx, status)) {
return;
}
}
if (mpd_isnegative(a)) {
mpd_qminus(result, a, ctx, status);
}
else {
mpd_qplus(result, a, ctx, status);
}
}
static inline void
_mpd_ptrswap(const mpd_t **a, const mpd_t **b)
{
const mpd_t *t = *a;
*a = *b;
*b = t;
}
/* Add or subtract infinities. */
static void
_mpd_qaddsub_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
uint32_t *status)
{
if (mpd_isinfinite(a)) {
if (mpd_sign(a) != sign_b && mpd_isinfinite(b)) {
mpd_seterror(result, MPD_Invalid_operation, status);
}
else {
mpd_setspecial(result, mpd_sign(a), MPD_INF);
}
return;
}
assert(mpd_isinfinite(b));
mpd_setspecial(result, sign_b, MPD_INF);
}
/* Add or subtract non-special numbers. */
static void
_mpd_qaddsub(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b,
const mpd_context_t *ctx, uint32_t *status)
{
const mpd_t *big, *small;
MPD_NEW_STATIC(big_aligned,0,0,0,0);
MPD_NEW_CONST(tiny,0,0,1,1,1,1);
mpd_uint_t carry;
mpd_ssize_t newsize, shift;
mpd_ssize_t exp, i;
int swap = 0;
/* compare exponents */
big = a; small = b;
if (big->exp != small->exp) {
if (small->exp > big->exp) {
_mpd_ptrswap(&big, &small);
swap++;
}
/* align the coefficients */
if (!mpd_iszerocoeff(big)) {
exp = big->exp - 1;
exp += (big->digits > ctx->prec) ? 0 : big->digits-ctx->prec-1;
if (mpd_adjexp(small) < exp) {
/*
* Avoid huge shifts by substituting a value for small that is
* guaranteed to produce the same results.
*
* adjexp(small) < exp if and only if:
*
* bdigits <= prec AND
* bdigits+shift >= prec+2+sdigits AND
* exp = bexp+bdigits-prec-2
*
* 1234567000000000 -> bdigits + shift
* ----------XX1234 -> sdigits
* ----------X1 -> tiny-digits
* |- prec -|
*
* OR
*
* bdigits > prec AND
* shift > sdigits AND
* exp = bexp-1
*
* 1234567892100000 -> bdigits + shift
* ----------XX1234 -> sdigits
* ----------X1 -> tiny-digits
* |- prec -|
*
* If tiny is zero, adding or subtracting is a no-op.
* Otherwise, adding tiny generates a non-zero digit either
* below the rounding digit or the least significant digit
* of big. When subtracting, tiny is in the same position as
* the carry that would be generated by subtracting sdigits.
*/
mpd_copy_flags(&tiny, small);
tiny.exp = exp;
tiny.digits = 1;
tiny.len = 1;
tiny.data[0] = mpd_iszerocoeff(small) ? 0 : 1;
small = &tiny;
}
/* This cannot wrap: the difference is positive and <= maxprec */
shift = big->exp - small->exp;
if (!mpd_qshiftl(&big_aligned, big, shift, status)) {
mpd_seterror(result, MPD_Malloc_error, status);
goto finish;
}
big = &big_aligned;
}
}
result->exp = small->exp;
/* compare length of coefficients */
if (big->len < small->len) {
_mpd_ptrswap(&big, &small);
swap++;
}
newsize = big->len;
if (!mpd_qresize(result, newsize, status)) {
goto finish;
}
if (mpd_sign(a) == sign_b) {
carry = _mpd_baseadd(result->data, big->data, small->data,
big->len, small->len);
if (carry) {
newsize = big->len + 1;
if (!mpd_qresize(result, newsize, status)) {
goto finish;
}
result->data[newsize-1] = carry;
}
result->len = newsize;
mpd_set_flags(result, sign_b);
}
else {
if (big->len == small->len) {
for (i=big->len-1; i >= 0; --i) {
if (big->data[i] != small->data[i]) {
if (big->data[i] < small->data[i]) {
_mpd_ptrswap(&big, &small);
swap++;
}
break;
}
}
}
_mpd_basesub(result->data, big->data, small->data,
big->len, small->len);
newsize = _mpd_real_size(result->data, big->len);
/* resize to smaller cannot fail */
(void)mpd_qresize(result, newsize, status);
result->len = newsize;
sign_b = (swap & 1) ? sign_b : mpd_sign(a);
mpd_set_flags(result, sign_b);
if (mpd_iszerocoeff(result)) {
mpd_set_positive(result);
if (ctx->round == MPD_ROUND_FLOOR) {
mpd_set_negative(result);
}
}
}
mpd_setdigits(result);
finish:
mpd_del(&big_aligned);
}
/* Add a and b. No specials, no finalizing. */
static void
_mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
_mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
}
/* Subtract b from a. No specials, no finalizing. */
static void
_mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
_mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
}
/* Add a and b. */
void
mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return;
}
_mpd_qaddsub_inf(result, a, b, mpd_sign(b), status);
return;
}
_mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status);
mpd_qfinalize(result, ctx, status);
}
/* Add a and b. Set NaN/Invalid_operation if the result is inexact. */
static void
_mpd_qadd_exact(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
uint32_t workstatus = 0;
mpd_qadd(result, a, b, ctx, &workstatus);
*status |= workstatus;
if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) {
mpd_seterror(result, MPD_Invalid_operation, status);
}
}
/* Subtract b from a. */
void
mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(result, a, b, ctx, status)) {
return;
}
_mpd_qaddsub_inf(result, a, b, !mpd_sign(b), status);
return;
}
_mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status);
mpd_qfinalize(result, ctx, status);
}
/* Subtract b from a. Set NaN/Invalid_operation if the result is inexact. */
static void
_mpd_qsub_exact(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
uint32_t workstatus = 0;
mpd_qsub(result, a, b, ctx, &workstatus);
*status |= workstatus;
if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) {
mpd_seterror(result, MPD_Invalid_operation, status);
}
}
/* Add decimal and mpd_ssize_t. */
void
mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qsset_ssize(&bb, b, &maxcontext, status);
mpd_qadd(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Add decimal and mpd_uint_t. */
void
mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qsset_uint(&bb, b, &maxcontext, status);
mpd_qadd(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Subtract mpd_ssize_t from decimal. */
void
mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qsset_ssize(&bb, b, &maxcontext, status);
mpd_qsub(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Subtract mpd_uint_t from decimal. */
void
mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qsset_uint(&bb, b, &maxcontext, status);
mpd_qsub(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Add decimal and int32_t. */
void
mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qadd_ssize(result, a, b, ctx, status);
}
/* Add decimal and uint32_t. */
void
mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qadd_uint(result, a, b, ctx, status);
}
#ifdef CONFIG_64
/* Add decimal and int64_t. */
void
mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qadd_ssize(result, a, b, ctx, status);
}
/* Add decimal and uint64_t. */
void
mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qadd_uint(result, a, b, ctx, status);
}
#elif !defined(LEGACY_COMPILER)
/* Add decimal and int64_t. */
void
mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qset_i64(&bb, b, &maxcontext, status);
mpd_qadd(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Add decimal and uint64_t. */
void
mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qset_u64(&bb, b, &maxcontext, status);
mpd_qadd(result, a, &bb, ctx, status);
mpd_del(&bb);
}
#endif
/* Subtract int32_t from decimal. */
void
mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qsub_ssize(result, a, b, ctx, status);
}
/* Subtract uint32_t from decimal. */
void
mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qsub_uint(result, a, b, ctx, status);
}
#ifdef CONFIG_64
/* Subtract int64_t from decimal. */
void
mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qsub_ssize(result, a, b, ctx, status);
}
/* Subtract uint64_t from decimal. */
void
mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_qsub_uint(result, a, b, ctx, status);
}
#elif !defined(LEGACY_COMPILER)
/* Subtract int64_t from decimal. */
void
mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qset_i64(&bb, b, &maxcontext, status);
mpd_qsub(result, a, &bb, ctx, status);
mpd_del(&bb);
}
/* Subtract uint64_t from decimal. */
void
mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b,
const mpd_context_t *ctx, uint32_t *status)
{
mpd_context_t maxcontext;
MPD_NEW_STATIC(bb,0,0,0,0);
mpd_maxcontext(&maxcontext);
mpd_qset_u64(&bb, b, &maxcontext, status);
mpd_qsub(result, a, &bb, ctx, status);
mpd_del(&bb);
}
#endif
/* Divide infinities. */
static void
_mpd_qdiv_inf(mpd_t *result, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
if (mpd_isinfinite(a)) {
if (mpd_isinfinite(b)) {
mpd_seterror(result, MPD_Invalid_operation, status);
return;
}
mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF);
return;
}
assert(mpd_isinfinite(b));
_settriple(result, mpd_sign(a)^mpd_sign(b), 0, mpd_etiny(ctx));
*status |= MPD_Clamped;
}
enum {NO_IDEAL_EXP, SET_IDEAL_EXP};
/* Divide a by b. */
static void
_mpd_qdiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
MPD_NEW_STATIC(aligned,0,0,0,0);
mpd_uint_t ld;
mpd_ssize_t shift, exp, tz;
mpd_ssize_t newsize;
mpd_ssize_t ideal_exp;
mpd_uint_t rem;
uint8_t sign_a = mpd_sign(a);
uint8_t sign_b = mpd_sign(b);
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(q, a, b, ctx, status)) {
return;
}
_mpd_qdiv_inf(q, a, b, ctx, status);
return;
}
if (mpd_iszerocoeff(b)) {
if (mpd_iszerocoeff(a)) {
mpd_seterror(q, MPD_Division_undefined, status);
}
else {
mpd_setspecial(q, sign_a^sign_b, MPD_INF);
*status |= MPD_Division_by_zero;
}
return;
}
if (mpd_iszerocoeff(a)) {
exp = a->exp - b->exp;
_settriple(q, sign_a^sign_b, 0, exp);
mpd_qfinalize(q, ctx, status);
return;
}
shift = (b->digits - a->digits) + ctx->prec + 1;
ideal_exp = a->exp - b->exp;
exp = ideal_exp - shift;
if (shift > 0) {
if (!mpd_qshiftl(&aligned, a, shift, status)) {
mpd_seterror(q, MPD_Malloc_error, status);
goto finish;
}
a = &aligned;
}
else if (shift < 0) {
shift = -shift;
if (!mpd_qshiftl(&aligned, b, shift, status)) {
mpd_seterror(q, MPD_Malloc_error, status);
goto finish;
}
b = &aligned;
}
newsize = a->len - b->len + 1;
if ((q != b && q != a) || (q == b && newsize > b->len)) {
if (!mpd_qresize(q, newsize, status)) {
mpd_seterror(q, MPD_Malloc_error, status);
goto finish;
}
}
if (b->len == 1) {
rem = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
}
else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
int ret = _mpd_basedivmod(q->data, NULL, a->data, b->data,
a->len, b->len);
if (ret < 0) {
mpd_seterror(q, MPD_Malloc_error, status);
goto finish;
}
rem = ret;
}
else {
MPD_NEW_STATIC(r,0,0,0,0);
_mpd_base_ndivmod(q, &r, a, b, status);
if (mpd_isspecial(q) || mpd_isspecial(&r)) {
mpd_setspecial(q, MPD_POS, MPD_NAN);
mpd_del(&r);
goto finish;
}
rem = !mpd_iszerocoeff(&r);
mpd_del(&r);
newsize = q->len;
}
newsize = _mpd_real_size(q->data, newsize);
/* resize to smaller cannot fail */
mpd_qresize(q, newsize, status);
mpd_set_flags(q, sign_a^sign_b);
q->len = newsize;
mpd_setdigits(q);
shift = ideal_exp - exp;
if (rem) {
ld = mpd_lsd(q->data[0]);
if (ld == 0 || ld == 5) {
q->data[0] += 1;
}
}
else if (action == SET_IDEAL_EXP && shift > 0) {
tz = mpd_trail_zeros(q);
shift = (tz > shift) ? shift : tz;
mpd_qshiftr_inplace(q, shift);
exp += shift;
}
q->exp = exp;
finish:
mpd_del(&aligned);
mpd_qfinalize(q, ctx, status);
}
/* Divide a by b. */
void
mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
_mpd_qdiv(SET_IDEAL_EXP, q, a, b, ctx, status);
}
/* Internal function. */
static void
_mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
MPD_NEW_STATIC(aligned,0,0,0,0);
mpd_ssize_t qsize, rsize;
mpd_ssize_t ideal_exp, expdiff, shift;
uint8_t sign_a = mpd_sign(a);
uint8_t sign_ab = mpd_sign(a)^mpd_sign(b);
ideal_exp = (a->exp > b->exp) ? b->exp : a->exp;
if (mpd_iszerocoeff(a)) {
if (!mpd_qcopy(r, a, status)) {
goto nanresult; /* GCOV_NOT_REACHED */
}
r->exp = ideal_exp;
_settriple(q, sign_ab, 0, 0);
return;
}
expdiff = mpd_adjexp(a) - mpd_adjexp(b);
if (expdiff < 0) {
if (a->exp > b->exp) {
/* positive and less than b->digits - a->digits */
shift = a->exp - b->exp;
if (!mpd_qshiftl(r, a, shift, status)) {
goto nanresult;
}
r->exp = ideal_exp;
}
else {
if (!mpd_qcopy(r, a, status)) {
goto nanresult;
}
}
_settriple(q, sign_ab, 0, 0);
return;
}
if (expdiff > ctx->prec) {
*status |= MPD_Division_impossible;
goto nanresult;
}
/*
* At this point we have:
* (1) 0 <= a->exp + a->digits - b->exp - b->digits <= prec
* (2) a->exp - b->exp >= b->digits - a->digits
* (3) a->exp - b->exp <= prec + b->digits - a->digits
*/
if (a->exp != b->exp) {
shift = a->exp - b->exp;
if (shift > 0) {
/* by (3), after the shift a->digits <= prec + b->digits */
if (!mpd_qshiftl(&aligned, a, shift, status)) {
goto nanresult;
}
a = &aligned;
}
else {
shift = -shift;
/* by (2), after the shift b->digits <= a->digits */
if (!mpd_qshiftl(&aligned, b, shift, status)) {
goto nanresult;
}
b = &aligned;
}
}
qsize = a->len - b->len + 1;
if (!(q == a && qsize < a->len) && !(q == b && qsize < b->len)) {
if (!mpd_qresize(q, qsize, status)) {
goto nanresult;
}
}
rsize = b->len;
if (!(r == a && rsize < a->len)) {
if (!mpd_qresize(r, rsize, status)) {
goto nanresult;
}
}
if (b->len == 1) {
if (a->len == 1) {
_mpd_div_word(&q->data[0], &r->data[0], a->data[0], b->data[0]);
}
else {
r->data[0] = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]);
}
}
else if (b->len <= MPD_NEWTONDIV_CUTOFF) {
int ret;
ret = _mpd_basedivmod(q->data, r->data, a->data, b->data,
a->len, b->len);
if (ret == -1) {
*status |= MPD_Malloc_error;
goto nanresult;
}
}
else {
_mpd_base_ndivmod(q, r, a, b, status);
if (mpd_isspecial(q) || mpd_isspecial(r)) {
goto nanresult;
}
qsize = q->len;
rsize = r->len;
}
qsize = _mpd_real_size(q->data, qsize);
/* resize to smaller cannot fail */
mpd_qresize(q, qsize, status);
q->len = qsize;
mpd_setdigits(q);
mpd_set_flags(q, sign_ab);
q->exp = 0;
if (q->digits > ctx->prec) {
*status |= MPD_Division_impossible;
goto nanresult;
}
rsize = _mpd_real_size(r->data, rsize);
/* resize to smaller cannot fail */
mpd_qresize(r, rsize, status);
r->len = rsize;
mpd_setdigits(r);
mpd_set_flags(r, sign_a);
r->exp = ideal_exp;
out:
mpd_del(&aligned);
return;
nanresult:
mpd_setspecial(q, MPD_POS, MPD_NAN);
mpd_setspecial(r, MPD_POS, MPD_NAN);
goto out;
}
/* Integer division with remainder. */
void
mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
uint8_t sign = mpd_sign(a)^mpd_sign(b);
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(q, a, b, ctx, status)) {
mpd_qcopy(r, q, status);
return;
}
if (mpd_isinfinite(a)) {
if (mpd_isinfinite(b)) {
mpd_setspecial(q, MPD_POS, MPD_NAN);
}
else {
mpd_setspecial(q, sign, MPD_INF);
}
mpd_setspecial(r, MPD_POS, MPD_NAN);
*status |= MPD_Invalid_operation;
return;
}
if (mpd_isinfinite(b)) {
if (!mpd_qcopy(r, a, status)) {
mpd_seterror(q, MPD_Malloc_error, status);
return;
}
mpd_qfinalize(r, ctx, status);
_settriple(q, sign, 0, 0);
return;
}
/* debug */
abort(); /* GCOV_NOT_REACHED */
}
if (mpd_iszerocoeff(b)) {
if (mpd_iszerocoeff(a)) {
mpd_setspecial(q, MPD_POS, MPD_NAN);
mpd_setspecial(r, MPD_POS, MPD_NAN);
*status |= MPD_Division_undefined;
}
else {
mpd_setspecial(q, sign, MPD_INF);
mpd_setspecial(r, MPD_POS, MPD_NAN);
*status |= (MPD_Division_by_zero|MPD_Invalid_operation);
}
return;
}
_mpd_qdivmod(q, r, a, b, ctx, status);
mpd_qfinalize(q, ctx, status);
mpd_qfinalize(r, ctx, status);
}
void
mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b,
const mpd_context_t *ctx, uint32_t *status)
{
MPD_NEW_STATIC(r,0,0,0,0);
uint8_t sign = mpd_sign(a)^mpd_sign(b);
if (mpd_isspecial(a) || mpd_isspecial(b)) {
if (mpd_qcheck_nans(q, a, b, ctx, status)) {
return;
}
if (mpd_isinfinite(a) && mpd_isinfinite(b)) {
mpd_seterror(q, MPD_Invalid_operation, status);
return;
}
if (mpd_isinfinite(a)) {
mpd_setspecial(q, sign, MPD_INF);
return;
}
if (mpd_isinfinite(b)) {
_settriple(q, sign, 0, 0);
return;
}
/* debug */
abort(); /* GCOV_NOT_REACHED */
}
if (mpd_iszerocoeff(b)) {
if (mpd_iszerocoeff(a)) {
mpd_seterror(q, MPD_Division_undefined, status);
}
else {
mpd_setspecial(q, sign, MPD_INF);
*status |= MPD_Division_by_zero;
}
return;
}
_mpd_qdivmod(q, &r, a, b, ctx, status);
mpd_del(&r);
mpd_qfinalize(q, ctx, status);
}