summaryrefslogtreecommitdiff
path: root/firmware/include/ap_int.h
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/include/ap_int.h')
-rw-r--r--firmware/include/ap_int.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/firmware/include/ap_int.h b/firmware/include/ap_int.h
new file mode 100644
index 0000000000..68cbb2fb71
--- /dev/null
+++ b/firmware/include/ap_int.h
@@ -0,0 +1,59 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2018 by Michael A. Sevakis
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef AP_INT_H
22#define AP_INT_H
23
24/* Miscellaneous large-sized integer functions */
25
26#include <stdbool.h>
27#include <stdint.h>
28
29/* return floor(log(2)*base2exp) - assists in estimating buffer sizes
30 * when converting to decimal */
31static inline int base10exp(int base2exp)
32{
33 /* 1292913986 = floor(2^32*log(2)) */
34 static const long log10of2 = 1292913986L;
35 return log10of2 * (int64_t)base2exp >> 32;
36}
37
38struct ap_int
39{
40 long numchunks; /* number of uint32_t chunks or zero */
41 long basechunk; /* chunk of start of value bits */
42 uint32_t *chunks; /* pointer to chunk array (caller alloced) */
43 long len; /* length of output */
44 long shift; /* number of fractional bits */
45 uint64_t val; /* value, if it fits and numchunks is zero */
46};
47
48bool round_number_string10(char *p_rdig, long len);
49
50/* format arbitrary-precision base 10 integer */
51char * format_ap_int10(struct ap_int *a,
52 char *p_end);
53
54/* format arbitrary-precision base 10 fraction */
55char * format_ap_frac10(struct ap_int *a,
56 char *p_start,
57 long precision);
58
59#endif /* AP_INT_H */