Coverage Report

Created: 2019-04-21 11:35

/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
517k
#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
517k
{
56
517k
    char *a1, *al, *am, *ah, *ls, *hs, *lo, *hi, *b;
57
517k
    size_t copied = 0;
58
517k
    size_t mid;
59
517k
60
517k
    mid = MID (low, high);
61
517k
62
517k
    if (mid + 1 < high)
63
165k
        msort (array, buf, mid + 1, high, size, compare, arg);
64
517k
65
517k
    if (mid > low)
66
254k
        msort (array, buf, low, mid, size, compare, arg);
67
517k
68
517k
    ah = ((char *) array) + ((high + 1) * size);
69
517k
    am = ((char *) array) + ((mid + 1) * size);
70
517k
    a1 = al = ((char *) array) + (low * size);
71
517k
72
517k
    b = (char *) buf;
73
517k
    lo = al;
74
517k
    hi = am;
75
517k
76
571k
    do {
77
571k
        ls = lo;
78
571k
        hs = hi;
79
571k
80
571k
        if (lo > al || 
hi > am528k
) {
81
53.5k
            /* our last loop already compared lo & hi and found lo <= hi */
82
53.5k
            lo += size;
83
53.5k
        }
84
571k
85
1.51M
        while (lo < am && 
compare (lo, hi, arg) <= 01.08M
)
86
947k
            lo += size;
87
571k
88
571k
        if (lo < am) {
89
136k
            if (copied == 0) {
90
98.1k
                /* avoid copying the leading items */
91
98.1k
                a1 = lo;
92
98.1k
                ls = lo;
93
98.1k
            }
94
136k
95
136k
            /* our last compare tells us hi < lo */
96
136k
            hi += size;
97
136k
98
246k
            while (hi < ah && 
compare (hi, lo, arg) < 0163k
)
99
109k
                hi += size;
100
136k
101
136k
            if (lo > ls) {
102
38.1k
                memcpy (b, ls, lo - ls);
103
38.1k
                copied += (lo - ls);
104
38.1k
                b += (lo - ls);
105
38.1k
            }
106
136k
107
136k
            memcpy (b, hs, hi - hs);
108
136k
            copied += (hi - hs);
109
136k
            b += (hi - hs);
110
434k
        } else if (copied) {
111
15.3k
            memcpy (b, ls, lo - ls);
112
15.3k
            copied += (lo - ls);
113
15.3k
            b += (lo - ls);
114
15.3k
115
15.3k
            /* copy everything we needed to re-order back into array */
116
15.3k
            memcpy (a1, buf, copied);
117
15.3k
            return;
118
419k
        } else {
119
419k
            /* everything already in order */
120
419k
            return;
121
419k
        }
122
136k
    } while (hi < ah);
123
517k
124
517k
    
if (82.7k
lo < am82.7k
) {
125
82.7k
        memcpy (b, lo, am - lo);
126
82.7k
        copied += (am - lo);
127
82.7k
    }
128
82.7k
129
82.7k
    memcpy (a1, buf, copied);
130
82.7k
}
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
116k
{
136
116k
    void *tmp;
137
116k
138
116k
    if (nmemb < 2)
139
18.8k
        return 0;
140
98.1k
141
98.1k
    if (!(tmp = malloc (nmemb * size))) {
142
0
        errno = ENOMEM;
143
0
        return -1;
144
0
    }
145
98.1k
146
98.1k
    msort (base, tmp, 0, nmemb - 1, size, compare, arg);
147
98.1k
148
98.1k
    free (tmp);
149
98.1k
150
98.1k
    return 0;
151
98.1k
}
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
116k
{
156
116k
    return MergeSort (pbase, total_elems, size, cmp, arg);
157
116k
}