summaryrefslogtreecommitdiff
path: root/lib/bitops.c
diff options
context:
space:
mode:
authorMartin Mares <mj@ucw.cz>1998-05-03 16:43:39 +0000
committerMartin Mares <mj@ucw.cz>1998-05-03 16:43:39 +0000
commit18c8241a91bd9208879666f1a1a13f454e66d75b (patch)
tree8bdc5f3e384b6b9198d5ab834d869c8d1fb261a7 /lib/bitops.c
parenta8b6038225d18155883e330c96b2bc2e44153e1e (diff)
BIRD library: The story continues.
Complete resource manages and IP address handling.
Diffstat (limited to 'lib/bitops.c')
-rw-r--r--lib/bitops.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/bitops.c b/lib/bitops.c
new file mode 100644
index 00000000..10bca047
--- /dev/null
+++ b/lib/bitops.c
@@ -0,0 +1,32 @@
+/*
+ * BIRD Library -- Generic Bit Operations
+ *
+ * (c) 1998 Martin Mares <mj@ucw.cz>
+ *
+ * Can be freely distributed and used under the terms of the GNU GPL.
+ */
+
+#include "nest/bird.h"
+#include "bitops.h"
+
+u32
+u32_mkmask(unsigned n)
+{
+ return n ? ~((1 << (32 - n)) - 1) : 0;
+}
+
+int
+u32_masklen(u32 x)
+{
+ int l = 0;
+ u32 n = ~x;
+
+ if (n & (n+1)) return -1;
+ if (x & 0x0000ffff) { x &= 0x0000ffff; l += 16; }
+ if (x & 0x00ff00ff) { x &= 0x00ff00ff; l += 8; }
+ if (x & 0x0f0f0f0f) { x &= 0x0f0f0f0f; l += 4; }
+ if (x & 0x33333333) { x &= 0x33333333; l += 2; }
+ if (x & 0x55555555) l++;
+ if (x & 0xaaaaaaaa) l++;
+ return l;
+}