Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#290838 - 26/11/2006 22:43 Boring C question
andym
carpal tunnel

Registered: 17/01/2002
Posts: 3996
Loc: Manchester UK
My google-fu is weak.....

I've got two arrays of floats, I need to add them together. Currently I'm doing it like so:

Code:

for (j = 0 ; j < numFrames ; j++)
tempVal[j] += buffer[j];



It works, but is there a quicker way?
_________________________
Cheers,

Andy M

Top
#290839 - 26/11/2006 22:50 Re: Boring C question [Re: andym]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14496
Loc: Canada
Quote:

for (j = 0 ; j < numFrames ; j++)
tempVal[j] += buffer[j];


Unroll the loop (some compilers may partially do this already for you), and reverse the loop index to count down to zero (saves 1-2 instructions, usually).

So, something like:Code:

// Assumes numFrames is multiple of 4
for ( j = numFrames - 4; j != 0; j -= 4) {
tempVal[j] = buffer[j];
tempVal[j+1] = buffer[j+1];
tempVal[j+2] = buffer[j+2];
tempVal[j+3] = buffer[j+3];
}


Switching to pointers rather than table indexes might save another couple of instructions per iteration.

But for *real* speed, one could use MMX/SSE(2) etc.. instructions. Any experts here?

Cheers

Top
#290840 - 26/11/2006 23:20 Re: Boring C question [Re: mlord]
andym
carpal tunnel

Registered: 17/01/2002
Posts: 3996
Loc: Manchester UK
I now remember reading about this ages ago. I think the article it was in was about early computer graphics or something. I did also consider using pointers too. Will give them both a try.

Thanks Mark!
_________________________
Cheers,

Andy M

Top
#290841 - 27/11/2006 07:36 Re: Boring C question [Re: mlord]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4180
Loc: Cambridge, England
Quote:
But for *real* speed, one could use MMX/SSE(2) etc.. instructions. Any experts here?

I'm no expert, but I do recall that modern GCC will use SSE instructions automatically in this sort of situation, especially if you've unrolled the loop in that way. It can't do that, though, unless it can prove that the two arrays don't overlap (consider "float buffer[101]; tempVal=&buffer[1]"). If one or more of buffer and tempVal is a local array variable, it can work out that they don't overlap; if this code is in a function where tempVal and buffer are both passed-in as float pointers, mark them "restrict" if you're sure they'll never overlap.

Peter

Top
#290842 - 27/11/2006 12:14 Re: Boring C question [Re: peter]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14496
Loc: Canada
Ahh.. good information there.

Also, gcc might not use MMX/SSE(2) instructions unless you explicitly tell it that it's okay to do so. On my Pentium-M notebook, I generally provide these flags: "-march=pentium-m -mfpmath=sse -msse2 -malign-double"

Top
#290843 - 28/11/2006 06:42 Re: Boring C question [Re: mlord]
julf
veteran

Registered: 01/10/2001
Posts: 1307
Loc: Amsterdam, The Netherlands
Quote:
On my Pentium-M notebook, I generally provide these flags: "-march=pentium-m -mfpmath=sse -msse2 -malign-double"


By the way, have you come across a tool that would test your CPU and find out it's capabilities (and tell what optimisations make sense)?

Top
#290844 - 28/11/2006 12:43 Re: Boring C question [Re: julf]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14496
Loc: Canada
Quote:
Quote:
On my Pentium-M notebook, I generally provide these flags: "-march=pentium-m -mfpmath=sse -msse2 -malign-double"


By the way, have you come across a tool that would test your CPU and find out it's capabilities (and tell what optimisations make sense)?


Under Linux: cat /proc/cpuinfo

Cheers

Top
#290845 - 28/11/2006 14:01 Re: Boring C question [Re: mlord]
andy
carpal tunnel

Registered: 10/06/1999
Posts: 5916
Loc: Wivenhoe, Essex, UK
Quote:

Under Linux: cat /proc/cpuinfo



Under Windows: http://www.cpuid.com/cpuz.php
_________________________
Remind me to change my signature to something more interesting someday

Top
#290846 - 28/11/2006 18:21 Re: Boring C question [Re: mlord]
julf
veteran

Registered: 01/10/2001
Posts: 1307
Loc: Amsterdam, The Netherlands
Quote:
Under Linux: cat /proc/cpuinfo


Duhh! Indeed, the first sign of Alzheimer's is... Hold on... It will come to me...

Top