PandA-2024.02
fixedptc.h
Go to the documentation of this file.
1 
2 #ifndef _FIXEDPTC_H_
3 #define _FIXEDPTC_H_
4 
5 #include <stdint.h>
6 
7 /*
8  * fixedptc.h is a 32-bit or 64-bit fixed point numeric library.
9  *
10  * The symbol FIXEDPT_BITS, if defined before this library header file
11  * is included, determines the number of bits in the data type (its "width").
12  * The default width is 32-bit (FIXEDPT_BITS=32) and it can be used
13  * on any recent C99 compiler. The 64-bit precision (FIXEDPT_BITS=64) is
14  * available on compilers which implement 128-bit "long long" types. This
15  * precision has been tested on GCC 4.2+.
16  *
17  * The FIXEDPT_WBITS symbols governs how many bits are dedicated to the
18  * "whole" part of the number (to the left of the decimal point). The larger
19  * this width is, the larger the numbers which can be stored in the fixedpt
20  * number. The rest of the bits (available in the FIXEDPT_FBITS symbol) are
21  * dedicated to the fraction part of the number (to the right of the decimal
22  * point).
23  *
24  * Since the number of bits in both cases is relatively low, many complex
25  * functions (more complex than div & mul) take a large hit on the precision
26  * of the end result because errors in precision accumulate.
27  * This loss of precision can be lessened by increasing the number of
28  * bits dedicated to the fraction part, but at the loss of range.
29  *
30  * Adventurous users might utilize this library to build two data types:
31  * one which has the range, and one which has the precision, and carefully
32  * convert between them (including adding two number of each type to produce
33  * a simulated type with a larger range and precision).
34  *
35  * The ideas and algorithms have been cherry-picked from a large number
36  * of previous implementations available on the Internet.
37  * Tim Hartrick has contributed cleanup and 64-bit support patches.
38  *
39  * == Special notes for the 32-bit precision ==
40  * Signed 32-bit fixed point numeric library for the 24.8 format.
41  * The specific limits are -8388608.999... to 8388607.999... and the
42  * most precise number is 0.00390625. In practice, you should not count
43  * on working with numbers larger than a million or to the precision
44  * of more than 2 decimal places. Make peace with the fact that PI
45  * is 3.14 here. :)
46  */
47 
48 /*-
49  * Copyright (c) 2010-2012 Ivan Voras <ivoras@freebsd.org>
50  * Copyright (c) 2012 Tim Hartrick <tim@edgecast.com>
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions
54  * are met:
55  * 1. Redistributions of source code must retain the above copyright
56  * notice, this list of conditions and the following disclaimer.
57  * 2. Redistributions in binary form must reproduce the above copyright
58  * notice, this list of conditions and the following disclaimer in the
59  * documentation and/or other materials provided with the distribution.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  */
73 
74 #ifndef FIXEDPT_BITS
75 #define FIXEDPT_BITS 32
76 #endif
77 
78 #if FIXEDPT_BITS == 32
79 typedef int32_t fixedpt;
80 typedef int64_t fixedptd;
81 typedef uint32_t fixedptu;
82 typedef uint64_t fixedptud;
83 #elif FIXEDPT_BITS == 64
84 typedef int64_t fixedpt;
85 //typedef long long __int128_t;
86 typedef __int128_t fixedptd;
87 typedef uint64_t fixedptu;
88 //typedef unsigned long long __uint128_t;
89 typedef __uint128_t fixedptud;
90 #else
91 #error "FIXEDPT_BITS must be equal to 32 or 64"
92 #endif
93 
94 #ifndef FIXEDPT_WBITS
95 #define FIXEDPT_WBITS 16
96 #endif
97 
98 #if FIXEDPT_WBITS >= FIXEDPT_BITS
99 #error "FIXEDPT_WBITS must be less than or equal to FIXEDPT_BITS"
100 #endif
101 
102 #define FIXEDPT_VCSID "$Id$"
103 
104 #define FIXEDPT_FBITS (FIXEDPT_BITS - FIXEDPT_WBITS)
105 #define FIXEDPT_FMASK (((fixedpt)1 << FIXEDPT_FBITS) - 1)
106 
107 #define fixedpt_rconst(R) ((fixedpt)((R) * FIXEDPT_ONE + ((R) >= 0 ? 0.5 : -0.5)))
108 #define fixedpt_fromint(I) ((fixedptd)(I) << FIXEDPT_FBITS)
109 #define fixedpt_toint(F) ((F) >> FIXEDPT_FBITS)
110 #define fixedpt_add(A,B) ((A) + (B))
111 #define fixedpt_sub(A,B) ((A) - (B))
112 #define fixedpt_xmul(A,B) \
113  ((fixedpt)(((fixedptd)(A) * (fixedptd)(B)) >> FIXEDPT_FBITS))
114 #define fixedpt_xdiv(A,B) \
115  ((fixedpt)(((fixedptd)(A) << FIXEDPT_FBITS) / (fixedptd)(B)))
116 #define fixedpt_fracpart(A) ((fixedpt)(A) & FIXEDPT_FMASK)
117 
118 #define FIXEDPT_ONE ((fixedpt)((fixedpt)1 << FIXEDPT_FBITS))
119 #define FIXEDPT_ONE_HALF (FIXEDPT_ONE >> 1)
120 #define FIXEDPT_TWO (FIXEDPT_ONE + FIXEDPT_ONE)
121 #define FIXEDPT_PI fixedpt_rconst(3.14159265358979323846)
122 #define FIXEDPT_TWO_PI fixedpt_rconst(2 * 3.14159265358979323846)
123 #define FIXEDPT_HALF_PI fixedpt_rconst(3.14159265358979323846 / 2)
124 #define FIXEDPT_E fixedpt_rconst(2.7182818284590452354)
125 
126 #define fixedpt_abs(A) ((A) < 0 ? -(A) : (A))
127 
128 /* Multiplies two fixedpt numbers, returns the result. */
130 
131 /* Divides two fixedpt numbers, returns the result. */
133 
134 //fixedpt fixedpt_mod(fixedpt A, fixedpt B);
135 
136 /*
137  * Note: adding and substracting fixedpt numbers can be done by using
138  * the regular integer operators + and -.
139  */
140 
150 void fixedpt_str(fixedpt A, char *str, int max_dec);
151 
152 /* Converts the given fixedpt number into a string, using a static
153  * (non-threadsafe) string buffer */
154 char* fixedpt_cstr(const fixedpt A, const int max_dec);
155 
156 /* Returns the square root of the given number, or -1 in case of error */
158 
159 /* Returns the sine of the given fixedpt number.
160  * Note: the loss of precision is extraordinary! */
162 
163 /* Returns the cosine of the given fixedpt number */
165 
166 /* Returns the tangens of the given fixedpt number */
168 
169 /* Returns the value exp(x), i.e. e^x of the given fixedpt number. */
171 
172 /* Returns the natural logarithm of the given fixedpt number. */
174 
175 /* Returns the logarithm of the given base of the given fixedpt number */
177 
178 
179 /* Return the power value (n^exp) of the given fixedpt numbers */
181 
182 void fixedpt_print(fixedpt A);
184 
185 #endif
fixedpt fixedpt_ln(fixedpt x)
Definition: fixedptc.c:259
char base
This version is stamped on May 10, 2016.
Definition: nussinov.c:24
fixedpt fixedpt_sqrt(fixedpt A)
Definition: fixedptc.c:96
#define A
Definition: generate.c:13
fixedpt fixedpt_log(fixedpt x, fixedpt base)
Definition: fixedptc.c:298
uint32_t fixedptu
Definition: fixedptc.h:81
void fixedpt_print_file(fixedpt A)
fixedpt fixedpt_pow(fixedpt n, fixedpt exp)
Definition: fixedptc.c:304
fixedpt fixedpt_tan(fixedpt A)
Definition: fixedptc.c:168
void fixedpt_str(fixedpt A, char *str, int max_dec)
Convert the given fixedpt number to a decimal string.
Definition: fixedptc.c:35
fixedpt fixedpt_exp(fixedpt fp)
Definition: fixedptc.c:174
char * fixedpt_cstr(const fixedpt A, const int max_dec)
Definition: fixedptc.c:88
void fixedpt_print(fixedpt A)
fixedpt fixedpt_div(fixedpt A, fixedpt B)
Definition: fixedptc.c:17
int64_t fixedptd
Definition: fixedptc.h:80
fixedpt fixedpt_mul(fixedpt A, fixedpt B)
Definition: fixedptc.c:11
fixedpt fixedpt_sin(fixedpt fp)
Definition: fixedptc.c:130
char str[25]
Definition: fixedptc.c:8
uint64_t fixedptud
Definition: fixedptc.h:82
fixedpt fixedpt_cos(fixedpt A)
Definition: fixedptc.c:162
x
Return the smallest n such that 2^n >= _x.
#define B
Definition: generate.c:14
int32_t fixedpt
Definition: fixedptc.h:79
uint32_t exp

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13