-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedpoint.c
46 lines (36 loc) · 930 Bytes
/
fixedpoint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "fixedpoint.h"
enum { N_FRACT_BITS = 26 };
fxp26 fxp26_from_int(int a) {
const fxp26 ret = {.val = a << N_FRACT_BITS};
return ret;
}
fxp26 fxp26_from_fl(float f) {
const fxp26 ret = {.val = f * (1 << N_FRACT_BITS)};
return ret;
}
fxp26 fxp26_from_dbl(double d) {
const fxp26 ret = {.val = d * (1 << N_FRACT_BITS)};
return ret;
}
float fxp26_to_fl(fxp26 a) {
float x = a.val;
return x / (1 << N_FRACT_BITS);
}
double fxp26_to_dbl(fxp26 a) {
double x = a.val;
return x / (1 << N_FRACT_BITS);
}
fxp26 fxp26_neg(fxp26 a) {
a.val *= -1;
return a;
}
fxp26 fxp26_mul(fxp26 a, fxp26 b) {
const fxp26 ret = {.val = (a.val >> (N_FRACT_BITS / 2)) *
(b.val >> (N_FRACT_BITS / 2))};
return ret;
}
fxp26 fxp26_add(fxp26 a, fxp26 b) {
const fxp26 ret = {.val = (a.val + b.val)};
return ret;
}
fxp26 fxp26_sub(fxp26 a, fxp26 b) { return fxp26_add(a, fxp26_neg(b)); }