1 | #ifndef __DARKRL__MATH_HPP__ |
---|---|
2 | #define __DARKRL__MATH_HPP__ |
3 | |
4 | #include <algorithm> |
5 | #include <cmath> |
6 | #include <stdint.h> |
7 | |
8 | #include "ForceInline.hpp" |
9 | |
10 | template<typename T> |
11 | static etcpak_force_inline T AlignPOT( T val ) |
12 | { |
13 | if( val == 0 ) return 1; |
14 | val--; |
15 | for( unsigned int i=1; i<sizeof( T ) * 8; i <<= 1 ) |
16 | { |
17 | val |= val >> i; |
18 | } |
19 | return val + 1; |
20 | } |
21 | |
22 | static etcpak_force_inline int CountSetBits( uint32_t val ) |
23 | { |
24 | val -= ( val >> 1 ) & 0x55555555; |
25 | val = ( ( val >> 2 ) & 0x33333333 ) + ( val & 0x33333333 ); |
26 | val = ( ( val >> 4 ) + val ) & 0x0f0f0f0f; |
27 | val += val >> 8; |
28 | val += val >> 16; |
29 | return val & 0x0000003f; |
30 | } |
31 | |
32 | static etcpak_force_inline int CountLeadingZeros( uint32_t val ) |
33 | { |
34 | val |= val >> 1; |
35 | val |= val >> 2; |
36 | val |= val >> 4; |
37 | val |= val >> 8; |
38 | val |= val >> 16; |
39 | return 32 - CountSetBits( val ); |
40 | } |
41 | |
42 | static etcpak_force_inline float sRGB2linear( float v ) |
43 | { |
44 | const float a = 0.055f; |
45 | if( v <= 0.04045f ) |
46 | { |
47 | return v / 12.92f; |
48 | } |
49 | else |
50 | { |
51 | return pow( ( v + a ) / ( 1 + a ), 2.4f ); |
52 | } |
53 | } |
54 | |
55 | static etcpak_force_inline float linear2sRGB( float v ) |
56 | { |
57 | const float a = 0.055f; |
58 | if( v <= 0.0031308f ) |
59 | { |
60 | return 12.92f * v; |
61 | } |
62 | else |
63 | { |
64 | return ( 1 + a ) * pow( v, 1/2.4f ) - a; |
65 | } |
66 | } |
67 | |
68 | template<class T> |
69 | static etcpak_force_inline T SmoothStep( T x ) |
70 | { |
71 | return x*x*(3-2*x); |
72 | } |
73 | |
74 | static etcpak_force_inline uint8_t clampu8( int32_t val ) |
75 | { |
76 | if( ( val & ~0xFF ) == 0 ) return val; |
77 | return ( ( ~val ) >> 31 ) & 0xFF; |
78 | } |
79 | |
80 | template<class T> |
81 | static etcpak_force_inline T sq( T val ) |
82 | { |
83 | return val * val; |
84 | } |
85 | |
86 | static etcpak_force_inline int mul8bit( int a, int b ) |
87 | { |
88 | int t = a*b + 128; |
89 | return ( t + ( t >> 8 ) ) >> 8; |
90 | } |
91 | |
92 | #endif |
93 |