1 /* TSI 2023.xmo */ 2 /******************************************************************************* 3 * Copyright (c) 2023 Think Silicon Single Member PC 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this header file and/or associated documentation files to use, copy, 7 * modify, merge, publish, distribute, sublicense, and/or sell copies of the 8 * Materials, and to permit persons to whom the Materials are furnished to do 9 * so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Materials. 13 * 14 * MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS 15 * NEMAGFX API. THE UNMODIFIED, NORMATIVE VERSIONS OF THINK-SILICON NEMAGFX 16 * SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT: 17 * https://think-silicon.com/products/software/nemagfx-api 18 * 19 * The software is provided 'as is', without warranty of any kind, express or 20 * implied, including but not limited to the warranties of merchantability, 21 * fitness for a particular purpose and noninfringement. In no event shall 22 * Think Silicon Single Member PC be liable for any claim, damages or other 23 * liability, whether in an action of contract, tort or otherwise, arising 24 * from, out of or in connection with the software or the use or other dealings 25 * in the software. 26 ******************************************************************************/ 27 28 29 #ifndef NEMA_MATH_H__ 30 #define NEMA_MATH_H__ 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 #define NEMA_E 2.71828182845904523536f /**< e */ 37 #define NEMA_LOG2E 1.44269504088896340736f /**< log2(e) */ 38 #define NEMA_LOG10E 0.434294481903251827651f /**< log10(e) */ 39 #define NEMA_LN2 0.693147180559945309417f /**< ln(2) */ 40 #define NEMA_LN10 2.30258509299404568402f /**< ln(10) */ 41 #define NEMA_PI 3.14159265358979323846f /**< pi */ 42 #define NEMA_PI_2 1.57079632679489661923f /**< pi/2 */ 43 #define NEMA_PI_4 0.785398163397448309616f /**< pi/4 */ 44 #define NEMA_1_PI 0.318309886183790671538f /**< 1/pi */ 45 #define NEMA_2_PI 0.636619772367581343076f /**< 2/pi */ 46 #define NEMA_2_SQRTPI 1.12837916709551257390f /**< 2/sqrt(pi) */ 47 #define NEMA_SQRT2 1.41421356237309504880f /**< sqrt(2) */ 48 #define NEMA_SQRT1_2 0.707106781186547524401f /**< 1/sqrt(2) */ 49 50 /** \brief Fast sine approximation of a given angle 51 * 52 * \param angle_degrees Angle in degrees 53 * \return Sine of the given angle 54 * 55 */ 56 float nema_sin(float angle_degrees); 57 58 59 /** \brief Fast cosine approximation of a given angle 60 * 61 * \param angle_degrees Angle in degrees 62 * \return Cosine of the given angle 63 * 64 */ 65 float nema_cos(float angle_degrees); 66 67 /** \brief Fast tangent approximation of a given angle 68 * 69 * \param angle_degrees Angle in degrees 70 * \return Tangent of the given angle 71 * 72 */ 73 float nema_tan(float angle_degrees); 74 75 76 /** \brief Fast sine approximation of a given angle 77 * 78 * \param angle_radians Angle in radians 79 * \return Sine of the given angle 80 * 81 */ 82 float nema_sin_r(float angle_radians); 83 84 85 /** \brief Fast cosine approximation of a given angle 86 * 87 * \param angle_radians Angle in radians 88 * \return Cosine of the given angle 89 * 90 */ 91 float nema_cos_r(float angle_radians); 92 93 /** \brief Fast tangent approximation of a given angle 94 * 95 * \param angle_radians Angle in radians 96 * \return Tangent of the given angle 97 * 98 */ 99 float nema_tan_r(float angle_radians); 100 101 /** \brief Fast arc tangent approximation of a y/x 102 * 103 * \param y value 104 * \param x value 105 * \return Arc tangent of the given y/x in degrees 106 * 107 */ 108 float nema_atan2(float y, float x); 109 110 /** \brief Fast arc tangent approximation of a y/x 111 * 112 * \param y value 113 * \param x value 114 * \return Arc tangent of the given y/x in radians 115 * 116 */ 117 float nema_atan2_r(float y, float x); 118 119 /** \brief A rough approximation of x raised to the power of y. USE WITH CAUTION! 120 * 121 * \param x base value. Must be non negative. 122 * \param y power value 123 * \return the result of raising x to the power y 124 * 125 */ 126 float nema_pow(float x, float y); 127 128 /** \brief A rough approximation of the square root of x. USE WITH CAUTION! 129 * 130 * \param x X value. Must be non negative 131 * \param 132 * \return The square root of x 133 * 134 */ 135 float nema_sqrt(float x); 136 137 138 /** \brief A floating-point approximation of the inverse tangent of x 139 * 140 * \param x X value 141 * \return Inverse tangent (angle) of x in degrees 142 * 143 */ 144 float nema_atan(float x); 145 146 /** \brief Find the minimum of two values 147 * 148 * \param a First value 149 * \param b Second value 150 * \return The minimum of a and b 151 * 152 */ 153 #define nema_min2(a,b) (((a)<(b))?( a):(b)) 154 155 /** \brief Find the maximum of two values 156 * 157 * \param a First value 158 * \param b Second value 159 * \return The maximum of a and b 160 * 161 */ 162 #define nema_max2(a,b) (((a)>(b))?( a):(b)) 163 164 /** \brief Clamp value 165 * 166 * \param val Value to clamp 167 * \param min Minimum value 168 * \param max Minimum value 169 * \return Clamped value 170 * 171 */ 172 #define nema_clamp(val, min, max) nema_min2((max), nema_max2((min), (val))) 173 174 /** \brief Calculate the absolute value of int 175 * 176 * \param a Value 177 * \return The absolute value of a 178 * 179 */ 180 #define nema_abs(a) (((a)< 0 )?(-(a)):(a)) 181 182 /** \brief Calculate the absolute value of float 183 * 184 * \param a Value 185 * \return The absolute value of a 186 * 187 */ 188 #define nema_absf(a) (((a)< 0.f )?(-(a)):(a)) 189 190 191 /** \brief Compare two floats 192 * 193 * \param x First float 194 * \param y Second float 195 * \return 1 if x == y, 0 if x != y 196 * 197 */ 198 #define nema_floats_equal(x, y) (nema_absf((x) - (y)) <= 0.00001f * nema_min2(nema_absf(x), nema_absf(y))) 199 200 /** \brief Checks if value x is zero 201 * 202 * \param x X value 203 * \return 1 if x == 0, 0 if x != 0 204 * 205 */ 206 #define nema_float_is_zero(x) (nema_absf(x) <= 0.00001f) 207 208 /** \brief Convert degrees to radians 209 * 210 * \param d Angle in degrees 211 * \return Angle in radians 212 * 213 */ 214 #define nema_deg_to_rad(d) (0.0174532925199f * (d)) //rad = deg * pi / 180 215 216 /** \brief Convert radians to degries 217 * 218 * \param r Angle in radians 219 * \return Angle in degrees 220 * 221 */ 222 #define nema_rad_to_deg(r) (57.295779513f * (r)) //deg = rad * 180 / pi 223 224 /** \brief Convert integer to 16.16 fixed point 225 * 226 * \param a Value to be converted 227 * \return 16.16 fixed point value 228 * 229 */ 230 #define nema_i2fx(a) ((a)*0x10000) 231 232 /** \brief Convert float to 16.16 fixed point 233 * 234 * \param a Value to be converted 235 * \return 16.16 fixed point value 236 * 237 */ 238 // #define nema_f2fx(a) ((int)(((a)*((float)0x10000)+0.5f))) 239 int nema_f2fx(float f); // ((int)(((a)*((float)0x10000)+0.5f))) 240 241 /** \brief Floor function 242 * 243 * \param a Value to be floored 244 * \return floored value 245 * 246 */ 247 #define nema_floor(f) ((int)(f) - ( (int)(f) > (f) )) 248 249 /** \brief Ceiling function 250 * 251 * \param a Value to be ceiled 252 * \return ceiled value 253 * 254 */ 255 #define nema_ceil(f) ((int)(f) + ( (int)(f) < (f) )) 256 257 /** \brief Truncate function 258 * 259 * \param x Value to be truncated 260 * \return truncated value 261 * 262 */ 263 #define nema_truncf(x) (x < 0.0f ? nema_ceil(x) : nema_floor(x)) 264 265 /** \brief Float Modulo function 266 * 267 * \param x Dividend 268 * \param y Divisor 269 * \return Remainder 270 * 271 */ 272 #define nema_fmod(x, y) ( (x) - nema_truncf( ( (x) / (y) ) ) * (y) ) 273 274 #ifdef __cplusplus 275 } 276 #endif 277 278 #endif 279