opencv的四舍五入原来是这样来的

来源:互联网 发布:淘宝女装店铺推荐 编辑:程序博客网 时间:2024/06/05 23:02
/* from  opencv-1.0.0/cxcore/include */CV_INLINE  int  cvRound( double value ){#if CV_SSE2    __m128d t = _mm_load_sd( &value );    return _mm_cvtsd_si32(t);#elif defined WIN32 && !defined WIN64 && defined _MSC_VER    int t;    __asm    {        fld value;        fistp t;    }    return t;#elif (defined HAVE_LRINT) || (defined WIN64 && !defined EM64T && defined CV_ICC)    return (int)lrint(value);#else    /*     the algorithm was taken from Agner Fog's optimization guide     at http://www.agner.org/assem     */    Cv64suf temp;    temp.f = value + 6755399441055744.0;    return (int)temp.u;#endif}

test:

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <emmintrin.h> //SSE2(include xmmintrin.h)  int main(){    double a;    srand((unsigned)time(NULL));    a=rand()/(double)(RAND_MAX);    a=1000000.0*a;    printf("%lf\n",a);    __m128d  t = _mm_load_sd( &a );    int b = _mm_cvtsd_si32(t);    printf("b=%d\n",b);    typedef union Cv64suf    {        long long i;        unsigned long long u;        double f;    }    Cv64suf;    Cv64suf temp;    temp.f = a + 6755399441055744.0;    int c = (int)temp.u;    printf("c=%d\n",c);    return 0;}

compile:gcc -Wall -march=pentium4 -mmmx -o round round.c

out:

root@ubuntu:/home/luoxiao/work# ./round 686349.396448b=686349c=686349root@ubuntu:/home/luoxiao/work# ./round 406454.875789b=406455c=406455root@ubuntu:/home/luoxiao/work# ./round 406454.875789b=406455c=406455root@ubuntu:/home/luoxiao/work# ./round 763588.931301b=763589c=763589





0 0