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 static inline bool SYSisInteger(fpreal32 val)
661  { return SYSisEqual(val, SYSfloor(val)) || SYSisEqual(val, SYSceil(val)); }
662 static inline bool SYSisInteger(fpreal64 val)
663  { return SYSisEqual(val, SYSfloor(val)) || SYSisEqual(val, SYSceil(val)); }
664 
665 #define h_max3(type) \
666  static constexpr inline type \
667  SYSmax(type v0, type v1, type v2) { \
668  return SYSmax(v2, SYSmax(v0, v1)); \
669  }
670 #define h_max4(type) \
671  static constexpr inline type \
672  SYSmax(type v0, type v1, type v2, type v3) { \
673  return SYSmax(SYSmax(v0, v1), SYSmax(v2, v3)); \
674  }
675 #define h_argmax3(type) \
676  static constexpr inline int \
677  SYSargmax(type v0, type v1, type v2) { \
678  return v2 > SYSmax(v0, v1) ? 2 : SYSargmax(v0, v1); \
679  }
680 #define h_argmax4(type) \
681  static constexpr inline int \
682  SYSargmax(type v0, type v1, type v2, type v3) { \
683  return SYSmax(v0, v1) < SYSmax(v2, v3) ? \
684  (SYSargmax(v2, v3) + 2) : SYSargmax(v0, v1); \
685  }
686 #define h_min3(type) \
687  static constexpr inline type \
688  SYSmin(type v0, type v1, type v2) { \
689  return SYSmin(v2, SYSmin(v0, v1)); \
690  }
691 #define h_min4(type) \
692  static constexpr inline type \
693  SYSmin(type v0, type v1, type v2, type v3) { \
694  return SYSmin(SYSmin(v0, v1), SYSmin(v2, v3)); \
695  }
696 #define h_argmin3(type) \
697  static constexpr inline int \
698  SYSargmin(type v0, type v1, type v2) { \
699  return v2 < SYSmin(v0, v1) ? 2 : SYSargmin(v0, v1); \
700  }
701 #define h_argmin4(type) \
702  static constexpr inline int \
703  SYSargmin(type v0, type v1, type v2, type v3) { \
704  return SYSmin(v0, v1) > SYSmin(v2, v3) ? \
705  (SYSargmin(v2, v3) + 2) : SYSargmin(v0, v1); \
706  }
707 
708 
709 #define h_max(type) h_min3(type) h_min4(type) h_max3(type) h_max4(type) \
710  h_argmin3(type) h_argmin4(type) \
711  h_argmax3(type) h_argmax4(type)
712 
713 #define h_avg3(type) \
714  static constexpr inline type \
715  SYSavg(type v0, type v1, type v2) { \
716  return (v0+v1+v2) * ((type)(1.0/3.0)); \
717  }
718 #define h_avg4(type) \
719  static constexpr inline type \
720  SYSavg(type v0, type v1, type v2, type v3) { \
721  return (v0+v1+v2+v3) * ((type)0.25); \
722  }
723 
724 #define h_avg(type) h_avg3(type) h_avg4(type)
725 
726 h_max(int8)
727 h_max(uint8)
728 h_max(int16)
729 h_max(uint16)
730 h_max(int32)
731 h_max(uint32)
732 h_max(int64)
733 h_max(uint64)
734 h_max(fpreal32) h_avg(fpreal32)
735 h_max(fpreal64) h_avg(fpreal64)
736 
737 static constexpr inline int32
738 SYSavg(int32 a, int32 b, int32 c)
739 { return (a + b + c + 1) / 3; }
740 static constexpr inline int32
741 SYSavg(int32 a, int32 b, int32 c, int32 d)
742 { return (a + b + c + d + 2) / 4; }
743 
744 static constexpr inline int64
745 SYSavg(int64 a, int64 b, int64 c)
746 { return (a + b + c + 1) / 3; }
747 static constexpr inline int64
748 SYSavg(int64 a, int64 b, int64 c, int64 d)
749 { return (a + b + c + d + 2) / 4; }
750 
751 // Some systems have size_t as a seperate type from uint. Some don't.
752 #if defined(MBSD)
753 h_max(size_t)
754 #endif
755 
756 #undef h_min3
757 #undef h_min4
758 #undef h_max3
759 #undef h_max4
760 #undef h_argmin3
761 #undef h_argmin4
762 #undef h_argmax3
763 #undef h_argmax4
764 #undef h_avg3
765 #undef h_avg4
766 #undef h_max
767 #undef h_avg
768 
769 /// @{
770 /// Linear interpolation between v0 and v1.
771 static constexpr inline fpreal32
773 {
774  return v1 + (v2 - v1)*t;
775 }
776 
777 static constexpr inline fpreal64
779 {
780  return v1 + (v2 - v1)*t;
781 }
782 /// @}
783 
784 /// @{
785 /// Bilinear interpolation over a quadrilateral: @code
786 /// (u=0,v=1) u0v1 u1v1 (u=1,v=1)
787 /// +--------+
788 /// | |
789 /// | |
790 /// +--------+
791 /// (u=0,v=0) u0v0 u1v0 (u=1,v=0)
792 /// @endcode
793 static constexpr inline fpreal32
794 SYSbilerp(fpreal32 u0v0, fpreal32 u1v0, fpreal32 u0v1, fpreal32 u1v1,
795  fpreal32 u, fpreal32 v)
796 {
797  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
798 }
799 
800 static constexpr inline fpreal64
801 SYSbilerp(fpreal64 u0v0, fpreal64 u1v0, fpreal64 u0v1, fpreal64 u1v1,
802  fpreal64 u, fpreal64 v)
803 {
804  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
805 }
806 /// @}
807 
808 /// @{
809 /// Barycentric coordinates do interpolation over the triangle specified by the
810 /// three vertices (v0, v1, v2): @code
811 /// | (u=0,v=1) |
812 /// | v2 |
813 /// | / \ |
814 /// | / \ |
815 /// | / \ |
816 /// | (u=0,v=0) v0------v1 (u=1,v=0) |
817 /// @endcode
818 static constexpr inline fpreal32
820 {
821  return v0*(1-u-v) + v1*u + v2*v;
822 }
823 
824 static constexpr inline fpreal64
826 {
827  return v0*(1-u-v) + v1*u + v2*v;
828 }
829 /// @}
830 
831 /// @{
832 /// Wrap mod is like mod, but returns [0..b) interval thereby
833 /// usually giving a more useful result.
834 /// This is always defined as safe!
835 /// @}
836 static constexpr inline fpreal32
837 SYSwrapmod(fpreal32 a, fpreal32 b)
838 {
839  fpreal32 r = SYSsafefmod(a, b);
840  return ((a < 0) ^ (b < 0) && r != 0.0f) ? r+b : r;
841 }
842 
843 static constexpr inline fpreal64
844 SYSwrapmod(fpreal64 a, fpreal64 b)
845 {
846  fpreal64 r = SYSsafefmod(a, b);
847  return ((a < 0) ^ (b < 0) && r != 0.0) ? r+b : r;
848 }
849 
850 static constexpr inline int32
851 SYSwrapmod(int32 a, int32 b)
852 {
853  if (b == 0)
854  return 0;
855  int32 r = a % b;
856  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
857 }
858 
859 static constexpr inline int64
860 SYSwrapmod(int64 a, int64 b)
861 {
862  if (b == 0)
863  return 0;
864  int64 r = a % b;
865  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
866 }
867 
868 static constexpr inline fpreal32
869 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 val)
870 {
871  if (val <= min) return 0;
872  if (val >= max) return 1;
873  fpreal32 t = max - min;
874  if (SYSequalZero(t, (fpreal32)1e-8)) return (fpreal32).5;
875  t = (val - min) / t;
876  return t*t*((fpreal32)3.0 - (fpreal32)2.0*t);
877 }
878 
879 static constexpr inline fpreal64
880 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 val)
881 {
882  if (val <= min) return 0;
883  if (val >= max) return 1;
884  fpreal64 t = max - min;
885  if (SYSequalZero(t, (fpreal64)1e-18)) return (fpreal64).5;
886  t = (val - min) / t;
887  return t*t*((fpreal64)3.0 - (fpreal64)2.0*t);
888 }
889 
890 static constexpr inline fpreal32
891 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 value, fpreal32 roll)
892 {
893  if (roll > 0)
894  {
895  fpreal32 f = SYSsmooth(min, max, value);
896  return roll < (fpreal32)1 ? (fpreal32)1-SYSpow((fpreal32)1-f,
897  (fpreal32)1/roll) : SYSpow(f, roll);
898  }
899  return (fpreal32)1;
900 }
901 
902 static constexpr inline fpreal64
903 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 value, fpreal64 roll)
904 {
905  if (roll > 0)
906  {
907  fpreal64 f = SYSsmooth(min, max, value);
908  return roll < (fpreal64)1 ? (fpreal64)1-SYSpow((fpreal64)1-f,
909  (fpreal64)1/roll) : SYSpow(f, roll);
910  }
911  return (fpreal64)1;
912 }
913 
914 static constexpr inline fpreal32
915 SYSfit(fpreal32 val, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
916 {
917  fpreal32 d = omax - omin;
918  if (SYSequalZero(d, (fpreal32)1e-8))
919  return (nmin+nmax)*(fpreal32).5;
920  if (omin < omax)
921  {
922  if (val < omin) return nmin;
923  if (val > omax) return nmax;
924  }
925  else
926  {
927  if (val < omax) return nmax;
928  if (val > omin) return nmin;
929  }
930  return nmin + (nmax-nmin)*(val-omin)/d;
931 }
932 
933 static constexpr inline fpreal64
934 SYSfit(fpreal64 val, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
935 {
936  fpreal64 d = omax - omin;
937  if (SYSequalZero(d, (fpreal64)1e-18))
938  return (nmin+nmax)*(fpreal64).5;
939  if (omin < omax)
940  {
941  if (val < omin) return nmin;
942  if (val > omax) return nmax;
943  }
944  else
945  {
946  if (val < omax) return nmax;
947  if (val > omin) return nmin;
948  }
949  return nmin + (nmax-nmin)*(val-omin)/d;
950 }
951 
952 /// SYSefit() will not clamp the values to the range
953 static constexpr inline fpreal32
954 SYSefit(fpreal32 v, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
955 {
956  fpreal32 d = omax - omin;
957  if (SYSequalZero(d, (fpreal32)1e-8))
958  return (nmin+nmax)*0.5f;
959  return nmin + (nmax-nmin)*(v-omin)/d;
960 }
961 
962 static constexpr inline fpreal64
963 SYSefit(fpreal64 v, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
964 {
965  fpreal64 d = omax - omin;
966  if (SYSequalZero(d, (fpreal64)1e-18))
967  return (nmin+nmax)*0.5f;
968  return nmin + (nmax-nmin)*(v-omin)/d;
969 }
970 
971 static constexpr inline fpreal32
972 SYSfit01(fpreal32 val, fpreal32 nmin, fpreal32 nmax)
973 {
974  if (val < 0) return nmin;
975  if (val > 1) return nmax;
976  return SYSlerp(nmin, nmax, val);
977 }
978 
979 static constexpr inline fpreal64
980 SYSfit01(fpreal64 val, fpreal64 nmin, fpreal64 nmax)
981 {
982  if (val < 0) return nmin;
983  if (val > 1) return nmax;
984  return SYSlerp(nmin, nmax, val);
985 }
986 
987 /// SYSinvlerp() will produce the bias needed for lerp, but avoid
988 /// zeros. Essentially built on efit.
989 static constexpr inline fpreal32
990 SYSinvlerp(fpreal32 v, fpreal32 omin, fpreal32 omax)
991 {
992  return SYSefit(v, omin, omax, 0.0f, 1.0f);
993 }
994 
995 static constexpr inline fpreal64
996 SYSinvlerp(fpreal64 v, fpreal64 omin, fpreal64 omax)
997 {
998  return SYSefit(v, omin, omax, 0.0, 1.0);
999 }
1000 
1001 // Linear hat function over kernel width dx.
1002 static inline fpreal32
1003 SYShat(fpreal32 x, fpreal32 dx)
1004 {
1005  const fpreal32 ax = SYSabs(x);
1006  return (ax > dx ? 0 : 1 - ax / dx);
1007 }
1008 
1009 static inline fpreal64
1010 SYShat(fpreal64 x, fpreal64 dx)
1011 {
1012  const fpreal64 ax = SYSabs(x);
1013  return (ax > dx ? 0 : 1 - ax / dx);
1014 }
1015 
1016 // Derivative of the linear hat function over kernel width dx.
1017 static constexpr inline fpreal32
1018 SYSdhat(fpreal32 x, fpreal32 dx)
1019 {
1020  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1021 }
1022 
1023 static constexpr inline fpreal64
1024 SYSdhat(fpreal64 x, fpreal64 dx)
1025 {
1026  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1027 }
1028 
1029 
1030 // For integer types.
1031 template<typename T>
1032 constexpr inline T
1033 SYSroundDownToMultipleOf(T val, T multiple)
1034 {
1035  // Only handle multiples of 2 and up.
1036  if (multiple <= 1)
1037  return val;
1038 
1039  int rem = val % multiple;
1040  if (rem == 0)
1041  return val;
1042 
1043  if (val < 0)
1044  return val - multiple - rem;
1045  else
1046  return val - rem;
1047 }
1048 
1049 template<>
1050 inline fpreal32
1051 SYSroundDownToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1052 {
1053  if (SYSequalZero(multiple))
1054  return val;
1055  fpreal32 modulus = SYSfmod(val, multiple);
1056  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1057  return val;
1058  fpreal32 retval = val - modulus;
1059  if( val < (fpreal32)0 && modulus!=(fpreal32)0 )
1060  retval -= multiple;
1061  return retval;
1062 }
1063 
1064 template<>
1065 inline fpreal64
1066 SYSroundDownToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1067 {
1068  if (SYSequalZero(multiple))
1069  return val;
1070  fpreal64 modulus = SYSfmod(val, multiple);
1071  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1072  return val;
1073  fpreal64 retval = val - modulus;
1074  if( val < (fpreal64)0 && modulus!=(fpreal64)0 )
1075  retval -= multiple;
1076  return retval;
1077 }
1078 
1079 template<typename T>
1080 constexpr inline T
1081 SYSroundUpToMultipleOf(T val, T multiple)
1082 {
1083  // Only handle multiples of 2 and up.
1084  if (multiple <= 1)
1085  return val;
1086 
1087  int rem = val % multiple;
1088  if (rem == 0)
1089  return val;
1090 
1091  if (val < 0)
1092  return val - rem;
1093  else
1094  return val + multiple - rem;
1095 }
1096 
1097 template<>
1098 inline fpreal32
1099 SYSroundUpToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1100 {
1101  fpreal32 modulus;
1102  if (SYSequalZero(multiple))
1103  return val;
1104  modulus = SYSfmod(val, multiple);
1105  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1106  return val;
1107  if (val > (fpreal32)0)
1108  val += multiple;
1109  return val - modulus;
1110 }
1111 
1112 template<>
1113 inline fpreal64
1114 SYSroundUpToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1115 {
1116  fpreal64 modulus;
1117  if (SYSequalZero(multiple))
1118  return val;
1119  modulus = SYSfmod(val, multiple);
1120  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1121  return val;
1122  if (val > (fpreal64)0)
1123  val += multiple;
1124  return val - modulus;
1125 }
1126 
1127 inline constexpr uint32
1128 SYSroundUpPow2(uint32 val)
1129 {
1130  // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1131  uint32 v = val;
1132 
1133  v--;
1134  v |= v >> 1;
1135  v |= v >> 2;
1136  v |= v >> 4;
1137  v |= v >> 8;
1138  v |= v >> 16;
1139  v++;
1140 
1141  return v;
1142 }
1143 
1144 static constexpr inline uint32
1145 SYSwang_inthash(uint32 key)
1146 {
1147  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1148  key += ~(key << 16);
1149  key ^= (key >> 5);
1150  key += (key << 3);
1151  key ^= (key >> 13);
1152  key += ~(key << 9);
1153  key ^= (key >> 17);
1154  return key;
1155 }
1156 
1157 
1158 static constexpr inline uint32
1159 SYSmultiplicative_inthash(uint32 key)
1160 {
1161  // Multiply by the golden mean of 2^32 (Knuth's multiplicative method) to
1162  // get a uniformly distributed hash value. Do a google search for
1163  // 2654435761 for more information.
1164  return key * 2654435761u;
1165 }
1166 
1167 static constexpr inline uint64
1168 SYSmultiplicative_inthash64(uint64 key)
1169 {
1170  // Same as the 32-bit version above, but using the golden mean of 2^64.
1171  return key * 11400714819323198485llu;
1172 }
1173 
1174 static constexpr inline uint64
1175 SYSwang_inthash64(uint64 key)
1176 {
1177  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1178  key += ~(key << 32ULL);
1179  key ^= (key >> 22);
1180  key += ~(key << 13);
1181  key ^= (key >> 8);
1182  key += (key << 3);
1183  key ^= (key >> 15);
1184  key += ~(key << 27);
1185  key ^= (key >> 31);
1186  return key;
1187 }
1188 
1189 static constexpr inline uint32
1190 SYSsharpe_inthash(uint32 key)
1191 {
1192  // Based on https://github.com/skeeto/hash-prospector, but with
1193  // modifications such that:
1194  // - bias value of 0.1406800449
1195  // - minimum cycle of 17 (the original algorithm had f(0) == 0)
1196  // - improved dieharder results
1197  key ^= (key >> 16);
1198  key *= 0xdbfb352dU;
1199  key ^= ~(key >> 15);
1200  key *= 0x6c2ca68bU;
1201  key ^= (key >> 16);
1202  return key;
1203 }
1204 
1205 static constexpr inline uint32
1206 SYSwang2_inthash(uint32 key)
1207 {
1208  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1209  // Updated integer hashing (2007)
1210  constexpr uint c2=0x27d4eb2d; // a prime or an odd constant
1211  key = (key ^ 61) ^ (key >> 16);
1212  key = key + (key << 3);
1213  key = key ^ (key >> 4);
1214  key = key * c2;
1215  key = key ^ (key >> 15);
1216  return key;
1217 }
1218 
1219 static constexpr inline uint64
1220 SYSwang2_inthash(uint64 key)
1221 {
1222  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1223  // Updated integer hashing (2007)
1224  key = (~key) + (key << 21); // key = (key<<21) - key - 1;
1225  key = key ^ (key >> 24);
1226  key = (key + (key << 3)) + (key << 8); // key * 265
1227  key = key ^ (key >> 14);
1228  key = (key + (key << 2)) + (key << 4); // key * 21
1229  key = key ^ (key >> 28);
1230  key = key + (key << 31);
1231  return key;
1232 }
1233 
1234 static inline uint
1235 SYSreal_hash(fpreal16 a, int lowmask=0x3)
1236 {
1237  return SYSwang_inthash(a.bits() & (~lowmask));
1238 }
1239 
1240 static inline uint
1241 SYSreal_hashseed(fpreal16 a, uint seed, int lowmask=0x3)
1242 {
1243  return SYSwang_inthash(seed + (a.bits() & (~lowmask)));
1244 }
1245 
1246 static inline uint
1247 SYSreal_hash(fpreal32 a, int lowmask=0xf)
1248 {
1249  SYS_FPRealUnionF ai;
1250  ai.fval = a;
1251  return SYSwang_inthash(ai.uval & (~lowmask));
1252 }
1253 
1254 static inline uint
1255 SYSreal_hashseed(fpreal32 a, uint seed, int lowmask=0xf)
1256 {
1257  SYS_FPRealUnionF ai;
1258  ai.fval = a;
1259  return SYSwang_inthash(seed + (ai.uval & (~lowmask)));
1260 }
1261 
1262 static inline uint
1263 SYSreal_hash(fpreal64 a, int lowmask=0xf)
1264 {
1265  SYS_FPRealUnionD ai;
1266  ai.fval = a;
1267  return SYSwang_inthash64(ai.uval & (~lowmask));
1268 }
1269 
1270 static inline uint
1271 SYSreal_hashseed(fpreal64 a, uint seed, int64 lowmask=0xf)
1272 {
1273  SYS_FPRealUnionD ai;
1274  ai.fval = a;
1275  return SYSwang_inthash64(seed + (ai.uval & (~lowmask)));
1276 }
1277 
1278 static constexpr inline uint
1279 SYSvector_hash(const int32 *vector, int size)
1280 {
1281  uint hash = 0;
1282  for (int i = 0; i < size; ++i)
1283  hash = SYSwang_inthash(hash + vector[i]);
1284  return hash;
1285 }
1286 
1287 static constexpr inline uint
1288 SYSvector_hash(const int64 *vector, int size)
1289 {
1290  uint hash = 0;
1291  for (int i = 0; i < size; ++i)
1292  hash = (uint)SYSwang_inthash64(hash + vector[i]);
1293  return hash;
1294 }
1295 
1296 static inline uint
1297 SYSvector_hash(const fpreal16 *vector, int size)
1298 {
1299  uint hash = 0;
1300  for (int i = 0; i < size; ++i)
1301  hash = SYSreal_hashseed(vector[i], hash);
1302  return hash;
1303 }
1304 
1305 static inline uint
1306 SYSvector_hash(const fpreal32 *vector, int size)
1307 {
1308  uint hash = 0;
1309  for (int i = 0; i < size; ++i)
1310  hash = SYSreal_hashseed(vector[i], hash);
1311  return hash;
1312 }
1313 
1314 static inline uint
1315 SYSvector_hash(const fpreal64 *vector, int size)
1316 {
1317  uint hash = 0;
1318  for (int i = 0; i < size; ++i)
1319  hash = SYSreal_hashseed(vector[i], hash);
1320  return hash;
1321 }
1322 
1323 // Spatial hashing function for 3-vectors
1324 // Reference:
1325 // "Optimized Spatial Hashing for Collision Detection of Deformable Objects"
1326 //
1327 template<class P>
1328 static inline size_t
1329 SYSvector3_hash(const P &vector)
1330 {
1331  static constexpr size_t p1 = 73856093;
1332  static constexpr size_t p2 = 19349663;
1333  static constexpr size_t p3 = 83492791;
1334 
1335  return size_t(vector.x()*p1) ^ size_t(vector.y()*p2) ^ size_t(vector.z()*p3);
1336 }
1337 
1338 static inline uint
1339 SYSpointerHash(const void *ptr)
1340 {
1341 #if SIZEOF_VOID_P == 8
1342  // 64 bit pointers
1343  return (uint)((int64)ptr & 0xffffffff);
1344 #else
1345  // 32 bit pointers
1346  return (uint)ptr;
1347 #endif
1348 }
1349 
1350 // Convert a uniform random bit pattern to a random float in the range [0, 1)
1351 inline static fpreal32
1352 SYShashToFloat01(uint hash)
1353 {
1354  SYS_FPRealUnionF tmp;
1355  tmp.uval = 0x3f800000 | (0x007fffff & hash);
1356  return tmp.fval-1.0F;
1357 }
1358 
1359 // Generate a random number in range [0, 1)
1360 static inline fpreal32
1361 SYSfastRandom(uint &seed)
1362 {
1363  seed = seed*1664525 + 1013904223;
1364  return SYShashToFloat01(seed);
1365 }
1366 
1367 inline static fpreal32
1368 SYSrandom(uint &seed)
1369 {
1370  seed = seed*1664525 + 1013904223;
1371  return SYShashToFloat01(SYSwang_inthash(seed));
1372 }
1373 
1374 static constexpr uint
1375 SYSfastRandomInt(uint &seed)
1376 {
1377  seed = seed*1664525 + 1013904223;
1378  return SYSwang_inthash(seed);
1379 }
1380 
1381 inline static fpreal32
1382 SYSfastRandomZero(uint &seed)
1383 {
1384  return SYSfastRandom(seed) - 0.5F;
1385 }
1386 
1387 inline static fpreal32
1388 SYSrandomZero(uint &seed)
1389 {
1390  return SYSrandom(seed) - 0.5F;
1391 }
1392 
1393 template <typename T>
1394 static constexpr inline void
1395 SYSminmax(T v0, T v1, T v2, T v3, T &min, T &max)
1396 {
1397  min = SYSmin(v0, v1, v2, v3);
1398  max = SYSmax(v0, v1, v2, v3);
1399 }
1400 
1401 template <typename T>
1402 static constexpr inline void
1403 SYSminmax(T v0, T v1, T v2, T &min, T &max)
1404 {
1405  min = SYSmin(v0, v1, v2);
1406  max = SYSmax(v0, v1, v2);
1407 }
1408 
1409 template <typename T>
1410 static constexpr inline void
1411 SYSminmax(T v0, T v1, T &min, T &max)
1412 {
1413  min = SYSmin(v0, v1);
1414  max = SYSmax(v0, v1);
1415 }
1416 
1417 inline static void
1418 SYSgetSinCosFromSlope(fpreal32 slope, fpreal32 &sintheta, fpreal32 &costheta)
1419 {
1420  fpreal32 one_over_m;
1421  sintheta = slope / SYSsqrt(slope*slope + (fpreal32)1);
1422  if ((slope = SYSabs(slope)) > (fpreal32)1)
1423  {
1424  one_over_m = (fpreal32)1 / slope;
1425  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1426  }
1427  else
1428  costheta = SYSsqrt((fpreal32)1 - sintheta*sintheta);
1429 }
1430 
1431 inline static void
1432 SYSgetSinCosFromSlope(fpreal64 slope, fpreal64 &sintheta, fpreal64 &costheta)
1433 {
1434  fpreal64 one_over_m;
1435  sintheta = slope / SYSsqrt(slope*slope + (fpreal64)1);
1436  if ((slope = SYSabs(slope)) > (fpreal64)1)
1437  {
1438  one_over_m = (fpreal64)1 / slope;
1439  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1440  }
1441  else
1442  costheta = SYSsqrt((fpreal64)1 - sintheta*sintheta);
1443 }
1444 
1445 inline constexpr static bool
1446 SYSsameSign( fpreal32 v0, fpreal32 v1 )
1447 {
1448  return (v0*v1)>0;
1449 }
1450 
1451 inline constexpr static bool
1452 SYSsameSign( fpreal64 v0, fpreal64 v1 )
1453 {
1454  return (v0*v1)>0;
1455 }
1456 
1457 inline constexpr static bool
1458 SYSsameSign( int32 v0, int32 v1 )
1459 {
1460  return (v0 ^ v1) >= 0;
1461 }
1462 
1463 inline constexpr static bool
1464 SYSsameSign( int64 v0, int64 v1 )
1465 {
1466  return (v0 ^ v1) >= 0;
1467 }
1468 
1469 static inline uint
1470 SYSnextPrime(uint num)
1471 {
1472  return SYSmakePrime(num+1);
1473 }
1474 
1475 static constexpr inline int
1476 SYShexCharToInt(char c)
1477 {
1478  // Given a hexidecimal character, return it's corresponding value between
1479  // 0 and 15, or -1 if it's not a hexidecimal character.
1480  if (c >= '0' && c <= '9')
1481  return c - '0';
1482  if (c >= 'a' && c <= 'f')
1483  return c - 'a' + 10;
1484  if (c >= 'A' && c <= 'F')
1485  return c - 'A' + 10;
1486  return -1;
1487 }
1488 
1489 static constexpr inline char
1490 SYSintToHexChar(int value)
1491 {
1492  // The value must be from 0-15 for this function to return a valid result.
1493  return value < 10 ? '0' + value : 'a' + value - 10;
1494 }
1495 
1496 SYS_API void SYSsort(int &a, int &b);
1497 SYS_API void SYSsort(int &a, int &b, int &c);
1498 SYS_API void SYSsort(int64 &a, int64 &b);
1499 SYS_API void SYSsort(int64 &a, int64 &b, int64 &c);
1500 SYS_API void SYSsort(float &a, float &b);
1501 SYS_API void SYSsort(float &a, float &b, float &c);
1502 SYS_API void SYSsort(double &a, double &b);
1503 SYS_API void SYSsort(double &a, double &b, double &c);
1504 
1505 // Compute both integer division and integer modulus
1506 // They are compiled into a single instruction
1507 static constexpr inline void
1508 SYSdivMod(int numerator, int denominator, int &quotient, int &remainder)
1509 {
1510  quotient = numerator / denominator;
1511  remainder = numerator % denominator;
1512 }
1513 
1514 static constexpr inline void
1515 SYSdivMod(int64 numerator, int64 denominator, int64 &quotient, int64 &remainder)
1516 {
1517  quotient = numerator / denominator;
1518  remainder = numerator % denominator;
1519 }
1520 
1521 // Include permutations of fpreal32/fpreal64
1522 #include "SYS_MathPermute.h"
1523 #include "SYS_MathRestrictive.h"
1524 
1525 #else /* cplusplus */
1526 #define SYSmax(a,b) ((a) > (b) ? (a) : (b))
1527 #define SYSmin(a,b) ((a) < (b) ? (a) : (b))
1528 #define SYSabs(a) ((a) < 0 ? (a) : -(a))
1529 #endif
1530 
1531 #endif
GLdouble s
Definition: glew.h:1390
SYS_API float acosf(float x)
#define SYSmax(a, b)
Definition: SYS_Math.h:1526
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:1528
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()
SYS_API fpreal32 SYSfloor(fpreal32 val)
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:1527
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
SYS_API fpreal32 SYSceil(fpreal32 val)