#pragma once #pragma warning(push, 0) #define __HI(x) *(1+(int*)&x) #define __LO(x) *(int*)&x #define __HIp(x) *(1+(int*)x) #define __LOp(x) *(int*)x #define POLYNOM1(x, a) ((a)[1]*(x)+(a)[0]) #define POLYNOM2(x, a) (POLYNOM1((x),(a)+1)*(x)+(a)[0]) #define POLYNOM3(x, a) (POLYNOM2((x),(a)+1)*(x)+(a)[0]) #define POLYNOM4(x, a) (POLYNOM3((x),(a)+1)*(x)+(a)[0]) #define POLYNOM5(x, a) (POLYNOM4((x),(a)+1)*(x)+(a)[0]) #define POLYNOM6(x, a) (POLYNOM5((x),(a)+1)*(x)+(a)[0]) #define POLYNOM7(x, a) (POLYNOM6((x),(a)+1)*(x)+(a)[0]) #define POLYNOM8(x, a) (POLYNOM7((x),(a)+1)*(x)+(a)[0]) #define POLYNOM9(x, a) (POLYNOM8((x),(a)+1)*(x)+(a)[0]) #define POLYNOM10(x, a) (POLYNOM9((x),(a)+1)*(x)+(a)[0]) #define POLYNOM11(x, a) (POLYNOM10((x),(a)+1)*(x)+(a)[0]) #define POLYNOM12(x, a) (POLYNOM11((x),(a)+1)*(x)+(a)[0]) #define POLYNOM13(x, a) (POLYNOM12((x),(a)+1)*(x)+(a)[0]) #define M_LN_MAX_D (M_LN2 * DBL_MAX_EXP) #define M_LN_MIN_D (M_LN2 * (DBL_MIN_EXP - 1)) struct f64 { uint32_t low_word; uint32_t high_word; }; #define F64_SIGN_SHIFT 31 #define F64_SIGN_MASK 1 #define F64_EXP_SHIFT 20 #define F64_EXP_MASK 0x7ff #define F64_EXP_BIAS 1023 #define F64_EXP_MAX 2047 #define F64_MANT_SHIFT 0 #define F64_MANT_MASK 0xfffff #define F64_GET_SIGN(fp) (((fp)->high_word >> F64_SIGN_SHIFT) & \ F64_SIGN_MASK) #define F64_GET_EXP(fp) (((fp)->high_word >> F64_EXP_SHIFT) & \ F64_EXP_MASK) #define F64_SET_EXP(fp, val) ((fp)->high_word= ((fp)->high_word & \ ~(F64_EXP_MASK << F64_EXP_SHIFT)) | \ (((val) & F64_EXP_MASK) << F64_EXP_SHIFT)) #define F64_GET_MANT_LOW(fp) ((fp)->low_word) #define F64_SET_MANT_LOW(fp, val) ((fp)->low_word= (val)) #define F64_GET_MANT_HIGH(fp) (((fp)->high_word >> F64_MANT_SHIFT) & \ F64_MANT_MASK) #define F64_SET_MANT_HIGH(fp, val) ((fp)->high_word= ((fp)->high_word & \ ~(F64_MANT_MASK << F64_MANT_SHIFT)) | \ (((val) & F64_MANT_MASK) << F64_MANT_SHIFT)) namespace math { static const double huge = 1.0e300; template struct position_t { _t x; _t y; _t length() { return x + y; } _t distance_to(const position_t<_t>& other) { return length() - other.length(); } template _n static_type_convert() { return position_t<_n>{static_cast<_n>(x), static_cast<_n>(y)}; } }; template struct dimension_t { _t w; _t h; }; constexpr double calculate_percentage(const double current, const double max) { if (max == 0.0) { return 0.0; } if (current > max) { return 100.0; } // G = max // W = value return ((current * 100.0) / max); } template < typename _t > struct range_t { _t min; _t max; }; template < typename _t > struct floating_point_holder2_t { _t x; _t y; }; template < typename _t > struct floating_point_holder3_t { _t x; _t y; _t z; }; template < typename _t > struct floating_point_holder4_t { _t x; _t y; _t z; _t h; }; inline float sqrtf(float _X) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(_X))); } inline float expf(float _X) { return _mm_cvtss_f32(_mm_exp_ps(_mm_set_ss(_X))); } inline float asinf(float _X) { return _mm_cvtss_f32(_mm_asin_ps(_mm_set_ss(_X))); } inline float fabsf(float x) { union { float f; uint32_t i; } u = { x }; u.i &= 0x7fffffff; return u.f; } inline int isinf(double x) { union { uint64_t u; double f; } ieee754; ieee754.f = x; return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 && ((unsigned)ieee754.u == 0); } inline int isnan(double x) { union { uint64_t u; double f; } ieee754; ieee754.f = x; return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0) > 0x7ff00000; } inline int signbit(double x) { union { double d; uint64_t i; } y = { x }; return y.i >> 63; } inline float fminf(float x, float y) { if (isnan(x)) return y; if (isnan(y)) return x; if (signbit(x) != signbit(y)) return signbit(x) ? x : y; return x < y ? x : y; } inline float fmaxf(float x, float y) { if (isnan(x)) return y; if (isnan(y)) return x; if (signbit(x) != signbit(y)) return signbit(x) ? y : x; return x < y ? y : x; } inline float atan2f(float y, float x) { float a, r, s, t, c, q, ax, ay, mx, mn; ax = fabsf(x); ay = fabsf(y); mx = fmaxf(ay, ax); mn = fminf(ay, ax); a = mn / mx; s = a * a; c = s * a; q = s * s; r = 0.024840285f * q + 0.18681418f; t = -0.094097948f * q - 0.33213072f; r = r * s + t; r = r * c + a; if (ay > ax) r = 1.57079637f - r; if (x < 0) r = 3.14159274f - r; if (y < 0) r = -r; return r; } inline static float FOPI = (float)1.27323954473516; inline static float DP1 = (float)0.78515625; inline static float DP2 = (float)2.4187564849853515625e-4; inline static float DP3 = (float)3.77489497744594108e-8; inline static float lossth = (float)8192.; inline static float T24M1 = (float)16777215.; inline static float sincof[] = { (float)-1.9515295891E-4, (float)8.3321608736E-3, (float)-1.6666654611E-1 }; inline static float coscof[] = { (float)2.443315711809948E-005, (float)-1.388731625493765E-003, (float)4.166664568298827E-002 }; inline float acos(float x) { float negate = float(x < 0); x = abs(x); float ret = -0.0187293; ret = ret * x; ret = ret + 0.0742610; ret = ret * x; ret = ret - 0.2121144; ret = ret * x; ret = ret + 1.5707288; ret = ret * sqrtf(1.0 - x); ret = ret - 2 * negate * ret; return negate * 3.14159265358979 + ret; } inline float sinf(float xx) { float* p; float x, y, z; unsigned long j; int sign; sign = 1; x = xx; if (xx < 0) { sign = -1; x = -xx; } if (x > T24M1) { return(0.0); } j = FOPI * x; y = j; if (j & 1) { j += 1; y += 1.0; } j &= 7; if (j > 3) { sign = -sign; j -= 4; } if (x > lossth) { x = x - y * M_PI_4; } else { x = ((x - y * DP1) - y * DP2) - y * DP3; } z = x * x; if ((j == 1) || (j == 2)) { p = coscof; y = *p++; y = y * z + *p++; y = y * z + *p++; y *= z * z; y -= 0.5 * z; y += 1.0; } else { p = sincof; y = *p++; y = y * z + *p++; y = y * z + *p++; y *= z * x; y += x; } if (sign < 0) y = -y; return(y); } inline float cosf(float xx) { float x, y, z; int j, sign; sign = 1; x = xx; if (x < 0) x = -x; if (x > T24M1) { return(0.0); } j = FOPI * x; y = j; if (j & 1) { j += 1; y += 1.0; } j &= 7; if (j > 3) { j -= 4; sign = -sign; } if (j > 1) sign = -sign; if (x > lossth) { x = x - y * M_PI_4; } else x = ((x - y * DP1) - y * DP2) - y * DP3; z = x * x; if ((j == 1) || (j == 2)) { y = (((-1.9515295891E-4 * z + 8.3321608736E-3) * z - 1.6666654611E-1) * z * x) + x; } else { y = ((2.443315711809948E-005 * z - 1.388731625493765E-003) * z + 4.166664568298827E-002) * z * z; y -= 0.5 * z; y += 1.0; } if (sign < 0) y = -y; return(y); } inline double floor(double x) { int i0, i1, j0; unsigned i, j; i0 = __HI(x); i1 = __LO(x); j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; if (j0 < 20) { if (j0 < 0) { /* raise inexact if x != 0 */ if (huge + x > 0.0) {/* return 0*sign(x) if |x|<1 */ if (i0 >= 0) { i0 = i1 = 0; } else if (((i0 & 0x7fffffff) | i1) != 0) { i0 = 0xbff00000; i1 = 0; } } } else { i = (0x000fffff) >> j0; if (((i0 & i) | i1) == 0) return x; /* x is integral */ if (huge + x > 0.0) { /* raise inexact flag */ if (i0 < 0) i0 += (0x00100000) >> j0; i0 &= (~i); i1 = 0; } } } else if (j0 > 51) { if (j0 == 0x400) return x + x; /* inf or NaN */ else return x; /* x is integral */ } else { i = ((unsigned)(0xffffffff)) >> (j0 - 20); if ((i1 & i) == 0) return x; /* x is integral */ if (huge + x > 0.0) { /* raise inexact flag */ if (i0 < 0) { if (j0 == 20) i0 += 1; else { j = i1 + (1 << (52 - j0)); if (j < i1) i0 += 1; /* got a carry */ i1 = j; } } i1 &= (~i); } } __HI(x) = i0; __LO(x) = i1; return x; } inline float floorf(float f) { return (float)floor((double)f); } inline float roundf(float x) { float t; if (x >= 0.0) { t = floorf(x); if (t - x <= -0.5) t += 1.0; return t; } else { t = floorf(-x); if (t + x <= -0.5) t += 1.0; return -t; } } inline double modf(double value, double* iptr) { struct f64* f64p; double tmp; int exp; int mask_bits; uint32_t mant; f64p = (struct f64*)&value; exp = F64_GET_EXP(f64p); exp -= F64_EXP_BIAS; if (exp < 0) { *iptr = 0; return value; } mask_bits = 52 - exp; if (mask_bits <= 0) { *iptr = value; return 0; } tmp = value; if (mask_bits >= 32) { F64_SET_MANT_LOW(f64p, 0); mask_bits -= 32; mant = F64_GET_MANT_HIGH(f64p); mant &= ~((1 << mask_bits) - 1); F64_SET_MANT_HIGH(f64p, mant); } else { mant = F64_GET_MANT_LOW(f64p); mant &= ~((1 << mask_bits) - 1); F64_SET_MANT_LOW(f64p, mant); } *iptr = value; return tmp - value; } inline double tan(double x) { /* Algorithm and coefficients from: "Software manual for the elementary functions" by W.J. Cody and W. Waite, Prentice-Hall, 1980 */ int negative = x < 0; int invert = 0; double y; static double p[] = { 1.0, -0.13338350006421960681e+0, 0.34248878235890589960e-2, -0.17861707342254426711e-4 }; static double q[] = { 1.0, -0.46671683339755294240e+0, 0.25663832289440112864e-1, -0.31181531907010027307e-3, 0.49819433993786512270e-6 }; if (math::isnan(x)) { return x; } if (negative) x = -x; /* ??? avoid loss of significance, error if x is too large ??? */ y = x * M_2_PI + 0.5; if (y >= DBL_MAX / M_PI_2) return 0.0; /* Use extended precision to calculate reduced argument. Here we used 12 bits of the mantissa for a1. Also split x in integer part x1 and fraction part x2. */ #define A1 1.57080078125 #define A2 -4.454455103380768678308e-6 { double x1, x2; modf(y, &y); if (modf(0.5 * y, &x1)) invert = 1; x2 = modf(x, &x1); x = x1 - y * A1; x += x2; x -= y * A2; #undef A1 #undef A2 } /* ??? avoid underflow ??? */ y = x * x; x += x * y * POLYNOM2(y, p + 1); y = POLYNOM4(y, q); if (negative) x = -x; return invert ? -y / x : x / y; } inline float tanf(float x) { return tan(x); } static int maxExponent = 511; /* Largest possible base 10 exponent. Any * exponent larger than this will already * produce underflow or overflow, so there's * no need to worry about additional digits. */ static double powersOf10[] = { /* Table giving binary powers of 10. Entry */ 10., /* is 10^2^i. Used to convert decimal */ 100., /* exponents into floating-point numbers. */ 1.0e4, 1.0e8, 1.0e16, 1.0e32, 1.0e64, 1.0e128, 1.0e256 }; inline int isspace(int c) { return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ' ? 1 : 0); } inline char isdigit(unsigned char c) { if (c >= '0' && c <= '9') return 1; return 0; } inline int isalpha(int c) { /* * Depends on ASCII-like character ordering. */ return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? 1 : 0); } inline char isupper(unsigned char c) { if (c >= UC('A') && c <= UC('Z')) return 1; return 0; } inline double atof(char* string) /* A decimal ASCII floating-point number, * optionally preceded by white space. * Must have form "-I.FE-X", where I is the * integer part of the mantissa, F is the * fractional part of the mantissa, and X * is the exponent. Either of the signs * may be "+", "-", or omitted. Either I * or F may be omitted, or both. The decimal * point isn't necessary unless F is present. * The "E" may actually be an "e". E and X * may both be omitted (but not just one). */ { int sign, expSign = FALSE; double fraction, dblExp, * d; register char* p, c; int exp = 0; /* Exponent read from "EX" field. */ int fracExp = 0; /* Exponent that derives from the fractional * part. Under normal circumstatnces, it is * the negative of the number of digits in F. * However, if I is very long, the last digits * of I get dropped (otherwise a long I with a * large negative exponent could cause an * unnecessary overflow on I alone). In this * case, fracExp is incremented one for each * dropped digit. */ int mantSize; /* Number of digits in mantissa. */ int decPt; /* Number of mantissa digits BEFORE decimal * point. */ char* pExp; /* Temporarily holds location of exponent * in string. */ /* * Strip off leading blanks and check for a sign. */ p = string; while (isspace(*p)) { p += 1; } if (*p == '-') { sign = TRUE; p += 1; } else { if (*p == '+') { p += 1; } sign = FALSE; } /* * Count the number of digits in the mantissa (including the decimal * point), and also locate the decimal point. */ decPt = -1; for (mantSize = 0; ; mantSize += 1) { c = *p; if (!isdigit(c)) { if ((c != '.') || (decPt >= 0)) { break; } decPt = mantSize; } p += 1; } /* * Now suck up the digits in the mantissa. Use two integers to * collect 9 digits each (this is faster than using floating-point). * If the mantissa has more than 18 digits, ignore the extras, since * they can't affect the value anyway. */ pExp = p; p -= mantSize; if (decPt < 0) { decPt = mantSize; } else { mantSize -= 1; /* One of the digits was the point. */ } if (mantSize > 18) { fracExp = decPt - 18; mantSize = 18; } else { fracExp = decPt - mantSize; } if (mantSize == 0) { return 0.0; } else { int frac1, frac2; frac1 = 0; for (; mantSize > 9; mantSize -= 1) { c = *p; p += 1; if (c == '.') { c = *p; p += 1; } frac1 = 10 * frac1 + (c - '0'); } frac2 = 0; for (; mantSize > 0; mantSize -= 1) { c = *p; p += 1; if (c == '.') { c = *p; p += 1; } frac2 = 10 * frac2 + (c - '0'); } fraction = (1.0e9 * frac1) + frac2; } /* * Skim off the exponent. */ p = pExp; if ((*p == 'E') || (*p == 'e')) { p += 1; if (*p == '-') { expSign = TRUE; p += 1; } else { if (*p == '+') { p += 1; } expSign = FALSE; } while (isdigit(*p)) { exp = exp * 10 + (*p - '0'); p += 1; } } if (expSign) { exp = fracExp - exp; } else { exp = fracExp + exp; } /* * Generate a floating-point number that represents the exponent. * Do this by processing the exponent one bit at a time to combine * many powers of 2 of 10. Then combine the exponent with the * fraction. */ if (exp < 0) { expSign = TRUE; exp = -exp; } else { expSign = FALSE; } if (exp > maxExponent) { exp = maxExponent; } dblExp = 1.0; for (d = powersOf10; exp != 0; exp >>= 1, d += 1) { if (exp & 01) { dblExp *= *d; } } if (expSign) { fraction /= dblExp; } else { fraction *= dblExp; } if (sign) { return -fraction; } return fraction; } } #pragma warning(pop)