Author Topic: How to subtract 2 vectors via SIMD?  (Read 11091 times)

0 Members and 1 Guest are viewing this topic.

Offline Michael Hartlef

  • Newbie
  • *
  • Posts: 2
  • User-Rate: +0/-1
  • I'm gone
How to subtract 2 vectors via SIMD?
« on: April 10, 2009, 12:48:36 PM »
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?

Code: [Select]
  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)

« Last Edit: April 10, 2009, 01:27:24 PM by Michael Hartlef »

Online Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 893
  • User-Rate: +33/-1
    • Charles Pegge
Re: How to subtract 2 vectors via SIMD?
« Reply #1 on: April 10, 2009, 02:42:39 PM »

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

Code: [Select]
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)

Offline Michael Hartlef

  • Newbie
  • *
  • Posts: 2
  • User-Rate: +0/-1
  • I'm gone
Re: How to subtract 2 vectors via SIMD?
« Reply #2 on: April 10, 2009, 03:05:53 PM »
Thanks Charles,

that will give me a good start.

Michael