45 #ifndef OPENCV_CORE_FAST_MATH_HPP
46 #define OPENCV_CORE_FAST_MATH_HPP
48 #include "opencv2/core/cvdef.h"
61 # include <fastmath.h>
67 #if defined(__CUDACC__)
70 #if ((defined _MSC_VER && defined _M_X64) \
71 || (defined __GNUC__ && defined __SSE2__)) \
72 && !defined(OPENCV_SKIP_INCLUDE_EMMINTRIN_H)
73 #include <emmintrin.h>
76 #if defined __PPC64__ && defined __GNUC__ && defined _ARCH_PWR8 \
77 && !defined(OPENCV_SKIP_INCLUDE_ALTIVEC_H)
84 #if defined(CV_INLINE_ROUND_FLT)
87 #elif defined __GNUC__ && defined __arm__ && (defined __ARM_PCS_VFP || defined __ARM_VFPV3__ || defined __ARM_NEON) && !defined __SOFTFP__
89 #define ARM_ROUND(_value, _asm_string) \
93 __asm__(_asm_string : [res] "=r" (res), [temp] "=w" (temp) : [value] "w" (_value)); \
97 #define CV_INLINE_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %[value] \n vmov %[res], %[temp]")
99 #define CV_INLINE_ROUND_DBL(value) ARM_ROUND(value, "vcvtr.s32.f64 %[temp], %P[value] \n vmov %[res], %[temp]")
102 #define CV_INLINE_ROUND_FLT(value) ARM_ROUND(value, "vcvtr.s32.f32 %[temp], %[value]\n vmov %[res], %[temp]")
103 #elif defined __PPC64__ && defined __GNUC__ && defined _ARCH_PWR8
105 #define CV_INLINE_ROUND_DBL(value) \
108 __asm__( "fctiw %[temp],%[in]\n\tmfvsrwz %[out],%[temp]\n\t" : [out] "=r" (out), [temp] "=d" (temp) : [in] "d" ((double)(value)) : ); \
112 #define CV_INLINE_ROUND_FLT(value) CV_INLINE_ROUND_DBL(value)
115 #ifdef CV_INLINE_ISINF_FLT
118 #elif defined __PPC64__ && defined _ARCH_PWR9 && defined(scalar_test_data_class)
119 #define CV_INLINE_ISINF_DBL(value) return scalar_test_data_class(value, 0x30);
120 #define CV_INLINE_ISINF_FLT(value) CV_INLINE_ISINF_DBL(value)
123 #ifdef CV_INLINE_ISNAN_FLT
126 #elif defined __PPC64__ && defined _ARCH_PWR9 && defined(scalar_test_data_class)
127 #define CV_INLINE_ISNAN_DBL(value) return scalar_test_data_class(value, 0x40);
128 #define CV_INLINE_ISNAN_FLT(value) CV_INLINE_ISNAN_DBL(value)
131 #if !defined(OPENCV_USE_FASTMATH_BUILTINS) \
133 defined(__x86_64__) || defined(__i686__) \
134 || defined(__arm__) \
135 || defined(__PPC64__) \
139 #define OPENCV_USE_FASTMATH_BUILTINS 1
147 #elif defined(OPENCV_USE_FASTMATH_BUILTINS) && OPENCV_USE_FASTMATH_BUILTINS
148 #if defined(__clang__)
149 #define CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
150 #if !defined(CV_INLINE_ISNAN_DBL) && __has_builtin(__builtin_isnan)
151 #define CV_INLINE_ISNAN_DBL(value) return __builtin_isnan(value);
153 #if !defined(CV_INLINE_ISNAN_FLT) && __has_builtin(__builtin_isnan)
154 #define CV_INLINE_ISNAN_FLT(value) return __builtin_isnan(value);
156 #if !defined(CV_INLINE_ISINF_DBL) && __has_builtin(__builtin_isinf)
157 #define CV_INLINE_ISINF_DBL(value) return __builtin_isinf(value);
159 #if !defined(CV_INLINE_ISINF_FLT) && __has_builtin(__builtin_isinf)
160 #define CV_INLINE_ISINF_FLT(value) return __builtin_isinf(value);
162 #elif defined(__GNUC__)
163 #define CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS
164 #if !defined(CV_INLINE_ISNAN_DBL)
165 #define CV_INLINE_ISNAN_DBL(value) return __builtin_isnan(value);
167 #if !defined(CV_INLINE_ISNAN_FLT)
168 #define CV_INLINE_ISNAN_FLT(value) return __builtin_isnanf(value);
170 #if !defined(CV_INLINE_ISINF_DBL)
171 #define CV_INLINE_ISINF_DBL(value) return __builtin_isinf(value);
173 #if !defined(CV_INLINE_ISINF_FLT)
174 #define CV_INLINE_ISINF_FLT(value) return __builtin_isinff(value);
176 #elif defined(_MSC_VER)
177 #if !defined(CV_INLINE_ISNAN_DBL)
178 #define CV_INLINE_ISNAN_DBL(value) return isnan(value);
180 #if !defined(CV_INLINE_ISNAN_FLT)
181 #define CV_INLINE_ISNAN_FLT(value) return isnan(value);
183 #if !defined(CV_INLINE_ISINF_DBL)
184 #define CV_INLINE_ISINF_DBL(value) return isinf(value);
186 #if !defined(CV_INLINE_ISINF_FLT)
187 #define CV_INLINE_ISINF_FLT(value) return isinf(value);
202 #if defined CV_INLINE_ROUND_DBL
203 CV_INLINE_ROUND_DBL(
value);
204 #elif ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __SSE2__)) && !defined(__CUDACC__)
205 __m128d t = _mm_set_sd(
value );
206 return _mm_cvtsd_si32(t);
207 #elif defined _MSC_VER && defined _M_IX86
215 #elif defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
216 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
217 return (
int)__builtin_lrint(
value);
219 return (
int)lrint(
value);
233 #if defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
234 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
235 return (
int)__builtin_floor(
value);
236 #elif defined __loongarch64
239 __asm__ (
"ftintrm.l.d %[tmp], %[in] \n\t"
240 "movfr2gr.d %[i], %[tmp] \n\t"
241 : [i]
"=r" (i), [tmp]
"=f" (tmp)
247 return i - (i >
value);
260 #if defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
261 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
262 return (
int)__builtin_ceil(
value);
263 #elif defined __loongarch64
266 __asm__ (
"ftintrp.l.d %[tmp], %[in] \n\t"
267 "movfr2gr.d %[i], %[tmp] \n\t"
268 : [i]
"=r" (i), [tmp]
"=f" (tmp)
274 return i + (i <
value);
286 #if defined CV_INLINE_ISNAN_DBL
287 CV_INLINE_ISNAN_DBL(
value);
291 return ((
unsigned)(ieee754.
u >> 32) & 0x7fffffff) +
292 ((unsigned)ieee754.
u != 0) > 0x7ff00000;
304 #if defined CV_INLINE_ISINF_DBL
305 CV_INLINE_ISINF_DBL(
value);
306 #elif defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__PPC64__) || defined(__loongarch64)
309 return (ieee754.
u & 0x7fffffffffffffff) ==
314 return ((
unsigned)(ieee754.
u >> 32) & 0x7fffffff) == 0x7ff00000 &&
315 (unsigned)ieee754.
u == 0;
324 #if defined CV_INLINE_ROUND_FLT
325 CV_INLINE_ROUND_FLT(
value);
326 #elif ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __SSE2__)) && !defined(__CUDACC__)
327 __m128 t = _mm_set_ss(
value );
328 return _mm_cvtss_si32(t);
329 #elif defined _MSC_VER && defined _M_IX86
337 #elif defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
338 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
339 return (
int)__builtin_lrintf(
value);
341 return (
int)lrintf(
value);
354 #if defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
355 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
356 return (
int)__builtin_floorf(
value);
357 #elif defined __loongarch__
360 __asm__ (
"ftintrm.w.s %[tmp], %[in] \n\t"
361 "movfr2gr.s %[i], %[tmp] \n\t"
362 : [i]
"=r" (i), [tmp]
"=f" (tmp)
368 return i - (i >
value);
381 #if defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || \
382 defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS
383 return (
int)__builtin_ceilf(
value);
384 #elif defined __loongarch__
387 __asm__ (
"ftintrp.w.s %[tmp], %[in] \n\t"
388 "movfr2gr.s %[i], %[tmp] \n\t"
389 : [i]
"=r" (i), [tmp]
"=f" (tmp)
395 return i + (i <
value);
408 #if defined CV_INLINE_ISNAN_FLT
409 CV_INLINE_ISNAN_FLT(
value);
413 return (ieee754.
u & 0x7fffffff) > 0x7f800000;
420 #if defined CV_INLINE_ISINF_FLT
421 CV_INLINE_ISINF_FLT(
value);
425 return (ieee754.
u & 0x7fffffff) == 0x7f800000;
int CvScalar value
Definition: core_c.h:720
CV_INLINE int cvIsNaN(double value)
Determines if the argument is Not A Number.
Definition: fast_math.hpp:284
CV_INLINE int cvIsInf(double value)
Determines if the argument is Infinity.
Definition: fast_math.hpp:302
CV_INLINE int cvRound(double value)
Rounds floating-point number to the nearest integer.
Definition: fast_math.hpp:200
CV_INLINE int cvCeil(double value)
Rounds floating-point number to the nearest integer not smaller than the original.
Definition: fast_math.hpp:258
CV_INLINE int cvFloor(double value)
Rounds floating-point number to the nearest integer not larger than the original.
Definition: fast_math.hpp:231
#define CV_INLINE
Definition: cvdef.h:218
unsigned u
Definition: cvdef.h:404
float f
Definition: cvdef.h:405
double f
Definition: cvdef.h:413
uint64 u
Definition: cvdef.h:412