Coverage Report

Created: 2017-06-23 12:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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
729k
#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
729k
{
56
729k
    char *a1, *al, *am, *ah, *ls, *hs, *lo, *hi, *b;
57
729k
    size_t copied = 0;
58
729k
    size_t mid;
59
729k
60
729k
    mid = MID (low, high);
61
729k
62
729k
    if (mid + 1 < high)
63
219k
        msort (array, buf, mid + 1, high, size, compare, arg);
64
729k
65
729k
    if (mid > low)
66
351k
        msort (array, buf, low, mid, size, compare, arg);
67
729k
68
729k
    ah = ((char *) array) + ((high + 1) * size);
69
729k
    am = ((char *) array) + ((mid + 1) * size);
70
729k
    a1 = al = ((char *) array) + (low * size);
71
729k
72
729k
    b = (char *) buf;
73
729k
    lo = al;
74
729k
    hi = am;
75
729k
76
810k
    do {
77
810k
        ls = lo;
78
810k
        hs = hi;
79
810k
80
810k
        if (
lo > al || 810k
hi > am747k
)
{80.8k
81
80.8k
            /* our last loop already compared lo & hi and found lo <= hi */
82
80.8k
            lo += size;
83
80.8k
        }
84
810k
85
2.03M
        while (
lo < am && 2.03M
compare (lo, hi, arg) <= 01.45M
)
86
1.22M
            lo += size;
87
810k
88
810k
        if (
lo < am810k
)
{225k
89
225k
            if (
copied == 0225k
)
{173k
90
173k
                /* avoid copying the leading items */
91
173k
                a1 = lo;
92
173k
                ls = lo;
93
173k
            }
94
225k
95
225k
            /* our last compare tells us hi < lo */
96
225k
            hi += size;
97
225k
98
373k
            while (
hi < ah && 373k
compare (hi, lo, arg) < 0228k
)
99
148k
                hi += size;
100
225k
101
225k
            if (
lo > ls225k
)
{51.8k
102
51.8k
                memcpy (b, ls, lo - ls);
103
51.8k
                copied += (lo - ls);
104
51.8k
                b += (lo - ls);
105
51.8k
            }
106
225k
107
225k
            memcpy (b, hs, hi - hs);
108
225k
            copied += (hi - hs);
109
225k
            b += (hi - hs);
110
585k
        } else 
if (585k
copied585k
)
{29.0k
111
29.0k
            memcpy (b, ls, lo - ls);
112
29.0k
            copied += (lo - ls);
113
29.0k
            b += (lo - ls);
114
29.0k
115
29.0k
            /* copy everything we needed to re-order back into array */
116
29.0k
            memcpy (a1, buf, copied);
117
29.0k
            return;
118
556k
        } else {
119
556k
            /* everything already in order */
120
556k
            return;
121
556k
        }
122
225k
    } while (hi < ah);
123
729k
124
144k
    
if (144k
lo < am144k
)
{144k
125
144k
        memcpy (b, lo, am - lo);
126
144k
        copied += (am - lo);
127
144k
    }
128
144k
129
144k
    memcpy (a1, buf, copied);
130
144k
}
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
189k
{
136
189k
    void *tmp;
137
189k
138
189k
    if (nmemb < 2)
139
30.8k
        return 0;
140
189k
141
158k
    
if (158k
!(tmp = malloc (nmemb * size))158k
)
{0
142
0
        errno = ENOMEM;
143
0
        return -1;
144
0
    }
145
158k
146
158k
    msort (base, tmp, 0, nmemb - 1, size, compare, arg);
147
158k
148
158k
    free (tmp);
149
158k
150
158k
    return 0;
151
158k
}
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
189k
{
156
189k
    return MergeSort (pbase, total_elems, size, cmp, arg);
157
189k
}