diff options
| author | orivej <[email protected]> | 2022-02-10 16:44:49 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:49 +0300 |
| commit | 718c552901d703c502ccbefdfc3c9028d608b947 (patch) | |
| tree | 46534a98bbefcd7b1f3faa5b52c138ab27db75b7 /contrib/tools/python3/src/Modules/_decimal | |
| parent | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Modules/_decimal')
32 files changed, 22155 insertions, 22155 deletions
diff --git a/contrib/tools/python3/src/Modules/_decimal/_decimal.c b/contrib/tools/python3/src/Modules/_decimal/_decimal.c index d80d7915507..44dc4b1b6cd 100644 --- a/contrib/tools/python3/src/Modules/_decimal/_decimal.c +++ b/contrib/tools/python3/src/Modules/_decimal/_decimal.c @@ -1,130 +1,130 @@ -/* - * Copyright (c) 2008-2012 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 <Python.h> -#include "longintrepr.h" -#include "complexobject.h" -#include "mpdecimal.h" - -#include <stdlib.h> - -#include "docstrings.h" - - +/* + * Copyright (c) 2008-2012 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 <Python.h> +#include "longintrepr.h" +#include "complexobject.h" +#include "mpdecimal.h" + +#include <stdlib.h> + +#include "docstrings.h" + + #if !defined(MPD_VERSION_HEX) || MPD_VERSION_HEX < 0x02050000 #error "libmpdec version >= 2.5.0 required" -#endif - - -/* - * Type sizes with assertions in mpdecimal.h and pyport.h: - * sizeof(size_t) == sizeof(Py_ssize_t) - * sizeof(size_t) == sizeof(mpd_uint_t) == sizeof(mpd_ssize_t) - */ - -#ifdef TEST_COVERAGE - #undef Py_LOCAL_INLINE - #define Py_LOCAL_INLINE Py_LOCAL -#endif - -#define MPD_Float_operation MPD_Not_implemented - -#define BOUNDS_CHECK(x, MIN, MAX) x = (x < MIN || MAX < x) ? MAX : x - +#endif + + +/* + * Type sizes with assertions in mpdecimal.h and pyport.h: + * sizeof(size_t) == sizeof(Py_ssize_t) + * sizeof(size_t) == sizeof(mpd_uint_t) == sizeof(mpd_ssize_t) + */ + +#ifdef TEST_COVERAGE + #undef Py_LOCAL_INLINE + #define Py_LOCAL_INLINE Py_LOCAL +#endif + +#define MPD_Float_operation MPD_Not_implemented + +#define BOUNDS_CHECK(x, MIN, MAX) x = (x < MIN || MAX < x) ? MAX : x + #if defined(__GNUC__) && !defined(__INTEL_COMPILER) #define UNUSED __attribute__((unused)) #else #define UNUSED #endif - -/* _Py_DEC_MINALLOC >= MPD_MINALLOC */ -#define _Py_DEC_MINALLOC 4 - -typedef struct { - PyObject_HEAD - Py_hash_t hash; - mpd_t dec; - mpd_uint_t data[_Py_DEC_MINALLOC]; -} PyDecObject; - -typedef struct { - PyObject_HEAD - uint32_t *flags; -} PyDecSignalDictObject; - -typedef struct { - PyObject_HEAD - mpd_context_t ctx; - PyObject *traps; - PyObject *flags; - int capitals; - PyThreadState *tstate; -} PyDecContextObject; - -typedef struct { - PyObject_HEAD - PyObject *local; - PyObject *global; -} PyDecContextManagerObject; - - -#undef MPD -#undef CTX -static PyTypeObject PyDec_Type; -static PyTypeObject *PyDecSignalDict_Type; -static PyTypeObject PyDecContext_Type; -static PyTypeObject PyDecContextManager_Type; + +/* _Py_DEC_MINALLOC >= MPD_MINALLOC */ +#define _Py_DEC_MINALLOC 4 + +typedef struct { + PyObject_HEAD + Py_hash_t hash; + mpd_t dec; + mpd_uint_t data[_Py_DEC_MINALLOC]; +} PyDecObject; + +typedef struct { + PyObject_HEAD + uint32_t *flags; +} PyDecSignalDictObject; + +typedef struct { + PyObject_HEAD + mpd_context_t ctx; + PyObject *traps; + PyObject *flags; + int capitals; + PyThreadState *tstate; +} PyDecContextObject; + +typedef struct { + PyObject_HEAD + PyObject *local; + PyObject *global; +} PyDecContextManagerObject; + + +#undef MPD +#undef CTX +static PyTypeObject PyDec_Type; +static PyTypeObject *PyDecSignalDict_Type; +static PyTypeObject PyDecContext_Type; +static PyTypeObject PyDecContextManager_Type; #define PyDec_CheckExact(v) Py_IS_TYPE(v, &PyDec_Type) -#define PyDec_Check(v) PyObject_TypeCheck(v, &PyDec_Type) +#define PyDec_Check(v) PyObject_TypeCheck(v, &PyDec_Type) #define PyDecSignalDict_Check(v) Py_IS_TYPE(v, PyDecSignalDict_Type) -#define PyDecContext_Check(v) PyObject_TypeCheck(v, &PyDecContext_Type) -#define MPD(v) (&((PyDecObject *)v)->dec) -#define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags) -#define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags) -#define CTX(v) (&((PyDecContextObject *)v)->ctx) -#define CtxCaps(v) (((PyDecContextObject *)v)->capitals) - - -Py_LOCAL_INLINE(PyObject *) -incr_true(void) -{ - Py_INCREF(Py_True); - return Py_True; -} - -Py_LOCAL_INLINE(PyObject *) -incr_false(void) -{ - Py_INCREF(Py_False); - return Py_False; -} - - +#define PyDecContext_Check(v) PyObject_TypeCheck(v, &PyDecContext_Type) +#define MPD(v) (&((PyDecObject *)v)->dec) +#define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags) +#define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags) +#define CTX(v) (&((PyDecContextObject *)v)->ctx) +#define CtxCaps(v) (((PyDecContextObject *)v)->capitals) + + +Py_LOCAL_INLINE(PyObject *) +incr_true(void) +{ + Py_INCREF(Py_True); + return Py_True; +} + +Py_LOCAL_INLINE(PyObject *) +incr_false(void) +{ + Py_INCREF(Py_False); + return Py_False; +} + + #ifndef WITH_DECIMAL_CONTEXTVAR /* Key for thread state dictionary */ static PyObject *tls_context_key = NULL; @@ -133,1382 +133,1382 @@ static PyDecContextObject *cached_context = NULL; #else static PyObject *current_context_var = NULL; #endif - -/* Template for creating new thread contexts, calling Context() without - * arguments and initializing the module_context on first access. */ -static PyObject *default_context_template = NULL; -/* Basic and extended context templates */ -static PyObject *basic_context_template = NULL; -static PyObject *extended_context_template = NULL; - - -/* Error codes for functions that return signals or conditions */ -#define DEC_INVALID_SIGNALS (MPD_Max_status+1U) -#define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1) -#define DEC_ERRORS (DEC_INVALID_SIGNALS|DEC_ERR_OCCURRED) - -typedef struct { - const char *name; /* condition or signal name */ - const char *fqname; /* fully qualified name */ - uint32_t flag; /* libmpdec flag */ - PyObject *ex; /* corresponding exception */ -} DecCondMap; - -/* Top level Exception; inherits from ArithmeticError */ -static PyObject *DecimalException = NULL; - -/* Exceptions that correspond to IEEE signals */ -#define SUBNORMAL 5 -#define INEXACT 6 -#define ROUNDED 7 -#define SIGNAL_MAP_LEN 9 -static DecCondMap signal_map[] = { - {"InvalidOperation", "decimal.InvalidOperation", MPD_IEEE_Invalid_operation, NULL}, - {"FloatOperation", "decimal.FloatOperation", MPD_Float_operation, NULL}, - {"DivisionByZero", "decimal.DivisionByZero", MPD_Division_by_zero, NULL}, - {"Overflow", "decimal.Overflow", MPD_Overflow, NULL}, - {"Underflow", "decimal.Underflow", MPD_Underflow, NULL}, - {"Subnormal", "decimal.Subnormal", MPD_Subnormal, NULL}, - {"Inexact", "decimal.Inexact", MPD_Inexact, NULL}, - {"Rounded", "decimal.Rounded", MPD_Rounded, NULL}, - {"Clamped", "decimal.Clamped", MPD_Clamped, NULL}, - {NULL} -}; - -/* Exceptions that inherit from InvalidOperation */ -static DecCondMap cond_map[] = { - {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL}, - {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL}, - {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible, NULL}, - {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NULL}, - {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL}, -#ifdef EXTRA_FUNCTIONALITY - {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL}, -#endif - {NULL} -}; - -static const char *dec_signal_string[MPD_NUM_FLAGS] = { - "Clamped", - "InvalidOperation", - "DivisionByZero", - "InvalidOperation", - "InvalidOperation", - "InvalidOperation", - "Inexact", - "InvalidOperation", - "InvalidOperation", - "InvalidOperation", - "FloatOperation", - "Overflow", - "Rounded", - "Subnormal", - "Underflow", -}; - -#ifdef EXTRA_FUNCTIONALITY - #define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD -#else - #define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1) -#endif -static PyObject *round_map[_PY_DEC_ROUND_GUARD]; - -static const char *invalid_rounding_err = -"valid values for rounding are:\n\ - [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\ - ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\ - ROUND_05UP]"; - -static const char *invalid_signals_err = -"valid values for signals are:\n\ - [InvalidOperation, FloatOperation, DivisionByZero,\n\ - Overflow, Underflow, Subnormal, Inexact, Rounded,\n\ - Clamped]"; - -#ifdef EXTRA_FUNCTIONALITY -static const char *invalid_flags_err = -"valid values for _flags or _traps are:\n\ - signals:\n\ - [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\ - DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\ - DecClamped]\n\ - conditions which trigger DecIEEEInvalidOperation:\n\ - [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\ - DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]"; -#endif - -static int -value_error_int(const char *mesg) -{ - PyErr_SetString(PyExc_ValueError, mesg); - return -1; -} - -#ifdef CONFIG_32 -static PyObject * -value_error_ptr(const char *mesg) -{ - PyErr_SetString(PyExc_ValueError, mesg); - return NULL; -} -#endif - -static int -type_error_int(const char *mesg) -{ - PyErr_SetString(PyExc_TypeError, mesg); - return -1; -} - -static int -runtime_error_int(const char *mesg) -{ - PyErr_SetString(PyExc_RuntimeError, mesg); - return -1; -} -#define INTERNAL_ERROR_INT(funcname) \ - return runtime_error_int("internal error in " funcname) - -static PyObject * -runtime_error_ptr(const char *mesg) -{ - PyErr_SetString(PyExc_RuntimeError, mesg); - return NULL; -} -#define INTERNAL_ERROR_PTR(funcname) \ - return runtime_error_ptr("internal error in " funcname) - -static void -dec_traphandler(mpd_context_t *ctx UNUSED) /* GCOV_NOT_REACHED */ -{ /* GCOV_NOT_REACHED */ - return; /* GCOV_NOT_REACHED */ -} - -static PyObject * -flags_as_exception(uint32_t flags) -{ - DecCondMap *cm; - - for (cm = signal_map; cm->name != NULL; cm++) { - if (flags&cm->flag) { - return cm->ex; - } - } - - INTERNAL_ERROR_PTR("flags_as_exception"); /* GCOV_NOT_REACHED */ -} - -Py_LOCAL_INLINE(uint32_t) -exception_as_flag(PyObject *ex) -{ - DecCondMap *cm; - - for (cm = signal_map; cm->name != NULL; cm++) { - if (cm->ex == ex) { - return cm->flag; - } - } - - PyErr_SetString(PyExc_KeyError, invalid_signals_err); - return DEC_INVALID_SIGNALS; -} - -static PyObject * -flags_as_list(uint32_t flags) -{ - PyObject *list; - DecCondMap *cm; - - list = PyList_New(0); - if (list == NULL) { - return NULL; - } - - for (cm = cond_map; cm->name != NULL; cm++) { - if (flags&cm->flag) { - if (PyList_Append(list, cm->ex) < 0) { - goto error; - } - } - } - for (cm = signal_map+1; cm->name != NULL; cm++) { - if (flags&cm->flag) { - if (PyList_Append(list, cm->ex) < 0) { - goto error; - } - } - } - - return list; - -error: - Py_DECREF(list); - return NULL; -} - -static PyObject * -signals_as_list(uint32_t flags) -{ - PyObject *list; - DecCondMap *cm; - - list = PyList_New(0); - if (list == NULL) { - return NULL; - } - - for (cm = signal_map; cm->name != NULL; cm++) { - if (flags&cm->flag) { - if (PyList_Append(list, cm->ex) < 0) { - Py_DECREF(list); - return NULL; - } - } - } - - return list; -} - -static uint32_t -list_as_flags(PyObject *list) -{ - PyObject *item; - uint32_t flags, x; - Py_ssize_t n, j; - - assert(PyList_Check(list)); - - n = PyList_Size(list); - flags = 0; - for (j = 0; j < n; j++) { - item = PyList_GetItem(list, j); - x = exception_as_flag(item); - if (x & DEC_ERRORS) { - return x; - } - flags |= x; - } - - return flags; -} - -static PyObject * -flags_as_dict(uint32_t flags) -{ - DecCondMap *cm; - PyObject *dict; - - dict = PyDict_New(); - if (dict == NULL) { - return NULL; - } - - for (cm = signal_map; cm->name != NULL; cm++) { - PyObject *b = flags&cm->flag ? Py_True : Py_False; - if (PyDict_SetItem(dict, cm->ex, b) < 0) { - Py_DECREF(dict); - return NULL; - } - } - - return dict; -} - -static uint32_t -dict_as_flags(PyObject *val) -{ - PyObject *b; - DecCondMap *cm; - uint32_t flags = 0; - int x; - - if (!PyDict_Check(val)) { - PyErr_SetString(PyExc_TypeError, - "argument must be a signal dict"); - return DEC_INVALID_SIGNALS; - } - - if (PyDict_Size(val) != SIGNAL_MAP_LEN) { - PyErr_SetString(PyExc_KeyError, - "invalid signal dict"); - return DEC_INVALID_SIGNALS; - } - - for (cm = signal_map; cm->name != NULL; cm++) { - b = PyDict_GetItemWithError(val, cm->ex); - if (b == NULL) { - if (PyErr_Occurred()) { - return DEC_ERR_OCCURRED; - } - PyErr_SetString(PyExc_KeyError, - "invalid signal dict"); - return DEC_INVALID_SIGNALS; - } - - x = PyObject_IsTrue(b); - if (x < 0) { - return DEC_ERR_OCCURRED; - } - if (x == 1) { - flags |= cm->flag; - } - } - - return flags; -} - -#ifdef EXTRA_FUNCTIONALITY -static uint32_t -long_as_flags(PyObject *v) -{ - long x; - - x = PyLong_AsLong(v); - if (x == -1 && PyErr_Occurred()) { - return DEC_ERR_OCCURRED; - } - if (x < 0 || x > (long)MPD_Max_status) { - PyErr_SetString(PyExc_TypeError, invalid_flags_err); - return DEC_INVALID_SIGNALS; - } - - return x; -} -#endif - -static int -dec_addstatus(PyObject *context, uint32_t status) -{ - mpd_context_t *ctx = CTX(context); - - ctx->status |= status; - if (status & (ctx->traps|MPD_Malloc_error)) { - PyObject *ex, *siglist; - - if (status & MPD_Malloc_error) { - PyErr_NoMemory(); - return 1; - } - - ex = flags_as_exception(ctx->traps&status); - if (ex == NULL) { - return 1; /* GCOV_NOT_REACHED */ - } - siglist = flags_as_list(ctx->traps&status); - if (siglist == NULL) { - return 1; - } - - PyErr_SetObject(ex, siglist); - Py_DECREF(siglist); - return 1; - } - return 0; -} - -static int -getround(PyObject *v) -{ - int i; - - if (PyUnicode_Check(v)) { - for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { - if (v == round_map[i]) { - return i; - } - } - for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { - if (PyUnicode_Compare(v, round_map[i]) == 0) { - return i; - } - } - } - - return type_error_int(invalid_rounding_err); -} - - -/******************************************************************************/ -/* SignalDict Object */ -/******************************************************************************/ - -/* The SignalDict is a MutableMapping that provides access to the - mpd_context_t flags, which reside in the context object. When a - new context is created, context.traps and context.flags are - initialized to new SignalDicts. Once a SignalDict is tied to - a context, it cannot be deleted. */ - -static int -signaldict_init(PyObject *self, PyObject *args UNUSED, PyObject *kwds UNUSED) -{ - SdFlagAddr(self) = NULL; - return 0; -} - -static Py_ssize_t -signaldict_len(PyObject *self UNUSED) -{ - return SIGNAL_MAP_LEN; -} - -static PyObject *SignalTuple; -static PyObject * -signaldict_iter(PyObject *self UNUSED) -{ - return PyTuple_Type.tp_iter(SignalTuple); -} - -static PyObject * -signaldict_getitem(PyObject *self, PyObject *key) -{ - uint32_t flag; - - flag = exception_as_flag(key); - if (flag & DEC_ERRORS) { - return NULL; - } - - return SdFlags(self)&flag ? incr_true() : incr_false(); -} - -static int -signaldict_setitem(PyObject *self, PyObject *key, PyObject *value) -{ - uint32_t flag; - int x; - - if (value == NULL) { - return value_error_int("signal keys cannot be deleted"); - } - - flag = exception_as_flag(key); - if (flag & DEC_ERRORS) { - return -1; - } - - x = PyObject_IsTrue(value); - if (x < 0) { - return -1; - } - - if (x == 1) { - SdFlags(self) |= flag; - } - else { - SdFlags(self) &= ~flag; - } - - return 0; -} - -static PyObject * -signaldict_repr(PyObject *self) -{ - DecCondMap *cm; - const char *n[SIGNAL_MAP_LEN]; /* name */ - const char *b[SIGNAL_MAP_LEN]; /* bool */ - int i; - - assert(SIGNAL_MAP_LEN == 9); - - for (cm=signal_map, i=0; cm->name != NULL; cm++, i++) { - n[i] = cm->fqname; - b[i] = SdFlags(self)&cm->flag ? "True" : "False"; - } - return PyUnicode_FromFormat( - "{<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " - "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " - "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s}", - n[0], b[0], n[1], b[1], n[2], b[2], - n[3], b[3], n[4], b[4], n[5], b[5], - n[6], b[6], n[7], b[7], n[8], b[8]); -} - -static PyObject * -signaldict_richcompare(PyObject *v, PyObject *w, int op) -{ - PyObject *res = Py_NotImplemented; - - assert(PyDecSignalDict_Check(v)); - - if (op == Py_EQ || op == Py_NE) { - if (PyDecSignalDict_Check(w)) { - res = (SdFlags(v)==SdFlags(w)) ^ (op==Py_NE) ? Py_True : Py_False; - } - else if (PyDict_Check(w)) { - uint32_t flags = dict_as_flags(w); - if (flags & DEC_ERRORS) { - if (flags & DEC_INVALID_SIGNALS) { - /* non-comparable: Py_NotImplemented */ - PyErr_Clear(); - } - else { - return NULL; - } - } - else { - res = (SdFlags(v)==flags) ^ (op==Py_NE) ? Py_True : Py_False; - } - } - } - - Py_INCREF(res); - return res; -} - -static PyObject * -signaldict_copy(PyObject *self, PyObject *args UNUSED) -{ - return flags_as_dict(SdFlags(self)); -} - - -static PyMappingMethods signaldict_as_mapping = { - (lenfunc)signaldict_len, /* mp_length */ - (binaryfunc)signaldict_getitem, /* mp_subscript */ - (objobjargproc)signaldict_setitem /* mp_ass_subscript */ -}; - -static PyMethodDef signaldict_methods[] = { - { "copy", (PyCFunction)signaldict_copy, METH_NOARGS, NULL}, - {NULL, NULL} -}; - - -static PyTypeObject PyDecSignalDictMixin_Type = -{ - PyVarObject_HEAD_INIT(0, 0) - "decimal.SignalDictMixin", /* tp_name */ - sizeof(PyDecSignalDictObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - 0, /* tp_dealloc */ + +/* Template for creating new thread contexts, calling Context() without + * arguments and initializing the module_context on first access. */ +static PyObject *default_context_template = NULL; +/* Basic and extended context templates */ +static PyObject *basic_context_template = NULL; +static PyObject *extended_context_template = NULL; + + +/* Error codes for functions that return signals or conditions */ +#define DEC_INVALID_SIGNALS (MPD_Max_status+1U) +#define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1) +#define DEC_ERRORS (DEC_INVALID_SIGNALS|DEC_ERR_OCCURRED) + +typedef struct { + const char *name; /* condition or signal name */ + const char *fqname; /* fully qualified name */ + uint32_t flag; /* libmpdec flag */ + PyObject *ex; /* corresponding exception */ +} DecCondMap; + +/* Top level Exception; inherits from ArithmeticError */ +static PyObject *DecimalException = NULL; + +/* Exceptions that correspond to IEEE signals */ +#define SUBNORMAL 5 +#define INEXACT 6 +#define ROUNDED 7 +#define SIGNAL_MAP_LEN 9 +static DecCondMap signal_map[] = { + {"InvalidOperation", "decimal.InvalidOperation", MPD_IEEE_Invalid_operation, NULL}, + {"FloatOperation", "decimal.FloatOperation", MPD_Float_operation, NULL}, + {"DivisionByZero", "decimal.DivisionByZero", MPD_Division_by_zero, NULL}, + {"Overflow", "decimal.Overflow", MPD_Overflow, NULL}, + {"Underflow", "decimal.Underflow", MPD_Underflow, NULL}, + {"Subnormal", "decimal.Subnormal", MPD_Subnormal, NULL}, + {"Inexact", "decimal.Inexact", MPD_Inexact, NULL}, + {"Rounded", "decimal.Rounded", MPD_Rounded, NULL}, + {"Clamped", "decimal.Clamped", MPD_Clamped, NULL}, + {NULL} +}; + +/* Exceptions that inherit from InvalidOperation */ +static DecCondMap cond_map[] = { + {"InvalidOperation", "decimal.InvalidOperation", MPD_Invalid_operation, NULL}, + {"ConversionSyntax", "decimal.ConversionSyntax", MPD_Conversion_syntax, NULL}, + {"DivisionImpossible", "decimal.DivisionImpossible", MPD_Division_impossible, NULL}, + {"DivisionUndefined", "decimal.DivisionUndefined", MPD_Division_undefined, NULL}, + {"InvalidContext", "decimal.InvalidContext", MPD_Invalid_context, NULL}, +#ifdef EXTRA_FUNCTIONALITY + {"MallocError", "decimal.MallocError", MPD_Malloc_error, NULL}, +#endif + {NULL} +}; + +static const char *dec_signal_string[MPD_NUM_FLAGS] = { + "Clamped", + "InvalidOperation", + "DivisionByZero", + "InvalidOperation", + "InvalidOperation", + "InvalidOperation", + "Inexact", + "InvalidOperation", + "InvalidOperation", + "InvalidOperation", + "FloatOperation", + "Overflow", + "Rounded", + "Subnormal", + "Underflow", +}; + +#ifdef EXTRA_FUNCTIONALITY + #define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD +#else + #define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1) +#endif +static PyObject *round_map[_PY_DEC_ROUND_GUARD]; + +static const char *invalid_rounding_err = +"valid values for rounding are:\n\ + [ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\ + ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN,\n\ + ROUND_05UP]"; + +static const char *invalid_signals_err = +"valid values for signals are:\n\ + [InvalidOperation, FloatOperation, DivisionByZero,\n\ + Overflow, Underflow, Subnormal, Inexact, Rounded,\n\ + Clamped]"; + +#ifdef EXTRA_FUNCTIONALITY +static const char *invalid_flags_err = +"valid values for _flags or _traps are:\n\ + signals:\n\ + [DecIEEEInvalidOperation, DecFloatOperation, DecDivisionByZero,\n\ + DecOverflow, DecUnderflow, DecSubnormal, DecInexact, DecRounded,\n\ + DecClamped]\n\ + conditions which trigger DecIEEEInvalidOperation:\n\ + [DecInvalidOperation, DecConversionSyntax, DecDivisionImpossible,\n\ + DecDivisionUndefined, DecFpuError, DecInvalidContext, DecMallocError]"; +#endif + +static int +value_error_int(const char *mesg) +{ + PyErr_SetString(PyExc_ValueError, mesg); + return -1; +} + +#ifdef CONFIG_32 +static PyObject * +value_error_ptr(const char *mesg) +{ + PyErr_SetString(PyExc_ValueError, mesg); + return NULL; +} +#endif + +static int +type_error_int(const char *mesg) +{ + PyErr_SetString(PyExc_TypeError, mesg); + return -1; +} + +static int +runtime_error_int(const char *mesg) +{ + PyErr_SetString(PyExc_RuntimeError, mesg); + return -1; +} +#define INTERNAL_ERROR_INT(funcname) \ + return runtime_error_int("internal error in " funcname) + +static PyObject * +runtime_error_ptr(const char *mesg) +{ + PyErr_SetString(PyExc_RuntimeError, mesg); + return NULL; +} +#define INTERNAL_ERROR_PTR(funcname) \ + return runtime_error_ptr("internal error in " funcname) + +static void +dec_traphandler(mpd_context_t *ctx UNUSED) /* GCOV_NOT_REACHED */ +{ /* GCOV_NOT_REACHED */ + return; /* GCOV_NOT_REACHED */ +} + +static PyObject * +flags_as_exception(uint32_t flags) +{ + DecCondMap *cm; + + for (cm = signal_map; cm->name != NULL; cm++) { + if (flags&cm->flag) { + return cm->ex; + } + } + + INTERNAL_ERROR_PTR("flags_as_exception"); /* GCOV_NOT_REACHED */ +} + +Py_LOCAL_INLINE(uint32_t) +exception_as_flag(PyObject *ex) +{ + DecCondMap *cm; + + for (cm = signal_map; cm->name != NULL; cm++) { + if (cm->ex == ex) { + return cm->flag; + } + } + + PyErr_SetString(PyExc_KeyError, invalid_signals_err); + return DEC_INVALID_SIGNALS; +} + +static PyObject * +flags_as_list(uint32_t flags) +{ + PyObject *list; + DecCondMap *cm; + + list = PyList_New(0); + if (list == NULL) { + return NULL; + } + + for (cm = cond_map; cm->name != NULL; cm++) { + if (flags&cm->flag) { + if (PyList_Append(list, cm->ex) < 0) { + goto error; + } + } + } + for (cm = signal_map+1; cm->name != NULL; cm++) { + if (flags&cm->flag) { + if (PyList_Append(list, cm->ex) < 0) { + goto error; + } + } + } + + return list; + +error: + Py_DECREF(list); + return NULL; +} + +static PyObject * +signals_as_list(uint32_t flags) +{ + PyObject *list; + DecCondMap *cm; + + list = PyList_New(0); + if (list == NULL) { + return NULL; + } + + for (cm = signal_map; cm->name != NULL; cm++) { + if (flags&cm->flag) { + if (PyList_Append(list, cm->ex) < 0) { + Py_DECREF(list); + return NULL; + } + } + } + + return list; +} + +static uint32_t +list_as_flags(PyObject *list) +{ + PyObject *item; + uint32_t flags, x; + Py_ssize_t n, j; + + assert(PyList_Check(list)); + + n = PyList_Size(list); + flags = 0; + for (j = 0; j < n; j++) { + item = PyList_GetItem(list, j); + x = exception_as_flag(item); + if (x & DEC_ERRORS) { + return x; + } + flags |= x; + } + + return flags; +} + +static PyObject * +flags_as_dict(uint32_t flags) +{ + DecCondMap *cm; + PyObject *dict; + + dict = PyDict_New(); + if (dict == NULL) { + return NULL; + } + + for (cm = signal_map; cm->name != NULL; cm++) { + PyObject *b = flags&cm->flag ? Py_True : Py_False; + if (PyDict_SetItem(dict, cm->ex, b) < 0) { + Py_DECREF(dict); + return NULL; + } + } + + return dict; +} + +static uint32_t +dict_as_flags(PyObject *val) +{ + PyObject *b; + DecCondMap *cm; + uint32_t flags = 0; + int x; + + if (!PyDict_Check(val)) { + PyErr_SetString(PyExc_TypeError, + "argument must be a signal dict"); + return DEC_INVALID_SIGNALS; + } + + if (PyDict_Size(val) != SIGNAL_MAP_LEN) { + PyErr_SetString(PyExc_KeyError, + "invalid signal dict"); + return DEC_INVALID_SIGNALS; + } + + for (cm = signal_map; cm->name != NULL; cm++) { + b = PyDict_GetItemWithError(val, cm->ex); + if (b == NULL) { + if (PyErr_Occurred()) { + return DEC_ERR_OCCURRED; + } + PyErr_SetString(PyExc_KeyError, + "invalid signal dict"); + return DEC_INVALID_SIGNALS; + } + + x = PyObject_IsTrue(b); + if (x < 0) { + return DEC_ERR_OCCURRED; + } + if (x == 1) { + flags |= cm->flag; + } + } + + return flags; +} + +#ifdef EXTRA_FUNCTIONALITY +static uint32_t +long_as_flags(PyObject *v) +{ + long x; + + x = PyLong_AsLong(v); + if (x == -1 && PyErr_Occurred()) { + return DEC_ERR_OCCURRED; + } + if (x < 0 || x > (long)MPD_Max_status) { + PyErr_SetString(PyExc_TypeError, invalid_flags_err); + return DEC_INVALID_SIGNALS; + } + + return x; +} +#endif + +static int +dec_addstatus(PyObject *context, uint32_t status) +{ + mpd_context_t *ctx = CTX(context); + + ctx->status |= status; + if (status & (ctx->traps|MPD_Malloc_error)) { + PyObject *ex, *siglist; + + if (status & MPD_Malloc_error) { + PyErr_NoMemory(); + return 1; + } + + ex = flags_as_exception(ctx->traps&status); + if (ex == NULL) { + return 1; /* GCOV_NOT_REACHED */ + } + siglist = flags_as_list(ctx->traps&status); + if (siglist == NULL) { + return 1; + } + + PyErr_SetObject(ex, siglist); + Py_DECREF(siglist); + return 1; + } + return 0; +} + +static int +getround(PyObject *v) +{ + int i; + + if (PyUnicode_Check(v)) { + for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { + if (v == round_map[i]) { + return i; + } + } + for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { + if (PyUnicode_Compare(v, round_map[i]) == 0) { + return i; + } + } + } + + return type_error_int(invalid_rounding_err); +} + + +/******************************************************************************/ +/* SignalDict Object */ +/******************************************************************************/ + +/* The SignalDict is a MutableMapping that provides access to the + mpd_context_t flags, which reside in the context object. When a + new context is created, context.traps and context.flags are + initialized to new SignalDicts. Once a SignalDict is tied to + a context, it cannot be deleted. */ + +static int +signaldict_init(PyObject *self, PyObject *args UNUSED, PyObject *kwds UNUSED) +{ + SdFlagAddr(self) = NULL; + return 0; +} + +static Py_ssize_t +signaldict_len(PyObject *self UNUSED) +{ + return SIGNAL_MAP_LEN; +} + +static PyObject *SignalTuple; +static PyObject * +signaldict_iter(PyObject *self UNUSED) +{ + return PyTuple_Type.tp_iter(SignalTuple); +} + +static PyObject * +signaldict_getitem(PyObject *self, PyObject *key) +{ + uint32_t flag; + + flag = exception_as_flag(key); + if (flag & DEC_ERRORS) { + return NULL; + } + + return SdFlags(self)&flag ? incr_true() : incr_false(); +} + +static int +signaldict_setitem(PyObject *self, PyObject *key, PyObject *value) +{ + uint32_t flag; + int x; + + if (value == NULL) { + return value_error_int("signal keys cannot be deleted"); + } + + flag = exception_as_flag(key); + if (flag & DEC_ERRORS) { + return -1; + } + + x = PyObject_IsTrue(value); + if (x < 0) { + return -1; + } + + if (x == 1) { + SdFlags(self) |= flag; + } + else { + SdFlags(self) &= ~flag; + } + + return 0; +} + +static PyObject * +signaldict_repr(PyObject *self) +{ + DecCondMap *cm; + const char *n[SIGNAL_MAP_LEN]; /* name */ + const char *b[SIGNAL_MAP_LEN]; /* bool */ + int i; + + assert(SIGNAL_MAP_LEN == 9); + + for (cm=signal_map, i=0; cm->name != NULL; cm++, i++) { + n[i] = cm->fqname; + b[i] = SdFlags(self)&cm->flag ? "True" : "False"; + } + return PyUnicode_FromFormat( + "{<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " + "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s, " + "<class '%s'>:%s, <class '%s'>:%s, <class '%s'>:%s}", + n[0], b[0], n[1], b[1], n[2], b[2], + n[3], b[3], n[4], b[4], n[5], b[5], + n[6], b[6], n[7], b[7], n[8], b[8]); +} + +static PyObject * +signaldict_richcompare(PyObject *v, PyObject *w, int op) +{ + PyObject *res = Py_NotImplemented; + + assert(PyDecSignalDict_Check(v)); + + if (op == Py_EQ || op == Py_NE) { + if (PyDecSignalDict_Check(w)) { + res = (SdFlags(v)==SdFlags(w)) ^ (op==Py_NE) ? Py_True : Py_False; + } + else if (PyDict_Check(w)) { + uint32_t flags = dict_as_flags(w); + if (flags & DEC_ERRORS) { + if (flags & DEC_INVALID_SIGNALS) { + /* non-comparable: Py_NotImplemented */ + PyErr_Clear(); + } + else { + return NULL; + } + } + else { + res = (SdFlags(v)==flags) ^ (op==Py_NE) ? Py_True : Py_False; + } + } + } + + Py_INCREF(res); + return res; +} + +static PyObject * +signaldict_copy(PyObject *self, PyObject *args UNUSED) +{ + return flags_as_dict(SdFlags(self)); +} + + +static PyMappingMethods signaldict_as_mapping = { + (lenfunc)signaldict_len, /* mp_length */ + (binaryfunc)signaldict_getitem, /* mp_subscript */ + (objobjargproc)signaldict_setitem /* mp_ass_subscript */ +}; + +static PyMethodDef signaldict_methods[] = { + { "copy", (PyCFunction)signaldict_copy, METH_NOARGS, NULL}, + {NULL, NULL} +}; + + +static PyTypeObject PyDecSignalDictMixin_Type = +{ + PyVarObject_HEAD_INIT(0, 0) + "decimal.SignalDictMixin", /* tp_name */ + sizeof(PyDecSignalDictObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - (getattrfunc) 0, /* tp_getattr */ - (setattrfunc) 0, /* tp_setattr */ + (getattrfunc) 0, /* tp_getattr */ + (setattrfunc) 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc) signaldict_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - &signaldict_as_mapping, /* tp_as_mapping */ - PyObject_HashNotImplemented, /* tp_hash */ - 0, /* tp_call */ - (reprfunc) 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ - (setattrofunc) 0, /* tp_setattro */ - (PyBufferProcs *) 0, /* tp_as_buffer */ + (reprfunc) signaldict_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + &signaldict_as_mapping, /* tp_as_mapping */ + PyObject_HashNotImplemented, /* tp_hash */ + 0, /* tp_call */ + (reprfunc) 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + (setattrofunc) 0, /* tp_setattro */ + (PyBufferProcs *) 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ - 0, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - signaldict_richcompare, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - (getiterfunc)signaldict_iter, /* tp_iter */ - 0, /* tp_iternext */ - signaldict_methods, /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - (initproc)signaldict_init, /* tp_init */ - 0, /* tp_alloc */ - PyType_GenericNew, /* tp_new */ -}; - - -/******************************************************************************/ -/* Context Object, Part 1 */ -/******************************************************************************/ - -#define Dec_CONTEXT_GET_SSIZE(mem) \ -static PyObject * \ -context_get##mem(PyObject *self, void *closure UNUSED) \ -{ \ - return PyLong_FromSsize_t(mpd_get##mem(CTX(self))); \ -} - -#define Dec_CONTEXT_GET_ULONG(mem) \ -static PyObject * \ -context_get##mem(PyObject *self, void *closure UNUSED) \ -{ \ - return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \ -} - -Dec_CONTEXT_GET_SSIZE(prec) -Dec_CONTEXT_GET_SSIZE(emax) -Dec_CONTEXT_GET_SSIZE(emin) -Dec_CONTEXT_GET_SSIZE(clamp) - -#ifdef EXTRA_FUNCTIONALITY -Dec_CONTEXT_GET_ULONG(traps) -Dec_CONTEXT_GET_ULONG(status) -#endif - -static PyObject * -context_getround(PyObject *self, void *closure UNUSED) -{ - int i = mpd_getround(CTX(self)); - - Py_INCREF(round_map[i]); - return round_map[i]; -} - -static PyObject * -context_getcapitals(PyObject *self, void *closure UNUSED) -{ - return PyLong_FromLong(CtxCaps(self)); -} - -#ifdef EXTRA_FUNCTIONALITY -static PyObject * -context_getallcr(PyObject *self, void *closure UNUSED) -{ - return PyLong_FromLong(mpd_getcr(CTX(self))); -} -#endif - -static PyObject * -context_getetiny(PyObject *self, PyObject *dummy UNUSED) -{ - return PyLong_FromSsize_t(mpd_etiny(CTX(self))); -} - -static PyObject * -context_getetop(PyObject *self, PyObject *dummy UNUSED) -{ - return PyLong_FromSsize_t(mpd_etop(CTX(self))); -} - -static int -context_setprec(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetprec(ctx, x)) { - return value_error_int( - "valid range for prec is [1, MAX_PREC]"); - } - - return 0; -} - -static int -context_setemin(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetemin(ctx, x)) { - return value_error_int( - "valid range for Emin is [MIN_EMIN, 0]"); - } - - return 0; -} - -static int -context_setemax(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetemax(ctx, x)) { - return value_error_int( - "valid range for Emax is [0, MAX_EMAX]"); - } - - return 0; -} - -#ifdef CONFIG_32 -static PyObject * -context_unsafe_setprec(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx = CTX(self); - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return NULL; - } - - if (x < 1 || x > 1070000000L) { - return value_error_ptr( - "valid range for unsafe prec is [1, 1070000000]"); - } - - ctx->prec = x; - Py_RETURN_NONE; -} - -static PyObject * -context_unsafe_setemin(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx = CTX(self); - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return NULL; - } - - if (x < -1070000000L || x > 0) { - return value_error_ptr( - "valid range for unsafe emin is [-1070000000, 0]"); - } - - ctx->emin = x; - Py_RETURN_NONE; -} - -static PyObject * -context_unsafe_setemax(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx = CTX(self); - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return NULL; - } - - if (x < 0 || x > 1070000000L) { - return value_error_ptr( - "valid range for unsafe emax is [0, 1070000000]"); - } - - ctx->emax = x; - Py_RETURN_NONE; -} -#endif - -static int -context_setround(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - int x; - - x = getround(value); - if (x == -1) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetround(ctx, x)) { - INTERNAL_ERROR_INT("context_setround"); /* GCOV_NOT_REACHED */ - } - - return 0; -} - -static int -context_setcapitals(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - - if (x != 0 && x != 1) { - return value_error_int( - "valid values for capitals are 0 or 1"); - } - CtxCaps(self) = (int)x; - - return 0; -} - -#ifdef EXTRA_FUNCTIONALITY -static int -context_settraps(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - uint32_t flags; - - flags = long_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsettraps(ctx, flags)) { - INTERNAL_ERROR_INT("context_settraps"); - } - - return 0; -} -#endif - -static int -context_settraps_list(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx; - uint32_t flags; - - flags = list_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsettraps(ctx, flags)) { - INTERNAL_ERROR_INT("context_settraps_list"); - } - - return 0; -} - -static int -context_settraps_dict(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx; - uint32_t flags; - - if (PyDecSignalDict_Check(value)) { - flags = SdFlags(value); - } - else { - flags = dict_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - } - - ctx = CTX(self); - if (!mpd_qsettraps(ctx, flags)) { - INTERNAL_ERROR_INT("context_settraps_dict"); - } - - return 0; -} - -#ifdef EXTRA_FUNCTIONALITY -static int -context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - uint32_t flags; - - flags = long_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetstatus(ctx, flags)) { - INTERNAL_ERROR_INT("context_setstatus"); - } - - return 0; -} -#endif - -static int -context_setstatus_list(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx; - uint32_t flags; - - flags = list_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - - ctx = CTX(self); - if (!mpd_qsetstatus(ctx, flags)) { - INTERNAL_ERROR_INT("context_setstatus_list"); - } - - return 0; -} - -static int -context_setstatus_dict(PyObject *self, PyObject *value) -{ - mpd_context_t *ctx; - uint32_t flags; - - if (PyDecSignalDict_Check(value)) { - flags = SdFlags(value); - } - else { - flags = dict_as_flags(value); - if (flags & DEC_ERRORS) { - return -1; - } - } - - ctx = CTX(self); - if (!mpd_qsetstatus(ctx, flags)) { - INTERNAL_ERROR_INT("context_setstatus_dict"); - } - - return 0; -} - -static int -context_setclamp(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - BOUNDS_CHECK(x, INT_MIN, INT_MAX); - - ctx = CTX(self); - if (!mpd_qsetclamp(ctx, (int)x)) { - return value_error_int("valid values for clamp are 0 or 1"); - } - - return 0; -} - -#ifdef EXTRA_FUNCTIONALITY -static int -context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED) -{ - mpd_context_t *ctx; - mpd_ssize_t x; - - x = PyLong_AsSsize_t(value); - if (x == -1 && PyErr_Occurred()) { - return -1; - } - BOUNDS_CHECK(x, INT_MIN, INT_MAX); - - ctx = CTX(self); - if (!mpd_qsetcr(ctx, (int)x)) { - return value_error_int("valid values for _allcr are 0 or 1"); - } - - return 0; -} -#endif - -static PyObject * -context_getattr(PyObject *self, PyObject *name) -{ - PyObject *retval; - - if (PyUnicode_Check(name)) { - if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { - retval = ((PyDecContextObject *)self)->traps; - Py_INCREF(retval); - return retval; - } - if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { - retval = ((PyDecContextObject *)self)->flags; - Py_INCREF(retval); - return retval; - } - } - - return PyObject_GenericGetAttr(self, name); -} - -static int -context_setattr(PyObject *self, PyObject *name, PyObject *value) -{ - if (value == NULL) { - PyErr_SetString(PyExc_AttributeError, - "context attributes cannot be deleted"); - return -1; - } - - if (PyUnicode_Check(name)) { - if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { - return context_settraps_dict(self, value); - } - if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { - return context_setstatus_dict(self, value); - } - } - - return PyObject_GenericSetAttr(self, name, value); -} - -static PyObject * -context_clear_traps(PyObject *self, PyObject *dummy UNUSED) -{ - CTX(self)->traps = 0; - Py_RETURN_NONE; -} - -static PyObject * -context_clear_flags(PyObject *self, PyObject *dummy UNUSED) -{ - CTX(self)->status = 0; - Py_RETURN_NONE; -} - -#define DEC_DFLT_EMAX 999999 -#define DEC_DFLT_EMIN -999999 - -static mpd_context_t dflt_ctx = { - 28, DEC_DFLT_EMAX, DEC_DFLT_EMIN, - MPD_IEEE_Invalid_operation|MPD_Division_by_zero|MPD_Overflow, - 0, 0, MPD_ROUND_HALF_EVEN, 0, 1 -}; - -static PyObject * -context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED) -{ - PyDecContextObject *self = NULL; - mpd_context_t *ctx; - - if (type == &PyDecContext_Type) { - self = PyObject_New(PyDecContextObject, &PyDecContext_Type); - } - else { - self = (PyDecContextObject *)type->tp_alloc(type, 0); - } - - if (self == NULL) { - return NULL; - } - - self->traps = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); - if (self->traps == NULL) { - self->flags = NULL; - Py_DECREF(self); - return NULL; - } - self->flags = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); - if (self->flags == NULL) { - Py_DECREF(self); - return NULL; - } - - ctx = CTX(self); - - if (default_context_template) { - *ctx = *CTX(default_context_template); - } - else { - *ctx = dflt_ctx; - } - - SdFlagAddr(self->traps) = &ctx->traps; - SdFlagAddr(self->flags) = &ctx->status; - - CtxCaps(self) = 1; - self->tstate = NULL; - - return (PyObject *)self; -} - -static void -context_dealloc(PyDecContextObject *self) -{ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + signaldict_richcompare, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + (getiterfunc)signaldict_iter, /* tp_iter */ + 0, /* tp_iternext */ + signaldict_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)signaldict_init, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + + +/******************************************************************************/ +/* Context Object, Part 1 */ +/******************************************************************************/ + +#define Dec_CONTEXT_GET_SSIZE(mem) \ +static PyObject * \ +context_get##mem(PyObject *self, void *closure UNUSED) \ +{ \ + return PyLong_FromSsize_t(mpd_get##mem(CTX(self))); \ +} + +#define Dec_CONTEXT_GET_ULONG(mem) \ +static PyObject * \ +context_get##mem(PyObject *self, void *closure UNUSED) \ +{ \ + return PyLong_FromUnsignedLong(mpd_get##mem(CTX(self))); \ +} + +Dec_CONTEXT_GET_SSIZE(prec) +Dec_CONTEXT_GET_SSIZE(emax) +Dec_CONTEXT_GET_SSIZE(emin) +Dec_CONTEXT_GET_SSIZE(clamp) + +#ifdef EXTRA_FUNCTIONALITY +Dec_CONTEXT_GET_ULONG(traps) +Dec_CONTEXT_GET_ULONG(status) +#endif + +static PyObject * +context_getround(PyObject *self, void *closure UNUSED) +{ + int i = mpd_getround(CTX(self)); + + Py_INCREF(round_map[i]); + return round_map[i]; +} + +static PyObject * +context_getcapitals(PyObject *self, void *closure UNUSED) +{ + return PyLong_FromLong(CtxCaps(self)); +} + +#ifdef EXTRA_FUNCTIONALITY +static PyObject * +context_getallcr(PyObject *self, void *closure UNUSED) +{ + return PyLong_FromLong(mpd_getcr(CTX(self))); +} +#endif + +static PyObject * +context_getetiny(PyObject *self, PyObject *dummy UNUSED) +{ + return PyLong_FromSsize_t(mpd_etiny(CTX(self))); +} + +static PyObject * +context_getetop(PyObject *self, PyObject *dummy UNUSED) +{ + return PyLong_FromSsize_t(mpd_etop(CTX(self))); +} + +static int +context_setprec(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetprec(ctx, x)) { + return value_error_int( + "valid range for prec is [1, MAX_PREC]"); + } + + return 0; +} + +static int +context_setemin(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetemin(ctx, x)) { + return value_error_int( + "valid range for Emin is [MIN_EMIN, 0]"); + } + + return 0; +} + +static int +context_setemax(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetemax(ctx, x)) { + return value_error_int( + "valid range for Emax is [0, MAX_EMAX]"); + } + + return 0; +} + +#ifdef CONFIG_32 +static PyObject * +context_unsafe_setprec(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx = CTX(self); + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return NULL; + } + + if (x < 1 || x > 1070000000L) { + return value_error_ptr( + "valid range for unsafe prec is [1, 1070000000]"); + } + + ctx->prec = x; + Py_RETURN_NONE; +} + +static PyObject * +context_unsafe_setemin(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx = CTX(self); + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return NULL; + } + + if (x < -1070000000L || x > 0) { + return value_error_ptr( + "valid range for unsafe emin is [-1070000000, 0]"); + } + + ctx->emin = x; + Py_RETURN_NONE; +} + +static PyObject * +context_unsafe_setemax(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx = CTX(self); + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return NULL; + } + + if (x < 0 || x > 1070000000L) { + return value_error_ptr( + "valid range for unsafe emax is [0, 1070000000]"); + } + + ctx->emax = x; + Py_RETURN_NONE; +} +#endif + +static int +context_setround(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + int x; + + x = getround(value); + if (x == -1) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetround(ctx, x)) { + INTERNAL_ERROR_INT("context_setround"); /* GCOV_NOT_REACHED */ + } + + return 0; +} + +static int +context_setcapitals(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + + if (x != 0 && x != 1) { + return value_error_int( + "valid values for capitals are 0 or 1"); + } + CtxCaps(self) = (int)x; + + return 0; +} + +#ifdef EXTRA_FUNCTIONALITY +static int +context_settraps(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + uint32_t flags; + + flags = long_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsettraps(ctx, flags)) { + INTERNAL_ERROR_INT("context_settraps"); + } + + return 0; +} +#endif + +static int +context_settraps_list(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx; + uint32_t flags; + + flags = list_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsettraps(ctx, flags)) { + INTERNAL_ERROR_INT("context_settraps_list"); + } + + return 0; +} + +static int +context_settraps_dict(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx; + uint32_t flags; + + if (PyDecSignalDict_Check(value)) { + flags = SdFlags(value); + } + else { + flags = dict_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + } + + ctx = CTX(self); + if (!mpd_qsettraps(ctx, flags)) { + INTERNAL_ERROR_INT("context_settraps_dict"); + } + + return 0; +} + +#ifdef EXTRA_FUNCTIONALITY +static int +context_setstatus(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + uint32_t flags; + + flags = long_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetstatus(ctx, flags)) { + INTERNAL_ERROR_INT("context_setstatus"); + } + + return 0; +} +#endif + +static int +context_setstatus_list(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx; + uint32_t flags; + + flags = list_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + + ctx = CTX(self); + if (!mpd_qsetstatus(ctx, flags)) { + INTERNAL_ERROR_INT("context_setstatus_list"); + } + + return 0; +} + +static int +context_setstatus_dict(PyObject *self, PyObject *value) +{ + mpd_context_t *ctx; + uint32_t flags; + + if (PyDecSignalDict_Check(value)) { + flags = SdFlags(value); + } + else { + flags = dict_as_flags(value); + if (flags & DEC_ERRORS) { + return -1; + } + } + + ctx = CTX(self); + if (!mpd_qsetstatus(ctx, flags)) { + INTERNAL_ERROR_INT("context_setstatus_dict"); + } + + return 0; +} + +static int +context_setclamp(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + BOUNDS_CHECK(x, INT_MIN, INT_MAX); + + ctx = CTX(self); + if (!mpd_qsetclamp(ctx, (int)x)) { + return value_error_int("valid values for clamp are 0 or 1"); + } + + return 0; +} + +#ifdef EXTRA_FUNCTIONALITY +static int +context_setallcr(PyObject *self, PyObject *value, void *closure UNUSED) +{ + mpd_context_t *ctx; + mpd_ssize_t x; + + x = PyLong_AsSsize_t(value); + if (x == -1 && PyErr_Occurred()) { + return -1; + } + BOUNDS_CHECK(x, INT_MIN, INT_MAX); + + ctx = CTX(self); + if (!mpd_qsetcr(ctx, (int)x)) { + return value_error_int("valid values for _allcr are 0 or 1"); + } + + return 0; +} +#endif + +static PyObject * +context_getattr(PyObject *self, PyObject *name) +{ + PyObject *retval; + + if (PyUnicode_Check(name)) { + if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { + retval = ((PyDecContextObject *)self)->traps; + Py_INCREF(retval); + return retval; + } + if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { + retval = ((PyDecContextObject *)self)->flags; + Py_INCREF(retval); + return retval; + } + } + + return PyObject_GenericGetAttr(self, name); +} + +static int +context_setattr(PyObject *self, PyObject *name, PyObject *value) +{ + if (value == NULL) { + PyErr_SetString(PyExc_AttributeError, + "context attributes cannot be deleted"); + return -1; + } + + if (PyUnicode_Check(name)) { + if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { + return context_settraps_dict(self, value); + } + if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { + return context_setstatus_dict(self, value); + } + } + + return PyObject_GenericSetAttr(self, name, value); +} + +static PyObject * +context_clear_traps(PyObject *self, PyObject *dummy UNUSED) +{ + CTX(self)->traps = 0; + Py_RETURN_NONE; +} + +static PyObject * +context_clear_flags(PyObject *self, PyObject *dummy UNUSED) +{ + CTX(self)->status = 0; + Py_RETURN_NONE; +} + +#define DEC_DFLT_EMAX 999999 +#define DEC_DFLT_EMIN -999999 + +static mpd_context_t dflt_ctx = { + 28, DEC_DFLT_EMAX, DEC_DFLT_EMIN, + MPD_IEEE_Invalid_operation|MPD_Division_by_zero|MPD_Overflow, + 0, 0, MPD_ROUND_HALF_EVEN, 0, 1 +}; + +static PyObject * +context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED) +{ + PyDecContextObject *self = NULL; + mpd_context_t *ctx; + + if (type == &PyDecContext_Type) { + self = PyObject_New(PyDecContextObject, &PyDecContext_Type); + } + else { + self = (PyDecContextObject *)type->tp_alloc(type, 0); + } + + if (self == NULL) { + return NULL; + } + + self->traps = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); + if (self->traps == NULL) { + self->flags = NULL; + Py_DECREF(self); + return NULL; + } + self->flags = PyObject_CallObject((PyObject *)PyDecSignalDict_Type, NULL); + if (self->flags == NULL) { + Py_DECREF(self); + return NULL; + } + + ctx = CTX(self); + + if (default_context_template) { + *ctx = *CTX(default_context_template); + } + else { + *ctx = dflt_ctx; + } + + SdFlagAddr(self->traps) = &ctx->traps; + SdFlagAddr(self->flags) = &ctx->status; + + CtxCaps(self) = 1; + self->tstate = NULL; + + return (PyObject *)self; +} + +static void +context_dealloc(PyDecContextObject *self) +{ #ifndef WITH_DECIMAL_CONTEXTVAR if (self == cached_context) { cached_context = NULL; } #endif - Py_XDECREF(self->traps); - Py_XDECREF(self->flags); - Py_TYPE(self)->tp_free(self); -} - -static int -context_init(PyObject *self, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = { - "prec", "rounding", "Emin", "Emax", "capitals", "clamp", - "flags", "traps", NULL - }; - PyObject *prec = Py_None; - PyObject *rounding = Py_None; - PyObject *emin = Py_None; - PyObject *emax = Py_None; - PyObject *capitals = Py_None; - PyObject *clamp = Py_None; - PyObject *status = Py_None; - PyObject *traps = Py_None; - int ret; - - assert(PyTuple_Check(args)); - - if (!PyArg_ParseTupleAndKeywords( - args, kwds, - "|OOOOOOOO", kwlist, - &prec, &rounding, &emin, &emax, &capitals, &clamp, &status, &traps - )) { - return -1; - } - - if (prec != Py_None && context_setprec(self, prec, NULL) < 0) { - return -1; - } - if (rounding != Py_None && context_setround(self, rounding, NULL) < 0) { - return -1; - } - if (emin != Py_None && context_setemin(self, emin, NULL) < 0) { - return -1; - } - if (emax != Py_None && context_setemax(self, emax, NULL) < 0) { - return -1; - } - if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) { - return -1; - } - if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) { - return -1; - } - - if (traps != Py_None) { - if (PyList_Check(traps)) { - ret = context_settraps_list(self, traps); - } -#ifdef EXTRA_FUNCTIONALITY - else if (PyLong_Check(traps)) { - ret = context_settraps(self, traps, NULL); - } -#endif - else { - ret = context_settraps_dict(self, traps); - } - if (ret < 0) { - return ret; - } - } - if (status != Py_None) { - if (PyList_Check(status)) { - ret = context_setstatus_list(self, status); - } -#ifdef EXTRA_FUNCTIONALITY - else if (PyLong_Check(status)) { - ret = context_setstatus(self, status, NULL); - } -#endif - else { - ret = context_setstatus_dict(self, status); - } - if (ret < 0) { - return ret; - } - } - - return 0; -} - -static PyObject * -context_repr(PyDecContextObject *self) -{ - mpd_context_t *ctx; - char flags[MPD_MAX_SIGNAL_LIST]; - char traps[MPD_MAX_SIGNAL_LIST]; - int n, mem; - - assert(PyDecContext_Check(self)); - ctx = CTX(self); - - mem = MPD_MAX_SIGNAL_LIST; - n = mpd_lsnprint_signals(flags, mem, ctx->status, dec_signal_string); - if (n < 0 || n >= mem) { - INTERNAL_ERROR_PTR("context_repr"); - } - - n = mpd_lsnprint_signals(traps, mem, ctx->traps, dec_signal_string); - if (n < 0 || n >= mem) { - INTERNAL_ERROR_PTR("context_repr"); - } - - return PyUnicode_FromFormat( - "Context(prec=%zd, rounding=%s, Emin=%zd, Emax=%zd, " - "capitals=%d, clamp=%d, flags=%s, traps=%s)", - ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, - self->capitals, ctx->clamp, flags, traps); -} - -static void -init_basic_context(PyObject *v) -{ - mpd_context_t ctx = dflt_ctx; - - ctx.prec = 9; - ctx.traps |= (MPD_Underflow|MPD_Clamped); - ctx.round = MPD_ROUND_HALF_UP; - - *CTX(v) = ctx; - CtxCaps(v) = 1; -} - -static void -init_extended_context(PyObject *v) -{ - mpd_context_t ctx = dflt_ctx; - - ctx.prec = 9; - ctx.traps = 0; - - *CTX(v) = ctx; - CtxCaps(v) = 1; -} - -#ifdef EXTRA_FUNCTIONALITY -/* Factory function for creating IEEE interchange format contexts */ -static PyObject * -ieee_context(PyObject *dummy UNUSED, PyObject *v) -{ - PyObject *context; - mpd_ssize_t bits; - mpd_context_t ctx; - - bits = PyLong_AsSsize_t(v); - if (bits == -1 && PyErr_Occurred()) { - return NULL; - } - if (bits <= 0 || bits > INT_MAX) { - goto error; - } - if (mpd_ieee_context(&ctx, (int)bits) < 0) { - goto error; - } - - context = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); - if (context == NULL) { - return NULL; - } - *CTX(context) = ctx; - - return context; - -error: - PyErr_Format(PyExc_ValueError, - "argument must be a multiple of 32, with a maximum of %d", - MPD_IEEE_CONTEXT_MAX_BITS); - - return NULL; -} -#endif - -static PyObject * -context_copy(PyObject *self, PyObject *args UNUSED) -{ - PyObject *copy; - - copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); - if (copy == NULL) { - return NULL; - } - - *CTX(copy) = *CTX(self); - CTX(copy)->newtrap = 0; - CtxCaps(copy) = CtxCaps(self); - - return copy; -} - -static PyObject * -context_reduce(PyObject *self, PyObject *args UNUSED) -{ - PyObject *flags; - PyObject *traps; - PyObject *ret; - mpd_context_t *ctx; - - ctx = CTX(self); - - flags = signals_as_list(ctx->status); - if (flags == NULL) { - return NULL; - } - traps = signals_as_list(ctx->traps); - if (traps == NULL) { - Py_DECREF(flags); - return NULL; - } - - ret = Py_BuildValue( - "O(nsnniiOO)", - Py_TYPE(self), - ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, - CtxCaps(self), ctx->clamp, flags, traps - ); - - Py_DECREF(flags); - Py_DECREF(traps); - return ret; -} - - -static PyGetSetDef context_getsets [] = -{ - { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL}, - { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL}, - { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL}, - { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL}, - { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL, NULL}, - { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL}, -#ifdef EXTRA_FUNCTIONALITY - { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL}, - { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL}, - { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL}, -#endif - {NULL} -}; - - -#define CONTEXT_CHECK(obj) \ - if (!PyDecContext_Check(obj)) { \ - PyErr_SetString(PyExc_TypeError, \ - "argument must be a context"); \ - return NULL; \ - } - -#define CONTEXT_CHECK_VA(obj) \ - if (obj == Py_None) { \ - CURRENT_CONTEXT(obj); \ - } \ - else if (!PyDecContext_Check(obj)) { \ - PyErr_SetString(PyExc_TypeError, \ - "optional argument must be a context"); \ - return NULL; \ - } - - -/******************************************************************************/ -/* Global, thread local and temporary contexts */ -/******************************************************************************/ - -/* - * Thread local storage currently has a speed penalty of about 4%. - * All functions that map Python's arithmetic operators to mpdecimal - * functions have to look up the current context for each and every - * operation. - */ - + Py_XDECREF(self->traps); + Py_XDECREF(self->flags); + Py_TYPE(self)->tp_free(self); +} + +static int +context_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = { + "prec", "rounding", "Emin", "Emax", "capitals", "clamp", + "flags", "traps", NULL + }; + PyObject *prec = Py_None; + PyObject *rounding = Py_None; + PyObject *emin = Py_None; + PyObject *emax = Py_None; + PyObject *capitals = Py_None; + PyObject *clamp = Py_None; + PyObject *status = Py_None; + PyObject *traps = Py_None; + int ret; + + assert(PyTuple_Check(args)); + + if (!PyArg_ParseTupleAndKeywords( + args, kwds, + "|OOOOOOOO", kwlist, + &prec, &rounding, &emin, &emax, &capitals, &clamp, &status, &traps + )) { + return -1; + } + + if (prec != Py_None && context_setprec(self, prec, NULL) < 0) { + return -1; + } + if (rounding != Py_None && context_setround(self, rounding, NULL) < 0) { + return -1; + } + if (emin != Py_None && context_setemin(self, emin, NULL) < 0) { + return -1; + } + if (emax != Py_None && context_setemax(self, emax, NULL) < 0) { + return -1; + } + if (capitals != Py_None && context_setcapitals(self, capitals, NULL) < 0) { + return -1; + } + if (clamp != Py_None && context_setclamp(self, clamp, NULL) < 0) { + return -1; + } + + if (traps != Py_None) { + if (PyList_Check(traps)) { + ret = context_settraps_list(self, traps); + } +#ifdef EXTRA_FUNCTIONALITY + else if (PyLong_Check(traps)) { + ret = context_settraps(self, traps, NULL); + } +#endif + else { + ret = context_settraps_dict(self, traps); + } + if (ret < 0) { + return ret; + } + } + if (status != Py_None) { + if (PyList_Check(status)) { + ret = context_setstatus_list(self, status); + } +#ifdef EXTRA_FUNCTIONALITY + else if (PyLong_Check(status)) { + ret = context_setstatus(self, status, NULL); + } +#endif + else { + ret = context_setstatus_dict(self, status); + } + if (ret < 0) { + return ret; + } + } + + return 0; +} + +static PyObject * +context_repr(PyDecContextObject *self) +{ + mpd_context_t *ctx; + char flags[MPD_MAX_SIGNAL_LIST]; + char traps[MPD_MAX_SIGNAL_LIST]; + int n, mem; + + assert(PyDecContext_Check(self)); + ctx = CTX(self); + + mem = MPD_MAX_SIGNAL_LIST; + n = mpd_lsnprint_signals(flags, mem, ctx->status, dec_signal_string); + if (n < 0 || n >= mem) { + INTERNAL_ERROR_PTR("context_repr"); + } + + n = mpd_lsnprint_signals(traps, mem, ctx->traps, dec_signal_string); + if (n < 0 || n >= mem) { + INTERNAL_ERROR_PTR("context_repr"); + } + + return PyUnicode_FromFormat( + "Context(prec=%zd, rounding=%s, Emin=%zd, Emax=%zd, " + "capitals=%d, clamp=%d, flags=%s, traps=%s)", + ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, + self->capitals, ctx->clamp, flags, traps); +} + +static void +init_basic_context(PyObject *v) +{ + mpd_context_t ctx = dflt_ctx; + + ctx.prec = 9; + ctx.traps |= (MPD_Underflow|MPD_Clamped); + ctx.round = MPD_ROUND_HALF_UP; + + *CTX(v) = ctx; + CtxCaps(v) = 1; +} + +static void +init_extended_context(PyObject *v) +{ + mpd_context_t ctx = dflt_ctx; + + ctx.prec = 9; + ctx.traps = 0; + + *CTX(v) = ctx; + CtxCaps(v) = 1; +} + +#ifdef EXTRA_FUNCTIONALITY +/* Factory function for creating IEEE interchange format contexts */ +static PyObject * +ieee_context(PyObject *dummy UNUSED, PyObject *v) +{ + PyObject *context; + mpd_ssize_t bits; + mpd_context_t ctx; + + bits = PyLong_AsSsize_t(v); + if (bits == -1 && PyErr_Occurred()) { + return NULL; + } + if (bits <= 0 || bits > INT_MAX) { + goto error; + } + if (mpd_ieee_context(&ctx, (int)bits) < 0) { + goto error; + } + + context = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); + if (context == NULL) { + return NULL; + } + *CTX(context) = ctx; + + return context; + +error: + PyErr_Format(PyExc_ValueError, + "argument must be a multiple of 32, with a maximum of %d", + MPD_IEEE_CONTEXT_MAX_BITS); + + return NULL; +} +#endif + +static PyObject * +context_copy(PyObject *self, PyObject *args UNUSED) +{ + PyObject *copy; + + copy = PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL); + if (copy == NULL) { + return NULL; + } + + *CTX(copy) = *CTX(self); + CTX(copy)->newtrap = 0; + CtxCaps(copy) = CtxCaps(self); + + return copy; +} + +static PyObject * +context_reduce(PyObject *self, PyObject *args UNUSED) +{ + PyObject *flags; + PyObject *traps; + PyObject *ret; + mpd_context_t *ctx; + + ctx = CTX(self); + + flags = signals_as_list(ctx->status); + if (flags == NULL) { + return NULL; + } + traps = signals_as_list(ctx->traps); + if (traps == NULL) { + Py_DECREF(flags); + return NULL; + } + + ret = Py_BuildValue( + "O(nsnniiOO)", + Py_TYPE(self), + ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax, + CtxCaps(self), ctx->clamp, flags, traps + ); + + Py_DECREF(flags); + Py_DECREF(traps); + return ret; +} + + +static PyGetSetDef context_getsets [] = +{ + { "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL}, + { "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL}, + { "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL}, + { "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL}, + { "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL, NULL}, + { "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL}, +#ifdef EXTRA_FUNCTIONALITY + { "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL}, + { "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL}, + { "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL}, +#endif + {NULL} +}; + + +#define CONTEXT_CHECK(obj) \ + if (!PyDecContext_Check(obj)) { \ + PyErr_SetString(PyExc_TypeError, \ + "argument must be a context"); \ + return NULL; \ + } + +#define CONTEXT_CHECK_VA(obj) \ + if (obj == Py_None) { \ + CURRENT_CONTEXT(obj); \ + } \ + else if (!PyDecContext_Check(obj)) { \ + PyErr_SetString(PyExc_TypeError, \ + "optional argument must be a context"); \ + return NULL; \ + } + + +/******************************************************************************/ +/* Global, thread local and temporary contexts */ +/******************************************************************************/ + +/* + * Thread local storage currently has a speed penalty of about 4%. + * All functions that map Python's arithmetic operators to mpdecimal + * functions have to look up the current context for each and every + * operation. + */ + #ifndef WITH_DECIMAL_CONTEXTVAR /* Get the context from the thread state dictionary. */ -static PyObject * +static PyObject * current_context_from_dict(void) { PyObject *dict; @@ -1635,3106 +1635,3106 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v) } #else static PyObject * -init_current_context(void) -{ - PyObject *tl_context = context_copy(default_context_template, NULL); - if (tl_context == NULL) { - return NULL; - } - CTX(tl_context)->status = 0; - - PyObject *tok = PyContextVar_Set(current_context_var, tl_context); - if (tok == NULL) { - Py_DECREF(tl_context); - return NULL; - } - Py_DECREF(tok); - - return tl_context; -} - -static inline PyObject * -current_context(void) -{ - PyObject *tl_context; - if (PyContextVar_Get(current_context_var, NULL, &tl_context) < 0) { - return NULL; - } - - if (tl_context != NULL) { - return tl_context; - } - - return init_current_context(); -} - -/* ctxobj := borrowed reference to the current context */ -#define CURRENT_CONTEXT(ctxobj) \ - ctxobj = current_context(); \ - if (ctxobj == NULL) { \ - return NULL; \ - } \ - Py_DECREF(ctxobj); - -/* Return a new reference to the current context */ -static PyObject * -PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED) -{ - return current_context(); -} - -/* Set the thread local context to a new context, decrement old reference */ -static PyObject * -PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v) -{ - CONTEXT_CHECK(v); - - /* If the new context is one of the templates, make a copy. - * This is the current behavior of decimal.py. */ - if (v == default_context_template || - v == basic_context_template || - v == extended_context_template) { - v = context_copy(v, NULL); - if (v == NULL) { - return NULL; - } - CTX(v)->status = 0; - } - else { - Py_INCREF(v); - } - - PyObject *tok = PyContextVar_Set(current_context_var, v); - Py_DECREF(v); - if (tok == NULL) { - return NULL; - } - Py_DECREF(tok); - - Py_RETURN_NONE; -} +init_current_context(void) +{ + PyObject *tl_context = context_copy(default_context_template, NULL); + if (tl_context == NULL) { + return NULL; + } + CTX(tl_context)->status = 0; + + PyObject *tok = PyContextVar_Set(current_context_var, tl_context); + if (tok == NULL) { + Py_DECREF(tl_context); + return NULL; + } + Py_DECREF(tok); + + return tl_context; +} + +static inline PyObject * +current_context(void) +{ + PyObject *tl_context; + if (PyContextVar_Get(current_context_var, NULL, &tl_context) < 0) { + return NULL; + } + + if (tl_context != NULL) { + return tl_context; + } + + return init_current_context(); +} + +/* ctxobj := borrowed reference to the current context */ +#define CURRENT_CONTEXT(ctxobj) \ + ctxobj = current_context(); \ + if (ctxobj == NULL) { \ + return NULL; \ + } \ + Py_DECREF(ctxobj); + +/* Return a new reference to the current context */ +static PyObject * +PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED) +{ + return current_context(); +} + +/* Set the thread local context to a new context, decrement old reference */ +static PyObject * +PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v) +{ + CONTEXT_CHECK(v); + + /* If the new context is one of the templates, make a copy. + * This is the current behavior of decimal.py. */ + if (v == default_context_template || + v == basic_context_template || + v == extended_context_template) { + v = context_copy(v, NULL); + if (v == NULL) { + return NULL; + } + CTX(v)->status = 0; + } + else { + Py_INCREF(v); + } + + PyObject *tok = PyContextVar_Set(current_context_var, v); + Py_DECREF(v); + if (tok == NULL) { + return NULL; + } + Py_DECREF(tok); + + Py_RETURN_NONE; +} #endif - -/* Context manager object for the 'with' statement. The manager - * owns one reference to the global (outer) context and one - * to the local (inner) context. */ -static PyObject * -ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"ctx", NULL}; - PyDecContextManagerObject *self; - PyObject *local = Py_None; - PyObject *global; - - CURRENT_CONTEXT(global); - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &local)) { - return NULL; - } - if (local == Py_None) { - local = global; - } - else if (!PyDecContext_Check(local)) { - PyErr_SetString(PyExc_TypeError, - "optional argument must be a context"); - return NULL; - } - - self = PyObject_New(PyDecContextManagerObject, - &PyDecContextManager_Type); - if (self == NULL) { - return NULL; - } - - self->local = context_copy(local, NULL); - if (self->local == NULL) { - self->global = NULL; - Py_DECREF(self); - return NULL; - } - self->global = global; - Py_INCREF(self->global); - - return (PyObject *)self; -} - -static void -ctxmanager_dealloc(PyDecContextManagerObject *self) -{ - Py_XDECREF(self->local); - Py_XDECREF(self->global); - PyObject_Del(self); -} - -static PyObject * -ctxmanager_set_local(PyDecContextManagerObject *self, PyObject *args UNUSED) -{ - PyObject *ret; - - ret = PyDec_SetCurrentContext(NULL, self->local); - if (ret == NULL) { - return NULL; - } - Py_DECREF(ret); - - Py_INCREF(self->local); - return self->local; -} - -static PyObject * -ctxmanager_restore_global(PyDecContextManagerObject *self, - PyObject *args UNUSED) -{ - PyObject *ret; - - ret = PyDec_SetCurrentContext(NULL, self->global); - if (ret == NULL) { - return NULL; - } - Py_DECREF(ret); - - Py_RETURN_NONE; -} - - -static PyMethodDef ctxmanager_methods[] = { - {"__enter__", (PyCFunction)ctxmanager_set_local, METH_NOARGS, NULL}, - {"__exit__", (PyCFunction)ctxmanager_restore_global, METH_VARARGS, NULL}, - {NULL, NULL} -}; - -static PyTypeObject PyDecContextManager_Type = -{ - PyVarObject_HEAD_INIT(NULL, 0) - "decimal.ContextManager", /* tp_name */ - sizeof(PyDecContextManagerObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor) ctxmanager_dealloc, /* tp_dealloc */ + +/* Context manager object for the 'with' statement. The manager + * owns one reference to the global (outer) context and one + * to the local (inner) context. */ +static PyObject * +ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"ctx", NULL}; + PyDecContextManagerObject *self; + PyObject *local = Py_None; + PyObject *global; + + CURRENT_CONTEXT(global); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &local)) { + return NULL; + } + if (local == Py_None) { + local = global; + } + else if (!PyDecContext_Check(local)) { + PyErr_SetString(PyExc_TypeError, + "optional argument must be a context"); + return NULL; + } + + self = PyObject_New(PyDecContextManagerObject, + &PyDecContextManager_Type); + if (self == NULL) { + return NULL; + } + + self->local = context_copy(local, NULL); + if (self->local == NULL) { + self->global = NULL; + Py_DECREF(self); + return NULL; + } + self->global = global; + Py_INCREF(self->global); + + return (PyObject *)self; +} + +static void +ctxmanager_dealloc(PyDecContextManagerObject *self) +{ + Py_XDECREF(self->local); + Py_XDECREF(self->global); + PyObject_Del(self); +} + +static PyObject * +ctxmanager_set_local(PyDecContextManagerObject *self, PyObject *args UNUSED) +{ + PyObject *ret; + + ret = PyDec_SetCurrentContext(NULL, self->local); + if (ret == NULL) { + return NULL; + } + Py_DECREF(ret); + + Py_INCREF(self->local); + return self->local; +} + +static PyObject * +ctxmanager_restore_global(PyDecContextManagerObject *self, + PyObject *args UNUSED) +{ + PyObject *ret; + + ret = PyDec_SetCurrentContext(NULL, self->global); + if (ret == NULL) { + return NULL; + } + Py_DECREF(ret); + + Py_RETURN_NONE; +} + + +static PyMethodDef ctxmanager_methods[] = { + {"__enter__", (PyCFunction)ctxmanager_set_local, METH_NOARGS, NULL}, + {"__exit__", (PyCFunction)ctxmanager_restore_global, METH_VARARGS, NULL}, + {NULL, NULL} +}; + +static PyTypeObject PyDecContextManager_Type = +{ + PyVarObject_HEAD_INIT(NULL, 0) + "decimal.ContextManager", /* tp_name */ + sizeof(PyDecContextManagerObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor) ctxmanager_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - (getattrfunc) 0, /* tp_getattr */ - (setattrfunc) 0, /* tp_setattr */ + (getattrfunc) 0, /* tp_getattr */ + (setattrfunc) 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc) 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ - (setattrofunc) 0, /* tp_setattro */ - (PyBufferProcs *) 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT, /* tp_flags */ - 0, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - ctxmanager_methods, /* tp_methods */ -}; - - -/******************************************************************************/ -/* New Decimal Object */ -/******************************************************************************/ - -static PyObject * -PyDecType_New(PyTypeObject *type) -{ - PyDecObject *dec; - - if (type == &PyDec_Type) { - dec = PyObject_New(PyDecObject, &PyDec_Type); - } - else { - dec = (PyDecObject *)type->tp_alloc(type, 0); - } - if (dec == NULL) { - return NULL; - } - - dec->hash = -1; - - MPD(dec)->flags = MPD_STATIC|MPD_STATIC_DATA; - MPD(dec)->exp = 0; - MPD(dec)->digits = 0; - MPD(dec)->len = 0; - MPD(dec)->alloc = _Py_DEC_MINALLOC; - MPD(dec)->data = dec->data; - - return (PyObject *)dec; -} -#define dec_alloc() PyDecType_New(&PyDec_Type) - -static void -dec_dealloc(PyObject *dec) -{ - mpd_del(MPD(dec)); - Py_TYPE(dec)->tp_free(dec); -} - - -/******************************************************************************/ -/* Conversions to Decimal */ -/******************************************************************************/ - -Py_LOCAL_INLINE(int) + (reprfunc) 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ + (setattrofunc) 0, /* tp_setattro */ + (PyBufferProcs *) 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT, /* tp_flags */ + 0, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + ctxmanager_methods, /* tp_methods */ +}; + + +/******************************************************************************/ +/* New Decimal Object */ +/******************************************************************************/ + +static PyObject * +PyDecType_New(PyTypeObject *type) +{ + PyDecObject *dec; + + if (type == &PyDec_Type) { + dec = PyObject_New(PyDecObject, &PyDec_Type); + } + else { + dec = (PyDecObject *)type->tp_alloc(type, 0); + } + if (dec == NULL) { + return NULL; + } + + dec->hash = -1; + + MPD(dec)->flags = MPD_STATIC|MPD_STATIC_DATA; + MPD(dec)->exp = 0; + MPD(dec)->digits = 0; + MPD(dec)->len = 0; + MPD(dec)->alloc = _Py_DEC_MINALLOC; + MPD(dec)->data = dec->data; + + return (PyObject *)dec; +} +#define dec_alloc() PyDecType_New(&PyDec_Type) + +static void +dec_dealloc(PyObject *dec) +{ + mpd_del(MPD(dec)); + Py_TYPE(dec)->tp_free(dec); +} + + +/******************************************************************************/ +/* Conversions to Decimal */ +/******************************************************************************/ + +Py_LOCAL_INLINE(int) is_space(enum PyUnicode_Kind kind, const void *data, Py_ssize_t pos) -{ - Py_UCS4 ch = PyUnicode_READ(kind, data, pos); - return Py_UNICODE_ISSPACE(ch); -} - -/* Return the ASCII representation of a numeric Unicode string. The numeric - string may contain ascii characters in the range [1, 127], any Unicode - space and any unicode digit. If strip_ws is true, leading and trailing - whitespace is stripped. If ignore_underscores is true, underscores are - ignored. - - Return NULL if malloc fails and an empty string if invalid characters - are found. */ -static char * -numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores) -{ - enum PyUnicode_Kind kind; +{ + Py_UCS4 ch = PyUnicode_READ(kind, data, pos); + return Py_UNICODE_ISSPACE(ch); +} + +/* Return the ASCII representation of a numeric Unicode string. The numeric + string may contain ascii characters in the range [1, 127], any Unicode + space and any unicode digit. If strip_ws is true, leading and trailing + whitespace is stripped. If ignore_underscores is true, underscores are + ignored. + + Return NULL if malloc fails and an empty string if invalid characters + are found. */ +static char * +numeric_as_ascii(const PyObject *u, int strip_ws, int ignore_underscores) +{ + enum PyUnicode_Kind kind; const void *data; - Py_UCS4 ch; - char *res, *cp; - Py_ssize_t j, len; - int d; - - if (PyUnicode_READY(u) == -1) { - return NULL; - } - - kind = PyUnicode_KIND(u); - data = PyUnicode_DATA(u); - len = PyUnicode_GET_LENGTH(u); - - cp = res = PyMem_Malloc(len+1); - if (res == NULL) { - PyErr_NoMemory(); - return NULL; - } - - j = 0; - if (strip_ws) { - while (len > 0 && is_space(kind, data, len-1)) { - len--; - } - while (j < len && is_space(kind, data, j)) { - j++; - } - } - - for (; j < len; j++) { - ch = PyUnicode_READ(kind, data, j); - if (ignore_underscores && ch == '_') { - continue; - } - if (0 < ch && ch <= 127) { - *cp++ = ch; - continue; - } - if (Py_UNICODE_ISSPACE(ch)) { - *cp++ = ' '; - continue; - } - d = Py_UNICODE_TODECIMAL(ch); - if (d < 0) { - /* empty string triggers ConversionSyntax */ - *res = '\0'; - return res; - } - *cp++ = '0' + d; - } - *cp = '\0'; - return res; -} - -/* Return a new PyDecObject or a subtype from a C string. Use the context - during conversion. */ -static PyObject * -PyDecType_FromCString(PyTypeObject *type, const char *s, - PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - mpd_qset_string(MPD(dec), s, CTX(context), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - return dec; -} - -/* Return a new PyDecObject or a subtype from a C string. Attempt exact - conversion. If the operand cannot be converted exactly, set - InvalidOperation. */ -static PyObject * -PyDecType_FromCStringExact(PyTypeObject *type, const char *s, - PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - mpd_context_t maxctx; - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - mpd_maxcontext(&maxctx); - - mpd_qset_string(MPD(dec), s, &maxctx, &status); - if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { - /* we want exact results */ - mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); - } - status &= MPD_Errors; - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - - return dec; -} - -/* Return a new PyDecObject or a subtype from a PyUnicodeObject. */ -static PyObject * -PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u, - PyObject *context) -{ - PyObject *dec; - char *s; - - s = numeric_as_ascii(u, 0, 0); - if (s == NULL) { - return NULL; - } - - dec = PyDecType_FromCString(type, s, context); - PyMem_Free(s); - return dec; -} - -/* Return a new PyDecObject or a subtype from a PyUnicodeObject. Attempt exact - * conversion. If the conversion is not exact, fail with InvalidOperation. - * Allow leading and trailing whitespace in the input operand. */ -static PyObject * -PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u, - PyObject *context) -{ - PyObject *dec; - char *s; - - s = numeric_as_ascii(u, 1, 1); - if (s == NULL) { - return NULL; - } - - dec = PyDecType_FromCStringExact(type, s, context); - PyMem_Free(s); - return dec; -} - -/* Set PyDecObject from triple without any error checking. */ -Py_LOCAL_INLINE(void) -_dec_settriple(PyObject *dec, uint8_t sign, uint32_t v, mpd_ssize_t exp) -{ - -#ifdef CONFIG_64 - MPD(dec)->data[0] = v; - MPD(dec)->len = 1; -#else - uint32_t q, r; - q = v / MPD_RADIX; - r = v - q * MPD_RADIX; - MPD(dec)->data[1] = q; - MPD(dec)->data[0] = r; - MPD(dec)->len = q ? 2 : 1; -#endif - mpd_set_flags(MPD(dec), sign); - MPD(dec)->exp = exp; - mpd_setdigits(MPD(dec)); -} - -/* Return a new PyDecObject from an mpd_ssize_t. */ -static PyObject * -PyDecType_FromSsize(PyTypeObject *type, mpd_ssize_t v, PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - mpd_qset_ssize(MPD(dec), v, CTX(context), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - return dec; -} - -/* Return a new PyDecObject from an mpd_ssize_t. Conversion is exact. */ -static PyObject * -PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - mpd_context_t maxctx; - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - mpd_maxcontext(&maxctx); - - mpd_qset_ssize(MPD(dec), v, &maxctx, &status); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - return dec; -} - -/* Convert from a PyLongObject. The context is not modified; flags set - during conversion are accumulated in the status parameter. */ -static PyObject * -dec_from_long(PyTypeObject *type, const PyObject *v, - const mpd_context_t *ctx, uint32_t *status) -{ - PyObject *dec; - PyLongObject *l = (PyLongObject *)v; - Py_ssize_t ob_size; - size_t len; - uint8_t sign; - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - ob_size = Py_SIZE(l); - if (ob_size == 0) { - _dec_settriple(dec, MPD_POS, 0, 0); - return dec; - } - - if (ob_size < 0) { - len = -ob_size; - sign = MPD_NEG; - } - else { - len = ob_size; - sign = MPD_POS; - } - - if (len == 1) { - _dec_settriple(dec, sign, *l->ob_digit, 0); - mpd_qfinalize(MPD(dec), ctx, status); - return dec; - } - -#if PYLONG_BITS_IN_DIGIT == 30 - mpd_qimport_u32(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, - ctx, status); -#elif PYLONG_BITS_IN_DIGIT == 15 - mpd_qimport_u16(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, - ctx, status); -#else - #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" -#endif - - return dec; -} - -/* Return a new PyDecObject from a PyLongObject. Use the context for - conversion. */ -static PyObject * -PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - - if (!PyLong_Check(v)) { - PyErr_SetString(PyExc_TypeError, "argument must be an integer"); - return NULL; - } - - dec = dec_from_long(type, v, CTX(context), &status); - if (dec == NULL) { - return NULL; - } - - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - - return dec; -} - -/* Return a new PyDecObject from a PyLongObject. Use a maximum context - for conversion. If the conversion is not exact, set InvalidOperation. */ -static PyObject * -PyDecType_FromLongExact(PyTypeObject *type, const PyObject *v, - PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - mpd_context_t maxctx; - - if (!PyLong_Check(v)) { - PyErr_SetString(PyExc_TypeError, "argument must be an integer"); - return NULL; - } - - mpd_maxcontext(&maxctx); - dec = dec_from_long(type, v, &maxctx, &status); - if (dec == NULL) { - return NULL; - } - - if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { - /* we want exact results */ - mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); - } - status &= MPD_Errors; - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - - return dec; -} - -/* External C-API functions */ -static binaryfunc _py_long_multiply; -static binaryfunc _py_long_floor_divide; -static ternaryfunc _py_long_power; -static unaryfunc _py_float_abs; -static PyCFunction _py_long_bit_length; -static PyCFunction _py_float_as_integer_ratio; - -/* Return a PyDecObject or a subtype from a PyFloatObject. - Conversion is exact. */ -static PyObject * -PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v, - PyObject *context) -{ - PyObject *dec, *tmp; - PyObject *n, *d, *n_d; - mpd_ssize_t k; - double x; - int sign; - mpd_t *d1, *d2; - uint32_t status = 0; - mpd_context_t maxctx; - - - assert(PyType_IsSubtype(type, &PyDec_Type)); - - if (PyLong_Check(v)) { - return PyDecType_FromLongExact(type, v, context); - } - if (!PyFloat_Check(v)) { - PyErr_SetString(PyExc_TypeError, - "argument must be int or float"); - return NULL; - } - - x = PyFloat_AsDouble(v); - if (x == -1.0 && PyErr_Occurred()) { - return NULL; - } - sign = (copysign(1.0, x) == 1.0) ? 0 : 1; - - if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) { - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - if (Py_IS_NAN(x)) { - /* decimal.py calls repr(float(+-nan)), - * which always gives a positive result. */ - mpd_setspecial(MPD(dec), MPD_POS, MPD_NAN); - } - else { - mpd_setspecial(MPD(dec), sign, MPD_INF); - } - return dec; - } - - /* absolute value of the float */ - tmp = _py_float_abs(v); - if (tmp == NULL) { - return NULL; - } - - /* float as integer ratio: numerator/denominator */ - n_d = _py_float_as_integer_ratio(tmp, NULL); - Py_DECREF(tmp); - if (n_d == NULL) { - return NULL; - } - n = PyTuple_GET_ITEM(n_d, 0); - d = PyTuple_GET_ITEM(n_d, 1); - - tmp = _py_long_bit_length(d, NULL); - if (tmp == NULL) { - Py_DECREF(n_d); - return NULL; - } - k = PyLong_AsSsize_t(tmp); - Py_DECREF(tmp); - if (k == -1 && PyErr_Occurred()) { - Py_DECREF(n_d); - return NULL; - } - k--; - - dec = PyDecType_FromLongExact(type, n, context); - Py_DECREF(n_d); - if (dec == NULL) { - return NULL; - } - - d1 = mpd_qnew(); - if (d1 == NULL) { - Py_DECREF(dec); - PyErr_NoMemory(); - return NULL; - } - d2 = mpd_qnew(); - if (d2 == NULL) { - mpd_del(d1); - Py_DECREF(dec); - PyErr_NoMemory(); - return NULL; - } - - mpd_maxcontext(&maxctx); - mpd_qset_uint(d1, 5, &maxctx, &status); - mpd_qset_ssize(d2, k, &maxctx, &status); - mpd_qpow(d1, d1, d2, &maxctx, &status); - if (dec_addstatus(context, status)) { - mpd_del(d1); - mpd_del(d2); - Py_DECREF(dec); - return NULL; - } - - /* result = n * 5**k */ - mpd_qmul(MPD(dec), MPD(dec), d1, &maxctx, &status); - mpd_del(d1); - mpd_del(d2); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - /* result = +- n * 5**k * 10**-k */ - mpd_set_sign(MPD(dec), sign); - MPD(dec)->exp = -k; - - return dec; -} - -static PyObject * -PyDecType_FromFloat(PyTypeObject *type, PyObject *v, - PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - - dec = PyDecType_FromFloatExact(type, v, context); - if (dec == NULL) { - return NULL; - } - - mpd_qfinalize(MPD(dec), CTX(context), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - - return dec; -} - -/* Return a new PyDecObject or a subtype from a Decimal. */ -static PyObject * -PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context) -{ - PyObject *dec; - uint32_t status = 0; - - if (type == &PyDec_Type && PyDec_CheckExact(v)) { - Py_INCREF(v); - return v; - } - - dec = PyDecType_New(type); - if (dec == NULL) { - return NULL; - } - - mpd_qcopy(MPD(dec), MPD(v), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(dec); - return NULL; - } - - return dec; -} - -static PyObject * -sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg) -{ - if (PyTuple_Check(v)) { - Py_INCREF(v); - return v; - } - if (PyList_Check(v)) { - return PyList_AsTuple(v); - } - - PyErr_SetString(ex, mesg); - return NULL; -} - -/* Return a new C string representation of a DecimalTuple. */ -static char * -dectuple_as_str(PyObject *dectuple) -{ - PyObject *digits = NULL, *tmp; - char *decstring = NULL; - char sign_special[6]; - char *cp; - long sign, l; - mpd_ssize_t exp = 0; - Py_ssize_t i, mem, tsize; - int is_infinite = 0; - int n; - - assert(PyTuple_Check(dectuple)); - - if (PyTuple_Size(dectuple) != 3) { - PyErr_SetString(PyExc_ValueError, - "argument must be a sequence of length 3"); - goto error; - } - - /* sign */ - tmp = PyTuple_GET_ITEM(dectuple, 0); - if (!PyLong_Check(tmp)) { - PyErr_SetString(PyExc_ValueError, - "sign must be an integer with the value 0 or 1"); - goto error; - } - sign = PyLong_AsLong(tmp); - if (sign == -1 && PyErr_Occurred()) { - goto error; - } - if (sign != 0 && sign != 1) { - PyErr_SetString(PyExc_ValueError, - "sign must be an integer with the value 0 or 1"); - goto error; - } - sign_special[0] = sign ? '-' : '+'; - sign_special[1] = '\0'; - - /* exponent or encoding for a special number */ - tmp = PyTuple_GET_ITEM(dectuple, 2); - if (PyUnicode_Check(tmp)) { - /* special */ - if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) { - strcat(sign_special, "Inf"); - is_infinite = 1; - } - else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) { - strcat(sign_special, "NaN"); - } - else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) { - strcat(sign_special, "sNaN"); - } - else { - PyErr_SetString(PyExc_ValueError, - "string argument in the third position " - "must be 'F', 'n' or 'N'"); - goto error; - } - } - else { - /* exponent */ - if (!PyLong_Check(tmp)) { - PyErr_SetString(PyExc_ValueError, - "exponent must be an integer"); - goto error; - } - exp = PyLong_AsSsize_t(tmp); - if (exp == -1 && PyErr_Occurred()) { - goto error; - } - } - - /* coefficient */ - digits = sequence_as_tuple(PyTuple_GET_ITEM(dectuple, 1), PyExc_ValueError, - "coefficient must be a tuple of digits"); - if (digits == NULL) { - goto error; - } - - tsize = PyTuple_Size(digits); - /* [sign][coeffdigits+1][E][-][expdigits+1]['\0'] */ - mem = 1 + tsize + 3 + MPD_EXPDIGITS + 2; - cp = decstring = PyMem_Malloc(mem); - if (decstring == NULL) { - PyErr_NoMemory(); - goto error; - } - - n = snprintf(cp, mem, "%s", sign_special); - if (n < 0 || n >= mem) { - PyErr_SetString(PyExc_RuntimeError, - "internal error in dec_sequence_as_str"); - goto error; - } - cp += n; - - if (tsize == 0 && sign_special[1] == '\0') { - /* empty tuple: zero coefficient, except for special numbers */ - *cp++ = '0'; - } - for (i = 0; i < tsize; i++) { - tmp = PyTuple_GET_ITEM(digits, i); - if (!PyLong_Check(tmp)) { - PyErr_SetString(PyExc_ValueError, - "coefficient must be a tuple of digits"); - goto error; - } - l = PyLong_AsLong(tmp); - if (l == -1 && PyErr_Occurred()) { - goto error; - } - if (l < 0 || l > 9) { - PyErr_SetString(PyExc_ValueError, - "coefficient must be a tuple of digits"); - goto error; - } - if (is_infinite) { - /* accept but ignore any well-formed coefficient for compatibility - with decimal.py */ - continue; - } - *cp++ = (char)l + '0'; - } - *cp = '\0'; - - if (sign_special[1] == '\0') { - /* not a special number */ - *cp++ = 'E'; - n = snprintf(cp, MPD_EXPDIGITS+2, "%" PRI_mpd_ssize_t, exp); - if (n < 0 || n >= MPD_EXPDIGITS+2) { - PyErr_SetString(PyExc_RuntimeError, - "internal error in dec_sequence_as_str"); - goto error; - } - } - - Py_XDECREF(digits); - return decstring; - - -error: - Py_XDECREF(digits); - if (decstring) PyMem_Free(decstring); - return NULL; -} - -/* Currently accepts tuples and lists. */ -static PyObject * -PyDecType_FromSequence(PyTypeObject *type, PyObject *v, - PyObject *context) -{ - PyObject *dectuple; - PyObject *dec; - char *s; - - dectuple = sequence_as_tuple(v, PyExc_TypeError, - "argument must be a tuple or list"); - if (dectuple == NULL) { - return NULL; - } - - s = dectuple_as_str(dectuple); - Py_DECREF(dectuple); - if (s == NULL) { - return NULL; - } - - dec = PyDecType_FromCString(type, s, context); - - PyMem_Free(s); - return dec; -} - -/* Currently accepts tuples and lists. */ -static PyObject * -PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v, - PyObject *context) -{ - PyObject *dectuple; - PyObject *dec; - char *s; - - dectuple = sequence_as_tuple(v, PyExc_TypeError, - "argument must be a tuple or list"); - if (dectuple == NULL) { - return NULL; - } - - s = dectuple_as_str(dectuple); - Py_DECREF(dectuple); - if (s == NULL) { - return NULL; - } - - dec = PyDecType_FromCStringExact(type, s, context); - - PyMem_Free(s); - return dec; -} - -#define PyDec_FromCString(str, context) \ - PyDecType_FromCString(&PyDec_Type, str, context) -#define PyDec_FromCStringExact(str, context) \ - PyDecType_FromCStringExact(&PyDec_Type, str, context) - -#define PyDec_FromUnicode(unicode, context) \ - PyDecType_FromUnicode(&PyDec_Type, unicode, context) -#define PyDec_FromUnicodeExact(unicode, context) \ - PyDecType_FromUnicodeExact(&PyDec_Type, unicode, context) -#define PyDec_FromUnicodeExactWS(unicode, context) \ - PyDecType_FromUnicodeExactWS(&PyDec_Type, unicode, context) - -#define PyDec_FromSsize(v, context) \ - PyDecType_FromSsize(&PyDec_Type, v, context) -#define PyDec_FromSsizeExact(v, context) \ - PyDecType_FromSsizeExact(&PyDec_Type, v, context) - -#define PyDec_FromLong(pylong, context) \ - PyDecType_FromLong(&PyDec_Type, pylong, context) -#define PyDec_FromLongExact(pylong, context) \ - PyDecType_FromLongExact(&PyDec_Type, pylong, context) - -#define PyDec_FromFloat(pyfloat, context) \ - PyDecType_FromFloat(&PyDec_Type, pyfloat, context) -#define PyDec_FromFloatExact(pyfloat, context) \ - PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context) - -#define PyDec_FromSequence(sequence, context) \ - PyDecType_FromSequence(&PyDec_Type, sequence, context) -#define PyDec_FromSequenceExact(sequence, context) \ - PyDecType_FromSequenceExact(&PyDec_Type, sequence, context) - -/* class method */ -static PyObject * -dec_from_float(PyObject *type, PyObject *pyfloat) -{ - PyObject *context; - PyObject *result; - - CURRENT_CONTEXT(context); - result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context); - if (type != (PyObject *)&PyDec_Type && result != NULL) { - Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL)); - } - - return result; -} - -/* create_decimal_from_float */ -static PyObject * -ctx_from_float(PyObject *context, PyObject *v) -{ - return PyDec_FromFloat(v, context); -} - -/* Apply the context to the input operand. Return a new PyDecObject. */ -static PyObject * -dec_apply(PyObject *v, PyObject *context) -{ - PyObject *result; - uint32_t status = 0; - - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - mpd_qcopy(MPD(result), MPD(v), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - mpd_qfinalize(MPD(result), CTX(context), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -/* 'v' can have any type accepted by the Decimal constructor. Attempt - an exact conversion. If the result does not meet the restrictions - for an mpd_t, fail with InvalidOperation. */ -static PyObject * -PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context) -{ - if (v == NULL) { - return PyDecType_FromSsizeExact(type, 0, context); - } - else if (PyDec_Check(v)) { - return PyDecType_FromDecimalExact(type, v, context); - } - else if (PyUnicode_Check(v)) { - return PyDecType_FromUnicodeExactWS(type, v, context); - } - else if (PyLong_Check(v)) { - return PyDecType_FromLongExact(type, v, context); - } - else if (PyTuple_Check(v) || PyList_Check(v)) { - return PyDecType_FromSequenceExact(type, v, context); - } - else if (PyFloat_Check(v)) { - if (dec_addstatus(context, MPD_Float_operation)) { - return NULL; - } - return PyDecType_FromFloatExact(type, v, context); - } - else { - PyErr_Format(PyExc_TypeError, - "conversion from %s to Decimal is not supported", + Py_UCS4 ch; + char *res, *cp; + Py_ssize_t j, len; + int d; + + if (PyUnicode_READY(u) == -1) { + return NULL; + } + + kind = PyUnicode_KIND(u); + data = PyUnicode_DATA(u); + len = PyUnicode_GET_LENGTH(u); + + cp = res = PyMem_Malloc(len+1); + if (res == NULL) { + PyErr_NoMemory(); + return NULL; + } + + j = 0; + if (strip_ws) { + while (len > 0 && is_space(kind, data, len-1)) { + len--; + } + while (j < len && is_space(kind, data, j)) { + j++; + } + } + + for (; j < len; j++) { + ch = PyUnicode_READ(kind, data, j); + if (ignore_underscores && ch == '_') { + continue; + } + if (0 < ch && ch <= 127) { + *cp++ = ch; + continue; + } + if (Py_UNICODE_ISSPACE(ch)) { + *cp++ = ' '; + continue; + } + d = Py_UNICODE_TODECIMAL(ch); + if (d < 0) { + /* empty string triggers ConversionSyntax */ + *res = '\0'; + return res; + } + *cp++ = '0' + d; + } + *cp = '\0'; + return res; +} + +/* Return a new PyDecObject or a subtype from a C string. Use the context + during conversion. */ +static PyObject * +PyDecType_FromCString(PyTypeObject *type, const char *s, + PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + mpd_qset_string(MPD(dec), s, CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + return dec; +} + +/* Return a new PyDecObject or a subtype from a C string. Attempt exact + conversion. If the operand cannot be converted exactly, set + InvalidOperation. */ +static PyObject * +PyDecType_FromCStringExact(PyTypeObject *type, const char *s, + PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + mpd_context_t maxctx; + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + mpd_maxcontext(&maxctx); + + mpd_qset_string(MPD(dec), s, &maxctx, &status); + if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { + /* we want exact results */ + mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); + } + status &= MPD_Errors; + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + + return dec; +} + +/* Return a new PyDecObject or a subtype from a PyUnicodeObject. */ +static PyObject * +PyDecType_FromUnicode(PyTypeObject *type, const PyObject *u, + PyObject *context) +{ + PyObject *dec; + char *s; + + s = numeric_as_ascii(u, 0, 0); + if (s == NULL) { + return NULL; + } + + dec = PyDecType_FromCString(type, s, context); + PyMem_Free(s); + return dec; +} + +/* Return a new PyDecObject or a subtype from a PyUnicodeObject. Attempt exact + * conversion. If the conversion is not exact, fail with InvalidOperation. + * Allow leading and trailing whitespace in the input operand. */ +static PyObject * +PyDecType_FromUnicodeExactWS(PyTypeObject *type, const PyObject *u, + PyObject *context) +{ + PyObject *dec; + char *s; + + s = numeric_as_ascii(u, 1, 1); + if (s == NULL) { + return NULL; + } + + dec = PyDecType_FromCStringExact(type, s, context); + PyMem_Free(s); + return dec; +} + +/* Set PyDecObject from triple without any error checking. */ +Py_LOCAL_INLINE(void) +_dec_settriple(PyObject *dec, uint8_t sign, uint32_t v, mpd_ssize_t exp) +{ + +#ifdef CONFIG_64 + MPD(dec)->data[0] = v; + MPD(dec)->len = 1; +#else + uint32_t q, r; + q = v / MPD_RADIX; + r = v - q * MPD_RADIX; + MPD(dec)->data[1] = q; + MPD(dec)->data[0] = r; + MPD(dec)->len = q ? 2 : 1; +#endif + mpd_set_flags(MPD(dec), sign); + MPD(dec)->exp = exp; + mpd_setdigits(MPD(dec)); +} + +/* Return a new PyDecObject from an mpd_ssize_t. */ +static PyObject * +PyDecType_FromSsize(PyTypeObject *type, mpd_ssize_t v, PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + mpd_qset_ssize(MPD(dec), v, CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + return dec; +} + +/* Return a new PyDecObject from an mpd_ssize_t. Conversion is exact. */ +static PyObject * +PyDecType_FromSsizeExact(PyTypeObject *type, mpd_ssize_t v, PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + mpd_context_t maxctx; + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + mpd_maxcontext(&maxctx); + + mpd_qset_ssize(MPD(dec), v, &maxctx, &status); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + return dec; +} + +/* Convert from a PyLongObject. The context is not modified; flags set + during conversion are accumulated in the status parameter. */ +static PyObject * +dec_from_long(PyTypeObject *type, const PyObject *v, + const mpd_context_t *ctx, uint32_t *status) +{ + PyObject *dec; + PyLongObject *l = (PyLongObject *)v; + Py_ssize_t ob_size; + size_t len; + uint8_t sign; + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + ob_size = Py_SIZE(l); + if (ob_size == 0) { + _dec_settriple(dec, MPD_POS, 0, 0); + return dec; + } + + if (ob_size < 0) { + len = -ob_size; + sign = MPD_NEG; + } + else { + len = ob_size; + sign = MPD_POS; + } + + if (len == 1) { + _dec_settriple(dec, sign, *l->ob_digit, 0); + mpd_qfinalize(MPD(dec), ctx, status); + return dec; + } + +#if PYLONG_BITS_IN_DIGIT == 30 + mpd_qimport_u32(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, + ctx, status); +#elif PYLONG_BITS_IN_DIGIT == 15 + mpd_qimport_u16(MPD(dec), l->ob_digit, len, sign, PyLong_BASE, + ctx, status); +#else + #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" +#endif + + return dec; +} + +/* Return a new PyDecObject from a PyLongObject. Use the context for + conversion. */ +static PyObject * +PyDecType_FromLong(PyTypeObject *type, const PyObject *v, PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "argument must be an integer"); + return NULL; + } + + dec = dec_from_long(type, v, CTX(context), &status); + if (dec == NULL) { + return NULL; + } + + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + + return dec; +} + +/* Return a new PyDecObject from a PyLongObject. Use a maximum context + for conversion. If the conversion is not exact, set InvalidOperation. */ +static PyObject * +PyDecType_FromLongExact(PyTypeObject *type, const PyObject *v, + PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + mpd_context_t maxctx; + + if (!PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, "argument must be an integer"); + return NULL; + } + + mpd_maxcontext(&maxctx); + dec = dec_from_long(type, v, &maxctx, &status); + if (dec == NULL) { + return NULL; + } + + if (status & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { + /* we want exact results */ + mpd_seterror(MPD(dec), MPD_Invalid_operation, &status); + } + status &= MPD_Errors; + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + + return dec; +} + +/* External C-API functions */ +static binaryfunc _py_long_multiply; +static binaryfunc _py_long_floor_divide; +static ternaryfunc _py_long_power; +static unaryfunc _py_float_abs; +static PyCFunction _py_long_bit_length; +static PyCFunction _py_float_as_integer_ratio; + +/* Return a PyDecObject or a subtype from a PyFloatObject. + Conversion is exact. */ +static PyObject * +PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v, + PyObject *context) +{ + PyObject *dec, *tmp; + PyObject *n, *d, *n_d; + mpd_ssize_t k; + double x; + int sign; + mpd_t *d1, *d2; + uint32_t status = 0; + mpd_context_t maxctx; + + + assert(PyType_IsSubtype(type, &PyDec_Type)); + + if (PyLong_Check(v)) { + return PyDecType_FromLongExact(type, v, context); + } + if (!PyFloat_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "argument must be int or float"); + return NULL; + } + + x = PyFloat_AsDouble(v); + if (x == -1.0 && PyErr_Occurred()) { + return NULL; + } + sign = (copysign(1.0, x) == 1.0) ? 0 : 1; + + if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) { + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + if (Py_IS_NAN(x)) { + /* decimal.py calls repr(float(+-nan)), + * which always gives a positive result. */ + mpd_setspecial(MPD(dec), MPD_POS, MPD_NAN); + } + else { + mpd_setspecial(MPD(dec), sign, MPD_INF); + } + return dec; + } + + /* absolute value of the float */ + tmp = _py_float_abs(v); + if (tmp == NULL) { + return NULL; + } + + /* float as integer ratio: numerator/denominator */ + n_d = _py_float_as_integer_ratio(tmp, NULL); + Py_DECREF(tmp); + if (n_d == NULL) { + return NULL; + } + n = PyTuple_GET_ITEM(n_d, 0); + d = PyTuple_GET_ITEM(n_d, 1); + + tmp = _py_long_bit_length(d, NULL); + if (tmp == NULL) { + Py_DECREF(n_d); + return NULL; + } + k = PyLong_AsSsize_t(tmp); + Py_DECREF(tmp); + if (k == -1 && PyErr_Occurred()) { + Py_DECREF(n_d); + return NULL; + } + k--; + + dec = PyDecType_FromLongExact(type, n, context); + Py_DECREF(n_d); + if (dec == NULL) { + return NULL; + } + + d1 = mpd_qnew(); + if (d1 == NULL) { + Py_DECREF(dec); + PyErr_NoMemory(); + return NULL; + } + d2 = mpd_qnew(); + if (d2 == NULL) { + mpd_del(d1); + Py_DECREF(dec); + PyErr_NoMemory(); + return NULL; + } + + mpd_maxcontext(&maxctx); + mpd_qset_uint(d1, 5, &maxctx, &status); + mpd_qset_ssize(d2, k, &maxctx, &status); + mpd_qpow(d1, d1, d2, &maxctx, &status); + if (dec_addstatus(context, status)) { + mpd_del(d1); + mpd_del(d2); + Py_DECREF(dec); + return NULL; + } + + /* result = n * 5**k */ + mpd_qmul(MPD(dec), MPD(dec), d1, &maxctx, &status); + mpd_del(d1); + mpd_del(d2); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + /* result = +- n * 5**k * 10**-k */ + mpd_set_sign(MPD(dec), sign); + MPD(dec)->exp = -k; + + return dec; +} + +static PyObject * +PyDecType_FromFloat(PyTypeObject *type, PyObject *v, + PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + + dec = PyDecType_FromFloatExact(type, v, context); + if (dec == NULL) { + return NULL; + } + + mpd_qfinalize(MPD(dec), CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + + return dec; +} + +/* Return a new PyDecObject or a subtype from a Decimal. */ +static PyObject * +PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context) +{ + PyObject *dec; + uint32_t status = 0; + + if (type == &PyDec_Type && PyDec_CheckExact(v)) { + Py_INCREF(v); + return v; + } + + dec = PyDecType_New(type); + if (dec == NULL) { + return NULL; + } + + mpd_qcopy(MPD(dec), MPD(v), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(dec); + return NULL; + } + + return dec; +} + +static PyObject * +sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg) +{ + if (PyTuple_Check(v)) { + Py_INCREF(v); + return v; + } + if (PyList_Check(v)) { + return PyList_AsTuple(v); + } + + PyErr_SetString(ex, mesg); + return NULL; +} + +/* Return a new C string representation of a DecimalTuple. */ +static char * +dectuple_as_str(PyObject *dectuple) +{ + PyObject *digits = NULL, *tmp; + char *decstring = NULL; + char sign_special[6]; + char *cp; + long sign, l; + mpd_ssize_t exp = 0; + Py_ssize_t i, mem, tsize; + int is_infinite = 0; + int n; + + assert(PyTuple_Check(dectuple)); + + if (PyTuple_Size(dectuple) != 3) { + PyErr_SetString(PyExc_ValueError, + "argument must be a sequence of length 3"); + goto error; + } + + /* sign */ + tmp = PyTuple_GET_ITEM(dectuple, 0); + if (!PyLong_Check(tmp)) { + PyErr_SetString(PyExc_ValueError, + "sign must be an integer with the value 0 or 1"); + goto error; + } + sign = PyLong_AsLong(tmp); + if (sign == -1 && PyErr_Occurred()) { + goto error; + } + if (sign != 0 && sign != 1) { + PyErr_SetString(PyExc_ValueError, + "sign must be an integer with the value 0 or 1"); + goto error; + } + sign_special[0] = sign ? '-' : '+'; + sign_special[1] = '\0'; + + /* exponent or encoding for a special number */ + tmp = PyTuple_GET_ITEM(dectuple, 2); + if (PyUnicode_Check(tmp)) { + /* special */ + if (PyUnicode_CompareWithASCIIString(tmp, "F") == 0) { + strcat(sign_special, "Inf"); + is_infinite = 1; + } + else if (PyUnicode_CompareWithASCIIString(tmp, "n") == 0) { + strcat(sign_special, "NaN"); + } + else if (PyUnicode_CompareWithASCIIString(tmp, "N") == 0) { + strcat(sign_special, "sNaN"); + } + else { + PyErr_SetString(PyExc_ValueError, + "string argument in the third position " + "must be 'F', 'n' or 'N'"); + goto error; + } + } + else { + /* exponent */ + if (!PyLong_Check(tmp)) { + PyErr_SetString(PyExc_ValueError, + "exponent must be an integer"); + goto error; + } + exp = PyLong_AsSsize_t(tmp); + if (exp == -1 && PyErr_Occurred()) { + goto error; + } + } + + /* coefficient */ + digits = sequence_as_tuple(PyTuple_GET_ITEM(dectuple, 1), PyExc_ValueError, + "coefficient must be a tuple of digits"); + if (digits == NULL) { + goto error; + } + + tsize = PyTuple_Size(digits); + /* [sign][coeffdigits+1][E][-][expdigits+1]['\0'] */ + mem = 1 + tsize + 3 + MPD_EXPDIGITS + 2; + cp = decstring = PyMem_Malloc(mem); + if (decstring == NULL) { + PyErr_NoMemory(); + goto error; + } + + n = snprintf(cp, mem, "%s", sign_special); + if (n < 0 || n >= mem) { + PyErr_SetString(PyExc_RuntimeError, + "internal error in dec_sequence_as_str"); + goto error; + } + cp += n; + + if (tsize == 0 && sign_special[1] == '\0') { + /* empty tuple: zero coefficient, except for special numbers */ + *cp++ = '0'; + } + for (i = 0; i < tsize; i++) { + tmp = PyTuple_GET_ITEM(digits, i); + if (!PyLong_Check(tmp)) { + PyErr_SetString(PyExc_ValueError, + "coefficient must be a tuple of digits"); + goto error; + } + l = PyLong_AsLong(tmp); + if (l == -1 && PyErr_Occurred()) { + goto error; + } + if (l < 0 || l > 9) { + PyErr_SetString(PyExc_ValueError, + "coefficient must be a tuple of digits"); + goto error; + } + if (is_infinite) { + /* accept but ignore any well-formed coefficient for compatibility + with decimal.py */ + continue; + } + *cp++ = (char)l + '0'; + } + *cp = '\0'; + + if (sign_special[1] == '\0') { + /* not a special number */ + *cp++ = 'E'; + n = snprintf(cp, MPD_EXPDIGITS+2, "%" PRI_mpd_ssize_t, exp); + if (n < 0 || n >= MPD_EXPDIGITS+2) { + PyErr_SetString(PyExc_RuntimeError, + "internal error in dec_sequence_as_str"); + goto error; + } + } + + Py_XDECREF(digits); + return decstring; + + +error: + Py_XDECREF(digits); + if (decstring) PyMem_Free(decstring); + return NULL; +} + +/* Currently accepts tuples and lists. */ +static PyObject * +PyDecType_FromSequence(PyTypeObject *type, PyObject *v, + PyObject *context) +{ + PyObject *dectuple; + PyObject *dec; + char *s; + + dectuple = sequence_as_tuple(v, PyExc_TypeError, + "argument must be a tuple or list"); + if (dectuple == NULL) { + return NULL; + } + + s = dectuple_as_str(dectuple); + Py_DECREF(dectuple); + if (s == NULL) { + return NULL; + } + + dec = PyDecType_FromCString(type, s, context); + + PyMem_Free(s); + return dec; +} + +/* Currently accepts tuples and lists. */ +static PyObject * +PyDecType_FromSequenceExact(PyTypeObject *type, PyObject *v, + PyObject *context) +{ + PyObject *dectuple; + PyObject *dec; + char *s; + + dectuple = sequence_as_tuple(v, PyExc_TypeError, + "argument must be a tuple or list"); + if (dectuple == NULL) { + return NULL; + } + + s = dectuple_as_str(dectuple); + Py_DECREF(dectuple); + if (s == NULL) { + return NULL; + } + + dec = PyDecType_FromCStringExact(type, s, context); + + PyMem_Free(s); + return dec; +} + +#define PyDec_FromCString(str, context) \ + PyDecType_FromCString(&PyDec_Type, str, context) +#define PyDec_FromCStringExact(str, context) \ + PyDecType_FromCStringExact(&PyDec_Type, str, context) + +#define PyDec_FromUnicode(unicode, context) \ + PyDecType_FromUnicode(&PyDec_Type, unicode, context) +#define PyDec_FromUnicodeExact(unicode, context) \ + PyDecType_FromUnicodeExact(&PyDec_Type, unicode, context) +#define PyDec_FromUnicodeExactWS(unicode, context) \ + PyDecType_FromUnicodeExactWS(&PyDec_Type, unicode, context) + +#define PyDec_FromSsize(v, context) \ + PyDecType_FromSsize(&PyDec_Type, v, context) +#define PyDec_FromSsizeExact(v, context) \ + PyDecType_FromSsizeExact(&PyDec_Type, v, context) + +#define PyDec_FromLong(pylong, context) \ + PyDecType_FromLong(&PyDec_Type, pylong, context) +#define PyDec_FromLongExact(pylong, context) \ + PyDecType_FromLongExact(&PyDec_Type, pylong, context) + +#define PyDec_FromFloat(pyfloat, context) \ + PyDecType_FromFloat(&PyDec_Type, pyfloat, context) +#define PyDec_FromFloatExact(pyfloat, context) \ + PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context) + +#define PyDec_FromSequence(sequence, context) \ + PyDecType_FromSequence(&PyDec_Type, sequence, context) +#define PyDec_FromSequenceExact(sequence, context) \ + PyDecType_FromSequenceExact(&PyDec_Type, sequence, context) + +/* class method */ +static PyObject * +dec_from_float(PyObject *type, PyObject *pyfloat) +{ + PyObject *context; + PyObject *result; + + CURRENT_CONTEXT(context); + result = PyDecType_FromFloatExact(&PyDec_Type, pyfloat, context); + if (type != (PyObject *)&PyDec_Type && result != NULL) { + Py_SETREF(result, PyObject_CallFunctionObjArgs(type, result, NULL)); + } + + return result; +} + +/* create_decimal_from_float */ +static PyObject * +ctx_from_float(PyObject *context, PyObject *v) +{ + return PyDec_FromFloat(v, context); +} + +/* Apply the context to the input operand. Return a new PyDecObject. */ +static PyObject * +dec_apply(PyObject *v, PyObject *context) +{ + PyObject *result; + uint32_t status = 0; + + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + mpd_qcopy(MPD(result), MPD(v), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + mpd_qfinalize(MPD(result), CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +/* 'v' can have any type accepted by the Decimal constructor. Attempt + an exact conversion. If the result does not meet the restrictions + for an mpd_t, fail with InvalidOperation. */ +static PyObject * +PyDecType_FromObjectExact(PyTypeObject *type, PyObject *v, PyObject *context) +{ + if (v == NULL) { + return PyDecType_FromSsizeExact(type, 0, context); + } + else if (PyDec_Check(v)) { + return PyDecType_FromDecimalExact(type, v, context); + } + else if (PyUnicode_Check(v)) { + return PyDecType_FromUnicodeExactWS(type, v, context); + } + else if (PyLong_Check(v)) { + return PyDecType_FromLongExact(type, v, context); + } + else if (PyTuple_Check(v) || PyList_Check(v)) { + return PyDecType_FromSequenceExact(type, v, context); + } + else if (PyFloat_Check(v)) { + if (dec_addstatus(context, MPD_Float_operation)) { + return NULL; + } + return PyDecType_FromFloatExact(type, v, context); + } + else { + PyErr_Format(PyExc_TypeError, + "conversion from %s to Decimal is not supported", Py_TYPE(v)->tp_name); - return NULL; - } -} - -/* The context is used during conversion. This function is the - equivalent of context.create_decimal(). */ -static PyObject * -PyDec_FromObject(PyObject *v, PyObject *context) -{ - if (v == NULL) { - return PyDec_FromSsize(0, context); - } - else if (PyDec_Check(v)) { - mpd_context_t *ctx = CTX(context); - if (mpd_isnan(MPD(v)) && - MPD(v)->digits > ctx->prec - ctx->clamp) { - /* Special case: too many NaN payload digits */ - PyObject *result; - if (dec_addstatus(context, MPD_Conversion_syntax)) { - return NULL; - } - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - mpd_setspecial(MPD(result), MPD_POS, MPD_NAN); - return result; - } - return dec_apply(v, context); - } - else if (PyUnicode_Check(v)) { - return PyDec_FromUnicode(v, context); - } - else if (PyLong_Check(v)) { - return PyDec_FromLong(v, context); - } - else if (PyTuple_Check(v) || PyList_Check(v)) { - return PyDec_FromSequence(v, context); - } - else if (PyFloat_Check(v)) { - if (dec_addstatus(context, MPD_Float_operation)) { - return NULL; - } - return PyDec_FromFloat(v, context); - } - else { - PyErr_Format(PyExc_TypeError, - "conversion from %s to Decimal is not supported", + return NULL; + } +} + +/* The context is used during conversion. This function is the + equivalent of context.create_decimal(). */ +static PyObject * +PyDec_FromObject(PyObject *v, PyObject *context) +{ + if (v == NULL) { + return PyDec_FromSsize(0, context); + } + else if (PyDec_Check(v)) { + mpd_context_t *ctx = CTX(context); + if (mpd_isnan(MPD(v)) && + MPD(v)->digits > ctx->prec - ctx->clamp) { + /* Special case: too many NaN payload digits */ + PyObject *result; + if (dec_addstatus(context, MPD_Conversion_syntax)) { + return NULL; + } + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + mpd_setspecial(MPD(result), MPD_POS, MPD_NAN); + return result; + } + return dec_apply(v, context); + } + else if (PyUnicode_Check(v)) { + return PyDec_FromUnicode(v, context); + } + else if (PyLong_Check(v)) { + return PyDec_FromLong(v, context); + } + else if (PyTuple_Check(v) || PyList_Check(v)) { + return PyDec_FromSequence(v, context); + } + else if (PyFloat_Check(v)) { + if (dec_addstatus(context, MPD_Float_operation)) { + return NULL; + } + return PyDec_FromFloat(v, context); + } + else { + PyErr_Format(PyExc_TypeError, + "conversion from %s to Decimal is not supported", Py_TYPE(v)->tp_name); - return NULL; - } -} - -static PyObject * -dec_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"value", "context", NULL}; - PyObject *v = NULL; - PyObject *context = Py_None; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, - &v, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - return PyDecType_FromObjectExact(type, v, context); -} - -static PyObject * -ctx_create_decimal(PyObject *context, PyObject *args) -{ - PyObject *v = NULL; - - if (!PyArg_ParseTuple(args, "|O", &v)) { - return NULL; - } - - return PyDec_FromObject(v, context); -} - - -/******************************************************************************/ -/* Implicit conversions to Decimal */ -/******************************************************************************/ - -/* Try to convert PyObject v to a new PyDecObject conv. If the conversion - fails, set conv to NULL (exception is set). If the conversion is not - implemented, set conv to Py_NotImplemented. */ -#define NOT_IMPL 0 -#define TYPE_ERR 1 -Py_LOCAL_INLINE(int) -convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context) -{ - - if (PyDec_Check(v)) { - *conv = v; - Py_INCREF(v); - return 1; - } - if (PyLong_Check(v)) { - *conv = PyDec_FromLongExact(v, context); - if (*conv == NULL) { - return 0; - } - return 1; - } - - if (type_err) { - PyErr_Format(PyExc_TypeError, - "conversion from %s to Decimal is not supported", + return NULL; + } +} + +static PyObject * +dec_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"value", "context", NULL}; + PyObject *v = NULL; + PyObject *context = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, + &v, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + return PyDecType_FromObjectExact(type, v, context); +} + +static PyObject * +ctx_create_decimal(PyObject *context, PyObject *args) +{ + PyObject *v = NULL; + + if (!PyArg_ParseTuple(args, "|O", &v)) { + return NULL; + } + + return PyDec_FromObject(v, context); +} + + +/******************************************************************************/ +/* Implicit conversions to Decimal */ +/******************************************************************************/ + +/* Try to convert PyObject v to a new PyDecObject conv. If the conversion + fails, set conv to NULL (exception is set). If the conversion is not + implemented, set conv to Py_NotImplemented. */ +#define NOT_IMPL 0 +#define TYPE_ERR 1 +Py_LOCAL_INLINE(int) +convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context) +{ + + if (PyDec_Check(v)) { + *conv = v; + Py_INCREF(v); + return 1; + } + if (PyLong_Check(v)) { + *conv = PyDec_FromLongExact(v, context); + if (*conv == NULL) { + return 0; + } + return 1; + } + + if (type_err) { + PyErr_Format(PyExc_TypeError, + "conversion from %s to Decimal is not supported", Py_TYPE(v)->tp_name); - } - else { - Py_INCREF(Py_NotImplemented); - *conv = Py_NotImplemented; - } - return 0; -} - -/* Return NotImplemented for unsupported types. */ -#define CONVERT_OP(a, v, context) \ - if (!convert_op(NOT_IMPL, a, v, context)) { \ - return *(a); \ - } - -#define CONVERT_BINOP(a, b, v, w, context) \ - if (!convert_op(NOT_IMPL, a, v, context)) { \ - return *(a); \ - } \ - if (!convert_op(NOT_IMPL, b, w, context)) { \ - Py_DECREF(*(a)); \ - return *(b); \ - } - -#define CONVERT_TERNOP(a, b, c, v, w, x, context) \ - if (!convert_op(NOT_IMPL, a, v, context)) { \ - return *(a); \ - } \ - if (!convert_op(NOT_IMPL, b, w, context)) { \ - Py_DECREF(*(a)); \ - return *(b); \ - } \ - if (!convert_op(NOT_IMPL, c, x, context)) { \ - Py_DECREF(*(a)); \ - Py_DECREF(*(b)); \ - return *(c); \ - } - -/* Raise TypeError for unsupported types. */ -#define CONVERT_OP_RAISE(a, v, context) \ - if (!convert_op(TYPE_ERR, a, v, context)) { \ - return NULL; \ - } - -#define CONVERT_BINOP_RAISE(a, b, v, w, context) \ - if (!convert_op(TYPE_ERR, a, v, context)) { \ - return NULL; \ - } \ - if (!convert_op(TYPE_ERR, b, w, context)) { \ - Py_DECREF(*(a)); \ - return NULL; \ - } - -#define CONVERT_TERNOP_RAISE(a, b, c, v, w, x, context) \ - if (!convert_op(TYPE_ERR, a, v, context)) { \ - return NULL; \ - } \ - if (!convert_op(TYPE_ERR, b, w, context)) { \ - Py_DECREF(*(a)); \ - return NULL; \ - } \ - if (!convert_op(TYPE_ERR, c, x, context)) { \ - Py_DECREF(*(a)); \ - Py_DECREF(*(b)); \ - return NULL; \ - } - - -/******************************************************************************/ -/* Implicit conversions to Decimal for comparison */ -/******************************************************************************/ - -/* Convert rationals for comparison */ -static PyObject *Rational = NULL; -static PyObject * -multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context) -{ - PyObject *result; - PyObject *tmp = NULL; - PyObject *denom = NULL; - uint32_t status = 0; - mpd_context_t maxctx; - mpd_ssize_t exp; - mpd_t *vv; - - /* v is not special, r is a rational */ - tmp = PyObject_GetAttrString(r, "denominator"); - if (tmp == NULL) { - return NULL; - } - denom = PyDec_FromLongExact(tmp, context); - Py_DECREF(tmp); - if (denom == NULL) { - return NULL; - } - - vv = mpd_qncopy(MPD(v)); - if (vv == NULL) { - Py_DECREF(denom); - PyErr_NoMemory(); - return NULL; - } - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(denom); - mpd_del(vv); - return NULL; - } - - mpd_maxcontext(&maxctx); - /* Prevent Overflow in the following multiplication. The result of - the multiplication is only used in mpd_qcmp, which can handle - values that are technically out of bounds, like (for 32-bit) - 99999999999999999999...99999999e+425000000. */ - exp = vv->exp; - vv->exp = 0; - mpd_qmul(MPD(result), vv, MPD(denom), &maxctx, &status); - MPD(result)->exp = exp; - - Py_DECREF(denom); - mpd_del(vv); - /* If any status has been accumulated during the multiplication, - the result is invalid. This is very unlikely, since even the - 32-bit version supports 425000000 digits. */ - if (status) { - PyErr_SetString(PyExc_ValueError, - "exact conversion for comparison failed"); - Py_DECREF(result); - return NULL; - } - - return result; -} - -static PyObject * -numerator_as_decimal(PyObject *r, PyObject *context) -{ - PyObject *tmp, *num; - - tmp = PyObject_GetAttrString(r, "numerator"); - if (tmp == NULL) { - return NULL; - } - - num = PyDec_FromLongExact(tmp, context); - Py_DECREF(tmp); - return num; -} - -/* Convert v and w for comparison. v is a Decimal. If w is a Rational, both - v and w have to be transformed. Return 1 for success, with new references - to the converted objects in vcmp and wcmp. Return 0 for failure. In that - case wcmp is either NULL or Py_NotImplemented (new reference) and vcmp - is undefined. */ -static int -convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w, - int op, PyObject *context) -{ - mpd_context_t *ctx = CTX(context); - - *vcmp = v; - - if (PyDec_Check(w)) { - Py_INCREF(w); - *wcmp = w; - } - else if (PyLong_Check(w)) { - *wcmp = PyDec_FromLongExact(w, context); - } - else if (PyFloat_Check(w)) { - if (op != Py_EQ && op != Py_NE && - dec_addstatus(context, MPD_Float_operation)) { - *wcmp = NULL; - } - else { - ctx->status |= MPD_Float_operation; - *wcmp = PyDec_FromFloatExact(w, context); - } - } - else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) { - Py_complex c = PyComplex_AsCComplex(w); - if (c.real == -1.0 && PyErr_Occurred()) { - *wcmp = NULL; - } - else if (c.imag == 0.0) { - PyObject *tmp = PyFloat_FromDouble(c.real); - if (tmp == NULL) { - *wcmp = NULL; - } - else { - ctx->status |= MPD_Float_operation; - *wcmp = PyDec_FromFloatExact(tmp, context); - Py_DECREF(tmp); - } - } - else { - Py_INCREF(Py_NotImplemented); - *wcmp = Py_NotImplemented; - } - } - else { - int is_rational = PyObject_IsInstance(w, Rational); - if (is_rational < 0) { - *wcmp = NULL; - } - else if (is_rational > 0) { - *wcmp = numerator_as_decimal(w, context); - if (*wcmp && !mpd_isspecial(MPD(v))) { - *vcmp = multiply_by_denominator(v, w, context); - if (*vcmp == NULL) { - Py_CLEAR(*wcmp); - } - } - } - else { - Py_INCREF(Py_NotImplemented); - *wcmp = Py_NotImplemented; - } - } - - if (*wcmp == NULL || *wcmp == Py_NotImplemented) { - return 0; - } - if (*vcmp == v) { - Py_INCREF(v); - } - return 1; -} - -#define CONVERT_BINOP_CMP(vcmp, wcmp, v, w, op, ctx) \ - if (!convert_op_cmp(vcmp, wcmp, v, w, op, ctx)) { \ - return *(wcmp); \ - } \ - - -/******************************************************************************/ -/* Conversions from decimal */ -/******************************************************************************/ - -static PyObject * -unicode_fromascii(const char *s, Py_ssize_t size) -{ - PyObject *res; - - res = PyUnicode_New(size, 127); - if (res == NULL) { - return NULL; - } - - memcpy(PyUnicode_1BYTE_DATA(res), s, size); - return res; -} - -/* PyDecObject as a string. The default module context is only used for - the value of 'capitals'. */ -static PyObject * -dec_str(PyObject *dec) -{ - PyObject *res, *context; - mpd_ssize_t size; - char *cp; - - CURRENT_CONTEXT(context); - size = mpd_to_sci_size(&cp, MPD(dec), CtxCaps(context)); - if (size < 0) { - PyErr_NoMemory(); - return NULL; - } - - res = unicode_fromascii(cp, size); - mpd_free(cp); - return res; -} - -/* Representation of a PyDecObject. */ -static PyObject * -dec_repr(PyObject *dec) -{ - PyObject *res, *context; - char *cp; - - CURRENT_CONTEXT(context); - cp = mpd_to_sci(MPD(dec), CtxCaps(context)); - if (cp == NULL) { - PyErr_NoMemory(); - return NULL; - } - - res = PyUnicode_FromFormat("Decimal('%s')", cp); - mpd_free(cp); - return res; -} - -/* Return a duplicate of src, copy embedded null characters. */ -static char * -dec_strdup(const char *src, Py_ssize_t size) -{ - char *dest = PyMem_Malloc(size+1); - if (dest == NULL) { - PyErr_NoMemory(); - return NULL; - } - - memcpy(dest, src, size); - dest[size] = '\0'; - return dest; -} - -static void -dec_replace_fillchar(char *dest) -{ - while (*dest != '\0') { - if (*dest == '\xff') *dest = '\0'; - dest++; - } -} - -/* Convert decimal_point or thousands_sep, which may be multibyte or in - the range [128, 255], to a UTF8 string. */ -static PyObject * -dotsep_as_utf8(const char *s) -{ - PyObject *utf8; - PyObject *tmp; - wchar_t buf[2]; - size_t n; - - n = mbstowcs(buf, s, 2); - if (n != 1) { /* Issue #7442 */ - PyErr_SetString(PyExc_ValueError, - "invalid decimal point or unsupported " - "combination of LC_CTYPE and LC_NUMERIC"); - return NULL; - } - tmp = PyUnicode_FromWideChar(buf, n); - if (tmp == NULL) { - return NULL; - } - utf8 = PyUnicode_AsUTF8String(tmp); - Py_DECREF(tmp); - return utf8; -} - -/* Formatted representation of a PyDecObject. */ -static PyObject * -dec_format(PyObject *dec, PyObject *args) -{ - PyObject *result = NULL; - PyObject *override = NULL; - PyObject *dot = NULL; - PyObject *sep = NULL; - PyObject *grouping = NULL; - PyObject *fmtarg; - PyObject *context; - mpd_spec_t spec; - char *fmt; - char *decstring = NULL; - uint32_t status = 0; - int replace_fillchar = 0; - Py_ssize_t size; - - - CURRENT_CONTEXT(context); - if (!PyArg_ParseTuple(args, "O|O", &fmtarg, &override)) { - return NULL; - } - - if (PyUnicode_Check(fmtarg)) { - fmt = (char *)PyUnicode_AsUTF8AndSize(fmtarg, &size); - if (fmt == NULL) { - return NULL; - } - if (size > 0 && fmt[0] == '\0') { - /* NUL fill character: must be replaced with a valid UTF-8 char - before calling mpd_parse_fmt_str(). */ - replace_fillchar = 1; - fmt = dec_strdup(fmt, size); - if (fmt == NULL) { - return NULL; - } - fmt[0] = '_'; - } - } - else { - PyErr_SetString(PyExc_TypeError, - "format arg must be str"); - return NULL; - } - - if (!mpd_parse_fmt_str(&spec, fmt, CtxCaps(context))) { - PyErr_SetString(PyExc_ValueError, - "invalid format string"); - goto finish; - } - if (replace_fillchar) { - /* In order to avoid clobbering parts of UTF-8 thousands separators or - decimal points when the substitution is reversed later, the actual - placeholder must be an invalid UTF-8 byte. */ - spec.fill[0] = '\xff'; - spec.fill[1] = '\0'; - } - - if (override) { - /* Values for decimal_point, thousands_sep and grouping can - be explicitly specified in the override dict. These values - take precedence over the values obtained from localeconv() - in mpd_parse_fmt_str(). The feature is not documented and - is only used in test_decimal. */ - if (!PyDict_Check(override)) { - PyErr_SetString(PyExc_TypeError, - "optional argument must be a dict"); - goto finish; - } - if ((dot = PyDict_GetItemString(override, "decimal_point"))) { - if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) { - goto finish; - } - spec.dot = PyBytes_AS_STRING(dot); - } - if ((sep = PyDict_GetItemString(override, "thousands_sep"))) { - if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { - goto finish; - } - spec.sep = PyBytes_AS_STRING(sep); - } - if ((grouping = PyDict_GetItemString(override, "grouping"))) { - if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) { - goto finish; - } - spec.grouping = PyBytes_AS_STRING(grouping); - } - if (mpd_validate_lconv(&spec) < 0) { - PyErr_SetString(PyExc_ValueError, - "invalid override dict"); - goto finish; - } - } - else { - size_t n = strlen(spec.dot); - if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) { - /* fix locale dependent non-ascii characters */ - dot = dotsep_as_utf8(spec.dot); - if (dot == NULL) { - goto finish; - } - spec.dot = PyBytes_AS_STRING(dot); - } - n = strlen(spec.sep); - if (n > 1 || (n == 1 && !isascii((uchar)spec.sep[0]))) { - /* fix locale dependent non-ascii characters */ - sep = dotsep_as_utf8(spec.sep); - if (sep == NULL) { - goto finish; - } - spec.sep = PyBytes_AS_STRING(sep); - } - } - - - decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status); - if (decstring == NULL) { - if (status & MPD_Malloc_error) { - PyErr_NoMemory(); - } - else { - PyErr_SetString(PyExc_ValueError, - "format specification exceeds internal limits of _decimal"); - } - goto finish; - } - size = strlen(decstring); - if (replace_fillchar) { - dec_replace_fillchar(decstring); - } - - result = PyUnicode_DecodeUTF8(decstring, size, NULL); - - -finish: - Py_XDECREF(grouping); - Py_XDECREF(sep); - Py_XDECREF(dot); - if (replace_fillchar) PyMem_Free(fmt); - if (decstring) mpd_free(decstring); - return result; -} - -/* Return a PyLongObject from a PyDecObject, using the specified rounding - * mode. The context precision is not observed. */ -static PyObject * -dec_as_long(PyObject *dec, PyObject *context, int round) -{ - PyLongObject *pylong; - digit *ob_digit; - size_t n; - Py_ssize_t i; - mpd_t *x; - mpd_context_t workctx; - uint32_t status = 0; - - if (mpd_isspecial(MPD(dec))) { - if (mpd_isnan(MPD(dec))) { - PyErr_SetString(PyExc_ValueError, - "cannot convert NaN to integer"); - } - else { - PyErr_SetString(PyExc_OverflowError, - "cannot convert Infinity to integer"); - } - return NULL; - } - - x = mpd_qnew(); - if (x == NULL) { - PyErr_NoMemory(); - return NULL; - } - workctx = *CTX(context); - workctx.round = round; - mpd_qround_to_int(x, MPD(dec), &workctx, &status); - if (dec_addstatus(context, status)) { - mpd_del(x); - return NULL; - } - - status = 0; - ob_digit = NULL; -#if PYLONG_BITS_IN_DIGIT == 30 - n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status); -#elif PYLONG_BITS_IN_DIGIT == 15 - n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status); -#else - #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" -#endif - - if (n == SIZE_MAX) { - PyErr_NoMemory(); - mpd_del(x); - return NULL; - } - - assert(n > 0); - pylong = _PyLong_New(n); - if (pylong == NULL) { - mpd_free(ob_digit); - mpd_del(x); - return NULL; - } - - memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit)); - mpd_free(ob_digit); - - i = n; - while ((i > 0) && (pylong->ob_digit[i-1] == 0)) { - i--; - } - + } + else { + Py_INCREF(Py_NotImplemented); + *conv = Py_NotImplemented; + } + return 0; +} + +/* Return NotImplemented for unsupported types. */ +#define CONVERT_OP(a, v, context) \ + if (!convert_op(NOT_IMPL, a, v, context)) { \ + return *(a); \ + } + +#define CONVERT_BINOP(a, b, v, w, context) \ + if (!convert_op(NOT_IMPL, a, v, context)) { \ + return *(a); \ + } \ + if (!convert_op(NOT_IMPL, b, w, context)) { \ + Py_DECREF(*(a)); \ + return *(b); \ + } + +#define CONVERT_TERNOP(a, b, c, v, w, x, context) \ + if (!convert_op(NOT_IMPL, a, v, context)) { \ + return *(a); \ + } \ + if (!convert_op(NOT_IMPL, b, w, context)) { \ + Py_DECREF(*(a)); \ + return *(b); \ + } \ + if (!convert_op(NOT_IMPL, c, x, context)) { \ + Py_DECREF(*(a)); \ + Py_DECREF(*(b)); \ + return *(c); \ + } + +/* Raise TypeError for unsupported types. */ +#define CONVERT_OP_RAISE(a, v, context) \ + if (!convert_op(TYPE_ERR, a, v, context)) { \ + return NULL; \ + } + +#define CONVERT_BINOP_RAISE(a, b, v, w, context) \ + if (!convert_op(TYPE_ERR, a, v, context)) { \ + return NULL; \ + } \ + if (!convert_op(TYPE_ERR, b, w, context)) { \ + Py_DECREF(*(a)); \ + return NULL; \ + } + +#define CONVERT_TERNOP_RAISE(a, b, c, v, w, x, context) \ + if (!convert_op(TYPE_ERR, a, v, context)) { \ + return NULL; \ + } \ + if (!convert_op(TYPE_ERR, b, w, context)) { \ + Py_DECREF(*(a)); \ + return NULL; \ + } \ + if (!convert_op(TYPE_ERR, c, x, context)) { \ + Py_DECREF(*(a)); \ + Py_DECREF(*(b)); \ + return NULL; \ + } + + +/******************************************************************************/ +/* Implicit conversions to Decimal for comparison */ +/******************************************************************************/ + +/* Convert rationals for comparison */ +static PyObject *Rational = NULL; +static PyObject * +multiply_by_denominator(PyObject *v, PyObject *r, PyObject *context) +{ + PyObject *result; + PyObject *tmp = NULL; + PyObject *denom = NULL; + uint32_t status = 0; + mpd_context_t maxctx; + mpd_ssize_t exp; + mpd_t *vv; + + /* v is not special, r is a rational */ + tmp = PyObject_GetAttrString(r, "denominator"); + if (tmp == NULL) { + return NULL; + } + denom = PyDec_FromLongExact(tmp, context); + Py_DECREF(tmp); + if (denom == NULL) { + return NULL; + } + + vv = mpd_qncopy(MPD(v)); + if (vv == NULL) { + Py_DECREF(denom); + PyErr_NoMemory(); + return NULL; + } + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(denom); + mpd_del(vv); + return NULL; + } + + mpd_maxcontext(&maxctx); + /* Prevent Overflow in the following multiplication. The result of + the multiplication is only used in mpd_qcmp, which can handle + values that are technically out of bounds, like (for 32-bit) + 99999999999999999999...99999999e+425000000. */ + exp = vv->exp; + vv->exp = 0; + mpd_qmul(MPD(result), vv, MPD(denom), &maxctx, &status); + MPD(result)->exp = exp; + + Py_DECREF(denom); + mpd_del(vv); + /* If any status has been accumulated during the multiplication, + the result is invalid. This is very unlikely, since even the + 32-bit version supports 425000000 digits. */ + if (status) { + PyErr_SetString(PyExc_ValueError, + "exact conversion for comparison failed"); + Py_DECREF(result); + return NULL; + } + + return result; +} + +static PyObject * +numerator_as_decimal(PyObject *r, PyObject *context) +{ + PyObject *tmp, *num; + + tmp = PyObject_GetAttrString(r, "numerator"); + if (tmp == NULL) { + return NULL; + } + + num = PyDec_FromLongExact(tmp, context); + Py_DECREF(tmp); + return num; +} + +/* Convert v and w for comparison. v is a Decimal. If w is a Rational, both + v and w have to be transformed. Return 1 for success, with new references + to the converted objects in vcmp and wcmp. Return 0 for failure. In that + case wcmp is either NULL or Py_NotImplemented (new reference) and vcmp + is undefined. */ +static int +convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w, + int op, PyObject *context) +{ + mpd_context_t *ctx = CTX(context); + + *vcmp = v; + + if (PyDec_Check(w)) { + Py_INCREF(w); + *wcmp = w; + } + else if (PyLong_Check(w)) { + *wcmp = PyDec_FromLongExact(w, context); + } + else if (PyFloat_Check(w)) { + if (op != Py_EQ && op != Py_NE && + dec_addstatus(context, MPD_Float_operation)) { + *wcmp = NULL; + } + else { + ctx->status |= MPD_Float_operation; + *wcmp = PyDec_FromFloatExact(w, context); + } + } + else if (PyComplex_Check(w) && (op == Py_EQ || op == Py_NE)) { + Py_complex c = PyComplex_AsCComplex(w); + if (c.real == -1.0 && PyErr_Occurred()) { + *wcmp = NULL; + } + else if (c.imag == 0.0) { + PyObject *tmp = PyFloat_FromDouble(c.real); + if (tmp == NULL) { + *wcmp = NULL; + } + else { + ctx->status |= MPD_Float_operation; + *wcmp = PyDec_FromFloatExact(tmp, context); + Py_DECREF(tmp); + } + } + else { + Py_INCREF(Py_NotImplemented); + *wcmp = Py_NotImplemented; + } + } + else { + int is_rational = PyObject_IsInstance(w, Rational); + if (is_rational < 0) { + *wcmp = NULL; + } + else if (is_rational > 0) { + *wcmp = numerator_as_decimal(w, context); + if (*wcmp && !mpd_isspecial(MPD(v))) { + *vcmp = multiply_by_denominator(v, w, context); + if (*vcmp == NULL) { + Py_CLEAR(*wcmp); + } + } + } + else { + Py_INCREF(Py_NotImplemented); + *wcmp = Py_NotImplemented; + } + } + + if (*wcmp == NULL || *wcmp == Py_NotImplemented) { + return 0; + } + if (*vcmp == v) { + Py_INCREF(v); + } + return 1; +} + +#define CONVERT_BINOP_CMP(vcmp, wcmp, v, w, op, ctx) \ + if (!convert_op_cmp(vcmp, wcmp, v, w, op, ctx)) { \ + return *(wcmp); \ + } \ + + +/******************************************************************************/ +/* Conversions from decimal */ +/******************************************************************************/ + +static PyObject * +unicode_fromascii(const char *s, Py_ssize_t size) +{ + PyObject *res; + + res = PyUnicode_New(size, 127); + if (res == NULL) { + return NULL; + } + + memcpy(PyUnicode_1BYTE_DATA(res), s, size); + return res; +} + +/* PyDecObject as a string. The default module context is only used for + the value of 'capitals'. */ +static PyObject * +dec_str(PyObject *dec) +{ + PyObject *res, *context; + mpd_ssize_t size; + char *cp; + + CURRENT_CONTEXT(context); + size = mpd_to_sci_size(&cp, MPD(dec), CtxCaps(context)); + if (size < 0) { + PyErr_NoMemory(); + return NULL; + } + + res = unicode_fromascii(cp, size); + mpd_free(cp); + return res; +} + +/* Representation of a PyDecObject. */ +static PyObject * +dec_repr(PyObject *dec) +{ + PyObject *res, *context; + char *cp; + + CURRENT_CONTEXT(context); + cp = mpd_to_sci(MPD(dec), CtxCaps(context)); + if (cp == NULL) { + PyErr_NoMemory(); + return NULL; + } + + res = PyUnicode_FromFormat("Decimal('%s')", cp); + mpd_free(cp); + return res; +} + +/* Return a duplicate of src, copy embedded null characters. */ +static char * +dec_strdup(const char *src, Py_ssize_t size) +{ + char *dest = PyMem_Malloc(size+1); + if (dest == NULL) { + PyErr_NoMemory(); + return NULL; + } + + memcpy(dest, src, size); + dest[size] = '\0'; + return dest; +} + +static void +dec_replace_fillchar(char *dest) +{ + while (*dest != '\0') { + if (*dest == '\xff') *dest = '\0'; + dest++; + } +} + +/* Convert decimal_point or thousands_sep, which may be multibyte or in + the range [128, 255], to a UTF8 string. */ +static PyObject * +dotsep_as_utf8(const char *s) +{ + PyObject *utf8; + PyObject *tmp; + wchar_t buf[2]; + size_t n; + + n = mbstowcs(buf, s, 2); + if (n != 1) { /* Issue #7442 */ + PyErr_SetString(PyExc_ValueError, + "invalid decimal point or unsupported " + "combination of LC_CTYPE and LC_NUMERIC"); + return NULL; + } + tmp = PyUnicode_FromWideChar(buf, n); + if (tmp == NULL) { + return NULL; + } + utf8 = PyUnicode_AsUTF8String(tmp); + Py_DECREF(tmp); + return utf8; +} + +/* Formatted representation of a PyDecObject. */ +static PyObject * +dec_format(PyObject *dec, PyObject *args) +{ + PyObject *result = NULL; + PyObject *override = NULL; + PyObject *dot = NULL; + PyObject *sep = NULL; + PyObject *grouping = NULL; + PyObject *fmtarg; + PyObject *context; + mpd_spec_t spec; + char *fmt; + char *decstring = NULL; + uint32_t status = 0; + int replace_fillchar = 0; + Py_ssize_t size; + + + CURRENT_CONTEXT(context); + if (!PyArg_ParseTuple(args, "O|O", &fmtarg, &override)) { + return NULL; + } + + if (PyUnicode_Check(fmtarg)) { + fmt = (char *)PyUnicode_AsUTF8AndSize(fmtarg, &size); + if (fmt == NULL) { + return NULL; + } + if (size > 0 && fmt[0] == '\0') { + /* NUL fill character: must be replaced with a valid UTF-8 char + before calling mpd_parse_fmt_str(). */ + replace_fillchar = 1; + fmt = dec_strdup(fmt, size); + if (fmt == NULL) { + return NULL; + } + fmt[0] = '_'; + } + } + else { + PyErr_SetString(PyExc_TypeError, + "format arg must be str"); + return NULL; + } + + if (!mpd_parse_fmt_str(&spec, fmt, CtxCaps(context))) { + PyErr_SetString(PyExc_ValueError, + "invalid format string"); + goto finish; + } + if (replace_fillchar) { + /* In order to avoid clobbering parts of UTF-8 thousands separators or + decimal points when the substitution is reversed later, the actual + placeholder must be an invalid UTF-8 byte. */ + spec.fill[0] = '\xff'; + spec.fill[1] = '\0'; + } + + if (override) { + /* Values for decimal_point, thousands_sep and grouping can + be explicitly specified in the override dict. These values + take precedence over the values obtained from localeconv() + in mpd_parse_fmt_str(). The feature is not documented and + is only used in test_decimal. */ + if (!PyDict_Check(override)) { + PyErr_SetString(PyExc_TypeError, + "optional argument must be a dict"); + goto finish; + } + if ((dot = PyDict_GetItemString(override, "decimal_point"))) { + if ((dot = PyUnicode_AsUTF8String(dot)) == NULL) { + goto finish; + } + spec.dot = PyBytes_AS_STRING(dot); + } + if ((sep = PyDict_GetItemString(override, "thousands_sep"))) { + if ((sep = PyUnicode_AsUTF8String(sep)) == NULL) { + goto finish; + } + spec.sep = PyBytes_AS_STRING(sep); + } + if ((grouping = PyDict_GetItemString(override, "grouping"))) { + if ((grouping = PyUnicode_AsUTF8String(grouping)) == NULL) { + goto finish; + } + spec.grouping = PyBytes_AS_STRING(grouping); + } + if (mpd_validate_lconv(&spec) < 0) { + PyErr_SetString(PyExc_ValueError, + "invalid override dict"); + goto finish; + } + } + else { + size_t n = strlen(spec.dot); + if (n > 1 || (n == 1 && !isascii((uchar)spec.dot[0]))) { + /* fix locale dependent non-ascii characters */ + dot = dotsep_as_utf8(spec.dot); + if (dot == NULL) { + goto finish; + } + spec.dot = PyBytes_AS_STRING(dot); + } + n = strlen(spec.sep); + if (n > 1 || (n == 1 && !isascii((uchar)spec.sep[0]))) { + /* fix locale dependent non-ascii characters */ + sep = dotsep_as_utf8(spec.sep); + if (sep == NULL) { + goto finish; + } + spec.sep = PyBytes_AS_STRING(sep); + } + } + + + decstring = mpd_qformat_spec(MPD(dec), &spec, CTX(context), &status); + if (decstring == NULL) { + if (status & MPD_Malloc_error) { + PyErr_NoMemory(); + } + else { + PyErr_SetString(PyExc_ValueError, + "format specification exceeds internal limits of _decimal"); + } + goto finish; + } + size = strlen(decstring); + if (replace_fillchar) { + dec_replace_fillchar(decstring); + } + + result = PyUnicode_DecodeUTF8(decstring, size, NULL); + + +finish: + Py_XDECREF(grouping); + Py_XDECREF(sep); + Py_XDECREF(dot); + if (replace_fillchar) PyMem_Free(fmt); + if (decstring) mpd_free(decstring); + return result; +} + +/* Return a PyLongObject from a PyDecObject, using the specified rounding + * mode. The context precision is not observed. */ +static PyObject * +dec_as_long(PyObject *dec, PyObject *context, int round) +{ + PyLongObject *pylong; + digit *ob_digit; + size_t n; + Py_ssize_t i; + mpd_t *x; + mpd_context_t workctx; + uint32_t status = 0; + + if (mpd_isspecial(MPD(dec))) { + if (mpd_isnan(MPD(dec))) { + PyErr_SetString(PyExc_ValueError, + "cannot convert NaN to integer"); + } + else { + PyErr_SetString(PyExc_OverflowError, + "cannot convert Infinity to integer"); + } + return NULL; + } + + x = mpd_qnew(); + if (x == NULL) { + PyErr_NoMemory(); + return NULL; + } + workctx = *CTX(context); + workctx.round = round; + mpd_qround_to_int(x, MPD(dec), &workctx, &status); + if (dec_addstatus(context, status)) { + mpd_del(x); + return NULL; + } + + status = 0; + ob_digit = NULL; +#if PYLONG_BITS_IN_DIGIT == 30 + n = mpd_qexport_u32(&ob_digit, 0, PyLong_BASE, x, &status); +#elif PYLONG_BITS_IN_DIGIT == 15 + n = mpd_qexport_u16(&ob_digit, 0, PyLong_BASE, x, &status); +#else + #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" +#endif + + if (n == SIZE_MAX) { + PyErr_NoMemory(); + mpd_del(x); + return NULL; + } + + assert(n > 0); + pylong = _PyLong_New(n); + if (pylong == NULL) { + mpd_free(ob_digit); + mpd_del(x); + return NULL; + } + + memcpy(pylong->ob_digit, ob_digit, n * sizeof(digit)); + mpd_free(ob_digit); + + i = n; + while ((i > 0) && (pylong->ob_digit[i-1] == 0)) { + i--; + } + Py_SET_SIZE(pylong, i); - if (mpd_isnegative(x) && !mpd_iszero(x)) { + if (mpd_isnegative(x) && !mpd_iszero(x)) { Py_SET_SIZE(pylong, -i); - } - - mpd_del(x); - return (PyObject *) pylong; -} - -/* Convert a Decimal to its exact integer ratio representation. */ -static PyObject * -dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED) -{ - PyObject *numerator = NULL; - PyObject *denominator = NULL; - PyObject *exponent = NULL; - PyObject *result = NULL; - PyObject *tmp; - mpd_ssize_t exp; - PyObject *context; - uint32_t status = 0; - - if (mpd_isspecial(MPD(self))) { - if (mpd_isnan(MPD(self))) { - PyErr_SetString(PyExc_ValueError, - "cannot convert NaN to integer ratio"); - } - else { - PyErr_SetString(PyExc_OverflowError, - "cannot convert Infinity to integer ratio"); - } - return NULL; - } - - CURRENT_CONTEXT(context); - - tmp = dec_alloc(); - if (tmp == NULL) { - return NULL; - } - - if (!mpd_qcopy(MPD(tmp), MPD(self), &status)) { - Py_DECREF(tmp); - PyErr_NoMemory(); - return NULL; - } - - exp = mpd_iszero(MPD(tmp)) ? 0 : MPD(tmp)->exp; - MPD(tmp)->exp = 0; - - /* context and rounding are unused here: the conversion is exact */ - numerator = dec_as_long(tmp, context, MPD_ROUND_FLOOR); - Py_DECREF(tmp); - if (numerator == NULL) { - goto error; - } - - exponent = PyLong_FromSsize_t(exp < 0 ? -exp : exp); - if (exponent == NULL) { - goto error; - } - - tmp = PyLong_FromLong(10); - if (tmp == NULL) { - goto error; - } - - Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None)); - Py_DECREF(tmp); - if (exponent == NULL) { - goto error; - } - - if (exp >= 0) { - Py_SETREF(numerator, _py_long_multiply(numerator, exponent)); - if (numerator == NULL) { - goto error; - } - denominator = PyLong_FromLong(1); - if (denominator == NULL) { - goto error; - } - } - else { - denominator = exponent; - exponent = NULL; - tmp = _PyLong_GCD(numerator, denominator); - if (tmp == NULL) { - goto error; - } - Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp)); - Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp)); - Py_DECREF(tmp); - if (numerator == NULL || denominator == NULL) { - goto error; - } - } - - result = PyTuple_Pack(2, numerator, denominator); - - -error: - Py_XDECREF(exponent); - Py_XDECREF(denominator); - Py_XDECREF(numerator); - return result; -} - -static PyObject * -PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"rounding", "context", NULL}; - PyObject *result; - PyObject *rounding = Py_None; - PyObject *context = Py_None; - uint32_t status = 0; - mpd_context_t workctx; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, - &rounding, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - workctx = *CTX(context); - if (rounding != Py_None) { - int round = getround(rounding); - if (round < 0) { - return NULL; - } - if (!mpd_qsetround(&workctx, round)) { - INTERNAL_ERROR_PTR("PyDec_ToIntegralValue"); /* GCOV_NOT_REACHED */ - } - } - - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - mpd_qround_to_int(MPD(result), MPD(dec), &workctx, &status); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -static PyObject * -PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"rounding", "context", NULL}; - PyObject *result; - PyObject *rounding = Py_None; - PyObject *context = Py_None; - uint32_t status = 0; - mpd_context_t workctx; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, - &rounding, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - workctx = *CTX(context); - if (rounding != Py_None) { - int round = getround(rounding); - if (round < 0) { - return NULL; - } - if (!mpd_qsetround(&workctx, round)) { - INTERNAL_ERROR_PTR("PyDec_ToIntegralExact"); /* GCOV_NOT_REACHED */ - } - } - - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - mpd_qround_to_intx(MPD(result), MPD(dec), &workctx, &status); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -static PyObject * -PyDec_AsFloat(PyObject *dec) -{ - PyObject *f, *s; - - if (mpd_isnan(MPD(dec))) { - if (mpd_issnan(MPD(dec))) { - PyErr_SetString(PyExc_ValueError, - "cannot convert signaling NaN to float"); - return NULL; - } - if (mpd_isnegative(MPD(dec))) { - s = PyUnicode_FromString("-nan"); - } - else { - s = PyUnicode_FromString("nan"); - } - } - else { - s = dec_str(dec); - } - - if (s == NULL) { - return NULL; - } - - f = PyFloat_FromString(s); - Py_DECREF(s); - - return f; -} - -static PyObject * -PyDec_Round(PyObject *dec, PyObject *args) -{ - PyObject *result; - PyObject *x = NULL; - uint32_t status = 0; - PyObject *context; - - - CURRENT_CONTEXT(context); - if (!PyArg_ParseTuple(args, "|O", &x)) { - return NULL; - } - - if (x) { - mpd_uint_t dq[1] = {1}; - mpd_t q = {MPD_STATIC|MPD_CONST_DATA,0,1,1,1,dq}; - mpd_ssize_t y; - - if (!PyLong_Check(x)) { - PyErr_SetString(PyExc_TypeError, - "optional arg must be an integer"); - return NULL; - } - - y = PyLong_AsSsize_t(x); - if (y == -1 && PyErr_Occurred()) { - return NULL; - } - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - q.exp = (y == MPD_SSIZE_MIN) ? MPD_SSIZE_MAX : -y; - mpd_qquantize(MPD(result), MPD(dec), &q, CTX(context), &status); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; - } - else { - return dec_as_long(dec, context, MPD_ROUND_HALF_EVEN); - } -} - -static PyTypeObject *DecimalTuple = NULL; -/* Return the DecimalTuple representation of a PyDecObject. */ -static PyObject * -PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED) -{ - PyObject *result = NULL; - PyObject *sign = NULL; - PyObject *coeff = NULL; - PyObject *expt = NULL; - PyObject *tmp = NULL; - mpd_t *x = NULL; - char *intstring = NULL; - Py_ssize_t intlen, i; - - - x = mpd_qncopy(MPD(dec)); - if (x == NULL) { - PyErr_NoMemory(); - goto out; - } - - sign = PyLong_FromUnsignedLong(mpd_sign(MPD(dec))); - if (sign == NULL) { - goto out; - } - - if (mpd_isinfinite(x)) { - expt = PyUnicode_FromString("F"); - if (expt == NULL) { - goto out; - } - /* decimal.py has non-compliant infinity payloads. */ - coeff = Py_BuildValue("(i)", 0); - if (coeff == NULL) { - goto out; - } - } - else { - if (mpd_isnan(x)) { - expt = PyUnicode_FromString(mpd_isqnan(x)?"n":"N"); - } - else { - expt = PyLong_FromSsize_t(MPD(dec)->exp); - } - if (expt == NULL) { - goto out; - } - - /* coefficient is defined */ - if (x->len > 0) { - - /* make an integer */ - x->exp = 0; - /* clear NaN and sign */ - mpd_clear_flags(x); - intstring = mpd_to_sci(x, 1); - if (intstring == NULL) { - PyErr_NoMemory(); - goto out; - } - - intlen = strlen(intstring); - coeff = PyTuple_New(intlen); - if (coeff == NULL) { - goto out; - } - - for (i = 0; i < intlen; i++) { - tmp = PyLong_FromLong(intstring[i]-'0'); - if (tmp == NULL) { - goto out; - } - PyTuple_SET_ITEM(coeff, i, tmp); - } - } - else { - coeff = PyTuple_New(0); - if (coeff == NULL) { - goto out; - } - } - } - - result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple, - sign, coeff, expt, NULL); - -out: - if (x) mpd_del(x); - if (intstring) mpd_free(intstring); - Py_XDECREF(sign); - Py_XDECREF(coeff); - Py_XDECREF(expt); - return result; -} - - -/******************************************************************************/ -/* Macros for converting mpdecimal functions to Decimal methods */ -/******************************************************************************/ - -/* Unary number method that uses the default module context. */ -#define Dec_UnaryNumberMethod(MPDFUNC) \ -static PyObject * \ -nm_##MPDFUNC(PyObject *self) \ -{ \ - PyObject *result; \ - PyObject *context; \ - uint32_t status = 0; \ - \ - CURRENT_CONTEXT(context); \ - if ((result = dec_alloc()) == NULL) { \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* Binary number method that uses default module context. */ -#define Dec_BinaryNumberMethod(MPDFUNC) \ -static PyObject * \ -nm_##MPDFUNC(PyObject *self, PyObject *other) \ -{ \ - PyObject *a, *b; \ - PyObject *result; \ - PyObject *context; \ - uint32_t status = 0; \ - \ - CURRENT_CONTEXT(context) ; \ - CONVERT_BINOP(&a, &b, self, other, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* Boolean function without a context arg. */ -#define Dec_BoolFunc(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *dummy UNUSED) \ -{ \ - return MPDFUNC(MPD(self)) ? incr_true() : incr_false(); \ -} - -/* Boolean function with an optional context arg. */ -#define Dec_BoolFuncVA(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ -{ \ - static char *kwlist[] = {"context", NULL}; \ - PyObject *context = Py_None; \ - \ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ - &context)) { \ - return NULL; \ - } \ - CONTEXT_CHECK_VA(context); \ - \ - return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \ -} - -/* Unary function with an optional context arg. */ -#define Dec_UnaryFuncVA(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ -{ \ - static char *kwlist[] = {"context", NULL}; \ - PyObject *result; \ - PyObject *context = Py_None; \ - uint32_t status = 0; \ - \ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ - &context)) { \ - return NULL; \ - } \ - CONTEXT_CHECK_VA(context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* Binary function with an optional context arg. */ -#define Dec_BinaryFuncVA(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ -{ \ - static char *kwlist[] = {"other", "context", NULL}; \ - PyObject *other; \ - PyObject *a, *b; \ - PyObject *result; \ - PyObject *context = Py_None; \ - uint32_t status = 0; \ - \ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ - &other, &context)) { \ - return NULL; \ - } \ - CONTEXT_CHECK_VA(context); \ - CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* Binary function with an optional context arg. Actual MPDFUNC does - NOT take a context. The context is used to record InvalidOperation - if the second operand cannot be converted exactly. */ -#define Dec_BinaryFuncVA_NO_CTX(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ -{ \ - static char *kwlist[] = {"other", "context", NULL}; \ - PyObject *context = Py_None; \ - PyObject *other; \ - PyObject *a, *b; \ - PyObject *result; \ - \ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ - &other, &context)) { \ - return NULL; \ - } \ - CONTEXT_CHECK_VA(context); \ - CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b)); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - \ - return result; \ -} - -/* Ternary function with an optional context arg. */ -#define Dec_TernaryFuncVA(MPDFUNC) \ -static PyObject * \ -dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ -{ \ - static char *kwlist[] = {"other", "third", "context", NULL}; \ - PyObject *other, *third; \ - PyObject *a, *b, *c; \ - PyObject *result; \ - PyObject *context = Py_None; \ - uint32_t status = 0; \ - \ - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, \ - &other, &third, &context)) { \ - return NULL; \ - } \ - CONTEXT_CHECK_VA(context); \ - CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - Py_DECREF(c); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - Py_DECREF(c); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - - -/**********************************************/ -/* Number methods */ -/**********************************************/ - -Dec_UnaryNumberMethod(mpd_qminus) -Dec_UnaryNumberMethod(mpd_qplus) -Dec_UnaryNumberMethod(mpd_qabs) - -Dec_BinaryNumberMethod(mpd_qadd) -Dec_BinaryNumberMethod(mpd_qsub) -Dec_BinaryNumberMethod(mpd_qmul) -Dec_BinaryNumberMethod(mpd_qdiv) -Dec_BinaryNumberMethod(mpd_qrem) -Dec_BinaryNumberMethod(mpd_qdivint) - -static PyObject * -nm_dec_as_long(PyObject *dec) -{ - PyObject *context; - - CURRENT_CONTEXT(context); - return dec_as_long(dec, context, MPD_ROUND_DOWN); -} - -static int -nm_nonzero(PyObject *v) -{ - return !mpd_iszero(MPD(v)); -} - -static PyObject * -nm_mpd_qdivmod(PyObject *v, PyObject *w) -{ - PyObject *a, *b; - PyObject *q, *r; - PyObject *context; - uint32_t status = 0; - PyObject *ret; - - CURRENT_CONTEXT(context); - CONVERT_BINOP(&a, &b, v, w, context); - - q = dec_alloc(); - if (q == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - r = dec_alloc(); - if (r == NULL) { - Py_DECREF(a); - Py_DECREF(b); - Py_DECREF(q); - return NULL; - } - - mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(r); - Py_DECREF(q); - return NULL; - } - - ret = Py_BuildValue("(OO)", q, r); - Py_DECREF(r); - Py_DECREF(q); - return ret; -} - -static PyObject * -nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod) -{ - PyObject *a, *b, *c = NULL; - PyObject *result; - PyObject *context; - uint32_t status = 0; - - CURRENT_CONTEXT(context); - CONVERT_BINOP(&a, &b, base, exp, context); - - if (mod != Py_None) { - if (!convert_op(NOT_IMPL, &c, mod, context)) { - Py_DECREF(a); - Py_DECREF(b); - return c; - } - } - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - Py_DECREF(b); - Py_XDECREF(c); - return NULL; - } - - if (c == NULL) { - mpd_qpow(MPD(result), MPD(a), MPD(b), - CTX(context), &status); - } - else { - mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), - CTX(context), &status); - Py_DECREF(c); - } - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - - -/******************************************************************************/ -/* Decimal Methods */ -/******************************************************************************/ - -/* Unary arithmetic functions, optional context arg */ -Dec_UnaryFuncVA(mpd_qexp) -Dec_UnaryFuncVA(mpd_qln) -Dec_UnaryFuncVA(mpd_qlog10) -Dec_UnaryFuncVA(mpd_qnext_minus) -Dec_UnaryFuncVA(mpd_qnext_plus) -Dec_UnaryFuncVA(mpd_qreduce) -Dec_UnaryFuncVA(mpd_qsqrt) - -/* Binary arithmetic functions, optional context arg */ -Dec_BinaryFuncVA(mpd_qcompare) -Dec_BinaryFuncVA(mpd_qcompare_signal) -Dec_BinaryFuncVA(mpd_qmax) -Dec_BinaryFuncVA(mpd_qmax_mag) -Dec_BinaryFuncVA(mpd_qmin) -Dec_BinaryFuncVA(mpd_qmin_mag) -Dec_BinaryFuncVA(mpd_qnext_toward) -Dec_BinaryFuncVA(mpd_qrem_near) - -/* Ternary arithmetic functions, optional context arg */ -Dec_TernaryFuncVA(mpd_qfma) - -/* Boolean functions, no context arg */ -Dec_BoolFunc(mpd_iscanonical) -Dec_BoolFunc(mpd_isfinite) -Dec_BoolFunc(mpd_isinfinite) -Dec_BoolFunc(mpd_isnan) -Dec_BoolFunc(mpd_isqnan) -Dec_BoolFunc(mpd_issnan) -Dec_BoolFunc(mpd_issigned) -Dec_BoolFunc(mpd_iszero) - -/* Boolean functions, optional context arg */ -Dec_BoolFuncVA(mpd_isnormal) -Dec_BoolFuncVA(mpd_issubnormal) - -/* Unary functions, no context arg */ -static PyObject * -dec_mpd_adjexp(PyObject *self, PyObject *dummy UNUSED) -{ - mpd_ssize_t retval; - - if (mpd_isspecial(MPD(self))) { - retval = 0; - } - else { - retval = mpd_adjexp(MPD(self)); - } - - return PyLong_FromSsize_t(retval); -} - -static PyObject * -dec_canonical(PyObject *self, PyObject *dummy UNUSED) -{ - Py_INCREF(self); - return self; -} - -static PyObject * -dec_conjugate(PyObject *self, PyObject *dummy UNUSED) -{ - Py_INCREF(self); - return self; -} - -static PyObject * -dec_mpd_radix(PyObject *self UNUSED, PyObject *dummy UNUSED) -{ - PyObject *result; - - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - _dec_settriple(result, MPD_POS, 10, 0); - return result; -} - -static PyObject * -dec_mpd_qcopy_abs(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *result; - uint32_t status = 0; - - if ((result = dec_alloc()) == NULL) { - return NULL; - } - - mpd_qcopy_abs(MPD(result), MPD(self), &status); - if (status & MPD_Malloc_error) { - Py_DECREF(result); - PyErr_NoMemory(); - return NULL; - } - - return result; -} - -static PyObject * -dec_mpd_qcopy_negate(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *result; - uint32_t status = 0; - - if ((result = dec_alloc()) == NULL) { - return NULL; - } - - mpd_qcopy_negate(MPD(result), MPD(self), &status); - if (status & MPD_Malloc_error) { - Py_DECREF(result); - PyErr_NoMemory(); - return NULL; - } - - return result; -} - -/* Unary functions, optional context arg */ -Dec_UnaryFuncVA(mpd_qinvert) -Dec_UnaryFuncVA(mpd_qlogb) - -static PyObject * -dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"context", NULL}; - PyObject *context = Py_None; - const char *cp; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, - &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - cp = mpd_class(MPD(self), CTX(context)); - return PyUnicode_FromString(cp); -} - -static PyObject * -dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"context", NULL}; - PyObject *result; - PyObject *context = Py_None; - mpd_ssize_t size; - char *s; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, - &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context)); - if (size < 0) { - PyErr_NoMemory(); - return NULL; - } - - result = unicode_fromascii(s, size); - mpd_free(s); - - return result; -} - -/* Binary functions, optional context arg for conversion errors */ -Dec_BinaryFuncVA_NO_CTX(mpd_compare_total) -Dec_BinaryFuncVA_NO_CTX(mpd_compare_total_mag) - -static PyObject * -dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"other", "context", NULL}; - PyObject *other; - PyObject *a, *b; - PyObject *result; - PyObject *context = Py_None; - uint32_t status = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, - &other, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - CONVERT_BINOP_RAISE(&a, &b, self, other, context); - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - - mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -static PyObject * -dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"other", "context", NULL}; - PyObject *other; - PyObject *a, *b; - PyObject *result; - PyObject *context = Py_None; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, - &other, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - CONVERT_BINOP_RAISE(&a, &b, self, other, context); - - result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); - Py_DECREF(a); - Py_DECREF(b); - - return result; -} - -/* Binary functions, optional context arg */ -Dec_BinaryFuncVA(mpd_qand) -Dec_BinaryFuncVA(mpd_qor) -Dec_BinaryFuncVA(mpd_qxor) - -Dec_BinaryFuncVA(mpd_qrotate) -Dec_BinaryFuncVA(mpd_qscaleb) -Dec_BinaryFuncVA(mpd_qshift) - -static PyObject * -dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"exp", "rounding", "context", NULL}; - PyObject *rounding = Py_None; - PyObject *context = Py_None; - PyObject *w, *a, *b; - PyObject *result; - uint32_t status = 0; - mpd_context_t workctx; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, - &w, &rounding, &context)) { - return NULL; - } - CONTEXT_CHECK_VA(context); - - workctx = *CTX(context); - if (rounding != Py_None) { - int round = getround(rounding); - if (round < 0) { - return NULL; - } - if (!mpd_qsetround(&workctx, round)) { - INTERNAL_ERROR_PTR("dec_mpd_qquantize"); /* GCOV_NOT_REACHED */ - } - } - - CONVERT_BINOP_RAISE(&a, &b, v, w, context); - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - - mpd_qquantize(MPD(result), MPD(a), MPD(b), &workctx, &status); - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -/* Special methods */ -static PyObject * -dec_richcompare(PyObject *v, PyObject *w, int op) -{ - PyObject *a; - PyObject *b; - PyObject *context; - uint32_t status = 0; - int a_issnan, b_issnan; - int r; - - assert(PyDec_Check(v)); - - CURRENT_CONTEXT(context); - CONVERT_BINOP_CMP(&a, &b, v, w, op, context); - - a_issnan = mpd_issnan(MPD(a)); - b_issnan = mpd_issnan(MPD(b)); - - r = mpd_qcmp(MPD(a), MPD(b), &status); - Py_DECREF(a); - Py_DECREF(b); - if (r == INT_MAX) { - /* sNaNs or op={le,ge,lt,gt} always signal. */ - if (a_issnan || b_issnan || (op != Py_EQ && op != Py_NE)) { - if (dec_addstatus(context, status)) { - return NULL; - } - } - /* qNaN comparison with op={eq,ne} or comparison - * with InvalidOperation disabled. */ - return (op == Py_NE) ? incr_true() : incr_false(); - } - - switch (op) { - case Py_EQ: - r = (r == 0); - break; - case Py_NE: - r = (r != 0); - break; - case Py_LE: - r = (r <= 0); - break; - case Py_GE: - r = (r >= 0); - break; - case Py_LT: - r = (r == -1); - break; - case Py_GT: - r = (r == 1); - break; - } - - return PyBool_FromLong(r); -} - -/* __ceil__ */ -static PyObject * -dec_ceil(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *context; - - CURRENT_CONTEXT(context); - return dec_as_long(self, context, MPD_ROUND_CEILING); -} - -/* __complex__ */ -static PyObject * -dec_complex(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *f; - double x; - - f = PyDec_AsFloat(self); - if (f == NULL) { - return NULL; - } - - x = PyFloat_AsDouble(f); - Py_DECREF(f); - if (x == -1.0 && PyErr_Occurred()) { - return NULL; - } - - return PyComplex_FromDoubles(x, 0); -} - -/* __copy__ and __deepcopy__ */ -static PyObject * -dec_copy(PyObject *self, PyObject *dummy UNUSED) -{ - Py_INCREF(self); - return self; -} - -/* __floor__ */ -static PyObject * -dec_floor(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *context; - - CURRENT_CONTEXT(context); - return dec_as_long(self, context, MPD_ROUND_FLOOR); -} - -/* Always uses the module context */ -static Py_hash_t -_dec_hash(PyDecObject *v) -{ -#if defined(CONFIG_64) && _PyHASH_BITS == 61 - /* 2**61 - 1 */ - mpd_uint_t p_data[1] = {2305843009213693951ULL}; - mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data}; - /* Inverse of 10 modulo p */ - mpd_uint_t inv10_p_data[1] = {2075258708292324556ULL}; - mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, - 0, 19, 1, 1, inv10_p_data}; -#elif defined(CONFIG_32) && _PyHASH_BITS == 31 - /* 2**31 - 1 */ - mpd_uint_t p_data[2] = {147483647UL, 2}; - mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 10, 2, 2, p_data}; - /* Inverse of 10 modulo p */ - mpd_uint_t inv10_p_data[2] = {503238553UL, 1}; - mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, - 0, 10, 2, 2, inv10_p_data}; -#else - #error "No valid combination of CONFIG_64, CONFIG_32 and _PyHASH_BITS" -#endif - const Py_hash_t py_hash_inf = 314159; - const Py_hash_t py_hash_nan = 0; - mpd_uint_t ten_data[1] = {10}; - mpd_t ten = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, - 0, 2, 1, 1, ten_data}; - Py_hash_t result; - mpd_t *exp_hash = NULL; - mpd_t *tmp = NULL; - mpd_ssize_t exp; - uint32_t status = 0; - mpd_context_t maxctx; - - - if (mpd_isspecial(MPD(v))) { - if (mpd_issnan(MPD(v))) { - PyErr_SetString(PyExc_TypeError, - "Cannot hash a signaling NaN value"); - return -1; - } - else if (mpd_isnan(MPD(v))) { - return py_hash_nan; - } - else { - return py_hash_inf * mpd_arith_sign(MPD(v)); - } - } - - mpd_maxcontext(&maxctx); - exp_hash = mpd_qnew(); - if (exp_hash == NULL) { - goto malloc_error; - } - tmp = mpd_qnew(); - if (tmp == NULL) { - goto malloc_error; - } - - /* - * exp(v): exponent of v - * int(v): coefficient of v - */ - exp = MPD(v)->exp; - if (exp >= 0) { - /* 10**exp(v) % p */ - mpd_qsset_ssize(tmp, exp, &maxctx, &status); - mpd_qpowmod(exp_hash, &ten, tmp, &p, &maxctx, &status); - } - else { - /* inv10_p**(-exp(v)) % p */ - mpd_qsset_ssize(tmp, -exp, &maxctx, &status); - mpd_qpowmod(exp_hash, &inv10_p, tmp, &p, &maxctx, &status); - } - - /* hash = (int(v) * exp_hash) % p */ - if (!mpd_qcopy(tmp, MPD(v), &status)) { - goto malloc_error; - } - tmp->exp = 0; - mpd_set_positive(tmp); - - maxctx.prec = MPD_MAX_PREC + 21; - maxctx.emax = MPD_MAX_EMAX + 21; - maxctx.emin = MPD_MIN_EMIN - 21; - - mpd_qmul(tmp, tmp, exp_hash, &maxctx, &status); - mpd_qrem(tmp, tmp, &p, &maxctx, &status); - - result = mpd_qget_ssize(tmp, &status); - result = mpd_ispositive(MPD(v)) ? result : -result; - result = (result == -1) ? -2 : result; - - if (status != 0) { - if (status & MPD_Malloc_error) { - goto malloc_error; - } - else { - PyErr_SetString(PyExc_RuntimeError, /* GCOV_NOT_REACHED */ - "dec_hash: internal error: please report"); /* GCOV_NOT_REACHED */ - } - result = -1; /* GCOV_NOT_REACHED */ - } - - -finish: - if (exp_hash) mpd_del(exp_hash); - if (tmp) mpd_del(tmp); - return result; - -malloc_error: - PyErr_NoMemory(); - result = -1; - goto finish; -} - -static Py_hash_t -dec_hash(PyDecObject *self) -{ - if (self->hash == -1) { - self->hash = _dec_hash(self); - } - - return self->hash; -} - -/* __reduce__ */ -static PyObject * -dec_reduce(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *result, *str; - - str = dec_str(self); - if (str == NULL) { - return NULL; - } - - result = Py_BuildValue("O(O)", Py_TYPE(self), str); - Py_DECREF(str); - - return result; -} - -/* __sizeof__ */ -static PyObject * -dec_sizeof(PyObject *v, PyObject *dummy UNUSED) -{ - Py_ssize_t res; - - res = _PyObject_SIZE(Py_TYPE(v)); - if (mpd_isdynamic_data(MPD(v))) { - res += MPD(v)->alloc * sizeof(mpd_uint_t); - } - return PyLong_FromSsize_t(res); -} - -/* __trunc__ */ -static PyObject * -dec_trunc(PyObject *self, PyObject *dummy UNUSED) -{ - PyObject *context; - - CURRENT_CONTEXT(context); - return dec_as_long(self, context, MPD_ROUND_DOWN); -} - -/* real and imag */ -static PyObject * -dec_real(PyObject *self, void *closure UNUSED) -{ - Py_INCREF(self); - return self; -} - -static PyObject * -dec_imag(PyObject *self UNUSED, void *closure UNUSED) -{ - PyObject *result; - - result = dec_alloc(); - if (result == NULL) { - return NULL; - } - - _dec_settriple(result, MPD_POS, 0, 0); - return result; -} - - -static PyGetSetDef dec_getsets [] = -{ - { "real", (getter)dec_real, NULL, NULL, NULL}, - { "imag", (getter)dec_imag, NULL, NULL, NULL}, - {NULL} -}; - -static PyNumberMethods dec_number_methods = -{ - (binaryfunc) nm_mpd_qadd, - (binaryfunc) nm_mpd_qsub, - (binaryfunc) nm_mpd_qmul, - (binaryfunc) nm_mpd_qrem, - (binaryfunc) nm_mpd_qdivmod, - (ternaryfunc) nm_mpd_qpow, - (unaryfunc) nm_mpd_qminus, - (unaryfunc) nm_mpd_qplus, - (unaryfunc) nm_mpd_qabs, - (inquiry) nm_nonzero, - (unaryfunc) 0, /* no bit-complement */ - (binaryfunc) 0, /* no shiftl */ - (binaryfunc) 0, /* no shiftr */ - (binaryfunc) 0, /* no bit-and */ - (binaryfunc) 0, /* no bit-xor */ - (binaryfunc) 0, /* no bit-ior */ - (unaryfunc) nm_dec_as_long, - 0, /* nb_reserved */ - (unaryfunc) PyDec_AsFloat, - 0, /* binaryfunc nb_inplace_add; */ - 0, /* binaryfunc nb_inplace_subtract; */ - 0, /* binaryfunc nb_inplace_multiply; */ - 0, /* binaryfunc nb_inplace_remainder; */ - 0, /* ternaryfunc nb_inplace_power; */ - 0, /* binaryfunc nb_inplace_lshift; */ - 0, /* binaryfunc nb_inplace_rshift; */ - 0, /* binaryfunc nb_inplace_and; */ - 0, /* binaryfunc nb_inplace_xor; */ - 0, /* binaryfunc nb_inplace_or; */ - (binaryfunc) nm_mpd_qdivint, /* binaryfunc nb_floor_divide; */ - (binaryfunc) nm_mpd_qdiv, /* binaryfunc nb_true_divide; */ - 0, /* binaryfunc nb_inplace_floor_divide; */ - 0, /* binaryfunc nb_inplace_true_divide; */ -}; - -static PyMethodDef dec_methods [] = -{ - /* Unary arithmetic functions, optional context arg */ + } + + mpd_del(x); + return (PyObject *) pylong; +} + +/* Convert a Decimal to its exact integer ratio representation. */ +static PyObject * +dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED) +{ + PyObject *numerator = NULL; + PyObject *denominator = NULL; + PyObject *exponent = NULL; + PyObject *result = NULL; + PyObject *tmp; + mpd_ssize_t exp; + PyObject *context; + uint32_t status = 0; + + if (mpd_isspecial(MPD(self))) { + if (mpd_isnan(MPD(self))) { + PyErr_SetString(PyExc_ValueError, + "cannot convert NaN to integer ratio"); + } + else { + PyErr_SetString(PyExc_OverflowError, + "cannot convert Infinity to integer ratio"); + } + return NULL; + } + + CURRENT_CONTEXT(context); + + tmp = dec_alloc(); + if (tmp == NULL) { + return NULL; + } + + if (!mpd_qcopy(MPD(tmp), MPD(self), &status)) { + Py_DECREF(tmp); + PyErr_NoMemory(); + return NULL; + } + + exp = mpd_iszero(MPD(tmp)) ? 0 : MPD(tmp)->exp; + MPD(tmp)->exp = 0; + + /* context and rounding are unused here: the conversion is exact */ + numerator = dec_as_long(tmp, context, MPD_ROUND_FLOOR); + Py_DECREF(tmp); + if (numerator == NULL) { + goto error; + } + + exponent = PyLong_FromSsize_t(exp < 0 ? -exp : exp); + if (exponent == NULL) { + goto error; + } + + tmp = PyLong_FromLong(10); + if (tmp == NULL) { + goto error; + } + + Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None)); + Py_DECREF(tmp); + if (exponent == NULL) { + goto error; + } + + if (exp >= 0) { + Py_SETREF(numerator, _py_long_multiply(numerator, exponent)); + if (numerator == NULL) { + goto error; + } + denominator = PyLong_FromLong(1); + if (denominator == NULL) { + goto error; + } + } + else { + denominator = exponent; + exponent = NULL; + tmp = _PyLong_GCD(numerator, denominator); + if (tmp == NULL) { + goto error; + } + Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp)); + Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp)); + Py_DECREF(tmp); + if (numerator == NULL || denominator == NULL) { + goto error; + } + } + + result = PyTuple_Pack(2, numerator, denominator); + + +error: + Py_XDECREF(exponent); + Py_XDECREF(denominator); + Py_XDECREF(numerator); + return result; +} + +static PyObject * +PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"rounding", "context", NULL}; + PyObject *result; + PyObject *rounding = Py_None; + PyObject *context = Py_None; + uint32_t status = 0; + mpd_context_t workctx; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, + &rounding, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + workctx = *CTX(context); + if (rounding != Py_None) { + int round = getround(rounding); + if (round < 0) { + return NULL; + } + if (!mpd_qsetround(&workctx, round)) { + INTERNAL_ERROR_PTR("PyDec_ToIntegralValue"); /* GCOV_NOT_REACHED */ + } + } + + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + mpd_qround_to_int(MPD(result), MPD(dec), &workctx, &status); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +static PyObject * +PyDec_ToIntegralExact(PyObject *dec, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"rounding", "context", NULL}; + PyObject *result; + PyObject *rounding = Py_None; + PyObject *context = Py_None; + uint32_t status = 0; + mpd_context_t workctx; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, + &rounding, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + workctx = *CTX(context); + if (rounding != Py_None) { + int round = getround(rounding); + if (round < 0) { + return NULL; + } + if (!mpd_qsetround(&workctx, round)) { + INTERNAL_ERROR_PTR("PyDec_ToIntegralExact"); /* GCOV_NOT_REACHED */ + } + } + + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + mpd_qround_to_intx(MPD(result), MPD(dec), &workctx, &status); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +static PyObject * +PyDec_AsFloat(PyObject *dec) +{ + PyObject *f, *s; + + if (mpd_isnan(MPD(dec))) { + if (mpd_issnan(MPD(dec))) { + PyErr_SetString(PyExc_ValueError, + "cannot convert signaling NaN to float"); + return NULL; + } + if (mpd_isnegative(MPD(dec))) { + s = PyUnicode_FromString("-nan"); + } + else { + s = PyUnicode_FromString("nan"); + } + } + else { + s = dec_str(dec); + } + + if (s == NULL) { + return NULL; + } + + f = PyFloat_FromString(s); + Py_DECREF(s); + + return f; +} + +static PyObject * +PyDec_Round(PyObject *dec, PyObject *args) +{ + PyObject *result; + PyObject *x = NULL; + uint32_t status = 0; + PyObject *context; + + + CURRENT_CONTEXT(context); + if (!PyArg_ParseTuple(args, "|O", &x)) { + return NULL; + } + + if (x) { + mpd_uint_t dq[1] = {1}; + mpd_t q = {MPD_STATIC|MPD_CONST_DATA,0,1,1,1,dq}; + mpd_ssize_t y; + + if (!PyLong_Check(x)) { + PyErr_SetString(PyExc_TypeError, + "optional arg must be an integer"); + return NULL; + } + + y = PyLong_AsSsize_t(x); + if (y == -1 && PyErr_Occurred()) { + return NULL; + } + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + q.exp = (y == MPD_SSIZE_MIN) ? MPD_SSIZE_MAX : -y; + mpd_qquantize(MPD(result), MPD(dec), &q, CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; + } + else { + return dec_as_long(dec, context, MPD_ROUND_HALF_EVEN); + } +} + +static PyTypeObject *DecimalTuple = NULL; +/* Return the DecimalTuple representation of a PyDecObject. */ +static PyObject * +PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED) +{ + PyObject *result = NULL; + PyObject *sign = NULL; + PyObject *coeff = NULL; + PyObject *expt = NULL; + PyObject *tmp = NULL; + mpd_t *x = NULL; + char *intstring = NULL; + Py_ssize_t intlen, i; + + + x = mpd_qncopy(MPD(dec)); + if (x == NULL) { + PyErr_NoMemory(); + goto out; + } + + sign = PyLong_FromUnsignedLong(mpd_sign(MPD(dec))); + if (sign == NULL) { + goto out; + } + + if (mpd_isinfinite(x)) { + expt = PyUnicode_FromString("F"); + if (expt == NULL) { + goto out; + } + /* decimal.py has non-compliant infinity payloads. */ + coeff = Py_BuildValue("(i)", 0); + if (coeff == NULL) { + goto out; + } + } + else { + if (mpd_isnan(x)) { + expt = PyUnicode_FromString(mpd_isqnan(x)?"n":"N"); + } + else { + expt = PyLong_FromSsize_t(MPD(dec)->exp); + } + if (expt == NULL) { + goto out; + } + + /* coefficient is defined */ + if (x->len > 0) { + + /* make an integer */ + x->exp = 0; + /* clear NaN and sign */ + mpd_clear_flags(x); + intstring = mpd_to_sci(x, 1); + if (intstring == NULL) { + PyErr_NoMemory(); + goto out; + } + + intlen = strlen(intstring); + coeff = PyTuple_New(intlen); + if (coeff == NULL) { + goto out; + } + + for (i = 0; i < intlen; i++) { + tmp = PyLong_FromLong(intstring[i]-'0'); + if (tmp == NULL) { + goto out; + } + PyTuple_SET_ITEM(coeff, i, tmp); + } + } + else { + coeff = PyTuple_New(0); + if (coeff == NULL) { + goto out; + } + } + } + + result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple, + sign, coeff, expt, NULL); + +out: + if (x) mpd_del(x); + if (intstring) mpd_free(intstring); + Py_XDECREF(sign); + Py_XDECREF(coeff); + Py_XDECREF(expt); + return result; +} + + +/******************************************************************************/ +/* Macros for converting mpdecimal functions to Decimal methods */ +/******************************************************************************/ + +/* Unary number method that uses the default module context. */ +#define Dec_UnaryNumberMethod(MPDFUNC) \ +static PyObject * \ +nm_##MPDFUNC(PyObject *self) \ +{ \ + PyObject *result; \ + PyObject *context; \ + uint32_t status = 0; \ + \ + CURRENT_CONTEXT(context); \ + if ((result = dec_alloc()) == NULL) { \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* Binary number method that uses default module context. */ +#define Dec_BinaryNumberMethod(MPDFUNC) \ +static PyObject * \ +nm_##MPDFUNC(PyObject *self, PyObject *other) \ +{ \ + PyObject *a, *b; \ + PyObject *result; \ + PyObject *context; \ + uint32_t status = 0; \ + \ + CURRENT_CONTEXT(context) ; \ + CONVERT_BINOP(&a, &b, self, other, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* Boolean function without a context arg. */ +#define Dec_BoolFunc(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *dummy UNUSED) \ +{ \ + return MPDFUNC(MPD(self)) ? incr_true() : incr_false(); \ +} + +/* Boolean function with an optional context arg. */ +#define Dec_BoolFuncVA(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ +{ \ + static char *kwlist[] = {"context", NULL}; \ + PyObject *context = Py_None; \ + \ + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ + &context)) { \ + return NULL; \ + } \ + CONTEXT_CHECK_VA(context); \ + \ + return MPDFUNC(MPD(self), CTX(context)) ? incr_true() : incr_false(); \ +} + +/* Unary function with an optional context arg. */ +#define Dec_UnaryFuncVA(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ +{ \ + static char *kwlist[] = {"context", NULL}; \ + PyObject *result; \ + PyObject *context = Py_None; \ + uint32_t status = 0; \ + \ + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, \ + &context)) { \ + return NULL; \ + } \ + CONTEXT_CHECK_VA(context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(self), CTX(context), &status); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* Binary function with an optional context arg. */ +#define Dec_BinaryFuncVA(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ +{ \ + static char *kwlist[] = {"other", "context", NULL}; \ + PyObject *other; \ + PyObject *a, *b; \ + PyObject *result; \ + PyObject *context = Py_None; \ + uint32_t status = 0; \ + \ + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ + &other, &context)) { \ + return NULL; \ + } \ + CONTEXT_CHECK_VA(context); \ + CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* Binary function with an optional context arg. Actual MPDFUNC does + NOT take a context. The context is used to record InvalidOperation + if the second operand cannot be converted exactly. */ +#define Dec_BinaryFuncVA_NO_CTX(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ +{ \ + static char *kwlist[] = {"other", "context", NULL}; \ + PyObject *context = Py_None; \ + PyObject *other; \ + PyObject *a, *b; \ + PyObject *result; \ + \ + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, \ + &other, &context)) { \ + return NULL; \ + } \ + CONTEXT_CHECK_VA(context); \ + CONVERT_BINOP_RAISE(&a, &b, self, other, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b)); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + \ + return result; \ +} + +/* Ternary function with an optional context arg. */ +#define Dec_TernaryFuncVA(MPDFUNC) \ +static PyObject * \ +dec_##MPDFUNC(PyObject *self, PyObject *args, PyObject *kwds) \ +{ \ + static char *kwlist[] = {"other", "third", "context", NULL}; \ + PyObject *other, *third; \ + PyObject *a, *b, *c; \ + PyObject *result; \ + PyObject *context = Py_None; \ + uint32_t status = 0; \ + \ + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, \ + &other, &third, &context)) { \ + return NULL; \ + } \ + CONTEXT_CHECK_VA(context); \ + CONVERT_TERNOP_RAISE(&a, &b, &c, self, other, third, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + Py_DECREF(c); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + Py_DECREF(c); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + + +/**********************************************/ +/* Number methods */ +/**********************************************/ + +Dec_UnaryNumberMethod(mpd_qminus) +Dec_UnaryNumberMethod(mpd_qplus) +Dec_UnaryNumberMethod(mpd_qabs) + +Dec_BinaryNumberMethod(mpd_qadd) +Dec_BinaryNumberMethod(mpd_qsub) +Dec_BinaryNumberMethod(mpd_qmul) +Dec_BinaryNumberMethod(mpd_qdiv) +Dec_BinaryNumberMethod(mpd_qrem) +Dec_BinaryNumberMethod(mpd_qdivint) + +static PyObject * +nm_dec_as_long(PyObject *dec) +{ + PyObject *context; + + CURRENT_CONTEXT(context); + return dec_as_long(dec, context, MPD_ROUND_DOWN); +} + +static int +nm_nonzero(PyObject *v) +{ + return !mpd_iszero(MPD(v)); +} + +static PyObject * +nm_mpd_qdivmod(PyObject *v, PyObject *w) +{ + PyObject *a, *b; + PyObject *q, *r; + PyObject *context; + uint32_t status = 0; + PyObject *ret; + + CURRENT_CONTEXT(context); + CONVERT_BINOP(&a, &b, v, w, context); + + q = dec_alloc(); + if (q == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + r = dec_alloc(); + if (r == NULL) { + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(q); + return NULL; + } + + mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(r); + Py_DECREF(q); + return NULL; + } + + ret = Py_BuildValue("(OO)", q, r); + Py_DECREF(r); + Py_DECREF(q); + return ret; +} + +static PyObject * +nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod) +{ + PyObject *a, *b, *c = NULL; + PyObject *result; + PyObject *context; + uint32_t status = 0; + + CURRENT_CONTEXT(context); + CONVERT_BINOP(&a, &b, base, exp, context); + + if (mod != Py_None) { + if (!convert_op(NOT_IMPL, &c, mod, context)) { + Py_DECREF(a); + Py_DECREF(b); + return c; + } + } + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + Py_DECREF(b); + Py_XDECREF(c); + return NULL; + } + + if (c == NULL) { + mpd_qpow(MPD(result), MPD(a), MPD(b), + CTX(context), &status); + } + else { + mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), + CTX(context), &status); + Py_DECREF(c); + } + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + + +/******************************************************************************/ +/* Decimal Methods */ +/******************************************************************************/ + +/* Unary arithmetic functions, optional context arg */ +Dec_UnaryFuncVA(mpd_qexp) +Dec_UnaryFuncVA(mpd_qln) +Dec_UnaryFuncVA(mpd_qlog10) +Dec_UnaryFuncVA(mpd_qnext_minus) +Dec_UnaryFuncVA(mpd_qnext_plus) +Dec_UnaryFuncVA(mpd_qreduce) +Dec_UnaryFuncVA(mpd_qsqrt) + +/* Binary arithmetic functions, optional context arg */ +Dec_BinaryFuncVA(mpd_qcompare) +Dec_BinaryFuncVA(mpd_qcompare_signal) +Dec_BinaryFuncVA(mpd_qmax) +Dec_BinaryFuncVA(mpd_qmax_mag) +Dec_BinaryFuncVA(mpd_qmin) +Dec_BinaryFuncVA(mpd_qmin_mag) +Dec_BinaryFuncVA(mpd_qnext_toward) +Dec_BinaryFuncVA(mpd_qrem_near) + +/* Ternary arithmetic functions, optional context arg */ +Dec_TernaryFuncVA(mpd_qfma) + +/* Boolean functions, no context arg */ +Dec_BoolFunc(mpd_iscanonical) +Dec_BoolFunc(mpd_isfinite) +Dec_BoolFunc(mpd_isinfinite) +Dec_BoolFunc(mpd_isnan) +Dec_BoolFunc(mpd_isqnan) +Dec_BoolFunc(mpd_issnan) +Dec_BoolFunc(mpd_issigned) +Dec_BoolFunc(mpd_iszero) + +/* Boolean functions, optional context arg */ +Dec_BoolFuncVA(mpd_isnormal) +Dec_BoolFuncVA(mpd_issubnormal) + +/* Unary functions, no context arg */ +static PyObject * +dec_mpd_adjexp(PyObject *self, PyObject *dummy UNUSED) +{ + mpd_ssize_t retval; + + if (mpd_isspecial(MPD(self))) { + retval = 0; + } + else { + retval = mpd_adjexp(MPD(self)); + } + + return PyLong_FromSsize_t(retval); +} + +static PyObject * +dec_canonical(PyObject *self, PyObject *dummy UNUSED) +{ + Py_INCREF(self); + return self; +} + +static PyObject * +dec_conjugate(PyObject *self, PyObject *dummy UNUSED) +{ + Py_INCREF(self); + return self; +} + +static PyObject * +dec_mpd_radix(PyObject *self UNUSED, PyObject *dummy UNUSED) +{ + PyObject *result; + + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + _dec_settriple(result, MPD_POS, 10, 0); + return result; +} + +static PyObject * +dec_mpd_qcopy_abs(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *result; + uint32_t status = 0; + + if ((result = dec_alloc()) == NULL) { + return NULL; + } + + mpd_qcopy_abs(MPD(result), MPD(self), &status); + if (status & MPD_Malloc_error) { + Py_DECREF(result); + PyErr_NoMemory(); + return NULL; + } + + return result; +} + +static PyObject * +dec_mpd_qcopy_negate(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *result; + uint32_t status = 0; + + if ((result = dec_alloc()) == NULL) { + return NULL; + } + + mpd_qcopy_negate(MPD(result), MPD(self), &status); + if (status & MPD_Malloc_error) { + Py_DECREF(result); + PyErr_NoMemory(); + return NULL; + } + + return result; +} + +/* Unary functions, optional context arg */ +Dec_UnaryFuncVA(mpd_qinvert) +Dec_UnaryFuncVA(mpd_qlogb) + +static PyObject * +dec_mpd_class(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"context", NULL}; + PyObject *context = Py_None; + const char *cp; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, + &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + cp = mpd_class(MPD(self), CTX(context)); + return PyUnicode_FromString(cp); +} + +static PyObject * +dec_mpd_to_eng(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"context", NULL}; + PyObject *result; + PyObject *context = Py_None; + mpd_ssize_t size; + char *s; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, + &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + size = mpd_to_eng_size(&s, MPD(self), CtxCaps(context)); + if (size < 0) { + PyErr_NoMemory(); + return NULL; + } + + result = unicode_fromascii(s, size); + mpd_free(s); + + return result; +} + +/* Binary functions, optional context arg for conversion errors */ +Dec_BinaryFuncVA_NO_CTX(mpd_compare_total) +Dec_BinaryFuncVA_NO_CTX(mpd_compare_total_mag) + +static PyObject * +dec_mpd_qcopy_sign(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"other", "context", NULL}; + PyObject *other; + PyObject *a, *b; + PyObject *result; + PyObject *context = Py_None; + uint32_t status = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, + &other, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + CONVERT_BINOP_RAISE(&a, &b, self, other, context); + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + + mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +static PyObject * +dec_mpd_same_quantum(PyObject *self, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"other", "context", NULL}; + PyObject *other; + PyObject *a, *b; + PyObject *result; + PyObject *context = Py_None; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, + &other, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + CONVERT_BINOP_RAISE(&a, &b, self, other, context); + + result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); + Py_DECREF(a); + Py_DECREF(b); + + return result; +} + +/* Binary functions, optional context arg */ +Dec_BinaryFuncVA(mpd_qand) +Dec_BinaryFuncVA(mpd_qor) +Dec_BinaryFuncVA(mpd_qxor) + +Dec_BinaryFuncVA(mpd_qrotate) +Dec_BinaryFuncVA(mpd_qscaleb) +Dec_BinaryFuncVA(mpd_qshift) + +static PyObject * +dec_mpd_qquantize(PyObject *v, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"exp", "rounding", "context", NULL}; + PyObject *rounding = Py_None; + PyObject *context = Py_None; + PyObject *w, *a, *b; + PyObject *result; + uint32_t status = 0; + mpd_context_t workctx; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, + &w, &rounding, &context)) { + return NULL; + } + CONTEXT_CHECK_VA(context); + + workctx = *CTX(context); + if (rounding != Py_None) { + int round = getround(rounding); + if (round < 0) { + return NULL; + } + if (!mpd_qsetround(&workctx, round)) { + INTERNAL_ERROR_PTR("dec_mpd_qquantize"); /* GCOV_NOT_REACHED */ + } + } + + CONVERT_BINOP_RAISE(&a, &b, v, w, context); + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + + mpd_qquantize(MPD(result), MPD(a), MPD(b), &workctx, &status); + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +/* Special methods */ +static PyObject * +dec_richcompare(PyObject *v, PyObject *w, int op) +{ + PyObject *a; + PyObject *b; + PyObject *context; + uint32_t status = 0; + int a_issnan, b_issnan; + int r; + + assert(PyDec_Check(v)); + + CURRENT_CONTEXT(context); + CONVERT_BINOP_CMP(&a, &b, v, w, op, context); + + a_issnan = mpd_issnan(MPD(a)); + b_issnan = mpd_issnan(MPD(b)); + + r = mpd_qcmp(MPD(a), MPD(b), &status); + Py_DECREF(a); + Py_DECREF(b); + if (r == INT_MAX) { + /* sNaNs or op={le,ge,lt,gt} always signal. */ + if (a_issnan || b_issnan || (op != Py_EQ && op != Py_NE)) { + if (dec_addstatus(context, status)) { + return NULL; + } + } + /* qNaN comparison with op={eq,ne} or comparison + * with InvalidOperation disabled. */ + return (op == Py_NE) ? incr_true() : incr_false(); + } + + switch (op) { + case Py_EQ: + r = (r == 0); + break; + case Py_NE: + r = (r != 0); + break; + case Py_LE: + r = (r <= 0); + break; + case Py_GE: + r = (r >= 0); + break; + case Py_LT: + r = (r == -1); + break; + case Py_GT: + r = (r == 1); + break; + } + + return PyBool_FromLong(r); +} + +/* __ceil__ */ +static PyObject * +dec_ceil(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *context; + + CURRENT_CONTEXT(context); + return dec_as_long(self, context, MPD_ROUND_CEILING); +} + +/* __complex__ */ +static PyObject * +dec_complex(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *f; + double x; + + f = PyDec_AsFloat(self); + if (f == NULL) { + return NULL; + } + + x = PyFloat_AsDouble(f); + Py_DECREF(f); + if (x == -1.0 && PyErr_Occurred()) { + return NULL; + } + + return PyComplex_FromDoubles(x, 0); +} + +/* __copy__ and __deepcopy__ */ +static PyObject * +dec_copy(PyObject *self, PyObject *dummy UNUSED) +{ + Py_INCREF(self); + return self; +} + +/* __floor__ */ +static PyObject * +dec_floor(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *context; + + CURRENT_CONTEXT(context); + return dec_as_long(self, context, MPD_ROUND_FLOOR); +} + +/* Always uses the module context */ +static Py_hash_t +_dec_hash(PyDecObject *v) +{ +#if defined(CONFIG_64) && _PyHASH_BITS == 61 + /* 2**61 - 1 */ + mpd_uint_t p_data[1] = {2305843009213693951ULL}; + mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 19, 1, 1, p_data}; + /* Inverse of 10 modulo p */ + mpd_uint_t inv10_p_data[1] = {2075258708292324556ULL}; + mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, + 0, 19, 1, 1, inv10_p_data}; +#elif defined(CONFIG_32) && _PyHASH_BITS == 31 + /* 2**31 - 1 */ + mpd_uint_t p_data[2] = {147483647UL, 2}; + mpd_t p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, 0, 10, 2, 2, p_data}; + /* Inverse of 10 modulo p */ + mpd_uint_t inv10_p_data[2] = {503238553UL, 1}; + mpd_t inv10_p = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, + 0, 10, 2, 2, inv10_p_data}; +#else + #error "No valid combination of CONFIG_64, CONFIG_32 and _PyHASH_BITS" +#endif + const Py_hash_t py_hash_inf = 314159; + const Py_hash_t py_hash_nan = 0; + mpd_uint_t ten_data[1] = {10}; + mpd_t ten = {MPD_POS|MPD_STATIC|MPD_CONST_DATA, + 0, 2, 1, 1, ten_data}; + Py_hash_t result; + mpd_t *exp_hash = NULL; + mpd_t *tmp = NULL; + mpd_ssize_t exp; + uint32_t status = 0; + mpd_context_t maxctx; + + + if (mpd_isspecial(MPD(v))) { + if (mpd_issnan(MPD(v))) { + PyErr_SetString(PyExc_TypeError, + "Cannot hash a signaling NaN value"); + return -1; + } + else if (mpd_isnan(MPD(v))) { + return py_hash_nan; + } + else { + return py_hash_inf * mpd_arith_sign(MPD(v)); + } + } + + mpd_maxcontext(&maxctx); + exp_hash = mpd_qnew(); + if (exp_hash == NULL) { + goto malloc_error; + } + tmp = mpd_qnew(); + if (tmp == NULL) { + goto malloc_error; + } + + /* + * exp(v): exponent of v + * int(v): coefficient of v + */ + exp = MPD(v)->exp; + if (exp >= 0) { + /* 10**exp(v) % p */ + mpd_qsset_ssize(tmp, exp, &maxctx, &status); + mpd_qpowmod(exp_hash, &ten, tmp, &p, &maxctx, &status); + } + else { + /* inv10_p**(-exp(v)) % p */ + mpd_qsset_ssize(tmp, -exp, &maxctx, &status); + mpd_qpowmod(exp_hash, &inv10_p, tmp, &p, &maxctx, &status); + } + + /* hash = (int(v) * exp_hash) % p */ + if (!mpd_qcopy(tmp, MPD(v), &status)) { + goto malloc_error; + } + tmp->exp = 0; + mpd_set_positive(tmp); + + maxctx.prec = MPD_MAX_PREC + 21; + maxctx.emax = MPD_MAX_EMAX + 21; + maxctx.emin = MPD_MIN_EMIN - 21; + + mpd_qmul(tmp, tmp, exp_hash, &maxctx, &status); + mpd_qrem(tmp, tmp, &p, &maxctx, &status); + + result = mpd_qget_ssize(tmp, &status); + result = mpd_ispositive(MPD(v)) ? result : -result; + result = (result == -1) ? -2 : result; + + if (status != 0) { + if (status & MPD_Malloc_error) { + goto malloc_error; + } + else { + PyErr_SetString(PyExc_RuntimeError, /* GCOV_NOT_REACHED */ + "dec_hash: internal error: please report"); /* GCOV_NOT_REACHED */ + } + result = -1; /* GCOV_NOT_REACHED */ + } + + +finish: + if (exp_hash) mpd_del(exp_hash); + if (tmp) mpd_del(tmp); + return result; + +malloc_error: + PyErr_NoMemory(); + result = -1; + goto finish; +} + +static Py_hash_t +dec_hash(PyDecObject *self) +{ + if (self->hash == -1) { + self->hash = _dec_hash(self); + } + + return self->hash; +} + +/* __reduce__ */ +static PyObject * +dec_reduce(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *result, *str; + + str = dec_str(self); + if (str == NULL) { + return NULL; + } + + result = Py_BuildValue("O(O)", Py_TYPE(self), str); + Py_DECREF(str); + + return result; +} + +/* __sizeof__ */ +static PyObject * +dec_sizeof(PyObject *v, PyObject *dummy UNUSED) +{ + Py_ssize_t res; + + res = _PyObject_SIZE(Py_TYPE(v)); + if (mpd_isdynamic_data(MPD(v))) { + res += MPD(v)->alloc * sizeof(mpd_uint_t); + } + return PyLong_FromSsize_t(res); +} + +/* __trunc__ */ +static PyObject * +dec_trunc(PyObject *self, PyObject *dummy UNUSED) +{ + PyObject *context; + + CURRENT_CONTEXT(context); + return dec_as_long(self, context, MPD_ROUND_DOWN); +} + +/* real and imag */ +static PyObject * +dec_real(PyObject *self, void *closure UNUSED) +{ + Py_INCREF(self); + return self; +} + +static PyObject * +dec_imag(PyObject *self UNUSED, void *closure UNUSED) +{ + PyObject *result; + + result = dec_alloc(); + if (result == NULL) { + return NULL; + } + + _dec_settriple(result, MPD_POS, 0, 0); + return result; +} + + +static PyGetSetDef dec_getsets [] = +{ + { "real", (getter)dec_real, NULL, NULL, NULL}, + { "imag", (getter)dec_imag, NULL, NULL, NULL}, + {NULL} +}; + +static PyNumberMethods dec_number_methods = +{ + (binaryfunc) nm_mpd_qadd, + (binaryfunc) nm_mpd_qsub, + (binaryfunc) nm_mpd_qmul, + (binaryfunc) nm_mpd_qrem, + (binaryfunc) nm_mpd_qdivmod, + (ternaryfunc) nm_mpd_qpow, + (unaryfunc) nm_mpd_qminus, + (unaryfunc) nm_mpd_qplus, + (unaryfunc) nm_mpd_qabs, + (inquiry) nm_nonzero, + (unaryfunc) 0, /* no bit-complement */ + (binaryfunc) 0, /* no shiftl */ + (binaryfunc) 0, /* no shiftr */ + (binaryfunc) 0, /* no bit-and */ + (binaryfunc) 0, /* no bit-xor */ + (binaryfunc) 0, /* no bit-ior */ + (unaryfunc) nm_dec_as_long, + 0, /* nb_reserved */ + (unaryfunc) PyDec_AsFloat, + 0, /* binaryfunc nb_inplace_add; */ + 0, /* binaryfunc nb_inplace_subtract; */ + 0, /* binaryfunc nb_inplace_multiply; */ + 0, /* binaryfunc nb_inplace_remainder; */ + 0, /* ternaryfunc nb_inplace_power; */ + 0, /* binaryfunc nb_inplace_lshift; */ + 0, /* binaryfunc nb_inplace_rshift; */ + 0, /* binaryfunc nb_inplace_and; */ + 0, /* binaryfunc nb_inplace_xor; */ + 0, /* binaryfunc nb_inplace_or; */ + (binaryfunc) nm_mpd_qdivint, /* binaryfunc nb_floor_divide; */ + (binaryfunc) nm_mpd_qdiv, /* binaryfunc nb_true_divide; */ + 0, /* binaryfunc nb_inplace_floor_divide; */ + 0, /* binaryfunc nb_inplace_true_divide; */ +}; + +static PyMethodDef dec_methods [] = +{ + /* Unary arithmetic functions, optional context arg */ { "exp", (PyCFunction)(void(*)(void))dec_mpd_qexp, METH_VARARGS|METH_KEYWORDS, doc_exp }, { "ln", (PyCFunction)(void(*)(void))dec_mpd_qln, METH_VARARGS|METH_KEYWORDS, doc_ln }, { "log10", (PyCFunction)(void(*)(void))dec_mpd_qlog10, METH_VARARGS|METH_KEYWORDS, doc_log10 }, @@ -4745,8 +4745,8 @@ static PyMethodDef dec_methods [] = { "to_integral_exact", (PyCFunction)(void(*)(void))PyDec_ToIntegralExact, METH_VARARGS|METH_KEYWORDS, doc_to_integral_exact }, { "to_integral_value", (PyCFunction)(void(*)(void))PyDec_ToIntegralValue, METH_VARARGS|METH_KEYWORDS, doc_to_integral_value }, { "sqrt", (PyCFunction)(void(*)(void))dec_mpd_qsqrt, METH_VARARGS|METH_KEYWORDS, doc_sqrt }, - - /* Binary arithmetic functions, optional context arg */ + + /* Binary arithmetic functions, optional context arg */ { "compare", (PyCFunction)(void(*)(void))dec_mpd_qcompare, METH_VARARGS|METH_KEYWORDS, doc_compare }, { "compare_signal", (PyCFunction)(void(*)(void))dec_mpd_qcompare_signal, METH_VARARGS|METH_KEYWORDS, doc_compare_signal }, { "max", (PyCFunction)(void(*)(void))dec_mpd_qmax, METH_VARARGS|METH_KEYWORDS, doc_max }, @@ -4756,1174 +4756,1174 @@ static PyMethodDef dec_methods [] = { "next_toward", (PyCFunction)(void(*)(void))dec_mpd_qnext_toward, METH_VARARGS|METH_KEYWORDS, doc_next_toward }, { "quantize", (PyCFunction)(void(*)(void))dec_mpd_qquantize, METH_VARARGS|METH_KEYWORDS, doc_quantize }, { "remainder_near", (PyCFunction)(void(*)(void))dec_mpd_qrem_near, METH_VARARGS|METH_KEYWORDS, doc_remainder_near }, - - /* Ternary arithmetic functions, optional context arg */ + + /* Ternary arithmetic functions, optional context arg */ { "fma", (PyCFunction)(void(*)(void))dec_mpd_qfma, METH_VARARGS|METH_KEYWORDS, doc_fma }, - - /* Boolean functions, no context arg */ - { "is_canonical", dec_mpd_iscanonical, METH_NOARGS, doc_is_canonical }, - { "is_finite", dec_mpd_isfinite, METH_NOARGS, doc_is_finite }, - { "is_infinite", dec_mpd_isinfinite, METH_NOARGS, doc_is_infinite }, - { "is_nan", dec_mpd_isnan, METH_NOARGS, doc_is_nan }, - { "is_qnan", dec_mpd_isqnan, METH_NOARGS, doc_is_qnan }, - { "is_snan", dec_mpd_issnan, METH_NOARGS, doc_is_snan }, - { "is_signed", dec_mpd_issigned, METH_NOARGS, doc_is_signed }, - { "is_zero", dec_mpd_iszero, METH_NOARGS, doc_is_zero }, - - /* Boolean functions, optional context arg */ + + /* Boolean functions, no context arg */ + { "is_canonical", dec_mpd_iscanonical, METH_NOARGS, doc_is_canonical }, + { "is_finite", dec_mpd_isfinite, METH_NOARGS, doc_is_finite }, + { "is_infinite", dec_mpd_isinfinite, METH_NOARGS, doc_is_infinite }, + { "is_nan", dec_mpd_isnan, METH_NOARGS, doc_is_nan }, + { "is_qnan", dec_mpd_isqnan, METH_NOARGS, doc_is_qnan }, + { "is_snan", dec_mpd_issnan, METH_NOARGS, doc_is_snan }, + { "is_signed", dec_mpd_issigned, METH_NOARGS, doc_is_signed }, + { "is_zero", dec_mpd_iszero, METH_NOARGS, doc_is_zero }, + + /* Boolean functions, optional context arg */ { "is_normal", (PyCFunction)(void(*)(void))dec_mpd_isnormal, METH_VARARGS|METH_KEYWORDS, doc_is_normal }, { "is_subnormal", (PyCFunction)(void(*)(void))dec_mpd_issubnormal, METH_VARARGS|METH_KEYWORDS, doc_is_subnormal }, - - /* Unary functions, no context arg */ - { "adjusted", dec_mpd_adjexp, METH_NOARGS, doc_adjusted }, - { "canonical", dec_canonical, METH_NOARGS, doc_canonical }, - { "conjugate", dec_conjugate, METH_NOARGS, doc_conjugate }, - { "radix", dec_mpd_radix, METH_NOARGS, doc_radix }, - - /* Unary functions, optional context arg for conversion errors */ - { "copy_abs", dec_mpd_qcopy_abs, METH_NOARGS, doc_copy_abs }, - { "copy_negate", dec_mpd_qcopy_negate, METH_NOARGS, doc_copy_negate }, - - /* Unary functions, optional context arg */ + + /* Unary functions, no context arg */ + { "adjusted", dec_mpd_adjexp, METH_NOARGS, doc_adjusted }, + { "canonical", dec_canonical, METH_NOARGS, doc_canonical }, + { "conjugate", dec_conjugate, METH_NOARGS, doc_conjugate }, + { "radix", dec_mpd_radix, METH_NOARGS, doc_radix }, + + /* Unary functions, optional context arg for conversion errors */ + { "copy_abs", dec_mpd_qcopy_abs, METH_NOARGS, doc_copy_abs }, + { "copy_negate", dec_mpd_qcopy_negate, METH_NOARGS, doc_copy_negate }, + + /* Unary functions, optional context arg */ { "logb", (PyCFunction)(void(*)(void))dec_mpd_qlogb, METH_VARARGS|METH_KEYWORDS, doc_logb }, { "logical_invert", (PyCFunction)(void(*)(void))dec_mpd_qinvert, METH_VARARGS|METH_KEYWORDS, doc_logical_invert }, { "number_class", (PyCFunction)(void(*)(void))dec_mpd_class, METH_VARARGS|METH_KEYWORDS, doc_number_class }, { "to_eng_string", (PyCFunction)(void(*)(void))dec_mpd_to_eng, METH_VARARGS|METH_KEYWORDS, doc_to_eng_string }, - - /* Binary functions, optional context arg for conversion errors */ + + /* Binary functions, optional context arg for conversion errors */ { "compare_total", (PyCFunction)(void(*)(void))dec_mpd_compare_total, METH_VARARGS|METH_KEYWORDS, doc_compare_total }, { "compare_total_mag", (PyCFunction)(void(*)(void))dec_mpd_compare_total_mag, METH_VARARGS|METH_KEYWORDS, doc_compare_total_mag }, { "copy_sign", (PyCFunction)(void(*)(void))dec_mpd_qcopy_sign, METH_VARARGS|METH_KEYWORDS, doc_copy_sign }, { "same_quantum", (PyCFunction)(void(*)(void))dec_mpd_same_quantum, METH_VARARGS|METH_KEYWORDS, doc_same_quantum }, - - /* Binary functions, optional context arg */ + + /* Binary functions, optional context arg */ { "logical_and", (PyCFunction)(void(*)(void))dec_mpd_qand, METH_VARARGS|METH_KEYWORDS, doc_logical_and }, { "logical_or", (PyCFunction)(void(*)(void))dec_mpd_qor, METH_VARARGS|METH_KEYWORDS, doc_logical_or }, { "logical_xor", (PyCFunction)(void(*)(void))dec_mpd_qxor, METH_VARARGS|METH_KEYWORDS, doc_logical_xor }, { "rotate", (PyCFunction)(void(*)(void))dec_mpd_qrotate, METH_VARARGS|METH_KEYWORDS, doc_rotate }, { "scaleb", (PyCFunction)(void(*)(void))dec_mpd_qscaleb, METH_VARARGS|METH_KEYWORDS, doc_scaleb }, { "shift", (PyCFunction)(void(*)(void))dec_mpd_qshift, METH_VARARGS|METH_KEYWORDS, doc_shift }, - - /* Miscellaneous */ - { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, - { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple }, - { "as_integer_ratio", dec_as_integer_ratio, METH_NOARGS, doc_as_integer_ratio }, - - /* Special methods */ - { "__copy__", dec_copy, METH_NOARGS, NULL }, - { "__deepcopy__", dec_copy, METH_O, NULL }, - { "__format__", dec_format, METH_VARARGS, NULL }, - { "__reduce__", dec_reduce, METH_NOARGS, NULL }, - { "__round__", PyDec_Round, METH_VARARGS, NULL }, - { "__ceil__", dec_ceil, METH_NOARGS, NULL }, - { "__floor__", dec_floor, METH_NOARGS, NULL }, - { "__trunc__", dec_trunc, METH_NOARGS, NULL }, - { "__complex__", dec_complex, METH_NOARGS, NULL }, - { "__sizeof__", dec_sizeof, METH_NOARGS, NULL }, - - { NULL, NULL, 1 } -}; - -static PyTypeObject PyDec_Type = -{ - PyVarObject_HEAD_INIT(NULL, 0) - "decimal.Decimal", /* tp_name */ - sizeof(PyDecObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor) dec_dealloc, /* tp_dealloc */ + + /* Miscellaneous */ + { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, + { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple }, + { "as_integer_ratio", dec_as_integer_ratio, METH_NOARGS, doc_as_integer_ratio }, + + /* Special methods */ + { "__copy__", dec_copy, METH_NOARGS, NULL }, + { "__deepcopy__", dec_copy, METH_O, NULL }, + { "__format__", dec_format, METH_VARARGS, NULL }, + { "__reduce__", dec_reduce, METH_NOARGS, NULL }, + { "__round__", PyDec_Round, METH_VARARGS, NULL }, + { "__ceil__", dec_ceil, METH_NOARGS, NULL }, + { "__floor__", dec_floor, METH_NOARGS, NULL }, + { "__trunc__", dec_trunc, METH_NOARGS, NULL }, + { "__complex__", dec_complex, METH_NOARGS, NULL }, + { "__sizeof__", dec_sizeof, METH_NOARGS, NULL }, + + { NULL, NULL, 1 } +}; + +static PyTypeObject PyDec_Type = +{ + PyVarObject_HEAD_INIT(NULL, 0) + "decimal.Decimal", /* tp_name */ + sizeof(PyDecObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor) dec_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - (getattrfunc) 0, /* tp_getattr */ - (setattrfunc) 0, /* tp_setattr */ + (getattrfunc) 0, /* tp_getattr */ + (setattrfunc) 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc) dec_repr, /* tp_repr */ - &dec_number_methods, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - (hashfunc) dec_hash, /* tp_hash */ - 0, /* tp_call */ - (reprfunc) dec_str, /* tp_str */ - (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ - (setattrofunc) 0, /* tp_setattro */ - (PyBufferProcs *) 0, /* tp_as_buffer */ - (Py_TPFLAGS_DEFAULT| - Py_TPFLAGS_BASETYPE), /* tp_flags */ - doc_decimal, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - dec_richcompare, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - dec_methods, /* tp_methods */ - 0, /* tp_members */ - dec_getsets, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - 0, /* tp_init */ - 0, /* tp_alloc */ - dec_new, /* tp_new */ - PyObject_Del, /* tp_free */ -}; - - -/******************************************************************************/ -/* Context Object, Part 2 */ -/******************************************************************************/ - - -/************************************************************************/ -/* Macros for converting mpdecimal functions to Context methods */ -/************************************************************************/ - -/* Boolean context method. */ -#define DecCtx_BoolFunc(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *v) \ -{ \ - PyObject *ret; \ - PyObject *a; \ - \ - CONVERT_OP_RAISE(&a, v, context); \ - \ - ret = MPDFUNC(MPD(a), CTX(context)) ? incr_true() : incr_false(); \ - Py_DECREF(a); \ - return ret; \ -} - -/* Boolean context method. MPDFUNC does NOT use a context. */ -#define DecCtx_BoolFunc_NO_CTX(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *v) \ -{ \ - PyObject *ret; \ - PyObject *a; \ - \ - CONVERT_OP_RAISE(&a, v, context); \ - \ - ret = MPDFUNC(MPD(a)) ? incr_true() : incr_false(); \ - Py_DECREF(a); \ - return ret; \ -} - -/* Unary context method. */ -#define DecCtx_UnaryFunc(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *v) \ -{ \ - PyObject *result, *a; \ - uint32_t status = 0; \ - \ - CONVERT_OP_RAISE(&a, v, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), CTX(context), &status); \ - Py_DECREF(a); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* Binary context method. */ -#define DecCtx_BinaryFunc(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *args) \ -{ \ - PyObject *v, *w; \ - PyObject *a, *b; \ - PyObject *result; \ - uint32_t status = 0; \ - \ - if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ - return NULL; \ - } \ - \ - CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - -/* - * Binary context method. The context is only used for conversion. - * The actual MPDFUNC does NOT take a context arg. - */ -#define DecCtx_BinaryFunc_NO_CTX(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *args) \ -{ \ - PyObject *v, *w; \ - PyObject *a, *b; \ - PyObject *result; \ - \ - if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ - return NULL; \ - } \ - \ - CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b)); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - \ - return result; \ -} - -/* Ternary context method. */ -#define DecCtx_TernaryFunc(MPDFUNC) \ -static PyObject * \ -ctx_##MPDFUNC(PyObject *context, PyObject *args) \ -{ \ - PyObject *v, *w, *x; \ - PyObject *a, *b, *c; \ - PyObject *result; \ - uint32_t status = 0; \ - \ - if (!PyArg_ParseTuple(args, "OOO", &v, &w, &x)) { \ - return NULL; \ - } \ - \ - CONVERT_TERNOP_RAISE(&a, &b, &c, v, w, x, context); \ - \ - if ((result = dec_alloc()) == NULL) { \ - Py_DECREF(a); \ - Py_DECREF(b); \ - Py_DECREF(c); \ - return NULL; \ - } \ - \ - MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ - Py_DECREF(a); \ - Py_DECREF(b); \ - Py_DECREF(c); \ - if (dec_addstatus(context, status)) { \ - Py_DECREF(result); \ - return NULL; \ - } \ - \ - return result; \ -} - - -/* Unary arithmetic functions */ -DecCtx_UnaryFunc(mpd_qabs) -DecCtx_UnaryFunc(mpd_qexp) -DecCtx_UnaryFunc(mpd_qln) -DecCtx_UnaryFunc(mpd_qlog10) -DecCtx_UnaryFunc(mpd_qminus) -DecCtx_UnaryFunc(mpd_qnext_minus) -DecCtx_UnaryFunc(mpd_qnext_plus) -DecCtx_UnaryFunc(mpd_qplus) -DecCtx_UnaryFunc(mpd_qreduce) -DecCtx_UnaryFunc(mpd_qround_to_int) -DecCtx_UnaryFunc(mpd_qround_to_intx) -DecCtx_UnaryFunc(mpd_qsqrt) - -/* Binary arithmetic functions */ -DecCtx_BinaryFunc(mpd_qadd) -DecCtx_BinaryFunc(mpd_qcompare) -DecCtx_BinaryFunc(mpd_qcompare_signal) -DecCtx_BinaryFunc(mpd_qdiv) -DecCtx_BinaryFunc(mpd_qdivint) -DecCtx_BinaryFunc(mpd_qmax) -DecCtx_BinaryFunc(mpd_qmax_mag) -DecCtx_BinaryFunc(mpd_qmin) -DecCtx_BinaryFunc(mpd_qmin_mag) -DecCtx_BinaryFunc(mpd_qmul) -DecCtx_BinaryFunc(mpd_qnext_toward) -DecCtx_BinaryFunc(mpd_qquantize) -DecCtx_BinaryFunc(mpd_qrem) -DecCtx_BinaryFunc(mpd_qrem_near) -DecCtx_BinaryFunc(mpd_qsub) - -static PyObject * -ctx_mpd_qdivmod(PyObject *context, PyObject *args) -{ - PyObject *v, *w; - PyObject *a, *b; - PyObject *q, *r; - uint32_t status = 0; - PyObject *ret; - - if (!PyArg_ParseTuple(args, "OO", &v, &w)) { - return NULL; - } - - CONVERT_BINOP_RAISE(&a, &b, v, w, context); - - q = dec_alloc(); - if (q == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - r = dec_alloc(); - if (r == NULL) { - Py_DECREF(a); - Py_DECREF(b); - Py_DECREF(q); - return NULL; - } - - mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(r); - Py_DECREF(q); - return NULL; - } - - ret = Py_BuildValue("(OO)", q, r); - Py_DECREF(r); - Py_DECREF(q); - return ret; -} - -/* Binary or ternary arithmetic functions */ -static PyObject * -ctx_mpd_qpow(PyObject *context, PyObject *args, PyObject *kwds) -{ - static char *kwlist[] = {"a", "b", "modulo", NULL}; - PyObject *base, *exp, *mod = Py_None; - PyObject *a, *b, *c = NULL; - PyObject *result; - uint32_t status = 0; - - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, - &base, &exp, &mod)) { - return NULL; - } - - CONVERT_BINOP_RAISE(&a, &b, base, exp, context); - - if (mod != Py_None) { - if (!convert_op(TYPE_ERR, &c, mod, context)) { - Py_DECREF(a); - Py_DECREF(b); - return c; - } - } - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - Py_DECREF(b); - Py_XDECREF(c); - return NULL; - } - - if (c == NULL) { - mpd_qpow(MPD(result), MPD(a), MPD(b), - CTX(context), &status); - } - else { - mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), - CTX(context), &status); - Py_DECREF(c); - } - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -/* Ternary arithmetic functions */ -DecCtx_TernaryFunc(mpd_qfma) - -/* No argument */ -static PyObject * -ctx_mpd_radix(PyObject *context, PyObject *dummy) -{ - return dec_mpd_radix(context, dummy); -} - -/* Boolean functions: single decimal argument */ -DecCtx_BoolFunc(mpd_isnormal) -DecCtx_BoolFunc(mpd_issubnormal) -DecCtx_BoolFunc_NO_CTX(mpd_isfinite) -DecCtx_BoolFunc_NO_CTX(mpd_isinfinite) -DecCtx_BoolFunc_NO_CTX(mpd_isnan) -DecCtx_BoolFunc_NO_CTX(mpd_isqnan) -DecCtx_BoolFunc_NO_CTX(mpd_issigned) -DecCtx_BoolFunc_NO_CTX(mpd_issnan) -DecCtx_BoolFunc_NO_CTX(mpd_iszero) - -static PyObject * -ctx_iscanonical(PyObject *context UNUSED, PyObject *v) -{ - if (!PyDec_Check(v)) { - PyErr_SetString(PyExc_TypeError, - "argument must be a Decimal"); - return NULL; - } - - return mpd_iscanonical(MPD(v)) ? incr_true() : incr_false(); -} - -/* Functions with a single decimal argument */ -static PyObject * -PyDecContext_Apply(PyObject *context, PyObject *v) -{ - PyObject *result, *a; - - CONVERT_OP_RAISE(&a, v, context); - - result = dec_apply(a, context); - Py_DECREF(a); - return result; -} - -static PyObject * -ctx_canonical(PyObject *context UNUSED, PyObject *v) -{ - if (!PyDec_Check(v)) { - PyErr_SetString(PyExc_TypeError, - "argument must be a Decimal"); - return NULL; - } - - Py_INCREF(v); - return v; -} - -static PyObject * -ctx_mpd_qcopy_abs(PyObject *context, PyObject *v) -{ - PyObject *result, *a; - uint32_t status = 0; - - CONVERT_OP_RAISE(&a, v, context); - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - return NULL; - } - - mpd_qcopy_abs(MPD(result), MPD(a), &status); - Py_DECREF(a); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -static PyObject * -ctx_copy_decimal(PyObject *context, PyObject *v) -{ - PyObject *result; - - CONVERT_OP_RAISE(&result, v, context); - return result; -} - -static PyObject * -ctx_mpd_qcopy_negate(PyObject *context, PyObject *v) -{ - PyObject *result, *a; - uint32_t status = 0; - - CONVERT_OP_RAISE(&a, v, context); - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - return NULL; - } - - mpd_qcopy_negate(MPD(result), MPD(a), &status); - Py_DECREF(a); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -DecCtx_UnaryFunc(mpd_qlogb) -DecCtx_UnaryFunc(mpd_qinvert) - -static PyObject * -ctx_mpd_class(PyObject *context, PyObject *v) -{ - PyObject *a; - const char *cp; - - CONVERT_OP_RAISE(&a, v, context); - - cp = mpd_class(MPD(a), CTX(context)); - Py_DECREF(a); - - return PyUnicode_FromString(cp); -} - -static PyObject * -ctx_mpd_to_sci(PyObject *context, PyObject *v) -{ - PyObject *result; - PyObject *a; - mpd_ssize_t size; - char *s; - - CONVERT_OP_RAISE(&a, v, context); - - size = mpd_to_sci_size(&s, MPD(a), CtxCaps(context)); - Py_DECREF(a); - if (size < 0) { - PyErr_NoMemory(); - return NULL; - } - - result = unicode_fromascii(s, size); - mpd_free(s); - - return result; -} - -static PyObject * -ctx_mpd_to_eng(PyObject *context, PyObject *v) -{ - PyObject *result; - PyObject *a; - mpd_ssize_t size; - char *s; - - CONVERT_OP_RAISE(&a, v, context); - - size = mpd_to_eng_size(&s, MPD(a), CtxCaps(context)); - Py_DECREF(a); - if (size < 0) { - PyErr_NoMemory(); - return NULL; - } - - result = unicode_fromascii(s, size); - mpd_free(s); - - return result; -} - -/* Functions with two decimal arguments */ -DecCtx_BinaryFunc_NO_CTX(mpd_compare_total) -DecCtx_BinaryFunc_NO_CTX(mpd_compare_total_mag) - -static PyObject * -ctx_mpd_qcopy_sign(PyObject *context, PyObject *args) -{ - PyObject *v, *w; - PyObject *a, *b; - PyObject *result; - uint32_t status = 0; - - if (!PyArg_ParseTuple(args, "OO", &v, &w)) { - return NULL; - } - - CONVERT_BINOP_RAISE(&a, &b, v, w, context); - - result = dec_alloc(); - if (result == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - - mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); - Py_DECREF(a); - Py_DECREF(b); - if (dec_addstatus(context, status)) { - Py_DECREF(result); - return NULL; - } - - return result; -} - -DecCtx_BinaryFunc(mpd_qand) -DecCtx_BinaryFunc(mpd_qor) -DecCtx_BinaryFunc(mpd_qxor) - -DecCtx_BinaryFunc(mpd_qrotate) -DecCtx_BinaryFunc(mpd_qscaleb) -DecCtx_BinaryFunc(mpd_qshift) - -static PyObject * -ctx_mpd_same_quantum(PyObject *context, PyObject *args) -{ - PyObject *v, *w; - PyObject *a, *b; - PyObject *result; - - if (!PyArg_ParseTuple(args, "OO", &v, &w)) { - return NULL; - } - - CONVERT_BINOP_RAISE(&a, &b, v, w, context); - - result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); - Py_DECREF(a); - Py_DECREF(b); - - return result; -} - - -static PyMethodDef context_methods [] = -{ - /* Unary arithmetic functions */ - { "abs", ctx_mpd_qabs, METH_O, doc_ctx_abs }, - { "exp", ctx_mpd_qexp, METH_O, doc_ctx_exp }, - { "ln", ctx_mpd_qln, METH_O, doc_ctx_ln }, - { "log10", ctx_mpd_qlog10, METH_O, doc_ctx_log10 }, - { "minus", ctx_mpd_qminus, METH_O, doc_ctx_minus }, - { "next_minus", ctx_mpd_qnext_minus, METH_O, doc_ctx_next_minus }, - { "next_plus", ctx_mpd_qnext_plus, METH_O, doc_ctx_next_plus }, - { "normalize", ctx_mpd_qreduce, METH_O, doc_ctx_normalize }, - { "plus", ctx_mpd_qplus, METH_O, doc_ctx_plus }, - { "to_integral", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral }, - { "to_integral_exact", ctx_mpd_qround_to_intx, METH_O, doc_ctx_to_integral_exact }, - { "to_integral_value", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral_value }, - { "sqrt", ctx_mpd_qsqrt, METH_O, doc_ctx_sqrt }, - - /* Binary arithmetic functions */ - { "add", ctx_mpd_qadd, METH_VARARGS, doc_ctx_add }, - { "compare", ctx_mpd_qcompare, METH_VARARGS, doc_ctx_compare }, - { "compare_signal", ctx_mpd_qcompare_signal, METH_VARARGS, doc_ctx_compare_signal }, - { "divide", ctx_mpd_qdiv, METH_VARARGS, doc_ctx_divide }, - { "divide_int", ctx_mpd_qdivint, METH_VARARGS, doc_ctx_divide_int }, - { "divmod", ctx_mpd_qdivmod, METH_VARARGS, doc_ctx_divmod }, - { "max", ctx_mpd_qmax, METH_VARARGS, doc_ctx_max }, - { "max_mag", ctx_mpd_qmax_mag, METH_VARARGS, doc_ctx_max_mag }, - { "min", ctx_mpd_qmin, METH_VARARGS, doc_ctx_min }, - { "min_mag", ctx_mpd_qmin_mag, METH_VARARGS, doc_ctx_min_mag }, - { "multiply", ctx_mpd_qmul, METH_VARARGS, doc_ctx_multiply }, - { "next_toward", ctx_mpd_qnext_toward, METH_VARARGS, doc_ctx_next_toward }, - { "quantize", ctx_mpd_qquantize, METH_VARARGS, doc_ctx_quantize }, - { "remainder", ctx_mpd_qrem, METH_VARARGS, doc_ctx_remainder }, - { "remainder_near", ctx_mpd_qrem_near, METH_VARARGS, doc_ctx_remainder_near }, - { "subtract", ctx_mpd_qsub, METH_VARARGS, doc_ctx_subtract }, - - /* Binary or ternary arithmetic functions */ + (reprfunc) dec_repr, /* tp_repr */ + &dec_number_methods, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + (hashfunc) dec_hash, /* tp_hash */ + 0, /* tp_call */ + (reprfunc) dec_str, /* tp_str */ + (getattrofunc) PyObject_GenericGetAttr, /* tp_getattro */ + (setattrofunc) 0, /* tp_setattro */ + (PyBufferProcs *) 0, /* tp_as_buffer */ + (Py_TPFLAGS_DEFAULT| + Py_TPFLAGS_BASETYPE), /* tp_flags */ + doc_decimal, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + dec_richcompare, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + dec_methods, /* tp_methods */ + 0, /* tp_members */ + dec_getsets, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + dec_new, /* tp_new */ + PyObject_Del, /* tp_free */ +}; + + +/******************************************************************************/ +/* Context Object, Part 2 */ +/******************************************************************************/ + + +/************************************************************************/ +/* Macros for converting mpdecimal functions to Context methods */ +/************************************************************************/ + +/* Boolean context method. */ +#define DecCtx_BoolFunc(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *v) \ +{ \ + PyObject *ret; \ + PyObject *a; \ + \ + CONVERT_OP_RAISE(&a, v, context); \ + \ + ret = MPDFUNC(MPD(a), CTX(context)) ? incr_true() : incr_false(); \ + Py_DECREF(a); \ + return ret; \ +} + +/* Boolean context method. MPDFUNC does NOT use a context. */ +#define DecCtx_BoolFunc_NO_CTX(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *v) \ +{ \ + PyObject *ret; \ + PyObject *a; \ + \ + CONVERT_OP_RAISE(&a, v, context); \ + \ + ret = MPDFUNC(MPD(a)) ? incr_true() : incr_false(); \ + Py_DECREF(a); \ + return ret; \ +} + +/* Unary context method. */ +#define DecCtx_UnaryFunc(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *v) \ +{ \ + PyObject *result, *a; \ + uint32_t status = 0; \ + \ + CONVERT_OP_RAISE(&a, v, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), CTX(context), &status); \ + Py_DECREF(a); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* Binary context method. */ +#define DecCtx_BinaryFunc(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *args) \ +{ \ + PyObject *v, *w; \ + PyObject *a, *b; \ + PyObject *result; \ + uint32_t status = 0; \ + \ + if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ + return NULL; \ + } \ + \ + CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b), CTX(context), &status); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + +/* + * Binary context method. The context is only used for conversion. + * The actual MPDFUNC does NOT take a context arg. + */ +#define DecCtx_BinaryFunc_NO_CTX(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *args) \ +{ \ + PyObject *v, *w; \ + PyObject *a, *b; \ + PyObject *result; \ + \ + if (!PyArg_ParseTuple(args, "OO", &v, &w)) { \ + return NULL; \ + } \ + \ + CONVERT_BINOP_RAISE(&a, &b, v, w, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b)); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + \ + return result; \ +} + +/* Ternary context method. */ +#define DecCtx_TernaryFunc(MPDFUNC) \ +static PyObject * \ +ctx_##MPDFUNC(PyObject *context, PyObject *args) \ +{ \ + PyObject *v, *w, *x; \ + PyObject *a, *b, *c; \ + PyObject *result; \ + uint32_t status = 0; \ + \ + if (!PyArg_ParseTuple(args, "OOO", &v, &w, &x)) { \ + return NULL; \ + } \ + \ + CONVERT_TERNOP_RAISE(&a, &b, &c, v, w, x, context); \ + \ + if ((result = dec_alloc()) == NULL) { \ + Py_DECREF(a); \ + Py_DECREF(b); \ + Py_DECREF(c); \ + return NULL; \ + } \ + \ + MPDFUNC(MPD(result), MPD(a), MPD(b), MPD(c), CTX(context), &status); \ + Py_DECREF(a); \ + Py_DECREF(b); \ + Py_DECREF(c); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + \ + return result; \ +} + + +/* Unary arithmetic functions */ +DecCtx_UnaryFunc(mpd_qabs) +DecCtx_UnaryFunc(mpd_qexp) +DecCtx_UnaryFunc(mpd_qln) +DecCtx_UnaryFunc(mpd_qlog10) +DecCtx_UnaryFunc(mpd_qminus) +DecCtx_UnaryFunc(mpd_qnext_minus) +DecCtx_UnaryFunc(mpd_qnext_plus) +DecCtx_UnaryFunc(mpd_qplus) +DecCtx_UnaryFunc(mpd_qreduce) +DecCtx_UnaryFunc(mpd_qround_to_int) +DecCtx_UnaryFunc(mpd_qround_to_intx) +DecCtx_UnaryFunc(mpd_qsqrt) + +/* Binary arithmetic functions */ +DecCtx_BinaryFunc(mpd_qadd) +DecCtx_BinaryFunc(mpd_qcompare) +DecCtx_BinaryFunc(mpd_qcompare_signal) +DecCtx_BinaryFunc(mpd_qdiv) +DecCtx_BinaryFunc(mpd_qdivint) +DecCtx_BinaryFunc(mpd_qmax) +DecCtx_BinaryFunc(mpd_qmax_mag) +DecCtx_BinaryFunc(mpd_qmin) +DecCtx_BinaryFunc(mpd_qmin_mag) +DecCtx_BinaryFunc(mpd_qmul) +DecCtx_BinaryFunc(mpd_qnext_toward) +DecCtx_BinaryFunc(mpd_qquantize) +DecCtx_BinaryFunc(mpd_qrem) +DecCtx_BinaryFunc(mpd_qrem_near) +DecCtx_BinaryFunc(mpd_qsub) + +static PyObject * +ctx_mpd_qdivmod(PyObject *context, PyObject *args) +{ + PyObject *v, *w; + PyObject *a, *b; + PyObject *q, *r; + uint32_t status = 0; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "OO", &v, &w)) { + return NULL; + } + + CONVERT_BINOP_RAISE(&a, &b, v, w, context); + + q = dec_alloc(); + if (q == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + r = dec_alloc(); + if (r == NULL) { + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(q); + return NULL; + } + + mpd_qdivmod(MPD(q), MPD(r), MPD(a), MPD(b), CTX(context), &status); + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(r); + Py_DECREF(q); + return NULL; + } + + ret = Py_BuildValue("(OO)", q, r); + Py_DECREF(r); + Py_DECREF(q); + return ret; +} + +/* Binary or ternary arithmetic functions */ +static PyObject * +ctx_mpd_qpow(PyObject *context, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"a", "b", "modulo", NULL}; + PyObject *base, *exp, *mod = Py_None; + PyObject *a, *b, *c = NULL; + PyObject *result; + uint32_t status = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O", kwlist, + &base, &exp, &mod)) { + return NULL; + } + + CONVERT_BINOP_RAISE(&a, &b, base, exp, context); + + if (mod != Py_None) { + if (!convert_op(TYPE_ERR, &c, mod, context)) { + Py_DECREF(a); + Py_DECREF(b); + return c; + } + } + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + Py_DECREF(b); + Py_XDECREF(c); + return NULL; + } + + if (c == NULL) { + mpd_qpow(MPD(result), MPD(a), MPD(b), + CTX(context), &status); + } + else { + mpd_qpowmod(MPD(result), MPD(a), MPD(b), MPD(c), + CTX(context), &status); + Py_DECREF(c); + } + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +/* Ternary arithmetic functions */ +DecCtx_TernaryFunc(mpd_qfma) + +/* No argument */ +static PyObject * +ctx_mpd_radix(PyObject *context, PyObject *dummy) +{ + return dec_mpd_radix(context, dummy); +} + +/* Boolean functions: single decimal argument */ +DecCtx_BoolFunc(mpd_isnormal) +DecCtx_BoolFunc(mpd_issubnormal) +DecCtx_BoolFunc_NO_CTX(mpd_isfinite) +DecCtx_BoolFunc_NO_CTX(mpd_isinfinite) +DecCtx_BoolFunc_NO_CTX(mpd_isnan) +DecCtx_BoolFunc_NO_CTX(mpd_isqnan) +DecCtx_BoolFunc_NO_CTX(mpd_issigned) +DecCtx_BoolFunc_NO_CTX(mpd_issnan) +DecCtx_BoolFunc_NO_CTX(mpd_iszero) + +static PyObject * +ctx_iscanonical(PyObject *context UNUSED, PyObject *v) +{ + if (!PyDec_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "argument must be a Decimal"); + return NULL; + } + + return mpd_iscanonical(MPD(v)) ? incr_true() : incr_false(); +} + +/* Functions with a single decimal argument */ +static PyObject * +PyDecContext_Apply(PyObject *context, PyObject *v) +{ + PyObject *result, *a; + + CONVERT_OP_RAISE(&a, v, context); + + result = dec_apply(a, context); + Py_DECREF(a); + return result; +} + +static PyObject * +ctx_canonical(PyObject *context UNUSED, PyObject *v) +{ + if (!PyDec_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "argument must be a Decimal"); + return NULL; + } + + Py_INCREF(v); + return v; +} + +static PyObject * +ctx_mpd_qcopy_abs(PyObject *context, PyObject *v) +{ + PyObject *result, *a; + uint32_t status = 0; + + CONVERT_OP_RAISE(&a, v, context); + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + return NULL; + } + + mpd_qcopy_abs(MPD(result), MPD(a), &status); + Py_DECREF(a); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +static PyObject * +ctx_copy_decimal(PyObject *context, PyObject *v) +{ + PyObject *result; + + CONVERT_OP_RAISE(&result, v, context); + return result; +} + +static PyObject * +ctx_mpd_qcopy_negate(PyObject *context, PyObject *v) +{ + PyObject *result, *a; + uint32_t status = 0; + + CONVERT_OP_RAISE(&a, v, context); + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + return NULL; + } + + mpd_qcopy_negate(MPD(result), MPD(a), &status); + Py_DECREF(a); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +DecCtx_UnaryFunc(mpd_qlogb) +DecCtx_UnaryFunc(mpd_qinvert) + +static PyObject * +ctx_mpd_class(PyObject *context, PyObject *v) +{ + PyObject *a; + const char *cp; + + CONVERT_OP_RAISE(&a, v, context); + + cp = mpd_class(MPD(a), CTX(context)); + Py_DECREF(a); + + return PyUnicode_FromString(cp); +} + +static PyObject * +ctx_mpd_to_sci(PyObject *context, PyObject *v) +{ + PyObject *result; + PyObject *a; + mpd_ssize_t size; + char *s; + + CONVERT_OP_RAISE(&a, v, context); + + size = mpd_to_sci_size(&s, MPD(a), CtxCaps(context)); + Py_DECREF(a); + if (size < 0) { + PyErr_NoMemory(); + return NULL; + } + + result = unicode_fromascii(s, size); + mpd_free(s); + + return result; +} + +static PyObject * +ctx_mpd_to_eng(PyObject *context, PyObject *v) +{ + PyObject *result; + PyObject *a; + mpd_ssize_t size; + char *s; + + CONVERT_OP_RAISE(&a, v, context); + + size = mpd_to_eng_size(&s, MPD(a), CtxCaps(context)); + Py_DECREF(a); + if (size < 0) { + PyErr_NoMemory(); + return NULL; + } + + result = unicode_fromascii(s, size); + mpd_free(s); + + return result; +} + +/* Functions with two decimal arguments */ +DecCtx_BinaryFunc_NO_CTX(mpd_compare_total) +DecCtx_BinaryFunc_NO_CTX(mpd_compare_total_mag) + +static PyObject * +ctx_mpd_qcopy_sign(PyObject *context, PyObject *args) +{ + PyObject *v, *w; + PyObject *a, *b; + PyObject *result; + uint32_t status = 0; + + if (!PyArg_ParseTuple(args, "OO", &v, &w)) { + return NULL; + } + + CONVERT_BINOP_RAISE(&a, &b, v, w, context); + + result = dec_alloc(); + if (result == NULL) { + Py_DECREF(a); + Py_DECREF(b); + return NULL; + } + + mpd_qcopy_sign(MPD(result), MPD(a), MPD(b), &status); + Py_DECREF(a); + Py_DECREF(b); + if (dec_addstatus(context, status)) { + Py_DECREF(result); + return NULL; + } + + return result; +} + +DecCtx_BinaryFunc(mpd_qand) +DecCtx_BinaryFunc(mpd_qor) +DecCtx_BinaryFunc(mpd_qxor) + +DecCtx_BinaryFunc(mpd_qrotate) +DecCtx_BinaryFunc(mpd_qscaleb) +DecCtx_BinaryFunc(mpd_qshift) + +static PyObject * +ctx_mpd_same_quantum(PyObject *context, PyObject *args) +{ + PyObject *v, *w; + PyObject *a, *b; + PyObject *result; + + if (!PyArg_ParseTuple(args, "OO", &v, &w)) { + return NULL; + } + + CONVERT_BINOP_RAISE(&a, &b, v, w, context); + + result = mpd_same_quantum(MPD(a), MPD(b)) ? incr_true() : incr_false(); + Py_DECREF(a); + Py_DECREF(b); + + return result; +} + + +static PyMethodDef context_methods [] = +{ + /* Unary arithmetic functions */ + { "abs", ctx_mpd_qabs, METH_O, doc_ctx_abs }, + { "exp", ctx_mpd_qexp, METH_O, doc_ctx_exp }, + { "ln", ctx_mpd_qln, METH_O, doc_ctx_ln }, + { "log10", ctx_mpd_qlog10, METH_O, doc_ctx_log10 }, + { "minus", ctx_mpd_qminus, METH_O, doc_ctx_minus }, + { "next_minus", ctx_mpd_qnext_minus, METH_O, doc_ctx_next_minus }, + { "next_plus", ctx_mpd_qnext_plus, METH_O, doc_ctx_next_plus }, + { "normalize", ctx_mpd_qreduce, METH_O, doc_ctx_normalize }, + { "plus", ctx_mpd_qplus, METH_O, doc_ctx_plus }, + { "to_integral", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral }, + { "to_integral_exact", ctx_mpd_qround_to_intx, METH_O, doc_ctx_to_integral_exact }, + { "to_integral_value", ctx_mpd_qround_to_int, METH_O, doc_ctx_to_integral_value }, + { "sqrt", ctx_mpd_qsqrt, METH_O, doc_ctx_sqrt }, + + /* Binary arithmetic functions */ + { "add", ctx_mpd_qadd, METH_VARARGS, doc_ctx_add }, + { "compare", ctx_mpd_qcompare, METH_VARARGS, doc_ctx_compare }, + { "compare_signal", ctx_mpd_qcompare_signal, METH_VARARGS, doc_ctx_compare_signal }, + { "divide", ctx_mpd_qdiv, METH_VARARGS, doc_ctx_divide }, + { "divide_int", ctx_mpd_qdivint, METH_VARARGS, doc_ctx_divide_int }, + { "divmod", ctx_mpd_qdivmod, METH_VARARGS, doc_ctx_divmod }, + { "max", ctx_mpd_qmax, METH_VARARGS, doc_ctx_max }, + { "max_mag", ctx_mpd_qmax_mag, METH_VARARGS, doc_ctx_max_mag }, + { "min", ctx_mpd_qmin, METH_VARARGS, doc_ctx_min }, + { "min_mag", ctx_mpd_qmin_mag, METH_VARARGS, doc_ctx_min_mag }, + { "multiply", ctx_mpd_qmul, METH_VARARGS, doc_ctx_multiply }, + { "next_toward", ctx_mpd_qnext_toward, METH_VARARGS, doc_ctx_next_toward }, + { "quantize", ctx_mpd_qquantize, METH_VARARGS, doc_ctx_quantize }, + { "remainder", ctx_mpd_qrem, METH_VARARGS, doc_ctx_remainder }, + { "remainder_near", ctx_mpd_qrem_near, METH_VARARGS, doc_ctx_remainder_near }, + { "subtract", ctx_mpd_qsub, METH_VARARGS, doc_ctx_subtract }, + + /* Binary or ternary arithmetic functions */ { "power", (PyCFunction)(void(*)(void))ctx_mpd_qpow, METH_VARARGS|METH_KEYWORDS, doc_ctx_power }, - - /* Ternary arithmetic functions */ - { "fma", ctx_mpd_qfma, METH_VARARGS, doc_ctx_fma }, - - /* No argument */ - { "Etiny", context_getetiny, METH_NOARGS, doc_ctx_Etiny }, - { "Etop", context_getetop, METH_NOARGS, doc_ctx_Etop }, - { "radix", ctx_mpd_radix, METH_NOARGS, doc_ctx_radix }, - - /* Boolean functions */ - { "is_canonical", ctx_iscanonical, METH_O, doc_ctx_is_canonical }, - { "is_finite", ctx_mpd_isfinite, METH_O, doc_ctx_is_finite }, - { "is_infinite", ctx_mpd_isinfinite, METH_O, doc_ctx_is_infinite }, - { "is_nan", ctx_mpd_isnan, METH_O, doc_ctx_is_nan }, - { "is_normal", ctx_mpd_isnormal, METH_O, doc_ctx_is_normal }, - { "is_qnan", ctx_mpd_isqnan, METH_O, doc_ctx_is_qnan }, - { "is_signed", ctx_mpd_issigned, METH_O, doc_ctx_is_signed }, - { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, - { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, - { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, - - /* Functions with a single decimal argument */ - { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ -#ifdef EXTRA_FUNCTIONALITY - { "apply", PyDecContext_Apply, METH_O, doc_ctx_apply }, -#endif - { "canonical", ctx_canonical, METH_O, doc_ctx_canonical }, - { "copy_abs", ctx_mpd_qcopy_abs, METH_O, doc_ctx_copy_abs }, - { "copy_decimal", ctx_copy_decimal, METH_O, doc_ctx_copy_decimal }, - { "copy_negate", ctx_mpd_qcopy_negate, METH_O, doc_ctx_copy_negate }, - { "logb", ctx_mpd_qlogb, METH_O, doc_ctx_logb }, - { "logical_invert", ctx_mpd_qinvert, METH_O, doc_ctx_logical_invert }, - { "number_class", ctx_mpd_class, METH_O, doc_ctx_number_class }, - { "to_sci_string", ctx_mpd_to_sci, METH_O, doc_ctx_to_sci_string }, - { "to_eng_string", ctx_mpd_to_eng, METH_O, doc_ctx_to_eng_string }, - - /* Functions with two decimal arguments */ - { "compare_total", ctx_mpd_compare_total, METH_VARARGS, doc_ctx_compare_total }, - { "compare_total_mag", ctx_mpd_compare_total_mag, METH_VARARGS, doc_ctx_compare_total_mag }, - { "copy_sign", ctx_mpd_qcopy_sign, METH_VARARGS, doc_ctx_copy_sign }, - { "logical_and", ctx_mpd_qand, METH_VARARGS, doc_ctx_logical_and }, - { "logical_or", ctx_mpd_qor, METH_VARARGS, doc_ctx_logical_or }, - { "logical_xor", ctx_mpd_qxor, METH_VARARGS, doc_ctx_logical_xor }, - { "rotate", ctx_mpd_qrotate, METH_VARARGS, doc_ctx_rotate }, - { "same_quantum", ctx_mpd_same_quantum, METH_VARARGS, doc_ctx_same_quantum }, - { "scaleb", ctx_mpd_qscaleb, METH_VARARGS, doc_ctx_scaleb }, - { "shift", ctx_mpd_qshift, METH_VARARGS, doc_ctx_shift }, - - /* Set context values */ - { "clear_flags", context_clear_flags, METH_NOARGS, doc_ctx_clear_flags }, - { "clear_traps", context_clear_traps, METH_NOARGS, doc_ctx_clear_traps }, - -#ifdef CONFIG_32 - /* Unsafe set functions with relaxed range checks */ - { "_unsafe_setprec", context_unsafe_setprec, METH_O, NULL }, - { "_unsafe_setemin", context_unsafe_setemin, METH_O, NULL }, - { "_unsafe_setemax", context_unsafe_setemax, METH_O, NULL }, -#endif - - /* Miscellaneous */ - { "__copy__", (PyCFunction)context_copy, METH_NOARGS, NULL }, - { "__reduce__", context_reduce, METH_NOARGS, NULL }, - { "copy", (PyCFunction)context_copy, METH_NOARGS, doc_ctx_copy }, - { "create_decimal", ctx_create_decimal, METH_VARARGS, doc_ctx_create_decimal }, - { "create_decimal_from_float", ctx_from_float, METH_O, doc_ctx_create_decimal_from_float }, - - { NULL, NULL, 1 } -}; - -static PyTypeObject PyDecContext_Type = -{ - PyVarObject_HEAD_INIT(NULL, 0) - "decimal.Context", /* tp_name */ - sizeof(PyDecContextObject), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor) context_dealloc, /* tp_dealloc */ + + /* Ternary arithmetic functions */ + { "fma", ctx_mpd_qfma, METH_VARARGS, doc_ctx_fma }, + + /* No argument */ + { "Etiny", context_getetiny, METH_NOARGS, doc_ctx_Etiny }, + { "Etop", context_getetop, METH_NOARGS, doc_ctx_Etop }, + { "radix", ctx_mpd_radix, METH_NOARGS, doc_ctx_radix }, + + /* Boolean functions */ + { "is_canonical", ctx_iscanonical, METH_O, doc_ctx_is_canonical }, + { "is_finite", ctx_mpd_isfinite, METH_O, doc_ctx_is_finite }, + { "is_infinite", ctx_mpd_isinfinite, METH_O, doc_ctx_is_infinite }, + { "is_nan", ctx_mpd_isnan, METH_O, doc_ctx_is_nan }, + { "is_normal", ctx_mpd_isnormal, METH_O, doc_ctx_is_normal }, + { "is_qnan", ctx_mpd_isqnan, METH_O, doc_ctx_is_qnan }, + { "is_signed", ctx_mpd_issigned, METH_O, doc_ctx_is_signed }, + { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, + { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, + { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, + + /* Functions with a single decimal argument */ + { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ +#ifdef EXTRA_FUNCTIONALITY + { "apply", PyDecContext_Apply, METH_O, doc_ctx_apply }, +#endif + { "canonical", ctx_canonical, METH_O, doc_ctx_canonical }, + { "copy_abs", ctx_mpd_qcopy_abs, METH_O, doc_ctx_copy_abs }, + { "copy_decimal", ctx_copy_decimal, METH_O, doc_ctx_copy_decimal }, + { "copy_negate", ctx_mpd_qcopy_negate, METH_O, doc_ctx_copy_negate }, + { "logb", ctx_mpd_qlogb, METH_O, doc_ctx_logb }, + { "logical_invert", ctx_mpd_qinvert, METH_O, doc_ctx_logical_invert }, + { "number_class", ctx_mpd_class, METH_O, doc_ctx_number_class }, + { "to_sci_string", ctx_mpd_to_sci, METH_O, doc_ctx_to_sci_string }, + { "to_eng_string", ctx_mpd_to_eng, METH_O, doc_ctx_to_eng_string }, + + /* Functions with two decimal arguments */ + { "compare_total", ctx_mpd_compare_total, METH_VARARGS, doc_ctx_compare_total }, + { "compare_total_mag", ctx_mpd_compare_total_mag, METH_VARARGS, doc_ctx_compare_total_mag }, + { "copy_sign", ctx_mpd_qcopy_sign, METH_VARARGS, doc_ctx_copy_sign }, + { "logical_and", ctx_mpd_qand, METH_VARARGS, doc_ctx_logical_and }, + { "logical_or", ctx_mpd_qor, METH_VARARGS, doc_ctx_logical_or }, + { "logical_xor", ctx_mpd_qxor, METH_VARARGS, doc_ctx_logical_xor }, + { "rotate", ctx_mpd_qrotate, METH_VARARGS, doc_ctx_rotate }, + { "same_quantum", ctx_mpd_same_quantum, METH_VARARGS, doc_ctx_same_quantum }, + { "scaleb", ctx_mpd_qscaleb, METH_VARARGS, doc_ctx_scaleb }, + { "shift", ctx_mpd_qshift, METH_VARARGS, doc_ctx_shift }, + + /* Set context values */ + { "clear_flags", context_clear_flags, METH_NOARGS, doc_ctx_clear_flags }, + { "clear_traps", context_clear_traps, METH_NOARGS, doc_ctx_clear_traps }, + +#ifdef CONFIG_32 + /* Unsafe set functions with relaxed range checks */ + { "_unsafe_setprec", context_unsafe_setprec, METH_O, NULL }, + { "_unsafe_setemin", context_unsafe_setemin, METH_O, NULL }, + { "_unsafe_setemax", context_unsafe_setemax, METH_O, NULL }, +#endif + + /* Miscellaneous */ + { "__copy__", (PyCFunction)context_copy, METH_NOARGS, NULL }, + { "__reduce__", context_reduce, METH_NOARGS, NULL }, + { "copy", (PyCFunction)context_copy, METH_NOARGS, doc_ctx_copy }, + { "create_decimal", ctx_create_decimal, METH_VARARGS, doc_ctx_create_decimal }, + { "create_decimal_from_float", ctx_from_float, METH_O, doc_ctx_create_decimal_from_float }, + + { NULL, NULL, 1 } +}; + +static PyTypeObject PyDecContext_Type = +{ + PyVarObject_HEAD_INIT(NULL, 0) + "decimal.Context", /* tp_name */ + sizeof(PyDecContextObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor) context_dealloc, /* tp_dealloc */ 0, /* tp_vectorcall_offset */ - (getattrfunc) 0, /* tp_getattr */ - (setattrfunc) 0, /* tp_setattr */ + (getattrfunc) 0, /* tp_getattr */ + (setattrfunc) 0, /* tp_setattr */ 0, /* tp_as_async */ - (reprfunc) context_repr, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - (hashfunc) 0, /* tp_hash */ - 0, /* tp_call */ + (reprfunc) context_repr, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + (hashfunc) 0, /* tp_hash */ + 0, /* tp_call */ 0, /* tp_str */ - (getattrofunc) context_getattr, /* tp_getattro */ - (setattrofunc) context_setattr, /* tp_setattro */ - (PyBufferProcs *) 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ - doc_context, /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - context_methods, /* tp_methods */ - 0, /* tp_members */ - context_getsets, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - context_init, /* tp_init */ - 0, /* tp_alloc */ - context_new, /* tp_new */ - PyObject_Del, /* tp_free */ -}; - - -static PyMethodDef _decimal_methods [] = -{ - { "getcontext", (PyCFunction)PyDec_GetCurrentContext, METH_NOARGS, doc_getcontext}, - { "setcontext", (PyCFunction)PyDec_SetCurrentContext, METH_O, doc_setcontext}, + (getattrofunc) context_getattr, /* tp_getattro */ + (setattrofunc) context_setattr, /* tp_setattro */ + (PyBufferProcs *) 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ + doc_context, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + context_methods, /* tp_methods */ + 0, /* tp_members */ + context_getsets, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + context_init, /* tp_init */ + 0, /* tp_alloc */ + context_new, /* tp_new */ + PyObject_Del, /* tp_free */ +}; + + +static PyMethodDef _decimal_methods [] = +{ + { "getcontext", (PyCFunction)PyDec_GetCurrentContext, METH_NOARGS, doc_getcontext}, + { "setcontext", (PyCFunction)PyDec_SetCurrentContext, METH_O, doc_setcontext}, { "localcontext", (PyCFunction)(void(*)(void))ctxmanager_new, METH_VARARGS|METH_KEYWORDS, doc_localcontext}, -#ifdef EXTRA_FUNCTIONALITY - { "IEEEContext", (PyCFunction)ieee_context, METH_O, doc_ieee_context}, -#endif - { NULL, NULL, 1, NULL } -}; - -static struct PyModuleDef _decimal_module = { - PyModuleDef_HEAD_INIT, - "decimal", - doc__decimal, - -1, - _decimal_methods, - NULL, - NULL, - NULL, - NULL -}; - -struct ssize_constmap { const char *name; mpd_ssize_t val; }; -static struct ssize_constmap ssize_constants [] = { - {"MAX_PREC", MPD_MAX_PREC}, - {"MAX_EMAX", MPD_MAX_EMAX}, - {"MIN_EMIN", MPD_MIN_EMIN}, - {"MIN_ETINY", MPD_MIN_ETINY}, - {NULL} -}; - -struct int_constmap { const char *name; int val; }; -static struct int_constmap int_constants [] = { - /* int constants */ -#ifdef EXTRA_FUNCTIONALITY - {"DECIMAL32", MPD_DECIMAL32}, - {"DECIMAL64", MPD_DECIMAL64}, - {"DECIMAL128", MPD_DECIMAL128}, - {"IEEE_CONTEXT_MAX_BITS", MPD_IEEE_CONTEXT_MAX_BITS}, - /* int condition flags */ - {"DecClamped", MPD_Clamped}, - {"DecConversionSyntax", MPD_Conversion_syntax}, - {"DecDivisionByZero", MPD_Division_by_zero}, - {"DecDivisionImpossible", MPD_Division_impossible}, - {"DecDivisionUndefined", MPD_Division_undefined}, - {"DecFpuError", MPD_Fpu_error}, - {"DecInexact", MPD_Inexact}, - {"DecInvalidContext", MPD_Invalid_context}, - {"DecInvalidOperation", MPD_Invalid_operation}, - {"DecIEEEInvalidOperation", MPD_IEEE_Invalid_operation}, - {"DecMallocError", MPD_Malloc_error}, - {"DecFloatOperation", MPD_Float_operation}, - {"DecOverflow", MPD_Overflow}, - {"DecRounded", MPD_Rounded}, - {"DecSubnormal", MPD_Subnormal}, - {"DecUnderflow", MPD_Underflow}, - {"DecErrors", MPD_Errors}, - {"DecTraps", MPD_Traps}, -#endif - {NULL} -}; - - -#define CHECK_INT(expr) \ - do { if ((expr) < 0) goto error; } while (0) -#define ASSIGN_PTR(result, expr) \ - do { result = (expr); if (result == NULL) goto error; } while (0) -#define CHECK_PTR(expr) \ - do { if ((expr) == NULL) goto error; } while (0) - - -static PyCFunction -cfunc_noargs(PyTypeObject *t, const char *name) -{ - struct PyMethodDef *m; - - if (t->tp_methods == NULL) { - goto error; - } - - for (m = t->tp_methods; m->ml_name != NULL; m++) { - if (strcmp(name, m->ml_name) == 0) { - if (!(m->ml_flags & METH_NOARGS)) { - goto error; - } - return m->ml_meth; - } - } - -error: - PyErr_Format(PyExc_RuntimeError, - "internal error: could not find method %s", name); - return NULL; -} - - -PyMODINIT_FUNC -PyInit__decimal(void) -{ - PyObject *m = NULL; - PyObject *numbers = NULL; - PyObject *Number = NULL; - PyObject *collections = NULL; - PyObject *collections_abc = NULL; - PyObject *MutableMapping = NULL; - PyObject *obj = NULL; - DecCondMap *cm; - struct ssize_constmap *ssize_cm; - struct int_constmap *int_cm; - int i; - - - /* Init libmpdec */ - mpd_traphandler = dec_traphandler; - mpd_mallocfunc = PyMem_Malloc; - mpd_reallocfunc = PyMem_Realloc; - mpd_callocfunc = mpd_callocfunc_em; - mpd_free = PyMem_Free; - mpd_setminalloc(_Py_DEC_MINALLOC); - - - /* Init external C-API functions */ - _py_long_multiply = PyLong_Type.tp_as_number->nb_multiply; - _py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide; - _py_long_power = PyLong_Type.tp_as_number->nb_power; - _py_float_abs = PyFloat_Type.tp_as_number->nb_absolute; - ASSIGN_PTR(_py_float_as_integer_ratio, cfunc_noargs(&PyFloat_Type, - "as_integer_ratio")); - ASSIGN_PTR(_py_long_bit_length, cfunc_noargs(&PyLong_Type, "bit_length")); - - - /* Init types */ - PyDec_Type.tp_base = &PyBaseObject_Type; - PyDecContext_Type.tp_base = &PyBaseObject_Type; - PyDecContextManager_Type.tp_base = &PyBaseObject_Type; - PyDecSignalDictMixin_Type.tp_base = &PyBaseObject_Type; - - CHECK_INT(PyType_Ready(&PyDec_Type)); - CHECK_INT(PyType_Ready(&PyDecContext_Type)); - CHECK_INT(PyType_Ready(&PyDecSignalDictMixin_Type)); - CHECK_INT(PyType_Ready(&PyDecContextManager_Type)); - - ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); - CHECK_INT(PyDict_SetItemString(PyDec_Type.tp_dict, "__module__", obj)); - CHECK_INT(PyDict_SetItemString(PyDecContext_Type.tp_dict, - "__module__", obj)); - Py_CLEAR(obj); - - - /* Numeric abstract base classes */ - ASSIGN_PTR(numbers, PyImport_ImportModule("numbers")); - ASSIGN_PTR(Number, PyObject_GetAttrString(numbers, "Number")); - /* Register Decimal with the Number abstract base class */ - ASSIGN_PTR(obj, PyObject_CallMethod(Number, "register", "(O)", - (PyObject *)&PyDec_Type)); - Py_CLEAR(obj); - /* Rational is a global variable used for fraction comparisons. */ - ASSIGN_PTR(Rational, PyObject_GetAttrString(numbers, "Rational")); - /* Done with numbers, Number */ - Py_CLEAR(numbers); - Py_CLEAR(Number); - - /* DecimalTuple */ - ASSIGN_PTR(collections, PyImport_ImportModule("collections")); - ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections, - "namedtuple", "(ss)", "DecimalTuple", - "sign digits exponent")); - - ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); - CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj)); - Py_CLEAR(obj); - - /* MutableMapping */ - ASSIGN_PTR(collections_abc, PyImport_ImportModule("collections.abc")); - ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections_abc, - "MutableMapping")); - /* Create SignalDict type */ - ASSIGN_PTR(PyDecSignalDict_Type, - (PyTypeObject *)PyObject_CallFunction( - (PyObject *)&PyType_Type, "s(OO){}", - "SignalDict", &PyDecSignalDictMixin_Type, - MutableMapping)); - - /* Done with collections, MutableMapping */ - Py_CLEAR(collections); - Py_CLEAR(collections_abc); - Py_CLEAR(MutableMapping); - - - /* Create the module */ - ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); - - - /* Add types to the module */ - Py_INCREF(&PyDec_Type); - CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type)); - Py_INCREF(&PyDecContext_Type); - CHECK_INT(PyModule_AddObject(m, "Context", - (PyObject *)&PyDecContext_Type)); - Py_INCREF(DecimalTuple); - CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple)); - - - /* Create top level exception */ - ASSIGN_PTR(DecimalException, PyErr_NewException( - "decimal.DecimalException", - PyExc_ArithmeticError, NULL)); - Py_INCREF(DecimalException); - CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException)); - - /* Create signal tuple */ - ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN)); - - /* Add exceptions that correspond to IEEE signals */ - for (i = SIGNAL_MAP_LEN-1; i >= 0; i--) { - PyObject *base; - - cm = signal_map + i; - - switch (cm->flag) { - case MPD_Float_operation: - base = PyTuple_Pack(2, DecimalException, PyExc_TypeError); - break; - case MPD_Division_by_zero: - base = PyTuple_Pack(2, DecimalException, PyExc_ZeroDivisionError); - break; - case MPD_Overflow: - base = PyTuple_Pack(2, signal_map[INEXACT].ex, - signal_map[ROUNDED].ex); - break; - case MPD_Underflow: - base = PyTuple_Pack(3, signal_map[INEXACT].ex, - signal_map[ROUNDED].ex, - signal_map[SUBNORMAL].ex); - break; - default: - base = PyTuple_Pack(1, DecimalException); - break; - } - - if (base == NULL) { - goto error; /* GCOV_NOT_REACHED */ - } - - ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); - Py_DECREF(base); - - /* add to module */ - Py_INCREF(cm->ex); - CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); - - /* add to signal tuple */ - Py_INCREF(cm->ex); - PyTuple_SET_ITEM(SignalTuple, i, cm->ex); - } - - /* - * Unfortunately, InvalidOperation is a signal that comprises - * several conditions, including InvalidOperation! Naming the - * signal IEEEInvalidOperation would prevent the confusion. - */ - cond_map[0].ex = signal_map[0].ex; - - /* Add remaining exceptions, inherit from InvalidOperation */ - for (cm = cond_map+1; cm->name != NULL; cm++) { - PyObject *base; - if (cm->flag == MPD_Division_undefined) { - base = PyTuple_Pack(2, signal_map[0].ex, PyExc_ZeroDivisionError); - } - else { - base = PyTuple_Pack(1, signal_map[0].ex); - } - if (base == NULL) { - goto error; /* GCOV_NOT_REACHED */ - } - - ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); - Py_DECREF(base); - - Py_INCREF(cm->ex); - CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); - } - - - /* Init default context template first */ - ASSIGN_PTR(default_context_template, - PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); - Py_INCREF(default_context_template); - CHECK_INT(PyModule_AddObject(m, "DefaultContext", - default_context_template)); - +#ifdef EXTRA_FUNCTIONALITY + { "IEEEContext", (PyCFunction)ieee_context, METH_O, doc_ieee_context}, +#endif + { NULL, NULL, 1, NULL } +}; + +static struct PyModuleDef _decimal_module = { + PyModuleDef_HEAD_INIT, + "decimal", + doc__decimal, + -1, + _decimal_methods, + NULL, + NULL, + NULL, + NULL +}; + +struct ssize_constmap { const char *name; mpd_ssize_t val; }; +static struct ssize_constmap ssize_constants [] = { + {"MAX_PREC", MPD_MAX_PREC}, + {"MAX_EMAX", MPD_MAX_EMAX}, + {"MIN_EMIN", MPD_MIN_EMIN}, + {"MIN_ETINY", MPD_MIN_ETINY}, + {NULL} +}; + +struct int_constmap { const char *name; int val; }; +static struct int_constmap int_constants [] = { + /* int constants */ +#ifdef EXTRA_FUNCTIONALITY + {"DECIMAL32", MPD_DECIMAL32}, + {"DECIMAL64", MPD_DECIMAL64}, + {"DECIMAL128", MPD_DECIMAL128}, + {"IEEE_CONTEXT_MAX_BITS", MPD_IEEE_CONTEXT_MAX_BITS}, + /* int condition flags */ + {"DecClamped", MPD_Clamped}, + {"DecConversionSyntax", MPD_Conversion_syntax}, + {"DecDivisionByZero", MPD_Division_by_zero}, + {"DecDivisionImpossible", MPD_Division_impossible}, + {"DecDivisionUndefined", MPD_Division_undefined}, + {"DecFpuError", MPD_Fpu_error}, + {"DecInexact", MPD_Inexact}, + {"DecInvalidContext", MPD_Invalid_context}, + {"DecInvalidOperation", MPD_Invalid_operation}, + {"DecIEEEInvalidOperation", MPD_IEEE_Invalid_operation}, + {"DecMallocError", MPD_Malloc_error}, + {"DecFloatOperation", MPD_Float_operation}, + {"DecOverflow", MPD_Overflow}, + {"DecRounded", MPD_Rounded}, + {"DecSubnormal", MPD_Subnormal}, + {"DecUnderflow", MPD_Underflow}, + {"DecErrors", MPD_Errors}, + {"DecTraps", MPD_Traps}, +#endif + {NULL} +}; + + +#define CHECK_INT(expr) \ + do { if ((expr) < 0) goto error; } while (0) +#define ASSIGN_PTR(result, expr) \ + do { result = (expr); if (result == NULL) goto error; } while (0) +#define CHECK_PTR(expr) \ + do { if ((expr) == NULL) goto error; } while (0) + + +static PyCFunction +cfunc_noargs(PyTypeObject *t, const char *name) +{ + struct PyMethodDef *m; + + if (t->tp_methods == NULL) { + goto error; + } + + for (m = t->tp_methods; m->ml_name != NULL; m++) { + if (strcmp(name, m->ml_name) == 0) { + if (!(m->ml_flags & METH_NOARGS)) { + goto error; + } + return m->ml_meth; + } + } + +error: + PyErr_Format(PyExc_RuntimeError, + "internal error: could not find method %s", name); + return NULL; +} + + +PyMODINIT_FUNC +PyInit__decimal(void) +{ + PyObject *m = NULL; + PyObject *numbers = NULL; + PyObject *Number = NULL; + PyObject *collections = NULL; + PyObject *collections_abc = NULL; + PyObject *MutableMapping = NULL; + PyObject *obj = NULL; + DecCondMap *cm; + struct ssize_constmap *ssize_cm; + struct int_constmap *int_cm; + int i; + + + /* Init libmpdec */ + mpd_traphandler = dec_traphandler; + mpd_mallocfunc = PyMem_Malloc; + mpd_reallocfunc = PyMem_Realloc; + mpd_callocfunc = mpd_callocfunc_em; + mpd_free = PyMem_Free; + mpd_setminalloc(_Py_DEC_MINALLOC); + + + /* Init external C-API functions */ + _py_long_multiply = PyLong_Type.tp_as_number->nb_multiply; + _py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide; + _py_long_power = PyLong_Type.tp_as_number->nb_power; + _py_float_abs = PyFloat_Type.tp_as_number->nb_absolute; + ASSIGN_PTR(_py_float_as_integer_ratio, cfunc_noargs(&PyFloat_Type, + "as_integer_ratio")); + ASSIGN_PTR(_py_long_bit_length, cfunc_noargs(&PyLong_Type, "bit_length")); + + + /* Init types */ + PyDec_Type.tp_base = &PyBaseObject_Type; + PyDecContext_Type.tp_base = &PyBaseObject_Type; + PyDecContextManager_Type.tp_base = &PyBaseObject_Type; + PyDecSignalDictMixin_Type.tp_base = &PyBaseObject_Type; + + CHECK_INT(PyType_Ready(&PyDec_Type)); + CHECK_INT(PyType_Ready(&PyDecContext_Type)); + CHECK_INT(PyType_Ready(&PyDecSignalDictMixin_Type)); + CHECK_INT(PyType_Ready(&PyDecContextManager_Type)); + + ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); + CHECK_INT(PyDict_SetItemString(PyDec_Type.tp_dict, "__module__", obj)); + CHECK_INT(PyDict_SetItemString(PyDecContext_Type.tp_dict, + "__module__", obj)); + Py_CLEAR(obj); + + + /* Numeric abstract base classes */ + ASSIGN_PTR(numbers, PyImport_ImportModule("numbers")); + ASSIGN_PTR(Number, PyObject_GetAttrString(numbers, "Number")); + /* Register Decimal with the Number abstract base class */ + ASSIGN_PTR(obj, PyObject_CallMethod(Number, "register", "(O)", + (PyObject *)&PyDec_Type)); + Py_CLEAR(obj); + /* Rational is a global variable used for fraction comparisons. */ + ASSIGN_PTR(Rational, PyObject_GetAttrString(numbers, "Rational")); + /* Done with numbers, Number */ + Py_CLEAR(numbers); + Py_CLEAR(Number); + + /* DecimalTuple */ + ASSIGN_PTR(collections, PyImport_ImportModule("collections")); + ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections, + "namedtuple", "(ss)", "DecimalTuple", + "sign digits exponent")); + + ASSIGN_PTR(obj, PyUnicode_FromString("decimal")); + CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj)); + Py_CLEAR(obj); + + /* MutableMapping */ + ASSIGN_PTR(collections_abc, PyImport_ImportModule("collections.abc")); + ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections_abc, + "MutableMapping")); + /* Create SignalDict type */ + ASSIGN_PTR(PyDecSignalDict_Type, + (PyTypeObject *)PyObject_CallFunction( + (PyObject *)&PyType_Type, "s(OO){}", + "SignalDict", &PyDecSignalDictMixin_Type, + MutableMapping)); + + /* Done with collections, MutableMapping */ + Py_CLEAR(collections); + Py_CLEAR(collections_abc); + Py_CLEAR(MutableMapping); + + + /* Create the module */ + ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); + + + /* Add types to the module */ + Py_INCREF(&PyDec_Type); + CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type)); + Py_INCREF(&PyDecContext_Type); + CHECK_INT(PyModule_AddObject(m, "Context", + (PyObject *)&PyDecContext_Type)); + Py_INCREF(DecimalTuple); + CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple)); + + + /* Create top level exception */ + ASSIGN_PTR(DecimalException, PyErr_NewException( + "decimal.DecimalException", + PyExc_ArithmeticError, NULL)); + Py_INCREF(DecimalException); + CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException)); + + /* Create signal tuple */ + ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN)); + + /* Add exceptions that correspond to IEEE signals */ + for (i = SIGNAL_MAP_LEN-1; i >= 0; i--) { + PyObject *base; + + cm = signal_map + i; + + switch (cm->flag) { + case MPD_Float_operation: + base = PyTuple_Pack(2, DecimalException, PyExc_TypeError); + break; + case MPD_Division_by_zero: + base = PyTuple_Pack(2, DecimalException, PyExc_ZeroDivisionError); + break; + case MPD_Overflow: + base = PyTuple_Pack(2, signal_map[INEXACT].ex, + signal_map[ROUNDED].ex); + break; + case MPD_Underflow: + base = PyTuple_Pack(3, signal_map[INEXACT].ex, + signal_map[ROUNDED].ex, + signal_map[SUBNORMAL].ex); + break; + default: + base = PyTuple_Pack(1, DecimalException); + break; + } + + if (base == NULL) { + goto error; /* GCOV_NOT_REACHED */ + } + + ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); + Py_DECREF(base); + + /* add to module */ + Py_INCREF(cm->ex); + CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); + + /* add to signal tuple */ + Py_INCREF(cm->ex); + PyTuple_SET_ITEM(SignalTuple, i, cm->ex); + } + + /* + * Unfortunately, InvalidOperation is a signal that comprises + * several conditions, including InvalidOperation! Naming the + * signal IEEEInvalidOperation would prevent the confusion. + */ + cond_map[0].ex = signal_map[0].ex; + + /* Add remaining exceptions, inherit from InvalidOperation */ + for (cm = cond_map+1; cm->name != NULL; cm++) { + PyObject *base; + if (cm->flag == MPD_Division_undefined) { + base = PyTuple_Pack(2, signal_map[0].ex, PyExc_ZeroDivisionError); + } + else { + base = PyTuple_Pack(1, signal_map[0].ex); + } + if (base == NULL) { + goto error; /* GCOV_NOT_REACHED */ + } + + ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); + Py_DECREF(base); + + Py_INCREF(cm->ex); + CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex)); + } + + + /* Init default context template first */ + ASSIGN_PTR(default_context_template, + PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); + Py_INCREF(default_context_template); + CHECK_INT(PyModule_AddObject(m, "DefaultContext", + default_context_template)); + #ifndef WITH_DECIMAL_CONTEXTVAR ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__")); Py_INCREF(Py_False); CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_False)); #else ASSIGN_PTR(current_context_var, PyContextVar_New("decimal_context", NULL)); - Py_INCREF(Py_True); + Py_INCREF(Py_True); CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_True)); #endif Py_INCREF(Py_True); - CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True)); - - /* Init basic context template */ - ASSIGN_PTR(basic_context_template, - PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); - init_basic_context(basic_context_template); - Py_INCREF(basic_context_template); - CHECK_INT(PyModule_AddObject(m, "BasicContext", - basic_context_template)); - - /* Init extended context template */ - ASSIGN_PTR(extended_context_template, - PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); - init_extended_context(extended_context_template); - Py_INCREF(extended_context_template); - CHECK_INT(PyModule_AddObject(m, "ExtendedContext", - extended_context_template)); - - - /* Init mpd_ssize_t constants */ - for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) { - ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val)); - CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj)); - obj = NULL; - } - - /* Init int constants */ - for (int_cm = int_constants; int_cm->name != NULL; int_cm++) { - CHECK_INT(PyModule_AddIntConstant(m, int_cm->name, - int_cm->val)); - } - - /* Init string constants */ - for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { - ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i])); - Py_INCREF(round_map[i]); - CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], round_map[i])); - } - - /* Add specification version number */ - CHECK_INT(PyModule_AddStringConstant(m, "__version__", "1.70")); - CHECK_INT(PyModule_AddStringConstant(m, "__libmpdec_version__", mpd_version())); - - - return m; - - -error: - Py_CLEAR(obj); /* GCOV_NOT_REACHED */ - Py_CLEAR(numbers); /* GCOV_NOT_REACHED */ - Py_CLEAR(Number); /* GCOV_NOT_REACHED */ - Py_CLEAR(Rational); /* GCOV_NOT_REACHED */ - Py_CLEAR(collections); /* GCOV_NOT_REACHED */ - Py_CLEAR(collections_abc); /* GCOV_NOT_REACHED */ - Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */ - Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */ - Py_CLEAR(DecimalTuple); /* GCOV_NOT_REACHED */ - Py_CLEAR(default_context_template); /* GCOV_NOT_REACHED */ + CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True)); + + /* Init basic context template */ + ASSIGN_PTR(basic_context_template, + PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); + init_basic_context(basic_context_template); + Py_INCREF(basic_context_template); + CHECK_INT(PyModule_AddObject(m, "BasicContext", + basic_context_template)); + + /* Init extended context template */ + ASSIGN_PTR(extended_context_template, + PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); + init_extended_context(extended_context_template); + Py_INCREF(extended_context_template); + CHECK_INT(PyModule_AddObject(m, "ExtendedContext", + extended_context_template)); + + + /* Init mpd_ssize_t constants */ + for (ssize_cm = ssize_constants; ssize_cm->name != NULL; ssize_cm++) { + ASSIGN_PTR(obj, PyLong_FromSsize_t(ssize_cm->val)); + CHECK_INT(PyModule_AddObject(m, ssize_cm->name, obj)); + obj = NULL; + } + + /* Init int constants */ + for (int_cm = int_constants; int_cm->name != NULL; int_cm++) { + CHECK_INT(PyModule_AddIntConstant(m, int_cm->name, + int_cm->val)); + } + + /* Init string constants */ + for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { + ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i])); + Py_INCREF(round_map[i]); + CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], round_map[i])); + } + + /* Add specification version number */ + CHECK_INT(PyModule_AddStringConstant(m, "__version__", "1.70")); + CHECK_INT(PyModule_AddStringConstant(m, "__libmpdec_version__", mpd_version())); + + + return m; + + +error: + Py_CLEAR(obj); /* GCOV_NOT_REACHED */ + Py_CLEAR(numbers); /* GCOV_NOT_REACHED */ + Py_CLEAR(Number); /* GCOV_NOT_REACHED */ + Py_CLEAR(Rational); /* GCOV_NOT_REACHED */ + Py_CLEAR(collections); /* GCOV_NOT_REACHED */ + Py_CLEAR(collections_abc); /* GCOV_NOT_REACHED */ + Py_CLEAR(MutableMapping); /* GCOV_NOT_REACHED */ + Py_CLEAR(SignalTuple); /* GCOV_NOT_REACHED */ + Py_CLEAR(DecimalTuple); /* GCOV_NOT_REACHED */ + Py_CLEAR(default_context_template); /* GCOV_NOT_REACHED */ #ifndef WITH_DECIMAL_CONTEXTVAR Py_CLEAR(tls_context_key); /* GCOV_NOT_REACHED */ #else Py_CLEAR(current_context_var); /* GCOV_NOT_REACHED */ #endif - Py_CLEAR(basic_context_template); /* GCOV_NOT_REACHED */ - Py_CLEAR(extended_context_template); /* GCOV_NOT_REACHED */ - Py_CLEAR(m); /* GCOV_NOT_REACHED */ - - return NULL; /* GCOV_NOT_REACHED */ -} - - + Py_CLEAR(basic_context_template); /* GCOV_NOT_REACHED */ + Py_CLEAR(extended_context_template); /* GCOV_NOT_REACHED */ + Py_CLEAR(m); /* GCOV_NOT_REACHED */ + + return NULL; /* GCOV_NOT_REACHED */ +} + + diff --git a/contrib/tools/python3/src/Modules/_decimal/docstrings.h b/contrib/tools/python3/src/Modules/_decimal/docstrings.h index f7fd6e79529..1bb8e3f99e9 100644 --- a/contrib/tools/python3/src/Modules/_decimal/docstrings.h +++ b/contrib/tools/python3/src/Modules/_decimal/docstrings.h @@ -1,884 +1,884 @@ -/* - * Copyright (c) 2001-2012 Python Software Foundation. All Rights Reserved. - * Modified and extended by Stefan Krah. - */ - - -#ifndef DOCSTRINGS_H -#define DOCSTRINGS_H - - -#include "pymacro.h" - - -/******************************************************************************/ -/* Module */ -/******************************************************************************/ - - -PyDoc_STRVAR(doc__decimal, -"C decimal arithmetic module"); - -PyDoc_STRVAR(doc_getcontext, -"getcontext($module, /)\n--\n\n\ -Get the current default context.\n\ -\n"); - -PyDoc_STRVAR(doc_setcontext, -"setcontext($module, context, /)\n--\n\n\ -Set a new default context.\n\ -\n"); - -PyDoc_STRVAR(doc_localcontext, -"localcontext($module, /, ctx=None)\n--\n\n\ -Return a context manager that will set the default context to a copy of ctx\n\ -on entry to the with-statement and restore the previous default context when\n\ -exiting the with-statement. If no context is specified, a copy of the current\n\ -default context is used.\n\ -\n"); - -#ifdef EXTRA_FUNCTIONALITY -PyDoc_STRVAR(doc_ieee_context, -"IEEEContext($module, bits, /)\n--\n\n\ -Return a context object initialized to the proper values for one of the\n\ -IEEE interchange formats. The argument must be a multiple of 32 and less\n\ -than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\ -DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\ -\n"); -#endif - - -/******************************************************************************/ -/* Decimal Object and Methods */ -/******************************************************************************/ - -PyDoc_STRVAR(doc_decimal, -"Decimal(value=\"0\", context=None)\n--\n\n\ -Construct a new Decimal object. 'value' can be an integer, string, tuple,\n\ -or another Decimal object. If no value is given, return Decimal('0'). The\n\ -context does not affect the conversion and is only passed to determine if\n\ -the InvalidOperation trap is active.\n\ -\n"); - -PyDoc_STRVAR(doc_adjusted, -"adjusted($self, /)\n--\n\n\ -Return the adjusted exponent of the number. Defined as exp + digits - 1.\n\ -\n"); - -PyDoc_STRVAR(doc_as_tuple, -"as_tuple($self, /)\n--\n\n\ -Return a tuple representation of the number.\n\ -\n"); - -PyDoc_STRVAR(doc_as_integer_ratio, -"as_integer_ratio($self, /)\n--\n\n\ -Decimal.as_integer_ratio() -> (int, int)\n\ -\n\ -Return a pair of integers, whose ratio is exactly equal to the original\n\ -Decimal and with a positive denominator. The ratio is in lowest terms.\n\ -Raise OverflowError on infinities and a ValueError on NaNs.\n\ -\n"); - -PyDoc_STRVAR(doc_canonical, -"canonical($self, /)\n--\n\n\ -Return the canonical encoding of the argument. Currently, the encoding\n\ -of a Decimal instance is always canonical, so this operation returns its\n\ -argument unchanged.\n\ -\n"); - -PyDoc_STRVAR(doc_compare, -"compare($self, /, other, context=None)\n--\n\n\ -Compare self to other. Return a decimal value:\n\ -\n\ - a or b is a NaN ==> Decimal('NaN')\n\ - a < b ==> Decimal('-1')\n\ - a == b ==> Decimal('0')\n\ - a > b ==> Decimal('1')\n\ -\n"); - -PyDoc_STRVAR(doc_compare_signal, -"compare_signal($self, /, other, context=None)\n--\n\n\ -Identical to compare, except that all NaNs signal.\n\ -\n"); - -PyDoc_STRVAR(doc_compare_total, -"compare_total($self, /, other, context=None)\n--\n\n\ -Compare two operands using their abstract representation rather than\n\ -their numerical value. Similar to the compare() method, but the result\n\ -gives a total ordering on Decimal instances. Two Decimal instances with\n\ -the same numeric value but different representations compare unequal\n\ -in this ordering:\n\ -\n\ - >>> Decimal('12.0').compare_total(Decimal('12'))\n\ - Decimal('-1')\n\ -\n\ -Quiet and signaling NaNs are also included in the total ordering. The result\n\ -of this function is Decimal('0') if both operands have the same representation,\n\ -Decimal('-1') if the first operand is lower in the total order than the second,\n\ -and Decimal('1') if the first operand is higher in the total order than the\n\ -second operand. See the specification for details of the total order.\n\ -\n\ -This operation is unaffected by context and is quiet: no flags are changed\n\ -and no rounding is performed. As an exception, the C version may raise\n\ -InvalidOperation if the second operand cannot be converted exactly.\n\ -\n"); - -PyDoc_STRVAR(doc_compare_total_mag, -"compare_total_mag($self, /, other, context=None)\n--\n\n\ -Compare two operands using their abstract representation rather than their\n\ -value as in compare_total(), but ignoring the sign of each operand.\n\ -\n\ -x.compare_total_mag(y) is equivalent to x.copy_abs().compare_total(y.copy_abs()).\n\ -\n\ -This operation is unaffected by context and is quiet: no flags are changed\n\ -and no rounding is performed. As an exception, the C version may raise\n\ -InvalidOperation if the second operand cannot be converted exactly.\n\ -\n"); - -PyDoc_STRVAR(doc_conjugate, -"conjugate($self, /)\n--\n\n\ -Return self.\n\ -\n"); - -PyDoc_STRVAR(doc_copy_abs, -"copy_abs($self, /)\n--\n\n\ -Return the absolute value of the argument. This operation is unaffected by\n\ -context and is quiet: no flags are changed and no rounding is performed.\n\ -\n"); - -PyDoc_STRVAR(doc_copy_negate, -"copy_negate($self, /)\n--\n\n\ -Return the negation of the argument. This operation is unaffected by context\n\ -and is quiet: no flags are changed and no rounding is performed.\n\ -\n"); - -PyDoc_STRVAR(doc_copy_sign, -"copy_sign($self, /, other, context=None)\n--\n\n\ -Return a copy of the first operand with the sign set to be the same as the\n\ -sign of the second operand. For example:\n\ -\n\ - >>> Decimal('2.3').copy_sign(Decimal('-1.5'))\n\ - Decimal('-2.3')\n\ -\n\ -This operation is unaffected by context and is quiet: no flags are changed\n\ -and no rounding is performed. As an exception, the C version may raise\n\ -InvalidOperation if the second operand cannot be converted exactly.\n\ -\n"); - -PyDoc_STRVAR(doc_exp, -"exp($self, /, context=None)\n--\n\n\ -Return the value of the (natural) exponential function e**x at the given\n\ -number. The function always uses the ROUND_HALF_EVEN mode and the result\n\ -is correctly rounded.\n\ -\n"); - -PyDoc_STRVAR(doc_from_float, -"from_float($type, f, /)\n--\n\n\ -Class method that converts a float to a decimal number, exactly.\n\ -Since 0.1 is not exactly representable in binary floating point,\n\ -Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\ -\n\ - >>> Decimal.from_float(0.1)\n\ - Decimal('0.1000000000000000055511151231257827021181583404541015625')\n\ - >>> Decimal.from_float(float('nan'))\n\ - Decimal('NaN')\n\ - >>> Decimal.from_float(float('inf'))\n\ - Decimal('Infinity')\n\ - >>> Decimal.from_float(float('-inf'))\n\ - Decimal('-Infinity')\n\ -\n\ -\n"); - -PyDoc_STRVAR(doc_fma, -"fma($self, /, other, third, context=None)\n--\n\n\ -Fused multiply-add. Return self*other+third with no rounding of the\n\ -intermediate product self*other.\n\ -\n\ - >>> Decimal(2).fma(3, 5)\n\ - Decimal('11')\n\ -\n\ -\n"); - -PyDoc_STRVAR(doc_is_canonical, -"is_canonical($self, /)\n--\n\n\ -Return True if the argument is canonical and False otherwise. Currently,\n\ -a Decimal instance is always canonical, so this operation always returns\n\ -True.\n\ -\n"); - -PyDoc_STRVAR(doc_is_finite, -"is_finite($self, /)\n--\n\n\ -Return True if the argument is a finite number, and False if the argument\n\ -is infinite or a NaN.\n\ -\n"); - -PyDoc_STRVAR(doc_is_infinite, -"is_infinite($self, /)\n--\n\n\ -Return True if the argument is either positive or negative infinity and\n\ -False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_is_nan, -"is_nan($self, /)\n--\n\n\ -Return True if the argument is a (quiet or signaling) NaN and False\n\ -otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_is_normal, -"is_normal($self, /, context=None)\n--\n\n\ -Return True if the argument is a normal finite non-zero number with an\n\ -adjusted exponent greater than or equal to Emin. Return False if the\n\ -argument is zero, subnormal, infinite or a NaN.\n\ -\n"); - -PyDoc_STRVAR(doc_is_qnan, -"is_qnan($self, /)\n--\n\n\ -Return True if the argument is a quiet NaN, and False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_is_signed, -"is_signed($self, /)\n--\n\n\ -Return True if the argument has a negative sign and False otherwise.\n\ -Note that both zeros and NaNs can carry signs.\n\ -\n"); - -PyDoc_STRVAR(doc_is_snan, -"is_snan($self, /)\n--\n\n\ -Return True if the argument is a signaling NaN and False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_is_subnormal, -"is_subnormal($self, /, context=None)\n--\n\n\ -Return True if the argument is subnormal, and False otherwise. A number is\n\ -subnormal if it is non-zero, finite, and has an adjusted exponent less\n\ -than Emin.\n\ -\n"); - -PyDoc_STRVAR(doc_is_zero, -"is_zero($self, /)\n--\n\n\ -Return True if the argument is a (positive or negative) zero and False\n\ -otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ln, -"ln($self, /, context=None)\n--\n\n\ -Return the natural (base e) logarithm of the operand. The function always\n\ -uses the ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ -\n"); - -PyDoc_STRVAR(doc_log10, -"log10($self, /, context=None)\n--\n\n\ -Return the base ten logarithm of the operand. The function always uses the\n\ -ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ -\n"); - -PyDoc_STRVAR(doc_logb, -"logb($self, /, context=None)\n--\n\n\ -For a non-zero number, return the adjusted exponent of the operand as a\n\ -Decimal instance. If the operand is a zero, then Decimal('-Infinity') is\n\ -returned and the DivisionByZero condition is raised. If the operand is\n\ -an infinity then Decimal('Infinity') is returned.\n\ -\n"); - -PyDoc_STRVAR(doc_logical_and, -"logical_and($self, /, other, context=None)\n--\n\n\ -Return the digit-wise 'and' of the two (logical) operands.\n\ -\n"); - -PyDoc_STRVAR(doc_logical_invert, -"logical_invert($self, /, context=None)\n--\n\n\ -Return the digit-wise inversion of the (logical) operand.\n\ -\n"); - -PyDoc_STRVAR(doc_logical_or, -"logical_or($self, /, other, context=None)\n--\n\n\ -Return the digit-wise 'or' of the two (logical) operands.\n\ -\n"); - -PyDoc_STRVAR(doc_logical_xor, -"logical_xor($self, /, other, context=None)\n--\n\n\ -Return the digit-wise 'exclusive or' of the two (logical) operands.\n\ -\n"); - -PyDoc_STRVAR(doc_max, -"max($self, /, other, context=None)\n--\n\n\ -Maximum of self and other. If one operand is a quiet NaN and the other is\n\ -numeric, the numeric operand is returned.\n\ -\n"); - -PyDoc_STRVAR(doc_max_mag, -"max_mag($self, /, other, context=None)\n--\n\n\ -Similar to the max() method, but the comparison is done using the absolute\n\ -values of the operands.\n\ -\n"); - -PyDoc_STRVAR(doc_min, -"min($self, /, other, context=None)\n--\n\n\ -Minimum of self and other. If one operand is a quiet NaN and the other is\n\ -numeric, the numeric operand is returned.\n\ -\n"); - -PyDoc_STRVAR(doc_min_mag, -"min_mag($self, /, other, context=None)\n--\n\n\ -Similar to the min() method, but the comparison is done using the absolute\n\ -values of the operands.\n\ -\n"); - -PyDoc_STRVAR(doc_next_minus, -"next_minus($self, /, context=None)\n--\n\n\ -Return the largest number representable in the given context (or in the\n\ -current default context if no context is given) that is smaller than the\n\ -given operand.\n\ -\n"); - -PyDoc_STRVAR(doc_next_plus, -"next_plus($self, /, context=None)\n--\n\n\ -Return the smallest number representable in the given context (or in the\n\ -current default context if no context is given) that is larger than the\n\ -given operand.\n\ -\n"); - -PyDoc_STRVAR(doc_next_toward, -"next_toward($self, /, other, context=None)\n--\n\n\ -If the two operands are unequal, return the number closest to the first\n\ -operand in the direction of the second operand. If both operands are\n\ -numerically equal, return a copy of the first operand with the sign set\n\ -to be the same as the sign of the second operand.\n\ -\n"); - -PyDoc_STRVAR(doc_normalize, -"normalize($self, /, context=None)\n--\n\n\ -Normalize the number by stripping the rightmost trailing zeros and\n\ -converting any result equal to Decimal('0') to Decimal('0e0'). Used\n\ -for producing canonical values for members of an equivalence class.\n\ -For example, Decimal('32.100') and Decimal('0.321000e+2') both normalize\n\ -to the equivalent value Decimal('32.1').\n\ -\n"); - -PyDoc_STRVAR(doc_number_class, -"number_class($self, /, context=None)\n--\n\n\ -Return a string describing the class of the operand. The returned value\n\ -is one of the following ten strings:\n\ -\n\ - * '-Infinity', indicating that the operand is negative infinity.\n\ - * '-Normal', indicating that the operand is a negative normal number.\n\ - * '-Subnormal', indicating that the operand is negative and subnormal.\n\ - * '-Zero', indicating that the operand is a negative zero.\n\ - * '+Zero', indicating that the operand is a positive zero.\n\ - * '+Subnormal', indicating that the operand is positive and subnormal.\n\ - * '+Normal', indicating that the operand is a positive normal number.\n\ - * '+Infinity', indicating that the operand is positive infinity.\n\ - * 'NaN', indicating that the operand is a quiet NaN (Not a Number).\n\ - * 'sNaN', indicating that the operand is a signaling NaN.\n\ -\n\ -\n"); - -PyDoc_STRVAR(doc_quantize, -"quantize($self, /, exp, rounding=None, context=None)\n--\n\n\ -Return a value equal to the first operand after rounding and having the\n\ -exponent of the second operand.\n\ -\n\ - >>> Decimal('1.41421356').quantize(Decimal('1.000'))\n\ - Decimal('1.414')\n\ -\n\ -Unlike other operations, if the length of the coefficient after the quantize\n\ -operation would be greater than precision, then an InvalidOperation is signaled.\n\ -This guarantees that, unless there is an error condition, the quantized exponent\n\ -is always equal to that of the right-hand operand.\n\ -\n\ -Also unlike other operations, quantize never signals Underflow, even if the\n\ -result is subnormal and inexact.\n\ -\n\ -If the exponent of the second operand is larger than that of the first, then\n\ -rounding may be necessary. In this case, the rounding mode is determined by the\n\ -rounding argument if given, else by the given context argument; if neither\n\ -argument is given, the rounding mode of the current thread's context is used.\n\ -\n"); - -PyDoc_STRVAR(doc_radix, -"radix($self, /)\n--\n\n\ -Return Decimal(10), the radix (base) in which the Decimal class does\n\ -all its arithmetic. Included for compatibility with the specification.\n\ -\n"); - -PyDoc_STRVAR(doc_remainder_near, -"remainder_near($self, /, other, context=None)\n--\n\n\ -Return the remainder from dividing self by other. This differs from\n\ -self % other in that the sign of the remainder is chosen so as to minimize\n\ -its absolute value. More precisely, the return value is self - n * other\n\ -where n is the integer nearest to the exact value of self / other, and\n\ -if two integers are equally near then the even one is chosen.\n\ -\n\ -If the result is zero then its sign will be the sign of self.\n\ -\n"); - -PyDoc_STRVAR(doc_rotate, -"rotate($self, /, other, context=None)\n--\n\n\ -Return the result of rotating the digits of the first operand by an amount\n\ -specified by the second operand. The second operand must be an integer in\n\ -the range -precision through precision. The absolute value of the second\n\ -operand gives the number of places to rotate. If the second operand is\n\ -positive then rotation is to the left; otherwise rotation is to the right.\n\ -The coefficient of the first operand is padded on the left with zeros to\n\ -length precision if necessary. The sign and exponent of the first operand are\n\ -unchanged.\n\ -\n"); - -PyDoc_STRVAR(doc_same_quantum, -"same_quantum($self, /, other, context=None)\n--\n\n\ -Test whether self and other have the same exponent or whether both are NaN.\n\ -\n\ -This operation is unaffected by context and is quiet: no flags are changed\n\ -and no rounding is performed. As an exception, the C version may raise\n\ -InvalidOperation if the second operand cannot be converted exactly.\n\ -\n"); - -PyDoc_STRVAR(doc_scaleb, -"scaleb($self, /, other, context=None)\n--\n\n\ -Return the first operand with the exponent adjusted the second. Equivalently,\n\ -return the first operand multiplied by 10**other. The second operand must be\n\ -an integer.\n\ -\n"); - -PyDoc_STRVAR(doc_shift, -"shift($self, /, other, context=None)\n--\n\n\ -Return the result of shifting the digits of the first operand by an amount\n\ -specified by the second operand. The second operand must be an integer in\n\ -the range -precision through precision. The absolute value of the second\n\ -operand gives the number of places to shift. If the second operand is\n\ -positive, then the shift is to the left; otherwise the shift is to the\n\ -right. Digits shifted into the coefficient are zeros. The sign and exponent\n\ -of the first operand are unchanged.\n\ -\n"); - -PyDoc_STRVAR(doc_sqrt, -"sqrt($self, /, context=None)\n--\n\n\ -Return the square root of the argument to full precision. The result is\n\ -correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\ -\n"); - -PyDoc_STRVAR(doc_to_eng_string, -"to_eng_string($self, /, context=None)\n--\n\n\ -Convert to an engineering-type string. Engineering notation has an exponent\n\ -which is a multiple of 3, so there are up to 3 digits left of the decimal\n\ -place. For example, Decimal('123E+1') is converted to Decimal('1.23E+3').\n\ -\n\ -The value of context.capitals determines whether the exponent sign is lower\n\ -or upper case. Otherwise, the context does not affect the operation.\n\ -\n"); - -PyDoc_STRVAR(doc_to_integral, -"to_integral($self, /, rounding=None, context=None)\n--\n\n\ -Identical to the to_integral_value() method. The to_integral() name has been\n\ -kept for compatibility with older versions.\n\ -\n"); - -PyDoc_STRVAR(doc_to_integral_exact, -"to_integral_exact($self, /, rounding=None, context=None)\n--\n\n\ -Round to the nearest integer, signaling Inexact or Rounded as appropriate if\n\ -rounding occurs. The rounding mode is determined by the rounding parameter\n\ -if given, else by the given context. If neither parameter is given, then the\n\ -rounding mode of the current default context is used.\n\ -\n"); - -PyDoc_STRVAR(doc_to_integral_value, -"to_integral_value($self, /, rounding=None, context=None)\n--\n\n\ -Round to the nearest integer without signaling Inexact or Rounded. The\n\ -rounding mode is determined by the rounding parameter if given, else by\n\ -the given context. If neither parameter is given, then the rounding mode\n\ -of the current default context is used.\n\ -\n"); - - -/******************************************************************************/ -/* Context Object and Methods */ -/******************************************************************************/ - -PyDoc_STRVAR(doc_context, -"Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)\n--\n\n\ -The context affects almost all operations and controls rounding,\n\ -Over/Underflow, raising of exceptions and much more. A new context\n\ -can be constructed as follows:\n\ -\n\ - >>> c = Context(prec=28, Emin=-425000000, Emax=425000000,\n\ - ... rounding=ROUND_HALF_EVEN, capitals=1, clamp=1,\n\ - ... traps=[InvalidOperation, DivisionByZero, Overflow],\n\ - ... flags=[])\n\ - >>>\n\ -\n\ -\n"); - -#ifdef EXTRA_FUNCTIONALITY -PyDoc_STRVAR(doc_ctx_apply, -"apply($self, x, /)\n--\n\n\ -Apply self to Decimal x.\n\ -\n"); -#endif - -PyDoc_STRVAR(doc_ctx_clear_flags, -"clear_flags($self, /)\n--\n\n\ -Reset all flags to False.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_clear_traps, -"clear_traps($self, /)\n--\n\n\ -Set all traps to False.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_copy, -"copy($self, /)\n--\n\n\ -Return a duplicate of the context with all flags cleared.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_copy_decimal, -"copy_decimal($self, x, /)\n--\n\n\ -Return a copy of Decimal x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_create_decimal, -"create_decimal($self, num=\"0\", /)\n--\n\n\ -Create a new Decimal instance from num, using self as the context. Unlike the\n\ -Decimal constructor, this function observes the context limits.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_create_decimal_from_float, -"create_decimal_from_float($self, f, /)\n--\n\n\ -Create a new Decimal instance from float f. Unlike the Decimal.from_float()\n\ -class method, this function observes the context limits.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_Etiny, -"Etiny($self, /)\n--\n\n\ -Return a value equal to Emin - prec + 1, which is the minimum exponent value\n\ -for subnormal results. When underflow occurs, the exponent is set to Etiny.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_Etop, -"Etop($self, /)\n--\n\n\ -Return a value equal to Emax - prec + 1. This is the maximum exponent\n\ -if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop()\n\ -must not be negative.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_abs, -"abs($self, x, /)\n--\n\n\ -Return the absolute value of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_add, -"add($self, x, y, /)\n--\n\n\ -Return the sum of x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_canonical, -"canonical($self, x, /)\n--\n\n\ -Return a new instance of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_compare, -"compare($self, x, y, /)\n--\n\n\ -Compare x and y numerically.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_compare_signal, -"compare_signal($self, x, y, /)\n--\n\n\ -Compare x and y numerically. All NaNs signal.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_compare_total, -"compare_total($self, x, y, /)\n--\n\n\ -Compare x and y using their abstract representation.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_compare_total_mag, -"compare_total_mag($self, x, y, /)\n--\n\n\ -Compare x and y using their abstract representation, ignoring sign.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_copy_abs, -"copy_abs($self, x, /)\n--\n\n\ -Return a copy of x with the sign set to 0.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_copy_negate, -"copy_negate($self, x, /)\n--\n\n\ -Return a copy of x with the sign inverted.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_copy_sign, -"copy_sign($self, x, y, /)\n--\n\n\ -Copy the sign from y to x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_divide, -"divide($self, x, y, /)\n--\n\n\ -Return x divided by y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_divide_int, -"divide_int($self, x, y, /)\n--\n\n\ -Return x divided by y, truncated to an integer.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_divmod, -"divmod($self, x, y, /)\n--\n\n\ -Return quotient and remainder of the division x / y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_exp, -"exp($self, x, /)\n--\n\n\ -Return e ** x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_fma, -"fma($self, x, y, z, /)\n--\n\n\ -Return x multiplied by y, plus z.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_canonical, -"is_canonical($self, x, /)\n--\n\n\ -Return True if x is canonical, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_finite, -"is_finite($self, x, /)\n--\n\n\ -Return True if x is finite, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_infinite, -"is_infinite($self, x, /)\n--\n\n\ -Return True if x is infinite, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_nan, -"is_nan($self, x, /)\n--\n\n\ -Return True if x is a qNaN or sNaN, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_normal, -"is_normal($self, x, /)\n--\n\n\ -Return True if x is a normal number, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_qnan, -"is_qnan($self, x, /)\n--\n\n\ -Return True if x is a quiet NaN, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_signed, -"is_signed($self, x, /)\n--\n\n\ -Return True if x is negative, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_snan, -"is_snan($self, x, /)\n--\n\n\ -Return True if x is a signaling NaN, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_subnormal, -"is_subnormal($self, x, /)\n--\n\n\ -Return True if x is subnormal, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_is_zero, -"is_zero($self, x, /)\n--\n\n\ -Return True if x is a zero, False otherwise.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_ln, -"ln($self, x, /)\n--\n\n\ -Return the natural (base e) logarithm of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_log10, -"log10($self, x, /)\n--\n\n\ -Return the base 10 logarithm of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_logb, -"logb($self, x, /)\n--\n\n\ -Return the exponent of the magnitude of the operand's MSD.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_logical_and, -"logical_and($self, x, y, /)\n--\n\n\ -Digit-wise and of x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_logical_invert, -"logical_invert($self, x, /)\n--\n\n\ -Invert all digits of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_logical_or, -"logical_or($self, x, y, /)\n--\n\n\ -Digit-wise or of x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_logical_xor, -"logical_xor($self, x, y, /)\n--\n\n\ -Digit-wise xor of x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_max, -"max($self, x, y, /)\n--\n\n\ -Compare the values numerically and return the maximum.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_max_mag, -"max_mag($self, x, y, /)\n--\n\n\ -Compare the values numerically with their sign ignored.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_min, -"min($self, x, y, /)\n--\n\n\ -Compare the values numerically and return the minimum.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_min_mag, -"min_mag($self, x, y, /)\n--\n\n\ -Compare the values numerically with their sign ignored.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_minus, -"minus($self, x, /)\n--\n\n\ -Minus corresponds to the unary prefix minus operator in Python, but applies\n\ -the context to the result.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_multiply, -"multiply($self, x, y, /)\n--\n\n\ -Return the product of x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_next_minus, -"next_minus($self, x, /)\n--\n\n\ -Return the largest representable number smaller than x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_next_plus, -"next_plus($self, x, /)\n--\n\n\ -Return the smallest representable number larger than x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_next_toward, -"next_toward($self, x, y, /)\n--\n\n\ -Return the number closest to x, in the direction towards y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_normalize, -"normalize($self, x, /)\n--\n\n\ -Reduce x to its simplest form. Alias for reduce(x).\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_number_class, -"number_class($self, x, /)\n--\n\n\ -Return an indication of the class of x.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_plus, -"plus($self, x, /)\n--\n\n\ -Plus corresponds to the unary prefix plus operator in Python, but applies\n\ -the context to the result.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_power, -"power($self, /, a, b, modulo=None)\n--\n\n\ -Compute a**b. If 'a' is negative, then 'b' must be integral. The result\n\ -will be inexact unless 'a' is integral and the result is finite and can\n\ -be expressed exactly in 'precision' digits. In the Python version the\n\ -result is always correctly rounded, in the C version the result is almost\n\ -always correctly rounded.\n\ -\n\ -If modulo is given, compute (a**b) % modulo. The following restrictions\n\ -hold:\n\ -\n\ - * all three arguments must be integral\n\ - * 'b' must be nonnegative\n\ - * at least one of 'a' or 'b' must be nonzero\n\ - * modulo must be nonzero and less than 10**prec in absolute value\n\ -\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_quantize, -"quantize($self, x, y, /)\n--\n\n\ -Return a value equal to x (rounded), having the exponent of y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_radix, -"radix($self, /)\n--\n\n\ -Return 10.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_remainder, -"remainder($self, x, y, /)\n--\n\n\ -Return the remainder from integer division. The sign of the result,\n\ -if non-zero, is the same as that of the original dividend.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_remainder_near, -"remainder_near($self, x, y, /)\n--\n\n\ -Return x - y * n, where n is the integer nearest the exact value of x / y\n\ -(if the result is 0 then its sign will be the sign of x).\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_rotate, -"rotate($self, x, y, /)\n--\n\n\ -Return a copy of x, rotated by y places.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_same_quantum, -"same_quantum($self, x, y, /)\n--\n\n\ -Return True if the two operands have the same exponent.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_scaleb, -"scaleb($self, x, y, /)\n--\n\n\ -Return the first operand after adding the second value to its exp.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_shift, -"shift($self, x, y, /)\n--\n\n\ -Return a copy of x, shifted by y places.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_sqrt, -"sqrt($self, x, /)\n--\n\n\ -Square root of a non-negative number to context precision.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_subtract, -"subtract($self, x, y, /)\n--\n\n\ -Return the difference between x and y.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_to_eng_string, -"to_eng_string($self, x, /)\n--\n\n\ -Convert a number to a string, using engineering notation.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_to_integral, -"to_integral($self, x, /)\n--\n\n\ -Identical to to_integral_value(x).\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_to_integral_exact, -"to_integral_exact($self, x, /)\n--\n\n\ -Round to an integer. Signal if the result is rounded or inexact.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_to_integral_value, -"to_integral_value($self, x, /)\n--\n\n\ -Round to an integer.\n\ -\n"); - -PyDoc_STRVAR(doc_ctx_to_sci_string, -"to_sci_string($self, x, /)\n--\n\n\ -Convert a number to a string using scientific notation.\n\ -\n"); - - -#endif /* DOCSTRINGS_H */ - - - +/* + * Copyright (c) 2001-2012 Python Software Foundation. All Rights Reserved. + * Modified and extended by Stefan Krah. + */ + + +#ifndef DOCSTRINGS_H +#define DOCSTRINGS_H + + +#include "pymacro.h" + + +/******************************************************************************/ +/* Module */ +/******************************************************************************/ + + +PyDoc_STRVAR(doc__decimal, +"C decimal arithmetic module"); + +PyDoc_STRVAR(doc_getcontext, +"getcontext($module, /)\n--\n\n\ +Get the current default context.\n\ +\n"); + +PyDoc_STRVAR(doc_setcontext, +"setcontext($module, context, /)\n--\n\n\ +Set a new default context.\n\ +\n"); + +PyDoc_STRVAR(doc_localcontext, +"localcontext($module, /, ctx=None)\n--\n\n\ +Return a context manager that will set the default context to a copy of ctx\n\ +on entry to the with-statement and restore the previous default context when\n\ +exiting the with-statement. If no context is specified, a copy of the current\n\ +default context is used.\n\ +\n"); + +#ifdef EXTRA_FUNCTIONALITY +PyDoc_STRVAR(doc_ieee_context, +"IEEEContext($module, bits, /)\n--\n\n\ +Return a context object initialized to the proper values for one of the\n\ +IEEE interchange formats. The argument must be a multiple of 32 and less\n\ +than IEEE_CONTEXT_MAX_BITS. For the most common values, the constants\n\ +DECIMAL32, DECIMAL64 and DECIMAL128 are provided.\n\ +\n"); +#endif + + +/******************************************************************************/ +/* Decimal Object and Methods */ +/******************************************************************************/ + +PyDoc_STRVAR(doc_decimal, +"Decimal(value=\"0\", context=None)\n--\n\n\ +Construct a new Decimal object. 'value' can be an integer, string, tuple,\n\ +or another Decimal object. If no value is given, return Decimal('0'). The\n\ +context does not affect the conversion and is only passed to determine if\n\ +the InvalidOperation trap is active.\n\ +\n"); + +PyDoc_STRVAR(doc_adjusted, +"adjusted($self, /)\n--\n\n\ +Return the adjusted exponent of the number. Defined as exp + digits - 1.\n\ +\n"); + +PyDoc_STRVAR(doc_as_tuple, +"as_tuple($self, /)\n--\n\n\ +Return a tuple representation of the number.\n\ +\n"); + +PyDoc_STRVAR(doc_as_integer_ratio, +"as_integer_ratio($self, /)\n--\n\n\ +Decimal.as_integer_ratio() -> (int, int)\n\ +\n\ +Return a pair of integers, whose ratio is exactly equal to the original\n\ +Decimal and with a positive denominator. The ratio is in lowest terms.\n\ +Raise OverflowError on infinities and a ValueError on NaNs.\n\ +\n"); + +PyDoc_STRVAR(doc_canonical, +"canonical($self, /)\n--\n\n\ +Return the canonical encoding of the argument. Currently, the encoding\n\ +of a Decimal instance is always canonical, so this operation returns its\n\ +argument unchanged.\n\ +\n"); + +PyDoc_STRVAR(doc_compare, +"compare($self, /, other, context=None)\n--\n\n\ +Compare self to other. Return a decimal value:\n\ +\n\ + a or b is a NaN ==> Decimal('NaN')\n\ + a < b ==> Decimal('-1')\n\ + a == b ==> Decimal('0')\n\ + a > b ==> Decimal('1')\n\ +\n"); + +PyDoc_STRVAR(doc_compare_signal, +"compare_signal($self, /, other, context=None)\n--\n\n\ +Identical to compare, except that all NaNs signal.\n\ +\n"); + +PyDoc_STRVAR(doc_compare_total, +"compare_total($self, /, other, context=None)\n--\n\n\ +Compare two operands using their abstract representation rather than\n\ +their numerical value. Similar to the compare() method, but the result\n\ +gives a total ordering on Decimal instances. Two Decimal instances with\n\ +the same numeric value but different representations compare unequal\n\ +in this ordering:\n\ +\n\ + >>> Decimal('12.0').compare_total(Decimal('12'))\n\ + Decimal('-1')\n\ +\n\ +Quiet and signaling NaNs are also included in the total ordering. The result\n\ +of this function is Decimal('0') if both operands have the same representation,\n\ +Decimal('-1') if the first operand is lower in the total order than the second,\n\ +and Decimal('1') if the first operand is higher in the total order than the\n\ +second operand. See the specification for details of the total order.\n\ +\n\ +This operation is unaffected by context and is quiet: no flags are changed\n\ +and no rounding is performed. As an exception, the C version may raise\n\ +InvalidOperation if the second operand cannot be converted exactly.\n\ +\n"); + +PyDoc_STRVAR(doc_compare_total_mag, +"compare_total_mag($self, /, other, context=None)\n--\n\n\ +Compare two operands using their abstract representation rather than their\n\ +value as in compare_total(), but ignoring the sign of each operand.\n\ +\n\ +x.compare_total_mag(y) is equivalent to x.copy_abs().compare_total(y.copy_abs()).\n\ +\n\ +This operation is unaffected by context and is quiet: no flags are changed\n\ +and no rounding is performed. As an exception, the C version may raise\n\ +InvalidOperation if the second operand cannot be converted exactly.\n\ +\n"); + +PyDoc_STRVAR(doc_conjugate, +"conjugate($self, /)\n--\n\n\ +Return self.\n\ +\n"); + +PyDoc_STRVAR(doc_copy_abs, +"copy_abs($self, /)\n--\n\n\ +Return the absolute value of the argument. This operation is unaffected by\n\ +context and is quiet: no flags are changed and no rounding is performed.\n\ +\n"); + +PyDoc_STRVAR(doc_copy_negate, +"copy_negate($self, /)\n--\n\n\ +Return the negation of the argument. This operation is unaffected by context\n\ +and is quiet: no flags are changed and no rounding is performed.\n\ +\n"); + +PyDoc_STRVAR(doc_copy_sign, +"copy_sign($self, /, other, context=None)\n--\n\n\ +Return a copy of the first operand with the sign set to be the same as the\n\ +sign of the second operand. For example:\n\ +\n\ + >>> Decimal('2.3').copy_sign(Decimal('-1.5'))\n\ + Decimal('-2.3')\n\ +\n\ +This operation is unaffected by context and is quiet: no flags are changed\n\ +and no rounding is performed. As an exception, the C version may raise\n\ +InvalidOperation if the second operand cannot be converted exactly.\n\ +\n"); + +PyDoc_STRVAR(doc_exp, +"exp($self, /, context=None)\n--\n\n\ +Return the value of the (natural) exponential function e**x at the given\n\ +number. The function always uses the ROUND_HALF_EVEN mode and the result\n\ +is correctly rounded.\n\ +\n"); + +PyDoc_STRVAR(doc_from_float, +"from_float($type, f, /)\n--\n\n\ +Class method that converts a float to a decimal number, exactly.\n\ +Since 0.1 is not exactly representable in binary floating point,\n\ +Decimal.from_float(0.1) is not the same as Decimal('0.1').\n\ +\n\ + >>> Decimal.from_float(0.1)\n\ + Decimal('0.1000000000000000055511151231257827021181583404541015625')\n\ + >>> Decimal.from_float(float('nan'))\n\ + Decimal('NaN')\n\ + >>> Decimal.from_float(float('inf'))\n\ + Decimal('Infinity')\n\ + >>> Decimal.from_float(float('-inf'))\n\ + Decimal('-Infinity')\n\ +\n\ +\n"); + +PyDoc_STRVAR(doc_fma, +"fma($self, /, other, third, context=None)\n--\n\n\ +Fused multiply-add. Return self*other+third with no rounding of the\n\ +intermediate product self*other.\n\ +\n\ + >>> Decimal(2).fma(3, 5)\n\ + Decimal('11')\n\ +\n\ +\n"); + +PyDoc_STRVAR(doc_is_canonical, +"is_canonical($self, /)\n--\n\n\ +Return True if the argument is canonical and False otherwise. Currently,\n\ +a Decimal instance is always canonical, so this operation always returns\n\ +True.\n\ +\n"); + +PyDoc_STRVAR(doc_is_finite, +"is_finite($self, /)\n--\n\n\ +Return True if the argument is a finite number, and False if the argument\n\ +is infinite or a NaN.\n\ +\n"); + +PyDoc_STRVAR(doc_is_infinite, +"is_infinite($self, /)\n--\n\n\ +Return True if the argument is either positive or negative infinity and\n\ +False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_is_nan, +"is_nan($self, /)\n--\n\n\ +Return True if the argument is a (quiet or signaling) NaN and False\n\ +otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_is_normal, +"is_normal($self, /, context=None)\n--\n\n\ +Return True if the argument is a normal finite non-zero number with an\n\ +adjusted exponent greater than or equal to Emin. Return False if the\n\ +argument is zero, subnormal, infinite or a NaN.\n\ +\n"); + +PyDoc_STRVAR(doc_is_qnan, +"is_qnan($self, /)\n--\n\n\ +Return True if the argument is a quiet NaN, and False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_is_signed, +"is_signed($self, /)\n--\n\n\ +Return True if the argument has a negative sign and False otherwise.\n\ +Note that both zeros and NaNs can carry signs.\n\ +\n"); + +PyDoc_STRVAR(doc_is_snan, +"is_snan($self, /)\n--\n\n\ +Return True if the argument is a signaling NaN and False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_is_subnormal, +"is_subnormal($self, /, context=None)\n--\n\n\ +Return True if the argument is subnormal, and False otherwise. A number is\n\ +subnormal if it is non-zero, finite, and has an adjusted exponent less\n\ +than Emin.\n\ +\n"); + +PyDoc_STRVAR(doc_is_zero, +"is_zero($self, /)\n--\n\n\ +Return True if the argument is a (positive or negative) zero and False\n\ +otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ln, +"ln($self, /, context=None)\n--\n\n\ +Return the natural (base e) logarithm of the operand. The function always\n\ +uses the ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ +\n"); + +PyDoc_STRVAR(doc_log10, +"log10($self, /, context=None)\n--\n\n\ +Return the base ten logarithm of the operand. The function always uses the\n\ +ROUND_HALF_EVEN mode and the result is correctly rounded.\n\ +\n"); + +PyDoc_STRVAR(doc_logb, +"logb($self, /, context=None)\n--\n\n\ +For a non-zero number, return the adjusted exponent of the operand as a\n\ +Decimal instance. If the operand is a zero, then Decimal('-Infinity') is\n\ +returned and the DivisionByZero condition is raised. If the operand is\n\ +an infinity then Decimal('Infinity') is returned.\n\ +\n"); + +PyDoc_STRVAR(doc_logical_and, +"logical_and($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'and' of the two (logical) operands.\n\ +\n"); + +PyDoc_STRVAR(doc_logical_invert, +"logical_invert($self, /, context=None)\n--\n\n\ +Return the digit-wise inversion of the (logical) operand.\n\ +\n"); + +PyDoc_STRVAR(doc_logical_or, +"logical_or($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'or' of the two (logical) operands.\n\ +\n"); + +PyDoc_STRVAR(doc_logical_xor, +"logical_xor($self, /, other, context=None)\n--\n\n\ +Return the digit-wise 'exclusive or' of the two (logical) operands.\n\ +\n"); + +PyDoc_STRVAR(doc_max, +"max($self, /, other, context=None)\n--\n\n\ +Maximum of self and other. If one operand is a quiet NaN and the other is\n\ +numeric, the numeric operand is returned.\n\ +\n"); + +PyDoc_STRVAR(doc_max_mag, +"max_mag($self, /, other, context=None)\n--\n\n\ +Similar to the max() method, but the comparison is done using the absolute\n\ +values of the operands.\n\ +\n"); + +PyDoc_STRVAR(doc_min, +"min($self, /, other, context=None)\n--\n\n\ +Minimum of self and other. If one operand is a quiet NaN and the other is\n\ +numeric, the numeric operand is returned.\n\ +\n"); + +PyDoc_STRVAR(doc_min_mag, +"min_mag($self, /, other, context=None)\n--\n\n\ +Similar to the min() method, but the comparison is done using the absolute\n\ +values of the operands.\n\ +\n"); + +PyDoc_STRVAR(doc_next_minus, +"next_minus($self, /, context=None)\n--\n\n\ +Return the largest number representable in the given context (or in the\n\ +current default context if no context is given) that is smaller than the\n\ +given operand.\n\ +\n"); + +PyDoc_STRVAR(doc_next_plus, +"next_plus($self, /, context=None)\n--\n\n\ +Return the smallest number representable in the given context (or in the\n\ +current default context if no context is given) that is larger than the\n\ +given operand.\n\ +\n"); + +PyDoc_STRVAR(doc_next_toward, +"next_toward($self, /, other, context=None)\n--\n\n\ +If the two operands are unequal, return the number closest to the first\n\ +operand in the direction of the second operand. If both operands are\n\ +numerically equal, return a copy of the first operand with the sign set\n\ +to be the same as the sign of the second operand.\n\ +\n"); + +PyDoc_STRVAR(doc_normalize, +"normalize($self, /, context=None)\n--\n\n\ +Normalize the number by stripping the rightmost trailing zeros and\n\ +converting any result equal to Decimal('0') to Decimal('0e0'). Used\n\ +for producing canonical values for members of an equivalence class.\n\ +For example, Decimal('32.100') and Decimal('0.321000e+2') both normalize\n\ +to the equivalent value Decimal('32.1').\n\ +\n"); + +PyDoc_STRVAR(doc_number_class, +"number_class($self, /, context=None)\n--\n\n\ +Return a string describing the class of the operand. The returned value\n\ +is one of the following ten strings:\n\ +\n\ + * '-Infinity', indicating that the operand is negative infinity.\n\ + * '-Normal', indicating that the operand is a negative normal number.\n\ + * '-Subnormal', indicating that the operand is negative and subnormal.\n\ + * '-Zero', indicating that the operand is a negative zero.\n\ + * '+Zero', indicating that the operand is a positive zero.\n\ + * '+Subnormal', indicating that the operand is positive and subnormal.\n\ + * '+Normal', indicating that the operand is a positive normal number.\n\ + * '+Infinity', indicating that the operand is positive infinity.\n\ + * 'NaN', indicating that the operand is a quiet NaN (Not a Number).\n\ + * 'sNaN', indicating that the operand is a signaling NaN.\n\ +\n\ +\n"); + +PyDoc_STRVAR(doc_quantize, +"quantize($self, /, exp, rounding=None, context=None)\n--\n\n\ +Return a value equal to the first operand after rounding and having the\n\ +exponent of the second operand.\n\ +\n\ + >>> Decimal('1.41421356').quantize(Decimal('1.000'))\n\ + Decimal('1.414')\n\ +\n\ +Unlike other operations, if the length of the coefficient after the quantize\n\ +operation would be greater than precision, then an InvalidOperation is signaled.\n\ +This guarantees that, unless there is an error condition, the quantized exponent\n\ +is always equal to that of the right-hand operand.\n\ +\n\ +Also unlike other operations, quantize never signals Underflow, even if the\n\ +result is subnormal and inexact.\n\ +\n\ +If the exponent of the second operand is larger than that of the first, then\n\ +rounding may be necessary. In this case, the rounding mode is determined by the\n\ +rounding argument if given, else by the given context argument; if neither\n\ +argument is given, the rounding mode of the current thread's context is used.\n\ +\n"); + +PyDoc_STRVAR(doc_radix, +"radix($self, /)\n--\n\n\ +Return Decimal(10), the radix (base) in which the Decimal class does\n\ +all its arithmetic. Included for compatibility with the specification.\n\ +\n"); + +PyDoc_STRVAR(doc_remainder_near, +"remainder_near($self, /, other, context=None)\n--\n\n\ +Return the remainder from dividing self by other. This differs from\n\ +self % other in that the sign of the remainder is chosen so as to minimize\n\ +its absolute value. More precisely, the return value is self - n * other\n\ +where n is the integer nearest to the exact value of self / other, and\n\ +if two integers are equally near then the even one is chosen.\n\ +\n\ +If the result is zero then its sign will be the sign of self.\n\ +\n"); + +PyDoc_STRVAR(doc_rotate, +"rotate($self, /, other, context=None)\n--\n\n\ +Return the result of rotating the digits of the first operand by an amount\n\ +specified by the second operand. The second operand must be an integer in\n\ +the range -precision through precision. The absolute value of the second\n\ +operand gives the number of places to rotate. If the second operand is\n\ +positive then rotation is to the left; otherwise rotation is to the right.\n\ +The coefficient of the first operand is padded on the left with zeros to\n\ +length precision if necessary. The sign and exponent of the first operand are\n\ +unchanged.\n\ +\n"); + +PyDoc_STRVAR(doc_same_quantum, +"same_quantum($self, /, other, context=None)\n--\n\n\ +Test whether self and other have the same exponent or whether both are NaN.\n\ +\n\ +This operation is unaffected by context and is quiet: no flags are changed\n\ +and no rounding is performed. As an exception, the C version may raise\n\ +InvalidOperation if the second operand cannot be converted exactly.\n\ +\n"); + +PyDoc_STRVAR(doc_scaleb, +"scaleb($self, /, other, context=None)\n--\n\n\ +Return the first operand with the exponent adjusted the second. Equivalently,\n\ +return the first operand multiplied by 10**other. The second operand must be\n\ +an integer.\n\ +\n"); + +PyDoc_STRVAR(doc_shift, +"shift($self, /, other, context=None)\n--\n\n\ +Return the result of shifting the digits of the first operand by an amount\n\ +specified by the second operand. The second operand must be an integer in\n\ +the range -precision through precision. The absolute value of the second\n\ +operand gives the number of places to shift. If the second operand is\n\ +positive, then the shift is to the left; otherwise the shift is to the\n\ +right. Digits shifted into the coefficient are zeros. The sign and exponent\n\ +of the first operand are unchanged.\n\ +\n"); + +PyDoc_STRVAR(doc_sqrt, +"sqrt($self, /, context=None)\n--\n\n\ +Return the square root of the argument to full precision. The result is\n\ +correctly rounded using the ROUND_HALF_EVEN rounding mode.\n\ +\n"); + +PyDoc_STRVAR(doc_to_eng_string, +"to_eng_string($self, /, context=None)\n--\n\n\ +Convert to an engineering-type string. Engineering notation has an exponent\n\ +which is a multiple of 3, so there are up to 3 digits left of the decimal\n\ +place. For example, Decimal('123E+1') is converted to Decimal('1.23E+3').\n\ +\n\ +The value of context.capitals determines whether the exponent sign is lower\n\ +or upper case. Otherwise, the context does not affect the operation.\n\ +\n"); + +PyDoc_STRVAR(doc_to_integral, +"to_integral($self, /, rounding=None, context=None)\n--\n\n\ +Identical to the to_integral_value() method. The to_integral() name has been\n\ +kept for compatibility with older versions.\n\ +\n"); + +PyDoc_STRVAR(doc_to_integral_exact, +"to_integral_exact($self, /, rounding=None, context=None)\n--\n\n\ +Round to the nearest integer, signaling Inexact or Rounded as appropriate if\n\ +rounding occurs. The rounding mode is determined by the rounding parameter\n\ +if given, else by the given context. If neither parameter is given, then the\n\ +rounding mode of the current default context is used.\n\ +\n"); + +PyDoc_STRVAR(doc_to_integral_value, +"to_integral_value($self, /, rounding=None, context=None)\n--\n\n\ +Round to the nearest integer without signaling Inexact or Rounded. The\n\ +rounding mode is determined by the rounding parameter if given, else by\n\ +the given context. If neither parameter is given, then the rounding mode\n\ +of the current default context is used.\n\ +\n"); + + +/******************************************************************************/ +/* Context Object and Methods */ +/******************************************************************************/ + +PyDoc_STRVAR(doc_context, +"Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)\n--\n\n\ +The context affects almost all operations and controls rounding,\n\ +Over/Underflow, raising of exceptions and much more. A new context\n\ +can be constructed as follows:\n\ +\n\ + >>> c = Context(prec=28, Emin=-425000000, Emax=425000000,\n\ + ... rounding=ROUND_HALF_EVEN, capitals=1, clamp=1,\n\ + ... traps=[InvalidOperation, DivisionByZero, Overflow],\n\ + ... flags=[])\n\ + >>>\n\ +\n\ +\n"); + +#ifdef EXTRA_FUNCTIONALITY +PyDoc_STRVAR(doc_ctx_apply, +"apply($self, x, /)\n--\n\n\ +Apply self to Decimal x.\n\ +\n"); +#endif + +PyDoc_STRVAR(doc_ctx_clear_flags, +"clear_flags($self, /)\n--\n\n\ +Reset all flags to False.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_clear_traps, +"clear_traps($self, /)\n--\n\n\ +Set all traps to False.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_copy, +"copy($self, /)\n--\n\n\ +Return a duplicate of the context with all flags cleared.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_copy_decimal, +"copy_decimal($self, x, /)\n--\n\n\ +Return a copy of Decimal x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_create_decimal, +"create_decimal($self, num=\"0\", /)\n--\n\n\ +Create a new Decimal instance from num, using self as the context. Unlike the\n\ +Decimal constructor, this function observes the context limits.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_create_decimal_from_float, +"create_decimal_from_float($self, f, /)\n--\n\n\ +Create a new Decimal instance from float f. Unlike the Decimal.from_float()\n\ +class method, this function observes the context limits.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_Etiny, +"Etiny($self, /)\n--\n\n\ +Return a value equal to Emin - prec + 1, which is the minimum exponent value\n\ +for subnormal results. When underflow occurs, the exponent is set to Etiny.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_Etop, +"Etop($self, /)\n--\n\n\ +Return a value equal to Emax - prec + 1. This is the maximum exponent\n\ +if the _clamp field of the context is set to 1 (IEEE clamp mode). Etop()\n\ +must not be negative.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_abs, +"abs($self, x, /)\n--\n\n\ +Return the absolute value of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_add, +"add($self, x, y, /)\n--\n\n\ +Return the sum of x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_canonical, +"canonical($self, x, /)\n--\n\n\ +Return a new instance of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_compare, +"compare($self, x, y, /)\n--\n\n\ +Compare x and y numerically.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_compare_signal, +"compare_signal($self, x, y, /)\n--\n\n\ +Compare x and y numerically. All NaNs signal.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_compare_total, +"compare_total($self, x, y, /)\n--\n\n\ +Compare x and y using their abstract representation.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_compare_total_mag, +"compare_total_mag($self, x, y, /)\n--\n\n\ +Compare x and y using their abstract representation, ignoring sign.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_copy_abs, +"copy_abs($self, x, /)\n--\n\n\ +Return a copy of x with the sign set to 0.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_copy_negate, +"copy_negate($self, x, /)\n--\n\n\ +Return a copy of x with the sign inverted.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_copy_sign, +"copy_sign($self, x, y, /)\n--\n\n\ +Copy the sign from y to x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_divide, +"divide($self, x, y, /)\n--\n\n\ +Return x divided by y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_divide_int, +"divide_int($self, x, y, /)\n--\n\n\ +Return x divided by y, truncated to an integer.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_divmod, +"divmod($self, x, y, /)\n--\n\n\ +Return quotient and remainder of the division x / y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_exp, +"exp($self, x, /)\n--\n\n\ +Return e ** x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_fma, +"fma($self, x, y, z, /)\n--\n\n\ +Return x multiplied by y, plus z.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_canonical, +"is_canonical($self, x, /)\n--\n\n\ +Return True if x is canonical, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_finite, +"is_finite($self, x, /)\n--\n\n\ +Return True if x is finite, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_infinite, +"is_infinite($self, x, /)\n--\n\n\ +Return True if x is infinite, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_nan, +"is_nan($self, x, /)\n--\n\n\ +Return True if x is a qNaN or sNaN, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_normal, +"is_normal($self, x, /)\n--\n\n\ +Return True if x is a normal number, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_qnan, +"is_qnan($self, x, /)\n--\n\n\ +Return True if x is a quiet NaN, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_signed, +"is_signed($self, x, /)\n--\n\n\ +Return True if x is negative, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_snan, +"is_snan($self, x, /)\n--\n\n\ +Return True if x is a signaling NaN, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_subnormal, +"is_subnormal($self, x, /)\n--\n\n\ +Return True if x is subnormal, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_is_zero, +"is_zero($self, x, /)\n--\n\n\ +Return True if x is a zero, False otherwise.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_ln, +"ln($self, x, /)\n--\n\n\ +Return the natural (base e) logarithm of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_log10, +"log10($self, x, /)\n--\n\n\ +Return the base 10 logarithm of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_logb, +"logb($self, x, /)\n--\n\n\ +Return the exponent of the magnitude of the operand's MSD.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_logical_and, +"logical_and($self, x, y, /)\n--\n\n\ +Digit-wise and of x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_logical_invert, +"logical_invert($self, x, /)\n--\n\n\ +Invert all digits of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_logical_or, +"logical_or($self, x, y, /)\n--\n\n\ +Digit-wise or of x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_logical_xor, +"logical_xor($self, x, y, /)\n--\n\n\ +Digit-wise xor of x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_max, +"max($self, x, y, /)\n--\n\n\ +Compare the values numerically and return the maximum.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_max_mag, +"max_mag($self, x, y, /)\n--\n\n\ +Compare the values numerically with their sign ignored.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_min, +"min($self, x, y, /)\n--\n\n\ +Compare the values numerically and return the minimum.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_min_mag, +"min_mag($self, x, y, /)\n--\n\n\ +Compare the values numerically with their sign ignored.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_minus, +"minus($self, x, /)\n--\n\n\ +Minus corresponds to the unary prefix minus operator in Python, but applies\n\ +the context to the result.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_multiply, +"multiply($self, x, y, /)\n--\n\n\ +Return the product of x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_next_minus, +"next_minus($self, x, /)\n--\n\n\ +Return the largest representable number smaller than x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_next_plus, +"next_plus($self, x, /)\n--\n\n\ +Return the smallest representable number larger than x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_next_toward, +"next_toward($self, x, y, /)\n--\n\n\ +Return the number closest to x, in the direction towards y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_normalize, +"normalize($self, x, /)\n--\n\n\ +Reduce x to its simplest form. Alias for reduce(x).\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_number_class, +"number_class($self, x, /)\n--\n\n\ +Return an indication of the class of x.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_plus, +"plus($self, x, /)\n--\n\n\ +Plus corresponds to the unary prefix plus operator in Python, but applies\n\ +the context to the result.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_power, +"power($self, /, a, b, modulo=None)\n--\n\n\ +Compute a**b. If 'a' is negative, then 'b' must be integral. The result\n\ +will be inexact unless 'a' is integral and the result is finite and can\n\ +be expressed exactly in 'precision' digits. In the Python version the\n\ +result is always correctly rounded, in the C version the result is almost\n\ +always correctly rounded.\n\ +\n\ +If modulo is given, compute (a**b) % modulo. The following restrictions\n\ +hold:\n\ +\n\ + * all three arguments must be integral\n\ + * 'b' must be nonnegative\n\ + * at least one of 'a' or 'b' must be nonzero\n\ + * modulo must be nonzero and less than 10**prec in absolute value\n\ +\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_quantize, +"quantize($self, x, y, /)\n--\n\n\ +Return a value equal to x (rounded), having the exponent of y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_radix, +"radix($self, /)\n--\n\n\ +Return 10.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_remainder, +"remainder($self, x, y, /)\n--\n\n\ +Return the remainder from integer division. The sign of the result,\n\ +if non-zero, is the same as that of the original dividend.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_remainder_near, +"remainder_near($self, x, y, /)\n--\n\n\ +Return x - y * n, where n is the integer nearest the exact value of x / y\n\ +(if the result is 0 then its sign will be the sign of x).\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_rotate, +"rotate($self, x, y, /)\n--\n\n\ +Return a copy of x, rotated by y places.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_same_quantum, +"same_quantum($self, x, y, /)\n--\n\n\ +Return True if the two operands have the same exponent.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_scaleb, +"scaleb($self, x, y, /)\n--\n\n\ +Return the first operand after adding the second value to its exp.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_shift, +"shift($self, x, y, /)\n--\n\n\ +Return a copy of x, shifted by y places.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_sqrt, +"sqrt($self, x, /)\n--\n\n\ +Square root of a non-negative number to context precision.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_subtract, +"subtract($self, x, y, /)\n--\n\n\ +Return the difference between x and y.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_to_eng_string, +"to_eng_string($self, x, /)\n--\n\n\ +Convert a number to a string, using engineering notation.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_to_integral, +"to_integral($self, x, /)\n--\n\n\ +Identical to to_integral_value(x).\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_to_integral_exact, +"to_integral_exact($self, x, /)\n--\n\n\ +Round to an integer. Signal if the result is rounded or inexact.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_to_integral_value, +"to_integral_value($self, x, /)\n--\n\n\ +Round to an integer.\n\ +\n"); + +PyDoc_STRVAR(doc_ctx_to_sci_string, +"to_sci_string($self, x, /)\n--\n\n\ +Convert a number to a string using scientific notation.\n\ +\n"); + + +#endif /* DOCSTRINGS_H */ + + + diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.c index 85c608fadf5..ac0bfc113af 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.c @@ -1,655 +1,655 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> -#include <stdio.h> +#include <stdio.h> #include "basearith.h" -#include "constants.h" -#include "typearith.h" - - -/*********************************************************************/ -/* Calculations in base MPD_RADIX */ -/*********************************************************************/ - - -/* - * Knuth, TAOCP, Volume 2, 4.3.1: - * w := sum of u (len m) and v (len n) - * n > 0 and m >= n - * The calling function has to handle a possible final carry. - */ -mpd_uint_t -_mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n) -{ - mpd_uint_t s; - mpd_uint_t carry = 0; - mpd_size_t i; - - assert(n > 0 && m >= n); - - /* add n members of u and v */ - for (i = 0; i < n; i++) { - s = u[i] + (v[i] + carry); - carry = (s < u[i]) | (s >= MPD_RADIX); - w[i] = carry ? s-MPD_RADIX : s; - } - /* if there is a carry, propagate it */ - for (; carry && i < m; i++) { - s = u[i] + carry; - carry = (s == MPD_RADIX); - w[i] = carry ? 0 : s; - } - /* copy the rest of u */ - for (; i < m; i++) { - w[i] = u[i]; - } - - return carry; -} - -/* - * Add the contents of u to w. Carries are propagated further. The caller - * has to make sure that w is big enough. - */ -void -_mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n) -{ - mpd_uint_t s; - mpd_uint_t carry = 0; - mpd_size_t i; - - if (n == 0) return; - - /* add n members of u to w */ - for (i = 0; i < n; i++) { - s = w[i] + (u[i] + carry); - carry = (s < w[i]) | (s >= MPD_RADIX); - w[i] = carry ? s-MPD_RADIX : s; - } - /* if there is a carry, propagate it */ - for (; carry; i++) { - s = w[i] + carry; - carry = (s == MPD_RADIX); - w[i] = carry ? 0 : s; - } -} - -/* - * Add v to w (len m). The calling function has to handle a possible - * final carry. Assumption: m > 0. - */ -mpd_uint_t -_mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v) -{ - mpd_uint_t s; - mpd_uint_t carry; - mpd_size_t i; - - assert(m > 0); - - /* add v to w */ - s = w[0] + v; - carry = (s < v) | (s >= MPD_RADIX); - w[0] = carry ? s-MPD_RADIX : s; - - /* if there is a carry, propagate it */ - for (i = 1; carry && i < m; i++) { - s = w[i] + carry; - carry = (s == MPD_RADIX); - w[i] = carry ? 0 : s; - } - - return carry; -} - -/* Increment u. The calling function has to handle a possible carry. */ -mpd_uint_t -_mpd_baseincr(mpd_uint_t *u, mpd_size_t n) -{ - mpd_uint_t s; - mpd_uint_t carry = 1; - mpd_size_t i; - - assert(n > 0); - - /* if there is a carry, propagate it */ - for (i = 0; carry && i < n; i++) { - s = u[i] + carry; - carry = (s == MPD_RADIX); - u[i] = carry ? 0 : s; - } - - return carry; -} - -/* - * Knuth, TAOCP, Volume 2, 4.3.1: - * w := difference of u (len m) and v (len n). - * number in u >= number in v; - */ -void -_mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n) -{ - mpd_uint_t d; - mpd_uint_t borrow = 0; - mpd_size_t i; - - assert(m > 0 && n > 0); - - /* subtract n members of v from u */ - for (i = 0; i < n; i++) { - d = u[i] - (v[i] + borrow); - borrow = (u[i] < d); - w[i] = borrow ? d + MPD_RADIX : d; - } - /* if there is a borrow, propagate it */ - for (; borrow && i < m; i++) { - d = u[i] - borrow; - borrow = (u[i] == 0); - w[i] = borrow ? MPD_RADIX-1 : d; - } - /* copy the rest of u */ - for (; i < m; i++) { - w[i] = u[i]; - } -} - -/* - * Subtract the contents of u from w. w is larger than u. Borrows are - * propagated further, but eventually w can absorb the final borrow. - */ -void -_mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n) -{ - mpd_uint_t d; - mpd_uint_t borrow = 0; - mpd_size_t i; - - if (n == 0) return; - - /* subtract n members of u from w */ - for (i = 0; i < n; i++) { - d = w[i] - (u[i] + borrow); - borrow = (w[i] < d); - w[i] = borrow ? d + MPD_RADIX : d; - } - /* if there is a borrow, propagate it */ - for (; borrow; i++) { - d = w[i] - borrow; - borrow = (w[i] == 0); - w[i] = borrow ? MPD_RADIX-1 : d; - } -} - -/* w := product of u (len n) and v (single word) */ -void -_mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) -{ - mpd_uint_t hi, lo; - mpd_uint_t carry = 0; - mpd_size_t i; - - assert(n > 0); - - for (i=0; i < n; i++) { - - _mpd_mul_words(&hi, &lo, u[i], v); - lo = carry + lo; - if (lo < carry) hi++; - - _mpd_div_words_r(&carry, &w[i], hi, lo); - } - w[i] = carry; -} - -/* - * Knuth, TAOCP, Volume 2, 4.3.1: - * w := product of u (len m) and v (len n) - * w must be initialized to zero - */ -void -_mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n) -{ - mpd_uint_t hi, lo; - mpd_uint_t carry; - mpd_size_t i, j; - - assert(m > 0 && n > 0); - - for (j=0; j < n; j++) { - carry = 0; - for (i=0; i < m; i++) { - - _mpd_mul_words(&hi, &lo, u[i], v[j]); - lo = w[i+j] + lo; - if (lo < w[i+j]) hi++; - lo = carry + lo; - if (lo < carry) hi++; - - _mpd_div_words_r(&carry, &w[i+j], hi, lo); - } - w[j+m] = carry; - } -} - -/* - * Knuth, TAOCP Volume 2, 4.3.1, exercise 16: - * w := quotient of u (len n) divided by a single word v - */ -mpd_uint_t -_mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) -{ - mpd_uint_t hi, lo; - mpd_uint_t rem = 0; - mpd_size_t i; - - assert(n > 0); - - for (i=n-1; i != MPD_SIZE_MAX; i--) { - - _mpd_mul_words(&hi, &lo, rem, MPD_RADIX); - lo = u[i] + lo; - if (lo < u[i]) hi++; - - _mpd_div_words(&w[i], &rem, hi, lo, v); - } - - return rem; -} - -/* - * Knuth, TAOCP Volume 2, 4.3.1: - * q, r := quotient and remainder of uconst (len nplusm) - * divided by vconst (len n) - * nplusm >= n - * - * If r is not NULL, r will contain the remainder. If r is NULL, the - * return value indicates if there is a remainder: 1 for true, 0 for - * false. A return value of -1 indicates an error. - */ -int -_mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, - const mpd_uint_t *uconst, const mpd_uint_t *vconst, - mpd_size_t nplusm, mpd_size_t n) -{ - mpd_uint_t ustatic[MPD_MINALLOC_MAX]; - mpd_uint_t vstatic[MPD_MINALLOC_MAX]; - mpd_uint_t *u = ustatic; - mpd_uint_t *v = vstatic; - mpd_uint_t d, qhat, rhat, w2[2]; - mpd_uint_t hi, lo, x; - mpd_uint_t carry; - mpd_size_t i, j, m; - int retval = 0; - - assert(n > 1 && nplusm >= n); - m = sub_size_t(nplusm, n); - - /* D1: normalize */ - d = MPD_RADIX / (vconst[n-1] + 1); - - if (nplusm >= MPD_MINALLOC_MAX) { - if ((u = mpd_alloc(nplusm+1, sizeof *u)) == NULL) { - return -1; - } - } - if (n >= MPD_MINALLOC_MAX) { - if ((v = mpd_alloc(n+1, sizeof *v)) == NULL) { - mpd_free(u); - return -1; - } - } - - _mpd_shortmul(u, uconst, nplusm, d); - _mpd_shortmul(v, vconst, n, d); - - /* D2: loop */ - for (j=m; j != MPD_SIZE_MAX; j--) { +#include "constants.h" +#include "typearith.h" + + +/*********************************************************************/ +/* Calculations in base MPD_RADIX */ +/*********************************************************************/ + + +/* + * Knuth, TAOCP, Volume 2, 4.3.1: + * w := sum of u (len m) and v (len n) + * n > 0 and m >= n + * The calling function has to handle a possible final carry. + */ +mpd_uint_t +_mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n) +{ + mpd_uint_t s; + mpd_uint_t carry = 0; + mpd_size_t i; + + assert(n > 0 && m >= n); + + /* add n members of u and v */ + for (i = 0; i < n; i++) { + s = u[i] + (v[i] + carry); + carry = (s < u[i]) | (s >= MPD_RADIX); + w[i] = carry ? s-MPD_RADIX : s; + } + /* if there is a carry, propagate it */ + for (; carry && i < m; i++) { + s = u[i] + carry; + carry = (s == MPD_RADIX); + w[i] = carry ? 0 : s; + } + /* copy the rest of u */ + for (; i < m; i++) { + w[i] = u[i]; + } + + return carry; +} + +/* + * Add the contents of u to w. Carries are propagated further. The caller + * has to make sure that w is big enough. + */ +void +_mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n) +{ + mpd_uint_t s; + mpd_uint_t carry = 0; + mpd_size_t i; + + if (n == 0) return; + + /* add n members of u to w */ + for (i = 0; i < n; i++) { + s = w[i] + (u[i] + carry); + carry = (s < w[i]) | (s >= MPD_RADIX); + w[i] = carry ? s-MPD_RADIX : s; + } + /* if there is a carry, propagate it */ + for (; carry; i++) { + s = w[i] + carry; + carry = (s == MPD_RADIX); + w[i] = carry ? 0 : s; + } +} + +/* + * Add v to w (len m). The calling function has to handle a possible + * final carry. Assumption: m > 0. + */ +mpd_uint_t +_mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v) +{ + mpd_uint_t s; + mpd_uint_t carry; + mpd_size_t i; + + assert(m > 0); + + /* add v to w */ + s = w[0] + v; + carry = (s < v) | (s >= MPD_RADIX); + w[0] = carry ? s-MPD_RADIX : s; + + /* if there is a carry, propagate it */ + for (i = 1; carry && i < m; i++) { + s = w[i] + carry; + carry = (s == MPD_RADIX); + w[i] = carry ? 0 : s; + } + + return carry; +} + +/* Increment u. The calling function has to handle a possible carry. */ +mpd_uint_t +_mpd_baseincr(mpd_uint_t *u, mpd_size_t n) +{ + mpd_uint_t s; + mpd_uint_t carry = 1; + mpd_size_t i; + + assert(n > 0); + + /* if there is a carry, propagate it */ + for (i = 0; carry && i < n; i++) { + s = u[i] + carry; + carry = (s == MPD_RADIX); + u[i] = carry ? 0 : s; + } + + return carry; +} + +/* + * Knuth, TAOCP, Volume 2, 4.3.1: + * w := difference of u (len m) and v (len n). + * number in u >= number in v; + */ +void +_mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n) +{ + mpd_uint_t d; + mpd_uint_t borrow = 0; + mpd_size_t i; + + assert(m > 0 && n > 0); + + /* subtract n members of v from u */ + for (i = 0; i < n; i++) { + d = u[i] - (v[i] + borrow); + borrow = (u[i] < d); + w[i] = borrow ? d + MPD_RADIX : d; + } + /* if there is a borrow, propagate it */ + for (; borrow && i < m; i++) { + d = u[i] - borrow; + borrow = (u[i] == 0); + w[i] = borrow ? MPD_RADIX-1 : d; + } + /* copy the rest of u */ + for (; i < m; i++) { + w[i] = u[i]; + } +} + +/* + * Subtract the contents of u from w. w is larger than u. Borrows are + * propagated further, but eventually w can absorb the final borrow. + */ +void +_mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n) +{ + mpd_uint_t d; + mpd_uint_t borrow = 0; + mpd_size_t i; + + if (n == 0) return; + + /* subtract n members of u from w */ + for (i = 0; i < n; i++) { + d = w[i] - (u[i] + borrow); + borrow = (w[i] < d); + w[i] = borrow ? d + MPD_RADIX : d; + } + /* if there is a borrow, propagate it */ + for (; borrow; i++) { + d = w[i] - borrow; + borrow = (w[i] == 0); + w[i] = borrow ? MPD_RADIX-1 : d; + } +} + +/* w := product of u (len n) and v (single word) */ +void +_mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) +{ + mpd_uint_t hi, lo; + mpd_uint_t carry = 0; + mpd_size_t i; + + assert(n > 0); + + for (i=0; i < n; i++) { + + _mpd_mul_words(&hi, &lo, u[i], v); + lo = carry + lo; + if (lo < carry) hi++; + + _mpd_div_words_r(&carry, &w[i], hi, lo); + } + w[i] = carry; +} + +/* + * Knuth, TAOCP, Volume 2, 4.3.1: + * w := product of u (len m) and v (len n) + * w must be initialized to zero + */ +void +_mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n) +{ + mpd_uint_t hi, lo; + mpd_uint_t carry; + mpd_size_t i, j; + + assert(m > 0 && n > 0); + + for (j=0; j < n; j++) { + carry = 0; + for (i=0; i < m; i++) { + + _mpd_mul_words(&hi, &lo, u[i], v[j]); + lo = w[i+j] + lo; + if (lo < w[i+j]) hi++; + lo = carry + lo; + if (lo < carry) hi++; + + _mpd_div_words_r(&carry, &w[i+j], hi, lo); + } + w[j+m] = carry; + } +} + +/* + * Knuth, TAOCP Volume 2, 4.3.1, exercise 16: + * w := quotient of u (len n) divided by a single word v + */ +mpd_uint_t +_mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) +{ + mpd_uint_t hi, lo; + mpd_uint_t rem = 0; + mpd_size_t i; + + assert(n > 0); + + for (i=n-1; i != MPD_SIZE_MAX; i--) { + + _mpd_mul_words(&hi, &lo, rem, MPD_RADIX); + lo = u[i] + lo; + if (lo < u[i]) hi++; + + _mpd_div_words(&w[i], &rem, hi, lo, v); + } + + return rem; +} + +/* + * Knuth, TAOCP Volume 2, 4.3.1: + * q, r := quotient and remainder of uconst (len nplusm) + * divided by vconst (len n) + * nplusm >= n + * + * If r is not NULL, r will contain the remainder. If r is NULL, the + * return value indicates if there is a remainder: 1 for true, 0 for + * false. A return value of -1 indicates an error. + */ +int +_mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, + const mpd_uint_t *uconst, const mpd_uint_t *vconst, + mpd_size_t nplusm, mpd_size_t n) +{ + mpd_uint_t ustatic[MPD_MINALLOC_MAX]; + mpd_uint_t vstatic[MPD_MINALLOC_MAX]; + mpd_uint_t *u = ustatic; + mpd_uint_t *v = vstatic; + mpd_uint_t d, qhat, rhat, w2[2]; + mpd_uint_t hi, lo, x; + mpd_uint_t carry; + mpd_size_t i, j, m; + int retval = 0; + + assert(n > 1 && nplusm >= n); + m = sub_size_t(nplusm, n); + + /* D1: normalize */ + d = MPD_RADIX / (vconst[n-1] + 1); + + if (nplusm >= MPD_MINALLOC_MAX) { + if ((u = mpd_alloc(nplusm+1, sizeof *u)) == NULL) { + return -1; + } + } + if (n >= MPD_MINALLOC_MAX) { + if ((v = mpd_alloc(n+1, sizeof *v)) == NULL) { + mpd_free(u); + return -1; + } + } + + _mpd_shortmul(u, uconst, nplusm, d); + _mpd_shortmul(v, vconst, n, d); + + /* D2: loop */ + for (j=m; j != MPD_SIZE_MAX; j--) { assert(2 <= j+n && j+n <= nplusm); /* annotation for scan-build */ - - /* D3: calculate qhat and rhat */ - rhat = _mpd_shortdiv(w2, u+j+n-1, 2, v[n-1]); - qhat = w2[1] * MPD_RADIX + w2[0]; - - while (1) { - if (qhat < MPD_RADIX) { - _mpd_singlemul(w2, qhat, v[n-2]); - if (w2[1] <= rhat) { - if (w2[1] != rhat || w2[0] <= u[j+n-2]) { - break; - } - } - } - qhat -= 1; - rhat += v[n-1]; - if (rhat < v[n-1] || rhat >= MPD_RADIX) { - break; - } - } - /* D4: multiply and subtract */ - carry = 0; - for (i=0; i <= n; i++) { - - _mpd_mul_words(&hi, &lo, qhat, v[i]); - - lo = carry + lo; - if (lo < carry) hi++; - - _mpd_div_words_r(&hi, &lo, hi, lo); - - x = u[i+j] - lo; - carry = (u[i+j] < x); - u[i+j] = carry ? x+MPD_RADIX : x; - carry += hi; - } - q[j] = qhat; - /* D5: test remainder */ - if (carry) { - q[j] -= 1; - /* D6: add back */ - (void)_mpd_baseadd(u+j, u+j, v, n+1, n); - } - } - - /* D8: unnormalize */ - if (r != NULL) { - _mpd_shortdiv(r, u, n, d); - /* we are not interested in the return value here */ - retval = 0; - } - else { - retval = !_mpd_isallzero(u, n); - } - - -if (u != ustatic) mpd_free(u); -if (v != vstatic) mpd_free(v); -return retval; -} - -/* - * Left shift of src by 'shift' digits; src may equal dest. - * - * dest := area of n mpd_uint_t with space for srcdigits+shift digits. - * src := coefficient with length m. - * - * The case splits in the function are non-obvious. The following - * equations might help: - * - * Let msdigits denote the number of digits in the most significant - * word of src. Then 1 <= msdigits <= rdigits. - * - * 1) shift = q * rdigits + r - * 2) srcdigits = qsrc * rdigits + msdigits - * 3) destdigits = shift + srcdigits - * = q * rdigits + r + qsrc * rdigits + msdigits - * = q * rdigits + (qsrc * rdigits + (r + msdigits)) - * - * The result has q zero words, followed by the coefficient that - * is left-shifted by r. The case r == 0 is trivial. For r > 0, it - * is important to keep in mind that we always read m source words, - * but write m+1 destination words if r + msdigits > rdigits, m words - * otherwise. - */ -void -_mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, 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; - - assert(m > 0 && n >= m); - - _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, src[m--], MPD_RDIGITS-r); - if (h != 0) { /* r + msdigits > rdigits <==> h != 0 */ - dest[n--] = h; - } - /* write m-1 shifted words */ - for (; m != MPD_SIZE_MAX; m--,n--) { - _mpd_divmod_pow10(&h, &l, src[m], MPD_RDIGITS-r); - dest[n] = ph * lprev + h; - lprev = l; - } - /* write least significant word */ - dest[q] = ph * lprev; - } - else { - while (--m != MPD_SIZE_MAX) { - dest[m+q] = src[m]; - } - } - - mpd_uint_zero(dest, q); -} - -/* - * Right shift of src by 'shift' digits; src may equal dest. - * Assumption: srcdigits-shift > 0. - * - * dest := area with space for srcdigits-shift digits. - * src := coefficient with length 'slen'. - * - * The case splits in the function rely on the following equations: - * - * Let msdigits denote the number of digits in the most significant - * word of src. Then 1 <= msdigits <= rdigits. - * - * 1) shift = q * rdigits + r - * 2) srcdigits = qsrc * rdigits + msdigits - * 3) destdigits = srcdigits - shift - * = qsrc * rdigits + msdigits - (q * rdigits + r) - * = (qsrc - q) * rdigits + msdigits - r - * - * Since destdigits > 0 and 1 <= msdigits <= rdigits: - * - * 4) qsrc >= q - * 5) qsrc == q ==> msdigits > r - * - * The result has slen-q words if msdigits > r, slen-q-1 words otherwise. - */ -mpd_uint_t -_mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen, - mpd_size_t shift) -{ -#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) - /* spurious uninitialized warnings */ - mpd_uint_t l=l, h=h, hprev=hprev; /* low, high, previous high */ -#else - mpd_uint_t l, h, hprev; /* low, high, previous high */ -#endif - mpd_uint_t rnd, rest; /* rounding digit, rest */ - mpd_uint_t q, r; - mpd_size_t i, j; - mpd_uint_t ph; - - assert(slen > 0); - - _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS); - - rnd = rest = 0; - if (r != 0) { - - ph = mpd_pow10[MPD_RDIGITS-r]; - - _mpd_divmod_pow10(&hprev, &rest, src[q], r); - _mpd_divmod_pow10(&rnd, &rest, rest, r-1); - - if (rest == 0 && q > 0) { - rest = !_mpd_isallzero(src, q); - } - /* write slen-q-1 words */ - for (j=0,i=q+1; i<slen; i++,j++) { - _mpd_divmod_pow10(&h, &l, src[i], r); - dest[j] = ph * l + hprev; - hprev = h; - } - /* write most significant word */ - if (hprev != 0) { /* always the case if slen==q-1 */ - dest[j] = hprev; - } - } - else { - if (q > 0) { - _mpd_divmod_pow10(&rnd, &rest, src[q-1], MPD_RDIGITS-1); - /* is there any non-zero digit below rnd? */ - if (rest == 0) rest = !_mpd_isallzero(src, q-1); - } - for (j = 0; j < slen-q; j++) { - dest[j] = src[q+j]; - } - } - - /* 0-4 ==> rnd+rest < 0.5 */ - /* 5 ==> rnd+rest == 0.5 */ - /* 6-9 ==> rnd+rest > 0.5 */ - return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd; -} - - -/*********************************************************************/ -/* Calculations in base b */ -/*********************************************************************/ - -/* - * Add v to w (len m). The calling function has to handle a possible - * final carry. Assumption: m > 0. - */ -mpd_uint_t -_mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, mpd_uint_t b) -{ - mpd_uint_t s; - mpd_uint_t carry; - mpd_size_t i; - - assert(m > 0); - - /* add v to w */ - s = w[0] + v; - carry = (s < v) | (s >= b); - w[0] = carry ? s-b : s; - - /* if there is a carry, propagate it */ - for (i = 1; carry && i < m; i++) { - s = w[i] + carry; - carry = (s == b); - w[i] = carry ? 0 : s; - } - - return carry; -} - -/* w := product of u (len n) and v (single word). Return carry. */ -mpd_uint_t -_mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) -{ - mpd_uint_t hi, lo; - mpd_uint_t carry = 0; - mpd_size_t i; - - assert(n > 0); - - for (i=0; i < n; i++) { - - _mpd_mul_words(&hi, &lo, u[i], v); - lo = carry + lo; - if (lo < carry) hi++; - - _mpd_div_words_r(&carry, &w[i], hi, lo); - } - - return carry; -} - -/* w := product of u (len n) and v (single word) */ -mpd_uint_t -_mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v, mpd_uint_t b) -{ - mpd_uint_t hi, lo; - mpd_uint_t carry = 0; - mpd_size_t i; - - assert(n > 0); - - for (i=0; i < n; i++) { - - _mpd_mul_words(&hi, &lo, u[i], v); - lo = carry + lo; - if (lo < carry) hi++; - - _mpd_div_words(&carry, &w[i], hi, lo, b); - } - - return carry; -} - -/* - * Knuth, TAOCP Volume 2, 4.3.1, exercise 16: - * w := quotient of u (len n) divided by a single word v - */ -mpd_uint_t -_mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v, mpd_uint_t b) -{ - mpd_uint_t hi, lo; - mpd_uint_t rem = 0; - mpd_size_t i; - - assert(n > 0); - - for (i=n-1; i != MPD_SIZE_MAX; i--) { - - _mpd_mul_words(&hi, &lo, rem, b); - lo = u[i] + lo; - if (lo < u[i]) hi++; - - _mpd_div_words(&w[i], &rem, hi, lo, v); - } - - return rem; -} + + /* D3: calculate qhat and rhat */ + rhat = _mpd_shortdiv(w2, u+j+n-1, 2, v[n-1]); + qhat = w2[1] * MPD_RADIX + w2[0]; + + while (1) { + if (qhat < MPD_RADIX) { + _mpd_singlemul(w2, qhat, v[n-2]); + if (w2[1] <= rhat) { + if (w2[1] != rhat || w2[0] <= u[j+n-2]) { + break; + } + } + } + qhat -= 1; + rhat += v[n-1]; + if (rhat < v[n-1] || rhat >= MPD_RADIX) { + break; + } + } + /* D4: multiply and subtract */ + carry = 0; + for (i=0; i <= n; i++) { + + _mpd_mul_words(&hi, &lo, qhat, v[i]); + + lo = carry + lo; + if (lo < carry) hi++; + + _mpd_div_words_r(&hi, &lo, hi, lo); + + x = u[i+j] - lo; + carry = (u[i+j] < x); + u[i+j] = carry ? x+MPD_RADIX : x; + carry += hi; + } + q[j] = qhat; + /* D5: test remainder */ + if (carry) { + q[j] -= 1; + /* D6: add back */ + (void)_mpd_baseadd(u+j, u+j, v, n+1, n); + } + } + + /* D8: unnormalize */ + if (r != NULL) { + _mpd_shortdiv(r, u, n, d); + /* we are not interested in the return value here */ + retval = 0; + } + else { + retval = !_mpd_isallzero(u, n); + } + + +if (u != ustatic) mpd_free(u); +if (v != vstatic) mpd_free(v); +return retval; +} + +/* + * Left shift of src by 'shift' digits; src may equal dest. + * + * dest := area of n mpd_uint_t with space for srcdigits+shift digits. + * src := coefficient with length m. + * + * The case splits in the function are non-obvious. The following + * equations might help: + * + * Let msdigits denote the number of digits in the most significant + * word of src. Then 1 <= msdigits <= rdigits. + * + * 1) shift = q * rdigits + r + * 2) srcdigits = qsrc * rdigits + msdigits + * 3) destdigits = shift + srcdigits + * = q * rdigits + r + qsrc * rdigits + msdigits + * = q * rdigits + (qsrc * rdigits + (r + msdigits)) + * + * The result has q zero words, followed by the coefficient that + * is left-shifted by r. The case r == 0 is trivial. For r > 0, it + * is important to keep in mind that we always read m source words, + * but write m+1 destination words if r + msdigits > rdigits, m words + * otherwise. + */ +void +_mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, 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; + + assert(m > 0 && n >= m); + + _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, src[m--], MPD_RDIGITS-r); + if (h != 0) { /* r + msdigits > rdigits <==> h != 0 */ + dest[n--] = h; + } + /* write m-1 shifted words */ + for (; m != MPD_SIZE_MAX; m--,n--) { + _mpd_divmod_pow10(&h, &l, src[m], MPD_RDIGITS-r); + dest[n] = ph * lprev + h; + lprev = l; + } + /* write least significant word */ + dest[q] = ph * lprev; + } + else { + while (--m != MPD_SIZE_MAX) { + dest[m+q] = src[m]; + } + } + + mpd_uint_zero(dest, q); +} + +/* + * Right shift of src by 'shift' digits; src may equal dest. + * Assumption: srcdigits-shift > 0. + * + * dest := area with space for srcdigits-shift digits. + * src := coefficient with length 'slen'. + * + * The case splits in the function rely on the following equations: + * + * Let msdigits denote the number of digits in the most significant + * word of src. Then 1 <= msdigits <= rdigits. + * + * 1) shift = q * rdigits + r + * 2) srcdigits = qsrc * rdigits + msdigits + * 3) destdigits = srcdigits - shift + * = qsrc * rdigits + msdigits - (q * rdigits + r) + * = (qsrc - q) * rdigits + msdigits - r + * + * Since destdigits > 0 and 1 <= msdigits <= rdigits: + * + * 4) qsrc >= q + * 5) qsrc == q ==> msdigits > r + * + * The result has slen-q words if msdigits > r, slen-q-1 words otherwise. + */ +mpd_uint_t +_mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen, + mpd_size_t shift) +{ +#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) + /* spurious uninitialized warnings */ + mpd_uint_t l=l, h=h, hprev=hprev; /* low, high, previous high */ +#else + mpd_uint_t l, h, hprev; /* low, high, previous high */ +#endif + mpd_uint_t rnd, rest; /* rounding digit, rest */ + mpd_uint_t q, r; + mpd_size_t i, j; + mpd_uint_t ph; + + assert(slen > 0); + + _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS); + + rnd = rest = 0; + if (r != 0) { + + ph = mpd_pow10[MPD_RDIGITS-r]; + + _mpd_divmod_pow10(&hprev, &rest, src[q], r); + _mpd_divmod_pow10(&rnd, &rest, rest, r-1); + + if (rest == 0 && q > 0) { + rest = !_mpd_isallzero(src, q); + } + /* write slen-q-1 words */ + for (j=0,i=q+1; i<slen; i++,j++) { + _mpd_divmod_pow10(&h, &l, src[i], r); + dest[j] = ph * l + hprev; + hprev = h; + } + /* write most significant word */ + if (hprev != 0) { /* always the case if slen==q-1 */ + dest[j] = hprev; + } + } + else { + if (q > 0) { + _mpd_divmod_pow10(&rnd, &rest, src[q-1], MPD_RDIGITS-1); + /* is there any non-zero digit below rnd? */ + if (rest == 0) rest = !_mpd_isallzero(src, q-1); + } + for (j = 0; j < slen-q; j++) { + dest[j] = src[q+j]; + } + } + + /* 0-4 ==> rnd+rest < 0.5 */ + /* 5 ==> rnd+rest == 0.5 */ + /* 6-9 ==> rnd+rest > 0.5 */ + return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd; +} + + +/*********************************************************************/ +/* Calculations in base b */ +/*********************************************************************/ + +/* + * Add v to w (len m). The calling function has to handle a possible + * final carry. Assumption: m > 0. + */ +mpd_uint_t +_mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, mpd_uint_t b) +{ + mpd_uint_t s; + mpd_uint_t carry; + mpd_size_t i; + + assert(m > 0); + + /* add v to w */ + s = w[0] + v; + carry = (s < v) | (s >= b); + w[0] = carry ? s-b : s; + + /* if there is a carry, propagate it */ + for (i = 1; carry && i < m; i++) { + s = w[i] + carry; + carry = (s == b); + w[i] = carry ? 0 : s; + } + + return carry; +} + +/* w := product of u (len n) and v (single word). Return carry. */ +mpd_uint_t +_mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v) +{ + mpd_uint_t hi, lo; + mpd_uint_t carry = 0; + mpd_size_t i; + + assert(n > 0); + + for (i=0; i < n; i++) { + + _mpd_mul_words(&hi, &lo, u[i], v); + lo = carry + lo; + if (lo < carry) hi++; + + _mpd_div_words_r(&carry, &w[i], hi, lo); + } + + return carry; +} + +/* w := product of u (len n) and v (single word) */ +mpd_uint_t +_mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v, mpd_uint_t b) +{ + mpd_uint_t hi, lo; + mpd_uint_t carry = 0; + mpd_size_t i; + + assert(n > 0); + + for (i=0; i < n; i++) { + + _mpd_mul_words(&hi, &lo, u[i], v); + lo = carry + lo; + if (lo < carry) hi++; + + _mpd_div_words(&carry, &w[i], hi, lo, b); + } + + return carry; +} + +/* + * Knuth, TAOCP Volume 2, 4.3.1, exercise 16: + * w := quotient of u (len n) divided by a single word v + */ +mpd_uint_t +_mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v, mpd_uint_t b) +{ + mpd_uint_t hi, lo; + mpd_uint_t rem = 0; + mpd_size_t i; + + assert(n > 0); + + for (i=n-1; i != MPD_SIZE_MAX; i--) { + + _mpd_mul_words(&hi, &lo, rem, b); + lo = u[i] + lo; + if (lo < u[i]) hi++; + + _mpd_div_words(&w[i], &rem, hi, lo, v); + } + + return rem; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.h index d35925aaddb..651b0645691 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/basearith.h @@ -1,218 +1,218 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_BASEARITH_H_ #define LIBMPDEC_BASEARITH_H_ - - -#include "mpdecimal.h" -#include "typearith.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n); -void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); -mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v); -mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, - mpd_uint_t b); -mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n); -void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n); -void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); -void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t m, mpd_size_t n); -void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v); -mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v); -mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v, mpd_uint_t b); -mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v); -mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, - mpd_uint_t v, mpd_uint_t b); -int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst, - const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n); -void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n, - mpd_size_t m, mpd_size_t shift); -mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen, - mpd_size_t shift); - - - -#ifdef CONFIG_64 -extern const mpd_uint_t mprime_rdx; - -/* - * Algorithm from: Division by Invariant Integers using Multiplication, - * T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '94 - * Conference on Programming Language Design and Implementation. - * - * http://gmplib.org/~tege/divcnst-pldi94.pdf - * - * Variables from the paper and their translations (See section 8): - * - * N := 64 - * d := MPD_RADIX - * l := 64 - * m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**64 - * - * Since N-l == 0: - * - * dnorm := d - * n2 := hi - * n10 := lo - * - * ACL2 proof: mpd-div-words-r-correct - */ -static inline void -_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) -{ - mpd_uint_t n_adj, h, l, t; - mpd_uint_t n1_neg; - - /* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */ - n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0; - /* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */ - n_adj = lo + (n1_neg & MPD_RADIX); - - /* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */ - _mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg); - l = l + n_adj; - if (l < n_adj) h++; - t = h + hi; - /* At this point t == qest, with q == qest or q == qest+1: - * 1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX - */ - - /* t = 2**64-1 - qest = 2**64 - (qest+1) */ - t = MPD_UINT_MAX - t; - - /* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */ - _mpd_mul_words(&h, &l, t, MPD_RADIX); - l = l + lo; - if (l < lo) h++; - h += hi; - h -= MPD_RADIX; - /* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128) - * Case q == qest+1: - * a) h == 0, l == r - * b) q := h - t == qest+1 - * c) r := l - * Case q == qest: - * a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r) - * b) q := h - t == qest - * c) r := l + MPD_RADIX = r - */ - - *q = (h - t); - *r = l + (MPD_RADIX & h); -} -#else -static inline void -_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) -{ - _mpd_div_words(q, r, hi, lo, MPD_RADIX); -} -#endif - - -/* Multiply two single base MPD_RADIX words, store result in array w[2]. */ -static inline void -_mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v) -{ - mpd_uint_t hi, lo; - - _mpd_mul_words(&hi, &lo, u, v); - _mpd_div_words_r(&w[1], &w[0], hi, lo); -} - -/* Multiply u (len 2) and v (len m, 1 <= m <= 2). */ -static inline void -_mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m) -{ - mpd_uint_t hi, lo; - - _mpd_mul_words(&hi, &lo, u[0], v[0]); - _mpd_div_words_r(&w[1], &w[0], hi, lo); - - _mpd_mul_words(&hi, &lo, u[1], v[0]); - lo = w[1] + lo; - if (lo < w[1]) hi++; - _mpd_div_words_r(&w[2], &w[1], hi, lo); - if (m == 1) return; - - _mpd_mul_words(&hi, &lo, u[0], v[1]); - lo = w[1] + lo; - if (lo < w[1]) hi++; - _mpd_div_words_r(&w[3], &w[1], hi, lo); - - _mpd_mul_words(&hi, &lo, u[1], v[1]); - lo = w[2] + lo; - if (lo < w[2]) hi++; - lo = w[3] + lo; - if (lo < w[3]) hi++; - _mpd_div_words_r(&w[3], &w[2], hi, lo); -} - - -/* - * Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing - * is tested and the coefficient is regarded as "all zero". - */ -static inline int -_mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len) -{ - while (--len >= 0) { - if (data[len] != 0) return 0; - } - return 1; -} - -/* - * Test if all full words from data[len-1] to data[0] are MPD_RADIX-1 - * (all nines). Return true if len == 0. - */ -static inline int -_mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len) -{ - while (--len >= 0) { - if (data[len] != MPD_RADIX-1) return 0; - } - return 1; -} - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" +#include "typearith.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n); +void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); +mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v); +mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, + mpd_uint_t b); +mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n); +void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n); +void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n); +void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t m, mpd_size_t n); +void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v); +mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v); +mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v, mpd_uint_t b); +mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v); +mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, + mpd_uint_t v, mpd_uint_t b); +int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst, + const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n); +void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n, + mpd_size_t m, mpd_size_t shift); +mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen, + mpd_size_t shift); + + + +#ifdef CONFIG_64 +extern const mpd_uint_t mprime_rdx; + +/* + * Algorithm from: Division by Invariant Integers using Multiplication, + * T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '94 + * Conference on Programming Language Design and Implementation. + * + * http://gmplib.org/~tege/divcnst-pldi94.pdf + * + * Variables from the paper and their translations (See section 8): + * + * N := 64 + * d := MPD_RADIX + * l := 64 + * m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**64 + * + * Since N-l == 0: + * + * dnorm := d + * n2 := hi + * n10 := lo + * + * ACL2 proof: mpd-div-words-r-correct + */ +static inline void +_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) +{ + mpd_uint_t n_adj, h, l, t; + mpd_uint_t n1_neg; + + /* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */ + n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0; + /* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */ + n_adj = lo + (n1_neg & MPD_RADIX); + + /* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */ + _mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg); + l = l + n_adj; + if (l < n_adj) h++; + t = h + hi; + /* At this point t == qest, with q == qest or q == qest+1: + * 1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX + */ + + /* t = 2**64-1 - qest = 2**64 - (qest+1) */ + t = MPD_UINT_MAX - t; + + /* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */ + _mpd_mul_words(&h, &l, t, MPD_RADIX); + l = l + lo; + if (l < lo) h++; + h += hi; + h -= MPD_RADIX; + /* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128) + * Case q == qest+1: + * a) h == 0, l == r + * b) q := h - t == qest+1 + * c) r := l + * Case q == qest: + * a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r) + * b) q := h - t == qest + * c) r := l + MPD_RADIX = r + */ + + *q = (h - t); + *r = l + (MPD_RADIX & h); +} +#else +static inline void +_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo) +{ + _mpd_div_words(q, r, hi, lo, MPD_RADIX); +} +#endif + + +/* Multiply two single base MPD_RADIX words, store result in array w[2]. */ +static inline void +_mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v) +{ + mpd_uint_t hi, lo; + + _mpd_mul_words(&hi, &lo, u, v); + _mpd_div_words_r(&w[1], &w[0], hi, lo); +} + +/* Multiply u (len 2) and v (len m, 1 <= m <= 2). */ +static inline void +_mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m) +{ + mpd_uint_t hi, lo; + + _mpd_mul_words(&hi, &lo, u[0], v[0]); + _mpd_div_words_r(&w[1], &w[0], hi, lo); + + _mpd_mul_words(&hi, &lo, u[1], v[0]); + lo = w[1] + lo; + if (lo < w[1]) hi++; + _mpd_div_words_r(&w[2], &w[1], hi, lo); + if (m == 1) return; + + _mpd_mul_words(&hi, &lo, u[0], v[1]); + lo = w[1] + lo; + if (lo < w[1]) hi++; + _mpd_div_words_r(&w[3], &w[1], hi, lo); + + _mpd_mul_words(&hi, &lo, u[1], v[1]); + lo = w[2] + lo; + if (lo < w[2]) hi++; + lo = w[3] + lo; + if (lo < w[3]) hi++; + _mpd_div_words_r(&w[3], &w[2], hi, lo); +} + + +/* + * Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing + * is tested and the coefficient is regarded as "all zero". + */ +static inline int +_mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len) +{ + while (--len >= 0) { + if (data[len] != 0) return 0; + } + return 1; +} + +/* + * Test if all full words from data[len-1] to data[0] are MPD_RADIX-1 + * (all nines). Return true if len == 0. + */ +static inline int +_mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len) +{ + while (--len >= 0) { + if (data[len] != MPD_RADIX-1) return 0; + } + return 1; +} + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_BASEARITH_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/bits.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/bits.h index aa9c3e77980..743b0e3b792 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/bits.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/bits.h @@ -1,188 +1,188 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_BITS_H_ #define LIBMPDEC_BITS_H_ - - -#include "mpdecimal.h" - - -/* Check if n is a power of 2. */ -static inline int -ispower2(mpd_size_t n) -{ - return n != 0 && (n & (n-1)) == 0; -} - -#if defined(ANSI) -/* - * Return the most significant bit position of n from 0 to 31 (63). - * Assumptions: n != 0. - */ -static inline int -mpd_bsr(mpd_size_t n) -{ - int pos = 0; - mpd_size_t tmp; - -#ifdef CONFIG_64 - tmp = n >> 32; - if (tmp != 0) { n = tmp; pos += 32; } -#endif - tmp = n >> 16; - if (tmp != 0) { n = tmp; pos += 16; } - tmp = n >> 8; - if (tmp != 0) { n = tmp; pos += 8; } - tmp = n >> 4; - if (tmp != 0) { n = tmp; pos += 4; } - tmp = n >> 2; - if (tmp != 0) { n = tmp; pos += 2; } - tmp = n >> 1; - if (tmp != 0) { n = tmp; pos += 1; } - - return pos + (int)n - 1; -} - -/* - * Return the least significant bit position of n from 0 to 31 (63). - * Assumptions: n != 0. - */ -static inline int -mpd_bsf(mpd_size_t n) -{ - int pos; - -#ifdef CONFIG_64 - pos = 63; - if (n & 0x00000000FFFFFFFFULL) { pos -= 32; } else { n >>= 32; } - if (n & 0x000000000000FFFFULL) { pos -= 16; } else { n >>= 16; } - if (n & 0x00000000000000FFULL) { pos -= 8; } else { n >>= 8; } - if (n & 0x000000000000000FULL) { pos -= 4; } else { n >>= 4; } - if (n & 0x0000000000000003ULL) { pos -= 2; } else { n >>= 2; } - if (n & 0x0000000000000001ULL) { pos -= 1; } -#else - pos = 31; - if (n & 0x000000000000FFFFUL) { pos -= 16; } else { n >>= 16; } - if (n & 0x00000000000000FFUL) { pos -= 8; } else { n >>= 8; } - if (n & 0x000000000000000FUL) { pos -= 4; } else { n >>= 4; } - if (n & 0x0000000000000003UL) { pos -= 2; } else { n >>= 2; } - if (n & 0x0000000000000001UL) { pos -= 1; } -#endif - return pos; -} -/* END ANSI */ - -#elif defined(ASM) -/* - * Bit scan reverse. Assumptions: a != 0. - */ -static inline int -mpd_bsr(mpd_size_t a) -{ - mpd_size_t retval; - - __asm__ ( -#ifdef CONFIG_64 - "bsrq %1, %0\n\t" -#else - "bsr %1, %0\n\t" -#endif - :"=r" (retval) - :"r" (a) - :"cc" - ); - - return (int)retval; -} - -/* - * Bit scan forward. Assumptions: a != 0. - */ -static inline int -mpd_bsf(mpd_size_t a) -{ - mpd_size_t retval; - - __asm__ ( -#ifdef CONFIG_64 - "bsfq %1, %0\n\t" -#else - "bsf %1, %0\n\t" -#endif - :"=r" (retval) - :"r" (a) - :"cc" - ); - - return (int)retval; -} -/* END ASM */ - -#elif defined(MASM) -#include <intrin.h> -/* - * Bit scan reverse. Assumptions: a != 0. - */ -static inline int __cdecl -mpd_bsr(mpd_size_t a) -{ - unsigned long retval; - -#ifdef CONFIG_64 - _BitScanReverse64(&retval, a); -#else - _BitScanReverse(&retval, a); -#endif - - return (int)retval; -} - -/* - * Bit scan forward. Assumptions: a != 0. - */ -static inline int __cdecl -mpd_bsf(mpd_size_t a) -{ - unsigned long retval; - -#ifdef CONFIG_64 - _BitScanForward64(&retval, a); -#else - _BitScanForward(&retval, a); -#endif - - return (int)retval; -} -/* END MASM (_MSC_VER) */ -#else - #error "missing preprocessor definitions" -#endif /* BSR/BSF */ - - + + +#include "mpdecimal.h" + + +/* Check if n is a power of 2. */ +static inline int +ispower2(mpd_size_t n) +{ + return n != 0 && (n & (n-1)) == 0; +} + +#if defined(ANSI) +/* + * Return the most significant bit position of n from 0 to 31 (63). + * Assumptions: n != 0. + */ +static inline int +mpd_bsr(mpd_size_t n) +{ + int pos = 0; + mpd_size_t tmp; + +#ifdef CONFIG_64 + tmp = n >> 32; + if (tmp != 0) { n = tmp; pos += 32; } +#endif + tmp = n >> 16; + if (tmp != 0) { n = tmp; pos += 16; } + tmp = n >> 8; + if (tmp != 0) { n = tmp; pos += 8; } + tmp = n >> 4; + if (tmp != 0) { n = tmp; pos += 4; } + tmp = n >> 2; + if (tmp != 0) { n = tmp; pos += 2; } + tmp = n >> 1; + if (tmp != 0) { n = tmp; pos += 1; } + + return pos + (int)n - 1; +} + +/* + * Return the least significant bit position of n from 0 to 31 (63). + * Assumptions: n != 0. + */ +static inline int +mpd_bsf(mpd_size_t n) +{ + int pos; + +#ifdef CONFIG_64 + pos = 63; + if (n & 0x00000000FFFFFFFFULL) { pos -= 32; } else { n >>= 32; } + if (n & 0x000000000000FFFFULL) { pos -= 16; } else { n >>= 16; } + if (n & 0x00000000000000FFULL) { pos -= 8; } else { n >>= 8; } + if (n & 0x000000000000000FULL) { pos -= 4; } else { n >>= 4; } + if (n & 0x0000000000000003ULL) { pos -= 2; } else { n >>= 2; } + if (n & 0x0000000000000001ULL) { pos -= 1; } +#else + pos = 31; + if (n & 0x000000000000FFFFUL) { pos -= 16; } else { n >>= 16; } + if (n & 0x00000000000000FFUL) { pos -= 8; } else { n >>= 8; } + if (n & 0x000000000000000FUL) { pos -= 4; } else { n >>= 4; } + if (n & 0x0000000000000003UL) { pos -= 2; } else { n >>= 2; } + if (n & 0x0000000000000001UL) { pos -= 1; } +#endif + return pos; +} +/* END ANSI */ + +#elif defined(ASM) +/* + * Bit scan reverse. Assumptions: a != 0. + */ +static inline int +mpd_bsr(mpd_size_t a) +{ + mpd_size_t retval; + + __asm__ ( +#ifdef CONFIG_64 + "bsrq %1, %0\n\t" +#else + "bsr %1, %0\n\t" +#endif + :"=r" (retval) + :"r" (a) + :"cc" + ); + + return (int)retval; +} + +/* + * Bit scan forward. Assumptions: a != 0. + */ +static inline int +mpd_bsf(mpd_size_t a) +{ + mpd_size_t retval; + + __asm__ ( +#ifdef CONFIG_64 + "bsfq %1, %0\n\t" +#else + "bsf %1, %0\n\t" +#endif + :"=r" (retval) + :"r" (a) + :"cc" + ); + + return (int)retval; +} +/* END ASM */ + +#elif defined(MASM) +#include <intrin.h> +/* + * Bit scan reverse. Assumptions: a != 0. + */ +static inline int __cdecl +mpd_bsr(mpd_size_t a) +{ + unsigned long retval; + +#ifdef CONFIG_64 + _BitScanReverse64(&retval, a); +#else + _BitScanReverse(&retval, a); +#endif + + return (int)retval; +} + +/* + * Bit scan forward. Assumptions: a != 0. + */ +static inline int __cdecl +mpd_bsf(mpd_size_t a) +{ + unsigned long retval; + +#ifdef CONFIG_64 + _BitScanForward64(&retval, a); +#else + _BitScanForward(&retval, a); +#endif + + return (int)retval; +} +/* END MASM (_MSC_VER) */ +#else + #error "missing preprocessor definitions" +#endif /* BSR/BSF */ + + #endif /* LIBMPDEC_BITS_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.c index 4c4de622bc6..3f6ca647055 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.c @@ -1,129 +1,129 @@ -/* +/* * Copyright (c) 2008-2020 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 "constants.h" - - -#if defined(CONFIG_64) - - /* number-theory.c */ - const mpd_uint_t mpd_moduli[3] = { - 18446744069414584321ULL, 18446744056529682433ULL, 18446742974197923841ULL - }; - const mpd_uint_t mpd_roots[3] = {7ULL, 10ULL, 19ULL}; - - /* crt.c */ - const mpd_uint_t INV_P1_MOD_P2 = 18446744055098026669ULL; - const mpd_uint_t INV_P1P2_MOD_P3 = 287064143708160ULL; - const mpd_uint_t LH_P1P2 = 18446744052234715137ULL; /* (P1*P2) % 2^64 */ - const mpd_uint_t UH_P1P2 = 18446744052234715141ULL; /* (P1*P2) / 2^64 */ - - /* transpose.c */ - const mpd_size_t mpd_bits[64] = { - 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, - 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, - 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, - 2147483648ULL, 4294967296ULL, 8589934592ULL, 17179869184ULL, 34359738368ULL, - 68719476736ULL, 137438953472ULL, 274877906944ULL, 549755813888ULL, - 1099511627776ULL, 2199023255552ULL, 4398046511104, 8796093022208ULL, - 17592186044416ULL, 35184372088832ULL, 70368744177664ULL, 140737488355328ULL, - 281474976710656ULL, 562949953421312ULL, 1125899906842624ULL, - 2251799813685248ULL, 4503599627370496ULL, 9007199254740992ULL, - 18014398509481984ULL, 36028797018963968ULL, 72057594037927936ULL, - 144115188075855872ULL, 288230376151711744ULL, 576460752303423488ULL, - 1152921504606846976ULL, 2305843009213693952ULL, 4611686018427387904ULL, - 9223372036854775808ULL - }; - - /* mpdecimal.c */ - const mpd_uint_t mpd_pow10[MPD_RDIGITS+1] = { - 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000, - 10000000000ULL,100000000000ULL,1000000000000ULL,10000000000000ULL, - 100000000000000ULL,1000000000000000ULL,10000000000000000ULL, - 100000000000000000ULL,1000000000000000000ULL,10000000000000000000ULL - }; - - /* magic number for constant division by MPD_RADIX */ - const mpd_uint_t mprime_rdx = 15581492618384294730ULL; - -#elif defined(CONFIG_32) - - /* number-theory.c */ - const mpd_uint_t mpd_moduli[3] = {2113929217UL, 2013265921UL, 1811939329UL}; - const mpd_uint_t mpd_roots[3] = {5UL, 31UL, 13UL}; - - /* PentiumPro modular multiplication: These constants have to be loaded as - * 80 bit long doubles, which are not supported by certain compilers. */ - const uint32_t mpd_invmoduli[3][3] = { - {4293885170U, 2181570688U, 16352U}, /* ((long double) 1 / 2113929217UL) */ - {1698898177U, 2290649223U, 16352U}, /* ((long double) 1 / 2013265921UL) */ - {2716021846U, 2545165803U, 16352U} /* ((long double) 1 / 1811939329UL) */ - }; - - const float MPD_TWO63 = 9223372036854775808.0; /* 2^63 */ - - /* crt.c */ - const mpd_uint_t INV_P1_MOD_P2 = 2013265901UL; - const mpd_uint_t INV_P1P2_MOD_P3 = 54UL; - const mpd_uint_t LH_P1P2 = 4127195137UL; /* (P1*P2) % 2^32 */ - const mpd_uint_t UH_P1P2 = 990904320UL; /* (P1*P2) / 2^32 */ - - /* transpose.c */ - const mpd_size_t mpd_bits[32] = { - 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, - 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, - 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, - 2147483648UL - }; - - /* mpdecimal.c */ - const mpd_uint_t mpd_pow10[MPD_RDIGITS+1] = { - 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 - }; - -#else - #error "CONFIG_64 or CONFIG_32 must be defined." -#endif - -const char *mpd_round_string[MPD_ROUND_GUARD] = { - "ROUND_UP", /* round away from 0 */ - "ROUND_DOWN", /* round toward 0 (truncate) */ - "ROUND_CEILING", /* round toward +infinity */ - "ROUND_FLOOR", /* round toward -infinity */ - "ROUND_HALF_UP", /* 0.5 is rounded up */ - "ROUND_HALF_DOWN", /* 0.5 is rounded down */ - "ROUND_HALF_EVEN", /* 0.5 is rounded to even */ - "ROUND_05UP", /* round zero or five away from 0 */ - "ROUND_TRUNC", /* truncate, but set infinity */ -}; - -const char *mpd_clamp_string[MPD_CLAMP_GUARD] = { - "CLAMP_DEFAULT", - "CLAMP_IEEE_754" -}; + * + * 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 "constants.h" + + +#if defined(CONFIG_64) + + /* number-theory.c */ + const mpd_uint_t mpd_moduli[3] = { + 18446744069414584321ULL, 18446744056529682433ULL, 18446742974197923841ULL + }; + const mpd_uint_t mpd_roots[3] = {7ULL, 10ULL, 19ULL}; + + /* crt.c */ + const mpd_uint_t INV_P1_MOD_P2 = 18446744055098026669ULL; + const mpd_uint_t INV_P1P2_MOD_P3 = 287064143708160ULL; + const mpd_uint_t LH_P1P2 = 18446744052234715137ULL; /* (P1*P2) % 2^64 */ + const mpd_uint_t UH_P1P2 = 18446744052234715141ULL; /* (P1*P2) / 2^64 */ + + /* transpose.c */ + const mpd_size_t mpd_bits[64] = { + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, + 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, + 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, + 2147483648ULL, 4294967296ULL, 8589934592ULL, 17179869184ULL, 34359738368ULL, + 68719476736ULL, 137438953472ULL, 274877906944ULL, 549755813888ULL, + 1099511627776ULL, 2199023255552ULL, 4398046511104, 8796093022208ULL, + 17592186044416ULL, 35184372088832ULL, 70368744177664ULL, 140737488355328ULL, + 281474976710656ULL, 562949953421312ULL, 1125899906842624ULL, + 2251799813685248ULL, 4503599627370496ULL, 9007199254740992ULL, + 18014398509481984ULL, 36028797018963968ULL, 72057594037927936ULL, + 144115188075855872ULL, 288230376151711744ULL, 576460752303423488ULL, + 1152921504606846976ULL, 2305843009213693952ULL, 4611686018427387904ULL, + 9223372036854775808ULL + }; + + /* mpdecimal.c */ + const mpd_uint_t mpd_pow10[MPD_RDIGITS+1] = { + 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000, + 10000000000ULL,100000000000ULL,1000000000000ULL,10000000000000ULL, + 100000000000000ULL,1000000000000000ULL,10000000000000000ULL, + 100000000000000000ULL,1000000000000000000ULL,10000000000000000000ULL + }; + + /* magic number for constant division by MPD_RADIX */ + const mpd_uint_t mprime_rdx = 15581492618384294730ULL; + +#elif defined(CONFIG_32) + + /* number-theory.c */ + const mpd_uint_t mpd_moduli[3] = {2113929217UL, 2013265921UL, 1811939329UL}; + const mpd_uint_t mpd_roots[3] = {5UL, 31UL, 13UL}; + + /* PentiumPro modular multiplication: These constants have to be loaded as + * 80 bit long doubles, which are not supported by certain compilers. */ + const uint32_t mpd_invmoduli[3][3] = { + {4293885170U, 2181570688U, 16352U}, /* ((long double) 1 / 2113929217UL) */ + {1698898177U, 2290649223U, 16352U}, /* ((long double) 1 / 2013265921UL) */ + {2716021846U, 2545165803U, 16352U} /* ((long double) 1 / 1811939329UL) */ + }; + + const float MPD_TWO63 = 9223372036854775808.0; /* 2^63 */ + + /* crt.c */ + const mpd_uint_t INV_P1_MOD_P2 = 2013265901UL; + const mpd_uint_t INV_P1P2_MOD_P3 = 54UL; + const mpd_uint_t LH_P1P2 = 4127195137UL; /* (P1*P2) % 2^32 */ + const mpd_uint_t UH_P1P2 = 990904320UL; /* (P1*P2) / 2^32 */ + + /* transpose.c */ + const mpd_size_t mpd_bits[32] = { + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, + 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, + 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, + 2147483648UL + }; + + /* mpdecimal.c */ + const mpd_uint_t mpd_pow10[MPD_RDIGITS+1] = { + 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000 + }; + +#else + #error "CONFIG_64 or CONFIG_32 must be defined." +#endif + +const char *mpd_round_string[MPD_ROUND_GUARD] = { + "ROUND_UP", /* round away from 0 */ + "ROUND_DOWN", /* round toward 0 (truncate) */ + "ROUND_CEILING", /* round toward +infinity */ + "ROUND_FLOOR", /* round toward -infinity */ + "ROUND_HALF_UP", /* 0.5 is rounded up */ + "ROUND_HALF_DOWN", /* 0.5 is rounded down */ + "ROUND_HALF_EVEN", /* 0.5 is rounded to even */ + "ROUND_05UP", /* round zero or five away from 0 */ + "ROUND_TRUNC", /* truncate, but set infinity */ +}; + +const char *mpd_clamp_string[MPD_CLAMP_GUARD] = { + "CLAMP_DEFAULT", + "CLAMP_IEEE_754" +}; diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.h index 7c1db839c20..889d899b7a8 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/constants.h @@ -1,89 +1,89 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_CONSTANTS_H_ #define LIBMPDEC_CONSTANTS_H_ - - -#include "mpdecimal.h" - + + +#include "mpdecimal.h" + #include <stdint.h> + - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -/* choice of optimized functions */ -#if defined(CONFIG_64) -/* x64 */ - #define MULMOD(a, b) x64_mulmod(a, b, umod) - #define MULMOD2C(a0, a1, w) x64_mulmod2c(a0, a1, w, umod) - #define MULMOD2(a0, b0, a1, b1) x64_mulmod2(a0, b0, a1, b1, umod) - #define POWMOD(base, exp) x64_powmod(base, exp, umod) - #define SETMODULUS(modnum) std_setmodulus(modnum, &umod) - #define SIZE3_NTT(x0, x1, x2, w3table) std_size3_ntt(x0, x1, x2, w3table, umod) -#elif defined(PPRO) -/* PentiumPro (or later) gcc inline asm */ - #define MULMOD(a, b) ppro_mulmod(a, b, &dmod, dinvmod) - #define MULMOD2C(a0, a1, w) ppro_mulmod2c(a0, a1, w, &dmod, dinvmod) - #define MULMOD2(a0, b0, a1, b1) ppro_mulmod2(a0, b0, a1, b1, &dmod, dinvmod) - #define POWMOD(base, exp) ppro_powmod(base, exp, &dmod, dinvmod) - #define SETMODULUS(modnum) ppro_setmodulus(modnum, &umod, &dmod, dinvmod) - #define SIZE3_NTT(x0, x1, x2, w3table) ppro_size3_ntt(x0, x1, x2, w3table, umod, &dmod, dinvmod) -#else - /* ANSI C99 */ - #define MULMOD(a, b) std_mulmod(a, b, umod) - #define MULMOD2C(a0, a1, w) std_mulmod2c(a0, a1, w, umod) - #define MULMOD2(a0, b0, a1, b1) std_mulmod2(a0, b0, a1, b1, umod) - #define POWMOD(base, exp) std_powmod(base, exp, umod) - #define SETMODULUS(modnum) std_setmodulus(modnum, &umod) - #define SIZE3_NTT(x0, x1, x2, w3table) std_size3_ntt(x0, x1, x2, w3table, umod) -#endif - -/* PentiumPro (or later) gcc inline asm */ -extern const float MPD_TWO63; -extern const uint32_t mpd_invmoduli[3][3]; - -enum {P1, P2, P3}; - -extern const mpd_uint_t mpd_moduli[]; -extern const mpd_uint_t mpd_roots[]; -extern const mpd_size_t mpd_bits[]; -extern const mpd_uint_t mpd_pow10[]; - -extern const mpd_uint_t INV_P1_MOD_P2; -extern const mpd_uint_t INV_P1P2_MOD_P3; -extern const mpd_uint_t LH_P1P2; -extern const mpd_uint_t UH_P1P2; - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +/* choice of optimized functions */ +#if defined(CONFIG_64) +/* x64 */ + #define MULMOD(a, b) x64_mulmod(a, b, umod) + #define MULMOD2C(a0, a1, w) x64_mulmod2c(a0, a1, w, umod) + #define MULMOD2(a0, b0, a1, b1) x64_mulmod2(a0, b0, a1, b1, umod) + #define POWMOD(base, exp) x64_powmod(base, exp, umod) + #define SETMODULUS(modnum) std_setmodulus(modnum, &umod) + #define SIZE3_NTT(x0, x1, x2, w3table) std_size3_ntt(x0, x1, x2, w3table, umod) +#elif defined(PPRO) +/* PentiumPro (or later) gcc inline asm */ + #define MULMOD(a, b) ppro_mulmod(a, b, &dmod, dinvmod) + #define MULMOD2C(a0, a1, w) ppro_mulmod2c(a0, a1, w, &dmod, dinvmod) + #define MULMOD2(a0, b0, a1, b1) ppro_mulmod2(a0, b0, a1, b1, &dmod, dinvmod) + #define POWMOD(base, exp) ppro_powmod(base, exp, &dmod, dinvmod) + #define SETMODULUS(modnum) ppro_setmodulus(modnum, &umod, &dmod, dinvmod) + #define SIZE3_NTT(x0, x1, x2, w3table) ppro_size3_ntt(x0, x1, x2, w3table, umod, &dmod, dinvmod) +#else + /* ANSI C99 */ + #define MULMOD(a, b) std_mulmod(a, b, umod) + #define MULMOD2C(a0, a1, w) std_mulmod2c(a0, a1, w, umod) + #define MULMOD2(a0, b0, a1, b1) std_mulmod2(a0, b0, a1, b1, umod) + #define POWMOD(base, exp) std_powmod(base, exp, umod) + #define SETMODULUS(modnum) std_setmodulus(modnum, &umod) + #define SIZE3_NTT(x0, x1, x2, w3table) std_size3_ntt(x0, x1, x2, w3table, umod) +#endif + +/* PentiumPro (or later) gcc inline asm */ +extern const float MPD_TWO63; +extern const uint32_t mpd_invmoduli[3][3]; + +enum {P1, P2, P3}; + +extern const mpd_uint_t mpd_moduli[]; +extern const mpd_uint_t mpd_roots[]; +extern const mpd_size_t mpd_bits[]; +extern const mpd_uint_t mpd_pow10[]; + +extern const mpd_uint_t INV_P1_MOD_P2; +extern const mpd_uint_t INV_P1P2_MOD_P3; +extern const mpd_uint_t LH_P1P2; +extern const mpd_uint_t UH_P1P2; + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_CONSTANTS_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/context.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/context.c index 9cbc2050959..c711e0918f9 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/context.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/context.c @@ -1,286 +1,286 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <signal.h> -#include <stdio.h> -#include <string.h> - - -void +#include <stdio.h> +#include <string.h> + + +void mpd_dflt_traphandler(mpd_context_t *ctx) -{ +{ (void)ctx; - raise(SIGFPE); -} - -void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler; - - -/* Set guaranteed minimum number of coefficient words. The function may - be used once at program start. Setting MPD_MINALLOC to out-of-bounds - values is a catastrophic error, so in that case the function exits rather - than relying on the user to check a return value. */ -void -mpd_setminalloc(mpd_ssize_t n) -{ - static int minalloc_is_set = 0; - - if (minalloc_is_set) { - mpd_err_warn("mpd_setminalloc: ignoring request to set " - "MPD_MINALLOC a second time\n"); - return; - } - if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) { - mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */ - } - MPD_MINALLOC = n; - minalloc_is_set = 1; -} - -void -mpd_init(mpd_context_t *ctx, mpd_ssize_t prec) -{ - mpd_ssize_t ideal_minalloc; - - mpd_defaultcontext(ctx); - - if (!mpd_qsetprec(ctx, prec)) { - mpd_addstatus_raise(ctx, MPD_Invalid_context); - return; - } - - ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS); - if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN; - if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX; - - mpd_setminalloc(ideal_minalloc); -} - -void -mpd_maxcontext(mpd_context_t *ctx) -{ - ctx->prec=MPD_MAX_PREC; - ctx->emax=MPD_MAX_EMAX; - ctx->emin=MPD_MIN_EMIN; - ctx->round=MPD_ROUND_HALF_EVEN; - ctx->traps=MPD_Traps; - ctx->status=0; - ctx->newtrap=0; - ctx->clamp=0; - ctx->allcr=1; -} - -void -mpd_defaultcontext(mpd_context_t *ctx) -{ - ctx->prec=2*MPD_RDIGITS; - ctx->emax=MPD_MAX_EMAX; - ctx->emin=MPD_MIN_EMIN; - ctx->round=MPD_ROUND_HALF_UP; - ctx->traps=MPD_Traps; - ctx->status=0; - ctx->newtrap=0; - ctx->clamp=0; - ctx->allcr=1; -} - -void -mpd_basiccontext(mpd_context_t *ctx) -{ - ctx->prec=9; - ctx->emax=MPD_MAX_EMAX; - ctx->emin=MPD_MIN_EMIN; - ctx->round=MPD_ROUND_HALF_UP; - ctx->traps=MPD_Traps|MPD_Clamped; - ctx->status=0; - ctx->newtrap=0; - ctx->clamp=0; - ctx->allcr=1; -} - -int -mpd_ieee_context(mpd_context_t *ctx, int bits) -{ - if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) { - return -1; - } - - ctx->prec = 9 * (bits/32) - 2; - ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3)); - ctx->emin = 1 - ctx->emax; - ctx->round=MPD_ROUND_HALF_EVEN; - ctx->traps=0; - ctx->status=0; - ctx->newtrap=0; - ctx->clamp=1; - ctx->allcr=1; - - return 0; -} - -mpd_ssize_t -mpd_getprec(const mpd_context_t *ctx) -{ - return ctx->prec; -} - -mpd_ssize_t -mpd_getemax(const mpd_context_t *ctx) -{ - return ctx->emax; -} - -mpd_ssize_t -mpd_getemin(const mpd_context_t *ctx) -{ - return ctx->emin; -} - -int -mpd_getround(const mpd_context_t *ctx) -{ - return ctx->round; -} - -uint32_t -mpd_gettraps(const mpd_context_t *ctx) -{ - return ctx->traps; -} - -uint32_t -mpd_getstatus(const mpd_context_t *ctx) -{ - return ctx->status; -} - -int -mpd_getclamp(const mpd_context_t *ctx) -{ - return ctx->clamp; -} - -int -mpd_getcr(const mpd_context_t *ctx) -{ - return ctx->allcr; -} - - -int -mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec) -{ - if (prec <= 0 || prec > MPD_MAX_PREC) { - return 0; - } - ctx->prec = prec; - return 1; -} - -int -mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax) -{ - if (emax < 0 || emax > MPD_MAX_EMAX) { - return 0; - } - ctx->emax = emax; - return 1; -} - -int -mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin) -{ - if (emin > 0 || emin < MPD_MIN_EMIN) { - return 0; - } - ctx->emin = emin; - return 1; -} - -int -mpd_qsetround(mpd_context_t *ctx, int round) -{ - if (!(0 <= round && round < MPD_ROUND_GUARD)) { - return 0; - } - ctx->round = round; - return 1; -} - -int -mpd_qsettraps(mpd_context_t *ctx, uint32_t traps) -{ - if (traps > MPD_Max_status) { - return 0; - } - ctx->traps = traps; - return 1; -} - -int -mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags) -{ - if (flags > MPD_Max_status) { - return 0; - } - ctx->status = flags; - return 1; -} - -int -mpd_qsetclamp(mpd_context_t *ctx, int c) -{ - if (c != 0 && c != 1) { - return 0; - } - ctx->clamp = c; - return 1; -} - -int -mpd_qsetcr(mpd_context_t *ctx, int c) -{ - if (c != 0 && c != 1) { - return 0; - } - ctx->allcr = c; - return 1; -} - - -void -mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags) -{ - ctx->status |= flags; - if (flags&ctx->traps) { - ctx->newtrap = (flags&ctx->traps); - mpd_traphandler(ctx); - } -} + raise(SIGFPE); +} + +void (* mpd_traphandler)(mpd_context_t *) = mpd_dflt_traphandler; + + +/* Set guaranteed minimum number of coefficient words. The function may + be used once at program start. Setting MPD_MINALLOC to out-of-bounds + values is a catastrophic error, so in that case the function exits rather + than relying on the user to check a return value. */ +void +mpd_setminalloc(mpd_ssize_t n) +{ + static int minalloc_is_set = 0; + + if (minalloc_is_set) { + mpd_err_warn("mpd_setminalloc: ignoring request to set " + "MPD_MINALLOC a second time\n"); + return; + } + if (n < MPD_MINALLOC_MIN || n > MPD_MINALLOC_MAX) { + mpd_err_fatal("illegal value for MPD_MINALLOC"); /* GCOV_NOT_REACHED */ + } + MPD_MINALLOC = n; + minalloc_is_set = 1; +} + +void +mpd_init(mpd_context_t *ctx, mpd_ssize_t prec) +{ + mpd_ssize_t ideal_minalloc; + + mpd_defaultcontext(ctx); + + if (!mpd_qsetprec(ctx, prec)) { + mpd_addstatus_raise(ctx, MPD_Invalid_context); + return; + } + + ideal_minalloc = 2 * ((prec+MPD_RDIGITS-1) / MPD_RDIGITS); + if (ideal_minalloc < MPD_MINALLOC_MIN) ideal_minalloc = MPD_MINALLOC_MIN; + if (ideal_minalloc > MPD_MINALLOC_MAX) ideal_minalloc = MPD_MINALLOC_MAX; + + mpd_setminalloc(ideal_minalloc); +} + +void +mpd_maxcontext(mpd_context_t *ctx) +{ + ctx->prec=MPD_MAX_PREC; + ctx->emax=MPD_MAX_EMAX; + ctx->emin=MPD_MIN_EMIN; + ctx->round=MPD_ROUND_HALF_EVEN; + ctx->traps=MPD_Traps; + ctx->status=0; + ctx->newtrap=0; + ctx->clamp=0; + ctx->allcr=1; +} + +void +mpd_defaultcontext(mpd_context_t *ctx) +{ + ctx->prec=2*MPD_RDIGITS; + ctx->emax=MPD_MAX_EMAX; + ctx->emin=MPD_MIN_EMIN; + ctx->round=MPD_ROUND_HALF_UP; + ctx->traps=MPD_Traps; + ctx->status=0; + ctx->newtrap=0; + ctx->clamp=0; + ctx->allcr=1; +} + +void +mpd_basiccontext(mpd_context_t *ctx) +{ + ctx->prec=9; + ctx->emax=MPD_MAX_EMAX; + ctx->emin=MPD_MIN_EMIN; + ctx->round=MPD_ROUND_HALF_UP; + ctx->traps=MPD_Traps|MPD_Clamped; + ctx->status=0; + ctx->newtrap=0; + ctx->clamp=0; + ctx->allcr=1; +} + +int +mpd_ieee_context(mpd_context_t *ctx, int bits) +{ + if (bits <= 0 || bits > MPD_IEEE_CONTEXT_MAX_BITS || bits % 32) { + return -1; + } + + ctx->prec = 9 * (bits/32) - 2; + ctx->emax = 3 * ((mpd_ssize_t)1<<(bits/16+3)); + ctx->emin = 1 - ctx->emax; + ctx->round=MPD_ROUND_HALF_EVEN; + ctx->traps=0; + ctx->status=0; + ctx->newtrap=0; + ctx->clamp=1; + ctx->allcr=1; + + return 0; +} + +mpd_ssize_t +mpd_getprec(const mpd_context_t *ctx) +{ + return ctx->prec; +} + +mpd_ssize_t +mpd_getemax(const mpd_context_t *ctx) +{ + return ctx->emax; +} + +mpd_ssize_t +mpd_getemin(const mpd_context_t *ctx) +{ + return ctx->emin; +} + +int +mpd_getround(const mpd_context_t *ctx) +{ + return ctx->round; +} + +uint32_t +mpd_gettraps(const mpd_context_t *ctx) +{ + return ctx->traps; +} + +uint32_t +mpd_getstatus(const mpd_context_t *ctx) +{ + return ctx->status; +} + +int +mpd_getclamp(const mpd_context_t *ctx) +{ + return ctx->clamp; +} + +int +mpd_getcr(const mpd_context_t *ctx) +{ + return ctx->allcr; +} + + +int +mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec) +{ + if (prec <= 0 || prec > MPD_MAX_PREC) { + return 0; + } + ctx->prec = prec; + return 1; +} + +int +mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax) +{ + if (emax < 0 || emax > MPD_MAX_EMAX) { + return 0; + } + ctx->emax = emax; + return 1; +} + +int +mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin) +{ + if (emin > 0 || emin < MPD_MIN_EMIN) { + return 0; + } + ctx->emin = emin; + return 1; +} + +int +mpd_qsetround(mpd_context_t *ctx, int round) +{ + if (!(0 <= round && round < MPD_ROUND_GUARD)) { + return 0; + } + ctx->round = round; + return 1; +} + +int +mpd_qsettraps(mpd_context_t *ctx, uint32_t traps) +{ + if (traps > MPD_Max_status) { + return 0; + } + ctx->traps = traps; + return 1; +} + +int +mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags) +{ + if (flags > MPD_Max_status) { + return 0; + } + ctx->status = flags; + return 1; +} + +int +mpd_qsetclamp(mpd_context_t *ctx, int c) +{ + if (c != 0 && c != 1) { + return 0; + } + ctx->clamp = c; + return 1; +} + +int +mpd_qsetcr(mpd_context_t *ctx, int c) +{ + if (c != 0 && c != 1) { + return 0; + } + ctx->allcr = c; + return 1; +} + + +void +mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags) +{ + ctx->status |= flags; + if (flags&ctx->traps) { + ctx->newtrap = (flags&ctx->traps); + mpd_traphandler(ctx); + } +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.c index 4bc8e8b5fd3..4323d3c6e1c 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.c @@ -1,171 +1,171 @@ -/* +/* * Copyright (c) 2008-2020 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 "bits.h" -#include "constants.h" + * + * 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 "bits.h" +#include "constants.h" #include "convolute.h" -#include "fnt.h" -#include "fourstep.h" -#include "numbertheory.h" -#include "sixstep.h" -#include "umodarith.h" - - -/* Bignum: Fast convolution using the Number Theoretic Transform. Used for - the multiplication of very large coefficients. */ - - -/* Convolute the data in c1 and c2. Result is in c1. */ -int -fnt_convolute(mpd_uint_t *c1, mpd_uint_t *c2, mpd_size_t n, int modnum) -{ - int (*fnt)(mpd_uint_t *, mpd_size_t, int); - int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int); -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t n_inv, umod; - mpd_size_t i; - - - SETMODULUS(modnum); - n_inv = POWMOD(n, (umod-2)); - - if (ispower2(n)) { - if (n > SIX_STEP_THRESHOLD) { - fnt = six_step_fnt; - inv_fnt = inv_six_step_fnt; - } - else { - fnt = std_fnt; - inv_fnt = std_inv_fnt; - } - } - else { - fnt = four_step_fnt; - inv_fnt = inv_four_step_fnt; - } - - if (!fnt(c1, n, modnum)) { - return 0; - } - if (!fnt(c2, n, modnum)) { - return 0; - } - for (i = 0; i < n-1; i += 2) { - mpd_uint_t x0 = c1[i]; - mpd_uint_t y0 = c2[i]; - mpd_uint_t x1 = c1[i+1]; - mpd_uint_t y1 = c2[i+1]; - MULMOD2(&x0, y0, &x1, y1); - c1[i] = x0; - c1[i+1] = x1; - } - - if (!inv_fnt(c1, n, modnum)) { - return 0; - } - for (i = 0; i < n-3; i += 4) { - mpd_uint_t x0 = c1[i]; - mpd_uint_t x1 = c1[i+1]; - mpd_uint_t x2 = c1[i+2]; - mpd_uint_t x3 = c1[i+3]; - MULMOD2C(&x0, &x1, n_inv); - MULMOD2C(&x2, &x3, n_inv); - c1[i] = x0; - c1[i+1] = x1; - c1[i+2] = x2; - c1[i+3] = x3; - } - - return 1; -} - -/* Autoconvolute the data in c1. Result is in c1. */ -int -fnt_autoconvolute(mpd_uint_t *c1, mpd_size_t n, int modnum) -{ - int (*fnt)(mpd_uint_t *, mpd_size_t, int); - int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int); -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t n_inv, umod; - mpd_size_t i; - - - SETMODULUS(modnum); - n_inv = POWMOD(n, (umod-2)); - - if (ispower2(n)) { - if (n > SIX_STEP_THRESHOLD) { - fnt = six_step_fnt; - inv_fnt = inv_six_step_fnt; - } - else { - fnt = std_fnt; - inv_fnt = std_inv_fnt; - } - } - else { - fnt = four_step_fnt; - inv_fnt = inv_four_step_fnt; - } - - if (!fnt(c1, n, modnum)) { - return 0; - } - for (i = 0; i < n-1; i += 2) { - mpd_uint_t x0 = c1[i]; - mpd_uint_t x1 = c1[i+1]; - MULMOD2(&x0, x0, &x1, x1); - c1[i] = x0; - c1[i+1] = x1; - } - - if (!inv_fnt(c1, n, modnum)) { - return 0; - } - for (i = 0; i < n-3; i += 4) { - mpd_uint_t x0 = c1[i]; - mpd_uint_t x1 = c1[i+1]; - mpd_uint_t x2 = c1[i+2]; - mpd_uint_t x3 = c1[i+3]; - MULMOD2C(&x0, &x1, n_inv); - MULMOD2C(&x2, &x3, n_inv); - c1[i] = x0; - c1[i+1] = x1; - c1[i+2] = x2; - c1[i+3] = x3; - } - - return 1; -} +#include "fnt.h" +#include "fourstep.h" +#include "numbertheory.h" +#include "sixstep.h" +#include "umodarith.h" + + +/* Bignum: Fast convolution using the Number Theoretic Transform. Used for + the multiplication of very large coefficients. */ + + +/* Convolute the data in c1 and c2. Result is in c1. */ +int +fnt_convolute(mpd_uint_t *c1, mpd_uint_t *c2, mpd_size_t n, int modnum) +{ + int (*fnt)(mpd_uint_t *, mpd_size_t, int); + int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int); +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t n_inv, umod; + mpd_size_t i; + + + SETMODULUS(modnum); + n_inv = POWMOD(n, (umod-2)); + + if (ispower2(n)) { + if (n > SIX_STEP_THRESHOLD) { + fnt = six_step_fnt; + inv_fnt = inv_six_step_fnt; + } + else { + fnt = std_fnt; + inv_fnt = std_inv_fnt; + } + } + else { + fnt = four_step_fnt; + inv_fnt = inv_four_step_fnt; + } + + if (!fnt(c1, n, modnum)) { + return 0; + } + if (!fnt(c2, n, modnum)) { + return 0; + } + for (i = 0; i < n-1; i += 2) { + mpd_uint_t x0 = c1[i]; + mpd_uint_t y0 = c2[i]; + mpd_uint_t x1 = c1[i+1]; + mpd_uint_t y1 = c2[i+1]; + MULMOD2(&x0, y0, &x1, y1); + c1[i] = x0; + c1[i+1] = x1; + } + + if (!inv_fnt(c1, n, modnum)) { + return 0; + } + for (i = 0; i < n-3; i += 4) { + mpd_uint_t x0 = c1[i]; + mpd_uint_t x1 = c1[i+1]; + mpd_uint_t x2 = c1[i+2]; + mpd_uint_t x3 = c1[i+3]; + MULMOD2C(&x0, &x1, n_inv); + MULMOD2C(&x2, &x3, n_inv); + c1[i] = x0; + c1[i+1] = x1; + c1[i+2] = x2; + c1[i+3] = x3; + } + + return 1; +} + +/* Autoconvolute the data in c1. Result is in c1. */ +int +fnt_autoconvolute(mpd_uint_t *c1, mpd_size_t n, int modnum) +{ + int (*fnt)(mpd_uint_t *, mpd_size_t, int); + int (*inv_fnt)(mpd_uint_t *, mpd_size_t, int); +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t n_inv, umod; + mpd_size_t i; + + + SETMODULUS(modnum); + n_inv = POWMOD(n, (umod-2)); + + if (ispower2(n)) { + if (n > SIX_STEP_THRESHOLD) { + fnt = six_step_fnt; + inv_fnt = inv_six_step_fnt; + } + else { + fnt = std_fnt; + inv_fnt = std_inv_fnt; + } + } + else { + fnt = four_step_fnt; + inv_fnt = inv_four_step_fnt; + } + + if (!fnt(c1, n, modnum)) { + return 0; + } + for (i = 0; i < n-1; i += 2) { + mpd_uint_t x0 = c1[i]; + mpd_uint_t x1 = c1[i+1]; + MULMOD2(&x0, x0, &x1, x1); + c1[i] = x0; + c1[i+1] = x1; + } + + if (!inv_fnt(c1, n, modnum)) { + return 0; + } + for (i = 0; i < n-3; i += 4) { + mpd_uint_t x0 = c1[i]; + mpd_uint_t x1 = c1[i+1]; + mpd_uint_t x2 = c1[i+2]; + mpd_uint_t x3 = c1[i+3]; + MULMOD2C(&x0, &x1, n_inv); + MULMOD2C(&x2, &x3, n_inv); + c1[i] = x0; + c1[i+1] = x1; + c1[i+2] = x2; + c1[i+3] = x3; + } + + return 1; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.h index 62edb3e4573..85b6eb67785 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/convolute.h @@ -1,49 +1,49 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_CONVOLUTE_H_ #define LIBMPDEC_CONVOLUTE_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -#define SIX_STEP_THRESHOLD 4096 - -int fnt_convolute(mpd_uint_t *c1, mpd_uint_t *c2, mpd_size_t n, int modnum); -int fnt_autoconvolute(mpd_uint_t *c1, mpd_size_t n, int modnum); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +#define SIX_STEP_THRESHOLD 4096 + +int fnt_convolute(mpd_uint_t *c1, mpd_uint_t *c2, mpd_size_t n, int modnum); +int fnt_autoconvolute(mpd_uint_t *c1, mpd_size_t n, int modnum); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_CONVOLUTE_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.c index 613274ee0c5..0a89d311123 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.c @@ -1,180 +1,180 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ + * + * 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 "mpdecimal.h" - -#include <assert.h> +#include <assert.h> #include "constants.h" #include "crt.h" -#include "numbertheory.h" -#include "umodarith.h" +#include "numbertheory.h" +#include "umodarith.h" #include "typearith.h" - - -/* Bignum: Chinese Remainder Theorem, extends the maximum transform length. */ - - -/* Multiply P1P2 by v, store result in w. */ -static inline void -_crt_mulP1P2_3(mpd_uint_t w[3], mpd_uint_t v) -{ - mpd_uint_t hi1, hi2, lo; - - _mpd_mul_words(&hi1, &lo, LH_P1P2, v); - w[0] = lo; - - _mpd_mul_words(&hi2, &lo, UH_P1P2, v); - lo = hi1 + lo; - if (lo < hi1) hi2++; - - w[1] = lo; - w[2] = hi2; -} - -/* Add 3 words from v to w. The result is known to fit in w. */ -static inline void -_crt_add3(mpd_uint_t w[3], mpd_uint_t v[3]) -{ - mpd_uint_t carry; - mpd_uint_t s; - - s = w[0] + v[0]; - carry = (s < w[0]); - w[0] = s; - - s = w[1] + (v[1] + carry); - carry = (s < w[1]); - w[1] = s; - - w[2] = w[2] + (v[2] + carry); -} - -/* Divide 3 words in u by v, store result in w, return remainder. */ -static inline mpd_uint_t -_crt_div3(mpd_uint_t *w, const mpd_uint_t *u, mpd_uint_t v) -{ - mpd_uint_t r1 = u[2]; - mpd_uint_t r2; - - if (r1 < v) { - w[2] = 0; - } - else { - _mpd_div_word(&w[2], &r1, u[2], v); /* GCOV_NOT_REACHED */ - } - - _mpd_div_words(&w[1], &r2, r1, u[1], v); - _mpd_div_words(&w[0], &r1, r2, u[0], v); - - return r1; -} - - -/* - * Chinese Remainder Theorem: - * Algorithm from Joerg Arndt, "Matters Computational", - * Chapter 37.4.1 [http://www.jjj.de/fxt/] - * - * See also Knuth, TAOCP, Volume 2, 4.3.2, exercise 7. - */ - -/* - * CRT with carry: x1, x2, x3 contain numbers modulo p1, p2, p3. For each - * triple of members of the arrays, find the unique z modulo p1*p2*p3, with - * zmax = p1*p2*p3 - 1. - * - * In each iteration of the loop, split z into result[i] = z % MPD_RADIX - * and carry = z / MPD_RADIX. Let N be the size of carry[] and cmax the - * maximum carry. - * - * Limits for the 32-bit build: - * - * N = 2**96 - * cmax = 7711435591312380274 - * - * Limits for the 64 bit build: - * - * N = 2**192 - * cmax = 627710135393475385904124401220046371710 - * - * The following statements hold for both versions: - * - * 1) cmax + zmax < N, so the addition does not overflow. - * - * 2) (cmax + zmax) / MPD_RADIX == cmax. - * - * 3) If c <= cmax, then c_next = (c + zmax) / MPD_RADIX <= cmax. - */ -void -crt3(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_size_t rsize) -{ - mpd_uint_t p1 = mpd_moduli[P1]; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t a1, a2, a3; - mpd_uint_t s; - mpd_uint_t z[3], t[3]; - mpd_uint_t carry[3] = {0,0,0}; - mpd_uint_t hi, lo; - mpd_size_t i; - - for (i = 0; i < rsize; i++) { - - a1 = x1[i]; - a2 = x2[i]; - a3 = x3[i]; - - SETMODULUS(P2); - s = ext_submod(a2, a1, umod); - s = MULMOD(s, INV_P1_MOD_P2); - - _mpd_mul_words(&hi, &lo, s, p1); - lo = lo + a1; - if (lo < a1) hi++; - - SETMODULUS(P3); - s = dw_submod(a3, hi, lo, umod); - s = MULMOD(s, INV_P1P2_MOD_P3); - - z[0] = lo; - z[1] = hi; - z[2] = 0; - - _crt_mulP1P2_3(t, s); - _crt_add3(z, t); - _crt_add3(carry, z); - - x1[i] = _crt_div3(carry, carry, MPD_RADIX); - } - - assert(carry[0] == 0 && carry[1] == 0 && carry[2] == 0); -} + + +/* Bignum: Chinese Remainder Theorem, extends the maximum transform length. */ + + +/* Multiply P1P2 by v, store result in w. */ +static inline void +_crt_mulP1P2_3(mpd_uint_t w[3], mpd_uint_t v) +{ + mpd_uint_t hi1, hi2, lo; + + _mpd_mul_words(&hi1, &lo, LH_P1P2, v); + w[0] = lo; + + _mpd_mul_words(&hi2, &lo, UH_P1P2, v); + lo = hi1 + lo; + if (lo < hi1) hi2++; + + w[1] = lo; + w[2] = hi2; +} + +/* Add 3 words from v to w. The result is known to fit in w. */ +static inline void +_crt_add3(mpd_uint_t w[3], mpd_uint_t v[3]) +{ + mpd_uint_t carry; + mpd_uint_t s; + + s = w[0] + v[0]; + carry = (s < w[0]); + w[0] = s; + + s = w[1] + (v[1] + carry); + carry = (s < w[1]); + w[1] = s; + + w[2] = w[2] + (v[2] + carry); +} + +/* Divide 3 words in u by v, store result in w, return remainder. */ +static inline mpd_uint_t +_crt_div3(mpd_uint_t *w, const mpd_uint_t *u, mpd_uint_t v) +{ + mpd_uint_t r1 = u[2]; + mpd_uint_t r2; + + if (r1 < v) { + w[2] = 0; + } + else { + _mpd_div_word(&w[2], &r1, u[2], v); /* GCOV_NOT_REACHED */ + } + + _mpd_div_words(&w[1], &r2, r1, u[1], v); + _mpd_div_words(&w[0], &r1, r2, u[0], v); + + return r1; +} + + +/* + * Chinese Remainder Theorem: + * Algorithm from Joerg Arndt, "Matters Computational", + * Chapter 37.4.1 [http://www.jjj.de/fxt/] + * + * See also Knuth, TAOCP, Volume 2, 4.3.2, exercise 7. + */ + +/* + * CRT with carry: x1, x2, x3 contain numbers modulo p1, p2, p3. For each + * triple of members of the arrays, find the unique z modulo p1*p2*p3, with + * zmax = p1*p2*p3 - 1. + * + * In each iteration of the loop, split z into result[i] = z % MPD_RADIX + * and carry = z / MPD_RADIX. Let N be the size of carry[] and cmax the + * maximum carry. + * + * Limits for the 32-bit build: + * + * N = 2**96 + * cmax = 7711435591312380274 + * + * Limits for the 64 bit build: + * + * N = 2**192 + * cmax = 627710135393475385904124401220046371710 + * + * The following statements hold for both versions: + * + * 1) cmax + zmax < N, so the addition does not overflow. + * + * 2) (cmax + zmax) / MPD_RADIX == cmax. + * + * 3) If c <= cmax, then c_next = (c + zmax) / MPD_RADIX <= cmax. + */ +void +crt3(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_size_t rsize) +{ + mpd_uint_t p1 = mpd_moduli[P1]; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t a1, a2, a3; + mpd_uint_t s; + mpd_uint_t z[3], t[3]; + mpd_uint_t carry[3] = {0,0,0}; + mpd_uint_t hi, lo; + mpd_size_t i; + + for (i = 0; i < rsize; i++) { + + a1 = x1[i]; + a2 = x2[i]; + a3 = x3[i]; + + SETMODULUS(P2); + s = ext_submod(a2, a1, umod); + s = MULMOD(s, INV_P1_MOD_P2); + + _mpd_mul_words(&hi, &lo, s, p1); + lo = lo + a1; + if (lo < a1) hi++; + + SETMODULUS(P3); + s = dw_submod(a3, hi, lo, umod); + s = MULMOD(s, INV_P1P2_MOD_P3); + + z[0] = lo; + z[1] = hi; + z[2] = 0; + + _crt_mulP1P2_3(t, s); + _crt_add3(z, t); + _crt_add3(carry, z); + + x1[i] = _crt_div3(carry, carry, MPD_RADIX); + } + + assert(carry[0] == 0 && carry[1] == 0 && carry[2] == 0); +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.h index 15a347d4cb3..0c4880a8a2d 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/crt.h @@ -1,46 +1,46 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_CRT_H_ #define LIBMPDEC_CRT_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -void crt3(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_size_t nmemb); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +void crt3(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_size_t nmemb); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_CRT_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.c index 049ecff65b6..fb0657493cd 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.c @@ -1,173 +1,173 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ + * + * 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 <assert.h> -#include "mpdecimal.h" - -#include <assert.h> - -#include "bits.h" +#include "bits.h" #include "constants.h" #include "difradix2.h" -#include "numbertheory.h" -#include "umodarith.h" - - -/* Bignum: The actual transform routine (decimation in frequency). */ - - -/* - * Generate index pairs (x, bitreverse(x)) and carry out the permutation. - * n must be a power of two. - * Algorithm due to Brent/Lehmann, see Joerg Arndt, "Matters Computational", - * Chapter 1.14.4. [http://www.jjj.de/fxt/] - */ -static inline void -bitreverse_permute(mpd_uint_t a[], mpd_size_t n) -{ - mpd_size_t x = 0; - mpd_size_t r = 0; - mpd_uint_t t; - - do { /* Invariant: r = bitreverse(x) */ - if (r > x) { - t = a[x]; - a[x] = a[r]; - a[r] = t; - } - /* Flip trailing consecutive 1 bits and the first zero bit - * that absorbs a possible carry. */ - x += 1; - /* Mirror the operation on r: Flip n_trailing_zeros(x)+1 - high bits of r. */ - r ^= (n - (n >> (mpd_bsf(x)+1))); - /* The loop invariant is preserved. */ - } while (x < n); -} - - -/* Fast Number Theoretic Transform, decimation in frequency. */ -void -fnt_dif2(mpd_uint_t a[], mpd_size_t n, struct fnt_params *tparams) -{ - mpd_uint_t *wtable = tparams->wtable; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t u0, u1, v0, v1; - mpd_uint_t w, w0, w1, wstep; - mpd_size_t m, mhalf; - mpd_size_t j, r; - - - assert(ispower2(n)); - assert(n >= 4); - - SETMODULUS(tparams->modnum); - - /* m == n */ - mhalf = n / 2; - for (j = 0; j < mhalf; j += 2) { - - w0 = wtable[j]; - w1 = wtable[j+1]; - - u0 = a[j]; - v0 = a[j+mhalf]; - - u1 = a[j+1]; - v1 = a[j+1+mhalf]; - - a[j] = addmod(u0, v0, umod); - v0 = submod(u0, v0, umod); - - a[j+1] = addmod(u1, v1, umod); - v1 = submod(u1, v1, umod); - - MULMOD2(&v0, w0, &v1, w1); - - a[j+mhalf] = v0; - a[j+1+mhalf] = v1; - - } - - wstep = 2; - for (m = n/2; m >= 2; m>>=1, wstep<<=1) { - - mhalf = m / 2; - - /* j == 0 */ - for (r = 0; r < n; r += 2*m) { - - u0 = a[r]; - v0 = a[r+mhalf]; - - u1 = a[m+r]; - v1 = a[m+r+mhalf]; - - a[r] = addmod(u0, v0, umod); - v0 = submod(u0, v0, umod); - - a[m+r] = addmod(u1, v1, umod); - v1 = submod(u1, v1, umod); - - a[r+mhalf] = v0; - a[m+r+mhalf] = v1; - } - - for (j = 1; j < mhalf; j++) { - - w = wtable[j*wstep]; - - for (r = 0; r < n; r += 2*m) { - - u0 = a[r+j]; - v0 = a[r+j+mhalf]; - - u1 = a[m+r+j]; - v1 = a[m+r+j+mhalf]; - - a[r+j] = addmod(u0, v0, umod); - v0 = submod(u0, v0, umod); - - a[m+r+j] = addmod(u1, v1, umod); - v1 = submod(u1, v1, umod); - - MULMOD2C(&v0, &v1, w); - - a[r+j+mhalf] = v0; - a[m+r+j+mhalf] = v1; - } - - } - - } - - bitreverse_permute(a, n); -} +#include "numbertheory.h" +#include "umodarith.h" + + +/* Bignum: The actual transform routine (decimation in frequency). */ + + +/* + * Generate index pairs (x, bitreverse(x)) and carry out the permutation. + * n must be a power of two. + * Algorithm due to Brent/Lehmann, see Joerg Arndt, "Matters Computational", + * Chapter 1.14.4. [http://www.jjj.de/fxt/] + */ +static inline void +bitreverse_permute(mpd_uint_t a[], mpd_size_t n) +{ + mpd_size_t x = 0; + mpd_size_t r = 0; + mpd_uint_t t; + + do { /* Invariant: r = bitreverse(x) */ + if (r > x) { + t = a[x]; + a[x] = a[r]; + a[r] = t; + } + /* Flip trailing consecutive 1 bits and the first zero bit + * that absorbs a possible carry. */ + x += 1; + /* Mirror the operation on r: Flip n_trailing_zeros(x)+1 + high bits of r. */ + r ^= (n - (n >> (mpd_bsf(x)+1))); + /* The loop invariant is preserved. */ + } while (x < n); +} + + +/* Fast Number Theoretic Transform, decimation in frequency. */ +void +fnt_dif2(mpd_uint_t a[], mpd_size_t n, struct fnt_params *tparams) +{ + mpd_uint_t *wtable = tparams->wtable; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t u0, u1, v0, v1; + mpd_uint_t w, w0, w1, wstep; + mpd_size_t m, mhalf; + mpd_size_t j, r; + + + assert(ispower2(n)); + assert(n >= 4); + + SETMODULUS(tparams->modnum); + + /* m == n */ + mhalf = n / 2; + for (j = 0; j < mhalf; j += 2) { + + w0 = wtable[j]; + w1 = wtable[j+1]; + + u0 = a[j]; + v0 = a[j+mhalf]; + + u1 = a[j+1]; + v1 = a[j+1+mhalf]; + + a[j] = addmod(u0, v0, umod); + v0 = submod(u0, v0, umod); + + a[j+1] = addmod(u1, v1, umod); + v1 = submod(u1, v1, umod); + + MULMOD2(&v0, w0, &v1, w1); + + a[j+mhalf] = v0; + a[j+1+mhalf] = v1; + + } + + wstep = 2; + for (m = n/2; m >= 2; m>>=1, wstep<<=1) { + + mhalf = m / 2; + + /* j == 0 */ + for (r = 0; r < n; r += 2*m) { + + u0 = a[r]; + v0 = a[r+mhalf]; + + u1 = a[m+r]; + v1 = a[m+r+mhalf]; + + a[r] = addmod(u0, v0, umod); + v0 = submod(u0, v0, umod); + + a[m+r] = addmod(u1, v1, umod); + v1 = submod(u1, v1, umod); + + a[r+mhalf] = v0; + a[m+r+mhalf] = v1; + } + + for (j = 1; j < mhalf; j++) { + + w = wtable[j*wstep]; + + for (r = 0; r < n; r += 2*m) { + + u0 = a[r+j]; + v0 = a[r+j+mhalf]; + + u1 = a[m+r+j]; + v1 = a[m+r+j+mhalf]; + + a[r+j] = addmod(u0, v0, umod); + v0 = submod(u0, v0, umod); + + a[m+r+j] = addmod(u1, v1, umod); + v1 = submod(u1, v1, umod); + + MULMOD2C(&v0, &v1, w); + + a[r+j+mhalf] = v0; + a[m+r+j+mhalf] = v1; + } + + } + + } + + bitreverse_permute(a, n); +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.h index cdcbcf9a710..fb99acb7b6a 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/difradix2.h @@ -1,47 +1,47 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_DIFRADIX2_H_ #define LIBMPDEC_DIFRADIX2_H_ - - -#include "mpdecimal.h" -#include "numbertheory.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -void fnt_dif2(mpd_uint_t a[], mpd_size_t n, struct fnt_params *tparams); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" +#include "numbertheory.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +void fnt_dif2(mpd_uint_t a[], mpd_size_t n, struct fnt_params *tparams); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_DIFRADIX2_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.c index 0dbe98fc71c..2f69ba25573 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.c @@ -1,79 +1,79 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> -#include <stdio.h> +#include <stdio.h> -#include "bits.h" -#include "difradix2.h" +#include "bits.h" +#include "difradix2.h" #include "fnt.h" -#include "numbertheory.h" - - -/* Bignum: Fast transform for medium-sized coefficients. */ - - -/* forward transform, sign = -1 */ -int -std_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - struct fnt_params *tparams; - - assert(ispower2(n)); - assert(n >= 4); - assert(n <= 3*MPD_MAXTRANSFORM_2N); - - if ((tparams = _mpd_init_fnt_params(n, -1, modnum)) == NULL) { - return 0; - } - fnt_dif2(a, n, tparams); - - mpd_free(tparams); - return 1; -} - -/* reverse transform, sign = 1 */ -int -std_inv_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - struct fnt_params *tparams; - - assert(ispower2(n)); - assert(n >= 4); - assert(n <= 3*MPD_MAXTRANSFORM_2N); - - if ((tparams = _mpd_init_fnt_params(n, 1, modnum)) == NULL) { - return 0; - } - fnt_dif2(a, n, tparams); - - mpd_free(tparams); - return 1; -} +#include "numbertheory.h" + + +/* Bignum: Fast transform for medium-sized coefficients. */ + + +/* forward transform, sign = -1 */ +int +std_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + struct fnt_params *tparams; + + assert(ispower2(n)); + assert(n >= 4); + assert(n <= 3*MPD_MAXTRANSFORM_2N); + + if ((tparams = _mpd_init_fnt_params(n, -1, modnum)) == NULL) { + return 0; + } + fnt_dif2(a, n, tparams); + + mpd_free(tparams); + return 1; +} + +/* reverse transform, sign = 1 */ +int +std_inv_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + struct fnt_params *tparams; + + assert(ispower2(n)); + assert(n >= 4); + assert(n <= 3*MPD_MAXTRANSFORM_2N); + + if ((tparams = _mpd_init_fnt_params(n, 1, modnum)) == NULL) { + return 0; + } + fnt_dif2(a, n, tparams); + + mpd_free(tparams); + return 1; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.h index 5222c476a3a..22e7b924dd1 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fnt.h @@ -1,47 +1,47 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_FNT_H_ #define LIBMPDEC_FNT_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -int std_fnt(mpd_uint_t a[], mpd_size_t n, int modnum); -int std_inv_fnt(mpd_uint_t a[], mpd_size_t n, int modnum); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +int std_fnt(mpd_uint_t a[], mpd_size_t n, int modnum); +int std_inv_fnt(mpd_uint_t a[], mpd_size_t n, int modnum); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_FNT_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.c index fb173ed5a52..ff7694ea6df 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.c @@ -1,259 +1,259 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ + * + * 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 "mpdecimal.h" - -#include <assert.h> +#include <assert.h> #include "constants.h" #include "fourstep.h" -#include "numbertheory.h" -#include "sixstep.h" -#include "umodarith.h" - - -/* Bignum: Cache efficient Matrix Fourier Transform for arrays of the - form 3 * 2**n (See literature/matrix-transform.txt). */ - - -#ifndef PPRO -static inline void -std_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, - mpd_uint_t w3table[3], mpd_uint_t umod) -{ - mpd_uint_t r1, r2; - mpd_uint_t w; - mpd_uint_t s, tmp; - - - /* k = 0 -> w = 1 */ - s = *x1; - s = addmod(s, *x2, umod); - s = addmod(s, *x3, umod); - - r1 = s; - - /* k = 1 */ - s = *x1; - - w = w3table[1]; - tmp = MULMOD(*x2, w); - s = addmod(s, tmp, umod); - - w = w3table[2]; - tmp = MULMOD(*x3, w); - s = addmod(s, tmp, umod); - - r2 = s; - - /* k = 2 */ - s = *x1; - - w = w3table[2]; - tmp = MULMOD(*x2, w); - s = addmod(s, tmp, umod); - - w = w3table[1]; - tmp = MULMOD(*x3, w); - s = addmod(s, tmp, umod); - - *x3 = s; - *x2 = r2; - *x1 = r1; -} -#else /* PPRO */ -static inline void -ppro_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_uint_t w3table[3], - mpd_uint_t umod, double *dmod, uint32_t dinvmod[3]) -{ - mpd_uint_t r1, r2; - mpd_uint_t w; - mpd_uint_t s, tmp; - - - /* k = 0 -> w = 1 */ - s = *x1; - s = addmod(s, *x2, umod); - s = addmod(s, *x3, umod); - - r1 = s; - - /* k = 1 */ - s = *x1; - - w = w3table[1]; - tmp = ppro_mulmod(*x2, w, dmod, dinvmod); - s = addmod(s, tmp, umod); - - w = w3table[2]; - tmp = ppro_mulmod(*x3, w, dmod, dinvmod); - s = addmod(s, tmp, umod); - - r2 = s; - - /* k = 2 */ - s = *x1; - - w = w3table[2]; - tmp = ppro_mulmod(*x2, w, dmod, dinvmod); - s = addmod(s, tmp, umod); - - w = w3table[1]; - tmp = ppro_mulmod(*x3, w, dmod, dinvmod); - s = addmod(s, tmp, umod); - - *x3 = s; - *x2 = r2; - *x1 = r1; -} -#endif - - -/* forward transform, sign = -1; transform length = 3 * 2**n */ -int -four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - mpd_size_t R = 3; /* number of rows */ - mpd_size_t C = n / 3; /* number of columns */ - mpd_uint_t w3table[3]; - mpd_uint_t kernel, w0, w1, wstep; - mpd_uint_t *s, *p0, *p1, *p2; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_size_t i, k; - - - assert(n >= 48); - assert(n <= 3*MPD_MAXTRANSFORM_2N); - - - /* Length R transform on the columns. */ - SETMODULUS(modnum); - _mpd_init_w3table(w3table, -1, modnum); - for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) { - - SIZE3_NTT(p0, p1, p2, w3table); - } - - /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ - kernel = _mpd_getkernel(n, -1, modnum); - for (i = 1; i < R; i++) { - w0 = 1; /* r**(i*0): initial value for k=0 */ - w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */ - wstep = MULMOD(w1, w1); /* r**(2*i) */ - for (k = 0; k < C-1; k += 2) { - mpd_uint_t x0 = a[i*C+k]; - mpd_uint_t x1 = a[i*C+k+1]; - MULMOD2(&x0, w0, &x1, w1); - MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */ - a[i*C+k] = x0; - a[i*C+k+1] = x1; - } - } - - /* Length C transform on the rows. */ - for (s = a; s < a+n; s += C) { - if (!six_step_fnt(s, C, modnum)) { - return 0; - } - } - -#if 0 - /* An unordered transform is sufficient for convolution. */ - /* Transpose the matrix. */ +#include "numbertheory.h" +#include "sixstep.h" +#include "umodarith.h" + + +/* Bignum: Cache efficient Matrix Fourier Transform for arrays of the + form 3 * 2**n (See literature/matrix-transform.txt). */ + + +#ifndef PPRO +static inline void +std_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, + mpd_uint_t w3table[3], mpd_uint_t umod) +{ + mpd_uint_t r1, r2; + mpd_uint_t w; + mpd_uint_t s, tmp; + + + /* k = 0 -> w = 1 */ + s = *x1; + s = addmod(s, *x2, umod); + s = addmod(s, *x3, umod); + + r1 = s; + + /* k = 1 */ + s = *x1; + + w = w3table[1]; + tmp = MULMOD(*x2, w); + s = addmod(s, tmp, umod); + + w = w3table[2]; + tmp = MULMOD(*x3, w); + s = addmod(s, tmp, umod); + + r2 = s; + + /* k = 2 */ + s = *x1; + + w = w3table[2]; + tmp = MULMOD(*x2, w); + s = addmod(s, tmp, umod); + + w = w3table[1]; + tmp = MULMOD(*x3, w); + s = addmod(s, tmp, umod); + + *x3 = s; + *x2 = r2; + *x1 = r1; +} +#else /* PPRO */ +static inline void +ppro_size3_ntt(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_uint_t w3table[3], + mpd_uint_t umod, double *dmod, uint32_t dinvmod[3]) +{ + mpd_uint_t r1, r2; + mpd_uint_t w; + mpd_uint_t s, tmp; + + + /* k = 0 -> w = 1 */ + s = *x1; + s = addmod(s, *x2, umod); + s = addmod(s, *x3, umod); + + r1 = s; + + /* k = 1 */ + s = *x1; + + w = w3table[1]; + tmp = ppro_mulmod(*x2, w, dmod, dinvmod); + s = addmod(s, tmp, umod); + + w = w3table[2]; + tmp = ppro_mulmod(*x3, w, dmod, dinvmod); + s = addmod(s, tmp, umod); + + r2 = s; + + /* k = 2 */ + s = *x1; + + w = w3table[2]; + tmp = ppro_mulmod(*x2, w, dmod, dinvmod); + s = addmod(s, tmp, umod); + + w = w3table[1]; + tmp = ppro_mulmod(*x3, w, dmod, dinvmod); + s = addmod(s, tmp, umod); + + *x3 = s; + *x2 = r2; + *x1 = r1; +} +#endif + + +/* forward transform, sign = -1; transform length = 3 * 2**n */ +int +four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + mpd_size_t R = 3; /* number of rows */ + mpd_size_t C = n / 3; /* number of columns */ + mpd_uint_t w3table[3]; + mpd_uint_t kernel, w0, w1, wstep; + mpd_uint_t *s, *p0, *p1, *p2; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_size_t i, k; + + + assert(n >= 48); + assert(n <= 3*MPD_MAXTRANSFORM_2N); + + + /* Length R transform on the columns. */ + SETMODULUS(modnum); + _mpd_init_w3table(w3table, -1, modnum); + for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) { + + SIZE3_NTT(p0, p1, p2, w3table); + } + + /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ + kernel = _mpd_getkernel(n, -1, modnum); + for (i = 1; i < R; i++) { + w0 = 1; /* r**(i*0): initial value for k=0 */ + w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */ + wstep = MULMOD(w1, w1); /* r**(2*i) */ + for (k = 0; k < C-1; k += 2) { + mpd_uint_t x0 = a[i*C+k]; + mpd_uint_t x1 = a[i*C+k+1]; + MULMOD2(&x0, w0, &x1, w1); + MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */ + a[i*C+k] = x0; + a[i*C+k+1] = x1; + } + } + + /* Length C transform on the rows. */ + for (s = a; s < a+n; s += C) { + if (!six_step_fnt(s, C, modnum)) { + return 0; + } + } + +#if 0 + /* An unordered transform is sufficient for convolution. */ + /* Transpose the matrix. */ #include "transpose.h" - transpose_3xpow2(a, R, C); -#endif - - return 1; -} - -/* backward transform, sign = 1; transform length = 3 * 2**n */ -int -inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - mpd_size_t R = 3; /* number of rows */ - mpd_size_t C = n / 3; /* number of columns */ - mpd_uint_t w3table[3]; - mpd_uint_t kernel, w0, w1, wstep; - mpd_uint_t *s, *p0, *p1, *p2; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_size_t i, k; - - - assert(n >= 48); - assert(n <= 3*MPD_MAXTRANSFORM_2N); - - -#if 0 - /* An unordered transform is sufficient for convolution. */ - /* Transpose the matrix, producing an R*C matrix. */ + transpose_3xpow2(a, R, C); +#endif + + return 1; +} + +/* backward transform, sign = 1; transform length = 3 * 2**n */ +int +inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + mpd_size_t R = 3; /* number of rows */ + mpd_size_t C = n / 3; /* number of columns */ + mpd_uint_t w3table[3]; + mpd_uint_t kernel, w0, w1, wstep; + mpd_uint_t *s, *p0, *p1, *p2; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_size_t i, k; + + + assert(n >= 48); + assert(n <= 3*MPD_MAXTRANSFORM_2N); + + +#if 0 + /* An unordered transform is sufficient for convolution. */ + /* Transpose the matrix, producing an R*C matrix. */ #include "transpose.h" - transpose_3xpow2(a, C, R); -#endif - - /* Length C transform on the rows. */ - for (s = a; s < a+n; s += C) { - if (!inv_six_step_fnt(s, C, modnum)) { - return 0; - } - } - - /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ - SETMODULUS(modnum); - kernel = _mpd_getkernel(n, 1, modnum); - for (i = 1; i < R; i++) { - w0 = 1; - w1 = POWMOD(kernel, i); - wstep = MULMOD(w1, w1); - for (k = 0; k < C; k += 2) { - mpd_uint_t x0 = a[i*C+k]; - mpd_uint_t x1 = a[i*C+k+1]; - MULMOD2(&x0, w0, &x1, w1); - MULMOD2C(&w0, &w1, wstep); - a[i*C+k] = x0; - a[i*C+k+1] = x1; - } - } - - /* Length R transform on the columns. */ - _mpd_init_w3table(w3table, 1, modnum); - for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) { - - SIZE3_NTT(p0, p1, p2, w3table); - } - - return 1; -} + transpose_3xpow2(a, C, R); +#endif + + /* Length C transform on the rows. */ + for (s = a; s < a+n; s += C) { + if (!inv_six_step_fnt(s, C, modnum)) { + return 0; + } + } + + /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ + SETMODULUS(modnum); + kernel = _mpd_getkernel(n, 1, modnum); + for (i = 1; i < R; i++) { + w0 = 1; + w1 = POWMOD(kernel, i); + wstep = MULMOD(w1, w1); + for (k = 0; k < C; k += 2) { + mpd_uint_t x0 = a[i*C+k]; + mpd_uint_t x1 = a[i*C+k+1]; + MULMOD2(&x0, w0, &x1, w1); + MULMOD2C(&w0, &w1, wstep); + a[i*C+k] = x0; + a[i*C+k+1] = x1; + } + } + + /* Length R transform on the columns. */ + _mpd_init_w3table(w3table, 1, modnum); + for (p0=a, p1=p0+C, p2=p0+2*C; p0<a+C; p0++,p1++,p2++) { + + SIZE3_NTT(p0, p1, p2, w3table); + } + + return 1; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.h index 5ffb6fcc8ec..5308e87067b 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/fourstep.h @@ -1,47 +1,47 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_FOURSTEP_H_ #define LIBMPDEC_FOURSTEP_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -int four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); -int inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +int four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); +int inv_four_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_FOURSTEP_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/io.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/io.c index 03f2acfc1a7..b1c21f6b3c7 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/io.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/io.c @@ -1,331 +1,331 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> #include <ctype.h> #include <errno.h> #include <limits.h> #include <locale.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "typearith.h" -#include "mpd_io.h" - - -/* This file contains functions for decimal <-> string conversions, including - PEP-3101 formatting for numeric types. */ - - -/* 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 - - -/* - * Work around the behavior of tolower() and strcasecmp() in certain - * locales. For example, in tr_TR.utf8: - * - * tolower((unsigned char)'I') == 'I' - * - * u is the exact uppercase version of l; n is strlen(l) or strlen(l)+1 - */ -static inline int -_mpd_strneq(const char *s, const char *l, const char *u, size_t n) -{ - while (--n != SIZE_MAX) { - if (*s != *l && *s != *u) { - return 0; - } - s++; u++; l++; - } - - return 1; -} - -static mpd_ssize_t -strtoexp(const char *s) -{ - char *end; - mpd_ssize_t retval; - - errno = 0; - retval = mpd_strtossize(s, &end, 10); - if (errno == 0 && !(*s != '\0' && *end == '\0')) - errno = EINVAL; - - return retval; -} - -/* - * Scan 'len' words. The most significant word contains 'r' digits, - * the remaining words are full words. Skip dpoint. The string 's' must - * consist of digits and an optional single decimal point at 'dpoint'. - */ -static void -string_to_coeff(mpd_uint_t *data, const char *s, const char *dpoint, int r, - size_t len) -{ - int j; - - if (r > 0) { - data[--len] = 0; - for (j = 0; j < r; j++, s++) { - if (s == dpoint) s++; - data[len] = 10 * data[len] + (*s - '0'); - } - } - - while (--len != SIZE_MAX) { - data[len] = 0; - for (j = 0; j < MPD_RDIGITS; j++, s++) { - if (s == dpoint) s++; - data[len] = 10 * data[len] + (*s - '0'); - } - } -} - -/* - * Partially verify a numeric string of the form: - * - * [cdigits][.][cdigits][eE][+-][edigits] - * - * If successful, return a pointer to the location of the first - * relevant coefficient digit. This digit is either non-zero or - * part of one of the following patterns: - * - * ["0\x00", "0.\x00", "0.E", "0.e", "0E", "0e"] - * - * The locations of a single optional dot or indicator are stored - * in 'dpoint' and 'exp'. - * - * The end of the string is stored in 'end'. If an indicator [eE] - * occurs without trailing [edigits], the condition is caught - * later by strtoexp(). - */ -static const char * -scan_dpoint_exp(const char *s, const char **dpoint, const char **exp, - const char **end) -{ - const char *coeff = NULL; - - *dpoint = NULL; - *exp = NULL; - for (; *s != '\0'; s++) { - switch (*s) { - case '.': - if (*dpoint != NULL || *exp != NULL) - return NULL; - *dpoint = s; - break; - case 'E': case 'e': - if (*exp != NULL) - return NULL; - *exp = s; - if (*(s+1) == '+' || *(s+1) == '-') - s++; - break; - default: - if (!isdigit((uchar)*s)) - return NULL; - if (coeff == NULL && *exp == NULL) { - if (*s == '0') { - if (!isdigit((uchar)*(s+1))) - if (!(*(s+1) == '.' && - isdigit((uchar)*(s+2)))) - coeff = s; - } - else { - coeff = s; - } - } - break; - - } - } - - *end = s; - return coeff; -} - -/* scan the payload of a NaN */ -static const char * -scan_payload(const char *s, const char **end) -{ - const char *coeff; - - while (*s == '0') - s++; - coeff = s; - - while (isdigit((uchar)*s)) - s++; - *end = s; - - return (*s == '\0') ? coeff : NULL; -} - -/* convert a character string to a decimal */ -void -mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_ssize_t q, r, len; - const char *coeff, *end; - const char *dpoint = NULL, *exp = NULL; - size_t digits; - uint8_t sign = MPD_POS; - - mpd_set_flags(dec, 0); - dec->len = 0; - dec->exp = 0; +#include <stdio.h> +#include <stdlib.h> +#include <string.h> - /* sign */ - if (*s == '+') { - s++; - } - else if (*s == '-') { - mpd_set_negative(dec); - sign = MPD_NEG; - s++; - } - - if (_mpd_strneq(s, "nan", "NAN", 3)) { /* NaN */ - s += 3; - mpd_setspecial(dec, sign, MPD_NAN); - if (*s == '\0') - return; - /* validate payload: digits only */ - if ((coeff = scan_payload(s, &end)) == NULL) - goto conversion_error; - /* payload consists entirely of zeros */ - if (*coeff == '\0') - return; +#include "typearith.h" +#include "mpd_io.h" + + +/* This file contains functions for decimal <-> string conversions, including + PEP-3101 formatting for numeric types. */ + + +/* 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 + + +/* + * Work around the behavior of tolower() and strcasecmp() in certain + * locales. For example, in tr_TR.utf8: + * + * tolower((unsigned char)'I') == 'I' + * + * u is the exact uppercase version of l; n is strlen(l) or strlen(l)+1 + */ +static inline int +_mpd_strneq(const char *s, const char *l, const char *u, size_t n) +{ + while (--n != SIZE_MAX) { + if (*s != *l && *s != *u) { + return 0; + } + s++; u++; l++; + } + + return 1; +} + +static mpd_ssize_t +strtoexp(const char *s) +{ + char *end; + mpd_ssize_t retval; + + errno = 0; + retval = mpd_strtossize(s, &end, 10); + if (errno == 0 && !(*s != '\0' && *end == '\0')) + errno = EINVAL; + + return retval; +} + +/* + * Scan 'len' words. The most significant word contains 'r' digits, + * the remaining words are full words. Skip dpoint. The string 's' must + * consist of digits and an optional single decimal point at 'dpoint'. + */ +static void +string_to_coeff(mpd_uint_t *data, const char *s, const char *dpoint, int r, + size_t len) +{ + int j; + + if (r > 0) { + data[--len] = 0; + for (j = 0; j < r; j++, s++) { + if (s == dpoint) s++; + data[len] = 10 * data[len] + (*s - '0'); + } + } + + while (--len != SIZE_MAX) { + data[len] = 0; + for (j = 0; j < MPD_RDIGITS; j++, s++) { + if (s == dpoint) s++; + data[len] = 10 * data[len] + (*s - '0'); + } + } +} + +/* + * Partially verify a numeric string of the form: + * + * [cdigits][.][cdigits][eE][+-][edigits] + * + * If successful, return a pointer to the location of the first + * relevant coefficient digit. This digit is either non-zero or + * part of one of the following patterns: + * + * ["0\x00", "0.\x00", "0.E", "0.e", "0E", "0e"] + * + * The locations of a single optional dot or indicator are stored + * in 'dpoint' and 'exp'. + * + * The end of the string is stored in 'end'. If an indicator [eE] + * occurs without trailing [edigits], the condition is caught + * later by strtoexp(). + */ +static const char * +scan_dpoint_exp(const char *s, const char **dpoint, const char **exp, + const char **end) +{ + const char *coeff = NULL; + + *dpoint = NULL; + *exp = NULL; + for (; *s != '\0'; s++) { + switch (*s) { + case '.': + if (*dpoint != NULL || *exp != NULL) + return NULL; + *dpoint = s; + break; + case 'E': case 'e': + if (*exp != NULL) + return NULL; + *exp = s; + if (*(s+1) == '+' || *(s+1) == '-') + s++; + break; + default: + if (!isdigit((uchar)*s)) + return NULL; + if (coeff == NULL && *exp == NULL) { + if (*s == '0') { + if (!isdigit((uchar)*(s+1))) + if (!(*(s+1) == '.' && + isdigit((uchar)*(s+2)))) + coeff = s; + } + else { + coeff = s; + } + } + break; + + } + } + + *end = s; + return coeff; +} + +/* scan the payload of a NaN */ +static const char * +scan_payload(const char *s, const char **end) +{ + const char *coeff; + + while (*s == '0') + s++; + coeff = s; + + while (isdigit((uchar)*s)) + s++; + *end = s; + + return (*s == '\0') ? coeff : NULL; +} + +/* convert a character string to a decimal */ +void +mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_ssize_t q, r, len; + const char *coeff, *end; + const char *dpoint = NULL, *exp = NULL; + size_t digits; + uint8_t sign = MPD_POS; + + mpd_set_flags(dec, 0); + dec->len = 0; + dec->exp = 0; + + /* sign */ + if (*s == '+') { + s++; + } + else if (*s == '-') { + mpd_set_negative(dec); + sign = MPD_NEG; + s++; + } + + if (_mpd_strneq(s, "nan", "NAN", 3)) { /* NaN */ + s += 3; + mpd_setspecial(dec, sign, MPD_NAN); + if (*s == '\0') + return; + /* validate payload: digits only */ + if ((coeff = scan_payload(s, &end)) == NULL) + goto conversion_error; + /* payload consists entirely of zeros */ + if (*coeff == '\0') + return; + digits = end - coeff; + /* prec >= 1, clamp is 0 or 1 */ + if (digits > (size_t)(ctx->prec-ctx->clamp)) + goto conversion_error; + } /* sNaN */ + else if (_mpd_strneq(s, "snan", "SNAN", 4)) { + s += 4; + mpd_setspecial(dec, sign, MPD_SNAN); + if (*s == '\0') + return; + /* validate payload: digits only */ + if ((coeff = scan_payload(s, &end)) == NULL) + goto conversion_error; + /* payload consists entirely of zeros */ + if (*coeff == '\0') + return; + digits = end - coeff; + if (digits > (size_t)(ctx->prec-ctx->clamp)) + goto conversion_error; + } + else if (_mpd_strneq(s, "inf", "INF", 3)) { + s += 3; + if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) { + /* numeric-value: infinity */ + mpd_setspecial(dec, sign, MPD_INF); + return; + } + goto conversion_error; + } + else { + /* scan for start of coefficient, decimal point, indicator, end */ + if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL) + goto conversion_error; + + /* numeric-value: [exponent-part] */ + if (exp) { + /* exponent-part */ + end = exp; exp++; + dec->exp = strtoexp(exp); + if (errno) { + if (!(errno == ERANGE && + (dec->exp == MPD_SSIZE_MAX || + dec->exp == MPD_SSIZE_MIN))) + goto conversion_error; + } + } + digits = end - coeff; - /* prec >= 1, clamp is 0 or 1 */ - if (digits > (size_t)(ctx->prec-ctx->clamp)) - goto conversion_error; - } /* sNaN */ - else if (_mpd_strneq(s, "snan", "SNAN", 4)) { - s += 4; - mpd_setspecial(dec, sign, MPD_SNAN); - if (*s == '\0') - return; - /* validate payload: digits only */ - if ((coeff = scan_payload(s, &end)) == NULL) - goto conversion_error; - /* payload consists entirely of zeros */ - if (*coeff == '\0') - return; - digits = end - coeff; - if (digits > (size_t)(ctx->prec-ctx->clamp)) - goto conversion_error; - } - else if (_mpd_strneq(s, "inf", "INF", 3)) { - s += 3; - if (*s == '\0' || _mpd_strneq(s, "inity", "INITY", 6)) { - /* numeric-value: infinity */ - mpd_setspecial(dec, sign, MPD_INF); - return; - } - goto conversion_error; - } - else { - /* scan for start of coefficient, decimal point, indicator, end */ - if ((coeff = scan_dpoint_exp(s, &dpoint, &exp, &end)) == NULL) - goto conversion_error; - - /* numeric-value: [exponent-part] */ - if (exp) { - /* exponent-part */ - end = exp; exp++; - dec->exp = strtoexp(exp); - if (errno) { - if (!(errno == ERANGE && - (dec->exp == MPD_SSIZE_MAX || - dec->exp == MPD_SSIZE_MIN))) - goto conversion_error; - } - } - - digits = end - coeff; - if (dpoint) { - size_t fracdigits = end-dpoint-1; - if (dpoint > coeff) digits--; - - if (fracdigits > MPD_MAX_PREC) { - goto conversion_error; - } - if (dec->exp < MPD_SSIZE_MIN+(mpd_ssize_t)fracdigits) { - dec->exp = MPD_SSIZE_MIN; - } - else { - dec->exp -= (mpd_ssize_t)fracdigits; - } - } - if (digits > MPD_MAX_PREC) { - goto conversion_error; - } - if (dec->exp > MPD_EXP_INF) { - dec->exp = MPD_EXP_INF; - } - if (dec->exp == MPD_SSIZE_MIN) { - dec->exp = MPD_SSIZE_MIN+1; - } - } - - _mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS); - - len = (r == 0) ? q : q+1; - if (len == 0) { - goto conversion_error; /* GCOV_NOT_REACHED */ - } - if (!mpd_qresize(dec, len, status)) { - mpd_seterror(dec, MPD_Malloc_error, status); - return; - } - dec->len = len; - - string_to_coeff(dec->data, coeff, dpoint, (int)r, len); - - mpd_setdigits(dec); - mpd_qfinalize(dec, ctx, status); - return; - -conversion_error: - /* standard wants a positive NaN */ - mpd_seterror(dec, MPD_Conversion_syntax, status); -} - + if (dpoint) { + size_t fracdigits = end-dpoint-1; + if (dpoint > coeff) digits--; + + if (fracdigits > MPD_MAX_PREC) { + goto conversion_error; + } + if (dec->exp < MPD_SSIZE_MIN+(mpd_ssize_t)fracdigits) { + dec->exp = MPD_SSIZE_MIN; + } + else { + dec->exp -= (mpd_ssize_t)fracdigits; + } + } + if (digits > MPD_MAX_PREC) { + goto conversion_error; + } + if (dec->exp > MPD_EXP_INF) { + dec->exp = MPD_EXP_INF; + } + if (dec->exp == MPD_SSIZE_MIN) { + dec->exp = MPD_SSIZE_MIN+1; + } + } + + _mpd_idiv_word(&q, &r, (mpd_ssize_t)digits, MPD_RDIGITS); + + len = (r == 0) ? q : q+1; + if (len == 0) { + goto conversion_error; /* GCOV_NOT_REACHED */ + } + if (!mpd_qresize(dec, len, status)) { + mpd_seterror(dec, MPD_Malloc_error, status); + return; + } + dec->len = len; + + string_to_coeff(dec->data, coeff, dpoint, (int)r, len); + + mpd_setdigits(dec); + mpd_qfinalize(dec, ctx, status); + return; + +conversion_error: + /* standard wants a positive NaN */ + mpd_seterror(dec, MPD_Conversion_syntax, status); +} + /* convert a character string to a decimal, use a maxcontext for conversion */ void mpd_qset_string_exact(mpd_t *dec, const char *s, uint32_t *status) @@ -342,1257 +342,1257 @@ mpd_qset_string_exact(mpd_t *dec, const char *s, uint32_t *status) *status &= MPD_Errors; } -/* Print word x with n decimal digits to string s. dot is either NULL - or the location of a decimal point. */ -#define EXTRACT_DIGIT(s, x, d, dot) \ - if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d -static inline char * -word_to_string(char *s, mpd_uint_t x, int n, char *dot) -{ - switch(n) { -#ifdef CONFIG_64 - case 20: EXTRACT_DIGIT(s, x, 10000000000000000000ULL, dot); /* GCOV_NOT_REACHED */ - case 19: EXTRACT_DIGIT(s, x, 1000000000000000000ULL, dot); - case 18: EXTRACT_DIGIT(s, x, 100000000000000000ULL, dot); - case 17: EXTRACT_DIGIT(s, x, 10000000000000000ULL, dot); - case 16: EXTRACT_DIGIT(s, x, 1000000000000000ULL, dot); - case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot); - case 14: EXTRACT_DIGIT(s, x, 10000000000000ULL, dot); - case 13: EXTRACT_DIGIT(s, x, 1000000000000ULL, dot); - case 12: EXTRACT_DIGIT(s, x, 100000000000ULL, dot); - case 11: EXTRACT_DIGIT(s, x, 10000000000ULL, dot); -#endif - case 10: EXTRACT_DIGIT(s, x, 1000000000UL, dot); - case 9: EXTRACT_DIGIT(s, x, 100000000UL, dot); - case 8: EXTRACT_DIGIT(s, x, 10000000UL, dot); - case 7: EXTRACT_DIGIT(s, x, 1000000UL, dot); - case 6: EXTRACT_DIGIT(s, x, 100000UL, dot); - case 5: EXTRACT_DIGIT(s, x, 10000UL, dot); - case 4: EXTRACT_DIGIT(s, x, 1000UL, dot); - case 3: EXTRACT_DIGIT(s, x, 100UL, dot); - case 2: EXTRACT_DIGIT(s, x, 10UL, dot); - default: if (s == dot) *s++ = '.'; *s++ = '0' + (char)x; - } - - *s = '\0'; - return s; -} - -/* Print exponent x to string s. Undefined for MPD_SSIZE_MIN. */ -static inline char * -exp_to_string(char *s, mpd_ssize_t x) -{ - char sign = '+'; - - if (x < 0) { - sign = '-'; - x = -x; - } - *s++ = sign; - - return word_to_string(s, x, mpd_word_digits(x), NULL); -} - -/* Print the coefficient of dec to string s. len(dec) > 0. */ -static inline char * -coeff_to_string(char *s, const mpd_t *dec) -{ - mpd_uint_t x; - mpd_ssize_t i; - - /* most significant word */ - x = mpd_msword(dec); - s = word_to_string(s, x, mpd_word_digits(x), NULL); - - /* remaining full words */ - for (i=dec->len-2; i >= 0; --i) { - x = dec->data[i]; - s = word_to_string(s, x, MPD_RDIGITS, NULL); - } - - return s; -} - -/* Print the coefficient of dec to string s. len(dec) > 0. dot is either - NULL or a pointer to the location of a decimal point. */ -static inline char * -coeff_to_string_dot(char *s, char *dot, const mpd_t *dec) -{ - mpd_uint_t x; - mpd_ssize_t i; - - /* most significant word */ - x = mpd_msword(dec); - s = word_to_string(s, x, mpd_word_digits(x), dot); - - /* remaining full words */ - for (i=dec->len-2; i >= 0; --i) { - x = dec->data[i]; - s = word_to_string(s, x, MPD_RDIGITS, dot); - } - - return s; -} - -/* Format type */ -#define MPD_FMT_LOWER 0x00000000 -#define MPD_FMT_UPPER 0x00000001 -#define MPD_FMT_TOSCI 0x00000002 -#define MPD_FMT_TOENG 0x00000004 -#define MPD_FMT_EXP 0x00000008 -#define MPD_FMT_FIXED 0x00000010 -#define MPD_FMT_PERCENT 0x00000020 -#define MPD_FMT_SIGN_SPACE 0x00000040 -#define MPD_FMT_SIGN_PLUS 0x00000080 - -/* Default place of the decimal point for MPD_FMT_TOSCI, MPD_FMT_EXP */ -#define MPD_DEFAULT_DOTPLACE 1 - -/* - * Set *result to the string representation of a decimal. Return the length - * of *result, not including the terminating '\0' character. - * - * Formatting is done according to 'flags'. A return value of -1 with *result - * set to NULL indicates MPD_Malloc_error. - * - * 'dplace' is the default place of the decimal point. It is always set to - * MPD_DEFAULT_DOTPLACE except for zeros in combination with MPD_FMT_EXP. - */ -static mpd_ssize_t -_mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) -{ - char *decstring = NULL, *cp = NULL; - mpd_ssize_t ldigits; - mpd_ssize_t mem = 0, k; - - if (mpd_isspecial(dec)) { - - mem = sizeof "-Infinity%"; - if (mpd_isnan(dec) && dec->len > 0) { - /* diagnostic code */ - mem += dec->digits; - } - cp = decstring = mpd_alloc(mem, sizeof *decstring); - if (cp == NULL) { - *result = NULL; - return -1; - } - - if (mpd_isnegative(dec)) { - *cp++ = '-'; - } - else if (flags&MPD_FMT_SIGN_SPACE) { - *cp++ = ' '; - } - else if (flags&MPD_FMT_SIGN_PLUS) { - *cp++ = '+'; - } - - if (mpd_isnan(dec)) { - if (mpd_isqnan(dec)) { - strcpy(cp, "NaN"); - cp += 3; - } - else { - strcpy(cp, "sNaN"); - cp += 4; - } - if (dec->len > 0) { /* diagnostic code */ - cp = coeff_to_string(cp, dec); - } - } - else if (mpd_isinfinite(dec)) { - strcpy(cp, "Infinity"); - cp += 8; - } - else { /* debug */ - abort(); /* GCOV_NOT_REACHED */ - } - } - else { - assert(dec->len > 0); - - /* - * For easier manipulation of the decimal point's location - * and the exponent that is finally printed, the number is - * rescaled to a virtual representation with exp = 0. Here - * ldigits denotes the number of decimal digits to the left - * of the decimal point and remains constant once initialized. - * - * dplace is the location of the decimal point relative to - * the start of the coefficient. Note that 3) always holds - * when dplace is shifted. - * - * 1) ldigits := dec->digits - dec->exp - * 2) dplace := ldigits (initially) - * 3) exp := ldigits - dplace (initially exp = 0) - * - * 0.00000_.____._____000000. - * ^ ^ ^ ^ - * | | | | - * | | | `- dplace >= digits - * | | `- dplace in the middle of the coefficient - * | ` dplace = 1 (after the first coefficient digit) - * `- dplace <= 0 - */ - - ldigits = dec->digits + dec->exp; - - if (flags&MPD_FMT_EXP) { - ; - } - else if (flags&MPD_FMT_FIXED || (dec->exp <= 0 && ldigits > -6)) { - /* MPD_FMT_FIXED: always use fixed point notation. - * MPD_FMT_TOSCI, MPD_FMT_TOENG: for a certain range, - * override exponent notation. */ - dplace = ldigits; - } - else if (flags&MPD_FMT_TOENG) { - if (mpd_iszero(dec)) { - /* If the exponent is divisible by three, - * dplace = 1. Otherwise, move dplace one - * or two places to the left. */ - dplace = -1 + mod_mpd_ssize_t(dec->exp+2, 3); - } - else { /* ldigits-1 is the adjusted exponent, which +/* Print word x with n decimal digits to string s. dot is either NULL + or the location of a decimal point. */ +#define EXTRACT_DIGIT(s, x, d, dot) \ + if (s == dot) *s++ = '.'; *s++ = '0' + (char)(x / d); x %= d +static inline char * +word_to_string(char *s, mpd_uint_t x, int n, char *dot) +{ + switch(n) { +#ifdef CONFIG_64 + case 20: EXTRACT_DIGIT(s, x, 10000000000000000000ULL, dot); /* GCOV_NOT_REACHED */ + case 19: EXTRACT_DIGIT(s, x, 1000000000000000000ULL, dot); + case 18: EXTRACT_DIGIT(s, x, 100000000000000000ULL, dot); + case 17: EXTRACT_DIGIT(s, x, 10000000000000000ULL, dot); + case 16: EXTRACT_DIGIT(s, x, 1000000000000000ULL, dot); + case 15: EXTRACT_DIGIT(s, x, 100000000000000ULL, dot); + case 14: EXTRACT_DIGIT(s, x, 10000000000000ULL, dot); + case 13: EXTRACT_DIGIT(s, x, 1000000000000ULL, dot); + case 12: EXTRACT_DIGIT(s, x, 100000000000ULL, dot); + case 11: EXTRACT_DIGIT(s, x, 10000000000ULL, dot); +#endif + case 10: EXTRACT_DIGIT(s, x, 1000000000UL, dot); + case 9: EXTRACT_DIGIT(s, x, 100000000UL, dot); + case 8: EXTRACT_DIGIT(s, x, 10000000UL, dot); + case 7: EXTRACT_DIGIT(s, x, 1000000UL, dot); + case 6: EXTRACT_DIGIT(s, x, 100000UL, dot); + case 5: EXTRACT_DIGIT(s, x, 10000UL, dot); + case 4: EXTRACT_DIGIT(s, x, 1000UL, dot); + case 3: EXTRACT_DIGIT(s, x, 100UL, dot); + case 2: EXTRACT_DIGIT(s, x, 10UL, dot); + default: if (s == dot) *s++ = '.'; *s++ = '0' + (char)x; + } + + *s = '\0'; + return s; +} + +/* Print exponent x to string s. Undefined for MPD_SSIZE_MIN. */ +static inline char * +exp_to_string(char *s, mpd_ssize_t x) +{ + char sign = '+'; + + if (x < 0) { + sign = '-'; + x = -x; + } + *s++ = sign; + + return word_to_string(s, x, mpd_word_digits(x), NULL); +} + +/* Print the coefficient of dec to string s. len(dec) > 0. */ +static inline char * +coeff_to_string(char *s, const mpd_t *dec) +{ + mpd_uint_t x; + mpd_ssize_t i; + + /* most significant word */ + x = mpd_msword(dec); + s = word_to_string(s, x, mpd_word_digits(x), NULL); + + /* remaining full words */ + for (i=dec->len-2; i >= 0; --i) { + x = dec->data[i]; + s = word_to_string(s, x, MPD_RDIGITS, NULL); + } + + return s; +} + +/* Print the coefficient of dec to string s. len(dec) > 0. dot is either + NULL or a pointer to the location of a decimal point. */ +static inline char * +coeff_to_string_dot(char *s, char *dot, const mpd_t *dec) +{ + mpd_uint_t x; + mpd_ssize_t i; + + /* most significant word */ + x = mpd_msword(dec); + s = word_to_string(s, x, mpd_word_digits(x), dot); + + /* remaining full words */ + for (i=dec->len-2; i >= 0; --i) { + x = dec->data[i]; + s = word_to_string(s, x, MPD_RDIGITS, dot); + } + + return s; +} + +/* Format type */ +#define MPD_FMT_LOWER 0x00000000 +#define MPD_FMT_UPPER 0x00000001 +#define MPD_FMT_TOSCI 0x00000002 +#define MPD_FMT_TOENG 0x00000004 +#define MPD_FMT_EXP 0x00000008 +#define MPD_FMT_FIXED 0x00000010 +#define MPD_FMT_PERCENT 0x00000020 +#define MPD_FMT_SIGN_SPACE 0x00000040 +#define MPD_FMT_SIGN_PLUS 0x00000080 + +/* Default place of the decimal point for MPD_FMT_TOSCI, MPD_FMT_EXP */ +#define MPD_DEFAULT_DOTPLACE 1 + +/* + * Set *result to the string representation of a decimal. Return the length + * of *result, not including the terminating '\0' character. + * + * Formatting is done according to 'flags'. A return value of -1 with *result + * set to NULL indicates MPD_Malloc_error. + * + * 'dplace' is the default place of the decimal point. It is always set to + * MPD_DEFAULT_DOTPLACE except for zeros in combination with MPD_FMT_EXP. + */ +static mpd_ssize_t +_mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) +{ + char *decstring = NULL, *cp = NULL; + mpd_ssize_t ldigits; + mpd_ssize_t mem = 0, k; + + if (mpd_isspecial(dec)) { + + mem = sizeof "-Infinity%"; + if (mpd_isnan(dec) && dec->len > 0) { + /* diagnostic code */ + mem += dec->digits; + } + cp = decstring = mpd_alloc(mem, sizeof *decstring); + if (cp == NULL) { + *result = NULL; + return -1; + } + + if (mpd_isnegative(dec)) { + *cp++ = '-'; + } + else if (flags&MPD_FMT_SIGN_SPACE) { + *cp++ = ' '; + } + else if (flags&MPD_FMT_SIGN_PLUS) { + *cp++ = '+'; + } + + if (mpd_isnan(dec)) { + if (mpd_isqnan(dec)) { + strcpy(cp, "NaN"); + cp += 3; + } + else { + strcpy(cp, "sNaN"); + cp += 4; + } + if (dec->len > 0) { /* diagnostic code */ + cp = coeff_to_string(cp, dec); + } + } + else if (mpd_isinfinite(dec)) { + strcpy(cp, "Infinity"); + cp += 8; + } + else { /* debug */ + abort(); /* GCOV_NOT_REACHED */ + } + } + else { + assert(dec->len > 0); + + /* + * For easier manipulation of the decimal point's location + * and the exponent that is finally printed, the number is + * rescaled to a virtual representation with exp = 0. Here + * ldigits denotes the number of decimal digits to the left + * of the decimal point and remains constant once initialized. + * + * dplace is the location of the decimal point relative to + * the start of the coefficient. Note that 3) always holds + * when dplace is shifted. + * + * 1) ldigits := dec->digits - dec->exp + * 2) dplace := ldigits (initially) + * 3) exp := ldigits - dplace (initially exp = 0) + * + * 0.00000_.____._____000000. + * ^ ^ ^ ^ + * | | | | + * | | | `- dplace >= digits + * | | `- dplace in the middle of the coefficient + * | ` dplace = 1 (after the first coefficient digit) + * `- dplace <= 0 + */ + + ldigits = dec->digits + dec->exp; + + if (flags&MPD_FMT_EXP) { + ; + } + else if (flags&MPD_FMT_FIXED || (dec->exp <= 0 && ldigits > -6)) { + /* MPD_FMT_FIXED: always use fixed point notation. + * MPD_FMT_TOSCI, MPD_FMT_TOENG: for a certain range, + * override exponent notation. */ + dplace = ldigits; + } + else if (flags&MPD_FMT_TOENG) { + if (mpd_iszero(dec)) { + /* If the exponent is divisible by three, + * dplace = 1. Otherwise, move dplace one + * or two places to the left. */ + dplace = -1 + mod_mpd_ssize_t(dec->exp+2, 3); + } + else { /* ldigits-1 is the adjusted exponent, which * should be divisible by three. If not, move * dplace one or two places to the right. */ - dplace += mod_mpd_ssize_t(ldigits-1, 3); - } - } - - /* - * Basic space requirements: - * - * [-][.][coeffdigits][E][-][expdigits+1][%]['\0'] - * - * If the decimal point lies outside of the coefficient digits, - * space is adjusted accordingly. - */ - if (dplace <= 0) { - mem = -dplace + dec->digits + 2; - } - else if (dplace >= dec->digits) { - mem = dplace; - } - else { - mem = dec->digits; - } - mem += (MPD_EXPDIGITS+1+6); - - cp = decstring = mpd_alloc(mem, sizeof *decstring); - if (cp == NULL) { - *result = NULL; - return -1; - } - - - if (mpd_isnegative(dec)) { - *cp++ = '-'; - } - else if (flags&MPD_FMT_SIGN_SPACE) { - *cp++ = ' '; - } - else if (flags&MPD_FMT_SIGN_PLUS) { - *cp++ = '+'; - } - - if (dplace <= 0) { - /* space: -dplace+dec->digits+2 */ - *cp++ = '0'; - *cp++ = '.'; - for (k = 0; k < -dplace; k++) { - *cp++ = '0'; - } - cp = coeff_to_string(cp, dec); - } - else if (dplace >= dec->digits) { - /* space: dplace */ - cp = coeff_to_string(cp, dec); - for (k = 0; k < dplace-dec->digits; k++) { - *cp++ = '0'; - } - } - else { - /* space: dec->digits+1 */ - cp = coeff_to_string_dot(cp, cp+dplace, dec); - } - - /* - * Conditions for printing an exponent: - * - * MPD_FMT_TOSCI, MPD_FMT_TOENG: only if ldigits != dplace - * MPD_FMT_FIXED: never (ldigits == dplace) - * MPD_FMT_EXP: always - */ - if (ldigits != dplace || flags&MPD_FMT_EXP) { - /* space: expdigits+2 */ - *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; - cp = exp_to_string(cp, ldigits-dplace); - } - } - - if (flags&MPD_FMT_PERCENT) { - *cp++ = '%'; - } - - assert(cp < decstring+mem); - assert(cp-decstring < MPD_SSIZE_MAX); - - *cp = '\0'; - *result = decstring; - return (mpd_ssize_t)(cp-decstring); -} - -char * -mpd_to_sci(const mpd_t *dec, int fmt) -{ - char *res; - int flags = MPD_FMT_TOSCI; - - flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; - (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); - return res; -} - -char * -mpd_to_eng(const mpd_t *dec, int fmt) -{ - char *res; - int flags = MPD_FMT_TOENG; - - flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; - (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); - return res; -} - -mpd_ssize_t -mpd_to_sci_size(char **res, const mpd_t *dec, int fmt) -{ - int flags = MPD_FMT_TOSCI; - - flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; - return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); -} - -mpd_ssize_t -mpd_to_eng_size(char **res, const mpd_t *dec, int fmt) -{ - int flags = MPD_FMT_TOENG; - - flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; - return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); -} - -/* Copy a single UTF-8 char to dest. See: The Unicode Standard, version 5.2, - chapter 3.9: Well-formed UTF-8 byte sequences. */ -static int -_mpd_copy_utf8(char dest[5], const char *s) -{ - const uchar *cp = (const uchar *)s; - uchar lb, ub; - int count, i; - - - if (*cp == 0) { - /* empty string */ - dest[0] = '\0'; - return 0; - } - else if (*cp <= 0x7f) { - /* ascii */ - dest[0] = *cp; - dest[1] = '\0'; - return 1; - } - else if (0xc2 <= *cp && *cp <= 0xdf) { - lb = 0x80; ub = 0xbf; - count = 2; - } - else if (*cp == 0xe0) { - lb = 0xa0; ub = 0xbf; - count = 3; - } - else if (*cp <= 0xec) { - lb = 0x80; ub = 0xbf; - count = 3; - } - else if (*cp == 0xed) { - lb = 0x80; ub = 0x9f; - count = 3; - } - else if (*cp <= 0xef) { - lb = 0x80; ub = 0xbf; - count = 3; - } - else if (*cp == 0xf0) { - lb = 0x90; ub = 0xbf; - count = 4; - } - else if (*cp <= 0xf3) { - lb = 0x80; ub = 0xbf; - count = 4; - } - else if (*cp == 0xf4) { - lb = 0x80; ub = 0x8f; - count = 4; - } - else { - /* invalid */ - goto error; - } - - dest[0] = *cp++; - if (*cp < lb || ub < *cp) { - goto error; - } - dest[1] = *cp++; - for (i = 2; i < count; i++) { - if (*cp < 0x80 || 0xbf < *cp) { - goto error; - } - dest[i] = *cp++; - } - dest[i] = '\0'; - - return count; - -error: - dest[0] = '\0'; - return -1; -} - -int -mpd_validate_lconv(mpd_spec_t *spec) -{ - size_t n; -#if CHAR_MAX == SCHAR_MAX - const char *cp = spec->grouping; - while (*cp != '\0') { - if (*cp++ < 0) { - return -1; - } - } -#endif - n = strlen(spec->dot); - if (n == 0 || n > 4) { - return -1; - } - if (strlen(spec->sep) > 4) { - return -1; - } - - return 0; -} - -int -mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps) -{ - char *cp = (char *)fmt; - int have_align = 0, n; - - /* defaults */ - spec->min_width = 0; - spec->prec = -1; - spec->type = caps ? 'G' : 'g'; - spec->align = '>'; - spec->sign = '-'; - spec->dot = ""; - spec->sep = ""; - spec->grouping = ""; - - - /* presume that the first character is a UTF-8 fill character */ - if ((n = _mpd_copy_utf8(spec->fill, cp)) < 0) { - return 0; - } - - /* alignment directive, prefixed by a fill character */ - if (*cp && (*(cp+n) == '<' || *(cp+n) == '>' || - *(cp+n) == '=' || *(cp+n) == '^')) { - cp += n; - spec->align = *cp++; - have_align = 1; - } /* alignment directive */ - else { - /* default fill character */ - spec->fill[0] = ' '; - spec->fill[1] = '\0'; - if (*cp == '<' || *cp == '>' || - *cp == '=' || *cp == '^') { - spec->align = *cp++; - have_align = 1; - } - } - - /* sign formatting */ - if (*cp == '+' || *cp == '-' || *cp == ' ') { - spec->sign = *cp++; - } - - /* zero padding */ - if (*cp == '0') { - /* zero padding implies alignment, which should not be - * specified twice. */ - if (have_align) { - return 0; - } - spec->align = 'z'; - spec->fill[0] = *cp++; - spec->fill[1] = '\0'; - } - - /* minimum width */ - if (isdigit((uchar)*cp)) { - if (*cp == '0') { - return 0; - } - errno = 0; - spec->min_width = mpd_strtossize(cp, &cp, 10); - if (errno == ERANGE || errno == EINVAL) { - return 0; - } - } - - /* thousands separator */ - if (*cp == ',') { - spec->dot = "."; - spec->sep = ","; - spec->grouping = "\003\003"; - cp++; - } - - /* fraction digits or significant digits */ - if (*cp == '.') { - cp++; - if (!isdigit((uchar)*cp)) { - return 0; - } - errno = 0; - spec->prec = mpd_strtossize(cp, &cp, 10); - if (errno == ERANGE || errno == EINVAL) { - return 0; - } - } - - /* type */ - if (*cp == 'E' || *cp == 'e' || *cp == 'F' || *cp == 'f' || - *cp == 'G' || *cp == 'g' || *cp == '%') { - spec->type = *cp++; - } - else if (*cp == 'N' || *cp == 'n') { - /* locale specific conversion */ - struct lconv *lc; - /* separator has already been specified */ - if (*spec->sep) { - return 0; - } - spec->type = *cp++; - spec->type = (spec->type == 'N') ? 'G' : 'g'; - lc = localeconv(); - spec->dot = lc->decimal_point; - spec->sep = lc->thousands_sep; - spec->grouping = lc->grouping; - if (mpd_validate_lconv(spec) < 0) { - return 0; /* GCOV_NOT_REACHED */ - } - } - - /* check correctness */ - if (*cp != '\0') { - return 0; - } - - return 1; -} - -/* - * The following functions assume that spec->min_width <= MPD_MAX_PREC, which - * is made sure in mpd_qformat_spec. Then, even with a spec that inserts a - * four-byte separator after each digit, nbytes in the following struct - * cannot overflow. - */ - -/* Multibyte string */ -typedef struct { - mpd_ssize_t nbytes; /* length in bytes */ - mpd_ssize_t nchars; /* length in chars */ - mpd_ssize_t cur; /* current write index */ - char *data; -} mpd_mbstr_t; - -static inline void -_mpd_bcopy(char *dest, const char *src, mpd_ssize_t n) -{ - while (--n >= 0) { - dest[n] = src[n]; - } -} - -static inline void -_mbstr_copy_char(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) -{ - dest->nbytes += n; - dest->nchars += (n > 0 ? 1 : 0); - dest->cur -= n; - - if (dest->data != NULL) { - _mpd_bcopy(dest->data+dest->cur, src, n); - } -} - -static inline void -_mbstr_copy_ascii(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) -{ - dest->nbytes += n; - dest->nchars += n; - dest->cur -= n; - - if (dest->data != NULL) { - _mpd_bcopy(dest->data+dest->cur, src, n); - } -} - -static inline void -_mbstr_copy_pad(mpd_mbstr_t *dest, mpd_ssize_t n) -{ - dest->nbytes += n; - dest->nchars += n; - dest->cur -= n; - - if (dest->data != NULL) { - char *cp = dest->data + dest->cur; - while (--n >= 0) { - cp[n] = '0'; - } - } -} - -/* - * Copy a numeric string to dest->data, adding separators in the integer - * part according to spec->grouping. If leading zero padding is enabled - * and the result is smaller than spec->min_width, continue adding zeros - * and separators until the minimum width is reached. - * - * The final length of dest->data is stored in dest->nbytes. The number - * of UTF-8 characters is stored in dest->nchars. - * - * First run (dest->data == NULL): determine the length of the result - * string and store it in dest->nbytes. - * - * Second run (write to dest->data): data is written in chunks and in - * reverse order, starting with the rest of the numeric string. - */ -static void -_mpd_add_sep_dot(mpd_mbstr_t *dest, - const char *sign, /* location of optional sign */ - const char *src, mpd_ssize_t n_src, /* integer part and length */ - const char *dot, /* location of optional decimal point */ - const char *rest, mpd_ssize_t n_rest, /* remaining part and length */ - const mpd_spec_t *spec) -{ - mpd_ssize_t n_sep, n_sign, consume; - const char *g; - int pad = 0; - - n_sign = sign ? 1 : 0; - n_sep = (mpd_ssize_t)strlen(spec->sep); - /* Initial write index: set to location of '\0' in the output string. - * Irrelevant for the first run. */ - dest->cur = dest->nbytes; - dest->nbytes = dest->nchars = 0; - - _mbstr_copy_ascii(dest, rest, n_rest); - - if (dot) { - _mbstr_copy_char(dest, dot, (mpd_ssize_t)strlen(dot)); - } - - g = spec->grouping; - consume = *g; - while (1) { - /* If the group length is 0 or CHAR_MAX or greater than the - * number of source bytes, consume all remaining bytes. */ - if (*g == 0 || *g == CHAR_MAX || consume > n_src) { - consume = n_src; - } - n_src -= consume; - if (pad) { - _mbstr_copy_pad(dest, consume); - } - else { - _mbstr_copy_ascii(dest, src+n_src, consume); - } - - if (n_src == 0) { - /* Either the real source of intpart digits or the virtual - * source of padding zeros is exhausted. */ - if (spec->align == 'z' && - dest->nchars + n_sign < spec->min_width) { - /* Zero padding is set and length < min_width: - * Generate n_src additional characters. */ - n_src = spec->min_width - (dest->nchars + n_sign); - /* Next iteration: - * case *g == 0 || *g == CHAR_MAX: - * consume all padding characters - * case consume < g*: - * fill remainder of current group - * case consume == g* - * copying is a no-op */ - consume = *g - consume; - /* Switch on virtual source of zeros. */ - pad = 1; - continue; - } - break; - } - - if (n_sep > 0) { - /* If padding is switched on, separators are counted - * as padding characters. This rule does not apply if - * the separator would be the first character of the - * result string. */ - if (pad && n_src > 1) n_src -= 1; - _mbstr_copy_char(dest, spec->sep, n_sep); - } - - /* If non-NUL, use the next value for grouping. */ - if (*g && *(g+1)) g++; - consume = *g; - } - - if (sign) { - _mbstr_copy_ascii(dest, sign, 1); - } - - if (dest->data) { - dest->data[dest->nbytes] = '\0'; - } -} - -/* - * Convert a numeric-string to its locale-specific appearance. - * The string must have one of these forms: - * - * 1) [sign] digits [exponent-part] - * 2) [sign] digits '.' [digits] [exponent-part] - * - * Not allowed, since _mpd_to_string() never returns this form: - * - * 3) [sign] '.' digits [exponent-part] - * - * Input: result->data := original numeric string (ASCII) - * result->bytes := strlen(result->data) - * result->nchars := strlen(result->data) - * - * Output: result->data := modified or original string - * result->bytes := strlen(result->data) - * result->nchars := number of characters (possibly UTF-8) - */ -static int -_mpd_apply_lconv(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) -{ - const char *sign = NULL, *intpart = NULL, *dot = NULL; - const char *rest, *dp; - char *decstring; - mpd_ssize_t n_int, n_rest; - - /* original numeric string */ - dp = result->data; - - /* sign */ - if (*dp == '+' || *dp == '-' || *dp == ' ') { - sign = dp++; - } - /* integer part */ - assert(isdigit((uchar)*dp)); - intpart = dp++; - while (isdigit((uchar)*dp)) { - dp++; - } - n_int = (mpd_ssize_t)(dp-intpart); - /* decimal point */ - if (*dp == '.') { - dp++; dot = spec->dot; - } - /* rest */ - rest = dp; - n_rest = result->nbytes - (mpd_ssize_t)(dp-result->data); - - if (dot == NULL && (*spec->sep == '\0' || *spec->grouping == '\0')) { - /* _mpd_add_sep_dot() would not change anything */ - return 1; - } - - /* Determine the size of the new decimal string after inserting the - * decimal point, optional separators and optional padding. */ - decstring = result->data; - result->data = NULL; - _mpd_add_sep_dot(result, sign, intpart, n_int, dot, - rest, n_rest, spec); - - result->data = mpd_alloc(result->nbytes+1, 1); - if (result->data == NULL) { - *status |= MPD_Malloc_error; - mpd_free(decstring); - return 0; - } - - /* Perform actual writes. */ - _mpd_add_sep_dot(result, sign, intpart, n_int, dot, - rest, n_rest, spec); - - mpd_free(decstring); - return 1; -} - -/* Add padding to the formatted string if necessary. */ -static int -_mpd_add_pad(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) -{ - if (result->nchars < spec->min_width) { - mpd_ssize_t add_chars, add_bytes; - size_t lpad = 0, rpad = 0; - size_t n_fill, len, i, j; - char align = spec->align; - uint8_t err = 0; - char *cp; - - n_fill = strlen(spec->fill); - add_chars = (spec->min_width - result->nchars); - /* max value: MPD_MAX_PREC * 4 */ - add_bytes = add_chars * (mpd_ssize_t)n_fill; - - cp = result->data = mpd_realloc(result->data, - result->nbytes+add_bytes+1, - sizeof *result->data, &err); - if (err) { - *status |= MPD_Malloc_error; - mpd_free(result->data); - return 0; - } - - if (align == 'z') { - align = '='; - } - - if (align == '<') { - rpad = add_chars; - } - else if (align == '>' || align == '=') { - lpad = add_chars; - } - else { /* align == '^' */ - lpad = add_chars/2; - rpad = add_chars-lpad; - } - - len = result->nbytes; - if (align == '=' && (*cp == '-' || *cp == '+' || *cp == ' ')) { - /* leave sign in the leading position */ - cp++; len--; - } - - memmove(cp+n_fill*lpad, cp, len); - for (i = 0; i < lpad; i++) { - for (j = 0; j < n_fill; j++) { - cp[i*n_fill+j] = spec->fill[j]; - } - } - cp += (n_fill*lpad + len); - for (i = 0; i < rpad; i++) { - for (j = 0; j < n_fill; j++) { - cp[i*n_fill+j] = spec->fill[j]; - } - } - - result->nbytes += add_bytes; - result->nchars += add_chars; - result->data[result->nbytes] = '\0'; - } - - return 1; -} - -/* Round a number to prec digits. The adjusted exponent stays the same - or increases by one if rounding up crosses a power of ten boundary. - If result->digits would exceed MPD_MAX_PREC+1, MPD_Invalid_operation - is set and the result is NaN. */ -static inline void -_mpd_round(mpd_t *result, const mpd_t *a, mpd_ssize_t prec, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_ssize_t exp = a->exp + a->digits - prec; - - if (prec <= 0) { - mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_NOT_REACHED */ - return; /* GCOV_NOT_REACHED */ - } - if (mpd_isspecial(a) || mpd_iszero(a)) { - mpd_qcopy(result, a, status); /* GCOV_NOT_REACHED */ - return; /* GCOV_NOT_REACHED */ - } - - mpd_qrescale_fmt(result, a, exp, ctx, status); - if (result->digits > prec) { - mpd_qrescale_fmt(result, result, exp+1, ctx, status); - } -} - -/* - * Return the string representation of an mpd_t, formatted according to 'spec'. - * The format specification is assumed to be valid. Memory errors are indicated - * as usual. This function is quiet. - */ -char * -mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_uint_t dt[MPD_MINALLOC_MAX]; - mpd_t tmp = {MPD_STATIC|MPD_STATIC_DATA,0,0,0,MPD_MINALLOC_MAX,dt}; - mpd_ssize_t dplace = MPD_DEFAULT_DOTPLACE; - mpd_mbstr_t result; - mpd_spec_t stackspec; - char type = spec->type; - int flags = 0; - - - if (spec->min_width > MPD_MAX_PREC) { - *status |= MPD_Invalid_operation; - return NULL; - } - - if (isupper((uchar)type)) { + dplace += mod_mpd_ssize_t(ldigits-1, 3); + } + } + + /* + * Basic space requirements: + * + * [-][.][coeffdigits][E][-][expdigits+1][%]['\0'] + * + * If the decimal point lies outside of the coefficient digits, + * space is adjusted accordingly. + */ + if (dplace <= 0) { + mem = -dplace + dec->digits + 2; + } + else if (dplace >= dec->digits) { + mem = dplace; + } + else { + mem = dec->digits; + } + mem += (MPD_EXPDIGITS+1+6); + + cp = decstring = mpd_alloc(mem, sizeof *decstring); + if (cp == NULL) { + *result = NULL; + return -1; + } + + + if (mpd_isnegative(dec)) { + *cp++ = '-'; + } + else if (flags&MPD_FMT_SIGN_SPACE) { + *cp++ = ' '; + } + else if (flags&MPD_FMT_SIGN_PLUS) { + *cp++ = '+'; + } + + if (dplace <= 0) { + /* space: -dplace+dec->digits+2 */ + *cp++ = '0'; + *cp++ = '.'; + for (k = 0; k < -dplace; k++) { + *cp++ = '0'; + } + cp = coeff_to_string(cp, dec); + } + else if (dplace >= dec->digits) { + /* space: dplace */ + cp = coeff_to_string(cp, dec); + for (k = 0; k < dplace-dec->digits; k++) { + *cp++ = '0'; + } + } + else { + /* space: dec->digits+1 */ + cp = coeff_to_string_dot(cp, cp+dplace, dec); + } + + /* + * Conditions for printing an exponent: + * + * MPD_FMT_TOSCI, MPD_FMT_TOENG: only if ldigits != dplace + * MPD_FMT_FIXED: never (ldigits == dplace) + * MPD_FMT_EXP: always + */ + if (ldigits != dplace || flags&MPD_FMT_EXP) { + /* space: expdigits+2 */ + *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; + cp = exp_to_string(cp, ldigits-dplace); + } + } + + if (flags&MPD_FMT_PERCENT) { + *cp++ = '%'; + } + + assert(cp < decstring+mem); + assert(cp-decstring < MPD_SSIZE_MAX); + + *cp = '\0'; + *result = decstring; + return (mpd_ssize_t)(cp-decstring); +} + +char * +mpd_to_sci(const mpd_t *dec, int fmt) +{ + char *res; + int flags = MPD_FMT_TOSCI; + + flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; + (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); + return res; +} + +char * +mpd_to_eng(const mpd_t *dec, int fmt) +{ + char *res; + int flags = MPD_FMT_TOENG; + + flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; + (void)_mpd_to_string(&res, dec, flags, MPD_DEFAULT_DOTPLACE); + return res; +} + +mpd_ssize_t +mpd_to_sci_size(char **res, const mpd_t *dec, int fmt) +{ + int flags = MPD_FMT_TOSCI; + + flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; + return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); +} + +mpd_ssize_t +mpd_to_eng_size(char **res, const mpd_t *dec, int fmt) +{ + int flags = MPD_FMT_TOENG; + + flags |= fmt ? MPD_FMT_UPPER : MPD_FMT_LOWER; + return _mpd_to_string(res, dec, flags, MPD_DEFAULT_DOTPLACE); +} + +/* Copy a single UTF-8 char to dest. See: The Unicode Standard, version 5.2, + chapter 3.9: Well-formed UTF-8 byte sequences. */ +static int +_mpd_copy_utf8(char dest[5], const char *s) +{ + const uchar *cp = (const uchar *)s; + uchar lb, ub; + int count, i; + + + if (*cp == 0) { + /* empty string */ + dest[0] = '\0'; + return 0; + } + else if (*cp <= 0x7f) { + /* ascii */ + dest[0] = *cp; + dest[1] = '\0'; + return 1; + } + else if (0xc2 <= *cp && *cp <= 0xdf) { + lb = 0x80; ub = 0xbf; + count = 2; + } + else if (*cp == 0xe0) { + lb = 0xa0; ub = 0xbf; + count = 3; + } + else if (*cp <= 0xec) { + lb = 0x80; ub = 0xbf; + count = 3; + } + else if (*cp == 0xed) { + lb = 0x80; ub = 0x9f; + count = 3; + } + else if (*cp <= 0xef) { + lb = 0x80; ub = 0xbf; + count = 3; + } + else if (*cp == 0xf0) { + lb = 0x90; ub = 0xbf; + count = 4; + } + else if (*cp <= 0xf3) { + lb = 0x80; ub = 0xbf; + count = 4; + } + else if (*cp == 0xf4) { + lb = 0x80; ub = 0x8f; + count = 4; + } + else { + /* invalid */ + goto error; + } + + dest[0] = *cp++; + if (*cp < lb || ub < *cp) { + goto error; + } + dest[1] = *cp++; + for (i = 2; i < count; i++) { + if (*cp < 0x80 || 0xbf < *cp) { + goto error; + } + dest[i] = *cp++; + } + dest[i] = '\0'; + + return count; + +error: + dest[0] = '\0'; + return -1; +} + +int +mpd_validate_lconv(mpd_spec_t *spec) +{ + size_t n; +#if CHAR_MAX == SCHAR_MAX + const char *cp = spec->grouping; + while (*cp != '\0') { + if (*cp++ < 0) { + return -1; + } + } +#endif + n = strlen(spec->dot); + if (n == 0 || n > 4) { + return -1; + } + if (strlen(spec->sep) > 4) { + return -1; + } + + return 0; +} + +int +mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps) +{ + char *cp = (char *)fmt; + int have_align = 0, n; + + /* defaults */ + spec->min_width = 0; + spec->prec = -1; + spec->type = caps ? 'G' : 'g'; + spec->align = '>'; + spec->sign = '-'; + spec->dot = ""; + spec->sep = ""; + spec->grouping = ""; + + + /* presume that the first character is a UTF-8 fill character */ + if ((n = _mpd_copy_utf8(spec->fill, cp)) < 0) { + return 0; + } + + /* alignment directive, prefixed by a fill character */ + if (*cp && (*(cp+n) == '<' || *(cp+n) == '>' || + *(cp+n) == '=' || *(cp+n) == '^')) { + cp += n; + spec->align = *cp++; + have_align = 1; + } /* alignment directive */ + else { + /* default fill character */ + spec->fill[0] = ' '; + spec->fill[1] = '\0'; + if (*cp == '<' || *cp == '>' || + *cp == '=' || *cp == '^') { + spec->align = *cp++; + have_align = 1; + } + } + + /* sign formatting */ + if (*cp == '+' || *cp == '-' || *cp == ' ') { + spec->sign = *cp++; + } + + /* zero padding */ + if (*cp == '0') { + /* zero padding implies alignment, which should not be + * specified twice. */ + if (have_align) { + return 0; + } + spec->align = 'z'; + spec->fill[0] = *cp++; + spec->fill[1] = '\0'; + } + + /* minimum width */ + if (isdigit((uchar)*cp)) { + if (*cp == '0') { + return 0; + } + errno = 0; + spec->min_width = mpd_strtossize(cp, &cp, 10); + if (errno == ERANGE || errno == EINVAL) { + return 0; + } + } + + /* thousands separator */ + if (*cp == ',') { + spec->dot = "."; + spec->sep = ","; + spec->grouping = "\003\003"; + cp++; + } + + /* fraction digits or significant digits */ + if (*cp == '.') { + cp++; + if (!isdigit((uchar)*cp)) { + return 0; + } + errno = 0; + spec->prec = mpd_strtossize(cp, &cp, 10); + if (errno == ERANGE || errno == EINVAL) { + return 0; + } + } + + /* type */ + if (*cp == 'E' || *cp == 'e' || *cp == 'F' || *cp == 'f' || + *cp == 'G' || *cp == 'g' || *cp == '%') { + spec->type = *cp++; + } + else if (*cp == 'N' || *cp == 'n') { + /* locale specific conversion */ + struct lconv *lc; + /* separator has already been specified */ + if (*spec->sep) { + return 0; + } + spec->type = *cp++; + spec->type = (spec->type == 'N') ? 'G' : 'g'; + lc = localeconv(); + spec->dot = lc->decimal_point; + spec->sep = lc->thousands_sep; + spec->grouping = lc->grouping; + if (mpd_validate_lconv(spec) < 0) { + return 0; /* GCOV_NOT_REACHED */ + } + } + + /* check correctness */ + if (*cp != '\0') { + return 0; + } + + return 1; +} + +/* + * The following functions assume that spec->min_width <= MPD_MAX_PREC, which + * is made sure in mpd_qformat_spec. Then, even with a spec that inserts a + * four-byte separator after each digit, nbytes in the following struct + * cannot overflow. + */ + +/* Multibyte string */ +typedef struct { + mpd_ssize_t nbytes; /* length in bytes */ + mpd_ssize_t nchars; /* length in chars */ + mpd_ssize_t cur; /* current write index */ + char *data; +} mpd_mbstr_t; + +static inline void +_mpd_bcopy(char *dest, const char *src, mpd_ssize_t n) +{ + while (--n >= 0) { + dest[n] = src[n]; + } +} + +static inline void +_mbstr_copy_char(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) +{ + dest->nbytes += n; + dest->nchars += (n > 0 ? 1 : 0); + dest->cur -= n; + + if (dest->data != NULL) { + _mpd_bcopy(dest->data+dest->cur, src, n); + } +} + +static inline void +_mbstr_copy_ascii(mpd_mbstr_t *dest, const char *src, mpd_ssize_t n) +{ + dest->nbytes += n; + dest->nchars += n; + dest->cur -= n; + + if (dest->data != NULL) { + _mpd_bcopy(dest->data+dest->cur, src, n); + } +} + +static inline void +_mbstr_copy_pad(mpd_mbstr_t *dest, mpd_ssize_t n) +{ + dest->nbytes += n; + dest->nchars += n; + dest->cur -= n; + + if (dest->data != NULL) { + char *cp = dest->data + dest->cur; + while (--n >= 0) { + cp[n] = '0'; + } + } +} + +/* + * Copy a numeric string to dest->data, adding separators in the integer + * part according to spec->grouping. If leading zero padding is enabled + * and the result is smaller than spec->min_width, continue adding zeros + * and separators until the minimum width is reached. + * + * The final length of dest->data is stored in dest->nbytes. The number + * of UTF-8 characters is stored in dest->nchars. + * + * First run (dest->data == NULL): determine the length of the result + * string and store it in dest->nbytes. + * + * Second run (write to dest->data): data is written in chunks and in + * reverse order, starting with the rest of the numeric string. + */ +static void +_mpd_add_sep_dot(mpd_mbstr_t *dest, + const char *sign, /* location of optional sign */ + const char *src, mpd_ssize_t n_src, /* integer part and length */ + const char *dot, /* location of optional decimal point */ + const char *rest, mpd_ssize_t n_rest, /* remaining part and length */ + const mpd_spec_t *spec) +{ + mpd_ssize_t n_sep, n_sign, consume; + const char *g; + int pad = 0; + + n_sign = sign ? 1 : 0; + n_sep = (mpd_ssize_t)strlen(spec->sep); + /* Initial write index: set to location of '\0' in the output string. + * Irrelevant for the first run. */ + dest->cur = dest->nbytes; + dest->nbytes = dest->nchars = 0; + + _mbstr_copy_ascii(dest, rest, n_rest); + + if (dot) { + _mbstr_copy_char(dest, dot, (mpd_ssize_t)strlen(dot)); + } + + g = spec->grouping; + consume = *g; + while (1) { + /* If the group length is 0 or CHAR_MAX or greater than the + * number of source bytes, consume all remaining bytes. */ + if (*g == 0 || *g == CHAR_MAX || consume > n_src) { + consume = n_src; + } + n_src -= consume; + if (pad) { + _mbstr_copy_pad(dest, consume); + } + else { + _mbstr_copy_ascii(dest, src+n_src, consume); + } + + if (n_src == 0) { + /* Either the real source of intpart digits or the virtual + * source of padding zeros is exhausted. */ + if (spec->align == 'z' && + dest->nchars + n_sign < spec->min_width) { + /* Zero padding is set and length < min_width: + * Generate n_src additional characters. */ + n_src = spec->min_width - (dest->nchars + n_sign); + /* Next iteration: + * case *g == 0 || *g == CHAR_MAX: + * consume all padding characters + * case consume < g*: + * fill remainder of current group + * case consume == g* + * copying is a no-op */ + consume = *g - consume; + /* Switch on virtual source of zeros. */ + pad = 1; + continue; + } + break; + } + + if (n_sep > 0) { + /* If padding is switched on, separators are counted + * as padding characters. This rule does not apply if + * the separator would be the first character of the + * result string. */ + if (pad && n_src > 1) n_src -= 1; + _mbstr_copy_char(dest, spec->sep, n_sep); + } + + /* If non-NUL, use the next value for grouping. */ + if (*g && *(g+1)) g++; + consume = *g; + } + + if (sign) { + _mbstr_copy_ascii(dest, sign, 1); + } + + if (dest->data) { + dest->data[dest->nbytes] = '\0'; + } +} + +/* + * Convert a numeric-string to its locale-specific appearance. + * The string must have one of these forms: + * + * 1) [sign] digits [exponent-part] + * 2) [sign] digits '.' [digits] [exponent-part] + * + * Not allowed, since _mpd_to_string() never returns this form: + * + * 3) [sign] '.' digits [exponent-part] + * + * Input: result->data := original numeric string (ASCII) + * result->bytes := strlen(result->data) + * result->nchars := strlen(result->data) + * + * Output: result->data := modified or original string + * result->bytes := strlen(result->data) + * result->nchars := number of characters (possibly UTF-8) + */ +static int +_mpd_apply_lconv(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) +{ + const char *sign = NULL, *intpart = NULL, *dot = NULL; + const char *rest, *dp; + char *decstring; + mpd_ssize_t n_int, n_rest; + + /* original numeric string */ + dp = result->data; + + /* sign */ + if (*dp == '+' || *dp == '-' || *dp == ' ') { + sign = dp++; + } + /* integer part */ + assert(isdigit((uchar)*dp)); + intpart = dp++; + while (isdigit((uchar)*dp)) { + dp++; + } + n_int = (mpd_ssize_t)(dp-intpart); + /* decimal point */ + if (*dp == '.') { + dp++; dot = spec->dot; + } + /* rest */ + rest = dp; + n_rest = result->nbytes - (mpd_ssize_t)(dp-result->data); + + if (dot == NULL && (*spec->sep == '\0' || *spec->grouping == '\0')) { + /* _mpd_add_sep_dot() would not change anything */ + return 1; + } + + /* Determine the size of the new decimal string after inserting the + * decimal point, optional separators and optional padding. */ + decstring = result->data; + result->data = NULL; + _mpd_add_sep_dot(result, sign, intpart, n_int, dot, + rest, n_rest, spec); + + result->data = mpd_alloc(result->nbytes+1, 1); + if (result->data == NULL) { + *status |= MPD_Malloc_error; + mpd_free(decstring); + return 0; + } + + /* Perform actual writes. */ + _mpd_add_sep_dot(result, sign, intpart, n_int, dot, + rest, n_rest, spec); + + mpd_free(decstring); + return 1; +} + +/* Add padding to the formatted string if necessary. */ +static int +_mpd_add_pad(mpd_mbstr_t *result, const mpd_spec_t *spec, uint32_t *status) +{ + if (result->nchars < spec->min_width) { + mpd_ssize_t add_chars, add_bytes; + size_t lpad = 0, rpad = 0; + size_t n_fill, len, i, j; + char align = spec->align; + uint8_t err = 0; + char *cp; + + n_fill = strlen(spec->fill); + add_chars = (spec->min_width - result->nchars); + /* max value: MPD_MAX_PREC * 4 */ + add_bytes = add_chars * (mpd_ssize_t)n_fill; + + cp = result->data = mpd_realloc(result->data, + result->nbytes+add_bytes+1, + sizeof *result->data, &err); + if (err) { + *status |= MPD_Malloc_error; + mpd_free(result->data); + return 0; + } + + if (align == 'z') { + align = '='; + } + + if (align == '<') { + rpad = add_chars; + } + else if (align == '>' || align == '=') { + lpad = add_chars; + } + else { /* align == '^' */ + lpad = add_chars/2; + rpad = add_chars-lpad; + } + + len = result->nbytes; + if (align == '=' && (*cp == '-' || *cp == '+' || *cp == ' ')) { + /* leave sign in the leading position */ + cp++; len--; + } + + memmove(cp+n_fill*lpad, cp, len); + for (i = 0; i < lpad; i++) { + for (j = 0; j < n_fill; j++) { + cp[i*n_fill+j] = spec->fill[j]; + } + } + cp += (n_fill*lpad + len); + for (i = 0; i < rpad; i++) { + for (j = 0; j < n_fill; j++) { + cp[i*n_fill+j] = spec->fill[j]; + } + } + + result->nbytes += add_bytes; + result->nchars += add_chars; + result->data[result->nbytes] = '\0'; + } + + return 1; +} + +/* Round a number to prec digits. The adjusted exponent stays the same + or increases by one if rounding up crosses a power of ten boundary. + If result->digits would exceed MPD_MAX_PREC+1, MPD_Invalid_operation + is set and the result is NaN. */ +static inline void +_mpd_round(mpd_t *result, const mpd_t *a, mpd_ssize_t prec, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_ssize_t exp = a->exp + a->digits - prec; + + if (prec <= 0) { + mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_NOT_REACHED */ + return; /* GCOV_NOT_REACHED */ + } + if (mpd_isspecial(a) || mpd_iszero(a)) { + mpd_qcopy(result, a, status); /* GCOV_NOT_REACHED */ + return; /* GCOV_NOT_REACHED */ + } + + mpd_qrescale_fmt(result, a, exp, ctx, status); + if (result->digits > prec) { + mpd_qrescale_fmt(result, result, exp+1, ctx, status); + } +} + +/* + * Return the string representation of an mpd_t, formatted according to 'spec'. + * The format specification is assumed to be valid. Memory errors are indicated + * as usual. This function is quiet. + */ +char * +mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_uint_t dt[MPD_MINALLOC_MAX]; + mpd_t tmp = {MPD_STATIC|MPD_STATIC_DATA,0,0,0,MPD_MINALLOC_MAX,dt}; + mpd_ssize_t dplace = MPD_DEFAULT_DOTPLACE; + mpd_mbstr_t result; + mpd_spec_t stackspec; + char type = spec->type; + int flags = 0; + + + if (spec->min_width > MPD_MAX_PREC) { + *status |= MPD_Invalid_operation; + return NULL; + } + + if (isupper((uchar)type)) { type = (char)tolower((uchar)type); - flags |= MPD_FMT_UPPER; - } - if (spec->sign == ' ') { - flags |= MPD_FMT_SIGN_SPACE; - } - else if (spec->sign == '+') { - flags |= MPD_FMT_SIGN_PLUS; - } - - if (mpd_isspecial(dec)) { - if (spec->align == 'z') { - stackspec = *spec; - stackspec.fill[0] = ' '; - stackspec.fill[1] = '\0'; - stackspec.align = '>'; - spec = &stackspec; - } + flags |= MPD_FMT_UPPER; + } + if (spec->sign == ' ') { + flags |= MPD_FMT_SIGN_SPACE; + } + else if (spec->sign == '+') { + flags |= MPD_FMT_SIGN_PLUS; + } + + if (mpd_isspecial(dec)) { + if (spec->align == 'z') { + stackspec = *spec; + stackspec.fill[0] = ' '; + stackspec.fill[1] = '\0'; + stackspec.align = '>'; + spec = &stackspec; + } assert(strlen(spec->fill) == 1); /* annotation for scan-build */ - if (type == '%') { - flags |= MPD_FMT_PERCENT; - } - } - else { - uint32_t workstatus = 0; - mpd_ssize_t prec; - - switch (type) { - case 'g': flags |= MPD_FMT_TOSCI; break; - case 'e': flags |= MPD_FMT_EXP; break; - case '%': flags |= MPD_FMT_PERCENT; - if (!mpd_qcopy(&tmp, dec, status)) { - return NULL; - } - tmp.exp += 2; - dec = &tmp; - type = 'f'; /* fall through */ - case 'f': flags |= MPD_FMT_FIXED; break; - default: abort(); /* debug: GCOV_NOT_REACHED */ - } - - if (spec->prec >= 0) { - if (spec->prec > MPD_MAX_PREC) { - *status |= MPD_Invalid_operation; - goto error; - } - - switch (type) { - case 'g': - prec = (spec->prec == 0) ? 1 : spec->prec; - if (dec->digits > prec) { - _mpd_round(&tmp, dec, prec, ctx, - &workstatus); - dec = &tmp; - } - break; - case 'e': - if (mpd_iszero(dec)) { - dplace = 1-spec->prec; - } - else { - _mpd_round(&tmp, dec, spec->prec+1, ctx, - &workstatus); - dec = &tmp; - } - break; - case 'f': - mpd_qrescale(&tmp, dec, -spec->prec, ctx, - &workstatus); - dec = &tmp; - break; - } - } - - if (type == 'f') { - if (mpd_iszero(dec) && dec->exp > 0) { - mpd_qrescale(&tmp, dec, 0, ctx, &workstatus); - dec = &tmp; - } - } - - if (workstatus&MPD_Errors) { - *status |= (workstatus&MPD_Errors); - goto error; - } - } - - /* - * At this point, for all scaled or non-scaled decimals: - * 1) 1 <= digits <= MAX_PREC+1 - * 2) adjexp(scaled) = adjexp(orig) [+1] - * 3) case 'g': MIN_ETINY <= exp <= MAX_EMAX+1 - * case 'e': MIN_ETINY-MAX_PREC <= exp <= MAX_EMAX+1 - * case 'f': MIN_ETINY <= exp <= MAX_EMAX+1 - * 4) max memory alloc in _mpd_to_string: - * case 'g': MAX_PREC+36 - * case 'e': MAX_PREC+36 - * case 'f': 2*MPD_MAX_PREC+30 - */ - result.nbytes = _mpd_to_string(&result.data, dec, flags, dplace); - result.nchars = result.nbytes; - if (result.nbytes < 0) { - *status |= MPD_Malloc_error; - goto error; - } - - if (*spec->dot != '\0' && !mpd_isspecial(dec)) { - if (result.nchars > MPD_MAX_PREC+36) { - /* Since a group length of one is not explicitly - * disallowed, ensure that it is always possible to - * insert a four byte separator after each digit. */ - *status |= MPD_Invalid_operation; - mpd_free(result.data); - goto error; - } - if (!_mpd_apply_lconv(&result, spec, status)) { - goto error; - } - } - - if (spec->min_width) { - if (!_mpd_add_pad(&result, spec, status)) { - goto error; - } - } - - mpd_del(&tmp); - return result.data; - -error: - mpd_del(&tmp); - return NULL; -} - -char * -mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_spec_t spec; - - if (!mpd_parse_fmt_str(&spec, fmt, 1)) { - *status |= MPD_Invalid_operation; - return NULL; - } - - return mpd_qformat_spec(dec, &spec, ctx, status); -} - -/* - * The specification has a *condition* called Invalid_operation and an - * IEEE *signal* called Invalid_operation. The former corresponds to - * MPD_Invalid_operation, the latter to MPD_IEEE_Invalid_operation. - * MPD_IEEE_Invalid_operation comprises the following conditions: - * - * [MPD_Conversion_syntax, MPD_Division_impossible, MPD_Division_undefined, - * MPD_Fpu_error, MPD_Invalid_context, MPD_Invalid_operation, - * MPD_Malloc_error] - * - * In the following functions, 'flag' denotes the condition, 'signal' - * denotes the IEEE signal. - */ - -static const char *mpd_flag_string[MPD_NUM_FLAGS] = { - "Clamped", - "Conversion_syntax", - "Division_by_zero", - "Division_impossible", - "Division_undefined", - "Fpu_error", - "Inexact", - "Invalid_context", - "Invalid_operation", - "Malloc_error", - "Not_implemented", - "Overflow", - "Rounded", - "Subnormal", - "Underflow", -}; - -static const char *mpd_signal_string[MPD_NUM_FLAGS] = { - "Clamped", - "IEEE_Invalid_operation", - "Division_by_zero", - "IEEE_Invalid_operation", - "IEEE_Invalid_operation", - "IEEE_Invalid_operation", - "Inexact", - "IEEE_Invalid_operation", - "IEEE_Invalid_operation", - "IEEE_Invalid_operation", - "Not_implemented", - "Overflow", - "Rounded", - "Subnormal", - "Underflow", -}; - -/* print conditions to buffer, separated by spaces */ -int -mpd_snprint_flags(char *dest, int nmemb, uint32_t flags) -{ - char *cp; - int n, j; - - assert(nmemb >= MPD_MAX_FLAG_STRING); - - *dest = '\0'; cp = dest; - for (j = 0; j < MPD_NUM_FLAGS; j++) { - if (flags & (1U<<j)) { - n = snprintf(cp, nmemb, "%s ", mpd_flag_string[j]); - if (n < 0 || n >= nmemb) return -1; - cp += n; nmemb -= n; - } - } - - if (cp != dest) { - *(--cp) = '\0'; - } - - return (int)(cp-dest); -} - -/* print conditions to buffer, in list form */ -int -mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]) -{ - char *cp; - int n, j; - - assert(nmemb >= MPD_MAX_FLAG_LIST); - if (flag_string == NULL) { - flag_string = mpd_flag_string; - } - - *dest = '['; - *(dest+1) = '\0'; - cp = dest+1; - --nmemb; - - for (j = 0; j < MPD_NUM_FLAGS; j++) { - if (flags & (1U<<j)) { - n = snprintf(cp, nmemb, "%s, ", flag_string[j]); - if (n < 0 || n >= nmemb) return -1; - cp += n; nmemb -= n; - } - } - - /* erase the last ", " */ - if (cp != dest+1) { - cp -= 2; - } - - *cp++ = ']'; - *cp = '\0'; - - return (int)(cp-dest); /* strlen, without NUL terminator */ -} - -/* print signals to buffer, in list form */ -int -mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]) -{ - char *cp; - int n, j; - int ieee_invalid_done = 0; - - assert(nmemb >= MPD_MAX_SIGNAL_LIST); - if (signal_string == NULL) { - signal_string = mpd_signal_string; - } - - *dest = '['; - *(dest+1) = '\0'; - cp = dest+1; - --nmemb; - - for (j = 0; j < MPD_NUM_FLAGS; j++) { - uint32_t f = flags & (1U<<j); - if (f) { - if (f&MPD_IEEE_Invalid_operation) { - if (ieee_invalid_done) { - continue; - } - ieee_invalid_done = 1; - } - n = snprintf(cp, nmemb, "%s, ", signal_string[j]); - if (n < 0 || n >= nmemb) return -1; - cp += n; nmemb -= n; - } - } - - /* erase the last ", " */ - if (cp != dest+1) { - cp -= 2; - } - - *cp++ = ']'; - *cp = '\0'; - - return (int)(cp-dest); /* strlen, without NUL terminator */ -} - -/* The following two functions are mainly intended for debugging. */ -void -mpd_fprint(FILE *file, const mpd_t *dec) -{ - char *decstring; - - decstring = mpd_to_sci(dec, 1); - if (decstring != NULL) { - fprintf(file, "%s\n", decstring); - mpd_free(decstring); - } - else { - fputs("mpd_fprint: output error\n", file); /* GCOV_NOT_REACHED */ - } -} - -void -mpd_print(const mpd_t *dec) -{ - char *decstring; - - decstring = mpd_to_sci(dec, 1); - if (decstring != NULL) { - printf("%s\n", decstring); - mpd_free(decstring); - } - else { - fputs("mpd_fprint: output error\n", stderr); /* GCOV_NOT_REACHED */ - } -} + if (type == '%') { + flags |= MPD_FMT_PERCENT; + } + } + else { + uint32_t workstatus = 0; + mpd_ssize_t prec; + + switch (type) { + case 'g': flags |= MPD_FMT_TOSCI; break; + case 'e': flags |= MPD_FMT_EXP; break; + case '%': flags |= MPD_FMT_PERCENT; + if (!mpd_qcopy(&tmp, dec, status)) { + return NULL; + } + tmp.exp += 2; + dec = &tmp; + type = 'f'; /* fall through */ + case 'f': flags |= MPD_FMT_FIXED; break; + default: abort(); /* debug: GCOV_NOT_REACHED */ + } + + if (spec->prec >= 0) { + if (spec->prec > MPD_MAX_PREC) { + *status |= MPD_Invalid_operation; + goto error; + } + + switch (type) { + case 'g': + prec = (spec->prec == 0) ? 1 : spec->prec; + if (dec->digits > prec) { + _mpd_round(&tmp, dec, prec, ctx, + &workstatus); + dec = &tmp; + } + break; + case 'e': + if (mpd_iszero(dec)) { + dplace = 1-spec->prec; + } + else { + _mpd_round(&tmp, dec, spec->prec+1, ctx, + &workstatus); + dec = &tmp; + } + break; + case 'f': + mpd_qrescale(&tmp, dec, -spec->prec, ctx, + &workstatus); + dec = &tmp; + break; + } + } + + if (type == 'f') { + if (mpd_iszero(dec) && dec->exp > 0) { + mpd_qrescale(&tmp, dec, 0, ctx, &workstatus); + dec = &tmp; + } + } + + if (workstatus&MPD_Errors) { + *status |= (workstatus&MPD_Errors); + goto error; + } + } + + /* + * At this point, for all scaled or non-scaled decimals: + * 1) 1 <= digits <= MAX_PREC+1 + * 2) adjexp(scaled) = adjexp(orig) [+1] + * 3) case 'g': MIN_ETINY <= exp <= MAX_EMAX+1 + * case 'e': MIN_ETINY-MAX_PREC <= exp <= MAX_EMAX+1 + * case 'f': MIN_ETINY <= exp <= MAX_EMAX+1 + * 4) max memory alloc in _mpd_to_string: + * case 'g': MAX_PREC+36 + * case 'e': MAX_PREC+36 + * case 'f': 2*MPD_MAX_PREC+30 + */ + result.nbytes = _mpd_to_string(&result.data, dec, flags, dplace); + result.nchars = result.nbytes; + if (result.nbytes < 0) { + *status |= MPD_Malloc_error; + goto error; + } + + if (*spec->dot != '\0' && !mpd_isspecial(dec)) { + if (result.nchars > MPD_MAX_PREC+36) { + /* Since a group length of one is not explicitly + * disallowed, ensure that it is always possible to + * insert a four byte separator after each digit. */ + *status |= MPD_Invalid_operation; + mpd_free(result.data); + goto error; + } + if (!_mpd_apply_lconv(&result, spec, status)) { + goto error; + } + } + + if (spec->min_width) { + if (!_mpd_add_pad(&result, spec, status)) { + goto error; + } + } + + mpd_del(&tmp); + return result.data; + +error: + mpd_del(&tmp); + return NULL; +} + +char * +mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_spec_t spec; + + if (!mpd_parse_fmt_str(&spec, fmt, 1)) { + *status |= MPD_Invalid_operation; + return NULL; + } + + return mpd_qformat_spec(dec, &spec, ctx, status); +} + +/* + * The specification has a *condition* called Invalid_operation and an + * IEEE *signal* called Invalid_operation. The former corresponds to + * MPD_Invalid_operation, the latter to MPD_IEEE_Invalid_operation. + * MPD_IEEE_Invalid_operation comprises the following conditions: + * + * [MPD_Conversion_syntax, MPD_Division_impossible, MPD_Division_undefined, + * MPD_Fpu_error, MPD_Invalid_context, MPD_Invalid_operation, + * MPD_Malloc_error] + * + * In the following functions, 'flag' denotes the condition, 'signal' + * denotes the IEEE signal. + */ + +static const char *mpd_flag_string[MPD_NUM_FLAGS] = { + "Clamped", + "Conversion_syntax", + "Division_by_zero", + "Division_impossible", + "Division_undefined", + "Fpu_error", + "Inexact", + "Invalid_context", + "Invalid_operation", + "Malloc_error", + "Not_implemented", + "Overflow", + "Rounded", + "Subnormal", + "Underflow", +}; + +static const char *mpd_signal_string[MPD_NUM_FLAGS] = { + "Clamped", + "IEEE_Invalid_operation", + "Division_by_zero", + "IEEE_Invalid_operation", + "IEEE_Invalid_operation", + "IEEE_Invalid_operation", + "Inexact", + "IEEE_Invalid_operation", + "IEEE_Invalid_operation", + "IEEE_Invalid_operation", + "Not_implemented", + "Overflow", + "Rounded", + "Subnormal", + "Underflow", +}; + +/* print conditions to buffer, separated by spaces */ +int +mpd_snprint_flags(char *dest, int nmemb, uint32_t flags) +{ + char *cp; + int n, j; + + assert(nmemb >= MPD_MAX_FLAG_STRING); + + *dest = '\0'; cp = dest; + for (j = 0; j < MPD_NUM_FLAGS; j++) { + if (flags & (1U<<j)) { + n = snprintf(cp, nmemb, "%s ", mpd_flag_string[j]); + if (n < 0 || n >= nmemb) return -1; + cp += n; nmemb -= n; + } + } + + if (cp != dest) { + *(--cp) = '\0'; + } + + return (int)(cp-dest); +} + +/* print conditions to buffer, in list form */ +int +mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]) +{ + char *cp; + int n, j; + + assert(nmemb >= MPD_MAX_FLAG_LIST); + if (flag_string == NULL) { + flag_string = mpd_flag_string; + } + + *dest = '['; + *(dest+1) = '\0'; + cp = dest+1; + --nmemb; + + for (j = 0; j < MPD_NUM_FLAGS; j++) { + if (flags & (1U<<j)) { + n = snprintf(cp, nmemb, "%s, ", flag_string[j]); + if (n < 0 || n >= nmemb) return -1; + cp += n; nmemb -= n; + } + } + + /* erase the last ", " */ + if (cp != dest+1) { + cp -= 2; + } + + *cp++ = ']'; + *cp = '\0'; + + return (int)(cp-dest); /* strlen, without NUL terminator */ +} + +/* print signals to buffer, in list form */ +int +mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]) +{ + char *cp; + int n, j; + int ieee_invalid_done = 0; + + assert(nmemb >= MPD_MAX_SIGNAL_LIST); + if (signal_string == NULL) { + signal_string = mpd_signal_string; + } + + *dest = '['; + *(dest+1) = '\0'; + cp = dest+1; + --nmemb; + + for (j = 0; j < MPD_NUM_FLAGS; j++) { + uint32_t f = flags & (1U<<j); + if (f) { + if (f&MPD_IEEE_Invalid_operation) { + if (ieee_invalid_done) { + continue; + } + ieee_invalid_done = 1; + } + n = snprintf(cp, nmemb, "%s, ", signal_string[j]); + if (n < 0 || n >= nmemb) return -1; + cp += n; nmemb -= n; + } + } + + /* erase the last ", " */ + if (cp != dest+1) { + cp -= 2; + } + + *cp++ = ']'; + *cp = '\0'; + + return (int)(cp-dest); /* strlen, without NUL terminator */ +} + +/* The following two functions are mainly intended for debugging. */ +void +mpd_fprint(FILE *file, const mpd_t *dec) +{ + char *decstring; + + decstring = mpd_to_sci(dec, 1); + if (decstring != NULL) { + fprintf(file, "%s\n", decstring); + mpd_free(decstring); + } + else { + fputs("mpd_fprint: output error\n", file); /* GCOV_NOT_REACHED */ + } +} + +void +mpd_print(const mpd_t *dec) +{ + char *decstring; + + decstring = mpd_to_sci(dec, 1); + if (decstring != NULL) { + printf("%s\n", decstring); + mpd_free(decstring); + } + else { + fputs("mpd_fprint: output error\n", stderr); /* GCOV_NOT_REACHED */ + } +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpalloc.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpalloc.h index 186808457b2..c8f7bbdf17a 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpalloc.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpalloc.h @@ -1,53 +1,53 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_MPALLOC_H_ #define LIBMPDEC_MPALLOC_H_ - - -#include "mpdecimal.h" - + + +#include "mpdecimal.h" + #include <stdint.h> + - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -int mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status); -int mpd_switch_to_dyn_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status); -int mpd_realloc_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status); - +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +int mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status); +int mpd_switch_to_dyn_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status); +int mpd_realloc_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status); + int mpd_switch_to_dyn_cxx(mpd_t *result, mpd_ssize_t size); int mpd_realloc_dyn_cxx(mpd_t *result, mpd_ssize_t size); + - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_MPALLOC_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpd_io.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpd_io.h index 79d7c05ce36..c1eb06918d6 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpd_io.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpd_io.h @@ -1,62 +1,62 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_IO_H_ #define LIBMPDEC_IO_H_ - - -#include "mpdecimal.h" - + + +#include "mpdecimal.h" + #include <stdint.h> + - -#if SIZE_MAX == MPD_SIZE_MAX - #define mpd_strtossize _mpd_strtossize -#else +#if SIZE_MAX == MPD_SIZE_MAX + #define mpd_strtossize _mpd_strtossize +#else #include <errno.h> -static inline mpd_ssize_t -mpd_strtossize(const char *s, char **end, int base) -{ - int64_t retval; - - errno = 0; - retval = _mpd_strtossize(s, end, base); - if (errno == 0 && (retval > MPD_SSIZE_MAX || retval < MPD_SSIZE_MIN)) { - errno = ERANGE; - } - if (errno == ERANGE) { - return (retval < 0) ? MPD_SSIZE_MIN : MPD_SSIZE_MAX; - } - - return (mpd_ssize_t)retval; -} -#endif - - +static inline mpd_ssize_t +mpd_strtossize(const char *s, char **end, int base) +{ + int64_t retval; + + errno = 0; + retval = _mpd_strtossize(s, end, base); + if (errno == 0 && (retval > MPD_SSIZE_MAX || retval < MPD_SSIZE_MIN)) { + errno = ERANGE; + } + if (errno == ERANGE) { + return (retval < 0) ? MPD_SSIZE_MIN : MPD_SSIZE_MAX; + } + + return (mpd_ssize_t)retval; +} +#endif + + #endif /* LIBMPDEC_IO_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.c index 28b639ccb40..7bef29482de 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.c @@ -1,521 +1,521 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> #include <limits.h> #include <math.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> -#include "basearith.h" -#include "bits.h" +#include "basearith.h" +#include "bits.h" #include "constants.h" -#include "convolute.h" -#include "crt.h" -#include "mpalloc.h" -#include "typearith.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 +#include "convolute.h" +#include "crt.h" +#include "mpalloc.h" +#include "typearith.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(__IBMC__) || 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' */ + #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' */ 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_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) -{ +{ (void)dec; - 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); -} - + 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 do not set the result no NaN on failure. */ static ALWAYS_INLINE int mpd_qresize_cxx(mpd_t *result, mpd_ssize_t nwords) @@ -538,738 +538,738 @@ mpd_qresize_cxx(mpd_t *result, mpd_ssize_t nwords) return mpd_realloc_dyn_cxx(result, nwords); } -/* 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]; - } - +/* 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_flags(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 -} - + 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 an int64_t, use a maxcontext for conversion */ void mpd_qset_i64_exact(mpd_t *result, int64_t a, uint32_t *status) @@ -1290,17 +1290,17 @@ mpd_qset_i64_exact(mpd_t *result, int64_t a, uint32_t *status) *status &= MPD_Errors; } -/* 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 -} +/* 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 +} /* quietly set a decimal from a uint64_t, use a maxcontext for conversion */ void @@ -1321,699 +1321,699 @@ mpd_qset_u64_exact(mpd_t *result, uint64_t a, uint32_t *status) } *status &= MPD_Errors; } -#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) -{ +#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) +{ uint32_t workstatus = 0; - mpd_uint_t u; - int isneg; - + mpd_uint_t u; + int isneg; + u = mpd_qabs_uint(a, &workstatus); if (workstatus&MPD_Invalid_operation) { *status |= workstatus; - 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) -{ + 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) +{ uint32_t workstatus = 0; uint64_t x = mpd_qget_uint(a, &workstatus); - + if (workstatus&MPD_Invalid_operation) { *status |= workstatus; - 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) -{ + 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) +{ uint32_t workstatus = 0; int64_t x = mpd_qget_ssize(a, &workstatus); - + if (workstatus&MPD_Invalid_operation) { *status |= workstatus; - 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 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) +{ uint32_t workstatus = 0; uint64_t x = _c32_qget_u64(1, a, &workstatus); *status |= workstatus; return x; -} - -/* quietly get an int64_t from a decimal */ -int64_t -mpd_qget_i64(const mpd_t *a, uint32_t *status) -{ +} + +/* quietly get an int64_t from a decimal */ +int64_t +mpd_qget_i64(const mpd_t *a, uint32_t *status) +{ uint32_t workstatus = 0; int64_t x = _c32_qget_i64(a, &workstatus); *status |= workstatus; return x; -} -#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; -} - +} +#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; +} + /* Same as mpd_qcopy, but do not set the result to NaN on failure. */ int mpd_qcopy_cxx(mpd_t *result, const mpd_t *a) @@ -2033,1849 +2033,1849 @@ mpd_qcopy_cxx(mpd_t *result, const mpd_t *a) 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) -{ +/* + * 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_NEW_STATIC(aa,0,0,0,0); MPD_NEW_STATIC(bb,0,0,0,0); uint32_t xstatus = 0; @@ -3942,4090 +3942,4090 @@ mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b, out: mpd_del(&aa); mpd_del(&bb); -} - -/* 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) { +} + +/* 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) { assert(b->data[0] != 0); /* annotation for scan-build */ - 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); -} - -/* Divide decimal by mpd_ssize_t. */ -void -mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -/* Divide decimal by mpd_uint_t. */ -void -mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -/* Divide decimal by int32_t. */ -void -mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qdiv_ssize(result, a, b, ctx, status); -} - -/* Divide decimal by uint32_t. */ -void -mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qdiv_uint(result, a, b, ctx, status); -} - -#ifdef CONFIG_64 -/* Divide decimal by int64_t. */ -void -mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qdiv_ssize(result, a, b, ctx, status); -} - -/* Divide decimal by uint64_t. */ -void -mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qdiv_uint(result, a, b, ctx, status); -} -#elif !defined(LEGACY_COMPILER) -/* Divide decimal by int64_t. */ -void -mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -/* Divide decimal by uint64_t. */ -void -mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); - mpd_del(&bb); -} -#endif - -/* Pad the result with trailing zeros if it has fewer digits than prec. */ -static void -_mpd_zeropad(mpd_t *result, const mpd_context_t *ctx, uint32_t *status) -{ - if (!mpd_isspecial(result) && !mpd_iszero(result) && - result->digits < ctx->prec) { - mpd_ssize_t shift = ctx->prec - result->digits; - mpd_qshiftl(result, result, shift, status); - result->exp -= shift; - } -} - -/* Check if the result is guaranteed to be one. */ -static int -_mpd_qexp_check_one(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - MPD_NEW_CONST(lim,0,-(ctx->prec+1),1,1,1,9); - MPD_NEW_SHARED(aa, a); - - mpd_set_positive(&aa); - - /* abs(a) <= 9 * 10**(-prec-1) */ - if (_mpd_cmp(&aa, &lim) <= 0) { - _settriple(result, 0, 1, 0); - *status |= MPD_Rounded|MPD_Inexact; - return 1; - } - - return 0; -} - -/* - * Get the number of iterations for the Horner scheme in _mpd_qexp(). - */ -static inline mpd_ssize_t -_mpd_get_exp_iterations(const mpd_t *r, mpd_ssize_t p) -{ - mpd_ssize_t log10pbyr; /* lower bound for log10(p / abs(r)) */ - mpd_ssize_t n; - - assert(p >= 10); - assert(!mpd_iszero(r)); - assert(-p < mpd_adjexp(r) && mpd_adjexp(r) <= -1); - -#ifdef CONFIG_64 - if (p > (mpd_ssize_t)(1ULL<<52)) { - return MPD_SSIZE_MAX; - } -#endif - - /* - * Lower bound for log10(p / abs(r)): adjexp(p) - (adjexp(r) + 1) - * At this point (for CONFIG_64, CONFIG_32 is not problematic): - * 1) 10 <= p <= 2**52 - * 2) -p < adjexp(r) <= -1 - * 3) 1 <= log10pbyr <= 2**52 + 14 - */ - log10pbyr = (mpd_word_digits(p)-1) - (mpd_adjexp(r)+1); - - /* - * The numerator in the paper is 1.435 * p - 1.182, calculated - * exactly. We compensate for rounding errors by using 1.43503. - * ACL2 proofs: - * 1) exp-iter-approx-lower-bound: The term below evaluated - * in 53-bit floating point arithmetic is greater than or - * equal to the exact term used in the paper. - * 2) exp-iter-approx-upper-bound: The term below is less than - * or equal to 3/2 * p <= 3/2 * 2**52. - */ - n = (mpd_ssize_t)ceil((1.43503*(double)p - 1.182) / (double)log10pbyr); - return n >= 3 ? n : 3; -} - -/* - * Internal function, specials have been dealt with. Apart from Overflow - * and Underflow, two cases must be considered for the error of the result: - * - * 1) abs(a) <= 9 * 10**(-prec-1) ==> result == 1 - * - * Absolute error: abs(1 - e**x) < 10**(-prec) - * ------------------------------------------- - * - * 2) abs(a) > 9 * 10**(-prec-1) - * - * Relative error: abs(result - e**x) < 0.5 * 10**(-prec) * e**x - * ------------------------------------------------------------- - * - * The algorithm is from Hull&Abrham, Variable Precision Exponential Function, - * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986. - * - * Main differences: - * - * - The number of iterations for the Horner scheme is calculated using - * 53-bit floating point arithmetic. - * - * - In the error analysis for ER (relative error accumulated in the - * evaluation of the truncated series) the reduced operand r may - * have any number of digits. - * ACL2 proof: exponent-relative-error - * - * - The analysis for early abortion has been adapted for the mpd_t - * ranges. - */ -static void -_mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_STATIC(tmp,0,0,0,0); - MPD_NEW_STATIC(sum,0,0,0,0); - MPD_NEW_CONST(word,0,0,1,1,1,1); - mpd_ssize_t j, n, t; - - assert(!mpd_isspecial(a)); - - if (mpd_iszerocoeff(a)) { - _settriple(result, MPD_POS, 1, 0); - return; - } - - /* - * We are calculating e^x = e^(r*10^t) = (e^r)^(10^t), where abs(r) < 1 and t >= 0. - * - * If t > 0, we have: - * - * (1) 0.1 <= r < 1, so e^0.1 <= e^r. If t > MAX_T, overflow occurs: - * - * MAX-EMAX+1 < log10(e^(0.1*10*t)) <= log10(e^(r*10^t)) < adjexp(e^(r*10^t))+1 - * - * (2) -1 < r <= -0.1, so e^r <= e^-0.1. If t > MAX_T, underflow occurs: - * - * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t)) < MIN-ETINY - */ -#if defined(CONFIG_64) - #define MPD_EXP_MAX_T 19 -#elif defined(CONFIG_32) - #define MPD_EXP_MAX_T 10 -#endif - t = a->digits + a->exp; - t = (t > 0) ? t : 0; - if (t > MPD_EXP_MAX_T) { - if (mpd_ispositive(a)) { - mpd_setspecial(result, MPD_POS, MPD_INF); - *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; - } - else { - _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); - *status |= (MPD_Inexact|MPD_Rounded|MPD_Subnormal| - MPD_Underflow|MPD_Clamped); - } - return; - } - - /* abs(a) <= 9 * 10**(-prec-1) */ - if (_mpd_qexp_check_one(result, a, ctx, status)) { - return; - } - - mpd_maxcontext(&workctx); - workctx.prec = ctx->prec + t + 2; - workctx.prec = (workctx.prec < 10) ? 10 : workctx.prec; - workctx.round = MPD_ROUND_HALF_EVEN; - - if (!mpd_qcopy(result, a, status)) { - return; - } - result->exp -= t; - - /* - * At this point: - * 1) 9 * 10**(-prec-1) < abs(a) - * 2) 9 * 10**(-prec-t-1) < abs(r) - * 3) log10(9) - prec - t - 1 < log10(abs(r)) < adjexp(abs(r)) + 1 - * 4) - prec - t - 2 < adjexp(abs(r)) <= -1 - */ - n = _mpd_get_exp_iterations(result, workctx.prec); - if (n == MPD_SSIZE_MAX) { - mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_UNLIKELY */ - return; /* GCOV_UNLIKELY */ - } - - _settriple(&sum, MPD_POS, 1, 0); - - for (j = n-1; j >= 1; j--) { - word.data[0] = j; - mpd_setdigits(&word); - mpd_qdiv(&tmp, result, &word, &workctx, &workctx.status); - mpd_qfma(&sum, &sum, &tmp, &one, &workctx, &workctx.status); - } - -#ifdef CONFIG_64 - _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); -#else - if (t <= MPD_MAX_POW10) { - _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); - } - else { - t -= MPD_MAX_POW10; - _mpd_qpow_uint(&tmp, &sum, mpd_pow10[MPD_MAX_POW10], MPD_POS, - &workctx, status); - _mpd_qpow_uint(result, &tmp, mpd_pow10[t], MPD_POS, &workctx, status); - } -#endif - - mpd_del(&tmp); - mpd_del(&sum); - *status |= (workctx.status&MPD_Errors); - *status |= (MPD_Inexact|MPD_Rounded); -} - -/* exp(a) */ -void -mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - if (mpd_isnegative(a)) { - _settriple(result, MPD_POS, 0, 0); - } - else { - mpd_setspecial(result, MPD_POS, MPD_INF); - } - return; - } - if (mpd_iszerocoeff(a)) { - _settriple(result, MPD_POS, 1, 0); - return; - } - - workctx = *ctx; - workctx.round = MPD_ROUND_HALF_EVEN; - - if (ctx->allcr) { - MPD_NEW_STATIC(t1, 0,0,0,0); - MPD_NEW_STATIC(t2, 0,0,0,0); - MPD_NEW_STATIC(ulp, 0,0,0,0); - MPD_NEW_STATIC(aa, 0,0,0,0); - mpd_ssize_t prec; - mpd_ssize_t ulpexp; - uint32_t workstatus; - - if (result == a) { - if (!mpd_qcopy(&aa, a, status)) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - a = &aa; - } - - workctx.clamp = 0; - prec = ctx->prec + 3; - while (1) { - workctx.prec = prec; - workstatus = 0; - - _mpd_qexp(result, a, &workctx, &workstatus); - *status |= workstatus; - - ulpexp = result->exp + result->digits - workctx.prec; - if (workstatus & MPD_Underflow) { - /* The effective work precision is result->digits. */ - ulpexp = result->exp; - } - _ssettriple(&ulp, MPD_POS, 1, ulpexp); - - /* - * At this point [1]: - * 1) abs(result - e**x) < 0.5 * 10**(-prec) * e**x - * 2) result - ulp < e**x < result + ulp - * 3) result - ulp < result < result + ulp - * - * If round(result-ulp)==round(result+ulp), then - * round(result)==round(e**x). Therefore the result - * is correctly rounded. - * - * [1] If abs(a) <= 9 * 10**(-prec-1), use the absolute - * error for a similar argument. - */ - workctx.prec = ctx->prec; - mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); - mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); - if (mpd_isspecial(result) || mpd_iszerocoeff(result) || - mpd_qcmp(&t1, &t2, status) == 0) { - workctx.clamp = ctx->clamp; - _mpd_zeropad(result, &workctx, status); - mpd_check_underflow(result, &workctx, status); - mpd_qfinalize(result, &workctx, status); - break; - } - prec += MPD_RDIGITS; - } - mpd_del(&t1); - mpd_del(&t2); - mpd_del(&ulp); - mpd_del(&aa); - } - else { - _mpd_qexp(result, a, &workctx, status); - _mpd_zeropad(result, &workctx, status); - mpd_check_underflow(result, &workctx, status); - mpd_qfinalize(result, &workctx, status); - } -} - -/* Fused multiply-add: (a * b) + c, with a single final rounding. */ -void -mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, - const mpd_context_t *ctx, uint32_t *status) -{ - uint32_t workstatus = 0; - mpd_t *cc = NULL; - - if (result == c) { - if ((cc = mpd_qncopy(c)) == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - c = cc; - } - - _mpd_qmul(result, a, b, ctx, &workstatus); - if (!(workstatus&MPD_Invalid_operation)) { - mpd_qadd(result, result, c, ctx, &workstatus); - } - - if (cc) mpd_del(cc); - *status |= workstatus; -} - -/* - * Schedule the optimal precision increase for the Newton iteration. - * v := input operand - * z_0 := initial approximation - * initprec := natural number such that abs(log(v) - z_0) < 10**-initprec - * maxprec := target precision - * - * For convenience the output klist contains the elements in reverse order: - * klist := [k_n-1, ..., k_0], where - * 1) k_0 <= initprec and - * 2) abs(log(v) - result) < 10**(-2*k_n-1 + 1) <= 10**-maxprec. - */ -static inline int -ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec, - mpd_ssize_t initprec) -{ - mpd_ssize_t k; - int i; - - assert(maxprec >= 2 && initprec >= 2); - if (maxprec <= initprec) return -1; - - i = 0; k = maxprec; - do { - k = (k+2) / 2; - klist[i++] = k; - } while (k > initprec); - - return i-1; -} - -/* The constants have been verified with both decimal.py and mpfr. */ -#ifdef CONFIG_64 -#if MPD_RDIGITS != 19 - #error "mpdecimal.c: MPD_RDIGITS must be 19." -#endif -static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { - 6983716328982174407ULL, 9089704281976336583ULL, 1515961135648465461ULL, - 4416816335727555703ULL, 2900988039194170265ULL, 2307925037472986509ULL, - 107598438319191292ULL, 3466624107184669231ULL, 4450099781311469159ULL, - 9807828059751193854ULL, 7713456862091670584ULL, 1492198849978748873ULL, - 6528728696511086257ULL, 2385392051446341972ULL, 8692180205189339507ULL, - 6518769751037497088ULL, 2375253577097505395ULL, 9095610299291824318ULL, - 982748238504564801ULL, 5438635917781170543ULL, 7547331541421808427ULL, - 752371033310119785ULL, 3171643095059950878ULL, 9785265383207606726ULL, - 2932258279850258550ULL, 5497347726624257094ULL, 2976979522110718264ULL, - 9221477656763693866ULL, 1979650047149510504ULL, 6674183485704422507ULL, - 9702766860595249671ULL, 9278096762712757753ULL, 9314848524948644871ULL, - 6826928280848118428ULL, 754403708474699401ULL, 230105703089634572ULL, - 1929203337658714166ULL, 7589402567763113569ULL, 4208241314695689016ULL, - 2922455440575892572ULL, 9356734206705811364ULL, 2684916746550586856ULL, - 644507064800027750ULL, 9476834636167921018ULL, 5659121373450747856ULL, - 2835522011480466371ULL, 6470806855677432162ULL, 7141748003688084012ULL, - 9619404400222105101ULL, 5504893431493939147ULL, 6674744042432743651ULL, - 2287698219886746543ULL, 7773262884616336622ULL, 1985283935053089653ULL, - 4680843799894826233ULL, 8168948290720832555ULL, 8067566662873690987ULL, - 6248633409525465082ULL, 9829834196778404228ULL, 3524802359972050895ULL, - 3327900967572609677ULL, 110148862877297603ULL, 179914546843642076ULL, - 2302585092994045684ULL -}; -#else -#if MPD_RDIGITS != 9 - #error "mpdecimal.c: MPD_RDIGITS must be 9." -#endif -static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { - 401682692UL, 708474699UL, 720754403UL, 30896345UL, 602301057UL, 765871416UL, - 192920333UL, 763113569UL, 589402567UL, 956890167UL, 82413146UL, 589257242UL, - 245544057UL, 811364292UL, 734206705UL, 868569356UL, 167465505UL, 775026849UL, - 706480002UL, 18064450UL, 636167921UL, 569476834UL, 734507478UL, 156591213UL, - 148046637UL, 283552201UL, 677432162UL, 470806855UL, 880840126UL, 417480036UL, - 210510171UL, 940440022UL, 939147961UL, 893431493UL, 436515504UL, 440424327UL, - 654366747UL, 821988674UL, 622228769UL, 884616336UL, 537773262UL, 350530896UL, - 319852839UL, 989482623UL, 468084379UL, 720832555UL, 168948290UL, 736909878UL, - 675666628UL, 546508280UL, 863340952UL, 404228624UL, 834196778UL, 508959829UL, - 23599720UL, 967735248UL, 96757260UL, 603332790UL, 862877297UL, 760110148UL, - 468436420UL, 401799145UL, 299404568UL, 230258509UL -}; -#endif -/* _mpd_ln10 is used directly for precisions smaller than MINALLOC_MAX*RDIGITS. - Otherwise, it serves as the initial approximation for calculating ln(10). */ -static const mpd_t _mpd_ln10 = { - MPD_STATIC|MPD_CONST_DATA, -(MPD_MINALLOC_MAX*MPD_RDIGITS-1), - MPD_MINALLOC_MAX*MPD_RDIGITS, MPD_MINALLOC_MAX, MPD_MINALLOC_MAX, - (mpd_uint_t *)mpd_ln10_data -}; - -/* - * Set 'result' to log(10). - * Ulp error: abs(result - log(10)) < ulp(log(10)) - * Relative error: abs(result - log(10)) < 5 * 10**-prec * log(10) - * - * NOTE: The relative error is not derived from the ulp error, but - * calculated separately using the fact that 23/10 < log(10) < 24/10. - */ -void -mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status) -{ - mpd_context_t varcontext, maxcontext; - MPD_NEW_STATIC(tmp, 0,0,0,0); - MPD_NEW_CONST(static10, 0,0,2,1,1,10); - mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; - mpd_uint_t rnd; - mpd_ssize_t shift; - int i; - - assert(prec >= 1); - - shift = MPD_MINALLOC_MAX*MPD_RDIGITS-prec; - shift = shift < 0 ? 0 : shift; - - rnd = mpd_qshiftr(result, &_mpd_ln10, shift, status); - if (rnd == MPD_UINT_MAX) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - result->exp = -(result->digits-1); - - mpd_maxcontext(&maxcontext); - if (prec < MPD_MINALLOC_MAX*MPD_RDIGITS) { - maxcontext.prec = prec; - _mpd_apply_round_excess(result, rnd, &maxcontext, status); - *status |= (MPD_Inexact|MPD_Rounded); - return; - } - - mpd_maxcontext(&varcontext); - varcontext.round = MPD_ROUND_TRUNC; - - i = ln_schedule_prec(klist, prec+2, -result->exp); - for (; i >= 0; i--) { - varcontext.prec = 2*klist[i]+3; - result->flags ^= MPD_NEG; - _mpd_qexp(&tmp, result, &varcontext, status); - result->flags ^= MPD_NEG; - mpd_qmul(&tmp, &static10, &tmp, &varcontext, status); - mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); - mpd_qadd(result, result, &tmp, &maxcontext, status); - if (mpd_isspecial(result)) { - break; - } - } - - mpd_del(&tmp); - maxcontext.prec = prec; - mpd_qfinalize(result, &maxcontext, status); -} - -/* - * Initial approximations for the ln() iteration. The values have the - * following properties (established with both decimal.py and mpfr): - * - * Index 0 - 400, logarithms of x in [1.00, 5.00]: - * abs(lnapprox[i] * 10**-3 - log((i+100)/100)) < 10**-2 - * abs(lnapprox[i] * 10**-3 - log((i+1+100)/100)) < 10**-2 - * - * Index 401 - 899, logarithms of x in (0.500, 0.999]: - * abs(-lnapprox[i] * 10**-3 - log((i+100)/1000)) < 10**-2 - * abs(-lnapprox[i] * 10**-3 - log((i+1+100)/1000)) < 10**-2 - */ -static const uint16_t lnapprox[900] = { - /* index 0 - 400: log((i+100)/100) * 1000 */ - 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157, - 166, 174, 182, 191, 199, 207, 215, 223, 231, 239, 247, 255, 262, 270, 278, - 285, 293, 300, 308, 315, 322, 329, 336, 344, 351, 358, 365, 372, 378, 385, - 392, 399, 406, 412, 419, 425, 432, 438, 445, 451, 457, 464, 470, 476, 482, - 489, 495, 501, 507, 513, 519, 525, 531, 536, 542, 548, 554, 560, 565, 571, - 577, 582, 588, 593, 599, 604, 610, 615, 621, 626, 631, 637, 642, 647, 652, - 658, 663, 668, 673, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728, - 732, 737, 742, 747, 751, 756, 761, 766, 770, 775, 779, 784, 788, 793, 798, - 802, 806, 811, 815, 820, 824, 829, 833, 837, 842, 846, 850, 854, 859, 863, - 867, 871, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924, - 928, 932, 936, 940, 944, 948, 952, 956, 959, 963, 967, 971, 975, 978, 982, - 986, 990, 993, 997, 1001, 1004, 1008, 1012, 1015, 1019, 1022, 1026, 1030, - 1033, 1037, 1040, 1044, 1047, 1051, 1054, 1058, 1061, 1065, 1068, 1072, 1075, - 1078, 1082, 1085, 1089, 1092, 1095, 1099, 1102, 1105, 1109, 1112, 1115, 1118, - 1122, 1125, 1128, 1131, 1135, 1138, 1141, 1144, 1147, 1151, 1154, 1157, 1160, - 1163, 1166, 1169, 1172, 1176, 1179, 1182, 1185, 1188, 1191, 1194, 1197, 1200, - 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227, 1230, 1233, 1235, 1238, - 1241, 1244, 1247, 1250, 1253, 1256, 1258, 1261, 1264, 1267, 1270, 1273, 1275, - 1278, 1281, 1284, 1286, 1289, 1292, 1295, 1297, 1300, 1303, 1306, 1308, 1311, - 1314, 1316, 1319, 1322, 1324, 1327, 1330, 1332, 1335, 1338, 1340, 1343, 1345, - 1348, 1351, 1353, 1356, 1358, 1361, 1364, 1366, 1369, 1371, 1374, 1376, 1379, - 1381, 1384, 1386, 1389, 1391, 1394, 1396, 1399, 1401, 1404, 1406, 1409, 1411, - 1413, 1416, 1418, 1421, 1423, 1426, 1428, 1430, 1433, 1435, 1437, 1440, 1442, - 1445, 1447, 1449, 1452, 1454, 1456, 1459, 1461, 1463, 1466, 1468, 1470, 1472, - 1475, 1477, 1479, 1482, 1484, 1486, 1488, 1491, 1493, 1495, 1497, 1500, 1502, - 1504, 1506, 1509, 1511, 1513, 1515, 1517, 1520, 1522, 1524, 1526, 1528, 1530, - 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1548, 1550, 1552, 1554, 1556, 1558, - 1560, 1562, 1564, 1567, 1569, 1571, 1573, 1575, 1577, 1579, 1581, 1583, 1585, - 1587, 1589, 1591, 1593, 1595, 1597, 1599, 1601, 1603, 1605, 1607, 1609, - /* index 401 - 899: -log((i+100)/1000) * 1000 */ - 691, 689, 687, 685, 683, 681, 679, 677, 675, 673, 671, 669, 668, 666, 664, - 662, 660, 658, 656, 654, 652, 650, 648, 646, 644, 642, 641, 639, 637, 635, - 633, 631, 629, 627, 626, 624, 622, 620, 618, 616, 614, 612, 611, 609, 607, - 605, 603, 602, 600, 598, 596, 594, 592, 591, 589, 587, 585, 583, 582, 580, - 578, 576, 574, 573, 571, 569, 567, 566, 564, 562, 560, 559, 557, 555, 553, - 552, 550, 548, 546, 545, 543, 541, 540, 538, 536, 534, 533, 531, 529, 528, - 526, 524, 523, 521, 519, 518, 516, 514, 512, 511, 509, 508, 506, 504, 502, - 501, 499, 498, 496, 494, 493, 491, 489, 488, 486, 484, 483, 481, 480, 478, - 476, 475, 473, 472, 470, 468, 467, 465, 464, 462, 460, 459, 457, 456, 454, - 453, 451, 449, 448, 446, 445, 443, 442, 440, 438, 437, 435, 434, 432, 431, - 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 410, 408, - 406, 405, 404, 402, 400, 399, 398, 396, 394, 393, 392, 390, 389, 387, 386, - 384, 383, 381, 380, 378, 377, 375, 374, 372, 371, 370, 368, 367, 365, 364, - 362, 361, 360, 358, 357, 355, 354, 352, 351, 350, 348, 347, 345, 344, 342, - 341, 340, 338, 337, 336, 334, 333, 331, 330, 328, 327, 326, 324, 323, 322, - 320, 319, 318, 316, 315, 313, 312, 311, 309, 308, 306, 305, 304, 302, 301, - 300, 298, 297, 296, 294, 293, 292, 290, 289, 288, 286, 285, 284, 282, 281, - 280, 278, 277, 276, 274, 273, 272, 270, 269, 268, 267, 265, 264, 263, 261, - 260, 259, 258, 256, 255, 254, 252, 251, 250, 248, 247, 246, 245, 243, 242, - 241, 240, 238, 237, 236, 234, 233, 232, 231, 229, 228, 227, 226, 224, 223, - 222, 221, 219, 218, 217, 216, 214, 213, 212, 211, 210, 208, 207, 206, 205, - 203, 202, 201, 200, 198, 197, 196, 195, 194, 192, 191, 190, 189, 188, 186, - 185, 184, 183, 182, 180, 179, 178, 177, 176, 174, 173, 172, 171, 170, 168, - 167, 166, 165, 164, 162, 161, 160, 159, 158, 157, 156, 154, 153, 152, 151, - 150, 148, 147, 146, 145, 144, 143, 142, 140, 139, 138, 137, 136, 135, 134, - 132, 131, 130, 129, 128, 127, 126, 124, 123, 122, 121, 120, 119, 118, 116, - 115, 114, 113, 112, 111, 110, 109, 108, 106, 105, 104, 103, 102, 101, 100, - 99, 98, 97, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79, - 78, 77, 76, 75, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, - 58, 57, 56, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, - 38, 37, 36, 35, 34, 33, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, - 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 -}; - -/* - * Internal ln() function that does not check for specials, zero or one. - * Relative error: abs(result - log(a)) < 0.1 * 10**-prec * abs(log(a)) - */ -static void -_mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t varcontext, maxcontext; - mpd_t *z = (mpd_t *) result; - MPD_NEW_STATIC(v,0,0,0,0); - MPD_NEW_STATIC(vtmp,0,0,0,0); - MPD_NEW_STATIC(tmp,0,0,0,0); - mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; - mpd_ssize_t maxprec, shift, t; - mpd_ssize_t a_digits, a_exp; - mpd_uint_t dummy, x; - int i; - - assert(!mpd_isspecial(a) && !mpd_iszerocoeff(a)); - - /* - * We are calculating ln(a) = ln(v * 10^t) = ln(v) + t*ln(10), - * where 0.5 < v <= 5. - */ - if (!mpd_qcopy(&v, a, status)) { - mpd_seterror(result, MPD_Malloc_error, status); - goto finish; - } - - /* Initial approximation: we have at least one non-zero digit */ - _mpd_get_msdigits(&dummy, &x, &v, 3); - if (x < 10) x *= 10; - if (x < 100) x *= 10; - x -= 100; - - /* a may equal z */ - a_digits = a->digits; - a_exp = a->exp; - - mpd_minalloc(z); - mpd_clear_flags(z); - z->data[0] = lnapprox[x]; - z->len = 1; - z->exp = -3; - mpd_setdigits(z); - - if (x <= 400) { - /* Reduce the input operand to 1.00 <= v <= 5.00. Let y = x + 100, - * so 100 <= y <= 500. Since y contains the most significant digits - * of v, y/100 <= v < (y+1)/100 and abs(z - log(v)) < 10**-2. */ - v.exp = -(a_digits - 1); - t = a_exp + a_digits - 1; - } - else { - /* Reduce the input operand to 0.500 < v <= 0.999. Let y = x + 100, - * so 500 < y <= 999. Since y contains the most significant digits - * of v, y/1000 <= v < (y+1)/1000 and abs(z - log(v)) < 10**-2. */ - v.exp = -a_digits; - t = a_exp + a_digits; - mpd_set_negative(z); - } - - mpd_maxcontext(&maxcontext); - mpd_maxcontext(&varcontext); - varcontext.round = MPD_ROUND_TRUNC; - - maxprec = ctx->prec + 2; - if (t == 0 && (x <= 15 || x >= 800)) { - /* 0.900 <= v <= 1.15: Estimate the magnitude of the logarithm. - * If ln(v) will underflow, skip the loop. Otherwise, adjust the - * precision upwards in order to obtain a sufficient number of - * significant digits. - * - * Case v > 1: - * abs((v-1)/10) < abs((v-1)/v) < abs(ln(v)) < abs(v-1) - * Case v < 1: - * abs(v-1) < abs(ln(v)) < abs((v-1)/v) < abs((v-1)*10) - */ - int cmp = _mpd_cmp(&v, &one); - - /* Upper bound (assume v > 1): abs(v-1), unrounded */ - _mpd_qsub(&tmp, &v, &one, &maxcontext, &maxcontext.status); - if (maxcontext.status & MPD_Errors) { - mpd_seterror(result, MPD_Malloc_error, status); - goto finish; - } - - if (cmp < 0) { - /* v < 1: abs((v-1)*10) */ - tmp.exp += 1; - } - if (mpd_adjexp(&tmp) < mpd_etiny(ctx)) { - /* The upper bound is less than etiny: Underflow to zero */ - _settriple(result, (cmp<0), 1, mpd_etiny(ctx)-1); - goto finish; - } - /* Lower bound: abs((v-1)/10) or abs(v-1) */ - tmp.exp -= 1; - if (mpd_adjexp(&tmp) < 0) { - /* Absolute error of the loop: abs(z - log(v)) < 10**-p. If - * p = ctx->prec+2-adjexp(lower), then the relative error of - * the result is (using 10**adjexp(x) <= abs(x)): - * - * abs(z - log(v)) / abs(log(v)) < 10**-p / abs(log(v)) - * <= 10**(-ctx->prec-2) - */ - maxprec = maxprec - mpd_adjexp(&tmp); - } - } - - i = ln_schedule_prec(klist, maxprec, 2); - for (; i >= 0; i--) { - varcontext.prec = 2*klist[i]+3; - z->flags ^= MPD_NEG; - _mpd_qexp(&tmp, z, &varcontext, status); - z->flags ^= MPD_NEG; - - if (v.digits > varcontext.prec) { - shift = v.digits - varcontext.prec; - mpd_qshiftr(&vtmp, &v, shift, status); - vtmp.exp += shift; - mpd_qmul(&tmp, &vtmp, &tmp, &varcontext, status); - } - else { - mpd_qmul(&tmp, &v, &tmp, &varcontext, status); - } - - mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); - mpd_qadd(z, z, &tmp, &maxcontext, status); - if (mpd_isspecial(z)) { - break; - } - } - - /* - * Case t == 0: - * t * log(10) == 0, the result does not change and the analysis - * above applies. If v < 0.900 or v > 1.15, the relative error is - * less than 10**(-ctx.prec-1). - * Case t != 0: - * z := approx(log(v)) - * y := approx(log(10)) - * p := maxprec = ctx->prec + 2 - * Absolute errors: - * 1) abs(z - log(v)) < 10**-p - * 2) abs(y - log(10)) < 10**-p - * The multiplication is exact, so: - * 3) abs(t*y - t*log(10)) < t*10**-p - * The sum is exact, so: - * 4) abs((z + t*y) - (log(v) + t*log(10))) < (abs(t) + 1) * 10**-p - * Bounds for log(v) and log(10): - * 5) -7/10 < log(v) < 17/10 - * 6) 23/10 < log(10) < 24/10 - * Using 4), 5), 6) and t != 0, the relative error is: - * - * 7) relerr < ((abs(t) + 1)*10**-p) / abs(log(v) + t*log(10)) - * < 0.5 * 10**(-p + 1) = 0.5 * 10**(-ctx->prec-1) - */ - mpd_qln10(&v, maxprec+1, status); - mpd_qmul_ssize(&tmp, &v, t, &maxcontext, status); - mpd_qadd(result, &tmp, z, &maxcontext, status); - - -finish: - *status |= (MPD_Inexact|MPD_Rounded); - mpd_del(&v); - mpd_del(&vtmp); - mpd_del(&tmp); -} - -/* ln(a) */ -void -mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - mpd_ssize_t adjexp, t; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - mpd_setspecial(result, MPD_POS, MPD_INF); - return; - } - if (mpd_iszerocoeff(a)) { - mpd_setspecial(result, MPD_NEG, MPD_INF); - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (_mpd_cmp(a, &one) == 0) { - _settriple(result, MPD_POS, 0, 0); - return; - } - /* - * Check if the result will overflow (0 < x, x != 1): - * 1) log10(x) < 0 iff adjexp(x) < 0 - * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) - * 3) 0 < x /\ x != 1 ==> 2 * abs(log10(x)) < abs(log(x)) - * 4) adjexp(x) <= log10(x) < adjexp(x) + 1 - * - * Case adjexp(x) >= 0: - * 5) 2 * adjexp(x) < abs(log(x)) - * Case adjexp(x) > 0: - * 6) adjexp(2 * adjexp(x)) <= adjexp(abs(log(x))) - * Case adjexp(x) == 0: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) - * - * Case adjexp(x) < 0: - * 7) 2 * (-adjexp(x) - 1) < abs(log(x)) - * Case adjexp(x) < -1: - * 8) adjexp(2 * (-adjexp(x) - 1)) <= adjexp(abs(log(x))) - * Case adjexp(x) == -1: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) - */ - adjexp = mpd_adjexp(a); - t = (adjexp < 0) ? -adjexp-1 : adjexp; - t *= 2; - if (mpd_exp_digits(t)-1 > ctx->emax) { - *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; - mpd_setspecial(result, (adjexp<0), MPD_INF); - return; - } - - workctx = *ctx; - workctx.round = MPD_ROUND_HALF_EVEN; - - if (ctx->allcr) { - MPD_NEW_STATIC(t1, 0,0,0,0); - MPD_NEW_STATIC(t2, 0,0,0,0); - MPD_NEW_STATIC(ulp, 0,0,0,0); - MPD_NEW_STATIC(aa, 0,0,0,0); - mpd_ssize_t prec; - - if (result == a) { - if (!mpd_qcopy(&aa, a, status)) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - a = &aa; - } - - workctx.clamp = 0; - prec = ctx->prec + 3; - while (1) { - workctx.prec = prec; - _mpd_qln(result, a, &workctx, status); - _ssettriple(&ulp, MPD_POS, 1, - result->exp + result->digits-workctx.prec); - - workctx.prec = ctx->prec; - mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); - mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); - if (mpd_isspecial(result) || mpd_iszerocoeff(result) || - mpd_qcmp(&t1, &t2, status) == 0) { - workctx.clamp = ctx->clamp; - mpd_check_underflow(result, &workctx, status); - mpd_qfinalize(result, &workctx, status); - break; - } - prec += MPD_RDIGITS; - } - mpd_del(&t1); - mpd_del(&t2); - mpd_del(&ulp); - mpd_del(&aa); - } - else { - _mpd_qln(result, a, &workctx, status); - mpd_check_underflow(result, &workctx, status); - mpd_qfinalize(result, &workctx, status); - } -} - -/* - * Internal log10() function that does not check for specials, zero or one. - * Case SKIP_FINALIZE: - * Relative error: abs(result - log10(a)) < 0.1 * 10**-prec * abs(log10(a)) - * Case DO_FINALIZE: - * Ulp error: abs(result - log10(a)) < ulp(log10(a)) - */ -enum {SKIP_FINALIZE, DO_FINALIZE}; -static void -_mpd_qlog10(int action, mpd_t *result, const mpd_t *a, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_STATIC(ln10,0,0,0,0); - - mpd_maxcontext(&workctx); - workctx.prec = ctx->prec + 3; - /* relative error: 0.1 * 10**(-p-3). The specific underflow shortcut - * in _mpd_qln() does not change the final result. */ - _mpd_qln(result, a, &workctx, status); - /* relative error: 5 * 10**(-p-3) */ - mpd_qln10(&ln10, workctx.prec, status); - - if (action == DO_FINALIZE) { - workctx = *ctx; - workctx.round = MPD_ROUND_HALF_EVEN; - } - /* SKIP_FINALIZE: relative error: 5 * 10**(-p-3) */ - _mpd_qdiv(NO_IDEAL_EXP, result, result, &ln10, &workctx, status); - - mpd_del(&ln10); -} - -/* log10(a) */ -void -mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - mpd_ssize_t adjexp, t; - - workctx = *ctx; - workctx.round = MPD_ROUND_HALF_EVEN; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - mpd_setspecial(result, MPD_POS, MPD_INF); - return; - } - if (mpd_iszerocoeff(a)) { - mpd_setspecial(result, MPD_NEG, MPD_INF); - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (mpd_coeff_ispow10(a)) { - uint8_t sign = 0; - adjexp = mpd_adjexp(a); - if (adjexp < 0) { - sign = 1; - adjexp = -adjexp; - } - _settriple(result, sign, adjexp, 0); - mpd_qfinalize(result, &workctx, status); - return; - } - /* - * Check if the result will overflow (0 < x, x != 1): - * 1) log10(x) < 0 iff adjexp(x) < 0 - * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) - * 3) adjexp(x) <= log10(x) < adjexp(x) + 1 - * - * Case adjexp(x) >= 0: - * 4) adjexp(x) <= abs(log10(x)) - * Case adjexp(x) > 0: - * 5) adjexp(adjexp(x)) <= adjexp(abs(log10(x))) - * Case adjexp(x) == 0: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) - * - * Case adjexp(x) < 0: - * 6) -adjexp(x) - 1 < abs(log10(x)) - * Case adjexp(x) < -1: - * 7) adjexp(-adjexp(x) - 1) <= adjexp(abs(log(x))) - * Case adjexp(x) == -1: - * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) - */ - adjexp = mpd_adjexp(a); - t = (adjexp < 0) ? -adjexp-1 : adjexp; - if (mpd_exp_digits(t)-1 > ctx->emax) { - *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; - mpd_setspecial(result, (adjexp<0), MPD_INF); - return; - } - - if (ctx->allcr) { - MPD_NEW_STATIC(t1, 0,0,0,0); - MPD_NEW_STATIC(t2, 0,0,0,0); - MPD_NEW_STATIC(ulp, 0,0,0,0); - MPD_NEW_STATIC(aa, 0,0,0,0); - mpd_ssize_t prec; - - if (result == a) { - if (!mpd_qcopy(&aa, a, status)) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - a = &aa; - } - - workctx.clamp = 0; - prec = ctx->prec + 3; - while (1) { - workctx.prec = prec; - _mpd_qlog10(SKIP_FINALIZE, result, a, &workctx, status); - _ssettriple(&ulp, MPD_POS, 1, - result->exp + result->digits-workctx.prec); - - workctx.prec = ctx->prec; - mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); - mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); - if (mpd_isspecial(result) || mpd_iszerocoeff(result) || - mpd_qcmp(&t1, &t2, status) == 0) { - workctx.clamp = ctx->clamp; - mpd_check_underflow(result, &workctx, status); - mpd_qfinalize(result, &workctx, status); - break; - } - prec += MPD_RDIGITS; - } - mpd_del(&t1); - mpd_del(&t2); - mpd_del(&ulp); - mpd_del(&aa); - } - else { - _mpd_qlog10(DO_FINALIZE, result, a, &workctx, status); - mpd_check_underflow(result, &workctx, status); - } -} - -/* - * Maximum of the two operands. Attention: If one operand is a quiet NaN and the - * other is numeric, the numeric operand is returned. This may not be what one - * expects. - */ -void -mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - int c; - - if (mpd_isqnan(a) && !mpd_isnan(b)) { - mpd_qcopy(result, b, status); - } - else if (mpd_isqnan(b) && !mpd_isnan(a)) { - mpd_qcopy(result, a, status); - } - else if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - else { - c = _mpd_cmp(a, b); - if (c == 0) { - c = _mpd_cmp_numequal(a, b); - } - - if (c < 0) { - mpd_qcopy(result, b, status); - } - else { - mpd_qcopy(result, a, status); - } - } - - mpd_qfinalize(result, ctx, status); -} - -/* - * Maximum magnitude: Same as mpd_max(), but compares the operands with their - * sign ignored. - */ -void -mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - int c; - - if (mpd_isqnan(a) && !mpd_isnan(b)) { - mpd_qcopy(result, b, status); - } - else if (mpd_isqnan(b) && !mpd_isnan(a)) { - mpd_qcopy(result, a, status); - } - else if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - else { - c = _mpd_cmp_abs(a, b); - if (c == 0) { - c = _mpd_cmp_numequal(a, b); - } - - if (c < 0) { - mpd_qcopy(result, b, status); - } - else { - mpd_qcopy(result, a, status); - } - } - - mpd_qfinalize(result, ctx, status); -} - -/* - * Minimum of the two operands. Attention: If one operand is a quiet NaN and the - * other is numeric, the numeric operand is returned. This may not be what one - * expects. - */ -void -mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - int c; - - if (mpd_isqnan(a) && !mpd_isnan(b)) { - mpd_qcopy(result, b, status); - } - else if (mpd_isqnan(b) && !mpd_isnan(a)) { - mpd_qcopy(result, a, status); - } - else if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - else { - c = _mpd_cmp(a, b); - if (c == 0) { - c = _mpd_cmp_numequal(a, b); - } - - if (c < 0) { - mpd_qcopy(result, a, status); - } - else { - mpd_qcopy(result, b, status); - } - } - - mpd_qfinalize(result, ctx, status); -} - -/* - * Minimum magnitude: Same as mpd_min(), but compares the operands with their - * sign ignored. - */ -void -mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - int c; - - if (mpd_isqnan(a) && !mpd_isnan(b)) { - mpd_qcopy(result, b, status); - } - else if (mpd_isqnan(b) && !mpd_isnan(a)) { - mpd_qcopy(result, a, status); - } - else if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - else { - c = _mpd_cmp_abs(a, b); - if (c == 0) { - c = _mpd_cmp_numequal(a, b); - } - - if (c < 0) { - mpd_qcopy(result, a, status); - } - else { - mpd_qcopy(result, b, status); - } - } - - mpd_qfinalize(result, ctx, status); -} - -/* Minimum space needed for the result array in _karatsuba_rec(). */ -static inline mpd_size_t -_kmul_resultsize(mpd_size_t la, mpd_size_t lb) -{ - mpd_size_t n, m; - - n = add_size_t(la, lb); - n = add_size_t(n, 1); - - m = (la+1)/2 + 1; - m = mul_size_t(m, 3); - - return (m > n) ? m : n; -} - -/* Work space needed in _karatsuba_rec(). lim >= 4 */ -static inline mpd_size_t -_kmul_worksize(mpd_size_t n, mpd_size_t lim) -{ - mpd_size_t m; - - if (n <= lim) { - return 0; - } - - m = (n+1)/2 + 1; - - return add_size_t(mul_size_t(m, 2), _kmul_worksize(m, lim)); -} - - -#define MPD_KARATSUBA_BASECASE 16 /* must be >= 4 */ - -/* - * Add the product of a and b to c. - * c must be _kmul_resultsize(la, lb) in size. - * w is used as a work array and must be _kmul_worksize(a, lim) in size. - * Roman E. Maeder, Storage Allocation for the Karatsuba Integer Multiplication - * Algorithm. In "Design and implementation of symbolic computation systems", - * Springer, 1993, ISBN 354057235X, 9783540572350. - */ -static void -_karatsuba_rec(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, - mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) -{ - mpd_size_t m, lt; - - assert(la >= lb && lb > 0); - assert(la <= MPD_KARATSUBA_BASECASE || w != NULL); - - if (la <= MPD_KARATSUBA_BASECASE) { - _mpd_basemul(c, a, b, la, lb); - return; - } - - m = (la+1)/2; /* ceil(la/2) */ - - /* lb <= m < la */ - if (lb <= m) { - - /* lb can now be larger than la-m */ - if (lb > la-m) { - lt = lb + lb + 1; /* space needed for result array */ - mpd_uint_zero(w, lt); /* clear result array */ - _karatsuba_rec(w, b, a+m, w+lt, lb, la-m); /* b*ah */ - } - else { - lt = (la-m) + (la-m) + 1; /* space needed for result array */ - mpd_uint_zero(w, lt); /* clear result array */ - _karatsuba_rec(w, a+m, b, w+lt, la-m, lb); /* ah*b */ - } - _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ - - lt = m + m + 1; /* space needed for the result array */ - mpd_uint_zero(w, lt); /* clear result array */ - _karatsuba_rec(w, a, b, w+lt, m, lb); /* al*b */ - _mpd_baseaddto(c, w, m+lb); /* add al*b */ - - return; - } - - /* la >= lb > m */ - memcpy(w, a, m * sizeof *w); - w[m] = 0; - _mpd_baseaddto(w, a+m, la-m); - - memcpy(w+(m+1), b, m * sizeof *w); - w[m+1+m] = 0; - _mpd_baseaddto(w+(m+1), b+m, lb-m); - - _karatsuba_rec(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1); - - lt = (la-m) + (la-m) + 1; - mpd_uint_zero(w, lt); - - _karatsuba_rec(w, a+m, b+m, w+lt, la-m, lb-m); - - _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); - _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); - - lt = m + m + 1; - mpd_uint_zero(w, lt); - - _karatsuba_rec(w, a, b, w+lt, m, m); - _mpd_baseaddto(c, w, m+m); - _mpd_basesubfrom(c+m, w, m+m); - - return; -} - -/* - * Multiply u and v, using Karatsuba multiplication. Returns a pointer - * to the result or NULL in case of failure (malloc error). - * Conditions: ulen >= vlen, ulen >= 4 - */ -static mpd_uint_t * -_mpd_kmul(const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t ulen, mpd_size_t vlen, - mpd_size_t *rsize) -{ - mpd_uint_t *result = NULL, *w = NULL; - mpd_size_t m; - - assert(ulen >= 4); - assert(ulen >= vlen); - - *rsize = _kmul_resultsize(ulen, vlen); - if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { - return NULL; - } - - m = _kmul_worksize(ulen, MPD_KARATSUBA_BASECASE); - if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { - mpd_free(result); - return NULL; - } - - _karatsuba_rec(result, u, v, w, ulen, vlen); - - - if (w) mpd_free(w); - return result; -} - - -/* - * Determine the minimum length for the number theoretic transform. Valid - * transform lengths are 2**n or 3*2**n, where 2**n <= MPD_MAXTRANSFORM_2N. - * The function finds the shortest length m such that rsize <= m. - */ -static inline mpd_size_t -_mpd_get_transform_len(mpd_size_t rsize) -{ - mpd_size_t log2rsize; - mpd_size_t x, step; - - assert(rsize >= 4); - log2rsize = mpd_bsr(rsize); - - if (rsize <= 1024) { - /* 2**n is faster in this range. */ - x = ((mpd_size_t)1)<<log2rsize; - return (rsize == x) ? x : x<<1; - } - else if (rsize <= MPD_MAXTRANSFORM_2N) { - x = ((mpd_size_t)1)<<log2rsize; - if (rsize == x) return x; - step = x>>1; - x += step; - return (rsize <= x) ? x : x + step; - } - else if (rsize <= MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2) { - return MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2; - } - else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { - return 3*MPD_MAXTRANSFORM_2N; - } - else { - return MPD_SIZE_MAX; - } -} - -#ifdef PPRO -#ifndef _MSC_VER -static inline unsigned short -_mpd_get_control87(void) -{ - unsigned short cw; - - __asm__ __volatile__ ("fnstcw %0" : "=m" (cw)); - return cw; -} - -static inline void -_mpd_set_control87(unsigned short cw) -{ - __asm__ __volatile__ ("fldcw %0" : : "m" (cw)); -} -#endif - -static unsigned int -mpd_set_fenv(void) -{ - unsigned int cw; -#ifdef _MSC_VER - unsigned int flags = - _EM_INVALID|_EM_DENORMAL|_EM_ZERODIVIDE|_EM_OVERFLOW| - _EM_UNDERFLOW|_EM_INEXACT|_RC_CHOP|_PC_64; - unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; - unsigned int dummy; - - __control87_2(0, 0, &cw, NULL); - __control87_2(flags, mask, &dummy, NULL); -#else - cw = _mpd_get_control87(); - _mpd_set_control87(cw|0xF3F); -#endif - return cw; -} - -static void -mpd_restore_fenv(unsigned int cw) -{ -#ifdef _MSC_VER - unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; - unsigned int dummy; - - __control87_2(cw, mask, &dummy, NULL); -#else - _mpd_set_control87((unsigned short)cw); -#endif -} -#endif /* PPRO */ - -/* - * Multiply u and v, using the fast number theoretic transform. Returns - * a pointer to the result or NULL in case of failure (malloc error). - */ -static mpd_uint_t * -_mpd_fntmul(const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t ulen, mpd_size_t vlen, - mpd_size_t *rsize) -{ - mpd_uint_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *vtmp = NULL; - mpd_size_t n; - -#ifdef PPRO - unsigned int cw; - cw = mpd_set_fenv(); -#endif - - *rsize = add_size_t(ulen, vlen); - if ((n = _mpd_get_transform_len(*rsize)) == MPD_SIZE_MAX) { - goto malloc_error; - } - - if ((c1 = mpd_calloc(n, sizeof *c1)) == NULL) { - goto malloc_error; - } - if ((c2 = mpd_calloc(n, sizeof *c2)) == NULL) { - goto malloc_error; - } - if ((c3 = mpd_calloc(n, sizeof *c3)) == NULL) { - goto malloc_error; - } - - memcpy(c1, u, ulen * (sizeof *c1)); - memcpy(c2, u, ulen * (sizeof *c2)); - memcpy(c3, u, ulen * (sizeof *c3)); - - if (u == v) { - if (!fnt_autoconvolute(c1, n, P1) || - !fnt_autoconvolute(c2, n, P2) || - !fnt_autoconvolute(c3, n, P3)) { - goto malloc_error; - } - } - else { - if ((vtmp = mpd_calloc(n, sizeof *vtmp)) == NULL) { - goto malloc_error; - } - - memcpy(vtmp, v, vlen * (sizeof *vtmp)); - if (!fnt_convolute(c1, vtmp, n, P1)) { - mpd_free(vtmp); - goto malloc_error; - } - - memcpy(vtmp, v, vlen * (sizeof *vtmp)); - mpd_uint_zero(vtmp+vlen, n-vlen); - if (!fnt_convolute(c2, vtmp, n, P2)) { - mpd_free(vtmp); - goto malloc_error; - } - - memcpy(vtmp, v, vlen * (sizeof *vtmp)); - mpd_uint_zero(vtmp+vlen, n-vlen); - if (!fnt_convolute(c3, vtmp, n, P3)) { - mpd_free(vtmp); - goto malloc_error; - } - - mpd_free(vtmp); - } - - crt3(c1, c2, c3, *rsize); - -out: -#ifdef PPRO - mpd_restore_fenv(cw); -#endif - if (c2) mpd_free(c2); - if (c3) mpd_free(c3); - return c1; - -malloc_error: - if (c1) mpd_free(c1); - c1 = NULL; - goto out; -} - - -/* - * Karatsuba multiplication with FNT/basemul as the base case. - */ -static int -_karatsuba_rec_fnt(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, - mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) -{ - mpd_size_t m, lt; - - assert(la >= lb && lb > 0); - assert(la <= 3*(MPD_MAXTRANSFORM_2N/2) || w != NULL); - - if (la <= 3*(MPD_MAXTRANSFORM_2N/2)) { - - if (lb <= 192) { - _mpd_basemul(c, b, a, lb, la); - } - else { - mpd_uint_t *result; - mpd_size_t dummy; - - if ((result = _mpd_fntmul(a, b, la, lb, &dummy)) == NULL) { - return 0; - } - memcpy(c, result, (la+lb) * (sizeof *result)); - mpd_free(result); - } - return 1; - } - - m = (la+1)/2; /* ceil(la/2) */ - - /* lb <= m < la */ - if (lb <= m) { - - /* lb can now be larger than la-m */ - if (lb > la-m) { - lt = lb + lb + 1; /* space needed for result array */ - mpd_uint_zero(w, lt); /* clear result array */ - if (!_karatsuba_rec_fnt(w, b, a+m, w+lt, lb, la-m)) { /* b*ah */ - return 0; /* GCOV_UNLIKELY */ - } - } - else { - lt = (la-m) + (la-m) + 1; /* space needed for result array */ - mpd_uint_zero(w, lt); /* clear result array */ - if (!_karatsuba_rec_fnt(w, a+m, b, w+lt, la-m, lb)) { /* ah*b */ - return 0; /* GCOV_UNLIKELY */ - } - } - _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ - - lt = m + m + 1; /* space needed for the result array */ - mpd_uint_zero(w, lt); /* clear result array */ - if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, lb)) { /* al*b */ - return 0; /* GCOV_UNLIKELY */ - } - _mpd_baseaddto(c, w, m+lb); /* add al*b */ - - return 1; - } - - /* la >= lb > m */ - memcpy(w, a, m * sizeof *w); - w[m] = 0; - _mpd_baseaddto(w, a+m, la-m); - - memcpy(w+(m+1), b, m * sizeof *w); - w[m+1+m] = 0; - _mpd_baseaddto(w+(m+1), b+m, lb-m); - - if (!_karatsuba_rec_fnt(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1)) { - return 0; /* GCOV_UNLIKELY */ - } - - lt = (la-m) + (la-m) + 1; - mpd_uint_zero(w, lt); - - if (!_karatsuba_rec_fnt(w, a+m, b+m, w+lt, la-m, lb-m)) { - return 0; /* GCOV_UNLIKELY */ - } - - _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); - _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); - - lt = m + m + 1; - mpd_uint_zero(w, lt); - - if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, m)) { - return 0; /* GCOV_UNLIKELY */ - } - _mpd_baseaddto(c, w, m+m); - _mpd_basesubfrom(c+m, w, m+m); - - return 1; -} - -/* - * Multiply u and v, using Karatsuba multiplication with the FNT as the - * base case. Returns a pointer to the result or NULL in case of failure - * (malloc error). Conditions: ulen >= vlen, ulen >= 4. - */ -static mpd_uint_t * -_mpd_kmul_fnt(const mpd_uint_t *u, const mpd_uint_t *v, - mpd_size_t ulen, mpd_size_t vlen, - mpd_size_t *rsize) -{ - mpd_uint_t *result = NULL, *w = NULL; - mpd_size_t m; - - assert(ulen >= 4); - assert(ulen >= vlen); - - *rsize = _kmul_resultsize(ulen, vlen); - if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { - return NULL; - } - - m = _kmul_worksize(ulen, 3*(MPD_MAXTRANSFORM_2N/2)); - if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { - mpd_free(result); /* GCOV_UNLIKELY */ - return NULL; /* GCOV_UNLIKELY */ - } - - if (!_karatsuba_rec_fnt(result, u, v, w, ulen, vlen)) { - mpd_free(result); - result = NULL; - } - - - if (w) mpd_free(w); - return result; -} - - -/* Deal with the special cases of multiplying infinities. */ -static void -_mpd_qmul_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status) -{ - if (mpd_isinfinite(a)) { - if (mpd_iszero(b)) { - mpd_seterror(result, MPD_Invalid_operation, status); - } - else { - mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); - } - return; - } - assert(mpd_isinfinite(b)); - if (mpd_iszero(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - } - else { - mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); - } -} - -/* - * Internal function: Multiply a and b. _mpd_qmul deals with specials but - * does NOT finalize the result. This is for use in mpd_fma(). - */ -static inline void -_mpd_qmul(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 *rdata = NULL; - mpd_uint_t rbuf[MPD_MINALLOC_MAX]; - mpd_size_t rsize, i; - - - if (mpd_isspecial(a) || mpd_isspecial(b)) { - if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - _mpd_qmul_inf(result, a, b, status); - return; - } - - if (small->len > big->len) { - _mpd_ptrswap(&big, &small); - } - - rsize = big->len + small->len; - - if (big->len == 1) { - _mpd_singlemul(result->data, big->data[0], small->data[0]); - goto finish; - } - if (rsize <= (mpd_size_t)MPD_MINALLOC_MAX) { - if (big->len == 2) { - _mpd_mul_2_le2(rbuf, big->data, small->data, small->len); - } - else { - mpd_uint_zero(rbuf, rsize); - if (small->len == 1) { - _mpd_shortmul(rbuf, big->data, big->len, small->data[0]); - } - else { - _mpd_basemul(rbuf, small->data, big->data, small->len, big->len); - } - } - if (!mpd_qresize(result, rsize, status)) { - return; - } - for(i = 0; i < rsize; i++) { - result->data[i] = rbuf[i]; - } - goto finish; - } - - - if (small->len <= 256) { - rdata = mpd_calloc(rsize, sizeof *rdata); - if (rdata != NULL) { - if (small->len == 1) { - _mpd_shortmul(rdata, big->data, big->len, small->data[0]); - } - else { - _mpd_basemul(rdata, small->data, big->data, small->len, big->len); - } - } - } - else if (rsize <= 1024) { - rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize); - } - else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { - rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize); - } - else { - rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize); - } - - if (rdata == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - - if (mpd_isdynamic_data(result)) { - mpd_free(result->data); - } - result->data = rdata; - result->alloc = rsize; - mpd_set_dynamic_data(result); - - -finish: - mpd_set_flags(result, mpd_sign(a)^mpd_sign(b)); - result->exp = big->exp + small->exp; - result->len = _mpd_real_size(result->data, rsize); - /* resize to smaller cannot fail */ - mpd_qresize(result, result->len, status); - mpd_setdigits(result); -} - -/* Multiply a and b. */ -void -mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - _mpd_qmul(result, a, b, ctx, status); - mpd_qfinalize(result, ctx, status); -} - -/* Multiply a and b. Set NaN/Invalid_operation if the result is inexact. */ -static void -_mpd_qmul_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_qmul(result, a, b, ctx, &workstatus); - *status |= workstatus; - if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { - mpd_seterror(result, MPD_Invalid_operation, status); - } -} - -/* Multiply decimal and mpd_ssize_t. */ -void -mpd_qmul_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_qmul(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -/* Multiply decimal and mpd_uint_t. */ -void -mpd_qmul_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_qmul(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -void -mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qmul_ssize(result, a, b, ctx, status); -} - -void -mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qmul_uint(result, a, b, ctx, status); -} - -#ifdef CONFIG_64 -void -mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qmul_ssize(result, a, b, ctx, status); -} - -void -mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_qmul_uint(result, a, b, ctx, status); -} -#elif !defined(LEGACY_COMPILER) -/* Multiply decimal and int64_t. */ -void -mpd_qmul_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_qmul(result, a, &bb, ctx, status); - mpd_del(&bb); -} - -/* Multiply decimal and uint64_t. */ -void -mpd_qmul_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_qmul(result, a, &bb, ctx, status); - mpd_del(&bb); -} -#endif - -/* Like the minus operator. */ -void -mpd_qminus(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_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { - mpd_qcopy_abs(result, a, status); - } - else { - mpd_qcopy_negate(result, a, status); - } - - mpd_qfinalize(result, ctx, status); -} - -/* Like the plus operator. */ -void -mpd_qplus(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_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { - mpd_qcopy_abs(result, a, status); - } - else { - mpd_qcopy(result, a, status); - } - - mpd_qfinalize(result, ctx, status); -} - -/* The largest representable number that is smaller than the operand. */ -void -mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - - assert(mpd_isinfinite(a)); - if (mpd_isnegative(a)) { - mpd_qcopy(result, a, status); - return; - } - else { - mpd_clear_flags(result); - mpd_qmaxcoeff(result, ctx, status); - if (mpd_isnan(result)) { - return; - } - result->exp = mpd_etop(ctx); - return; - } - } - - mpd_workcontext(&workctx, ctx); - workctx.round = MPD_ROUND_FLOOR; - - if (!mpd_qcopy(result, a, status)) { - return; - } - - mpd_qfinalize(result, &workctx, &workctx.status); - if (workctx.status&(MPD_Inexact|MPD_Errors)) { - *status |= (workctx.status&MPD_Errors); - return; - } - - workctx.status = 0; - mpd_qsub(result, a, &tiny, &workctx, &workctx.status); - *status |= (workctx.status&MPD_Errors); -} - -/* The smallest representable number that is larger than the operand. */ -void -mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - - assert(mpd_isinfinite(a)); - if (mpd_ispositive(a)) { - mpd_qcopy(result, a, status); - } - else { - mpd_clear_flags(result); - mpd_qmaxcoeff(result, ctx, status); - if (mpd_isnan(result)) { - return; - } - mpd_set_flags(result, MPD_NEG); - result->exp = mpd_etop(ctx); - } - return; - } - - mpd_workcontext(&workctx, ctx); - workctx.round = MPD_ROUND_CEILING; - - if (!mpd_qcopy(result, a, status)) { - return; - } - - mpd_qfinalize(result, &workctx, &workctx.status); - if (workctx.status & (MPD_Inexact|MPD_Errors)) { - *status |= (workctx.status&MPD_Errors); - return; - } - - workctx.status = 0; - mpd_qadd(result, a, &tiny, &workctx, &workctx.status); - *status |= (workctx.status&MPD_Errors); -} - -/* - * The number closest to the first operand that is in the direction towards - * the second operand. - */ -void -mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - int c; - - if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - - c = _mpd_cmp(a, b); - if (c == 0) { - mpd_qcopy_sign(result, a, b, status); - return; - } - - if (c < 0) { - mpd_qnext_plus(result, a, ctx, status); - } - else { - mpd_qnext_minus(result, a, ctx, status); - } - - if (mpd_isinfinite(result)) { - *status |= (MPD_Overflow|MPD_Rounded|MPD_Inexact); - } - else if (mpd_adjexp(result) < ctx->emin) { - *status |= (MPD_Underflow|MPD_Subnormal|MPD_Rounded|MPD_Inexact); - if (mpd_iszero(result)) { - *status |= MPD_Clamped; - } - } -} - -/* - * Internal function: Integer power with mpd_uint_t exponent. The function - * can fail with MPD_Malloc_error. - * - * The error is equal to the error incurred in k-1 multiplications. Assuming - * the upper bound for the relative error in each operation: - * - * abs(err) = 5 * 10**-prec - * result = x**k * (1 + err)**(k-1) - */ -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) -{ - uint32_t workstatus = 0; - mpd_uint_t n; - - if (exp == 0) { - _settriple(result, resultsign, 1, 0); /* GCOV_NOT_REACHED */ - return; /* GCOV_NOT_REACHED */ - } - - if (!mpd_qcopy(result, base, status)) { - return; - } - - n = mpd_bits[mpd_bsr(exp)]; - while (n >>= 1) { - mpd_qmul(result, result, result, ctx, &workstatus); - if (exp & n) { - mpd_qmul(result, result, base, ctx, &workstatus); - } - if (mpd_isspecial(result) || - (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { - break; - } - } - - *status |= workstatus; - mpd_set_sign(result, resultsign); -} - -/* - * Internal function: Integer power with mpd_t exponent, tbase and texp - * are modified!! Function can fail with MPD_Malloc_error. - * - * The error is equal to the error incurred in k multiplications. Assuming - * the upper bound for the relative error in each operation: - * - * abs(err) = 5 * 10**-prec - * result = x**k * (1 + err)**k - */ -static inline void -_mpd_qpow_mpd(mpd_t *result, mpd_t *tbase, mpd_t *texp, uint8_t resultsign, - const mpd_context_t *ctx, uint32_t *status) -{ - uint32_t workstatus = 0; - mpd_context_t maxctx; - MPD_NEW_CONST(two,0,0,1,1,1,2); - - - mpd_maxcontext(&maxctx); - - /* resize to smaller cannot fail */ - mpd_qcopy(result, &one, status); - - while (!mpd_iszero(texp)) { - if (mpd_isodd(texp)) { - mpd_qmul(result, result, tbase, ctx, &workstatus); - *status |= workstatus; - if (mpd_isspecial(result) || - (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { - break; - } - } - mpd_qmul(tbase, tbase, tbase, ctx, &workstatus); - mpd_qdivint(texp, texp, &two, &maxctx, &workstatus); - if (mpd_isnan(tbase) || mpd_isnan(texp)) { - mpd_seterror(result, workstatus&MPD_Errors, status); - return; - } - } - mpd_set_sign(result, resultsign); -} - -/* - * The power function for integer exponents. Relative error _before_ the - * final rounding to prec: - * abs(result - base**exp) < 0.1 * 10**-prec * abs(base**exp) - */ -static void -_mpd_qpow_int(mpd_t *result, const mpd_t *base, const mpd_t *exp, - uint8_t resultsign, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_STATIC(tbase,0,0,0,0); - MPD_NEW_STATIC(texp,0,0,0,0); - mpd_ssize_t n; - - - mpd_workcontext(&workctx, ctx); - workctx.prec += (exp->digits + exp->exp + 2); - workctx.round = MPD_ROUND_HALF_EVEN; - workctx.clamp = 0; - if (mpd_isnegative(exp)) { + 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); +} + +/* Divide decimal by mpd_ssize_t. */ +void +mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +/* Divide decimal by mpd_uint_t. */ +void +mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +/* Divide decimal by int32_t. */ +void +mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qdiv_ssize(result, a, b, ctx, status); +} + +/* Divide decimal by uint32_t. */ +void +mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qdiv_uint(result, a, b, ctx, status); +} + +#ifdef CONFIG_64 +/* Divide decimal by int64_t. */ +void +mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qdiv_ssize(result, a, b, ctx, status); +} + +/* Divide decimal by uint64_t. */ +void +mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qdiv_uint(result, a, b, ctx, status); +} +#elif !defined(LEGACY_COMPILER) +/* Divide decimal by int64_t. */ +void +mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +/* Divide decimal by uint64_t. */ +void +mpd_qdiv_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_qdiv(result, a, &bb, ctx, status); + mpd_del(&bb); +} +#endif + +/* Pad the result with trailing zeros if it has fewer digits than prec. */ +static void +_mpd_zeropad(mpd_t *result, const mpd_context_t *ctx, uint32_t *status) +{ + if (!mpd_isspecial(result) && !mpd_iszero(result) && + result->digits < ctx->prec) { + mpd_ssize_t shift = ctx->prec - result->digits; + mpd_qshiftl(result, result, shift, status); + result->exp -= shift; + } +} + +/* Check if the result is guaranteed to be one. */ +static int +_mpd_qexp_check_one(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + MPD_NEW_CONST(lim,0,-(ctx->prec+1),1,1,1,9); + MPD_NEW_SHARED(aa, a); + + mpd_set_positive(&aa); + + /* abs(a) <= 9 * 10**(-prec-1) */ + if (_mpd_cmp(&aa, &lim) <= 0) { + _settriple(result, 0, 1, 0); + *status |= MPD_Rounded|MPD_Inexact; + return 1; + } + + return 0; +} + +/* + * Get the number of iterations for the Horner scheme in _mpd_qexp(). + */ +static inline mpd_ssize_t +_mpd_get_exp_iterations(const mpd_t *r, mpd_ssize_t p) +{ + mpd_ssize_t log10pbyr; /* lower bound for log10(p / abs(r)) */ + mpd_ssize_t n; + + assert(p >= 10); + assert(!mpd_iszero(r)); + assert(-p < mpd_adjexp(r) && mpd_adjexp(r) <= -1); + +#ifdef CONFIG_64 + if (p > (mpd_ssize_t)(1ULL<<52)) { + return MPD_SSIZE_MAX; + } +#endif + + /* + * Lower bound for log10(p / abs(r)): adjexp(p) - (adjexp(r) + 1) + * At this point (for CONFIG_64, CONFIG_32 is not problematic): + * 1) 10 <= p <= 2**52 + * 2) -p < adjexp(r) <= -1 + * 3) 1 <= log10pbyr <= 2**52 + 14 + */ + log10pbyr = (mpd_word_digits(p)-1) - (mpd_adjexp(r)+1); + + /* + * The numerator in the paper is 1.435 * p - 1.182, calculated + * exactly. We compensate for rounding errors by using 1.43503. + * ACL2 proofs: + * 1) exp-iter-approx-lower-bound: The term below evaluated + * in 53-bit floating point arithmetic is greater than or + * equal to the exact term used in the paper. + * 2) exp-iter-approx-upper-bound: The term below is less than + * or equal to 3/2 * p <= 3/2 * 2**52. + */ + n = (mpd_ssize_t)ceil((1.43503*(double)p - 1.182) / (double)log10pbyr); + return n >= 3 ? n : 3; +} + +/* + * Internal function, specials have been dealt with. Apart from Overflow + * and Underflow, two cases must be considered for the error of the result: + * + * 1) abs(a) <= 9 * 10**(-prec-1) ==> result == 1 + * + * Absolute error: abs(1 - e**x) < 10**(-prec) + * ------------------------------------------- + * + * 2) abs(a) > 9 * 10**(-prec-1) + * + * Relative error: abs(result - e**x) < 0.5 * 10**(-prec) * e**x + * ------------------------------------------------------------- + * + * The algorithm is from Hull&Abrham, Variable Precision Exponential Function, + * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986. + * + * Main differences: + * + * - The number of iterations for the Horner scheme is calculated using + * 53-bit floating point arithmetic. + * + * - In the error analysis for ER (relative error accumulated in the + * evaluation of the truncated series) the reduced operand r may + * have any number of digits. + * ACL2 proof: exponent-relative-error + * + * - The analysis for early abortion has been adapted for the mpd_t + * ranges. + */ +static void +_mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_STATIC(tmp,0,0,0,0); + MPD_NEW_STATIC(sum,0,0,0,0); + MPD_NEW_CONST(word,0,0,1,1,1,1); + mpd_ssize_t j, n, t; + + assert(!mpd_isspecial(a)); + + if (mpd_iszerocoeff(a)) { + _settriple(result, MPD_POS, 1, 0); + return; + } + + /* + * We are calculating e^x = e^(r*10^t) = (e^r)^(10^t), where abs(r) < 1 and t >= 0. + * + * If t > 0, we have: + * + * (1) 0.1 <= r < 1, so e^0.1 <= e^r. If t > MAX_T, overflow occurs: + * + * MAX-EMAX+1 < log10(e^(0.1*10*t)) <= log10(e^(r*10^t)) < adjexp(e^(r*10^t))+1 + * + * (2) -1 < r <= -0.1, so e^r <= e^-0.1. If t > MAX_T, underflow occurs: + * + * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t)) < MIN-ETINY + */ +#if defined(CONFIG_64) + #define MPD_EXP_MAX_T 19 +#elif defined(CONFIG_32) + #define MPD_EXP_MAX_T 10 +#endif + t = a->digits + a->exp; + t = (t > 0) ? t : 0; + if (t > MPD_EXP_MAX_T) { + if (mpd_ispositive(a)) { + mpd_setspecial(result, MPD_POS, MPD_INF); + *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; + } + else { + _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); + *status |= (MPD_Inexact|MPD_Rounded|MPD_Subnormal| + MPD_Underflow|MPD_Clamped); + } + return; + } + + /* abs(a) <= 9 * 10**(-prec-1) */ + if (_mpd_qexp_check_one(result, a, ctx, status)) { + return; + } + + mpd_maxcontext(&workctx); + workctx.prec = ctx->prec + t + 2; + workctx.prec = (workctx.prec < 10) ? 10 : workctx.prec; + workctx.round = MPD_ROUND_HALF_EVEN; + + if (!mpd_qcopy(result, a, status)) { + return; + } + result->exp -= t; + + /* + * At this point: + * 1) 9 * 10**(-prec-1) < abs(a) + * 2) 9 * 10**(-prec-t-1) < abs(r) + * 3) log10(9) - prec - t - 1 < log10(abs(r)) < adjexp(abs(r)) + 1 + * 4) - prec - t - 2 < adjexp(abs(r)) <= -1 + */ + n = _mpd_get_exp_iterations(result, workctx.prec); + if (n == MPD_SSIZE_MAX) { + mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_UNLIKELY */ + return; /* GCOV_UNLIKELY */ + } + + _settriple(&sum, MPD_POS, 1, 0); + + for (j = n-1; j >= 1; j--) { + word.data[0] = j; + mpd_setdigits(&word); + mpd_qdiv(&tmp, result, &word, &workctx, &workctx.status); + mpd_qfma(&sum, &sum, &tmp, &one, &workctx, &workctx.status); + } + +#ifdef CONFIG_64 + _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); +#else + if (t <= MPD_MAX_POW10) { + _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); + } + else { + t -= MPD_MAX_POW10; + _mpd_qpow_uint(&tmp, &sum, mpd_pow10[MPD_MAX_POW10], MPD_POS, + &workctx, status); + _mpd_qpow_uint(result, &tmp, mpd_pow10[t], MPD_POS, &workctx, status); + } +#endif + + mpd_del(&tmp); + mpd_del(&sum); + *status |= (workctx.status&MPD_Errors); + *status |= (MPD_Inexact|MPD_Rounded); +} + +/* exp(a) */ +void +mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + if (mpd_isnegative(a)) { + _settriple(result, MPD_POS, 0, 0); + } + else { + mpd_setspecial(result, MPD_POS, MPD_INF); + } + return; + } + if (mpd_iszerocoeff(a)) { + _settriple(result, MPD_POS, 1, 0); + return; + } + + workctx = *ctx; + workctx.round = MPD_ROUND_HALF_EVEN; + + if (ctx->allcr) { + MPD_NEW_STATIC(t1, 0,0,0,0); + MPD_NEW_STATIC(t2, 0,0,0,0); + MPD_NEW_STATIC(ulp, 0,0,0,0); + MPD_NEW_STATIC(aa, 0,0,0,0); + mpd_ssize_t prec; + mpd_ssize_t ulpexp; + uint32_t workstatus; + + if (result == a) { + if (!mpd_qcopy(&aa, a, status)) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + a = &aa; + } + + workctx.clamp = 0; + prec = ctx->prec + 3; + while (1) { + workctx.prec = prec; + workstatus = 0; + + _mpd_qexp(result, a, &workctx, &workstatus); + *status |= workstatus; + + ulpexp = result->exp + result->digits - workctx.prec; + if (workstatus & MPD_Underflow) { + /* The effective work precision is result->digits. */ + ulpexp = result->exp; + } + _ssettriple(&ulp, MPD_POS, 1, ulpexp); + + /* + * At this point [1]: + * 1) abs(result - e**x) < 0.5 * 10**(-prec) * e**x + * 2) result - ulp < e**x < result + ulp + * 3) result - ulp < result < result + ulp + * + * If round(result-ulp)==round(result+ulp), then + * round(result)==round(e**x). Therefore the result + * is correctly rounded. + * + * [1] If abs(a) <= 9 * 10**(-prec-1), use the absolute + * error for a similar argument. + */ + workctx.prec = ctx->prec; + mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); + mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); + if (mpd_isspecial(result) || mpd_iszerocoeff(result) || + mpd_qcmp(&t1, &t2, status) == 0) { + workctx.clamp = ctx->clamp; + _mpd_zeropad(result, &workctx, status); + mpd_check_underflow(result, &workctx, status); + mpd_qfinalize(result, &workctx, status); + break; + } + prec += MPD_RDIGITS; + } + mpd_del(&t1); + mpd_del(&t2); + mpd_del(&ulp); + mpd_del(&aa); + } + else { + _mpd_qexp(result, a, &workctx, status); + _mpd_zeropad(result, &workctx, status); + mpd_check_underflow(result, &workctx, status); + mpd_qfinalize(result, &workctx, status); + } +} + +/* Fused multiply-add: (a * b) + c, with a single final rounding. */ +void +mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, + const mpd_context_t *ctx, uint32_t *status) +{ + uint32_t workstatus = 0; + mpd_t *cc = NULL; + + if (result == c) { + if ((cc = mpd_qncopy(c)) == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + c = cc; + } + + _mpd_qmul(result, a, b, ctx, &workstatus); + if (!(workstatus&MPD_Invalid_operation)) { + mpd_qadd(result, result, c, ctx, &workstatus); + } + + if (cc) mpd_del(cc); + *status |= workstatus; +} + +/* + * Schedule the optimal precision increase for the Newton iteration. + * v := input operand + * z_0 := initial approximation + * initprec := natural number such that abs(log(v) - z_0) < 10**-initprec + * maxprec := target precision + * + * For convenience the output klist contains the elements in reverse order: + * klist := [k_n-1, ..., k_0], where + * 1) k_0 <= initprec and + * 2) abs(log(v) - result) < 10**(-2*k_n-1 + 1) <= 10**-maxprec. + */ +static inline int +ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec, + mpd_ssize_t initprec) +{ + mpd_ssize_t k; + int i; + + assert(maxprec >= 2 && initprec >= 2); + if (maxprec <= initprec) return -1; + + i = 0; k = maxprec; + do { + k = (k+2) / 2; + klist[i++] = k; + } while (k > initprec); + + return i-1; +} + +/* The constants have been verified with both decimal.py and mpfr. */ +#ifdef CONFIG_64 +#if MPD_RDIGITS != 19 + #error "mpdecimal.c: MPD_RDIGITS must be 19." +#endif +static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { + 6983716328982174407ULL, 9089704281976336583ULL, 1515961135648465461ULL, + 4416816335727555703ULL, 2900988039194170265ULL, 2307925037472986509ULL, + 107598438319191292ULL, 3466624107184669231ULL, 4450099781311469159ULL, + 9807828059751193854ULL, 7713456862091670584ULL, 1492198849978748873ULL, + 6528728696511086257ULL, 2385392051446341972ULL, 8692180205189339507ULL, + 6518769751037497088ULL, 2375253577097505395ULL, 9095610299291824318ULL, + 982748238504564801ULL, 5438635917781170543ULL, 7547331541421808427ULL, + 752371033310119785ULL, 3171643095059950878ULL, 9785265383207606726ULL, + 2932258279850258550ULL, 5497347726624257094ULL, 2976979522110718264ULL, + 9221477656763693866ULL, 1979650047149510504ULL, 6674183485704422507ULL, + 9702766860595249671ULL, 9278096762712757753ULL, 9314848524948644871ULL, + 6826928280848118428ULL, 754403708474699401ULL, 230105703089634572ULL, + 1929203337658714166ULL, 7589402567763113569ULL, 4208241314695689016ULL, + 2922455440575892572ULL, 9356734206705811364ULL, 2684916746550586856ULL, + 644507064800027750ULL, 9476834636167921018ULL, 5659121373450747856ULL, + 2835522011480466371ULL, 6470806855677432162ULL, 7141748003688084012ULL, + 9619404400222105101ULL, 5504893431493939147ULL, 6674744042432743651ULL, + 2287698219886746543ULL, 7773262884616336622ULL, 1985283935053089653ULL, + 4680843799894826233ULL, 8168948290720832555ULL, 8067566662873690987ULL, + 6248633409525465082ULL, 9829834196778404228ULL, 3524802359972050895ULL, + 3327900967572609677ULL, 110148862877297603ULL, 179914546843642076ULL, + 2302585092994045684ULL +}; +#else +#if MPD_RDIGITS != 9 + #error "mpdecimal.c: MPD_RDIGITS must be 9." +#endif +static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { + 401682692UL, 708474699UL, 720754403UL, 30896345UL, 602301057UL, 765871416UL, + 192920333UL, 763113569UL, 589402567UL, 956890167UL, 82413146UL, 589257242UL, + 245544057UL, 811364292UL, 734206705UL, 868569356UL, 167465505UL, 775026849UL, + 706480002UL, 18064450UL, 636167921UL, 569476834UL, 734507478UL, 156591213UL, + 148046637UL, 283552201UL, 677432162UL, 470806855UL, 880840126UL, 417480036UL, + 210510171UL, 940440022UL, 939147961UL, 893431493UL, 436515504UL, 440424327UL, + 654366747UL, 821988674UL, 622228769UL, 884616336UL, 537773262UL, 350530896UL, + 319852839UL, 989482623UL, 468084379UL, 720832555UL, 168948290UL, 736909878UL, + 675666628UL, 546508280UL, 863340952UL, 404228624UL, 834196778UL, 508959829UL, + 23599720UL, 967735248UL, 96757260UL, 603332790UL, 862877297UL, 760110148UL, + 468436420UL, 401799145UL, 299404568UL, 230258509UL +}; +#endif +/* _mpd_ln10 is used directly for precisions smaller than MINALLOC_MAX*RDIGITS. + Otherwise, it serves as the initial approximation for calculating ln(10). */ +static const mpd_t _mpd_ln10 = { + MPD_STATIC|MPD_CONST_DATA, -(MPD_MINALLOC_MAX*MPD_RDIGITS-1), + MPD_MINALLOC_MAX*MPD_RDIGITS, MPD_MINALLOC_MAX, MPD_MINALLOC_MAX, + (mpd_uint_t *)mpd_ln10_data +}; + +/* + * Set 'result' to log(10). + * Ulp error: abs(result - log(10)) < ulp(log(10)) + * Relative error: abs(result - log(10)) < 5 * 10**-prec * log(10) + * + * NOTE: The relative error is not derived from the ulp error, but + * calculated separately using the fact that 23/10 < log(10) < 24/10. + */ +void +mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status) +{ + mpd_context_t varcontext, maxcontext; + MPD_NEW_STATIC(tmp, 0,0,0,0); + MPD_NEW_CONST(static10, 0,0,2,1,1,10); + mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; + mpd_uint_t rnd; + mpd_ssize_t shift; + int i; + + assert(prec >= 1); + + shift = MPD_MINALLOC_MAX*MPD_RDIGITS-prec; + shift = shift < 0 ? 0 : shift; + + rnd = mpd_qshiftr(result, &_mpd_ln10, shift, status); + if (rnd == MPD_UINT_MAX) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + result->exp = -(result->digits-1); + + mpd_maxcontext(&maxcontext); + if (prec < MPD_MINALLOC_MAX*MPD_RDIGITS) { + maxcontext.prec = prec; + _mpd_apply_round_excess(result, rnd, &maxcontext, status); + *status |= (MPD_Inexact|MPD_Rounded); + return; + } + + mpd_maxcontext(&varcontext); + varcontext.round = MPD_ROUND_TRUNC; + + i = ln_schedule_prec(klist, prec+2, -result->exp); + for (; i >= 0; i--) { + varcontext.prec = 2*klist[i]+3; + result->flags ^= MPD_NEG; + _mpd_qexp(&tmp, result, &varcontext, status); + result->flags ^= MPD_NEG; + mpd_qmul(&tmp, &static10, &tmp, &varcontext, status); + mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); + mpd_qadd(result, result, &tmp, &maxcontext, status); + if (mpd_isspecial(result)) { + break; + } + } + + mpd_del(&tmp); + maxcontext.prec = prec; + mpd_qfinalize(result, &maxcontext, status); +} + +/* + * Initial approximations for the ln() iteration. The values have the + * following properties (established with both decimal.py and mpfr): + * + * Index 0 - 400, logarithms of x in [1.00, 5.00]: + * abs(lnapprox[i] * 10**-3 - log((i+100)/100)) < 10**-2 + * abs(lnapprox[i] * 10**-3 - log((i+1+100)/100)) < 10**-2 + * + * Index 401 - 899, logarithms of x in (0.500, 0.999]: + * abs(-lnapprox[i] * 10**-3 - log((i+100)/1000)) < 10**-2 + * abs(-lnapprox[i] * 10**-3 - log((i+1+100)/1000)) < 10**-2 + */ +static const uint16_t lnapprox[900] = { + /* index 0 - 400: log((i+100)/100) * 1000 */ + 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157, + 166, 174, 182, 191, 199, 207, 215, 223, 231, 239, 247, 255, 262, 270, 278, + 285, 293, 300, 308, 315, 322, 329, 336, 344, 351, 358, 365, 372, 378, 385, + 392, 399, 406, 412, 419, 425, 432, 438, 445, 451, 457, 464, 470, 476, 482, + 489, 495, 501, 507, 513, 519, 525, 531, 536, 542, 548, 554, 560, 565, 571, + 577, 582, 588, 593, 599, 604, 610, 615, 621, 626, 631, 637, 642, 647, 652, + 658, 663, 668, 673, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728, + 732, 737, 742, 747, 751, 756, 761, 766, 770, 775, 779, 784, 788, 793, 798, + 802, 806, 811, 815, 820, 824, 829, 833, 837, 842, 846, 850, 854, 859, 863, + 867, 871, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924, + 928, 932, 936, 940, 944, 948, 952, 956, 959, 963, 967, 971, 975, 978, 982, + 986, 990, 993, 997, 1001, 1004, 1008, 1012, 1015, 1019, 1022, 1026, 1030, + 1033, 1037, 1040, 1044, 1047, 1051, 1054, 1058, 1061, 1065, 1068, 1072, 1075, + 1078, 1082, 1085, 1089, 1092, 1095, 1099, 1102, 1105, 1109, 1112, 1115, 1118, + 1122, 1125, 1128, 1131, 1135, 1138, 1141, 1144, 1147, 1151, 1154, 1157, 1160, + 1163, 1166, 1169, 1172, 1176, 1179, 1182, 1185, 1188, 1191, 1194, 1197, 1200, + 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227, 1230, 1233, 1235, 1238, + 1241, 1244, 1247, 1250, 1253, 1256, 1258, 1261, 1264, 1267, 1270, 1273, 1275, + 1278, 1281, 1284, 1286, 1289, 1292, 1295, 1297, 1300, 1303, 1306, 1308, 1311, + 1314, 1316, 1319, 1322, 1324, 1327, 1330, 1332, 1335, 1338, 1340, 1343, 1345, + 1348, 1351, 1353, 1356, 1358, 1361, 1364, 1366, 1369, 1371, 1374, 1376, 1379, + 1381, 1384, 1386, 1389, 1391, 1394, 1396, 1399, 1401, 1404, 1406, 1409, 1411, + 1413, 1416, 1418, 1421, 1423, 1426, 1428, 1430, 1433, 1435, 1437, 1440, 1442, + 1445, 1447, 1449, 1452, 1454, 1456, 1459, 1461, 1463, 1466, 1468, 1470, 1472, + 1475, 1477, 1479, 1482, 1484, 1486, 1488, 1491, 1493, 1495, 1497, 1500, 1502, + 1504, 1506, 1509, 1511, 1513, 1515, 1517, 1520, 1522, 1524, 1526, 1528, 1530, + 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1548, 1550, 1552, 1554, 1556, 1558, + 1560, 1562, 1564, 1567, 1569, 1571, 1573, 1575, 1577, 1579, 1581, 1583, 1585, + 1587, 1589, 1591, 1593, 1595, 1597, 1599, 1601, 1603, 1605, 1607, 1609, + /* index 401 - 899: -log((i+100)/1000) * 1000 */ + 691, 689, 687, 685, 683, 681, 679, 677, 675, 673, 671, 669, 668, 666, 664, + 662, 660, 658, 656, 654, 652, 650, 648, 646, 644, 642, 641, 639, 637, 635, + 633, 631, 629, 627, 626, 624, 622, 620, 618, 616, 614, 612, 611, 609, 607, + 605, 603, 602, 600, 598, 596, 594, 592, 591, 589, 587, 585, 583, 582, 580, + 578, 576, 574, 573, 571, 569, 567, 566, 564, 562, 560, 559, 557, 555, 553, + 552, 550, 548, 546, 545, 543, 541, 540, 538, 536, 534, 533, 531, 529, 528, + 526, 524, 523, 521, 519, 518, 516, 514, 512, 511, 509, 508, 506, 504, 502, + 501, 499, 498, 496, 494, 493, 491, 489, 488, 486, 484, 483, 481, 480, 478, + 476, 475, 473, 472, 470, 468, 467, 465, 464, 462, 460, 459, 457, 456, 454, + 453, 451, 449, 448, 446, 445, 443, 442, 440, 438, 437, 435, 434, 432, 431, + 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 410, 408, + 406, 405, 404, 402, 400, 399, 398, 396, 394, 393, 392, 390, 389, 387, 386, + 384, 383, 381, 380, 378, 377, 375, 374, 372, 371, 370, 368, 367, 365, 364, + 362, 361, 360, 358, 357, 355, 354, 352, 351, 350, 348, 347, 345, 344, 342, + 341, 340, 338, 337, 336, 334, 333, 331, 330, 328, 327, 326, 324, 323, 322, + 320, 319, 318, 316, 315, 313, 312, 311, 309, 308, 306, 305, 304, 302, 301, + 300, 298, 297, 296, 294, 293, 292, 290, 289, 288, 286, 285, 284, 282, 281, + 280, 278, 277, 276, 274, 273, 272, 270, 269, 268, 267, 265, 264, 263, 261, + 260, 259, 258, 256, 255, 254, 252, 251, 250, 248, 247, 246, 245, 243, 242, + 241, 240, 238, 237, 236, 234, 233, 232, 231, 229, 228, 227, 226, 224, 223, + 222, 221, 219, 218, 217, 216, 214, 213, 212, 211, 210, 208, 207, 206, 205, + 203, 202, 201, 200, 198, 197, 196, 195, 194, 192, 191, 190, 189, 188, 186, + 185, 184, 183, 182, 180, 179, 178, 177, 176, 174, 173, 172, 171, 170, 168, + 167, 166, 165, 164, 162, 161, 160, 159, 158, 157, 156, 154, 153, 152, 151, + 150, 148, 147, 146, 145, 144, 143, 142, 140, 139, 138, 137, 136, 135, 134, + 132, 131, 130, 129, 128, 127, 126, 124, 123, 122, 121, 120, 119, 118, 116, + 115, 114, 113, 112, 111, 110, 109, 108, 106, 105, 104, 103, 102, 101, 100, + 99, 98, 97, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79, + 78, 77, 76, 75, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, + 58, 57, 56, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, + 38, 37, 36, 35, 34, 33, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, + 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 +}; + +/* + * Internal ln() function that does not check for specials, zero or one. + * Relative error: abs(result - log(a)) < 0.1 * 10**-prec * abs(log(a)) + */ +static void +_mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t varcontext, maxcontext; + mpd_t *z = (mpd_t *) result; + MPD_NEW_STATIC(v,0,0,0,0); + MPD_NEW_STATIC(vtmp,0,0,0,0); + MPD_NEW_STATIC(tmp,0,0,0,0); + mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; + mpd_ssize_t maxprec, shift, t; + mpd_ssize_t a_digits, a_exp; + mpd_uint_t dummy, x; + int i; + + assert(!mpd_isspecial(a) && !mpd_iszerocoeff(a)); + + /* + * We are calculating ln(a) = ln(v * 10^t) = ln(v) + t*ln(10), + * where 0.5 < v <= 5. + */ + if (!mpd_qcopy(&v, a, status)) { + mpd_seterror(result, MPD_Malloc_error, status); + goto finish; + } + + /* Initial approximation: we have at least one non-zero digit */ + _mpd_get_msdigits(&dummy, &x, &v, 3); + if (x < 10) x *= 10; + if (x < 100) x *= 10; + x -= 100; + + /* a may equal z */ + a_digits = a->digits; + a_exp = a->exp; + + mpd_minalloc(z); + mpd_clear_flags(z); + z->data[0] = lnapprox[x]; + z->len = 1; + z->exp = -3; + mpd_setdigits(z); + + if (x <= 400) { + /* Reduce the input operand to 1.00 <= v <= 5.00. Let y = x + 100, + * so 100 <= y <= 500. Since y contains the most significant digits + * of v, y/100 <= v < (y+1)/100 and abs(z - log(v)) < 10**-2. */ + v.exp = -(a_digits - 1); + t = a_exp + a_digits - 1; + } + else { + /* Reduce the input operand to 0.500 < v <= 0.999. Let y = x + 100, + * so 500 < y <= 999. Since y contains the most significant digits + * of v, y/1000 <= v < (y+1)/1000 and abs(z - log(v)) < 10**-2. */ + v.exp = -a_digits; + t = a_exp + a_digits; + mpd_set_negative(z); + } + + mpd_maxcontext(&maxcontext); + mpd_maxcontext(&varcontext); + varcontext.round = MPD_ROUND_TRUNC; + + maxprec = ctx->prec + 2; + if (t == 0 && (x <= 15 || x >= 800)) { + /* 0.900 <= v <= 1.15: Estimate the magnitude of the logarithm. + * If ln(v) will underflow, skip the loop. Otherwise, adjust the + * precision upwards in order to obtain a sufficient number of + * significant digits. + * + * Case v > 1: + * abs((v-1)/10) < abs((v-1)/v) < abs(ln(v)) < abs(v-1) + * Case v < 1: + * abs(v-1) < abs(ln(v)) < abs((v-1)/v) < abs((v-1)*10) + */ + int cmp = _mpd_cmp(&v, &one); + + /* Upper bound (assume v > 1): abs(v-1), unrounded */ + _mpd_qsub(&tmp, &v, &one, &maxcontext, &maxcontext.status); + if (maxcontext.status & MPD_Errors) { + mpd_seterror(result, MPD_Malloc_error, status); + goto finish; + } + + if (cmp < 0) { + /* v < 1: abs((v-1)*10) */ + tmp.exp += 1; + } + if (mpd_adjexp(&tmp) < mpd_etiny(ctx)) { + /* The upper bound is less than etiny: Underflow to zero */ + _settriple(result, (cmp<0), 1, mpd_etiny(ctx)-1); + goto finish; + } + /* Lower bound: abs((v-1)/10) or abs(v-1) */ + tmp.exp -= 1; + if (mpd_adjexp(&tmp) < 0) { + /* Absolute error of the loop: abs(z - log(v)) < 10**-p. If + * p = ctx->prec+2-adjexp(lower), then the relative error of + * the result is (using 10**adjexp(x) <= abs(x)): + * + * abs(z - log(v)) / abs(log(v)) < 10**-p / abs(log(v)) + * <= 10**(-ctx->prec-2) + */ + maxprec = maxprec - mpd_adjexp(&tmp); + } + } + + i = ln_schedule_prec(klist, maxprec, 2); + for (; i >= 0; i--) { + varcontext.prec = 2*klist[i]+3; + z->flags ^= MPD_NEG; + _mpd_qexp(&tmp, z, &varcontext, status); + z->flags ^= MPD_NEG; + + if (v.digits > varcontext.prec) { + shift = v.digits - varcontext.prec; + mpd_qshiftr(&vtmp, &v, shift, status); + vtmp.exp += shift; + mpd_qmul(&tmp, &vtmp, &tmp, &varcontext, status); + } + else { + mpd_qmul(&tmp, &v, &tmp, &varcontext, status); + } + + mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); + mpd_qadd(z, z, &tmp, &maxcontext, status); + if (mpd_isspecial(z)) { + break; + } + } + + /* + * Case t == 0: + * t * log(10) == 0, the result does not change and the analysis + * above applies. If v < 0.900 or v > 1.15, the relative error is + * less than 10**(-ctx.prec-1). + * Case t != 0: + * z := approx(log(v)) + * y := approx(log(10)) + * p := maxprec = ctx->prec + 2 + * Absolute errors: + * 1) abs(z - log(v)) < 10**-p + * 2) abs(y - log(10)) < 10**-p + * The multiplication is exact, so: + * 3) abs(t*y - t*log(10)) < t*10**-p + * The sum is exact, so: + * 4) abs((z + t*y) - (log(v) + t*log(10))) < (abs(t) + 1) * 10**-p + * Bounds for log(v) and log(10): + * 5) -7/10 < log(v) < 17/10 + * 6) 23/10 < log(10) < 24/10 + * Using 4), 5), 6) and t != 0, the relative error is: + * + * 7) relerr < ((abs(t) + 1)*10**-p) / abs(log(v) + t*log(10)) + * < 0.5 * 10**(-p + 1) = 0.5 * 10**(-ctx->prec-1) + */ + mpd_qln10(&v, maxprec+1, status); + mpd_qmul_ssize(&tmp, &v, t, &maxcontext, status); + mpd_qadd(result, &tmp, z, &maxcontext, status); + + +finish: + *status |= (MPD_Inexact|MPD_Rounded); + mpd_del(&v); + mpd_del(&vtmp); + mpd_del(&tmp); +} + +/* ln(a) */ +void +mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + mpd_ssize_t adjexp, t; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + mpd_setspecial(result, MPD_POS, MPD_INF); + return; + } + if (mpd_iszerocoeff(a)) { + mpd_setspecial(result, MPD_NEG, MPD_INF); + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (_mpd_cmp(a, &one) == 0) { + _settriple(result, MPD_POS, 0, 0); + return; + } + /* + * Check if the result will overflow (0 < x, x != 1): + * 1) log10(x) < 0 iff adjexp(x) < 0 + * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) + * 3) 0 < x /\ x != 1 ==> 2 * abs(log10(x)) < abs(log(x)) + * 4) adjexp(x) <= log10(x) < adjexp(x) + 1 + * + * Case adjexp(x) >= 0: + * 5) 2 * adjexp(x) < abs(log(x)) + * Case adjexp(x) > 0: + * 6) adjexp(2 * adjexp(x)) <= adjexp(abs(log(x))) + * Case adjexp(x) == 0: + * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * + * Case adjexp(x) < 0: + * 7) 2 * (-adjexp(x) - 1) < abs(log(x)) + * Case adjexp(x) < -1: + * 8) adjexp(2 * (-adjexp(x) - 1)) <= adjexp(abs(log(x))) + * Case adjexp(x) == -1: + * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + */ + adjexp = mpd_adjexp(a); + t = (adjexp < 0) ? -adjexp-1 : adjexp; + t *= 2; + if (mpd_exp_digits(t)-1 > ctx->emax) { + *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; + mpd_setspecial(result, (adjexp<0), MPD_INF); + return; + } + + workctx = *ctx; + workctx.round = MPD_ROUND_HALF_EVEN; + + if (ctx->allcr) { + MPD_NEW_STATIC(t1, 0,0,0,0); + MPD_NEW_STATIC(t2, 0,0,0,0); + MPD_NEW_STATIC(ulp, 0,0,0,0); + MPD_NEW_STATIC(aa, 0,0,0,0); + mpd_ssize_t prec; + + if (result == a) { + if (!mpd_qcopy(&aa, a, status)) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + a = &aa; + } + + workctx.clamp = 0; + prec = ctx->prec + 3; + while (1) { + workctx.prec = prec; + _mpd_qln(result, a, &workctx, status); + _ssettriple(&ulp, MPD_POS, 1, + result->exp + result->digits-workctx.prec); + + workctx.prec = ctx->prec; + mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); + mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); + if (mpd_isspecial(result) || mpd_iszerocoeff(result) || + mpd_qcmp(&t1, &t2, status) == 0) { + workctx.clamp = ctx->clamp; + mpd_check_underflow(result, &workctx, status); + mpd_qfinalize(result, &workctx, status); + break; + } + prec += MPD_RDIGITS; + } + mpd_del(&t1); + mpd_del(&t2); + mpd_del(&ulp); + mpd_del(&aa); + } + else { + _mpd_qln(result, a, &workctx, status); + mpd_check_underflow(result, &workctx, status); + mpd_qfinalize(result, &workctx, status); + } +} + +/* + * Internal log10() function that does not check for specials, zero or one. + * Case SKIP_FINALIZE: + * Relative error: abs(result - log10(a)) < 0.1 * 10**-prec * abs(log10(a)) + * Case DO_FINALIZE: + * Ulp error: abs(result - log10(a)) < ulp(log10(a)) + */ +enum {SKIP_FINALIZE, DO_FINALIZE}; +static void +_mpd_qlog10(int action, mpd_t *result, const mpd_t *a, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_STATIC(ln10,0,0,0,0); + + mpd_maxcontext(&workctx); + workctx.prec = ctx->prec + 3; + /* relative error: 0.1 * 10**(-p-3). The specific underflow shortcut + * in _mpd_qln() does not change the final result. */ + _mpd_qln(result, a, &workctx, status); + /* relative error: 5 * 10**(-p-3) */ + mpd_qln10(&ln10, workctx.prec, status); + + if (action == DO_FINALIZE) { + workctx = *ctx; + workctx.round = MPD_ROUND_HALF_EVEN; + } + /* SKIP_FINALIZE: relative error: 5 * 10**(-p-3) */ + _mpd_qdiv(NO_IDEAL_EXP, result, result, &ln10, &workctx, status); + + mpd_del(&ln10); +} + +/* log10(a) */ +void +mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + mpd_ssize_t adjexp, t; + + workctx = *ctx; + workctx.round = MPD_ROUND_HALF_EVEN; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + mpd_setspecial(result, MPD_POS, MPD_INF); + return; + } + if (mpd_iszerocoeff(a)) { + mpd_setspecial(result, MPD_NEG, MPD_INF); + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (mpd_coeff_ispow10(a)) { + uint8_t sign = 0; + adjexp = mpd_adjexp(a); + if (adjexp < 0) { + sign = 1; + adjexp = -adjexp; + } + _settriple(result, sign, adjexp, 0); + mpd_qfinalize(result, &workctx, status); + return; + } + /* + * Check if the result will overflow (0 < x, x != 1): + * 1) log10(x) < 0 iff adjexp(x) < 0 + * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) + * 3) adjexp(x) <= log10(x) < adjexp(x) + 1 + * + * Case adjexp(x) >= 0: + * 4) adjexp(x) <= abs(log10(x)) + * Case adjexp(x) > 0: + * 5) adjexp(adjexp(x)) <= adjexp(abs(log10(x))) + * Case adjexp(x) == 0: + * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + * + * Case adjexp(x) < 0: + * 6) -adjexp(x) - 1 < abs(log10(x)) + * Case adjexp(x) < -1: + * 7) adjexp(-adjexp(x) - 1) <= adjexp(abs(log(x))) + * Case adjexp(x) == -1: + * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) + */ + adjexp = mpd_adjexp(a); + t = (adjexp < 0) ? -adjexp-1 : adjexp; + if (mpd_exp_digits(t)-1 > ctx->emax) { + *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; + mpd_setspecial(result, (adjexp<0), MPD_INF); + return; + } + + if (ctx->allcr) { + MPD_NEW_STATIC(t1, 0,0,0,0); + MPD_NEW_STATIC(t2, 0,0,0,0); + MPD_NEW_STATIC(ulp, 0,0,0,0); + MPD_NEW_STATIC(aa, 0,0,0,0); + mpd_ssize_t prec; + + if (result == a) { + if (!mpd_qcopy(&aa, a, status)) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + a = &aa; + } + + workctx.clamp = 0; + prec = ctx->prec + 3; + while (1) { + workctx.prec = prec; + _mpd_qlog10(SKIP_FINALIZE, result, a, &workctx, status); + _ssettriple(&ulp, MPD_POS, 1, + result->exp + result->digits-workctx.prec); + + workctx.prec = ctx->prec; + mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); + mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); + if (mpd_isspecial(result) || mpd_iszerocoeff(result) || + mpd_qcmp(&t1, &t2, status) == 0) { + workctx.clamp = ctx->clamp; + mpd_check_underflow(result, &workctx, status); + mpd_qfinalize(result, &workctx, status); + break; + } + prec += MPD_RDIGITS; + } + mpd_del(&t1); + mpd_del(&t2); + mpd_del(&ulp); + mpd_del(&aa); + } + else { + _mpd_qlog10(DO_FINALIZE, result, a, &workctx, status); + mpd_check_underflow(result, &workctx, status); + } +} + +/* + * Maximum of the two operands. Attention: If one operand is a quiet NaN and the + * other is numeric, the numeric operand is returned. This may not be what one + * expects. + */ +void +mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + int c; + + if (mpd_isqnan(a) && !mpd_isnan(b)) { + mpd_qcopy(result, b, status); + } + else if (mpd_isqnan(b) && !mpd_isnan(a)) { + mpd_qcopy(result, a, status); + } + else if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + else { + c = _mpd_cmp(a, b); + if (c == 0) { + c = _mpd_cmp_numequal(a, b); + } + + if (c < 0) { + mpd_qcopy(result, b, status); + } + else { + mpd_qcopy(result, a, status); + } + } + + mpd_qfinalize(result, ctx, status); +} + +/* + * Maximum magnitude: Same as mpd_max(), but compares the operands with their + * sign ignored. + */ +void +mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + int c; + + if (mpd_isqnan(a) && !mpd_isnan(b)) { + mpd_qcopy(result, b, status); + } + else if (mpd_isqnan(b) && !mpd_isnan(a)) { + mpd_qcopy(result, a, status); + } + else if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + else { + c = _mpd_cmp_abs(a, b); + if (c == 0) { + c = _mpd_cmp_numequal(a, b); + } + + if (c < 0) { + mpd_qcopy(result, b, status); + } + else { + mpd_qcopy(result, a, status); + } + } + + mpd_qfinalize(result, ctx, status); +} + +/* + * Minimum of the two operands. Attention: If one operand is a quiet NaN and the + * other is numeric, the numeric operand is returned. This may not be what one + * expects. + */ +void +mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + int c; + + if (mpd_isqnan(a) && !mpd_isnan(b)) { + mpd_qcopy(result, b, status); + } + else if (mpd_isqnan(b) && !mpd_isnan(a)) { + mpd_qcopy(result, a, status); + } + else if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + else { + c = _mpd_cmp(a, b); + if (c == 0) { + c = _mpd_cmp_numequal(a, b); + } + + if (c < 0) { + mpd_qcopy(result, a, status); + } + else { + mpd_qcopy(result, b, status); + } + } + + mpd_qfinalize(result, ctx, status); +} + +/* + * Minimum magnitude: Same as mpd_min(), but compares the operands with their + * sign ignored. + */ +void +mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + int c; + + if (mpd_isqnan(a) && !mpd_isnan(b)) { + mpd_qcopy(result, b, status); + } + else if (mpd_isqnan(b) && !mpd_isnan(a)) { + mpd_qcopy(result, a, status); + } + else if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + else { + c = _mpd_cmp_abs(a, b); + if (c == 0) { + c = _mpd_cmp_numequal(a, b); + } + + if (c < 0) { + mpd_qcopy(result, a, status); + } + else { + mpd_qcopy(result, b, status); + } + } + + mpd_qfinalize(result, ctx, status); +} + +/* Minimum space needed for the result array in _karatsuba_rec(). */ +static inline mpd_size_t +_kmul_resultsize(mpd_size_t la, mpd_size_t lb) +{ + mpd_size_t n, m; + + n = add_size_t(la, lb); + n = add_size_t(n, 1); + + m = (la+1)/2 + 1; + m = mul_size_t(m, 3); + + return (m > n) ? m : n; +} + +/* Work space needed in _karatsuba_rec(). lim >= 4 */ +static inline mpd_size_t +_kmul_worksize(mpd_size_t n, mpd_size_t lim) +{ + mpd_size_t m; + + if (n <= lim) { + return 0; + } + + m = (n+1)/2 + 1; + + return add_size_t(mul_size_t(m, 2), _kmul_worksize(m, lim)); +} + + +#define MPD_KARATSUBA_BASECASE 16 /* must be >= 4 */ + +/* + * Add the product of a and b to c. + * c must be _kmul_resultsize(la, lb) in size. + * w is used as a work array and must be _kmul_worksize(a, lim) in size. + * Roman E. Maeder, Storage Allocation for the Karatsuba Integer Multiplication + * Algorithm. In "Design and implementation of symbolic computation systems", + * Springer, 1993, ISBN 354057235X, 9783540572350. + */ +static void +_karatsuba_rec(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, + mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) +{ + mpd_size_t m, lt; + + assert(la >= lb && lb > 0); + assert(la <= MPD_KARATSUBA_BASECASE || w != NULL); + + if (la <= MPD_KARATSUBA_BASECASE) { + _mpd_basemul(c, a, b, la, lb); + return; + } + + m = (la+1)/2; /* ceil(la/2) */ + + /* lb <= m < la */ + if (lb <= m) { + + /* lb can now be larger than la-m */ + if (lb > la-m) { + lt = lb + lb + 1; /* space needed for result array */ + mpd_uint_zero(w, lt); /* clear result array */ + _karatsuba_rec(w, b, a+m, w+lt, lb, la-m); /* b*ah */ + } + else { + lt = (la-m) + (la-m) + 1; /* space needed for result array */ + mpd_uint_zero(w, lt); /* clear result array */ + _karatsuba_rec(w, a+m, b, w+lt, la-m, lb); /* ah*b */ + } + _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ + + lt = m + m + 1; /* space needed for the result array */ + mpd_uint_zero(w, lt); /* clear result array */ + _karatsuba_rec(w, a, b, w+lt, m, lb); /* al*b */ + _mpd_baseaddto(c, w, m+lb); /* add al*b */ + + return; + } + + /* la >= lb > m */ + memcpy(w, a, m * sizeof *w); + w[m] = 0; + _mpd_baseaddto(w, a+m, la-m); + + memcpy(w+(m+1), b, m * sizeof *w); + w[m+1+m] = 0; + _mpd_baseaddto(w+(m+1), b+m, lb-m); + + _karatsuba_rec(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1); + + lt = (la-m) + (la-m) + 1; + mpd_uint_zero(w, lt); + + _karatsuba_rec(w, a+m, b+m, w+lt, la-m, lb-m); + + _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); + _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); + + lt = m + m + 1; + mpd_uint_zero(w, lt); + + _karatsuba_rec(w, a, b, w+lt, m, m); + _mpd_baseaddto(c, w, m+m); + _mpd_basesubfrom(c+m, w, m+m); + + return; +} + +/* + * Multiply u and v, using Karatsuba multiplication. Returns a pointer + * to the result or NULL in case of failure (malloc error). + * Conditions: ulen >= vlen, ulen >= 4 + */ +static mpd_uint_t * +_mpd_kmul(const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t ulen, mpd_size_t vlen, + mpd_size_t *rsize) +{ + mpd_uint_t *result = NULL, *w = NULL; + mpd_size_t m; + + assert(ulen >= 4); + assert(ulen >= vlen); + + *rsize = _kmul_resultsize(ulen, vlen); + if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { + return NULL; + } + + m = _kmul_worksize(ulen, MPD_KARATSUBA_BASECASE); + if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { + mpd_free(result); + return NULL; + } + + _karatsuba_rec(result, u, v, w, ulen, vlen); + + + if (w) mpd_free(w); + return result; +} + + +/* + * Determine the minimum length for the number theoretic transform. Valid + * transform lengths are 2**n or 3*2**n, where 2**n <= MPD_MAXTRANSFORM_2N. + * The function finds the shortest length m such that rsize <= m. + */ +static inline mpd_size_t +_mpd_get_transform_len(mpd_size_t rsize) +{ + mpd_size_t log2rsize; + mpd_size_t x, step; + + assert(rsize >= 4); + log2rsize = mpd_bsr(rsize); + + if (rsize <= 1024) { + /* 2**n is faster in this range. */ + x = ((mpd_size_t)1)<<log2rsize; + return (rsize == x) ? x : x<<1; + } + else if (rsize <= MPD_MAXTRANSFORM_2N) { + x = ((mpd_size_t)1)<<log2rsize; + if (rsize == x) return x; + step = x>>1; + x += step; + return (rsize <= x) ? x : x + step; + } + else if (rsize <= MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2) { + return MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2; + } + else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { + return 3*MPD_MAXTRANSFORM_2N; + } + else { + return MPD_SIZE_MAX; + } +} + +#ifdef PPRO +#ifndef _MSC_VER +static inline unsigned short +_mpd_get_control87(void) +{ + unsigned short cw; + + __asm__ __volatile__ ("fnstcw %0" : "=m" (cw)); + return cw; +} + +static inline void +_mpd_set_control87(unsigned short cw) +{ + __asm__ __volatile__ ("fldcw %0" : : "m" (cw)); +} +#endif + +static unsigned int +mpd_set_fenv(void) +{ + unsigned int cw; +#ifdef _MSC_VER + unsigned int flags = + _EM_INVALID|_EM_DENORMAL|_EM_ZERODIVIDE|_EM_OVERFLOW| + _EM_UNDERFLOW|_EM_INEXACT|_RC_CHOP|_PC_64; + unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; + unsigned int dummy; + + __control87_2(0, 0, &cw, NULL); + __control87_2(flags, mask, &dummy, NULL); +#else + cw = _mpd_get_control87(); + _mpd_set_control87(cw|0xF3F); +#endif + return cw; +} + +static void +mpd_restore_fenv(unsigned int cw) +{ +#ifdef _MSC_VER + unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; + unsigned int dummy; + + __control87_2(cw, mask, &dummy, NULL); +#else + _mpd_set_control87((unsigned short)cw); +#endif +} +#endif /* PPRO */ + +/* + * Multiply u and v, using the fast number theoretic transform. Returns + * a pointer to the result or NULL in case of failure (malloc error). + */ +static mpd_uint_t * +_mpd_fntmul(const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t ulen, mpd_size_t vlen, + mpd_size_t *rsize) +{ + mpd_uint_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *vtmp = NULL; + mpd_size_t n; + +#ifdef PPRO + unsigned int cw; + cw = mpd_set_fenv(); +#endif + + *rsize = add_size_t(ulen, vlen); + if ((n = _mpd_get_transform_len(*rsize)) == MPD_SIZE_MAX) { + goto malloc_error; + } + + if ((c1 = mpd_calloc(n, sizeof *c1)) == NULL) { + goto malloc_error; + } + if ((c2 = mpd_calloc(n, sizeof *c2)) == NULL) { + goto malloc_error; + } + if ((c3 = mpd_calloc(n, sizeof *c3)) == NULL) { + goto malloc_error; + } + + memcpy(c1, u, ulen * (sizeof *c1)); + memcpy(c2, u, ulen * (sizeof *c2)); + memcpy(c3, u, ulen * (sizeof *c3)); + + if (u == v) { + if (!fnt_autoconvolute(c1, n, P1) || + !fnt_autoconvolute(c2, n, P2) || + !fnt_autoconvolute(c3, n, P3)) { + goto malloc_error; + } + } + else { + if ((vtmp = mpd_calloc(n, sizeof *vtmp)) == NULL) { + goto malloc_error; + } + + memcpy(vtmp, v, vlen * (sizeof *vtmp)); + if (!fnt_convolute(c1, vtmp, n, P1)) { + mpd_free(vtmp); + goto malloc_error; + } + + memcpy(vtmp, v, vlen * (sizeof *vtmp)); + mpd_uint_zero(vtmp+vlen, n-vlen); + if (!fnt_convolute(c2, vtmp, n, P2)) { + mpd_free(vtmp); + goto malloc_error; + } + + memcpy(vtmp, v, vlen * (sizeof *vtmp)); + mpd_uint_zero(vtmp+vlen, n-vlen); + if (!fnt_convolute(c3, vtmp, n, P3)) { + mpd_free(vtmp); + goto malloc_error; + } + + mpd_free(vtmp); + } + + crt3(c1, c2, c3, *rsize); + +out: +#ifdef PPRO + mpd_restore_fenv(cw); +#endif + if (c2) mpd_free(c2); + if (c3) mpd_free(c3); + return c1; + +malloc_error: + if (c1) mpd_free(c1); + c1 = NULL; + goto out; +} + + +/* + * Karatsuba multiplication with FNT/basemul as the base case. + */ +static int +_karatsuba_rec_fnt(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, + mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) +{ + mpd_size_t m, lt; + + assert(la >= lb && lb > 0); + assert(la <= 3*(MPD_MAXTRANSFORM_2N/2) || w != NULL); + + if (la <= 3*(MPD_MAXTRANSFORM_2N/2)) { + + if (lb <= 192) { + _mpd_basemul(c, b, a, lb, la); + } + else { + mpd_uint_t *result; + mpd_size_t dummy; + + if ((result = _mpd_fntmul(a, b, la, lb, &dummy)) == NULL) { + return 0; + } + memcpy(c, result, (la+lb) * (sizeof *result)); + mpd_free(result); + } + return 1; + } + + m = (la+1)/2; /* ceil(la/2) */ + + /* lb <= m < la */ + if (lb <= m) { + + /* lb can now be larger than la-m */ + if (lb > la-m) { + lt = lb + lb + 1; /* space needed for result array */ + mpd_uint_zero(w, lt); /* clear result array */ + if (!_karatsuba_rec_fnt(w, b, a+m, w+lt, lb, la-m)) { /* b*ah */ + return 0; /* GCOV_UNLIKELY */ + } + } + else { + lt = (la-m) + (la-m) + 1; /* space needed for result array */ + mpd_uint_zero(w, lt); /* clear result array */ + if (!_karatsuba_rec_fnt(w, a+m, b, w+lt, la-m, lb)) { /* ah*b */ + return 0; /* GCOV_UNLIKELY */ + } + } + _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ + + lt = m + m + 1; /* space needed for the result array */ + mpd_uint_zero(w, lt); /* clear result array */ + if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, lb)) { /* al*b */ + return 0; /* GCOV_UNLIKELY */ + } + _mpd_baseaddto(c, w, m+lb); /* add al*b */ + + return 1; + } + + /* la >= lb > m */ + memcpy(w, a, m * sizeof *w); + w[m] = 0; + _mpd_baseaddto(w, a+m, la-m); + + memcpy(w+(m+1), b, m * sizeof *w); + w[m+1+m] = 0; + _mpd_baseaddto(w+(m+1), b+m, lb-m); + + if (!_karatsuba_rec_fnt(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1)) { + return 0; /* GCOV_UNLIKELY */ + } + + lt = (la-m) + (la-m) + 1; + mpd_uint_zero(w, lt); + + if (!_karatsuba_rec_fnt(w, a+m, b+m, w+lt, la-m, lb-m)) { + return 0; /* GCOV_UNLIKELY */ + } + + _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); + _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); + + lt = m + m + 1; + mpd_uint_zero(w, lt); + + if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, m)) { + return 0; /* GCOV_UNLIKELY */ + } + _mpd_baseaddto(c, w, m+m); + _mpd_basesubfrom(c+m, w, m+m); + + return 1; +} + +/* + * Multiply u and v, using Karatsuba multiplication with the FNT as the + * base case. Returns a pointer to the result or NULL in case of failure + * (malloc error). Conditions: ulen >= vlen, ulen >= 4. + */ +static mpd_uint_t * +_mpd_kmul_fnt(const mpd_uint_t *u, const mpd_uint_t *v, + mpd_size_t ulen, mpd_size_t vlen, + mpd_size_t *rsize) +{ + mpd_uint_t *result = NULL, *w = NULL; + mpd_size_t m; + + assert(ulen >= 4); + assert(ulen >= vlen); + + *rsize = _kmul_resultsize(ulen, vlen); + if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { + return NULL; + } + + m = _kmul_worksize(ulen, 3*(MPD_MAXTRANSFORM_2N/2)); + if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { + mpd_free(result); /* GCOV_UNLIKELY */ + return NULL; /* GCOV_UNLIKELY */ + } + + if (!_karatsuba_rec_fnt(result, u, v, w, ulen, vlen)) { + mpd_free(result); + result = NULL; + } + + + if (w) mpd_free(w); + return result; +} + + +/* Deal with the special cases of multiplying infinities. */ +static void +_mpd_qmul_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status) +{ + if (mpd_isinfinite(a)) { + if (mpd_iszero(b)) { + mpd_seterror(result, MPD_Invalid_operation, status); + } + else { + mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); + } + return; + } + assert(mpd_isinfinite(b)); + if (mpd_iszero(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + } + else { + mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); + } +} + +/* + * Internal function: Multiply a and b. _mpd_qmul deals with specials but + * does NOT finalize the result. This is for use in mpd_fma(). + */ +static inline void +_mpd_qmul(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 *rdata = NULL; + mpd_uint_t rbuf[MPD_MINALLOC_MAX]; + mpd_size_t rsize, i; + + + if (mpd_isspecial(a) || mpd_isspecial(b)) { + if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + _mpd_qmul_inf(result, a, b, status); + return; + } + + if (small->len > big->len) { + _mpd_ptrswap(&big, &small); + } + + rsize = big->len + small->len; + + if (big->len == 1) { + _mpd_singlemul(result->data, big->data[0], small->data[0]); + goto finish; + } + if (rsize <= (mpd_size_t)MPD_MINALLOC_MAX) { + if (big->len == 2) { + _mpd_mul_2_le2(rbuf, big->data, small->data, small->len); + } + else { + mpd_uint_zero(rbuf, rsize); + if (small->len == 1) { + _mpd_shortmul(rbuf, big->data, big->len, small->data[0]); + } + else { + _mpd_basemul(rbuf, small->data, big->data, small->len, big->len); + } + } + if (!mpd_qresize(result, rsize, status)) { + return; + } + for(i = 0; i < rsize; i++) { + result->data[i] = rbuf[i]; + } + goto finish; + } + + + if (small->len <= 256) { + rdata = mpd_calloc(rsize, sizeof *rdata); + if (rdata != NULL) { + if (small->len == 1) { + _mpd_shortmul(rdata, big->data, big->len, small->data[0]); + } + else { + _mpd_basemul(rdata, small->data, big->data, small->len, big->len); + } + } + } + else if (rsize <= 1024) { + rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize); + } + else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { + rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize); + } + else { + rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize); + } + + if (rdata == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + + if (mpd_isdynamic_data(result)) { + mpd_free(result->data); + } + result->data = rdata; + result->alloc = rsize; + mpd_set_dynamic_data(result); + + +finish: + mpd_set_flags(result, mpd_sign(a)^mpd_sign(b)); + result->exp = big->exp + small->exp; + result->len = _mpd_real_size(result->data, rsize); + /* resize to smaller cannot fail */ + mpd_qresize(result, result->len, status); + mpd_setdigits(result); +} + +/* Multiply a and b. */ +void +mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + _mpd_qmul(result, a, b, ctx, status); + mpd_qfinalize(result, ctx, status); +} + +/* Multiply a and b. Set NaN/Invalid_operation if the result is inexact. */ +static void +_mpd_qmul_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_qmul(result, a, b, ctx, &workstatus); + *status |= workstatus; + if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { + mpd_seterror(result, MPD_Invalid_operation, status); + } +} + +/* Multiply decimal and mpd_ssize_t. */ +void +mpd_qmul_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_qmul(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +/* Multiply decimal and mpd_uint_t. */ +void +mpd_qmul_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_qmul(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +void +mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qmul_ssize(result, a, b, ctx, status); +} + +void +mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qmul_uint(result, a, b, ctx, status); +} + +#ifdef CONFIG_64 +void +mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qmul_ssize(result, a, b, ctx, status); +} + +void +mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_qmul_uint(result, a, b, ctx, status); +} +#elif !defined(LEGACY_COMPILER) +/* Multiply decimal and int64_t. */ +void +mpd_qmul_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_qmul(result, a, &bb, ctx, status); + mpd_del(&bb); +} + +/* Multiply decimal and uint64_t. */ +void +mpd_qmul_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_qmul(result, a, &bb, ctx, status); + mpd_del(&bb); +} +#endif + +/* Like the minus operator. */ +void +mpd_qminus(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_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { + mpd_qcopy_abs(result, a, status); + } + else { + mpd_qcopy_negate(result, a, status); + } + + mpd_qfinalize(result, ctx, status); +} + +/* Like the plus operator. */ +void +mpd_qplus(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_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { + mpd_qcopy_abs(result, a, status); + } + else { + mpd_qcopy(result, a, status); + } + + mpd_qfinalize(result, ctx, status); +} + +/* The largest representable number that is smaller than the operand. */ +void +mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + + assert(mpd_isinfinite(a)); + if (mpd_isnegative(a)) { + mpd_qcopy(result, a, status); + return; + } + else { + mpd_clear_flags(result); + mpd_qmaxcoeff(result, ctx, status); + if (mpd_isnan(result)) { + return; + } + result->exp = mpd_etop(ctx); + return; + } + } + + mpd_workcontext(&workctx, ctx); + workctx.round = MPD_ROUND_FLOOR; + + if (!mpd_qcopy(result, a, status)) { + return; + } + + mpd_qfinalize(result, &workctx, &workctx.status); + if (workctx.status&(MPD_Inexact|MPD_Errors)) { + *status |= (workctx.status&MPD_Errors); + return; + } + + workctx.status = 0; + mpd_qsub(result, a, &tiny, &workctx, &workctx.status); + *status |= (workctx.status&MPD_Errors); +} + +/* The smallest representable number that is larger than the operand. */ +void +mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + + assert(mpd_isinfinite(a)); + if (mpd_ispositive(a)) { + mpd_qcopy(result, a, status); + } + else { + mpd_clear_flags(result); + mpd_qmaxcoeff(result, ctx, status); + if (mpd_isnan(result)) { + return; + } + mpd_set_flags(result, MPD_NEG); + result->exp = mpd_etop(ctx); + } + return; + } + + mpd_workcontext(&workctx, ctx); + workctx.round = MPD_ROUND_CEILING; + + if (!mpd_qcopy(result, a, status)) { + return; + } + + mpd_qfinalize(result, &workctx, &workctx.status); + if (workctx.status & (MPD_Inexact|MPD_Errors)) { + *status |= (workctx.status&MPD_Errors); + return; + } + + workctx.status = 0; + mpd_qadd(result, a, &tiny, &workctx, &workctx.status); + *status |= (workctx.status&MPD_Errors); +} + +/* + * The number closest to the first operand that is in the direction towards + * the second operand. + */ +void +mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + int c; + + if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + + c = _mpd_cmp(a, b); + if (c == 0) { + mpd_qcopy_sign(result, a, b, status); + return; + } + + if (c < 0) { + mpd_qnext_plus(result, a, ctx, status); + } + else { + mpd_qnext_minus(result, a, ctx, status); + } + + if (mpd_isinfinite(result)) { + *status |= (MPD_Overflow|MPD_Rounded|MPD_Inexact); + } + else if (mpd_adjexp(result) < ctx->emin) { + *status |= (MPD_Underflow|MPD_Subnormal|MPD_Rounded|MPD_Inexact); + if (mpd_iszero(result)) { + *status |= MPD_Clamped; + } + } +} + +/* + * Internal function: Integer power with mpd_uint_t exponent. The function + * can fail with MPD_Malloc_error. + * + * The error is equal to the error incurred in k-1 multiplications. Assuming + * the upper bound for the relative error in each operation: + * + * abs(err) = 5 * 10**-prec + * result = x**k * (1 + err)**(k-1) + */ +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) +{ + uint32_t workstatus = 0; + mpd_uint_t n; + + if (exp == 0) { + _settriple(result, resultsign, 1, 0); /* GCOV_NOT_REACHED */ + return; /* GCOV_NOT_REACHED */ + } + + if (!mpd_qcopy(result, base, status)) { + return; + } + + n = mpd_bits[mpd_bsr(exp)]; + while (n >>= 1) { + mpd_qmul(result, result, result, ctx, &workstatus); + if (exp & n) { + mpd_qmul(result, result, base, ctx, &workstatus); + } + if (mpd_isspecial(result) || + (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { + break; + } + } + + *status |= workstatus; + mpd_set_sign(result, resultsign); +} + +/* + * Internal function: Integer power with mpd_t exponent, tbase and texp + * are modified!! Function can fail with MPD_Malloc_error. + * + * The error is equal to the error incurred in k multiplications. Assuming + * the upper bound for the relative error in each operation: + * + * abs(err) = 5 * 10**-prec + * result = x**k * (1 + err)**k + */ +static inline void +_mpd_qpow_mpd(mpd_t *result, mpd_t *tbase, mpd_t *texp, uint8_t resultsign, + const mpd_context_t *ctx, uint32_t *status) +{ + uint32_t workstatus = 0; + mpd_context_t maxctx; + MPD_NEW_CONST(two,0,0,1,1,1,2); + + + mpd_maxcontext(&maxctx); + + /* resize to smaller cannot fail */ + mpd_qcopy(result, &one, status); + + while (!mpd_iszero(texp)) { + if (mpd_isodd(texp)) { + mpd_qmul(result, result, tbase, ctx, &workstatus); + *status |= workstatus; + if (mpd_isspecial(result) || + (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { + break; + } + } + mpd_qmul(tbase, tbase, tbase, ctx, &workstatus); + mpd_qdivint(texp, texp, &two, &maxctx, &workstatus); + if (mpd_isnan(tbase) || mpd_isnan(texp)) { + mpd_seterror(result, workstatus&MPD_Errors, status); + return; + } + } + mpd_set_sign(result, resultsign); +} + +/* + * The power function for integer exponents. Relative error _before_ the + * final rounding to prec: + * abs(result - base**exp) < 0.1 * 10**-prec * abs(base**exp) + */ +static void +_mpd_qpow_int(mpd_t *result, const mpd_t *base, const mpd_t *exp, + uint8_t resultsign, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_STATIC(tbase,0,0,0,0); + MPD_NEW_STATIC(texp,0,0,0,0); + mpd_ssize_t n; + + + mpd_workcontext(&workctx, ctx); + workctx.prec += (exp->digits + exp->exp + 2); + workctx.round = MPD_ROUND_HALF_EVEN; + workctx.clamp = 0; + if (mpd_isnegative(exp)) { uint32_t workstatus = 0; - workctx.prec += 1; + workctx.prec += 1; mpd_qdiv(&tbase, &one, base, &workctx, &workstatus); *status |= workstatus; if (workstatus&MPD_Errors) { - mpd_setspecial(result, MPD_POS, MPD_NAN); - goto finish; - } - } - else { - if (!mpd_qcopy(&tbase, base, status)) { - mpd_setspecial(result, MPD_POS, MPD_NAN); - goto finish; - } - } - - n = mpd_qabs_uint(exp, &workctx.status); - if (workctx.status&MPD_Invalid_operation) { - if (!mpd_qcopy(&texp, exp, status)) { - mpd_setspecial(result, MPD_POS, MPD_NAN); /* GCOV_UNLIKELY */ - goto finish; /* GCOV_UNLIKELY */ - } - _mpd_qpow_mpd(result, &tbase, &texp, resultsign, &workctx, status); - } - else { - _mpd_qpow_uint(result, &tbase, n, resultsign, &workctx, status); - } - - if (mpd_isinfinite(result)) { - /* for ROUND_DOWN, ROUND_FLOOR, etc. */ - _settriple(result, resultsign, 1, MPD_EXP_INF); - } - -finish: - mpd_del(&tbase); - mpd_del(&texp); - mpd_qfinalize(result, ctx, status); -} - -/* - * If the exponent is infinite and base equals one, the result is one - * with a coefficient of length prec. Otherwise, result is undefined. - * Return the value of the comparison against one. - */ -static int -_qcheck_pow_one_inf(mpd_t *result, const mpd_t *base, uint8_t resultsign, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_ssize_t shift; - int cmp; - - if ((cmp = _mpd_cmp(base, &one)) == 0) { - shift = ctx->prec-1; - mpd_qshiftl(result, &one, shift, status); - result->exp = -shift; - mpd_set_flags(result, resultsign); - *status |= (MPD_Inexact|MPD_Rounded); - } - - return cmp; -} - -/* - * If abs(base) equals one, calculate the correct power of one result. - * Otherwise, result is undefined. Return the value of the comparison - * against 1. - * - * This is an internal function that does not check for specials. - */ -static int -_qcheck_pow_one(mpd_t *result, const mpd_t *base, const mpd_t *exp, - uint8_t resultsign, - const mpd_context_t *ctx, uint32_t *status) -{ - uint32_t workstatus = 0; - mpd_ssize_t shift; - int cmp; - - if ((cmp = _mpd_cmp_abs(base, &one)) == 0) { - if (_mpd_isint(exp)) { - if (mpd_isnegative(exp)) { - _settriple(result, resultsign, 1, 0); - return 0; - } - /* 1.000**3 = 1.000000000 */ - mpd_qmul_ssize(result, exp, -base->exp, ctx, &workstatus); - if (workstatus&MPD_Errors) { - *status |= (workstatus&MPD_Errors); - return 0; - } - /* digits-1 after exponentiation */ - shift = mpd_qget_ssize(result, &workstatus); - /* shift is MPD_SSIZE_MAX if result is too large */ - if (shift > ctx->prec-1) { - shift = ctx->prec-1; - *status |= MPD_Rounded; - } - } - else if (mpd_ispositive(base)) { - shift = ctx->prec-1; - *status |= (MPD_Inexact|MPD_Rounded); - } - else { - return -2; /* GCOV_NOT_REACHED */ - } - if (!mpd_qshiftl(result, &one, shift, status)) { - return 0; - } - result->exp = -shift; - mpd_set_flags(result, resultsign); - } - - return cmp; -} - -/* - * Detect certain over/underflow of x**y. - * ACL2 proof: pow-bounds.lisp. - * - * Symbols: - * - * e: EXP_INF or EXP_CLAMP - * x: base - * y: exponent - * - * omega(e) = log10(abs(e)) - * zeta(x) = log10(abs(log10(x))) - * theta(y) = log10(abs(y)) - * - * Upper and lower bounds: - * - * ub_omega(e) = ceil(log10(abs(e))) - * lb_theta(y) = floor(log10(abs(y))) - * - * | floor(log10(floor(abs(log10(x))))) if x < 1/10 or x >= 10 - * lb_zeta(x) = | floor(log10(abs(x-1)/10)) if 1/10 <= x < 1 - * | floor(log10(abs((x-1)/100))) if 1 < x < 10 - * - * ub_omega(e) and lb_theta(y) are obviously upper and lower bounds - * for omega(e) and theta(y). - * - * lb_zeta is a lower bound for zeta(x): - * - * x < 1/10 or x >= 10: - * - * abs(log10(x)) >= 1, so the outer log10 is well defined. Since log10 - * is strictly increasing, the end result is a lower bound. - * - * 1/10 <= x < 1: - * - * We use: log10(x) <= (x-1)/log(10) - * abs(log10(x)) >= abs(x-1)/log(10) - * abs(log10(x)) >= abs(x-1)/10 - * - * 1 < x < 10: - * - * We use: (x-1)/(x*log(10)) < log10(x) - * abs((x-1)/100) < abs(log10(x)) - * - * XXX: abs((x-1)/10) would work, need ACL2 proof. - * - * - * Let (0 < x < 1 and y < 0) or (x > 1 and y > 0). (H1) - * Let ub_omega(exp_inf) < lb_zeta(x) + lb_theta(y) (H2) - * - * Then: - * log10(abs(exp_inf)) < log10(abs(log10(x))) + log10(abs(y)). (1) - * exp_inf < log10(x) * y (2) - * 10**exp_inf < x**y (3) - * - * Let (0 < x < 1 and y > 0) or (x > 1 and y < 0). (H3) - * Let ub_omega(exp_clamp) < lb_zeta(x) + lb_theta(y) (H4) - * - * Then: - * log10(abs(exp_clamp)) < log10(abs(log10(x))) + log10(abs(y)). (4) - * log10(x) * y < exp_clamp (5) - * x**y < 10**exp_clamp (6) - * - */ -static mpd_ssize_t -_lower_bound_zeta(const mpd_t *x, uint32_t *status) -{ - mpd_context_t maxctx; - MPD_NEW_STATIC(scratch,0,0,0,0); - mpd_ssize_t t, u; - - t = mpd_adjexp(x); - if (t > 0) { - /* x >= 10 -> floor(log10(floor(abs(log10(x))))) */ - return mpd_exp_digits(t) - 1; - } - else if (t < -1) { - /* x < 1/10 -> floor(log10(floor(abs(log10(x))))) */ - return mpd_exp_digits(t+1) - 1; - } - else { - mpd_maxcontext(&maxctx); - mpd_qsub(&scratch, x, &one, &maxctx, status); - if (mpd_isspecial(&scratch)) { - mpd_del(&scratch); - return MPD_SSIZE_MAX; - } - u = mpd_adjexp(&scratch); - mpd_del(&scratch); - - /* t == -1, 1/10 <= x < 1 -> floor(log10(abs(x-1)/10)) - * t == 0, 1 < x < 10 -> floor(log10(abs(x-1)/100)) */ - return (t == 0) ? u-2 : u-1; - } -} - -/* - * Detect cases of certain overflow/underflow in the power function. - * Assumptions: x != 1, y != 0. The proof above is for positive x. - * If x is negative and y is an odd integer, x**y == -(abs(x)**y), - * so the analysis does not change. - */ -static int -_qcheck_pow_bounds(mpd_t *result, const mpd_t *x, const mpd_t *y, - uint8_t resultsign, - const mpd_context_t *ctx, uint32_t *status) -{ - MPD_NEW_SHARED(abs_x, x); - mpd_ssize_t ub_omega, lb_zeta, lb_theta; - uint8_t sign; - - mpd_set_positive(&abs_x); - - lb_theta = mpd_adjexp(y); - lb_zeta = _lower_bound_zeta(&abs_x, status); - if (lb_zeta == MPD_SSIZE_MAX) { - mpd_seterror(result, MPD_Malloc_error, status); - return 1; - } - - sign = (mpd_adjexp(&abs_x) < 0) ^ mpd_sign(y); - if (sign == 0) { - /* (0 < |x| < 1 and y < 0) or (|x| > 1 and y > 0) */ - ub_omega = mpd_exp_digits(ctx->emax); - if (ub_omega < lb_zeta + lb_theta) { - _settriple(result, resultsign, 1, MPD_EXP_INF); - mpd_qfinalize(result, ctx, status); - return 1; - } - } - else { - /* (0 < |x| < 1 and y > 0) or (|x| > 1 and y < 0). */ - ub_omega = mpd_exp_digits(mpd_etiny(ctx)); - if (ub_omega < lb_zeta + lb_theta) { - _settriple(result, resultsign, 1, mpd_etiny(ctx)-1); - mpd_qfinalize(result, ctx, status); - return 1; - } - } - - return 0; -} - -/* - * TODO: Implement algorithm for computing exact powers from decimal.py. - * In order to prevent infinite loops, this has to be called before - * using Ziv's strategy for correct rounding. - */ -/* -static int -_mpd_qpow_exact(mpd_t *result, const mpd_t *base, const mpd_t *exp, - const mpd_context_t *ctx, uint32_t *status) -{ - return 0; -} -*/ - -/* - * The power function for real exponents. - * Relative error: abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) - */ -static void -_mpd_qpow_real(mpd_t *result, const mpd_t *base, const mpd_t *exp, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_STATIC(texp,0,0,0,0); - - if (!mpd_qcopy(&texp, exp, status)) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - - mpd_maxcontext(&workctx); - workctx.prec = (base->digits > ctx->prec) ? base->digits : ctx->prec; - workctx.prec += (4 + MPD_EXPDIGITS); - workctx.round = MPD_ROUND_HALF_EVEN; - workctx.allcr = ctx->allcr; - - /* - * extra := MPD_EXPDIGITS = MPD_EXP_MAX_T - * wp := prec + 4 + extra - * abs(err) < 5 * 10**-wp - * y := log(base) * exp - * Calculate: - * 1) e**(y * (1 + err)**2) * (1 + err) - * = e**y * e**(y * (2*err + err**2)) * (1 + err) - * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - * Relative error of the underlined term: - * 2) abs(e**(y * (2*err + err**2)) - 1) - * Case abs(y) >= 10**extra: - * 3) adjexp(y)+1 > log10(abs(y)) >= extra - * This triggers the Overflow/Underflow shortcut in _mpd_qexp(), - * so no further analysis is necessary. - * Case abs(y) < 10**extra: - * 4) abs(y * (2*err + err**2)) < 1/5 * 10**(-prec - 2) - * Use (see _mpd_qexp): - * 5) abs(x) <= 9/10 * 10**-p ==> abs(e**x - 1) < 10**-p - * With 2), 4) and 5): - * 6) abs(e**(y * (2*err + err**2)) - 1) < 10**(-prec - 2) - * The complete relative error of 1) is: - * 7) abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) - */ - mpd_qln(result, base, &workctx, &workctx.status); - mpd_qmul(result, result, &texp, &workctx, &workctx.status); - mpd_qexp(result, result, &workctx, status); - - mpd_del(&texp); - *status |= (workctx.status&MPD_Errors); - *status |= (MPD_Inexact|MPD_Rounded); -} - -/* The power function: base**exp */ -void -mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, - const mpd_context_t *ctx, uint32_t *status) -{ - uint8_t resultsign = 0; - int intexp = 0; - int cmp; - - if (mpd_isspecial(base) || mpd_isspecial(exp)) { - if (mpd_qcheck_nans(result, base, exp, ctx, status)) { - return; - } - } - if (mpd_isinteger(exp)) { - intexp = 1; - resultsign = mpd_isnegative(base) && mpd_isodd(exp); - } - - if (mpd_iszero(base)) { - if (mpd_iszero(exp)) { - mpd_seterror(result, MPD_Invalid_operation, status); - } - else if (mpd_isnegative(exp)) { - mpd_setspecial(result, resultsign, MPD_INF); - } - else { - _settriple(result, resultsign, 0, 0); - } - return; - } - if (mpd_isnegative(base)) { - if (!intexp || mpd_isinfinite(exp)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - } - if (mpd_isinfinite(exp)) { - /* power of one */ - cmp = _qcheck_pow_one_inf(result, base, resultsign, ctx, status); - if (cmp == 0) { - return; - } - else { - cmp *= mpd_arith_sign(exp); - if (cmp < 0) { - _settriple(result, resultsign, 0, 0); - } - else { - mpd_setspecial(result, resultsign, MPD_INF); - } - } - return; - } - if (mpd_isinfinite(base)) { - if (mpd_iszero(exp)) { - _settriple(result, resultsign, 1, 0); - } - else if (mpd_isnegative(exp)) { - _settriple(result, resultsign, 0, 0); - } - else { - mpd_setspecial(result, resultsign, MPD_INF); - } - return; - } - if (mpd_iszero(exp)) { - _settriple(result, resultsign, 1, 0); - return; - } - if (_qcheck_pow_one(result, base, exp, resultsign, ctx, status) == 0) { - return; - } - if (_qcheck_pow_bounds(result, base, exp, resultsign, ctx, status)) { - return; - } - - if (intexp) { - _mpd_qpow_int(result, base, exp, resultsign, ctx, status); - } - else { - _mpd_qpow_real(result, base, exp, ctx, status); - if (!mpd_isspecial(result) && _mpd_cmp(result, &one) == 0) { - mpd_ssize_t shift = ctx->prec-1; - mpd_qshiftl(result, &one, shift, status); - result->exp = -shift; - } - if (mpd_isinfinite(result)) { - /* for ROUND_DOWN, ROUND_FLOOR, etc. */ - _settriple(result, MPD_POS, 1, MPD_EXP_INF); - } - mpd_qfinalize(result, ctx, status); - } -} - -/* - * Internal function: Integer powmod with mpd_uint_t exponent, base is modified! - * Function can fail with MPD_Malloc_error. - */ -static inline void -_mpd_qpowmod_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp, - const mpd_t *mod, uint32_t *status) -{ - mpd_context_t maxcontext; - - mpd_maxcontext(&maxcontext); - - /* resize to smaller cannot fail */ - mpd_qcopy(result, &one, status); - - while (exp > 0) { - if (exp & 1) { - _mpd_qmul_exact(result, result, base, &maxcontext, status); - mpd_qrem(result, result, mod, &maxcontext, status); - } - _mpd_qmul_exact(base, base, base, &maxcontext, status); - mpd_qrem(base, base, mod, &maxcontext, status); - exp >>= 1; - } -} - -/* The powmod function: (base**exp) % mod */ -void -mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, - const mpd_t *mod, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t maxcontext; - MPD_NEW_STATIC(tbase,0,0,0,0); - MPD_NEW_STATIC(texp,0,0,0,0); - MPD_NEW_STATIC(tmod,0,0,0,0); - MPD_NEW_STATIC(tmp,0,0,0,0); - MPD_NEW_CONST(two,0,0,1,1,1,2); - mpd_ssize_t tbase_exp, texp_exp; - mpd_ssize_t i; - mpd_t t; - mpd_uint_t r; - uint8_t sign; - - - if (mpd_isspecial(base) || mpd_isspecial(exp) || mpd_isspecial(mod)) { - if (mpd_qcheck_3nans(result, base, exp, mod, ctx, status)) { - return; - } - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - - if (!_mpd_isint(base) || !_mpd_isint(exp) || !_mpd_isint(mod)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (mpd_iszerocoeff(mod)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (mod->digits+mod->exp > ctx->prec) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - sign = (mpd_isnegative(base)) && (mpd_isodd(exp)); - if (mpd_iszerocoeff(exp)) { - if (mpd_iszerocoeff(base)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - r = (_mpd_cmp_abs(mod, &one)==0) ? 0 : 1; - _settriple(result, sign, r, 0); - return; - } - if (mpd_isnegative(exp)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (mpd_iszerocoeff(base)) { - _settriple(result, sign, 0, 0); - return; - } - - mpd_maxcontext(&maxcontext); - - mpd_qrescale(&tmod, mod, 0, &maxcontext, &maxcontext.status); - if (maxcontext.status&MPD_Errors) { - mpd_seterror(result, maxcontext.status&MPD_Errors, status); - goto out; - } - maxcontext.status = 0; - mpd_set_positive(&tmod); - - mpd_qround_to_int(&tbase, base, &maxcontext, status); - mpd_set_positive(&tbase); - tbase_exp = tbase.exp; - tbase.exp = 0; - - mpd_qround_to_int(&texp, exp, &maxcontext, status); - texp_exp = texp.exp; - texp.exp = 0; - - /* base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo */ - mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); - mpd_qshiftl(result, &one, tbase_exp, status); - mpd_qrem(result, result, &tmod, &maxcontext, status); - _mpd_qmul_exact(&tbase, &tbase, result, &maxcontext, status); - mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); - if (mpd_isspecial(&tbase) || - mpd_isspecial(&texp) || - mpd_isspecial(&tmod)) { - goto mpd_errors; - } - - for (i = 0; i < texp_exp; i++) { - _mpd_qpowmod_uint(&tmp, &tbase, 10, &tmod, status); - t = tmp; - tmp = tbase; - tbase = t; - } - if (mpd_isspecial(&tbase)) { - goto mpd_errors; /* GCOV_UNLIKELY */ - } - - /* resize to smaller cannot fail */ - mpd_qcopy(result, &one, status); - while (mpd_isfinite(&texp) && !mpd_iszero(&texp)) { - if (mpd_isodd(&texp)) { - _mpd_qmul_exact(result, result, &tbase, &maxcontext, status); - mpd_qrem(result, result, &tmod, &maxcontext, status); - } - _mpd_qmul_exact(&tbase, &tbase, &tbase, &maxcontext, status); - mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); - mpd_qdivint(&texp, &texp, &two, &maxcontext, status); - } - if (mpd_isspecial(&texp) || mpd_isspecial(&tbase) || - mpd_isspecial(&tmod) || mpd_isspecial(result)) { - /* MPD_Malloc_error */ - goto mpd_errors; - } - else { - mpd_set_sign(result, sign); - } - -out: - mpd_del(&tbase); - mpd_del(&texp); - mpd_del(&tmod); - mpd_del(&tmp); - return; - -mpd_errors: - mpd_setspecial(result, MPD_POS, MPD_NAN); - goto out; -} - -void -mpd_qquantize(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 b_exp = b->exp; - mpd_ssize_t expdiff, shift; - mpd_uint_t rnd; - - if (mpd_isspecial(a) || mpd_isspecial(b)) { - if (mpd_qcheck_nans(result, a, b, ctx, status)) { - return; - } - if (mpd_isinfinite(a) && mpd_isinfinite(b)) { - mpd_qcopy(result, a, status); - return; - } - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - if (b->exp > ctx->emax || b->exp < mpd_etiny(ctx)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - if (mpd_iszero(a)) { - _settriple(result, mpd_sign(a), 0, b->exp); - mpd_qfinalize(result, ctx, status); - return; - } - - - expdiff = a->exp - b->exp; - if (a->digits + expdiff > ctx->prec) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - if (expdiff >= 0) { - shift = expdiff; - if (!mpd_qshiftl(result, a, shift, status)) { - return; - } - result->exp = b_exp; - } - else { - /* At this point expdiff < 0 and a->digits+expdiff <= prec, - * so the shift before an increment will fit in prec. */ - shift = -expdiff; - rnd = mpd_qshiftr(result, a, shift, status); - if (rnd == MPD_UINT_MAX) { - return; - } - result->exp = b_exp; - if (!_mpd_apply_round_fit(result, rnd, ctx, status)) { - return; - } - workstatus |= MPD_Rounded; - if (rnd) { - workstatus |= MPD_Inexact; - } - } - - if (mpd_adjexp(result) > ctx->emax || - mpd_adjexp(result) < mpd_etiny(ctx)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - *status |= workstatus; - mpd_qfinalize(result, ctx, status); -} - -void -mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_ssize_t shift, maxexp, maxshift; - uint8_t sign_a = mpd_sign(a); - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - mpd_qcopy(result, a, status); - return; - } - - if (!mpd_qcopy(result, a, status)) { - return; - } - mpd_qfinalize(result, ctx, status); - if (mpd_isspecial(result)) { - return; - } - if (mpd_iszero(result)) { - _settriple(result, sign_a, 0, 0); - return; - } - - shift = mpd_trail_zeros(result); - maxexp = (ctx->clamp) ? mpd_etop(ctx) : ctx->emax; - /* After the finalizing above result->exp <= maxexp. */ - maxshift = maxexp - result->exp; - shift = (shift > maxshift) ? maxshift : shift; - - mpd_qshiftr_inplace(result, shift); - result->exp += shift; -} - -void -mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, - uint32_t *status) -{ - MPD_NEW_STATIC(q,0,0,0,0); - - if (mpd_isspecial(a) || mpd_isspecial(b)) { - if (mpd_qcheck_nans(r, a, b, ctx, status)) { - return; - } - if (mpd_isinfinite(a)) { - mpd_seterror(r, MPD_Invalid_operation, status); - return; - } - if (mpd_isinfinite(b)) { - mpd_qcopy(r, a, status); - mpd_qfinalize(r, ctx, status); - return; - } - /* debug */ - abort(); /* GCOV_NOT_REACHED */ - } - if (mpd_iszerocoeff(b)) { - if (mpd_iszerocoeff(a)) { - mpd_seterror(r, MPD_Division_undefined, status); - } - else { - mpd_seterror(r, MPD_Invalid_operation, status); - } - return; - } - - _mpd_qdivmod(&q, r, a, b, ctx, status); - mpd_del(&q); - mpd_qfinalize(r, ctx, status); -} - -void -mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t workctx; - MPD_NEW_STATIC(btmp,0,0,0,0); - MPD_NEW_STATIC(q,0,0,0,0); - mpd_ssize_t expdiff, qdigits; - int cmp, isodd, allnine; - + mpd_setspecial(result, MPD_POS, MPD_NAN); + goto finish; + } + } + else { + if (!mpd_qcopy(&tbase, base, status)) { + mpd_setspecial(result, MPD_POS, MPD_NAN); + goto finish; + } + } + + n = mpd_qabs_uint(exp, &workctx.status); + if (workctx.status&MPD_Invalid_operation) { + if (!mpd_qcopy(&texp, exp, status)) { + mpd_setspecial(result, MPD_POS, MPD_NAN); /* GCOV_UNLIKELY */ + goto finish; /* GCOV_UNLIKELY */ + } + _mpd_qpow_mpd(result, &tbase, &texp, resultsign, &workctx, status); + } + else { + _mpd_qpow_uint(result, &tbase, n, resultsign, &workctx, status); + } + + if (mpd_isinfinite(result)) { + /* for ROUND_DOWN, ROUND_FLOOR, etc. */ + _settriple(result, resultsign, 1, MPD_EXP_INF); + } + +finish: + mpd_del(&tbase); + mpd_del(&texp); + mpd_qfinalize(result, ctx, status); +} + +/* + * If the exponent is infinite and base equals one, the result is one + * with a coefficient of length prec. Otherwise, result is undefined. + * Return the value of the comparison against one. + */ +static int +_qcheck_pow_one_inf(mpd_t *result, const mpd_t *base, uint8_t resultsign, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_ssize_t shift; + int cmp; + + if ((cmp = _mpd_cmp(base, &one)) == 0) { + shift = ctx->prec-1; + mpd_qshiftl(result, &one, shift, status); + result->exp = -shift; + mpd_set_flags(result, resultsign); + *status |= (MPD_Inexact|MPD_Rounded); + } + + return cmp; +} + +/* + * If abs(base) equals one, calculate the correct power of one result. + * Otherwise, result is undefined. Return the value of the comparison + * against 1. + * + * This is an internal function that does not check for specials. + */ +static int +_qcheck_pow_one(mpd_t *result, const mpd_t *base, const mpd_t *exp, + uint8_t resultsign, + const mpd_context_t *ctx, uint32_t *status) +{ + uint32_t workstatus = 0; + mpd_ssize_t shift; + int cmp; + + if ((cmp = _mpd_cmp_abs(base, &one)) == 0) { + if (_mpd_isint(exp)) { + if (mpd_isnegative(exp)) { + _settriple(result, resultsign, 1, 0); + return 0; + } + /* 1.000**3 = 1.000000000 */ + mpd_qmul_ssize(result, exp, -base->exp, ctx, &workstatus); + if (workstatus&MPD_Errors) { + *status |= (workstatus&MPD_Errors); + return 0; + } + /* digits-1 after exponentiation */ + shift = mpd_qget_ssize(result, &workstatus); + /* shift is MPD_SSIZE_MAX if result is too large */ + if (shift > ctx->prec-1) { + shift = ctx->prec-1; + *status |= MPD_Rounded; + } + } + else if (mpd_ispositive(base)) { + shift = ctx->prec-1; + *status |= (MPD_Inexact|MPD_Rounded); + } + else { + return -2; /* GCOV_NOT_REACHED */ + } + if (!mpd_qshiftl(result, &one, shift, status)) { + return 0; + } + result->exp = -shift; + mpd_set_flags(result, resultsign); + } + + return cmp; +} + +/* + * Detect certain over/underflow of x**y. + * ACL2 proof: pow-bounds.lisp. + * + * Symbols: + * + * e: EXP_INF or EXP_CLAMP + * x: base + * y: exponent + * + * omega(e) = log10(abs(e)) + * zeta(x) = log10(abs(log10(x))) + * theta(y) = log10(abs(y)) + * + * Upper and lower bounds: + * + * ub_omega(e) = ceil(log10(abs(e))) + * lb_theta(y) = floor(log10(abs(y))) + * + * | floor(log10(floor(abs(log10(x))))) if x < 1/10 or x >= 10 + * lb_zeta(x) = | floor(log10(abs(x-1)/10)) if 1/10 <= x < 1 + * | floor(log10(abs((x-1)/100))) if 1 < x < 10 + * + * ub_omega(e) and lb_theta(y) are obviously upper and lower bounds + * for omega(e) and theta(y). + * + * lb_zeta is a lower bound for zeta(x): + * + * x < 1/10 or x >= 10: + * + * abs(log10(x)) >= 1, so the outer log10 is well defined. Since log10 + * is strictly increasing, the end result is a lower bound. + * + * 1/10 <= x < 1: + * + * We use: log10(x) <= (x-1)/log(10) + * abs(log10(x)) >= abs(x-1)/log(10) + * abs(log10(x)) >= abs(x-1)/10 + * + * 1 < x < 10: + * + * We use: (x-1)/(x*log(10)) < log10(x) + * abs((x-1)/100) < abs(log10(x)) + * + * XXX: abs((x-1)/10) would work, need ACL2 proof. + * + * + * Let (0 < x < 1 and y < 0) or (x > 1 and y > 0). (H1) + * Let ub_omega(exp_inf) < lb_zeta(x) + lb_theta(y) (H2) + * + * Then: + * log10(abs(exp_inf)) < log10(abs(log10(x))) + log10(abs(y)). (1) + * exp_inf < log10(x) * y (2) + * 10**exp_inf < x**y (3) + * + * Let (0 < x < 1 and y > 0) or (x > 1 and y < 0). (H3) + * Let ub_omega(exp_clamp) < lb_zeta(x) + lb_theta(y) (H4) + * + * Then: + * log10(abs(exp_clamp)) < log10(abs(log10(x))) + log10(abs(y)). (4) + * log10(x) * y < exp_clamp (5) + * x**y < 10**exp_clamp (6) + * + */ +static mpd_ssize_t +_lower_bound_zeta(const mpd_t *x, uint32_t *status) +{ + mpd_context_t maxctx; + MPD_NEW_STATIC(scratch,0,0,0,0); + mpd_ssize_t t, u; + + t = mpd_adjexp(x); + if (t > 0) { + /* x >= 10 -> floor(log10(floor(abs(log10(x))))) */ + return mpd_exp_digits(t) - 1; + } + else if (t < -1) { + /* x < 1/10 -> floor(log10(floor(abs(log10(x))))) */ + return mpd_exp_digits(t+1) - 1; + } + else { + mpd_maxcontext(&maxctx); + mpd_qsub(&scratch, x, &one, &maxctx, status); + if (mpd_isspecial(&scratch)) { + mpd_del(&scratch); + return MPD_SSIZE_MAX; + } + u = mpd_adjexp(&scratch); + mpd_del(&scratch); + + /* t == -1, 1/10 <= x < 1 -> floor(log10(abs(x-1)/10)) + * t == 0, 1 < x < 10 -> floor(log10(abs(x-1)/100)) */ + return (t == 0) ? u-2 : u-1; + } +} + +/* + * Detect cases of certain overflow/underflow in the power function. + * Assumptions: x != 1, y != 0. The proof above is for positive x. + * If x is negative and y is an odd integer, x**y == -(abs(x)**y), + * so the analysis does not change. + */ +static int +_qcheck_pow_bounds(mpd_t *result, const mpd_t *x, const mpd_t *y, + uint8_t resultsign, + const mpd_context_t *ctx, uint32_t *status) +{ + MPD_NEW_SHARED(abs_x, x); + mpd_ssize_t ub_omega, lb_zeta, lb_theta; + uint8_t sign; + + mpd_set_positive(&abs_x); + + lb_theta = mpd_adjexp(y); + lb_zeta = _lower_bound_zeta(&abs_x, status); + if (lb_zeta == MPD_SSIZE_MAX) { + mpd_seterror(result, MPD_Malloc_error, status); + return 1; + } + + sign = (mpd_adjexp(&abs_x) < 0) ^ mpd_sign(y); + if (sign == 0) { + /* (0 < |x| < 1 and y < 0) or (|x| > 1 and y > 0) */ + ub_omega = mpd_exp_digits(ctx->emax); + if (ub_omega < lb_zeta + lb_theta) { + _settriple(result, resultsign, 1, MPD_EXP_INF); + mpd_qfinalize(result, ctx, status); + return 1; + } + } + else { + /* (0 < |x| < 1 and y > 0) or (|x| > 1 and y < 0). */ + ub_omega = mpd_exp_digits(mpd_etiny(ctx)); + if (ub_omega < lb_zeta + lb_theta) { + _settriple(result, resultsign, 1, mpd_etiny(ctx)-1); + mpd_qfinalize(result, ctx, status); + return 1; + } + } + + return 0; +} + +/* + * TODO: Implement algorithm for computing exact powers from decimal.py. + * In order to prevent infinite loops, this has to be called before + * using Ziv's strategy for correct rounding. + */ +/* +static int +_mpd_qpow_exact(mpd_t *result, const mpd_t *base, const mpd_t *exp, + const mpd_context_t *ctx, uint32_t *status) +{ + return 0; +} +*/ + +/* + * The power function for real exponents. + * Relative error: abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) + */ +static void +_mpd_qpow_real(mpd_t *result, const mpd_t *base, const mpd_t *exp, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_STATIC(texp,0,0,0,0); + + if (!mpd_qcopy(&texp, exp, status)) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + + mpd_maxcontext(&workctx); + workctx.prec = (base->digits > ctx->prec) ? base->digits : ctx->prec; + workctx.prec += (4 + MPD_EXPDIGITS); + workctx.round = MPD_ROUND_HALF_EVEN; + workctx.allcr = ctx->allcr; + + /* + * extra := MPD_EXPDIGITS = MPD_EXP_MAX_T + * wp := prec + 4 + extra + * abs(err) < 5 * 10**-wp + * y := log(base) * exp + * Calculate: + * 1) e**(y * (1 + err)**2) * (1 + err) + * = e**y * e**(y * (2*err + err**2)) * (1 + err) + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * Relative error of the underlined term: + * 2) abs(e**(y * (2*err + err**2)) - 1) + * Case abs(y) >= 10**extra: + * 3) adjexp(y)+1 > log10(abs(y)) >= extra + * This triggers the Overflow/Underflow shortcut in _mpd_qexp(), + * so no further analysis is necessary. + * Case abs(y) < 10**extra: + * 4) abs(y * (2*err + err**2)) < 1/5 * 10**(-prec - 2) + * Use (see _mpd_qexp): + * 5) abs(x) <= 9/10 * 10**-p ==> abs(e**x - 1) < 10**-p + * With 2), 4) and 5): + * 6) abs(e**(y * (2*err + err**2)) - 1) < 10**(-prec - 2) + * The complete relative error of 1) is: + * 7) abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) + */ + mpd_qln(result, base, &workctx, &workctx.status); + mpd_qmul(result, result, &texp, &workctx, &workctx.status); + mpd_qexp(result, result, &workctx, status); + + mpd_del(&texp); + *status |= (workctx.status&MPD_Errors); + *status |= (MPD_Inexact|MPD_Rounded); +} + +/* The power function: base**exp */ +void +mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, + const mpd_context_t *ctx, uint32_t *status) +{ + uint8_t resultsign = 0; + int intexp = 0; + int cmp; + + if (mpd_isspecial(base) || mpd_isspecial(exp)) { + if (mpd_qcheck_nans(result, base, exp, ctx, status)) { + return; + } + } + if (mpd_isinteger(exp)) { + intexp = 1; + resultsign = mpd_isnegative(base) && mpd_isodd(exp); + } + + if (mpd_iszero(base)) { + if (mpd_iszero(exp)) { + mpd_seterror(result, MPD_Invalid_operation, status); + } + else if (mpd_isnegative(exp)) { + mpd_setspecial(result, resultsign, MPD_INF); + } + else { + _settriple(result, resultsign, 0, 0); + } + return; + } + if (mpd_isnegative(base)) { + if (!intexp || mpd_isinfinite(exp)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + } + if (mpd_isinfinite(exp)) { + /* power of one */ + cmp = _qcheck_pow_one_inf(result, base, resultsign, ctx, status); + if (cmp == 0) { + return; + } + else { + cmp *= mpd_arith_sign(exp); + if (cmp < 0) { + _settriple(result, resultsign, 0, 0); + } + else { + mpd_setspecial(result, resultsign, MPD_INF); + } + } + return; + } + if (mpd_isinfinite(base)) { + if (mpd_iszero(exp)) { + _settriple(result, resultsign, 1, 0); + } + else if (mpd_isnegative(exp)) { + _settriple(result, resultsign, 0, 0); + } + else { + mpd_setspecial(result, resultsign, MPD_INF); + } + return; + } + if (mpd_iszero(exp)) { + _settriple(result, resultsign, 1, 0); + return; + } + if (_qcheck_pow_one(result, base, exp, resultsign, ctx, status) == 0) { + return; + } + if (_qcheck_pow_bounds(result, base, exp, resultsign, ctx, status)) { + return; + } + + if (intexp) { + _mpd_qpow_int(result, base, exp, resultsign, ctx, status); + } + else { + _mpd_qpow_real(result, base, exp, ctx, status); + if (!mpd_isspecial(result) && _mpd_cmp(result, &one) == 0) { + mpd_ssize_t shift = ctx->prec-1; + mpd_qshiftl(result, &one, shift, status); + result->exp = -shift; + } + if (mpd_isinfinite(result)) { + /* for ROUND_DOWN, ROUND_FLOOR, etc. */ + _settriple(result, MPD_POS, 1, MPD_EXP_INF); + } + mpd_qfinalize(result, ctx, status); + } +} + +/* + * Internal function: Integer powmod with mpd_uint_t exponent, base is modified! + * Function can fail with MPD_Malloc_error. + */ +static inline void +_mpd_qpowmod_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp, + const mpd_t *mod, uint32_t *status) +{ + mpd_context_t maxcontext; + + mpd_maxcontext(&maxcontext); + + /* resize to smaller cannot fail */ + mpd_qcopy(result, &one, status); + + while (exp > 0) { + if (exp & 1) { + _mpd_qmul_exact(result, result, base, &maxcontext, status); + mpd_qrem(result, result, mod, &maxcontext, status); + } + _mpd_qmul_exact(base, base, base, &maxcontext, status); + mpd_qrem(base, base, mod, &maxcontext, status); + exp >>= 1; + } +} + +/* The powmod function: (base**exp) % mod */ +void +mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, + const mpd_t *mod, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_context_t maxcontext; + MPD_NEW_STATIC(tbase,0,0,0,0); + MPD_NEW_STATIC(texp,0,0,0,0); + MPD_NEW_STATIC(tmod,0,0,0,0); + MPD_NEW_STATIC(tmp,0,0,0,0); + MPD_NEW_CONST(two,0,0,1,1,1,2); + mpd_ssize_t tbase_exp, texp_exp; + mpd_ssize_t i; + mpd_t t; + mpd_uint_t r; + uint8_t sign; + + + if (mpd_isspecial(base) || mpd_isspecial(exp) || mpd_isspecial(mod)) { + if (mpd_qcheck_3nans(result, base, exp, mod, ctx, status)) { + return; + } + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + + if (!_mpd_isint(base) || !_mpd_isint(exp) || !_mpd_isint(mod)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (mpd_iszerocoeff(mod)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (mod->digits+mod->exp > ctx->prec) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + sign = (mpd_isnegative(base)) && (mpd_isodd(exp)); + if (mpd_iszerocoeff(exp)) { + if (mpd_iszerocoeff(base)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + r = (_mpd_cmp_abs(mod, &one)==0) ? 0 : 1; + _settriple(result, sign, r, 0); + return; + } + if (mpd_isnegative(exp)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (mpd_iszerocoeff(base)) { + _settriple(result, sign, 0, 0); + return; + } + + mpd_maxcontext(&maxcontext); + + mpd_qrescale(&tmod, mod, 0, &maxcontext, &maxcontext.status); + if (maxcontext.status&MPD_Errors) { + mpd_seterror(result, maxcontext.status&MPD_Errors, status); + goto out; + } + maxcontext.status = 0; + mpd_set_positive(&tmod); + + mpd_qround_to_int(&tbase, base, &maxcontext, status); + mpd_set_positive(&tbase); + tbase_exp = tbase.exp; + tbase.exp = 0; + + mpd_qround_to_int(&texp, exp, &maxcontext, status); + texp_exp = texp.exp; + texp.exp = 0; + + /* base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo */ + mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); + mpd_qshiftl(result, &one, tbase_exp, status); + mpd_qrem(result, result, &tmod, &maxcontext, status); + _mpd_qmul_exact(&tbase, &tbase, result, &maxcontext, status); + mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); + if (mpd_isspecial(&tbase) || + mpd_isspecial(&texp) || + mpd_isspecial(&tmod)) { + goto mpd_errors; + } + + for (i = 0; i < texp_exp; i++) { + _mpd_qpowmod_uint(&tmp, &tbase, 10, &tmod, status); + t = tmp; + tmp = tbase; + tbase = t; + } + if (mpd_isspecial(&tbase)) { + goto mpd_errors; /* GCOV_UNLIKELY */ + } + + /* resize to smaller cannot fail */ + mpd_qcopy(result, &one, status); + while (mpd_isfinite(&texp) && !mpd_iszero(&texp)) { + if (mpd_isodd(&texp)) { + _mpd_qmul_exact(result, result, &tbase, &maxcontext, status); + mpd_qrem(result, result, &tmod, &maxcontext, status); + } + _mpd_qmul_exact(&tbase, &tbase, &tbase, &maxcontext, status); + mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); + mpd_qdivint(&texp, &texp, &two, &maxcontext, status); + } + if (mpd_isspecial(&texp) || mpd_isspecial(&tbase) || + mpd_isspecial(&tmod) || mpd_isspecial(result)) { + /* MPD_Malloc_error */ + goto mpd_errors; + } + else { + mpd_set_sign(result, sign); + } + +out: + mpd_del(&tbase); + mpd_del(&texp); + mpd_del(&tmod); + mpd_del(&tmp); + return; + +mpd_errors: + mpd_setspecial(result, MPD_POS, MPD_NAN); + goto out; +} + +void +mpd_qquantize(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 b_exp = b->exp; + mpd_ssize_t expdiff, shift; + mpd_uint_t rnd; + + if (mpd_isspecial(a) || mpd_isspecial(b)) { + if (mpd_qcheck_nans(result, a, b, ctx, status)) { + return; + } + if (mpd_isinfinite(a) && mpd_isinfinite(b)) { + mpd_qcopy(result, a, status); + return; + } + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + if (b->exp > ctx->emax || b->exp < mpd_etiny(ctx)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + if (mpd_iszero(a)) { + _settriple(result, mpd_sign(a), 0, b->exp); + mpd_qfinalize(result, ctx, status); + return; + } + + + expdiff = a->exp - b->exp; + if (a->digits + expdiff > ctx->prec) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + if (expdiff >= 0) { + shift = expdiff; + if (!mpd_qshiftl(result, a, shift, status)) { + return; + } + result->exp = b_exp; + } + else { + /* At this point expdiff < 0 and a->digits+expdiff <= prec, + * so the shift before an increment will fit in prec. */ + shift = -expdiff; + rnd = mpd_qshiftr(result, a, shift, status); + if (rnd == MPD_UINT_MAX) { + return; + } + result->exp = b_exp; + if (!_mpd_apply_round_fit(result, rnd, ctx, status)) { + return; + } + workstatus |= MPD_Rounded; + if (rnd) { + workstatus |= MPD_Inexact; + } + } + + if (mpd_adjexp(result) > ctx->emax || + mpd_adjexp(result) < mpd_etiny(ctx)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + *status |= workstatus; + mpd_qfinalize(result, ctx, status); +} + +void +mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_ssize_t shift, maxexp, maxshift; + uint8_t sign_a = mpd_sign(a); + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + mpd_qcopy(result, a, status); + return; + } + + if (!mpd_qcopy(result, a, status)) { + return; + } + mpd_qfinalize(result, ctx, status); + if (mpd_isspecial(result)) { + return; + } + if (mpd_iszero(result)) { + _settriple(result, sign_a, 0, 0); + return; + } + + shift = mpd_trail_zeros(result); + maxexp = (ctx->clamp) ? mpd_etop(ctx) : ctx->emax; + /* After the finalizing above result->exp <= maxexp. */ + maxshift = maxexp - result->exp; + shift = (shift > maxshift) ? maxshift : shift; + + mpd_qshiftr_inplace(result, shift); + result->exp += shift; +} + +void +mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, + uint32_t *status) +{ + MPD_NEW_STATIC(q,0,0,0,0); + + if (mpd_isspecial(a) || mpd_isspecial(b)) { + if (mpd_qcheck_nans(r, a, b, ctx, status)) { + return; + } + if (mpd_isinfinite(a)) { + mpd_seterror(r, MPD_Invalid_operation, status); + return; + } + if (mpd_isinfinite(b)) { + mpd_qcopy(r, a, status); + mpd_qfinalize(r, ctx, status); + return; + } + /* debug */ + abort(); /* GCOV_NOT_REACHED */ + } + if (mpd_iszerocoeff(b)) { + if (mpd_iszerocoeff(a)) { + mpd_seterror(r, MPD_Division_undefined, status); + } + else { + mpd_seterror(r, MPD_Invalid_operation, status); + } + return; + } + + _mpd_qdivmod(&q, r, a, b, ctx, status); + mpd_del(&q); + mpd_qfinalize(r, ctx, status); +} + +void +mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_context_t workctx; + MPD_NEW_STATIC(btmp,0,0,0,0); + MPD_NEW_STATIC(q,0,0,0,0); + mpd_ssize_t expdiff, qdigits; + int cmp, isodd, allnine; + assert(r != NULL); /* annotation for scan-build */ - if (mpd_isspecial(a) || mpd_isspecial(b)) { - if (mpd_qcheck_nans(r, a, b, ctx, status)) { - return; - } - if (mpd_isinfinite(a)) { - mpd_seterror(r, MPD_Invalid_operation, status); - return; - } - if (mpd_isinfinite(b)) { - mpd_qcopy(r, a, status); - mpd_qfinalize(r, ctx, status); - return; - } - /* debug */ - abort(); /* GCOV_NOT_REACHED */ - } - if (mpd_iszerocoeff(b)) { - if (mpd_iszerocoeff(a)) { - mpd_seterror(r, MPD_Division_undefined, status); - } - else { - mpd_seterror(r, MPD_Invalid_operation, status); - } - return; - } - - if (r == b) { - if (!mpd_qcopy(&btmp, b, status)) { - mpd_seterror(r, MPD_Malloc_error, status); - return; - } - b = &btmp; - } - - _mpd_qdivmod(&q, r, a, b, ctx, status); - if (mpd_isnan(&q) || mpd_isnan(r)) { - goto finish; - } - if (mpd_iszerocoeff(r)) { - goto finish; - } - - expdiff = mpd_adjexp(b) - mpd_adjexp(r); - if (-1 <= expdiff && expdiff <= 1) { - - allnine = mpd_coeff_isallnine(&q); - qdigits = q.digits; - isodd = mpd_isodd(&q); - - mpd_maxcontext(&workctx); - if (mpd_sign(a) == mpd_sign(b)) { - /* sign(r) == sign(b) */ - _mpd_qsub(&q, r, b, &workctx, &workctx.status); - } - else { - /* sign(r) != sign(b) */ - _mpd_qadd(&q, r, b, &workctx, &workctx.status); - } - - if (workctx.status&MPD_Errors) { - mpd_seterror(r, workctx.status&MPD_Errors, status); - goto finish; - } - - cmp = _mpd_cmp_abs(&q, r); - if (cmp < 0 || (cmp == 0 && isodd)) { - /* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */ - if (allnine && qdigits == ctx->prec) { - /* abs(quotient) + 1 == 10**prec */ - mpd_seterror(r, MPD_Division_impossible, status); - goto finish; - } - mpd_qcopy(r, &q, status); - } - } - - -finish: - mpd_del(&btmp); - mpd_del(&q); - mpd_qfinalize(r, ctx, status); -} - -static void -_mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_ssize_t expdiff, shift; - mpd_uint_t rnd; - + if (mpd_isspecial(a) || mpd_isspecial(b)) { + if (mpd_qcheck_nans(r, a, b, ctx, status)) { + return; + } + if (mpd_isinfinite(a)) { + mpd_seterror(r, MPD_Invalid_operation, status); + return; + } + if (mpd_isinfinite(b)) { + mpd_qcopy(r, a, status); + mpd_qfinalize(r, ctx, status); + return; + } + /* debug */ + abort(); /* GCOV_NOT_REACHED */ + } + if (mpd_iszerocoeff(b)) { + if (mpd_iszerocoeff(a)) { + mpd_seterror(r, MPD_Division_undefined, status); + } + else { + mpd_seterror(r, MPD_Invalid_operation, status); + } + return; + } + + if (r == b) { + if (!mpd_qcopy(&btmp, b, status)) { + mpd_seterror(r, MPD_Malloc_error, status); + return; + } + b = &btmp; + } + + _mpd_qdivmod(&q, r, a, b, ctx, status); + if (mpd_isnan(&q) || mpd_isnan(r)) { + goto finish; + } + if (mpd_iszerocoeff(r)) { + goto finish; + } + + expdiff = mpd_adjexp(b) - mpd_adjexp(r); + if (-1 <= expdiff && expdiff <= 1) { + + allnine = mpd_coeff_isallnine(&q); + qdigits = q.digits; + isodd = mpd_isodd(&q); + + mpd_maxcontext(&workctx); + if (mpd_sign(a) == mpd_sign(b)) { + /* sign(r) == sign(b) */ + _mpd_qsub(&q, r, b, &workctx, &workctx.status); + } + else { + /* sign(r) != sign(b) */ + _mpd_qadd(&q, r, b, &workctx, &workctx.status); + } + + if (workctx.status&MPD_Errors) { + mpd_seterror(r, workctx.status&MPD_Errors, status); + goto finish; + } + + cmp = _mpd_cmp_abs(&q, r); + if (cmp < 0 || (cmp == 0 && isodd)) { + /* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */ + if (allnine && qdigits == ctx->prec) { + /* abs(quotient) + 1 == 10**prec */ + mpd_seterror(r, MPD_Division_impossible, status); + goto finish; + } + mpd_qcopy(r, &q, status); + } + } + + +finish: + mpd_del(&btmp); + mpd_del(&q); + mpd_qfinalize(r, ctx, status); +} + +static void +_mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_ssize_t expdiff, shift; + mpd_uint_t rnd; + + if (mpd_isspecial(a)) { + mpd_qcopy(result, a, status); + return; + } + + if (mpd_iszero(a)) { + _settriple(result, mpd_sign(a), 0, exp); + return; + } + + expdiff = a->exp - exp; + if (expdiff >= 0) { + shift = expdiff; + if (a->digits + shift > MPD_MAX_PREC+1) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + if (!mpd_qshiftl(result, a, shift, status)) { + return; + } + result->exp = exp; + } + else { + shift = -expdiff; + rnd = mpd_qshiftr(result, a, shift, status); + if (rnd == MPD_UINT_MAX) { + return; + } + result->exp = exp; + _mpd_apply_round_excess(result, rnd, ctx, status); + *status |= MPD_Rounded; + if (rnd) { + *status |= MPD_Inexact; + } + } + + if (mpd_issubnormal(result, ctx)) { + *status |= MPD_Subnormal; + } +} + +/* + * Rescale a number so that it has exponent 'exp'. Does not regard context + * precision, emax, emin, but uses the rounding mode. Special numbers are + * quietly copied. Restrictions: + * + * MPD_MIN_ETINY <= exp <= MPD_MAX_EMAX+1 + * result->digits <= MPD_MAX_PREC+1 + */ +void +mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, + const mpd_context_t *ctx, uint32_t *status) +{ + if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + _mpd_qrescale(result, a, exp, ctx, status); +} + +/* + * Same as mpd_qrescale, but with relaxed restrictions. The result of this + * function should only be used for formatting a number and never as input + * for other operations. + * + * MPD_MIN_ETINY-MPD_MAX_PREC <= exp <= MPD_MAX_EMAX+1 + * result->digits <= MPD_MAX_PREC+1 + */ +void +mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, + const mpd_context_t *ctx, uint32_t *status) +{ + if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY-MPD_MAX_PREC) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + _mpd_qrescale(result, a, exp, ctx, status); +} + +/* Round to an integer according to 'action' and ctx->round. */ +enum {TO_INT_EXACT, TO_INT_SILENT, TO_INT_TRUNC}; +static void +_mpd_qround_to_integral(int action, mpd_t *result, const mpd_t *a, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_uint_t rnd; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + mpd_qcopy(result, a, status); + return; + } + if (a->exp >= 0) { + mpd_qcopy(result, a, status); + return; + } + if (mpd_iszerocoeff(a)) { + _settriple(result, mpd_sign(a), 0, 0); + return; + } + + rnd = mpd_qshiftr(result, a, -a->exp, status); + if (rnd == MPD_UINT_MAX) { + return; + } + result->exp = 0; + + if (action == TO_INT_EXACT || action == TO_INT_SILENT) { + _mpd_apply_round_excess(result, rnd, ctx, status); + if (action == TO_INT_EXACT) { + *status |= MPD_Rounded; + if (rnd) { + *status |= MPD_Inexact; + } + } + } +} + +void +mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + (void)_mpd_qround_to_integral(TO_INT_EXACT, result, a, ctx, status); +} + +void +mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, ctx, status); +} + +void +mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ if (mpd_isspecial(a)) { - mpd_qcopy(result, a, status); - return; - } - - if (mpd_iszero(a)) { - _settriple(result, mpd_sign(a), 0, exp); - return; - } - - expdiff = a->exp - exp; - if (expdiff >= 0) { - shift = expdiff; - if (a->digits + shift > MPD_MAX_PREC+1) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - if (!mpd_qshiftl(result, a, shift, status)) { - return; - } - result->exp = exp; - } - else { - shift = -expdiff; - rnd = mpd_qshiftr(result, a, shift, status); - if (rnd == MPD_UINT_MAX) { - return; - } - result->exp = exp; - _mpd_apply_round_excess(result, rnd, ctx, status); - *status |= MPD_Rounded; - if (rnd) { - *status |= MPD_Inexact; - } - } - - if (mpd_issubnormal(result, ctx)) { - *status |= MPD_Subnormal; - } -} - -/* - * Rescale a number so that it has exponent 'exp'. Does not regard context - * precision, emax, emin, but uses the rounding mode. Special numbers are - * quietly copied. Restrictions: - * - * MPD_MIN_ETINY <= exp <= MPD_MAX_EMAX+1 - * result->digits <= MPD_MAX_PREC+1 - */ -void -mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, - const mpd_context_t *ctx, uint32_t *status) -{ - if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY) { mpd_seterror(result, MPD_Invalid_operation, status); return; } - _mpd_qrescale(result, a, exp, ctx, status); -} - -/* - * Same as mpd_qrescale, but with relaxed restrictions. The result of this - * function should only be used for formatting a number and never as input - * for other operations. - * - * MPD_MIN_ETINY-MPD_MAX_PREC <= exp <= MPD_MAX_EMAX+1 - * result->digits <= MPD_MAX_PREC+1 - */ -void -mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, - const mpd_context_t *ctx, uint32_t *status) -{ - if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY-MPD_MAX_PREC) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - _mpd_qrescale(result, a, exp, ctx, status); -} - -/* Round to an integer according to 'action' and ctx->round. */ -enum {TO_INT_EXACT, TO_INT_SILENT, TO_INT_TRUNC}; -static void -_mpd_qround_to_integral(int action, mpd_t *result, const mpd_t *a, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_uint_t rnd; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - mpd_qcopy(result, a, status); - return; - } - if (a->exp >= 0) { - mpd_qcopy(result, a, status); - return; - } - if (mpd_iszerocoeff(a)) { - _settriple(result, mpd_sign(a), 0, 0); - return; - } - - rnd = mpd_qshiftr(result, a, -a->exp, status); - if (rnd == MPD_UINT_MAX) { - return; - } - result->exp = 0; - - if (action == TO_INT_EXACT || action == TO_INT_SILENT) { - _mpd_apply_round_excess(result, rnd, ctx, status); - if (action == TO_INT_EXACT) { - *status |= MPD_Rounded; - if (rnd) { - *status |= MPD_Inexact; - } - } - } -} - -void -mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - (void)_mpd_qround_to_integral(TO_INT_EXACT, result, a, ctx, status); -} - -void -mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, ctx, status); -} - -void -mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - if (mpd_isspecial(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - (void)_mpd_qround_to_integral(TO_INT_TRUNC, result, a, ctx, status); -} - -void -mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx = *ctx; + (void)_mpd_qround_to_integral(TO_INT_TRUNC, result, a, ctx, status); +} + +void +mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx = *ctx; if (mpd_isspecial(a)) { mpd_seterror(result, MPD_Invalid_operation, status); return; } - workctx.round = MPD_ROUND_FLOOR; - (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, - &workctx, status); -} - -void -mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx = *ctx; + workctx.round = MPD_ROUND_FLOOR; + (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, + &workctx, status); +} + +void +mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx = *ctx; if (mpd_isspecial(a)) { mpd_seterror(result, MPD_Invalid_operation, status); return; } - workctx.round = MPD_ROUND_CEILING; - (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, - &workctx, status); -} - -int -mpd_same_quantum(const mpd_t *a, const mpd_t *b) -{ - if (mpd_isspecial(a) || mpd_isspecial(b)) { - return ((mpd_isnan(a) && mpd_isnan(b)) || - (mpd_isinfinite(a) && mpd_isinfinite(b))); - } - - return a->exp == b->exp; -} - -/* Schedule the increase in precision for the Newton iteration. */ -static inline int -recpr_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], - mpd_ssize_t maxprec, mpd_ssize_t initprec) -{ - mpd_ssize_t k; - int i; - - assert(maxprec > 0 && initprec > 0); - if (maxprec <= initprec) return -1; - - i = 0; k = maxprec; - do { - k = (k+1) / 2; - klist[i++] = k; - } while (k > initprec); - - return i-1; -} - -/* - * Initial approximation for the reciprocal: - * k_0 := MPD_RDIGITS-2 - * z_0 := 10**(-k_0) * floor(10**(2*k_0 + 2) / floor(v * 10**(k_0 + 2))) - * Absolute error: - * |1/v - z_0| < 10**(-k_0) - * ACL2 proof: maxerror-inverse-approx - */ -static void -_mpd_qreciprocal_approx(mpd_t *z, const mpd_t *v, uint32_t *status) -{ - mpd_uint_t p10data[2] = {0, mpd_pow10[MPD_RDIGITS-2]}; - mpd_uint_t dummy, word; - int n; - - assert(v->exp == -v->digits); - - _mpd_get_msdigits(&dummy, &word, v, MPD_RDIGITS); - n = mpd_word_digits(word); - word *= mpd_pow10[MPD_RDIGITS-n]; - - mpd_qresize(z, 2, status); - (void)_mpd_shortdiv(z->data, p10data, 2, word); - - mpd_clear_flags(z); - z->exp = -(MPD_RDIGITS-2); - z->len = (z->data[1] == 0) ? 1 : 2; - mpd_setdigits(z); -} - -/* - * Reciprocal, calculated with Newton's Method. Assumption: result != a. - * NOTE: The comments in the function show that certain operations are - * exact. The proof for the maximum error is too long to fit in here. - * ACL2 proof: maxerror-inverse-complete - */ -static void -_mpd_qreciprocal(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t varcontext, maxcontext; - mpd_t *z = result; /* current approximation */ - mpd_t *v; /* a, normalized to a number between 0.1 and 1 */ - MPD_NEW_SHARED(vtmp, a); /* v shares data with a */ - MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ - MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ - MPD_NEW_CONST(two,0,0,1,1,1,2); /* const 2 */ - mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; - mpd_ssize_t adj, maxprec, initprec; - uint8_t sign = mpd_sign(a); - int i; - - assert(result != a); - - v = &vtmp; - mpd_clear_flags(v); - adj = v->digits + v->exp; - v->exp = -v->digits; - - /* Initial approximation */ - _mpd_qreciprocal_approx(z, v, status); - - mpd_maxcontext(&varcontext); - mpd_maxcontext(&maxcontext); - varcontext.round = maxcontext.round = MPD_ROUND_TRUNC; - varcontext.emax = maxcontext.emax = MPD_MAX_EMAX + 100; - varcontext.emin = maxcontext.emin = MPD_MIN_EMIN - 100; - maxcontext.prec = MPD_MAX_PREC + 100; - - maxprec = ctx->prec; - maxprec += 2; - initprec = MPD_RDIGITS-3; - - i = recpr_schedule_prec(klist, maxprec, initprec); - for (; i >= 0; i--) { - /* Loop invariant: z->digits <= klist[i]+7 */ - /* Let s := z**2, exact result */ - _mpd_qmul_exact(&s, z, z, &maxcontext, status); - varcontext.prec = 2*klist[i] + 5; - if (v->digits > varcontext.prec) { - /* Let t := v, truncated to n >= 2*k+5 fraction digits */ - mpd_qshiftr(&t, v, v->digits-varcontext.prec, status); - t.exp = -varcontext.prec; - /* Let t := trunc(v)*s, truncated to n >= 2*k+1 fraction digits */ - mpd_qmul(&t, &t, &s, &varcontext, status); - } - else { /* v->digits <= 2*k+5 */ - /* Let t := v*s, truncated to n >= 2*k+1 fraction digits */ - mpd_qmul(&t, v, &s, &varcontext, status); - } - /* Let s := 2*z, exact result */ - _mpd_qmul_exact(&s, z, &two, &maxcontext, status); - /* s.digits < t.digits <= 2*k+5, |adjexp(s)-adjexp(t)| <= 1, - * so the subtraction generates at most 2*k+6 <= klist[i+1]+7 - * digits. The loop invariant is preserved. */ - _mpd_qsub_exact(z, &s, &t, &maxcontext, status); - } - - if (!mpd_isspecial(z)) { - z->exp -= adj; - mpd_set_flags(z, sign); - } - - mpd_del(&s); - mpd_del(&t); - mpd_qfinalize(z, ctx, status); -} - -/* - * Internal function for large numbers: - * - * q, r = divmod(coeff(a), coeff(b)) - * - * Strategy: Multiply the dividend by the reciprocal of the divisor. The - * inexact result is fixed by a small loop, using at most one iteration. - * - * ACL2 proofs: - * ------------ - * 1) q is a natural number. (ndivmod-quotient-natp) - * 2) r is a natural number. (ndivmod-remainder-natp) - * 3) a = q * b + r (ndivmod-q*b+r==a) - * 4) r < b (ndivmod-remainder-<-b) - */ -static void -_mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, - uint32_t *status) -{ - mpd_context_t workctx; - mpd_t *qq = q, *rr = r; - mpd_t aa, bb; - int k; - - _mpd_copy_shared(&aa, a); - _mpd_copy_shared(&bb, b); - - mpd_set_positive(&aa); - mpd_set_positive(&bb); - aa.exp = 0; - bb.exp = 0; - - if (q == a || q == b) { - if ((qq = mpd_qnew()) == NULL) { - *status |= MPD_Malloc_error; - goto nanresult; - } - } - if (r == a || r == b) { - if ((rr = mpd_qnew()) == NULL) { - *status |= MPD_Malloc_error; - goto nanresult; - } - } - - mpd_maxcontext(&workctx); - - /* Let prec := adigits - bdigits + 4 */ - workctx.prec = a->digits - b->digits + 1 + 3; - if (a->digits > MPD_MAX_PREC || workctx.prec > MPD_MAX_PREC) { - *status |= MPD_Division_impossible; - goto nanresult; - } - - /* Let x := _mpd_qreciprocal(b, prec) - * Then x is bounded by: - * 1) 1/b - 10**(-prec - bdigits) < x < 1/b + 10**(-prec - bdigits) - * 2) 1/b - 10**(-adigits - 4) < x < 1/b + 10**(-adigits - 4) - */ - _mpd_qreciprocal(rr, &bb, &workctx, &workctx.status); - - /* Get an estimate for the quotient. Let q := a * x - * Then q is bounded by: - * 3) a/b - 10**-4 < q < a/b + 10**-4 - */ - _mpd_qmul(qq, &aa, rr, &workctx, &workctx.status); - /* Truncate q to an integer: - * 4) a/b - 2 < trunc(q) < a/b + 1 - */ - mpd_qtrunc(qq, qq, &workctx, &workctx.status); - - workctx.prec = aa.digits + 3; - workctx.emax = MPD_MAX_EMAX + 3; - workctx.emin = MPD_MIN_EMIN - 3; - /* Multiply the estimate for q by b: - * 5) a - 2 * b < trunc(q) * b < a + b - */ - _mpd_qmul(rr, &bb, qq, &workctx, &workctx.status); - /* Get the estimate for r such that a = q * b + r. */ - _mpd_qsub_exact(rr, &aa, rr, &workctx, &workctx.status); - - /* Fix the result. At this point -b < r < 2*b, so the correction loop - takes at most one iteration. */ - for (k = 0;; k++) { - if (mpd_isspecial(qq) || mpd_isspecial(rr)) { - *status |= (workctx.status&MPD_Errors); - goto nanresult; - } - if (k > 2) { /* Allow two iterations despite the proof. */ - mpd_err_warn("libmpdec: internal error in " /* GCOV_NOT_REACHED */ - "_mpd_base_ndivmod: please report"); /* GCOV_NOT_REACHED */ - *status |= MPD_Invalid_operation; /* GCOV_NOT_REACHED */ - goto nanresult; /* GCOV_NOT_REACHED */ - } - /* r < 0 */ - else if (_mpd_cmp(&zero, rr) == 1) { - _mpd_qadd_exact(rr, rr, &bb, &workctx, &workctx.status); - _mpd_qadd_exact(qq, qq, &minus_one, &workctx, &workctx.status); - } - /* 0 <= r < b */ - else if (_mpd_cmp(rr, &bb) == -1) { - break; - } - /* r >= b */ - else { - _mpd_qsub_exact(rr, rr, &bb, &workctx, &workctx.status); - _mpd_qadd_exact(qq, qq, &one, &workctx, &workctx.status); - } - } - - if (qq != q) { - if (!mpd_qcopy(q, qq, status)) { - goto nanresult; /* GCOV_UNLIKELY */ - } - mpd_del(qq); - } - if (rr != r) { - if (!mpd_qcopy(r, rr, status)) { - goto nanresult; /* GCOV_UNLIKELY */ - } - mpd_del(rr); - } - - *status |= (workctx.status&MPD_Errors); - return; - - -nanresult: - if (qq && qq != q) mpd_del(qq); - if (rr && rr != r) mpd_del(rr); - mpd_setspecial(q, MPD_POS, MPD_NAN); - mpd_setspecial(r, MPD_POS, MPD_NAN); -} - -/* LIBMPDEC_ONLY */ -/* - * Schedule the optimal precision increase for the Newton iteration. - * v := input operand - * z_0 := initial approximation - * initprec := natural number such that abs(sqrt(v) - z_0) < 10**-initprec - * maxprec := target precision - * - * For convenience the output klist contains the elements in reverse order: - * klist := [k_n-1, ..., k_0], where - * 1) k_0 <= initprec and - * 2) abs(sqrt(v) - result) < 10**(-2*k_n-1 + 2) <= 10**-maxprec. - */ -static inline int -invroot_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], - mpd_ssize_t maxprec, mpd_ssize_t initprec) -{ - mpd_ssize_t k; - int i; - - assert(maxprec >= 3 && initprec >= 3); - if (maxprec <= initprec) return -1; - - i = 0; k = maxprec; - do { - k = (k+3) / 2; - klist[i++] = k; - } while (k > initprec); - - return i-1; -} - -/* - * Initial approximation for the inverse square root function. - * Input: - * v := rational number, with 1 <= v < 100 - * vhat := floor(v * 10**6) - * Output: - * z := approximation to 1/sqrt(v), such that abs(z - 1/sqrt(v)) < 10**-3. - */ -static inline void -_invroot_init_approx(mpd_t *z, mpd_uint_t vhat) -{ - mpd_uint_t lo = 1000; - mpd_uint_t hi = 10000; - mpd_uint_t a, sq; - - assert(lo*lo <= vhat && vhat < (hi+1)*(hi+1)); - - for(;;) { - a = (lo + hi) / 2; - sq = a * a; - if (vhat >= sq) { - if (vhat < sq + 2*a + 1) { - break; - } - lo = a + 1; - } - else { - hi = a - 1; - } - } - - /* - * After the binary search we have: - * 1) a**2 <= floor(v * 10**6) < (a + 1)**2 - * This implies: - * 2) a**2 <= v * 10**6 < (a + 1)**2 - * 3) a <= sqrt(v) * 10**3 < a + 1 - * Since 10**3 <= a: - * 4) 0 <= 10**prec/a - 1/sqrt(v) < 10**-prec - * We have: - * 5) 10**3/a - 10**-3 < floor(10**9/a) * 10**-6 <= 10**3/a - * Merging 4) and 5): - * 6) abs(floor(10**9/a) * 10**-6 - 1/sqrt(v)) < 10**-3 - */ - mpd_minalloc(z); - mpd_clear_flags(z); - z->data[0] = 1000000000UL / a; - z->len = 1; - z->exp = -6; - mpd_setdigits(z); -} - -/* - * Set 'result' to 1/sqrt(a). - * Relative error: abs(result - 1/sqrt(a)) < 10**-prec * 1/sqrt(a) - */ -static void -_mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - uint32_t workstatus = 0; - mpd_context_t varcontext, maxcontext; - mpd_t *z = result; /* current approximation */ - mpd_t *v; /* a, normalized to a number between 1 and 100 */ - MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */ - MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ - MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ - MPD_NEW_CONST(one_half,0,-1,1,1,1,5); - MPD_NEW_CONST(three,0,0,1,1,1,3); - mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; - mpd_ssize_t ideal_exp, shift; - mpd_ssize_t adj, tz; - mpd_ssize_t maxprec, fracdigits; - mpd_uint_t vhat, dummy; - int i, n; - - - ideal_exp = -(a->exp - (a->exp & 1)) / 2; - - v = &vtmp; - if (result == a) { - if ((v = mpd_qncopy(a)) == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - } - - /* normalize a to 1 <= v < 100 */ - if ((v->digits+v->exp) & 1) { - fracdigits = v->digits - 1; - v->exp = -fracdigits; - n = (v->digits > 7) ? 7 : (int)v->digits; - /* Let vhat := floor(v * 10**(2*initprec)) */ - _mpd_get_msdigits(&dummy, &vhat, v, n); - if (n < 7) { - vhat *= mpd_pow10[7-n]; - } - } - else { - fracdigits = v->digits - 2; - v->exp = -fracdigits; - n = (v->digits > 8) ? 8 : (int)v->digits; - /* Let vhat := floor(v * 10**(2*initprec)) */ - _mpd_get_msdigits(&dummy, &vhat, v, n); - if (n < 8) { - vhat *= mpd_pow10[8-n]; - } - } - adj = (a->exp-v->exp) / 2; - - /* initial approximation */ - _invroot_init_approx(z, vhat); - - mpd_maxcontext(&maxcontext); - mpd_maxcontext(&varcontext); - varcontext.round = MPD_ROUND_TRUNC; - maxprec = ctx->prec + 1; - - /* initprec == 3 */ - i = invroot_schedule_prec(klist, maxprec, 3); - for (; i >= 0; i--) { - varcontext.prec = 2*klist[i]+2; - mpd_qmul(&s, z, z, &maxcontext, &workstatus); - if (v->digits > varcontext.prec) { - shift = v->digits - varcontext.prec; - mpd_qshiftr(&t, v, shift, &workstatus); - t.exp += shift; - mpd_qmul(&t, &t, &s, &varcontext, &workstatus); - } - else { - mpd_qmul(&t, v, &s, &varcontext, &workstatus); - } - mpd_qsub(&t, &three, &t, &maxcontext, &workstatus); - mpd_qmul(z, z, &t, &varcontext, &workstatus); - mpd_qmul(z, z, &one_half, &maxcontext, &workstatus); - } - - z->exp -= adj; - - tz = mpd_trail_zeros(result); - shift = ideal_exp - result->exp; - shift = (tz > shift) ? shift : tz; - if (shift > 0) { - mpd_qshiftr_inplace(result, shift); - result->exp += shift; - } - - - mpd_del(&s); - mpd_del(&t); - if (v != &vtmp) mpd_del(v); - *status |= (workstatus&MPD_Errors); - *status |= (MPD_Rounded|MPD_Inexact); -} - -void -mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, - uint32_t *status) -{ - mpd_context_t workctx; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - /* positive infinity */ - _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); - *status |= MPD_Clamped; - return; - } - if (mpd_iszero(a)) { - mpd_setspecial(result, mpd_sign(a), MPD_INF); - *status |= MPD_Division_by_zero; - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - workctx = *ctx; - workctx.prec += 2; - workctx.round = MPD_ROUND_HALF_EVEN; - _mpd_qinvroot(result, a, &workctx, status); - mpd_qfinalize(result, ctx, status); -} -/* END LIBMPDEC_ONLY */ - -/* Algorithm from decimal.py */ + workctx.round = MPD_ROUND_CEILING; + (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, + &workctx, status); +} + +int +mpd_same_quantum(const mpd_t *a, const mpd_t *b) +{ + if (mpd_isspecial(a) || mpd_isspecial(b)) { + return ((mpd_isnan(a) && mpd_isnan(b)) || + (mpd_isinfinite(a) && mpd_isinfinite(b))); + } + + return a->exp == b->exp; +} + +/* Schedule the increase in precision for the Newton iteration. */ +static inline int +recpr_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], + mpd_ssize_t maxprec, mpd_ssize_t initprec) +{ + mpd_ssize_t k; + int i; + + assert(maxprec > 0 && initprec > 0); + if (maxprec <= initprec) return -1; + + i = 0; k = maxprec; + do { + k = (k+1) / 2; + klist[i++] = k; + } while (k > initprec); + + return i-1; +} + +/* + * Initial approximation for the reciprocal: + * k_0 := MPD_RDIGITS-2 + * z_0 := 10**(-k_0) * floor(10**(2*k_0 + 2) / floor(v * 10**(k_0 + 2))) + * Absolute error: + * |1/v - z_0| < 10**(-k_0) + * ACL2 proof: maxerror-inverse-approx + */ +static void +_mpd_qreciprocal_approx(mpd_t *z, const mpd_t *v, uint32_t *status) +{ + mpd_uint_t p10data[2] = {0, mpd_pow10[MPD_RDIGITS-2]}; + mpd_uint_t dummy, word; + int n; + + assert(v->exp == -v->digits); + + _mpd_get_msdigits(&dummy, &word, v, MPD_RDIGITS); + n = mpd_word_digits(word); + word *= mpd_pow10[MPD_RDIGITS-n]; + + mpd_qresize(z, 2, status); + (void)_mpd_shortdiv(z->data, p10data, 2, word); + + mpd_clear_flags(z); + z->exp = -(MPD_RDIGITS-2); + z->len = (z->data[1] == 0) ? 1 : 2; + mpd_setdigits(z); +} + +/* + * Reciprocal, calculated with Newton's Method. Assumption: result != a. + * NOTE: The comments in the function show that certain operations are + * exact. The proof for the maximum error is too long to fit in here. + * ACL2 proof: maxerror-inverse-complete + */ +static void +_mpd_qreciprocal(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t varcontext, maxcontext; + mpd_t *z = result; /* current approximation */ + mpd_t *v; /* a, normalized to a number between 0.1 and 1 */ + MPD_NEW_SHARED(vtmp, a); /* v shares data with a */ + MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ + MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ + MPD_NEW_CONST(two,0,0,1,1,1,2); /* const 2 */ + mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; + mpd_ssize_t adj, maxprec, initprec; + uint8_t sign = mpd_sign(a); + int i; + + assert(result != a); + + v = &vtmp; + mpd_clear_flags(v); + adj = v->digits + v->exp; + v->exp = -v->digits; + + /* Initial approximation */ + _mpd_qreciprocal_approx(z, v, status); + + mpd_maxcontext(&varcontext); + mpd_maxcontext(&maxcontext); + varcontext.round = maxcontext.round = MPD_ROUND_TRUNC; + varcontext.emax = maxcontext.emax = MPD_MAX_EMAX + 100; + varcontext.emin = maxcontext.emin = MPD_MIN_EMIN - 100; + maxcontext.prec = MPD_MAX_PREC + 100; + + maxprec = ctx->prec; + maxprec += 2; + initprec = MPD_RDIGITS-3; + + i = recpr_schedule_prec(klist, maxprec, initprec); + for (; i >= 0; i--) { + /* Loop invariant: z->digits <= klist[i]+7 */ + /* Let s := z**2, exact result */ + _mpd_qmul_exact(&s, z, z, &maxcontext, status); + varcontext.prec = 2*klist[i] + 5; + if (v->digits > varcontext.prec) { + /* Let t := v, truncated to n >= 2*k+5 fraction digits */ + mpd_qshiftr(&t, v, v->digits-varcontext.prec, status); + t.exp = -varcontext.prec; + /* Let t := trunc(v)*s, truncated to n >= 2*k+1 fraction digits */ + mpd_qmul(&t, &t, &s, &varcontext, status); + } + else { /* v->digits <= 2*k+5 */ + /* Let t := v*s, truncated to n >= 2*k+1 fraction digits */ + mpd_qmul(&t, v, &s, &varcontext, status); + } + /* Let s := 2*z, exact result */ + _mpd_qmul_exact(&s, z, &two, &maxcontext, status); + /* s.digits < t.digits <= 2*k+5, |adjexp(s)-adjexp(t)| <= 1, + * so the subtraction generates at most 2*k+6 <= klist[i+1]+7 + * digits. The loop invariant is preserved. */ + _mpd_qsub_exact(z, &s, &t, &maxcontext, status); + } + + if (!mpd_isspecial(z)) { + z->exp -= adj; + mpd_set_flags(z, sign); + } + + mpd_del(&s); + mpd_del(&t); + mpd_qfinalize(z, ctx, status); +} + +/* + * Internal function for large numbers: + * + * q, r = divmod(coeff(a), coeff(b)) + * + * Strategy: Multiply the dividend by the reciprocal of the divisor. The + * inexact result is fixed by a small loop, using at most one iteration. + * + * ACL2 proofs: + * ------------ + * 1) q is a natural number. (ndivmod-quotient-natp) + * 2) r is a natural number. (ndivmod-remainder-natp) + * 3) a = q * b + r (ndivmod-q*b+r==a) + * 4) r < b (ndivmod-remainder-<-b) + */ +static void +_mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, + uint32_t *status) +{ + mpd_context_t workctx; + mpd_t *qq = q, *rr = r; + mpd_t aa, bb; + int k; + + _mpd_copy_shared(&aa, a); + _mpd_copy_shared(&bb, b); + + mpd_set_positive(&aa); + mpd_set_positive(&bb); + aa.exp = 0; + bb.exp = 0; + + if (q == a || q == b) { + if ((qq = mpd_qnew()) == NULL) { + *status |= MPD_Malloc_error; + goto nanresult; + } + } + if (r == a || r == b) { + if ((rr = mpd_qnew()) == NULL) { + *status |= MPD_Malloc_error; + goto nanresult; + } + } + + mpd_maxcontext(&workctx); + + /* Let prec := adigits - bdigits + 4 */ + workctx.prec = a->digits - b->digits + 1 + 3; + if (a->digits > MPD_MAX_PREC || workctx.prec > MPD_MAX_PREC) { + *status |= MPD_Division_impossible; + goto nanresult; + } + + /* Let x := _mpd_qreciprocal(b, prec) + * Then x is bounded by: + * 1) 1/b - 10**(-prec - bdigits) < x < 1/b + 10**(-prec - bdigits) + * 2) 1/b - 10**(-adigits - 4) < x < 1/b + 10**(-adigits - 4) + */ + _mpd_qreciprocal(rr, &bb, &workctx, &workctx.status); + + /* Get an estimate for the quotient. Let q := a * x + * Then q is bounded by: + * 3) a/b - 10**-4 < q < a/b + 10**-4 + */ + _mpd_qmul(qq, &aa, rr, &workctx, &workctx.status); + /* Truncate q to an integer: + * 4) a/b - 2 < trunc(q) < a/b + 1 + */ + mpd_qtrunc(qq, qq, &workctx, &workctx.status); + + workctx.prec = aa.digits + 3; + workctx.emax = MPD_MAX_EMAX + 3; + workctx.emin = MPD_MIN_EMIN - 3; + /* Multiply the estimate for q by b: + * 5) a - 2 * b < trunc(q) * b < a + b + */ + _mpd_qmul(rr, &bb, qq, &workctx, &workctx.status); + /* Get the estimate for r such that a = q * b + r. */ + _mpd_qsub_exact(rr, &aa, rr, &workctx, &workctx.status); + + /* Fix the result. At this point -b < r < 2*b, so the correction loop + takes at most one iteration. */ + for (k = 0;; k++) { + if (mpd_isspecial(qq) || mpd_isspecial(rr)) { + *status |= (workctx.status&MPD_Errors); + goto nanresult; + } + if (k > 2) { /* Allow two iterations despite the proof. */ + mpd_err_warn("libmpdec: internal error in " /* GCOV_NOT_REACHED */ + "_mpd_base_ndivmod: please report"); /* GCOV_NOT_REACHED */ + *status |= MPD_Invalid_operation; /* GCOV_NOT_REACHED */ + goto nanresult; /* GCOV_NOT_REACHED */ + } + /* r < 0 */ + else if (_mpd_cmp(&zero, rr) == 1) { + _mpd_qadd_exact(rr, rr, &bb, &workctx, &workctx.status); + _mpd_qadd_exact(qq, qq, &minus_one, &workctx, &workctx.status); + } + /* 0 <= r < b */ + else if (_mpd_cmp(rr, &bb) == -1) { + break; + } + /* r >= b */ + else { + _mpd_qsub_exact(rr, rr, &bb, &workctx, &workctx.status); + _mpd_qadd_exact(qq, qq, &one, &workctx, &workctx.status); + } + } + + if (qq != q) { + if (!mpd_qcopy(q, qq, status)) { + goto nanresult; /* GCOV_UNLIKELY */ + } + mpd_del(qq); + } + if (rr != r) { + if (!mpd_qcopy(r, rr, status)) { + goto nanresult; /* GCOV_UNLIKELY */ + } + mpd_del(rr); + } + + *status |= (workctx.status&MPD_Errors); + return; + + +nanresult: + if (qq && qq != q) mpd_del(qq); + if (rr && rr != r) mpd_del(rr); + mpd_setspecial(q, MPD_POS, MPD_NAN); + mpd_setspecial(r, MPD_POS, MPD_NAN); +} + +/* LIBMPDEC_ONLY */ +/* + * Schedule the optimal precision increase for the Newton iteration. + * v := input operand + * z_0 := initial approximation + * initprec := natural number such that abs(sqrt(v) - z_0) < 10**-initprec + * maxprec := target precision + * + * For convenience the output klist contains the elements in reverse order: + * klist := [k_n-1, ..., k_0], where + * 1) k_0 <= initprec and + * 2) abs(sqrt(v) - result) < 10**(-2*k_n-1 + 2) <= 10**-maxprec. + */ +static inline int +invroot_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], + mpd_ssize_t maxprec, mpd_ssize_t initprec) +{ + mpd_ssize_t k; + int i; + + assert(maxprec >= 3 && initprec >= 3); + if (maxprec <= initprec) return -1; + + i = 0; k = maxprec; + do { + k = (k+3) / 2; + klist[i++] = k; + } while (k > initprec); + + return i-1; +} + +/* + * Initial approximation for the inverse square root function. + * Input: + * v := rational number, with 1 <= v < 100 + * vhat := floor(v * 10**6) + * Output: + * z := approximation to 1/sqrt(v), such that abs(z - 1/sqrt(v)) < 10**-3. + */ +static inline void +_invroot_init_approx(mpd_t *z, mpd_uint_t vhat) +{ + mpd_uint_t lo = 1000; + mpd_uint_t hi = 10000; + mpd_uint_t a, sq; + + assert(lo*lo <= vhat && vhat < (hi+1)*(hi+1)); + + for(;;) { + a = (lo + hi) / 2; + sq = a * a; + if (vhat >= sq) { + if (vhat < sq + 2*a + 1) { + break; + } + lo = a + 1; + } + else { + hi = a - 1; + } + } + + /* + * After the binary search we have: + * 1) a**2 <= floor(v * 10**6) < (a + 1)**2 + * This implies: + * 2) a**2 <= v * 10**6 < (a + 1)**2 + * 3) a <= sqrt(v) * 10**3 < a + 1 + * Since 10**3 <= a: + * 4) 0 <= 10**prec/a - 1/sqrt(v) < 10**-prec + * We have: + * 5) 10**3/a - 10**-3 < floor(10**9/a) * 10**-6 <= 10**3/a + * Merging 4) and 5): + * 6) abs(floor(10**9/a) * 10**-6 - 1/sqrt(v)) < 10**-3 + */ + mpd_minalloc(z); + mpd_clear_flags(z); + z->data[0] = 1000000000UL / a; + z->len = 1; + z->exp = -6; + mpd_setdigits(z); +} + +/* + * Set 'result' to 1/sqrt(a). + * Relative error: abs(result - 1/sqrt(a)) < 10**-prec * 1/sqrt(a) + */ +static void +_mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + uint32_t workstatus = 0; + mpd_context_t varcontext, maxcontext; + mpd_t *z = result; /* current approximation */ + mpd_t *v; /* a, normalized to a number between 1 and 100 */ + MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */ + MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ + MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ + MPD_NEW_CONST(one_half,0,-1,1,1,1,5); + MPD_NEW_CONST(three,0,0,1,1,1,3); + mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; + mpd_ssize_t ideal_exp, shift; + mpd_ssize_t adj, tz; + mpd_ssize_t maxprec, fracdigits; + mpd_uint_t vhat, dummy; + int i, n; + + + ideal_exp = -(a->exp - (a->exp & 1)) / 2; + + v = &vtmp; + if (result == a) { + if ((v = mpd_qncopy(a)) == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + } + + /* normalize a to 1 <= v < 100 */ + if ((v->digits+v->exp) & 1) { + fracdigits = v->digits - 1; + v->exp = -fracdigits; + n = (v->digits > 7) ? 7 : (int)v->digits; + /* Let vhat := floor(v * 10**(2*initprec)) */ + _mpd_get_msdigits(&dummy, &vhat, v, n); + if (n < 7) { + vhat *= mpd_pow10[7-n]; + } + } + else { + fracdigits = v->digits - 2; + v->exp = -fracdigits; + n = (v->digits > 8) ? 8 : (int)v->digits; + /* Let vhat := floor(v * 10**(2*initprec)) */ + _mpd_get_msdigits(&dummy, &vhat, v, n); + if (n < 8) { + vhat *= mpd_pow10[8-n]; + } + } + adj = (a->exp-v->exp) / 2; + + /* initial approximation */ + _invroot_init_approx(z, vhat); + + mpd_maxcontext(&maxcontext); + mpd_maxcontext(&varcontext); + varcontext.round = MPD_ROUND_TRUNC; + maxprec = ctx->prec + 1; + + /* initprec == 3 */ + i = invroot_schedule_prec(klist, maxprec, 3); + for (; i >= 0; i--) { + varcontext.prec = 2*klist[i]+2; + mpd_qmul(&s, z, z, &maxcontext, &workstatus); + if (v->digits > varcontext.prec) { + shift = v->digits - varcontext.prec; + mpd_qshiftr(&t, v, shift, &workstatus); + t.exp += shift; + mpd_qmul(&t, &t, &s, &varcontext, &workstatus); + } + else { + mpd_qmul(&t, v, &s, &varcontext, &workstatus); + } + mpd_qsub(&t, &three, &t, &maxcontext, &workstatus); + mpd_qmul(z, z, &t, &varcontext, &workstatus); + mpd_qmul(z, z, &one_half, &maxcontext, &workstatus); + } + + z->exp -= adj; + + tz = mpd_trail_zeros(result); + shift = ideal_exp - result->exp; + shift = (tz > shift) ? shift : tz; + if (shift > 0) { + mpd_qshiftr_inplace(result, shift); + result->exp += shift; + } + + + mpd_del(&s); + mpd_del(&t); + if (v != &vtmp) mpd_del(v); + *status |= (workstatus&MPD_Errors); + *status |= (MPD_Rounded|MPD_Inexact); +} + +void +mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, + uint32_t *status) +{ + mpd_context_t workctx; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + /* positive infinity */ + _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); + *status |= MPD_Clamped; + return; + } + if (mpd_iszero(a)) { + mpd_setspecial(result, mpd_sign(a), MPD_INF); + *status |= MPD_Division_by_zero; + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + workctx = *ctx; + workctx.prec += 2; + workctx.round = MPD_ROUND_HALF_EVEN; + _mpd_qinvroot(result, a, &workctx, status); + mpd_qfinalize(result, ctx, status); +} +/* END LIBMPDEC_ONLY */ + +/* Algorithm from decimal.py */ static void _mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status) -{ - mpd_context_t maxcontext; - MPD_NEW_STATIC(c,0,0,0,0); - MPD_NEW_STATIC(q,0,0,0,0); - MPD_NEW_STATIC(r,0,0,0,0); - MPD_NEW_CONST(two,0,0,1,1,1,2); - mpd_ssize_t prec, ideal_exp; - mpd_ssize_t l, shift; - int exact = 0; - - - ideal_exp = (a->exp - (a->exp & 1)) / 2; - - if (mpd_isspecial(a)) { - if (mpd_qcheck_nan(result, a, ctx, status)) { - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - mpd_setspecial(result, MPD_POS, MPD_INF); - return; - } - if (mpd_iszero(a)) { - _settriple(result, mpd_sign(a), 0, ideal_exp); - mpd_qfinalize(result, ctx, status); - return; - } - if (mpd_isnegative(a)) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - mpd_maxcontext(&maxcontext); - prec = ctx->prec + 1; - - if (!mpd_qcopy(&c, a, status)) { - goto malloc_error; - } - c.exp = 0; - - if (a->exp & 1) { - if (!mpd_qshiftl(&c, &c, 1, status)) { - goto malloc_error; - } - l = (a->digits >> 1) + 1; - } - else { - l = (a->digits + 1) >> 1; - } - - shift = prec - l; - if (shift >= 0) { - if (!mpd_qshiftl(&c, &c, 2*shift, status)) { - goto malloc_error; - } - exact = 1; - } - else { - exact = !mpd_qshiftr_inplace(&c, -2*shift); - } - - ideal_exp -= shift; - - /* find result = floor(sqrt(c)) using Newton's method */ - if (!mpd_qshiftl(result, &one, prec, status)) { - goto malloc_error; - } - - while (1) { - _mpd_qdivmod(&q, &r, &c, result, &maxcontext, &maxcontext.status); - if (mpd_isspecial(result) || mpd_isspecial(&q)) { - mpd_seterror(result, maxcontext.status&MPD_Errors, status); - goto out; - } - if (_mpd_cmp(result, &q) <= 0) { - break; - } - _mpd_qadd_exact(result, result, &q, &maxcontext, &maxcontext.status); - if (mpd_isspecial(result)) { - mpd_seterror(result, maxcontext.status&MPD_Errors, status); - goto out; - } - _mpd_qdivmod(result, &r, result, &two, &maxcontext, &maxcontext.status); - } - - if (exact) { - _mpd_qmul_exact(&r, result, result, &maxcontext, &maxcontext.status); - if (mpd_isspecial(&r)) { - mpd_seterror(result, maxcontext.status&MPD_Errors, status); - goto out; - } - exact = (_mpd_cmp(&r, &c) == 0); - } - - if (exact) { - if (shift >= 0) { - mpd_qshiftr_inplace(result, shift); - } - else { - if (!mpd_qshiftl(result, result, -shift, status)) { - goto malloc_error; - } - } - ideal_exp += shift; - } - else { - int lsd = (int)mpd_lsd(result->data[0]); - if (lsd == 0 || lsd == 5) { - result->data[0] += 1; - } - } - - result->exp = ideal_exp; - - -out: - mpd_del(&c); - mpd_del(&q); - mpd_del(&r); - maxcontext = *ctx; - maxcontext.round = MPD_ROUND_HALF_EVEN; - mpd_qfinalize(result, &maxcontext, status); - return; - -malloc_error: - mpd_seterror(result, MPD_Malloc_error, status); - goto out; -} - +{ + mpd_context_t maxcontext; + MPD_NEW_STATIC(c,0,0,0,0); + MPD_NEW_STATIC(q,0,0,0,0); + MPD_NEW_STATIC(r,0,0,0,0); + MPD_NEW_CONST(two,0,0,1,1,1,2); + mpd_ssize_t prec, ideal_exp; + mpd_ssize_t l, shift; + int exact = 0; + + + ideal_exp = (a->exp - (a->exp & 1)) / 2; + + if (mpd_isspecial(a)) { + if (mpd_qcheck_nan(result, a, ctx, status)) { + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + mpd_setspecial(result, MPD_POS, MPD_INF); + return; + } + if (mpd_iszero(a)) { + _settriple(result, mpd_sign(a), 0, ideal_exp); + mpd_qfinalize(result, ctx, status); + return; + } + if (mpd_isnegative(a)) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + mpd_maxcontext(&maxcontext); + prec = ctx->prec + 1; + + if (!mpd_qcopy(&c, a, status)) { + goto malloc_error; + } + c.exp = 0; + + if (a->exp & 1) { + if (!mpd_qshiftl(&c, &c, 1, status)) { + goto malloc_error; + } + l = (a->digits >> 1) + 1; + } + else { + l = (a->digits + 1) >> 1; + } + + shift = prec - l; + if (shift >= 0) { + if (!mpd_qshiftl(&c, &c, 2*shift, status)) { + goto malloc_error; + } + exact = 1; + } + else { + exact = !mpd_qshiftr_inplace(&c, -2*shift); + } + + ideal_exp -= shift; + + /* find result = floor(sqrt(c)) using Newton's method */ + if (!mpd_qshiftl(result, &one, prec, status)) { + goto malloc_error; + } + + while (1) { + _mpd_qdivmod(&q, &r, &c, result, &maxcontext, &maxcontext.status); + if (mpd_isspecial(result) || mpd_isspecial(&q)) { + mpd_seterror(result, maxcontext.status&MPD_Errors, status); + goto out; + } + if (_mpd_cmp(result, &q) <= 0) { + break; + } + _mpd_qadd_exact(result, result, &q, &maxcontext, &maxcontext.status); + if (mpd_isspecial(result)) { + mpd_seterror(result, maxcontext.status&MPD_Errors, status); + goto out; + } + _mpd_qdivmod(result, &r, result, &two, &maxcontext, &maxcontext.status); + } + + if (exact) { + _mpd_qmul_exact(&r, result, result, &maxcontext, &maxcontext.status); + if (mpd_isspecial(&r)) { + mpd_seterror(result, maxcontext.status&MPD_Errors, status); + goto out; + } + exact = (_mpd_cmp(&r, &c) == 0); + } + + if (exact) { + if (shift >= 0) { + mpd_qshiftr_inplace(result, shift); + } + else { + if (!mpd_qshiftl(result, result, -shift, status)) { + goto malloc_error; + } + } + ideal_exp += shift; + } + else { + int lsd = (int)mpd_lsd(result->data[0]); + if (lsd == 0 || lsd == 5) { + result->data[0] += 1; + } + } + + result->exp = ideal_exp; + + +out: + mpd_del(&c); + mpd_del(&q); + mpd_del(&r); + maxcontext = *ctx; + maxcontext.round = MPD_ROUND_HALF_EVEN; + mpd_qfinalize(result, &maxcontext, status); + return; + +malloc_error: + mpd_seterror(result, MPD_Malloc_error, status); + goto out; +} + void mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status) { MPD_NEW_STATIC(aa,0,0,0,0); uint32_t xstatus = 0; - + if (result == a) { if (!mpd_qcopy(&aa, a, status)) { mpd_seterror(result, MPD_Malloc_error, status); @@ -8070,589 +8070,589 @@ out: } -/******************************************************************************/ -/* Base conversions */ -/******************************************************************************/ - -/* Space needed to represent an integer mpd_t in base 'base'. */ -size_t -mpd_sizeinbase(const mpd_t *a, uint32_t base) -{ - double x; - size_t digits; +/******************************************************************************/ +/* Base conversions */ +/******************************************************************************/ + +/* Space needed to represent an integer mpd_t in base 'base'. */ +size_t +mpd_sizeinbase(const mpd_t *a, uint32_t base) +{ + double x; + size_t digits; double upper_bound; - - assert(mpd_isinteger(a)); - assert(base >= 2); - - if (mpd_iszero(a)) { - return 1; - } - - digits = a->digits+a->exp; - assert(digits > 0); - -#ifdef CONFIG_64 - /* ceil(2711437152599294 / log10(2)) + 4 == 2**53 */ - if (digits > 2711437152599294ULL) { - return SIZE_MAX; - } + + assert(mpd_isinteger(a)); + assert(base >= 2); + + if (mpd_iszero(a)) { + return 1; + } + + digits = a->digits+a->exp; + assert(digits > 0); + +#ifdef CONFIG_64 + /* ceil(2711437152599294 / log10(2)) + 4 == 2**53 */ + if (digits > 2711437152599294ULL) { + return SIZE_MAX; + } upper_bound = (double)((1ULL<<53)-1); #else upper_bound = (double)(SIZE_MAX-1); -#endif - - x = (double)digits / log10(base); +#endif + + x = (double)digits / log10(base); return (x > upper_bound) ? SIZE_MAX : (size_t)x + 1; -} - -/* Space needed to import a base 'base' integer of length 'srclen'. */ -static mpd_ssize_t -_mpd_importsize(size_t srclen, uint32_t base) -{ - double x; +} + +/* Space needed to import a base 'base' integer of length 'srclen'. */ +static mpd_ssize_t +_mpd_importsize(size_t srclen, uint32_t base) +{ + double x; double upper_bound; - - assert(srclen > 0); - assert(base >= 2); - -#if SIZE_MAX == UINT64_MAX - if (srclen > (1ULL<<53)) { - return MPD_SSIZE_MAX; - } + + assert(srclen > 0); + assert(base >= 2); + +#if SIZE_MAX == UINT64_MAX + if (srclen > (1ULL<<53)) { + return MPD_SSIZE_MAX; + } assert((1ULL<<53) <= MPD_MAXIMPORT); upper_bound = (double)((1ULL<<53)-1); #else upper_bound = MPD_MAXIMPORT-1; -#endif - - x = (double)srclen * (log10(base)/MPD_RDIGITS); +#endif + + x = (double)srclen * (log10(base)/MPD_RDIGITS); return (x > upper_bound) ? MPD_SSIZE_MAX : (mpd_ssize_t)x + 1; -} - -static uint8_t -mpd_resize_u16(uint16_t **w, size_t nmemb) -{ - uint8_t err = 0; - *w = mpd_realloc(*w, nmemb, sizeof **w, &err); - return !err; -} - -static uint8_t -mpd_resize_u32(uint32_t **w, size_t nmemb) -{ - uint8_t err = 0; - *w = mpd_realloc(*w, nmemb, sizeof **w, &err); - return !err; -} - -static size_t -_baseconv_to_u16(uint16_t **w, size_t wlen, mpd_uint_t wbase, - mpd_uint_t *u, mpd_ssize_t ulen) -{ - size_t n = 0; - - assert(wlen > 0 && ulen > 0); - assert(wbase <= (1U<<16)); - - do { - if (n >= wlen) { - if (!mpd_resize_u16(w, n+1)) { - return SIZE_MAX; - } - wlen = n+1; - } - (*w)[n++] = (uint16_t)_mpd_shortdiv(u, u, ulen, wbase); - /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ - ulen = _mpd_real_size(u, ulen); - - } while (u[ulen-1] != 0); - - return n; -} - -static size_t -_coeff_from_u16(mpd_t *w, mpd_ssize_t wlen, - const mpd_uint_t *u, size_t ulen, uint32_t ubase, - uint32_t *status) -{ - mpd_ssize_t n = 0; - mpd_uint_t carry; - - assert(wlen > 0 && ulen > 0); - assert(ubase <= (1U<<16)); - - w->data[n++] = u[--ulen]; - while (--ulen != SIZE_MAX) { - carry = _mpd_shortmul_c(w->data, w->data, n, ubase); - if (carry) { - if (n >= wlen) { - if (!mpd_qresize(w, n+1, status)) { - return SIZE_MAX; - } - wlen = n+1; - } - w->data[n++] = carry; - } - carry = _mpd_shortadd(w->data, n, u[ulen]); - if (carry) { - if (n >= wlen) { - if (!mpd_qresize(w, n+1, status)) { - return SIZE_MAX; - } - wlen = n+1; - } - w->data[n++] = carry; - } - } - - return n; -} - -/* target base wbase < source base ubase */ -static size_t -_baseconv_to_smaller(uint32_t **w, size_t wlen, uint32_t wbase, - mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase) -{ - size_t n = 0; - - assert(wlen > 0 && ulen > 0); - assert(wbase < ubase); - - do { - if (n >= wlen) { - if (!mpd_resize_u32(w, n+1)) { - return SIZE_MAX; - } - wlen = n+1; - } - (*w)[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); - /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ - ulen = _mpd_real_size(u, ulen); - - } while (u[ulen-1] != 0); - - return n; -} - -#ifdef CONFIG_32 -/* target base 'wbase' == source base 'ubase' */ -static size_t -_copy_equal_base(uint32_t **w, size_t wlen, - const uint32_t *u, size_t ulen) -{ - if (wlen < ulen) { - if (!mpd_resize_u32(w, ulen)) { - return SIZE_MAX; - } - } - - memcpy(*w, u, ulen * (sizeof **w)); - return ulen; -} - -/* target base 'wbase' > source base 'ubase' */ -static size_t -_baseconv_to_larger(uint32_t **w, size_t wlen, mpd_uint_t wbase, - const mpd_uint_t *u, size_t ulen, mpd_uint_t ubase) -{ - size_t n = 0; - mpd_uint_t carry; - - assert(wlen > 0 && ulen > 0); - assert(ubase < wbase); - - (*w)[n++] = u[--ulen]; - while (--ulen != SIZE_MAX) { - carry = _mpd_shortmul_b(*w, *w, n, ubase, wbase); - if (carry) { - if (n >= wlen) { - if (!mpd_resize_u32(w, n+1)) { - return SIZE_MAX; - } - wlen = n+1; - } - (*w)[n++] = carry; - } - carry = _mpd_shortadd_b(*w, n, u[ulen], wbase); - if (carry) { - if (n >= wlen) { - if (!mpd_resize_u32(w, n+1)) { - return SIZE_MAX; - } - wlen = n+1; - } - (*w)[n++] = carry; - } - } - - return n; -} - -/* target base wbase < source base ubase */ -static size_t -_coeff_from_larger_base(mpd_t *w, size_t wlen, mpd_uint_t wbase, - mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase, - uint32_t *status) -{ - size_t n = 0; - - assert(wlen > 0 && ulen > 0); - assert(wbase < ubase); - - do { - if (n >= wlen) { - if (!mpd_qresize(w, n+1, status)) { - return SIZE_MAX; - } - wlen = n+1; - } - w->data[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); - /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ - ulen = _mpd_real_size(u, ulen); - - } while (u[ulen-1] != 0); - - return n; -} -#endif - -/* target base 'wbase' > source base 'ubase' */ -static size_t -_coeff_from_smaller_base(mpd_t *w, mpd_ssize_t wlen, mpd_uint_t wbase, - const uint32_t *u, size_t ulen, mpd_uint_t ubase, - uint32_t *status) -{ - mpd_ssize_t n = 0; - mpd_uint_t carry; - - assert(wlen > 0 && ulen > 0); - assert(wbase > ubase); - - w->data[n++] = u[--ulen]; - while (--ulen != SIZE_MAX) { - carry = _mpd_shortmul_b(w->data, w->data, n, ubase, wbase); - if (carry) { - if (n >= wlen) { - if (!mpd_qresize(w, n+1, status)) { - return SIZE_MAX; - } - wlen = n+1; - } - w->data[n++] = carry; - } - carry = _mpd_shortadd_b(w->data, n, u[ulen], wbase); - if (carry) { - if (n >= wlen) { - if (!mpd_qresize(w, n+1, status)) { - return SIZE_MAX; - } - wlen = n+1; - } - w->data[n++] = carry; - } - } - - return n; -} - -/* - * Convert an integer mpd_t to a multiprecision integer with base <= 2**16. - * The least significant word of the result is (*rdata)[0]. - * - * If rdata is NULL, space is allocated by the function and rlen is irrelevant. - * In case of an error any allocated storage is freed and rdata is set back to - * NULL. - * - * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation - * functions and rlen MUST be correct. If necessary, the function will resize - * rdata. In case of an error the caller must free rdata. - * - * Return value: In case of success, the exact length of rdata, SIZE_MAX - * otherwise. - */ -size_t -mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t rbase, - const mpd_t *src, uint32_t *status) -{ - MPD_NEW_STATIC(tsrc,0,0,0,0); - int alloc = 0; /* rdata == NULL */ - size_t n; - - assert(rbase <= (1U<<16)); - - if (mpd_isspecial(src) || !_mpd_isint(src)) { - *status |= MPD_Invalid_operation; - return SIZE_MAX; - } - - if (*rdata == NULL) { - rlen = mpd_sizeinbase(src, rbase); - if (rlen == SIZE_MAX) { - *status |= MPD_Invalid_operation; - return SIZE_MAX; - } - *rdata = mpd_alloc(rlen, sizeof **rdata); - if (*rdata == NULL) { - goto malloc_error; - } - alloc = 1; - } - - if (mpd_iszero(src)) { - **rdata = 0; - return 1; - } - - if (src->exp >= 0) { - if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { - goto malloc_error; - } - } - else { - if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { - goto malloc_error; - } - } - - n = _baseconv_to_u16(rdata, rlen, rbase, tsrc.data, tsrc.len); - if (n == SIZE_MAX) { - goto malloc_error; - } - - -out: - mpd_del(&tsrc); - return n; - -malloc_error: - if (alloc) { - mpd_free(*rdata); - *rdata = NULL; - } - n = SIZE_MAX; - *status |= MPD_Malloc_error; - goto out; -} - -/* - * Convert an integer mpd_t to a multiprecision integer with base<=UINT32_MAX. - * The least significant word of the result is (*rdata)[0]. - * - * If rdata is NULL, space is allocated by the function and rlen is irrelevant. - * In case of an error any allocated storage is freed and rdata is set back to - * NULL. - * - * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation - * functions and rlen MUST be correct. If necessary, the function will resize - * rdata. In case of an error the caller must free rdata. - * - * Return value: In case of success, the exact length of rdata, SIZE_MAX - * otherwise. - */ -size_t -mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t rbase, - const mpd_t *src, uint32_t *status) -{ - MPD_NEW_STATIC(tsrc,0,0,0,0); - int alloc = 0; /* rdata == NULL */ - size_t n; - - if (mpd_isspecial(src) || !_mpd_isint(src)) { - *status |= MPD_Invalid_operation; - return SIZE_MAX; - } - - if (*rdata == NULL) { - rlen = mpd_sizeinbase(src, rbase); - if (rlen == SIZE_MAX) { - *status |= MPD_Invalid_operation; - return SIZE_MAX; - } - *rdata = mpd_alloc(rlen, sizeof **rdata); - if (*rdata == NULL) { - goto malloc_error; - } - alloc = 1; - } - - if (mpd_iszero(src)) { - **rdata = 0; - return 1; - } - - if (src->exp >= 0) { - if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { - goto malloc_error; - } - } - else { - if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { - goto malloc_error; - } - } - -#ifdef CONFIG_64 - n = _baseconv_to_smaller(rdata, rlen, rbase, - tsrc.data, tsrc.len, MPD_RADIX); -#else - if (rbase == MPD_RADIX) { - n = _copy_equal_base(rdata, rlen, tsrc.data, tsrc.len); - } - else if (rbase < MPD_RADIX) { - n = _baseconv_to_smaller(rdata, rlen, rbase, - tsrc.data, tsrc.len, MPD_RADIX); - } - else { - n = _baseconv_to_larger(rdata, rlen, rbase, - tsrc.data, tsrc.len, MPD_RADIX); - } -#endif - - if (n == SIZE_MAX) { - goto malloc_error; - } - - -out: - mpd_del(&tsrc); - return n; - -malloc_error: - if (alloc) { - mpd_free(*rdata); - *rdata = NULL; - } - n = SIZE_MAX; - *status |= MPD_Malloc_error; - goto out; -} - - -/* - * Converts a multiprecision integer with base <= UINT16_MAX+1 to an mpd_t. - * The least significant word of the source is srcdata[0]. - */ -void -mpd_qimport_u16(mpd_t *result, - const uint16_t *srcdata, size_t srclen, - uint8_t srcsign, uint32_t srcbase, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_uint_t *usrc; /* uint16_t src copied to an mpd_uint_t array */ - mpd_ssize_t rlen; /* length of the result */ - size_t n; - - assert(srclen > 0); - assert(srcbase <= (1U<<16)); - - rlen = _mpd_importsize(srclen, srcbase); - if (rlen == MPD_SSIZE_MAX) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); - if (usrc == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - for (n = 0; n < srclen; n++) { - usrc[n] = srcdata[n]; - } - - if (!mpd_qresize(result, rlen, status)) { - goto finish; - } - - n = _coeff_from_u16(result, rlen, usrc, srclen, srcbase, status); - if (n == SIZE_MAX) { - goto finish; - } - - mpd_set_flags(result, srcsign); - result->exp = 0; - result->len = n; - mpd_setdigits(result); - - mpd_qresize(result, result->len, status); - mpd_qfinalize(result, ctx, status); - - -finish: - mpd_free(usrc); -} - -/* - * Converts a multiprecision integer with base <= UINT32_MAX to an mpd_t. - * The least significant word of the source is srcdata[0]. - */ -void -mpd_qimport_u32(mpd_t *result, - const uint32_t *srcdata, size_t srclen, - uint8_t srcsign, uint32_t srcbase, - const mpd_context_t *ctx, uint32_t *status) -{ - mpd_ssize_t rlen; /* length of the result */ - size_t n; - - assert(srclen > 0); - - rlen = _mpd_importsize(srclen, srcbase); - if (rlen == MPD_SSIZE_MAX) { - mpd_seterror(result, MPD_Invalid_operation, status); - return; - } - - if (!mpd_qresize(result, rlen, status)) { - return; - } - -#ifdef CONFIG_64 - n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, - srcdata, srclen, srcbase, - status); -#else - if (srcbase == MPD_RADIX) { - if (!mpd_qresize(result, srclen, status)) { - return; - } - memcpy(result->data, srcdata, srclen * (sizeof *srcdata)); - n = srclen; - } - else if (srcbase < MPD_RADIX) { - n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, - srcdata, srclen, srcbase, - status); - } - else { - mpd_uint_t *usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); - if (usrc == NULL) { - mpd_seterror(result, MPD_Malloc_error, status); - return; - } - for (n = 0; n < srclen; n++) { - usrc[n] = srcdata[n]; - } - - n = _coeff_from_larger_base(result, rlen, MPD_RADIX, - usrc, (mpd_ssize_t)srclen, srcbase, - status); - mpd_free(usrc); - } -#endif - - if (n == SIZE_MAX) { - return; - } - - mpd_set_flags(result, srcsign); - result->exp = 0; - result->len = n; - mpd_setdigits(result); - - mpd_qresize(result, result->len, status); - mpd_qfinalize(result, ctx, status); -} +} + +static uint8_t +mpd_resize_u16(uint16_t **w, size_t nmemb) +{ + uint8_t err = 0; + *w = mpd_realloc(*w, nmemb, sizeof **w, &err); + return !err; +} + +static uint8_t +mpd_resize_u32(uint32_t **w, size_t nmemb) +{ + uint8_t err = 0; + *w = mpd_realloc(*w, nmemb, sizeof **w, &err); + return !err; +} + +static size_t +_baseconv_to_u16(uint16_t **w, size_t wlen, mpd_uint_t wbase, + mpd_uint_t *u, mpd_ssize_t ulen) +{ + size_t n = 0; + + assert(wlen > 0 && ulen > 0); + assert(wbase <= (1U<<16)); + + do { + if (n >= wlen) { + if (!mpd_resize_u16(w, n+1)) { + return SIZE_MAX; + } + wlen = n+1; + } + (*w)[n++] = (uint16_t)_mpd_shortdiv(u, u, ulen, wbase); + /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ + ulen = _mpd_real_size(u, ulen); + + } while (u[ulen-1] != 0); + + return n; +} + +static size_t +_coeff_from_u16(mpd_t *w, mpd_ssize_t wlen, + const mpd_uint_t *u, size_t ulen, uint32_t ubase, + uint32_t *status) +{ + mpd_ssize_t n = 0; + mpd_uint_t carry; + + assert(wlen > 0 && ulen > 0); + assert(ubase <= (1U<<16)); + + w->data[n++] = u[--ulen]; + while (--ulen != SIZE_MAX) { + carry = _mpd_shortmul_c(w->data, w->data, n, ubase); + if (carry) { + if (n >= wlen) { + if (!mpd_qresize(w, n+1, status)) { + return SIZE_MAX; + } + wlen = n+1; + } + w->data[n++] = carry; + } + carry = _mpd_shortadd(w->data, n, u[ulen]); + if (carry) { + if (n >= wlen) { + if (!mpd_qresize(w, n+1, status)) { + return SIZE_MAX; + } + wlen = n+1; + } + w->data[n++] = carry; + } + } + + return n; +} + +/* target base wbase < source base ubase */ +static size_t +_baseconv_to_smaller(uint32_t **w, size_t wlen, uint32_t wbase, + mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase) +{ + size_t n = 0; + + assert(wlen > 0 && ulen > 0); + assert(wbase < ubase); + + do { + if (n >= wlen) { + if (!mpd_resize_u32(w, n+1)) { + return SIZE_MAX; + } + wlen = n+1; + } + (*w)[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); + /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ + ulen = _mpd_real_size(u, ulen); + + } while (u[ulen-1] != 0); + + return n; +} + +#ifdef CONFIG_32 +/* target base 'wbase' == source base 'ubase' */ +static size_t +_copy_equal_base(uint32_t **w, size_t wlen, + const uint32_t *u, size_t ulen) +{ + if (wlen < ulen) { + if (!mpd_resize_u32(w, ulen)) { + return SIZE_MAX; + } + } + + memcpy(*w, u, ulen * (sizeof **w)); + return ulen; +} + +/* target base 'wbase' > source base 'ubase' */ +static size_t +_baseconv_to_larger(uint32_t **w, size_t wlen, mpd_uint_t wbase, + const mpd_uint_t *u, size_t ulen, mpd_uint_t ubase) +{ + size_t n = 0; + mpd_uint_t carry; + + assert(wlen > 0 && ulen > 0); + assert(ubase < wbase); + + (*w)[n++] = u[--ulen]; + while (--ulen != SIZE_MAX) { + carry = _mpd_shortmul_b(*w, *w, n, ubase, wbase); + if (carry) { + if (n >= wlen) { + if (!mpd_resize_u32(w, n+1)) { + return SIZE_MAX; + } + wlen = n+1; + } + (*w)[n++] = carry; + } + carry = _mpd_shortadd_b(*w, n, u[ulen], wbase); + if (carry) { + if (n >= wlen) { + if (!mpd_resize_u32(w, n+1)) { + return SIZE_MAX; + } + wlen = n+1; + } + (*w)[n++] = carry; + } + } + + return n; +} + +/* target base wbase < source base ubase */ +static size_t +_coeff_from_larger_base(mpd_t *w, size_t wlen, mpd_uint_t wbase, + mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase, + uint32_t *status) +{ + size_t n = 0; + + assert(wlen > 0 && ulen > 0); + assert(wbase < ubase); + + do { + if (n >= wlen) { + if (!mpd_qresize(w, n+1, status)) { + return SIZE_MAX; + } + wlen = n+1; + } + w->data[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); + /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ + ulen = _mpd_real_size(u, ulen); + + } while (u[ulen-1] != 0); + + return n; +} +#endif + +/* target base 'wbase' > source base 'ubase' */ +static size_t +_coeff_from_smaller_base(mpd_t *w, mpd_ssize_t wlen, mpd_uint_t wbase, + const uint32_t *u, size_t ulen, mpd_uint_t ubase, + uint32_t *status) +{ + mpd_ssize_t n = 0; + mpd_uint_t carry; + + assert(wlen > 0 && ulen > 0); + assert(wbase > ubase); + + w->data[n++] = u[--ulen]; + while (--ulen != SIZE_MAX) { + carry = _mpd_shortmul_b(w->data, w->data, n, ubase, wbase); + if (carry) { + if (n >= wlen) { + if (!mpd_qresize(w, n+1, status)) { + return SIZE_MAX; + } + wlen = n+1; + } + w->data[n++] = carry; + } + carry = _mpd_shortadd_b(w->data, n, u[ulen], wbase); + if (carry) { + if (n >= wlen) { + if (!mpd_qresize(w, n+1, status)) { + return SIZE_MAX; + } + wlen = n+1; + } + w->data[n++] = carry; + } + } + + return n; +} + +/* + * Convert an integer mpd_t to a multiprecision integer with base <= 2**16. + * The least significant word of the result is (*rdata)[0]. + * + * If rdata is NULL, space is allocated by the function and rlen is irrelevant. + * In case of an error any allocated storage is freed and rdata is set back to + * NULL. + * + * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation + * functions and rlen MUST be correct. If necessary, the function will resize + * rdata. In case of an error the caller must free rdata. + * + * Return value: In case of success, the exact length of rdata, SIZE_MAX + * otherwise. + */ +size_t +mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t rbase, + const mpd_t *src, uint32_t *status) +{ + MPD_NEW_STATIC(tsrc,0,0,0,0); + int alloc = 0; /* rdata == NULL */ + size_t n; + + assert(rbase <= (1U<<16)); + + if (mpd_isspecial(src) || !_mpd_isint(src)) { + *status |= MPD_Invalid_operation; + return SIZE_MAX; + } + + if (*rdata == NULL) { + rlen = mpd_sizeinbase(src, rbase); + if (rlen == SIZE_MAX) { + *status |= MPD_Invalid_operation; + return SIZE_MAX; + } + *rdata = mpd_alloc(rlen, sizeof **rdata); + if (*rdata == NULL) { + goto malloc_error; + } + alloc = 1; + } + + if (mpd_iszero(src)) { + **rdata = 0; + return 1; + } + + if (src->exp >= 0) { + if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { + goto malloc_error; + } + } + else { + if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { + goto malloc_error; + } + } + + n = _baseconv_to_u16(rdata, rlen, rbase, tsrc.data, tsrc.len); + if (n == SIZE_MAX) { + goto malloc_error; + } + + +out: + mpd_del(&tsrc); + return n; + +malloc_error: + if (alloc) { + mpd_free(*rdata); + *rdata = NULL; + } + n = SIZE_MAX; + *status |= MPD_Malloc_error; + goto out; +} + +/* + * Convert an integer mpd_t to a multiprecision integer with base<=UINT32_MAX. + * The least significant word of the result is (*rdata)[0]. + * + * If rdata is NULL, space is allocated by the function and rlen is irrelevant. + * In case of an error any allocated storage is freed and rdata is set back to + * NULL. + * + * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation + * functions and rlen MUST be correct. If necessary, the function will resize + * rdata. In case of an error the caller must free rdata. + * + * Return value: In case of success, the exact length of rdata, SIZE_MAX + * otherwise. + */ +size_t +mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t rbase, + const mpd_t *src, uint32_t *status) +{ + MPD_NEW_STATIC(tsrc,0,0,0,0); + int alloc = 0; /* rdata == NULL */ + size_t n; + + if (mpd_isspecial(src) || !_mpd_isint(src)) { + *status |= MPD_Invalid_operation; + return SIZE_MAX; + } + + if (*rdata == NULL) { + rlen = mpd_sizeinbase(src, rbase); + if (rlen == SIZE_MAX) { + *status |= MPD_Invalid_operation; + return SIZE_MAX; + } + *rdata = mpd_alloc(rlen, sizeof **rdata); + if (*rdata == NULL) { + goto malloc_error; + } + alloc = 1; + } + + if (mpd_iszero(src)) { + **rdata = 0; + return 1; + } + + if (src->exp >= 0) { + if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { + goto malloc_error; + } + } + else { + if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { + goto malloc_error; + } + } + +#ifdef CONFIG_64 + n = _baseconv_to_smaller(rdata, rlen, rbase, + tsrc.data, tsrc.len, MPD_RADIX); +#else + if (rbase == MPD_RADIX) { + n = _copy_equal_base(rdata, rlen, tsrc.data, tsrc.len); + } + else if (rbase < MPD_RADIX) { + n = _baseconv_to_smaller(rdata, rlen, rbase, + tsrc.data, tsrc.len, MPD_RADIX); + } + else { + n = _baseconv_to_larger(rdata, rlen, rbase, + tsrc.data, tsrc.len, MPD_RADIX); + } +#endif + + if (n == SIZE_MAX) { + goto malloc_error; + } + + +out: + mpd_del(&tsrc); + return n; + +malloc_error: + if (alloc) { + mpd_free(*rdata); + *rdata = NULL; + } + n = SIZE_MAX; + *status |= MPD_Malloc_error; + goto out; +} + + +/* + * Converts a multiprecision integer with base <= UINT16_MAX+1 to an mpd_t. + * The least significant word of the source is srcdata[0]. + */ +void +mpd_qimport_u16(mpd_t *result, + const uint16_t *srcdata, size_t srclen, + uint8_t srcsign, uint32_t srcbase, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_uint_t *usrc; /* uint16_t src copied to an mpd_uint_t array */ + mpd_ssize_t rlen; /* length of the result */ + size_t n; + + assert(srclen > 0); + assert(srcbase <= (1U<<16)); + + rlen = _mpd_importsize(srclen, srcbase); + if (rlen == MPD_SSIZE_MAX) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); + if (usrc == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + for (n = 0; n < srclen; n++) { + usrc[n] = srcdata[n]; + } + + if (!mpd_qresize(result, rlen, status)) { + goto finish; + } + + n = _coeff_from_u16(result, rlen, usrc, srclen, srcbase, status); + if (n == SIZE_MAX) { + goto finish; + } + + mpd_set_flags(result, srcsign); + result->exp = 0; + result->len = n; + mpd_setdigits(result); + + mpd_qresize(result, result->len, status); + mpd_qfinalize(result, ctx, status); + + +finish: + mpd_free(usrc); +} + +/* + * Converts a multiprecision integer with base <= UINT32_MAX to an mpd_t. + * The least significant word of the source is srcdata[0]. + */ +void +mpd_qimport_u32(mpd_t *result, + const uint32_t *srcdata, size_t srclen, + uint8_t srcsign, uint32_t srcbase, + const mpd_context_t *ctx, uint32_t *status) +{ + mpd_ssize_t rlen; /* length of the result */ + size_t n; + + assert(srclen > 0); + + rlen = _mpd_importsize(srclen, srcbase); + if (rlen == MPD_SSIZE_MAX) { + mpd_seterror(result, MPD_Invalid_operation, status); + return; + } + + if (!mpd_qresize(result, rlen, status)) { + return; + } + +#ifdef CONFIG_64 + n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, + srcdata, srclen, srcbase, + status); +#else + if (srcbase == MPD_RADIX) { + if (!mpd_qresize(result, srclen, status)) { + return; + } + memcpy(result->data, srcdata, srclen * (sizeof *srcdata)); + n = srclen; + } + else if (srcbase < MPD_RADIX) { + n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, + srcdata, srclen, srcbase, + status); + } + else { + mpd_uint_t *usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); + if (usrc == NULL) { + mpd_seterror(result, MPD_Malloc_error, status); + return; + } + for (n = 0; n < srclen; n++) { + usrc[n] = srcdata[n]; + } + + n = _coeff_from_larger_base(result, rlen, MPD_RADIX, + usrc, (mpd_ssize_t)srclen, srcbase, + status); + mpd_free(usrc); + } +#endif + + if (n == SIZE_MAX) { + return; + } + + mpd_set_flags(result, srcsign); + result->exp = 0; + result->len = n; + mpd_setdigits(result); + + mpd_qresize(result, result->len, status); + mpd_qfinalize(result, ctx, status); +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.h index f66f6e59207..547d32bec48 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/mpdecimal.h @@ -1,55 +1,55 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_MPDECIMAL_H_ #define LIBMPDEC_MPDECIMAL_H_ - - + + #ifndef _MSC_VER #include "pyconfig.h" #endif -#ifdef __cplusplus +#ifdef __cplusplus #include <cinttypes> #include <climits> #include <cstdint> #include <cstdio> #include <cstdlib> -extern "C" { +extern "C" { #else #include <inttypes.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#endif - - +#endif + + #if (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) && \ defined(__GNUC__) && __GNUC__ >= 4 && !defined(__INTEL_COMPILER) #define MPD_PRAGMA(x) _Pragma(x) @@ -59,775 +59,775 @@ extern "C" { #define MPD_PRAGMA(x) #define MPD_HIDE_SYMBOLS_START #define MPD_HIDE_SYMBOLS_END -#endif - +#endif + #if defined(_MSC_VER) - #include "vccompat.h" - #define EXTINLINE extern inline -#else - #define EXTINLINE -#endif - - -/* This header file is internal for the purpose of building _decimal.so. - * All symbols should have local scope in the DSO. */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -#if !defined(LEGACY_COMPILER) - #if !defined(UINT64_MAX) - /* The following #error is just a warning. If the compiler indeed does - * not have uint64_t, it is perfectly safe to comment out the #error. */ - #error "Warning: Compiler without uint64_t. Comment out this line." - #define LEGACY_COMPILER - #endif -#endif - - -/******************************************************************************/ -/* Version */ -/******************************************************************************/ - -#define MPD_MAJOR_VERSION 2 + #include "vccompat.h" + #define EXTINLINE extern inline +#else + #define EXTINLINE +#endif + + +/* This header file is internal for the purpose of building _decimal.so. + * All symbols should have local scope in the DSO. */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +#if !defined(LEGACY_COMPILER) + #if !defined(UINT64_MAX) + /* The following #error is just a warning. If the compiler indeed does + * not have uint64_t, it is perfectly safe to comment out the #error. */ + #error "Warning: Compiler without uint64_t. Comment out this line." + #define LEGACY_COMPILER + #endif +#endif + + +/******************************************************************************/ +/* Version */ +/******************************************************************************/ + +#define MPD_MAJOR_VERSION 2 #define MPD_MINOR_VERSION 5 #define MPD_MICRO_VERSION 0 - + #define MPD_VERSION "2.5.0" - -#define MPD_VERSION_HEX ((MPD_MAJOR_VERSION << 24) | \ - (MPD_MINOR_VERSION << 16) | \ - (MPD_MICRO_VERSION << 8)) - -const char *mpd_version(void); - - -/******************************************************************************/ -/* Configuration */ -/******************************************************************************/ - -#if 1 - #if defined(CONFIG_64) || defined(CONFIG_32) - #error "cannot use CONFIG_64 or CONFIG_32 with UNIVERSAL." - #endif + +#define MPD_VERSION_HEX ((MPD_MAJOR_VERSION << 24) | \ + (MPD_MINOR_VERSION << 16) | \ + (MPD_MICRO_VERSION << 8)) + +const char *mpd_version(void); + + +/******************************************************************************/ +/* Configuration */ +/******************************************************************************/ + +#if 1 + #if defined(CONFIG_64) || defined(CONFIG_32) + #error "cannot use CONFIG_64 or CONFIG_32 with UNIVERSAL." + #endif #if defined(__powerpc64__) || defined(_M_AMD64) || defined(__aarch64__) #define CONFIG_64 #define ANSI #elif defined(__powerpc__) - #define CONFIG_32 - #define ANSI - #elif defined(__i386__) || defined(_M_IX86) - #define CONFIG_32 - #define ANSI - #elif defined(__x86_64__) - #define CONFIG_64 - #define ASM + #define CONFIG_32 + #define ANSI + #elif defined(__i386__) || defined(_M_IX86) + #define CONFIG_32 + #define ANSI + #elif defined(__x86_64__) + #define CONFIG_64 + #define ASM #elif defined(__arm64__) #define CONFIG_64 #define ANSI - #else - #error "unknown architecture for universal build." - #endif -#endif - - -/* BEGIN CONFIG_64 */ -#if defined(CONFIG_64) -/* types for modular and base arithmetic */ -#define MPD_UINT_MAX UINT64_MAX -#define MPD_BITS_PER_UINT 64 -typedef uint64_t mpd_uint_t; /* unsigned mod type */ - -#define MPD_SIZE_MAX SIZE_MAX -typedef size_t mpd_size_t; /* unsigned size type */ - -/* type for exp, digits, len, prec */ -#define MPD_SSIZE_MAX INT64_MAX -#define MPD_SSIZE_MIN INT64_MIN -typedef int64_t mpd_ssize_t; -#define _mpd_strtossize strtoll - -/* decimal arithmetic */ -#define MPD_RADIX 10000000000000000000ULL /* 10**19 */ -#define MPD_RDIGITS 19 -#define MPD_MAX_POW10 19 -#define MPD_EXPDIGITS 19 /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */ - -#define MPD_MAXTRANSFORM_2N 4294967296ULL /* 2**32 */ -#define MPD_MAX_PREC 999999999999999999LL -#define MPD_MAX_PREC_LOG2 64 -#define MPD_ELIMIT 1000000000000000000LL -#define MPD_MAX_EMAX 999999999999999999LL /* ELIMIT-1 */ -#define MPD_MIN_EMIN (-999999999999999999LL) /* -EMAX */ -#define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1)) -#define MPD_EXP_INF 2000000000000000001LL -#define MPD_EXP_CLAMP (-4000000000000000001LL) -#define MPD_MAXIMPORT 105263157894736842L /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */ - -/* conversion specifiers */ -#define PRI_mpd_uint_t PRIu64 -#define PRI_mpd_ssize_t PRIi64 -/* END CONFIG_64 */ - - -/* BEGIN CONFIG_32 */ -#elif defined(CONFIG_32) -/* types for modular and base arithmetic */ -#define MPD_UINT_MAX UINT32_MAX -#define MPD_BITS_PER_UINT 32 -typedef uint32_t mpd_uint_t; /* unsigned mod type */ - -#ifndef LEGACY_COMPILER -#define MPD_UUINT_MAX UINT64_MAX -typedef uint64_t mpd_uuint_t; /* double width unsigned mod type */ -#endif - -#define MPD_SIZE_MAX SIZE_MAX -typedef size_t mpd_size_t; /* unsigned size type */ - -/* type for dec->len, dec->exp, ctx->prec */ -#define MPD_SSIZE_MAX INT32_MAX -#define MPD_SSIZE_MIN INT32_MIN -typedef int32_t mpd_ssize_t; -#define _mpd_strtossize strtol - -/* decimal arithmetic */ -#define MPD_RADIX 1000000000UL /* 10**9 */ -#define MPD_RDIGITS 9 -#define MPD_MAX_POW10 9 -#define MPD_EXPDIGITS 10 /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */ - -#define MPD_MAXTRANSFORM_2N 33554432UL /* 2**25 */ -#define MPD_MAX_PREC 425000000L -#define MPD_MAX_PREC_LOG2 32 -#define MPD_ELIMIT 425000001L -#define MPD_MAX_EMAX 425000000L /* ELIMIT-1 */ -#define MPD_MIN_EMIN (-425000000L) /* -EMAX */ -#define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1)) -#define MPD_EXP_INF 1000000001L /* allows for emax=999999999 in the tests */ -#define MPD_EXP_CLAMP (-2000000001L) /* allows for emin=-999999999 in the tests */ -#define MPD_MAXIMPORT 94444445L /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */ - -/* conversion specifiers */ -#define PRI_mpd_uint_t PRIu32 -#define PRI_mpd_ssize_t PRIi32 -/* END CONFIG_32 */ - -#else - #error "define CONFIG_64 or CONFIG_32" -#endif -/* END CONFIG */ - - -#if MPD_SIZE_MAX != MPD_UINT_MAX - #error "unsupported platform: need mpd_size_t == mpd_uint_t" -#endif - - -/******************************************************************************/ -/* Context */ -/******************************************************************************/ - -enum { - MPD_ROUND_UP, /* round away from 0 */ - MPD_ROUND_DOWN, /* round toward 0 (truncate) */ - MPD_ROUND_CEILING, /* round toward +infinity */ - MPD_ROUND_FLOOR, /* round toward -infinity */ - MPD_ROUND_HALF_UP, /* 0.5 is rounded up */ - MPD_ROUND_HALF_DOWN, /* 0.5 is rounded down */ - MPD_ROUND_HALF_EVEN, /* 0.5 is rounded to even */ - MPD_ROUND_05UP, /* round zero or five away from 0 */ - MPD_ROUND_TRUNC, /* truncate, but set infinity */ - MPD_ROUND_GUARD -}; - -enum { MPD_CLAMP_DEFAULT, MPD_CLAMP_IEEE_754, MPD_CLAMP_GUARD }; - -extern const char *mpd_round_string[MPD_ROUND_GUARD]; -extern const char *mpd_clamp_string[MPD_CLAMP_GUARD]; - - -typedef struct mpd_context_t { - mpd_ssize_t prec; /* precision */ - mpd_ssize_t emax; /* max positive exp */ - mpd_ssize_t emin; /* min negative exp */ - uint32_t traps; /* status events that should be trapped */ - uint32_t status; /* status flags */ - uint32_t newtrap; /* set by mpd_addstatus_raise() */ - int round; /* rounding mode */ - int clamp; /* clamp mode */ - int allcr; /* all functions correctly rounded */ -} mpd_context_t; - - -/* Status flags */ -#define MPD_Clamped 0x00000001U -#define MPD_Conversion_syntax 0x00000002U -#define MPD_Division_by_zero 0x00000004U -#define MPD_Division_impossible 0x00000008U -#define MPD_Division_undefined 0x00000010U -#define MPD_Fpu_error 0x00000020U -#define MPD_Inexact 0x00000040U -#define MPD_Invalid_context 0x00000080U -#define MPD_Invalid_operation 0x00000100U -#define MPD_Malloc_error 0x00000200U -#define MPD_Not_implemented 0x00000400U -#define MPD_Overflow 0x00000800U -#define MPD_Rounded 0x00001000U -#define MPD_Subnormal 0x00002000U -#define MPD_Underflow 0x00004000U -#define MPD_Max_status (0x00008000U-1U) - -/* Conditions that result in an IEEE 754 exception */ -#define MPD_IEEE_Invalid_operation (MPD_Conversion_syntax | \ - MPD_Division_impossible | \ - MPD_Division_undefined | \ - MPD_Fpu_error | \ - MPD_Invalid_context | \ - MPD_Invalid_operation | \ - MPD_Malloc_error) \ - -/* Errors that require the result of an operation to be set to NaN */ -#define MPD_Errors (MPD_IEEE_Invalid_operation | \ - MPD_Division_by_zero) - -/* Default traps */ -#define MPD_Traps (MPD_IEEE_Invalid_operation | \ - MPD_Division_by_zero | \ - MPD_Overflow | \ - MPD_Underflow) - -/* Official name */ -#define MPD_Insufficient_storage MPD_Malloc_error - -/* IEEE 754 interchange format contexts */ -#define MPD_IEEE_CONTEXT_MAX_BITS 512 /* 16*(log2(MPD_MAX_EMAX / 3)-3) */ -#define MPD_DECIMAL32 32 -#define MPD_DECIMAL64 64 -#define MPD_DECIMAL128 128 - - -#define MPD_MINALLOC_MIN 2 -#define MPD_MINALLOC_MAX 64 -extern mpd_ssize_t MPD_MINALLOC; -extern void (* mpd_traphandler)(mpd_context_t *); -void mpd_dflt_traphandler(mpd_context_t *); - -void mpd_setminalloc(mpd_ssize_t n); -void mpd_init(mpd_context_t *ctx, mpd_ssize_t prec); - -void mpd_maxcontext(mpd_context_t *ctx); -void mpd_defaultcontext(mpd_context_t *ctx); -void mpd_basiccontext(mpd_context_t *ctx); -int mpd_ieee_context(mpd_context_t *ctx, int bits); - -mpd_ssize_t mpd_getprec(const mpd_context_t *ctx); -mpd_ssize_t mpd_getemax(const mpd_context_t *ctx); -mpd_ssize_t mpd_getemin(const mpd_context_t *ctx); -int mpd_getround(const mpd_context_t *ctx); -uint32_t mpd_gettraps(const mpd_context_t *ctx); -uint32_t mpd_getstatus(const mpd_context_t *ctx); -int mpd_getclamp(const mpd_context_t *ctx); -int mpd_getcr(const mpd_context_t *ctx); - -int mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec); -int mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax); -int mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin); -int mpd_qsetround(mpd_context_t *ctx, int newround); -int mpd_qsettraps(mpd_context_t *ctx, uint32_t flags); -int mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags); -int mpd_qsetclamp(mpd_context_t *ctx, int c); -int mpd_qsetcr(mpd_context_t *ctx, int c); -void mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags); - - -/******************************************************************************/ -/* Decimal Arithmetic */ -/******************************************************************************/ - -/* mpd_t flags */ -#define MPD_POS ((uint8_t)0) -#define MPD_NEG ((uint8_t)1) -#define MPD_INF ((uint8_t)2) -#define MPD_NAN ((uint8_t)4) -#define MPD_SNAN ((uint8_t)8) -#define MPD_SPECIAL (MPD_INF|MPD_NAN|MPD_SNAN) -#define MPD_STATIC ((uint8_t)16) -#define MPD_STATIC_DATA ((uint8_t)32) -#define MPD_SHARED_DATA ((uint8_t)64) -#define MPD_CONST_DATA ((uint8_t)128) -#define MPD_DATAFLAGS (MPD_STATIC_DATA|MPD_SHARED_DATA|MPD_CONST_DATA) - -/* mpd_t */ -typedef struct mpd_t { - uint8_t flags; - mpd_ssize_t exp; - mpd_ssize_t digits; - mpd_ssize_t len; - mpd_ssize_t alloc; - mpd_uint_t *data; -} mpd_t; - - -typedef unsigned char uchar; - - -/******************************************************************************/ -/* Quiet, thread-safe functions */ -/******************************************************************************/ - -/* format specification */ -typedef struct mpd_spec_t { - mpd_ssize_t min_width; /* minimum field width */ - mpd_ssize_t prec; /* fraction digits or significant digits */ - char type; /* conversion specifier */ - char align; /* alignment */ - char sign; /* sign printing/alignment */ - char fill[5]; /* fill character */ - const char *dot; /* decimal point */ - const char *sep; /* thousands separator */ - const char *grouping; /* grouping of digits */ -} mpd_spec_t; - -/* output to a string */ -char *mpd_to_sci(const mpd_t *dec, int fmt); -char *mpd_to_eng(const mpd_t *dec, int fmt); -mpd_ssize_t mpd_to_sci_size(char **res, const mpd_t *dec, int fmt); -mpd_ssize_t mpd_to_eng_size(char **res, const mpd_t *dec, int fmt); -int mpd_validate_lconv(mpd_spec_t *spec); -int mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps); -char *mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, const mpd_context_t *ctx, uint32_t *status); -char *mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, uint32_t *status); - -#define MPD_NUM_FLAGS 15 -#define MPD_MAX_FLAG_STRING 208 -#define MPD_MAX_FLAG_LIST (MPD_MAX_FLAG_STRING+18) -#define MPD_MAX_SIGNAL_LIST 121 -int mpd_snprint_flags(char *dest, int nmemb, uint32_t flags); -int mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]); -int mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]); - -/* output to a file */ -void mpd_fprint(FILE *file, const mpd_t *dec); -void mpd_print(const mpd_t *dec); - -/* assignment from a string */ -void mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, uint32_t *status); + #else + #error "unknown architecture for universal build." + #endif +#endif + + +/* BEGIN CONFIG_64 */ +#if defined(CONFIG_64) +/* types for modular and base arithmetic */ +#define MPD_UINT_MAX UINT64_MAX +#define MPD_BITS_PER_UINT 64 +typedef uint64_t mpd_uint_t; /* unsigned mod type */ + +#define MPD_SIZE_MAX SIZE_MAX +typedef size_t mpd_size_t; /* unsigned size type */ + +/* type for exp, digits, len, prec */ +#define MPD_SSIZE_MAX INT64_MAX +#define MPD_SSIZE_MIN INT64_MIN +typedef int64_t mpd_ssize_t; +#define _mpd_strtossize strtoll + +/* decimal arithmetic */ +#define MPD_RADIX 10000000000000000000ULL /* 10**19 */ +#define MPD_RDIGITS 19 +#define MPD_MAX_POW10 19 +#define MPD_EXPDIGITS 19 /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */ + +#define MPD_MAXTRANSFORM_2N 4294967296ULL /* 2**32 */ +#define MPD_MAX_PREC 999999999999999999LL +#define MPD_MAX_PREC_LOG2 64 +#define MPD_ELIMIT 1000000000000000000LL +#define MPD_MAX_EMAX 999999999999999999LL /* ELIMIT-1 */ +#define MPD_MIN_EMIN (-999999999999999999LL) /* -EMAX */ +#define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1)) +#define MPD_EXP_INF 2000000000000000001LL +#define MPD_EXP_CLAMP (-4000000000000000001LL) +#define MPD_MAXIMPORT 105263157894736842L /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */ + +/* conversion specifiers */ +#define PRI_mpd_uint_t PRIu64 +#define PRI_mpd_ssize_t PRIi64 +/* END CONFIG_64 */ + + +/* BEGIN CONFIG_32 */ +#elif defined(CONFIG_32) +/* types for modular and base arithmetic */ +#define MPD_UINT_MAX UINT32_MAX +#define MPD_BITS_PER_UINT 32 +typedef uint32_t mpd_uint_t; /* unsigned mod type */ + +#ifndef LEGACY_COMPILER +#define MPD_UUINT_MAX UINT64_MAX +typedef uint64_t mpd_uuint_t; /* double width unsigned mod type */ +#endif + +#define MPD_SIZE_MAX SIZE_MAX +typedef size_t mpd_size_t; /* unsigned size type */ + +/* type for dec->len, dec->exp, ctx->prec */ +#define MPD_SSIZE_MAX INT32_MAX +#define MPD_SSIZE_MIN INT32_MIN +typedef int32_t mpd_ssize_t; +#define _mpd_strtossize strtol + +/* decimal arithmetic */ +#define MPD_RADIX 1000000000UL /* 10**9 */ +#define MPD_RDIGITS 9 +#define MPD_MAX_POW10 9 +#define MPD_EXPDIGITS 10 /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */ + +#define MPD_MAXTRANSFORM_2N 33554432UL /* 2**25 */ +#define MPD_MAX_PREC 425000000L +#define MPD_MAX_PREC_LOG2 32 +#define MPD_ELIMIT 425000001L +#define MPD_MAX_EMAX 425000000L /* ELIMIT-1 */ +#define MPD_MIN_EMIN (-425000000L) /* -EMAX */ +#define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1)) +#define MPD_EXP_INF 1000000001L /* allows for emax=999999999 in the tests */ +#define MPD_EXP_CLAMP (-2000000001L) /* allows for emin=-999999999 in the tests */ +#define MPD_MAXIMPORT 94444445L /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */ + +/* conversion specifiers */ +#define PRI_mpd_uint_t PRIu32 +#define PRI_mpd_ssize_t PRIi32 +/* END CONFIG_32 */ + +#else + #error "define CONFIG_64 or CONFIG_32" +#endif +/* END CONFIG */ + + +#if MPD_SIZE_MAX != MPD_UINT_MAX + #error "unsupported platform: need mpd_size_t == mpd_uint_t" +#endif + + +/******************************************************************************/ +/* Context */ +/******************************************************************************/ + +enum { + MPD_ROUND_UP, /* round away from 0 */ + MPD_ROUND_DOWN, /* round toward 0 (truncate) */ + MPD_ROUND_CEILING, /* round toward +infinity */ + MPD_ROUND_FLOOR, /* round toward -infinity */ + MPD_ROUND_HALF_UP, /* 0.5 is rounded up */ + MPD_ROUND_HALF_DOWN, /* 0.5 is rounded down */ + MPD_ROUND_HALF_EVEN, /* 0.5 is rounded to even */ + MPD_ROUND_05UP, /* round zero or five away from 0 */ + MPD_ROUND_TRUNC, /* truncate, but set infinity */ + MPD_ROUND_GUARD +}; + +enum { MPD_CLAMP_DEFAULT, MPD_CLAMP_IEEE_754, MPD_CLAMP_GUARD }; + +extern const char *mpd_round_string[MPD_ROUND_GUARD]; +extern const char *mpd_clamp_string[MPD_CLAMP_GUARD]; + + +typedef struct mpd_context_t { + mpd_ssize_t prec; /* precision */ + mpd_ssize_t emax; /* max positive exp */ + mpd_ssize_t emin; /* min negative exp */ + uint32_t traps; /* status events that should be trapped */ + uint32_t status; /* status flags */ + uint32_t newtrap; /* set by mpd_addstatus_raise() */ + int round; /* rounding mode */ + int clamp; /* clamp mode */ + int allcr; /* all functions correctly rounded */ +} mpd_context_t; + + +/* Status flags */ +#define MPD_Clamped 0x00000001U +#define MPD_Conversion_syntax 0x00000002U +#define MPD_Division_by_zero 0x00000004U +#define MPD_Division_impossible 0x00000008U +#define MPD_Division_undefined 0x00000010U +#define MPD_Fpu_error 0x00000020U +#define MPD_Inexact 0x00000040U +#define MPD_Invalid_context 0x00000080U +#define MPD_Invalid_operation 0x00000100U +#define MPD_Malloc_error 0x00000200U +#define MPD_Not_implemented 0x00000400U +#define MPD_Overflow 0x00000800U +#define MPD_Rounded 0x00001000U +#define MPD_Subnormal 0x00002000U +#define MPD_Underflow 0x00004000U +#define MPD_Max_status (0x00008000U-1U) + +/* Conditions that result in an IEEE 754 exception */ +#define MPD_IEEE_Invalid_operation (MPD_Conversion_syntax | \ + MPD_Division_impossible | \ + MPD_Division_undefined | \ + MPD_Fpu_error | \ + MPD_Invalid_context | \ + MPD_Invalid_operation | \ + MPD_Malloc_error) \ + +/* Errors that require the result of an operation to be set to NaN */ +#define MPD_Errors (MPD_IEEE_Invalid_operation | \ + MPD_Division_by_zero) + +/* Default traps */ +#define MPD_Traps (MPD_IEEE_Invalid_operation | \ + MPD_Division_by_zero | \ + MPD_Overflow | \ + MPD_Underflow) + +/* Official name */ +#define MPD_Insufficient_storage MPD_Malloc_error + +/* IEEE 754 interchange format contexts */ +#define MPD_IEEE_CONTEXT_MAX_BITS 512 /* 16*(log2(MPD_MAX_EMAX / 3)-3) */ +#define MPD_DECIMAL32 32 +#define MPD_DECIMAL64 64 +#define MPD_DECIMAL128 128 + + +#define MPD_MINALLOC_MIN 2 +#define MPD_MINALLOC_MAX 64 +extern mpd_ssize_t MPD_MINALLOC; +extern void (* mpd_traphandler)(mpd_context_t *); +void mpd_dflt_traphandler(mpd_context_t *); + +void mpd_setminalloc(mpd_ssize_t n); +void mpd_init(mpd_context_t *ctx, mpd_ssize_t prec); + +void mpd_maxcontext(mpd_context_t *ctx); +void mpd_defaultcontext(mpd_context_t *ctx); +void mpd_basiccontext(mpd_context_t *ctx); +int mpd_ieee_context(mpd_context_t *ctx, int bits); + +mpd_ssize_t mpd_getprec(const mpd_context_t *ctx); +mpd_ssize_t mpd_getemax(const mpd_context_t *ctx); +mpd_ssize_t mpd_getemin(const mpd_context_t *ctx); +int mpd_getround(const mpd_context_t *ctx); +uint32_t mpd_gettraps(const mpd_context_t *ctx); +uint32_t mpd_getstatus(const mpd_context_t *ctx); +int mpd_getclamp(const mpd_context_t *ctx); +int mpd_getcr(const mpd_context_t *ctx); + +int mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec); +int mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax); +int mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin); +int mpd_qsetround(mpd_context_t *ctx, int newround); +int mpd_qsettraps(mpd_context_t *ctx, uint32_t flags); +int mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags); +int mpd_qsetclamp(mpd_context_t *ctx, int c); +int mpd_qsetcr(mpd_context_t *ctx, int c); +void mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags); + + +/******************************************************************************/ +/* Decimal Arithmetic */ +/******************************************************************************/ + +/* mpd_t flags */ +#define MPD_POS ((uint8_t)0) +#define MPD_NEG ((uint8_t)1) +#define MPD_INF ((uint8_t)2) +#define MPD_NAN ((uint8_t)4) +#define MPD_SNAN ((uint8_t)8) +#define MPD_SPECIAL (MPD_INF|MPD_NAN|MPD_SNAN) +#define MPD_STATIC ((uint8_t)16) +#define MPD_STATIC_DATA ((uint8_t)32) +#define MPD_SHARED_DATA ((uint8_t)64) +#define MPD_CONST_DATA ((uint8_t)128) +#define MPD_DATAFLAGS (MPD_STATIC_DATA|MPD_SHARED_DATA|MPD_CONST_DATA) + +/* mpd_t */ +typedef struct mpd_t { + uint8_t flags; + mpd_ssize_t exp; + mpd_ssize_t digits; + mpd_ssize_t len; + mpd_ssize_t alloc; + mpd_uint_t *data; +} mpd_t; + + +typedef unsigned char uchar; + + +/******************************************************************************/ +/* Quiet, thread-safe functions */ +/******************************************************************************/ + +/* format specification */ +typedef struct mpd_spec_t { + mpd_ssize_t min_width; /* minimum field width */ + mpd_ssize_t prec; /* fraction digits or significant digits */ + char type; /* conversion specifier */ + char align; /* alignment */ + char sign; /* sign printing/alignment */ + char fill[5]; /* fill character */ + const char *dot; /* decimal point */ + const char *sep; /* thousands separator */ + const char *grouping; /* grouping of digits */ +} mpd_spec_t; + +/* output to a string */ +char *mpd_to_sci(const mpd_t *dec, int fmt); +char *mpd_to_eng(const mpd_t *dec, int fmt); +mpd_ssize_t mpd_to_sci_size(char **res, const mpd_t *dec, int fmt); +mpd_ssize_t mpd_to_eng_size(char **res, const mpd_t *dec, int fmt); +int mpd_validate_lconv(mpd_spec_t *spec); +int mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps); +char *mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, const mpd_context_t *ctx, uint32_t *status); +char *mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, uint32_t *status); + +#define MPD_NUM_FLAGS 15 +#define MPD_MAX_FLAG_STRING 208 +#define MPD_MAX_FLAG_LIST (MPD_MAX_FLAG_STRING+18) +#define MPD_MAX_SIGNAL_LIST 121 +int mpd_snprint_flags(char *dest, int nmemb, uint32_t flags); +int mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]); +int mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]); + +/* output to a file */ +void mpd_fprint(FILE *file, const mpd_t *dec); +void mpd_print(const mpd_t *dec); + +/* assignment from a string */ +void mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, uint32_t *status); void mpd_qset_string_exact(mpd_t *dec, const char *s, uint32_t *status); - -/* set to NaN with error flags */ -void mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status); -/* set a special with sign and type */ -void mpd_setspecial(mpd_t *dec, uint8_t sign, uint8_t type); -/* set coefficient to zero or all nines */ -void mpd_zerocoeff(mpd_t *result); -void mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status); - -/* quietly assign a C integer type to an mpd_t */ -void mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status); -#ifndef LEGACY_COMPILER -void mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status); + +/* set to NaN with error flags */ +void mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status); +/* set a special with sign and type */ +void mpd_setspecial(mpd_t *dec, uint8_t sign, uint8_t type); +/* set coefficient to zero or all nines */ +void mpd_zerocoeff(mpd_t *result); +void mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status); + +/* quietly assign a C integer type to an mpd_t */ +void mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status); +#ifndef LEGACY_COMPILER +void mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status); void mpd_qset_i64_exact(mpd_t *result, int64_t a, uint32_t *status); void mpd_qset_u64_exact(mpd_t *result, uint64_t a, uint32_t *status); -#endif - -/* quietly assign a C integer type to an mpd_t with a static coefficient */ -void mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status); - -/* quietly get a C integer type from an mpd_t */ -mpd_ssize_t mpd_qget_ssize(const mpd_t *dec, uint32_t *status); -mpd_uint_t mpd_qget_uint(const mpd_t *dec, uint32_t *status); -mpd_uint_t mpd_qabs_uint(const mpd_t *dec, uint32_t *status); - -int32_t mpd_qget_i32(const mpd_t *dec, uint32_t *status); -uint32_t mpd_qget_u32(const mpd_t *dec, uint32_t *status); -#ifndef LEGACY_COMPILER -int64_t mpd_qget_i64(const mpd_t *dec, uint32_t *status); -uint64_t mpd_qget_u64(const mpd_t *dec, uint32_t *status); -#endif - -/* quiet functions */ -int mpd_qcheck_nan(mpd_t *nanresult, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -int mpd_qcheck_nans(mpd_t *nanresult, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status); - -const char *mpd_class(const mpd_t *a, const mpd_context_t *ctx); - +#endif + +/* quietly assign a C integer type to an mpd_t with a static coefficient */ +void mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status); + +/* quietly get a C integer type from an mpd_t */ +mpd_ssize_t mpd_qget_ssize(const mpd_t *dec, uint32_t *status); +mpd_uint_t mpd_qget_uint(const mpd_t *dec, uint32_t *status); +mpd_uint_t mpd_qabs_uint(const mpd_t *dec, uint32_t *status); + +int32_t mpd_qget_i32(const mpd_t *dec, uint32_t *status); +uint32_t mpd_qget_u32(const mpd_t *dec, uint32_t *status); +#ifndef LEGACY_COMPILER +int64_t mpd_qget_i64(const mpd_t *dec, uint32_t *status); +uint64_t mpd_qget_u64(const mpd_t *dec, uint32_t *status); +#endif + +/* quiet functions */ +int mpd_qcheck_nan(mpd_t *nanresult, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +int mpd_qcheck_nans(mpd_t *nanresult, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status); + +const char *mpd_class(const mpd_t *a, const mpd_context_t *ctx); + int mpd_qcopy(mpd_t *result, const mpd_t *a, uint32_t *status); int mpd_qcopy_cxx(mpd_t *result, const mpd_t *a); -mpd_t *mpd_qncopy(const mpd_t *a); -int mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status); -int mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status); -int mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status); - -void mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -int mpd_same_quantum(const mpd_t *a, const mpd_t *b); - -void mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -int mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status); -mpd_uint_t mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status); -mpd_uint_t mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n); -void mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx, uint32_t *status); - -int mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status); -int mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -int mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -int mpd_cmp_total(const mpd_t *a, const mpd_t *b); -int mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b); -int mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b); -int mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b); - -void mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); - -void mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status); -void mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status); -void mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); -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); -void mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_context_t *ctx, uint32_t *status); -void mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_t *mod, const mpd_context_t *ctx, uint32_t *status); -void mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status); -void mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); - -#ifndef LEGACY_COMPILER -void mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); -void mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); -#endif - - -size_t mpd_sizeinbase(const mpd_t *a, uint32_t base); -void mpd_qimport_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen, - uint8_t srcsign, uint32_t srcbase, - const mpd_context_t *ctx, uint32_t *status); -void mpd_qimport_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen, - uint8_t srcsign, uint32_t srcbase, - const mpd_context_t *ctx, uint32_t *status); -size_t mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t base, - const mpd_t *src, uint32_t *status); -size_t mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t base, - const mpd_t *src, uint32_t *status); - - -/******************************************************************************/ -/* Signalling functions */ -/******************************************************************************/ - -char *mpd_format(const mpd_t *dec, const char *fmt, mpd_context_t *ctx); -void mpd_import_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx); -void mpd_import_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx); -size_t mpd_export_u16(uint16_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx); -size_t mpd_export_u32(uint32_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx); -void mpd_finalize(mpd_t *result, mpd_context_t *ctx); -int mpd_check_nan(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -int mpd_check_nans(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_set_string(mpd_t *result, const char *s, mpd_context_t *ctx); -void mpd_maxcoeff(mpd_t *result, mpd_context_t *ctx); -void mpd_sset_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx); -void mpd_sset_i32(mpd_t *result, int32_t a, mpd_context_t *ctx); -void mpd_sset_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx); -void mpd_sset_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx); -void mpd_set_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx); -void mpd_set_i32(mpd_t *result, int32_t a, mpd_context_t *ctx); -void mpd_set_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx); -void mpd_set_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx); -#ifndef LEGACY_COMPILER -void mpd_set_i64(mpd_t *result, int64_t a, mpd_context_t *ctx); -void mpd_set_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx); -#endif -mpd_ssize_t mpd_get_ssize(const mpd_t *a, mpd_context_t *ctx); -mpd_uint_t mpd_get_uint(const mpd_t *a, mpd_context_t *ctx); -mpd_uint_t mpd_abs_uint(const mpd_t *a, mpd_context_t *ctx); -int32_t mpd_get_i32(const mpd_t *a, mpd_context_t *ctx); -uint32_t mpd_get_u32(const mpd_t *a, mpd_context_t *ctx); -#ifndef LEGACY_COMPILER -int64_t mpd_get_i64(const mpd_t *a, mpd_context_t *ctx); -uint64_t mpd_get_u64(const mpd_t *a, mpd_context_t *ctx); -#endif -void mpd_and(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_copy(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_canonical(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_copy_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_copy_negate(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_copy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_invert(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_logb(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_or(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_rotate(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_scaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_shiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); -mpd_uint_t mpd_shiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); -void mpd_shiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); -void mpd_shift(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_xor(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -int mpd_cmp(const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -int mpd_compare(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -int mpd_compare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_add(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_add_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); -void mpd_add_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); -void mpd_add_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); -void mpd_add_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); -void mpd_sub(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_sub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); -void mpd_sub_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); -void mpd_sub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); -void mpd_sub_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); -void mpd_div(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_div_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); -void mpd_div_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); -void mpd_div_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); -void mpd_div_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); -void mpd_divmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_divint(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_exp(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_fma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, mpd_context_t *ctx); -void mpd_ln(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_log10(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_max(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_max_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_min(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_min_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_mul(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_mul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); -void mpd_mul_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); -void mpd_mul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); -void mpd_mul_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); -void mpd_next_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_next_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_next_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_pow(mpd_t *result, const mpd_t *base, const mpd_t *exp, mpd_context_t *ctx); -void mpd_powmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_t *mod, mpd_context_t *ctx); -void mpd_quantize(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_rescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, mpd_context_t *ctx); -void mpd_reduce(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_rem(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_rem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); -void mpd_round_to_intx(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_round_to_int(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_trunc(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_floor(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_ceil(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_sqrt(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); -void mpd_invroot(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); - -#ifndef LEGACY_COMPILER -void mpd_add_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); -void mpd_add_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); -void mpd_sub_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); -void mpd_sub_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); -void mpd_div_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); -void mpd_div_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); -void mpd_mul_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); -void mpd_mul_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); -#endif - - -/******************************************************************************/ -/* Configuration specific */ -/******************************************************************************/ - -#ifdef CONFIG_64 -void mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status); -void mpd_sset_i64(mpd_t *result, int64_t a, mpd_context_t *ctx); -void mpd_sset_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx); -#endif - - -/******************************************************************************/ -/* Get attributes of a decimal */ -/******************************************************************************/ - -EXTINLINE mpd_ssize_t mpd_adjexp(const mpd_t *dec); -EXTINLINE mpd_ssize_t mpd_etiny(const mpd_context_t *ctx); -EXTINLINE mpd_ssize_t mpd_etop(const mpd_context_t *ctx); -EXTINLINE mpd_uint_t mpd_msword(const mpd_t *dec); -EXTINLINE int mpd_word_digits(mpd_uint_t word); -/* most significant digit of a word */ -EXTINLINE mpd_uint_t mpd_msd(mpd_uint_t word); -/* least significant digit of a word */ -EXTINLINE mpd_uint_t mpd_lsd(mpd_uint_t word); -/* coefficient size needed to store 'digits' */ -EXTINLINE mpd_ssize_t mpd_digits_to_size(mpd_ssize_t digits); -/* number of digits in the exponent, undefined for MPD_SSIZE_MIN */ -EXTINLINE int mpd_exp_digits(mpd_ssize_t exp); +mpd_t *mpd_qncopy(const mpd_t *a); +int mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status); +int mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status); +int mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status); + +void mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +int mpd_same_quantum(const mpd_t *a, const mpd_t *b); + +void mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +int mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status); +mpd_uint_t mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status); +mpd_uint_t mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n); +void mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx, uint32_t *status); + +int mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status); +int mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +int mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +int mpd_cmp_total(const mpd_t *a, const mpd_t *b); +int mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b); +int mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b); +int mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b); + +void mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); + +void mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status); +void mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status); +void mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status); +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); +void mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_context_t *ctx, uint32_t *status); +void mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_t *mod, const mpd_context_t *ctx, uint32_t *status); +void mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status); +void mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status); + +#ifndef LEGACY_COMPILER +void mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status); +void mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status); +#endif + + +size_t mpd_sizeinbase(const mpd_t *a, uint32_t base); +void mpd_qimport_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen, + uint8_t srcsign, uint32_t srcbase, + const mpd_context_t *ctx, uint32_t *status); +void mpd_qimport_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen, + uint8_t srcsign, uint32_t srcbase, + const mpd_context_t *ctx, uint32_t *status); +size_t mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t base, + const mpd_t *src, uint32_t *status); +size_t mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t base, + const mpd_t *src, uint32_t *status); + + +/******************************************************************************/ +/* Signalling functions */ +/******************************************************************************/ + +char *mpd_format(const mpd_t *dec, const char *fmt, mpd_context_t *ctx); +void mpd_import_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx); +void mpd_import_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx); +size_t mpd_export_u16(uint16_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx); +size_t mpd_export_u32(uint32_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx); +void mpd_finalize(mpd_t *result, mpd_context_t *ctx); +int mpd_check_nan(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +int mpd_check_nans(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_set_string(mpd_t *result, const char *s, mpd_context_t *ctx); +void mpd_maxcoeff(mpd_t *result, mpd_context_t *ctx); +void mpd_sset_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx); +void mpd_sset_i32(mpd_t *result, int32_t a, mpd_context_t *ctx); +void mpd_sset_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx); +void mpd_sset_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx); +void mpd_set_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx); +void mpd_set_i32(mpd_t *result, int32_t a, mpd_context_t *ctx); +void mpd_set_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx); +void mpd_set_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx); +#ifndef LEGACY_COMPILER +void mpd_set_i64(mpd_t *result, int64_t a, mpd_context_t *ctx); +void mpd_set_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx); +#endif +mpd_ssize_t mpd_get_ssize(const mpd_t *a, mpd_context_t *ctx); +mpd_uint_t mpd_get_uint(const mpd_t *a, mpd_context_t *ctx); +mpd_uint_t mpd_abs_uint(const mpd_t *a, mpd_context_t *ctx); +int32_t mpd_get_i32(const mpd_t *a, mpd_context_t *ctx); +uint32_t mpd_get_u32(const mpd_t *a, mpd_context_t *ctx); +#ifndef LEGACY_COMPILER +int64_t mpd_get_i64(const mpd_t *a, mpd_context_t *ctx); +uint64_t mpd_get_u64(const mpd_t *a, mpd_context_t *ctx); +#endif +void mpd_and(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_copy(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_canonical(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_copy_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_copy_negate(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_copy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_invert(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_logb(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_or(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_rotate(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_scaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_shiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); +mpd_uint_t mpd_shiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); +void mpd_shiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx); +void mpd_shift(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_xor(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +int mpd_cmp(const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +int mpd_compare(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +int mpd_compare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_add(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_add_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); +void mpd_add_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); +void mpd_add_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); +void mpd_add_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); +void mpd_sub(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_sub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); +void mpd_sub_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); +void mpd_sub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); +void mpd_sub_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); +void mpd_div(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_div_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); +void mpd_div_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); +void mpd_div_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); +void mpd_div_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); +void mpd_divmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_divint(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_exp(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_fma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, mpd_context_t *ctx); +void mpd_ln(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_log10(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_max(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_max_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_min(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_min_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_mul(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_mul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx); +void mpd_mul_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx); +void mpd_mul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx); +void mpd_mul_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx); +void mpd_next_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_next_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_next_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_pow(mpd_t *result, const mpd_t *base, const mpd_t *exp, mpd_context_t *ctx); +void mpd_powmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_t *mod, mpd_context_t *ctx); +void mpd_quantize(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_rescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, mpd_context_t *ctx); +void mpd_reduce(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_rem(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_rem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx); +void mpd_round_to_intx(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_round_to_int(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_trunc(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_floor(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_ceil(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_sqrt(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); +void mpd_invroot(mpd_t *result, const mpd_t *a, mpd_context_t *ctx); + +#ifndef LEGACY_COMPILER +void mpd_add_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); +void mpd_add_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); +void mpd_sub_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); +void mpd_sub_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); +void mpd_div_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); +void mpd_div_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); +void mpd_mul_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx); +void mpd_mul_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx); +#endif + + +/******************************************************************************/ +/* Configuration specific */ +/******************************************************************************/ + +#ifdef CONFIG_64 +void mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status); +void mpd_sset_i64(mpd_t *result, int64_t a, mpd_context_t *ctx); +void mpd_sset_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx); +#endif + + +/******************************************************************************/ +/* Get attributes of a decimal */ +/******************************************************************************/ + +EXTINLINE mpd_ssize_t mpd_adjexp(const mpd_t *dec); +EXTINLINE mpd_ssize_t mpd_etiny(const mpd_context_t *ctx); +EXTINLINE mpd_ssize_t mpd_etop(const mpd_context_t *ctx); +EXTINLINE mpd_uint_t mpd_msword(const mpd_t *dec); +EXTINLINE int mpd_word_digits(mpd_uint_t word); +/* most significant digit of a word */ +EXTINLINE mpd_uint_t mpd_msd(mpd_uint_t word); +/* least significant digit of a word */ +EXTINLINE mpd_uint_t mpd_lsd(mpd_uint_t word); +/* coefficient size needed to store 'digits' */ +EXTINLINE mpd_ssize_t mpd_digits_to_size(mpd_ssize_t digits); +/* number of digits in the exponent, undefined for MPD_SSIZE_MIN */ +EXTINLINE int mpd_exp_digits(mpd_ssize_t exp); EXTINLINE int mpd_iscanonical(const mpd_t *dec); -EXTINLINE int mpd_isfinite(const mpd_t *dec); -EXTINLINE int mpd_isinfinite(const mpd_t *dec); -EXTINLINE int mpd_isinteger(const mpd_t *dec); -EXTINLINE int mpd_isnan(const mpd_t *dec); -EXTINLINE int mpd_isnegative(const mpd_t *dec); -EXTINLINE int mpd_ispositive(const mpd_t *dec); -EXTINLINE int mpd_isqnan(const mpd_t *dec); -EXTINLINE int mpd_issigned(const mpd_t *dec); -EXTINLINE int mpd_issnan(const mpd_t *dec); -EXTINLINE int mpd_isspecial(const mpd_t *dec); -EXTINLINE int mpd_iszero(const mpd_t *dec); -/* undefined for special numbers */ -EXTINLINE int mpd_iszerocoeff(const mpd_t *dec); -EXTINLINE int mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx); -EXTINLINE int mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx); -/* odd word */ -EXTINLINE int mpd_isoddword(mpd_uint_t word); -/* odd coefficient */ -EXTINLINE int mpd_isoddcoeff(const mpd_t *dec); -/* odd decimal, only defined for integers */ -int mpd_isodd(const mpd_t *dec); -/* even decimal, only defined for integers */ -int mpd_iseven(const mpd_t *dec); -/* 0 if dec is positive, 1 if dec is negative */ -EXTINLINE uint8_t mpd_sign(const mpd_t *dec); -/* 1 if dec is positive, -1 if dec is negative */ -EXTINLINE int mpd_arith_sign(const mpd_t *dec); -EXTINLINE long mpd_radix(void); -EXTINLINE int mpd_isdynamic(const mpd_t *dec); -EXTINLINE int mpd_isstatic(const mpd_t *dec); -EXTINLINE int mpd_isdynamic_data(const mpd_t *dec); -EXTINLINE int mpd_isstatic_data(const mpd_t *dec); -EXTINLINE int mpd_isshared_data(const mpd_t *dec); -EXTINLINE int mpd_isconst_data(const mpd_t *dec); -EXTINLINE mpd_ssize_t mpd_trail_zeros(const mpd_t *dec); - - -/******************************************************************************/ -/* Set attributes of a decimal */ -/******************************************************************************/ - -/* set number of decimal digits in the coefficient */ -EXTINLINE void mpd_setdigits(mpd_t *result); -EXTINLINE void mpd_set_sign(mpd_t *result, uint8_t sign); -/* copy sign from another decimal */ -EXTINLINE void mpd_signcpy(mpd_t *result, const mpd_t *a); -EXTINLINE void mpd_set_infinity(mpd_t *result); -EXTINLINE void mpd_set_qnan(mpd_t *result); -EXTINLINE void mpd_set_snan(mpd_t *result); -EXTINLINE void mpd_set_negative(mpd_t *result); -EXTINLINE void mpd_set_positive(mpd_t *result); -EXTINLINE void mpd_set_dynamic(mpd_t *result); -EXTINLINE void mpd_set_static(mpd_t *result); -EXTINLINE void mpd_set_dynamic_data(mpd_t *result); -EXTINLINE void mpd_set_static_data(mpd_t *result); -EXTINLINE void mpd_set_shared_data(mpd_t *result); -EXTINLINE void mpd_set_const_data(mpd_t *result); -EXTINLINE void mpd_clear_flags(mpd_t *result); -EXTINLINE void mpd_set_flags(mpd_t *result, uint8_t flags); -EXTINLINE void mpd_copy_flags(mpd_t *result, const mpd_t *a); - - -/******************************************************************************/ -/* Error Macros */ -/******************************************************************************/ - -#define mpd_err_fatal(...) \ - do {fprintf(stderr, "%s:%d: error: ", __FILE__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); \ - abort(); \ - } while (0) -#define mpd_err_warn(...) \ - do {fprintf(stderr, "%s:%d: warning: ", __FILE__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); \ - } while (0) - - -/******************************************************************************/ -/* Memory handling */ -/******************************************************************************/ - -extern void *(* mpd_mallocfunc)(size_t size); -extern void *(* mpd_callocfunc)(size_t nmemb, size_t size); -extern void *(* mpd_reallocfunc)(void *ptr, size_t size); -extern void (* mpd_free)(void *ptr); - -void *mpd_callocfunc_em(size_t nmemb, size_t size); - -void *mpd_alloc(mpd_size_t nmemb, mpd_size_t size); -void *mpd_calloc(mpd_size_t nmemb, mpd_size_t size); -void *mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err); -void *mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size); - -mpd_t *mpd_qnew(void); -mpd_t *mpd_new(mpd_context_t *ctx); -mpd_t *mpd_qnew_size(mpd_ssize_t size); -EXTINLINE void mpd_del(mpd_t *dec); - -EXTINLINE void mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len); -EXTINLINE int mpd_qresize(mpd_t *result, mpd_ssize_t size, uint32_t *status); -EXTINLINE int mpd_qresize_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status); -EXTINLINE void mpd_minalloc(mpd_t *result); - -int mpd_resize(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx); -int mpd_resize_zero(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - -#ifdef __cplusplus -} /* END extern "C" */ -#endif - - +EXTINLINE int mpd_isfinite(const mpd_t *dec); +EXTINLINE int mpd_isinfinite(const mpd_t *dec); +EXTINLINE int mpd_isinteger(const mpd_t *dec); +EXTINLINE int mpd_isnan(const mpd_t *dec); +EXTINLINE int mpd_isnegative(const mpd_t *dec); +EXTINLINE int mpd_ispositive(const mpd_t *dec); +EXTINLINE int mpd_isqnan(const mpd_t *dec); +EXTINLINE int mpd_issigned(const mpd_t *dec); +EXTINLINE int mpd_issnan(const mpd_t *dec); +EXTINLINE int mpd_isspecial(const mpd_t *dec); +EXTINLINE int mpd_iszero(const mpd_t *dec); +/* undefined for special numbers */ +EXTINLINE int mpd_iszerocoeff(const mpd_t *dec); +EXTINLINE int mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx); +EXTINLINE int mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx); +/* odd word */ +EXTINLINE int mpd_isoddword(mpd_uint_t word); +/* odd coefficient */ +EXTINLINE int mpd_isoddcoeff(const mpd_t *dec); +/* odd decimal, only defined for integers */ +int mpd_isodd(const mpd_t *dec); +/* even decimal, only defined for integers */ +int mpd_iseven(const mpd_t *dec); +/* 0 if dec is positive, 1 if dec is negative */ +EXTINLINE uint8_t mpd_sign(const mpd_t *dec); +/* 1 if dec is positive, -1 if dec is negative */ +EXTINLINE int mpd_arith_sign(const mpd_t *dec); +EXTINLINE long mpd_radix(void); +EXTINLINE int mpd_isdynamic(const mpd_t *dec); +EXTINLINE int mpd_isstatic(const mpd_t *dec); +EXTINLINE int mpd_isdynamic_data(const mpd_t *dec); +EXTINLINE int mpd_isstatic_data(const mpd_t *dec); +EXTINLINE int mpd_isshared_data(const mpd_t *dec); +EXTINLINE int mpd_isconst_data(const mpd_t *dec); +EXTINLINE mpd_ssize_t mpd_trail_zeros(const mpd_t *dec); + + +/******************************************************************************/ +/* Set attributes of a decimal */ +/******************************************************************************/ + +/* set number of decimal digits in the coefficient */ +EXTINLINE void mpd_setdigits(mpd_t *result); +EXTINLINE void mpd_set_sign(mpd_t *result, uint8_t sign); +/* copy sign from another decimal */ +EXTINLINE void mpd_signcpy(mpd_t *result, const mpd_t *a); +EXTINLINE void mpd_set_infinity(mpd_t *result); +EXTINLINE void mpd_set_qnan(mpd_t *result); +EXTINLINE void mpd_set_snan(mpd_t *result); +EXTINLINE void mpd_set_negative(mpd_t *result); +EXTINLINE void mpd_set_positive(mpd_t *result); +EXTINLINE void mpd_set_dynamic(mpd_t *result); +EXTINLINE void mpd_set_static(mpd_t *result); +EXTINLINE void mpd_set_dynamic_data(mpd_t *result); +EXTINLINE void mpd_set_static_data(mpd_t *result); +EXTINLINE void mpd_set_shared_data(mpd_t *result); +EXTINLINE void mpd_set_const_data(mpd_t *result); +EXTINLINE void mpd_clear_flags(mpd_t *result); +EXTINLINE void mpd_set_flags(mpd_t *result, uint8_t flags); +EXTINLINE void mpd_copy_flags(mpd_t *result, const mpd_t *a); + + +/******************************************************************************/ +/* Error Macros */ +/******************************************************************************/ + +#define mpd_err_fatal(...) \ + do {fprintf(stderr, "%s:%d: error: ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); \ + abort(); \ + } while (0) +#define mpd_err_warn(...) \ + do {fprintf(stderr, "%s:%d: warning: ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); \ + } while (0) + + +/******************************************************************************/ +/* Memory handling */ +/******************************************************************************/ + +extern void *(* mpd_mallocfunc)(size_t size); +extern void *(* mpd_callocfunc)(size_t nmemb, size_t size); +extern void *(* mpd_reallocfunc)(void *ptr, size_t size); +extern void (* mpd_free)(void *ptr); + +void *mpd_callocfunc_em(size_t nmemb, size_t size); + +void *mpd_alloc(mpd_size_t nmemb, mpd_size_t size); +void *mpd_calloc(mpd_size_t nmemb, mpd_size_t size); +void *mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err); +void *mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size); + +mpd_t *mpd_qnew(void); +mpd_t *mpd_new(mpd_context_t *ctx); +mpd_t *mpd_qnew_size(mpd_ssize_t size); +EXTINLINE void mpd_del(mpd_t *dec); + +EXTINLINE void mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len); +EXTINLINE int mpd_qresize(mpd_t *result, mpd_ssize_t size, uint32_t *status); +EXTINLINE int mpd_qresize_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status); +EXTINLINE void mpd_minalloc(mpd_t *result); + +int mpd_resize(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx); +int mpd_resize_zero(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + +#ifdef __cplusplus +} /* END extern "C" */ +#endif + + #endif /* LIBMPDEC_MPDECIMAL_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.c index 210e0deb371..8778fbb8f72 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.c @@ -1,132 +1,132 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> -#include <stdlib.h> +#include <stdlib.h> -#include "bits.h" +#include "bits.h" #include "numbertheory.h" -#include "umodarith.h" - - -/* Bignum: Initialize the Number Theoretic Transform. */ - - -/* - * Return the nth root of unity in F(p). This corresponds to e**((2*pi*i)/n) - * in the Fourier transform. We have w**n == 1 (mod p). - * n := transform length. - * sign := -1 for forward transform, 1 for backward transform. - * modnum := one of {P1, P2, P3}. - */ -mpd_uint_t -_mpd_getkernel(mpd_uint_t n, int sign, int modnum) -{ - mpd_uint_t umod, p, r, xi; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - - SETMODULUS(modnum); - r = mpd_roots[modnum]; /* primitive root of F(p) */ - p = umod; - xi = (p-1) / n; - - if (sign == -1) - return POWMOD(r, (p-1-xi)); - else - return POWMOD(r, xi); -} - -/* - * Initialize and return transform parameters. - * n := transform length. - * sign := -1 for forward transform, 1 for backward transform. - * modnum := one of {P1, P2, P3}. - */ -struct fnt_params * -_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum) -{ - struct fnt_params *tparams; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t kernel, w; - mpd_uint_t i; - mpd_size_t nhalf; - - assert(ispower2(n)); - assert(sign == -1 || sign == 1); - assert(P1 <= modnum && modnum <= P3); - - nhalf = n/2; - tparams = mpd_sh_alloc(sizeof *tparams, nhalf, sizeof (mpd_uint_t)); - if (tparams == NULL) { - return NULL; - } - - SETMODULUS(modnum); - kernel = _mpd_getkernel(n, sign, modnum); - - tparams->modnum = modnum; - tparams->modulus = umod; - tparams->kernel = kernel; - - /* wtable[] := w**0, w**1, ..., w**(nhalf-1) */ - w = 1; - for (i = 0; i < nhalf; i++) { - tparams->wtable[i] = w; - w = MULMOD(w, kernel); - } - - return tparams; -} - -/* Initialize wtable of size three. */ -void -_mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum) -{ - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t kernel; - - SETMODULUS(modnum); - kernel = _mpd_getkernel(3, sign, modnum); - - w3table[0] = 1; - w3table[1] = kernel; - w3table[2] = POWMOD(kernel, 2); -} +#include "umodarith.h" + + +/* Bignum: Initialize the Number Theoretic Transform. */ + + +/* + * Return the nth root of unity in F(p). This corresponds to e**((2*pi*i)/n) + * in the Fourier transform. We have w**n == 1 (mod p). + * n := transform length. + * sign := -1 for forward transform, 1 for backward transform. + * modnum := one of {P1, P2, P3}. + */ +mpd_uint_t +_mpd_getkernel(mpd_uint_t n, int sign, int modnum) +{ + mpd_uint_t umod, p, r, xi; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + + SETMODULUS(modnum); + r = mpd_roots[modnum]; /* primitive root of F(p) */ + p = umod; + xi = (p-1) / n; + + if (sign == -1) + return POWMOD(r, (p-1-xi)); + else + return POWMOD(r, xi); +} + +/* + * Initialize and return transform parameters. + * n := transform length. + * sign := -1 for forward transform, 1 for backward transform. + * modnum := one of {P1, P2, P3}. + */ +struct fnt_params * +_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum) +{ + struct fnt_params *tparams; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t kernel, w; + mpd_uint_t i; + mpd_size_t nhalf; + + assert(ispower2(n)); + assert(sign == -1 || sign == 1); + assert(P1 <= modnum && modnum <= P3); + + nhalf = n/2; + tparams = mpd_sh_alloc(sizeof *tparams, nhalf, sizeof (mpd_uint_t)); + if (tparams == NULL) { + return NULL; + } + + SETMODULUS(modnum); + kernel = _mpd_getkernel(n, sign, modnum); + + tparams->modnum = modnum; + tparams->modulus = umod; + tparams->kernel = kernel; + + /* wtable[] := w**0, w**1, ..., w**(nhalf-1) */ + w = 1; + for (i = 0; i < nhalf; i++) { + tparams->wtable[i] = w; + w = MULMOD(w, kernel); + } + + return tparams; +} + +/* Initialize wtable of size three. */ +void +_mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum) +{ + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t kernel; + + SETMODULUS(modnum); + kernel = _mpd_getkernel(3, sign, modnum); + + w3table[0] = 1; + w3table[1] = kernel; + w3table[2] = POWMOD(kernel, 2); +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.h index 47b7753b831..280dcb8c727 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/numbertheory.h @@ -1,76 +1,76 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_NUMBERTHEORY_H_ #define LIBMPDEC_NUMBERTHEORY_H_ - - + + #include "mpdecimal.h" -#include "constants.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -/* transform parameters */ -struct fnt_params { - int modnum; - mpd_uint_t modulus; - mpd_uint_t kernel; - mpd_uint_t wtable[]; -}; - - -mpd_uint_t _mpd_getkernel(mpd_uint_t n, int sign, int modnum); -struct fnt_params *_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum); -void _mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum); - - -#ifdef PPRO -static inline void -ppro_setmodulus(int modnum, mpd_uint_t *umod, double *dmod, uint32_t dinvmod[3]) -{ - *dmod = *umod = mpd_moduli[modnum]; - dinvmod[0] = mpd_invmoduli[modnum][0]; - dinvmod[1] = mpd_invmoduli[modnum][1]; - dinvmod[2] = mpd_invmoduli[modnum][2]; -} -#else -static inline void -std_setmodulus(int modnum, mpd_uint_t *umod) -{ - *umod = mpd_moduli[modnum]; -} -#endif - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - +#include "constants.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +/* transform parameters */ +struct fnt_params { + int modnum; + mpd_uint_t modulus; + mpd_uint_t kernel; + mpd_uint_t wtable[]; +}; + + +mpd_uint_t _mpd_getkernel(mpd_uint_t n, int sign, int modnum); +struct fnt_params *_mpd_init_fnt_params(mpd_size_t n, int sign, int modnum); +void _mpd_init_w3table(mpd_uint_t w3table[3], int sign, int modnum); + + +#ifdef PPRO +static inline void +ppro_setmodulus(int modnum, mpd_uint_t *umod, double *dmod, uint32_t dinvmod[3]) +{ + *dmod = *umod = mpd_moduli[modnum]; + dinvmod[0] = mpd_invmoduli[modnum][0]; + dinvmod[1] = mpd_invmoduli[modnum][1]; + dinvmod[2] = mpd_invmoduli[modnum][2]; +} +#else +static inline void +std_setmodulus(int modnum, mpd_uint_t *umod) +{ + *umod = mpd_moduli[modnum]; +} +#endif + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_NUMBERTHEORY_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.c index a4d1dbed781..5dab2a5c0ca 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.c @@ -1,214 +1,214 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> -#include <stdio.h> +#include <stdio.h> -#include "bits.h" +#include "bits.h" #include "constants.h" -#include "difradix2.h" -#include "numbertheory.h" +#include "difradix2.h" +#include "numbertheory.h" #include "sixstep.h" -#include "transpose.h" -#include "umodarith.h" - - -/* Bignum: Cache efficient Matrix Fourier Transform for arrays of the - form 2**n (See literature/six-step.txt). */ - - -/* forward transform with sign = -1 */ -int -six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - struct fnt_params *tparams; - mpd_size_t log2n, C, R; - mpd_uint_t kernel; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t *x, w0, w1, wstep; - mpd_size_t i, k; - - - assert(ispower2(n)); - assert(n >= 16); - assert(n <= MPD_MAXTRANSFORM_2N); - - log2n = mpd_bsr(n); - C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */ - R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */ - - - /* Transpose the matrix. */ - if (!transpose_pow2(a, R, C)) { - return 0; - } - - /* Length R transform on the rows. */ - if ((tparams = _mpd_init_fnt_params(R, -1, modnum)) == NULL) { - return 0; - } - for (x = a; x < a+n; x += R) { - fnt_dif2(x, R, tparams); - } - - /* Transpose the matrix. */ - if (!transpose_pow2(a, C, R)) { - mpd_free(tparams); - return 0; - } - - /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ - SETMODULUS(modnum); - kernel = _mpd_getkernel(n, -1, modnum); - for (i = 1; i < R; i++) { - w0 = 1; /* r**(i*0): initial value for k=0 */ - w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */ - wstep = MULMOD(w1, w1); /* r**(2*i) */ - for (k = 0; k < C; k += 2) { - mpd_uint_t x0 = a[i*C+k]; - mpd_uint_t x1 = a[i*C+k+1]; - MULMOD2(&x0, w0, &x1, w1); - MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */ - a[i*C+k] = x0; - a[i*C+k+1] = x1; - } - } - - /* Length C transform on the rows. */ - if (C != R) { - mpd_free(tparams); - if ((tparams = _mpd_init_fnt_params(C, -1, modnum)) == NULL) { - return 0; - } - } - for (x = a; x < a+n; x += C) { - fnt_dif2(x, C, tparams); - } - mpd_free(tparams); - -#if 0 - /* An unordered transform is sufficient for convolution. */ - /* Transpose the matrix. */ - if (!transpose_pow2(a, R, C)) { - return 0; - } -#endif - - return 1; -} - - -/* reverse transform, sign = 1 */ -int -inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) -{ - struct fnt_params *tparams; - mpd_size_t log2n, C, R; - mpd_uint_t kernel; - mpd_uint_t umod; -#ifdef PPRO - double dmod; - uint32_t dinvmod[3]; -#endif - mpd_uint_t *x, w0, w1, wstep; - mpd_size_t i, k; - - - assert(ispower2(n)); - assert(n >= 16); - assert(n <= MPD_MAXTRANSFORM_2N); - - log2n = mpd_bsr(n); - C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */ - R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */ - - -#if 0 - /* An unordered transform is sufficient for convolution. */ - /* Transpose the matrix, producing an R*C matrix. */ - if (!transpose_pow2(a, C, R)) { - return 0; - } -#endif - - /* Length C transform on the rows. */ - if ((tparams = _mpd_init_fnt_params(C, 1, modnum)) == NULL) { - return 0; - } - for (x = a; x < a+n; x += C) { - fnt_dif2(x, C, tparams); - } - - /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ - SETMODULUS(modnum); - kernel = _mpd_getkernel(n, 1, modnum); - for (i = 1; i < R; i++) { - w0 = 1; - w1 = POWMOD(kernel, i); - wstep = MULMOD(w1, w1); - for (k = 0; k < C; k += 2) { - mpd_uint_t x0 = a[i*C+k]; - mpd_uint_t x1 = a[i*C+k+1]; - MULMOD2(&x0, w0, &x1, w1); - MULMOD2C(&w0, &w1, wstep); - a[i*C+k] = x0; - a[i*C+k+1] = x1; - } - } - - /* Transpose the matrix. */ - if (!transpose_pow2(a, R, C)) { - mpd_free(tparams); - return 0; - } - - /* Length R transform on the rows. */ - if (R != C) { - mpd_free(tparams); - if ((tparams = _mpd_init_fnt_params(R, 1, modnum)) == NULL) { - return 0; - } - } - for (x = a; x < a+n; x += R) { - fnt_dif2(x, R, tparams); - } - mpd_free(tparams); - - /* Transpose the matrix. */ - if (!transpose_pow2(a, C, R)) { - return 0; - } - - return 1; -} +#include "transpose.h" +#include "umodarith.h" + + +/* Bignum: Cache efficient Matrix Fourier Transform for arrays of the + form 2**n (See literature/six-step.txt). */ + + +/* forward transform with sign = -1 */ +int +six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + struct fnt_params *tparams; + mpd_size_t log2n, C, R; + mpd_uint_t kernel; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t *x, w0, w1, wstep; + mpd_size_t i, k; + + + assert(ispower2(n)); + assert(n >= 16); + assert(n <= MPD_MAXTRANSFORM_2N); + + log2n = mpd_bsr(n); + C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */ + R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */ + + + /* Transpose the matrix. */ + if (!transpose_pow2(a, R, C)) { + return 0; + } + + /* Length R transform on the rows. */ + if ((tparams = _mpd_init_fnt_params(R, -1, modnum)) == NULL) { + return 0; + } + for (x = a; x < a+n; x += R) { + fnt_dif2(x, R, tparams); + } + + /* Transpose the matrix. */ + if (!transpose_pow2(a, C, R)) { + mpd_free(tparams); + return 0; + } + + /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ + SETMODULUS(modnum); + kernel = _mpd_getkernel(n, -1, modnum); + for (i = 1; i < R; i++) { + w0 = 1; /* r**(i*0): initial value for k=0 */ + w1 = POWMOD(kernel, i); /* r**(i*1): initial value for k=1 */ + wstep = MULMOD(w1, w1); /* r**(2*i) */ + for (k = 0; k < C; k += 2) { + mpd_uint_t x0 = a[i*C+k]; + mpd_uint_t x1 = a[i*C+k+1]; + MULMOD2(&x0, w0, &x1, w1); + MULMOD2C(&w0, &w1, wstep); /* r**(i*(k+2)) = r**(i*k) * r**(2*i) */ + a[i*C+k] = x0; + a[i*C+k+1] = x1; + } + } + + /* Length C transform on the rows. */ + if (C != R) { + mpd_free(tparams); + if ((tparams = _mpd_init_fnt_params(C, -1, modnum)) == NULL) { + return 0; + } + } + for (x = a; x < a+n; x += C) { + fnt_dif2(x, C, tparams); + } + mpd_free(tparams); + +#if 0 + /* An unordered transform is sufficient for convolution. */ + /* Transpose the matrix. */ + if (!transpose_pow2(a, R, C)) { + return 0; + } +#endif + + return 1; +} + + +/* reverse transform, sign = 1 */ +int +inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum) +{ + struct fnt_params *tparams; + mpd_size_t log2n, C, R; + mpd_uint_t kernel; + mpd_uint_t umod; +#ifdef PPRO + double dmod; + uint32_t dinvmod[3]; +#endif + mpd_uint_t *x, w0, w1, wstep; + mpd_size_t i, k; + + + assert(ispower2(n)); + assert(n >= 16); + assert(n <= MPD_MAXTRANSFORM_2N); + + log2n = mpd_bsr(n); + C = ((mpd_size_t)1) << (log2n / 2); /* number of columns */ + R = ((mpd_size_t)1) << (log2n - (log2n / 2)); /* number of rows */ + + +#if 0 + /* An unordered transform is sufficient for convolution. */ + /* Transpose the matrix, producing an R*C matrix. */ + if (!transpose_pow2(a, C, R)) { + return 0; + } +#endif + + /* Length C transform on the rows. */ + if ((tparams = _mpd_init_fnt_params(C, 1, modnum)) == NULL) { + return 0; + } + for (x = a; x < a+n; x += C) { + fnt_dif2(x, C, tparams); + } + + /* Multiply each matrix element (addressed by i*C+k) by r**(i*k). */ + SETMODULUS(modnum); + kernel = _mpd_getkernel(n, 1, modnum); + for (i = 1; i < R; i++) { + w0 = 1; + w1 = POWMOD(kernel, i); + wstep = MULMOD(w1, w1); + for (k = 0; k < C; k += 2) { + mpd_uint_t x0 = a[i*C+k]; + mpd_uint_t x1 = a[i*C+k+1]; + MULMOD2(&x0, w0, &x1, w1); + MULMOD2C(&w0, &w1, wstep); + a[i*C+k] = x0; + a[i*C+k+1] = x1; + } + } + + /* Transpose the matrix. */ + if (!transpose_pow2(a, R, C)) { + mpd_free(tparams); + return 0; + } + + /* Length R transform on the rows. */ + if (R != C) { + mpd_free(tparams); + if ((tparams = _mpd_init_fnt_params(R, 1, modnum)) == NULL) { + return 0; + } + } + for (x = a; x < a+n; x += R) { + fnt_dif2(x, R, tparams); + } + mpd_free(tparams); + + /* Transpose the matrix. */ + if (!transpose_pow2(a, C, R)) { + return 0; + } + + return 1; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.h index 89b4a33afc7..0e72f3b5207 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/sixstep.h @@ -1,47 +1,47 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_SIXSTEP_H_ #define LIBMPDEC_SIXSTEP_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -int six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); -int inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +int six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); +int inv_six_step_fnt(mpd_uint_t *a, mpd_size_t n, int modnum); + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_SIXSTEP_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.c b/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.c index 56321b5f39a..c79c34948a2 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.c +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.c @@ -1,276 +1,276 @@ -/* +/* * Copyright (c) 2008-2020 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" + * + * 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 <assert.h> #include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> -#include "bits.h" -#include "constants.h" +#include "bits.h" +#include "constants.h" #include "transpose.h" -#include "typearith.h" - - -#define BUFSIZE 4096 -#define SIDE 128 - - -/* Bignum: The transpose functions are used for very large transforms - in sixstep.c and fourstep.c. */ - - -/* Definition of the matrix transpose */ -void -std_trans(mpd_uint_t dest[], mpd_uint_t src[], mpd_size_t rows, mpd_size_t cols) -{ - mpd_size_t idest, isrc; - mpd_size_t r, c; - - for (r = 0; r < rows; r++) { - isrc = r * cols; - idest = r; - for (c = 0; c < cols; c++) { - dest[idest] = src[isrc]; - isrc += 1; - idest += rows; - } - } -} - -/* - * Swap half-rows of 2^n * (2*2^n) matrix. - * FORWARD_CYCLE: even/odd permutation of the halfrows. - * BACKWARD_CYCLE: reverse the even/odd permutation. - */ -static int -swap_halfrows_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols, int dir) -{ - mpd_uint_t buf1[BUFSIZE]; - mpd_uint_t buf2[BUFSIZE]; - mpd_uint_t *readbuf, *writebuf, *hp; - mpd_size_t *done, dbits; - mpd_size_t b = BUFSIZE, stride; - mpd_size_t hn, hmax; /* halfrow number */ - mpd_size_t m, r=0; - mpd_size_t offset; - mpd_size_t next; - - - assert(cols == mul_size_t(2, rows)); - - if (dir == FORWARD_CYCLE) { - r = rows; - } - else if (dir == BACKWARD_CYCLE) { - r = 2; - } - else { - abort(); /* GCOV_NOT_REACHED */ - } - - m = cols - 1; - hmax = rows; /* cycles start at odd halfrows */ - dbits = 8 * sizeof *done; - if ((done = mpd_calloc(hmax/(sizeof *done) + 1, sizeof *done)) == NULL) { - return 0; - } - - for (hn = 1; hn <= hmax; hn += 2) { - - if (done[hn/dbits] & mpd_bits[hn%dbits]) { - continue; - } - - readbuf = buf1; writebuf = buf2; - - for (offset = 0; offset < cols/2; offset += b) { - - stride = (offset + b < cols/2) ? b : cols/2-offset; - - hp = matrix + hn*cols/2; - memcpy(readbuf, hp+offset, stride*(sizeof *readbuf)); - pointerswap(&readbuf, &writebuf); - - next = mulmod_size_t(hn, r, m); - hp = matrix + next*cols/2; - - while (next != hn) { - - memcpy(readbuf, hp+offset, stride*(sizeof *readbuf)); - memcpy(hp+offset, writebuf, stride*(sizeof *writebuf)); - pointerswap(&readbuf, &writebuf); - - done[next/dbits] |= mpd_bits[next%dbits]; - - next = mulmod_size_t(next, r, m); - hp = matrix + next*cols/2; - - } - - memcpy(hp+offset, writebuf, stride*(sizeof *writebuf)); - - done[hn/dbits] |= mpd_bits[hn%dbits]; - } - } - - mpd_free(done); - return 1; -} - -/* In-place transpose of a square matrix */ -static inline void -squaretrans(mpd_uint_t *buf, mpd_size_t cols) -{ - mpd_uint_t tmp; - mpd_size_t idest, isrc; - mpd_size_t r, c; - - for (r = 0; r < cols; r++) { - c = r+1; - isrc = r*cols + c; - idest = c*cols + r; - for (c = r+1; c < cols; c++) { - tmp = buf[isrc]; - buf[isrc] = buf[idest]; - buf[idest] = tmp; - isrc += 1; - idest += cols; - } - } -} - -/* - * Transpose 2^n * 2^n matrix. For cache efficiency, the matrix is split into - * square blocks with side length 'SIDE'. First, the blocks are transposed, - * then a square transposition is done on each individual block. - */ -static void -squaretrans_pow2(mpd_uint_t *matrix, mpd_size_t size) -{ - mpd_uint_t buf1[SIDE*SIDE]; - mpd_uint_t buf2[SIDE*SIDE]; - mpd_uint_t *to, *from; - mpd_size_t b = size; - mpd_size_t r, c; - mpd_size_t i; - - while (b > SIDE) b >>= 1; - - for (r = 0; r < size; r += b) { - - for (c = r; c < size; c += b) { - - from = matrix + r*size + c; - to = buf1; - for (i = 0; i < b; i++) { - memcpy(to, from, b*(sizeof *to)); - from += size; - to += b; - } - squaretrans(buf1, b); - - if (r == c) { - to = matrix + r*size + c; - from = buf1; - for (i = 0; i < b; i++) { - memcpy(to, from, b*(sizeof *to)); - from += b; - to += size; - } - continue; - } - else { - from = matrix + c*size + r; - to = buf2; - for (i = 0; i < b; i++) { - memcpy(to, from, b*(sizeof *to)); - from += size; - to += b; - } - squaretrans(buf2, b); - - to = matrix + c*size + r; - from = buf1; - for (i = 0; i < b; i++) { - memcpy(to, from, b*(sizeof *to)); - from += b; - to += size; - } - - to = matrix + r*size + c; - from = buf2; - for (i = 0; i < b; i++) { - memcpy(to, from, b*(sizeof *to)); - from += b; - to += size; - } - } - } - } - -} - -/* - * In-place transposition of a 2^n x 2^n or a 2^n x (2*2^n) - * or a (2*2^n) x 2^n matrix. - */ -int -transpose_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols) -{ - mpd_size_t size = mul_size_t(rows, cols); - - assert(ispower2(rows)); - assert(ispower2(cols)); - - if (cols == rows) { - squaretrans_pow2(matrix, rows); - } - else if (cols == mul_size_t(2, rows)) { - if (!swap_halfrows_pow2(matrix, rows, cols, FORWARD_CYCLE)) { - return 0; - } - squaretrans_pow2(matrix, rows); - squaretrans_pow2(matrix+(size/2), rows); - } - else if (rows == mul_size_t(2, cols)) { - squaretrans_pow2(matrix, cols); - squaretrans_pow2(matrix+(size/2), cols); - if (!swap_halfrows_pow2(matrix, cols, rows, BACKWARD_CYCLE)) { - return 0; - } - } - else { - abort(); /* GCOV_NOT_REACHED */ - } - - return 1; -} +#include "typearith.h" + + +#define BUFSIZE 4096 +#define SIDE 128 + + +/* Bignum: The transpose functions are used for very large transforms + in sixstep.c and fourstep.c. */ + + +/* Definition of the matrix transpose */ +void +std_trans(mpd_uint_t dest[], mpd_uint_t src[], mpd_size_t rows, mpd_size_t cols) +{ + mpd_size_t idest, isrc; + mpd_size_t r, c; + + for (r = 0; r < rows; r++) { + isrc = r * cols; + idest = r; + for (c = 0; c < cols; c++) { + dest[idest] = src[isrc]; + isrc += 1; + idest += rows; + } + } +} + +/* + * Swap half-rows of 2^n * (2*2^n) matrix. + * FORWARD_CYCLE: even/odd permutation of the halfrows. + * BACKWARD_CYCLE: reverse the even/odd permutation. + */ +static int +swap_halfrows_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols, int dir) +{ + mpd_uint_t buf1[BUFSIZE]; + mpd_uint_t buf2[BUFSIZE]; + mpd_uint_t *readbuf, *writebuf, *hp; + mpd_size_t *done, dbits; + mpd_size_t b = BUFSIZE, stride; + mpd_size_t hn, hmax; /* halfrow number */ + mpd_size_t m, r=0; + mpd_size_t offset; + mpd_size_t next; + + + assert(cols == mul_size_t(2, rows)); + + if (dir == FORWARD_CYCLE) { + r = rows; + } + else if (dir == BACKWARD_CYCLE) { + r = 2; + } + else { + abort(); /* GCOV_NOT_REACHED */ + } + + m = cols - 1; + hmax = rows; /* cycles start at odd halfrows */ + dbits = 8 * sizeof *done; + if ((done = mpd_calloc(hmax/(sizeof *done) + 1, sizeof *done)) == NULL) { + return 0; + } + + for (hn = 1; hn <= hmax; hn += 2) { + + if (done[hn/dbits] & mpd_bits[hn%dbits]) { + continue; + } + + readbuf = buf1; writebuf = buf2; + + for (offset = 0; offset < cols/2; offset += b) { + + stride = (offset + b < cols/2) ? b : cols/2-offset; + + hp = matrix + hn*cols/2; + memcpy(readbuf, hp+offset, stride*(sizeof *readbuf)); + pointerswap(&readbuf, &writebuf); + + next = mulmod_size_t(hn, r, m); + hp = matrix + next*cols/2; + + while (next != hn) { + + memcpy(readbuf, hp+offset, stride*(sizeof *readbuf)); + memcpy(hp+offset, writebuf, stride*(sizeof *writebuf)); + pointerswap(&readbuf, &writebuf); + + done[next/dbits] |= mpd_bits[next%dbits]; + + next = mulmod_size_t(next, r, m); + hp = matrix + next*cols/2; + + } + + memcpy(hp+offset, writebuf, stride*(sizeof *writebuf)); + + done[hn/dbits] |= mpd_bits[hn%dbits]; + } + } + + mpd_free(done); + return 1; +} + +/* In-place transpose of a square matrix */ +static inline void +squaretrans(mpd_uint_t *buf, mpd_size_t cols) +{ + mpd_uint_t tmp; + mpd_size_t idest, isrc; + mpd_size_t r, c; + + for (r = 0; r < cols; r++) { + c = r+1; + isrc = r*cols + c; + idest = c*cols + r; + for (c = r+1; c < cols; c++) { + tmp = buf[isrc]; + buf[isrc] = buf[idest]; + buf[idest] = tmp; + isrc += 1; + idest += cols; + } + } +} + +/* + * Transpose 2^n * 2^n matrix. For cache efficiency, the matrix is split into + * square blocks with side length 'SIDE'. First, the blocks are transposed, + * then a square transposition is done on each individual block. + */ +static void +squaretrans_pow2(mpd_uint_t *matrix, mpd_size_t size) +{ + mpd_uint_t buf1[SIDE*SIDE]; + mpd_uint_t buf2[SIDE*SIDE]; + mpd_uint_t *to, *from; + mpd_size_t b = size; + mpd_size_t r, c; + mpd_size_t i; + + while (b > SIDE) b >>= 1; + + for (r = 0; r < size; r += b) { + + for (c = r; c < size; c += b) { + + from = matrix + r*size + c; + to = buf1; + for (i = 0; i < b; i++) { + memcpy(to, from, b*(sizeof *to)); + from += size; + to += b; + } + squaretrans(buf1, b); + + if (r == c) { + to = matrix + r*size + c; + from = buf1; + for (i = 0; i < b; i++) { + memcpy(to, from, b*(sizeof *to)); + from += b; + to += size; + } + continue; + } + else { + from = matrix + c*size + r; + to = buf2; + for (i = 0; i < b; i++) { + memcpy(to, from, b*(sizeof *to)); + from += size; + to += b; + } + squaretrans(buf2, b); + + to = matrix + c*size + r; + from = buf1; + for (i = 0; i < b; i++) { + memcpy(to, from, b*(sizeof *to)); + from += b; + to += size; + } + + to = matrix + r*size + c; + from = buf2; + for (i = 0; i < b; i++) { + memcpy(to, from, b*(sizeof *to)); + from += b; + to += size; + } + } + } + } + +} + +/* + * In-place transposition of a 2^n x 2^n or a 2^n x (2*2^n) + * or a (2*2^n) x 2^n matrix. + */ +int +transpose_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols) +{ + mpd_size_t size = mul_size_t(rows, cols); + + assert(ispower2(rows)); + assert(ispower2(cols)); + + if (cols == rows) { + squaretrans_pow2(matrix, rows); + } + else if (cols == mul_size_t(2, rows)) { + if (!swap_halfrows_pow2(matrix, rows, cols, FORWARD_CYCLE)) { + return 0; + } + squaretrans_pow2(matrix, rows); + squaretrans_pow2(matrix+(size/2), rows); + } + else if (rows == mul_size_t(2, cols)) { + squaretrans_pow2(matrix, cols); + squaretrans_pow2(matrix+(size/2), cols); + if (!swap_halfrows_pow2(matrix, cols, rows, BACKWARD_CYCLE)) { + return 0; + } + } + else { + abort(); /* GCOV_NOT_REACHED */ + } + + return 1; +} diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.h index e91c18d7435..8e6ec56a282 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/transpose.h @@ -1,61 +1,61 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_TRANSPOSE_H_ #define LIBMPDEC_TRANSPOSE_H_ - - -#include "mpdecimal.h" - - -/* Internal header file: all symbols have local scope in the DSO */ -MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) - - -enum {FORWARD_CYCLE, BACKWARD_CYCLE}; - - -void std_trans(mpd_uint_t dest[], mpd_uint_t src[], mpd_size_t rows, mpd_size_t cols); -int transpose_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols); -void transpose_3xpow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols); - - -static inline void pointerswap(mpd_uint_t **a, mpd_uint_t **b) -{ - mpd_uint_t *tmp; - - tmp = *b; - *b = *a; - *a = tmp; -} - - -MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ - - + + +#include "mpdecimal.h" + + +/* Internal header file: all symbols have local scope in the DSO */ +MPD_PRAGMA(MPD_HIDE_SYMBOLS_START) + + +enum {FORWARD_CYCLE, BACKWARD_CYCLE}; + + +void std_trans(mpd_uint_t dest[], mpd_uint_t src[], mpd_size_t rows, mpd_size_t cols); +int transpose_pow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols); +void transpose_3xpow2(mpd_uint_t *matrix, mpd_size_t rows, mpd_size_t cols); + + +static inline void pointerswap(mpd_uint_t **a, mpd_uint_t **b) +{ + mpd_uint_t *tmp; + + tmp = *b; + *b = *a; + *a = tmp; +} + + +MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */ + + #endif /* LIBMPDEC_TRANSPOSE_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/typearith.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/typearith.h index 47961788d76..5c00d1e6b18 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/typearith.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/typearith.h @@ -1,668 +1,668 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_TYPEARITH_H_ #define LIBMPDEC_TYPEARITH_H_ - - -#include "mpdecimal.h" - + + +#include "mpdecimal.h" + #include <assert.h> + - -/*****************************************************************************/ -/* Low level native arithmetic on basic types */ -/*****************************************************************************/ - - -/** ------------------------------------------------------------ - ** Double width multiplication and division - ** ------------------------------------------------------------ - */ - -#if defined(CONFIG_64) -#if defined(ANSI) -#if defined(HAVE_UINT128_T) -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - __uint128_t hl; - - hl = (__uint128_t)a * b; - - *hi = hl >> 64; - *lo = (mpd_uint_t)hl; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d) -{ - __uint128_t hl; - - hl = ((__uint128_t)hi<<64) + lo; - *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */ - *r = (mpd_uint_t)(hl - (__uint128_t)(*q) * d); -} -#else -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - uint32_t w[4], carry; - uint32_t ah, al, bh, bl; - uint64_t hl; - - ah = (uint32_t)(a>>32); al = (uint32_t)a; - bh = (uint32_t)(b>>32); bl = (uint32_t)b; - - hl = (uint64_t)al * bl; - w[0] = (uint32_t)hl; - carry = (uint32_t)(hl>>32); - - hl = (uint64_t)ah * bl + carry; - w[1] = (uint32_t)hl; - w[2] = (uint32_t)(hl>>32); - - hl = (uint64_t)al * bh + w[1]; - w[1] = (uint32_t)hl; - carry = (uint32_t)(hl>>32); - - hl = ((uint64_t)ah * bh + w[2]) + carry; - w[2] = (uint32_t)hl; - w[3] = (uint32_t)(hl>>32); - - *hi = ((uint64_t)w[3]<<32) + w[2]; - *lo = ((uint64_t)w[1]<<32) + w[0]; -} - -/* - * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt - * http://www.hackersdelight.org/permissions.htm: - * "You are free to use, copy, and distribute any of the code on this web - * site, whether modified by you or not. You need not give attribution." - * - * Slightly modified, comments are mine. - */ -static inline int -nlz(uint64_t x) -{ - int n; - - if (x == 0) return(64); - - n = 0; - if (x <= 0x00000000FFFFFFFF) {n = n +32; x = x <<32;} - if (x <= 0x0000FFFFFFFFFFFF) {n = n +16; x = x <<16;} - if (x <= 0x00FFFFFFFFFFFFFF) {n = n + 8; x = x << 8;} - if (x <= 0x0FFFFFFFFFFFFFFF) {n = n + 4; x = x << 4;} - if (x <= 0x3FFFFFFFFFFFFFFF) {n = n + 2; x = x << 2;} - if (x <= 0x7FFFFFFFFFFFFFFF) {n = n + 1;} - - return n; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0, - mpd_uint_t v) -{ - const mpd_uint_t b = 4294967296; - mpd_uint_t un1, un0, - vn1, vn0, - q1, q0, - un32, un21, un10, - rhat, t; - int s; - - assert(u1 < v); - - s = nlz(v); - v = v << s; - vn1 = v >> 32; - vn0 = v & 0xFFFFFFFF; - - t = (s == 0) ? 0 : u0 >> (64 - s); - un32 = (u1 << s) | t; - un10 = u0 << s; - - un1 = un10 >> 32; - un0 = un10 & 0xFFFFFFFF; - - q1 = un32 / vn1; - rhat = un32 - q1*vn1; -again1: - if (q1 >= b || q1*vn0 > b*rhat + un1) { - q1 = q1 - 1; - rhat = rhat + vn1; - if (rhat < b) goto again1; - } - - /* - * Before again1 we had: - * (1) q1*vn1 + rhat = un32 - * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1 - * - * The statements inside the if-clause do not change the value - * of the left-hand side of (2), and the loop is only exited - * if q1*vn0 <= rhat*b + un1, so: - * - * (3) q1*vn1*b + q1*vn0 <= un32*b + un1 - * (4) q1*v <= un32*b + un1 - * (5) 0 <= un32*b + un1 - q1*v - * - * By (5) we are certain that the possible add-back step from - * Knuth's algorithm D is never required. - * - * Since the final quotient is less than 2**64, the following - * must be true: - * - * (6) un32*b + un1 - q1*v <= UINT64_MAX - * - * This means that in the following line, the high words - * of un32*b and q1*v can be discarded without any effect - * on the result. - */ - un21 = un32*b + un1 - q1*v; - - q0 = un21 / vn1; - rhat = un21 - q0*vn1; -again2: - if (q0 >= b || q0*vn0 > b*rhat + un0) { - q0 = q0 - 1; - rhat = rhat + vn1; - if (rhat < b) goto again2; - } - - *q = q1*b + q0; - *r = (un21*b + un0 - q0*v) >> s; -} -#endif - -/* END ANSI */ -#elif defined(ASM) -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - mpd_uint_t h, l; - - __asm__ ( "mulq %3\n\t" - : "=d" (h), "=a" (l) - : "%a" (a), "rm" (b) - : "cc" - ); - - *hi = h; - *lo = l; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d) -{ - mpd_uint_t qq, rr; - - __asm__ ( "divq %4\n\t" - : "=a" (qq), "=d" (rr) - : "a" (lo), "d" (hi), "rm" (d) - : "cc" - ); - - *q = qq; - *r = rr; -} -/* END GCC ASM */ -#elif defined(MASM) -#include <intrin.h> -#pragma intrinsic(_umul128) - -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - *lo = _umul128(a, b, hi); -} - -void _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d); - -/* END MASM (_MSC_VER) */ -#else - #error "need platform specific 128 bit multiplication and division" -#endif - -#define DIVMOD(q, r, v, d) *q = v / d; *r = v - *q * d -static inline void -_mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp) -{ - assert(exp <= 19); - - if (exp <= 9) { - if (exp <= 4) { - switch (exp) { - case 0: *q = v; *r = 0; break; - case 1: DIVMOD(q, r, v, 10UL); break; - case 2: DIVMOD(q, r, v, 100UL); break; - case 3: DIVMOD(q, r, v, 1000UL); break; - case 4: DIVMOD(q, r, v, 10000UL); break; - } - } - else { - switch (exp) { - case 5: DIVMOD(q, r, v, 100000UL); break; - case 6: DIVMOD(q, r, v, 1000000UL); break; - case 7: DIVMOD(q, r, v, 10000000UL); break; - case 8: DIVMOD(q, r, v, 100000000UL); break; - case 9: DIVMOD(q, r, v, 1000000000UL); break; - } - } - } - else { - if (exp <= 14) { - switch (exp) { - case 10: DIVMOD(q, r, v, 10000000000ULL); break; - case 11: DIVMOD(q, r, v, 100000000000ULL); break; - case 12: DIVMOD(q, r, v, 1000000000000ULL); break; - case 13: DIVMOD(q, r, v, 10000000000000ULL); break; - case 14: DIVMOD(q, r, v, 100000000000000ULL); break; - } - } - else { - switch (exp) { - case 15: DIVMOD(q, r, v, 1000000000000000ULL); break; - case 16: DIVMOD(q, r, v, 10000000000000000ULL); break; - case 17: DIVMOD(q, r, v, 100000000000000000ULL); break; - case 18: DIVMOD(q, r, v, 1000000000000000000ULL); break; - case 19: DIVMOD(q, r, v, 10000000000000000000ULL); break; /* GCOV_NOT_REACHED */ - } - } - } -} - -/* END CONFIG_64 */ -#elif defined(CONFIG_32) -#if defined(ANSI) -#if !defined(LEGACY_COMPILER) -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - mpd_uuint_t hl; - - hl = (mpd_uuint_t)a * b; - - *hi = hl >> 32; - *lo = (mpd_uint_t)hl; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d) -{ - mpd_uuint_t hl; - - hl = ((mpd_uuint_t)hi<<32) + lo; - *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */ - *r = (mpd_uint_t)(hl - (mpd_uuint_t)(*q) * d); -} -/* END ANSI + uint64_t */ -#else -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - uint16_t w[4], carry; - uint16_t ah, al, bh, bl; - uint32_t hl; - - ah = (uint16_t)(a>>16); al = (uint16_t)a; - bh = (uint16_t)(b>>16); bl = (uint16_t)b; - - hl = (uint32_t)al * bl; - w[0] = (uint16_t)hl; - carry = (uint16_t)(hl>>16); - - hl = (uint32_t)ah * bl + carry; - w[1] = (uint16_t)hl; - w[2] = (uint16_t)(hl>>16); - - hl = (uint32_t)al * bh + w[1]; - w[1] = (uint16_t)hl; - carry = (uint16_t)(hl>>16); - - hl = ((uint32_t)ah * bh + w[2]) + carry; - w[2] = (uint16_t)hl; - w[3] = (uint16_t)(hl>>16); - - *hi = ((uint32_t)w[3]<<16) + w[2]; - *lo = ((uint32_t)w[1]<<16) + w[0]; -} - -/* - * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt - * http://www.hackersdelight.org/permissions.htm: - * "You are free to use, copy, and distribute any of the code on this web - * site, whether modified by you or not. You need not give attribution." - * - * Slightly modified, comments are mine. - */ -static inline int -nlz(uint32_t x) -{ - int n; - - if (x == 0) return(32); - - n = 0; - if (x <= 0x0000FFFF) {n = n +16; x = x <<16;} - if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;} - if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;} - if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;} - if (x <= 0x7FFFFFFF) {n = n + 1;} - - return n; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0, - mpd_uint_t v) -{ - const mpd_uint_t b = 65536; - mpd_uint_t un1, un0, - vn1, vn0, - q1, q0, - un32, un21, un10, - rhat, t; - int s; - - assert(u1 < v); - - s = nlz(v); - v = v << s; - vn1 = v >> 16; - vn0 = v & 0xFFFF; - - t = (s == 0) ? 0 : u0 >> (32 - s); - un32 = (u1 << s) | t; - un10 = u0 << s; - - un1 = un10 >> 16; - un0 = un10 & 0xFFFF; - - q1 = un32 / vn1; - rhat = un32 - q1*vn1; -again1: - if (q1 >= b || q1*vn0 > b*rhat + un1) { - q1 = q1 - 1; - rhat = rhat + vn1; - if (rhat < b) goto again1; - } - - /* - * Before again1 we had: - * (1) q1*vn1 + rhat = un32 - * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1 - * - * The statements inside the if-clause do not change the value - * of the left-hand side of (2), and the loop is only exited - * if q1*vn0 <= rhat*b + un1, so: - * - * (3) q1*vn1*b + q1*vn0 <= un32*b + un1 - * (4) q1*v <= un32*b + un1 - * (5) 0 <= un32*b + un1 - q1*v - * - * By (5) we are certain that the possible add-back step from - * Knuth's algorithm D is never required. - * - * Since the final quotient is less than 2**32, the following - * must be true: - * - * (6) un32*b + un1 - q1*v <= UINT32_MAX - * - * This means that in the following line, the high words - * of un32*b and q1*v can be discarded without any effect - * on the result. - */ - un21 = un32*b + un1 - q1*v; - - q0 = un21 / vn1; - rhat = un21 - q0*vn1; -again2: - if (q0 >= b || q0*vn0 > b*rhat + un0) { - q0 = q0 - 1; - rhat = rhat + vn1; - if (rhat < b) goto again2; - } - - *q = q1*b + q0; - *r = (un21*b + un0 - q0*v) >> s; -} -#endif /* END ANSI + LEGACY_COMPILER */ - -/* END ANSI */ -#elif defined(ASM) -static inline void -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - mpd_uint_t h, l; - - __asm__ ( "mull %3\n\t" - : "=d" (h), "=a" (l) - : "%a" (a), "rm" (b) - : "cc" - ); - - *hi = h; - *lo = l; -} - -static inline void -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d) -{ - mpd_uint_t qq, rr; - - __asm__ ( "divl %4\n\t" - : "=a" (qq), "=d" (rr) - : "a" (lo), "d" (hi), "rm" (d) - : "cc" - ); - - *q = qq; - *r = rr; -} -/* END GCC ASM */ -#elif defined(MASM) -static inline void __cdecl -_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) -{ - mpd_uint_t h, l; - - __asm { - mov eax, a - mul b - mov h, edx - mov l, eax - } - - *hi = h; - *lo = l; -} - -static inline void __cdecl -_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, - mpd_uint_t d) -{ - mpd_uint_t qq, rr; - - __asm { - mov eax, lo - mov edx, hi - div d - mov qq, eax - mov rr, edx - } - - *q = qq; - *r = rr; -} -/* END MASM (_MSC_VER) */ -#else - #error "need platform specific 64 bit multiplication and division" -#endif - -#define DIVMOD(q, r, v, d) *q = v / d; *r = v - *q * d -static inline void -_mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp) -{ - assert(exp <= 9); - - if (exp <= 4) { - switch (exp) { - case 0: *q = v; *r = 0; break; - case 1: DIVMOD(q, r, v, 10UL); break; - case 2: DIVMOD(q, r, v, 100UL); break; - case 3: DIVMOD(q, r, v, 1000UL); break; - case 4: DIVMOD(q, r, v, 10000UL); break; - } - } - else { - switch (exp) { - case 5: DIVMOD(q, r, v, 100000UL); break; - case 6: DIVMOD(q, r, v, 1000000UL); break; - case 7: DIVMOD(q, r, v, 10000000UL); break; - case 8: DIVMOD(q, r, v, 100000000UL); break; - case 9: DIVMOD(q, r, v, 1000000000UL); break; /* GCOV_NOT_REACHED */ - } - } -} -/* END CONFIG_32 */ - -/* NO CONFIG */ -#else - #error "define CONFIG_64 or CONFIG_32" -#endif /* CONFIG */ - - -static inline void -_mpd_div_word(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t d) -{ - *q = v / d; - *r = v - *q * d; -} - -static inline void -_mpd_idiv_word(mpd_ssize_t *q, mpd_ssize_t *r, mpd_ssize_t v, mpd_ssize_t d) -{ - *q = v / d; - *r = v - *q * d; -} - - -/** ------------------------------------------------------------ - ** Arithmetic with overflow checking - ** ------------------------------------------------------------ - */ - -/* The following macros do call exit() in case of an overflow. - If the library is used correctly (i.e. with valid context - parameters), such overflows cannot occur. The macros are used - as sanity checks in a couple of strategic places and should - be viewed as a handwritten version of gcc's -ftrapv option. */ - -static inline mpd_size_t -add_size_t(mpd_size_t a, mpd_size_t b) -{ - if (a > MPD_SIZE_MAX - b) { - mpd_err_fatal("add_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ - } - return a + b; -} - -static inline mpd_size_t -sub_size_t(mpd_size_t a, mpd_size_t b) -{ - if (b > a) { - mpd_err_fatal("sub_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ - } - return a - b; -} - -#if MPD_SIZE_MAX != MPD_UINT_MAX - #error "adapt mul_size_t() and mulmod_size_t()" -#endif - -static inline mpd_size_t -mul_size_t(mpd_size_t a, mpd_size_t b) -{ - mpd_uint_t hi, lo; - - _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b); - if (hi) { - mpd_err_fatal("mul_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ - } - return lo; -} - -static inline mpd_size_t -add_size_t_overflow(mpd_size_t a, mpd_size_t b, mpd_size_t *overflow) -{ - mpd_size_t ret; - - *overflow = 0; - ret = a + b; - if (ret < a) *overflow = 1; - return ret; -} - -static inline mpd_size_t -mul_size_t_overflow(mpd_size_t a, mpd_size_t b, mpd_size_t *overflow) -{ - mpd_uint_t lo; - - _mpd_mul_words((mpd_uint_t *)overflow, &lo, (mpd_uint_t)a, - (mpd_uint_t)b); - return lo; -} - -static inline mpd_ssize_t -mod_mpd_ssize_t(mpd_ssize_t a, mpd_ssize_t m) -{ - mpd_ssize_t r = a % m; - return (r < 0) ? r + m : r; -} - -static inline mpd_size_t -mulmod_size_t(mpd_size_t a, mpd_size_t b, mpd_size_t m) -{ - mpd_uint_t hi, lo; - mpd_uint_t q, r; - - _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b); - _mpd_div_words(&q, &r, hi, lo, (mpd_uint_t)m); - - return r; -} - - +/*****************************************************************************/ +/* Low level native arithmetic on basic types */ +/*****************************************************************************/ + + +/** ------------------------------------------------------------ + ** Double width multiplication and division + ** ------------------------------------------------------------ + */ + +#if defined(CONFIG_64) +#if defined(ANSI) +#if defined(HAVE_UINT128_T) +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + __uint128_t hl; + + hl = (__uint128_t)a * b; + + *hi = hl >> 64; + *lo = (mpd_uint_t)hl; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d) +{ + __uint128_t hl; + + hl = ((__uint128_t)hi<<64) + lo; + *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */ + *r = (mpd_uint_t)(hl - (__uint128_t)(*q) * d); +} +#else +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + uint32_t w[4], carry; + uint32_t ah, al, bh, bl; + uint64_t hl; + + ah = (uint32_t)(a>>32); al = (uint32_t)a; + bh = (uint32_t)(b>>32); bl = (uint32_t)b; + + hl = (uint64_t)al * bl; + w[0] = (uint32_t)hl; + carry = (uint32_t)(hl>>32); + + hl = (uint64_t)ah * bl + carry; + w[1] = (uint32_t)hl; + w[2] = (uint32_t)(hl>>32); + + hl = (uint64_t)al * bh + w[1]; + w[1] = (uint32_t)hl; + carry = (uint32_t)(hl>>32); + + hl = ((uint64_t)ah * bh + w[2]) + carry; + w[2] = (uint32_t)hl; + w[3] = (uint32_t)(hl>>32); + + *hi = ((uint64_t)w[3]<<32) + w[2]; + *lo = ((uint64_t)w[1]<<32) + w[0]; +} + +/* + * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt + * http://www.hackersdelight.org/permissions.htm: + * "You are free to use, copy, and distribute any of the code on this web + * site, whether modified by you or not. You need not give attribution." + * + * Slightly modified, comments are mine. + */ +static inline int +nlz(uint64_t x) +{ + int n; + + if (x == 0) return(64); + + n = 0; + if (x <= 0x00000000FFFFFFFF) {n = n +32; x = x <<32;} + if (x <= 0x0000FFFFFFFFFFFF) {n = n +16; x = x <<16;} + if (x <= 0x00FFFFFFFFFFFFFF) {n = n + 8; x = x << 8;} + if (x <= 0x0FFFFFFFFFFFFFFF) {n = n + 4; x = x << 4;} + if (x <= 0x3FFFFFFFFFFFFFFF) {n = n + 2; x = x << 2;} + if (x <= 0x7FFFFFFFFFFFFFFF) {n = n + 1;} + + return n; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0, + mpd_uint_t v) +{ + const mpd_uint_t b = 4294967296; + mpd_uint_t un1, un0, + vn1, vn0, + q1, q0, + un32, un21, un10, + rhat, t; + int s; + + assert(u1 < v); + + s = nlz(v); + v = v << s; + vn1 = v >> 32; + vn0 = v & 0xFFFFFFFF; + + t = (s == 0) ? 0 : u0 >> (64 - s); + un32 = (u1 << s) | t; + un10 = u0 << s; + + un1 = un10 >> 32; + un0 = un10 & 0xFFFFFFFF; + + q1 = un32 / vn1; + rhat = un32 - q1*vn1; +again1: + if (q1 >= b || q1*vn0 > b*rhat + un1) { + q1 = q1 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again1; + } + + /* + * Before again1 we had: + * (1) q1*vn1 + rhat = un32 + * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1 + * + * The statements inside the if-clause do not change the value + * of the left-hand side of (2), and the loop is only exited + * if q1*vn0 <= rhat*b + un1, so: + * + * (3) q1*vn1*b + q1*vn0 <= un32*b + un1 + * (4) q1*v <= un32*b + un1 + * (5) 0 <= un32*b + un1 - q1*v + * + * By (5) we are certain that the possible add-back step from + * Knuth's algorithm D is never required. + * + * Since the final quotient is less than 2**64, the following + * must be true: + * + * (6) un32*b + un1 - q1*v <= UINT64_MAX + * + * This means that in the following line, the high words + * of un32*b and q1*v can be discarded without any effect + * on the result. + */ + un21 = un32*b + un1 - q1*v; + + q0 = un21 / vn1; + rhat = un21 - q0*vn1; +again2: + if (q0 >= b || q0*vn0 > b*rhat + un0) { + q0 = q0 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again2; + } + + *q = q1*b + q0; + *r = (un21*b + un0 - q0*v) >> s; +} +#endif + +/* END ANSI */ +#elif defined(ASM) +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + mpd_uint_t h, l; + + __asm__ ( "mulq %3\n\t" + : "=d" (h), "=a" (l) + : "%a" (a), "rm" (b) + : "cc" + ); + + *hi = h; + *lo = l; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d) +{ + mpd_uint_t qq, rr; + + __asm__ ( "divq %4\n\t" + : "=a" (qq), "=d" (rr) + : "a" (lo), "d" (hi), "rm" (d) + : "cc" + ); + + *q = qq; + *r = rr; +} +/* END GCC ASM */ +#elif defined(MASM) +#include <intrin.h> +#pragma intrinsic(_umul128) + +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + *lo = _umul128(a, b, hi); +} + +void _mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d); + +/* END MASM (_MSC_VER) */ +#else + #error "need platform specific 128 bit multiplication and division" +#endif + +#define DIVMOD(q, r, v, d) *q = v / d; *r = v - *q * d +static inline void +_mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp) +{ + assert(exp <= 19); + + if (exp <= 9) { + if (exp <= 4) { + switch (exp) { + case 0: *q = v; *r = 0; break; + case 1: DIVMOD(q, r, v, 10UL); break; + case 2: DIVMOD(q, r, v, 100UL); break; + case 3: DIVMOD(q, r, v, 1000UL); break; + case 4: DIVMOD(q, r, v, 10000UL); break; + } + } + else { + switch (exp) { + case 5: DIVMOD(q, r, v, 100000UL); break; + case 6: DIVMOD(q, r, v, 1000000UL); break; + case 7: DIVMOD(q, r, v, 10000000UL); break; + case 8: DIVMOD(q, r, v, 100000000UL); break; + case 9: DIVMOD(q, r, v, 1000000000UL); break; + } + } + } + else { + if (exp <= 14) { + switch (exp) { + case 10: DIVMOD(q, r, v, 10000000000ULL); break; + case 11: DIVMOD(q, r, v, 100000000000ULL); break; + case 12: DIVMOD(q, r, v, 1000000000000ULL); break; + case 13: DIVMOD(q, r, v, 10000000000000ULL); break; + case 14: DIVMOD(q, r, v, 100000000000000ULL); break; + } + } + else { + switch (exp) { + case 15: DIVMOD(q, r, v, 1000000000000000ULL); break; + case 16: DIVMOD(q, r, v, 10000000000000000ULL); break; + case 17: DIVMOD(q, r, v, 100000000000000000ULL); break; + case 18: DIVMOD(q, r, v, 1000000000000000000ULL); break; + case 19: DIVMOD(q, r, v, 10000000000000000000ULL); break; /* GCOV_NOT_REACHED */ + } + } + } +} + +/* END CONFIG_64 */ +#elif defined(CONFIG_32) +#if defined(ANSI) +#if !defined(LEGACY_COMPILER) +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + mpd_uuint_t hl; + + hl = (mpd_uuint_t)a * b; + + *hi = hl >> 32; + *lo = (mpd_uint_t)hl; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d) +{ + mpd_uuint_t hl; + + hl = ((mpd_uuint_t)hi<<32) + lo; + *q = (mpd_uint_t)(hl / d); /* quotient is known to fit */ + *r = (mpd_uint_t)(hl - (mpd_uuint_t)(*q) * d); +} +/* END ANSI + uint64_t */ +#else +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + uint16_t w[4], carry; + uint16_t ah, al, bh, bl; + uint32_t hl; + + ah = (uint16_t)(a>>16); al = (uint16_t)a; + bh = (uint16_t)(b>>16); bl = (uint16_t)b; + + hl = (uint32_t)al * bl; + w[0] = (uint16_t)hl; + carry = (uint16_t)(hl>>16); + + hl = (uint32_t)ah * bl + carry; + w[1] = (uint16_t)hl; + w[2] = (uint16_t)(hl>>16); + + hl = (uint32_t)al * bh + w[1]; + w[1] = (uint16_t)hl; + carry = (uint16_t)(hl>>16); + + hl = ((uint32_t)ah * bh + w[2]) + carry; + w[2] = (uint16_t)hl; + w[3] = (uint16_t)(hl>>16); + + *hi = ((uint32_t)w[3]<<16) + w[2]; + *lo = ((uint32_t)w[1]<<16) + w[0]; +} + +/* + * By Henry S. Warren: http://www.hackersdelight.org/HDcode/divlu.c.txt + * http://www.hackersdelight.org/permissions.htm: + * "You are free to use, copy, and distribute any of the code on this web + * site, whether modified by you or not. You need not give attribution." + * + * Slightly modified, comments are mine. + */ +static inline int +nlz(uint32_t x) +{ + int n; + + if (x == 0) return(32); + + n = 0; + if (x <= 0x0000FFFF) {n = n +16; x = x <<16;} + if (x <= 0x00FFFFFF) {n = n + 8; x = x << 8;} + if (x <= 0x0FFFFFFF) {n = n + 4; x = x << 4;} + if (x <= 0x3FFFFFFF) {n = n + 2; x = x << 2;} + if (x <= 0x7FFFFFFF) {n = n + 1;} + + return n; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t u1, mpd_uint_t u0, + mpd_uint_t v) +{ + const mpd_uint_t b = 65536; + mpd_uint_t un1, un0, + vn1, vn0, + q1, q0, + un32, un21, un10, + rhat, t; + int s; + + assert(u1 < v); + + s = nlz(v); + v = v << s; + vn1 = v >> 16; + vn0 = v & 0xFFFF; + + t = (s == 0) ? 0 : u0 >> (32 - s); + un32 = (u1 << s) | t; + un10 = u0 << s; + + un1 = un10 >> 16; + un0 = un10 & 0xFFFF; + + q1 = un32 / vn1; + rhat = un32 - q1*vn1; +again1: + if (q1 >= b || q1*vn0 > b*rhat + un1) { + q1 = q1 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again1; + } + + /* + * Before again1 we had: + * (1) q1*vn1 + rhat = un32 + * (2) q1*vn1*b + rhat*b + un1 = un32*b + un1 + * + * The statements inside the if-clause do not change the value + * of the left-hand side of (2), and the loop is only exited + * if q1*vn0 <= rhat*b + un1, so: + * + * (3) q1*vn1*b + q1*vn0 <= un32*b + un1 + * (4) q1*v <= un32*b + un1 + * (5) 0 <= un32*b + un1 - q1*v + * + * By (5) we are certain that the possible add-back step from + * Knuth's algorithm D is never required. + * + * Since the final quotient is less than 2**32, the following + * must be true: + * + * (6) un32*b + un1 - q1*v <= UINT32_MAX + * + * This means that in the following line, the high words + * of un32*b and q1*v can be discarded without any effect + * on the result. + */ + un21 = un32*b + un1 - q1*v; + + q0 = un21 / vn1; + rhat = un21 - q0*vn1; +again2: + if (q0 >= b || q0*vn0 > b*rhat + un0) { + q0 = q0 - 1; + rhat = rhat + vn1; + if (rhat < b) goto again2; + } + + *q = q1*b + q0; + *r = (un21*b + un0 - q0*v) >> s; +} +#endif /* END ANSI + LEGACY_COMPILER */ + +/* END ANSI */ +#elif defined(ASM) +static inline void +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + mpd_uint_t h, l; + + __asm__ ( "mull %3\n\t" + : "=d" (h), "=a" (l) + : "%a" (a), "rm" (b) + : "cc" + ); + + *hi = h; + *lo = l; +} + +static inline void +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d) +{ + mpd_uint_t qq, rr; + + __asm__ ( "divl %4\n\t" + : "=a" (qq), "=d" (rr) + : "a" (lo), "d" (hi), "rm" (d) + : "cc" + ); + + *q = qq; + *r = rr; +} +/* END GCC ASM */ +#elif defined(MASM) +static inline void __cdecl +_mpd_mul_words(mpd_uint_t *hi, mpd_uint_t *lo, mpd_uint_t a, mpd_uint_t b) +{ + mpd_uint_t h, l; + + __asm { + mov eax, a + mul b + mov h, edx + mov l, eax + } + + *hi = h; + *lo = l; +} + +static inline void __cdecl +_mpd_div_words(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo, + mpd_uint_t d) +{ + mpd_uint_t qq, rr; + + __asm { + mov eax, lo + mov edx, hi + div d + mov qq, eax + mov rr, edx + } + + *q = qq; + *r = rr; +} +/* END MASM (_MSC_VER) */ +#else + #error "need platform specific 64 bit multiplication and division" +#endif + +#define DIVMOD(q, r, v, d) *q = v / d; *r = v - *q * d +static inline void +_mpd_divmod_pow10(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t exp) +{ + assert(exp <= 9); + + if (exp <= 4) { + switch (exp) { + case 0: *q = v; *r = 0; break; + case 1: DIVMOD(q, r, v, 10UL); break; + case 2: DIVMOD(q, r, v, 100UL); break; + case 3: DIVMOD(q, r, v, 1000UL); break; + case 4: DIVMOD(q, r, v, 10000UL); break; + } + } + else { + switch (exp) { + case 5: DIVMOD(q, r, v, 100000UL); break; + case 6: DIVMOD(q, r, v, 1000000UL); break; + case 7: DIVMOD(q, r, v, 10000000UL); break; + case 8: DIVMOD(q, r, v, 100000000UL); break; + case 9: DIVMOD(q, r, v, 1000000000UL); break; /* GCOV_NOT_REACHED */ + } + } +} +/* END CONFIG_32 */ + +/* NO CONFIG */ +#else + #error "define CONFIG_64 or CONFIG_32" +#endif /* CONFIG */ + + +static inline void +_mpd_div_word(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t v, mpd_uint_t d) +{ + *q = v / d; + *r = v - *q * d; +} + +static inline void +_mpd_idiv_word(mpd_ssize_t *q, mpd_ssize_t *r, mpd_ssize_t v, mpd_ssize_t d) +{ + *q = v / d; + *r = v - *q * d; +} + + +/** ------------------------------------------------------------ + ** Arithmetic with overflow checking + ** ------------------------------------------------------------ + */ + +/* The following macros do call exit() in case of an overflow. + If the library is used correctly (i.e. with valid context + parameters), such overflows cannot occur. The macros are used + as sanity checks in a couple of strategic places and should + be viewed as a handwritten version of gcc's -ftrapv option. */ + +static inline mpd_size_t +add_size_t(mpd_size_t a, mpd_size_t b) +{ + if (a > MPD_SIZE_MAX - b) { + mpd_err_fatal("add_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ + } + return a + b; +} + +static inline mpd_size_t +sub_size_t(mpd_size_t a, mpd_size_t b) +{ + if (b > a) { + mpd_err_fatal("sub_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ + } + return a - b; +} + +#if MPD_SIZE_MAX != MPD_UINT_MAX + #error "adapt mul_size_t() and mulmod_size_t()" +#endif + +static inline mpd_size_t +mul_size_t(mpd_size_t a, mpd_size_t b) +{ + mpd_uint_t hi, lo; + + _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b); + if (hi) { + mpd_err_fatal("mul_size_t(): overflow: check the context"); /* GCOV_NOT_REACHED */ + } + return lo; +} + +static inline mpd_size_t +add_size_t_overflow(mpd_size_t a, mpd_size_t b, mpd_size_t *overflow) +{ + mpd_size_t ret; + + *overflow = 0; + ret = a + b; + if (ret < a) *overflow = 1; + return ret; +} + +static inline mpd_size_t +mul_size_t_overflow(mpd_size_t a, mpd_size_t b, mpd_size_t *overflow) +{ + mpd_uint_t lo; + + _mpd_mul_words((mpd_uint_t *)overflow, &lo, (mpd_uint_t)a, + (mpd_uint_t)b); + return lo; +} + +static inline mpd_ssize_t +mod_mpd_ssize_t(mpd_ssize_t a, mpd_ssize_t m) +{ + mpd_ssize_t r = a % m; + return (r < 0) ? r + m : r; +} + +static inline mpd_size_t +mulmod_size_t(mpd_size_t a, mpd_size_t b, mpd_size_t m) +{ + mpd_uint_t hi, lo; + mpd_uint_t q, r; + + _mpd_mul_words(&hi, &lo, (mpd_uint_t)a, (mpd_uint_t)b); + _mpd_div_words(&q, &r, hi, lo, (mpd_uint_t)m); + + return r; +} + + #endif /* LIBMPDEC_TYPEARITH_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/umodarith.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/umodarith.h index d7dbbbe6a73..20748b6b914 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/umodarith.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/umodarith.h @@ -1,648 +1,648 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_UMODARITH_H_ #define LIBMPDEC_UMODARITH_H_ - - + + #include "mpdecimal.h" -#include "constants.h" -#include "typearith.h" - - -/* Bignum: Low level routines for unsigned modular arithmetic. These are - used in the fast convolution functions for very large coefficients. */ - - -/**************************************************************************/ -/* ANSI modular arithmetic */ -/**************************************************************************/ - - -/* - * Restrictions: a < m and b < m - * ACL2 proof: umodarith.lisp: addmod-correct - */ -static inline mpd_uint_t -addmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - mpd_uint_t s; - - s = a + b; - s = (s < a) ? s - m : s; - s = (s >= m) ? s - m : s; - - return s; -} - -/* - * Restrictions: a < m and b < m - * ACL2 proof: umodarith.lisp: submod-2-correct - */ -static inline mpd_uint_t -submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - mpd_uint_t d; - - d = a - b; - d = (a < b) ? d + m : d; - - return d; -} - -/* - * Restrictions: a < 2m and b < 2m - * ACL2 proof: umodarith.lisp: section ext-submod - */ -static inline mpd_uint_t -ext_submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - mpd_uint_t d; - - a = (a >= m) ? a - m : a; - b = (b >= m) ? b - m : b; - - d = a - b; - d = (a < b) ? d + m : d; - - return d; -} - -/* - * Reduce double word modulo m. - * Restrictions: m != 0 - * ACL2 proof: umodarith.lisp: section dw-reduce - */ -static inline mpd_uint_t -dw_reduce(mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m) -{ - mpd_uint_t r1, r2, w; - - _mpd_div_word(&w, &r1, hi, m); - _mpd_div_words(&w, &r2, r1, lo, m); - - return r2; -} - -/* - * Subtract double word from a. - * Restrictions: a < m - * ACL2 proof: umodarith.lisp: section dw-submod - */ -static inline mpd_uint_t -dw_submod(mpd_uint_t a, mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m) -{ - mpd_uint_t d, r; - - r = dw_reduce(hi, lo, m); - d = a - r; - d = (a < r) ? d + m : d; - - return d; -} - -#ifdef CONFIG_64 - -/**************************************************************************/ -/* 64-bit modular arithmetic */ -/**************************************************************************/ - -/* - * A proof of the algorithm is in literature/mulmod-64.txt. An ACL2 - * proof is in umodarith.lisp: section "Fast modular reduction". - * - * Algorithm: calculate (a * b) % p: - * - * a) hi, lo <- a * b # Calculate a * b. - * - * b) hi, lo <- R(hi, lo) # Reduce modulo p. - * - * c) Repeat step b) until 0 <= hi * 2**64 + lo < 2*p. - * - * d) If the result is less than p, return lo. Otherwise return lo - p. - */ - -static inline mpd_uint_t -x64_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - mpd_uint_t hi, lo, x, y; - - - _mpd_mul_words(&hi, &lo, a, b); - - if (m & (1ULL<<32)) { /* P1 */ - - /* first reduction */ - x = y = hi; - hi >>= 32; - - x = lo - x; - if (x > lo) hi--; - - y <<= 32; - lo = y + x; - if (lo < y) hi++; - - /* second reduction */ - x = y = hi; - hi >>= 32; - - x = lo - x; - if (x > lo) hi--; - - y <<= 32; - lo = y + x; - if (lo < y) hi++; - - return (hi || lo >= m ? lo - m : lo); - } - else if (m & (1ULL<<34)) { /* P2 */ - - /* first reduction */ - x = y = hi; - hi >>= 30; - - x = lo - x; - if (x > lo) hi--; - - y <<= 34; - lo = y + x; - if (lo < y) hi++; - - /* second reduction */ - x = y = hi; - hi >>= 30; - - x = lo - x; - if (x > lo) hi--; - - y <<= 34; - lo = y + x; - if (lo < y) hi++; - - /* third reduction */ - x = y = hi; - hi >>= 30; - - x = lo - x; - if (x > lo) hi--; - - y <<= 34; - lo = y + x; - if (lo < y) hi++; - - return (hi || lo >= m ? lo - m : lo); - } - else { /* P3 */ - - /* first reduction */ - x = y = hi; - hi >>= 24; - - x = lo - x; - if (x > lo) hi--; - - y <<= 40; - lo = y + x; - if (lo < y) hi++; - - /* second reduction */ - x = y = hi; - hi >>= 24; - - x = lo - x; - if (x > lo) hi--; - - y <<= 40; - lo = y + x; - if (lo < y) hi++; - - /* third reduction */ - x = y = hi; - hi >>= 24; - - x = lo - x; - if (x > lo) hi--; - - y <<= 40; - lo = y + x; - if (lo < y) hi++; - - return (hi || lo >= m ? lo - m : lo); - } -} - -static inline void -x64_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) -{ - *a = x64_mulmod(*a, w, m); - *b = x64_mulmod(*b, w, m); -} - -static inline void -x64_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, - mpd_uint_t m) -{ - *a0 = x64_mulmod(*a0, b0, m); - *a1 = x64_mulmod(*a1, b1, m); -} - -static inline mpd_uint_t -x64_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod) -{ - mpd_uint_t r = 1; - - while (exp > 0) { - if (exp & 1) - r = x64_mulmod(r, base, umod); - base = x64_mulmod(base, base, umod); - exp >>= 1; - } - - return r; -} - -/* END CONFIG_64 */ -#else /* CONFIG_32 */ - - -/**************************************************************************/ -/* 32-bit modular arithmetic */ -/**************************************************************************/ - -#if defined(ANSI) -#if !defined(LEGACY_COMPILER) -/* HAVE_UINT64_T */ -static inline mpd_uint_t -std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - return ((mpd_uuint_t) a * b) % m; -} - -static inline void -std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) -{ - *a = ((mpd_uuint_t) *a * w) % m; - *b = ((mpd_uuint_t) *b * w) % m; -} - -static inline void -std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, - mpd_uint_t m) -{ - *a0 = ((mpd_uuint_t) *a0 * b0) % m; - *a1 = ((mpd_uuint_t) *a1 * b1) % m; -} -/* END HAVE_UINT64_T */ -#else -/* LEGACY_COMPILER */ -static inline mpd_uint_t -std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) -{ - mpd_uint_t hi, lo, q, r; - _mpd_mul_words(&hi, &lo, a, b); - _mpd_div_words(&q, &r, hi, lo, m); - return r; -} - -static inline void -std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) -{ - *a = std_mulmod(*a, w, m); - *b = std_mulmod(*b, w, m); -} - -static inline void -std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, - mpd_uint_t m) -{ - *a0 = std_mulmod(*a0, b0, m); - *a1 = std_mulmod(*a1, b1, m); -} -/* END LEGACY_COMPILER */ -#endif - -static inline mpd_uint_t -std_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod) -{ - mpd_uint_t r = 1; - - while (exp > 0) { - if (exp & 1) - r = std_mulmod(r, base, umod); - base = std_mulmod(base, base, umod); - exp >>= 1; - } - - return r; -} -#endif /* ANSI CONFIG_32 */ - - -/**************************************************************************/ -/* Pentium Pro modular arithmetic */ -/**************************************************************************/ - -/* - * A proof of the algorithm is in literature/mulmod-ppro.txt. The FPU - * control word must be set to 64-bit precision and truncation mode - * prior to using these functions. - * - * Algorithm: calculate (a * b) % p: - * - * p := prime < 2**31 - * pinv := (long double)1.0 / p (precalculated) - * - * a) n = a * b # Calculate exact product. - * b) qest = n * pinv # Calculate estimate for q = n / p. - * c) q = (qest+2**63)-2**63 # Truncate qest to the exact quotient. - * d) r = n - q * p # Calculate remainder. - * - * Remarks: - * - * - p = dmod and pinv = dinvmod. - * - dinvmod points to an array of three uint32_t, which is interpreted - * as an 80 bit long double by fldt. - * - Intel compilers prior to version 11 do not seem to handle the - * __GNUC__ inline assembly correctly. - * - random tests are provided in tests/extended/ppro_mulmod.c - */ - -#if defined(PPRO) -#if defined(ASM) - -/* Return (a * b) % dmod */ -static inline mpd_uint_t -ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod) -{ - mpd_uint_t retval; - - __asm__ ( - "fildl %2\n\t" - "fildl %1\n\t" - "fmulp %%st, %%st(1)\n\t" - "fldt (%4)\n\t" - "fmul %%st(1), %%st\n\t" - "flds %5\n\t" - "fadd %%st, %%st(1)\n\t" - "fsubrp %%st, %%st(1)\n\t" - "fldl (%3)\n\t" - "fmulp %%st, %%st(1)\n\t" - "fsubrp %%st, %%st(1)\n\t" - "fistpl %0\n\t" - : "=m" (retval) - : "m" (a), "m" (b), "r" (dmod), "r" (dinvmod), "m" (MPD_TWO63) - : "st", "memory" - ); - - return retval; -} - -/* - * Two modular multiplications in parallel: - * *a0 = (*a0 * w) % dmod - * *a1 = (*a1 * w) % dmod - */ -static inline void -ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w, - double *dmod, uint32_t *dinvmod) -{ - __asm__ ( - "fildl %2\n\t" - "fildl (%1)\n\t" - "fmul %%st(1), %%st\n\t" - "fxch %%st(1)\n\t" - "fildl (%0)\n\t" - "fmulp %%st, %%st(1) \n\t" - "fldt (%4)\n\t" - "flds %5\n\t" - "fld %%st(2)\n\t" - "fmul %%st(2)\n\t" - "fadd %%st(1)\n\t" - "fsub %%st(1)\n\t" - "fmull (%3)\n\t" - "fsubrp %%st, %%st(3)\n\t" - "fxch %%st(2)\n\t" - "fistpl (%0)\n\t" - "fmul %%st(2)\n\t" - "fadd %%st(1)\n\t" - "fsubp %%st, %%st(1)\n\t" - "fmull (%3)\n\t" - "fsubrp %%st, %%st(1)\n\t" - "fistpl (%1)\n\t" - : : "r" (a0), "r" (a1), "m" (w), - "r" (dmod), "r" (dinvmod), - "m" (MPD_TWO63) - : "st", "memory" - ); -} - -/* - * Two modular multiplications in parallel: - * *a0 = (*a0 * b0) % dmod - * *a1 = (*a1 * b1) % dmod - */ -static inline void -ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, - double *dmod, uint32_t *dinvmod) -{ - __asm__ ( - "fildl %3\n\t" - "fildl (%2)\n\t" - "fmulp %%st, %%st(1)\n\t" - "fildl %1\n\t" - "fildl (%0)\n\t" - "fmulp %%st, %%st(1)\n\t" - "fldt (%5)\n\t" - "fld %%st(2)\n\t" - "fmul %%st(1), %%st\n\t" - "fxch %%st(1)\n\t" - "fmul %%st(2), %%st\n\t" - "flds %6\n\t" - "fldl (%4)\n\t" - "fxch %%st(3)\n\t" - "fadd %%st(1), %%st\n\t" - "fxch %%st(2)\n\t" - "fadd %%st(1), %%st\n\t" - "fxch %%st(2)\n\t" - "fsub %%st(1), %%st\n\t" - "fxch %%st(2)\n\t" - "fsubp %%st, %%st(1)\n\t" - "fxch %%st(1)\n\t" - "fmul %%st(2), %%st\n\t" - "fxch %%st(1)\n\t" - "fmulp %%st, %%st(2)\n\t" - "fsubrp %%st, %%st(3)\n\t" - "fsubrp %%st, %%st(1)\n\t" - "fxch %%st(1)\n\t" - "fistpl (%2)\n\t" - "fistpl (%0)\n\t" - : : "r" (a0), "m" (b0), "r" (a1), "m" (b1), - "r" (dmod), "r" (dinvmod), - "m" (MPD_TWO63) - : "st", "memory" - ); -} -/* END PPRO GCC ASM */ -#elif defined(MASM) - -/* Return (a * b) % dmod */ -static inline mpd_uint_t __cdecl -ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod) -{ - mpd_uint_t retval; - - __asm { - mov eax, dinvmod - mov edx, dmod - fild b - fild a - fmulp st(1), st - fld TBYTE PTR [eax] - fmul st, st(1) - fld MPD_TWO63 - fadd st(1), st - fsubp st(1), st - fld QWORD PTR [edx] - fmulp st(1), st - fsubp st(1), st - fistp retval - } - - return retval; -} - -/* - * Two modular multiplications in parallel: - * *a0 = (*a0 * w) % dmod - * *a1 = (*a1 * w) % dmod - */ -static inline mpd_uint_t __cdecl -ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w, - double *dmod, uint32_t *dinvmod) -{ - __asm { - mov ecx, dmod - mov edx, a1 - mov ebx, dinvmod - mov eax, a0 - fild w - fild DWORD PTR [edx] - fmul st, st(1) - fxch st(1) - fild DWORD PTR [eax] - fmulp st(1), st - fld TBYTE PTR [ebx] - fld MPD_TWO63 - fld st(2) - fmul st, st(2) - fadd st, st(1) - fsub st, st(1) - fmul QWORD PTR [ecx] - fsubp st(3), st - fxch st(2) - fistp DWORD PTR [eax] - fmul st, st(2) - fadd st, st(1) - fsubrp st(1), st - fmul QWORD PTR [ecx] - fsubp st(1), st - fistp DWORD PTR [edx] - } -} - -/* - * Two modular multiplications in parallel: - * *a0 = (*a0 * b0) % dmod - * *a1 = (*a1 * b1) % dmod - */ -static inline void __cdecl -ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, - double *dmod, uint32_t *dinvmod) -{ - __asm { - mov ecx, dmod - mov edx, a1 - mov ebx, dinvmod - mov eax, a0 - fild b1 - fild DWORD PTR [edx] - fmulp st(1), st - fild b0 - fild DWORD PTR [eax] - fmulp st(1), st - fld TBYTE PTR [ebx] - fld st(2) - fmul st, st(1) - fxch st(1) - fmul st, st(2) - fld DWORD PTR MPD_TWO63 - fld QWORD PTR [ecx] - fxch st(3) - fadd st, st(1) - fxch st(2) - fadd st, st(1) - fxch st(2) - fsub st, st(1) - fxch st(2) - fsubrp st(1), st - fxch st(1) - fmul st, st(2) - fxch st(1) - fmulp st(2), st - fsubp st(3), st - fsubp st(1), st - fxch st(1) - fistp DWORD PTR [edx] - fistp DWORD PTR [eax] - } -} -#endif /* PPRO MASM (_MSC_VER) */ - - -/* Return (base ** exp) % dmod */ -static inline mpd_uint_t -ppro_powmod(mpd_uint_t base, mpd_uint_t exp, double *dmod, uint32_t *dinvmod) -{ - mpd_uint_t r = 1; - - while (exp > 0) { - if (exp & 1) - r = ppro_mulmod(r, base, dmod, dinvmod); - base = ppro_mulmod(base, base, dmod, dinvmod); - exp >>= 1; - } - - return r; -} -#endif /* PPRO */ -#endif /* CONFIG_32 */ - - +#include "constants.h" +#include "typearith.h" + + +/* Bignum: Low level routines for unsigned modular arithmetic. These are + used in the fast convolution functions for very large coefficients. */ + + +/**************************************************************************/ +/* ANSI modular arithmetic */ +/**************************************************************************/ + + +/* + * Restrictions: a < m and b < m + * ACL2 proof: umodarith.lisp: addmod-correct + */ +static inline mpd_uint_t +addmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + mpd_uint_t s; + + s = a + b; + s = (s < a) ? s - m : s; + s = (s >= m) ? s - m : s; + + return s; +} + +/* + * Restrictions: a < m and b < m + * ACL2 proof: umodarith.lisp: submod-2-correct + */ +static inline mpd_uint_t +submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + mpd_uint_t d; + + d = a - b; + d = (a < b) ? d + m : d; + + return d; +} + +/* + * Restrictions: a < 2m and b < 2m + * ACL2 proof: umodarith.lisp: section ext-submod + */ +static inline mpd_uint_t +ext_submod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + mpd_uint_t d; + + a = (a >= m) ? a - m : a; + b = (b >= m) ? b - m : b; + + d = a - b; + d = (a < b) ? d + m : d; + + return d; +} + +/* + * Reduce double word modulo m. + * Restrictions: m != 0 + * ACL2 proof: umodarith.lisp: section dw-reduce + */ +static inline mpd_uint_t +dw_reduce(mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m) +{ + mpd_uint_t r1, r2, w; + + _mpd_div_word(&w, &r1, hi, m); + _mpd_div_words(&w, &r2, r1, lo, m); + + return r2; +} + +/* + * Subtract double word from a. + * Restrictions: a < m + * ACL2 proof: umodarith.lisp: section dw-submod + */ +static inline mpd_uint_t +dw_submod(mpd_uint_t a, mpd_uint_t hi, mpd_uint_t lo, mpd_uint_t m) +{ + mpd_uint_t d, r; + + r = dw_reduce(hi, lo, m); + d = a - r; + d = (a < r) ? d + m : d; + + return d; +} + +#ifdef CONFIG_64 + +/**************************************************************************/ +/* 64-bit modular arithmetic */ +/**************************************************************************/ + +/* + * A proof of the algorithm is in literature/mulmod-64.txt. An ACL2 + * proof is in umodarith.lisp: section "Fast modular reduction". + * + * Algorithm: calculate (a * b) % p: + * + * a) hi, lo <- a * b # Calculate a * b. + * + * b) hi, lo <- R(hi, lo) # Reduce modulo p. + * + * c) Repeat step b) until 0 <= hi * 2**64 + lo < 2*p. + * + * d) If the result is less than p, return lo. Otherwise return lo - p. + */ + +static inline mpd_uint_t +x64_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + mpd_uint_t hi, lo, x, y; + + + _mpd_mul_words(&hi, &lo, a, b); + + if (m & (1ULL<<32)) { /* P1 */ + + /* first reduction */ + x = y = hi; + hi >>= 32; + + x = lo - x; + if (x > lo) hi--; + + y <<= 32; + lo = y + x; + if (lo < y) hi++; + + /* second reduction */ + x = y = hi; + hi >>= 32; + + x = lo - x; + if (x > lo) hi--; + + y <<= 32; + lo = y + x; + if (lo < y) hi++; + + return (hi || lo >= m ? lo - m : lo); + } + else if (m & (1ULL<<34)) { /* P2 */ + + /* first reduction */ + x = y = hi; + hi >>= 30; + + x = lo - x; + if (x > lo) hi--; + + y <<= 34; + lo = y + x; + if (lo < y) hi++; + + /* second reduction */ + x = y = hi; + hi >>= 30; + + x = lo - x; + if (x > lo) hi--; + + y <<= 34; + lo = y + x; + if (lo < y) hi++; + + /* third reduction */ + x = y = hi; + hi >>= 30; + + x = lo - x; + if (x > lo) hi--; + + y <<= 34; + lo = y + x; + if (lo < y) hi++; + + return (hi || lo >= m ? lo - m : lo); + } + else { /* P3 */ + + /* first reduction */ + x = y = hi; + hi >>= 24; + + x = lo - x; + if (x > lo) hi--; + + y <<= 40; + lo = y + x; + if (lo < y) hi++; + + /* second reduction */ + x = y = hi; + hi >>= 24; + + x = lo - x; + if (x > lo) hi--; + + y <<= 40; + lo = y + x; + if (lo < y) hi++; + + /* third reduction */ + x = y = hi; + hi >>= 24; + + x = lo - x; + if (x > lo) hi--; + + y <<= 40; + lo = y + x; + if (lo < y) hi++; + + return (hi || lo >= m ? lo - m : lo); + } +} + +static inline void +x64_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) +{ + *a = x64_mulmod(*a, w, m); + *b = x64_mulmod(*b, w, m); +} + +static inline void +x64_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, + mpd_uint_t m) +{ + *a0 = x64_mulmod(*a0, b0, m); + *a1 = x64_mulmod(*a1, b1, m); +} + +static inline mpd_uint_t +x64_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod) +{ + mpd_uint_t r = 1; + + while (exp > 0) { + if (exp & 1) + r = x64_mulmod(r, base, umod); + base = x64_mulmod(base, base, umod); + exp >>= 1; + } + + return r; +} + +/* END CONFIG_64 */ +#else /* CONFIG_32 */ + + +/**************************************************************************/ +/* 32-bit modular arithmetic */ +/**************************************************************************/ + +#if defined(ANSI) +#if !defined(LEGACY_COMPILER) +/* HAVE_UINT64_T */ +static inline mpd_uint_t +std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + return ((mpd_uuint_t) a * b) % m; +} + +static inline void +std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) +{ + *a = ((mpd_uuint_t) *a * w) % m; + *b = ((mpd_uuint_t) *b * w) % m; +} + +static inline void +std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, + mpd_uint_t m) +{ + *a0 = ((mpd_uuint_t) *a0 * b0) % m; + *a1 = ((mpd_uuint_t) *a1 * b1) % m; +} +/* END HAVE_UINT64_T */ +#else +/* LEGACY_COMPILER */ +static inline mpd_uint_t +std_mulmod(mpd_uint_t a, mpd_uint_t b, mpd_uint_t m) +{ + mpd_uint_t hi, lo, q, r; + _mpd_mul_words(&hi, &lo, a, b); + _mpd_div_words(&q, &r, hi, lo, m); + return r; +} + +static inline void +std_mulmod2c(mpd_uint_t *a, mpd_uint_t *b, mpd_uint_t w, mpd_uint_t m) +{ + *a = std_mulmod(*a, w, m); + *b = std_mulmod(*b, w, m); +} + +static inline void +std_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, + mpd_uint_t m) +{ + *a0 = std_mulmod(*a0, b0, m); + *a1 = std_mulmod(*a1, b1, m); +} +/* END LEGACY_COMPILER */ +#endif + +static inline mpd_uint_t +std_powmod(mpd_uint_t base, mpd_uint_t exp, mpd_uint_t umod) +{ + mpd_uint_t r = 1; + + while (exp > 0) { + if (exp & 1) + r = std_mulmod(r, base, umod); + base = std_mulmod(base, base, umod); + exp >>= 1; + } + + return r; +} +#endif /* ANSI CONFIG_32 */ + + +/**************************************************************************/ +/* Pentium Pro modular arithmetic */ +/**************************************************************************/ + +/* + * A proof of the algorithm is in literature/mulmod-ppro.txt. The FPU + * control word must be set to 64-bit precision and truncation mode + * prior to using these functions. + * + * Algorithm: calculate (a * b) % p: + * + * p := prime < 2**31 + * pinv := (long double)1.0 / p (precalculated) + * + * a) n = a * b # Calculate exact product. + * b) qest = n * pinv # Calculate estimate for q = n / p. + * c) q = (qest+2**63)-2**63 # Truncate qest to the exact quotient. + * d) r = n - q * p # Calculate remainder. + * + * Remarks: + * + * - p = dmod and pinv = dinvmod. + * - dinvmod points to an array of three uint32_t, which is interpreted + * as an 80 bit long double by fldt. + * - Intel compilers prior to version 11 do not seem to handle the + * __GNUC__ inline assembly correctly. + * - random tests are provided in tests/extended/ppro_mulmod.c + */ + +#if defined(PPRO) +#if defined(ASM) + +/* Return (a * b) % dmod */ +static inline mpd_uint_t +ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod) +{ + mpd_uint_t retval; + + __asm__ ( + "fildl %2\n\t" + "fildl %1\n\t" + "fmulp %%st, %%st(1)\n\t" + "fldt (%4)\n\t" + "fmul %%st(1), %%st\n\t" + "flds %5\n\t" + "fadd %%st, %%st(1)\n\t" + "fsubrp %%st, %%st(1)\n\t" + "fldl (%3)\n\t" + "fmulp %%st, %%st(1)\n\t" + "fsubrp %%st, %%st(1)\n\t" + "fistpl %0\n\t" + : "=m" (retval) + : "m" (a), "m" (b), "r" (dmod), "r" (dinvmod), "m" (MPD_TWO63) + : "st", "memory" + ); + + return retval; +} + +/* + * Two modular multiplications in parallel: + * *a0 = (*a0 * w) % dmod + * *a1 = (*a1 * w) % dmod + */ +static inline void +ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w, + double *dmod, uint32_t *dinvmod) +{ + __asm__ ( + "fildl %2\n\t" + "fildl (%1)\n\t" + "fmul %%st(1), %%st\n\t" + "fxch %%st(1)\n\t" + "fildl (%0)\n\t" + "fmulp %%st, %%st(1) \n\t" + "fldt (%4)\n\t" + "flds %5\n\t" + "fld %%st(2)\n\t" + "fmul %%st(2)\n\t" + "fadd %%st(1)\n\t" + "fsub %%st(1)\n\t" + "fmull (%3)\n\t" + "fsubrp %%st, %%st(3)\n\t" + "fxch %%st(2)\n\t" + "fistpl (%0)\n\t" + "fmul %%st(2)\n\t" + "fadd %%st(1)\n\t" + "fsubp %%st, %%st(1)\n\t" + "fmull (%3)\n\t" + "fsubrp %%st, %%st(1)\n\t" + "fistpl (%1)\n\t" + : : "r" (a0), "r" (a1), "m" (w), + "r" (dmod), "r" (dinvmod), + "m" (MPD_TWO63) + : "st", "memory" + ); +} + +/* + * Two modular multiplications in parallel: + * *a0 = (*a0 * b0) % dmod + * *a1 = (*a1 * b1) % dmod + */ +static inline void +ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, + double *dmod, uint32_t *dinvmod) +{ + __asm__ ( + "fildl %3\n\t" + "fildl (%2)\n\t" + "fmulp %%st, %%st(1)\n\t" + "fildl %1\n\t" + "fildl (%0)\n\t" + "fmulp %%st, %%st(1)\n\t" + "fldt (%5)\n\t" + "fld %%st(2)\n\t" + "fmul %%st(1), %%st\n\t" + "fxch %%st(1)\n\t" + "fmul %%st(2), %%st\n\t" + "flds %6\n\t" + "fldl (%4)\n\t" + "fxch %%st(3)\n\t" + "fadd %%st(1), %%st\n\t" + "fxch %%st(2)\n\t" + "fadd %%st(1), %%st\n\t" + "fxch %%st(2)\n\t" + "fsub %%st(1), %%st\n\t" + "fxch %%st(2)\n\t" + "fsubp %%st, %%st(1)\n\t" + "fxch %%st(1)\n\t" + "fmul %%st(2), %%st\n\t" + "fxch %%st(1)\n\t" + "fmulp %%st, %%st(2)\n\t" + "fsubrp %%st, %%st(3)\n\t" + "fsubrp %%st, %%st(1)\n\t" + "fxch %%st(1)\n\t" + "fistpl (%2)\n\t" + "fistpl (%0)\n\t" + : : "r" (a0), "m" (b0), "r" (a1), "m" (b1), + "r" (dmod), "r" (dinvmod), + "m" (MPD_TWO63) + : "st", "memory" + ); +} +/* END PPRO GCC ASM */ +#elif defined(MASM) + +/* Return (a * b) % dmod */ +static inline mpd_uint_t __cdecl +ppro_mulmod(mpd_uint_t a, mpd_uint_t b, double *dmod, uint32_t *dinvmod) +{ + mpd_uint_t retval; + + __asm { + mov eax, dinvmod + mov edx, dmod + fild b + fild a + fmulp st(1), st + fld TBYTE PTR [eax] + fmul st, st(1) + fld MPD_TWO63 + fadd st(1), st + fsubp st(1), st + fld QWORD PTR [edx] + fmulp st(1), st + fsubp st(1), st + fistp retval + } + + return retval; +} + +/* + * Two modular multiplications in parallel: + * *a0 = (*a0 * w) % dmod + * *a1 = (*a1 * w) % dmod + */ +static inline mpd_uint_t __cdecl +ppro_mulmod2c(mpd_uint_t *a0, mpd_uint_t *a1, mpd_uint_t w, + double *dmod, uint32_t *dinvmod) +{ + __asm { + mov ecx, dmod + mov edx, a1 + mov ebx, dinvmod + mov eax, a0 + fild w + fild DWORD PTR [edx] + fmul st, st(1) + fxch st(1) + fild DWORD PTR [eax] + fmulp st(1), st + fld TBYTE PTR [ebx] + fld MPD_TWO63 + fld st(2) + fmul st, st(2) + fadd st, st(1) + fsub st, st(1) + fmul QWORD PTR [ecx] + fsubp st(3), st + fxch st(2) + fistp DWORD PTR [eax] + fmul st, st(2) + fadd st, st(1) + fsubrp st(1), st + fmul QWORD PTR [ecx] + fsubp st(1), st + fistp DWORD PTR [edx] + } +} + +/* + * Two modular multiplications in parallel: + * *a0 = (*a0 * b0) % dmod + * *a1 = (*a1 * b1) % dmod + */ +static inline void __cdecl +ppro_mulmod2(mpd_uint_t *a0, mpd_uint_t b0, mpd_uint_t *a1, mpd_uint_t b1, + double *dmod, uint32_t *dinvmod) +{ + __asm { + mov ecx, dmod + mov edx, a1 + mov ebx, dinvmod + mov eax, a0 + fild b1 + fild DWORD PTR [edx] + fmulp st(1), st + fild b0 + fild DWORD PTR [eax] + fmulp st(1), st + fld TBYTE PTR [ebx] + fld st(2) + fmul st, st(1) + fxch st(1) + fmul st, st(2) + fld DWORD PTR MPD_TWO63 + fld QWORD PTR [ecx] + fxch st(3) + fadd st, st(1) + fxch st(2) + fadd st, st(1) + fxch st(2) + fsub st, st(1) + fxch st(2) + fsubrp st(1), st + fxch st(1) + fmul st, st(2) + fxch st(1) + fmulp st(2), st + fsubp st(3), st + fsubp st(1), st + fxch st(1) + fistp DWORD PTR [edx] + fistp DWORD PTR [eax] + } +} +#endif /* PPRO MASM (_MSC_VER) */ + + +/* Return (base ** exp) % dmod */ +static inline mpd_uint_t +ppro_powmod(mpd_uint_t base, mpd_uint_t exp, double *dmod, uint32_t *dinvmod) +{ + mpd_uint_t r = 1; + + while (exp > 0) { + if (exp & 1) + r = ppro_mulmod(r, base, dmod, dinvmod); + base = ppro_mulmod(base, base, dmod, dinvmod); + exp >>= 1; + } + + return r; +} +#endif /* PPRO */ +#endif /* CONFIG_32 */ + + #endif /* LIBMPDEC_UMODARITH_H_ */ diff --git a/contrib/tools/python3/src/Modules/_decimal/libmpdec/vccompat.h b/contrib/tools/python3/src/Modules/_decimal/libmpdec/vccompat.h index e2e1c42cc02..e473ac370bf 100644 --- a/contrib/tools/python3/src/Modules/_decimal/libmpdec/vccompat.h +++ b/contrib/tools/python3/src/Modules/_decimal/libmpdec/vccompat.h @@ -1,56 +1,56 @@ -/* +/* * Copyright (c) 2008-2020 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. - */ - - + * + * 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. + */ + + #ifndef LIBMPDEC_VCCOMPAT_H_ #define LIBMPDEC_VCCOMPAT_H_ - - + + /* Visual C fixes: no snprintf ... */ -#ifdef _MSC_VER +#ifdef _MSC_VER #ifndef __cplusplus #undef inline #define inline __inline #endif - #undef random - #define random rand - #undef srandom - #define srandom srand - #undef snprintf - #define snprintf sprintf_s - #define HAVE_SNPRINTF - #undef strncasecmp - #define strncasecmp _strnicmp - #undef strcasecmp - #define strcasecmp _stricmp - #undef strtoll - #define strtoll _strtoi64 - #define strdup _strdup -#endif - - + #undef random + #define random rand + #undef srandom + #define srandom srand + #undef snprintf + #define snprintf sprintf_s + #define HAVE_SNPRINTF + #undef strncasecmp + #define strncasecmp _strnicmp + #undef strcasecmp + #define strcasecmp _stricmp + #undef strtoll + #define strtoll _strtoi64 + #define strdup _strdup +#endif + + #endif /* LIBMPDEC_VCCOMPAT_H_ */ |
