fpmath.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Voxel-a-tord
  3. * fpmath.h: fixed point math implementation.
  4. * Copyright (c) 2022 986-Studio. All rights reserved.
  5. *
  6. * Created by Manoël Trapier on 29/09/2022..
  7. */
  8. #include <stdint.h>
  9. #include <math.h>
  10. #ifndef VOXELATOR_FPMATH_H
  11. #define VOXELATOR_FPMATH_H
  12. /* Enable to debug code without having the fix point implementation interfering */
  13. //#define USE_FLOATING_POINT
  14. #ifndef USE_FLOATING_POINT
  15. /*** Setting for the Fixed Point numbers. ***/
  16. typedef int32_t fp_num_t; /**< The base type used for the FP number */
  17. /* These defines should not be used outside of this header file */
  18. #define FP_DECIMAL_BITS (16) /**< Number of bit used for the decimal part */
  19. #define FP_DIVISOR (1 << FP_DECIMAL_BITS)
  20. #define FP_GET_INT(_fp) ((_fp) / FP_DIVISOR)
  21. #define FP_SET_INT(_val) (fp_num_t)((_val) * FP_DIVISOR)
  22. #define FP_GET_FRAC(_val) (fp_num_t)(_val - FP_GET_INT(_val))
  23. #define FP_SET_FLT(_val) (fp_num_t)(floor((_val) * FP_DIVISOR))
  24. #define FP_MUL(_a, _b) (fp_num_t)(((int64_t)(_a) * (int64_t)(_b)) / FP_DIVISOR)
  25. #define FP_DIV(_a, _b) (fp_num_t)(((int64_t)(_a) * FP_DIVISOR) / (_b))
  26. #define fpsin(_val) (fp_num_t)(sin((_val) / 180 * M_PI) * FP_DIVISOR)
  27. #define fpcos(_val) (fp_num_t)(cos((_val) / 180 * M_PI) * FP_DIVISOR)
  28. #else
  29. typedef double fp_num_t;
  30. #define FP_GET_INT(_fp) ( floor(_fp) )
  31. #define FP_SET_INT(_val) (fp_num_t)(_val)
  32. #define FP_GET_FRAC(_val) (fp_num_t)(_val - FP_GET_INT(_val))
  33. #define FP_SET_FLT(_val) (fp_num_t)(_val)
  34. #define FP_MUL(_a, _b) ((_a) * (_b))
  35. #define FP_DIV(_a, _b) ((_a) / (_b))
  36. #define fpsin(_val) sin((_val) / 180 * M_PI)
  37. #define fpcos(_val) cos((_val) / 180 * M_PI)
  38. #endif
  39. #endif /* VOXELATOR_FPMATH_H */