6

I need to quickly compare two string on the machine with SSE4 support. How can I do it without writing assembler inserts?

Some wrappers like long long bitmask = strcmp(char* a, char* b) would be perfect.

Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70

3 Answers3

9

Instead of using inline assembly, you should use the Intel SSE intrinsics.

For string comparison, you'll need the SSE4.2 intrinsics:

Documentation is here: http://software.intel.com/sites/products/documentation/doclib/stdxe/2013/composerxe/compiler/cpp-mac/GUID-6E9CFDF2-5DF6-42A4-B429-0E2CD441342E.htm

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • And you are correct. They changed the link. Updated with the new one. – Mysticial Jan 28 '13 at 23:53
  • Note that SSE4.2 instructions are generally *not* worth using for strcmp or strlen. You can do better with SSE2 or especially AVX2, which is what glibc does in practice. See links at the top of [my answer on Why does glibc's strlen need to be so complicated to run quickly?](https://stackoverflow.com/a/57676035) (That question is about glibc's fallback C implementation, which is never used on x86.) – Peter Cordes Oct 26 '20 at 06:14
2

Use Agner Fog's asmlib. http://www.agner.org/optimize/#asmlib

He's already taken the trouble for writing the code in assembly for you including using SSE4.2 instructions. Use his function A_strcmp (or the case insensitive version A_stricmp).

It would be interesting so how a method using intrinsics compares in performance.

0

Here is a good article of using SSE 4.2 for boosting string operations: http://www.strchr.com/strcmp_and_strlen_using_sse_4.2