Maurus Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 1 | #ifndef _FRACMUL_H |
| 2 | #define _FRACMUL_H |
| 3 | |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | #include "gcc_extensions.h" |
| 6 | |
Sean Bartell | b5716df | 2011-06-24 01:25:21 -0400 | [diff] [blame] | 7 | /** FRACTIONAL MULTIPLICATION |
Maurus Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 8 | * 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énius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 27 | static 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 Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 35 | |
| 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énius | 4a04c47 | 2011-06-30 08:22:56 +0000 | [diff] [blame] | 40 | static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z) |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 41 | { |
| 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 Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 56 | |
| 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énius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 62 | static 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 Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 72 | |
| 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énius | 4a04c47 | 2011-06-30 08:22:56 +0000 | [diff] [blame] | 76 | static FORCE_INLINE int32_t FRACMUL_SHL(int32_t x, int32_t y, int z) |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 77 | { |
Thomas Jarosch | 366686b | 2011-06-28 21:16:39 +0000 | [diff] [blame] | 78 | int32_t t, t2; |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 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), |
Nils Wallménius | 4a04c47 | 2011-06-30 08:22:56 +0000 | [diff] [blame] | 84 | [c] "Mr" ((z) + 1), [d] "Mr" (31 - (z))); |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 85 | return t; |
| 86 | } |
Maurus Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 87 | |
| 88 | #else |
| 89 | |
Nils Wallménius | 5fd9471 | 2011-05-24 10:44:12 +0000 | [diff] [blame] | 90 | static inline int32_t FRACMUL(int32_t x, int32_t y) |
| 91 | { |
| 92 | return (int32_t) (((int64_t)x * y) >> 31); |
| 93 | } |
| 94 | |
| 95 | static 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 Cuelenaere | 802743a | 2009-07-05 18:06:07 +0000 | [diff] [blame] | 99 | |
| 100 | #endif |
| 101 | |
| 102 | #endif |