| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#define FRAC_BITS 12 |
| 22 |
#define INT_MASK 0x7FFFF000 // 20 bits |
| 23 |
#define FIXED_1 4096 // (1 << FRAC_BITS) |
| 24 |
#define FIXED_255 1044480 // (255 << FRAC_BITS) |
| 25 |
#define FIXED_HALF 2048 // (fixed_t)(0.5 * (float)(1L << FRAC_BITS) + 0.5) |
| 26 |
#define FIXED_EPSILON 1 |
| 27 |
|
| 28 |
#define ROUND_FIXED_TO_INT(x) ((int)(x < 0 ? 0 : (x > FIXED_255) ? 255 : fixed_to_int(x + FIXED_HALF))) |
| 29 |
|
| 30 |
typedef int32_t fixed_t; |
| 31 |
|
| 32 |
static inline fixed_t int_to_fixed(int32_t x) { |
| 33 |
return x << FRAC_BITS; |
| 34 |
} |
| 35 |
|
| 36 |
static inline int32_t fixed_to_int(fixed_t x) { |
| 37 |
return x >> FRAC_BITS; |
| 38 |
} |
| 39 |
|
| 40 |
static inline fixed_t float_to_fixed(float x) { |
| 41 |
return ((fixed_t)((x) * (float)(1L << FRAC_BITS) + 0.5)); |
| 42 |
} |
| 43 |
|
| 44 |
static inline float fixed_to_float(fixed_t x) { |
| 45 |
return ((float)((x) / (float)(1L << FRAC_BITS))); |
| 46 |
} |
| 47 |
|
| 48 |
#if defined(__GNUC__) |
| 49 |
# if defined(__arm__) |
| 50 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) { |
| 51 |
fixed_t __hi, __lo, __result; |
| 52 |
__asm__ __volatile__( |
| 53 |
"smull %0, %1, %3, %4\n\t" |
| 54 |
"movs %0, %0, lsr %5\n\t" |
| 55 |
"add %2, %0, %1, lsl %6" |
| 56 |
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) |
| 57 |
: "%r" (x), "r" (y), "M" (FRAC_BITS), "M" (32 - (FRAC_BITS)) |
| 58 |
: "cc" |
| 59 |
); |
| 60 |
return __result; |
| 61 |
} |
| 62 |
# elif defined(__i386__) || defined(__x86_64__) |
| 63 |
|
| 64 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) |
| 65 |
{ |
| 66 |
fixed_t __hi, __lo; |
| 67 |
__asm__ __volatile__( |
| 68 |
"imull %3\n" |
| 69 |
"shrdl %4, %1, %0" |
| 70 |
: "=a"(__lo), "=d"(__hi) |
| 71 |
: "%a"(x), "rm"(y), "I"(FRAC_BITS) |
| 72 |
: "cc" |
| 73 |
); |
| 74 |
return __lo; |
| 75 |
} |
| 76 |
# elif defined(PADRE) |
| 77 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) |
| 78 |
{ |
| 79 |
fixed_t __hi, __lo, __result; |
| 80 |
__asm__ __volatile__( |
| 81 |
" nop\n" |
| 82 |
" nop\n" |
| 83 |
" smul %3, %4, %0\n" |
| 84 |
" mov %%y, %1\n" |
| 85 |
" srl %0, %5, %0\n" |
| 86 |
" sll %1, %6, %1\n" |
| 87 |
" add %0, %1, %2\n" |
| 88 |
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) |
| 89 |
: "%r" (x), "r" (y), "M" (FRAC_BITS), "M" (32 - (FRAC_BITS)) |
| 90 |
: "cc" |
| 91 |
); |
| 92 |
return __result; |
| 93 |
} |
| 94 |
# else |
| 95 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) { |
| 96 |
return (fixed_t)(((int64_t)x * y) >> FRAC_BITS); |
| 97 |
} |
| 98 |
# endif |
| 99 |
#elif defined(_MSC_VER) |
| 100 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) { |
| 101 |
enum { |
| 102 |
fracbits = FRAC_BITS |
| 103 |
}; |
| 104 |
|
| 105 |
__asm { |
| 106 |
mov eax, x |
| 107 |
imul y |
| 108 |
shrd eax, edx, fracbits |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
#else |
| 113 |
static inline fixed_t fixed_mul(fixed_t x, fixed_t y) { |
| 114 |
return (fixed_t)(((int64_t)x * y) >> FRAC_BITS); |
| 115 |
} |
| 116 |
#endif |
| 117 |
|
| 118 |
|
| 119 |
static inline fixed_t fixed_div(fixed_t x, fixed_t y) { |
| 120 |
return (fixed_t)(((int64_t)x << FRAC_BITS) / y); |
| 121 |
} |
| 122 |
|
| 123 |
static inline fixed_t fixed_floor(fixed_t x) { |
| 124 |
return x & INT_MASK; |
| 125 |
} |