# Max and argmax blocks with SIMD instructions

**URL:** <https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711>\
**Category:** GNU Radio\
**Created:** [April 23, 2007, 10:50am UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711 "2007-04-23T10:50:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 23, 2007, 10:50am UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/1 "2007-04-23T10:50:52Z")

</div>

Hi everyone,

I’ve written a couple of blocks for GNU Radio, but am not satisfied  
with the performance. I am therefore thinking of using SIMD  
instructions. However, I am not that familiar with x86 assembly  
instructions, and finding the reference manual on Intel’s website was  
not easy. I know that DSPs such as the Blackfin has special vector  
instructions that would make this very simple, but I am not sure about  
x86.

I am also going to write a general purpose multiply and accumulate  
block that would benefit much from SIMD instructions.

Any comments are appreciated.

–  
Trond D.

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 23, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/2 "2007-04-23T16:42:26Z")

</div>

On Mon, Apr 23, 2007 at 10:48:58AM +0200, Trond D. wrote:

> I am also going to write a general purpose multiply and accumulate  
> block that would benefit much from SIMD instructions.
> 
> Any comments are appreciated.
> 
> –  
> Trond D.

Hi Trond,

Can you point us at your code? Before diving into SIMD, it would be  
good to confirm that there isn’t an easier change to make. Have you  
run oprofile on your code?

In general when going for a speed up, you want to be packaging enough  
cycles in the block to have it make a difference. I.e., I’m not sure  
that a general purpose multiple-accumulate (MAC) block is going to  
solve your problem. However, if you take a look at the gr\_fir\__.cc  
code, you’ll find that at the bottom of them they call out to SIMD  
assembler in {c,}complex\_dotprod\__.S that implements the kernel of the  
FIR filter. In those cases the equivalent of the MAC function is buried  
in an unrolled inner loop.

With SIMD programming, a lot of the complexity is figuring out how to  
schedule the loads and stores, since unless you’re careful, your  
performance is dominated by the memory hierarchy and not the math.

Also, on the x86 architecuture, there are not enough registers  
available to hide the load latencies. On the x86-64 it’s better,  
since you’ve got twice as many registers. For a comparison, on the  
Cell SPE you’ve got 128 (!) 128-bit registers. No shortage of  
registers there 😉

In addition to the “IA-32 Archicture Software Developer’s Manual” (I  
suspect that was the one you had trouble finding), you’ll want to look  
for the microarchitecture-specific optimization manuals. The one I’ve  
got in front of me, “Intel Pentium 4 and Intel Xeon Processor  
Optimization Reference Manual” (Order Number: 248966-04) isn’t the  
latest, but is an example. I suspect that there’s a new one out that  
covers the Pentium M, Core, Core Duo, Core 2 Duo, etc. AMD also has  
similar manuals. All these are on the vendor web sites, typically in  
the “developer” section somewhere.

Be sure to create meaningful benchmarks to measure the performance of  
your code. That’s a whole art into into itself.

When all is said and done, algorithmic changes often result in bigger  
wins than SIMD assembler. Be sure to look there first. In our  
case, the FFT based FIR code is faster than the hand-coded SIMD code  
for pretty much all cases where ntaps \>= 20.

Have fun!  
Eric

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 23, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/3 "2007-04-23T16:47:27Z")

</div>

If you don’t want to use assembly, you can use MMX and SSE intrisics  
compiler support. These are C functions/macros to allow the use of SSE  
instructions directly from C/C++. You can start with this introduction:  
[http://www.codeproject.com/cpp/sseintro.asp](http://www.codeproject.com/cpp/sseintro.asp) . For a reference, you can  
go on  
MSDN website and search for intrinsic . Even if it’s for Windows, it’s  
still  
valid for GNU. Have a look at this files on your computer (in your  
system  
include dir):  
mmintrin.h → MMX  
emmintrin.h → SSE2  
xmmintrin.h → SSE  
pmmintrin.h → SSE3

If you use it, don’t forget to use compilation macros to offer  
alternative  
for processor that don’t have these assembly instructions.

Pascal

PS From a personnal point of view, I don’t know if these instructions  
are as  
fast as assembly. But they are quite easy to use, which improve my code  
writing speed 😉

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 24, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/4 "2007-04-24T13:12:17Z")

</div>

2007/4/23, Eric B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > I am also going to write a general purpose multiply and accumulate  
> > block that would benefit much from SIMD instructions.
> > 
> > Any comments are appreciated.
> 
> Hi Trond,
> 
> Can you point us at your code? Before diving into SIMD, it would be  
> good to confirm that there isn’t an easier change to make. Have you  
> run oprofile on your code?

Thanks a lot for your answer, very enlightening!

The max and argmax blocks can be found here:  
ftp://open-gnss.org/pub/opengnss. If you find it useful I do not mind  
including them in GNU Radio.

I haven’t profiled the code, so really cannot verify that it is the  
main problem atm. I was just curious because I know that such  
instructions exist for other processors.

–  
Trond D.

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 24, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/5 "2007-04-24T13:19:17Z")

</div>

I have some C/C++ functions that utilize MMX and/or SSE available that  
work on 16 bit signed integers. The URL is:

[http://www.ngs.noaa.gov/gps-toolbox/Heckler.htm](http://www.ngs.noaa.gov/gps-toolbox/Heckler.htm)

They work particularly well for building a software correlator :).

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 24, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/6 "2007-04-24T13:15:17Z")

</div>

2007/4/23, Pascal C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If you don’t want to use assembly, you can use MMX and SSE intrisics  
> compiler support. These are C functions/macros to allow the use of SSE  
> instructions directly from C/C++.

Thanks a lot, I will definitely look into that, as i would pref ere to  
stay out of assembly land a little longer.

–  
Trond D.

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 25, 2007, 12:27am UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/7 "2007-04-25T00:27:13Z")

</div>

Another example of how to do this in a portable way is  
found in FFTW.  
Since fftw is prerequisite software to gnuradio most of us  
already have fftw on our computers.

FFTW supports SSE/SSE2/3dNow!/Altivec and uses which  
ever is available and fastest.

— Gregory W Heckler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Discuss-gnuradio mailing list  
> removed\_email\_address@domain.invalid  
> [Discuss-gnuradio Info Page](http://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

Chris Albertson  
Home: 310-376-1029 removed\_email\_address@domain.invalid  
Office: 310-336-5189 removed\_email\_address@domain.invalid

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[Yahoo Mail](https://mail.yahoo.com/)**
>
> Take a trip into an upgraded, more organized inbox. Sign in and start exploring all the free, organizational tools for your email. Check out new themes, send GIFs, find every photo you've ever sent or received, and search your account faster than...

---

<div class="post-metadata">

**Author:** ![Trond\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@Trond\_D](https://www.ruby-forum.com/u/Trond_D)\
**Post date:** [April 24, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/max-and-argmax-blocks-with-simd-instructions/92711/8 "2007-04-24T16:11:09Z")

</div>

Trond:

I, as well as GNU radio and others, need your copyright statement and  
your grant of a GPL license to this code IN the code and header modules.  
It would be best if you added this to your code as soon as possible.

THANK YOU for sharing this.

Bob

Trond D. wrote:

> > > x86.  
> > > run oprofile on your code?

–  
AMSAT Director and VP Engineering. Member: ARRL, AMSAT-DL,  
TAPR, Packrats, NJQRP, QRP ARCI, QCWA, FRC. ARRL SDR WG Chair  
“Taking fun as simply fun and earnestness in earnest shows  
how thoroughly thou none of the two discernest.” - Piet Hine
