aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Wagner <daniel.wagner@bmw-carit.de>2015-06-05 15:03:38 +1000
committerMichael Ellerman <mpe@ellerman.id.au>2015-06-05 15:03:38 +1000
commitc4af96c9b22fab1ab0dd7d198b56597e0a0beb89 (patch)
tree500cd0cfbc9a826b6e0e138b952b6890363865d9
parent23008da383d2235377e607d525719de2cf12b886 (diff)
downloadlinux-next-c4af96c9b22fab1ab0dd7d198b56597e0a0beb89.tar.gz
lib/sort: Move the alignment check into a function
Rasmus Villemoes suggestes to move the aliment check into a separate function in order to improve the readability. Furthermore, let's avoid the switch statement since gcc might mess it up. My gcc 4.9.2 produces the same code for the x86_64 configuration. That is switch (size) { case 4: if (alignment_ok(base, 4)) swap_func = u32_swap; break; case 8: if (alignment_ok(base, 8)) swap_func = u64_swap; break; } if (!swap_func) swap_func = generic_swap; } resulted into the same object code. Link: https://lkml.org/lkml/2015/5/15/89 Signed-off-by: Daniel Wagner <daniel.wagner@bmw-carit.de> Suggested-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--lib/sort.c30
1 files changed, 9 insertions, 21 deletions
diff --git a/lib/sort.c b/lib/sort.c
index 9c6b229049cea4..fc20df42aa6f29 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -8,6 +8,12 @@
#include <linux/export.h>
#include <linux/sort.h>
+static int alignment_ok(const void *base, int align)
+{
+ return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
+ ((unsigned long)base & (align - 1)) == 0;
+}
+
static void u32_swap(void *a, void *b, int size)
{
u32 t = *(u32 *)a;
@@ -58,29 +64,11 @@ void sort(void *base, size_t num, size_t size,
int i = (num/2 - 1) * size, n = num * size, c, r;
if (!swap_func) {
-#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
- switch (size) {
- case 4:
+ if (size == 4 && alignment_ok(base, 4))
swap_func = u32_swap;
- break;
- case 8:
+ else if (size == 8 && alignment_ok(base, 8))
swap_func = u64_swap;
- break;
- }
-#else
- switch (size) {
- case 4:
- if (((unsigned long)base & 3) == 0)
- swap_func = u32_swap;
- break;
- case 8:
- if (((unsigned long)base & 7) == 0)
- swap_func = u64_swap;
- break;
- }
-#endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
-
- if (!swap_func)
+ else
swap_func = generic_swap;
}