Math.h
Math.h
CPP1/* Copyright Statement:2 *3 * (C) 2021-2024 Cubios Inc. All rights reserved.4 */56#ifndef WASMLIBS_CPP_MATH_H_7#define WASMLIBS_CPP_MATH_H_89#include <math.h>1011#ifndef LN_212#define LN_2 0.693147180559945f13#endif1415#ifndef PI16#define PI (3.141592653589f)17#endif1819#ifndef PI_220#define PI_2 (6.28318530718f)21#endif2223#ifndef TINY24#define TINY (0.0000001)25#endif2627#define n_max(a,b) (((a) > (b)) ? (a) : (b))28#define n_min(a,b) (((a) < (b)) ? (a) : (b))29#define n_abs(a) (((a)<0.0f) ? (-(a)) : (a))30#define n_sgn(a) (((a)<0.0f) ? (-1) : (1))31#define n_deg2rad(d) (((d)*PI)/180.0f)32#define n_rad2deg(r) (((r)*180.0f)/PI)33#define n_sin(x) (float(sin(x)))34#define n_cos(x) (float(cos(x)))35#define n_tan(x) (float(tan(x)))36#define n_atan(x) (float(atan(x)))373839namespace Cubios40{41 namespace Math42 {4344 //log2() function.45static inline float log2(float f)46{47 return logf(f) / LN_2;48}4950//Integer clamping.51static inline int iclamp(int val, int minVal, int maxVal)52{53 if (val < minVal) return minVal;54 else if (val > maxVal) return maxVal;55 else return val;56}5758 // acos with value clamping.59static inline float acos(float x)60{61 if(x > 1.0f) x = 1.0f;62 if(x < -1.0f) x = -1.0f;63 return (float)acos(x);64}6566//asin with value clamping.67static inline float asin(float x)68{69 if(x > 1.0f) x = 1.0f;70 if(x < -1.0f) x = -1.0f;71 return (float)asin(x);72}7374//Safe sqrt.75static inline float sqrt(float x)76{77 if (x<0.0f) x=(float)0.0f;78 return (float) sqrt(x);79}8081//A fuzzy floating point equality check82static inline bool fequal(float f0, float f1, float tol)83{84 float f = f0-f1;85 if ((f>(-tol)) && (f<tol)) return true;86 else return false;87}8889//A fuzzy floating point less-then check.90static inline bool fless(float f0, float f1, float tol)91{92 if ((f0-f1)<tol) return true;93 else return false;94}9596//A fuzzy floating point greater-then check.97static inline bool fgreater(float f0, float f1, float tol)98{99 if ((f0-f1)>tol) return true;100 else return false;101}102103/**104 fast float to int conversion (always truncates)105 see http://www.stereopsis.com/FPU.html for a discussion.106 NOTE: this works only on little endian machines.107*/108static inline long ftol(float val)109{110 double v = double(val) + (68719476736.0*1.5);111 return ((long*)&v)[0] >> 16;112}113114115//Smooth a new value towards an old value using a change value.116static inline float smooth(float newVal, float curVal, float maxChange)117{118 float diff = newVal - curVal;119 if (fabs(diff) > maxChange)120 {121 if (diff > 0.0f)122 {123 curVal += maxChange;124 if (curVal > newVal)125 {126 curVal = newVal;127 }128 }129 else if (diff < 0.0f)130 {131 curVal -= maxChange;132 if (curVal < newVal)133 {134 curVal = newVal;135 }136 }137 }138 else139 {140 curVal = newVal;141 }142 return curVal;143}144145146//Clamp a value against lower und upper boundary.147static inline float clamp(float val, float lower, float upper)148{149 if (val < lower) return lower;150 else if (val > upper) return upper;151 else return val;152}153154//Saturate a value (clamps between 0.0f and 1.0f)155static inline float saturate(float val)156{157 if (val < 0.0f) return 0.0f;158 else if (val > 1.0f) return 1.0f;159 else return val;160}161162static unsigned long seed = 0;163164//Return a pseudo random number between 0 and 1.165static inline float rand2()166{167 return float(rand()) / float(RAND_MAX);168}169170171// Geteral randomizer172static inline unsigned long rand()173{174 seed = (1664525L*seed + 1013904223L) & 0xffffffff;175 return seed;176}177178179// Gets randomizer seed180static inline unsigned long randGetSeed()181{182 return seed;183}184185// Initializes randomizer's seed186static inline void randSetSeed (unsigned long s)187{188 seed = s;189}190191// Returns random int in range of [0,n]192static inline int randInt (int n)193{194 double a = double(n) / 4294967296.0;195 return (int) (double(rand()) * a);196}197198// Returns random float in [0,1]199static inline float randReal()200{201 return ((float) rand()) / ((float) 0xffffffff);202}203204//Linearly interpolate between 2 values: ret = x + l * (y - x)205static inline float lerp(float x, float y, float l)206{207 return x + l * (y - x);208}209210 }211212}213#endif /* WASMLIBS_CPP_MATH_H_ */214
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.