1*38fd1498Szrj /* Sorting algorithms.
2*38fd1498Szrj Copyright (C) 2000-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Contributed by Mark Mitchell <mark@codesourcery.com>.
4*38fd1498Szrj
5*38fd1498Szrj This file is part of GNU CC.
6*38fd1498Szrj
7*38fd1498Szrj GNU CC is free software; you can redistribute it and/or modify it
8*38fd1498Szrj under the terms of the GNU General Public License as published by
9*38fd1498Szrj the Free Software Foundation; either version 2, or (at your option)
10*38fd1498Szrj any later version.
11*38fd1498Szrj
12*38fd1498Szrj GNU CC is distributed in the hope that it will be useful, but
13*38fd1498Szrj WITHOUT ANY WARRANTY; without even the implied warranty of
14*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15*38fd1498Szrj General Public License for more details.
16*38fd1498Szrj
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GNU CC; see the file COPYING. If not, write to
19*38fd1498Szrj the Free Software Foundation, 51 Franklin Street - Fifth Floor,
20*38fd1498Szrj Boston, MA 02110-1301, USA. */
21*38fd1498Szrj
22*38fd1498Szrj #ifdef HAVE_CONFIG_H
23*38fd1498Szrj #include "config.h"
24*38fd1498Szrj #endif
25*38fd1498Szrj #include "libiberty.h"
26*38fd1498Szrj #include "sort.h"
27*38fd1498Szrj #ifdef HAVE_LIMITS_H
28*38fd1498Szrj #include <limits.h>
29*38fd1498Szrj #endif
30*38fd1498Szrj #ifdef HAVE_SYS_PARAM_H
31*38fd1498Szrj #include <sys/param.h>
32*38fd1498Szrj #endif
33*38fd1498Szrj #ifdef HAVE_STDLIB_H
34*38fd1498Szrj #include <stdlib.h>
35*38fd1498Szrj #endif
36*38fd1498Szrj #ifdef HAVE_STRING_H
37*38fd1498Szrj #include <string.h>
38*38fd1498Szrj #endif
39*38fd1498Szrj
40*38fd1498Szrj #ifndef UCHAR_MAX
41*38fd1498Szrj #define UCHAR_MAX ((unsigned char)(-1))
42*38fd1498Szrj #endif
43*38fd1498Szrj
44*38fd1498Szrj /* POINTERS and WORK are both arrays of N pointers. When this
45*38fd1498Szrj function returns POINTERS will be sorted in ascending order. */
46*38fd1498Szrj
sort_pointers(size_t n,void ** pointers,void ** work)47*38fd1498Szrj void sort_pointers (size_t n, void **pointers, void **work)
48*38fd1498Szrj {
49*38fd1498Szrj /* The type of a single digit. This can be any unsigned integral
50*38fd1498Szrj type. When changing this, DIGIT_MAX should be changed as
51*38fd1498Szrj well. */
52*38fd1498Szrj typedef unsigned char digit_t;
53*38fd1498Szrj
54*38fd1498Szrj /* The maximum value a single digit can have. */
55*38fd1498Szrj #define DIGIT_MAX (UCHAR_MAX + 1)
56*38fd1498Szrj
57*38fd1498Szrj /* The Ith entry is the number of elements in *POINTERSP that have I
58*38fd1498Szrj in the digit on which we are currently sorting. */
59*38fd1498Szrj unsigned int count[DIGIT_MAX];
60*38fd1498Szrj /* Nonzero if we are running on a big-endian machine. */
61*38fd1498Szrj int big_endian_p;
62*38fd1498Szrj size_t i;
63*38fd1498Szrj size_t j;
64*38fd1498Szrj
65*38fd1498Szrj /* The algorithm used here is radix sort which takes time linear in
66*38fd1498Szrj the number of elements in the array. */
67*38fd1498Szrj
68*38fd1498Szrj /* The algorithm here depends on being able to swap the two arrays
69*38fd1498Szrj an even number of times. */
70*38fd1498Szrj if ((sizeof (void *) / sizeof (digit_t)) % 2 != 0)
71*38fd1498Szrj abort ();
72*38fd1498Szrj
73*38fd1498Szrj /* Figure out the endianness of the machine. */
74*38fd1498Szrj for (i = 0, j = 0; i < sizeof (size_t); ++i)
75*38fd1498Szrj {
76*38fd1498Szrj j *= (UCHAR_MAX + 1);
77*38fd1498Szrj j += i;
78*38fd1498Szrj }
79*38fd1498Szrj big_endian_p = (((char *)&j)[0] == 0);
80*38fd1498Szrj
81*38fd1498Szrj /* Move through the pointer values from least significant to most
82*38fd1498Szrj significant digits. */
83*38fd1498Szrj for (i = 0; i < sizeof (void *) / sizeof (digit_t); ++i)
84*38fd1498Szrj {
85*38fd1498Szrj digit_t *digit;
86*38fd1498Szrj digit_t *bias;
87*38fd1498Szrj digit_t *top;
88*38fd1498Szrj unsigned int *countp;
89*38fd1498Szrj void **pointerp;
90*38fd1498Szrj
91*38fd1498Szrj /* The offset from the start of the pointer will depend on the
92*38fd1498Szrj endianness of the machine. */
93*38fd1498Szrj if (big_endian_p)
94*38fd1498Szrj j = sizeof (void *) / sizeof (digit_t) - i;
95*38fd1498Szrj else
96*38fd1498Szrj j = i;
97*38fd1498Szrj
98*38fd1498Szrj /* Now, perform a stable sort on this digit. We use counting
99*38fd1498Szrj sort. */
100*38fd1498Szrj memset (count, 0, DIGIT_MAX * sizeof (unsigned int));
101*38fd1498Szrj
102*38fd1498Szrj /* Compute the address of the appropriate digit in the first and
103*38fd1498Szrj one-past-the-end elements of the array. On a little-endian
104*38fd1498Szrj machine, the least-significant digit is closest to the front. */
105*38fd1498Szrj bias = ((digit_t *) pointers) + j;
106*38fd1498Szrj top = ((digit_t *) (pointers + n)) + j;
107*38fd1498Szrj
108*38fd1498Szrj /* Count how many there are of each value. At the end of this
109*38fd1498Szrj loop, COUNT[K] will contain the number of pointers whose Ith
110*38fd1498Szrj digit is K. */
111*38fd1498Szrj for (digit = bias;
112*38fd1498Szrj digit < top;
113*38fd1498Szrj digit += sizeof (void *) / sizeof (digit_t))
114*38fd1498Szrj ++count[*digit];
115*38fd1498Szrj
116*38fd1498Szrj /* Now, make COUNT[K] contain the number of pointers whose Ith
117*38fd1498Szrj digit is less than or equal to K. */
118*38fd1498Szrj for (countp = count + 1; countp < count + DIGIT_MAX; ++countp)
119*38fd1498Szrj *countp += countp[-1];
120*38fd1498Szrj
121*38fd1498Szrj /* Now, drop the pointers into their correct locations. */
122*38fd1498Szrj for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
123*38fd1498Szrj work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
124*38fd1498Szrj
125*38fd1498Szrj /* Swap WORK and POINTERS so that POINTERS contains the sorted
126*38fd1498Szrj array. */
127*38fd1498Szrj pointerp = pointers;
128*38fd1498Szrj pointers = work;
129*38fd1498Szrj work = pointerp;
130*38fd1498Szrj }
131*38fd1498Szrj }
132*38fd1498Szrj
133*38fd1498Szrj /* Everything below here is a unit test for the routines in this
134*38fd1498Szrj file. */
135*38fd1498Szrj
136*38fd1498Szrj #ifdef UNIT_TEST
137*38fd1498Szrj
138*38fd1498Szrj #include <stdio.h>
139*38fd1498Szrj
xmalloc(size_t n)140*38fd1498Szrj void *xmalloc (size_t n)
141*38fd1498Szrj {
142*38fd1498Szrj return malloc (n);
143*38fd1498Szrj }
144*38fd1498Szrj
main(int argc,char ** argv)145*38fd1498Szrj int main (int argc, char **argv)
146*38fd1498Szrj {
147*38fd1498Szrj int k;
148*38fd1498Szrj int result;
149*38fd1498Szrj size_t i;
150*38fd1498Szrj void **pointers;
151*38fd1498Szrj void **work;
152*38fd1498Szrj
153*38fd1498Szrj if (argc > 1)
154*38fd1498Szrj k = atoi (argv[1]);
155*38fd1498Szrj else
156*38fd1498Szrj k = 10;
157*38fd1498Szrj
158*38fd1498Szrj pointers = XNEWVEC (void*, k);
159*38fd1498Szrj work = XNEWVEC (void*, k);
160*38fd1498Szrj
161*38fd1498Szrj for (i = 0; i < k; ++i)
162*38fd1498Szrj {
163*38fd1498Szrj pointers[i] = (void *) random ();
164*38fd1498Szrj printf ("%x\n", pointers[i]);
165*38fd1498Szrj }
166*38fd1498Szrj
167*38fd1498Szrj sort_pointers (k, pointers, work);
168*38fd1498Szrj
169*38fd1498Szrj printf ("\nSorted\n\n");
170*38fd1498Szrj
171*38fd1498Szrj result = 0;
172*38fd1498Szrj
173*38fd1498Szrj for (i = 0; i < k; ++i)
174*38fd1498Szrj {
175*38fd1498Szrj printf ("%x\n", pointers[i]);
176*38fd1498Szrj if (i > 0 && (char*) pointers[i] < (char*) pointers[i - 1])
177*38fd1498Szrj result = 1;
178*38fd1498Szrj }
179*38fd1498Szrj
180*38fd1498Szrj free (pointers);
181*38fd1498Szrj free (work);
182*38fd1498Szrj
183*38fd1498Szrj return result;
184*38fd1498Szrj }
185*38fd1498Szrj
186*38fd1498Szrj #endif
187