# HG changeset patch # User Josef 'Jeff' Sipek # Date 1559141899 14400 # Node ID 337fad78b8ae0319d844345f26d4c15a9652a01c # Parent 7a839d21735a7a1fa85d40ea5df87918e126fbd6 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 diff -r 7a839d21735a -r 337fad78b8ae base64.c --- 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;