OSDN Git Service

Implement a Radiance prototype.
[android-x86/external-swiftshader.git] / src / Radiance / libRAD / mathutil.h
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
14 #ifndef LIBGLESV2_MATHUTIL_H_
15 #define LIBGLESV2_MATHUTIL_H_
16
17 #include "common/debug.h"
18
19 #include <math.h>
20
21 namespace es2
22 {
23 inline bool isPow2(int x)
24 {
25     return (x & (x - 1)) == 0 && (x != 0);
26 }
27
28 inline int log2(int x)
29 {
30     int r = 0;
31     while((x >> r) > 1) r++;
32     return r;
33 }
34
35 inline unsigned int ceilPow2(unsigned int x)
36 {
37     if(x != 0) x--;
38     x |= x >> 1;
39     x |= x >> 2;
40     x |= x >> 4;
41     x |= x >> 8;
42     x |= x >> 16;
43     x++;
44
45     return x;
46 }
47
48 template<typename T, typename MIN, typename MAX>
49 inline T clamp(T x, MIN min, MAX max)
50 {
51     return x < min ? min : (x > max ? max : x);
52 }
53
54 inline float clamp01(float x)
55 {
56     return clamp(x, 0.0f, 1.0f);
57 }
58
59 template<const int n>
60 inline unsigned int unorm(float x)
61 {
62     const unsigned int max = 0xFFFFFFFF >> (32 - n);
63
64     if(x > 1)
65     {
66         return max;
67     }
68     else if(x < 0)
69     {
70         return 0;
71     }
72     else
73     {
74         return (unsigned int)(max * x + 0.5f);
75     }
76 }
77 }
78
79 #endif   // LIBGLESV2_MATHUTIL_H_