changeset 742:337fad78b8ae

base64: use a less complicated validity check in the decode loop Checking each of the four input chars for validity generates horrible machine code with many comparisons and conditional jumps. We can reduce this to just one comparison and one conditional jump by making assumptions about what invalid inputs look like (i.e., 0xff). Suggested by Timo Sirainen. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 29 May 2019 10:58:19 -0400
parents 7a839d21735a
children b7e409c72ad8
files base64.c
diffstat 1 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/base64.c	Mon Apr 01 16:20:08 2019 +0300
+++ b/base64.c	Wed May 29 10:58:19 2019 -0400
@@ -188,7 +188,15 @@
 		const uint8_t d = table[in[(i * 4) + 3]];
 		uint32_t v;
 
-		if ((a == INV) || (b == INV) || (c == INV) || (d == INV))
+		/*
+		 * If any one of the four chars in this group is INV, error
+		 * out.
+		 *
+		 * Optimization: Since no valid char translates to a value
+		 * with with 0x40 or 0x80 set, the only way to get 0xff out
+		 * is if one of the four values was 0xff - aka. INV.
+		 */
+		if ((a | b | c | d) == INV)
 			return -1;
 
 		v = (a << 18) | (b << 12) | (c << 6) | d;