Add fls function

This commit is contained in:
xj 2024-07-04 05:25:53 -07:00
parent 4a657a3044
commit d2164713b3
2 changed files with 49 additions and 5 deletions

View File

@ -128,9 +128,8 @@ static inline size_t xhci_align ( size_t len ) {
size_t align;
/* Align to own length (rounded up to a power of two) */
// align = ( 1 << fls ( len - 1 ) );
// align = ( 1 << fls ( len - 1 ) );
align = XHCI_MIN_ALIGN;
align = ( 1 << fls ( len - 1 ) );
// align = XHCI_MIN_ALIGN;
/* Round up to XHCI_MIN_ALIGN if needed */
if ( align < XHCI_MIN_ALIGN )
@ -2228,8 +2227,8 @@ int xhci_work_endpoint_open ( struct xhci_host *xhci, struct xhci_slot *slot, st
/* Calculate interval */
if ( ctx_type & XHCI_EP_TYPE_PERIODIC ) {
// ep->interval = ( fls ( ep->interval ) - 1 );
ep->interval = 256;
ep->interval = ( fls ( ep->interval ) - 1 );
// ep->interval = 256;
}
ep->ctx_type = ctx_type;

View File

@ -992,4 +992,49 @@ void xhci_dump_trbs(const union xhci_trb *trbs, unsigned int count);
/* Dump slot context */
void xhci_dump_slot_ctx(const struct xhci_slot_context *const sc);
/*
* Find the last (most-significant) bit set
* @x: the 64 bit length variate to search.
*/
static __always_inline int fls(uint64_t x)
{
int r = 64;
if(!x)
return 0;
if (!(x & 0xffffffff00000000u)) {
x <<= 32;
r -= 32;
}
if (!(x & 0xffff000000000000u)) {
x <<= 16;
r -= 16;
}
if (!(x & 0xff00000000000000u)) {
x <<= 8;
r -= 8;
}
if (!(x & 0xf000000000000000u)) {
x <<= 4;
r -= 4;
}
if (!(x & 0xc000000000000000u)) {
x <<= 2;
r -= 2;
}
if (!(x & 0x8000000000000000u)) {
x <<= 1;
r -= 1;
}
return r;
}
#endif