Intel IA-32 Computer Accessories User Manual


 
Coding for SIMD Architectures 3
3-29
Performing SIMD operations on the original AoS format can require
more calculations and some of the operations do not take advantage of
all of the SIMD elements available. Therefore, this option is generally
less efficient.
The recommended way for computing data in AoS format is to swizzle
each set of elements to SoA format before processing it using SIMD
technologies. This swizzling can either be done dynamically during
program execution or statically when the data structures are generated;
see Chapters 4 and 5 for specific examples of swizzling code.
Performing the swizzle dynamically is usually better than using AoS,
addps xmm1, xmm0 ; xmm0 = DC, DC, DC,
; x0*xF+z0*zF
movaps xmm2, xmm1
shufps xmm2, xmm2,55h ; xmm2 = DC, DC, DC, y0*yF
addps xmm2, xmm1 ; xmm1 = DC, DC, DC,
; x0*xF+y0*yF+z0*zF
; SoA code
;
; X = x0,x1,x2,x3
; Y = y0,y1,y2,y3
; Z = z0,z1,z2,z3
; A = xF,xF,xF,xF
; B = yF,yF,yF,yF
; C = zF,zF,zF,zF
movaps xmm0, X ; xmm0 = x0,x1,x2,x3
movaps xmm1, Y ; xmm0 = y0,y1,y2,y3
movaps xmm2, Z ; xmm0 = z0,z1,z2,z3
mulps xmm0, A ; xmm0 = x0*xF, x1*xF, x2*xF, x3*xF
mulps xmm1, B ; xmm1 = y0*yF, y1*yF, y2*yF, y3*xF
mulps xmm2, C ; xmm2 = z0*zF, z1*zF, z2*zF, z3*zF
addps xmm0, xmm1
addps xmm0, xmm2 ; xmm0 = (x0*xF+y0*yF+z0*zF), ...
Example 3-16 AoS and SoA Code Samples (continued)