xref: /onnv-gate/usr/src/uts/common/sys/sysmacros.h (revision 8023:faf256d5c16c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55621Seschrock  * Common Development and Distribution License (the "License").
65621Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
267837SMatthew.Ahrens@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #ifndef _SYS_SYSMACROS_H
310Sstevel@tonic-gate #define	_SYS_SYSMACROS_H
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef	__cplusplus
360Sstevel@tonic-gate extern "C" {
370Sstevel@tonic-gate #endif
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Some macros for units conversion
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Disk blocks (sectors) and bytes.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #define	dtob(DD)	((DD) << DEV_BSHIFT)
460Sstevel@tonic-gate #define	btod(BB)	(((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
470Sstevel@tonic-gate #define	btodt(BB)	((BB) >> DEV_BSHIFT)
480Sstevel@tonic-gate #define	lbtod(BB)	(((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* common macros */
510Sstevel@tonic-gate #ifndef MIN
520Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate #ifndef MAX
550Sstevel@tonic-gate #define	MAX(a, b)	((a) < (b) ? (b) : (a))
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate #ifndef ABS
580Sstevel@tonic-gate #define	ABS(a)		((a) < 0 ? -(a) : (a))
590Sstevel@tonic-gate #endif
60*8023SPhil.Kirk@Sun.COM #ifndef	SIGNOF
61*8023SPhil.Kirk@Sun.COM #define	SIGNOF(a)	((a) < 0 ? -1 : (a) > 0)
62*8023SPhil.Kirk@Sun.COM #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #ifdef _KERNEL
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Convert a single byte to/from binary-coded decimal (BCD).
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate extern unsigned char byte_to_bcd[256];
700Sstevel@tonic-gate extern unsigned char bcd_to_byte[256];
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	BYTE_TO_BCD(x)	byte_to_bcd[(x) & 0xff]
730Sstevel@tonic-gate #define	BCD_TO_BYTE(x)	bcd_to_byte[(x) & 0xff]
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #endif	/* _KERNEL */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * WARNING: The device number macros defined here should not be used by device
790Sstevel@tonic-gate  * drivers or user software. Device drivers should use the device functions
800Sstevel@tonic-gate  * defined in the DDI/DKI interface (see also ddi.h). Application software
810Sstevel@tonic-gate  * should make use of the library routines available in makedev(3). A set of
820Sstevel@tonic-gate  * new device macros are provided to operate on the expanded device number
830Sstevel@tonic-gate  * format supported in SVR4. Macro versions of the DDI device functions are
840Sstevel@tonic-gate  * provided for use by kernel proper routines only. Macro routines bmajor(),
850Sstevel@tonic-gate  * major(), minor(), emajor(), eminor(), and makedev() will be removed or
860Sstevel@tonic-gate  * their definitions changed at the next major release following SVR4.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	O_BITSMAJOR	7	/* # of SVR3 major device bits */
900Sstevel@tonic-gate #define	O_BITSMINOR	8	/* # of SVR3 minor device bits */
910Sstevel@tonic-gate #define	O_MAXMAJ	0x7f	/* SVR3 max major value */
920Sstevel@tonic-gate #define	O_MAXMIN	0xff	/* SVR3 max minor value */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	L_BITSMAJOR32	14	/* # of SVR4 major device bits */
960Sstevel@tonic-gate #define	L_BITSMINOR32	18	/* # of SVR4 minor device bits */
970Sstevel@tonic-gate #define	L_MAXMAJ32	0x3fff	/* SVR4 max major value */
980Sstevel@tonic-gate #define	L_MAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
990Sstevel@tonic-gate 				/* For 3b2 hardware devices the minor is */
1000Sstevel@tonic-gate 				/* restricted to 256 (0-255) */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #ifdef _LP64
1030Sstevel@tonic-gate #define	L_BITSMAJOR	32	/* # of major device bits in 64-bit Solaris */
1040Sstevel@tonic-gate #define	L_BITSMINOR	32	/* # of minor device bits in 64-bit Solaris */
1050Sstevel@tonic-gate #define	L_MAXMAJ	0xfffffffful	/* max major value */
1060Sstevel@tonic-gate #define	L_MAXMIN	0xfffffffful	/* max minor value */
1070Sstevel@tonic-gate #else
1080Sstevel@tonic-gate #define	L_BITSMAJOR	L_BITSMAJOR32
1090Sstevel@tonic-gate #define	L_BITSMINOR	L_BITSMINOR32
1100Sstevel@tonic-gate #define	L_MAXMAJ	L_MAXMAJ32
1110Sstevel@tonic-gate #define	L_MAXMIN	L_MAXMIN32
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #ifdef _KERNEL
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /* major part of a device internal to the kernel */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1190Sstevel@tonic-gate #define	bmajor(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /* get internal major part of expanded device number */
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate #define	getmajor(x)	(major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /* minor part of a device internal to the kernel */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* get internal minor part of expanded device number */
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	getminor(x)	(minor_t)((x) & L_MAXMIN)
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #else
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /* major part of a device external from the kernel (same as emajor below) */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* minor part of a device external from the kernel  (same as eminor below) */
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #endif	/* _KERNEL */
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /* create old device number */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #define	makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* make an new device number */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #define	makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * emajor() allows kernel/driver code to print external major numbers
1560Sstevel@tonic-gate  * eminor() allows kernel/driver code to print external minor numbers
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	emajor(x) \
1600Sstevel@tonic-gate 	(major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
1610Sstevel@tonic-gate 	    NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate #define	eminor(x) \
1640Sstevel@tonic-gate 	(minor_t)((x) & O_MAXMIN)
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * get external major and minor device
1680Sstevel@tonic-gate  * components from expanded device number
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate #define	getemajor(x)	(major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
1710Sstevel@tonic-gate 			    NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
1720Sstevel@tonic-gate #define	geteminor(x)	(minor_t)((x) & L_MAXMIN)
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * These are versions of the kernel routines for compressing and
1760Sstevel@tonic-gate  * expanding long device numbers that don't return errors.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate #define	DEVCMPL(x)	(x)
1810Sstevel@tonic-gate #define	DEVEXPL(x)	(x)
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate #else
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #define	DEVCMPL(x)	\
1860Sstevel@tonic-gate 	(dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
1870Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
1880Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate #define	DEVEXPL(x)	\
1910Sstevel@tonic-gate 	(((x) == NODEV32) ? NODEV : \
1920Sstevel@tonic-gate 	makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate #endif /* L_BITSMAJOR32 ... */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /* convert to old (SVR3.2) dev format */
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #define	cmpdev(x) \
1990Sstevel@tonic-gate 	(o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
2000Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
2010Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /* convert to new (SVR4) dev format */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #define	expdev(x) \
2060Sstevel@tonic-gate 	(dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
2070Sstevel@tonic-gate 	    ((x) & O_MAXMIN))
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Macro for checking power of 2 address alignment.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate #define	IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Macros for counting and rounding.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate #define	howmany(x, y)	(((x)+((y)-1))/(y))
2180Sstevel@tonic-gate #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Macro to determine if value is a power of 2
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate #define	ISP2(x)		(((x) & ((x) - 1)) == 0)
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2267837SMatthew.Ahrens@Sun.COM  * Macros for various sorts of alignment and rounding.  The "align" must
2277837SMatthew.Ahrens@Sun.COM  * be a power of 2.  Often times it is a block, sector, or page.
2287837SMatthew.Ahrens@Sun.COM  */
2297837SMatthew.Ahrens@Sun.COM 
2307837SMatthew.Ahrens@Sun.COM /*
2317837SMatthew.Ahrens@Sun.COM  * return x rounded down to an align boundary
2327837SMatthew.Ahrens@Sun.COM  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
2337837SMatthew.Ahrens@Sun.COM  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
2347837SMatthew.Ahrens@Sun.COM  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
2357837SMatthew.Ahrens@Sun.COM  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate #define	P2ALIGN(x, align)		((x) & -(align))
2387837SMatthew.Ahrens@Sun.COM 
2397837SMatthew.Ahrens@Sun.COM /*
2407837SMatthew.Ahrens@Sun.COM  * return x % (mod) align
2417837SMatthew.Ahrens@Sun.COM  * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
2427837SMatthew.Ahrens@Sun.COM  * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
2437837SMatthew.Ahrens@Sun.COM  */
2440Sstevel@tonic-gate #define	P2PHASE(x, align)		((x) & ((align) - 1))
2457837SMatthew.Ahrens@Sun.COM 
2467837SMatthew.Ahrens@Sun.COM /*
2477837SMatthew.Ahrens@Sun.COM  * return how much space is left in this block (but if it's perfectly
2487837SMatthew.Ahrens@Sun.COM  * aligned, return 0).
2497837SMatthew.Ahrens@Sun.COM  * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
2507837SMatthew.Ahrens@Sun.COM  * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
2517837SMatthew.Ahrens@Sun.COM  */
2520Sstevel@tonic-gate #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
2537837SMatthew.Ahrens@Sun.COM 
2547837SMatthew.Ahrens@Sun.COM /*
2557837SMatthew.Ahrens@Sun.COM  * return x rounded up to an align boundary
2567837SMatthew.Ahrens@Sun.COM  * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
2577837SMatthew.Ahrens@Sun.COM  * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
2587837SMatthew.Ahrens@Sun.COM  */
2590Sstevel@tonic-gate #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
2607837SMatthew.Ahrens@Sun.COM 
2617837SMatthew.Ahrens@Sun.COM /*
2627837SMatthew.Ahrens@Sun.COM  * return the ending address of the block that x is in
2637837SMatthew.Ahrens@Sun.COM  * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
2647837SMatthew.Ahrens@Sun.COM  * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
2657837SMatthew.Ahrens@Sun.COM  */
2660Sstevel@tonic-gate #define	P2END(x, align)			(-(~(x) & -(align)))
2677837SMatthew.Ahrens@Sun.COM 
2687837SMatthew.Ahrens@Sun.COM /*
2697837SMatthew.Ahrens@Sun.COM  * return x rounded up to the next phase (offset) within align.
2707837SMatthew.Ahrens@Sun.COM  * phase should be < align.
2717837SMatthew.Ahrens@Sun.COM  * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
2727837SMatthew.Ahrens@Sun.COM  * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
2737837SMatthew.Ahrens@Sun.COM  */
2740Sstevel@tonic-gate #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
2757837SMatthew.Ahrens@Sun.COM 
2760Sstevel@tonic-gate /*
2777837SMatthew.Ahrens@Sun.COM  * return TRUE if adding len to off would cause it to cross an align
2787837SMatthew.Ahrens@Sun.COM  * boundary.
2797837SMatthew.Ahrens@Sun.COM  * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
2807837SMatthew.Ahrens@Sun.COM  * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
2817837SMatthew.Ahrens@Sun.COM  */
2827837SMatthew.Ahrens@Sun.COM #define	P2BOUNDARY(off, len, align) \
2837837SMatthew.Ahrens@Sun.COM 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
2847837SMatthew.Ahrens@Sun.COM 
2857837SMatthew.Ahrens@Sun.COM /*
2867837SMatthew.Ahrens@Sun.COM  * Return TRUE if they have the same highest bit set.
2877837SMatthew.Ahrens@Sun.COM  * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
2887837SMatthew.Ahrens@Sun.COM  * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate #define	P2SAMEHIGHBIT(x, y)		(((x) ^ (y)) < ((x) & (y)))
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * Typed version of the P2* macros.  These macros should be used to ensure
2940Sstevel@tonic-gate  * that the result is correctly calculated based on the data type of (x),
2950Sstevel@tonic-gate  * which is passed in as the last argument, regardless of the data
2960Sstevel@tonic-gate  * type of the alignment.  For example, if (x) is of type uint64_t,
2970Sstevel@tonic-gate  * and we want to round it up to a page boundary using "PAGESIZE" as
2980Sstevel@tonic-gate  * the alignment, we can do either
2990Sstevel@tonic-gate  *	P2ROUNDUP(x, (uint64_t)PAGESIZE)
3000Sstevel@tonic-gate  * or
3010Sstevel@tonic-gate  *	P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate #define	P2ALIGN_TYPED(x, align, type)	\
3040Sstevel@tonic-gate 	((type)(x) & -(type)(align))
3050Sstevel@tonic-gate #define	P2PHASE_TYPED(x, align, type)	\
3060Sstevel@tonic-gate 	((type)(x) & ((type)(align) - 1))
3070Sstevel@tonic-gate #define	P2NPHASE_TYPED(x, align, type)	\
3080Sstevel@tonic-gate 	(-(type)(x) & ((type)(align) - 1))
3090Sstevel@tonic-gate #define	P2ROUNDUP_TYPED(x, align, type)	\
3100Sstevel@tonic-gate 	(-(-(type)(x) & -(type)(align)))
3110Sstevel@tonic-gate #define	P2END_TYPED(x, align, type)	\
3120Sstevel@tonic-gate 	(-(~(type)(x) & -(type)(align)))
3130Sstevel@tonic-gate #define	P2PHASEUP_TYPED(x, align, phase, type)	\
3140Sstevel@tonic-gate 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
3150Sstevel@tonic-gate #define	P2CROSS_TYPED(x, y, align, type)	\
3160Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
3170Sstevel@tonic-gate #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
3180Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Macros to atomically increment/decrement a variable.  mutex and var
3220Sstevel@tonic-gate  * must be pointers.
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate #define	INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
3250Sstevel@tonic-gate #define	DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
3260Sstevel@tonic-gate 
3275621Seschrock /*
3285621Seschrock  * Macros to declare bitfields - the order in the parameter list is
3295621Seschrock  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
3305621Seschrock  * because if a field crosses a byte boundary it's not likely to be meaningful
3315621Seschrock  * without reassembly in its nonnative endianness.
3325621Seschrock  */
3335621Seschrock #if defined(_BIT_FIELDS_LTOH)
3345621Seschrock #define	DECL_BITFIELD2(_a, _b)				\
3355621Seschrock 	uint8_t _a, _b
3365621Seschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3375621Seschrock 	uint8_t _a, _b, _c
3385621Seschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3395621Seschrock 	uint8_t _a, _b, _c, _d
3405621Seschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3415621Seschrock 	uint8_t _a, _b, _c, _d, _e
3425621Seschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3435621Seschrock 	uint8_t _a, _b, _c, _d, _e, _f
3445621Seschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3455621Seschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g
3465621Seschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3475621Seschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g, _h
3485621Seschrock #elif defined(_BIT_FIELDS_HTOL)
3495621Seschrock #define	DECL_BITFIELD2(_a, _b)				\
3505621Seschrock 	uint8_t _b, _a
3515621Seschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3525621Seschrock 	uint8_t _c, _b, _a
3535621Seschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3545621Seschrock 	uint8_t _d, _c, _b, _a
3555621Seschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3565621Seschrock 	uint8_t _e, _d, _c, _b, _a
3575621Seschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3585621Seschrock 	uint8_t _f, _e, _d, _c, _b, _a
3595621Seschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3605621Seschrock 	uint8_t _g, _f, _e, _d, _c, _b, _a
3615621Seschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3625621Seschrock 	uint8_t _h, _g, _f, _e, _d, _c, _b, _a
3635621Seschrock #else
3645621Seschrock #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
3655621Seschrock #endif  /* _BIT_FIELDS_LTOH */
3665621Seschrock 
3670Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /* avoid any possibility of clashing with <stddef.h> version */
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
3720Sstevel@tonic-gate #endif
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate #ifdef	__cplusplus
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate #endif
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate #endif	/* _SYS_SYSMACROS_H */
379