Compare strings by SSE4 wrappers
Asked Answered
T

3

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.

Transpacific answered 13/5, 2012 at 20:19 Comment(4)
it might depend on the compiler (including the version) and the flags used. If you use gcc-4.7 did you try -mcpu=native -O3 etc.?Wingback
software.intel.com/sites/products/documentation/studio/composer/…Adenocarcinoma
Googling "sse4 strcmp" gives a lot of results with ready made assembler code. You can try to translate those to C using intrinsics. Have fun.Shuddering
@Adenocarcinoma it's exactly that I need! Post it as answer.Transpacific
A
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

Adenocarcinoma answered 13/5, 2012 at 21:7 Comment(2)
And you are correct. They changed the link. Updated with the new one.Adenocarcinoma
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? (That question is about glibc's fallback C implementation, which is never used on x86.)Galliard
S
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.

Sacksen answered 3/6, 2013 at 14:5 Comment(0)
U
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

Unrequited answered 3/6, 2013 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.