Coverage Report

Created: 2025-12-27 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/wsutil/ws_mempbrk_sse42.c
Line
Count
Source
1
/* strcspn with SSE4.2 intrinsics
2
   Copyright (C) 2009-2014 Free Software Foundation, Inc.
3
   Contributed by Intel Corporation.
4
   This file is part of the GNU C Library.
5
6
   SPDX-License-Identifier: LGPL-2.1-or-later
7
*/
8
9
10
#include "config.h"
11
12
#ifdef HAVE_SSE4_2
13
14
#include <glib.h>
15
#include "ws_cpuid.h"
16
17
#ifdef _WIN32
18
  #include <tmmintrin.h>
19
#endif
20
21
#include <nmmintrin.h>
22
#include <string.h>
23
#include "ws_mempbrk.h"
24
#include "ws_mempbrk_int.h"
25
26
/* __has_feature(address_sanitizer) is used later for Clang, this is for
27
 * compatibility with other compilers (such as GCC and MSVC) */
28
#ifndef __has_feature
29
#   define __has_feature(x) 0
30
#endif
31
32
107k
#define cast_128aligned__m128i(p) ((const __m128i *) (const void *) (p))
33
34
/* Helper for variable shifts of SSE registers.
35
   Copyright (C) 2010 Free Software Foundation, Inc.
36
 */
37
38
static const int8_t ___m128i_shift_right[31] =
39
  {
40
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
41
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
42
  };
43
44
static inline __m128i
45
__m128i_shift_right (__m128i value, unsigned long int offset)
46
42.7k
{
47
  /* _mm_loadu_si128() works with unaligned data, cast safe */
48
42.7k
  return _mm_shuffle_epi8 (value,
49
42.7k
                           _mm_loadu_si128 (cast_128aligned__m128i(___m128i_shift_right + offset)));
50
42.7k
}
51
52
53
void
54
ws_mempbrk_sse42_compile(ws_mempbrk_pattern* pattern, const char *needles)
55
287
{
56
287
    size_t length = strlen(needles);
57
58
287
    pattern->use_sse42 = ws_cpuid_sse42() && (length <= 16);
59
60
287
    if (pattern->use_sse42) {
61
273
        pattern->mask = _mm_setzero_si128();
62
273
        memcpy(&(pattern->mask), needles, length);
63
273
    }
64
287
}
65
66
/* We use 0x2:
67
        _SIDD_SBYTE_OPS
68
        | _SIDD_CMP_EQUAL_ANY
69
        | _SIDD_POSITIVE_POLARITY
70
        | _SIDD_LEAST_SIGNIFICANT
71
   on pcmpistri to compare xmm/mem128
72
73
   0 1 2 3 4 5 6 7 8 9 A B C D E F
74
   X X X X X X X X X X X X X X X X
75
76
   against xmm
77
78
   0 1 2 3 4 5 6 7 8 9 A B C D E F
79
   A A A A A A A A A A A A A A A A
80
81
   to find out if the first 16byte data element has any byte A and
82
   the offset of the first byte.  There are 3 cases:
83
84
   1. The first 16byte data element has the byte A at the offset X.
85
   2. The first 16byte data element has EOS and doesn't have the byte A.
86
   3. The first 16byte data element is valid and doesn't have the byte A.
87
88
   Here is the table of ECX, CFlag, ZFlag and SFlag for 2 cases:
89
90
    1            X        1      0/1      0
91
    2           16        0       1       0
92
    3           16        0       0       0
93
94
   We exit from the loop for cases 1 and 2 with jbe which branches
95
   when either CFlag or ZFlag is 1.  If CFlag == 1, ECX has the offset
96
   X for case 1.  */
97
98
const char *
99
ws_mempbrk_sse42_exec(const char *haystack, size_t haystacklen, const ws_mempbrk_pattern* pattern, unsigned char *found_needle)
100
46.7k
{
101
46.7k
  const char *aligned;
102
46.7k
  int offset;
103
104
46.7k
  offset = (int) ((size_t) haystack & 15);
105
46.7k
  aligned = (const char *) ((size_t) haystack & -16L);
106
46.7k
  if (offset != 0)
107
42.7k
    {
108
      /* Check partial string. cast safe it's 16B aligned */
109
42.7k
      __m128i value = __m128i_shift_right (_mm_load_si128 (cast_128aligned__m128i(aligned)), offset);
110
111
42.7k
      int length = _mm_cmpistri (pattern->mask, value, 0x2);
112
      /* No need to check ZFlag since ZFlag is always 1.  */
113
42.7k
      int cflag = _mm_cmpistrc (pattern->mask, value, 0x2);
114
      /* XXX: why does this compare value with value? */
115
42.7k
      int idx = _mm_cmpistri (value, value, 0x3a);
116
117
42.7k
      if (cflag) {
118
20.8k
        if (found_needle)
119
20.4k
                *found_needle = *(haystack + length);
120
20.8k
        return haystack + length;
121
20.8k
      }
122
123
      /* Find where the NULL terminator is.  */
124
21.8k
      if (idx < 16 - offset)
125
11.0k
      {
126
         /* found NUL @ 'idx', need to switch to slower mempbrk */
127
11.0k
         return (const char*)ws_mempbrk_portable_exec((const uint8_t*)(haystack + idx + 1), haystacklen - idx - 1, pattern, found_needle); /* haystacklen is bigger than 16 & idx < 16 so no underflow here */
128
11.0k
      }
129
10.8k
      aligned += 16;
130
10.8k
      haystacklen -= (16 - offset);
131
10.8k
    }
132
4.01k
  else
133
4.01k
    aligned = haystack;
134
135
23.1k
  while (haystacklen >= 16)
136
21.4k
    {
137
21.4k
      __m128i value = _mm_load_si128 (cast_128aligned__m128i(aligned));
138
21.4k
      int idx = _mm_cmpistri (pattern->mask, value, 0x2);
139
21.4k
      int cflag = _mm_cmpistrc (pattern->mask, value, 0x2);
140
21.4k
      int zflag = _mm_cmpistrz (pattern->mask, value, 0x2);
141
142
21.4k
      if (cflag) {
143
5.49k
        if (found_needle)
144
5.38k
            *found_needle = *(aligned + idx);
145
5.49k
        return aligned + idx;
146
5.49k
      }
147
148
15.9k
      if (zflag)
149
7.68k
      {
150
         /* found NUL, need to switch to slower mempbrk */
151
7.68k
         return (const char*)ws_mempbrk_portable_exec((const uint8_t*)aligned, haystacklen, pattern, found_needle);
152
7.68k
      }
153
8.30k
      aligned += 16;
154
8.30k
      haystacklen -= 16;
155
8.30k
    }
156
157
    /* XXX, use mempbrk_slow here? */
158
1.69k
    return (const char*)ws_mempbrk_portable_exec((const uint8_t*)aligned, haystacklen, pattern, found_needle);
159
14.8k
}
160
161
#endif /* HAVE_SSE4_2 */
162
/*
163
 * Editor modelines
164
 *
165
 * Local Variables:
166
 * c-basic-offset: 2
167
 * tab-width: 8
168
 * indent-tabs-mode: nil
169
 * End:
170
 *
171
 * ex: set shiftwidth=2 tabstop=8 expandtab:
172
 * :indentSize=2:tabSize=8:noTabs=true:
173
 */