Coverage Report

Created: 2025-09-27 06:26

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
6.39k
static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
25
6.39k
  if (cmp(a, b) > 0) {
26
4.39k
    swp(a, b);
27
4.39k
  }
28
6.39k
}
29
/* }}} */
30
31
136k
static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
32
136k
  if (!(cmp(a, b) > 0)) {
33
39.7k
    if (!(cmp(b, c) > 0)) {
34
22.8k
      return;
35
22.8k
    }
36
16.9k
    swp(b, c);
37
16.9k
    if (cmp(a, b) > 0) {
38
6.49k
      swp(a, b);
39
6.49k
    }
40
16.9k
    return;
41
39.7k
  }
42
96.9k
  if (!(cmp(c, b) > 0)) {
43
9.00k
    swp(a, c);
44
9.00k
    return;
45
9.00k
  }
46
87.9k
  swp(a, b);
47
87.9k
  if (cmp(b, c) > 0) {
48
44.8k
    swp(b, c);
49
44.8k
  }
50
87.9k
}
51
/* }}} */
52
53
17.2k
static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
54
17.2k
  zend_sort_3(a, b, c, cmp, swp);
55
17.2k
  if (cmp(c, d) > 0) {
56
13.4k
    swp(c, d);
57
13.4k
    if (cmp(b, c) > 0) {
58
6.21k
      swp(b, c);
59
6.21k
      if (cmp(a, b) > 0) {
60
3.23k
        swp(a, b);
61
3.23k
      }
62
6.21k
    }
63
13.4k
  }
64
17.2k
}
65
/* }}} */
66
67
6.85k
static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
68
6.85k
  zend_sort_4(a, b, c, d, cmp, swp);
69
6.85k
  if (cmp(d, e) > 0) {
70
5.43k
    swp(d, e);
71
5.43k
    if (cmp(c, d) > 0) {
72
4.86k
      swp(c, d);
73
4.86k
      if (cmp(b, c) > 0) {
74
1.08k
        swp(b, c);
75
1.08k
        if (cmp(a, b) > 0) {
76
568
          swp(a, b);
77
568
        }
78
1.08k
      }
79
4.86k
    }
80
5.43k
  }
81
6.85k
}
82
/* }}} */
83
84
113k
ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
85
113k
  switch (nmemb) {
86
0
    case 0:
87
5.28k
    case 1:
88
5.28k
      break;
89
6.39k
    case 2:
90
6.39k
      zend_sort_2(base, (char *)base + siz, cmp, swp);
91
6.39k
      break;
92
11.3k
    case 3:
93
11.3k
      zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
94
11.3k
      break;
95
10.4k
    case 4:
96
10.4k
      {
97
10.4k
        size_t siz2 = siz + siz;
98
10.4k
        zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
99
10.4k
      }
100
10.4k
      break;
101
5.78k
    case 5:
102
5.78k
      {
103
5.78k
        size_t siz2 = siz + siz;
104
5.78k
        zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
105
5.78k
      }
106
5.78k
      break;
107
74.6k
    default:
108
74.6k
      {
109
74.6k
        char *i, *j, *k;
110
74.6k
        char *start = (char *)base;
111
74.6k
        char *end = start + (nmemb * siz);
112
74.6k
        size_t siz2= siz + siz;
113
74.6k
        char *sentry = start + (6 * siz);
114
447k
        for (i = start + siz; i < sentry; i += siz) {
115
373k
          j = i - siz;
116
373k
          if (!(cmp(j, i) > 0)) {
117
146k
            continue;
118
146k
          }
119
481k
          while (j != start) {
120
399k
            j -= siz;
121
399k
            if (!(cmp(j, i) > 0)) {
122
144k
              j += siz;
123
144k
              break;
124
144k
            }
125
399k
          }
126
707k
          for (k = i; k > j; k -= siz) {
127
481k
            swp(k, k - siz);
128
481k
          }
129
226k
        }
130
466k
        for (i = sentry; i < end; i += siz) {
131
391k
          j = i - siz;
132
391k
          if (!(cmp(j, i) > 0)) {
133
83.9k
            continue;
134
83.9k
          }
135
671k
          do {
136
671k
            j -= siz2;
137
671k
            if (!(cmp(j, i) > 0)) {
138
267k
              j += siz;
139
267k
              if (!(cmp(j, i) > 0)) {
140
144k
                j += siz;
141
144k
              }
142
267k
              break;
143
267k
            }
144
403k
            if (j == start) {
145
10.7k
              break;
146
10.7k
            }
147
392k
            if (j == start + siz) {
148
28.9k
              j -= siz;
149
28.9k
              if (cmp(i, j) > 0) {
150
14.4k
                j += siz;
151
14.4k
              }
152
28.9k
              break;
153
28.9k
            }
154
392k
          } while (1);
155
1.55M
          for (k = i; k > j; k -= siz) {
156
1.25M
            swp(k, k - siz);
157
1.25M
          }
158
307k
        }
159
74.6k
      }
160
74.6k
      break;
161
113k
  }
162
113k
}
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
113k
{
250
223k
  while (1) {
251
223k
    if (nmemb <= 16) {
252
113k
      zend_insert_sort(base, nmemb, siz, cmp, swp);
253
113k
      return;
254
113k
    } else {
255
109k
      char *i, *j;
256
109k
      char *start = (char *)base;
257
109k
      char *end = start + (nmemb * siz);
258
109k
      size_t offset = (nmemb >> Z_L(1));
259
109k
      char *pivot = start + (offset * siz);
260
261
109k
      if ((nmemb >> Z_L(10))) {
262
1.06k
        size_t delta = (offset >> Z_L(1)) * siz;
263
1.06k
        zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
264
108k
      } else {
265
108k
        zend_sort_3(start, pivot, end - siz, cmp, swp);
266
108k
      }
267
109k
      swp(start + siz, pivot);
268
109k
      pivot = start + siz;
269
109k
      i = pivot + siz;
270
109k
      j = end - siz;
271
734k
      while (1) {
272
6.37M
        while (cmp(pivot, i) > 0) {
273
5.70M
          i += siz;
274
5.70M
          if (UNEXPECTED(i == j)) {
275
63.9k
            goto done;
276
63.9k
          }
277
5.70M
        }
278
670k
        j -= siz;
279
670k
        if (UNEXPECTED(j == i)) {
280
16.2k
          goto done;
281
16.2k
        }
282
3.63M
        while (cmp(j, pivot) > 0) {
283
2.99M
          j -= siz;
284
2.99M
          if (UNEXPECTED(j == i)) {
285
16.5k
            goto done;
286
16.5k
          }
287
2.99M
        }
288
637k
        swp(i, j);
289
637k
        i += siz;
290
637k
        if (UNEXPECTED(i == j)) {
291
12.4k
          goto done;
292
12.4k
        }
293
637k
      }
294
109k
done:
295
109k
      swp(pivot, i - siz);
296
109k
      if ((i - siz) - start < end - i) {
297
42.5k
        zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
298
42.5k
        base = i;
299
42.5k
        nmemb = (end - i)/siz;
300
66.7k
      } else {
301
66.7k
        zend_sort(i, (end - i)/siz, siz, cmp, swp);
302
66.7k
        nmemb = (i - start)/siz - 1;
303
66.7k
      }
304
109k
    }
305
223k
  }
306
113k
}
307
/* }}} */