Fix fixed-point returns

This commit is contained in:
2024-10-16 19:48:00 +01:00
parent d5f913de2b
commit a7a6d0b9d4

View File

@@ -17,14 +17,14 @@ typedef struct
inline fp32_t inline fp32_t
int_to_fp (int32_t n) int_to_fp (int32_t n)
{ {
return { n * CONVERSION_CONST }; return (fp32_t){ n * CONVERSION_CONST };
} }
/* Handles conversion of fixed point to integer. First version truncates, second one rounds */ /* Handles conversion of fixed point to integer. First version truncates, second one rounds */
inline int32_t inline int32_t
fp_floor (fp32_t x) fp_floor (fp32_t x)
{ {
return { x.raw / CONVERSION_CONST }; return x.raw / CONVERSION_CONST;
} }
inline int32_t inline int32_t
@@ -40,56 +40,56 @@ fp_round (fp32_t x)
inline fp32_t inline fp32_t
fp_add (fp32_t x, fp32_t y) fp_add (fp32_t x, fp32_t y)
{ {
return { x.raw + y.raw }; return (fp32_t){ x.raw + y.raw };
} }
/* Subtract two fixed points */ /* Subtract two fixed points */
inline fp32_t inline fp32_t
fp_sub (fp32_t x, fp32_t y) fp_sub (fp32_t x, fp32_t y)
{ {
return { x.raw - y.raw }; return (fp32_t){ x.raw - y.raw };
} }
/* Add fixed point to integer */ /* Add fixed point to integer */
inline fp32_t inline fp32_t
fp_add_int (fp32_t x, int32_t n) fp_add_int (fp32_t x, int32_t n)
{ {
return { x.raw + n * CONVERSION_CONST }; return (fp32_t){ x.raw + n * CONVERSION_CONST };
} }
/* Subtract integer from fixed point */ /* Subtract integer from fixed point */
inline fp32_t inline fp32_t
fp_sub_int (fp32_t x, int32_t n) fp_sub_int (fp32_t x, int32_t n)
{ {
return { x.raw - n * CONVERSION_CONST }; return (fp32_t){ x.raw - n * CONVERSION_CONST };
} }
/* Multiple two fixed points */ /* Multiple two fixed points */
inline fp32_t inline fp32_t
fp_mul (fp32_t x, fp32_t y) fp_mul (fp32_t x, fp32_t y)
{ {
return { ((int64_t)x.raw) * y.raw / CONVERSION_CONST }; return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_CONST };
} }
/* Divide two fixed points */ /* Divide two fixed points */
inline fp32_t inline fp32_t
fp_div (fp32_t x, fp32_t y) fp_div (fp32_t x, fp32_t y)
{ {
return { ((int64_t)x.raw) * CONVERSION_CONST / y.raw }; return (fp32_t){ ((int64_t)x.raw) * CONVERSION_CONST / y.raw };
} }
/* Multiply fixed point and integer */ /* Multiply fixed point and integer */
inline fp32_t inline fp32_t
fp_mul_int (fp32_t x, int32_t n) fp_mul_int (fp32_t x, int32_t n)
{ {
return { x.raw * n }; return (fp32_t){ x.raw * n };
} }
/* Divide fixed point by integer */ /* Divide fixed point by integer */
inline fp32_t inline fp32_t
fp_div_int (fp32_t x, int32_t n) fp_div_int (fp32_t x, int32_t n)
{ {
return { x.raw / n }; return (fp32_t){ x.raw / n };
} }
#endif //FIXED_POINT_H #endif //FIXED_POINT_H