Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 1 | // SwiftShader Software Renderer |
| 2 | // |
| 3 | // Copyright(c) 2005-2012 TransGaming Inc. |
| 4 | // |
| 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. |
| 10 | // |
| 11 | |
| 12 | // mathutil.h: Math and bit manipulation functions. |
| 13 | |
Nicolas Capens | 1115782 | 2014-10-23 23:00:29 -0400 | [diff] [blame] | 14 | #ifndef LIBGLES_CM_MATHUTIL_H_ |
| 15 | #define LIBGLES_CM_MATHUTIL_H_ |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 16 | |
| 17 | #include "common/debug.h" |
Nicolas Capens | 5524f05 | 2015-12-21 15:48:31 -0500 | [diff] [blame] | 18 | #include "Common/Math.hpp" |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 20 | namespace es1 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 21 | { |
| 22 | inline bool isPow2(int x) |
| 23 | { |
| 24 | return (x & (x - 1)) == 0 && (x != 0); |
| 25 | } |
| 26 | |
| 27 | inline int log2(int x) |
| 28 | { |
| 29 | int r = 0; |
| 30 | while((x >> r) > 1) r++; |
| 31 | return r; |
| 32 | } |
| 33 | |
| 34 | inline unsigned int ceilPow2(unsigned int x) |
| 35 | { |
| 36 | if(x != 0) x--; |
| 37 | x |= x >> 1; |
| 38 | x |= x >> 2; |
| 39 | x |= x >> 4; |
| 40 | x |= x >> 8; |
| 41 | x |= x >> 16; |
| 42 | x++; |
| 43 | |
| 44 | return x; |
| 45 | } |
| 46 | |
Nicolas Capens | dc4ae86 | 2016-03-21 16:59:03 -0400 | [diff] [blame^] | 47 | using sw::clamp; |
| 48 | using sw::clamp01; |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 49 | |
| 50 | template<const int n> |
| 51 | inline unsigned int unorm(float x) |
| 52 | { |
| 53 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
| 54 | |
| 55 | if(x > 1) |
| 56 | { |
| 57 | return max; |
| 58 | } |
| 59 | else if(x < 0) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | return (unsigned int)(max * x + 0.5f); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Nicolas Capens | 1115782 | 2014-10-23 23:00:29 -0400 | [diff] [blame] | 70 | #endif // LIBGLES_CM_MATHUTIL_H_ |