当前位置:天才代写 > tutorial > C语言/C++ 教程 > Pentium III处理惩罚器的单指令大都据流扩展指令(3)

Pentium III处理惩罚器的单指令大都据流扩展指令(3)

2017-11-04 08:00 星期六 所属: C语言/C++ 教程 浏览:522

副标题#e#

提要 :

跟着Intel Pentium III处理惩罚器的宣布,给措施设计人员又带来了很多新的特性。操作这些新特性,措施员可觉得用户缔造出更好的产物. Pentium III和Pentium III Xeon(至强处理惩罚器)的很多新特性,可以使她可以或许比Pentium II和Pentium II Xeon处理惩罚器有更快的运行速度,这些新特性包罗一个处理惩罚器序列号(unique processor ID)和新增SSE处理惩罚器指令集,这些新的指令集就像Pentium II在经典Pentium的基本上添加的MMX指令集.

1. Data Swizzling

要操作Pentium III处理惩罚器SSE指令的加快成果也是要有必然的价钱的.因为SSE指令只能操纵她界说的新数据范例(128位).假如应用措施利用本身的数据范例名目,那么在举办SSE指令操纵之前要将他转换为这种新数据范例,操纵完成之后又要把他转换返来.

这种把一种数据名目转换到另一种数据名目标操纵就称为"data swizzling".

这种转换是需要时间和淹灭处理惩罚器焦点周期的.假如一个应用措施频繁的举办数据名目转换,处理惩罚器焦点周期的挥霍是很严重的.因此,这种数据名目标转换是必需获得存眷的.

1.1 数据组织

凡是,3D应用措施将一个极点生存在一个和他相配的数据布局中.当表达多个极点时,应用措施利用这个布局的数组,也叫做AoS来暗示.一个典范的操纵是对暗示极点的x、y和z坐标举办的.下面的代码就给出了这样一个暗示3D极点的数据布局.假如要暗示大量的这样的极点,就要用到这个布局的一个数组了,如图9所示.

struct point {

float x, y, z;
};
...
point dataset[...];

Pentium III处理惩罚处罚器的单指令多半据流扩展指令(3)

图九 :布局数组

SSE的利益,就是可以同时对多个极点的举办处理惩罚.这样我们就必需要可以或许利便的处理惩罚到暗示多个极点的数据(好比暗示4个极点x坐标的4个浮点数).这是可以实现的,我们可以把暗示一个极点的x、y和z三个坐标值别离荟萃在一起,然后应用措施对他们举办处理惩罚.要实现这些,应用措施必需从头组织数据到三个单独的数组中,可能建设一个数组布局,布局中每个数组对应一个这样的坐标值数组.这个数据布局也称为SoA布局.(我是这样领略的: 在不消SSE时是把暗示一个极点的三个坐标值组合到一个数据布局中,处理惩罚时一个值一个值的举办.而利用SSE今后,可以把所有点的各坐标值别离组合到3个数组中,处理惩罚时取出这样一个数组的4个值同时执行).

下面的代码就界说了这样的一个数组布局,如图10用图表来暗示的数组布局.

struct point {

float *x, *y, *z;
};

Pentium III处理惩罚处罚器的单指令多半据流扩展指令(3)

图十: 数组布局


#p#副标题#e#

2. 内存问题

2.1 对齐

简朴的处理惩罚和利用新数据范例变量是没有问题的.然而,一个推荐的做法是利用16字节界线对齐来界说新数据范例变量.可以通过配置一个特定的编译器参数可能在变量声名时显式的利用一个对齐标志来实现.

声名变量时可以指定__declspec编译器标志来强制变量利用16字节界线对齐,就像下面的例子代码所示.下面的变量myVar在利用这个对齐标志今后将被强制到16字节界线对齐.但当在直接声明这个新数据范例变量(也就是SSE界说的128位新数据范例)时就不需要指定这个对齐标志了,编译器会在碰着这个新数据范例时自动按16字节对齐.下面是对齐标志的用法 :

__declspec(align(16)) float[4] myVar;

2.2 动态分派

由于新数据范例要求的16字节界线对齐,使得在动态分派内存可能用指针来分派内存时会发生问题.

当利用指针会见数组时,我们必需担保这个指针是16字节界线对齐的.

在运行时分派内存可以利用malloc可能new呼吁.默认环境下这两种要领分派的指针都不是16字节界线对齐的.因此我们必需把这个分派获得的指针转换到16字节对齐,可能利用_mm_malloc函数来分派内存.利用_mm_malloc函数分派的内存块是按16字节界线对齐的. 和malloc函数有一个free函数一样,_mm_malloc函数也有一个对应的函数_mm_free.分派内存块利用_mm_malloc函数而排除(free)内存块用_mm_free函数.

