Coverage Report

Created: 2025-07-23 06:33

/src/php-src/Zend/zend_sort.c
Line
Count
Source (jump to first uncovered line)
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.16k
static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
25
6.16k
  if (cmp(a, b) > 0) {
26
3.32k
    swp(a, b);
27
3.32k
  }
28
6.16k
}
29
/* }}} */
30
31
156k
static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
32
156k
  if (!(cmp(a, b) > 0)) {
33
53.5k
    if (!(cmp(b, c) > 0)) {
34
26.6k
      return;
35
26.6k
    }
36
26.9k
    swp(b, c);
37
26.9k
    if (cmp(a, b) > 0) {
38
11.4k
      swp(a, b);
39
11.4k
    }
40
26.9k
    return;
41
53.5k
  }
42
102k
  if (!(cmp(c, b) > 0)) {
43
11.5k
    swp(a, c);
44
11.5k
    return;
45
11.5k
  }
46
91.1k
  swp(a, b);
47
91.1k
  if (cmp(b, c) > 0) {
48
46.7k
    swp(b, c);
49
46.7k
  }
50
91.1k
}
51
/* }}} */
52
53
19.9k
static void zend_sort_4(void *a, void *b, void *c, void *d, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
54
19.9k
  zend_sort_3(a, b, c, cmp, swp);
55
19.9k
  if (cmp(c, d) > 0) {
56
14.7k
    swp(c, d);
57
14.7k
    if (cmp(b, c) > 0) {
58
5.99k
      swp(b, c);
59
5.99k
      if (cmp(a, b) > 0) {
60
3.15k
        swp(a, b);
61
3.15k
      }
62
5.99k
    }
63
14.7k
  }
64
19.9k
}
65
/* }}} */
66
67
10.2k
static void zend_sort_5(void *a, void *b, void *c, void *d, void *e, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
68
10.2k
  zend_sort_4(a, b, c, d, cmp, swp);
69
10.2k
  if (cmp(d, e) > 0) {
70
8.47k
    swp(d, e);
71
8.47k
    if (cmp(c, d) > 0) {
72
7.67k
      swp(c, d);
73
7.67k
      if (cmp(b, c) > 0) {
74
1.66k
        swp(b, c);
75
1.66k
        if (cmp(a, b) > 0) {
76
913
          swp(a, b);
77
913
        }
78
1.66k
      }
79
7.67k
    }
80
8.47k
  }
81
10.2k
}
82
/* }}} */
83
84
129k
ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp, swap_func_t swp) /* {{{ */{
85
129k
  switch (nmemb) {
86
0
    case 0:
87
6.02k
    case 1:
88
6.02k
      break;
89
6.16k
    case 2:
90
6.16k
      zend_sort_2(base, (char *)base + siz, cmp, swp);
91
6.16k
      break;
92
13.2k
    case 3:
93
13.2k
      zend_sort_3(base, (char *)base + siz, (char *)base + siz + siz, cmp, swp);
94
13.2k
      break;
95
9.68k
    case 4:
96
9.68k
      {
97
9.68k
        size_t siz2 = siz + siz;
98
9.68k
        zend_sort_4(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, cmp, swp);
99
9.68k
      }
100
9.68k
      break;
101
8.97k
    case 5:
102
8.97k
      {
103
8.97k
        size_t siz2 = siz + siz;
104
8.97k
        zend_sort_5(base, (char *)base + siz, (char *)base + siz2, (char *)base + siz + siz2, (char *)base + siz2 + siz2, cmp, swp);
105
8.97k
      }
106
8.97k
      break;
107
84.9k
    default:
108
84.9k
      {
109
84.9k
        char *i, *j, *k;
110
84.9k
        char *start = (char *)base;
111
84.9k
        char *end = start + (nmemb * siz);
112
84.9k
        size_t siz2= siz + siz;
113
84.9k
        char *sentry = start + (6 * siz);
114
509k
        for (i = start + siz; i < sentry; i += siz) {
115
424k
          j = i - siz;
116
424k
          if (!(cmp(j, i) > 0)) {
117
181k
            continue;
118
181k
          }
119
522k
          while (j != start) {
120
430k
            j -= siz;
121
430k
            if (!(cmp(j, i) > 0)) {
122
151k
              j += siz;
123
151k
              break;
124
151k
            }
125
430k
          }
126
765k
          for (k = i; k > j; k -= siz) {
127
522k
            swp(k, k - siz);
128
522k
          }
129
243k
        }
130
528k
        for (i = sentry; i < end; i += siz) {
131
443k
          j = i - siz;
132
443k
          if (!(cmp(j, i) > 0)) {
133
111k
            continue;
134
111k
          }
135
755k
          do {
136
755k
            j -= siz2;
137
755k
            if (!(cmp(j, i) > 0)) {
138
281k
              j += siz;
139
281k
              if (!(cmp(j, i) > 0)) {
140
168k
                j += siz;
141
168k
              }
142
281k
              break;
143
281k
            }
144
474k
            if (j == start) {
145
14.9k
              break;
146
14.9k
            }
147
459k
            if (j == start + siz) {
148
35.8k
              j -= siz;
149
35.8k
              if (cmp(i, j) > 0) {
150
17.3k
                j += siz;
151
17.3k
              }
152
35.8k
              break;
153
35.8k
            }
154
459k
          } while (1);
155
1.74M
          for (k = i; k > j; k -= siz) {
156
1.41M
            swp(k, k - siz);
157
1.41M
          }
158
331k
        }
159
84.9k
      }
160
84.9k
      break;
161
129k
  }
162
129k
}
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
129k
{
250
253k
  while (1) {
251
253k
    if (nmemb <= 16) {
252
129k
      zend_insert_sort(base, nmemb, siz, cmp, swp);
253
129k
      return;
254
129k
    } else {
255
124k
      char *i, *j;
256
124k
      char *start = (char *)base;
257
124k
      char *end = start + (nmemb * siz);
258
124k
      size_t offset = (nmemb >> Z_L(1));
259
124k
      char *pivot = start + (offset * siz);
260
261
124k
      if ((nmemb >> Z_L(10))) {
262
1.31k
        size_t delta = (offset >> Z_L(1)) * siz;
263
1.31k
        zend_sort_5(start, start + delta, pivot, pivot + delta, end - siz, cmp, swp);
264
123k
      } else {
265
123k
        zend_sort_3(start, pivot, end - siz, cmp, swp);
266
123k
      }
267
124k
      swp(start + siz, pivot);
268
124k
      pivot = start + siz;
269
124k
      i = pivot + siz;
270
124k
      j = end - siz;
271
1.05M
      while (1) {
272
7.22M
        while (cmp(pivot, i) > 0) {
273
6.23M
          i += siz;
274
6.23M
          if (UNEXPECTED(i == j)) {
275
68.6k
            goto done;
276
68.6k
          }
277
6.23M
        }
278
986k
        j -= siz;
279
986k
        if (UNEXPECTED(j == i)) {
280
18.5k
          goto done;
281
18.5k
        }
282
4.52M
        while (cmp(j, pivot) > 0) {
283
3.57M
          j -= siz;
284
3.57M
          if (UNEXPECTED(j == i)) {
285
23.6k
            goto done;
286
23.6k
          }
287
3.57M
        }
288
944k
        swp(i, j);
289
944k
        i += siz;
290
944k
        if (UNEXPECTED(i == j)) {
291
13.4k
          goto done;
292
13.4k
        }
293
944k
      }
294
124k
done:
295
124k
      swp(pivot, i - siz);
296
124k
      if ((i - siz) - start < end - i) {
297
51.1k
        zend_sort(start, (i - start)/siz - 1, siz, cmp, swp);
298
51.1k
        base = i;
299
51.1k
        nmemb = (end - i)/siz;
300
73.2k
      } else {
301
73.2k
        zend_sort(i, (end - i)/siz, siz, cmp, swp);
302
73.2k
        nmemb = (i - start)/siz - 1;
303
73.2k
      }
304
124k
    }
305
253k
  }
306
129k
}
307
/* }}} */