xref: /titanic_50/usr/src/uts/common/sys/sysmacros.h (revision b819cea2f73f98c5662230cc9affc8cc84f77fcf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52c32020fSeschrock  * Common Development and Distribution License (the "License").
62c32020fSeschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
2688b7b0f2SMatthew Ahrens  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
281d99877fSAlexey Zaytsev  *
29*b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifndef _SYS_SYSMACROS_H
337c478bd9Sstevel@tonic-gate #define	_SYS_SYSMACROS_H
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Some macros for units conversion
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Disk blocks (sectors) and bytes.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate #define	dtob(DD)	((DD) << DEV_BSHIFT)
487c478bd9Sstevel@tonic-gate #define	btod(BB)	(((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
497c478bd9Sstevel@tonic-gate #define	btodt(BB)	((BB) >> DEV_BSHIFT)
507c478bd9Sstevel@tonic-gate #define	lbtod(BB)	(((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* common macros */
537c478bd9Sstevel@tonic-gate #ifndef MIN
547c478bd9Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate #ifndef MAX
577c478bd9Sstevel@tonic-gate #define	MAX(a, b)	((a) < (b) ? (b) : (a))
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate #ifndef ABS
607c478bd9Sstevel@tonic-gate #define	ABS(a)		((a) < 0 ? -(a) : (a))
617c478bd9Sstevel@tonic-gate #endif
62b127ac41SPhilip Kirk #ifndef	SIGNOF
63b127ac41SPhilip Kirk #define	SIGNOF(a)	((a) < 0 ? -1 : (a) > 0)
64b127ac41SPhilip Kirk #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifdef _KERNEL
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Convert a single byte to/from binary-coded decimal (BCD).
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate extern unsigned char byte_to_bcd[256];
727c478bd9Sstevel@tonic-gate extern unsigned char bcd_to_byte[256];
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	BYTE_TO_BCD(x)	byte_to_bcd[(x) & 0xff]
757c478bd9Sstevel@tonic-gate #define	BCD_TO_BYTE(x)	bcd_to_byte[(x) & 0xff]
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * WARNING: The device number macros defined here should not be used by device
817c478bd9Sstevel@tonic-gate  * drivers or user software. Device drivers should use the device functions
827c478bd9Sstevel@tonic-gate  * defined in the DDI/DKI interface (see also ddi.h). Application software
837c478bd9Sstevel@tonic-gate  * should make use of the library routines available in makedev(3). A set of
847c478bd9Sstevel@tonic-gate  * new device macros are provided to operate on the expanded device number
857c478bd9Sstevel@tonic-gate  * format supported in SVR4. Macro versions of the DDI device functions are
867c478bd9Sstevel@tonic-gate  * provided for use by kernel proper routines only. Macro routines bmajor(),
877c478bd9Sstevel@tonic-gate  * major(), minor(), emajor(), eminor(), and makedev() will be removed or
887c478bd9Sstevel@tonic-gate  * their definitions changed at the next major release following SVR4.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	O_BITSMAJOR	7	/* # of SVR3 major device bits */
927c478bd9Sstevel@tonic-gate #define	O_BITSMINOR	8	/* # of SVR3 minor device bits */
937c478bd9Sstevel@tonic-gate #define	O_MAXMAJ	0x7f	/* SVR3 max major value */
947c478bd9Sstevel@tonic-gate #define	O_MAXMIN	0xff	/* SVR3 max minor value */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR32	14	/* # of SVR4 major device bits */
987c478bd9Sstevel@tonic-gate #define	L_BITSMINOR32	18	/* # of SVR4 minor device bits */
997c478bd9Sstevel@tonic-gate #define	L_MAXMAJ32	0x3fff	/* SVR4 max major value */
1007c478bd9Sstevel@tonic-gate #define	L_MAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
1017c478bd9Sstevel@tonic-gate 				/* For 3b2 hardware devices the minor is */
1027c478bd9Sstevel@tonic-gate 				/* restricted to 256 (0-255) */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #ifdef _LP64
1057c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR	32	/* # of major device bits in 64-bit Solaris */
1067c478bd9Sstevel@tonic-gate #define	L_BITSMINOR	32	/* # of minor device bits in 64-bit Solaris */
1077c478bd9Sstevel@tonic-gate #define	L_MAXMAJ	0xfffffffful	/* max major value */
1087c478bd9Sstevel@tonic-gate #define	L_MAXMIN	0xfffffffful	/* max minor value */
1097c478bd9Sstevel@tonic-gate #else
1107c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR	L_BITSMAJOR32
1117c478bd9Sstevel@tonic-gate #define	L_BITSMINOR	L_BITSMINOR32
1127c478bd9Sstevel@tonic-gate #define	L_MAXMAJ	L_MAXMAJ32
1137c478bd9Sstevel@tonic-gate #define	L_MAXMIN	L_MAXMIN32
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /* major part of a device internal to the kernel */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1217c478bd9Sstevel@tonic-gate #define	bmajor(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* get internal major part of expanded device number */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	getmajor(x)	(major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /* minor part of a device internal to the kernel */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* get internal minor part of expanded device number */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #define	getminor(x)	(minor_t)((x) & L_MAXMIN)
1347c478bd9Sstevel@tonic-gate 
135*b819cea2SGordon Ross #else	/* _KERNEL */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /* major part of a device external from the kernel (same as emajor below) */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /* minor part of a device external from the kernel  (same as eminor below) */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* create old device number */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define	makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* make an new device number */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #define	makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * emajor() allows kernel/driver code to print external major numbers
1587c478bd9Sstevel@tonic-gate  * eminor() allows kernel/driver code to print external minor numbers
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	emajor(x) \
1627c478bd9Sstevel@tonic-gate 	(major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
1637c478bd9Sstevel@tonic-gate 	    NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #define	eminor(x) \
1667c478bd9Sstevel@tonic-gate 	(minor_t)((x) & O_MAXMIN)
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * get external major and minor device
1707c478bd9Sstevel@tonic-gate  * components from expanded device number
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate #define	getemajor(x)	(major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
1737c478bd9Sstevel@tonic-gate 			    NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
1747c478bd9Sstevel@tonic-gate #define	geteminor(x)	(minor_t)((x) & L_MAXMIN)
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * These are versions of the kernel routines for compressing and
1787c478bd9Sstevel@tonic-gate  * expanding long device numbers that don't return errors.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	DEVCMPL(x)	(x)
1837c478bd9Sstevel@tonic-gate #define	DEVEXPL(x)	(x)
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #else
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate #define	DEVCMPL(x)	\
1887c478bd9Sstevel@tonic-gate 	(dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
1897c478bd9Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
1907c478bd9Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #define	DEVEXPL(x)	\
1937c478bd9Sstevel@tonic-gate 	(((x) == NODEV32) ? NODEV : \
1947c478bd9Sstevel@tonic-gate 	makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #endif /* L_BITSMAJOR32 ... */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* convert to old (SVR3.2) dev format */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #define	cmpdev(x) \
2017c478bd9Sstevel@tonic-gate 	(o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
2027c478bd9Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
2037c478bd9Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* convert to new (SVR4) dev format */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #define	expdev(x) \
2087c478bd9Sstevel@tonic-gate 	(dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
2097c478bd9Sstevel@tonic-gate 	    ((x) & O_MAXMIN))
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Macro for checking power of 2 address alignment.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate #define	IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * Macros for counting and rounding.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate #define	howmany(x, y)	(((x)+((y)-1))/(y))
2207c478bd9Sstevel@tonic-gate #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Macro to determine if value is a power of 2
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate #define	ISP2(x)		(((x) & ((x) - 1)) == 0)
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
22888b7b0f2SMatthew Ahrens  * Macros for various sorts of alignment and rounding.  The "align" must
22988b7b0f2SMatthew Ahrens  * be a power of 2.  Often times it is a block, sector, or page.
23088b7b0f2SMatthew Ahrens  */
23188b7b0f2SMatthew Ahrens 
23288b7b0f2SMatthew Ahrens /*
23388b7b0f2SMatthew Ahrens  * return x rounded down to an align boundary
23488b7b0f2SMatthew Ahrens  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
23588b7b0f2SMatthew Ahrens  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
23688b7b0f2SMatthew Ahrens  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
23788b7b0f2SMatthew Ahrens  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate #define	P2ALIGN(x, align)		((x) & -(align))
24088b7b0f2SMatthew Ahrens 
2417c478bd9Sstevel@tonic-gate /*
24288b7b0f2SMatthew Ahrens  * return x % (mod) align
24388b7b0f2SMatthew Ahrens  * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
24488b7b0f2SMatthew Ahrens  * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
24588b7b0f2SMatthew Ahrens  */
24688b7b0f2SMatthew Ahrens #define	P2PHASE(x, align)		((x) & ((align) - 1))
24788b7b0f2SMatthew Ahrens 
24888b7b0f2SMatthew Ahrens /*
24988b7b0f2SMatthew Ahrens  * return how much space is left in this block (but if it's perfectly
25088b7b0f2SMatthew Ahrens  * aligned, return 0).
25188b7b0f2SMatthew Ahrens  * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
25288b7b0f2SMatthew Ahrens  * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
25388b7b0f2SMatthew Ahrens  */
25488b7b0f2SMatthew Ahrens #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
25588b7b0f2SMatthew Ahrens 
25688b7b0f2SMatthew Ahrens /*
25788b7b0f2SMatthew Ahrens  * return x rounded up to an align boundary
25888b7b0f2SMatthew Ahrens  * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
25988b7b0f2SMatthew Ahrens  * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
26088b7b0f2SMatthew Ahrens  */
26188b7b0f2SMatthew Ahrens #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
26288b7b0f2SMatthew Ahrens 
26388b7b0f2SMatthew Ahrens /*
26488b7b0f2SMatthew Ahrens  * return the ending address of the block that x is in
26588b7b0f2SMatthew Ahrens  * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
26688b7b0f2SMatthew Ahrens  * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
26788b7b0f2SMatthew Ahrens  */
26888b7b0f2SMatthew Ahrens #define	P2END(x, align)			(-(~(x) & -(align)))
26988b7b0f2SMatthew Ahrens 
27088b7b0f2SMatthew Ahrens /*
27188b7b0f2SMatthew Ahrens  * return x rounded up to the next phase (offset) within align.
27288b7b0f2SMatthew Ahrens  * phase should be < align.
27388b7b0f2SMatthew Ahrens  * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
27488b7b0f2SMatthew Ahrens  * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
27588b7b0f2SMatthew Ahrens  */
27688b7b0f2SMatthew Ahrens #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
27788b7b0f2SMatthew Ahrens 
27888b7b0f2SMatthew Ahrens /*
27988b7b0f2SMatthew Ahrens  * return TRUE if adding len to off would cause it to cross an align
28088b7b0f2SMatthew Ahrens  * boundary.
28188b7b0f2SMatthew Ahrens  * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
28288b7b0f2SMatthew Ahrens  * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
28388b7b0f2SMatthew Ahrens  */
28488b7b0f2SMatthew Ahrens #define	P2BOUNDARY(off, len, align) \
28588b7b0f2SMatthew Ahrens 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
28688b7b0f2SMatthew Ahrens 
28788b7b0f2SMatthew Ahrens /*
28888b7b0f2SMatthew Ahrens  * Return TRUE if they have the same highest bit set.
28988b7b0f2SMatthew Ahrens  * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
29088b7b0f2SMatthew Ahrens  * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate #define	P2SAMEHIGHBIT(x, y)		(((x) ^ (y)) < ((x) & (y)))
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * Typed version of the P2* macros.  These macros should be used to ensure
2967c478bd9Sstevel@tonic-gate  * that the result is correctly calculated based on the data type of (x),
2977c478bd9Sstevel@tonic-gate  * which is passed in as the last argument, regardless of the data
2987c478bd9Sstevel@tonic-gate  * type of the alignment.  For example, if (x) is of type uint64_t,
2997c478bd9Sstevel@tonic-gate  * and we want to round it up to a page boundary using "PAGESIZE" as
3007c478bd9Sstevel@tonic-gate  * the alignment, we can do either
3017c478bd9Sstevel@tonic-gate  *	P2ROUNDUP(x, (uint64_t)PAGESIZE)
3027c478bd9Sstevel@tonic-gate  * or
3037c478bd9Sstevel@tonic-gate  *	P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate #define	P2ALIGN_TYPED(x, align, type)	\
3067c478bd9Sstevel@tonic-gate 	((type)(x) & -(type)(align))
3077c478bd9Sstevel@tonic-gate #define	P2PHASE_TYPED(x, align, type)	\
3087c478bd9Sstevel@tonic-gate 	((type)(x) & ((type)(align) - 1))
3097c478bd9Sstevel@tonic-gate #define	P2NPHASE_TYPED(x, align, type)	\
3107c478bd9Sstevel@tonic-gate 	(-(type)(x) & ((type)(align) - 1))
3117c478bd9Sstevel@tonic-gate #define	P2ROUNDUP_TYPED(x, align, type)	\
3127c478bd9Sstevel@tonic-gate 	(-(-(type)(x) & -(type)(align)))
3137c478bd9Sstevel@tonic-gate #define	P2END_TYPED(x, align, type)	\
3147c478bd9Sstevel@tonic-gate 	(-(~(type)(x) & -(type)(align)))
3157c478bd9Sstevel@tonic-gate #define	P2PHASEUP_TYPED(x, align, phase, type)	\
3167c478bd9Sstevel@tonic-gate 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
3177c478bd9Sstevel@tonic-gate #define	P2CROSS_TYPED(x, y, align, type)	\
3187c478bd9Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
3197c478bd9Sstevel@tonic-gate #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
3207c478bd9Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /*
3237c478bd9Sstevel@tonic-gate  * Macros to atomically increment/decrement a variable.  mutex and var
3247c478bd9Sstevel@tonic-gate  * must be pointers.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate #define	INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
3277c478bd9Sstevel@tonic-gate #define	DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
3287c478bd9Sstevel@tonic-gate 
3292c32020fSeschrock /*
3302c32020fSeschrock  * Macros to declare bitfields - the order in the parameter list is
3312c32020fSeschrock  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
3322c32020fSeschrock  * because if a field crosses a byte boundary it's not likely to be meaningful
3332c32020fSeschrock  * without reassembly in its nonnative endianness.
3342c32020fSeschrock  */
3352c32020fSeschrock #if defined(_BIT_FIELDS_LTOH)
3362c32020fSeschrock #define	DECL_BITFIELD2(_a, _b)				\
3372c32020fSeschrock 	uint8_t _a, _b
3382c32020fSeschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3392c32020fSeschrock 	uint8_t _a, _b, _c
3402c32020fSeschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3412c32020fSeschrock 	uint8_t _a, _b, _c, _d
3422c32020fSeschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3432c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e
3442c32020fSeschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3452c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f
3462c32020fSeschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3472c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g
3482c32020fSeschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3492c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g, _h
3502c32020fSeschrock #elif defined(_BIT_FIELDS_HTOL)
3512c32020fSeschrock #define	DECL_BITFIELD2(_a, _b)				\
3522c32020fSeschrock 	uint8_t _b, _a
3532c32020fSeschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3542c32020fSeschrock 	uint8_t _c, _b, _a
3552c32020fSeschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3562c32020fSeschrock 	uint8_t _d, _c, _b, _a
3572c32020fSeschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3582c32020fSeschrock 	uint8_t _e, _d, _c, _b, _a
3592c32020fSeschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3602c32020fSeschrock 	uint8_t _f, _e, _d, _c, _b, _a
3612c32020fSeschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3622c32020fSeschrock 	uint8_t _g, _f, _e, _d, _c, _b, _a
3632c32020fSeschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3642c32020fSeschrock 	uint8_t _h, _g, _f, _e, _d, _c, _b, _a
3652c32020fSeschrock #else
3662c32020fSeschrock #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
3672c32020fSeschrock #endif  /* _BIT_FIELDS_LTOH */
3682c32020fSeschrock 
3697c478bd9Sstevel@tonic-gate /* avoid any possibility of clashing with <stddef.h> version */
370*b819cea2SGordon Ross #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_KMEMUSER)
3717c478bd9Sstevel@tonic-gate 
3721d99877fSAlexey Zaytsev #if !defined(offsetof)
3737c478bd9Sstevel@tonic-gate #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
3741d99877fSAlexey Zaytsev #endif /* !offsetof */
3751d99877fSAlexey Zaytsev 
3761d99877fSAlexey Zaytsev #define	container_of(m, s, name)			\
3771d99877fSAlexey Zaytsev 	(void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name))
3781d99877fSAlexey Zaytsev 
3791d99877fSAlexey Zaytsev #define	ARRAY_SIZE(x)	(sizeof (x) / sizeof (x[0]))
3801d99877fSAlexey Zaytsev #endif /* _KERNEL, !_KMEMUSER */
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate #endif
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate #endif	/* _SYS_SYSMACROS_H */
387