Fixed Point Arithmetic  0
Fixed-point math library.
fixed_point.h
Go to the documentation of this file.
1 
42 /* Some compilers, such as GCC, support the "extern inline" definition which
43  * allows the inlining version of functions to be defined in the header. This
44  * leaves to the decision of where to inline to the compiler (or to the user by
45  * means of compiler switches). For those with less advanced compilers, they
46  * have to choose: either have the routines in a different module and do
47  * zero inlining or include them in ALL source files with the "static" keyword
48  * so one may have inlining and optimizations.
49  */
50 
51 #include <stdbool.h>
52 #include "../fixed_point.h"
53 
69 FXP_SCMP(f_gt, >)
70 
71 
78 FXP_SCMP(f_ge, >=)
79 
87 FXP_SCMP(f_lt, <)
88 
96 FXP_SCMP(f_le, <=)
97 
105 FXP_SCMP(f_eq, ==)
106 
114 FXP_DCMP(df_gt, >)
115 
123 FXP_DCMP(df_ge, >=)
124 
132 FXP_DCMP(df_lt, <)
133 
141 FXP_DCMP(df_le, <=)
142 
150 FXP_DCMP(df_eq, ==)
151 
159 FXP_OP3(f_add, frac, +)
160 
162 FXP_OP3(f_sub, frac, -)
163 
165 FXP_OP3(df_add, dfrac, +)
166 
168 FXP_OP3(df_sub, dfrac, -)
169 
171 FXP_OP3(ef_add, efrac, +)
172 
174 FXP_OP3(ef_sub, efrac, -)
175 
178 {
179  a.v += b.v;
180  return a;
181 }
182 
185 {
186  frac r = {-a.v};
187  return r;
188 }
189 
192 {
193  dfrac r = {-a.v};
194  return r;
195 }
196 
199 {
200  efrac r = {-a.v};
201  return r;
202 }
203 
215 {
216  frac r = {((((dfrac_base)a.v) * b.v) << 1) >> FRAC_BIT};
217  return r;
218 }
219 
229 {
230  dfrac r = {(((dfrac_base)a.v) * b.v)};
231  return r;
232 }
233 
243 {
244  efrac r = {(((efrac_base)a.v) * b.v)>>8};
245  return r;
246 }
247 
258 {
259  frac r = { a.v * b };
260  return r;
261 }
262 
273 {
274  return (((int64_t)b)*a.v) >> 15;
275 }
276 
287 {
288  efrac r = { ((efrac_base)a.v) * b };
289  return r;
290 }
291 
302 {
303  dfrac r = { a.v * b };
304  return r;
305 }
306 
317 {
318  efrac r = { a.v * b };
319  return r;
320 }
321 
332 {
333  frac r = { a.v / b };
334  return r;
335 }
336 
347 {
348  dfrac r = { a.v / b };
349  return r;
350 }
351 
362 {
363  efrac r = { a.v / b };
364  return r;
365 }
366 
376 {
377  dfrac r = { a.v << b };
378  return r;
379 }
380 
390 {
391  dfrac r = { a.v >> b };
392  return r;
393 }
394 
407 { /* 2.30 -> 1.15 */
408  frac r;
409 
410  if (x.v >= DFRAC_1_V)
411  r.v = FRAC_1_V;
412  else if ( x.v < DFRAC_minus1_V)
413  r.v = FRAC_minus1_V;
414  else
415  r.v = (x.v << 1) >> FRAC_BIT;
416 
417  return r;
418 }
419 
427 { /* 1.15 -> 2.30 */
428  dfrac r = { ((dfrac_base)x.v)<<(FRAC_BIT - 1) };
429  return r;
430 }
431 
436 {
437  efrac r = {((efrac_base)x.v)};
438  return r;
439 }
440 
445 {
446  /* When dividing, the implicit divisors (2**(FRAC_BIT-1)) cancel out,
447  * and we get the ratio as an integer. To get a efrac we must multiply
448  * the divident by 2**(FRAC_BIT-1) BEFORE taking the quotient */
449  efrac_base new_dividend = f_to_ef(dividend).v << (FRAC_BIT - 1);
450  efrac r = {new_dividend/divisor.v};
451 
452  return r;
453 }
454 
461 {
462  frac r = { (x.v > FRAC_minus1_V)?
463  ((x.v < FRAC_1_V)? x.v : FRAC_1_V)
464  : FRAC_minus1_V
465  };
466  return r;
467 }
468 
479 {
480 
481  frac r = {(x.v > -limit.v)? ((x.v < limit.v)? x.v : limit.v) : -limit.v};
482 
483  return r;
484 }
485 
493 {
494  dfrac_base result = x1.v + x2.v;
495  dfrac r;
496 
497  if (x1.v > 0 && x2.v > 0)
498  r.v = ((result < x1.v) ? DFRAC_MAX_V : result);
499  else if (x1.v < 0 && x2.v < 0)
500  r.v = ((result > x1.v) ? DFRAC_MIN_V : result);
501  else
502  r.v = result;
503 
504  return r;
505 }
506 
519 {
520  return df_addsat(z, f_mul_df(x,y));
521 }
522 
efrac f_to_ef(frac x)
Extend a single precision fractional to an extended-fractional.
Definition: fixed_point.h:435
dfrac df_shiftr(dfrac a, int16_t b)
Arithmetic shift right a double precision fractional.
Definition: fixed_point.h:389
bool f_eq(frac a, frac b)
Equality comparison - single precision.
Definition: fixed_point.h:105
bool f_lt(frac a, frac b)
Less-than comparison - single precision.
Definition: fixed_point.h:87
efrac f_imul_ef(frac a, int16_t b)
Multiply single precision by integer, yield extended precision.
Definition: fixed_point.h:286
16 bit fractional number in Q8.8 format.
Definition: types.h:76
dfrac df_imul(dfrac a, int16_t b)
Multiply double precision by integer, yield double precision.
Definition: fixed_point.h:301
frac f_imul(frac a, int16_t b)
Multiply single precision by integer, yield single precision.
Definition: fixed_point.h:257
frac_base v
Definition: types.h:87
frac f_sub(frac a, frac b)
Substract two single precision fractional numbers - may overflow.
Definition: fixed_point.h:162
frac df_to_f(dfrac x)
Truncate to single precision.
Definition: fixed_point.h:406
dfrac f_to_df(frac x)
Extend a single precision number to double precision.
Definition: fixed_point.h:426
bool df_ge(dfrac a, dfrac b)
Greater or equal comparison - double precision.
Definition: fixed_point.h:123
bool df_lt(dfrac a, dfrac b)
Less-than comparison - double precision.
Definition: fixed_point.h:132
dfrac df_shiftl(dfrac a, int16_t b)
Arithmetic shift left a double precision fractional.
Definition: fixed_point.h:375
frac f_idiv(frac a, int16_t b)
Divide single precision by integer, yield single precision.
Definition: fixed_point.h:331
efrac ef_neg(efrac a)
Negate extended precision fractional.
Definition: fixed_point.h:198
int32_t dfrac_base
Definition: types.h:66
efrac ef_idiv(efrac a, int16_t b)
Divide extended precision by integer, yield extended precision.
Definition: fixed_point.h:361
int32_t efrac_base
Definition: types.h:67
frac f_neg(frac a)
Negate single precision fractional.
Definition: fixed_point.h:184
dfrac f_macs_df(frac x, frac y, dfrac z)
Multiply-accumulate with saturation.
Definition: fixed_point.h:518
dfrac df_sub(dfrac a, dfrac b)
Substract two double precision fractional numbers - may overflow.
Definition: fixed_point.h:168
bool df_gt(dfrac a, dfrac b)
Greater-than comparison - double precision.
Definition: fixed_point.h:114
dfrac df_neg(dfrac a)
Negate double precision fractional.
Definition: fixed_point.h:191
efrac ef_f_add(efrac a, frac b)
Add a single precision fractional to an extended precision fractional.
Definition: fixed_point.h:177
dfrac df_idiv(dfrac a, int16_t b)
Divide double precision by integer, yield double precision.
Definition: fixed_point.h:346
efrac ef_sub(efrac a, efrac b)
Substract two extended precision fractional numbers - may overflow.
Definition: fixed_point.h:174
32 bit fractional number in Q2.30 format.
Definition: types.h:99
#define FXP_DECLARATION
Definition: quaternion.h:51
frac f_clip(frac x, frac limit)
Clip a number to lie between -limit and limit.
Definition: fixed_point.h:478
dfrac f_mul_df(frac a, frac b)
Multiply single precision, yield double precision.
Definition: fixed_point.h:228
frac ef_to_f(efrac x)
Saturate an extended fractional to yield a fractional.
Definition: fixed_point.h:460
dfrac_base v
Definition: types.h:100
mfrac_base v
Definition: types.h:77
bool f_le(frac a, frac b)
Less or equal comparison - single precision.
Definition: fixed_point.h:96
frac f_add(frac a, frac b)
Add two single precision fractional numbers - may overflow.
Definition: fixed_point.h:159
bool df_eq(dfrac a, dfrac b)
Equality comparison - double precision.
Definition: fixed_point.h:150
16 bit fractional number in Q1.15 format.
Definition: types.h:86
efrac ef_add(efrac a, efrac b)
Add two extended precision fractional numbers - may overflow.
Definition: fixed_point.h:171
bool df_le(dfrac a, dfrac b)
Less or equal comparison - double precision.
Definition: fixed_point.h:141
bool f_gt(frac a, frac b)
Greater-than comparison - single precision.
Definition: fixed_point.h:69
efrac f_ef_div(frac dividend, efrac divisor)
Divide a frac by an efrac and return an efrac.
Definition: fixed_point.h:444
int f_imul_i(frac a, int b)
Multiply single precision by integer, yield integer.
Definition: fixed_point.h:272
dfrac df_add(dfrac a, dfrac b)
Add two double precision fractional numbers - may overflow.
Definition: fixed_point.h:165
frac f_mul(frac a, frac b)
Multiply single precision, yield single precision.
Definition: fixed_point.h:214
#define FRAC_BIT
Definition: types.h:144
dfrac df_addsat(dfrac x1, dfrac x2)
Add with saturation.
Definition: fixed_point.h:492
efrac ef_imul(efrac a, int16_t b)
Multiply extended precision by integer, yield extended precision.
Definition: fixed_point.h:316
32 bit fractional number in Q17.15 format.
Definition: types.h:112
efrac f_mf_mul_ef(frac a, mfrac b)
Multiply single precision by mixed fractional, yield extended precision,.
Definition: fixed_point.h:242
bool f_ge(frac a, frac b)
Greater or equal comparison - single precision.
Definition: fixed_point.h:78
efrac_base v
Definition: types.h:113