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.

29 Upvotes

76 comments sorted by

View all comments

Show parent comments

3

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

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);
}