SSE2 beginner guide

xiaoxiao2021-04-11  1.9K+

INTRO

What is sse2?

SSE2 is an extension of assembly language which allows programs to execute one operation on multiple pieces of data at a time. Because SSE2 is assembly however, it only works on processors that support it. If the commands are attempted to be executed on a machine which Is Not Capable of Doing So, a General Protection Fault Will Be Encountered. Luckily There is Easy Ways To Tell if The Processor (S) You are running on supports ssE2.

Basic structure of sse2

SSE2 works just like any other set of assembly calls. There are registers in which data can be stored and operations that can execute on these registers. Each register is 16 bytes (2 doubles). The 8 registers are named xmm0 through xmm7.

Basics

Some code

Inline Void Add (Double * X, Double * Y, Double * RetVal)

{

ASM

{

// Copy the first 16 bytes Into Xmm0, Starting At the Memory X Points To

MovUpd XMM0, [x]

// Copy the first 16 bytes Into Xmm1, Starting At the Memory Y Points To

MovUpd XMM1, [Y]

// add the 2 Doubles in xmm1 to the 2 Doubles in xmm0, and put the

// Result in Xmm0, Overwriting The Previous Data Stored There

AddPD XMM0, XMM1

// copy the 16 bytes of data in xmm0 to the memory Ret Points to

Movupd [RetVal], XMM0

}

}

Hopefully My Comments Before Each Line Were Enough To Let You Know What Was Going On. In Case They Wern't, I'll Go Into a Little More Detail About Each Line.

ASM {}

This keyword lets your compiler know that the code you are giving it will be in assembly and that it should compile it as such. It also, conveniently, tells the compiler to inline the code. This means that there is NO overhead for the asm block .

MovUpd XMM0, [x]

MovUpd XMM1, [Y]

This command copies data from the second operand to the first;. As always in Intel syntax, the asm is in dest, src order By putting brackets around the x, we tell the mov command to copy the data that x points to the actual value Of The Pointer. The Square Brackets Can Be Thought of as a Method of Dereferencing a Pointer.Addpd XMM0, XMM1

This is the line there is the 1st Operand, Dest, And Stores The Resulting Value In The 1st Operands, DEST. 02...... It Takes The Line That.

Movupd [RetVal], XMM0

Here, We Copy The Data That In Xmm0 To The Memory That RetVal Points To The Memory That RetVal Points To The Memory That RetVal Points To. Again, The Square Brackets Dereference The Variable Retvalin The Same Way That A '*' Does IN C / C .

Some more Operations

Subpd Dest, Src // Subtract Dest from SRC, Store in Dest

Mulpd Dest, Src // Multiply Dest and SRC, Store in Dest

Divpd Dest, Src // Divide Src By Dest, Store in Dest

MINPD DEST, SRC // Store The Smallst Value, Either Dest OR SRC, IN DEST

Maxpd Dest, Src // Store The Largest Value, Either Dest OR SRC, IN DEST

SQRTPD DEST, SRC // Take The Square Root of Src and Put The Result in DEST

A Full List of SSE2 Operations and a description of each Can Be Found at hayes technology.

MAKING THE MOST OF SSE2

The Faster Move Instruction

Up until now, we have been using movapd to move data to and from our registers. This is much slower than the instruction movapd which does the exact same thing, but assumes that the data is 16 byte aligned. This means that the pointer supplied must be divisible by 16. This becomes a rather large problem if you are compiling your code with gcc or the one supplied with Microsoft Visual C . One solution to this problem is to use a different compiler such as one that Intel provides. The inherent problem with . this is that the Intel compiler is not freeware like gcc If you have already spent money on some other compiler, you probably do not want to spend more on this new compiler One hack that I have come up with is the following:. #define AllignData (Void *) ((int) DATA 15) & ~ 0x0f)

// or an inline function if you prefer:

Inline void * Alligndata (Void * Data)

{

Return (Void *) ((int) DATA 15) & ~ 0x0f);

}

Void Main

{

Const int sizeofdata = 512;

Double * lotsofdata;

Double * Tempptr;

Tempptr = New Double [SizeOfData 2];

LotsofData = AllIGndata (Tempptr);

ASM

{

Movapd XMM0, [LotsofData];

// Do Lots of CPU Intensive Sse2 Operations on LotsofData Here

}

DELETE [] Tempptr;

LotsofData = 0;

// Do Not Delete LotsofData, Just Set It To 0, THE MEMORY IT

// Points to is no longer valid

}

What We need for this instruction to work is memory That Has A 16-byte allright memory address.

Memory That IS 16BYTE Alligned:

When we use the new command in C / C to allocate memory, we are given memory that may or may not be 16byte alligned. So, what we do, is instead of using the very first bit of our memory block, we start using it at the first place that is 16byte alligned .-- yellow is unusbale, green is what we actually use --By doing this, we waste the memory that comes before the first 16byte alligned memory address. Normally this is not too big of a problem as the overhead is only once per memory block and if we are allocating large amounts of memory at once, 1 or even 12 bytes will hardly make a difference. The only problem with this is that by not using the beginning of our memory, WE End Up Having a Smaller Amount of USAble Memory Than We asked for. In The Worst Case, 15 Bytes Are Not Usable:

