4 /* Macros for converting between various fixed-point representations and floating point. */
5 #define ONE_16 (1L << 16)
6 #define fixtof64(x) (float)((float)(x) / (float)(1 << 16)) //does not work on int64_t!
7 #define ftofix32(x) ((int32_t)((x) * (float)(1 << 16) + ((x) < 0 ? -0.5 : 0.5)))
8 #define ftofix31(x) ((int32_t)((x) * (float)(1 << 31) + ((x) < 0 ? -0.5 : 0.5)))
9 #define fix31tof64(x) (float)((float)(x) / (float)(1 << 31))
11 /* Fixed point math routines for use in atrac3.c */
14 #define fixmul16(X,Y) \
18 asm volatile ( /* calculates: result = (X*Y)>>16 */ \
19 "smull %0,%1,%2,%3 \n\t" /* 64 = 32x32 multiply */ \
20 "mov %0, %0, lsr #16 \n\t" /* %0 = %0 >> 16 */ \
21 "orr %0, %0, %1, lsl #16 \n\t"/* result = %0 OR (%1 << 16) */ \
22 : "=&r"(low), "=&r" (high) \
27 #define fixmul31(X,Y) \
31 asm volatile ( /* calculates: result = (X*Y)>>31 */ \
32 "smull %0,%1,%2,%3 \n\t" /* 64 = 32x32 multiply */ \
33 "mov %0, %0, lsr #31 \n\t" /* %0 = %0 >> 31 */ \
34 "orr %0, %0, %1, lsl #1 \n\t" /* result = %0 OR (%1 << 1) */ \
35 : "=&r"(low), "=&r" (high) \
39 #elif defined(CPU_COLDFIRE)
40 #define fixmul16(X,Y) \
44 "mac.l %[x],%[y],%%acc0\n\t" /* multiply */ \
45 "mulu.l %[y],%[x] \n\t" /* get lower half, avoid emac stall */ \
46 "movclr.l %%acc0,%[t1] \n\t" /* get higher half */ \
47 "moveq.l #15,%[t2] \n\t" \
48 "asl.l %[t2],%[t1] \n\t" /* hi <<= 15, plus one free */ \
49 "moveq.l #16,%[t2] \n\t" \
50 "lsr.l %[t2],%[x] \n\t" /* (unsigned)lo >>= 16 */ \
51 "or.l %[x],%[t1] \n\t" /* combine result */ \
61 #define fixmul31(X,Y) \
65 "mac.l %[x], %[y], %%acc0\n\t" /* multiply */ \
66 "movclr.l %%acc0, %[t]\n\t" /* get higher half as result */ \
68 : [x] "r" ((X)), [y] "r" ((Y))); \
72 static inline int32_t fixmul16(int32_t x
, int32_t y
)
83 static inline int32_t fixmul31(int32_t x
, int32_t y
)
89 temp
>>= 31; //16+31-16 = 31 bits
95 static inline int32_t fixdiv16(int32_t x
, int32_t y
)
101 return (int32_t)temp
;
105 * Fast integer square root adapted from algorithm,
106 * Martin Guy @ UKC, June 1985.
107 * Originally from a book on programming abaci by Mr C. Woo.
108 * This is taken from :
109 * http://wiki.forum.nokia.com/index.php/How_to_use_fixed_point_maths#How_to_get_square_root_for_integers
110 * with a added shift up of the result by 8 bits to return result in 16.16 fixed-point representation.
112 static inline int32_t fastSqrt(int32_t n
)
115 * Logically, these are unsigned.
116 * We need the sign bit to test
117 * whether (op - res - one) underflowed.
119 int32_t op
, res
, one
;
122 /* "one" starts at the highest power of four <= than the argument. */
123 one
= 1 << 30; /* second-to-top bit set */
124 while (one
> op
) one
>>= 2;
129 op
= op
- (res
+ one
);
130 res
= res
+ (one
<<1);