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