aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPetteri.Aimonen <Petteri.Aimonen@gmail.com>2012-08-30 16:52:52 +0000
committerPetteri.Aimonen <Petteri.Aimonen@gmail.com>2012-08-30 16:52:52 +0000
commit10af3a6c608432e9f51562f6a67e873cc111ea50 (patch)
treead1e8c964b055fe574125114a5da8130461fafc5
parent0bec62409ba354dec5ea0e3d23d9461ccba1a368 (diff)
downloadlibfixmath-10af3a6c608432e9f51562f6a67e873cc111ea50.tar.gz
Add shorthand macro F16() for defining numeric constants
-rw-r--r--libfixmath/fix16.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/libfixmath/fix16.h b/libfixmath/fix16.h
index 3b41d2c..6f3599b 100644
--- a/libfixmath/fix16.h
+++ b/libfixmath/fix16.h
@@ -75,7 +75,16 @@ static inline fix16_t fix16_from_dbl(double a)
return (fix16_t)temp;
}
+/* Macro for defining fix16_t constant values.
+ The functions above can't be used from e.g. global variable initializers,
+ and their names are quite long also. This macro is useful for constants
+ springled alongside code, e.g. F16(1.234).
+ Note that the argument is evaluated multiple times, and also otherwise
+ you should only use this for constant values. For runtime-conversions,
+ use the functions above.
+*/
+#define F16(x) ((fix16_t)(((x) >= 0) ? ((x) * 65536.0 + 0.5) : ((x) * 65536.0 - 0.5)))
static inline fix16_t fix16_abs(fix16_t x)
{ return (x < 0 ? -x : x); }