Support for ASTC compressed textures wrapped in KTX files
[platform/core/uifw/dali-core.git] / dali / public-api / math / compile-time-math.h
1 #ifndef __DALI_COMPILE_TIME_MATH_H__
2 #define __DALI_COMPILE_TIME_MATH_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <stdlib.h>
23 #include <cfloat>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_math
32  * @{
33  */
34
35 /**
36  * @brief Compile time template to calculate base to the power of N.
37  *
38  * Note! values need to be compile time constants
39  * Usage: <code>Power< 10, 2 >::value; // value=100</code>
40  * @param mantissa to raise to exponent
41  * @param N exponent to use for mantissa
42  */
43 template< size_t mantissa, size_t exponent >
44 struct Power
45 {
46   enum { value = mantissa * Power< mantissa, exponent - 1 >::value };
47 };
48
49 /**
50  * @brief Compile time template to calculate base to the power of N.
51  *
52  * Specialisation for power of 1
53  * @param mantissa to raise to exponent
54  */
55 template< size_t mantissa >
56 struct Power< mantissa, 1 >
57 {
58   enum { value = mantissa };
59 };
60
61 /**
62  * @brief Compile time template to calculate base to the power of N.
63  *
64  * Specialisation for power of 0
65  * @param mantissa to raise to exponent
66  */
67 template< size_t mantissa >
68 struct Power< mantissa, 0 >
69 {
70   enum { value = 1 };
71 };
72
73 /**
74  * @brief Compile time template to calculate base logarithm of N.
75  *
76  * Note! values need to be compile time constants
77  * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
78  * @param number for which to calculate the logarithm
79  * @param base logarithm to calculate
80  */
81 template< size_t number, size_t base = 2 >
82 struct Log
83 {
84   enum { value = 1 + Log< number / base, base >::value };
85 };
86
87 /**
88  * @brief Compile time template to calculate base logarithm of N.
89  *
90  * Specialisation for logarithm of 1
91  * @param base logarithm to calculate
92  */
93 template< size_t base >
94 struct Log< 1, base >
95 {
96   enum { value = 0 };
97 };
98
99 /**
100  * @brief Compile time template to calculate base logarithm of N.
101  *
102  * Specialisation for logarithm of 0
103  * @param base logarithm to calculate
104  */
105 template< size_t base >
106 struct Log< 0, base >
107 {
108   enum { value = 0 };
109 };
110
111
112 /**
113  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
114  *
115  * Note! value needs to be compile time constant
116  * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
117  * @param N the number for which to calculate the machine epsilon
118  */
119 template< size_t N >
120 struct Epsilon
121 {
122   // take log10 of the number to get to the nearest power of 10 number and divide that by 10
123   // template recursion will take care of the rest
124 #ifdef _CPP11
125   static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
126 #else
127   static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
128 #endif
129 };
130
131 /**
132  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
133  *
134  * Specialisation for epsilon of 1
135  */
136 template<>
137 struct Epsilon< 1 >
138 {
139 #ifdef _CPP11
140   static constexpr float value = FLT_EPSILON;
141 #else
142   static const float value = FLT_EPSILON;
143 #endif
144 };
145
146 /**
147  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
148  *
149  * Specialisation for epsilon of 0
150  */
151 template<>
152 struct Epsilon< 0 >
153 {
154 #ifdef _CPP11
155   static constexpr float value = FLT_MIN;
156 #else
157   static const float value = FLT_MIN;
158 #endif
159 };
160
161 /**
162  * @}
163  */
164 } // namespace Dali
165
166 #endif // __DALI_COMPILE_TIME_MATH_H__