blob: 47b85e59ef19829737708e5d9c3703f35c5075ee [file] [log] [blame]
Maurus Cuelenaere802743a2009-07-05 18:06:07 +00001#ifndef _FRACMUL_H
2#define _FRACMUL_H
3
Nils Wallménius5fd94712011-05-24 10:44:12 +00004#include <stdint.h>
5#include "gcc_extensions.h"
6
Sean Bartellb5716df2011-06-24 01:25:21 -04007/** FRACTIONAL MULTIPLICATION
Maurus Cuelenaere802743a2009-07-05 18:06:07 +00008 * Multiply two fixed point numbers with 31 fractional bits:
9 * FRACMUL(x, y)
10 *
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.
15 */
16
17
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.
22 */
23
24/* Multiply two S.31 fractional integers and return the sign bit and the
25 * 31 most significant bits of the result.
26 */
Nils Wallménius5fd94712011-05-24 10:44:12 +000027static inline int32_t FRACMUL(int32_t x, int32_t y)
28{
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;
34}
Maurus Cuelenaere802743a2009-07-05 18:06:07 +000035
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!
39 */
Nils Wallménius4a04c472011-06-30 08:22:56 +000040static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
Nils Wallménius5fd94712011-05-24 10:44:12 +000041{
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"
46 "and.l %[mask], %[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;
55}
Maurus Cuelenaere802743a2009-07-05 18:06:07 +000056
57#elif defined(CPU_ARM)
58
59/* Multiply two S.31 fractional integers and return the sign bit and the
60 * 31 most significant bits of the result.
61 */
Nils Wallménius5fd94712011-05-24 10:44:12 +000062static inline int32_t FRACMUL(int32_t x, int32_t y)
63{
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;
71}
Maurus Cuelenaere802743a2009-07-05 18:06:07 +000072
73/* Multiply two S.31 fractional integers, and return the 32 most significant
74 * bits after a shift left by the constant z.
75 */
Nils Wallménius4a04c472011-06-30 08:22:56 +000076static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
Nils Wallménius5fd94712011-05-24 10:44:12 +000077{
Thomas Jarosch366686b2011-06-28 21:16:39 +000078 int32_t t, t2;
Nils Wallménius5fd94712011-05-24 10:44:12 +000079 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),
Nils Wallménius4a04c472011-06-30 08:22:56 +000084 [c] "Mr" ((z) + 1), [d] "Mr" (31 - (z)));
Nils Wallménius5fd94712011-05-24 10:44:12 +000085 return t;
86}
Maurus Cuelenaere802743a2009-07-05 18:06:07 +000087
88#else
89
Nils Wallménius5fd94712011-05-24 10:44:12 +000090static inline int32_t FRACMUL(int32_t x, int32_t y)
91{
92 return (int32_t) (((int64_t)x * y) >> 31);
93}
94
95static inline int32_t FRACMUL_SHL(int32_t x, int32_t y, int z)
96{
97 return (int32_t) (((int64_t)x * y) >> (31 - z));
98}
Maurus Cuelenaere802743a2009-07-05 18:06:07 +000099
100#endif
101
102#endif