• Welcome to Jose's Read Only Forum 2023.
 

How to subtract 2 vectors via SIMD?

Started by Michael Hartlef, April 10, 2009, 12:48:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Michael Hartlef

Hi folks,

I try to code a vector subtract sub in assembler. But when I call it, I get a GPF. What I am doing wrong?


  TYPE tpoint
     x AS SINGLE
     y AS SINGLE
  END TYPE
 
  DIM v1, v2,v3 AS TPOINT

  SUB vec2Sub(a AS tpoint, b AS tpoint, c AS tpoint)
    ! MOVUPS XMM0, [eax]
    ! SUBPS  XMM0, [ebx]
    ! MOVUPS [ecx], XMM0
  END SUB

  v1.x = 2.0
  v1.y = 1.5


  v2.x = 0.7
  v2.y = 1.2


  vec2sub(v1,v2,v3)


Charles Pegge


Hi Michael.

Good question! Most of the SIMD instructions require their memory operands to be aligned to 16 byte boundaries, which does rather restrict their general usage. The compromise, I will use in Oxygen is to load the operand into xmm7 (or some other xmm?) before prior to doing any arithmetic on it.

btw: MOVUPS accepts unaligned data MOVAPS does not.

Another consideration is accessing variables passed BYREF. I normally use VARPTR to resolve each address, not making any assumptions as to how the compiler will treat an BYREF variable within the assembler.

Charles


TYPE tpoint
     x AS SINGLE
     y AS SINGLE
  END TYPE
 
  DIM v1, v2,v3 AS TPOINT

  SUB vec2Sub(a AS tpoint, b AS tpoint, c AS tpoint)
    local pa,pb,pc as long
    pa=varptr(a)
    pb=varptr(b)
    pc=varptr(c)
    ! mov eax,pa
    ! MOVUPS XMM0, [eax]
    ! mov eax,pb
    ! MOVUPS XMM7, [eax]
    ! SUBPS  XMM0, XMM7
    ! mov eax,pc
    ! MOVUPS [eax], XMM0
  END SUB

  v1.x = 2.0
  v1.y = 1.5


  v2.x = 0.7
  v2.y = 1.2


  vec2sub(v1,v2,v3)


Michael Hartlef

Thanks Charles,

that will give me a good start.

Michael