aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-24 21:41:12 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-24 21:45:25 +0100
commit33a383b98e7517a22ab5dd65cdbc947a551a5f32 (patch)
tree8829620c539cec00b75b0dc869ee8ce94c93c604
parentb5c0c9c5f4dad6648894076c15ec63ef5155e107 (diff)
libfixmath/fix16.h: replace static const with defineHEADfork
This allows using them with compile-time expressions.
-rw-r--r--libfixmath/fix16.h30
1 files changed, 18 insertions, 12 deletions
diff --git a/libfixmath/fix16.h b/libfixmath/fix16.h
index e378f6b..372f711 100644
--- a/libfixmath/fix16.h
+++ b/libfixmath/fix16.h
@@ -25,20 +25,20 @@ extern "C"
typedef int32_t fix16_t;
-static const fix16_t FOUR_DIV_PI = 0x145F3; /*!< Fix16 value of 4/PI */
-static const fix16_t _FOUR_DIV_PI2 = 0xFFFF9840; /*!< Fix16 value of -4/PI² */
-static const fix16_t X4_CORRECTION_COMPONENT = 0x399A; /*!< Fix16 value of 0.225 */
-static const fix16_t PI_DIV_4 = 0x0000C90F; /*!< Fix16 value of PI/4 */
-static const fix16_t THREE_PI_DIV_4 = 0x00025B2F; /*!< Fix16 value of 3PI/4 */
+#define FOUR_DIV_PI ((fix16_t)0x145F3) /*!< Fix16 value of 4/PI */
+#define _FOUR_DIV_PI2 ((fix16_t)0xFFFF9840) /*!< Fix16 value of -4/PI² */
+#define X4_CORRECTION_COMPONENT ((fix16_t)0x399A) /*!< Fix16 value of 0.225 */
+#define PI_DIV_4 ((fix16_t)0x0000C90F) /*!< Fix16 value of PI/4 */
+#define THREE_PI_DIV_4 ((fix16_t)0x00025B2F) /*!< Fix16 value of 3PI/4 */
-static const fix16_t fix16_maximum = 0x7FFFFFFF; /*!< the maximum value of fix16_t */
-static const fix16_t fix16_minimum = 0x80000000; /*!< the minimum value of fix16_t */
-static const fix16_t fix16_overflow = 0x80000000; /*!< the value used to indicate overflows when FIXMATH_NO_OVERFLOW is not specified */
+#define fix16_maximum ((fix16_t)0x7FFFFFFF) /*!< the maximum value of fix16_t */
+#define fix16_minimum ((fix16_t)0x80000000) /*!< the minimum value of fix16_t */
+#define fix16_overflow ((fix16_t)0x80000000) /*!< the value used to indicate overflows when FIXMATH_NO_OVERFLOW is not specified */
-static const fix16_t fix16_pi = 205887; /*!< fix16_t value of pi */
-static const fix16_t fix16_e = 178145; /*!< fix16_t value of e */
-static const fix16_t fix16_one = 0x00010000; /*!< fix16_t value of 1 */
-static const fix16_t fix16_eps = 1; /*!< fix16_t epsilon */
+#define fix16_pi ((fix16_t)205887) /*!< fix16_t value of pi */
+#define fix16_e ((fix16_t)178145) /*!< fix16_t value of e */
+#define fix16_one ((fix16_t)0x00010000) /*!< fix16_t value of 1 */
+#define fix16_eps ((fix16_t)1) /*!< fix16_t epsilon */
/* Conversion functions between fix16_t and float/integer.
* These are inlined to allow compiler to optimize away constant numbers
@@ -47,6 +47,12 @@ static inline fix16_t fix16_from_int(int a) { return a * fix16_one; }
static inline float fix16_to_float(fix16_t a) { return (float)a / fix16_one; }
static inline double fix16_to_dbl(fix16_t a) { return (double)a / fix16_one; }
+/* Static inline functions cannot be used to determine compile-time values,
+ * so use this expression instead if needed. */
+#define FIX16_C_FROM_INT(a) ((a) * fix16_one)
+#define FIX16_C_FROM_FLOAT(a) (((float)(a) * fix16_one) >= 0 ? \
+ (((float)(a) * fix16_one)) + 0.5f : ((float)(a) * fix16_one) - 0.5f)
+
static inline int fix16_to_int(fix16_t a)
{
#ifdef FIXMATH_NO_ROUNDING