From 7bf92a1ee80c988f736d4f3f1a5a8edbca74456e Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 24 Aug 2009 21:27:30 +0200 Subject: * digital/avr/modules/math/fixed: - round division result (closes #96). - changed macros to inlined. --- digital/avr/modules/math/fixed/fixed.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'digital/avr/modules/math/fixed/fixed.h') diff --git a/digital/avr/modules/math/fixed/fixed.h b/digital/avr/modules/math/fixed/fixed.h index 7bc45d6a..89aa5ba4 100644 --- a/digital/avr/modules/math/fixed/fixed.h +++ b/digital/avr/modules/math/fixed/fixed.h @@ -54,13 +54,23 @@ fixed_div_f824 (int32_t a, int32_t b); #else /* HOST */ /** Multiply f8.24 by f8.24, return f8.24. */ -#define fixed_mul_f824(a, b) \ - (((int64_t) (int32_t) (a) * (int64_t) (int32_t) (b) \ - + 0x800000LL) >> 24) +extern inline +int32_t +fixed_mul_f824 (int32_t a, int32_t b) +{ + return ((int64_t) a * b + 0x800000) >> 24; +} /** Divide f8.24 by f8.24, return f8.24. */ -#define fixed_div_f824(a, b) \ - (((int64_t) (int32_t) (a) << 24) / (int64_t) (int32_t) (b)) +extern inline +int32_t +fixed_div_f824 (int32_t a, int32_t b) +{ + if ((a ^ b) >= 0) + return (((int64_t) a << 24) + b / 2) / b; + else + return (((int64_t) a << 24) - b / 2) / b; +} #endif -- cgit v1.2.3