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