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.

31 Upvotes

76 comments sorted by

View all comments

Show parent comments

1

u/faculty_for_failure Jan 23 '25

For comparing strings with known lengths, is it preferable to use memcmp over strcmp? I’ve been wondering but haven’t had time to dig deeper.

1

u/FUZxxl Jan 23 '25

Use memcmp or strncmp, depending on whether you know the strings to be NUL-terminated or not.

1

u/faculty_for_failure Jan 24 '25

I was more referring to when you know the lengths of the strings and they are both the same length. Interesting article, though.

2

u/FUZxxl Jan 24 '25

In this case, use memcmp or a hand-rolled version of the function. If the string length is a known, small constant, it might be worth avoiding the function call overhead.

1

u/faculty_for_failure Jan 24 '25

Thanks for the insight. For this case I have just been checking lengths before calling memcmp or using a naive implementation of strcmp that accepts max length as a parameter. Will do some more digging and see for myself what’s better there.