1

Is there a more efficient way to get the sum of absolute differences of two memory blocks arrays of double in C/C++ than to perform a loop on the array elements? So, what I wonder is if there is a function for this purpose similar to memset, memcpy, memcmp etc.

How do you define the difference?

The memory blocks contain double values, and what I need is the sum of the absolute numerical differences between the values in the corresponding indexes of the memory blocks arrays.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2503950
  • 65
  • 1
  • 1
  • 6
  • 1
    How do you define the difference? – detly Jun 23 '13 at 02:55
  • The memory blocks contain double values, and what I need is the sum of the absolute numerical differences between the values in the corresponding indexes of the memory blocks. – user2503950 Jun 23 '13 at 03:15
  • 2
    If you're talking about doubles you're not talking about "memory blocks", you're talking about arrays. – Hot Licks Jun 23 '13 at 03:22
  • There are several x86 instructions (in SSE4.1) specifically devoted to sum-of-absolute-differences: M/PSADBW. Sadly, they operate on unsigned bytes, not doubles. –  Jun 23 '13 at 05:03

1 Answers1

1

Yes, there is SIMD. If you use GCC you can try adding things like -msse2 -O3 and see if it automatically generates SIMD instructions for you. If it does, or if you choose to use a library to do explicit SIMD, you can process four floats per instruction. To improve your chances, try to make your inner loop nice and simple so the optimizer can deal with it easily.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • What you're talking about here is called *autovectorization*. It's largely a crapshoot -- it will *occasionally* work, but it only works on simple loops, is highly dependent on the compiler version, and is frequently subject to bugs. It's almost always more effective to [use compiler intrinsics](http://stackoverflow.com/questions/661338/sse-sse2-and-sse3-for-gnu-c). –  Jun 23 '13 at 05:05
  • You can try stronger flags, such as `-msse4` or `-mavx`, if the processor you'll run on is new enough. – ugoren Jun 23 '13 at 05:36