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
30 // GCC does something stupid with optimization on release builds if we try
31 // to assert in these functions
32 inline int round_int (double x)
34 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
35 assert(x < static_cast <double>(INT_MAX / 2) + 1.0);
36 const float round_to_nearest = 0.5f;
49 #if defined(__powerpc__) || defined(__ppc__)
50 i = floor(x + round_to_nearest);
51 #elif defined(__arm__)
52 __asm__ __volatile__ (
53 "vmov.F64 d1,%[rnd_val] \n\t" // Copy round_to_nearest into a working register
54 "vadd.F64 %P[value],%P[value],d1 \n\t" // Add round_to_nearest to value
55 "vcvt.S32.F64 %[result],%P[value] \n\t" // Truncate(round towards zero) and store the result
56 : [result] "=w"(i), [value] "+w"(x) // Outputs
57 : [rnd_val] "Dv" (round_to_nearest) // Inputs
60 __asm__ __volatile__ (
65 : "=m"(i) : "u"(round_to_nearest), "t"(x) : "st"
72 inline int truncate_int(double x)
74 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
75 assert(x < static_cast <double>(INT_MAX / 2) + 1.0);
77 #if !defined(__powerpc__) && !defined(__ppc__) && !defined(__arm__)
78 const float round_towards_m_i = -0.5f;
88 fadd round_towards_m_i
93 #if defined(__powerpc__) || defined(__ppc__) || defined(__arm__)
96 __asm__ __volatile__ (
102 : "=m"(i) : "u"(round_towards_m_i), "t"(x) : "st"
111 inline int64_t abs(int64_t a)
113 return (a < 0) ? -a : a;
118 // stupid hack to keep compiler from dropping these
119 // functions as unused
120 MathUtils::round_int(0.0);
121 MathUtils::truncate_int(0.0);
124 } // namespace MathUtils