diff options
author | Mohamed Tarek <mt@rockbox.org> | 2009-08-13 20:38:59 +0000 |
---|---|---|
committer | Mohamed Tarek <mt@rockbox.org> | 2009-08-13 20:38:59 +0000 |
commit | 432e2ecc137d4fb4d9f6ac87cbbc38830a1f3c2c (patch) | |
tree | 27feb800527b18bbebe29c0414fc0b0e529c8ebc /apps/codecs/libatrac/fixp_math.c | |
parent | c956059ec5a2d85311914fb31a36d5127797fbc2 (diff) | |
download | rockbox-432e2ecc137d4fb4d9f6ac87cbbc38830a1f3c2c.tar.gz rockbox-432e2ecc137d4fb4d9f6ac87cbbc38830a1f3c2c.zip |
Modify libatrac to use fixed-point arithmetic.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22298 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libatrac/fixp_math.c')
-rw-r--r-- | apps/codecs/libatrac/fixp_math.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/apps/codecs/libatrac/fixp_math.c b/apps/codecs/libatrac/fixp_math.c new file mode 100644 index 0000000000..3f578a1ade --- /dev/null +++ b/apps/codecs/libatrac/fixp_math.c | |||
@@ -0,0 +1,66 @@ | |||
1 | #include "fixp_math.h" | ||
2 | |||
3 | inline int32_t fixmul31(int32_t x, int32_t y) | ||
4 | { | ||
5 | int64_t temp; | ||
6 | temp = x; | ||
7 | temp *= y; | ||
8 | |||
9 | temp >>= 31; //16+31-16 = 31 bits | ||
10 | |||
11 | return (int32_t)temp; | ||
12 | } | ||
13 | |||
14 | /* | ||
15 | * Fast integer square root adapted from algorithm, | ||
16 | * Martin Guy @ UKC, June 1985. | ||
17 | * Originally from a book on programming abaci by Mr C. Woo. | ||
18 | * This is taken from : | ||
19 | * http://wiki.forum.nokia.com/index.php/How_to_use_fixed_point_maths#How_to_get_square_root_for_integers | ||
20 | * with a added shift up of the result by 8 bits to return result in 16.16 fixed-point representation. | ||
21 | */ | ||
22 | inline int32_t fastSqrt(int32_t n) | ||
23 | { | ||
24 | /* | ||
25 | * Logically, these are unsigned. | ||
26 | * We need the sign bit to test | ||
27 | * whether (op - res - one) underflowed. | ||
28 | */ | ||
29 | int32_t op, res, one; | ||
30 | op = n; | ||
31 | res = 0; | ||
32 | /* "one" starts at the highest power of four <= than the argument. */ | ||
33 | one = 1 << 30; /* second-to-top bit set */ | ||
34 | while (one > op) one >>= 2; | ||
35 | while (one != 0) | ||
36 | { | ||
37 | if (op >= res + one) | ||
38 | { | ||
39 | op = op - (res + one); | ||
40 | res = res + (one<<1); | ||
41 | } | ||
42 | res >>= 1; | ||
43 | one >>= 2; | ||
44 | } | ||
45 | return(res << 8); | ||
46 | } | ||
47 | |||
48 | inline int32_t fixmul16(int32_t x, int32_t y) | ||
49 | { | ||
50 | int64_t temp; | ||
51 | temp = x; | ||
52 | temp *= y; | ||
53 | |||
54 | temp >>= 16; | ||
55 | |||
56 | return (int32_t)temp; | ||
57 | } | ||
58 | |||
59 | inline int32_t fixdiv16(int32_t x, int32_t y) | ||
60 | { | ||
61 | int64_t temp; | ||
62 | temp = x << 16; | ||
63 | temp /= y; | ||
64 | |||
65 | return (int32_t)temp; | ||
66 | } | ||