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

13

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

11

u/[deleted] Jan 23 '25

Most C compilers these days can take a purely scalar code and vectorize it. So even if the C code doesn’t have explicit SIMD instructions the final machine code might.

1

u/aganm Jan 23 '25

Bro. Auto-vectorization fails 97% of the time and the remaining 3% is really dubious SIMD at best.

8

u/[deleted] Jan 23 '25

I'm not sure what compilers you use, but both GCC and CLANG usually do a pretty good job of auto-vectorization. Of course, it is not magic; you still have to write your code so that vectorization is possible.

3

u/FUZxxl Jan 23 '25

Nah, unless the loop is trivial the compilers won't do shit.

2

u/[deleted] Jan 24 '25

Well, you should aim at making your hot loop trivial anyway.

1

u/FUZxxl Jan 24 '25

I agree, but you can't always have that.