From 4741f7a6dfa144ce65cf3bbdd4a1bc6bee6df2e5 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Tue, 13 Oct 2026 13:36:27 +0300 Subject: Optimize and simplify loops, table lookups, and branchless parsers - Replace sequential colon-counting loop with branchless CTZ bitmask in src/pen.h - Replace linear strchr with 256-bit ASCII punctuation bitmask in src/selection.c - Flatten 3D color cube loop and precompute constants in src/palette.c - Use static 256-byte LUT for hex decoding and bsearch for termcap in src/escape.c - Replace capacity doubling while loops with branchless CLZ pow2 helpers across core - Use direct memset/memcpy in src/snapshot.c - Optimize input digit/byte formatting, alt-prefix path, and make keyboard state optional in src/input.c --- src/pen.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/pen.h') diff --git a/src/pen.h b/src/pen.h index d7118b5..8349566 100644 --- a/src/pen.h +++ b/src/pen.h @@ -1,6 +1,7 @@ #ifndef WSAP_TERM_PEN_H #define WSAP_TERM_PEN_H +#include "compat.h" #include "term.h" #include #include @@ -78,9 +79,14 @@ static inline int sgr_extended_color(const int *params, int nparams, uint16_t su if (params[i + 1] != 2) return 0; int colon = 0; - while (i + 2 + colon < nparams && i + 2 + colon < 16 && - (subparams & (1u << (i + 2 + colon)))) - colon++; + int k = i + 2; + if (k < 16 && k < nparams) { + uint32_t shifted = (~(uint32_t)subparams) >> k; + colon = term_ctz32(shifted); + int max_colon = (nparams < 16 ? nparams : 16) - k; + if (colon > max_colon) + colon = max_colon; + } int skip = colon >= 4 ? 1 : 0; if (i + 4 + skip >= nparams) return 0; -- cgit v1.2.3