0

So, there is a task: the numbers are written in a file through a space, it is necessary to choose only positive ones and calculate their sum.

Will there be an acceleration when using simd comparison instructions with zero and addition? In theory, this should be so, but I'm too stupid to test this. Or will the auto-vectorizer handle this normally?

  • 5
    I would have though the file io burden would far outweigh the cpu work involved summing positive numbers that simd seems pointless here. What have you tried? What problems have you encountered? – keith Mar 12 '18 at 14:16
  • @keith you can technically parse integers with simd instructions :). Combining this with virtual mapping you can get something ... interesting. – Raxvan Mar 12 '18 at 14:27
  • @Raxvan Parsing with simd? How? – reydoposti Mar 12 '18 at 15:10
  • 1
    Are the numbers written as binary, or as text? If text, then parsing them is far more work than summing them, but see [How to implement atoi using SIMD?](https://stackoverflow.com/questions/35127060/how-to-implement-atoi-using-simd), which is what @Raxvan was talking about. You can even [parse an IPv4 dotted quad with SSSE3](https://stackoverflow.com/questions/31679341/fastest-way-to-get-ipv4-address-from-string), using x86 SSSE3 for `pshufb`, using a shuffle-control vector from a lookup table based on a `pcmpeqb` / `pmovmskb` result to find the `.` separator positions. – Peter Cordes Mar 12 '18 at 23:35
  • Conditional summing is easy by comparison, and compilers should generally do ok auto-vectorizing `int tmp = array[i]; sum += tmp= 0 ? tmp : 0`. e.g. for x86, you should get `pcmpeqd` / `pand` / `paddd`, because adding `0` is a no-op, so the compiler should mask the array elements using a compare result, so it adds 0 for the elements that fail the compare. – Peter Cordes Mar 12 '18 at 23:38

0 Answers0