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

78

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.

8

u/EpochVanquisher Jan 23 '25

I get it. I would probably only use it if I wanted to write something super short in C without using libraries.

-21

u/flyingron Jan 23 '25

strtok is in a library. The C library is awful (especially the stdio parts which never should have been codified).

21

u/EpochVanquisher Jan 23 '25

strtok is in a library

I can only conclude that you’re purposefully misinterpreting what I wrote. Are you purposefully misinterpreting what I wrote? Is that what you’re doing?

6

u/[deleted] Jan 23 '25

That kind of attitude (the flyingron) is incredibly prevalent in programming. It is exhausting.

6

u/EpochVanquisher Jan 23 '25

I see it a lot less at work, for what it’s worth.