windows下sse性能对比

来源:互联网 发布:能在淘宝买燕尾型材吗 编辑:程序博客网 时间:2024/06/01 10:45

SSE(Streaming SIMD Extensions)是英特尔在AMD的3D Now!发布一年之后,在其计算机芯片Pentium III中引入的指令集,是MMX的超集。AMD后来在Athlon XP中加入了对这个指令集的支持。这个指令集增加了对8个128位寄存器XMM0-XMM7的支持,每个寄存器可以存储4个单精度浮点数。使用这些寄存器的程序必须使用FXSAVE和FXRSTR指令来保持和恢复状态。但是在Pentium III对SSE的实现中,浮点数寄存器又一次被新的指令集占用了,但是这一次切换运算模式不是必要的了,只是SSE和浮点数指令不能同时进入CPU的处理线而已。

库文件说明

#ifndef __METHOD#define __METHODvoid ScaleValue1(float *pArray, DWORD dwCount, float fScale);//乘法void ScaleValue2(float *pArray, DWORD dwCount, float fScale);void Add1(float *pArray, DWORD dwCount, float fScale);//加法void Add2(float *pArray, DWORD dwCount, float fScale);void Sqrt1(float *pArray, DWORD dwCount, float fScale);//平方void Sqrt2(float *pArray, DWORD dwCount, float fScale);void Min1(float *pArray, DWORD dwCount, float fScale);//最小值void Min2(float *pArray, DWORD dwCount, float fScale);//最小值void Max1(float *pArray, DWORD dwCount, float fScale);//最小值void Max2(float *pArray, DWORD dwCount, float fScale);//最小值void And1(float *pArray, DWORD dwCount, float fScale);//与操作void And2(float *pArray, DWORD dwCount, float fScale);//与操作#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
#include <xmmintrin.h>#include <Windows.h>#include <math.h>void ScaleValue1(float *pArray, DWORD dwCount, float fScale)//乘法{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_mul_ps( *(__m128*)(pArray + i*4),e_Scale);    }}void ScaleValue2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] *= fScale;    }}void Add1(float *pArray, DWORD dwCount, float fScale)//加法{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_add_ps( *(__m128*)(pArray + i*4),e_Scale);    }}void Add2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] += fScale;    }}void Sqrt1(float *pArray, DWORD dwCount, float fScale)//平方{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_sqrt_ps(e_Scale);    }}void Sqrt2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] = sqrt(fScale);    }}void Min1(float *pArray, DWORD dwCount, float fScale)//最小值{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_min_ps( *(__m128*)(pArray + i*4),e_Scale);    }}void Min2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] = (pArray[i]>fScale? fScale : pArray[i]);    }}void Max1(float *pArray, DWORD dwCount, float fScale)//最大值{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_max_ps( *(__m128*)(pArray + i*4),e_Scale);    }}void Max2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] = (pArray[i]<fScale? fScale : pArray[i]);    }}void And1(float *pArray, DWORD dwCount, float fScale)//与操作{    DWORD dwGroupCount = dwCount/4;    __m128 e_Scale = _mm_set_ps1(fScale);//设置所有4个值为同一值    for (DWORD i=0; i<dwGroupCount; i++)    {        *(__m128*)(pArray + i*4) = _mm_and_ps( *(__m128*)(pArray + i*4),e_Scale);    }}void And2(float *pArray, DWORD dwCount, float fScale){    for (DWORD i =0; i<dwCount; i++)    {        pArray[i] = (int)(pArray[i]) & (int)(fScale);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117

采用SSE和不采用SSE的数学计算操作速度对比:

#include <xmmintrin.h>#include <Windows.h>#include <iostream>#include "Method.h"using namespace std;#define ARRAYCOUNT 1000#define COUNTSIZE 10000class CTimer{public:    __forceinline CTimer(void)    {        QueryPerformanceFrequency(&m_Frequency);// 获取时钟周期        QueryPerformanceCounter(&m_StartCount);// 获取时钟计数    }    __forceinline void Reset(void)    {        QueryPerformanceCounter(&m_StartCount);    }    __forceinline double End(void)    {        QueryPerformanceCounter(&m_EndCount);        return ( m_EndCount.QuadPart - m_StartCount.QuadPart )*1000/m_Frequency.QuadPart;    }private:    LARGE_INTEGER m_Frequency;    LARGE_INTEGER m_StartCount;    LARGE_INTEGER m_EndCount;};int __cdecl main(){    float __declspec(align(16))Array[ARRAYCOUNT];    //__declspec(align(16))做为数组定义的修释符,这表示该数组是以16字节为边界对齐的,    //因为SSE指令只能支持这种格式的内存数据    memset(Array, 0, sizeof(float)*ARRAYCOUNT);    CTimer t;    double dTime;    //乘法    cout<<"乘法:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        ScaleValue1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        ScaleValue2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;//加法    cout<<"加法:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Add1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Add2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;    //平方    cout<<"平方:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Sqrt1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Sqrt2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;    //最小值    cout<<"最小值:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Min1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Min2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;    //最大值    cout<<"最大值:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Max1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        Max2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;    //与操作    cout<<"与操作:"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        And1(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;    t.Reset();    for (int i=0; i<COUNTSIZE; i++)    {        And2(Array, ARRAYCOUNT, 1000.0f);    }    dTime = t.End();    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;    system("pause");    return 0;}
0 0
原创粉丝点击