Skip to content

Commit ce81312

Browse files
committed
misc: Add count_lead_ones() function, useful for UTF-8 handling.
1 parent 63143c9 commit ce81312

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

py/misc.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,17 @@ int DEBUG_printf(const char *fmt, ...);
166166

167167
extern uint mp_verbose_flag;
168168

169+
// This is useful for unicode handling. Some CPU archs has
170+
// special instructions for efficient implentation of this
171+
// function (e.g. CLZ on ARM).
172+
#ifndef count_lead_ones
173+
static inline uint count_lead_ones(byte val) {
174+
uint c = 0;
175+
for (byte mask = 0x80; val & mask; mask >>= 1) {
176+
c++;
177+
}
178+
return c;
179+
}
180+
#endif
181+
169182
#endif // _INCLUDED_MINILIB_H

0 commit comments

Comments
 (0)