190792Sgshapiro/*
2261194Sgshapiro * Copyright (c) 1998-2001 Proofpoint, Inc. and its suppliers.
390792Sgshapiro *	All rights reserved.
490792Sgshapiro * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
590792Sgshapiro * Copyright (c) 1988, 1993
690792Sgshapiro *	The Regents of the University of California.  All rights reserved.
790792Sgshapiro *
890792Sgshapiro * By using this file, you agree to the terms and conditions set
990792Sgshapiro * forth in the LICENSE file which can be found at the top level of
1090792Sgshapiro * the sendmail distribution.
1190792Sgshapiro *
1290792Sgshapiro *
13266527Sgshapiro *	$Id: bitops.h,v 1.3 2013-11-22 20:51:31 ca Exp $
1490792Sgshapiro */
1590792Sgshapiro
1690792Sgshapiro#ifndef	SM_BITOPS_H
1790792Sgshapiro# define SM_BITOPS_H
1890792Sgshapiro
1990792Sgshapiro/*
2090792Sgshapiro**  Data structure for bit maps.
2190792Sgshapiro**
2290792Sgshapiro**	Each bit in this map can be referenced by an ascii character.
2390792Sgshapiro**	This is 256 possible bits, or 32 8-bit bytes.
2490792Sgshapiro*/
2590792Sgshapiro
2690792Sgshapiro# define BITMAPBITS	256	/* number of bits in a bit map */
2790792Sgshapiro# define BYTEBITS	8	/* number of bits in a byte */
2890792Sgshapiro# define BITMAPBYTES	(BITMAPBITS / BYTEBITS)	/* number of bytes in bit map */
2990792Sgshapiro# define BITMAPMAX	((BITMAPBYTES / sizeof (int)) - 1)
3090792Sgshapiro
3190792Sgshapiro/* internal macros */
3290792Sgshapiro
3390792Sgshapiro/* make sure this index never leaves the allowed range: 0 to BITMAPMAX */
3490792Sgshapiro# define _BITWORD(bit)	(((unsigned char)(bit) / (BYTEBITS * sizeof (int))) & BITMAPMAX)
3590792Sgshapiro# define _BITBIT(bit)	((unsigned int)1 << ((unsigned char)(bit) % (BYTEBITS * sizeof (int))))
3690792Sgshapiro
3790792Sgshapirotypedef unsigned int	BITMAP256[BITMAPBYTES / sizeof (int)];
3890792Sgshapiro
3990792Sgshapiro/* properly case and truncate bit */
4090792Sgshapiro# define bitidx(bit)		((unsigned int) (bit) & 0xff)
4190792Sgshapiro
4290792Sgshapiro/* test bit number N */
4390792Sgshapiro# define bitnset(bit, map)	((map)[_BITWORD(bit)] & _BITBIT(bit))
4490792Sgshapiro
4590792Sgshapiro/* set bit number N */
4690792Sgshapiro# define setbitn(bit, map)	(map)[_BITWORD(bit)] |= _BITBIT(bit)
4790792Sgshapiro
4890792Sgshapiro/* clear bit number N */
4990792Sgshapiro# define clrbitn(bit, map)	(map)[_BITWORD(bit)] &= ~_BITBIT(bit)
5090792Sgshapiro
5190792Sgshapiro/* clear an entire bit map */
5290792Sgshapiro# define clrbitmap(map)		memset((char *) map, '\0', BITMAPBYTES)
5390792Sgshapiro
5490792Sgshapiro/* bit hacking */
5590792Sgshapiro# define bitset(bit, word)	(((word) & (bit)) != 0)
5690792Sgshapiro
5790792Sgshapiro#endif /* ! SM_BITOPS_H */
58