Skip to content

Commit

Permalink
add fallback implementations of clz/ctz
Browse files Browse the repository at this point in the history
  • Loading branch information
yamt committed Aug 12, 2024
1 parent afe5bf1 commit 8236407
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/insn.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,17 @@ clz(uint32_t v)
if (v == 0) {
return 32;
}
#if __has_builtin(__builtin_clz)
return __builtin_clz(v);
#else
uint32_t cnt = 0;
uint32_t u = v;
while ((u & 0x80000000) == 0) {
cnt++;
u << 1;
}
return cnt;
#endif
}

static uint32_t
Expand All @@ -247,7 +257,17 @@ ctz(uint32_t v)
if (v == 0) {
return 32;
}
#if __has_builtin(__builtin_ctz)
return __builtin_ctz(v);
#else
uint32_t cnt = 0;
uint32_t u = v;
while ((u & 1) == 0) {
cnt++;
u >>= 1;
}
return cnt;
#endif
}

static uint32_t
Expand Down

0 comments on commit 8236407

Please sign in to comment.