• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Tiny arbitrary precision floating point library
3  *
4  * Copyright (c) 2017-2020 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #ifndef LIBBF_H
25 #define LIBBF_H
26 
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #if INTPTR_MAX >= INT64_MAX
31 #define LIMB_LOG2_BITS 6
32 #else
33 #define LIMB_LOG2_BITS 5
34 #endif
35 
36 #define LIMB_BITS (1 << LIMB_LOG2_BITS)
37 
38 #if LIMB_BITS == 64
39 typedef __int128 int128_t;
40 typedef unsigned __int128 uint128_t;
41 typedef int64_t slimb_t;
42 typedef uint64_t limb_t;
43 typedef uint128_t dlimb_t;
44 #define BF_RAW_EXP_MIN INT64_MIN
45 #define BF_RAW_EXP_MAX INT64_MAX
46 
47 #define LIMB_DIGITS 19
48 #define BF_DEC_BASE UINT64_C(10000000000000000000)
49 
50 #else
51 
52 typedef int32_t slimb_t;
53 typedef uint32_t limb_t;
54 typedef uint64_t dlimb_t;
55 #define BF_RAW_EXP_MIN INT32_MIN
56 #define BF_RAW_EXP_MAX INT32_MAX
57 
58 #define LIMB_DIGITS 9
59 #define BF_DEC_BASE 1000000000U
60 
61 #endif
62 
63 /* in bits */
64 /* minimum number of bits for the exponent */
65 #define BF_EXP_BITS_MIN 3
66 /* maximum number of bits for the exponent */
67 #define BF_EXP_BITS_MAX (LIMB_BITS - 3)
68 /* extended range for exponent, used internally */
69 #define BF_EXT_EXP_BITS_MAX (BF_EXP_BITS_MAX + 1)
70 /* minimum possible precision */
71 #define BF_PREC_MIN 2
72 /* minimum possible precision */
73 #define BF_PREC_MAX (((limb_t)1 << (LIMB_BITS - 2)) - 2)
74 /* some operations support infinite precision */
75 #define BF_PREC_INF (BF_PREC_MAX + 1) /* infinite precision */
76 
77 #if LIMB_BITS == 64
78 #define BF_CHKSUM_MOD (UINT64_C(975620677) * UINT64_C(9795002197))
79 #else
80 #define BF_CHKSUM_MOD 975620677U
81 #endif
82 
83 #define BF_EXP_ZERO BF_RAW_EXP_MIN
84 #define BF_EXP_INF (BF_RAW_EXP_MAX - 1)
85 #define BF_EXP_NAN BF_RAW_EXP_MAX
86 
87 /* +/-zero is represented with expn = BF_EXP_ZERO and len = 0,
88    +/-infinity is represented with expn = BF_EXP_INF and len = 0,
89    NaN is represented with expn = BF_EXP_NAN and len = 0 (sign is ignored)
90  */
91 typedef struct {
92     struct bf_context_t *ctx;
93     int sign;
94     slimb_t expn;
95     limb_t len;
96     limb_t *tab;
97 } bf_t;
98 
99 typedef struct {
100     /* must be kept identical to bf_t */
101     struct bf_context_t *ctx;
102     int sign;
103     slimb_t expn;
104     limb_t len;
105     limb_t *tab;
106 } bfdec_t;
107 
108 typedef enum {
109     BF_RNDN, /* round to nearest, ties to even */
110     BF_RNDZ, /* round to zero */
111     BF_RNDD, /* round to -inf (the code relies on (BF_RNDD xor BF_RNDU) = 1) */
112     BF_RNDU, /* round to +inf */
113     BF_RNDNA, /* round to nearest, ties away from zero */
114     BF_RNDA, /* round away from zero */
115     BF_RNDF, /* faithful rounding (nondeterministic, either RNDD or RNDU,
116                 inexact flag is always set)  */
117 } bf_rnd_t;
118 
119 /* allow subnormal numbers. Only available if the number of exponent
120    bits is <= BF_EXP_BITS_USER_MAX and prec != BF_PREC_INF. */
121 #define BF_FLAG_SUBNORMAL (1 << 3)
122 /* 'prec' is the precision after the radix point instead of the whole
123    mantissa. Can only be used with bf_round() and
124    bfdec_[add|sub|mul|div|sqrt|round](). */
125 #define BF_FLAG_RADPNT_PREC (1 << 4)
126 
127 #define BF_RND_MASK 0x7
128 #define BF_EXP_BITS_SHIFT 5
129 #define BF_EXP_BITS_MASK 0x3f
130 
131 /* shortcut for bf_set_exp_bits(BF_EXT_EXP_BITS_MAX) */
132 #define BF_FLAG_EXT_EXP (BF_EXP_BITS_MASK << BF_EXP_BITS_SHIFT)
133 
134 /* contains the rounding mode and number of exponents bits */
135 typedef uint32_t bf_flags_t;
136 
137 typedef void *bf_realloc_func_t(void *opaque, void *ptr, size_t size);
138 
139 typedef struct {
140     bf_t val;
141     limb_t prec;
142 } BFConstCache;
143 
144 typedef struct bf_context_t {
145     void *realloc_opaque;
146     bf_realloc_func_t *realloc_func;
147     BFConstCache log2_cache;
148     BFConstCache pi_cache;
149     struct BFNTTState *ntt_state;
150 } bf_context_t;
151 
bf_get_exp_bits(bf_flags_t flags)152 static inline int bf_get_exp_bits(bf_flags_t flags)
153 {
154     int e;
155     e = (flags >> BF_EXP_BITS_SHIFT) & BF_EXP_BITS_MASK;
156     if (e == BF_EXP_BITS_MASK)
157         return BF_EXP_BITS_MAX + 1;
158     else
159         return BF_EXP_BITS_MAX - e;
160 }
161 
bf_set_exp_bits(int n)162 static inline bf_flags_t bf_set_exp_bits(int n)
163 {
164     return ((BF_EXP_BITS_MAX - n) & BF_EXP_BITS_MASK) << BF_EXP_BITS_SHIFT;
165 }
166 
167 /* returned status */
168 #define BF_ST_INVALID_OP  (1 << 0)
169 #define BF_ST_DIVIDE_ZERO (1 << 1)
170 #define BF_ST_OVERFLOW    (1 << 2)
171 #define BF_ST_UNDERFLOW   (1 << 3)
172 #define BF_ST_INEXACT     (1 << 4)
173 /* indicate that a memory allocation error occured. NaN is returned */
174 #define BF_ST_MEM_ERROR   (1 << 5)
175 
176 #define BF_RADIX_MAX 36 /* maximum radix for bf_atof() and bf_ftoa() */
177 
bf_max(slimb_t a,slimb_t b)178 static inline slimb_t bf_max(slimb_t a, slimb_t b)
179 {
180     if (a > b)
181         return a;
182     else
183         return b;
184 }
185 
bf_min(slimb_t a,slimb_t b)186 static inline slimb_t bf_min(slimb_t a, slimb_t b)
187 {
188     if (a < b)
189         return a;
190     else
191         return b;
192 }
193 
194 void bf_context_init(bf_context_t *s, bf_realloc_func_t *realloc_func,
195                      void *realloc_opaque);
196 void bf_context_end(bf_context_t *s);
197 /* free memory allocated for the bf cache data */
198 void bf_clear_cache(bf_context_t *s);
199 
bf_realloc(bf_context_t * s,void * ptr,size_t size)200 static inline void *bf_realloc(bf_context_t *s, void *ptr, size_t size)
201 {
202     return s->realloc_func(s->realloc_opaque, ptr, size);
203 }
204 
205 /* 'size' must be != 0 */
bf_malloc(bf_context_t * s,size_t size)206 static inline void *bf_malloc(bf_context_t *s, size_t size)
207 {
208     return bf_realloc(s, NULL, size);
209 }
210 
bf_free(bf_context_t * s,void * ptr)211 static inline void bf_free(bf_context_t *s, void *ptr)
212 {
213     /* must test ptr otherwise equivalent to malloc(0) */
214     if (ptr)
215         bf_realloc(s, ptr, 0);
216 }
217 
218 void bf_init(bf_context_t *s, bf_t *r);
219 
bf_delete(bf_t * r)220 static inline void bf_delete(bf_t *r)
221 {
222     bf_context_t *s = r->ctx;
223     /* we accept to delete a zeroed bf_t structure */
224     if (s && r->tab) {
225         bf_realloc(s, r->tab, 0);
226     }
227 }
228 
bf_neg(bf_t * r)229 static inline void bf_neg(bf_t *r)
230 {
231     r->sign ^= 1;
232 }
233 
bf_is_finite(const bf_t * a)234 static inline int bf_is_finite(const bf_t *a)
235 {
236     return (a->expn < BF_EXP_INF);
237 }
238 
bf_is_nan(const bf_t * a)239 static inline int bf_is_nan(const bf_t *a)
240 {
241     return (a->expn == BF_EXP_NAN);
242 }
243 
bf_is_zero(const bf_t * a)244 static inline int bf_is_zero(const bf_t *a)
245 {
246     return (a->expn == BF_EXP_ZERO);
247 }
248 
bf_memcpy(bf_t * r,const bf_t * a)249 static inline void bf_memcpy(bf_t *r, const bf_t *a)
250 {
251     *r = *a;
252 }
253 
254 int bf_set_ui(bf_t *r, uint64_t a);
255 int bf_set_si(bf_t *r, int64_t a);
256 void bf_set_nan(bf_t *r);
257 void bf_set_zero(bf_t *r, int is_neg);
258 void bf_set_inf(bf_t *r, int is_neg);
259 int bf_set(bf_t *r, const bf_t *a);
260 void bf_move(bf_t *r, bf_t *a);
261 int bf_get_float64(const bf_t *a, double *pres, bf_rnd_t rnd_mode);
262 int bf_set_float64(bf_t *a, double d);
263 
264 int bf_cmpu(const bf_t *a, const bf_t *b);
265 int bf_cmp_full(const bf_t *a, const bf_t *b);
266 int bf_cmp(const bf_t *a, const bf_t *b);
bf_cmp_eq(const bf_t * a,const bf_t * b)267 static inline int bf_cmp_eq(const bf_t *a, const bf_t *b)
268 {
269     return bf_cmp(a, b) == 0;
270 }
271 
bf_cmp_le(const bf_t * a,const bf_t * b)272 static inline int bf_cmp_le(const bf_t *a, const bf_t *b)
273 {
274     return bf_cmp(a, b) <= 0;
275 }
276 
bf_cmp_lt(const bf_t * a,const bf_t * b)277 static inline int bf_cmp_lt(const bf_t *a, const bf_t *b)
278 {
279     return bf_cmp(a, b) < 0;
280 }
281 
282 int bf_add(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
283 int bf_sub(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
284 int bf_add_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec, bf_flags_t flags);
285 int bf_mul(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
286 int bf_mul_ui(bf_t *r, const bf_t *a, uint64_t b1, limb_t prec, bf_flags_t flags);
287 int bf_mul_si(bf_t *r, const bf_t *a, int64_t b1, limb_t prec,
288               bf_flags_t flags);
289 int bf_mul_2exp(bf_t *r, slimb_t e, limb_t prec, bf_flags_t flags);
290 int bf_div(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec, bf_flags_t flags);
291 #define BF_DIVREM_EUCLIDIAN BF_RNDF
292 int bf_divrem(bf_t *q, bf_t *r, const bf_t *a, const bf_t *b,
293               limb_t prec, bf_flags_t flags, int rnd_mode);
294 int bf_rem(bf_t *r, const bf_t *a, const bf_t *b, limb_t prec,
295            bf_flags_t flags, int rnd_mode);
296 int bf_remquo(slimb_t *pq, bf_t *r, const bf_t *a, const bf_t *b, limb_t prec,
297               bf_flags_t flags, int rnd_mode);
298 /* round to integer with infinite precision */
299 int bf_rint(bf_t *r, int rnd_mode);
300 int bf_round(bf_t *r, limb_t prec, bf_flags_t flags);
301 int bf_sqrtrem(bf_t *r, bf_t *rem1, const bf_t *a);
302 int bf_sqrt(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
303 slimb_t bf_get_exp_min(const bf_t *a);
304 int bf_logic_or(bf_t *r, const bf_t *a, const bf_t *b);
305 int bf_logic_xor(bf_t *r, const bf_t *a, const bf_t *b);
306 int bf_logic_and(bf_t *r, const bf_t *a, const bf_t *b);
307 
308 /* additional flags for bf_atof */
309 /* do not accept hex radix prefix (0x or 0X) if radix = 0 or radix = 16 */
310 #define BF_ATOF_NO_HEX       (1 << 16)
311 /* accept binary (0b or 0B) or octal (0o or 0O) radix prefix if radix = 0 */
312 #define BF_ATOF_BIN_OCT      (1 << 17)
313 /* Do not parse NaN or Inf */
314 #define BF_ATOF_NO_NAN_INF   (1 << 18)
315 /* return the exponent separately */
316 #define BF_ATOF_EXPONENT       (1 << 19)
317 
318 int bf_atof(bf_t *a, const char *str, const char **pnext, int radix,
319             limb_t prec, bf_flags_t flags);
320 /* this version accepts prec = BF_PREC_INF and returns the radix
321    exponent */
322 int bf_atof2(bf_t *r, slimb_t *pexponent,
323              const char *str, const char **pnext, int radix,
324              limb_t prec, bf_flags_t flags);
325 int bf_mul_pow_radix(bf_t *r, const bf_t *T, limb_t radix,
326                      slimb_t expn, limb_t prec, bf_flags_t flags);
327 
328 
329 /* Conversion of floating point number to string. Return a null
330    terminated string or NULL if memory error. *plen contains its
331    length if plen != NULL.  The exponent letter is "e" for base 10,
332    "p" for bases 2, 8, 16 with a binary exponent and "@" for the other
333    bases. */
334 
335 #define BF_FTOA_FORMAT_MASK (3 << 16)
336 
337 /* fixed format: prec significant digits rounded with (flags &
338    BF_RND_MASK). Exponential notation is used if too many zeros are
339    needed.*/
340 #define BF_FTOA_FORMAT_FIXED (0 << 16)
341 /* fractional format: prec digits after the decimal point rounded with
342    (flags & BF_RND_MASK) */
343 #define BF_FTOA_FORMAT_FRAC  (1 << 16)
344 /* free format:
345 
346    For binary radices with bf_ftoa() and for bfdec_ftoa(): use the minimum
347    number of digits to represent 'a'. The precision and the rounding
348    mode are ignored.
349 
350    For the non binary radices with bf_ftoa(): use as many digits as
351    necessary so that bf_atof() return the same number when using
352    precision 'prec', rounding to nearest and the subnormal
353    configuration of 'flags'. The result is meaningful only if 'a' is
354    already rounded to 'prec' bits. If the subnormal flag is set, the
355    exponent in 'flags' must also be set to the desired exponent range.
356 */
357 #define BF_FTOA_FORMAT_FREE  (2 << 16)
358 /* same as BF_FTOA_FORMAT_FREE but uses the minimum number of digits
359    (takes more computation time). Identical to BF_FTOA_FORMAT_FREE for
360    binary radices with bf_ftoa() and for bfdec_ftoa(). */
361 #define BF_FTOA_FORMAT_FREE_MIN (3 << 16)
362 
363 /* force exponential notation for fixed or free format */
364 #define BF_FTOA_FORCE_EXP    (1 << 20)
365 /* add 0x prefix for base 16, 0o prefix for base 8 or 0b prefix for
366    base 2 if non zero value */
367 #define BF_FTOA_ADD_PREFIX   (1 << 21)
368 /* return "Infinity" instead of "Inf" and add a "+" for positive
369    exponents */
370 #define BF_FTOA_JS_QUIRKS    (1 << 22)
371 
372 char *bf_ftoa(size_t *plen, const bf_t *a, int radix, limb_t prec,
373               bf_flags_t flags);
374 
375 /* modulo 2^n instead of saturation. NaN and infinity return 0 */
376 #define BF_GET_INT_MOD (1 << 0)
377 int bf_get_int32(int *pres, const bf_t *a, int flags);
378 int bf_get_int64(int64_t *pres, const bf_t *a, int flags);
379 int bf_get_uint64(uint64_t *pres, const bf_t *a);
380 
381 /* the following functions are exported for testing only. */
382 void mp_print_str(const char *str, const limb_t *tab, limb_t n);
383 void bf_print_str(const char *str, const bf_t *a);
384 int bf_resize(bf_t *r, limb_t len);
385 int bf_get_fft_size(int *pdpl, int *pnb_mods, limb_t len);
386 int bf_normalize_and_round(bf_t *r, limb_t prec1, bf_flags_t flags);
387 int bf_can_round(const bf_t *a, slimb_t prec, bf_rnd_t rnd_mode, slimb_t k);
388 slimb_t bf_mul_log2_radix(slimb_t a1, unsigned int radix, int is_inv,
389                           int is_ceil1);
390 int mp_mul(bf_context_t *s, limb_t *result,
391            const limb_t *op1, limb_t op1_size,
392            const limb_t *op2, limb_t op2_size);
393 limb_t mp_add(limb_t *res, const limb_t *op1, const limb_t *op2,
394               limb_t n, limb_t carry);
395 limb_t mp_add_ui(limb_t *tab, limb_t b, size_t n);
396 int mp_sqrtrem(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n);
397 int mp_recip(bf_context_t *s, limb_t *tabr, const limb_t *taba, limb_t n);
398 limb_t bf_isqrt(limb_t a);
399 
400 /* transcendental functions */
401 int bf_const_log2(bf_t *T, limb_t prec, bf_flags_t flags);
402 int bf_const_pi(bf_t *T, limb_t prec, bf_flags_t flags);
403 int bf_exp(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
404 int bf_log(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
405 #define BF_POW_JS_QUIRKS (1 << 16) /* (+/-1)^(+/-Inf) = NaN, 1^NaN = NaN */
406 int bf_pow(bf_t *r, const bf_t *x, const bf_t *y, limb_t prec, bf_flags_t flags);
407 int bf_cos(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
408 int bf_sin(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
409 int bf_tan(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
410 int bf_atan(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
411 int bf_atan2(bf_t *r, const bf_t *y, const bf_t *x,
412              limb_t prec, bf_flags_t flags);
413 int bf_asin(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
414 int bf_acos(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags);
415 
416 /* decimal floating point */
417 
bfdec_init(bf_context_t * s,bfdec_t * r)418 static inline void bfdec_init(bf_context_t *s, bfdec_t *r)
419 {
420     bf_init(s, (bf_t *)r);
421 }
bfdec_delete(bfdec_t * r)422 static inline void bfdec_delete(bfdec_t *r)
423 {
424     bf_delete((bf_t *)r);
425 }
426 
bfdec_neg(bfdec_t * r)427 static inline void bfdec_neg(bfdec_t *r)
428 {
429     r->sign ^= 1;
430 }
431 
bfdec_is_finite(const bfdec_t * a)432 static inline int bfdec_is_finite(const bfdec_t *a)
433 {
434     return (a->expn < BF_EXP_INF);
435 }
436 
bfdec_is_nan(const bfdec_t * a)437 static inline int bfdec_is_nan(const bfdec_t *a)
438 {
439     return (a->expn == BF_EXP_NAN);
440 }
441 
bfdec_is_zero(const bfdec_t * a)442 static inline int bfdec_is_zero(const bfdec_t *a)
443 {
444     return (a->expn == BF_EXP_ZERO);
445 }
446 
bfdec_memcpy(bfdec_t * r,const bfdec_t * a)447 static inline void bfdec_memcpy(bfdec_t *r, const bfdec_t *a)
448 {
449     bf_memcpy((bf_t *)r, (const bf_t *)a);
450 }
451 
452 int bfdec_set_ui(bfdec_t *r, uint64_t a);
453 int bfdec_set_si(bfdec_t *r, int64_t a);
454 
bfdec_set_nan(bfdec_t * r)455 static inline void bfdec_set_nan(bfdec_t *r)
456 {
457     bf_set_nan((bf_t *)r);
458 }
bfdec_set_zero(bfdec_t * r,int is_neg)459 static inline void bfdec_set_zero(bfdec_t *r, int is_neg)
460 {
461     bf_set_zero((bf_t *)r, is_neg);
462 }
bfdec_set_inf(bfdec_t * r,int is_neg)463 static inline void bfdec_set_inf(bfdec_t *r, int is_neg)
464 {
465     bf_set_inf((bf_t *)r, is_neg);
466 }
bfdec_set(bfdec_t * r,const bfdec_t * a)467 static inline int bfdec_set(bfdec_t *r, const bfdec_t *a)
468 {
469     return bf_set((bf_t *)r, (bf_t *)a);
470 }
bfdec_move(bfdec_t * r,bfdec_t * a)471 static inline void bfdec_move(bfdec_t *r, bfdec_t *a)
472 {
473     bf_move((bf_t *)r, (bf_t *)a);
474 }
bfdec_cmpu(const bfdec_t * a,const bfdec_t * b)475 static inline int bfdec_cmpu(const bfdec_t *a, const bfdec_t *b)
476 {
477     return bf_cmpu((const bf_t *)a, (const bf_t *)b);
478 }
bfdec_cmp_full(const bfdec_t * a,const bfdec_t * b)479 static inline int bfdec_cmp_full(const bfdec_t *a, const bfdec_t *b)
480 {
481     return bf_cmp_full((const bf_t *)a, (const bf_t *)b);
482 }
bfdec_cmp(const bfdec_t * a,const bfdec_t * b)483 static inline int bfdec_cmp(const bfdec_t *a, const bfdec_t *b)
484 {
485     return bf_cmp((const bf_t *)a, (const bf_t *)b);
486 }
bfdec_cmp_eq(const bfdec_t * a,const bfdec_t * b)487 static inline int bfdec_cmp_eq(const bfdec_t *a, const bfdec_t *b)
488 {
489     return bfdec_cmp(a, b) == 0;
490 }
bfdec_cmp_le(const bfdec_t * a,const bfdec_t * b)491 static inline int bfdec_cmp_le(const bfdec_t *a, const bfdec_t *b)
492 {
493     return bfdec_cmp(a, b) <= 0;
494 }
bfdec_cmp_lt(const bfdec_t * a,const bfdec_t * b)495 static inline int bfdec_cmp_lt(const bfdec_t *a, const bfdec_t *b)
496 {
497     return bfdec_cmp(a, b) < 0;
498 }
499 
500 int bfdec_add(bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec,
501               bf_flags_t flags);
502 int bfdec_sub(bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec,
503               bf_flags_t flags);
504 int bfdec_add_si(bfdec_t *r, const bfdec_t *a, int64_t b1, limb_t prec,
505                  bf_flags_t flags);
506 int bfdec_mul(bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec,
507               bf_flags_t flags);
508 int bfdec_mul_si(bfdec_t *r, const bfdec_t *a, int64_t b1, limb_t prec,
509                  bf_flags_t flags);
510 int bfdec_div(bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec,
511               bf_flags_t flags);
512 int bfdec_divrem(bfdec_t *q, bfdec_t *r, const bfdec_t *a, const bfdec_t *b,
513                  limb_t prec, bf_flags_t flags, int rnd_mode);
514 int bfdec_rem(bfdec_t *r, const bfdec_t *a, const bfdec_t *b, limb_t prec,
515               bf_flags_t flags, int rnd_mode);
516 int bfdec_rint(bfdec_t *r, int rnd_mode);
517 int bfdec_sqrt(bfdec_t *r, const bfdec_t *a, limb_t prec, bf_flags_t flags);
518 int bfdec_round(bfdec_t *r, limb_t prec, bf_flags_t flags);
519 int bfdec_get_int32(int *pres, const bfdec_t *a);
520 int bfdec_pow_ui(bfdec_t *r, const bfdec_t *a, limb_t b);
521 
522 char *bfdec_ftoa(size_t *plen, const bfdec_t *a, limb_t prec, bf_flags_t flags);
523 int bfdec_atof(bfdec_t *r, const char *str, const char **pnext,
524                limb_t prec, bf_flags_t flags);
525 
526 /* the following functions are exported for testing only. */
527 extern const limb_t mp_pow_dec[LIMB_DIGITS + 1];
528 void bfdec_print_str(const char *str, const bfdec_t *a);
bfdec_resize(bfdec_t * r,limb_t len)529 static inline int bfdec_resize(bfdec_t *r, limb_t len)
530 {
531     return bf_resize((bf_t *)r, len);
532 }
533 int bfdec_normalize_and_round(bfdec_t *r, limb_t prec1, bf_flags_t flags);
534 
535 #endif /* LIBBF_H */
536