1 /*
2  * Copyright (c) 2008-2016 Stefan Krah. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 
29 #ifndef MPDECIMAL_H
30 #define MPDECIMAL_H
31 
32 
33 #ifdef __cplusplus
34 extern "C" {
35   #ifndef __STDC_LIMIT_MACROS
36     #define __STDC_LIMIT_MACROS
37     #define MPD_CLEAR_STDC_LIMIT_MACROS
38   #endif
39 #endif
40 
41 
42 #ifndef _MSC_VER
43   #include "pyconfig.h"
44 #endif
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <limits.h>
50 #include <assert.h>
51 #include <stdint.h>
52 #include <inttypes.h>
53 
54 #ifdef _MSC_VER
55   #include "vccompat.h"
56   #ifndef UNUSED
57     #define UNUSED
58   #endif
59   #define MPD_PRAGMA(x)
60   #define MPD_HIDE_SYMBOLS_START
61   #define MPD_HIDE_SYMBOLS_END
62   #define EXTINLINE extern inline
63 #else
64   #ifndef __GNUC_STDC_INLINE__
65     #define __GNUC_STDC_INLINE__ 1
66   #endif
67   #if defined(__GNUC__) && !defined(__INTEL_COMPILER)
68     #define UNUSED __attribute__((unused))
69   #else
70     #define UNUSED
71   #endif
72   #if (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) && \
73       defined(__GNUC__) && __GNUC__ >= 4 && !defined(__INTEL_COMPILER)
74     #define MPD_PRAGMA(x) _Pragma(x)
75     #define MPD_HIDE_SYMBOLS_START "GCC visibility push(hidden)"
76     #define MPD_HIDE_SYMBOLS_END "GCC visibility pop"
77   #else
78     #define MPD_PRAGMA(x)
79     #define MPD_HIDE_SYMBOLS_START
80     #define MPD_HIDE_SYMBOLS_END
81   #endif
82   #define EXTINLINE
83 #endif
84 
85 
86 /* This header file is internal for the purpose of building _decimal.so.
87  * All symbols should have local scope in the DSO. */
88 MPD_PRAGMA(MPD_HIDE_SYMBOLS_START)
89 
90 
91 #if !defined(LEGACY_COMPILER)
92   #if !defined(UINT64_MAX)
93     /* The following #error is just a warning. If the compiler indeed does
94      * not have uint64_t, it is perfectly safe to comment out the #error. */
95     #error "Warning: Compiler without uint64_t. Comment out this line."
96     #define LEGACY_COMPILER
97   #endif
98 #endif
99 
100 
101 /******************************************************************************/
102 /*                                  Version                                   */
103 /******************************************************************************/
104 
105 #define MPD_MAJOR_VERSION 2
106 #define MPD_MINOR_VERSION 4
107 #define MPD_MICRO_VERSION 2
108 
109 #define MPD_VERSION "2.4.2"
110 
111 #define MPD_VERSION_HEX ((MPD_MAJOR_VERSION << 24) | \
112                          (MPD_MINOR_VERSION << 16) | \
113                          (MPD_MICRO_VERSION <<  8))
114 
115 const char *mpd_version(void);
116 
117 
118 /******************************************************************************/
119 /*                              Configuration                                 */
120 /******************************************************************************/
121 
122 #if defined(UNIVERSAL)
123   #if defined(CONFIG_64) || defined(CONFIG_32)
124     #error "cannot use CONFIG_64 or CONFIG_32 with UNIVERSAL."
125   #endif
126   #if defined(__ppc__)
127     #define CONFIG_32
128     #define ANSI
129   #elif defined(__ppc64__)
130     #define CONFIG_64
131     #define ANSI
132   #elif defined(__i386__)
133     #define CONFIG_32
134     #define ANSI
135   #elif defined(__x86_64__)
136     #define CONFIG_64
137     #define ASM
138   #else
139     #error "unknown architecture for universal build."
140   #endif
141 #endif
142 
143 
144 /* BEGIN CONFIG_64 */
145 #if defined(CONFIG_64)
146 /* types for modular and base arithmetic */
147 #define MPD_UINT_MAX UINT64_MAX
148 #define MPD_BITS_PER_UINT 64
149 typedef uint64_t mpd_uint_t;  /* unsigned mod type */
150 
151 #define MPD_SIZE_MAX SIZE_MAX
152 typedef size_t mpd_size_t; /* unsigned size type */
153 
154 /* type for exp, digits, len, prec */
155 #define MPD_SSIZE_MAX INT64_MAX
156 #define MPD_SSIZE_MIN INT64_MIN
157 typedef int64_t mpd_ssize_t;
158 #define _mpd_strtossize strtoll
159 
160 /* decimal arithmetic */
161 #define MPD_RADIX 10000000000000000000ULL  /* 10**19 */
162 #define MPD_RDIGITS 19
163 #define MPD_MAX_POW10 19
164 #define MPD_EXPDIGITS 19  /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */
165 
166 #define MPD_MAXTRANSFORM_2N 4294967296ULL      /* 2**32 */
167 #define MPD_MAX_PREC 999999999999999999LL
168 #define MPD_MAX_PREC_LOG2 64
169 #define MPD_ELIMIT  1000000000000000000LL
170 #define MPD_MAX_EMAX   999999999999999999LL    /* ELIMIT-1 */
171 #define MPD_MIN_EMIN  (-999999999999999999LL)  /* -EMAX */
172 #define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1))
173 #define MPD_EXP_INF 2000000000000000001LL
174 #define MPD_EXP_CLAMP (-4000000000000000001LL)
175 #define MPD_MAXIMPORT 105263157894736842L /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */
176 
177 /* conversion specifiers */
178 #define PRI_mpd_uint_t PRIu64
179 #define PRI_mpd_ssize_t PRIi64
180 /* END CONFIG_64 */
181 
182 
183 /* BEGIN CONFIG_32 */
184 #elif defined(CONFIG_32)
185 /* types for modular and base arithmetic */
186 #define MPD_UINT_MAX UINT32_MAX
187 #define MPD_BITS_PER_UINT 32
188 typedef uint32_t mpd_uint_t;  /* unsigned mod type */
189 
190 #ifndef LEGACY_COMPILER
191 #define MPD_UUINT_MAX UINT64_MAX
192 typedef uint64_t mpd_uuint_t; /* double width unsigned mod type */
193 #endif
194 
195 #define MPD_SIZE_MAX SIZE_MAX
196 typedef size_t mpd_size_t; /* unsigned size type */
197 
198 /* type for dec->len, dec->exp, ctx->prec */
199 #define MPD_SSIZE_MAX INT32_MAX
200 #define MPD_SSIZE_MIN INT32_MIN
201 typedef int32_t mpd_ssize_t;
202 #define _mpd_strtossize strtol
203 
204 /* decimal arithmetic */
205 #define MPD_RADIX 1000000000UL  /* 10**9 */
206 #define MPD_RDIGITS 9
207 #define MPD_MAX_POW10 9
208 #define MPD_EXPDIGITS 10 /* MPD_EXPDIGITS <= MPD_RDIGITS+1 */
209 
210 #define MPD_MAXTRANSFORM_2N 33554432UL /* 2**25 */
211 #define MPD_MAX_PREC 425000000L
212 #define MPD_MAX_PREC_LOG2 32
213 #define MPD_ELIMIT 425000001L
214 #define MPD_MAX_EMAX 425000000L        /* ELIMIT-1 */
215 #define MPD_MIN_EMIN (-425000000L)     /* -EMAX */
216 #define MPD_MIN_ETINY (MPD_MIN_EMIN-(MPD_MAX_PREC-1))
217 #define MPD_EXP_INF 1000000001L      /* allows for emax=999999999 in the tests */
218 #define MPD_EXP_CLAMP (-2000000001L) /* allows for emin=-999999999 in the tests */
219 #define MPD_MAXIMPORT 94444445L      /* ceil((2*MPD_MAX_PREC)/MPD_RDIGITS) */
220 
221 /* conversion specifiers */
222 #define PRI_mpd_uint_t PRIu32
223 #define PRI_mpd_ssize_t PRIi32
224 /* END CONFIG_32 */
225 
226 #else
227   #error "define CONFIG_64 or CONFIG_32"
228 #endif
229 /* END CONFIG */
230 
231 
232 #if MPD_SIZE_MAX != MPD_UINT_MAX
233   #error "unsupported platform: need mpd_size_t == mpd_uint_t"
234 #endif
235 
236 
237 /******************************************************************************/
238 /*                                Context                                     */
239 /******************************************************************************/
240 
241 enum {
242     MPD_ROUND_UP,          /* round away from 0               */
243     MPD_ROUND_DOWN,        /* round toward 0 (truncate)       */
244     MPD_ROUND_CEILING,     /* round toward +infinity          */
245     MPD_ROUND_FLOOR,       /* round toward -infinity          */
246     MPD_ROUND_HALF_UP,     /* 0.5 is rounded up               */
247     MPD_ROUND_HALF_DOWN,   /* 0.5 is rounded down             */
248     MPD_ROUND_HALF_EVEN,   /* 0.5 is rounded to even          */
249     MPD_ROUND_05UP,        /* round zero or five away from 0  */
250     MPD_ROUND_TRUNC,       /* truncate, but set infinity      */
251     MPD_ROUND_GUARD
252 };
253 
254 enum { MPD_CLAMP_DEFAULT, MPD_CLAMP_IEEE_754, MPD_CLAMP_GUARD };
255 
256 extern const char *mpd_round_string[MPD_ROUND_GUARD];
257 extern const char *mpd_clamp_string[MPD_CLAMP_GUARD];
258 
259 
260 typedef struct mpd_context_t {
261     mpd_ssize_t prec;   /* precision */
262     mpd_ssize_t emax;   /* max positive exp */
263     mpd_ssize_t emin;   /* min negative exp */
264     uint32_t traps;     /* status events that should be trapped */
265     uint32_t status;    /* status flags */
266     uint32_t newtrap;   /* set by mpd_addstatus_raise() */
267     int      round;     /* rounding mode */
268     int      clamp;     /* clamp mode */
269     int      allcr;     /* all functions correctly rounded */
270 } mpd_context_t;
271 
272 
273 /* Status flags */
274 #define MPD_Clamped             0x00000001U
275 #define MPD_Conversion_syntax   0x00000002U
276 #define MPD_Division_by_zero    0x00000004U
277 #define MPD_Division_impossible 0x00000008U
278 #define MPD_Division_undefined  0x00000010U
279 #define MPD_Fpu_error           0x00000020U
280 #define MPD_Inexact             0x00000040U
281 #define MPD_Invalid_context     0x00000080U
282 #define MPD_Invalid_operation   0x00000100U
283 #define MPD_Malloc_error        0x00000200U
284 #define MPD_Not_implemented     0x00000400U
285 #define MPD_Overflow            0x00000800U
286 #define MPD_Rounded             0x00001000U
287 #define MPD_Subnormal           0x00002000U
288 #define MPD_Underflow           0x00004000U
289 #define MPD_Max_status         (0x00008000U-1U)
290 
291 /* Conditions that result in an IEEE 754 exception */
292 #define MPD_IEEE_Invalid_operation (MPD_Conversion_syntax |   \
293                                     MPD_Division_impossible | \
294                                     MPD_Division_undefined |  \
295                                     MPD_Fpu_error |           \
296                                     MPD_Invalid_context |     \
297                                     MPD_Invalid_operation |   \
298                                     MPD_Malloc_error)         \
299 
300 /* Errors that require the result of an operation to be set to NaN */
301 #define MPD_Errors (MPD_IEEE_Invalid_operation | \
302                     MPD_Division_by_zero)
303 
304 /* Default traps */
305 #define MPD_Traps (MPD_IEEE_Invalid_operation | \
306                    MPD_Division_by_zero |       \
307                    MPD_Overflow |               \
308                    MPD_Underflow)
309 
310 /* Official name */
311 #define MPD_Insufficient_storage MPD_Malloc_error
312 
313 /* IEEE 754 interchange format contexts */
314 #define MPD_IEEE_CONTEXT_MAX_BITS 512 /* 16*(log2(MPD_MAX_EMAX / 3)-3) */
315 #define MPD_DECIMAL32 32
316 #define MPD_DECIMAL64 64
317 #define MPD_DECIMAL128 128
318 
319 
320 #define MPD_MINALLOC_MIN 2
321 #define MPD_MINALLOC_MAX 64
322 extern mpd_ssize_t MPD_MINALLOC;
323 extern void (* mpd_traphandler)(mpd_context_t *);
324 void mpd_dflt_traphandler(mpd_context_t *);
325 
326 void mpd_setminalloc(mpd_ssize_t n);
327 void mpd_init(mpd_context_t *ctx, mpd_ssize_t prec);
328 
329 void mpd_maxcontext(mpd_context_t *ctx);
330 void mpd_defaultcontext(mpd_context_t *ctx);
331 void mpd_basiccontext(mpd_context_t *ctx);
332 int mpd_ieee_context(mpd_context_t *ctx, int bits);
333 
334 mpd_ssize_t mpd_getprec(const mpd_context_t *ctx);
335 mpd_ssize_t mpd_getemax(const mpd_context_t *ctx);
336 mpd_ssize_t mpd_getemin(const mpd_context_t *ctx);
337 int mpd_getround(const mpd_context_t *ctx);
338 uint32_t mpd_gettraps(const mpd_context_t *ctx);
339 uint32_t mpd_getstatus(const mpd_context_t *ctx);
340 int mpd_getclamp(const mpd_context_t *ctx);
341 int mpd_getcr(const mpd_context_t *ctx);
342 
343 int mpd_qsetprec(mpd_context_t *ctx, mpd_ssize_t prec);
344 int mpd_qsetemax(mpd_context_t *ctx, mpd_ssize_t emax);
345 int mpd_qsetemin(mpd_context_t *ctx, mpd_ssize_t emin);
346 int mpd_qsetround(mpd_context_t *ctx, int newround);
347 int mpd_qsettraps(mpd_context_t *ctx, uint32_t flags);
348 int mpd_qsetstatus(mpd_context_t *ctx, uint32_t flags);
349 int mpd_qsetclamp(mpd_context_t *ctx, int c);
350 int mpd_qsetcr(mpd_context_t *ctx, int c);
351 void mpd_addstatus_raise(mpd_context_t *ctx, uint32_t flags);
352 
353 
354 /******************************************************************************/
355 /*                           Decimal Arithmetic                               */
356 /******************************************************************************/
357 
358 /* mpd_t flags */
359 #define MPD_POS                 ((uint8_t)0)
360 #define MPD_NEG                 ((uint8_t)1)
361 #define MPD_INF                 ((uint8_t)2)
362 #define MPD_NAN                 ((uint8_t)4)
363 #define MPD_SNAN                ((uint8_t)8)
364 #define MPD_SPECIAL (MPD_INF|MPD_NAN|MPD_SNAN)
365 #define MPD_STATIC              ((uint8_t)16)
366 #define MPD_STATIC_DATA         ((uint8_t)32)
367 #define MPD_SHARED_DATA         ((uint8_t)64)
368 #define MPD_CONST_DATA          ((uint8_t)128)
369 #define MPD_DATAFLAGS (MPD_STATIC_DATA|MPD_SHARED_DATA|MPD_CONST_DATA)
370 
371 /* mpd_t */
372 typedef struct mpd_t {
373     uint8_t flags;
374     mpd_ssize_t exp;
375     mpd_ssize_t digits;
376     mpd_ssize_t len;
377     mpd_ssize_t alloc;
378     mpd_uint_t *data;
379 } mpd_t;
380 
381 
382 typedef unsigned char uchar;
383 
384 
385 /******************************************************************************/
386 /*                       Quiet, thread-safe functions                         */
387 /******************************************************************************/
388 
389 /* format specification */
390 typedef struct mpd_spec_t {
391     mpd_ssize_t min_width; /* minimum field width */
392     mpd_ssize_t prec;      /* fraction digits or significant digits */
393     char type;             /* conversion specifier */
394     char align;            /* alignment */
395     char sign;             /* sign printing/alignment */
396     char fill[5];          /* fill character */
397     const char *dot;       /* decimal point */
398     const char *sep;       /* thousands separator */
399     const char *grouping;  /* grouping of digits */
400 } mpd_spec_t;
401 
402 /* output to a string */
403 char *mpd_to_sci(const mpd_t *dec, int fmt);
404 char *mpd_to_eng(const mpd_t *dec, int fmt);
405 mpd_ssize_t mpd_to_sci_size(char **res, const mpd_t *dec, int fmt);
406 mpd_ssize_t mpd_to_eng_size(char **res, const mpd_t *dec, int fmt);
407 int mpd_validate_lconv(mpd_spec_t *spec);
408 int mpd_parse_fmt_str(mpd_spec_t *spec, const char *fmt, int caps);
409 char *mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, const mpd_context_t *ctx, uint32_t *status);
410 char *mpd_qformat(const mpd_t *dec, const char *fmt, const mpd_context_t *ctx, uint32_t *status);
411 
412 #define MPD_NUM_FLAGS 15
413 #define MPD_MAX_FLAG_STRING 208
414 #define MPD_MAX_FLAG_LIST (MPD_MAX_FLAG_STRING+18)
415 #define MPD_MAX_SIGNAL_LIST 121
416 int mpd_snprint_flags(char *dest, int nmemb, uint32_t flags);
417 int mpd_lsnprint_flags(char *dest, int nmemb, uint32_t flags, const char *flag_string[]);
418 int mpd_lsnprint_signals(char *dest, int nmemb, uint32_t flags, const char *signal_string[]);
419 
420 /* output to a file */
421 void mpd_fprint(FILE *file, const mpd_t *dec);
422 void mpd_print(const mpd_t *dec);
423 
424 /* assignment from a string */
425 void mpd_qset_string(mpd_t *dec, const char *s, const mpd_context_t *ctx, uint32_t *status);
426 
427 /* set to NaN with error flags */
428 void mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status);
429 /* set a special with sign and type */
430 void mpd_setspecial(mpd_t *dec, uint8_t sign, uint8_t type);
431 /* set coefficient to zero or all nines */
432 void mpd_zerocoeff(mpd_t *result);
433 void mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status);
434 
435 /* quietly assign a C integer type to an mpd_t */
436 void mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status);
437 void mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status);
438 void mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status);
439 void mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status);
440 #ifndef LEGACY_COMPILER
441 void mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status);
442 void mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status);
443 #endif
444 
445 /* quietly assign a C integer type to an mpd_t with a static coefficient */
446 void mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, uint32_t *status);
447 void mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, uint32_t *status);
448 void mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, uint32_t *status);
449 void mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, uint32_t *status);
450 
451 /* quietly get a C integer type from an mpd_t */
452 mpd_ssize_t mpd_qget_ssize(const mpd_t *dec, uint32_t *status);
453 mpd_uint_t mpd_qget_uint(const mpd_t *dec, uint32_t *status);
454 mpd_uint_t mpd_qabs_uint(const mpd_t *dec, uint32_t *status);
455 
456 int32_t mpd_qget_i32(const mpd_t *dec, uint32_t *status);
457 uint32_t mpd_qget_u32(const mpd_t *dec, uint32_t *status);
458 #ifndef LEGACY_COMPILER
459 int64_t mpd_qget_i64(const mpd_t *dec, uint32_t *status);
460 uint64_t mpd_qget_u64(const mpd_t *dec, uint32_t *status);
461 #endif
462 
463 /* quiet functions */
464 int mpd_qcheck_nan(mpd_t *nanresult, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
465 int mpd_qcheck_nans(mpd_t *nanresult, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
466 void mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status);
467 
468 const char *mpd_class(const mpd_t *a, const mpd_context_t *ctx);
469 
470 int mpd_qcopy(mpd_t *result, const mpd_t *a,  uint32_t *status);
471 mpd_t *mpd_qncopy(const mpd_t *a);
472 int mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status);
473 int mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status);
474 int mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status);
475 
476 void mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
477 void mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
478 void mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
479 void mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
480 void mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
481 void mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
482 int mpd_same_quantum(const mpd_t *a, const mpd_t *b);
483 
484 void mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
485 int mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status);
486 mpd_uint_t mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status);
487 mpd_uint_t mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n);
488 void mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
489 void mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx, uint32_t *status);
490 
491 int mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status);
492 int mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
493 int mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
494 int mpd_cmp_total(const mpd_t *a, const mpd_t *b);
495 int mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b);
496 int mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b);
497 int mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b);
498 
499 void mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
500 void mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
501 void mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
502 void mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
503 void mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
504 
505 void mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
506 void mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
507 void mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
508 void mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
509 void mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
510 void mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
511 void mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
512 void mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
513 void mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
514 void mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
515 void mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
516 void mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status);
517 void mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, const mpd_context_t *ctx, uint32_t *status);
518 void mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
519 void mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
520 void mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status);
521 void mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status);
522 void mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status);
523 void mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status);
524 void mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
525 void mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status);
526 void mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status);
527 void mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status);
528 void mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status);
529 void mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
530 void mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status);
531 void mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status);
532 void mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status);
533 void mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status);
534 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);
535 void mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
536 void mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, const mpd_context_t *ctx, uint32_t *status);
537 void mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, const mpd_context_t *ctx, uint32_t *status);
538 void mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, const mpd_context_t *ctx, uint32_t *status);
539 void mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, const mpd_context_t *ctx, uint32_t *status);
540 void mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
541 void mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
542 void mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, uint32_t *status);
543 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);
544 void mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_context_t *ctx, uint32_t *status);
545 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);
546 void mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
547 void mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status);
548 void mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
549 void mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
550 void mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
551 void mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, uint32_t *status);
552 
553 #ifndef LEGACY_COMPILER
554 void mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status);
555 void mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status);
556 void mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status);
557 void mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status);
558 void mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status);
559 void mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status);
560 void mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, const mpd_context_t *ctx, uint32_t *status);
561 void mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, const mpd_context_t *ctx, uint32_t *status);
562 #endif
563 
564 
565 size_t mpd_sizeinbase(const mpd_t *a, uint32_t base);
566 void mpd_qimport_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen,
567                      uint8_t srcsign, uint32_t srcbase,
568                      const mpd_context_t *ctx, uint32_t *status);
569 void mpd_qimport_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen,
570                      uint8_t srcsign, uint32_t srcbase,
571                      const mpd_context_t *ctx, uint32_t *status);
572 size_t mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t base,
573                        const mpd_t *src, uint32_t *status);
574 size_t mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t base,
575                        const mpd_t *src, uint32_t *status);
576 
577 
578 /******************************************************************************/
579 /*                           Signalling functions                             */
580 /******************************************************************************/
581 
582 char *mpd_format(const mpd_t *dec, const char *fmt, mpd_context_t *ctx);
583 void mpd_import_u16(mpd_t *result, const uint16_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx);
584 void mpd_import_u32(mpd_t *result, const uint32_t *srcdata, size_t srclen, uint8_t srcsign, uint32_t base, mpd_context_t *ctx);
585 size_t mpd_export_u16(uint16_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx);
586 size_t mpd_export_u32(uint32_t **rdata, size_t rlen, uint32_t base, const mpd_t *src, mpd_context_t *ctx);
587 void mpd_finalize(mpd_t *result, mpd_context_t *ctx);
588 int mpd_check_nan(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
589 int mpd_check_nans(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
590 void mpd_set_string(mpd_t *result, const char *s, mpd_context_t *ctx);
591 void mpd_maxcoeff(mpd_t *result, mpd_context_t *ctx);
592 void mpd_sset_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx);
593 void mpd_sset_i32(mpd_t *result, int32_t a, mpd_context_t *ctx);
594 void mpd_sset_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx);
595 void mpd_sset_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx);
596 void mpd_set_ssize(mpd_t *result, mpd_ssize_t a, mpd_context_t *ctx);
597 void mpd_set_i32(mpd_t *result, int32_t a, mpd_context_t *ctx);
598 void mpd_set_uint(mpd_t *result, mpd_uint_t a, mpd_context_t *ctx);
599 void mpd_set_u32(mpd_t *result, uint32_t a, mpd_context_t *ctx);
600 #ifndef LEGACY_COMPILER
601 void mpd_set_i64(mpd_t *result, int64_t a, mpd_context_t *ctx);
602 void mpd_set_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx);
603 #endif
604 mpd_ssize_t mpd_get_ssize(const mpd_t *a, mpd_context_t *ctx);
605 mpd_uint_t mpd_get_uint(const mpd_t *a, mpd_context_t *ctx);
606 mpd_uint_t mpd_abs_uint(const mpd_t *a, mpd_context_t *ctx);
607 int32_t mpd_get_i32(const mpd_t *a, mpd_context_t *ctx);
608 uint32_t mpd_get_u32(const mpd_t *a, mpd_context_t *ctx);
609 #ifndef LEGACY_COMPILER
610 int64_t mpd_get_i64(const mpd_t *a, mpd_context_t *ctx);
611 uint64_t mpd_get_u64(const mpd_t *a, mpd_context_t *ctx);
612 #endif
613 void mpd_and(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
614 void mpd_copy(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
615 void mpd_canonical(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
616 void mpd_copy_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
617 void mpd_copy_negate(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
618 void mpd_copy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
619 void mpd_invert(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
620 void mpd_logb(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
621 void mpd_or(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
622 void mpd_rotate(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
623 void mpd_scaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
624 void mpd_shiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx);
625 mpd_uint_t mpd_shiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx);
626 void mpd_shiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, mpd_context_t *ctx);
627 void mpd_shift(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
628 void mpd_xor(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
629 void mpd_abs(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
630 int mpd_cmp(const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
631 int mpd_compare(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
632 int mpd_compare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
633 void mpd_add(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
634 void mpd_add_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx);
635 void mpd_add_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx);
636 void mpd_add_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx);
637 void mpd_add_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx);
638 void mpd_sub(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
639 void mpd_sub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx);
640 void mpd_sub_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx);
641 void mpd_sub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx);
642 void mpd_sub_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx);
643 void mpd_div(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
644 void mpd_div_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx);
645 void mpd_div_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx);
646 void mpd_div_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx);
647 void mpd_div_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx);
648 void mpd_divmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
649 void mpd_divint(mpd_t *q, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
650 void mpd_exp(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
651 void mpd_fma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, mpd_context_t *ctx);
652 void mpd_ln(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
653 void mpd_log10(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
654 void mpd_max(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
655 void mpd_max_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
656 void mpd_min(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
657 void mpd_min_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
658 void mpd_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
659 void mpd_mul(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
660 void mpd_mul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, mpd_context_t *ctx);
661 void mpd_mul_i32(mpd_t *result, const mpd_t *a, int32_t b, mpd_context_t *ctx);
662 void mpd_mul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, mpd_context_t *ctx);
663 void mpd_mul_u32(mpd_t *result, const mpd_t *a, uint32_t b, mpd_context_t *ctx);
664 void mpd_next_minus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
665 void mpd_next_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
666 void mpd_next_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
667 void mpd_plus(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
668 void mpd_pow(mpd_t *result, const mpd_t *base, const mpd_t *exp, mpd_context_t *ctx);
669 void mpd_powmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, const mpd_t *mod, mpd_context_t *ctx);
670 void mpd_quantize(mpd_t *result, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
671 void mpd_rescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, mpd_context_t *ctx);
672 void mpd_reduce(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
673 void mpd_rem(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
674 void mpd_rem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, mpd_context_t *ctx);
675 void mpd_round_to_intx(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
676 void mpd_round_to_int(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
677 void mpd_trunc(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
678 void mpd_floor(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
679 void mpd_ceil(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
680 void mpd_sqrt(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
681 void mpd_invroot(mpd_t *result, const mpd_t *a, mpd_context_t *ctx);
682 
683 #ifndef LEGACY_COMPILER
684 void mpd_add_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx);
685 void mpd_add_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx);
686 void mpd_sub_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx);
687 void mpd_sub_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx);
688 void mpd_div_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx);
689 void mpd_div_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx);
690 void mpd_mul_i64(mpd_t *result, const mpd_t *a, int64_t b, mpd_context_t *ctx);
691 void mpd_mul_u64(mpd_t *result, const mpd_t *a, uint64_t b, mpd_context_t *ctx);
692 #endif
693 
694 
695 /******************************************************************************/
696 /*                          Configuration specific                            */
697 /******************************************************************************/
698 
699 #ifdef CONFIG_64
700 void mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, uint32_t *status);
701 void mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, uint32_t *status);
702 void mpd_sset_i64(mpd_t *result, int64_t a, mpd_context_t *ctx);
703 void mpd_sset_u64(mpd_t *result, uint64_t a, mpd_context_t *ctx);
704 #endif
705 
706 
707 /******************************************************************************/
708 /*                       Get attributes of a decimal                          */
709 /******************************************************************************/
710 
711 EXTINLINE mpd_ssize_t mpd_adjexp(const mpd_t *dec);
712 EXTINLINE mpd_ssize_t mpd_etiny(const mpd_context_t *ctx);
713 EXTINLINE mpd_ssize_t mpd_etop(const mpd_context_t *ctx);
714 EXTINLINE mpd_uint_t mpd_msword(const mpd_t *dec);
715 EXTINLINE int mpd_word_digits(mpd_uint_t word);
716 /* most significant digit of a word */
717 EXTINLINE mpd_uint_t mpd_msd(mpd_uint_t word);
718 /* least significant digit of a word */
719 EXTINLINE mpd_uint_t mpd_lsd(mpd_uint_t word);
720 /* coefficient size needed to store 'digits' */
721 EXTINLINE mpd_ssize_t mpd_digits_to_size(mpd_ssize_t digits);
722 /* number of digits in the exponent, undefined for MPD_SSIZE_MIN */
723 EXTINLINE int mpd_exp_digits(mpd_ssize_t exp);
724 EXTINLINE int mpd_iscanonical(const mpd_t *dec UNUSED);
725 EXTINLINE int mpd_isfinite(const mpd_t *dec);
726 EXTINLINE int mpd_isinfinite(const mpd_t *dec);
727 EXTINLINE int mpd_isinteger(const mpd_t *dec);
728 EXTINLINE int mpd_isnan(const mpd_t *dec);
729 EXTINLINE int mpd_isnegative(const mpd_t *dec);
730 EXTINLINE int mpd_ispositive(const mpd_t *dec);
731 EXTINLINE int mpd_isqnan(const mpd_t *dec);
732 EXTINLINE int mpd_issigned(const mpd_t *dec);
733 EXTINLINE int mpd_issnan(const mpd_t *dec);
734 EXTINLINE int mpd_isspecial(const mpd_t *dec);
735 EXTINLINE int mpd_iszero(const mpd_t *dec);
736 /* undefined for special numbers */
737 EXTINLINE int mpd_iszerocoeff(const mpd_t *dec);
738 EXTINLINE int mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx);
739 EXTINLINE int mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx);
740 /* odd word */
741 EXTINLINE int mpd_isoddword(mpd_uint_t word);
742 /* odd coefficient */
743 EXTINLINE int mpd_isoddcoeff(const mpd_t *dec);
744 /* odd decimal, only defined for integers */
745 int mpd_isodd(const mpd_t *dec);
746 /* even decimal, only defined for integers */
747 int mpd_iseven(const mpd_t *dec);
748 /* 0 if dec is positive, 1 if dec is negative */
749 EXTINLINE uint8_t mpd_sign(const mpd_t *dec);
750 /* 1 if dec is positive, -1 if dec is negative */
751 EXTINLINE int mpd_arith_sign(const mpd_t *dec);
752 EXTINLINE long mpd_radix(void);
753 EXTINLINE int mpd_isdynamic(const mpd_t *dec);
754 EXTINLINE int mpd_isstatic(const mpd_t *dec);
755 EXTINLINE int mpd_isdynamic_data(const mpd_t *dec);
756 EXTINLINE int mpd_isstatic_data(const mpd_t *dec);
757 EXTINLINE int mpd_isshared_data(const mpd_t *dec);
758 EXTINLINE int mpd_isconst_data(const mpd_t *dec);
759 EXTINLINE mpd_ssize_t mpd_trail_zeros(const mpd_t *dec);
760 
761 
762 /******************************************************************************/
763 /*                       Set attributes of a decimal                          */
764 /******************************************************************************/
765 
766 /* set number of decimal digits in the coefficient */
767 EXTINLINE void mpd_setdigits(mpd_t *result);
768 EXTINLINE void mpd_set_sign(mpd_t *result, uint8_t sign);
769 /* copy sign from another decimal */
770 EXTINLINE void mpd_signcpy(mpd_t *result, const mpd_t *a);
771 EXTINLINE void mpd_set_infinity(mpd_t *result);
772 EXTINLINE void mpd_set_qnan(mpd_t *result);
773 EXTINLINE void mpd_set_snan(mpd_t *result);
774 EXTINLINE void mpd_set_negative(mpd_t *result);
775 EXTINLINE void mpd_set_positive(mpd_t *result);
776 EXTINLINE void mpd_set_dynamic(mpd_t *result);
777 EXTINLINE void mpd_set_static(mpd_t *result);
778 EXTINLINE void mpd_set_dynamic_data(mpd_t *result);
779 EXTINLINE void mpd_set_static_data(mpd_t *result);
780 EXTINLINE void mpd_set_shared_data(mpd_t *result);
781 EXTINLINE void mpd_set_const_data(mpd_t *result);
782 EXTINLINE void mpd_clear_flags(mpd_t *result);
783 EXTINLINE void mpd_set_flags(mpd_t *result, uint8_t flags);
784 EXTINLINE void mpd_copy_flags(mpd_t *result, const mpd_t *a);
785 
786 
787 /******************************************************************************/
788 /*                              Error Macros                                  */
789 /******************************************************************************/
790 
791 #define mpd_err_fatal(...) \
792     do {fprintf(stderr, "%s:%d: error: ", __FILE__, __LINE__); \
793         fprintf(stderr, __VA_ARGS__);  fputc('\n', stderr);    \
794         abort();                                               \
795     } while (0)
796 #define mpd_err_warn(...) \
797     do {fprintf(stderr, "%s:%d: warning: ", __FILE__, __LINE__); \
798         fprintf(stderr, __VA_ARGS__); fputc('\n', stderr);       \
799     } while (0)
800 
801 
802 /******************************************************************************/
803 /*                            Memory handling                                 */
804 /******************************************************************************/
805 
806 extern void *(* mpd_mallocfunc)(size_t size);
807 extern void *(* mpd_callocfunc)(size_t nmemb, size_t size);
808 extern void *(* mpd_reallocfunc)(void *ptr, size_t size);
809 extern void (* mpd_free)(void *ptr);
810 
811 void *mpd_callocfunc_em(size_t nmemb, size_t size);
812 
813 void *mpd_alloc(mpd_size_t nmemb, mpd_size_t size);
814 void *mpd_calloc(mpd_size_t nmemb, mpd_size_t size);
815 void *mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err);
816 void *mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size);
817 
818 mpd_t *mpd_qnew(void);
819 mpd_t *mpd_new(mpd_context_t *ctx);
820 mpd_t *mpd_qnew_size(mpd_ssize_t size);
821 EXTINLINE void mpd_del(mpd_t *dec);
822 
823 EXTINLINE void mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len);
824 EXTINLINE int mpd_qresize(mpd_t *result, mpd_ssize_t size, uint32_t *status);
825 EXTINLINE int mpd_qresize_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status);
826 EXTINLINE void mpd_minalloc(mpd_t *result);
827 
828 int mpd_resize(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx);
829 int mpd_resize_zero(mpd_t *result, mpd_ssize_t size, mpd_context_t *ctx);
830 
831 
832 MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */
833 
834 
835 #ifdef __cplusplus
836   #ifdef MPD_CLEAR_STDC_LIMIT_MACROS
837     #undef MPD_CLEAR_STDC_LIMIT_MACROS
838     #undef __STDC_LIMIT_MACROS
839   #endif
840 } /* END extern "C" */
841 #endif
842 
843 
844 #endif /* MPDECIMAL_H */
845 
846 
847 
848