Several commands of SSE intrinsic _ mm_prefetch/_ mm_movehl_ps/_ mm_shuffle_ps

Source: Internet
Author: User

 

1


,


_ Mm_prefetch

Void_mm_prefetch (char * P, int I)

The argument "* P" gives the address of the byte (and
Corresponding cache line) to be prefetched. The value "I" gives
Constant (_ mm_hint_t0, _ mm_hint_t1, _ mm_hint_t2, or _ mm_hint_nta) that
Specifies the type of prefetch operation to be completed MED.

T0 (temporal data) -- prefetch
Data into all cache levels.

T1 (temporal data with respect
To first level cache) -- prefetch data in all cache levels limit t 0th cache level

T2 (temporal data with respect
To second level cache) -- prefetch data in all cache levels, bytes t 0th and 1st
Cache levels.

NTA (non-temporal data
Respect to all cache levels) -- prefetch data into non-temporal cache structure.
(This hint can be used to minimize pollution of caches .)


Void _ mm_prefetch (char * P, int I)

Prefetch data from address P with the size of cache line. Parameter I indicates the prefetch method (_ mm_hint_t0, _ mm_hint_t1, _ mm_hint_t2, _ mm_hint_nta, indicating different prefetch methods respectively)
T0 prefetch data to all levels of cache, including l0.
Data prefetch at T1 is cached at all levels except l0.
Data prefetch on T2 is cached at all levels except l0 and L1.
NTA prefetch data to a non-temporary buffer structure to minimize cache pollution.
If we have already manually loaded the data to the cache before CPU operations, this reduces the amount of data that needs to be retrieved from the memory due to cache miss, in this way, the operation can be accelerated to improve the performance. Use the active Cache Technology to optimize the memory copy.

Note
Yes, the CPU is absolutely free to operate data! The use of prefetch commands only supplements the CPU data operations according to our own ideas. It is possible that the CPU does not need the data we load to the cache currently.
In this case, our prefetch command may bring the opposite result. For example, for a multi-task system, we may have washed out the useful cache. However, in a multi-task system, the time required for thread or process switching
The prefetch operation is too long, so you can ignore the impact of thread or process switching on Cache prefetch.


2


,


_ Mm_movehl_ps

Moves the upper two single-precision,
Floating-point values
B
To
The lower two single-precision, floating-point values of the result.
The upper two single-precision, floating-point values
Of a are passed through to the result.


Set


B


High


64


Low displacement to result


64


Bit,


A


High


64


Bit to the result.



For example:



R = _ m128
_ Mm_movehl_ps (_ m128 A, _ m128 B); // R = {A3, A2, B3, B2 }//
High
-
Low


S = _ mm_movehl_ps (x, x );//
High --
Low S = {X3, X2, X3, X2}

 

Example :(
Code)

 _ M128 A = {1.0f, 2.0f, 3.0f, 4.0f}; <br/> _ m128 B = {5.0f, 6.0f, 7.0f, 8.0f }; <br/> _ m128 r = _ mm_movehl_ps (a, B); // R = {1, 2, 5, 6}

 



3


,


_ Mm_shuffle_ps

Selects
Four specific single-precision, floating-point values from a and B,
Based on the mask
I.
Where,
I
Is
8 bit
Constant.
1 ~ 8
Bitwise controls the selection of components from two operands respectively.

_ M128
_ Mm_shuffle_ps (_ m128 A, _ m128 B, int I );

 

S = _ mm_shuffle_ps (R, R, 1) // R = {R3, R2, R1, R0}, S = {r0,
R0, R0, R1}

 

It can arrange the components of two operands in a specific order and assign them to the target number. For example

_ M128 B = _ mm_shuffle_ps (a, a, 0 );

  

Then
B
All components of are
A
As
0
. The third parameter controls component allocation, which is
8bit
Constant.
1 ~ 8
Bitwise controls the selection of components from two operands respectively. In use
Intrinsic
It is best to use
_ Mm_shuffle
Macro, which can define the allocation.

Shuffle FUNCTION macro

_ Mm_shuffle (Z,
Y, X, W)

/* Expands
To the following value */

(Z <6)
| (Y <4) | (x <2) | W

 

M3 = _ mm_shuffle_ps (M1, M2, _ mm_shuffle (1, 0, 3, 2 ))

 
It is a simple selection operation of the operands M1 and m2.

So, _ mm_shuffle (z, Y, X, W) selects X & W 32 bit double words from M1 and Z & Y from m2. how simple !!.

 

