Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755047AbbEKTzH (ORCPT ); Mon, 11 May 2015 15:55:07 -0400 Received: from ns.horizon.com ([71.41.210.147]:39811 "HELO ns.horizon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1754876AbbEKTzC (ORCPT ); Mon, 11 May 2015 15:55:02 -0400 Date: 11 May 2015 15:55:01 -0400 Message-ID: <20150511195501.3772.qmail@ns.horizon.com> From: "George Spelvin" To: joe@perches.com, linux@rasmusvillemoes.dk Subject: [PATCH 2/2] lib/vsprintf.c: Further simplify uuid_string() Cc: linux-kernel@vger.kernel.org, linux@horizon.com In-Reply-To: <20150511163230.14333.qmail@ns.horizon.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2009 Lines: 68 Make the endianness permutation table do double duty by having it list not source offsets, but destination offsets. Thus, it both puts the bytes in the right order and skips the hyphens. This further shrinks the code from 256 to 214 bytes. Eliminating erratic branches probably helps speed, too. This has been verified to produce the same output as the older code using a user-space test harness. Signed-off-by: George Spelvin --- lib/vsprintf.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 4c4f9055..38c1d87e 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1265,10 +1265,9 @@ char *uuid_string(char *buf, char *end, const u8 *addr, struct printf_spec spec, const char *fmt) { char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; - char *p = uuid; int i; - static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; - static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; + static const u8 be[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34}; + static const u8 le[16] = {6,4,2,0,11,9,16,14,19,21,24,26,28,30,32,34}; const u8 *index = be; const char *hex = hex_asc; @@ -1284,20 +1283,14 @@ char *uuid_string(char *buf, char *end, const u8 *addr, } for (i = 0; i < 16; i++) { - u8 byte = addr[index[i]]; - *p++ = hex[byte >> 4]; - *p++ = hex[byte & 0x0f]; - switch (i) { - case 3: - case 5: - case 7: - case 9: - *p++ = '-'; - break; - } - } + u8 byte = addr[i]; + char *p = uuid + index[i]; - *p = 0; + p[0] = hex[byte >> 4]; + p[1] = hex[byte & 0x0f]; + } + uuid[23] = uuid[18] = uuid[13] = uuid[8] = '-'; + uuid[36] = 0; return string(buf, end, uuid, spec); } -- 2.1.4 -- 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/