Updated CAPI documentation style.
[platform/core/uifw/dali-core.git] / capi / 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) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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  * @addtogroup CAPI_DALI_MATH_MODULE
22  * @{
23  */
24
25 // EXTERNAL INCLUDES
26 #include <stdlib.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/public-api/common/dali-common.h>
30
31 namespace Dali DALI_IMPORT_API
32 {
33 /**
34  * @brief Compile time template to calculate base to the power of N.
35  *
36  * Note! values need to be compile time constants
37  * Usage: <code>Power< 10, 2 >::value; // value=100</code>
38  * @param mantissa to raise to exponent
39  * @param N exponent to use for mantissa
40  */
41 template< size_t mantissa, size_t exponent >
42 struct Power
43 {
44   enum { value = mantissa * Power< mantissa, exponent - 1 >::value };
45 };
46
47 /**
48  * @brief Compile time template to calculate base to the power of N.
49  *
50  * Specialisation for power of 1
51  * @param mantissa to raise to exponent
52  */
53 template< size_t mantissa >
54 struct Power< mantissa, 1 >
55 {
56   enum { value = mantissa };
57 };
58
59 /**
60  * @brief Compile time template to calculate base to the power of N.
61  *
62  * Specialisation for power of 0
63  * @param mantissa to raise to exponent
64  */
65 template< size_t mantissa >
66 struct Power< mantissa, 0 >
67 {
68   enum { value = 1 };
69 };
70
71 /**
72  * @brief Compile time template to calculate base logarithm of N.
73  *
74  * Note! values need to be compile time constants
75  * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
76  * @param number for which to calculate the logarithm
77  * @param base logarithm to calculate
78  */
79 template< size_t number, size_t base = 2 >
80 struct Log
81 {
82   enum { value = 1 + Log< number / base, base >::value };
83 };
84
85 /**
86  * @brief Compile time template to calculate base logarithm of N.
87  *
88  * Specialisation for logarithm of 1
89  * @param base logarithm to calculate
90  */
91 template< size_t base >
92 struct Log< 1, base >
93 {
94   enum { value = 0 };
95 };
96
97 /**
98  * @brief Compile time template to calculate base logarithm of N.
99  *
100  * Specialisation for logarithm of 0
101  * @param base logarithm to calculate
102  */
103 template< size_t base >
104 struct Log< 0, base >
105 {
106   enum { value = 0 };
107 };
108
109
110 /**
111  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
112  *
113  * Note! value needs to be compile time constant
114  * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
115  * @param N the number for which to calculate the machine epsilon
116  */
117 template< size_t N >
118 struct Epsilon
119 {
120   // take log10 of the number to get to the nearest power of 10 number and divide that by 10
121   // template recursion will take care of the rest
122 #ifdef _CPP11
123   static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
124 #else
125   static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
126 #endif
127 };
128
129 /**
130  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
131  *
132  * Specialisation for epsilon of 1
133  * predefined value calculated on ARM Cortex A9 target
134  */
135 template<>
136 struct Epsilon< 1 >
137 {
138 #ifdef _CPP11
139   static constexpr float value = 1.19209e-07f;
140 #else
141   static const float value = 1.19209e-07f;
142 #endif
143 };
144
145 /**
146  * @brief Compile time template to calculate the machine epsilon for a given floating point number.
147  *
148  * Specialisation for epsilon of 0
149  * predefined value calculated on ARM Cortex A9 target
150  */
151 template<>
152 struct Epsilon< 0 >
153 {
154 #ifdef _CPP11
155   static constexpr float value = 1.4013e-45f;
156 #else
157   static const float value = 1.4013e-45f;
158 #endif
159 };
160
161 } // namespace Dali
162
163 /**
164  * @}
165  */
166 #endif // __DALI_COMPILE_TIME_MATH_H__