| // SwiftShader Software Renderer | |
| // | |
| // Copyright(c) 2005-2012 TransGaming Inc. | |
| // | |
| // All rights reserved. No part of this software may be copied, distributed, transmitted, | |
| // transcribed, stored in a retrieval system, translated into any human or computer | |
| // language by any means, or disclosed to third parties without the explicit written | |
| // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express | |
| // or implied, including but not limited to any patent rights, are granted to you. | |
| // | |
| // mathutil.h: Math and bit manipulation functions. | |
| #ifndef LIBGLESV2_MATHUTIL_H_ | |
| #define LIBGLESV2_MATHUTIL_H_ | |
| #include "common/debug.h" | |
| #include <math.h> | |
| namespace es2 | |
| { | |
| inline bool isPow2(int x) | |
| { | |
| return (x & (x - 1)) == 0 && (x != 0); | |
| } | |
| inline int log2(int x) | |
| { | |
| int r = 0; | |
| while((x >> r) > 1) r++; | |
| return r; | |
| } | |
| inline unsigned int ceilPow2(unsigned int x) | |
| { | |
| if(x != 0) x--; | |
| x |= x >> 1; | |
| x |= x >> 2; | |
| x |= x >> 4; | |
| x |= x >> 8; | |
| x |= x >> 16; | |
| x++; | |
| return x; | |
| } | |
| template<typename T, typename MIN, typename MAX> | |
| inline T clamp(T x, MIN min, MAX max) | |
| { | |
| return x < min ? min : (x > max ? max : x); | |
| } | |
| inline float clamp01(float x) | |
| { | |
| return clamp(x, 0.0f, 1.0f); | |
| } | |
| template<const int n> | |
| inline unsigned int unorm(float x) | |
| { | |
| const unsigned int max = 0xFFFFFFFF >> (32 - n); | |
| if(x > 1) | |
| { | |
| return max; | |
| } | |
| else if(x < 0) | |
| { | |
| return 0; | |
| } | |
| else | |
| { | |
| return (unsigned int)(max * x + 0.5f); | |
| } | |
| } | |
| } | |
| #endif // LIBGLESV2_MATHUTIL_H_ |