You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.3KB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2015 Leonardo Laguna Ruiz
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. NOTE: The code for the fixed-point operations is based on the project:
  20. https://code.google.com/p/libfixmath/
  21. */
  22. #ifndef VULTIN_H
  23. #define VULTIN_H
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #ifdef _MSC_VER
  27. #define static_inline static __inline
  28. #else
  29. #define static_inline static inline
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. typedef int32_t fix16_t;
  35. // Type conversion
  36. static_inline float fix_to_float(fix16_t a)
  37. {
  38. return (float)a / 0x00010000;
  39. }
  40. static_inline fix16_t float_to_fix(float a)
  41. {
  42. float temp = a * 0x00010000;
  43. return (fix16_t)temp;
  44. }
  45. static_inline float int_to_float(int a)
  46. {
  47. return (float)a;
  48. }
  49. static_inline int float_to_int(float a)
  50. {
  51. return (int)a;
  52. }
  53. static_inline fix16_t int_to_fix(int a)
  54. {
  55. return a * 0x00010000;
  56. }
  57. static_inline int fix_to_int(fix16_t a)
  58. {
  59. return (a >> 16);
  60. }
  61. static_inline int int_clip(int v, int minv, int maxv)
  62. {
  63. return v > maxv ? maxv : (v < minv ? minv : v);
  64. }
  65. // Basic operations for fixed point numbers
  66. static_inline fix16_t fix_add(fix16_t x, fix16_t y)
  67. {
  68. return x + y;
  69. }
  70. static_inline fix16_t fix_sub(fix16_t x, fix16_t y)
  71. {
  72. return x - y;
  73. }
  74. static_inline fix16_t fix_mul(fix16_t x, fix16_t y)
  75. {
  76. int64_t res = (int64_t)x * y;
  77. return (fix16_t)(res >> 16);
  78. }
  79. static_inline fix16_t fix_div(fix16_t a, fix16_t b)
  80. {
  81. if (b == 0)
  82. return 0;
  83. fix16_t result = (((int64_t)a) << 16) / ((int64_t)b);
  84. return result;
  85. }
  86. static_inline fix16_t fix_minus(fix16_t x)
  87. {
  88. return -x;
  89. }
  90. static_inline fix16_t fix_abs(fix16_t x)
  91. {
  92. return x < 0 ? (-x) : x;
  93. }
  94. static_inline fix16_t fix_min(fix16_t a, fix16_t b)
  95. {
  96. return a < b ? a : b;
  97. }
  98. static_inline fix16_t fix_max(fix16_t a, fix16_t b)
  99. {
  100. return a > b ? a : b;
  101. }
  102. static_inline fix16_t fix_clip(fix16_t v, fix16_t minv, fix16_t maxv)
  103. {
  104. return v > maxv ? maxv : (v < minv ? minv : v);
  105. }
  106. static_inline fix16_t fix_floor(fix16_t x)
  107. {
  108. return (x & 0xFFFF0000);
  109. }
  110. static_inline fix16_t fix_not(fix16_t x)
  111. {
  112. return ~x;
  113. }
  114. static_inline float float_eps()
  115. {
  116. return 1e-18f;
  117. }
  118. static_inline fix16_t fix_eps()
  119. {
  120. return 1;
  121. }
  122. static_inline float float_pi()
  123. {
  124. return 3.1415926535897932384f;
  125. }
  126. static_inline fix16_t fix_pi()
  127. {
  128. return 205887;
  129. }
  130. fix16_t fix_exp(fix16_t inValue);
  131. fix16_t fix_sin(fix16_t inAngle);
  132. fix16_t fix_cos(fix16_t inAngle);
  133. fix16_t fix_tan(fix16_t inAngle);
  134. fix16_t fix_sinh(fix16_t inAngle);
  135. fix16_t fix_cosh(fix16_t inAngle);
  136. fix16_t fix_tanh(fix16_t inAngle);
  137. fix16_t fix_sqrt(fix16_t inValue);
  138. /* Floating point operations */
  139. static_inline float float_clip(float value, float low, float high)
  140. {
  141. return value < low ? low : (value > high ? high : value);
  142. }
  143. /* Array get and set */
  144. static_inline void float_set(float a[], int i, float value) { a[i] = value; }
  145. static_inline float float_get(float a[], int i) { return a[i]; }
  146. static_inline void fix_set(fix16_t a[], int i, fix16_t value) { a[i] = value; }
  147. static_inline fix16_t fix_get(fix16_t a[], int i) { return a[i]; }
  148. static_inline void int_set(int a[], int i, int value) { a[i] = value; }
  149. static_inline int int_get(int a[], int i) { return a[i]; }
  150. static_inline void bool_set(uint8_t a[], int i, uint8_t value) { a[i] = value; }
  151. static_inline uint8_t bool_get(uint8_t a[], int i) { return a[i]; }
  152. /* Array initialization */
  153. void float_init_array(int size, float value, float *data);
  154. void int_init_array(int size, int value, int *data);
  155. void bool_init_array(int size, uint8_t value, uint8_t *data);
  156. void fix_init_array(int size, fix16_t value, fix16_t *data);
  157. /* Array copy */
  158. void float_copy_array(int size, float *dest, float *src);
  159. void int_copy_array(int size, int *dest, int *src);
  160. void bool_copy_array(int size, uint8_t *dest, uint8_t *src);
  161. void fix_copy_array(int size, fix16_t *dest, fix16_t *src);
  162. static_inline uint8_t bool_not(uint8_t x)
  163. {
  164. return !x;
  165. }
  166. /* Tables */
  167. static_inline fix16_t *fix_wrap_array(const fix16_t x[]) { return (fix16_t *)x; };
  168. static_inline float *float_wrap_array(const float x[]) { return (float *)x; };
  169. /* Random numbers */
  170. float float_random();
  171. fix16_t fix_random();
  172. int irandom();
  173. /* Print values */
  174. void float_print(float value);
  175. void fix_print(fix16_t value);
  176. void int_print(int value);
  177. void string_print(char *value);
  178. void bool_print(uint8_t value);
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif // VULTIN_H