Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 8 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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. |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 14 | |
| 15 | // mathutil.h: Math and bit manipulation functions. |
| 16 | |
Nicolas Capens | 1115782 | 2014-10-23 23:00:29 -0400 | [diff] [blame] | 17 | #ifndef LIBGLES_CM_MATHUTIL_H_ |
| 18 | #define LIBGLES_CM_MATHUTIL_H_ |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 19 | |
| 20 | #include "common/debug.h" |
Nicolas Capens | 5524f05 | 2015-12-21 15:48:31 -0500 | [diff] [blame] | 21 | #include "Common/Math.hpp" |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 22 | |
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 23 | namespace es1 |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 24 | { |
| 25 | inline bool isPow2(int x) |
| 26 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 27 | return (x & (x - 1)) == 0 && (x != 0); |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | inline int log2(int x) |
| 31 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 32 | int r = 0; |
| 33 | while((x >> r) > 1) r++; |
| 34 | return r; |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | inline unsigned int ceilPow2(unsigned int x) |
| 38 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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++; |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 46 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 47 | return x; |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Nicolas Capens | dc4ae86 | 2016-03-21 16:59:03 -0400 | [diff] [blame] | 50 | using sw::clamp; |
| 51 | using sw::clamp01; |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 52 | |
| 53 | template<const int n> |
| 54 | inline unsigned int unorm(float x) |
| 55 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 56 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 57 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | } |
Nicolas Capens | dbf6fc8 | 2014-10-23 13:33:20 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Nicolas Capens | 1115782 | 2014-10-23 23:00:29 -0400 | [diff] [blame] | 73 | #endif // LIBGLES_CM_MATHUTIL_H_ |