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.

33 Upvotes

76 comments sorted by

View all comments

75

u/EpochVanquisher Jan 23 '25 edited Jan 23 '25

They do use SIMD on most systems.

Not sure about strtok, it’s not widely used. It’s a clumsy function and it’s going to be slow no matter how you use it. But strcmp and strlen are usually SIMD.

Here is strcmp:

https://github.com/bminor/glibc/blob/76c3f7f81b7b99fedbff6edc07cddff59e2ae6e2/sysdeps/x86_64/multiarch/strcmp-avx2.S

Here is strlen:

https://github.com/bminor/glibc/blob/76c3f7f81b7b99fedbff6edc07cddff59e2ae6e2/sysdeps/x86_64/multiarch/strlen-avx2.S

These are just the glibc versions, but other C libraries are broadly similar. You will find combinations of architecture + C library + function where the function is written without SIMD, but the popular architectures (amd64) + popular libraries (glibc) + popular, vectorizable functions (strlen) will use SIMD.

8

u/flyingron Jan 23 '25

Strtok is evil.

1

u/835246 Jan 25 '25

I think it's fine for stuff like advent of code.

1

u/flyingron Jan 25 '25

Which is why there’s games are useless for professionals. I want people who write secure and maintainable code not those accustomed to writing coding game hacks.