Coverage Report

Created: 2018-08-14 02:14

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_sort.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The code of this file was taken from http://jeffreystedfast.blogspot.be,
3
 * where it was posted in 2011 by Jeffrey Stedfast under the MIT license.
4
 * The MIT license text is as follows:
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 */
24
25
#include <errno.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <isl_sort.h>
29
30
1.47M
#define MID(lo, hi) (lo + ((hi - lo) >> 1))
31
32
/* The code here is an optimized merge sort. Starting from a generic merge sort
33
 * the following optimizations were applied:
34
 *
35
 * o Batching of memcpy() calls: Instead of calling memcpy() to copy each and
36
 *   every element into a temporary buffer, blocks of elements are copied
37
 *   at a time.
38
 *
39
 * o To reduce the number of memcpy() calls further, copying leading
40
 *   and trailing elements into our temporary buffer is avoided, in case it is
41
 *   not necessary to merge them.
42
 *
43
 * A further optimization could be to specialize memcpy calls based on the
44
 * size of the types we compare. For now, this code does not include the
45
 * relevant optimization, as clang e.g. inlines a very efficient memcpy()
46
 * implementation. It is not clear, that the specialized version as provided in
47
 * the blog post, is really superior to the one that will be inlined by
48
 * default. So we decided to keep the code simple until this optimization was
49
 * proven to be beneficial.
50
 */
51
52
static void
53
msort (void *array, void *buf, size_t low, size_t high, size_t size,
54
       int (* compare) (const void *, const void *, void *), void *arg)
55
1.47M
{
56
1.47M
    char *a1, *al, *am, *ah, *ls, *hs, *lo, *hi, *b;
57
1.47M
    size_t copied = 0;
58
1.47M
    size_t mid;
59
1.47M
60
1.47M
    mid = MID (low, high);
61
1.47M
62
1.47M
    if (mid + 1 < high)
63
467k
        msort (array, buf, mid + 1, high, size, compare, arg);
64
1.47M
65
1.47M
    if (mid > low)
66
733k
        msort (array, buf, low, mid, size, compare, arg);
67
1.47M
68
1.47M
    ah = ((char *) array) + ((high + 1) * size);
69
1.47M
    am = ((char *) array) + ((mid + 1) * size);
70
1.47M
    a1 = al = ((char *) array) + (low * size);
71
1.47M
72
1.47M
    b = (char *) buf;
73
1.47M
    lo = al;
74
1.47M
    hi = am;
75
1.47M
76
1.60M
    do {
77
1.60M
        ls = lo;
78
1.60M
        hs = hi;
79
1.60M
80
1.60M
        if (lo > al || 
hi > am1.51M
) {
81
126k
            /* our last loop already compared lo & hi and found lo <= hi */
82
126k
            lo += size;
83
126k
        }
84
1.60M
85
4.28M
        while (lo < am && 
compare (lo, hi, arg) <= 03.04M
)
86
2.67M
            lo += size;
87
1.60M
88
1.60M
        if (lo < am) {
89
365k
            if (copied == 0) {
90
284k
                /* avoid copying the leading items */
91
284k
                a1 = lo;
92
284k
                ls = lo;
93
284k
            }
94
365k
95
365k
            /* our last compare tells us hi < lo */
96
365k
            hi += size;
97
365k
98
638k
            while (hi < ah && 
compare (hi, lo, arg) < 0399k
)
99
273k
                hi += size;
100
365k
101
365k
            if (lo > ls) {
102
80.8k
                memcpy (b, ls, lo - ls);
103
80.8k
                copied += (lo - ls);
104
80.8k
                b += (lo - ls);
105
80.8k
            }
106
365k
107
365k
            memcpy (b, hs, hi - hs);
108
365k
            copied += (hi - hs);
109
365k
            b += (hi - hs);
110
1.23M
        } else if (copied) {
111
45.2k
            memcpy (b, ls, lo - ls);
112
45.2k
            copied += (lo - ls);
113
45.2k
            b += (lo - ls);
114
45.2k
115
45.2k
            /* copy everything we needed to re-order back into array */
116
45.2k
            memcpy (a1, buf, copied);
117
45.2k
            return;
118
1.19M
        } else {
119
1.19M
            /* everything already in order */
120
1.19M
            return;
121
1.19M
        }
122
365k
    } while (hi < ah);
123
1.47M
124
1.47M
    
if (238k
lo < am238k
) {
125
238k
        memcpy (b, lo, am - lo);
126
238k
        copied += (am - lo);
127
238k
    }
128
238k
129
238k
    memcpy (a1, buf, copied);
130
238k
}
131
132
static int
133
MergeSort (void *base, size_t nmemb, size_t size,
134
           int (* compare) (const void *, const void *, void *), void *arg)
135
334k
{
136
334k
    void *tmp;
137
334k
138
334k
    if (nmemb < 2)
139
56.4k
        return 0;
140
277k
141
277k
    if (!(tmp = malloc (nmemb * size))) {
142
0
        errno = ENOMEM;
143
0
        return -1;
144
0
    }
145
277k
146
277k
    msort (base, tmp, 0, nmemb - 1, size, compare, arg);
147
277k
148
277k
    free (tmp);
149
277k
150
277k
    return 0;
151
277k
}
152
153
int isl_sort(void *const pbase, size_t total_elems, size_t size,
154
  int (*cmp)(const void *, const void *, void *arg), void *arg)
155
334k
{
156
334k
    return MergeSort (pbase, total_elems, size, cmp, arg);
157
334k
}