OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / libiberty / sort.c
index 6dca09a..3738dd7 100644 (file)
@@ -16,27 +16,35 @@ General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 #include "libiberty.h"
 #include "sort.h"
+#ifdef HAVE_LIMITS_H
 #include <limits.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
-/* POINTERSP and WORKP both point to arrays of N pointers.  When
-   this function returns POINTERSP will point to a sorted version of
-   the original array pointed to by POINTERSP.  */
+#ifndef UCHAR_MAX
+#define UCHAR_MAX ((unsigned char)(-1))
+#endif
 
-void sort_pointers (n, pointers, work)
-     size_t n;
-     void **pointers;
-     void **work;
+/* POINTERS and WORK are both arrays of N pointers.  When this
+   function returns POINTERS will be sorted in ascending order.  */
+
+void sort_pointers (size_t n, void **pointers, void **work)
 {
   /* The type of a single digit.  This can be any unsigned integral
      type.  When changing this, DIGIT_MAX should be changed as 
@@ -63,8 +71,11 @@ void sort_pointers (n, pointers, work)
     abort ();
 
   /* Figure out the endianness of the machine.  */
-  for (i = 0; i < sizeof (size_t); ++i)
-    ((char *)&j)[i] = i;
+  for (i = 0, j = 0; i < sizeof (size_t); ++i)
+    {
+      j *= (UCHAR_MAX + 1);
+      j += i;
+    }
   big_endian_p = (((char *)&j)[0] == 0);
 
   /* Move through the pointer values from least significant to most
@@ -91,8 +102,8 @@ void sort_pointers (n, pointers, work)
       /* Compute the address of the appropriate digit in the first and
         one-past-the-end elements of the array.  On a little-endian
         machine, the least-significant digit is closest to the front.  */
-      bias = ((digit_t *) pointers) + i;
-      top = ((digit_t *) (pointers + n)) + i;
+      bias = ((digit_t *) pointers) + j;
+      top = ((digit_t *) (pointers + n)) + j;
 
       /* Count how many there are of each value.  At the end of this
         loop, COUNT[K] will contain the number of pointers whose Ith
@@ -109,7 +120,7 @@ void sort_pointers (n, pointers, work)
 
       /* Now, drop the pointers into their correct locations.  */
       for (pointerp = pointers + n - 1; pointerp >= pointers; --pointerp)
-       work[--count[((digit_t *) pointerp)[i]]] = *pointerp;
+       work[--count[((digit_t *) pointerp)[j]]] = *pointerp;
 
       /* Swap WORK and POINTERS so that POINTERS contains the sorted
         array.  */
@@ -126,8 +137,7 @@ void sort_pointers (n, pointers, work)
 
 #include <stdio.h>
 
-void *xmalloc (n)
-     size_t n;
+void *xmalloc (size_t n)
 {
   return malloc (n);
 }
@@ -145,8 +155,8 @@ int main (int argc, char **argv)
   else
     k = 10;
 
-  pointers = xmalloc (k * sizeof (void *));
-  work = xmalloc (k * sizeof (void *));
+  pointers = XNEWVEC (void*, k);
+  work = XNEWVEC (void*, k);
 
   for (i = 0; i < k; ++i)
     {