天天看點

SSE 加速運算例子詳解:乘法、加法、平方、最小值、最大值、與操作庫檔案說明采用SSE和不采用SSE的數學計算操作速度對比:

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 __METHOD


void 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
           
#include <xmmintrin.h>
#include <Windows.h>
#include <math.h>

void ScaleValue1(float *pArray, DWORD dwCount, float fScale)//乘法
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_mul_ps( *(__m128*)(pArray + i*),e_Scale);
    }
}

void ScaleValue2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] *= fScale;
    }
}

void Add1(float *pArray, DWORD dwCount, float fScale)//加法
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_add_ps( *(__m128*)(pArray + i*),e_Scale);
    }
}

void Add2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] += fScale;
    }
}

void Sqrt1(float *pArray, DWORD dwCount, float fScale)//平方
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_sqrt_ps(e_Scale);
    }
}

void Sqrt2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] = sqrt(fScale);
    }
}

void Min1(float *pArray, DWORD dwCount, float fScale)//最小值
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_min_ps( *(__m128*)(pArray + i*),e_Scale);
    }
}

void Min2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] = (pArray[i]>fScale? fScale : pArray[i]);
    }
}

void Max1(float *pArray, DWORD dwCount, float fScale)//最大值
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_max_ps( *(__m128*)(pArray + i*),e_Scale);
    }
}

void Max2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] = (pArray[i]<fScale? fScale : pArray[i]);
    }
}

void And1(float *pArray, DWORD dwCount, float fScale)//與操作
{
    DWORD dwGroupCount = dwCount/;
    __m128 e_Scale = _mm_set_ps1(fScale);//設定所有4個值為同一值

    for (DWORD i=; i<dwGroupCount; i++)
    {
        *(__m128*)(pArray + i*) = _mm_and_ps( *(__m128*)(pArray + i*),e_Scale);
    }
}

void And2(float *pArray, DWORD dwCount, float fScale)
{
    for (DWORD i =; i<dwCount; i++)
    {
        pArray[i] = (int)(pArray[i]) & (int)(fScale);
    }
}
           

采用SSE和不采用SSE的數學計算操作速度對比:

#include <xmmintrin.h>
#include <Windows.h>
#include <iostream>

#include "Method.h"

using namespace std;

#define ARRAYCOUNT 1000

#define COUNTSIZE 10000

class 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 )*/m_Frequency.QuadPart;
    }


private:
    LARGE_INTEGER m_Frequency;
    LARGE_INTEGER m_StartCount;
    LARGE_INTEGER m_EndCount;
};



int __cdecl main()
{
    float __declspec(align())Array[ARRAYCOUNT];
    //__declspec(align(16))做為數組定義的修釋符,這表示該數組是以16位元組為邊界對齊的,
    //因為SSE指令隻能支援這種格式的記憶體資料
    memset(Array, , sizeof(float)*ARRAYCOUNT);
    CTimer t;
    double dTime;

    //乘法
    cout<<"乘法:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        ScaleValue1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;


    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        ScaleValue2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;


//加法
    cout<<"加法:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Add1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;


    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Add2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;


    //平方
    cout<<"平方:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Sqrt1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;

    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Sqrt2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;


    //最小值
    cout<<"最小值:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Min1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;

    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Min2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;


    //最大值
    cout<<"最大值:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Max1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;

    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        Max2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;


    //與操作
    cout<<"與操作:"<<endl;
    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        And1(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Use SSE: "<<dTime<<"毫秒"<<endl;

    t.Reset();
    for (int i=; i<COUNTSIZE; i++)
    {
        And2(Array, ARRAYCOUNT, );
    }
    dTime = t.End();

    cout<<"Not Use SSE: "<<dTime<<"毫秒"<<endl;

    system("pause");

    return ;

}
           

繼續閱讀