00001
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef VL_MATHOP
00014 #define VL_MATHOP
00015
00016 #include "generic.h"
00017
00019 #define VL_LOG_OF_2 0.693147180559945
00020
00022 #define VL_PI 3.141592653589793
00023
00031 #define VL_EPSILON_F 1.19209290E-07F
00032
00039 #define VL_EPSILON_D 2.220446049250313e-16
00040
00041
00042
00043
00044
00045
00046
00047
00049 static union { vl_uint32 raw ; float value ; }
00050 const vl_nan_f =
00051 { 0x7FC00000UL } ;
00052
00054 static union { vl_uint32 raw ; float value ; }
00055 const vl_infinity_f =
00056 { 0x7F800000UL } ;
00057
00059 static union { vl_uint64 raw ; double value ; }
00060 const vl_nan_d =
00061 #ifdef VL_COMPILER_MSC
00062 { 0x7FF8000000000000ui64 } ;
00063 #else
00064 { 0x7FF8000000000000ULL } ;
00065 #endif
00066
00068 static union { vl_uint64 raw ; double value ; }
00069 const vl_infinity_d =
00070 #ifdef VL_COMPILER_MSC
00071 { 0x7FF0000000000000ui64 } ;
00072 #else
00073 { 0x7FF0000000000000ULL } ;
00074 #endif
00075
00077 #define VL_NAN_F (vl_nan_f.value)
00078
00080 #define VL_INFINITY_F (vl_infinity_f.value)
00081
00083 #define VL_NAN_D (vl_nan_d.value)
00084
00086 #define VL_INFINITY_D (vl_infinity_d.value)
00087
00103 VL_INLINE
00104 float vl_mod_2pi_f (float x)
00105 {
00106 while (x > 2 * VL_PI) x -= (float) (2 * VL_PI) ;
00107 while (x < 0.0F ) x += (float) (2 * VL_PI);
00108 return x ;
00109 }
00110
00111 VL_INLINE
00112 double vl_mod_2pi_d (double x)
00113 {
00114 while (x > 2 * VL_PI) x -= 2 * VL_PI ;
00115 while (x < 0.0 ) x += 2 * VL_PI ;
00116 return x ;
00117 }
00127 VL_INLINE
00128 int
00129 vl_floor_f (float x)
00130 {
00131 int xi = (int) x ;
00132 if (x >= 0 || (float) xi == x) return xi ;
00133 else return xi - 1 ;
00134 }
00135
00136 VL_INLINE
00137 int
00138 vl_floor_d (double x)
00139 {
00140 int xi = (int) x ;
00141 if (x >= 0 || (double) xi == x) return xi ;
00142 else return xi - 1 ;
00143 }
00144
00145
00153 VL_INLINE
00154 float
00155 vl_abs_f (float x)
00156 {
00157 return (x >= 0) ? x : -x ;
00158 }
00159
00160 VL_INLINE
00161 double
00162 vl_abs_d (double x)
00163 {
00164 return (x >= 0) ? x : -x ;
00165 }
00205 VL_INLINE
00206 float
00207 vl_fast_atan2_f (float y, float x)
00208 {
00209 float angle, r ;
00210 float const c3 = 0.1821F ;
00211 float const c1 = 0.9675F ;
00212 float abs_y = vl_abs_f (y) + VL_EPSILON_F ;
00213
00214 if (x >= 0) {
00215 r = (x - abs_y) / (x + abs_y) ;
00216 angle = (float) (VL_PI / 4) ;
00217 } else {
00218 r = (x + abs_y) / (abs_y - x) ;
00219 angle = (float) (3 * VL_PI / 4) ;
00220 }
00221 angle += (c3*r*r - c1) * r ;
00222 return (y < 0) ? - angle : angle ;
00223 }
00224
00225 VL_INLINE
00226 double
00227 vl_fast_atan2_d (double y, double x)
00228 {
00229 double angle, r ;
00230 double const c3 = 0.1821 ;
00231 double const c1 = 0.9675 ;
00232 double abs_y = vl_abs_d (y) + VL_EPSILON_D ;
00233
00234 if (x >= 0) {
00235 r = (x - abs_y) / (x + abs_y) ;
00236 angle = VL_PI / 4 ;
00237 } else {
00238 r = (x + abs_y) / (abs_y - x) ;
00239 angle = 3 * VL_PI / 4 ;
00240 }
00241 angle += (c3*r*r - c1) * r ;
00242 return (y < 0) ? - angle : angle ;
00243 }
00277 VL_INLINE
00278 float
00279 vl_fast_resqrt_f (float x)
00280 {
00281
00282 union {
00283 float x ;
00284 vl_int32 i ;
00285 } u ;
00286
00287 float xhalf = (float) 0.5 * x ;
00288
00289
00290 u.x = x ;
00291
00292
00293 u.i = 0x5f3759df - (u.i >> 1);
00294
00295
00296
00297 u.x = u.x * ( (float) 1.5 - xhalf*u.x*u.x) ;
00298 u.x = u.x * ( (float) 1.5 - xhalf*u.x*u.x) ;
00299 return u.x ;
00300 }
00301
00302 VL_INLINE
00303 double
00304 vl_fast_resqrt_d (double x)
00305 {
00306
00307 union {
00308 double x ;
00309 vl_int64 i ;
00310 } u ;
00311
00312 double xhalf = (double) 0.5 * x ;
00313
00314
00315 u.x = x ;
00316
00317
00318 #ifdef VL_COMPILER_MSC
00319 u.i = 0x5fe6ec85e7de30dai64 - (u.i >> 1) ;
00320 #else
00321 u.i = 0x5fe6ec85e7de30daLL - (u.i >> 1) ;
00322 #endif
00323
00324
00325 u.x = u.x * ( (double) 1.5 - xhalf*u.x*u.x) ;
00326 u.x = u.x * ( (double) 1.5 - xhalf*u.x*u.x) ;
00327 return u.x ;
00328 }
00329
00380 VL_INLINE
00381 float
00382 vl_fast_sqrt_f (float x)
00383 {
00384 return (x < 1e-8) ? 0 : x * vl_fast_resqrt_f (x) ;
00385 }
00386
00387 VL_INLINE
00388 double
00389 vl_fast_sqrt_d (float x)
00390 {
00391 return (x < 1e-8) ? 0 : x * vl_fast_resqrt_d (x) ;
00392 }
00393
00394 VL_INLINE vl_uint32 vl_fast_sqrt_ui32 (vl_uint32 x) ;
00395 VL_INLINE vl_uint16 vl_fast_sqrt_ui16 (vl_uint16 x) ;
00396 VL_INLINE vl_uint8 vl_fast_sqrt_ui8 (vl_uint8 x) ;
00397
00402 #define VL_FAST_SQRT_UI(T, SFX) \
00403 T \
00404 vl_fast_sqrt_ ## SFX (T x) \
00405 { \
00406 T y = 0 ; \
00407 T tmp = 0 ; \
00408 int twok ; \
00409 \
00410 for (twok = 8 * sizeof(T) - 2 ; \
00411 twok >= 0 ; twok -= 2) { \
00412 y <<= 1 ; \
00413 tmp = (2*y + 1) << twok ; \
00414 if (x >= tmp) { \
00415 x -= tmp ; \
00416 y += 1 ; \
00417 } \
00418 } \
00419 return y ; \
00420 }
00421
00422
00423 VL_FAST_SQRT_UI(vl_uint32, ui32)
00424 VL_FAST_SQRT_UI(vl_uint16, ui16)
00425 VL_FAST_SQRT_UI(vl_uint8, ui8 )
00439 float
00440 vl_dist_l2_f (float * dist, int M, int NX, int NY,
00441 float const* x, float const* y) ;
00442
00443 float
00444 vl_dist_l2_d (double * dist, int M, int NX, int NY,
00445 double const* x, double const* y) ;
00446
00449
00450 #endif