blob: 89c2e54a44af05792ff5cc55f8309f8bd0883cb9 [file] [log] [blame]
Nicolas Capensdbf6fc82014-10-23 13:33:20 -04001// 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 Capens11157822014-10-23 23:00:29 -040014#ifndef LIBGLES_CM_MATHUTIL_H_
15#define LIBGLES_CM_MATHUTIL_H_
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040016
17#include "common/debug.h"
Nicolas Capens5524f052015-12-21 15:48:31 -050018#include "Common/Math.hpp"
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040019
Nicolas Capens14ee7622014-10-28 23:48:41 -040020namespace es1
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040021{
22inline bool isPow2(int x)
23{
24 return (x & (x - 1)) == 0 && (x != 0);
25}
26
27inline int log2(int x)
28{
29 int r = 0;
30 while((x >> r) > 1) r++;
31 return r;
32}
33
34inline 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 Capensdc4ae862016-03-21 16:59:03 -040047using sw::clamp;
48using sw::clamp01;
Nicolas Capensdbf6fc82014-10-23 13:33:20 -040049
50template<const int n>
51inline 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 Capens11157822014-10-23 23:00:29 -040070#endif // LIBGLES_CM_MATHUTIL_H_