Skip to content

Commit 4260cc7

Browse files
authored
Merge pull request #45 from weihsinyeh/macro
Consolidate macros for fixed-point multiplication and division
2 parents 8653835 + 1f2d13c commit 4260cc7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

include/twin_private.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,37 @@ typedef int8_t twin_gfixed_t;
8888
#define twin_fixed_to_sfixed(f) ((twin_sfixed_t) ((f) >> 12))
8989

9090
#define twin_sfixed_to_dfixed(s) (((twin_dfixed_t) (s)) << 4)
91+
#define twin_dfixed_to_sfixed(d) ((twin_sfixed_t) ((d) >> 4))
92+
93+
/*
94+
* twin_sfixed_t a = b'10100;
95+
* twin_sfixed_t b = b'10000;
96+
* exact a is 1*(2^0) + 1*(2^(-2)) = 1.25
97+
* exact b is 1*(2^0) = 1
98+
*
99+
* The result of twin_sfixed_div(a, b) is of type twin_sfixed_t
100+
* (a << 4) / (b) = (b'101000000) / (b'10000) = b'10100
101+
* exact result is 1*(2^0) + 1*(2^(-2)) = 1.25
102+
*
103+
* twin_dfixed_t a = b'10100;
104+
* twin_dfixed_t b = b'10000;
105+
* exact a is 1*(2^(-4)) + 1*(2^(-6)) = 0.078125
106+
* exact b is 1*(2^(-4)) = 0.0625
107+
*
108+
* The result of twin_dfixed_mul(a, b) is of type twin_dfixed_t
109+
* (a * b) >> 8 = (b'101000000) >> 8 = b'1
110+
* exact result is 1*(2^(-8)) = 0.00390625
111+
*/
112+
113+
#define twin_sfixed_mul(a, b) \
114+
((((twin_sfixed_t) (a)) * ((twin_sfixed_t) (b))) >> 4)
115+
#define twin_sfixed_div(a, b) \
116+
((((twin_sfixed_t) (a)) << 4) / ((twin_sfixed_t) (b)))
117+
118+
#define twin_dfixed_mul(a, b) \
119+
((((twin_dfixed_t) (a)) * ((twin_dfixed_t) (b))) >> 8)
120+
#define twin_dfixed_div(a, b) \
121+
((((twin_dfixed_t) (a)) << 8) / ((twin_dfixed_t) (b)))
91122

92123
/*
93124
* 'double' is a no-no in any shipping code, but useful during

0 commit comments

Comments
 (0)