diff options
author | Ralf Baechle <ralf@linux-mips.org> | 2017-08-01 11:50:34 +0200 |
---|---|---|
committer | Ralf Baechle <ralf@linux-mips.org> | 2017-08-03 04:21:00 +0200 |
commit | 3c8c639c2a8863260b171c1a99b7a7286030b42c (patch) | |
tree | 2965cca47906a5b32bcd32e1558e4e48c2e455bd /tcpip/rip98d.c | |
parent | 875cc7a37dc6b3fb9d29473d6c70f23160887ee9 (diff) |
rip98d: Replace naive mask2bits and bits2mask implementations.
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'tcpip/rip98d.c')
-rw-r--r-- | tcpip/rip98d.c | 57 |
1 files changed, 7 insertions, 50 deletions
diff --git a/tcpip/rip98d.c b/tcpip/rip98d.c index f98be6f..43d31ba 100644 --- a/tcpip/rip98d.c +++ b/tcpip/rip98d.c @@ -39,45 +39,6 @@ int logging = FALSE; struct route_struct *first_route; -static struct mask_struct { - unsigned long int mask; - unsigned int bits; -} mask_table[] = { - {0xFFFFFFFF, 32}, - {0xFFFFFFFE, 31}, - {0xFFFFFFFC, 30}, - {0xFFFFFFF8, 29}, - {0xFFFFFFF0, 28}, - {0xFFFFFFE0, 27}, - {0xFFFFFFC0, 26}, - {0xFFFFFF80, 25}, - {0xFFFFFF00, 24}, - {0xFFFFFE00, 23}, - {0xFFFFFC00, 22}, - {0xFFFFF800, 21}, - {0xFFFFF000, 20}, - {0xFFFFE000, 19}, - {0xFFFFC000, 18}, - {0xFFFF8000, 17}, - {0xFFFF0000, 16}, - {0xFFFE0000, 15}, - {0xFFFC0000, 14}, - {0xFFF80000, 13}, - {0xFFF00000, 12}, - {0xFFE00000, 11}, - {0xFFC00000, 10}, - {0xFF800000, 9}, - {0xFF000000, 8}, - {0xFE000000, 7}, - {0xFC000000, 6}, - {0xF8000000, 5}, - {0xF0000000, 4}, - {0xE0000000, 3}, - {0xC0000000, 2}, - {0x80000000, 1}, - {0x00000000, 0}, -}; - static void terminate(int sig) { if (logging) { @@ -90,24 +51,20 @@ static void terminate(int sig) unsigned int mask2bits(unsigned int mask) { - struct mask_struct *t; + mask = ~mask; - for (t = mask_table; t->mask != 0; t++) - if (mask == t->mask) - return t->bits; + if (!mask) + return 32; - return 0; + return __builtin_clz(mask); } unsigned int bits2mask(unsigned int bits) { - struct mask_struct *t; - - for (t = mask_table; t->mask != 0; t++) - if (bits == t->bits) - return htonl(t->mask); + if (!bits) + return 0; - return 0; + return ~0 << (sizeof(unsigned int) * 8 - bits); } static unsigned long int hex2intrev(char *buffer) |