Skip to content

Commit 01d21b0

Browse files
committed
Revert integerlength() to pure (but more efficient) C as glibc does not provide fls()
1 parent 3569e54 commit 01d21b0

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/gcfinal.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
/*************************************************************************/
4444

4545
#include <stdio.h> // for printf
46-
#include <strings.h> // for fls
4746
#include "address.h" // for HILOC
4847
#include "adr68k.h" // for NativeAligned4FromLAddr, LAddrFromNative
4948
#include "array.h" // for arrayblock, ARRAYBLOCKTRAILERCELLS, MAXBUCK...
@@ -128,7 +127,29 @@ struct buffer {
128127
#endif /* BYTESWAP */
129128

130129
static int integerlength(unsigned int n) {
131-
return (fls(n));
130+
int p = 0;
131+
132+
if (n <= 2) return (n); /* easy case */
133+
if (n >= 65536) {
134+
n >>= 16;
135+
p += 16;
136+
}
137+
if (n >= 256) {
138+
n >>= 8;
139+
p += 8;
140+
}
141+
if (n >= 16) {
142+
n >>= 4;
143+
p += 4;
144+
}
145+
if (n >= 4) {
146+
n >>= 2;
147+
p += 2;
148+
}
149+
if (n >= 2) {
150+
p += 1;
151+
}
152+
return (p + 1);
132153
}
133154

134155
/************************************************************************/

0 commit comments

Comments
 (0)