Post · 11 min read· pp. 19–29

SSE4 string search — modification of Karp-Rabin algorithm

This article is obsolete, refer to SIMD-friendly algorithms for substring searching.

Contents

Introduction

String search is a common task in text processing. There are many algorithms that try to minimize number of exact comparing substrings.

One of them is Karp-Rabin algorithm — char-wise comparison is performed only when values of hash function calculated for a part of text and a substring are equal.

SSE4 introduced complex instruction MPSADBW which calculate eight Manhattan distances (L1) between given 4-byte vector and 8 subsequent vectors; if the distance is zero, then vectors are equal.

The idea of modification is to use equality of 4-byte substring's prefix instead of hash values equality. MPSADBW is fast, it has latency 4 cycles and throughput 2 cycles. Even if latency is not compensated, overall performance is very promising — 0.5 cycle per one 4-byte vectors comparison.

Unfortunately there are three disadvantages:

  1. Searching a substring shorter than 4 chars need some additional work.
  2. A hash is calculated for whole substring, MPSADBW consider just 4-byte prefix, thus the number of false-negative alarms could be greater.
  3. At least the length of a substring must be known. In the sample application text length is also given — this make program shorter and faster.

Algorithm outline

  1. Load 4-byte prefix of substring (done once):

    text

movdqu (%%eax), %%xmm0 ; xmm0 = "ABCD"

code
2. Load next 16 bytes of text (MPSADBW needs first 11 bytes):

   ```text
movdqu  (%%esi), %%xmm1         ; xmm1 = "ABCD__ABCD______"
                                ;         0123456789abcdef <- index

addl    $8, %%esi               ; advance pointer
  1. Locate prefix — result is vector of 8 words:

    text

mpsadbw $0, %%xmm0, %%xmm1 ; ABCD__ABCD______ ; ABCD => xmm1[0] := 0 ; ABCD => xmm1[1] := x > 0 ; ABCD => xmm1[2] := x > 0 ; ABCD => xmm1[3] := x > 0 ; ABCD => xmm1[4] := x > 0 ; ABCD => xmm1[5] := x > 0 ; ABCD => xmm1[6] := 0 ; ABCD => xmm1[7] := x > 0

code
4. MPSADBW result is converted to word-mask:

   ```text
pcmpeqw ZEROS, %%xmm1           ; xmm1 = [ffff|0000|0000|0000|0000|0000|fffff|0000]
  1. If xmm1 is equal zero, goto 2.

  2. Otherwise perform exact comparison of a substring and a text: words of value 0xffff indicate positions where the prefix is located. Comparison could be done with built-in functions like strncmp or memcmp.

    However since the length of substring is known we can use few SSE instructions to compare short strings, up to 36 chars.

Implementation

sse4_strstr.c include five functions:

  • sse4_strstr_any — is the most generic procedure, it uses MPSADBW to locate possible positions of substring, but comparison is done by a standard library function strncmp.
  • sse4_strstr_len4 — is the most specialized, it accepts only strings of length 4 chars, and no additional comparisons are needed.
  • sse4_strstr_len3 — this function accepts only strings of length 3 chars, and no additional comparisons are needed.
  • sse4_strstr_max20 — this function accept strings of length in range 4..20 chars. Substring's suffix of length 0..16 chars is loaded just once into a single XMM register, and comparison is done with few assembler instructions.
  • sse4_strstr_max36 — this function is similar to previous one: it stores suffix in two XMM register, comparison is also done with few assembler instructions.

There is a wrapper sse4_strstr that calls proper function depending on length of s1.

All functions fits following declaration:

text
foo(char* s1, int n1, char* s2, int n2);

Note: these functions were not well optimized, I guess there are room for some improvements, but at the moment don't have any ideas.

Performance tests

The application (sse4_strstr.c) loads some text file, and in a loop perform repeat substring searching using built-in function strstr or sse4_strstr wrapper.

With assistance of Python script following tests were done. Script loads desired text and picks 3*59 substring to locate. Substrings have length from 4 to 63 chars, their positions are selected from 3 different ranges:

  • first 64 bytes (short offsets, 0..63)
  • first 1kB (medium offsets, 64..1023)
  • rest of text (long offsets, 1024..)

Then results of built-in strstr and sse4_strstr are compared and if matched, performance tests were done. First strstr is measured — the number of iterations is adjusted to make running time longer than one second. Then sse4_strstr is timed using the same number of iterations.

Tests have been done on Core 2 Duo E8200 clocked at 2.6GHz, under Linux control. Article Assembly language from Wikipedia has been used, it has 38 kilobytes.

Results

Speedup mostly depend on the offset, i.e. position of s1 the first occurrence in a text.

chart

offsetsspeedup
minavgmax
short0.972.204.75
medium2.454.678.04
long3.796.2110.47

Short offsets