One little very formal suggestion:
_ Mm_shuffle (z, Y, X, W) does not select anything, this macro just creates a mask. shufps instruction (or _ mm_shuffle_ps wrapper function) performs selection, using mask created by _ mm_shuffle macro.

 

Example:

If you define a community

Typedef Union {_ m128 m; float m128_f32 [4];} my_m128;

_ M128 M1 = {1.0f, 2.0f, 3.0f, 4.0f };

Then, m128_f32 [0] = 4, m128_f32 [1] = 3, m128_f32 [2] = 2, m128_f32 [4] = 1

 

_ M128 M1 = {1.0f, 2.0f, 3.0f, 4.0f}; <br/> _ m 128 m2 = {5.0f, 6.0f, 7.0f, 8.0f }; <br/> // X. M = S1; <br/> _ m128 temp = _ mm_shuffle_ps (M1, M2, _ mm_shuffle (0, 1, 2, 3 )); <br/> temp = {8, 7, 2, 1}


 

 

Appendix:

Next, let's review the method of cross product.

C = A x B

Can be written:

Vector cross (const vector & A, const vector & B) {<br/> return vector (<br/> (A [1] * B [2]-A [2] * B [1]), <br/> (A [2] * B [0]-A [0] * B [2]), <br/> (A [0] * B [1]-A [1] * B [0]); <br/>}

Then write
SSE intrinsic
The format is:

/* Cross */<br/>__ m128 _ mm_cross_ps (_ m128a, _ m128 B) {<br/> _ m128 EA, Eb; <br/> // set to a [1] [2] [0] [3], B [2] [0] [1] [3] <br/> Ea = _ mm_shuffle_ps (a, a, _ mm_shuffle (3, 0, 2, 1 )); <br/> EB = _ mm_shuffle_ps (B, B, _ mm_shuffle (3, 1, 0, 2 )); <br/> // multiply <br/> _ m128 XA = _ mm_mul_ps (EA, EB ); <br/> // set to a [2] [0] [1] [3], B [1] [2] [0] [3] <br/> A = _ mm_shuffle_ps (a, a, _ mm_shuffle (3, 1, 0, 2 )); <br/> B = _ mm_shuffle_ps (B, B, _ mm_shuffle (3, 0, 2, 1 )); <br/> // multiply <br/> _ m128 XB = _ mm_mul_ps (A, B ); <br/> // subtract <br/> return _ mm_sub_ps (XA, XB); <br/>}

 

The three-point vector is used to calculate the dot product. It can be written as follows:

Float dot (const float & A, const float & B) const {<br/> return a [0] * B [0] + A [1] * B [1] + A [2] * B [2]; <br/>}< br/> you can use SSE intrinsic to write: <br/>/* X [0] * X [1] + Y [0] * Y [1] + Z [0] * Z [1] */<br/ >__ m128 _ mm_dot_ps (_ m128 X, _ m128 y) {<br/> _ m128 S, R; <br/> S = _ mm_mul_ps (x, y ); <br/> r = _ mm_add_ss (S, _ mm_movehl_ps (S, S); <br/> r = _ mm_add_ss (R, _ mm_shuffle_ps (R, 1); <br/> return r; <br/>}

 

In these two examples, we can see that the vertical addition of elements in the vector is generally in the form:

/* X [0] + X [1] + X [2] + X [3] */<br/>__ m128 _ mm_sum_ps (_ m128 X) {<br/> _ m128 R; <br/> r = _ mm_add_ps (x, _ mm_movehl_ps (x, x )); <br/> r = _ mm_add_ss (R, _ mm_shuffle_ps (R, R, 1); <br/> return r; <br/>}

Then, through expansion, we can obtain the function for finding the vector length. First, we need to calculate the sum of squares of components:

/* X [0] * X [0] + Y [0] * Y [0] + Z [0] * Z [0] */<br/>__ m128 _ mm_square_ps (_ m128 X) {<br/> _ m128 S, R; <br/> S = _ mm_mul_ps (x, x); <br/> r = _ mm_add_ss (S, _ mm_movehl_ps (S, S); <br/> r = _ mm_add_ss (R, _ mm_shuffle_ps (R, R, 1); <br/> return R; <br/>}

 

 

Reference: msdn


Http://hi.baidu.com/sige_online/blog/item/a80522ceec812433b700c829.html

Http://www.codeguru.com/forum/archive/index.php/t-337156.html

Http://blog.csdn.net/igame/archive/2007/08/21/1752430.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.