Author Topic: Fast Normalize vector  (Read 14770 times)

0 Members and 1 Guest are viewing this topic.

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #20 on: April 17, 2007 »

here it is Paul

Code: [Select]
/'==================================================='/
'   using fast SSE to caculate a normale of vector
'         
'                  by Emil halim
'
/'==================================================='/


type Vector4
as single x,y,z,w
end type

type Vector3
as single x,y,z
end type

   dim as integer i
   dim as Vector4 in_vec(0 to 200)
   for i = 0 to 199
       in_vec(i).x = rnd * 10.0
       in_vec(i).y = rnd * 10.0
       in_vec(i).z = rnd * 10.0
       in_vec(i).w = 0.0
   next   

   dim as Vector3 out_vec(0 to 200)

   dim as integer Cycles1 , Cycles , save
   
   '
   ' normalizing vector
   '====================
   '
   
   ' FreeBasic code
   asm rdtsc          '  measure of time
   asm mov [save],Eax
   for i = 0 to 199
     dim as single Rec_len = 1.0 / sqr((in_vec(i).x*in_vec(i).x) + (in_vec(i).y*in_vec(i).y) + (in_vec(i).z*in_vec(i).z))
     in_vec(i).x *= Rec_len
     in_vec(i).y *= Rec_len
     in_vec(i).z *= Rec_len
   next
   asm rdtsc          '  measure of time
   asm SUB Eax, [save]
   asm mov [Cycles1],eax


   ' SSE asm code
   for i = 0 to 199
       in_vec(i).x = rnd * 10.0
    in_vec(i).y = rnd * 10.0
    in_vec(i).z = rnd * 10.0
    in_vec(i).w = 0.0
next   
   dim as integer in_addr  = @in_vec(0).x
   dim as integer out_addr = @out_vec(0).x
   asm
      rdtsc             '  measure of time
      mov [save],Eax         
      mov    esi , [in_addr]
      mov    edi , [out_addr]
      mov    ecx , 199
      shr    ecx , 1
     .lab:
      movups xmm0, [esi] 
      movups xmm3, [esi+16] 
      'prefetchnta  [esi+3*16]
      movaps xmm2, xmm0     
      movaps xmm5, xmm3       
      mulps  xmm0, xmm0
      mulps  xmm3, xmm3           
      movaps xmm1, xmm0  'ddccbbaa
      movaps xmm4, xmm3
      shufps xmm0, xmm1,0b01001110
      shufps xmm3, xmm4,0b01001110
      addps  xmm0, xmm1   
      addps  xmm3, xmm4
      movaps xmm1, xmm0
      movaps xmm4, xmm3
      shufps xmm1, xmm1,0b00010001
      shufps xmm4, xmm4,0b00010001
      addps  xmm0, xmm1   
      addps  xmm3, xmm4
      rsqrtps xmm0, xmm0   
      rsqrtps xmm3, xmm3
      mulps  xmm2, xmm0   
      mulps  xmm5, xmm3
      movups [edi], xmm2
      movups [edi+12], xmm5
      'prefetchnta   [edi+3*12]
      add esi,4*4*2
      add edi,3*4*2
      dec ecx
      jnz .lab   
      rdtsc             '  measure of time
      sub Eax, [save]
      mov [Cycles],eax
   end asm

  'test the results
   i = 10
   print in_vec(i).x , in_vec(i).y , in_vec(i).z , in_vec(i).w
   print out_vec(i).x , out_vec(i).y , out_vec(i).z
   
   dim as single v_len =  sqr((out_vec(i).x*out_vec(i).x) + (out_vec(i).y*out_vec(i).y) + (out_vec(i).z*out_vec(i).z))
   print v_len  ' must be 1.0
   
   print "SSE       time = " ; Cycles
   print "FreeBasic time = " ; Cycles1
   
   Do
   Loop


Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Fast Normalize vector
« Reply #21 on: April 17, 2007 »
Even better result :D

 2.655853      0.7265174     1.123683      0
 0.893011      0.2442862     0.3778302
 0.9999501
SSE       time =  3851
FreeBasic time =  16128

thats 4.1880031160737470786808621137367* faster :)
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #22 on: April 17, 2007 »

great news.  :)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Fast Normalize vector
« Reply #23 on: April 21, 2007 »
Not much luck with c.l.a.x86 :(
I tried the code on mine and got about 3x speed.  It's interesting to run it a few times and see the different results.  When I made it do 20000 instead of 200 I get something more reliable.

Jim
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #24 on: April 21, 2007 »

No luck with Comp.lang.asm ,  :(

I have posted there but get no answer.

I think that , the effect of SEE is noticable when you use a huge calculations , just as your test.

so i consider it is good news too. :)   

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Fast Normalize vector
« Reply #25 on: April 27, 2007 »
Man, this makes me want to learn ASM really, really bad. Good job!  :clap:

SSE       time =  3933
FreeBasic time =  47493

The Dr. is INsane!!!

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #26 on: April 28, 2007 »
that is great news too.  :)

Edited:

it is faster by 12 times , !!!!!!!!!!

did you test it more than one and took the avr or what ?

what is the config of your system ?
« Last Edit: April 28, 2007 by Emil_halim »

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Fast Normalize vector
« Reply #27 on: April 28, 2007 »
Well, I just ran it a few times and just posted the results from the last one. They were all very good though. ;)

I'm running xp sp2 with a 2gig sempron and 512mb ddr sdram.
The Dr. is INsane!!!

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #28 on: April 28, 2007 »
ok, that really nice , i think AMD will beat Intel in the few next monthes.

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Fast Normalize vector
« Reply #29 on: April 28, 2007 »
i made a little demo of a skeletal animation a while back. Do you mind if I stick this code in there to test it with a real application?  :cheers:
The Dr. is INsane!!!

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Fast Normalize vector
« Reply #30 on: April 28, 2007 »

Sure man , use it as you want.  :)

If I have some times I will do a matrix mutably too with SEE and of course you can use it too.

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Fast Normalize vector
« Reply #31 on: April 28, 2007 »
Man, that would be awesome! :D I really need to take a class on asm programming. :-\
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Fast Normalize vector
« Reply #32 on: April 28, 2007 »
I really need to take a class on asm programming. :-\

We have an asm forum here so we can deal with your questions :)
Shockwave ^ Codigos
Challenge Trophies Won: