xref: /netbsd-src/external/bsd/ipf/dist/lib/count4bits.c (revision 13885a665959c62f13a82b3caedf986eaa17aa31)
1*13885a66Sdarrenr /*	$NetBSD: count4bits.c,v 1.2 2012/07/22 14:27:36 darrenr Exp $	*/
2bc4097aaSchristos 
3bc4097aaSchristos /*
4c9d5dc6cSdarrenr  * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos  *
6bc4097aaSchristos  * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos  *
8*13885a66Sdarrenr  * Id: count4bits.c,v 1.1.1.2 2012/07/22 13:44:38 darrenr Exp $
9bc4097aaSchristos  */
10bc4097aaSchristos 
11bc4097aaSchristos #include "ipf.h"
12bc4097aaSchristos 
13bc4097aaSchristos 
14bc4097aaSchristos /*
15bc4097aaSchristos  * count consecutive 1's in bit mask.  If the mask generated by counting
16bc4097aaSchristos  * consecutive 1's is different to that passed, return -1, else return #
17bc4097aaSchristos  * of bits.
18bc4097aaSchristos  */
count4bits(ip)19bc4097aaSchristos int	count4bits(ip)
20bc4097aaSchristos 	u_int	ip;
21bc4097aaSchristos {
22bc4097aaSchristos 	int cnt = 0, i, j;
23bc4097aaSchristos 	u_int ipn;
24bc4097aaSchristos 
25bc4097aaSchristos 	ip = ipn = ntohl(ip);
26bc4097aaSchristos 	for (i = 32; i; i--, ipn *= 2)
27bc4097aaSchristos 		if (ipn & 0x80000000)
28bc4097aaSchristos 			cnt++;
29bc4097aaSchristos 		else
30bc4097aaSchristos 			break;
31bc4097aaSchristos 	ipn = 0;
32bc4097aaSchristos 	for (i = 32, j = cnt; i; i--, j--) {
33bc4097aaSchristos 		ipn *= 2;
34bc4097aaSchristos 		if (j > 0)
35bc4097aaSchristos 			ipn++;
36bc4097aaSchristos 	}
37bc4097aaSchristos 	if (ipn == ip)
38bc4097aaSchristos 		return cnt;
39bc4097aaSchristos 	return -1;
40bc4097aaSchristos }
41