HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_Math.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_Math.h (SYS Library, C++)
7  *
8  * COMMENTS: Houdini math interface.
9  * statements like: a = sin(x); b = sinf(y);
10  * we use: a = SYSsin(x); b = SYSsin(y);
11  *
12  * Functions defined here:
13  * SYSmin(a,b) - Returns the minimum of a or b
14  * SYSmin(a,b,c) - Returns the minimum of a, b or c
15  * SYSmin(a,b,c,d) - Returns minimum of a, b, c or d
16  * SYSargmin(a,b) - Returns index of minimum number
17  * SYSargmin(a,b,c) - Returns index of minimum number
18  * SYSargmin(a,b,c,d) - Returns index of minimum number
19  * SYSmax(a,b) - Returns the maximum of a or b
20  * SYSmax(a,b,c) - Returns the maximum of a, b or c
21  * SYSmax(a,b,c,d) - Returns maximum of a, b, c or d
22  * SYSsort(a, b) - Sorts so a < b
23  * SYSsort(a, b, c) - Sorts so a < b < c
24  * SYSargmax(a,b) - Returns index of largest number
25  * SYSargmax(a,b,c) - Returns index of largest number
26  * SYSargmax(a,b,c,d) - Returns index of largest number
27  * SYSabs(x) - Works for all types (including fpreal)
28  * SYSavg(a,b,c) - Return average value of 3 values
29  * SYSavg(a,b,c,d) - Return average value of 4 values
30  *
31  * SYSminmax(a,b,&min.&max) - Find min and max of 2 values
32  * SYSminmax(a,b,c,&min.&max) - Find min and max of 3 values
33  * SYSminmax(a,b,c,d,&min.&max) - Find min and max of 4 values
34  *
35  * SYSwrapmod(a, b) - Returns [0..b) interval for +ve b.
36  *
37  * SYSsignum(a) - Numerically robust Sign function:
38  * -1, if a is negative
39  * 1, if a is positive
40  * 0, otherwise:
41  *
42  * Comparison:
43  * SYSequalZero(a, [tol]) - Is a equal to zero
44  * SYSisEqual(a, b, tol) -
45  * SYSalmostEqual(a, b, ulps, tol) - Almost equal given units in last place
46  *
47  * Interpolation/Range:
48  * SYSclamp(a, min, max) - Clamp value between min and max
49  * SYSsmooth(min, max, a) - Ease-in ease-out curve
50  * SYSsmooth(min, max, a, roll) - Ease-in ease-out curve (with rolloff)
51  * SYSlerp(v1, v2, bias) - Linear interpolation
52  * SYSinvlerp(a, min, max) - Bias required to return a when
53  * lerping min and max.
54  * SYSfit(v, omin,omax, nmin,nmax) - Map v in (omin,omax) to (nmin,nmax)
55  *
56  * The bilerp function expects:
57  * u0v1--u1v1
58  * | |
59  * u0v0--u1v0
60  * Where u is horizontal and v is vertical in the diagram above.
61  * SYSbilerp(u0v0, u1v0, u0v1, u1v1, u, v)
62  *
63  * Standard math (single/double precision signatures):
64  * SYSsin(x)
65  * SYScos(x)
66  * SYStan(x)
67  * SYSsqrt(x)
68  * SYSlog(x)
69  * SYSfabs(x)
70  * SYStrunc(x)
71  * SYSfloor(x)
72  * SYSceil(x)
73  * SYScbrt(x) -- Use #include <SYS/SYS_MathCbrt.h>
74  * SYSlog10(x)
75  * SYSfmod(x,y)
76  * SYSpow(x,y)
77  * SYSsafepow(x, y)
78  * SYSatan(x)
79  * SYSatan(y,x) -- Note, SYSatan(y,x) is equivalent to SYSatan2(y,x)
80  * SYSatan2(y,x)
81  * SYShypot(x,y)
82  * SYSrecip(x)
83  * SYSsafediv(x,y) - Divide only if y != 0
84  * SYSsaferecip(x) - Recip only if x != 0
85  * SYSsafesqrt(x) - Compute sqrt only if x >= 0
86  * SYSsafefmod(x, y) - Mod only if y != 0
87  * SYSsincos(x,*out_sin, *out_cos)
88  *
89  * Random:
90  * SYSpointerHash - Generate a hash key for a pointer
91  * SYSwang_inthash - 32-bit Integer hashing function
92  * SYSwang_inthash64 - 64-bit Integer hashing function
93  * SYSwang2_inthash - Newer 32-bit Integer hashing function
94  * SYSwang2_inthash64 - Newer 64-bit Integer hashing function
95  * SYSreal_hash - Generate a hash for a real number
96  * SYSreal_hashseed - Generate a hash for a real number
97  * SYSvector_hash - Generate a hash for a real vector
98  * SYSfastRandom - Really fast random number generator (0,1)
99  * SYSrandom - Fast random number (fewer artifacts) (0,1)
100  * SYSfastRandomZero - Really fast random number generator (-.5, .5)
101  * SYSrandomZero - Fast random number (fewer artifacts) (-.5, .5)
102  *
103  * Rounding:
104  * SYSroundDownToMultipleOf -
105  * SYSroundUpToMultipleOf -
106  * SYSroundAngle -
107  *
108  * Misc:
109  * SYSisNan - Is this a valid floating-point number?
110  * SYSisFinite - Is not a NAN or Infinity
111  * SYSisInt - Is the string an integer
112  * SYSisFloat - Is the string a float
113  * SYSisPrime - Is it a prime?
114  * SYSsameSign(a,b) - Do the non-zero numbers have the same sign?
115  * (Note: does not work if either number is zero)
116  * SYSmakePrime - Make it a prime
117  * SYSnextPrime - Next prime
118  * SYSgetSinCosFromSlope - Compute sin/cos given a slope
119  * SYShexCharToInt - map '0'-'9,'a'-'z' to 0-15, or -1
120  * SYSintToHexChar - map 0-15 to '0'-'9,'a'-'z'
121  * SYSdivMod(n,d,q,r) - q = n/d; r = n%d; The compiler should make
122  * just one DIV or IDIV instruction.
123  *
124  * For information on almostEqual, please see:
125  * http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
126  *
127  */
128 
129 #ifndef __SYS_Math__
130 #define __SYS_Math__
131 
132 #include "SYS_API.h"
133 #include "SYS_Types.h"
134 
135 /*
136  * System dependent includes
137  */
138 #if defined(LINUX)
139 # include "SYS_FastMath.h"
140 #endif
141 
142 #include <float.h>
143 #include <limits>
144 #include <math.h>
145 #include <stdio.h>
146 #include <stdlib.h>
147 
148 // We do not want to use the built-ins for srand48 or drand48 as they
149 // are not threadsafe. This version uses thread local storage to
150 // avoid that.
151 SYS_API void SYSsrand48(long seed);
152 SYS_API double SYSdrand48();
153 
154 #if !defined(SESI_ALLOW_DRAND48)
155 
156 // These macros are to aggressively prevent anyone from accidetnally
157 // using thread unsafe functions
158 #undef srand48
159 #undef drand48
160 #define srand48(X) static_assert(0, "Use SYSsrand48() instead")
161 #define drand48(X) 0; { static_assert(0, "Use SYSdrand48() instead"); }
162 
163 #endif
164 
165 // It would be useful to disable rand() and srand() as well, but
166 // they are used by std::rand :<
167 
168 template< typename F >
169 constexpr inline int
170 SYSsignum(const F a)
171 {
172  return int(0 < a) - int(a < 0);
173 }
174 
175 // Determine whether f is not-a-number (NAN) for any floating point type f
176 template< typename F >
177 constexpr inline bool
178 SYSisNan(const F f)
179 {
180  return(f != f);
181 }
182 
183 template <>
184 inline bool
186 {
187  return f.isNan();
188 }
189 
190 /// @{
191 /// Check whether a number is finite. That is, not Nan and not infinity.
192 /// @see SYSisNan()
193 #include <cmath>
194 inline bool SYSisFinite(fpreal64 f) { return std::isfinite(f); }
195 inline bool SYSisFinite(fpreal32 f) { return std::isfinite(f); }
196 inline bool SYSisFinite(fpreal16 f) { return f.isFinite(); }
197 constexpr inline bool SYSisFinite(int32 f) { return true; }
198 constexpr inline bool SYSisFinite(int64 f) { return true; }
199 /// @}
200 
201 /// SYSisNan() checks whether the string represents a non-finite number
202 SYS_API bool SYSisNan(const char *number);
203 SYS_API bool SYSisInt(const char *str);
204 SYS_API bool SYSisFloat(const char *str);
209 SYS_API bool SYSisPrime(uint num);
213 
214 #if defined(__cplusplus)
215 
216 // NOTE:
217 // These have been carefully written so that in the case of equality
218 // we always return the first parameter. This is so that NANs in
219 // in the second parameter are suppressed.
220 #define h_min(a, b) (((a) > (b)) ? (b) : (a))
221 #define h_argmin(a, b) (((a) > (b)) ? 1 : 0)
222 #define h_max(a, b) (((a) < (b)) ? (b) : (a))
223 #define h_argmax(a, b) (((a) < (b)) ? 1 : 0)
224 // DO NOT CHANGE THE ABOVE WITHOUT READING THE COMMENT
225 #define h_abs(a) (((a) > 0) ? (a) : -(a))
226 #define h_sgn(a) (((a) > 0) ? 1 : (((a) < 0) ? -1 : 0))
227 
228 
229 static constexpr inline int16 SYSmin(int16 a, int16 b) { return h_min(a,b); }
230 static constexpr inline int16 SYSmax(int16 a, int16 b) { return h_max(a,b); }
231 static constexpr inline int16 SYSabs(int16 a) { return h_abs(a); }
232 static constexpr inline int16 SYSsgn(int16 a) { return h_sgn(a); }
233 static constexpr inline int SYSargmin(int16 a, int16 b) { return h_argmin(a,b);}
234 static constexpr inline int SYSargmax(int16 a, int16 b) { return h_argmax(a,b);}
235 static constexpr inline int32 SYSmin(int32 a, int32 b) { return h_min(a,b); }
236 static constexpr inline int32 SYSmax(int32 a, int32 b) { return h_max(a,b); }
237 static constexpr inline int32 SYSabs(int32 a) { return h_abs(a); }
238 static constexpr inline int32 SYSsgn(int32 a) { return h_sgn(a); }
239 static constexpr inline int SYSargmin(int32 a, int32 b) { return h_argmin(a,b);}
240 static constexpr inline int SYSargmax(int32 a, int32 b) { return h_argmax(a,b);}
241 static constexpr inline int64 SYSmin(int64 a, int64 b) { return h_min(a,b); }
242 static constexpr inline int64 SYSmax(int64 a, int64 b) { return h_max(a,b); }
243 static constexpr inline int64 SYSmin(int32 a, int64 b) { return h_min(a,b); }
244 static constexpr inline int64 SYSmax(int32 a, int64 b) { return h_max(a,b); }
245 static constexpr inline int64 SYSmin(int64 a, int32 b) { return h_min(a,b); }
246 static constexpr inline int64 SYSmax(int64 a, int32 b) { return h_max(a,b); }
247 static constexpr inline int64 SYSabs(int64 a) { return h_abs(a); }
248 static constexpr inline int64 SYSsgn(int64 a) { return h_sgn(a); }
249 static constexpr inline int SYSargmin(int64 a, int64 b) { return h_argmin(a,b);}
250 static constexpr inline int SYSargmax(int64 a, int64 b) { return h_argmax(a,b);}
251 static constexpr inline uint16 SYSmin(uint16 a, uint16 b) { return h_min(a,b); }
252 static constexpr inline uint16 SYSmax(uint16 a, uint16 b) { return h_max(a,b); }
253 static constexpr inline int SYSargmin(uint16 a, uint16 b) { return h_argmin(a,b);}
254 static constexpr inline int SYSargmax(uint16 a, uint16 b) { return h_argmax(a,b);}
255 static constexpr inline uint32 SYSmin(uint32 a, uint32 b) { return h_min(a,b); }
256 static constexpr inline uint32 SYSmax(uint32 a, uint32 b) { return h_max(a,b); }
257 static constexpr inline int SYSargmin(uint32 a, uint32 b) { return h_argmin(a,b);}
258 static constexpr inline int SYSargmax(uint32 a, uint32 b) { return h_argmax(a,b);}
259 static constexpr inline uint64 SYSmin(uint64 a, uint64 b) { return h_min(a,b); }
260 static constexpr inline uint64 SYSmax(uint64 a, uint64 b) { return h_max(a,b); }
261 static constexpr inline int SYSargmin(uint64 a, uint64 b) { return h_argmin(a,b);}
262 static constexpr inline int SYSargmax(uint64 a, uint64 b) { return h_argmax(a,b);}
263 static inline fpreal16 SYSmin(fpreal16 a, fpreal16 b) { return h_min(a,b); }
264 static inline fpreal16 SYSmax(fpreal16 a, fpreal16 b) { return h_max(a,b); }
265 static inline fpreal16 SYSsgn(fpreal16 a) { return h_sgn(a); }
266 static inline int SYSargmin(fpreal16 a, fpreal16 b){ return h_argmin(a,b);}
267 static inline int SYSargmax(fpreal16 a, fpreal16 b){ return h_argmax(a,b);}
268 static constexpr inline fpreal32 SYSmin(fpreal32 a, fpreal32 b) { return h_min(a,b); }
269 static constexpr inline fpreal32 SYSmax(fpreal32 a, fpreal32 b) { return h_max(a,b); }
270 static constexpr inline fpreal32 SYSsgn(fpreal32 a) { return h_sgn(a); }
271 static constexpr inline int SYSargmin(fpreal32 a, fpreal32 b){ return h_argmin(a,b);}
272 static constexpr inline int SYSargmax(fpreal32 a, fpreal32 b){ return h_argmax(a,b);}
273 static constexpr inline fpreal64 SYSmin(fpreal64 a, fpreal64 b) { return h_min(a,b); }
274 static constexpr inline fpreal64 SYSmax(fpreal64 a, fpreal64 b) { return h_max(a,b); }
275 static constexpr inline fpreal64 SYSsgn(fpreal64 a) { return h_sgn(a); }
276 static constexpr inline int SYSargmin(fpreal64 a, fpreal64 b){ return h_argmin(a,b);}
277 static constexpr inline int SYSargmax(fpreal64 a, fpreal64 b){ return h_argmax(a,b);}
278 
279 // Some systems have size_t as a seperate type from uint. Some don't.
280 #if defined(MBSD)
281 static constexpr inline size_t SYSmin(size_t a, size_t b) { return h_min(a,b); }
282 static constexpr inline size_t SYSmax(size_t a, size_t b) { return h_max(a,b); }
283 static constexpr inline int SYSargmin(size_t a, size_t b) { return h_argmin(a,b);}
284 static constexpr inline int SYSargmax(size_t a, size_t b) { return h_argmax(a,b);}
285 #endif
286 
287 #undef h_min
288 #undef h_max
289 #undef h_abs
290 #undef h_sgn
291 
292 #define h_clamp(val, min, max, tol) \
293  ((val <= min+tol) ? min : ((val >= max-tol) ? max : val))
294 
295  static constexpr inline int
296  SYSclamp(int v, int min, int max)
297  { return h_clamp(v, min, max, 0); }
298 
299  static constexpr inline uint
301  { return h_clamp(v, min, max, 0); }
302 
303  static constexpr inline int64
305  { return h_clamp(v, min, max, CONST_INT64(0)); }
306 
307  static constexpr inline uint64
308  SYSclamp(uint64 v, uint64 min, uint64 max)
309  { return h_clamp(v, min, max, CONST_UINT64(0)); }
310 
311  static constexpr inline fpreal32
312  SYSclamp(fpreal32 v, fpreal32 min, fpreal32 max, fpreal32 tol=(fpreal32)0)
313  { return h_clamp(v, min, max, tol); }
314 
315  static constexpr inline fpreal64
316  SYSclamp(fpreal64 v, fpreal64 min, fpreal64 max, fpreal64 tol=(fpreal64)0)
317  { return h_clamp(v, min, max, tol); }
318 
319 #undef h_clamp
320 
321 // clamp float to [0, 1]
322 static constexpr inline float SYSclamp01(float v)
323  { return SYSclamp(v, 0.f, 1.f); }
324 static constexpr inline double SYSclamp01(double v)
325  { return SYSclamp(v, 0.0, 1.0); }
326 
327 // clamp float to [0, 1)
328 static constexpr inline float SYSclamp01_excl1(float v)
329  { return SYSclamp(v, 0.f, 1.f - FLT_EPSILON/FLT_RADIX); }
330 static constexpr inline double SYSclamp01_excl1(double v)
331  { return SYSclamp(v, 0.0, 1.0 - DBL_EPSILON/FLT_RADIX); }
332 
333 /// This converts from one integer type to another by clamping
334 /// the range, instead of the usual wrapping.
335 template<typename OUTTYPE,typename INTYPE>
336 constexpr OUTTYPE SYSclampInt(INTYPE value)
337 {
338  value = SYSclamp(value, (INTYPE)std::numeric_limits<OUTTYPE>::min(),
340  return OUTTYPE(value);
341 }
342 
343 // Wrapper for libm math function calls
344 #if defined(LINUX)
345 # define SYS_MF(X) SYS_FastMath::X
346 #else
347 # define SYS_MF(X) ::X
348 #endif
349 
350 #define SYS_UNARY(func) \
351  static inline fpreal64 SYS##func(fpreal64 arg) \
352  { return SYS_MF(func)(arg); } \
353  static inline fpreal32 SYS##func(fpreal32 arg) \
354  { return SYS_MF(func##f)(arg); } \
355  static inline fpreal64 SYS##func(int64 arg) \
356  { return SYS_MF(func)((fpreal64)arg); } \
357  static inline fpreal64 SYS##func(int32 arg) \
358  { return SYS_MF(func)((fpreal64)arg); } \
359  /* end macro */
360 #define SYS_BINARY(func) \
361  static inline fpreal64 SYS##func(fpreal64 a, fpreal64 b) \
362  { return SYS_MF(func)(a,b); } \
363  static inline fpreal32 SYS##func(fpreal32 a, fpreal32 b) \
364  { return SYS_MF(func##f)(a, b); } \
365  /* end macro */
366 
367 #if defined(WIN32)
368 #define hypotf(x,y) hypot((x),(y))
369 #endif
370 
371  SYS_UNARY(sin)
372  SYS_UNARY(cos)
373  SYS_UNARY(tan)
374  SYS_UNARY(sinh)
375  SYS_UNARY(cosh)
376  SYS_UNARY(tanh)
377  SYS_UNARY(sqrt)
378  SYS_UNARY(trunc)
379  SYS_UNARY(exp)
380  SYS_BINARY(fmod)
381  SYS_BINARY(pow)
382  SYS_BINARY(atan2)
383  SYS_BINARY(hypot)
384  SYS_BINARY(copysign)
385 
386  static constexpr inline fpreal32 SYSsafediv(fpreal32 x, fpreal32 y)
387  { return x/(y != 0 ? y : SYS_Types<fpreal32>::infinity()); }
388  static constexpr inline fpreal64 SYSsafediv(fpreal64 x, fpreal64 y)
389  { return x/(y != 0 ? y : SYS_Types<fpreal64>::infinity()); }
390  static constexpr inline fpreal32 SYSsafesqrt(fpreal32 x)
391  { return x > 0 ? SYSsqrt(x) : 0; }
392  static constexpr inline fpreal64 SYSsafesqrt(fpreal64 x)
393  { return x > 0 ? SYSsqrt(x) : 0; }
394  static constexpr inline fpreal32 SYSsafefmod(fpreal32 x, fpreal32 y)
395  { return y != 0 ? SYSfmod(x, y) : 0; }
396  static constexpr inline fpreal64 SYSsafefmod(fpreal64 x, fpreal64 y)
397  { return y != 0 ? SYSfmod(x, y) : 0; }
398 
399 #if defined(LINUX)
400  static inline void SYSsincos(fpreal32 x, fpreal32 *s, fpreal32 *c)
401  { SYS_MF(sincosf)(x,s,c); }
402  static inline void SYSsincos(fpreal64 x, fpreal64 *s, fpreal64 *c)
403  { SYS_MF(sincos)(x,s,c); }
404  static inline void SYSsincos(fpreal32 x, fpreal16 *s, fpreal16 *c)
405  {
406  fpreal32 s32, c32;
407  SYS_MF(sincosf)(x,&s32,&c32);
408  *s = (fpreal16)s32;
409  *c = (fpreal16)c32;
410  }
411 #elif defined(MBSD)
412  static inline void SYSsincos(fpreal32 x, fpreal32 *s, fpreal32 *c)
413  { __sincosf(x,s,c); }
414  static inline void SYSsincos(fpreal64 x, fpreal64 *s, fpreal64 *c)
415  { __sincos(x,s,c); }
416  static inline void SYSsincos(fpreal32 x, fpreal16 *s, fpreal16 *c)
417  {
418  fpreal32 s32, c32;
419  __sincosf(x,&s32,&c32);
420  *s = (fpreal16)s32;
421  *c = (fpreal16)c32;
422  }
423 #else
424  static inline void SYSsincos(fpreal32 x, fpreal32 *s, fpreal32 *c)
425  { *s = SYSsin(x); *c = SYScos(x); }
426  static inline void SYSsincos(fpreal64 x, fpreal64 *s, fpreal64 *c)
427  { *s = SYSsin(x); *c = SYScos(x); }
428  static inline void SYSsincos(fpreal32 x, fpreal16 *s, fpreal16 *c)
429  { *s = (fpreal16)SYSsin(x); *c = (fpreal16)SYScos(x); }
430 #endif
431 
432 #if 0
433 #include <xmmintrin.h>
434  /// Computes 1/sqrt(x) to about 11.4 bits of accuracy
435  static inline fpreal32 SYSrsqrt11(fpreal32 x)
436  {
437 #if defined(WIN32) || defined(LINUX) || defined(MBSD)
438  union {
439  fpreal32 val;
440  __m128 vec;
441  };
442  val = x;
443  vec = _mm_rsqrt_ss(vec);
444  return val;
445 #else
446  return 1.0f/SYSsqrt(x);
447 #endif
448  }
449 
450  /// Computes 1/sqrt(x) to about 22.2 bits of accuracy
451  static inline fpreal32 SYSrsqrt22(fpreal32 x)
452  {
453 #if defined(WIN32) || defined(LINUX) || defined(MBSD)
454  fpreal32 approximate = SYSrsqrt11(x);
455  // -(((1+e)/sqrt(x))^2 * x - 3) * ((1+e)/sqrt(x)) / 2
456  // -(-1 + e + (e^2)/2) * ((1+e)/sqrt(x))
457  // -(-1 + e + (e^2)/2 - e + e^2 + (e^3)/2) / sqrt(x)
458  // (1 - 3(e^2)/2 - (e^3)/2) / sqrt(x);
459  fpreal32 result = (approximate*approximate*x - 3)*approximate*-0.5f;
460 #else
461  fpreal32 result = 1.0f/SYSsqrt(x);
462 #endif
463  return result;
464  }
465 #endif
466 
467  static inline fpreal32 SYSlog(fpreal32 v) { return v <= 0 ? 0 : SYS_MF(logf)(v); }
468  static inline fpreal64 SYSlog(fpreal64 v) { return v <= 0 ? 0 : SYS_MF(log)(v); }
469  static inline fpreal32 SYSlog10(fpreal32 v) { return v <= 0 ? 0 : SYS_MF(log10f)(v); }
470  static inline fpreal64 SYSlog10(fpreal64 v) { return v <= 0 ? 0 : SYS_MF(log10)(v); }
471 
472 #if defined(WIN32)
473  static inline fpreal32 SYSexpm1(fpreal32 x) { return SYSexp(x) - 1; }
474  static inline fpreal64 SYSexpm1(fpreal64 x) { return SYSexp(x) - 1; }
475  static inline fpreal32 SYSlog1p(fpreal32 x) { return SYSlog(x+1); }
476  static inline fpreal64 SYSlog1p(fpreal64 x) { return SYSlog(x+1); }
477 #else
478  SYS_UNARY(expm1)
479  SYS_UNARY(log1p)
480 #endif
481 
482 #undef SYS_UNARY
483 #undef SYS_BINARY
484 #undef hypotf
485 
486 static inline fpreal32 SYSabs(fpreal32 a) { return SYS_MF(fabsf)(a); }
487 static inline fpreal64 SYSabs(fpreal64 a) { return SYS_MF(fabs)(a); }
488 static inline fpreal32 SYSfabs(fpreal32 a) { return SYS_MF(fabsf)(a); }
489 static inline fpreal64 SYSfabs(fpreal64 a) { return SYS_MF(fabs)(a); }
490 
491 #include "SYS_Floor.h"
492 
493 static inline fpreal32 SYSasin(fpreal32 a)
494  { return SYS_MF(asinf)(SYSclamp(a, (fpreal32)-1, (fpreal32)1)); }
495 static inline fpreal32 SYSacos(fpreal32 a)
496  { return SYS_MF(acosf)(SYSclamp(a, (fpreal32)-1, (fpreal32)1)); }
497 static inline fpreal32 SYSatan(fpreal32 a)
498  { return SYS_MF(atanf)(a); }
499 static inline fpreal32 SYSatan(fpreal32 y, fpreal32 x)
500  { return SYS_MF(atan2f)(y, x); }
501 static inline fpreal64 SYSasin(fpreal64 a)
502  { return SYS_MF(asin) (SYSclamp(a, (fpreal64)-1, (fpreal64)1)); }
503 static inline fpreal64 SYSacos(fpreal64 a)
504  { return SYS_MF(acos) (SYSclamp(a, (fpreal64)-1, (fpreal64)1)); }
505 static inline fpreal64 SYSatan(fpreal64 a)
506  { return SYS_MF(atan) (a); }
507 static inline fpreal64 SYSatan(fpreal64 y, fpreal64 x)
508  { return SYS_MF(atan2)(y, x); }
509 
510 static inline fpreal32 SYSsafepow(fpreal32 x, fpreal32 y)
511  { return SYSpow(x, x < 0 ? SYSrint(y) : y); }
512 static inline fpreal64 SYSsafepow(fpreal64 x, fpreal64 y)
513  { return SYSpow(x, x < 0 ? SYSrint(y) : y); }
514 
515 static constexpr inline fpreal32 SYSrecip(fpreal32 a) { return 1 / a; }
516 static constexpr inline fpreal64 SYSrecip(fpreal64 a) { return 1 / a; }
517 static constexpr inline fpreal32 SYSrecip(int32 a) { return 1 / (fpreal32)a; }
518 static constexpr inline fpreal64 SYSrecip(int64 a) { return 1 / (fpreal64)a; }
519 static constexpr inline fpreal32 SYSsaferecip(fpreal32 a)
520  { return SYSsafediv( (fpreal32)1, a ); }
521 static constexpr inline fpreal64 SYSsaferecip(fpreal64 a)
522  { return SYSsafediv( (fpreal64)1, a ); }
523 static constexpr inline fpreal32 SYSsaferecip(int32 a)
524  { return SYSsafediv( (fpreal32)1, (fpreal32)a ); }
525 static constexpr inline fpreal64 SYSsaferecip(int64 a)
526  { return SYSsafediv( (fpreal64)1, (fpreal64)a ); }
527 
528 static constexpr inline fpreal32 SYSdegToRad(fpreal32 a) { return a*(fpreal32)(M_PI/180.0); }
529 static constexpr inline fpreal64 SYSdegToRad(fpreal64 a) { return a*(fpreal64)(M_PI/180.0); }
530 static constexpr inline fpreal64 SYSdegToRad(int32 a) { return a*(fpreal64)(M_PI/180.0); }
531 static constexpr inline fpreal64 SYSdegToRad(int64 a) { return a*(fpreal64)(M_PI/180.0); }
532 
533 static constexpr inline fpreal32 SYSradToDeg(fpreal32 a) { return a*(fpreal32)(180.0/M_PI); }
534 static constexpr inline fpreal64 SYSradToDeg(fpreal64 a) { return a*(fpreal64)(180.0/M_PI); }
535 static constexpr inline fpreal64 SYSradToDeg(int32 a) { return a*(fpreal32)(180.0/M_PI); }
536 static constexpr inline fpreal64 SYSradToDeg(int64 a) { return a*(fpreal64)(180.0/M_PI); }
537 
538 #define h_compare(func, code) \
539  static inline bool func(fpreal32 a, fpreal32 b, \
540  fpreal32 tol=SYS_FTOLERANCE) \
541  { return code; } \
542  static inline bool func(fpreal64 a, fpreal64 b, \
543  fpreal64 tol=SYS_FTOLERANCE_D) \
544  { return code; }
545 #define h_compare_ce(func, code) \
546  static constexpr inline bool func(fpreal32 a, fpreal32 b, \
547  fpreal32 tol=SYS_FTOLERANCE) \
548  { return code; } \
549  static constexpr inline bool func(fpreal64 a, fpreal64 b, \
550  fpreal64 tol=SYS_FTOLERANCE_D) \
551  { return code; }
552 
553  static constexpr inline bool
555  { return a >= -tol && a <= tol; }
556 
557  static constexpr inline bool
559  { return a >= -tol && a <= tol; }
560 
561  static constexpr inline bool
563  { return a >= -tol && a <= tol; }
564 
565  static constexpr inline bool
567  { return a >= -tol && a <= tol; }
568 
569  h_compare(SYSisEqual, SYSabs(a-b)<=tol)
570  h_compare_ce(SYSisGreater, (a-b) > tol)
571  h_compare_ce(SYSisGreaterOrEqual, (a-b) >= -tol)
572  h_compare_ce(SYSisLess, (a-b) < -tol)
573  h_compare_ce(SYSisLessOrEqual, (a-b) <= tol)
574 #undef h_compare
575 #undef h_compare_ce
576 
577 constexpr inline bool SYSisEqual(int32 a, int32 b) { return a == b; }
578 constexpr inline bool SYSisEqual(int64 a, int64 b) { return a == b; }
579 constexpr inline bool SYSisGreater(int32 a, int32 b) { return a > b; }
580 constexpr inline bool SYSisGreater(int64 a, int64 b) { return a > b; }
581 constexpr inline bool SYSisGreaterOrEqual(int32 a, int32 b) { return a >= b; }
582 constexpr inline bool SYSisGreaterOrEqual(int64 a, int64 b) { return a >= b; }
583 constexpr inline bool SYSisLess(int32 a, int32 b) { return a < b; }
584 constexpr inline bool SYSisLess(int64 a, int64 b) { return a < b; }
585 constexpr inline bool SYSisLessOrEqual(int32 a, int32 b) { return a <= b; }
586 constexpr inline bool SYSisLessOrEqual(int64 a, int64 b) { return a <= b; }
587 
588 static inline bool
589 SYSalmostEqual(fpreal32 a, fpreal32 b, int ulps=50, fpreal32 tol = 1e-6)
590 {
591  SYS_FPRealUnionF ai, bi;
592  ai.fval = a;
593  bi.fval = b;
594 
595  // check if they are absolutely close together (only possible near zero)
596  if (SYSfabs(a - b) <= tol)
597  return true;
598 
599  // we keep the following check in to allow points that are within 1e-15 of
600  // zero to be "equal" for backwards compatibility (so 6e-16 and -6e-16 are
601  // "equal" even though they may not be within 1e-15, for example):
602  // If both double-precision floats are very close to zero, we consider them
603  // equal.
604  if((SYSabs(a) < 1e-6) && (SYSabs(b) < 1e-6))
605  return true;
606 
607  // if they have different signs they are not equal (since they must be at
608  // least tol distance apart by this point, there is no trickiness with +-0)
609  if ((ai.fval < 0.0) != (bi.fval < 0.0))
610  return false;
611 
612  // find the difference in ulps
613  // We're very careful to avoid signed integer overflow here.
614  if ((ai.uval > bi.uval ? ai.uval - bi.uval : bi.uval - ai.uval) <= ulps)
615  return true;
616 
617  return false;
618 }
619 
620 // adapted from AlmostEqualUlpsAndAbs from:
621 // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
622 static inline bool
623 SYSalmostEqual(fpreal64 a, fpreal64 b, int64 ulps, fpreal64 tol = 1e-15)
624 {
625  SYS_FPRealUnionD ai, bi;
626  ai.fval = a;
627  bi.fval = b;
628 
629  // check if they are absolutely close together (only possible near zero)
630  if (SYSfabs(a - b) <= tol)
631  return true;
632 
633  // we keep the following check in to allow points that are within 1e-15 of
634  // zero to be "equal" for backwards compatibility (so 6e-16 and -6e-16 are
635  // "equal" even though they may not be within 1e-15, for example):
636  // If both double-precision floats are very close to zero, we consider them
637  // equal.
638  if((SYSabs(a) < 1e-15) && (SYSabs(b) < 1e-15))
639  return true;
640 
641  // if they have different signs they are not equal (since they must be at
642  // least tol distance apart by this point, there is no trickiness with +-0)
643  if ((ai.fval < 0.0) != (bi.fval < 0.0))
644  return false;
645 
646  // find the difference in ulps
647  // We're very careful to avoid signed integer overflow here.
648  if ((ai.uval > bi.uval ? ai.uval - bi.uval : bi.uval - ai.uval) <= ulps)
649  return true;
650 
651  return false;
652 }
653 
654 static inline bool
655 SYSalmostEqual(fpreal64 a, fpreal64 b, int32 ulps=50, fpreal64 tol = 1e-15)
656 {
657  return SYSalmostEqual(a, b, (int64)ulps, tol);
658 }
659 
660 #define h_max3(type) \
661  static constexpr inline type \
662  SYSmax(type v0, type v1, type v2) { \
663  return SYSmax(v2, SYSmax(v0, v1)); \
664  }
665 #define h_max4(type) \
666  static constexpr inline type \
667  SYSmax(type v0, type v1, type v2, type v3) { \
668  return SYSmax(SYSmax(v0, v1), SYSmax(v2, v3)); \
669  }
670 #define h_argmax3(type) \
671  static constexpr inline int \
672  SYSargmax(type v0, type v1, type v2) { \
673  return v2 > SYSmax(v0, v1) ? 2 : SYSargmax(v0, v1); \
674  }
675 #define h_argmax4(type) \
676  static constexpr inline int \
677  SYSargmax(type v0, type v1, type v2, type v3) { \
678  return SYSmax(v0, v1) < SYSmax(v2, v3) ? \
679  (SYSargmax(v2, v3) + 2) : SYSargmax(v0, v1); \
680  }
681 #define h_min3(type) \
682  static constexpr inline type \
683  SYSmin(type v0, type v1, type v2) { \
684  return SYSmin(v2, SYSmin(v0, v1)); \
685  }
686 #define h_min4(type) \
687  static constexpr inline type \
688  SYSmin(type v0, type v1, type v2, type v3) { \
689  return SYSmin(SYSmin(v0, v1), SYSmin(v2, v3)); \
690  }
691 #define h_argmin3(type) \
692  static constexpr inline int \
693  SYSargmin(type v0, type v1, type v2) { \
694  return v2 < SYSmin(v0, v1) ? 2 : SYSargmin(v0, v1); \
695  }
696 #define h_argmin4(type) \
697  static constexpr inline int \
698  SYSargmin(type v0, type v1, type v2, type v3) { \
699  return SYSmin(v0, v1) > SYSmin(v2, v3) ? \
700  (SYSargmin(v2, v3) + 2) : SYSargmin(v0, v1); \
701  }
702 
703 
704 #define h_max(type) h_min3(type) h_min4(type) h_max3(type) h_max4(type) \
705  h_argmin3(type) h_argmin4(type) \
706  h_argmax3(type) h_argmax4(type)
707 
708 #define h_avg3(type) \
709  static constexpr inline type \
710  SYSavg(type v0, type v1, type v2) { \
711  return (v0+v1+v2) * ((type)(1.0/3.0)); \
712  }
713 #define h_avg4(type) \
714  static constexpr inline type \
715  SYSavg(type v0, type v1, type v2, type v3) { \
716  return (v0+v1+v2+v3) * ((type)0.25); \
717  }
718 
719 #define h_avg(type) h_avg3(type) h_avg4(type)
720 
721 h_max(int8)
722 h_max(uint8)
723 h_max(int16)
724 h_max(uint16)
725 h_max(int32)
726 h_max(uint32)
727 h_max(int64)
728 h_max(uint64)
729 h_max(fpreal32) h_avg(fpreal32)
730 h_max(fpreal64) h_avg(fpreal64)
731 
732 static constexpr inline int32
733 SYSavg(int32 a, int32 b, int32 c)
734 { return (a + b + c + 1) / 3; }
735 static constexpr inline int32
736 SYSavg(int32 a, int32 b, int32 c, int32 d)
737 { return (a + b + c + d + 2) / 4; }
738 
739 static constexpr inline int64
740 SYSavg(int64 a, int64 b, int64 c)
741 { return (a + b + c + 1) / 3; }
742 static constexpr inline int64
743 SYSavg(int64 a, int64 b, int64 c, int64 d)
744 { return (a + b + c + d + 2) / 4; }
745 
746 // Some systems have size_t as a seperate type from uint. Some don't.
747 #if defined(MBSD)
748 h_max(size_t)
749 #endif
750 
751 #undef h_min3
752 #undef h_min4
753 #undef h_max3
754 #undef h_max4
755 #undef h_argmin3
756 #undef h_argmin4
757 #undef h_argmax3
758 #undef h_argmax4
759 #undef h_avg3
760 #undef h_avg4
761 #undef h_max
762 #undef h_avg
763 
764 /// @{
765 /// Linear interpolation between v0 and v1.
766 static constexpr inline fpreal32
768 {
769  return v1 + (v2 - v1)*t;
770 }
771 
772 static constexpr inline fpreal64
774 {
775  return v1 + (v2 - v1)*t;
776 }
777 /// @}
778 
779 /// @{
780 /// Bilinear interpolation over a quadrilateral: @code
781 /// (u=0,v=1) u0v1 u1v1 (u=1,v=1)
782 /// +--------+
783 /// | |
784 /// | |
785 /// +--------+
786 /// (u=0,v=0) u0v0 u1v0 (u=1,v=0)
787 /// @endcode
788 static constexpr inline fpreal32
789 SYSbilerp(fpreal32 u0v0, fpreal32 u1v0, fpreal32 u0v1, fpreal32 u1v1,
790  fpreal32 u, fpreal32 v)
791 {
792  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
793 }
794 
795 static constexpr inline fpreal64
796 SYSbilerp(fpreal64 u0v0, fpreal64 u1v0, fpreal64 u0v1, fpreal64 u1v1,
797  fpreal64 u, fpreal64 v)
798 {
799  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
800 }
801 /// @}
802 
803 /// @{
804 /// Barycentric coordinates do interpolation over the triangle specified by the
805 /// three vertices (v0, v1, v2): @code
806 /// | (u=0,v=1) |
807 /// | v2 |
808 /// | / \ |
809 /// | / \ |
810 /// | / \ |
811 /// | (u=0,v=0) v0------v1 (u=1,v=0) |
812 /// @endcode
813 static constexpr inline fpreal32
815 {
816  return v0*(1-u-v) + v1*u + v2*v;
817 }
818 
819 static constexpr inline fpreal64
821 {
822  return v0*(1-u-v) + v1*u + v2*v;
823 }
824 /// @}
825 
826 /// @{
827 /// Wrap mod is like mod, but returns [0..b) interval thereby
828 /// usually giving a more useful result.
829 /// This is always defined as safe!
830 /// @}
831 static constexpr inline fpreal32
832 SYSwrapmod(fpreal32 a, fpreal32 b)
833 {
834  fpreal32 r = SYSsafefmod(a, b);
835  return ((a < 0) ^ (b < 0) && r != 0.0f) ? r+b : r;
836 }
837 
838 static constexpr inline fpreal64
839 SYSwrapmod(fpreal64 a, fpreal64 b)
840 {
841  fpreal64 r = SYSsafefmod(a, b);
842  return ((a < 0) ^ (b < 0) && r != 0.0) ? r+b : r;
843 }
844 
845 static constexpr inline int32
846 SYSwrapmod(int32 a, int32 b)
847 {
848  if (b == 0)
849  return 0;
850  int32 r = a % b;
851  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
852 }
853 
854 static constexpr inline int64
855 SYSwrapmod(int64 a, int64 b)
856 {
857  if (b == 0)
858  return 0;
859  int64 r = a % b;
860  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
861 }
862 
863 static constexpr inline fpreal32
864 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 val)
865 {
866  if (val <= min) return 0;
867  if (val >= max) return 1;
868  fpreal32 t = max - min;
869  if (SYSequalZero(t, (fpreal32)1e-8)) return (fpreal32).5;
870  t = (val - min) / t;
871  return t*t*((fpreal32)3.0 - (fpreal32)2.0*t);
872 }
873 
874 static constexpr inline fpreal64
875 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 val)
876 {
877  if (val <= min) return 0;
878  if (val >= max) return 1;
879  fpreal64 t = max - min;
880  if (SYSequalZero(t, (fpreal64)1e-18)) return (fpreal64).5;
881  t = (val - min) / t;
882  return t*t*((fpreal64)3.0 - (fpreal64)2.0*t);
883 }
884 
885 static constexpr inline fpreal32
886 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 value, fpreal32 roll)
887 {
888  if (roll > 0)
889  {
890  fpreal32 f = SYSsmooth(min, max, value);
891  return roll < (fpreal32)1 ? (fpreal32)1-SYSpow((fpreal32)1-f,
892  (fpreal32)1/roll) : SYSpow(f, roll);
893  }
894  return (fpreal32)1;
895 }
896 
897 static constexpr inline fpreal64
898 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 value, fpreal64 roll)
899 {
900  if (roll > 0)
901  {
902  fpreal64 f = SYSsmooth(min, max, value);
903  return roll < (fpreal64)1 ? (fpreal64)1-SYSpow((fpreal64)1-f,
904  (fpreal64)1/roll) : SYSpow(f, roll);
905  }
906  return (fpreal64)1;
907 }
908 
909 static constexpr inline fpreal32
910 SYSfit(fpreal32 val, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
911 {
912  fpreal32 d = omax - omin;
913  if (SYSequalZero(d, (fpreal32)1e-8))
914  return (nmin+nmax)*(fpreal32).5;
915  if (omin < omax)
916  {
917  if (val < omin) return nmin;
918  if (val > omax) return nmax;
919  }
920  else
921  {
922  if (val < omax) return nmax;
923  if (val > omin) return nmin;
924  }
925  return nmin + (nmax-nmin)*(val-omin)/d;
926 }
927 
928 static constexpr inline fpreal64
929 SYSfit(fpreal64 val, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
930 {
931  fpreal64 d = omax - omin;
932  if (SYSequalZero(d, (fpreal64)1e-18))
933  return (nmin+nmax)*(fpreal64).5;
934  if (omin < omax)
935  {
936  if (val < omin) return nmin;
937  if (val > omax) return nmax;
938  }
939  else
940  {
941  if (val < omax) return nmax;
942  if (val > omin) return nmin;
943  }
944  return nmin + (nmax-nmin)*(val-omin)/d;
945 }
946 
947 /// SYSefit() will not clamp the values to the range
948 static constexpr inline fpreal32
949 SYSefit(fpreal32 v, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
950 {
951  fpreal32 d = omax - omin;
952  if (SYSequalZero(d, (fpreal32)1e-8))
953  return (nmin+nmax)*0.5f;
954  return nmin + (nmax-nmin)*(v-omin)/d;
955 }
956 
957 static constexpr inline fpreal64
958 SYSefit(fpreal64 v, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
959 {
960  fpreal64 d = omax - omin;
961  if (SYSequalZero(d, (fpreal64)1e-18))
962  return (nmin+nmax)*0.5f;
963  return nmin + (nmax-nmin)*(v-omin)/d;
964 }
965 
966 static constexpr inline fpreal32
967 SYSfit01(fpreal32 val, fpreal32 nmin, fpreal32 nmax)
968 {
969  if (val < 0) return nmin;
970  if (val > 1) return nmax;
971  return SYSlerp(nmin, nmax, val);
972 }
973 
974 static constexpr inline fpreal64
975 SYSfit01(fpreal64 val, fpreal64 nmin, fpreal64 nmax)
976 {
977  if (val < 0) return nmin;
978  if (val > 1) return nmax;
979  return SYSlerp(nmin, nmax, val);
980 }
981 
982 /// SYSinvlerp() will produce the bias needed for lerp, but avoid
983 /// zeros. Essentially built on efit.
984 static constexpr inline fpreal32
985 SYSinvlerp(fpreal32 v, fpreal32 omin, fpreal32 omax)
986 {
987  return SYSefit(v, omin, omax, 0.0f, 1.0f);
988 }
989 
990 static constexpr inline fpreal64
991 SYSinvlerp(fpreal64 v, fpreal64 omin, fpreal64 omax)
992 {
993  return SYSefit(v, omin, omax, 0.0, 1.0);
994 }
995 
996 // Linear hat function over kernel width dx.
997 static inline fpreal32
998 SYShat(fpreal32 x, fpreal32 dx)
999 {
1000  const fpreal32 ax = SYSabs(x);
1001  return (ax > dx ? 0 : 1 - ax / dx);
1002 }
1003 
1004 static inline fpreal64
1005 SYShat(fpreal64 x, fpreal64 dx)
1006 {
1007  const fpreal64 ax = SYSabs(x);
1008  return (ax > dx ? 0 : 1 - ax / dx);
1009 }
1010 
1011 // Derivative of the linear hat function over kernel width dx.
1012 static constexpr inline fpreal32
1013 SYSdhat(fpreal32 x, fpreal32 dx)
1014 {
1015  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1016 }
1017 
1018 static constexpr inline fpreal64
1019 SYSdhat(fpreal64 x, fpreal64 dx)
1020 {
1021  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1022 }
1023 
1024 
1025 // For integer types.
1026 template<typename T>
1027 constexpr inline T
1028 SYSroundDownToMultipleOf(T val, T multiple)
1029 {
1030  // Only handle multiples of 2 and up.
1031  if (multiple <= 1)
1032  return val;
1033 
1034  int rem = val % multiple;
1035  if (rem == 0)
1036  return val;
1037 
1038  if (val < 0)
1039  return val - multiple - rem;
1040  else
1041  return val - rem;
1042 }
1043 
1044 template<>
1045 inline fpreal32
1046 SYSroundDownToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1047 {
1048  if (SYSequalZero(multiple))
1049  return val;
1050  fpreal32 modulus = SYSfmod(val, multiple);
1051  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1052  return val;
1053  fpreal32 retval = val - modulus;
1054  if( val < (fpreal32)0 && modulus!=(fpreal32)0 )
1055  retval -= multiple;
1056  return retval;
1057 }
1058 
1059 template<>
1060 inline fpreal64
1061 SYSroundDownToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1062 {
1063  if (SYSequalZero(multiple))
1064  return val;
1065  fpreal64 modulus = SYSfmod(val, multiple);
1066  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1067  return val;
1068  fpreal64 retval = val - modulus;
1069  if( val < (fpreal64)0 && modulus!=(fpreal64)0 )
1070  retval -= multiple;
1071  return retval;
1072 }
1073 
1074 template<typename T>
1075 constexpr inline T
1076 SYSroundUpToMultipleOf(T val, T multiple)
1077 {
1078  // Only handle multiples of 2 and up.
1079  if (multiple <= 1)
1080  return val;
1081 
1082  int rem = val % multiple;
1083  if (rem == 0)
1084  return val;
1085 
1086  if (val < 0)
1087  return val - rem;
1088  else
1089  return val + multiple - rem;
1090 }
1091 
1092 template<>
1093 inline fpreal32
1094 SYSroundUpToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1095 {
1096  fpreal32 modulus;
1097  if (SYSequalZero(multiple))
1098  return val;
1099  modulus = SYSfmod(val, multiple);
1100  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1101  return val;
1102  if (val > (fpreal32)0)
1103  val += multiple;
1104  return val - modulus;
1105 }
1106 
1107 template<>
1108 inline fpreal64
1109 SYSroundUpToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1110 {
1111  fpreal64 modulus;
1112  if (SYSequalZero(multiple))
1113  return val;
1114  modulus = SYSfmod(val, multiple);
1115  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1116  return val;
1117  if (val > (fpreal64)0)
1118  val += multiple;
1119  return val - modulus;
1120 }
1121 
1122 inline constexpr uint32
1123 SYSroundUpPow2(uint32 val)
1124 {
1125  // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1126  uint32 v = val;
1127 
1128  v--;
1129  v |= v >> 1;
1130  v |= v >> 2;
1131  v |= v >> 4;
1132  v |= v >> 8;
1133  v |= v >> 16;
1134  v++;
1135 
1136  return v;
1137 }
1138 
1139 static constexpr inline uint32
1140 SYSwang_inthash(uint32 key)
1141 {
1142  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1143  key += ~(key << 16);
1144  key ^= (key >> 5);
1145  key += (key << 3);
1146  key ^= (key >> 13);
1147  key += ~(key << 9);
1148  key ^= (key >> 17);
1149  return key;
1150 }
1151 
1152 
1153 static constexpr inline uint32
1154 SYSmultiplicative_inthash(uint32 key)
1155 {
1156  // Multiply by the golden mean of 2^32 (Knuth's multiplicative method) to
1157  // get a uniformly distributed hash value. Do a google search for
1158  // 2654435761 for more information.
1159  return key * 2654435761u;
1160 }
1161 
1162 static constexpr inline uint64
1163 SYSwang_inthash64(uint64 key)
1164 {
1165  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1166  key += ~(key << 32ULL);
1167  key ^= (key >> 22);
1168  key += ~(key << 13);
1169  key ^= (key >> 8);
1170  key += (key << 3);
1171  key ^= (key >> 15);
1172  key += ~(key << 27);
1173  key ^= (key >> 31);
1174  return key;
1175 }
1176 
1177 static constexpr inline uint32
1178 SYSsharpe_inthash(uint32 key)
1179 {
1180  // Based on https://github.com/skeeto/hash-prospector, but with
1181  // modifications such that:
1182  // - bias value of 0.1406800449
1183  // - minimum cycle of 17 (the original algorithm had f(0) == 0)
1184  // - improved dieharder results
1185  key ^= (key >> 16);
1186  key *= 0xdbfb352dU;
1187  key ^= ~(key >> 15);
1188  key *= 0x6c2ca68bU;
1189  key ^= (key >> 16);
1190  return key;
1191 }
1192 
1193 static constexpr inline uint32
1194 SYSwang2_inthash(uint32 key)
1195 {
1196  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1197  // Updated integer hashing (2007)
1198  constexpr uint c2=0x27d4eb2d; // a prime or an odd constant
1199  key = (key ^ 61) ^ (key >> 16);
1200  key = key + (key << 3);
1201  key = key ^ (key >> 4);
1202  key = key * c2;
1203  key = key ^ (key >> 15);
1204  return key;
1205 }
1206 
1207 static constexpr inline uint64
1208 SYSwang2_inthash(uint64 key)
1209 {
1210  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1211  // Updated integer hashing (2007)
1212  key = (~key) + (key << 21); // key = (key<<21) - key - 1;
1213  key = key ^ (key >> 24);
1214  key = (key + (key << 3)) + (key << 8); // key * 265
1215  key = key ^ (key >> 14);
1216  key = (key + (key << 2)) + (key << 4); // key * 21
1217  key = key ^ (key >> 28);
1218  key = key + (key << 31);
1219  return key;
1220 }
1221 
1222 static inline uint
1223 SYSreal_hash(fpreal16 a, int lowmask=0x3)
1224 {
1225  return SYSwang_inthash(a.bits() & (~lowmask));
1226 }
1227 
1228 static inline uint
1229 SYSreal_hashseed(fpreal16 a, uint seed, int lowmask=0x3)
1230 {
1231  return SYSwang_inthash(seed + (a.bits() & (~lowmask)));
1232 }
1233 
1234 static inline uint
1235 SYSreal_hash(fpreal32 a, int lowmask=0xf)
1236 {
1237  SYS_FPRealUnionF ai;
1238  ai.fval = a;
1239  return SYSwang_inthash(ai.uval & (~lowmask));
1240 }
1241 
1242 static inline uint
1243 SYSreal_hashseed(fpreal32 a, uint seed, int lowmask=0xf)
1244 {
1245  SYS_FPRealUnionF ai;
1246  ai.fval = a;
1247  return SYSwang_inthash(seed + (ai.uval & (~lowmask)));
1248 }
1249 
1250 static inline uint
1251 SYSreal_hash(fpreal64 a, int lowmask=0xf)
1252 {
1253  SYS_FPRealUnionD ai;
1254  ai.fval = a;
1255  return SYSwang_inthash64(ai.uval & (~lowmask));
1256 }
1257 
1258 static inline uint
1259 SYSreal_hashseed(fpreal64 a, uint seed, int64 lowmask=0xf)
1260 {
1261  SYS_FPRealUnionD ai;
1262  ai.fval = a;
1263  return SYSwang_inthash64(seed + (ai.uval & (~lowmask)));
1264 }
1265 
1266 static constexpr inline uint
1267 SYSvector_hash(const int32 *vector, int size)
1268 {
1269  uint hash = 0;
1270  for (int i = 0; i < size; ++i)
1271  hash = SYSwang_inthash(hash + vector[i]);
1272  return hash;
1273 }
1274 
1275 static constexpr inline uint
1276 SYSvector_hash(const int64 *vector, int size)
1277 {
1278  uint hash = 0;
1279  for (int i = 0; i < size; ++i)
1280  hash = (uint)SYSwang_inthash64(hash + vector[i]);
1281  return hash;
1282 }
1283 
1284 static inline uint
1285 SYSvector_hash(const fpreal16 *vector, int size)
1286 {
1287  uint hash = 0;
1288  for (int i = 0; i < size; ++i)
1289  hash = SYSreal_hashseed(vector[i], hash);
1290  return hash;
1291 }
1292 
1293 static inline uint
1294 SYSvector_hash(const fpreal32 *vector, int size)
1295 {
1296  uint hash = 0;
1297  for (int i = 0; i < size; ++i)
1298  hash = SYSreal_hashseed(vector[i], hash);
1299  return hash;
1300 }
1301 
1302 static inline uint
1303 SYSvector_hash(const fpreal64 *vector, int size)
1304 {
1305  uint hash = 0;
1306  for (int i = 0; i < size; ++i)
1307  hash = SYSreal_hashseed(vector[i], hash);
1308  return hash;
1309 }
1310 
1311 // Spatial hashing function for 3-vectors
1312 // Reference:
1313 // "Optimized Spatial Hashing for Collision Detection of Deformable Objects"
1314 //
1315 template<class P>
1316 static inline size_t
1317 SYSvector3_hash(const P &vector)
1318 {
1319  static constexpr size_t p1 = 73856093;
1320  static constexpr size_t p2 = 19349663;
1321  static constexpr size_t p3 = 83492791;
1322 
1323  return size_t(vector.x()*p1) ^ size_t(vector.y()*p2) ^ size_t(vector.z()*p3);
1324 }
1325 
1326 static inline uint
1327 SYSpointerHash(const void *ptr)
1328 {
1329 #if SIZEOF_VOID_P == 8
1330  // 64 bit pointers
1331  return (uint)((int64)ptr & 0xffffffff);
1332 #else
1333  // 32 bit pointers
1334  return (uint)ptr;
1335 #endif
1336 }
1337 
1338 // Convert a uniform random bit pattern to a random float in the range [0, 1)
1339 inline static fpreal32
1340 SYShashToFloat01(uint hash)
1341 {
1342  SYS_FPRealUnionF tmp;
1343  tmp.uval = 0x3f800000 | (0x007fffff & hash);
1344  return tmp.fval-1.0F;
1345 }
1346 
1347 // Generate a random number in range [0, 1)
1348 static inline fpreal32
1349 SYSfastRandom(uint &seed)
1350 {
1351  seed = seed*1664525 + 1013904223;
1352  return SYShashToFloat01(seed);
1353 }
1354 
1355 inline static fpreal32
1356 SYSrandom(uint &seed)
1357 {
1358  seed = seed*1664525 + 1013904223;
1359  return SYShashToFloat01(SYSwang_inthash(seed));
1360 }
1361 
1362 static constexpr uint
1363 SYSfastRandomInt(uint &seed)
1364 {
1365  seed = seed*1664525 + 1013904223;
1366  return SYSwang_inthash(seed);
1367 }
1368 
1369 inline static fpreal32
1370 SYSfastRandomZero(uint &seed)
1371 {
1372  return SYSfastRandom(seed) - 0.5F;
1373 }
1374 
1375 inline static fpreal32
1376 SYSrandomZero(uint &seed)
1377 {
1378  return SYSrandom(seed) - 0.5F;
1379 }
1380 
1381 template <typename T>
1382 static constexpr inline void
1383 SYSminmax(T v0, T v1, T v2, T v3, T &min, T &max)
1384 {
1385  min = SYSmin(v0, v1, v2, v3);
1386  max = SYSmax(v0, v1, v2, v3);
1387 }
1388 
1389 template <typename T>
1390 static constexpr inline void
1391 SYSminmax(T v0, T v1, T v2, T &min, T &max)
1392 {
1393  min = SYSmin(v0, v1, v2);
1394  max = SYSmax(v0, v1, v2);
1395 }
1396 
1397 template <typename T>
1398 static constexpr inline void
1399 SYSminmax(T v0, T v1, T &min, T &max)
1400 {
1401  min = SYSmin(v0, v1);
1402  max = SYSmax(v0, v1);
1403 }
1404 
1405 inline static void
1406 SYSgetSinCosFromSlope(fpreal32 slope, fpreal32 &sintheta, fpreal32 &costheta)
1407 {
1408  fpreal32 one_over_m;
1409  sintheta = slope / SYSsqrt(slope*slope + (fpreal32)1);
1410  if ((slope = SYSabs(slope)) > (fpreal32)1)
1411  {
1412  one_over_m = (fpreal32)1 / slope;
1413  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1414  }
1415  else
1416  costheta = SYSsqrt((fpreal32)1 - sintheta*sintheta);
1417 }
1418 
1419 inline static void
1420 SYSgetSinCosFromSlope(fpreal64 slope, fpreal64 &sintheta, fpreal64 &costheta)
1421 {
1422  fpreal64 one_over_m;
1423  sintheta = slope / SYSsqrt(slope*slope + (fpreal64)1);
1424  if ((slope = SYSabs(slope)) > (fpreal64)1)
1425  {
1426  one_over_m = (fpreal64)1 / slope;
1427  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1428  }
1429  else
1430  costheta = SYSsqrt((fpreal64)1 - sintheta*sintheta);
1431 }
1432 
1433 inline constexpr static bool
1434 SYSsameSign( fpreal32 v0, fpreal32 v1 )
1435 {
1436  return (v0*v1)>0;
1437 }
1438 
1439 inline constexpr static bool
1440 SYSsameSign( fpreal64 v0, fpreal64 v1 )
1441 {
1442  return (v0*v1)>0;
1443 }
1444 
1445 inline constexpr static bool
1446 SYSsameSign( int32 v0, int32 v1 )
1447 {
1448  return (v0 ^ v1) >= 0;
1449 }
1450 
1451 inline constexpr static bool
1452 SYSsameSign( int64 v0, int64 v1 )
1453 {
1454  return (v0 ^ v1) >= 0;
1455 }
1456 
1457 static inline uint
1458 SYSnextPrime(uint num)
1459 {
1460  return SYSmakePrime(num+1);
1461 }
1462 
1463 static constexpr inline int
1464 SYShexCharToInt(char c)
1465 {
1466  // Given a hexidecimal character, return it's corresponding value between
1467  // 0 and 15, or -1 if it's not a hexidecimal character.
1468  if (c >= '0' && c <= '9')
1469  return c - '0';
1470  if (c >= 'a' && c <= 'f')
1471  return c - 'a' + 10;
1472  if (c >= 'A' && c <= 'F')
1473  return c - 'A' + 10;
1474  return -1;
1475 }
1476 
1477 static constexpr inline char
1478 SYSintToHexChar(int value)
1479 {
1480  // The value must be from 0-15 for this function to return a valid result.
1481  return value < 10 ? '0' + value : 'a' + value - 10;
1482 }
1483 
1484 SYS_API void SYSsort(int &a, int &b);
1485 SYS_API void SYSsort(int &a, int &b, int &c);
1486 SYS_API void SYSsort(int64 &a, int64 &b);
1487 SYS_API void SYSsort(int64 &a, int64 &b, int64 &c);
1488 SYS_API void SYSsort(float &a, float &b);
1489 SYS_API void SYSsort(float &a, float &b, float &c);
1490 SYS_API void SYSsort(double &a, double &b);
1491 SYS_API void SYSsort(double &a, double &b, double &c);
1492 
1493 // Compute both integer division and integer modulus
1494 // They are compiled into a single instruction
1495 static constexpr inline void
1496 SYSdivMod(int numerator, int denominator, int &quotient, int &remainder)
1497 {
1498  quotient = numerator / denominator;
1499  remainder = numerator % denominator;
1500 }
1501 
1502 static constexpr inline void
1503 SYSdivMod(int64 numerator, int64 denominator, int64 &quotient, int64 &remainder)
1504 {
1505  quotient = numerator / denominator;
1506  remainder = numerator % denominator;
1507 }
1508 
1509 // Include permutations of fpreal32/fpreal64
1510 #include "SYS_MathPermute.h"
1511 #include "SYS_MathRestrictive.h"
1512 
1513 #else /* cplusplus */
1514 #define SYSmax(a,b) ((a) > (b) ? (a) : (b))
1515 #define SYSmin(a,b) ((a) < (b) ? (a) : (b))
1516 #define SYSabs(a) ((a) < 0 ? (a) : -(a))
1517 #endif
1518 
1519 #endif
GLdouble s
Definition: glew.h:1390
SYS_API float acosf(float x)
#define SYSmax(a, b)
Definition: SYS_Math.h:1514
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
unsigned short uint16
Definition: SYS_Types.h:38
SYS_API double fmod(double x, double y)
Definition: SYS_FPUMath.h:85
SYS_API double atan2(double y, double x)
Definition: SYS_FPUMath.h:79
UT_Vector3T< T > SYSrecip(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:862
SYS_API double expm1(double x)
Definition: SYS_FPUMath.h:95
GLsizeiptr size
Definition: glew.h:1681
int int32
Definition: SYS_Types.h:39
int trunc(T x)
Definition: ImathFun.h:165
SYS_API void SYSsrand48(long seed)
GLuint const GLfloat * val
Definition: glew.h:2794
bool SYSisFinite(fpreal64 f)
Definition: SYS_Math.h:194
#define CONST_INT64(x)
Definition: SYS_Types.h:320
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7231
OIIO_HOSTDEVICE void sincos(float x, float *sine, float *cosine)
Definition: fmath.h:534
constexpr bool SYSisNan(const F f)
Definition: SYS_Math.h:178
#define SYSabs(a)
Definition: SYS_Math.h:1516
SYS_API float atan2f(float y, float x)
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
UT_Matrix2T< T > SYSbilerp(const UT_Matrix2T< T > &u0v0, const UT_Matrix2T< T > &u1v0, const UT_Matrix2T< T > &u0v1, const UT_Matrix2T< T > &u1v1, S u, S v)
Bilinear interpolation.
Definition: UT_Matrix2.h:72
const GLdouble * v
Definition: glew.h:1391
#define SYS_FTOLERANCE_D
Definition: SYS_Types.h:209
UT_Matrix2T< T > SYSlerp(const UT_Matrix2T< T > &v1, const UT_Matrix2T< T > &v2, S t)
Definition: UT_Matrix2.h:604
unsigned long long uint64
Definition: SYS_Types.h:117
SYS_API double log10(double x)
Definition: SYS_FPUMath.h:89
UT_Matrix2T< T > SYSbarycentric(const UT_Matrix2T< T > &v0, const UT_Matrix2T< T > &v1, const UT_Matrix2T< T > &v2, S u, S v)
Barycentric interpolation.
Definition: UT_Matrix2.h:79
float fpreal32
Definition: SYS_Types.h:200
#define M_PI
Definition: ImathPlatform.h:51
constexpr int SYSsignum(const F a)
Definition: SYS_Math.h:170
SYS_API float log10f(float x)
double fpreal64
Definition: SYS_Types.h:201
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
unsigned char uint8
Definition: SYS_Types.h:36
INT32 * numerator
Definition: wglew.h:1180
GLfloat GLfloat GLfloat v2
Definition: glew.h:1856
SYS_API double asin(double x)
Definition: SYS_FPUMath.h:81
SYS_API double sinh(double x)
GLclampf f
Definition: glew.h:3499
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
SYS_API double copysign(double x, double y)
INT32 INT32 * denominator
Definition: wglew.h:1180
SYS_API float atanf(float x)
SYS_API fpreal32 SYSroundAngle(fpreal32 base, fpreal32 source)
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:836
SYS_API double cosh(double x)
SYS_API bool SYSisPrime(uint num)
const GLfloat * c
Definition: glew.h:16296
SYS_API void sincosf(float x, float *s, float *c)
bool isFinite() const
Definition: fpreal16.h:632
long long int64
Definition: SYS_Types.h:116
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
SYS_API double SYSdrand48()
UT_Vector2T< T > SYSinvlerp(const UT_Vector2T< T > &a, const UT_Vector2T< T > &v1, const UT_Vector2T< T > &v2)
Componentwise inverse linear interpolation.
Definition: UT_Vector2.h:491
SYS_API float logf(float x)
T exp(const T &v)
Definition: simd.h:7377
SYS_API double acos(double x)
Definition: SYS_FPUMath.h:83
signed char int8
Definition: SYS_Types.h:35
SYS_API bool SYSisInt(const char *str)
SYS_API double hypot(double x, double y)
fpreal32 SYSrint(fpreal32 val)
Definition: SYS_Floor.h:163
SYS_API double log1p(double x)
Definition: SYS_FPUMath.h:93
SYS_API double tanh(double x)
Definition: SYS_FPUMath.h:103
bool SYSequalZero(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:848
SYS_API uint SYSmakePrime(uint num)
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
SYS_API double tan(double x)
Definition: SYS_FPUMath.h:75
T log(const T &v)
Definition: simd.h:7432
GLuint num
Definition: glew.h:2690
const void * ptr(const T *p)
Definition: format.h:3292
short int16
Definition: SYS_Types.h:37
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
SYS_API double atan(double x)
Definition: SYS_FPUMath.h:77
bool isNan() const
Definition: fpreal16.h:664
GLfloat v0
Definition: glew.h:1848
unsigned int uint32
Definition: SYS_Types.h:40
SYS_API float asinf(float x)
GLuint64EXT * result
Definition: glew.h:14007
INT64 INT64 INT64 remainder
Definition: wglew.h:1182
#define SYS_FTOLERANCE
Definition: SYS_Types.h:208
#define SYS_API
Definition: SYS_API.h:11
#define CONST_UINT64(x)
Definition: SYS_Types.h:321
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
unsigned short bits() const
Definition: fpreal16.h:725
#define SYSmin(a, b)
Definition: SYS_Math.h:1515
GLsizei const GLfloat * value
Definition: glew.h:1849
SYS_API bool SYSisFloat(const char *str)
unsigned int uint
Definition: SYS_Types.h:45
GLfloat GLfloat GLfloat GLfloat v3
Definition: glew.h:1860
GLdouble GLdouble t
Definition: glew.h:1398
GLfloat GLfloat v1
Definition: glew.h:1852
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71