Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752905AbbERJuT (ORCPT ); Mon, 18 May 2015 05:50:19 -0400 Received: from mail.bmw-carit.de ([62.245.222.98]:49408 "EHLO linuxmail.bmw-carit.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753288AbbERJuN (ORCPT ); Mon, 18 May 2015 05:50:13 -0400 From: Daniel Wagner To: Andrew Morton , Rasmus Villemoes Cc: mm-commits@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Wagner Subject: [PATCH] lib/sort: Move the alignment check into a function Date: Mon, 18 May 2015 11:50:09 +0200 Message-Id: <1431942609-14457-1-git-send-email-daniel.wagner@bmw-carit.de> X-Mailer: git-send-email 2.1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2507 Lines: 103 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 Suggested-by: Rasmus Villemoes Cc: Andrew Morton -- Hi, I did this patch on top of current mmotm tree [1]. Hopefully I got this right, if you want it merged into v2 of this patch or any different version just let me know. Eventually I will get this right :) cheers, daniel [1] http://ozlabs.org/~akpm/mmotm/ --- lib/sort.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/sort.c b/lib/sort.c index 9c6b229..fc20df4 100644 --- a/lib/sort.c +++ b/lib/sort.c @@ -8,6 +8,12 @@ #include #include +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; } -- 2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/