1 #ifndef __DALI_COMPILE_TIME_MATH_H__
2 #define __DALI_COMPILE_TIME_MATH_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <dali/public-api/common/dali-common.h>
31 * @addtogroup dali_core_math
36 * @brief Compile time template to calculate base to the power of N.
38 * Note! values need to be compile time constants
39 * Usage: <code>Power< 10, 2 >::value; // value=100</code>
40 * @param mantissa to raise to exponent
41 * @param N exponent to use for mantissa
43 template< size_t mantissa, size_t exponent >
46 enum { value = mantissa * Power< mantissa, exponent - 1 >::value };
50 * @brief Compile time template to calculate base to the power of N.
52 * Specialisation for power of 1
53 * @param mantissa to raise to exponent
55 template< size_t mantissa >
56 struct Power< mantissa, 1 >
58 enum { value = mantissa };
62 * @brief Compile time template to calculate base to the power of N.
64 * Specialisation for power of 0
65 * @param mantissa to raise to exponent
67 template< size_t mantissa >
68 struct Power< mantissa, 0 >
74 * @brief Compile time template to calculate base logarithm of N.
76 * Note! values need to be compile time constants
77 * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
78 * @param number for which to calculate the logarithm
79 * @param base logarithm to calculate
81 template< size_t number, size_t base = 2 >
84 enum { value = 1 + Log< number / base, base >::value };
88 * @brief Compile time template to calculate base logarithm of N.
90 * Specialisation for logarithm of 1
91 * @param base logarithm to calculate
93 template< size_t base >
100 * @brief Compile time template to calculate base logarithm of N.
102 * Specialisation for logarithm of 0
103 * @param base logarithm to calculate
105 template< size_t base >
106 struct Log< 0, base >
113 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
115 * Note! value needs to be compile time constant
116 * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
117 * @param N the number for which to calculate the machine epsilon
122 // take log10 of the number to get to the nearest power of 10 number and divide that by 10
123 // template recursion will take care of the rest
125 static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
127 static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
132 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
134 * Specialisation for epsilon of 1
140 static constexpr float value = FLT_EPSILON;
142 static const float value = FLT_EPSILON;
147 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
149 * Specialisation for epsilon of 0
155 static constexpr float value = FLT_MIN;
157 static const float value = FLT_MIN;
166 #endif // __DALI_COMPILE_TIME_MATH_H__