source: NonGTP/OpenEXR/include/Imath/ImathFun.h @ 855

Revision 855, 5.8 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[855]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
35
36
37#ifndef INCLUDED_IMATHFUN_H
38#define INCLUDED_IMATHFUN_H
39
40//-----------------------------------------------------------------------------
41//
42//      Miscellaneous utility functions
43//
44//-----------------------------------------------------------------------------
45
46#include <ImathLimits.h>
47
48namespace Imath {
49
50template <class T>
51inline T
52abs (T a)
53{
54    return (a > 0) ? a : -a;
55}
56
57
58template <class T>
59inline int
60sign (T a)
61{
62    return (a > 0)? 1 : ((a < 0) ? -1 : 0);
63}
64
65
66template <class T, class Q>
67inline T
68lerp (T a, T b, Q t)
69{
70    return (T) (a + (b - a) * t);
71}
72
73
74template <class T, class Q>
75inline T
76ulerp (T a, T b, Q t)
77{
78    return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t));
79}
80
81
82template <class T>
83inline T
84lerpfactor(T m, T a, T b)
85{
86    //
87    // Return how far m is between a and b, that is return t such that
88    // if:
89    //     t = lerpfactor(m, a, b);
90    // then:
91    //     m = lerp(a, b, t);
92    //
93    // If a==b, return 0.
94    //
95
96    T d = b - a;
97    T n = m - a;
98
99    if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d))
100        return n / d;
101
102    return T(0);
103}
104
105
106template <class T>
107inline T
108clamp (T a, T l, T h)
109{
110    return (a < l)? l : ((a > h)? h : a);
111}
112
113
114template <class T>
115inline int
116cmp (T a, T b)
117{
118    return Imath::sign (a - b);
119}
120
121
122template <class T>
123inline int
124cmpt (T a, T b, T t)
125{
126    return (Imath::abs (a - b) <= t)? 0 : cmp (a, b);
127}
128
129
130template <class T>
131inline bool
132iszero (T a, T t)
133{
134    return (Imath::abs (a) <= t) ? 1 : 0;
135}
136
137
138template <class T1, class T2, class T3>
139inline bool
140equal (T1 a, T2 b, T3 t)
141{
142    return Imath::abs (a - b) <= t;
143}
144
145template <class T>
146inline int
147floor (T x)
148{
149    return (x >= 0)? int (x): -(int (-x) + (-x > int (-x)));
150}
151
152
153template <class T>
154inline int
155ceil (T x)
156{
157    return -floor (-x);
158}
159
160template <class T>
161inline int
162trunc (T x)
163{
164    return (x >= 0) ? int(x) : -int(-x);
165}
166
167
168//
169// Integer division and remainder where the
170// remainder of x/y has the same sign as x:
171//
172//      divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
173//      mods(x,y) == x - y * divs(x,y)
174//
175
176inline int
177divs (int x, int y)
178{
179    return (x >= 0)? ((y >= 0)?  ( x / y): -( x / -y)):
180                     ((y >= 0)? -(-x / y):  (-x / -y));
181}
182
183
184inline int
185mods (int x, int y)
186{
187    return (x >= 0)? ((y >= 0)?  ( x % y):  ( x % -y)):
188                     ((y >= 0)? -(-x % y): -(-x % -y));
189}
190
191
192//
193// Integer division and remainder where the
194// remainder of x/y is always positive:
195//
196//      divp(x,y) == floor (double(x) / double (y))
197//      modp(x,y) == x - y * divp(x,y)
198//
199
200inline int
201divp (int x, int y)
202{
203    return (x >= 0)? ((y >= 0)?  (     x  / y): -(      x  / -y)):
204                     ((y >= 0)? -((y-1-x) / y):  ((-y-1-x) / -y));
205}
206
207
208inline int
209modp (int x, int y)
210{
211    return x - y * divp (x, y);
212}
213
214//----------------------------------------------------------
215// Successor and predecessor for floating-point numbers:
216//
217// succf(f)     returns float(f+e), where e is the smallest
218//              positive number such that float(f+e) != f.
219//
220// predf(f)     returns float(f-e), where e is the smallest
221//              positive number such that float(f-e) != f.
222//
223// succd(d)     returns double(d+e), where e is the smallest
224//              positive number such that double(d+e) != d.
225//
226// predd(d)     returns double(d-e), where e is the smallest
227//              positive number such that double(d-e) != d.
228//
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.
232//
233//----------------------------------------------------------
234
235float succf (float f);
236float predf (float f);
237
238double succd (double d);
239double predd (double d);
240
241//
242// Return true if the number is not a NaN or Infinity.
243//
244
245inline bool
246finitef (float f)
247{
248    union {float f; int i;} u;
249    u.f = f;
250
251    return (u.i & 0x7f800000) != 0x7f800000;
252}
253
254inline bool
255finited (double d)
256{
257#if ULONG_MAX == 18446744073709551615LU
258    typedef      long unsigned int Int64;
259#else
260    typedef long long unsigned int Int64;
261#endif
262
263    union {double d; Int64 i;} u;
264    u.d = d;
265
266    return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;
267}
268
269
270} // namespace Imath
271
272#endif
Note: See TracBrowser for help on using the repository browser.