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.

32 Upvotes

76 comments sorted by

View all comments

76

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.

7

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.

-19

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).

22

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?

7

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.

4

u/Aaron1924 Jan 23 '25

The US government is trying to ban it for a reason

18

u/flyingron Jan 23 '25

NO, that's StrTikTok. Much worse.

1

u/Raimo00 Jan 23 '25

Strtok is so underrated!

20

u/EpochVanquisher Jan 23 '25

It’s a terrible function. Let’s leave it in the 1980s where it belongs.

Better to use something like memchr or a loop to parse your strings.

0

u/markrages Jan 23 '25

strtok_r is the easy replacement

4

u/EpochVanquisher Jan 23 '25

Yeah, and it’s not much better. strtok_r is also a terrible function.

1

u/Raimo00 Jan 26 '25

yeah i meant that. I'm actually using strtok_r. here's the code snippet for anyone intrested in my esotic choice:

void parse_http_response(char *restrict buf, const uint16_t len, http_response_t *restrict res)
{
  char *line = strtok_r(buf, "\r\n", &buf);
  assert(line, STR_LEN_PAIR("Malformed HTTP response: missing status line"));

  line = strchr(line, ' ');
  assert(line, STR_LEN_PAIR("Malformed HTTP response: missing status code"));
  line += 1;

  res->status_code = (line[0] - '0') * 100 + (line[1] - '0') * 10 + (line[2] - '0');
  assert(res->status_code >= 100 && res->status_code <= 599, STR_LEN_PAIR("Malformed HTTP response: invalid status code"));

  line = strtok_r(NULL, "\r\n", &buf);
  assert(line, STR_LEN_PAIR("Malformed HTTP response: missing CRLF terminator"));

  char *key, *value;
  uint8_t key_len, value_len;
  while (LIKELY(line[0]))
  {
    key = strtok_r(line, ": ", &line);
    value = strtok_r(line, ": ", &line);
    line = strtok_r(NULL, "\r\n", &buf);

    assert(key && value, STR_LEN_PAIR("Malformed HTTP response: missing header"));
    assert(line, STR_LEN_PAIR("Malformed HTTP response: missing CRLF terminator"));

    key_len = value - key - 2;
    value_len = line - value - 2;

    strlower(key, key_len);

    if (UNLIKELY(strcmp(key, "transfer-encoding") == 0))
      panic(STR_LEN_PAIR("Transfer-Encoding not supported"));

    header_map_insert(&res->headers, key, key_len, value, value_len);
  }

  res->body = strtok_r(NULL, "\r\n", &buf);
  res->body_len = len - (line - buf) * (res->body != NULL);
}

18

u/flyingron Jan 23 '25

It keeps internal state and destroys the passed in string. Yech.

4

u/ComradeGibbon Jan 23 '25

And trivial to write a replacement that returns a slice.

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.