xref: /illumos-gate/usr/src/cmd/sendmail/include/sm/bitops.h (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
1 /*
2  * Copyright (c) 1998-2001 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  *
13  *	$Id: bitops.h,v 1.2 2001/09/22 22:05:42 ca Exp $
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #ifndef	SM_BITOPS_H
19 # define SM_BITOPS_H
20 
21 /*
22 **  Data structure for bit maps.
23 **
24 **	Each bit in this map can be referenced by an ascii character.
25 **	This is 256 possible bits, or 32 8-bit bytes.
26 */
27 
28 # define BITMAPBITS	256	/* number of bits in a bit map */
29 # define BYTEBITS	8	/* number of bits in a byte */
30 # define BITMAPBYTES	(BITMAPBITS / BYTEBITS)	/* number of bytes in bit map */
31 # define BITMAPMAX	((BITMAPBYTES / sizeof (int)) - 1)
32 
33 /* internal macros */
34 
35 /* make sure this index never leaves the allowed range: 0 to BITMAPMAX */
36 # define _BITWORD(bit)	(((unsigned char)(bit) / (BYTEBITS * sizeof (int))) & BITMAPMAX)
37 # define _BITBIT(bit)	((unsigned int)1 << ((unsigned char)(bit) % (BYTEBITS * sizeof (int))))
38 
39 typedef unsigned int	BITMAP256[BITMAPBYTES / sizeof (int)];
40 
41 /* properly case and truncate bit */
42 # define bitidx(bit)		((unsigned int) (bit) & 0xff)
43 
44 /* test bit number N */
45 # define bitnset(bit, map)	((map)[_BITWORD(bit)] & _BITBIT(bit))
46 
47 /* set bit number N */
48 # define setbitn(bit, map)	(map)[_BITWORD(bit)] |= _BITBIT(bit)
49 
50 /* clear bit number N */
51 # define clrbitn(bit, map)	(map)[_BITWORD(bit)] &= ~_BITBIT(bit)
52 
53 /* clear an entire bit map */
54 # define clrbitmap(map)		memset((char *) map, '\0', BITMAPBYTES)
55 
56 /* bit hacking */
57 # define bitset(bit, word)	(((word) & (bit)) != 0)
58 
59 #endif /* ! SM_BITOPS_H */
60