Coverage Report

Created: 2026-07-25 06:39

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.48k
static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
24
3.48k
  if (cmp(a, b) > 0) {
25
2.34k
    swp(a, b);
26
2.34k
  }
27
3.48k
}
28
/* }}} */
29
30
142k
static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
31
142k
  if (!(cmp(a, b) > 0)) {
32
45.6k
    if (!(cmp(b, c) > 0)) {
33
30.8k
      return;
34
30.8k
    }
35
14.7k
    swp(b, c);
36
14.7k
    if (cmp(a, b) > 0) {
37
4.11k
      swp(a, b);
38
4.11k
    }
39
14.7k
    return;
40
45.6k
  }
41
97.2k
  if (!(cmp(c, b) > 0)) {
42
14.4k
    swp(a, c);
43
14.4k
    return;
44
14.4k
  }
45
82.7k
  swp(a, b);
46
82.7k
  if (cmp(b, c) > 0) {
47
40.4k
    swp(b, c);
48
40.4k
  }
49
82.7k
}
50
/* }}} */
51
52
14.8k
static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
53
14.8k
  zend_sort_3(a, b, c, cmp, swp);
54
14.8k
  if (cmp(c, d) > 0) {
55
10.7k
    swp(c, d);
56
10.7k
    if (cmp(b, c) > 0) {
57
6.44k
      swp(b, c);
58
6.44k
      if (cmp(a, b) > 0) {
59
5.21k
        swp(a, b);
60
5.21k
      }
61
6.44k
    }
62
10.7k
  }
63
14.8k
}
64
/* }}} */
65
66
11.3k
static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
67
11.3k
  zend_sort_4(a, b, c, d, cmp, swp);
68
11.3k
  if (cmp(d, e) > 0) {
69
9.82k
    swp(d, e);
70
9.82k
    if (cmp(c, d) > 0) {
71
8.54k
      swp(c, d);
72
8.54k
      if (cmp(b, c) > 0) {
73
5.04k
        swp(b, c);
74
5.04k
        if (cmp(a, b) > 0) {
75
423
          swp(a, b);
76
423
        }
77
5.04k
      }
78
8.54k
    }
79
9.82k
  }
80
11.3k
}
81
/* }}} */
82
83
164k
ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
84
164k
  switch (nmemb) {
85
0
    case 0:
86
6.83k
    case 1:
87
6.83k
      break;
88
3.48k
    case 2:
89
3.48k
      zend_sort_2(base, (char *)base + siz, cmp, swp);
90
3.48k
      break;
91
15.4k
    case 3:
92
15.4k
      zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
93
15.4k
      break;
94
3.48k
    case 4:
95
3.48k
      {
96
3.48k
        size_t siz2 = siz + siz;
97
3.48k
        zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
98
3.48k
      }
99
3.48k
      break;
100
10.5k
    case 5:
101
10.5k
      {
102
10.5k
        size_t siz2 = siz + siz;
103
10.5k
        zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
104
10.5k
      }
105
10.5k
      break;
106
124k
    default:
107
124k
      {
108
124k
        char *i, *j, *k;
109
124k
        char *start = (char *)base;
110
124k
        char *end = start + (nmemb * siz);
111
124k
        size_t siz2= siz + siz;
112
124k
        char *sentry = start + (6 * siz);
113
748k
        for (i = start + siz; i < sentry; i += siz) {
114
623k
          j = i - siz;
115
623k
          if (!(cmp(j, i) > 0)) {
116
160k
            continue;
117
160k
          }
118
785k
          while (j != start) {
119
652k
            j -= siz;
120
652k
            if (!(cmp(j, i) > 0)) {
121
330k
              j += siz;
122
330k
              break;
123
330k
            }
124
652k
          }
125
1.24M
          for (k = i; k > j; k -= siz) {
126
785k
            swp(k, k - siz);
127
785k
          }
128
463k
        }
129
880k
        for (i = sentry; i < end; i += siz) {
130
755k
          j = i - siz;
131
755k
          if (!(cmp(j, i) > 0)) {
132
70.6k
            continue;
133
70.6k
          }
134
1.19M
          do {
135
1.19M
            j -= siz2;
136
1.19M
            if (!(cmp(j, i) > 0)) {
137
656k
              j += siz;
138
656k
              if (!(cmp(j, i) > 0)) {
139
459k
                j += siz;
140
459k
              }
141
656k
              break;
142
656k
            }
143
536k
            if (j == start) {
144
5.72k
              break;
145
5.72k
            }
146
531k
            if (j == start + siz) {
147
22.4k
              j -= siz;
148
22.4k
              if (cmp(i, j) > 0) {
149
9.16k
                j += siz;
150
9.16k
              }
151
22.4k
              break;
152
22.4k
            }
153
531k
          } while (1);
154
2.65M
          for (k = i; k > j; k -= siz) {
155
1.96M
            swp(k, k - siz);
156
1.96M
          }
157
684k
        }
158
124k
      }
159
124k
      break;
160
164k
  }
161
164k
}
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
164k
{
249
277k
  while (1) {
250
277k
    if (nmemb <= 16) {
251
164k
      zend_insert_sort(base, nmemb, siz, cmp, swp);
252
164k
      return;
253
164k
    } else {
254
113k
      char *i, *j;
255
113k
      char *start = (char *)base;
256
113k
      char *end = start + (nmemb * siz);
257
113k
      size_t offset = (nmemb >> Z_L(1));
258
113k
      char *pivot = start + (offset * siz);
259
260
113k
      if ((nmemb >> Z_L(10))) {
261
769
        size_t delta = (offset >> Z_L(1)) * siz;
262
769
        zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
263
112k
      } else {
264
112k
        zend_sort_3(start, pivot, end - siz, cmp, swp);
265
112k
      }
266
113k
      swp(start + siz, pivot);
267
113k
      pivot = start + siz;
268
113k
      i = pivot + siz;
269
113k
      j = end - siz;
270
597k
      while (1) {
271
5.28M
        while (cmp(pivot, i) > 0) {
272
4.74M
          i += siz;
273
4.74M
          if (UNEXPECTED(i == j)) {
274
66.6k
            goto done;
275
66.6k
          }
276
4.74M
        }
277
531k
        j -= siz;
278
531k
        if (UNEXPECTED(j == i)) {
279
17.6k
          goto done;
280
17.6k
        }
281
2.99M
        while (cmp(j, pivot) > 0) {
282
2.49M
          j -= siz;
283
2.49M
          if (UNEXPECTED(j == i)) {
284
16.3k
            goto done;
285
16.3k
          }
286
2.49M
        }
287
497k
        swp(i, j);
288
497k
        i += siz;
289
497k
        if (UNEXPECTED(i == j)) {
290
12.8k
          goto done;
291
12.8k
        }
292
497k
      }
293
113k
done:
294
113k
      swp(pivot, i - siz);
295
113k
      if ((i - siz) - start < end - i) {
296
40.8k
        zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
297
40.8k
        base = i;
298
40.8k
        nmemb = (end - i)/siz;
299
72.6k
      } else {
300
72.6k
        zend_sort(i, (end - i)/siz, siz, cmp, swp);
301
72.6k
        nmemb = (i - start)/siz - 1;
302
72.6k
      }
303
113k
    }
304
277k
  }
305
164k
}
306
/* }}} */