[libc] Adds string compare (strcmp) implementation to the libc string library.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
libc/src/string/strcmp.cpp | ||
---|---|---|
15 | Is there benefit in comparing words at a time? I know that it leads to potential OOB reads, so we can handle it separately. But if it can lead to speed up, just a leave a TODO about it and we will get to it in a later round. | |
22 | Use reinterpret cast instead of C style casts. | |
libc/test/src/string/strcmp_test.cpp | ||
11 | Where relevant in the following tests, can you add a check with the operands reversed? |
libc/src/string/strcmp.cpp | ||
---|---|---|
15 | I would suggest to opt for more descriptive variable names over single letter one. i.e ("left" instead of "l"). |
libc/src/string/strcmp.cpp | ||
---|---|---|
15 | What is the standard for leaving TODOs? I'm not finding anything in the llvm Coding Standards. |
Add TODO for word comparison, add better variable names, add operands reversed to tests, removed unnecessary header.
As @abrachet has said, you should consider adding a fuzz target as a follow up. Also, before committing, please update the commit message as follows:
[libc] Add strcmp implementation.
The [libc] prefix is important (and even I miss it sometimes.)
libc/src/string/strcmp.cpp | ||
---|---|---|
15 |
Technically we have to read the buffers byte per byte as we're not supposed to read past the \0. |
@tschuett I didn't dig into the address granularity, thank you for mentioning it.
Problem here is that we have two pointers to read from so this makes for the following logic:
- both pointers are aligned: we can use 16B loads,
- both pointers are unaligned of the same amount, we can load the first few bytes up to the next 16B boundary and then load 16B at a time,
- pointers have unrelated alignment, not much we can do...
On top of this the return of investment of the "align + load 16B chunks" strategy heavily depends on the size of the two strings - which we can't know in advance since they're 0 terminated.
If on average strings are a few tens of bytes the added complexity will never pay off.
This is different from memcmp which provides the size argument that we can use to decide the best strategy in advance.
So it's unclear whether the added complexity will yield any substantial benefit over a simple version that also uses less space in the L1 instruction cache.
libc/src/string/strcmp.cpp | ||
---|---|---|
15 |
So, going by that, we cannot employ similar techniques (of comparing word lengths at a time) even for functions like strlen. Just to note, few other libcs take such an approach with strlen. |
libc/src/string/strcmp.cpp | ||
---|---|---|
15 |
My understanding is that reading memory past the null character is undefined behavior. 7.21.1.1 p325 of the C standard: These optimizations work for now on a particular implementation but the standard does not make any guarantees whatsoever. As a matter of fact neither GCC nor Clang try to vectorize this code with optimization turned on. |
No need for this include