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