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.

4
  • 1
    How do you define the difference?
    – detly
    Jun 23, 2013 at 2: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. Jun 23, 2013 at 3:15
  • 2
    If you're talking about doubles you're not talking about "memory blocks", you're talking about arrays.
    – Hot Licks
    Jun 23, 2013 at 3: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.
    – user149341
    Jun 23, 2013 at 5:03

1 Answer 1

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.

2
  • 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.
    – user149341
    Jun 23, 2013 at 5:05
  • You can try stronger flags, such as -msse4 or -mavx, if the processor you'll run on is new enough.
    – ugoren
    Jun 23, 2013 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.