Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_sort.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright © Zend Technologies Ltd., a subsidiary company of          |
6
   |     Perforce Software, Inc., and Contributors.                       |
7
   +----------------------------------------------------------------------+
8
   | This source file is subject to the Modified BSD License that is      |
9
   | bundled with this package in the file LICENSE, and is available      |
10
   | through the World Wide Web at <https://www.php.net/license/>.        |
11
   |                                                                      |
12
   | SPDX-License-Identifier: BSD-3-Clause                                |
13
   +----------------------------------------------------------------------+
14
   | Authors: Xinchen Hui <laruence@php.net>                              |
15
   |          Sterling Hughes <sterling@php.net>                          |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "zend.h"
20
#include "zend_sort.h"
21
#include <limits.h>
22
23
3.18k
static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
24
3.18k
  if (cmp(a, b) > 0) {
25
2.15k
    swp(a, b);
26
2.15k
  }
27
3.18k
}
28
/* }}} */
29
30
120k
static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
31
120k
  if (!(cmp(a, b) > 0)) {
32
35.8k
    if (!(cmp(b, c) > 0)) {
33
22.0k
      return;
34
22.0k
    }
35
13.7k
    swp(b, c);
36
13.7k
    if (cmp(a, b) > 0) {
37
3.43k
      swp(a, b);
38
3.43k
    }
39
13.7k
    return;
40
35.8k
  }
41
84.6k
  if (!(cmp(c, b) > 0)) {
42
10.8k
    swp(a, c);
43
10.8k
    return;
44
10.8k
  }
45
73.8k
  swp(a, b);
46
73.8k
  if (cmp(b, c) > 0) {
47
36.2k
    swp(b, c);
48
36.2k
  }
49
73.8k
}
50
/* }}} */
51
52
11.7k
static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
53
11.7k
  zend_sort_3(a, b, c, cmp, swp);
54
11.7k
  if (cmp(c, d) > 0) {
55
7.49k
    swp(c, d);
56
7.49k
    if (cmp(b, c) > 0) {
57
2.63k
      swp(b, c);
58
2.63k
      if (cmp(a, b) > 0) {
59
1.75k
        swp(a, b);
60
1.75k
      }
61
2.63k
    }
62
7.49k
  }
63
11.7k
}
64
/* }}} */
65
66
8.58k
static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
67
8.58k
  zend_sort_4(a, b, c, d, cmp, swp);
68
8.58k
  if (cmp(d, e) > 0) {
69
7.26k
    swp(d, e);
70
7.26k
    if (cmp(c, d) > 0) {
71
6.69k
      swp(c, d);
72
6.69k
      if (cmp(b, c) > 0) {
73
1.80k
        swp(b, c);
74
1.80k
        if (cmp(a, b) > 0) {
75
565
          swp(a, b);
76
565
        }
77
1.80k
      }
78
6.69k
    }
79
7.26k
  }
80
8.58k
}
81
/* }}} */
82
83
116k
ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
84
116k
  switch (nmemb) {
85
0
    case 0:
86
4.11k
    case 1:
87
4.11k
      break;
88
3.18k
    case 2:
89
3.18k
      zend_sort_2(base, (char *)base + siz, cmp, swp);
90
3.18k
      break;
91
17.2k
    case 3:
92
17.2k
      zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
93
17.2k
      break;
94
3.19k
    case 4:
95
3.19k
      {
96
3.19k
        size_t siz2 = siz + siz;
97
3.19k
        zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
98
3.19k
      }
99
3.19k
      break;
100
7.84k
    case 5:
101
7.84k
      {
102
7.84k
        size_t siz2 = siz + siz;
103
7.84k
        zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
104
7.84k
      }
105
7.84k
      break;
106
81.0k
    default:
107
81.0k
      {
108
81.0k
        char *i, *j, *k;
109
81.0k
        char *start = (char *)base;
110
81.0k
        char *end = start + (nmemb * siz);
111
81.0k
        size_t siz2= siz + siz;
112
81.0k
        char *sentry = start + (6 * siz);
113
486k
        for (i = start + siz; i < sentry; i += siz) {
114
405k
          j = i - siz;
115
405k
          if (!(cmp(j, i) > 0)) {
116
116k
            continue;
117
116k
          }
118
548k
          while (j != start) {
119
456k
            j -= siz;
120
456k
            if (!(cmp(j, i) > 0)) {
121
197k
              j += siz;
122
197k
              break;
123
197k
            }
124
456k
          }
125
837k
          for (k = i; k > j; k -= siz) {
126
548k
            swp(k, k - siz);
127
548k
          }
128
288k
        }
129
557k
        for (i = sentry; i < end; i += siz) {
130
476k
          j = i - siz;
131
476k
          if (!(cmp(j, i) > 0)) {
132
50.1k
            continue;
133
50.1k
          }
134
798k
          do {
135
798k
            j -= siz2;
136
798k
            if (!(cmp(j, i) > 0)) {
137
402k
              j += siz;
138
402k
              if (!(cmp(j, i) > 0)) {
139
292k
                j += siz;
140
292k
              }
141
402k
              break;
142
402k
            }
143
395k
            if (j == start) {
144
4.71k
              break;
145
4.71k
            }
146
390k
            if (j == start + siz) {
147
18.6k
              j -= siz;
148
18.6k
              if (cmp(i, j) > 0) {
149
9.29k
                j += siz;
150
9.29k
              }
151
18.6k
              break;
152
18.6k
            }
153
390k
          } while (1);
154
1.76M
          for (k = i; k > j; k -= siz) {
155
1.33M
            swp(k, k - siz);
156
1.33M
          }
157
425k
        }
158
81.0k
      }
159
81.0k
      break;
160
116k
  }