. Therefore, to make sure that we get a specific number of usable bytes, we allocate at least 15 extra bytes In the case of allocating double values, this means we must allocate an extra 2 doubles, giving us 16 extra bytes:

If you are not using Doupla, Just make Sure That at Least 15 extra bytes area allocated. Just determine the size of the data type and compute how it ...

Another important thing to notice is that we delete the variable that holds the original pointer to all of our memory. If you try to delete the memory starting in the middle of our memory block, you cause a general protection fault and have a memory leak.

Order your Operations

One thing that is often over looked is the order that registers are used. When an operation is performed, there is a delay while the result is bieng moved to its destination. If the next operation requires this value, it must wait for it to be stored into the register. If however, the next operation does not need this data, it does not need to wait for it to be stored, it can go ahead and execute at the same time that the previous result is getting stored.For instance, There Will Be a Speed ​​Difference Between The Following TWO Code Segments:

#1:

ASM

{

MovUpd XMM0, [X] // XMM0 = X

MovUpd XMM1, [Y] // XMM1 = Y

MovUpd XMM2, [Z] // XMM2 = Z

MovUpd XMM3, [W] // XMM3 = W

Movapd XMM4, XMM2 // XMM4 = Z

Movapd XMM5, XMM1 // XMM5 = Y

Movapd XMM6, XMM0 // XMM6 = X

AddPD XMM2, XMM3 // XMM2 = Z W

AddPD XMM1, XMM2 // XMM1 = Y Z W

Addpd XMM0, XMM1 // XMM0 = X Y Z W

Mulpd XMM4, XMM3 // XMM4 = Z * W

Mulpd XMM5, XMM4 // XMM5 = Y * Z * W

Mulpd XMM6, XMM5 // XMM6 = x * Y * z * w

DIVPD XMM0, XMM6 // XMM0 = (x * Y * z * w) / (x y z w)

MovUpd [RET], XMM0 // Ret = (x * Y * z * w) / (x y z w)

}

#2:

ASM

{

MovUpd XMM0, [X] // XMM0 = X

MovUpd XMM1, [Y] // XMM1 = Y

MovUpd XMM2, [Z] // XMM2 = Z

MovUpd XMM3, [W] // XMM3 = W

Movapd XMM4, XMM2 // XMM4 = Z

Movapd XMM5, XMM1 // XMM5 = Y

Movapd XMM6, XMM0 // XMM6 = X

AddPD XMM2, XMM3 // XMM2 = Z W

Mulpd XMM4, XMM3 // XMM4 = Z * W

AddPD XMM1, XMM2 // XMM1 = Y Z W

Mulpd XMM5, XMM4 // XMM5 = Y * Z * W

AddPD XMM0, XMM1 // XMM0 = X Y Z WMULPD XMM6, XMM5 // XMM6 = x * Y * Z * W

DIVPD XMM0, XMM6 // XMM0 = (x * Y * z * w) / (x y z w)

MovUpd [RET], XMM0 // Ret = (x * Y * z * w) / (x y z w)

}

The second piece of code will run faster. This is because in the second case, there are only 2 cases where one instruction relies on the data from the previous one to perform its computations. Because of this, instructions can be executed immediately after the previous One Finsihes Instead of Waiting for It To Store Its Result in The Registers.

Don't get carried away

A common mistake made by people new to SSE2 is to convert a lot of their old and future code into SSE2. This can actually result in slower code. The reason for this is the very large overhead for the CPU to copy memory to the registers. IF you have an applications on a small number set, you can expect to belless efficient than if you are doing a lot of operations on a small amount of data.

Compiableing SSE2 with GCC / G

The first thing that you need to remember to do when you want to compile SSE2 embedded C / C code with gcc / g , is to throw in the -masm = intel switch during compile. You must also put ".intel_syntax noprefix" in front Of Your ASM Code and Surround IT with Quotes Like this:

ASM (". Intel_syntax noprefix / n");

ASM ("MOV EAX, X / N");

ASM ("MovUpd XMM0, [EAX 0x00] / N");

ASM ("MovuPD XMM1, [EAX 0x10] / N");

ASM ("AddPD XMM0, XMM1 / N");

ASM ("MovUpd [EAX 0x20], XMM0 / N");

oral

asm (". Intel_syntax noprefix

Mov Eax, X

MovUpd XMM0, [EAX 0x00] movupd XMM1, [EAX 0x10]

AddPD XMM0, XMM1

MovUpd [EAX 0x20], XMM0 / N ");

Note that the asm block is inside "()" not "{}". Also, if you want to use a variable declared in your C / C code, you must define it publicly. Any variables defined locally, whether inside your main function , Inside a for loop, etc, Will Not Be seen by the Linker and will be concered an "undefined reference".

End

Questions? Comments? Suggestions? Mis-spellings? Grammatical errors? Email me at shilindalian@msn.com

转载请注明原文地址:https://www.9cbs.com/read-133531.html

New Post(0)