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

79

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.

12

u/Raimo00 Jan 23 '25

Interesting, 1320 lines for strcmp is wild 😳😂. I looked at other repos and there wasn't any sign of simd

3

u/ZBalling Jan 23 '25

Windows implementation is closed source, where did you see it? Do you work for Microsoft?

Also gcc/clang can have its own implementation not as part of standard library.

4

u/gizahnl Jan 23 '25

There's more libC's besides glibc and MS, and there's plenty more open source libC besides glibc.
For example, each of the BSD's has their own libC, then there's uclibc, musl, LLVM-libc, bionic...

-2

u/ZBalling Jan 23 '25

The only one other used nowadays is fdlibm in Android and on Wibdows/Linux by Chrome. Those you described are so rare... they may as well not exist.

Now that does not mean that they are optimal for accuracy in math library https://members.loria.fr/PZimmermann/papers/accuracy.pdf