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.
39 * @tparam mantissa to raise to exponent
40 * @tparam exponent to use for mantissa
41 * @note values need to be compile time constants
42 * Usage: <code>Power< 10, 2 >::value; // value=100</code>
44 template< size_t mantissa, size_t exponent >
47 enum { value = mantissa * Power< mantissa, exponent - 1 >::value };
51 * @brief Compile time template to calculate base to the power of N.
53 * Specialisation for power of 1
55 * @tparam mantissa to raise to exponent
57 template< size_t mantissa >
58 struct Power< mantissa, 1 >
60 enum { value = mantissa };
64 * @brief Compile time template to calculate base to the power of N.
66 * Specialisation for power of 0
68 * @tparam mantissa to raise to exponent
70 template< size_t mantissa >
71 struct Power< mantissa, 0 >
77 * @brief Compile time template to calculate base logarithm of N.
80 * @tparam number for which to calculate the logarithm
81 * @tparam base logarithm to calculate
82 * @note values need to be compile time constants
83 * Usage: <code>Log< 100, 10 >::value; value equals 2</code>
85 template< size_t number, size_t base = 2 >
88 enum { value = 1 + Log< number / base, base >::value };
92 * @brief Compile time template to calculate base logarithm of N.
94 * Specialisation for logarithm of 1
96 * @tparam base logarithm to calculate
98 template< size_t base >
105 * @brief Compile time template to calculate base logarithm of N.
107 * Specialisation for logarithm of 0
109 * @tparam base logarithm to calculate
111 template< size_t base >
112 struct Log< 0, base >
119 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
122 * @tparam N the number for which to calculate the machine epsilon
123 * @note value needs to be compile time constant
124 * Usage: <code>Epsilon<1000>::value; value equals 0.000119209</code>
129 // take log10 of the number to get to the nearest power of 10 number and divide that by 10
130 // template recursion will take care of the rest
132 static constexpr float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
134 static const float value = 10.0f * Epsilon< Power< 10, Log< N, 10 >::value >::value / 10 >::value;
139 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
141 * Specialisation for epsilon of 1
148 static constexpr float value = FLT_EPSILON;
150 static const float value = FLT_EPSILON;
155 * @brief Compile time template to calculate the machine epsilon for a given floating point number.
157 * Specialisation for epsilon of 0
164 static constexpr float value = FLT_MIN;
166 static const float value = FLT_MIN;
175 #endif // __DALI_COMPILE_TIME_MATH_H__