blob: 7f62c5b6543565053258ffbaf0fb7446ae7bc65d [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
Nicolas Capensdbf6fc82014-10-23 13:33:20 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// 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 Capensdbf6fc82014-10-23 13:33:20 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
Nicolas Capensdbf6fc82014-10-23 13:33:20 -04008//
Nicolas Capens0bac2852016-05-07 06:09:58 -04009// 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 Capensdbf6fc82014-10-23 13:33:20 -040014
15// mathutil.h: Math and bit manipulation functions.
16
Nicolas Capens11157822014-10-23 23:00:29 -040017#ifndef LIBGLES_CM_MATHUTIL_H_
18#define LIBGLES_CM_MATHUTIL_H_
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040019
20#include "common/debug.h"
Nicolas Capens5524f052015-12-21 15:48:31 -050021#include "Common/Math.hpp"
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040022
Nicolas Capens14ee7622014-10-28 23:48:41 -040023namespace es1
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040024{
25inline bool isPow2(int x)
26{
Nicolas Capens0bac2852016-05-07 06:09:58 -040027 return (x & (x - 1)) == 0 && (x != 0);
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040028}
29
30inline int log2(int x)
31{
Nicolas Capens0bac2852016-05-07 06:09:58 -040032 int r = 0;
33 while((x >> r) > 1) r++;
34 return r;
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040035}
36
37inline unsigned int ceilPow2(unsigned int x)
38{
Nicolas Capens0bac2852016-05-07 06:09:58 -040039 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 Capensdbf6fc82014-10-23 13:33:20 -040046
Nicolas Capens0bac2852016-05-07 06:09:58 -040047 return x;
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040048}
49
Nicolas Capensdc4ae862016-03-21 16:59:03 -040050using sw::clamp;
51using sw::clamp01;
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040052
53template<const int n>
54inline unsigned int unorm(float x)
55{
Nicolas Capens0bac2852016-05-07 06:09:58 -040056 const unsigned int max = 0xFFFFFFFF >> (32 - n);
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040057
Nicolas Capens0bac2852016-05-07 06:09:58 -040058 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 Capensdbf6fc82014-10-23 13:33:20 -040070}
71}
72
Nicolas Capens11157822014-10-23 23:00:29 -040073#endif // LIBGLES_CM_MATHUTIL_H_