2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/public-api/math/math-utils.h>
25 #include <dali/public-api/common/constants.h>
31 unsigned int NextPowerOfTwo(unsigned int i)
33 DALI_ASSERT_DEBUG(i <= 1U << (sizeof(unsigned) * 8 - 1) && "Return type cannot represent the next power of two greater than the argument.");
49 bool IsPowerOfTwo(unsigned int i)
51 return (i != 0) && ((i & (i - 1)) == 0);
54 float GetRangedEpsilon(float a, float b)
56 float abs_f = std::max(fabsf(a), fabsf(b));
57 int abs_i = (int) abs_f;
59 float epsilon = Math::MACHINE_EPSILON_10000;
62 return Math::MACHINE_EPSILON_0;
66 return Math::MACHINE_EPSILON_1;
70 return Math::MACHINE_EPSILON_10;
74 return Math::MACHINE_EPSILON_100;
76 else if (abs_i < 2000)
78 return Math::MACHINE_EPSILON_1000;
83 // TODO: Change this to use #pragma GCC diagnostic push / pop when the compiler is updated to 4.6.0+
84 #pragma GCC diagnostic ignored "-Wfloat-equal"
85 bool EqualsZero( float value )
89 #pragma GCC diagnostic error "-Wfloat-equal"
91 bool Equals( float a, float b )
93 return ( fabsf( a - b ) <= GetRangedEpsilon( a, b ) );
96 bool Equals( float a, float b, float epsilon )
98 return ( fabsf( a - b ) <= epsilon );
101 float Round(float value, int pos)
104 temp = value * powf( 10, pos );
105 temp = floorf( temp + 0.5 );
106 temp *= powf( 10, -pos );
110 float WrapInDomain(float x, float start, float end)
112 float domain = end - start;
115 if(fabsf(domain) > Math::MACHINE_EPSILON_1)
117 return start + (x - floorf(x / domain) * domain);
123 float ShortestDistanceInDomain(float a, float b, float start, float end)
126 float size = end-start;
131 // +ve vector, let's try perspective 1 domain to the right,
132 // and see if closer.
133 float aRight = a+size;
134 if( aRight-b < vect )
141 // -ve vector, let's try perspective 1 domain to the left,
142 // and see if closer.
143 float aLeft = a-size;