Skip to content

Commit b587e76

Browse files
committed
Rewrite with faster clz instruction
1 parent f0a24fe commit b587e76

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010
#define MASK(n) (~((~0U << (n))))
1111

1212
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
13+
14+
/* Ensure that __builtin_clz is never called with 0 argument */
15+
static inline int ilog2(int x)
16+
{
17+
return 31 - __builtin_clz(x | 1);
18+
}

riscv.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,7 @@ void vm_step(vm_t *vm)
763763

764764
if ((vm->sstatus_sie || !vm->s_mode) && (vm->sip & vm->sie)) {
765765
uint32_t applicable = (vm->sip & vm->sie);
766-
uint8_t idx = 32;
767-
while (!(applicable & (1 << --idx)))
768-
; /* TODO: Rewrite with faster a lookup table. */
766+
uint8_t idx = ilog2(applicable);
769767
vm->exc_cause = (1U << 31) | idx;
770768
vm->stval = 0;
771769
vm_trap(vm);

uart.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ void u8250_update_interrupts(u8250_state_t *uart)
4848
uart->pending_ints &= uart->ier;
4949

5050
/* Update current interrupt (higher bits -> more priority) */
51-
if (uart->pending_ints) {
52-
uart->current_int = 8;
53-
while (!(uart->pending_ints & (1 << --uart->current_int)))
54-
; /* TODO: Rewrite with faster lookup table. */
55-
}
51+
if (uart->pending_ints)
52+
uart->current_int = ilog2(uart->pending_ints);
5653
}
5754

5855
void u8250_check_ready(u8250_state_t *uart)

0 commit comments

Comments
 (0)