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 LIBGLESV2_MATHUTIL_H_ 18 #define LIBGLESV2_MATHUTIL_H_ 19 20 #include "common/debug.h" 21 #include "Common/Math.hpp" 22 #include <limits> 23 24 namespace es2 25 { isPow2(int x)26inline bool isPow2(int x) 27 { 28 return (x & (x - 1)) == 0 && (x != 0); 29 } 30 log2(int x)31inline int log2(int x) 32 { 33 int r = 0; 34 while((x >> r) > 1) r++; 35 return r; 36 } 37 ceilPow2(unsigned int x)38inline unsigned int ceilPow2(unsigned int x) 39 { 40 if(x != 0) x--; 41 x |= x >> 1; 42 x |= x >> 2; 43 x |= x >> 4; 44 x |= x >> 8; 45 x |= x >> 16; 46 x++; 47 48 return x; 49 } 50 51 using sw::swap; 52 using sw::clamp; 53 using sw::clamp01; 54 55 template<const int n> unorm(float x)56inline unsigned int unorm(float x) 57 { 58 const unsigned int max = 0xFFFFFFFF >> (32 - n); 59 60 if(x > 1) 61 { 62 return max; 63 } 64 else if(x < 0) 65 { 66 return 0; 67 } 68 else 69 { 70 return (unsigned int)(max * x + 0.5f); 71 } 72 } 73 74 // Converts floating-point values to the nearest representable integer convert_float_int(float x)75inline int convert_float_int(float x) 76 { 77 // The largest positive integer value that is exactly representable in IEEE 754 binary32 is 0x7FFFFF80. 78 // The next floating-point value is 128 larger and thus needs clamping to 0x7FFFFFFF. 79 static_assert(std::numeric_limits<float>::is_iec559, "Unsupported floating-point format"); 80 81 if(x > 0x7FFFFF80) 82 { 83 return 0x7FFFFFFF; 84 } 85 86 if(x < (signed)0x80000000) 87 { 88 return 0x80000000; 89 } 90 91 return static_cast<int>(roundf(x)); 92 } 93 } 94 95 #endif // LIBGLESV2_MATHUTIL_H_ 96