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"
47
#include "ImathInt64.h"
55
return (a > T(0)) ? a : -a;
63
return (a > T(0))? 1 : ((a < T(0)) ? -1 : 0);
67
template <class T, class Q>
71
return (T) (a * (1 - t) + b * t);
75
template <class T, class Q>
79
return (T) ((a > b)? (a - (a - b) * t): (a + (b - a) * t));
85
lerpfactor(T m, T a, T b)
88
// Return how far m is between a and b, that is return t such that
90
// t = lerpfactor(m, a, b);
100
if (abs(d) > T(1) || abs(n) < limits<T>::max() * abs(d))
109
clamp (T a, T l, T h)
111
return (a < l)? l : ((a > h)? h : a);
119
return Imath::sign (a - b);
127
return (Imath::abs (a - b) <= t)? 0 : cmp (a, b);
135
return (Imath::abs (a) <= t) ? 1 : 0;
139
template <class T1, class T2, class T3>
141
equal (T1 a, T2 b, T3 t)
143
return Imath::abs (a - b) <= t;
150
return (x >= 0)? int (x): -(int (-x) + (-x > int (-x)));
165
return (x >= 0) ? int(x) : -int(-x);
170
// Integer division and remainder where the
171
// remainder of x/y has the same sign as x:
173
// divs(x,y) == (abs(x) / abs(y)) * (sign(x) * sign(y))
174
// mods(x,y) == x - y * divs(x,y)
180
return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
181
((y >= 0)? -(-x / y): (-x / -y));
188
return (x >= 0)? ((y >= 0)? ( x % y): ( x % -y)):
189
((y >= 0)? -(-x % y): -(-x % -y));
194
// Integer division and remainder where the
195
// remainder of x/y is always positive:
197
// divp(x,y) == floor (double(x) / double (y))
198
// modp(x,y) == x - y * divp(x,y)
204
return (x >= 0)? ((y >= 0)? ( x / y): -( x / -y)):
205
((y >= 0)? -((y-1-x) / y): ((-y-1-x) / -y));
212
return x - y * divp (x, y);
215
//----------------------------------------------------------
216
// Successor and predecessor for floating-point numbers:
218
// succf(f) returns float(f+e), where e is the smallest
219
// positive number such that float(f+e) != f.
221
// predf(f) returns float(f-e), where e is the smallest
222
// positive number such that float(f-e) != f.
224
// succd(d) returns double(d+e), where e is the smallest
225
// positive number such that double(d+e) != d.
227
// predd(d) returns double(d-e), where e is the smallest
228
// positive number such that double(d-e) != d.
230
// Exceptions: If the input value is an infinity or a nan,
231
// succf(), predf(), succd(), and predd() all
232
// return the input value without changing it.
234
//----------------------------------------------------------
236
float succf (float f);
237
float predf (float f);
239
double succd (double d);
240
double predd (double d);
243
// Return true if the number is not a NaN or Infinity.
249
union {float f; int i;} u;
252
return (u.i & 0x7f800000) != 0x7f800000;
258
union {double d; Int64 i;} u;
261
return (u.i & 0x7ff0000000000000LL) != 0x7ff0000000000000LL;