X-Git-Url: https://git.rohieb.name/hackover2013-badge-firmware.git/blobdiff_plain/113fbd0d57be3df9bc6ac56b44d8bd154cc90d38..f80406559852f60c406f655c6138d1b7bcbee2e2:/badge/util/fixed_point.h?ds=inline diff --git a/badge/util/fixed_point.h b/badge/util/fixed_point.h index 05a8c79..43c7bf0 100644 --- a/badge/util/fixed_point.h +++ b/badge/util/fixed_point.h @@ -23,13 +23,19 @@ static inline bool fixed_point_eq(fixed_point x, fixed_point y) { return x.data static inline bool fixed_point_ne(fixed_point x, fixed_point y) { return x.data != y.data; } #define FIXED_POINT_I(x, y) { ((x) * 256) + ((y) * 256 / 1000) } +#define FIXED_INT_I(x) FIXED_POINT_I(x, 0) -static inline fixed_point FIXED_POINT(unsigned x, unsigned y) { - fixed_point r = { ((int) x * 256) + ((int) y * 256 / 1000) }; +static inline fixed_point FIXED_POINT(int32_t x, int32_t y) { + fixed_point r = FIXED_POINT_I(x, y); return r; } -static inline int fixed_point_cast_int(fixed_point x) { return x.data / 256; } +static inline fixed_point FIXED_INT(int32_t x) { return FIXED_POINT(x, 0); } + +// sign bit is shifted in if x.data < 0, so this is x.data / 256 - (x.data < 0). +// This means 0.123 is cast to 1, which is what we want when we cast a model coordinate +// to a screen coordinate. +static inline int fixed_point_cast_int(fixed_point x) { return x.data >> 8; } static inline fixed_point fixed_point_min(fixed_point x, fixed_point y) { return fixed_point_lt(x, y) ? x : y; } static inline fixed_point fixed_point_max(fixed_point x, fixed_point y) { return fixed_point_gt(x, y) ? x : y; }