1 // SwiftShader Software Renderer
3 // Copyright(c) 2005-2012 TransGaming Inc.
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
6 // transcribed, stored in a retrieval system, translated into any human or computer
7 // language by any means, or disclosed to third parties without the explicit written
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9 // or implied, including but not limited to any patent rights, are granted to you.
12 // mathutil.h: Math and bit manipulation functions.
14 #ifndef LIBGLESV2_MATHUTIL_H_
15 #define LIBGLESV2_MATHUTIL_H_
17 #include "common/debug.h"
23 inline bool isPow2(int x)
25 return (x & (x - 1)) == 0 && (x != 0);
28 inline int log2(int x)
31 while((x >> r) > 1) r++;
35 inline unsigned int ceilPow2(unsigned int x)
48 template<typename T, typename MIN, typename MAX>
49 inline T clamp(T x, MIN min, MAX max)
51 return x < min ? min : (x > max ? max : x);
54 inline float clamp01(float x)
56 return clamp(x, 0.0f, 1.0f);
60 inline unsigned int unorm(float x)
62 const unsigned int max = 0xFFFFFFFF >> (32 - n);
74 return (unsigned int)(max * x + 0.5f);
79 #endif // LIBGLESV2_MATHUTIL_H_