1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
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
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.
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.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMATHFUN_H
38 #define INCLUDED_IMATHFUN_H
40 //-----------------------------------------------------------------------------
42 // Miscellaneous utility functions
44 //-----------------------------------------------------------------------------
46 #include "ImathLimits.h"
54 return (a > 0) ? a : -a;
62 return (a > 0)? 1 : ((a < 0) ? -1 : 0);
66 template <class T, class Q>
70 return (T) (a + (b - a) * t);
74 template <class T, class Q>
78 return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t));
84 lerpfactor(T m, T a, T b)
87 // Return how far m is between a and b, that is return t such that
89 // t = lerpfactor(m, a, b);
99 if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d))
108 clamp (T a, T l, T h)
110 return (a < l)? l : ((a > h)? h : a);
118 return Imath::sign (a - b);
126 return (Imath::abs (a - b) <= t)? 0 : cmp (a, b);
134 return (Imath::abs (a) <= t) ? 1 : 0;
138 template <class T1, class T2, class T3>
140 equal (T1 a, T2 b, T3 t)
142 return Imath::abs (a - b) <= t;
149 return (x >= 0)? int (x): -(int (-x) + (-x > int (-x)));
164 return (x >= 0) ? int(x) : -int(-x);
169 // Integer division and remainder where the
170 // remainder of x/y has the same sign as x:
172 // divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
173 // mods(x,y) == x - y * divs(x,y)
179 return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
180 ((y >= 0)? -(-x / y): (-x / -y));
187 return (x >= 0)? ((y >= 0)? ( x % y): ( x % -y)):
188 ((y >= 0)? -(-x % y): -(-x % -y));
193 // Integer division and remainder where the
194 // remainder of x/y is always positive:
196 // divp(x,y) == floor (double(x) / double (y))
197 // modp(x,y) == x - y * divp(x,y)
203 return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
204 ((y >= 0)? -((y-1-x) / y): ((-y-1-x) / -y));
211 return x - y * divp (x, y);
214 //----------------------------------------------------------
215 // Successor and predecessor for floating-point numbers:
217 // succf(f) returns float(f+e), where e is the smallest
218 // positive number such that float(f+e) != f.
220 // predf(f) returns float(f-e), where e is the smallest
221 // positive number such that float(f-e) != f.
223 // succd(d) returns double(d+e), where e is the smallest
224 // positive number such that double(d+e) != d.
226 // predd(d) returns double(d-e), where e is the smallest
227 // positive number such that double(d-e) != d.
229 // Exceptions: If the input value is an infinity or a nan,
230 // succf(), predf(), succd(), and predd() all
231 // return the input value without changing it.
233 //----------------------------------------------------------
235 float succf (float f);
236 float predf (float f);
238 double succd (double d);
239 double predd (double d);
242 // Return true if the number is not a NaN or Infinity.
248 union {float f; int i;} u;
251 return (u.i & 0x7f800000) != 0x7f800000;
257 #if ULONG_MAX == 18446744073709551615LU
258 typedef long unsigned int Int64;
260 typedef long long unsigned int Int64;
263 union {double d; Int64 i;} u;
266 return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;