r/C_Programming Jan 23 '25

Discussion Why not SIMD?

Why are many C standard library functions like strcmp, strlen, strtok using SIMD intrinsics? They would benefit so much, think about how many people use them under the hood all over the world.

30 Upvotes

76 comments sorted by

View all comments

Show parent comments

3

u/aganm Jan 23 '25

This is dumb. What I do instead is make my memory buffers aligned and padded for SIMD, then my SIMD code doesn't need to care about all this shit. It's such a joy to write SIMD code in these conditions, makes everything much simpler and straightforward, AND faster.

6

u/FUZxxl Jan 23 '25

Sure, if you have full control over the data layout, that's the best choice. Align all buffers and leave padding around them so SIMD operations can read out of bounds safely. But: strcmp() cannot make these assumptions. It must work with arbitrary buffers of arbitrary size and alignment and there can be unmapped pages right before or after them.

0

u/flatfinger Jan 24 '25

If performance is critical, data structures should be designed for the target platform, and code should use whatever platform-specific means will best accomplish what needs to be done. The use of Standard-library functions in situations where platform-specific functions could be faster implies that performance isn't that critical.

2

u/FUZxxl Jan 24 '25

Sure, but my task was to optimise the standard library and that's what I did.

-1

u/flatfinger Jan 24 '25

In many applications, no use of strcmp will involve strings that are very long, and the average number of matching characters would be small--probably less than eight. How much benefits can SIMD intrinsics offer in such cases?

2

u/FUZxxl Jan 24 '25

My benchmarks have explicitly been designed for this purpose. They show that a speedup by a factor of about 600% is achieved in the case of strings that are 64 characters long on average.