1// Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// mathutil.h: Math and bit manipulation functions. 16 17#ifndef LIBGL_MATHUTIL_H_ 18#define LIBGL_MATHUTIL_H_ 19 20#include "common/debug.h" 21#include "Common/Math.hpp" 22 23namespace gl 24{ 25inline bool isPow2(int x) 26{ 27 return (x & (x - 1)) == 0 && (x != 0); 28} 29 30inline int log2(int x) 31{ 32 int r = 0; 33 while((x >> r) > 1) r++; 34 return r; 35} 36 37inline unsigned int ceilPow2(unsigned int x) 38{ 39 if(x != 0) x--; 40 x |= x >> 1; 41 x |= x >> 2; 42 x |= x >> 4; 43 x |= x >> 8; 44 x |= x >> 16; 45 x++; 46 47 return x; 48} 49 50using sw::clamp; 51using sw::clamp01; 52 53template<const int n> 54inline unsigned int unorm(float x) 55{ 56 const unsigned int max = 0xFFFFFFFF >> (32 - n); 57 58 if(x > 1) 59 { 60 return max; 61 } 62 else if(x < 0) 63 { 64 return 0; 65 } 66 else 67 { 68 return (unsigned int)(max * x + 0.5f); 69 } 70} 71} 72 73#endif // LIBGL_MATHUTIL_H_ 74