blob 47b85e59ef19829737708e5d9c3703f35c5075ee
1 #ifndef _FRACMUL_H
2 #define _FRACMUL_H
4 #include <stdint.h>
5 #include "gcc_extensions.h"
7 /** FRACTIONAL MULTIPLICATION
8 * Multiply two fixed point numbers with 31 fractional bits:
9 * FRACMUL(x, y)
11 * Multiply two fixed point numbers with 31 fractional bits,
12 * then shift left by z bits:
13 * FRACMUL_SHL(x, y, z)
14 * NOTE: z must be in the range 1-8 on Coldfire targets.
18 /* A bunch of fixed point assembler helper macros */
19 #if defined(CPU_COLDFIRE)
20 /* These macros use the Coldfire EMAC extension and need the MACSR flags set
21 * to fractional mode with no rounding.
24 /* Multiply two S.31 fractional integers and return the sign bit and the
25 * 31 most significant bits of the result.
27 static inline int32_t FRACMUL(int32_t x, int32_t y)
29 int32_t t;
30 asm ("mac.l %[a], %[b], %%acc0\n\t"
31 "movclr.l %%acc0, %[t]\n\t"
32 : [t] "=r" (t) : [a] "r" (x), [b] "r" (y));
33 return t;
36 /* Multiply two S.31 fractional integers, and return the 32 most significant
37 * bits after a shift left by the constant z. NOTE: Only works for shifts of
38 * 1 to 8 on Coldfire!
40 static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
42 int32_t t, t2;
43 asm ("mac.l %[a], %[b], %%acc0\n\t"
44 "move.l %[d], %[t]\n\t"
45 "move.l %%accext01, %[t2]\n\t"
47 "lsr.l %[t], %[t2]\n\t"
48 "movclr.l %%acc0, %[t]\n\t"
49 "asl.l %[c], %[t]\n\t"
50 "or.l %[t2], %[t]\n\t"
51 : [t] "=&d" (t), [t2] "=&d" (t2)
52 : [a] "r" (x), [b] "r" (y), [mask] "d" (0xff),
53 [c] "id" ((z)), [d] "id" (8 - (z)));
54 return t;
57 #elif defined(CPU_ARM)
59 /* Multiply two S.31 fractional integers and return the sign bit and the
60 * 31 most significant bits of the result.
62 static inline int32_t FRACMUL(int32_t x, int32_t y)
64 int32_t t, t2;
65 asm ("smull %[t], %[t2], %[a], %[b]\n\t"
66 "mov %[t2], %[t2], asl #1\n\t"
67 "orr %[t], %[t2], %[t], lsr #31\n\t"
68 : [t] "=&r" (t), [t2] "=&r" (t2)
69 : [a] "r" (x), [b] "r" (y));
70 return t;
73 /* Multiply two S.31 fractional integers, and return the 32 most significant
74 * bits after a shift left by the constant z.
76 static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
78 int32_t t, t2;
79 asm ("smull %[t], %[t2], %[a], %[b]\n\t"
80 "mov %[t2], %[t2], asl %[c]\n\t"
81 "orr %[t], %[t2], %[t], lsr %[d]\n\t"
82 : [t] "=&r" (t), [t2] "=&r" (t2)
83 : [a] "r" (x), [b] "r" (y),
84 [c] "Mr" ((z) + 1), [d] "Mr" (31 - (z)));
85 return t;
88 #else
90 static inline int32_t FRACMUL(int32_t x, int32_t y)
92 return (int32_t) (((int64_t)x * y) >> 31);
95 static inline int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
97 return (int32_t) (((int64_t)x * y) >> (31 - z));
100 #endif
102 #endif