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
29 #include <emmintrin.h>
32 // use real compiler defines in here as we want to
33 // avoid including system.h or other magic includes.
34 // use 'gcc -dM -E - < /dev/null' or similar to find them.
36 #if defined(__ppc__) || \
37 defined(__powerpc__) || \
38 (defined(__APPLE__) && defined(__arm__) && defined(__llvm__))
39 #define DISABLE_MATHUTILS_ASM_ROUND_INT
42 #if defined(__ppc__) || \
43 defined(__powerpc__) || \
44 (defined(__APPLE__) && defined(__llvm__))
45 #define DISABLE_MATHUTILS_ASM_TRUNCATE_INT
48 /*! \brief Math utility class.
49 Note that the test() routine should return true for all implementations
51 See http://ldesoras.free.fr/doc/articles/rounding_en.pdf for an explanation
52 of the technique used on x86.
56 // GCC does something stupid with optimization on release builds if we try
57 // to assert in these functions
59 /*! \brief Round to nearest integer.
60 This routine does fast rounding to the nearest integer.
61 In the case (k + 0.5 for any integer k) we round up to k+1, and in all other
62 instances we should return the nearest integer.
63 Thus, { -1.5, -0.5, 0.5, 1.5 } is rounded to { -1, 0, 1, 2 }.
64 It preserves the property that round(k) - round(k-1) = 1 for all doubles k.
66 Make sure MathUtils::test() returns true for each implementation.
67 \sa truncate_int, test
69 inline int round_int(double x)
71 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
72 assert(x < static_cast<double>(INT_MAX / 2) + 1.0);
73 const float round_to_nearest = 0.5f;
76 #if defined(DISABLE_MATHUTILS_ASM_ROUND_INT)
77 i = floor(x + round_to_nearest);
79 #elif defined(__arm__)
80 // From 'ARM-v7-M Architecture Reference Manual' page A7-569:
81 // "The floating-point to integer operation (vcvt) [normally] uses the Round towards Zero rounding mode"
82 // Because of this...we must use some less-than-straightforward logic to perform this operation without
83 // changing the rounding mode flags
85 /* The assembly below implements the following logic:
90 int_val = trunc(x+inc);
97 __asm__ __volatile__ (
98 #if defined(__ARM_PCS_VFP)
99 "fconstd d1,#%G[rnd_val] \n\t" // Copy round_to_nearest into a working register (d1 = 0.5)
101 "vmov.F64 d1,%[rnd_val] \n\t"
103 "fcmpezd %P[value] \n\t" // Check value against zero (value == 0?)
104 "fmstat \n\t" // Copy the floating-point status flags into the general-purpose status flags
106 "vnegmi.F64 d1, d1 \n\t" // if N-flag is set, negate round_to_nearest (if (value < 0) d1 = -1 * d1)
107 "vadd.F64 d1,%P[value],d1 \n\t" // Add round_to_nearest to value, store result in working register (d1 += value)
108 "vcvt.S32.F64 s3,d1 \n\t" // Truncate(round towards zero) (s3 = (int)d1)
109 "vmov %[result],s3 \n\t" // Store the integer result in a general-purpose register (result = s3)
110 "vcvt.F64.S32 d1,s3 \n\t" // Convert back to floating-point (d1 = (double)s3)
111 "vsub.F64 d1,%P[value],d1 \n\t" // Calculate the error (d1 = value - d1)
112 #if defined(__ARM_PCS_VFP)
113 "fconstd d2,#%G[rnd_val] \n\t" // d2 = 0.5;
115 "vmov.F64 d2,%[rnd_val] \n\t"
117 "fcmped d1, d2 \n\t" // (d1 == 0.5?)
118 "fmstat \n\t" // Copy the floating-point status flags into the general-purpose status flags
120 "addeq %[result],#1 \n\t" // (if (d1 == d2) result++;)
121 : [result] "=r"(i) // Outputs
122 : [rnd_val] "Dv" (round_to_nearest), [value] "w"(x) // Inputs
123 : "d1", "d2", "s3" // Clobbers
126 #elif defined(__SSE2__)
127 const float round_dn_to_nearest = 0.4999999f;
128 i = (x > 0) ? _mm_cvttsd_si32(_mm_set_sd(x + round_to_nearest)) : _mm_cvttsd_si32(_mm_set_sd(x - round_dn_to_nearest));
130 #elif defined(_WIN32)
135 fadd round_to_nearest
141 __asm__ __volatile__ (
146 : "=m"(i) : "u"(round_to_nearest), "t"(x) : "st"
154 /*! \brief Truncate to nearest integer.
155 This routine does fast truncation to an integer.
156 It should simply drop the fractional portion of the floating point number.
158 Make sure MathUtils::test() returns true for each implementation.
161 inline int truncate_int(double x)
163 assert(x > static_cast<double>(INT_MIN / 2) - 1.0);
164 assert(x < static_cast<double>(INT_MAX / 2) + 1.0);
167 #if defined(DISABLE_MATHUTILS_ASM_TRUNCATE_INT)
170 #elif defined(__arm__)
171 __asm__ __volatile__ (
172 "vcvt.S32.F64 %[result],%P[value] \n\t" // Truncate(round towards zero) and store the result
173 : [result] "=w"(i) // Outputs
174 : [value] "w"(x) // Inputs
178 #elif defined(_WIN32)
179 const float round_towards_m_i = -0.5f;
185 fadd round_towards_m_i
191 const float round_towards_m_i = -0.5f;
192 __asm__ __volatile__ (
198 : "=m"(i) : "u"(round_towards_m_i), "t"(x) : "st"
206 inline int64_t abs(int64_t a)
208 return (a < 0) ? -a : a;
211 inline unsigned bitcount(unsigned v)
215 v &= v - 1; // clear the least significant bit set
221 // stupid hack to keep compiler from dropping these
222 // functions as unused
223 MathUtils::round_int(0.0);
224 MathUtils::truncate_int(0.0);
229 /*! \brief test routine for round_int and truncate_int
230 Must return true on all platforms.
234 for (int i = -8; i < 8; ++i)
237 int r = (i < 0) ? (i - 1) / 4 : (i + 2) / 4;
239 if (round_int(d) != r || truncate_int(d) != t)
245 } // namespace MathUtils