3 * Copyright (C) 2005-2008 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * http://www.gnu.org/copyleft/gpl.html
28 /*! \brief Math utility class.
29 Note that the test() routine should return true for all implementations
33 // GCC does something stupid with optimization on release builds if we try
34 // to assert in these functions
36 /*! \brief Round to nearest integer.
37 This routine does fast rounding to the nearest integer.
38 In the case (k + 0.5 for any integer k) we round up to k+1, and in all other
39 instances we should return the nearest integer.
40 Thus, { -1.5, -0.5, 0.5, 1.5 } is rounded to { -1, 0, 1, 2 }.
41 It preserves the property that round(k) - round(k-1) = 1 for all doubles k.
43 Make sure MathUtils::test() returns true for each implementation.
44 \sa truncate_int, test
46 inline int round_int (double x)
48 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
49 assert(x < static_cast <double>(INT_MAX / 2) + 1.0);
50 const float round_to_nearest = 0.5f;
63 #if defined(__powerpc__) || defined(__ppc__)
64 i = floor(x + round_to_nearest);
65 #elif defined(__arm__)
66 //BIG FIXME here (still has issues with rounding -0.5 to zero and not -1)
67 //the asm codes below do the following - trunc(x+0.5)
68 //this isn't correct for negativ x - values - for example
69 //-1 gets rounded to zero because trunc(-1+0.5) == 0
70 //this is a dirty hack until someone fixes this propably in asm
71 //i've created a trac ticket for this #11767
72 //this hacks decrements the x by 1 if it is negativ
73 // so for -1 it would be trunc(-2+0.5) - which would be correct -1 then ...
76 __asm__ __volatile__ (
77 "vmov.F64 d1,%[rnd_val] \n\t" // Copy round_to_nearest into a working register
78 "vadd.F64 %P[value],%P[value],d1 \n\t" // Add round_to_nearest to value
79 "vcvt.S32.F64 %[result],%P[value] \n\t" // Truncate(round towards zero) and store the result
80 : [result] "=w"(i), [value] "+w"(x) // Outputs
81 : [rnd_val] "Dv" (round_to_nearest) // Inputs
84 __asm__ __volatile__ (
89 : "=m"(i) : "u"(round_to_nearest), "t"(x) : "st"
96 /*! \brief Truncate to nearest integer.
97 This routine does fast truncation to an integer.
98 It should simply drop the fractional portion of the floating point number.
100 Make sure MathUtils::test() returns true for each implementation.
103 inline int truncate_int(double x)
105 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
106 assert(x < static_cast <double>(INT_MAX / 2) + 1.0);
108 #if !defined(__powerpc__) && !defined(__ppc__) && !defined(__arm__)
109 const float round_towards_m_i = -0.5f;
119 fadd round_towards_m_i
124 #if defined(__powerpc__) || defined(__ppc__) || defined(__arm__)
127 __asm__ __volatile__ (
133 : "=m"(i) : "u"(round_towards_m_i), "t"(x) : "st"
142 inline int64_t abs(int64_t a)
144 return (a < 0) ? -a : a;
149 // stupid hack to keep compiler from dropping these
150 // functions as unused
151 MathUtils::round_int(0.0);
152 MathUtils::truncate_int(0.0);
157 /*! \brief test routine for round_int and truncate_int
158 Must return true on all platforms.
162 for (int i = -8; i < 8; ++i)
165 int r = (i < 0) ? (i - 1) / 4 : (i + 2) / 4;
167 if (round_int(d) != r || truncate_int(d) != t)
173 } // namespace MathUtils