[dali_2.3.40] Merge branch 'devel/master'
[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) 2020 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 Compiles time template to calculate base to the power of N.
37  *
38  * @SINCE_1_0.0
39  * @tparam mantissa to raise to exponent
40  * @tparam exponent to use for mantissa
41  * @note Values need to be compile time constants.
42  * Usage: <code>Power< 10, 2 >::value; // value=100</code>
43  */
44 template<size_t mantissa, size_t exponent>
45 struct Power
46 {
47   enum
48   {
49     value = mantissa * Power<mantissa, exponent - 1>::value
50   };
51 };
52
53 /**
54  * @brief Compiles time template to calculate base to the power of N.
55  *
56  * Specialization for power of 1.
57  * @SINCE_1_0.0
58  * @tparam mantissa to raise to exponent
59  */
60 template<size_t mantissa>
61 struct Power<mantissa, 1>
62 {
63   enum
64   {
65     value = mantissa
66   };
67 };
68
69 /**
70  * @brief Compiles time template to calculate base to the power of N.
71  *
72  * Specialization for power of 0.
73  * @SINCE_1_0.0
74  * @tparam mantissa to raise to exponent
75  */
76 template<size_t mantissa>
77 struct Power<mantissa, 0>
78 {
79   enum
80   {
81     value = 1
82   };
83 };
84
85 /**
86  * @brief Compiles time template to calculate base logarithm of N.
87  *
88  * @SINCE_1_0.0
89  * @tparam number for which to calculate the logarithm
90  * @tparam base logarithm to calculate
91  * @note Values need to be compile time constants.
92  * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
93  */
94 template<size_t number, size_t base = 2>
95 struct Log
96 {
97   enum
98   {
99     value = 1 + Log<number / base, base>::value
100   };
101 };
102
103 /**
104  * @brief Compiles time template to calculate base logarithm of N.
105  *
106  * Specialization for logarithm of 1.
107  * @SINCE_1_0.0
108  * @tparam base logarithm to calculate
109  */
110 template<size_t base>
111 struct Log<1, base>
112 {
113   enum
114   {
115     value = 0
116   };
117 };
118
119 /**
120  * @brief Compiles time template to calculate base logarithm of N.
121  *
122  * Specialization for logarithm of 0.
123  * @SINCE_1_0.0
124  * @tparam base logarithm to calculate
125  */
126 template<size_t base>
127 struct Log<0, base>
128 {
129   enum
130   {
131     value = 0
132   };
133 };
134
135 /**
136  * @brief Compiles time template to calculate the machine epsilon for a given floating point number.
137  *
138  * @SINCE_1_0.0
139  * @tparam N the number for which to calculate the machine epsilon
140  * @note Value needs to be compile time constant.
141  * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
142  */
143 template<size_t N>
144 struct Epsilon
145 {
146   // take log10 of the number to get to the nearest power of 10 number and divide that by 10
147   // template recursion will take care of the rest
148 #ifdef _CPP11
149   static constexpr float value = 10.0f * Epsilon<Power<10, Log<N, 10>::value>::value / 10>::value;
150 #else
151   static const float value = 10.0f * Epsilon<Power<10, Log<N, 10>::value>::value / 10>::value;
152 #endif
153 };
154
155 /**
156  * @brief Compiles time template to calculate the machine epsilon for a given floating point number.
157  *
158  * Specialization for epsilon of 1.
159  * @SINCE_1_0.0
160  */
161 template<>
162 struct Epsilon<1>
163 {
164 #ifdef _CPP11
165   static constexpr float value = FLT_EPSILON;
166 #else
167   static const float value = FLT_EPSILON;
168 #endif
169 };
170
171 /**
172  * @brief Compiles time template to calculate the machine epsilon for a given floating point number.
173  *
174  * Specialization for epsilon of 0.
175  * @SINCE_1_0.0
176  */
177 template<>
178 struct Epsilon<0>
179 {
180 #ifdef _CPP11
181   static constexpr float value = FLT_MIN;
182 #else
183   static const float value = FLT_MIN;
184 #endif
185 };
186
187 /**
188  * @}
189  */
190 } // namespace Dali
191
192 #endif // DALI_COMPILE_TIME_MATH_H