File: isl_sort.c

package info (click to toggle)
isl 0.20-2
  • links: PTS
  • area: main
  • in suites: buster, sid
  • size: 12,708 kB
  • sloc: ansic: 125,702; python: 5,770; sh: 4,291; cpp: 1,057; makefile: 497; lisp: 54; perl: 9
file content (157 lines) | stat: -rw-r--r-- 4,922 bytes parent folder | download | duplicates (27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * The code of this file was taken from http://jeffreystedfast.blogspot.be,
 * where it was posted in 2011 by Jeffrey Stedfast under the MIT license.
 * The MIT license text is as follows:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <isl_sort.h>

#define MID(lo, hi) (lo + ((hi - lo) >> 1))

/* The code here is an optimized merge sort. Starting from a generic merge sort
 * the following optimizations were applied:
 *
 * o Batching of memcpy() calls: Instead of calling memcpy() to copy each and
 *   every element into a temporary buffer, blocks of elements are copied
 *   at a time.
 *
 * o To reduce the number of memcpy() calls further, copying leading
 *   and trailing elements into our temporary buffer is avoided, in case it is
 *   not necessary to merge them.
 *
 * A further optimization could be to specialize memcpy calls based on the
 * size of the types we compare. For now, this code does not include the
 * relevant optimization, as clang e.g. inlines a very efficient memcpy()
 * implementation. It is not clear, that the specialized version as provided in
 * the blog post, is really superior to the one that will be inlined by
 * default. So we decided to keep the code simple until this optimization was
 * proven to be beneficial.
 */

static void
msort (void *array, void *buf, size_t low, size_t high, size_t size,
       int (* compare) (const void *, const void *, void *), void *arg)
{
    char *a1, *al, *am, *ah, *ls, *hs, *lo, *hi, *b;
    size_t copied = 0;
    size_t mid;

    mid = MID (low, high);

    if (mid + 1 < high)
        msort (array, buf, mid + 1, high, size, compare, arg);

    if (mid > low)
        msort (array, buf, low, mid, size, compare, arg);

    ah = ((char *) array) + ((high + 1) * size);
    am = ((char *) array) + ((mid + 1) * size);
    a1 = al = ((char *) array) + (low * size);

    b = (char *) buf;
    lo = al;
    hi = am;

    do {
        ls = lo;
        hs = hi;

        if (lo > al || hi > am) {
            /* our last loop already compared lo & hi and found lo <= hi */
            lo += size;
        }

        while (lo < am && compare (lo, hi, arg) <= 0)
            lo += size;

        if (lo < am) {
            if (copied == 0) {
                /* avoid copying the leading items */
                a1 = lo;
                ls = lo;
            }

            /* our last compare tells us hi < lo */
            hi += size;

            while (hi < ah && compare (hi, lo, arg) < 0)
                hi += size;

            if (lo > ls) {
                memcpy (b, ls, lo - ls);
                copied += (lo - ls);
                b += (lo - ls);
            }

            memcpy (b, hs, hi - hs);
            copied += (hi - hs);
            b += (hi - hs);
        } else if (copied) {
            memcpy (b, ls, lo - ls);
            copied += (lo - ls);
            b += (lo - ls);

            /* copy everything we needed to re-order back into array */
            memcpy (a1, buf, copied);
            return;
        } else {
            /* everything already in order */
            return;
        }
    } while (hi < ah);

    if (lo < am) {
        memcpy (b, lo, am - lo);
        copied += (am - lo);
    }

    memcpy (a1, buf, copied);
}

static int
MergeSort (void *base, size_t nmemb, size_t size,
           int (* compare) (const void *, const void *, void *), void *arg)
{
    void *tmp;

    if (nmemb < 2)
        return 0;

    if (!(tmp = malloc (nmemb * size))) {
        errno = ENOMEM;
        return -1;
    }

    msort (base, tmp, 0, nmemb - 1, size, compare, arg);

    free (tmp);

    return 0;
}

int isl_sort(void *const pbase, size_t total_elems, size_t size,
	int (*cmp)(const void *, const void *, void *arg), void *arg)
{
    return MergeSort (pbase, total_elems, size, cmp, arg);
}