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 __x86_64__ && 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 __x86_64__ \
205
&& defined __SSE2__ && !defined __APPLE__) || CV_SSE2) \
206
&& !defined(__CUDACC__)
207
__m128d t = _mm_set_sd( value );
208
return
_mm_cvtsd_si32(t);
209
#elif defined _MSC_VER && defined _M_IX86
217
#elif defined CV_ICC || defined __GNUC__
218
return
(
int)(lrint(value));
222
return
(
int)(value + (value >= 0 ? 0.5 : -0.5));
236
#if (defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS) \
240
return
__builtin_floor(value);
243
return
i - (i > value);
256
#if (defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS) \
260
return
__builtin_ceil(value);
263
return
i + (i < value);
275
#if defined CV_INLINE_ISNAN_DBL
276
CV_INLINE_ISNAN_DBL(value);
280
return
((
unsigned)(ieee754.u >> 32) & 0x7fffffff) +
281
((unsigned)ieee754.u != 0) > 0x7ff00000;
293
#if defined CV_INLINE_ISINF_DBL
294
CV_INLINE_ISINF_DBL(value);
295
#elif defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || defined(_M_ARM64) || defined(__PPC64__)
298
return
(ieee754.u & 0x7fffffff00000000) ==
303
return
((
unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
304
(unsigned)ieee754.u == 0;
311CV_INLINE
int
cvRound(
float
value)
313
#if defined CV_INLINE_ROUND_FLT
314
CV_INLINE_ROUND_FLT(value);
315
#elif ((defined _MSC_VER && defined _M_X64) || (defined __GNUC__ && defined __x86_64__ \
316
&& defined __SSE2__ && !defined __APPLE__) || CV_SSE2) \
317
&& !defined(__CUDACC__)
318
__m128 t = _mm_set_ss( value );
319
return
_mm_cvtss_si32(t);
320
#elif defined _MSC_VER && defined _M_IX86
328
#elif defined CV_ICC || defined __GNUC__
329
return
(
int)(lrintf(value));
333
return
(
int)(value + (value >= 0 ? 0.5f : -0.5f));
338CV_INLINE
int
cvRound(
int
value )
344CV_INLINE
int
cvFloor(
float
value )
346
#if (defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS) \
350
return
__builtin_floorf(value);
353
return
i - (i > value);
358CV_INLINE
int
cvFloor(
int
value )
364CV_INLINE
int
cvCeil(
float
value )
366
#if (defined CV__FASTMATH_ENABLE_GCC_MATH_BUILTINS || defined CV__FASTMATH_ENABLE_CLANG_MATH_BUILTINS) \
370
return
__builtin_ceilf(value);
373
return
i + (i < value);
378CV_INLINE
int
cvCeil(
int
value )
384CV_INLINE
int
cvIsNaN(
float
value )
386
#if defined CV_INLINE_ISNAN_FLT
387
CV_INLINE_ISNAN_FLT(value);
391
return
(ieee754.u & 0x7fffffff) > 0x7f800000;
396CV_INLINE
int
cvIsInf(
float
value )
398
#if defined CV_INLINE_ISINF_FLT
399
CV_INLINE_ISINF_FLT(value);
403
return
(ieee754.u & 0x7fffffff) == 0x7f800000;
CV_INLINE int cvIsNaN(double value)
Determines if the argument is Not A Number.
Definition:
fast_math.hpp:273
CV_INLINE int cvIsInf(double value)
Determines if the argument is Infinity.
Definition:
fast_math.hpp:291
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:254
CV_INLINE int cvFloor(double value)
Rounds floating-point number to the nearest integer not larger than the original.
Definition:
fast_math.hpp:234