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) noexcept
171 {
172  return int(F(0) < a) - int(a < F(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 static constexpr inline fpreal32 SYSpow2(fpreal32 a) { return a*a; }
539 static constexpr inline fpreal64 SYSpow2(fpreal64 a) { return a*a; }
540 static constexpr inline fpreal32 SYSpow3(fpreal32 a) { return a*a*a; }
541 static constexpr inline fpreal64 SYSpow3(fpreal64 a) { return a*a*a; }
542 static constexpr inline fpreal32 SYSpow4(fpreal32 a) { return SYSpow2(SYSpow2(a)); }
543 static constexpr inline fpreal64 SYSpow4(fpreal64 a) { return SYSpow2(SYSpow2(a)); }
544 static constexpr inline fpreal32 SYSpow5(fpreal32 a) { return SYSpow4(a) * a; }
545 static constexpr inline fpreal64 SYSpow5(fpreal64 a) { return SYSpow4(a) * a; }
546 
547 #define h_compare(func, code) \
548  static inline bool func(fpreal32 a, fpreal32 b, \
549  fpreal32 tol=SYS_FTOLERANCE) \
550  { return code; } \
551  static inline bool func(fpreal64 a, fpreal64 b, \
552  fpreal64 tol=SYS_FTOLERANCE_D) \
553  { return code; }
554 #define h_compare_ce(func, code) \
555  static constexpr inline bool func(fpreal32 a, fpreal32 b, \
556  fpreal32 tol=SYS_FTOLERANCE) \
557  { return code; } \
558  static constexpr inline bool func(fpreal64 a, fpreal64 b, \
559  fpreal64 tol=SYS_FTOLERANCE_D) \
560  { return code; }
561 
562  static constexpr inline bool
564  { return a >= -tol && a <= tol; }
565 
566  static constexpr inline bool
568  { return a >= -tol && a <= tol; }
569 
570  static constexpr inline bool
572  { return a >= -tol && a <= tol; }
573 
574  static constexpr inline bool
576  { return a >= -tol && a <= tol; }
577 
578  h_compare(SYSisEqual, SYSabs(a-b)<=tol)
579  h_compare_ce(SYSisGreater, (a-b) > tol)
580  h_compare_ce(SYSisGreaterOrEqual, (a-b) >= -tol)
581  h_compare_ce(SYSisLess, (a-b) < -tol)
582  h_compare_ce(SYSisLessOrEqual, (a-b) <= tol)
583 #undef h_compare
584 #undef h_compare_ce
585 
586 constexpr inline bool SYSisEqual(int32 a, int32 b) { return a == b; }
587 constexpr inline bool SYSisEqual(int64 a, int64 b) { return a == b; }
588 constexpr inline bool SYSisGreater(int32 a, int32 b) { return a > b; }
589 constexpr inline bool SYSisGreater(int64 a, int64 b) { return a > b; }
590 constexpr inline bool SYSisGreaterOrEqual(int32 a, int32 b) { return a >= b; }
591 constexpr inline bool SYSisGreaterOrEqual(int64 a, int64 b) { return a >= b; }
592 constexpr inline bool SYSisLess(int32 a, int32 b) { return a < b; }
593 constexpr inline bool SYSisLess(int64 a, int64 b) { return a < b; }
594 constexpr inline bool SYSisLessOrEqual(int32 a, int32 b) { return a <= b; }
595 constexpr inline bool SYSisLessOrEqual(int64 a, int64 b) { return a <= b; }
596 
597 static inline bool
598 SYSalmostEqual(fpreal32 a, fpreal32 b, int ulps=50, fpreal32 tol = 1e-6)
599 {
600  SYS_FPRealUnionF ai, bi;
601  ai.fval = a;
602  bi.fval = b;
603 
604  // check if they are absolutely close together (only possible near zero)
605  if (SYSfabs(a - b) <= tol)
606  return true;
607 
608  // we keep the following check in to allow points that are within 1e-15 of
609  // zero to be "equal" for backwards compatibility (so 6e-16 and -6e-16 are
610  // "equal" even though they may not be within 1e-15, for example):
611  // If both double-precision floats are very close to zero, we consider them
612  // equal.
613  if((SYSabs(a) < 1e-6) && (SYSabs(b) < 1e-6))
614  return true;
615 
616  // if they have different signs they are not equal (since they must be at
617  // least tol distance apart by this point, there is no trickiness with +-0)
618  if ((ai.fval < 0.0) != (bi.fval < 0.0))
619  return false;
620 
621  // find the difference in ulps
622  // We're very careful to avoid signed integer overflow here.
623  if ((ai.uval > bi.uval ? ai.uval - bi.uval : bi.uval - ai.uval) <= ulps)
624  return true;
625 
626  return false;
627 }
628 
629 // adapted from AlmostEqualUlpsAndAbs from:
630 // https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
631 static inline bool
632 SYSalmostEqual(fpreal64 a, fpreal64 b, int64 ulps, fpreal64 tol = 1e-15)
633 {
634  SYS_FPRealUnionD ai, bi;
635  ai.fval = a;
636  bi.fval = b;
637 
638  // check if they are absolutely close together (only possible near zero)
639  if (SYSfabs(a - b) <= tol)
640  return true;
641 
642  // we keep the following check in to allow points that are within 1e-15 of
643  // zero to be "equal" for backwards compatibility (so 6e-16 and -6e-16 are
644  // "equal" even though they may not be within 1e-15, for example):
645  // If both double-precision floats are very close to zero, we consider them
646  // equal.
647  if((SYSabs(a) < 1e-15) && (SYSabs(b) < 1e-15))
648  return true;
649 
650  // if they have different signs they are not equal (since they must be at
651  // least tol distance apart by this point, there is no trickiness with +-0)
652  if ((ai.fval < 0.0) != (bi.fval < 0.0))
653  return false;
654 
655  // find the difference in ulps
656  // We're very careful to avoid signed integer overflow here.
657  if ((ai.uval > bi.uval ? ai.uval - bi.uval : bi.uval - ai.uval) <= ulps)
658  return true;
659 
660  return false;
661 }
662 
663 static inline bool
664 SYSalmostEqual(fpreal64 a, fpreal64 b, int32 ulps=50, fpreal64 tol = 1e-15)
665 {
666  return SYSalmostEqual(a, b, (int64)ulps, tol);
667 }
668 
669 static inline bool SYSisInteger(fpreal32 val)
670  { return SYSisEqual(val, SYSfloor(val)) || SYSisEqual(val, SYSceil(val)); }
671 static inline bool SYSisInteger(fpreal64 val)
672  { return SYSisEqual(val, SYSfloor(val)) || SYSisEqual(val, SYSceil(val)); }
673 
674 #define h_max3(type) \
675  static constexpr inline type \
676  SYSmax(type v0, type v1, type v2) { \
677  return SYSmax(v2, SYSmax(v0, v1)); \
678  }
679 #define h_max4(type) \
680  static constexpr inline type \
681  SYSmax(type v0, type v1, type v2, type v3) { \
682  return SYSmax(SYSmax(v0, v1), SYSmax(v2, v3)); \
683  }
684 #define h_argmax3(type) \
685  static constexpr inline int \
686  SYSargmax(type v0, type v1, type v2) { \
687  return v2 > SYSmax(v0, v1) ? 2 : SYSargmax(v0, v1); \
688  }
689 #define h_argmax4(type) \
690  static constexpr inline int \
691  SYSargmax(type v0, type v1, type v2, type v3) { \
692  return SYSmax(v0, v1) < SYSmax(v2, v3) ? \
693  (SYSargmax(v2, v3) + 2) : SYSargmax(v0, v1); \
694  }
695 #define h_min3(type) \
696  static constexpr inline type \
697  SYSmin(type v0, type v1, type v2) { \
698  return SYSmin(v2, SYSmin(v0, v1)); \
699  }
700 #define h_min4(type) \
701  static constexpr inline type \
702  SYSmin(type v0, type v1, type v2, type v3) { \
703  return SYSmin(SYSmin(v0, v1), SYSmin(v2, v3)); \
704  }
705 #define h_argmin3(type) \
706  static constexpr inline int \
707  SYSargmin(type v0, type v1, type v2) { \
708  return v2 < SYSmin(v0, v1) ? 2 : SYSargmin(v0, v1); \
709  }
710 #define h_argmin4(type) \
711  static constexpr inline int \
712  SYSargmin(type v0, type v1, type v2, type v3) { \
713  return SYSmin(v0, v1) > SYSmin(v2, v3) ? \
714  (SYSargmin(v2, v3) + 2) : SYSargmin(v0, v1); \
715  }
716 
717 
718 #define h_max(type) h_min3(type) h_min4(type) h_max3(type) h_max4(type) \
719  h_argmin3(type) h_argmin4(type) \
720  h_argmax3(type) h_argmax4(type)
721 
722 #define h_avg3(type) \
723  static constexpr inline type \
724  SYSavg(type v0, type v1, type v2) { \
725  return (v0+v1+v2) * ((type)(1.0/3.0)); \
726  }
727 #define h_avg4(type) \
728  static constexpr inline type \
729  SYSavg(type v0, type v1, type v2, type v3) { \
730  return (v0+v1+v2+v3) * ((type)0.25); \
731  }
732 
733 #define h_avg(type) h_avg3(type) h_avg4(type)
734 
735 h_max(int8)
736 h_max(uint8)
737 h_max(int16)
738 h_max(uint16)
739 h_max(int32)
740 h_max(uint32)
741 h_max(int64)
742 h_max(uint64)
743 h_max(fpreal32) h_avg(fpreal32)
744 h_max(fpreal64) h_avg(fpreal64)
745 
746 static constexpr inline int32
747 SYSavg(int32 a, int32 b, int32 c)
748 { return (a + b + c + 1) / 3; }
749 static constexpr inline int32
750 SYSavg(int32 a, int32 b, int32 c, int32 d)
751 { return (a + b + c + d + 2) / 4; }
752 
753 static constexpr inline int64
754 SYSavg(int64 a, int64 b, int64 c)
755 { return (a + b + c + 1) / 3; }
756 static constexpr inline int64
757 SYSavg(int64 a, int64 b, int64 c, int64 d)
758 { return (a + b + c + d + 2) / 4; }
759 
760 // Some systems have size_t as a seperate type from uint. Some don't.
761 #if defined(MBSD)
762 h_max(size_t)
763 #endif
764 
765 #undef h_min3
766 #undef h_min4
767 #undef h_max3
768 #undef h_max4
769 #undef h_argmin3
770 #undef h_argmin4
771 #undef h_argmax3
772 #undef h_argmax4
773 #undef h_avg3
774 #undef h_avg4
775 #undef h_max
776 #undef h_avg
777 
778 /// @{
779 /// Linear interpolation between v0 and v1.
780 static constexpr inline fpreal32
782 {
783  return v1 + (v2 - v1)*t;
784 }
785 
786 static constexpr inline fpreal64
788 {
789  return v1 + (v2 - v1)*t;
790 }
791 /// @}
792 
793 /// @{
794 /// Bilinear interpolation over a quadrilateral: @code
795 /// (u=0,v=1) u0v1 u1v1 (u=1,v=1)
796 /// +--------+
797 /// | |
798 /// | |
799 /// +--------+
800 /// (u=0,v=0) u0v0 u1v0 (u=1,v=0)
801 /// @endcode
802 static constexpr inline fpreal32
803 SYSbilerp(fpreal32 u0v0, fpreal32 u1v0, fpreal32 u0v1, fpreal32 u1v1,
804  fpreal32 u, fpreal32 v)
805 {
806  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
807 }
808 
809 static constexpr inline fpreal64
810 SYSbilerp(fpreal64 u0v0, fpreal64 u1v0, fpreal64 u0v1, fpreal64 u1v1,
811  fpreal64 u, fpreal64 v)
812 {
813  return SYSlerp(SYSlerp(u0v0, u0v1, v), SYSlerp(u1v0, u1v1, v), u);
814 }
815 /// @}
816 
817 /// @{
818 /// Barycentric coordinates do interpolation over the triangle specified by the
819 /// three vertices (v0, v1, v2): @code
820 /// | (u=0,v=1) |
821 /// | v2 |
822 /// | / \ |
823 /// | / \ |
824 /// | / \ |
825 /// | (u=0,v=0) v0------v1 (u=1,v=0) |
826 /// @endcode
827 static constexpr inline fpreal32
829 {
830  return v0*(1-u-v) + v1*u + v2*v;
831 }
832 
833 static constexpr inline fpreal64
835 {
836  return v0*(1-u-v) + v1*u + v2*v;
837 }
838 /// @}
839 
840 /// @{
841 /// Wrap mod is like mod, but returns [0..b) interval thereby
842 /// usually giving a more useful result.
843 /// This is always defined as safe!
844 /// @}
845 static constexpr inline fpreal32
846 SYSwrapmod(fpreal32 a, fpreal32 b)
847 {
848  fpreal32 r = SYSsafefmod(a, b);
849  return ((a < 0) ^ (b < 0) && r != 0.0f) ? r+b : r;
850 }
851 
852 static constexpr inline fpreal64
853 SYSwrapmod(fpreal64 a, fpreal64 b)
854 {
855  fpreal64 r = SYSsafefmod(a, b);
856  return ((a < 0) ^ (b < 0) && r != 0.0) ? r+b : r;
857 }
858 
859 static constexpr inline int32
860 SYSwrapmod(int32 a, int32 b)
861 {
862  if (b == 0)
863  return 0;
864  int32 r = a % b;
865  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
866 }
867 
868 static constexpr inline int64
869 SYSwrapmod(int64 a, int64 b)
870 {
871  if (b == 0)
872  return 0;
873  int64 r = a % b;
874  return ((a < 0) ^ (b < 0) && r) ? r+b : r;
875 }
876 
877 static constexpr inline fpreal32
878 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 val)
879 {
880  if (val <= min) return 0;
881  if (val >= max) return 1;
882  fpreal32 t = max - min;
883  if (SYSequalZero(t, (fpreal32)1e-8)) return (fpreal32).5;
884  t = (val - min) / t;
885  return t*t*((fpreal32)3.0 - (fpreal32)2.0*t);
886 }
887 
888 static constexpr inline fpreal64
889 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 val)
890 {
891  if (val <= min) return 0;
892  if (val >= max) return 1;
893  fpreal64 t = max - min;
894  if (SYSequalZero(t, (fpreal64)1e-18)) return (fpreal64).5;
895  t = (val - min) / t;
896  return t*t*((fpreal64)3.0 - (fpreal64)2.0*t);
897 }
898 
899 static constexpr inline fpreal32
900 SYSsmooth(fpreal32 min, fpreal32 max, fpreal32 value, fpreal32 roll)
901 {
902  if (roll > 0)
903  {
904  fpreal32 f = SYSsmooth(min, max, value);
905  return roll < (fpreal32)1 ? (fpreal32)1-SYSpow((fpreal32)1-f,
906  (fpreal32)1/roll) : SYSpow(f, roll);
907  }
908  return (fpreal32)1;
909 }
910 
911 static constexpr inline fpreal64
912 SYSsmooth(fpreal64 min, fpreal64 max, fpreal64 value, fpreal64 roll)
913 {
914  if (roll > 0)
915  {
916  fpreal64 f = SYSsmooth(min, max, value);
917  return roll < (fpreal64)1 ? (fpreal64)1-SYSpow((fpreal64)1-f,
918  (fpreal64)1/roll) : SYSpow(f, roll);
919  }
920  return (fpreal64)1;
921 }
922 
923 static constexpr inline fpreal32
924 SYSfit(fpreal32 val, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
925 {
926  fpreal32 d = omax - omin;
927  if (SYSequalZero(d, (fpreal32)1e-8))
928  return (nmin+nmax)*(fpreal32).5;
929  if (omin < omax)
930  {
931  if (val < omin) return nmin;
932  if (val > omax) return nmax;
933  }
934  else
935  {
936  if (val < omax) return nmax;
937  if (val > omin) return nmin;
938  }
939  return nmin + (nmax-nmin)*(val-omin)/d;
940 }
941 
942 static constexpr inline fpreal64
943 SYSfit(fpreal64 val, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
944 {
945  fpreal64 d = omax - omin;
946  if (SYSequalZero(d, (fpreal64)1e-18))
947  return (nmin+nmax)*(fpreal64).5;
948  if (omin < omax)
949  {
950  if (val < omin) return nmin;
951  if (val > omax) return nmax;
952  }
953  else
954  {
955  if (val < omax) return nmax;
956  if (val > omin) return nmin;
957  }
958  return nmin + (nmax-nmin)*(val-omin)/d;
959 }
960 
961 /// SYSefit() will not clamp the values to the range
962 static constexpr inline fpreal32
963 SYSefit(fpreal32 v, fpreal32 omin, fpreal32 omax, fpreal32 nmin, fpreal32 nmax)
964 {
965  fpreal32 d = omax - omin;
966  if (SYSequalZero(d, (fpreal32)1e-8))
967  return (nmin+nmax)*0.5f;
968  return nmin + (nmax-nmin)*(v-omin)/d;
969 }
970 
971 static constexpr inline fpreal64
972 SYSefit(fpreal64 v, fpreal64 omin, fpreal64 omax, fpreal64 nmin, fpreal64 nmax)
973 {
974  fpreal64 d = omax - omin;
975  if (SYSequalZero(d, (fpreal64)1e-18))
976  return (nmin+nmax)*0.5f;
977  return nmin + (nmax-nmin)*(v-omin)/d;
978 }
979 
980 static constexpr inline fpreal32
981 SYSfit01(fpreal32 val, fpreal32 nmin, fpreal32 nmax)
982 {
983  if (val < 0) return nmin;
984  if (val > 1) return nmax;
985  return SYSlerp(nmin, nmax, val);
986 }
987 
988 static constexpr inline fpreal64
989 SYSfit01(fpreal64 val, fpreal64 nmin, fpreal64 nmax)
990 {
991  if (val < 0) return nmin;
992  if (val > 1) return nmax;
993  return SYSlerp(nmin, nmax, val);
994 }
995 
996 /// SYSinvlerp() will produce the bias needed for lerp, but avoid
997 /// zeros. Essentially built on efit.
998 static constexpr inline fpreal32
999 SYSinvlerp(fpreal32 v, fpreal32 omin, fpreal32 omax)
1000 {
1001  return SYSefit(v, omin, omax, 0.0f, 1.0f);
1002 }
1003 
1004 static constexpr inline fpreal64
1005 SYSinvlerp(fpreal64 v, fpreal64 omin, fpreal64 omax)
1006 {
1007  return SYSefit(v, omin, omax, 0.0, 1.0);
1008 }
1009 
1010 // Linear hat function over kernel width dx.
1011 static inline fpreal32
1012 SYShat(fpreal32 x, fpreal32 dx)
1013 {
1014  const fpreal32 ax = SYSabs(x);
1015  return (ax > dx ? 0 : 1 - ax / dx);
1016 }
1017 
1018 static inline fpreal64
1019 SYShat(fpreal64 x, fpreal64 dx)
1020 {
1021  const fpreal64 ax = SYSabs(x);
1022  return (ax > dx ? 0 : 1 - ax / dx);
1023 }
1024 
1025 // Derivative of the linear hat function over kernel width dx.
1026 static constexpr inline fpreal32
1027 SYSdhat(fpreal32 x, fpreal32 dx)
1028 {
1029  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1030 }
1031 
1032 static constexpr inline fpreal64
1033 SYSdhat(fpreal64 x, fpreal64 dx)
1034 {
1035  return (x < -dx || x > dx ? 0 : -SYSsgn(x) / dx);
1036 }
1037 
1038 
1039 // For integer types.
1040 template<typename T>
1041 constexpr inline T
1042 SYSroundDownToMultipleOf(T val, T multiple)
1043 {
1044  // Only handle multiples of 2 and up.
1045  if (multiple <= 1)
1046  return val;
1047 
1048  int rem = val % multiple;
1049  if (rem == 0)
1050  return val;
1051 
1052  if (val < 0)
1053  return val - multiple - rem;
1054  else
1055  return val - rem;
1056 }
1057 
1058 template<>
1059 inline fpreal32
1060 SYSroundDownToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1061 {
1062  if (SYSequalZero(multiple))
1063  return val;
1064  fpreal32 modulus = SYSfmod(val, multiple);
1065  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1066  return val;
1067  fpreal32 retval = val - modulus;
1068  if( val < (fpreal32)0 && modulus!=(fpreal32)0 )
1069  retval -= multiple;
1070  return retval;
1071 }
1072 
1073 template<>
1074 inline fpreal64
1075 SYSroundDownToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1076 {
1077  if (SYSequalZero(multiple))
1078  return val;
1079  fpreal64 modulus = SYSfmod(val, multiple);
1080  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1081  return val;
1082  fpreal64 retval = val - modulus;
1083  if( val < (fpreal64)0 && modulus!=(fpreal64)0 )
1084  retval -= multiple;
1085  return retval;
1086 }
1087 
1088 template<typename T>
1089 constexpr inline T
1090 SYSroundUpToMultipleOf(T val, T multiple)
1091 {
1092  // Only handle multiples of 2 and up.
1093  if (multiple <= 1)
1094  return val;
1095 
1096  int rem = val % multiple;
1097  if (rem == 0)
1098  return val;
1099 
1100  if (val < 0)
1101  return val - rem;
1102  else
1103  return val + multiple - rem;
1104 }
1105 
1106 template<>
1107 inline fpreal32
1108 SYSroundUpToMultipleOf<fpreal32>(fpreal32 val, fpreal32 multiple)
1109 {
1110  fpreal32 modulus;
1111  if (SYSequalZero(multiple))
1112  return val;
1113  modulus = SYSfmod(val, multiple);
1114  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1115  return val;
1116  if (val > (fpreal32)0)
1117  val += multiple;
1118  return val - modulus;
1119 }
1120 
1121 template<>
1122 inline fpreal64
1123 SYSroundUpToMultipleOf<fpreal64>(fpreal64 val, fpreal64 multiple)
1124 {
1125  fpreal64 modulus;
1126  if (SYSequalZero(multiple))
1127  return val;
1128  modulus = SYSfmod(val, multiple);
1129  if (SYSequalZero(modulus) || SYSequalZero(modulus-multiple))
1130  return val;
1131  if (val > (fpreal64)0)
1132  val += multiple;
1133  return val - modulus;
1134 }
1135 
1136 inline constexpr uint32
1137 SYSroundUpPow2(uint32 val)
1138 {
1139  // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
1140  uint32 v = val;
1141 
1142  v--;
1143  v |= v >> 1;
1144  v |= v >> 2;
1145  v |= v >> 4;
1146  v |= v >> 8;
1147  v |= v >> 16;
1148  v++;
1149 
1150  return v;
1151 }
1152 
1153 static constexpr inline uint32
1154 SYSwang_inthash(uint32 key)
1155 {
1156  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1157  key += ~(key << 16);
1158  key ^= (key >> 5);
1159  key += (key << 3);
1160  key ^= (key >> 13);
1161  key += ~(key << 9);
1162  key ^= (key >> 17);
1163  return key;
1164 }
1165 
1166 
1167 static constexpr inline uint32
1168 SYSmultiplicative_inthash(uint32 key)
1169 {
1170  // Multiply by the golden mean of 2^32 (Knuth's multiplicative method) to
1171  // get a uniformly distributed hash value. Do a google search for
1172  // 2654435761 for more information.
1173  return key * 2654435761u;
1174 }
1175 
1176 static constexpr inline uint64
1177 SYSmultiplicative_inthash64(uint64 key)
1178 {
1179  // Same as the 32-bit version above, but using the golden mean of 2^64.
1180  return key * 11400714819323198485llu;
1181 }
1182 
1183 static constexpr inline uint64
1184 SYSwang_inthash64(uint64 key)
1185 {
1186  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1187  key += ~(key << 32ULL);
1188  key ^= (key >> 22);
1189  key += ~(key << 13);
1190  key ^= (key >> 8);
1191  key += (key << 3);
1192  key ^= (key >> 15);
1193  key += ~(key << 27);
1194  key ^= (key >> 31);
1195  return key;
1196 }
1197 
1198 static constexpr inline uint32
1199 SYSsharpe_inthash(uint32 key)
1200 {
1201  // Based on https://github.com/skeeto/hash-prospector, but with
1202  // modifications such that:
1203  // - bias value of 0.1406800449
1204  // - minimum cycle of 17 (the original algorithm had f(0) == 0)
1205  // - improved dieharder results
1206  key ^= (key >> 16);
1207  key *= 0xdbfb352dU;
1208  key ^= ~(key >> 15);
1209  key *= 0x6c2ca68bU;
1210  key ^= (key >> 16);
1211  return key;
1212 }
1213 
1214 static constexpr inline uint32
1215 SYSwang2_inthash(uint32 key)
1216 {
1217  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1218  // Updated integer hashing (2007)
1219  constexpr uint c2=0x27d4eb2d; // a prime or an odd constant
1220  key = (key ^ 61) ^ (key >> 16);
1221  key = key + (key << 3);
1222  key = key ^ (key >> 4);
1223  key = key * c2;
1224  key = key ^ (key >> 15);
1225  return key;
1226 }
1227 
1228 static constexpr inline uint64
1229 SYSwang2_inthash(uint64 key)
1230 {
1231  // From http://www.concentric.net/~Ttwang/tech/inthash.htm
1232  // Updated integer hashing (2007)
1233  key = (~key) + (key << 21); // key = (key<<21) - key - 1;
1234  key = key ^ (key >> 24);
1235  key = (key + (key << 3)) + (key << 8); // key * 265
1236  key = key ^ (key >> 14);
1237  key = (key + (key << 2)) + (key << 4); // key * 21
1238  key = key ^ (key >> 28);
1239  key = key + (key << 31);
1240  return key;
1241 }
1242 
1243 static inline uint
1244 SYSreal_hash(fpreal16 a, int lowmask=0x3)
1245 {
1246  return SYSwang_inthash(a.bits() & (~lowmask));
1247 }
1248 
1249 static inline uint
1250 SYSreal_hashseed(fpreal16 a, uint seed, int lowmask=0x3)
1251 {
1252  return SYSwang_inthash(seed + (a.bits() & (~lowmask)));
1253 }
1254 
1255 static inline uint
1256 SYSreal_hash(fpreal32 a, int lowmask=0xf)
1257 {
1258  SYS_FPRealUnionF ai;
1259  ai.fval = a;
1260  return SYSwang_inthash(ai.uval & (~lowmask));
1261 }
1262 
1263 static inline uint
1264 SYSreal_hashseed(fpreal32 a, uint seed, int lowmask=0xf)
1265 {
1266  SYS_FPRealUnionF ai;
1267  ai.fval = a;
1268  return SYSwang_inthash(seed + (ai.uval & (~lowmask)));
1269 }
1270 
1271 static inline uint
1272 SYSreal_hash(fpreal64 a, int lowmask=0xf)
1273 {
1274  SYS_FPRealUnionD ai;
1275  ai.fval = a;
1276  return SYSwang_inthash64(ai.uval & (~lowmask));
1277 }
1278 
1279 static inline uint
1280 SYSreal_hashseed(fpreal64 a, uint seed, int64 lowmask=0xf)
1281 {
1282  SYS_FPRealUnionD ai;
1283  ai.fval = a;
1284  return SYSwang_inthash64(seed + (ai.uval & (~lowmask)));
1285 }
1286 
1287 static constexpr inline uint
1288 SYSvector_hash(const int32 *vector, int size)
1289 {
1290  uint hash = 0;
1291  for (int i = 0; i < size; ++i)
1292  hash = SYSwang_inthash(hash + vector[i]);
1293  return hash;
1294 }
1295 
1296 static constexpr inline uint
1297 SYSvector_hash(const int64 *vector, int size)
1298 {
1299  uint hash = 0;
1300  for (int i = 0; i < size; ++i)
1301  hash = (uint)SYSwang_inthash64(hash + vector[i]);
1302  return hash;
1303 }
1304 
1305 static inline uint
1306 SYSvector_hash(const fpreal16 *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 fpreal32 *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 static inline uint
1324 SYSvector_hash(const fpreal64 *vector, int size)
1325 {
1326  uint hash = 0;
1327  for (int i = 0; i < size; ++i)
1328  hash = SYSreal_hashseed(vector[i], hash);
1329  return hash;
1330 }
1331 
1332 // Spatial hashing function for 3-vectors
1333 // Reference:
1334 // "Optimized Spatial Hashing for Collision Detection of Deformable Objects"
1335 //
1336 template<class P>
1337 static inline size_t
1338 SYSvector3_hash(const P &vector)
1339 {
1340  static constexpr size_t p1 = 73856093;
1341  static constexpr size_t p2 = 19349663;
1342  static constexpr size_t p3 = 83492791;
1343 
1344  return size_t(vector.x()*p1) ^ size_t(vector.y()*p2) ^ size_t(vector.z()*p3);
1345 }
1346 
1347 static inline uint
1348 SYSpointerHash(const void *ptr)
1349 {
1350 #if SIZEOF_VOID_P == 8
1351  // 64 bit pointers
1352  return (uint)((int64)ptr & 0xffffffff);
1353 #else
1354  // 32 bit pointers
1355  return (uint)ptr;
1356 #endif
1357 }
1358 
1359 // Convert a uniform random bit pattern to a random float in the range [0, 1)
1360 inline static fpreal32
1361 SYShashToFloat01(uint hash)
1362 {
1363  SYS_FPRealUnionF tmp;
1364  tmp.uval = 0x3f800000 | (0x007fffff & hash);
1365  return tmp.fval-1.0F;
1366 }
1367 
1368 // Generate a random number in range [0, 1)
1369 static inline fpreal32
1370 SYSfastRandom(uint &seed)
1371 {
1372  seed = seed*1664525 + 1013904223;
1373  return SYShashToFloat01(seed);
1374 }
1375 
1376 inline static fpreal32
1377 SYSrandom(uint &seed)
1378 {
1379  seed = seed*1664525 + 1013904223;
1380  return SYShashToFloat01(SYSwang_inthash(seed));
1381 }
1382 
1383 static constexpr uint
1384 SYSfastRandomInt(uint &seed)
1385 {
1386  seed = seed*1664525 + 1013904223;
1387  return SYSwang_inthash(seed);
1388 }
1389 
1390 inline static fpreal32
1391 SYSfastRandomZero(uint &seed)
1392 {
1393  return SYSfastRandom(seed) - 0.5F;
1394 }
1395 
1396 inline static fpreal32
1397 SYSrandomZero(uint &seed)
1398 {
1399  return SYSrandom(seed) - 0.5F;
1400 }
1401 
1402 template <typename T>
1403 static constexpr inline void
1404 SYSminmax(T v0, T v1, T v2, T v3, T &min, T &max)
1405 {
1406  min = SYSmin(v0, v1, v2, v3);
1407  max = SYSmax(v0, v1, v2, v3);
1408 }
1409 
1410 template <typename T>
1411 static constexpr inline void
1412 SYSminmax(T v0, T v1, T v2, T &min, T &max)
1413 {
1414  min = SYSmin(v0, v1, v2);
1415  max = SYSmax(v0, v1, v2);
1416 }
1417 
1418 template <typename T>
1419 static constexpr inline void
1420 SYSminmax(T v0, T v1, T &min, T &max)
1421 {
1422  min = SYSmin(v0, v1);
1423  max = SYSmax(v0, v1);
1424 }
1425 
1426 inline static void
1427 SYSgetSinCosFromSlope(fpreal32 slope, fpreal32 &sintheta, fpreal32 &costheta)
1428 {
1429  fpreal32 one_over_m;
1430  sintheta = slope / SYSsqrt(slope*slope + (fpreal32)1);
1431  if ((slope = SYSabs(slope)) > (fpreal32)1)
1432  {
1433  one_over_m = (fpreal32)1 / slope;
1434  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1435  }
1436  else
1437  costheta = SYSsqrt((fpreal32)1 - sintheta*sintheta);
1438 }
1439 
1440 inline static void
1441 SYSgetSinCosFromSlope(fpreal64 slope, fpreal64 &sintheta, fpreal64 &costheta)
1442 {
1443  fpreal64 one_over_m;
1444  sintheta = slope / SYSsqrt(slope*slope + (fpreal64)1);
1445  if ((slope = SYSabs(slope)) > (fpreal64)1)
1446  {
1447  one_over_m = (fpreal64)1 / slope;
1448  costheta = one_over_m / SYSsqrt(one_over_m*one_over_m + 1);
1449  }
1450  else
1451  costheta = SYSsqrt((fpreal64)1 - sintheta*sintheta);
1452 }
1453 
1454 inline constexpr static bool
1455 SYSsameSign( fpreal32 v0, fpreal32 v1 )
1456 {
1457  return (v0*v1)>0;
1458 }
1459 
1460 inline constexpr static bool
1461 SYSsameSign( fpreal64 v0, fpreal64 v1 )
1462 {
1463  return (v0*v1)>0;
1464 }
1465 
1466 inline constexpr static bool
1467 SYSsameSign( int32 v0, int32 v1 )
1468 {
1469  return (v0 ^ v1) >= 0;
1470 }
1471 
1472 inline constexpr static bool
1473 SYSsameSign( int64 v0, int64 v1 )
1474 {
1475  return (v0 ^ v1) >= 0;
1476 }
1477 
1478 static inline uint
1479 SYSnextPrime(uint num)
1480 {
1481  return SYSmakePrime(num+1);
1482 }
1483 
1484 static constexpr inline int
1485 SYShexCharToInt(char c)
1486 {
1487  // Given a hexidecimal character, return it's corresponding value between
1488  // 0 and 15, or -1 if it's not a hexidecimal character.
1489  if (c >= '0' && c <= '9')
1490  return c - '0';
1491  if (c >= 'a' && c <= 'f')
1492  return c - 'a' + 10;
1493  if (c >= 'A' && c <= 'F')
1494  return c - 'A' + 10;
1495  return -1;
1496 }
1497 
1498 static constexpr inline char
1499 SYSintToHexChar(int value)
1500 {
1501  // The value must be from 0-15 for this function to return a valid result.
1502  return value < 10 ? '0' + value : 'a' + value - 10;
1503 }
1504 
1505 SYS_API void SYSsort(int &a, int &b);
1506 SYS_API void SYSsort(int &a, int &b, int &c);
1507 SYS_API void SYSsort(int64 &a, int64 &b);
1508 SYS_API void SYSsort(int64 &a, int64 &b, int64 &c);
1509 SYS_API void SYSsort(float &a, float &b);
1510 SYS_API void SYSsort(float &a, float &b, float &c);
1511 SYS_API void SYSsort(double &a, double &b);
1512 SYS_API void SYSsort(double &a, double &b, double &c);
1513 
1514 // Compute both integer division and integer modulus
1515 // They are compiled into a single instruction
1516 static constexpr inline void
1517 SYSdivMod(int numerator, int denominator, int &quotient, int &remainder)
1518 {
1519  quotient = numerator / denominator;
1520  remainder = numerator % denominator;
1521 }
1522 
1523 static constexpr inline void
1524 SYSdivMod(int64 numerator, int64 denominator, int64 &quotient, int64 &remainder)
1525 {
1526  quotient = numerator / denominator;
1527  remainder = numerator % denominator;
1528 }
1529 
1530 // Include permutations of fpreal32/fpreal64
1531 #include "SYS_MathPermute.h"
1532 #include "SYS_MathRestrictive.h"
1533 
1534 #else /* cplusplus */
1535 #define SYSmax(a,b) ((a) > (b) ? (a) : (b))
1536 #define SYSmin(a,b) ((a) < (b) ? (a) : (b))
1537 #define SYSabs(a) ((a) < 0 ? (a) : -(a))
1538 #endif
1539 
1540 #endif
SYS_API float acosf(float x)
#define SYSmax(a, b)
Definition: SYS_Math.h:1535
SYS_API double cos(double x)
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
unsigned short uint16
Definition: SYS_Types.h:38
SYS_API double fmod(double x, double y)
SYS_API double atan2(double y, double x)
UT_Vector3T< T > SYSrecip(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:858
SYS_API double expm1(double x)
int int32
Definition: SYS_Types.h:39
int trunc(T x)
Definition: ImathFun.h:165
SYS_API void SYSsrand48(long seed)
bool SYSisFinite(fpreal64 f)
Definition: SYS_Math.h:194
#define CONST_INT64(x)
Definition: SYS_Types.h:320
const GLfloat * c
Definition: glew.h:16631
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7458
OIIO_HOSTDEVICE void sincos(float x, float *sine, float *cosine)
Definition: fmath.h:686
constexpr bool SYSisNan(const F f)
Definition: SYS_Math.h:178
#define SYSabs(a)
Definition: SYS_Math.h:1537
SYS_API float atan2f(float y, float x)
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
#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:651
unsigned long long uint64
Definition: SYS_Types.h:117
SYS_API double log10(double x)
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
GLdouble GLdouble t
Definition: glew.h:1403
#define M_PI
Definition: ImathPlatform.h:51
SYS_API float log10f(float x)
GLint GLenum GLint x
Definition: glcorearb.h:408
GLsizeiptr size
Definition: glcorearb.h:663
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
GLuint64EXT * result
Definition: glew.h:14311
SYS_API double asin(double x)
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
SYS_API double sinh(double x)
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
SYS_API double copysign(double x, double y)
INT32 INT32 * denominator
Definition: wglew.h:1180
SYS_API float atanf(float x)
const GLdouble * v
Definition: glcorearb.h:836
SYS_API fpreal32 SYSroundAngle(fpreal32 base, fpreal32 source)
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:832
GLuint num
Definition: glew.h:2695
SYS_API double cosh(double x)
SYS_API bool SYSisPrime(uint num)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
SYS_API void sincosf(float x, float *s, float *c)
constexpr int SYSsignum(const F a) noexcept
Definition: SYS_Math.h:170
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:487
SYS_API float logf(float x)
T exp(const T &v)
Definition: simd.h:7610
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:818
SYS_API double acos(double x)
signed char int8
Definition: SYS_Types.h:35
SYS_API bool SYSisInt(const char *str)
SYS_API double hypot(double x, double y)
GLfloat v0
Definition: glcorearb.h:815
fpreal32 SYSrint(fpreal32 val)
Definition: SYS_Floor.h:163
SYS_API double log1p(double x)
SYS_API double tanh(double x)
bool SYSequalZero(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:844
SYS_API uint SYSmakePrime(uint num)
GLuint GLfloat * val
Definition: glcorearb.h:1607
SYS_API double tan(double x)
T log(const T &v)
Definition: simd.h:7665
const void * ptr(const T *p)
Definition: format.h:3603
short int16
Definition: SYS_Types.h:37
SYS_API double atan(double x)
bool isNan() const
Definition: fpreal16.h:664
GLsizei const GLfloat * value
Definition: glcorearb.h:823
GLfloat f
Definition: glcorearb.h:1925
unsigned int uint32
Definition: SYS_Types.h:40
SYS_API float asinf(float x)
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
GLfloat GLfloat v1
Definition: glcorearb.h:816
unsigned short bits() const
Definition: fpreal16.h:725
GLboolean r
Definition: glcorearb.h:1221
#define SYSmin(a, b)
Definition: SYS_Math.h:1536
GLdouble s
Definition: glew.h:1395
SYS_API bool SYSisFloat(const char *str)
unsigned int uint
Definition: SYS_Types.h:45
GLint y
Definition: glcorearb.h:102
SYS_API double sin(double x)
SYS_API fpreal32 SYSceil(fpreal32 val)