xref: /freebsd/contrib/sendmail/include/sm/bitops.h (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
140266059SGregory Neil Shapiro /*
25dd76dd0SGregory Neil Shapiro  * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
340266059SGregory Neil Shapiro  *	All rights reserved.
440266059SGregory Neil Shapiro  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
540266059SGregory Neil Shapiro  * Copyright (c) 1988, 1993
640266059SGregory Neil Shapiro  *	The Regents of the University of California.  All rights reserved.
740266059SGregory Neil Shapiro  *
840266059SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
940266059SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
1040266059SGregory Neil Shapiro  * the sendmail distribution.
1140266059SGregory Neil Shapiro  *
1240266059SGregory Neil Shapiro  *
13*4313cc83SGregory Neil Shapiro  *	$Id: bitops.h,v 1.3 2013-11-22 20:51:31 ca Exp $
1440266059SGregory Neil Shapiro  */
1540266059SGregory Neil Shapiro 
1640266059SGregory Neil Shapiro #ifndef	SM_BITOPS_H
1740266059SGregory Neil Shapiro # define SM_BITOPS_H
1840266059SGregory Neil Shapiro 
1940266059SGregory Neil Shapiro /*
2040266059SGregory Neil Shapiro **  Data structure for bit maps.
2140266059SGregory Neil Shapiro **
2240266059SGregory Neil Shapiro **	Each bit in this map can be referenced by an ascii character.
2340266059SGregory Neil Shapiro **	This is 256 possible bits, or 32 8-bit bytes.
2440266059SGregory Neil Shapiro */
2540266059SGregory Neil Shapiro 
2640266059SGregory Neil Shapiro # define BITMAPBITS	256	/* number of bits in a bit map */
2740266059SGregory Neil Shapiro # define BYTEBITS	8	/* number of bits in a byte */
2840266059SGregory Neil Shapiro # define BITMAPBYTES	(BITMAPBITS / BYTEBITS)	/* number of bytes in bit map */
2940266059SGregory Neil Shapiro # define BITMAPMAX	((BITMAPBYTES / sizeof (int)) - 1)
3040266059SGregory Neil Shapiro 
3140266059SGregory Neil Shapiro /* internal macros */
3240266059SGregory Neil Shapiro 
3340266059SGregory Neil Shapiro /* make sure this index never leaves the allowed range: 0 to BITMAPMAX */
3440266059SGregory Neil Shapiro # define _BITWORD(bit)	(((unsigned char)(bit) / (BYTEBITS * sizeof (int))) & BITMAPMAX)
3540266059SGregory Neil Shapiro # define _BITBIT(bit)	((unsigned int)1 << ((unsigned char)(bit) % (BYTEBITS * sizeof (int))))
3640266059SGregory Neil Shapiro 
3740266059SGregory Neil Shapiro typedef unsigned int	BITMAP256[BITMAPBYTES / sizeof (int)];
3840266059SGregory Neil Shapiro 
3940266059SGregory Neil Shapiro /* properly case and truncate bit */
4040266059SGregory Neil Shapiro # define bitidx(bit)		((unsigned int) (bit) & 0xff)
4140266059SGregory Neil Shapiro 
4240266059SGregory Neil Shapiro /* test bit number N */
4340266059SGregory Neil Shapiro # define bitnset(bit, map)	((map)[_BITWORD(bit)] & _BITBIT(bit))
4440266059SGregory Neil Shapiro 
4540266059SGregory Neil Shapiro /* set bit number N */
4640266059SGregory Neil Shapiro # define setbitn(bit, map)	(map)[_BITWORD(bit)] |= _BITBIT(bit)
4740266059SGregory Neil Shapiro 
4840266059SGregory Neil Shapiro /* clear bit number N */
4940266059SGregory Neil Shapiro # define clrbitn(bit, map)	(map)[_BITWORD(bit)] &= ~_BITBIT(bit)
5040266059SGregory Neil Shapiro 
5140266059SGregory Neil Shapiro /* clear an entire bit map */
5240266059SGregory Neil Shapiro # define clrbitmap(map)		memset((char *) map, '\0', BITMAPBYTES)
5340266059SGregory Neil Shapiro 
5440266059SGregory Neil Shapiro /* bit hacking */
5540266059SGregory Neil Shapiro # define bitset(bit, word)	(((word) & (bit)) != 0)
5640266059SGregory Neil Shapiro 
5740266059SGregory Neil Shapiro #endif /* ! SM_BITOPS_H */
58