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 LIBGLES_CM_MATHUTIL_H_
18 #define LIBGLES_CM_MATHUTIL_H_
19 
20 #include "common/debug.h"
21 #include "Common/Math.hpp"
22 
23 namespace es1
24 {
isPow2(int x)25 inline bool isPow2(int x)
26 {
27 	return (x & (x - 1)) == 0 && (x != 0);
28 }
29 
log2(int x)30 inline int log2(int x)
31 {
32 	int r = 0;
33 	while((x >> r) > 1) r++;
34 	return r;
35 }
36 
ceilPow2(unsigned int x)37 inline 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 
50 using sw::clamp;
51 using sw::clamp01;
52 
53 template<const int n>
unorm(float x)54 inline 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   // LIBGLES_CM_MATHUTIL_H_
74