Blame SOURCES/0110-Add-quicksort-implementation.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Peter Jones <pjones@redhat.com>
8631a2
Date: Fri, 9 Dec 2016 15:39:47 -0500
8631a2
Subject: [PATCH] Add quicksort implementation
8631a2
8631a2
This will be used to sort the boot menu entries that are read from
8631a2
the BootLoaderSpec config files.
8631a2
---
8631a2
 grub-core/kern/qsort.c | 279 +++++++++++++++++++++++++++++++++++++++++++++++++
8631a2
 include/grub/misc.h    |  15 +++
8631a2
 2 files changed, 294 insertions(+)
8631a2
 create mode 100644 grub-core/kern/qsort.c
8631a2
8631a2
diff --git a/grub-core/kern/qsort.c b/grub-core/kern/qsort.c
8631a2
new file mode 100644
8631a2
index 00000000000..7f3fc9ffdae
8631a2
--- /dev/null
8631a2
+++ b/grub-core/kern/qsort.c
8631a2
@@ -0,0 +1,279 @@
8631a2
+/* quicksort
8631a2
+ * This file from the GNU C Library.
8631a2
+ * Copyright (C) 1991-2016 Free Software Foundation, Inc.
8631a2
+ * Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
8631a2
+ *
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+/* If you consider tuning this algorithm, you should consult first:
8631a2
+   Engineering a sort function; Jon Bentley and M. Douglas McIlroy;
8631a2
+   Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993.  */
8631a2
+
8631a2
+#include <grub/types.h>
8631a2
+#include <grub/misc.h>
8631a2
+#include <grub/mm.h>
8631a2
+
8631a2
+#define CHAR_BIT 8
8631a2
+
8631a2
+/* Byte-wise swap two items of size SIZE. */
8631a2
+#define SWAP(a, b, size)						      \
8631a2
+  do									      \
8631a2
+    {									      \
8631a2
+      grub_size_t __size = (size);						      \
8631a2
+      char *__a = (a), *__b = (b);					      \
8631a2
+      do								      \
8631a2
+	{								      \
8631a2
+	  char __tmp = *__a;						      \
8631a2
+	  *__a++ = *__b;						      \
8631a2
+	  *__b++ = __tmp;						      \
8631a2
+	} while (--__size > 0);						      \
8631a2
+    } while (0)
8631a2
+
8631a2
+/* Discontinue quicksort algorithm when partition gets below this size.
8631a2
+   This particular magic number was chosen to work best on a Sun 4/260. */
8631a2
+#define MAX_THRESH 4
8631a2
+
8631a2
+/* Stack node declarations used to store unfulfilled partition obligations. */
8631a2
+typedef struct
8631a2
+  {
8631a2
+    char *lo;
8631a2
+    char *hi;
8631a2
+  } stack_node;
8631a2
+
8631a2
+/* The next 4 #defines implement a very fast in-line stack abstraction. */
8631a2
+/* The stack needs log (total_elements) entries (we could even subtract
8631a2
+   log(MAX_THRESH)).  Since total_elements has type grub_size_t, we get as
8631a2
+   upper bound for log (total_elements):
8631a2
+   bits per byte (CHAR_BIT) * sizeof(grub_size_t).  */
8631a2
+#define STACK_SIZE	(CHAR_BIT * sizeof(grub_size_t))
8631a2
+#define PUSH(low, high)	((void) ((top->lo = (low)), (top->hi = (high)), ++top))
8631a2
+#define	POP(low, high)	((void) (--top, (low = top->lo), (high = top->hi)))
8631a2
+#define	STACK_NOT_EMPTY	(stack < top)
8631a2
+
8631a2
+
8631a2
+/* Order size using quicksort.  This implementation incorporates
8631a2
+   four optimizations discussed in Sedgewick:
8631a2
+
8631a2
+   1. Non-recursive, using an explicit stack of pointer that store the
8631a2
+      next array partition to sort.  To save time, this maximum amount
8631a2
+      of space required to store an array of SIZE_MAX is allocated on the
8631a2
+      stack.  Assuming a 32-bit (64 bit) integer for grub_size_t, this needs
8631a2
+      only 32 * sizeof(stack_node) == 256 bytes (for 64 bit: 1024 bytes).
8631a2
+      Pretty cheap, actually.
8631a2
+
8631a2
+   2. Chose the pivot element using a median-of-three decision tree.
8631a2
+      This reduces the probability of selecting a bad pivot value and
8631a2
+      eliminates certain extraneous comparisons.
8631a2
+
8631a2
+   3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
8631a2
+      insertion sort to order the MAX_THRESH items within each partition.
8631a2
+      This is a big win, since insertion sort is faster for small, mostly
8631a2
+      sorted array segments.
8631a2
+
8631a2
+   4. The larger of the two sub-partitions is always pushed onto the
8631a2
+      stack first, with the algorithm then concentrating on the
8631a2
+      smaller partition.  This *guarantees* no more than log (total_elems)
8631a2
+      stack size is needed (actually O(1) in this case)!  */
8631a2
+
8631a2
+void
8631a2
+grub_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
8631a2
+	    grub_compar_d_fn_t cmp, void *arg)
8631a2
+{
8631a2
+  char *base_ptr = (char *) pbase;
8631a2
+
8631a2
+  const grub_size_t max_thresh = MAX_THRESH * size;
8631a2
+
8631a2
+  if (total_elems == 0)
8631a2
+    /* Avoid lossage with unsigned arithmetic below.  */
8631a2
+    return;
8631a2
+
8631a2
+  if (total_elems > MAX_THRESH)
8631a2
+    {
8631a2
+      char *lo = base_ptr;
8631a2
+      char *hi = &lo[size * (total_elems - 1)];
8631a2
+      stack_node stack[STACK_SIZE];
8631a2
+      stack_node *top = stack;
8631a2
+
8631a2
+      PUSH (NULL, NULL);
8631a2
+
8631a2
+      while (STACK_NOT_EMPTY)
8631a2
+        {
8631a2
+          char *left_ptr;
8631a2
+          char *right_ptr;
8631a2
+
8631a2
+	  /* Select median value from among LO, MID, and HI. Rearrange
8631a2
+	     LO and HI so the three values are sorted. This lowers the
8631a2
+	     probability of picking a pathological pivot value and
8631a2
+	     skips a comparison for both the LEFT_PTR and RIGHT_PTR in
8631a2
+	     the while loops. */
8631a2
+
8631a2
+	  char *mid = lo + size * ((hi - lo) / size >> 1);
8631a2
+
8631a2
+	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
8631a2
+	    SWAP (mid, lo, size);
8631a2
+	  if ((*cmp) ((void *) hi, (void *) mid, arg) < 0)
8631a2
+	    SWAP (mid, hi, size);
8631a2
+	  else
8631a2
+	    goto jump_over;
8631a2
+	  if ((*cmp) ((void *) mid, (void *) lo, arg) < 0)
8631a2
+	    SWAP (mid, lo, size);
8631a2
+	jump_over:;
8631a2
+
8631a2
+	  left_ptr  = lo + size;
8631a2
+	  right_ptr = hi - size;
8631a2
+
8631a2
+	  /* Here's the famous ``collapse the walls'' section of quicksort.
8631a2
+	     Gotta like those tight inner loops!  They are the main reason
8631a2
+	     that this algorithm runs much faster than others. */
8631a2
+	  do
8631a2
+	    {
8631a2
+	      while ((*cmp) ((void *) left_ptr, (void *) mid, arg) < 0)
8631a2
+		left_ptr += size;
8631a2
+
8631a2
+	      while ((*cmp) ((void *) mid, (void *) right_ptr, arg) < 0)
8631a2
+		right_ptr -= size;
8631a2
+
8631a2
+	      if (left_ptr < right_ptr)
8631a2
+		{
8631a2
+		  SWAP (left_ptr, right_ptr, size);
8631a2
+		  if (mid == left_ptr)
8631a2
+		    mid = right_ptr;
8631a2
+		  else if (mid == right_ptr)
8631a2
+		    mid = left_ptr;
8631a2
+		  left_ptr += size;
8631a2
+		  right_ptr -= size;
8631a2
+		}
8631a2
+	      else if (left_ptr == right_ptr)
8631a2
+		{
8631a2
+		  left_ptr += size;
8631a2
+		  right_ptr -= size;
8631a2
+		  break;
8631a2
+		}
8631a2
+	    }
8631a2
+	  while (left_ptr <= right_ptr);
8631a2
+
8631a2
+          /* Set up pointers for next iteration.  First determine whether
8631a2
+             left and right partitions are below the threshold size.  If so,
8631a2
+             ignore one or both.  Otherwise, push the larger partition's
8631a2
+             bounds on the stack and continue sorting the smaller one. */
8631a2
+
8631a2
+          if ((grub_size_t) (right_ptr - lo) <= max_thresh)
8631a2
+            {
8631a2
+              if ((grub_size_t) (hi - left_ptr) <= max_thresh)
8631a2
+		/* Ignore both small partitions. */
8631a2
+                POP (lo, hi);
8631a2
+              else
8631a2
+		/* Ignore small left partition. */
8631a2
+                lo = left_ptr;
8631a2
+            }
8631a2
+          else if ((grub_size_t) (hi - left_ptr) <= max_thresh)
8631a2
+	    /* Ignore small right partition. */
8631a2
+            hi = right_ptr;
8631a2
+          else if ((right_ptr - lo) > (hi - left_ptr))
8631a2
+            {
8631a2
+	      /* Push larger left partition indices. */
8631a2
+              PUSH (lo, right_ptr);
8631a2
+              lo = left_ptr;
8631a2
+            }
8631a2
+          else
8631a2
+            {
8631a2
+	      /* Push larger right partition indices. */
8631a2
+              PUSH (left_ptr, hi);
8631a2
+              hi = right_ptr;
8631a2
+            }
8631a2
+        }
8631a2
+    }
8631a2
+
8631a2
+  /* Once the BASE_PTR array is partially sorted by quicksort the rest
8631a2
+     is completely sorted using insertion sort, since this is efficient
8631a2
+     for partitions below MAX_THRESH size. BASE_PTR points to the beginning
8631a2
+     of the array to sort, and END_PTR points at the very last element in
8631a2
+     the array (*not* one beyond it!). */
8631a2
+
8631a2
+#define min(x, y) ((x) < (y) ? (x) : (y))
8631a2
+
8631a2
+  {
8631a2
+    char *const end_ptr = &base_ptr[size * (total_elems - 1)];
8631a2
+    char *tmp_ptr = base_ptr;
8631a2
+    char *thresh = min(end_ptr, base_ptr + max_thresh);
8631a2
+    char *run_ptr;
8631a2
+
8631a2
+    /* Find smallest element in first threshold and place it at the
8631a2
+       array's beginning.  This is the smallest array element,
8631a2
+       and the operation speeds up insertion sort's inner loop. */
8631a2
+
8631a2
+    for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
8631a2
+      if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
8631a2
+        tmp_ptr = run_ptr;
8631a2
+
8631a2
+    if (tmp_ptr != base_ptr)
8631a2
+      SWAP (tmp_ptr, base_ptr, size);
8631a2
+
8631a2
+    /* Insertion sort, running from left-hand-side up to right-hand-side.  */
8631a2
+
8631a2
+    run_ptr = base_ptr + size;
8631a2
+    while ((run_ptr += size) <= end_ptr)
8631a2
+      {
8631a2
+	tmp_ptr = run_ptr - size;
8631a2
+	while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0)
8631a2
+	  tmp_ptr -= size;
8631a2
+
8631a2
+	tmp_ptr += size;
8631a2
+        if (tmp_ptr != run_ptr)
8631a2
+          {
8631a2
+            char *trav;
8631a2
+
8631a2
+	    trav = run_ptr + size;
8631a2
+	    while (--trav >= run_ptr)
8631a2
+              {
8631a2
+                char c = *trav;
8631a2
+                char *hi, *lo;
8631a2
+
8631a2
+                for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
8631a2
+                  *hi = *lo;
8631a2
+                *hi = c;
8631a2
+              }
8631a2
+          }
8631a2
+      }
8631a2
+  }
8631a2
+}
8631a2
+
8631a2
+void *
8631a2
+grub_bsearch (const void *key, const void *base, grub_size_t nmemb, grub_size_t size,
8631a2
+	 grub_compar_d_fn_t compar, void *state)
8631a2
+{
8631a2
+  grub_size_t l, u, idx;
8631a2
+  const void *p;
8631a2
+  int comparison;
8631a2
+
8631a2
+  l = 0;
8631a2
+  u = nmemb;
8631a2
+  while (l < u)
8631a2
+    {
8631a2
+      idx = (l + u) / 2;
8631a2
+      p = (void *) (((const char *) base) + (idx * size));
8631a2
+      comparison = (*compar) (key, p, state);
8631a2
+      if (comparison < 0)
8631a2
+	u = idx;
8631a2
+      else if (comparison > 0)
8631a2
+	l = idx + 1;
8631a2
+      else
8631a2
+	return (void *) p;
8631a2
+    }
8631a2
+
8631a2
+  return NULL;
8631a2
+}
8631a2
diff --git a/include/grub/misc.h b/include/grub/misc.h
8631a2
index fcaf1201e39..cbfae75a1b4 100644
8631a2
--- a/include/grub/misc.h
8631a2
+++ b/include/grub/misc.h
8631a2
@@ -507,4 +507,19 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
8631a2
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
8631a2
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
8631a2
 
8631a2
+typedef int (*grub_compar_d_fn_t) (const void *p0, const void *p1, void *state);
8631a2
+
8631a2
+void *EXPORT_FUNC(grub_bsearch) (const void *key,
8631a2
+			    const void *base,
8631a2
+			    grub_size_t nmemb,
8631a2
+			    grub_size_t size,
8631a2
+			    grub_compar_d_fn_t compar,
8631a2
+			    void *state);
8631a2
+
8631a2
+void EXPORT_FUNC(grub_qsort) (void *const pbase,
8631a2
+			 grub_size_t total_elems,
8631a2
+			 grub_size_t size,
8631a2
+			 grub_compar_d_fn_t cmp,
8631a2
+			 void *state);
8631a2
+
8631a2
 #endif /* ! GRUB_MISC_HEADER */