161
116k
}
162
/* }}} */
163
164
/* {{{ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
165
 *
166
 * Derived from LLVM's libc++ implementation of std::sort.
167
 *
168
 * ===========================================================================
169
 * libc++ License
170
 * ===========================================================================
171
 *
172
 * The libc++ library is dual licensed under both the University of Illinois
173
 * "BSD-Like" license and the MIT license. As a user of this code you may
174
 * choose to use it under either license. As a contributor, you agree to allow
175
 * your code to be used under both.
176
 *
177
 * Full text of the relevant licenses is included below.
178
 *
179
 * ===========================================================================
180
 *
181
 * University of Illinois/NCSA
182
 * Open Source License
183
 *
184
 * Copyright (c) 2009-2012 by the contributors listed at
185
 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
186
 *
187
 * All rights reserved.
188
 *
189
 * Developed by:
190
 *
191
 *     LLVM Team
192
 *
193
 *     University of Illinois at Urbana-Champaign
194
 *
195
 *     http://llvm.org
196
 *
197
 * Permission is hereby granted, free of charge, to any person obtaining a copy
198
 * of this software and associated documentation files (the "Software"), to
199
 * deal with the Software without restriction, including without limitation the
200
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
201
 * sell copies of the Software, and to permit persons to whom the Software is
202
 * furnished to do so, subject to the following conditions:
203
 *
204
 *     * Redistributions of source code must retain the above copyright notice,
205
 *       this list of conditions and the following disclaimers.
206
 *
207
 *     * Redistributions in binary form must reproduce the above copyright
208
 *       notice, this list of conditions and the following disclaimers in the
209
 *       documentation and/or other materials provided with the distribution.
210
 *
211
 *     * Neither the names of the LLVM Team, University of Illinois at
212
 *       Urbana-Champaign, nor the names of its contributors may be used to
213
 *       endorse or promote products derived from this Software without
214
 *       specific prior written permission.
215
 *
216
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
217
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
219
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
220
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
221
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
222
 * WITH THE SOFTWARE.
223
 *
224
 * ===========================================================================
225
 *
226
 * Copyright (c) 2009-2012 by the contributors listed at
227
 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
228
 *
229
 * Permission is hereby granted, free of charge, to any person obtaining a copy
230
 * of this software and associated documentation files (the "Software"), to
231
 * deal in the Software without restriction, including without limitation the
232
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
233
 * sell copies of the Software, and to permit persons to whom the Software is
234
 * furnished to do so, subject to the following conditions:
235
 *
236
 * The above copyright notice and this permission notice shall be included in
237
 * all copies or substantial portions of the Software.
238
 *
239
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
240
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
242
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
243
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
244
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
245
 * IN THE SOFTWARE.
246
 */
247
ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
248
116k
{
249
208k
  while (1) {
250
208k
    if (nmemb <= 16) {
251
116k
      zend_insert_sort(base, nmemb, siz, cmp, swp);
252
116k
      return;
253
116k
    } else {
254
92.2k
      char *i, *j;
255
92.2k
      char *start = (char *)base;
256
92.2k
      char *end = start + (nmemb * siz);
257
92.2k
      size_t offset = (nmemb >> Z_L(1));
258
92.2k
      char *pivot = start + (offset * siz);
259
260
92.2k
      if ((nmemb >> Z_L(10))) {
261
738
        size_t delta = (offset >> Z_L(1)) * siz;
262
738
        zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
263
91.5k
      } else {
264
91.5k
        zend_sort_3(start, pivot, end - siz, cmp, swp);
265
91.5k
      }
266
92.2k
      swp(start + siz, pivot);
267
92.2k
      pivot = start + siz;
268
92.2k
      i = pivot + siz;
269
92.2k
      j = end - siz;
270
475k
      while (1) {
271
4.86M
        while (cmp(pivot, i) > 0) {
272
4.44M
          i += siz;
273
4.44M
          if (UNEXPECTED(i == j)) {
274
55.9k
            goto done;
275
55.9k
          }
276
4.44M
        }
277
419k
        j -= siz;
278
419k
        if (UNEXPECTED(j == i)) {
279
16.9k
          goto done;
280
16.9k
        }
281
2.64M
        while (cmp(j, pivot) > 0) {
282
2.25M
          j -= siz;
283
2.25M
          if (UNEXPECTED(j == i)) {
284
10.7k
            goto done;
285
10.7k
          }
286
2.25M
        }
287
391k
        swp(i, j);
288
391k
        i += siz;
289
391k
        if (UNEXPECTED(i == j)) {
290
8.62k
          goto done;
291
8.62k
        }
292
391k
      }
293
92.2k
done:
294
92.2k
      swp(pivot, i - siz);
295
92.2k
      if ((i - siz) - start < end - i) {
296
35.8k
        zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
297
35.8k
        base = i;
298
35.8k
        nmemb = (end - i)/siz;
299
56.4k
      } else {
300
56.4k
        zend_sort(i, (end - i)/siz, siz, cmp, swp);
301
56.4k
        nmemb = (i - start)/siz - 1;
302
56.4k
      }
303
92.2k
    }
304
208k
  }
305
116k
}
306
/* }}} */