Coverage Report

Created: 2025-11-16 06:23

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 (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Xinchen Hui <laruence@php.net>                              |
16
   |          Sterling Hughes <sterling@php.net>                          |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend.h"
21
#include "zend_sort.h"
22
#include <limits.h>
23
24
3.51k
static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
25
3.51k
  if (cmp(a, b) > 0) {
26
2.49k
    swp(a, b);
27
2.49k
  }
28
3.51k
}
29
/* }}} */
30
31
95.1k
static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
32
95.1k
  if (!(cmp(a, b) > 0)) {
33
29.2k
    if (!(cmp(b, c) > 0)) {
34
15.4k
      return;
35
15.4k
    }
36
13.7k
    swp(b, c);
37
13.7k
    if (cmp(a, b) > 0) {
38
4.92k
      swp(a, b);
39
4.92k
    }
40
13.7k
    return;
41
29.2k
  }
42
65.9k
  if (!(cmp(c, b) > 0)) {
43
7.30k
    swp(a, c);
44
7.30k
    return;
45
7.30k
  }
46
58.6k
  swp(a, b);
47
58.6k
  if (cmp(b, c) > 0) {
48
30.2k
    swp(b, c);
49
30.2k
  }
50
58.6k
}
51
/* }}} */
52
53
12.2k
static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
54
12.2k
  zend_sort_3(a, b, c, cmp, swp);
55
12.2k
  if (cmp(c, d) > 0) {
56
9.30k
    swp(c, d);
57
9.30k
    if (cmp(b, c) > 0) {
58
3.96k
      swp(b, c);
59
3.96k
      if (cmp(a, b) > 0) {
60
1.98k
        swp(a, b);
61
1.98k
      }
62
3.96k
    }
63
9.30k
  }
64
12.2k
}
65
/* }}} */
66
67
5.91k
static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
68
5.91k
  zend_sort_4(a, b, c, d, cmp, swp);
69
5.91k
  if (cmp(d, e) > 0) {
70
4.86k
    swp(d, e);
71
4.86k
    if (cmp(c, d) > 0) {
72
4.28k
      swp(c, d);
73
4.28k
      if (cmp(b, c) > 0) {
74
1.02k
        swp(b, c);
75
1.02k
        if (cmp(a, b) > 0) {
76
526
          swp(a, b);
77
526
        }
78
1.02k
      }
79
4.28k
    }
80
4.86k
  }
81
5.91k
}
82
/* }}} */
83
84
77.1k
ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
85
77.1k
  switch (nmemb) {
86
0
    case 0:
87
1.97k
    case 1:
88
1.97k
      break;
89
3.51k
    case 2:
90
3.51k
      zend_sort_2(base, (char *)base + siz, cmp, swp);
91
3.51k
      break;
92
9.65k
    case 3:
93
9.65k
      zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
94
9.65k
      break;
95
6.30k
    case 4:
96
6.30k
      {
97
6.30k
        size_t siz2 = siz + siz;
98
6.30k
        zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
99
6.30k
      }
100
6.30k
      break;
101
5.21k
    case 5:
102
5.21k
      {
103
5.21k
        size_t siz2 = siz + siz;
104
5.21k
        zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
105
5.21k
      }
106
5.21k
      break;
107
50.4k
    default:
108
50.4k
      {
109
50.4k
        char *i, *j, *k;
110
50.4k
        char *start = (char *)base;
111
50.4k
        char *end = start + (nmemb * siz);
112
50.4k
        size_t siz2= siz + siz;
113
50.4k
        char *sentry = start + (6 * siz);
114
302k
        for (i = start + siz; i < sentry; i += siz) {
115
252k
          j = i - siz;
116
252k
          if (!(cmp(j, i) > 0)) {
117
101k
            continue;
118
101k
          }
119
338k
          while (j != start) {
120
281k
            j -= siz;
121
281k
            if (!(cmp(j, i) > 0)) {
122
93.0k
              j += siz;
123
93.0k
              break;
124
93.0k
            }
125
281k
          }
126
488k
          for (k = i; k > j; k -= siz) {
127
338k
            swp(k, k - siz);
128
338k
          }
129
150k
        }
130
311k
        for (i = sentry; i < end; i += siz) {
131
261k
          j = i - siz;
132
261k
          if (!(cmp(j, i) > 0)) {
133
50.8k
            continue;
134
50.8k
          }
135
487k
          do {
136
487k
            j -= siz2;
137
487k
            if (!(cmp(j, i) > 0)) {
138
181k
              j += siz;
139
181k
              if (!(cmp(j, i) > 0)) {
140
107k
                j += siz;
141
107k
              }
142
181k
              break;
143
181k
            }
144
305k
            if (j == start) {
145
7.87k
              break;
146
7.87k
            }
147
297k
            if (j == start + siz) {
148
21.1k
              j -= siz;
149
21.1k
              if (cmp(i, j) > 0) {
150
10.6k
                j += siz;
151
10.6k
              }
152
21.1k
              break;
153
21.1k
            }
154
297k
          } while (1);
155
1.11M
          for (k = i; k > j; k -= siz) {
156
906k
            swp(k, k - siz);
157
906k
          }
158
210k
        }
159
50.4k
      }
160
50.4k
      break;
161
77.1k
  }
