Calculating Variance Using SIMD Registers

Started by Charles Pegge, March 13, 2024, 12:50:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

O2 uses the FPU to do its floating-point computations, but SIMD assembler is easy to incorporate for high performance data processing.

Variance plays an important role in Statistics and AI.

Data with values beyond the mean +/- 4 times the variance are considered statistically significant.

'variance using simd registers
==============================
float t      'total
float d[100] 'data set
float v      'variance
float t      'total
float m      'mean
float n      'data count
int i        'iterator
int e        'iterator end
'
string cr=chr(13,10)
'
'test figures
=============
d={1,2,2,4,5,6,3,4,7,9}
n=10
'
'calc total and mean
====================
t=0
int i
t=0
e=n
movss xmm0,t
float di
for i=1 to e
  di=d[i]
  addss xmm0,di
next
movss t,xmm0 'total
divss xmm0,n '
movss m,xmm0 'mean
'
'calc variance
==============
subss xmm0,xmm0 'nul
movss xmm1,m 'mean
for i=1 to e
  di=d[i]
  movss xmm2,di
  subss xmm2,xmm1  'deduct mean
  mulss xmm2,xmm2  'square
  addss xmm0,xmm2  'add variance to total
next
divss xmm0,n  'mean variance
movss v,xmm0  'store variance

'display results
================
print "count=" n cr+
      "total=" str(t,4) cr+
      "mean=" str(m,4) cr+
      "variance=" str(v,4)

'standard deviation sd is sqr(v)

Frank Brübach

Hi Charles found this little simd example and Convert it to oxygen

uses console

indexbase 1 '0

' Declare the size of the arrays
Const int N = 5

' Declare and initialize the arrays
Dim a(N) as single = {1.0, 2.0, 3.0, 4.0, 5.0} '1 to N
Dim b(N) as single = {6.0, 7.0, 8.0, 9.0, 10.0}
Dim c(N) as single

' Use inline assembly to add the arrays using SSE instructions
#Ifdef __x86_64__
    Dim xmm0 As __m128
    Dim xmm1 As __m128
    Dim xmm2 As __m128

    __asm
    {
        movups xmm0, [a]
        movups xmm1, [b]
        addps xmm0, xmm1
        movups [c], xmm0
    }
#EndIf

int i

' Print the result
For i = 1 to N
    Print c(i)
Next

print " ok results: " + c(i)
wait
  •