Author Topic: Arithmetic with the SIMD registers  (Read 3676 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Arithmetic with the SIMD registers
« on: January 22, 2014, 02:29:58 PM »
SIMD: Single Instruction Multiple Data

Code: [Select]
'SIMD ARITHMETIC USING DOUBLES
'2 DOUBLES CALCULATED IN PARALLEL
indexbase 0
double d[100]={1,2,3,4,5,6}
d[18]<=10, 100
movupd xmm0,d[0]
addpd  xmm0,xmm0
movupd xmm1,d[2]
mulpd  xmm0,xmm1
movupd xmm2,d[18]
divpd  xmm0,xmm2
movupd d[16],xmm0
print "SIMD: " str(d[16],4) "    " str(d[17],4)



Charles Pegge

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #1 on: January 22, 2014, 11:00:47 PM »
Calculating Hypotenuse (single precision)

This requires an instruction from the SSE3 set, so it will only work with Intel CPUs 2004.. or AMD 2005..

Code: [Select]
'SIMD ARITHMETIC USING SINGLES
'HYPOTENUSE OF 3,4 and 1,1
single s[20]={3.0,4.0,1,1}
movups xmm1,s[0]
mulps  xmm1,xmm1
subps  xmm0,xmm0
haddps xmm0,xmm1 'Horizontal Add requires SSE3 instruction set (INTEL 2004 / AMD 2005)
sqrtps xmm1,xmm0
movups s[16],xmm1
print "SIMD: " str(s[18],4) "    " str(s[19],4) 'result: 5  1.4142

Note that array expressions with a const index are useable in Assembler instructions. s[16] etc.
« Last Edit: January 23, 2014, 03:57:15 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #2 on: January 23, 2014, 08:32:08 AM »

SIMD Comparison

Works on first elements only.

Code: [Select]
'SIMD COMPARES USING SINGLES
single s[20]={0,1,2,3,4,5,6,7}
movups xmm0,s[0]
movups xmm1,s[4]
comiss xmm0,xmm1 'comparing 0 with 4
jae nv
print "below"
.nv

Charles Pegge

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #3 on: January 23, 2014, 09:17:55 AM »

SIMD Interleaving and Shuffling

Code: [Select]
'SIMD INTERLEAVING (LOW PAIR)
single s[20]={0,1,2,3,4,5,6,7}
movups xmm0,s[0]
movups xmm1,s[4]
unpcklps xmm0,xmm1
movups s[0],xmm0
print "SIMD: " s[0] "    " s[1] "    " s[2] "    " s[3] "    " ' 0 4 1 5


Code: [Select]
'SIMD INTERLEAVING (HIGH PAIR)
single s[20]={0,1,2,3,4,5,6,7}
movups xmm0,s[0]
movups xmm1,s[4]
unpckhps xmm0,xmm1
movups s[0],xmm0
print "SIMD: " s[0] "    " s[1] "    " s[2] "    " s[3] "    " ' 2 6 3 7


Code: [Select]
'SIMD SHUFFLING
single s[20]={0,1,2,3,4}
movups xmm0,s[0]
shufps xmm0,xmm0,0b00011011 'reverses order of elements 00 01 10 11
movups s[0],xmm0
print "SIMD: " s[0] "    " s[1] "    " s[2] "    " s[3] "    " ' 3 2 1 0



JRS

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #4 on: January 23, 2014, 10:22:43 AM »
For those like myself wondering what Charles is up to.

Quote
Single instruction, multiple data (SIMD), is a class of parallel computers in Flynn's taxonomy. It describes computers with multiple processing elements that perform the same operation on multiple data points simultaneously. Thus, such machines exploit data level parallelism. SIMD is particularly applicable to common tasks like adjusting the contrast in a digital image or adjusting the volume of digital audio. Most modern CPU designs include SIMD instructions in order to improve the performance of multimedia use.


Peter

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #5 on: January 23, 2014, 10:54:55 AM »
Hi Charles,

Simd is quit simple.
Code: [Select]
single v1[4] = {1.1,2.2,3.3,4.4}
single v2[4] = {5.5,6.6,7.7,8.8}
single v3[4]

movups xmm0,v1
movups xmm1,v2

addps xmm0,xmm1
mulps xmm0,xmm1
subps xmm0,xmm1

movups v3,xmm0

print str(v3[1],8)
print str(v3[2],8) 
print str(v3[3],8)
print str(v3[4],8)

Charles Pegge

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #6 on: January 23, 2014, 11:42:46 AM »
Yes, it is very simple for parallel arithmetic, but shuffling values horizontally and diagonally is like trying to solve one of your magic number squares. :)

JRS

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #7 on: January 23, 2014, 12:38:11 PM »
Pool congestion.




Charles Pegge

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #8 on: January 23, 2014, 10:36:50 PM »

X86: CPU, FPU, MMX, SSE. They are almost separate devices




Peter

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #9 on: January 24, 2014, 05:01:47 AM »
another simd for kids.
Code: [Select]
single A[4] = {5.0,0.0,6.6,3.1}
single B[4] = {1.1,2.0,2.2,4.0}
single C[4]

lea eax,A
lea edx,B

movups xmm0,[eax]
movups xmm1,[edx]

addps  xmm0,xmm1
movups C,xmm0

print Str(C[1],4)
print Str(C[2],4)
print Str(C[3],4)
print Str(C[4],4)


Peter

  • Guest
Re: Arithmetic with the SIMD registers
« Reply #10 on: January 24, 2014, 05:02:25 AM »
Simple shuffling
Code: [Select]
single A[4] = {1.5,1.6,1.7,1.8}
single B[4] = {1.1,2.1,3.1,4.1}
single C[4]

lea eax,A
lea edx,B

movups xmm0,[eax]              
MOVUPS xmm1,[edx]    

MOVAPS xmm2,xmm0              
MOVAPS xmm3,xmm1              

SHUFPS xmm0,xmm0,0xD8      
SHUFPS xmm1,xmm1,0xE1      
MULPS  xmm0,xmm1            
                
SHUFPS xmm2,xmm2,0xE1    
SHUFPS xmm3,xmm3,0xD8    
MULPS  xmm2,xmm3            
              
subps  xmm0,xmm2          
movups C,xmm0    

print Str(C[1],8)
print Str(C[2],8)
print Str(C[3],8)
print Str(C[4],8)