162
77.1k
}
163
/* }}} */
164
165
/* {{{ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
166
 *
167
 * Derived from LLVM's libc++ implementation of std::sort.
168
 *
169
 * ===========================================================================
170
 * libc++ License
171
 * ===========================================================================
172
 *
173
 * The libc++ library is dual licensed under both the University of Illinois
174
 * "BSD-Like" license and the MIT license. As a user of this code you may
175
 * choose to use it under either license. As a contributor, you agree to allow
176
 * your code to be used under both.
177
 *
178
 * Full text of the relevant licenses is included below.
179
 *
180
 * ===========================================================================
181
 *
182
 * University of Illinois/NCSA
183
 * Open Source License
184
 *
185
 * Copyright (c) 2009-2012 by the contributors listed at
186
 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
187
 *
188
 * All rights reserved.
189
 *
190
 * Developed by:
191
 *
192
 *     LLVM Team
193
 *
194
 *     University of Illinois at Urbana-Champaign
195
 *
196
 *     http://llvm.org
197
 *
198
 * Permission is hereby granted, free of charge, to any person obtaining a copy
199
 * of this software and associated documentation files (the "Software"), to
200
 * deal with the Software without restriction, including without limitation the
201
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
202
 * sell copies of the Software, and to permit persons to whom the Software is
203
 * furnished to do so, subject to the following conditions:
204
 *
205
 *     * Redistributions of source code must retain the above copyright notice,
206
 *       this list of conditions and the following disclaimers.
207
 *
208
 *     * Redistributions in binary form must reproduce the above copyright
209
 *       notice, this list of conditions and the following disclaimers in the
210
 *       documentation and/or other materials provided with the distribution.
211
 *
212
 *     * Neither the names of the LLVM Team, University of Illinois at
213
 *       Urbana-Champaign, nor the names of its contributors may be used to
214
 *       endorse or promote products derived from this Software without
215
 *       specific prior written permission.
216
 *
217
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
219
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
220
 * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
221
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
222
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
223
 * WITH THE SOFTWARE.
224
 *
225
 * ===========================================================================
226
 *
227
 * Copyright (c) 2009-2012 by the contributors listed at
228
 * http://llvm.org/svn/llvm-project/libcxx/trunk/CREDITS.TXT
229
 *
230
 * Permission is hereby granted, free of charge, to any person obtaining a copy
231
 * of this software and associated documentation files (the "Software"), to
232
 * deal in the Software without restriction, including without limitation the
233
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
234
 * sell copies of the Software, and to permit persons to whom the Software is
235
 * furnished to do so, subject to the following conditions:
236
 *
237
 * The above copyright notice and this permission notice shall be included in
238
 * all copies or substantial portions of the Software.
239
 *
240
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
241
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
242
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
243
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
244
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
245
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
246
 * IN THE SOFTWARE.
247
 */
248
ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp)
249
77.1k
{
250
151k
  while (1) {
251
151k
    if (nmemb <= 16) {
252
77.1k
      zend_insert_sort(base, nmemb, siz, cmp, swp);
253
77.1k
      return;
254
77.1k
    } else {
255
73.9k
      char *i, *j;
256
73.9k
      char *start = (char *)base;
257
73.9k
      char *end = start + (nmemb * siz);
258
73.9k
      size_t offset = (nmemb >> Z_L(1));
259
73.9k
      char *pivot = start + (offset * siz);
260
261
73.9k
      if ((nmemb >> Z_L(10))) {
262
701
        size_t delta = (offset >> Z_L(1)) * siz;
263
701
        zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
264
73.2k
      } else {
265
73.2k
        zend_sort_3(start, pivot, end - siz, cmp, swp);
266
73.2k
      }
267
73.9k
      swp(start + siz, pivot);
268
73.9k
      pivot = start + siz;
269
73.9k
      i = pivot + siz;
270
73.9k
      j = end - siz;
271
554k
      while (1) {
272
4.22M
        while (cmp(pivot, i) > 0) {
273
3.71M
          i += siz;
274
3.71M
          if (UNEXPECTED(i == j)) {
275
44.0k
            goto done;
276
44.0k
          }
277
3.71M
        }
278
510k
        j -= siz;
279
510k
        if (UNEXPECTED(j == i)) {
280
12.8k
          goto done;
281
12.8k
        }
282
2.45M
        while (cmp(j, pivot) > 0) {
283
1.97M
          j -= siz;
284
1.97M
          if (UNEXPECTED(j == i)) {
285
10.2k
            goto done;
286
10.2k
          }
287
1.97M
        }
288
487k
        swp(i, j);
289
487k
        i += siz;
290
487k
        if (UNEXPECTED(i == j)) {
291
6.86k
          goto done;
292
6.86k
        }
293
487k
      }
294
73.9k
done:
295
73.9k
      swp(pivot, i - siz);
296
73.9k
      if ((i - siz) - start < end - i) {
297
30.4k
        zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
298
30.4k
        base = i;
299
30.4k
        nmemb = (end - i)/siz;
300
43.4k
      } else {
301
43.4k
        zend_sort(i, (end - i)/siz, siz, cmp, swp);
302
43.4k
        nmemb = (i - start)/siz - 1;
303
43.4k
      }
304
73.9k
    }
305
151k
  }
306
77.1k
}
307
/* }}} */