2.3 自界说数据范例

指针必需是16字节界线对齐的限制很贫苦,假如可以或许不答理这种限制就好了.

在利用128位新数据范例时,我们常常需要去会见储存在内里的(4个)浮点数.在利用汇编语言时我们没有很多的选择.但在利用C可能C++和intrinsics库时,我们老是用__mm128数据范例来暗示这个128位的数据.利用这种数据范例(__mm128)时,一旦这个数值被配置今后,我们就不大大概直接去会见存储在内里的单独的浮点数了.有一个途径是把存储在内里的浮点数转移到一个浮点数数组内里,修改他们的数值今后再把他们转换归去.另一种途径是把这个新的数据范例映射到一个浮点数数组的要领来会见这些须要的元素.上面的第一种要领较量挥霍时间而第二种要领假如利用不妥则会产生问题.

#p#分页标题#e#

界说一个自界说的数据范例可以或许制止这个问题.这个自界说的数据范例用连系(union)的要领把新数据范例(__mm128)和一个4个元素的浮点数数组关联起来.下面给出代码,此刻可以用sse4来声名新数据范例了.

union sse4 {

__m128 m;

float f[4];
};

利用上面界说的数据范例,声明变量时不必利用对齐标志来强制16字节对齐,因为编译器在碰着这个数据连系内里的__m128数据范例时会自动按16字节对齐来分派内存的.这个数据范例的一个长处就是可以直接会见到存储在内里的单独的浮点数.

#p#副标题#e#

2.4 侦测CPU

利用SSE指令集需要有Pentium III处理惩罚器,应用措施一个首要的任务就是要去查抄有没有Pentium III芯片.这可以用cpuid的指令完成.

想要cpuid事情,需要把eax寄存器配置为特定的值.这里我们只想获得CPU的ID,需要把eax寄存器置为1然后挪用cpuid指令. 下面给出查抄PentiumIII处理惩罚器是否存在的代码.要想使下面的代码事情,还必需包括fvec.h头文件.

BOOL CheckP3HW()
{

BOOL SSEHW = FALSE;

_asm {

// Move the number 1 into eax - this will move the

// feature bits into EDX when a CPUID is issued, that

// is, EDX will then hold the key to the cpuid

mov eax, 1

// Does this processor have SSE support?

cpuid

// Perform CPUID (puts processor feature info in EDX)

// Shift the bits in edx to the right by 26, thus bit 25

// (SSE bit) is now in CF bit in EFLAGS register.

shr edx,0x1A

// If CF is not set, jump over next instruction

jnc nocarryflag

// set the return value to 1 if the CF flag is set

mov [SSEHW], 1

nocarryflag:

}

return SSEHW;
}

#p#副标题#e#

SSE SDK尚有一个Pentium III和SSE寄存器的异常模式.用下面的代码也可以用来查抄是否有Pentium III处理惩罚器.要编译下面的代码,需要包括fvec.h头文件.

// Checking for SSE emulation support
BOOL CheckP3Emu()
{

BOOL SSEEmu = TRUE;

Fvec32 pNormal = (1.0, 2.0, 3.0, 4.0);

Fvec32 pZero = 0.0;

// Checking for SSE HW emulation

__try {

_asm {

// Issue a move instruction that will cause exception

// w/out HW support emulation

movups xmm1, [pNormal]

// Issue a computational instruction that will cause

// exception w/out HW support emulation

divps xmm1, [pZero]

}

}

// If there''s an exception, set emulation variable to false

__except(EXCEPTION_EXECUTE_HANDLER){

SSEEmu = FALSE;

}

return SSEEmu;
}

3. 参考

For more details about the architecture of the Pentium III, refer [11], [12], [13] and [10].

For more information about Processor identification and CPUID, refer [15] and [7].

For more information about the Streaming SIMD Extensions, refer [19].

For more information about the programming issue, the software conventions and the software development strategies, refer [9], [16] and [17] respectively.

For more about application tuning for SSE and the VTune performance enhancement application, refer [1] and [8] respectively.

For more details about VTune and the Intel C/C++ Compiler, refer [3] and [2] respectively.

For additional information about the Pentium III processor and its capabilities, refer [14], [18], [20], [6], [5] and [4].

#p#副标题#e#

4. 附加示例

在这一节,我们将向你先容几个SSE指令集(Streaming SIMD Extensions)利用的例子.

4.1 数组操纵

下面的例子中,我们建设了两个数组,每个包括400个浮点数.将两个数组的每个元素相乘,功效生存在第三个数组中.前面两个数组作为操纵数A和B,生存功效的数组为C.下面给出所有的代码.