s1 offsets1 lengthstrstr timeSSE4_strstr timespeedup
041.350s1.390s0.97
1253.460s2.030s1.70
3064.540s2.680s1.69
3975.740s2.640s2.17
2984.820s2.630s1.83
092.770s1.860s1.49
40108.800s2.840s3.10
63119.560s3.970s2.41
29125.760s3.370s1.71
61131.080s0.320s3.38
24145.720s2.430s2.35
27156.350s2.960s2.15
39167.910s3.050s2.59
34177.520s3.020s2.49
42188.950s3.320s2.70
60191.490s0.390s3.82
18207.500s2.840s2.64
61211.290s0.430s3.00
8227.500s2.620s2.86
40231.240s0.430s2.88
21249.530s3.170s3.01
53251.150s0.430s2.67
34261.010s0.350s2.89
27271.050s0.380s2.76
44281.500s0.380s3.95
45291.530s0.460s3.33
26301.230s0.350s3.51
35311.160s0.350s3.31
57321.490s0.480s3.10
64331.560s0.400s3.90
15341.070s0.240s4.46
13351.090s0.320s3.41
49361.460s0.400s3.65
36371.370s0.590s2.32
31381.320s0.620s2.13
43391.440s0.630s2.29
18401.340s0.590s2.27
11411.220s0.580s2.10
40421.760s0.720s2.44
41431.760s0.720s2.44
59441.960s0.720s2.72
30451.520s0.650s2.34
45462.000s0.730s2.74
60472.280s0.760s3.00
4481.370s0.640s2.14
13491.530s0.660s2.32
25501.700s0.730s2.33
29511.660s0.720s2.31
42521.840s0.760s2.42
6531.540s0.680s2.26
19541.720s0.740s2.32
42551.920s0.780s2.46
43563.780s0.790s4.78
30571.860s0.760s2.45
8581.730s0.770s2.25
8591.730s0.760s2.28
59602.410s0.890s2.71
60612.680s0.880s3.05
23621.900s0.830s2.29
61632.480s0.900s2.76

chart

Min: 0.97, max: 4.78, average: 2.20

Medium offsets

s1 offsets1 lengthstrstr timeSSE4_strstr timespeedup
69949.860s1.940s5.08
55652.900s0.950s3.05
12061.870s0.480s3.90
69171.040s0.190s5.47
20283.380s0.670s5.04
46493.850s1.020s3.77
73101.450s0.360s4.03
362115.170s1.720s3.01
790121.680s0.220s7.64
207133.050s0.670s4.55
664141.020s0.220s4.64
321156.750s1.010s6.68
189162.970s0.700s4.24
539177.970s1.620s4.92
634189.330s1.870s4.99
214192.890s1.180s2.45
643206.850s1.900s3.61
416216.270s1.330s4.71
714221.020s0.200s5.10
278235.430s0.960s5.66
638249.230s1.960s4.71
549256.540s1.060s6.17
403266.420s1.230s5.22
736277.570s0.960s7.89
576286.990s1.780s3.93
273294.120s0.960s4.29
767308.000s2.440s3.28
184313.760s0.740s5.08
468328.680s1.740s4.99
747331.060s0.210s5.05
753349.390s2.510s3.74
847351.240s0.230s5.39
359365.990s1.470s4.07
927371.660s0.280s5.93
931389.110s2.900s3.14
643397.380s2.180s3.39
871401.580s0.310s5.10
121412.750s0.840s3.27
630421.010s0.220s4.59
419436.500s1.980s3.28
565445.670s0.970s5.85
764455.700s1.020s5.59
681461.260s0.240s5.25
331475.790s1.780s3.25
561487.720s0.960s8.04
879491.380s0.280s4.93
929501.050s0.330s3.18
845511.860s0.270s6.89
397529.450s1.640s5.76
128534.180s0.980s4.27
155543.040s1.070s2.84
777554.640s1.280s3.62
254564.980s2.020s2.47
293575.590s1.420s3.94
387589.520s2.180s4.37
710596.060s0.950s6.38
1023601.950s0.330s5.91
447617.370s1.810s4.07
562624.800s0.950s5.05
181634.180s1.190s3.51

chart

Min: 2.45, max: 8.04, average: 4.67

Long offsets

s1 offsets1 lengthstrstr timeSSE4_strstr timespeedup
965541.100s0.220s5.00
2511353.800s0.610s6.23
3395864.940s0.840s5.88
469678.720s1.840s4.74
389086.010s1.200s5.01
2364996.100s1.000s6.10
9118101.300s0.220s5.91
12530112.530s0.300s8.43
15698121.830s0.380s4.82
24124134.190s0.590s7.10
9425141.860s0.230s8.09
24289153.020s0.590s5.12
8296161.350s0.200s6.75
29318176.880s0.720s9.56
3785184.280s0.950s4.51
19056194.970s0.680s7.31
29172204.720s0.720s6.56
30185218.220s0.960s8.56
35429223.300s0.860s3.84
30084234.030s0.850s4.74
21020242.470s0.510s4.84
1940253.030s0.580s5.22
32933263.840s0.960s4.00
28921272.820s0.740s3.81
24984282.830s0.620s4.56
12179291.780s0.300s5.93
8794308.940s2.190s4.08
26064316.700s0.640s10.47
26132325.140s0.640s8.03
37875334.230s0.950s4.45
36593344.430s1.110s3.99
31745353.400s0.780s4.36
10705361.420s0.260s5.46
30480375.150s0.760s6.78
15060383.290s0.430s7.65
28798393.080s0.710s4.34
30460407.170s0.750s9.56
3355414.890s0.870s5.62
20004425.130s0.660s7.77
31452437.580s0.780s9.72
30040443.660s0.750s4.88
8599452.020s0.210s9.62
37178461.020s0.140s7.29
35331473.480s0.860s4.05
24538489.340s0.960s9.73
31491496.310s0.790s7.99
9371501.230s0.240s5.12
11476511.100s0.290s3.79
2153524.740s0.960s4.94
36412535.780s0.900s6.42
15820543.230s0.420s7.69
18578553.340s0.730s4.58
17378566.930s1.500s4.62
22165573.180s0.550s5.78
21275582.060s0.520s3.96
14870593.460s0.360s9.61
26933603.800s0.890s4.27
16482613.990s0.450s8.87
5921627.380s1.620s4.56
13398633.370s0.330s10.21

chart

Min: 3.79, max: 10.47, average: 6.21

Downloads

All files are available at github repository. Subdirectory original contains a 32-bit code from 2008, the root directory contains fresh C++11 programs.

See also