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