#include < FVEC.H >
#define ARRSIZE 400
__declspec(align(16)) float a[ARRSIZE], b[ARRSIZE], c[ARRSIZE];

4.1.1 Assembly Language

_asm{

push esi;

push edi;

mov edi, a;

mov esi, b;

mov edx, c;

mov ecx, 100;
loop:

movaps xmm0, [edi];

movups xmm1, [esi];

mulps xmm0, xmm1;

movups [edx], xmm0;

add edi, 16;

add esi, 16;

add edx, 16;

dec ecx;

jnz loop;

pop edi;

pop esi;
}

4.1.2 Intrinsics

#p#分页标题#e#

__m128 m1, m2, m3;
for ( int i = 0; i < ARRSIZE; i += 4 )
{

m1= _mm_loadu_ps(a+i);

m2= _mm_loadu_ps(b+i);

m3= _mm_mul_ps(m1,m2);

_mm_storeu_ps(c+i, m3);
}

#p#副标题#e#

4.1.3 C++

F32vec4 f1, f2, f3;
for ( int i = 0; i < ARRSIZE; i +=4 )
{

loadu(f1, a+i);

loadu(f2, b+i);

f3 = f1 * f2;

storeu(c+i, f3);
}

4.2 矢量3D

下面的例子先容3D矢量.矢量被封装在一个类中.在下面类的函数内里挪用了intrinsics库.

类声明.

union sse4 {
  __m128 m;
  float f[4];
};
class sVector3 {
protected:
  sse4 val;
public:
  sVector3(float, float, float);
  float& operator [](int);
  sVector3& operator +=(const sVector3&);
  float length() const;
friend float dot(const sVector3&, const sVector3&);
};

类实现.

sVector3::sVector3(float x, float y, float z) {
  val.m = _mm_set_ps(0, z, y, x);
}
float& sgmVector3::operator [](int i) {
  return val.f[i];
}
sVector3& sVector3::operator +=(const sVector3& v) {
  val.m = _mm_add_ps(val.m, v.val.m);
  return *this;
}
float sVector3::length() const {
  sse4 m1;
  m1.m = _mm_sqrt_ps(_mm_mul_ps(val.m, val.m));
  return m1.f[0] + m1.f[1] + m1.f[2];
}
float dot(const sVector3& v1, const sVector3& v2) {
  sVector3 v(v1);
  v.val.m = _mm_mul_ps(v.val.m, v2.val.m);
  return v.val.f[0] + v.val.f[1] + v.val.f[2];
}

#p#副标题#e#

4.3 4×4矩阵

下面的例子先容一个4×4的矩阵.矩阵被封装在一个类中.在下面类的函数内里挪用了intrinsics库.

类声明.

float const sEPSILON = 1.0e-10f;
union sse16 {
  __m128 m[4];
  float f[4][4];
};
class sMatrix4 {
protected:
  sse16 val;
  sse4 sFuzzy;
public:
  sMatrix4(float*);
  float& operator()(int, int);
  sMatrix4& operator +=(const sMatrix4&);
  bool operator ==(const sMatrix4&) const;
  sVector4 operator *(const sVector4&) const;
private:
  float RCD(const sMatrix4& B, int i, int j) const;
};

类实现.

