All of lore.kernel.org
 help / color / mirror / Atom feed
From: "George Spelvin" <linux@horizon.com>
To: joe@perches.com, linux@horizon.com
Cc: linux-kernel@vger.kernel.org, linux@rasmusvillemoes.dk
Subject: [PATCH 2/2 v2] lib/vsprintf.c: Further simplify uuid_string()
Date: 12 May 2015 05:10:36 -0400	[thread overview]
Message-ID: <20150512091036.5773.qmail@ns.horizon.com> (raw)
In-Reply-To: <1431396840.2884.40.camel@perches.com>

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.

Signed-off-by: George Spelvin <linux@horizon.com>
---
> These might be better with a little comment/explanation
> of the values as output offsets for each index.

Like this?  I had thought about it, and had decied not to change the
existing lacomic code style, as it didn't seem any harder to understand
than the original.  But I'm happy to add comments.

 lib/vsprintf.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index c3fb18bb..e5db83a9 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1265,14 +1265,14 @@ 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};
+	/* Offset in uuid[] where each byte is printed, two cases. */
+	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;
+	int i;
 
-	switch (*(++fmt)) {
+	switch (fmt[1]) {
 	case 'L':
 		hex = hex_asc_upper;	/* fall-through */
 	case 'l':
@@ -1283,21 +1283,17 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
 		break;
 	}
 
+	/* Format each byte of the raw uuid into the buffer */
 	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];
+	}
+	/* Insert the fixed punctuation */
+	uuid[23] = uuid[18] = uuid[13] = uuid[8] = '-';
+	uuid[36] = 0;
 
 	return string(buf, end, uuid, spec);
 }
-- 
2.1.4


  reply	other threads:[~2015-05-12  9:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-11 16:32 [RFC PATCH] lib/vsprintf.c: Simplify uuid_string() George Spelvin
2015-05-11 16:49 ` Joe Perches
2015-05-11 16:55   ` George Spelvin
2015-05-11 19:53 ` [PATCH 1/2] " George Spelvin
2015-05-11 19:55 ` [PATCH 2/2] lib/vsprintf.c: Further simplify uuid_string() George Spelvin
2015-05-12  2:14   ` Joe Perches
2015-05-12  9:10     ` George Spelvin [this message]
2015-05-12 11:26       ` [PATCH 2/2 v2] " Rasmus Villemoes
2015-05-12 13:57         ` George Spelvin
2015-05-12 16:59           ` Joe Perches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150512091036.5773.qmail@ns.horizon.com \
    --to=linux@horizon.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.