/src/FreeImage/Source/OpenEXR/Imath/ImathFun.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 267 lines · 130 code · 53 blank · 84 comment · 4 complexity · 577bc020c78de6a487920878857a6585 MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHFUN_H
  35. #define INCLUDED_IMATHFUN_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Miscellaneous utility functions
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "ImathLimits.h"
  42. #include "ImathInt64.h"
  43. namespace Imath {
  44. template <class T>
  45. inline T
  46. abs (T a)
  47. {
  48. return (a > 0) ? a : -a;
  49. }
  50. template <class T>
  51. inline int
  52. sign (T a)
  53. {
  54. return (a > 0)? 1 : ((a < 0) ? -1 : 0);
  55. }
  56. template <class T, class Q>
  57. inline T
  58. lerp (T a, T b, Q t)
  59. {
  60. return (T) (a * (1 - t) + b * t);
  61. }
  62. template <class T, class Q>
  63. inline T
  64. ulerp (T a, T b, Q t)
  65. {
  66. return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t));
  67. }
  68. template <class T>
  69. inline T
  70. lerpfactor(T m, T a, T b)
  71. {
  72. //
  73. // Return how far m is between a and b, that is return t such that
  74. // if:
  75. // t = lerpfactor(m, a, b);
  76. // then:
  77. // m = lerp(a, b, t);
  78. //
  79. // If a==b, return 0.
  80. //
  81. T d = b - a;
  82. T n = m - a;
  83. if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d))
  84. return n / d;
  85. return T(0);
  86. }
  87. template <class T>
  88. inline T
  89. clamp (T a, T l, T h)
  90. {
  91. return (a < l)? l : ((a > h)? h : a);
  92. }
  93. template <class T>
  94. inline int
  95. cmp (T a, T b)
  96. {
  97. return Imath::sign (a - b);
  98. }
  99. template <class T>
  100. inline int
  101. cmpt (T a, T b, T t)
  102. {
  103. return (Imath::abs (a - b) <= t)? 0 : cmp (a, b);
  104. }
  105. template <class T>
  106. inline bool
  107. iszero (T a, T t)
  108. {
  109. return (Imath::abs (a) <= t) ? 1 : 0;
  110. }
  111. template <class T1, class T2, class T3>
  112. inline bool
  113. equal (T1 a, T2 b, T3 t)
  114. {
  115. return Imath::abs (a - b) <= t;
  116. }
  117. template <class T>
  118. inline int
  119. floor (T x)
  120. {
  121. return (x >= 0)? int (x): -(int (-x) + (-x > int (-x)));
  122. }
  123. template <class T>
  124. inline int
  125. ceil (T x)
  126. {
  127. return -floor (-x);
  128. }
  129. template <class T>
  130. inline int
  131. trunc (T x)
  132. {
  133. return (x >= 0) ? int(x) : -int(-x);
  134. }
  135. //
  136. // Integer division and remainder where the
  137. // remainder of x/y has the same sign as x:
  138. //
  139. // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
  140. // mods(x,y) == x - y * divs(x,y)
  141. //
  142. inline int
  143. divs (int x, int y)
  144. {
  145. return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
  146. ((y >= 0)? -(-x / y): (-x / -y));
  147. }
  148. inline int
  149. mods (int x, int y)
  150. {
  151. return (x >= 0)? ((y >= 0)? ( x % y): ( x % -y)):
  152. ((y >= 0)? -(-x % y): -(-x % -y));
  153. }
  154. //
  155. // Integer division and remainder where the
  156. // remainder of x/y is always positive:
  157. //
  158. // divp(x,y) == floor (double(x) / double (y))
  159. // modp(x,y) == x - y * divp(x,y)
  160. //
  161. inline int
  162. divp (int x, int y)
  163. {
  164. return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
  165. ((y >= 0)? -((y-1-x) / y): ((-y-1-x) / -y));
  166. }
  167. inline int
  168. modp (int x, int y)
  169. {
  170. return x - y * divp (x, y);
  171. }
  172. //----------------------------------------------------------
  173. // Successor and predecessor for floating-point numbers:
  174. //
  175. // succf(f) returns float(f+e), where e is the smallest
  176. // positive number such that float(f+e) != f.
  177. //
  178. // predf(f) returns float(f-e), where e is the smallest
  179. // positive number such that float(f-e) != f.
  180. //
  181. // succd(d) returns double(d+e), where e is the smallest
  182. // positive number such that double(d+e) != d.
  183. //
  184. // predd(d) returns double(d-e), where e is the smallest
  185. // positive number such that double(d-e) != d.
  186. //
  187. // Exceptions: If the input value is an infinity or a nan,
  188. // succf(), predf(), succd(), and predd() all
  189. // return the input value without changing it.
  190. //
  191. //----------------------------------------------------------
  192. float succf (float f);
  193. float predf (float f);
  194. double succd (double d);
  195. double predd (double d);
  196. //
  197. // Return true if the number is not a NaN or Infinity.
  198. //
  199. inline bool
  200. finitef (float f)
  201. {
  202. union {float f; int i;} u;
  203. u.f = f;
  204. return (u.i & 0x7f800000) != 0x7f800000;
  205. }
  206. inline bool
  207. finited (double d)
  208. {
  209. union {double d; Int64 i;} u;
  210. u.d = d;
  211. return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
  212. }
  213. } // namespace Imath
  214. #endif