Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.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
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26
27 namespace Dali
28 {
29 /**
30  * @addtogroup dali_core_math
31  * @{
32  */
33
34 /**
35  * @brief Compile time template to calculate base to the power of N.
36  *
37  * @since_tizen 2.4
38  * @tparam mantissa to raise to exponent
39  * @tparam N exponent to use for mantissa
40  * @note values need to be compile time constants
41  * Usage: <code>Power< 10, 2 >::value; // value=100</code>
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  * @since_tizen 2.4
54  * @tparam mantissa to raise to exponent
55  */
56 template< size_t mantissa >
57 struct Power< mantissa, 1 >
58 {
59   enum { value = mantissa };
60 };
61
62 /**
63  * @brief Compile time template to calculate base to the power of N.
64  *
65  * Specialisation for power of 0
66  * @since_tizen 2.4
67  * @tparam mantissa to raise to exponent
68  */
69 template< size_t mantissa >
70 struct Power< mantissa, 0 >
71 {
72   enum { value = 1 };
73 };
74
75 /**
76  * @brief Compile time template to calculate base logarithm of N.
77  *
78  * @since_tizen 2.4
79  * @tparam number for which to calculate the logarithm
80  * @tparam base logarithm to calculate
81  * @note values need to be compile time constants
82  * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
83  */
84 template< size_t number, size_t base = 2 >
85 struct Log
86 {
87   enum { value = 1 + Log< number / base, base >::value };
88 };
89
90 /**
91  * @brief Compile time template to calculate base logarithm of N.
92  *
93  * Specialisation for logarithm of 1
94  * @since_tizen 2.4
95  * @tparam base logarithm to calculate
96  */
97 template< size_t base >
98 struct Log< 1, base >
99 {
100   enum { value = 0 };
101 };
102
103 /**
104  * @brief Compile time template to calculate base logarithm of N.
105  *
106  * Specialisation for logarithm of 0
107  * @since_tizen 2.4
108  * @tparam base logarithm to calculate
109  */
110 template< size_t base >
111 struct Log< 0, base >
112 {
113   enum { value = 0 };
114 };
115
116
117 /**
118  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
119  *
120  * @since_tizen 2.4
121  * @tparam N the number for which to calculate the machine epsilon
122  * @note value needs to be compile time constant
123  * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
124  */
125 template< size_t N >
126 struct Epsilon
127 {
128   // take log10 of the number to get to the nearest power of 10 number and divide that by 10
129   // template recursion will take care of the rest
130 #ifdef _CPP11
131   static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
132 #else
133   static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
134 #endif
135 };
136
137 /**
138  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
139  *
140  * Specialisation for epsilon of 1
141  * predefined value calculated on ARM Cortex A9 target
142  * @since_tizen 2.4
143  */
144 template<>
145 struct Epsilon< 1 >
146 {
147 #ifdef _CPP11
148   static constexpr float value = 1.19209e-07f;
149 #else
150   static const float value = 1.19209e-07f;
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  * predefined value calculated on ARM Cortex A9 target
159  * @since_tizen 2.4
160  */
161 template<>
162 struct Epsilon< 0 >
163 {
164 #ifdef _CPP11
165   static constexpr float value = 1.4013e-45f;
166 #else
167   static const float value = 1.4013e-45f;
168 #endif
169 };
170
171 /**
172  * @}
173  */
174 } // namespace Dali
175
176 #endif // __DALI_COMPILE_TIME_MATH_H__