Skip to content

Commit 6328301

Browse files
committed
revert to use __builtin_ctz() for optimized time
1 parent b992b6d commit 6328301

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/portable/renesas/rusb2/dcd_rusb2.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,18 @@ void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
915915
//--------------------------------------------------------------------+
916916
// ISR
917917
//--------------------------------------------------------------------+
918+
919+
#if defined(__CCRX__)
920+
TU_ATTR_ALWAYS_INLINE static inline unsigned __builtin_ctz(unsigned int value) {
921+
unsigned int count = 0;
922+
while ((value & 1) == 0) {
923+
value >>= 1;
924+
count++;
925+
}
926+
return count;
927+
}
928+
#endif
929+
918930
void dcd_int_handler(uint8_t rhport)
919931
{
920932
rusb2_reg_t* rusb = RUSB2_REG(rhport);
@@ -1002,13 +1014,13 @@ void dcd_int_handler(uint8_t rhport)
10021014
// Buffer ready
10031015
if ( is0 & RUSB2_INTSTS0_BRDY_Msk ) {
10041016
const unsigned m = rusb->BRDYENB;
1005-
const unsigned s = rusb->BRDYSTS & m;
1017+
unsigned s = rusb->BRDYSTS & m;
10061018
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
10071019
rusb->BRDYSTS = ~s;
1008-
for (unsigned p = 0; p < PIPE_COUNT; ++p) {
1009-
if (tu_bit_test(s, p)) {
1010-
process_pipe_brdy(rhport, p);
1011-
}
1020+
while (s) {
1021+
const unsigned num = __builtin_ctz(s);
1022+
process_pipe_brdy(rhport, num);
1023+
s &= ~TU_BIT(num);
10121024
}
10131025
}
10141026
}

src/portable/renesas/rusb2/hcd_rusb2.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,17 @@ bool hcd_edpt_clear_stall(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr) {
758758
//--------------------------------------------------------------------+
759759
// ISR
760760
//--------------------------------------------------------------------+
761+
#if defined(__CCRX__)
762+
TU_ATTR_ALWAYS_INLINE static inline unsigned __builtin_ctz(unsigned int value) {
763+
unsigned int count = 0;
764+
while ((value & 1) == 0) {
765+
value >>= 1;
766+
count++;
767+
}
768+
return count;
769+
}
770+
#endif
771+
761772
void hcd_int_handler(uint8_t rhport, bool in_isr) {
762773
(void) in_isr;
763774

@@ -807,23 +818,12 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
807818
}
808819
}
809820

810-
#if defined(__CCRX__)
811-
static const int Mod37BitPosition[] = {
812-
-1, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4,
813-
7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5,
814-
20, 8, 19, 18};
815-
#endif
816-
817821
if (is0 & RUSB2_INTSTS0_NRDY_Msk) {
818822
const unsigned m = rusb->NRDYENB;
819823
unsigned s = rusb->NRDYSTS & m;
820824
rusb->NRDYSTS = ~s;
821825
while (s) {
822-
#if defined(__CCRX__)
823-
const unsigned num = Mod37BitPosition[(-s & s) % 37];
824-
#else
825826
const unsigned num = __builtin_ctz(s);
826-
#endif
827827
process_pipe_nrdy(rhport, num);
828828
s &= ~TU_BIT(num);
829829
}
@@ -833,10 +833,10 @@ void hcd_int_handler(uint8_t rhport, bool in_isr) {
833833
unsigned s = rusb->BRDYSTS & m;
834834
/* clear active bits (don't write 0 to already cleared bits according to the HW manual) */
835835
rusb->BRDYSTS = ~s;
836-
for (unsigned p = 0; p < PIPE_COUNT; ++p) {
837-
if (tu_bit_test(s, p)) {
838-
process_pipe_brdy(rhport, p);
839-
}
836+
while (s) {
837+
const unsigned num = __builtin_ctz(s);
838+
process_pipe_brdy(rhport, num);
839+
s &= ~TU_BIT(num);
840840
}
841841
}
842842
}

0 commit comments

Comments
 (0)