OSDN Git Service

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