sMatrix4::sMatrix4(float* fv) {
  val.m[0] = _mm_set_ps(fv[3], fv[2], fv[1], fv[0]);
  val.m[1] = _mm_set_ps(fv[7], fv[6], fv[5], fv[4]);
  val.m[2] = _mm_set_ps(fv[11], fv[10], fv[9], fv[8]);
  val.m[3] = _mm_set_ps(fv[15], fv[14], fv[13], fv[12]);
  float f = sEPSILON;
  sFuzzy.m = _mm_set_ps(f, f, f, f);
}
float& sMatrix4::operator()(int i, int j) {
  return val.f[i][j];
}
sMatrix4& sMatrix4::operator +=(const sMatrix4& M) {
  val.m[0] = _mm_add_ps(val.m[0], M.val.m[0]);
  val.m[1] = _mm_add_ps(val.m[1], M.val.m[1]);
  val.m[2] = _mm_add_ps(val.m[2], M.val.m[2]);
  val.m[3] = _mm_add_ps(val.m[3], M.val.m[3]);
  return *this;
}
bool sMatrix4::operator ==(const sMatrix4& M) const {
  int res[4];
  res[0] = res[1] = res[2] = res[3] = 0;
  res[0] = _mm_movemask_ps(_mm_cmplt_ps(_mm_sub_ps(
    _mm_max_ps(val.m[0], M.val.m[0]),
    _mm_min_ps(val.m[0], M.val.m[0])), sFuzzy.m));
  res[1] = _mm_movemask_ps(_mm_cmplt_ps(_mm_sub_ps(
    _mm_max_ps(val.m[1], M.val.m[1]),
    _mm_min_ps(val.m[1], M.val.m[1])), sFuzzy.m));
  res[2] = _mm_movemask_ps(_mm_cmplt_ps(_mm_sub_ps(
    _mm_max_ps(val.m[2], M.val.m[2]),
    _mm_min_ps(val.m[2], M.val.m[2])), sFuzzy.m));
  res[3] = _mm_movemask_ps(_mm_cmplt_ps(_mm_sub_ps(
    _mm_max_ps(val.m[3], M.val.m[3]),
    _mm_min_ps(val.m[3], M.val.m[3])), sFuzzy.m));
  if ( (15 == res[0]) && (15 == res[1])
      && (15 == res[2]) && (15 == res[3]) )
    return 1;
  return 0;
}
sVector4 sMatrix4::operator *(const sVector4& v) const {
  return sVector4(
    val.f[0][0] * v[0] + val.f[0][1] * v[1]       + val.f[0][2] * v[2] + val.f[0][3] * v[3],
    val.f[1][0] * v[0] + val.f[1][1] * v[1]       + val.f[1][2] * v[2] + val.f[1][3] * v[3],
    val.f[2][0] * v[0] + val.f[2][1] * v[1]       + val.f[2][2] * v[2] + val.f[2][3] * v[3],
    val.f[3][0] * v[0] + val.f[3][1] * v[1]       + val.f[3][2] * v[2] + val.f[3][3] * v[3]);
}
float sMatrix4::RCD(const sMatrix4& B, int i, int j) const {
  return val.f[i][0] * B.val.f[0][j] + val.f[i][1] * B.val.f[1][j]     + val.f[i][2] * B.val.f[2][j] + val.f[i][3] * B.val.f[3][j];
}

#p#副标题#e#

[参考书目] [1] James Abel, Kumar Balasubramanian, Mike Bargeron, Tom Craver, and Mike Phlipot. Applications tuning for streaming simd extensions. Technical report, Intel Corporation, 1999.

[2] Intel Corporation. Intel C/C++ Compiler Web Site. http://developer.intel.com/vtune/icl.

[3] Intel Corporation. Vtune Performance Analyzer Web Site. http://developer.intel.com/vtune/performance.

[4] Intel Corpotation. Developer Relations Group Web Site. http://developer.intel.com/drg.

[5] Intel Corpotation. Intel Developer Web Site. http://developer.intel.com.

[6] Intel Corpotation. Web site. http://www.intel.com.

[7] Stephan Fischer, James Mi, and Albert Tang. Pentium iii processor serial number feature and applications. Technical report, Intel Corporation, 1999.

[8] Joe Wolf III. Programming methods for the pentium iii processor streaming simd extensions using the vtune performance enhancement environment. Technical report, Intel Corporation, 1999.

[9] Intel Corporation. Data Alignment and Programming Issues for the Streaming SIMD Extensions with the Intel C/C++ Compiler, 1999. App Note ap833.

[10] Intel Corporation. Intel Architecture Optimization Reference Manual, 1999.

[11] Intel Corporation. Intel Architecture Software Development Manual. Volume 1: Basic Architecture, 1999.

[12] Intel Corporation. Intel Architecture Software Development Manual. Volume 2: Instruction Set Reference, 1999.

[13] Intel Corporation. Intel Architecture Software Development Manual. Volume 3: Systems Programming Guide, 1999.

[14] Intel Corporation. Intel Pentium III Processor Performance Brief, 1999.

[15] Intel Corporation. Intel Processor Identification and CPUID Instruction, 1999. App Note Ap-485.

[16] Intel Corporation. Software Conventions for Streaming SIMD Extensions, 1999. App Note AP589.

[17] Intel Corporation. Software Development Strategies for Streaming SIMD Extensions, 1999. App Note AP814.

[18] Jagannath Keshavan and Vladimir Penkovski. Pentium iii processor implementation trade-offs. Technical report, Intel Corporation, 1999.

[19] Shreekant Thakkar and Tom Huff. Internet streaming simd extensions. Technical report, Intel Corporation, 1999.

#p#分页标题#e#

[20] Paul Zagacki, Deep Duch, Emil Hsiech, Daniel Melaku, and Vladimir Pentkovski. Architecture of 3d software stack for peak pentium iii processor performance. Technical report, Intel Corporation, 1999.

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式