Emscripten workarounds and llvm syntax fixes
[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_FRAMEWORK
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  * Compile time template to calculate base to the power of N
35  * Note! values need to be compile time constants
36  * Usage: <code>Power< 10, 2 >::value; // value=100</code>
37  * @param mantissa to raise to exponent
38  * @param N exponent to use for mantissa
39  */
40 template< size_t mantissa, size_t exponent >
41 struct Power
42 {
43   enum { value = mantissa * Power< mantissa, exponent - 1 >::value };
44 };
45
46 /**
47  * Compile time template to calculate base to the power of N
48  * Specialisation for power of 1
49  * @param mantissa to raise to exponent
50  */
51 template< size_t mantissa >
52 struct Power< mantissa, 1 >
53 {
54   enum { value = mantissa };
55 };
56
57 /**
58  * Compile time template to calculate base to the power of N
59  * Specialisation for power of 0
60  * @param mantissa to raise to exponent
61  */
62 template< size_t mantissa >
63 struct Power< mantissa, 0 >
64 {
65   enum { value = 1 };
66 };
67
68 /**
69  * Compile time template to calculate base logarithm of N
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  * Compile time template to calculate base logarithm of N
83  * Specialisation for logarithm of 1
84  * @param base logarithm to calculate
85  */
86 template< size_t base >
87 struct Log< 1, base >
88 {
89   enum { value = 0 };
90 };
91
92 /**
93  * Compile time template to calculate base logarithm of N
94  * Specialisation for logarithm of 0
95  * @param base logarithm to calculate
96  */
97 template< size_t base >
98 struct Log< 0, base >
99 {
100   enum { value = 0 };
101 };
102
103
104 /**
105  * Compile time template to calculate the machine epsilon for a given floating point number
106  * Note! value needs to be compile time constant
107  * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
108  * @param N the number for which to calculate the machine epsilon
109  */
110 template< size_t N >
111 struct Epsilon
112 {
113   // take log10 of the number to get to the nearest power of 10 number and divide that by 10
114   // template recursion will take care of the rest
115 #ifdef _CPP11
116   static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
117 #else
118   static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
119 #endif
120 };
121
122 /**
123  * Compile time template to calculate the machine epsilon for a given floating point number
124  * Specialisation for epsilon of 1
125  * predefined value calculated on ARM Cortex A9 target
126  */
127 template<>
128 struct Epsilon< 1 >
129 {
130 #ifdef _CPP11
131   static constexpr float value = 1.19209e-07f;
132 #else
133   static const float value = 1.19209e-07f;
134 #endif
135 };
136
137 /**
138  * Compile time template to calculate the machine epsilon for a given floating point number
139  * Specialisation for epsilon of 0
140  * predefined value calculated on ARM Cortex A9 target
141  */
142 template<>
143 struct Epsilon< 0 >
144 {
145 #ifdef _CPP11
146   static constexpr float value = 1.4013e-45f;
147 #else
148   static const float value = 1.4013e-45f;
149 #endif
150 };
151
152 } // namespace Dali
153
154 /**
155  * @}
156  */
157 #endif // __DALI_COMPILE_TIME_MATH_H__