1// 2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. 3// Use of this source code is governed by a BSD-style license that can be 4// found in the LICENSE file. 5// 6 7// mathutil.h: Math and bit manipulation functions. 8 9#ifndef LIBGLESV2_MATHUTIL_H_ 10#define LIBGLESV2_MATHUTIL_H_ 11 12#include <math.h> 13 14namespace gl 15{ 16inline bool isPow2(int x) 17{ 18 return (x & (x - 1)) == 0 && (x != 0); 19} 20 21inline int log2(int x) 22{ 23 int r = 0; 24 while ((x >> r) > 1) r++; 25 return r; 26} 27 28inline unsigned int ceilPow2(unsigned int x) 29{ 30 if (x != 0) x--; 31 x |= x >> 1; 32 x |= x >> 2; 33 x |= x >> 4; 34 x |= x >> 8; 35 x |= x >> 16; 36 x++; 37 38 return x; 39} 40 41inline float clamp01(float x) 42{ 43 return x < 0 ? 0 : (x > 1 ? 1 : x); 44} 45 46template<const int n> 47inline unsigned int unorm(float x) 48{ 49 const unsigned int max = 0xFFFFFFFF >> (32 - n); 50 51 if (x > 1) 52 { 53 return max; 54 } 55 else if (x < 0) 56 { 57 return 0; 58 } 59 else 60 { 61 return (unsigned int)(max * x + 0.5f); 62 } 63} 64} 65 66#endif // LIBGLESV2_MATHUTIL_H_ 67