xref: /illumos-gate/usr/src/uts/common/sys/sysmacros.h (revision 10ae99ee)
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  *
29b819cea2SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
3074ecdb51SJohn Levon  *
3174ecdb51SJohn Levon  * Copyright 2018 Joyent Inc.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef _SYS_SYSMACROS_H
357c478bd9Sstevel@tonic-gate #define	_SYS_SYSMACROS_H
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
38ff7af0d3SToomas Soome #include <sys/stddef.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
417c478bd9Sstevel@tonic-gate extern "C" {
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Some macros for units conversion
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Disk blocks (sectors) and bytes.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #define	dtob(DD)	((DD) << DEV_BSHIFT)
517c478bd9Sstevel@tonic-gate #define	btod(BB)	(((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
527c478bd9Sstevel@tonic-gate #define	btodt(BB)	((BB) >> DEV_BSHIFT)
537c478bd9Sstevel@tonic-gate #define	lbtod(BB)	(((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* common macros */
567c478bd9Sstevel@tonic-gate #ifndef MIN
577c478bd9Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate #ifndef MAX
607c478bd9Sstevel@tonic-gate #define	MAX(a, b)	((a) < (b) ? (b) : (a))
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate #ifndef ABS
637c478bd9Sstevel@tonic-gate #define	ABS(a)		((a) < 0 ? -(a) : (a))
647c478bd9Sstevel@tonic-gate #endif
65b127ac41SPhilip Kirk #ifndef	SIGNOF
66b127ac41SPhilip Kirk #define	SIGNOF(a)	((a) < 0 ? -1 : (a) > 0)
67b127ac41SPhilip Kirk #endif
687c478bd9Sstevel@tonic-gate 
69*10ae99eeSToomas Soome #ifndef	__DECONST
70*10ae99eeSToomas Soome #define	__DECONST(type, var)	((type)(uintptr_t)(const void *)(var))
71*10ae99eeSToomas Soome #endif
72*10ae99eeSToomas Soome 
737c478bd9Sstevel@tonic-gate #ifdef _KERNEL
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Convert a single byte to/from binary-coded decimal (BCD).
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate extern unsigned char byte_to_bcd[256];
797c478bd9Sstevel@tonic-gate extern unsigned char bcd_to_byte[256];
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	BYTE_TO_BCD(x)	byte_to_bcd[(x) & 0xff]
827c478bd9Sstevel@tonic-gate #define	BCD_TO_BYTE(x)	bcd_to_byte[(x) & 0xff]
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * WARNING: The device number macros defined here should not be used by device
887c478bd9Sstevel@tonic-gate  * drivers or user software. Device drivers should use the device functions
897c478bd9Sstevel@tonic-gate  * defined in the DDI/DKI interface (see also ddi.h). Application software
907c478bd9Sstevel@tonic-gate  * should make use of the library routines available in makedev(3). A set of
917c478bd9Sstevel@tonic-gate  * new device macros are provided to operate on the expanded device number
927c478bd9Sstevel@tonic-gate  * format supported in SVR4. Macro versions of the DDI device functions are
937c478bd9Sstevel@tonic-gate  * provided for use by kernel proper routines only. Macro routines bmajor(),
947c478bd9Sstevel@tonic-gate  * major(), minor(), emajor(), eminor(), and makedev() will be removed or
957c478bd9Sstevel@tonic-gate  * their definitions changed at the next major release following SVR4.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	O_BITSMAJOR	7	/* # of SVR3 major device bits */
997c478bd9Sstevel@tonic-gate #define	O_BITSMINOR	8	/* # of SVR3 minor device bits */
1007c478bd9Sstevel@tonic-gate #define	O_MAXMAJ	0x7f	/* SVR3 max major value */
1017c478bd9Sstevel@tonic-gate #define	O_MAXMIN	0xff	/* SVR3 max minor value */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR32	14	/* # of SVR4 major device bits */
1057c478bd9Sstevel@tonic-gate #define	L_BITSMINOR32	18	/* # of SVR4 minor device bits */
1067c478bd9Sstevel@tonic-gate #define	L_MAXMAJ32	0x3fff	/* SVR4 max major value */
1077c478bd9Sstevel@tonic-gate #define	L_MAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
1087c478bd9Sstevel@tonic-gate 				/* For 3b2 hardware devices the minor is */
1097c478bd9Sstevel@tonic-gate 				/* restricted to 256 (0-255) */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifdef _LP64
1127c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR	32	/* # of major device bits in 64-bit Solaris */
1137c478bd9Sstevel@tonic-gate #define	L_BITSMINOR	32	/* # of minor device bits in 64-bit Solaris */
1147c478bd9Sstevel@tonic-gate #define	L_MAXMAJ	0xfffffffful	/* max major value */
1157c478bd9Sstevel@tonic-gate #define	L_MAXMIN	0xfffffffful	/* max minor value */
1167c478bd9Sstevel@tonic-gate #else
1177c478bd9Sstevel@tonic-gate #define	L_BITSMAJOR	L_BITSMAJOR32
1187c478bd9Sstevel@tonic-gate #define	L_BITSMINOR	L_BITSMINOR32
1197c478bd9Sstevel@tonic-gate #define	L_MAXMAJ	L_MAXMAJ32
1207c478bd9Sstevel@tonic-gate #define	L_MAXMIN	L_MAXMIN32
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* major part of a device internal to the kernel */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1287c478bd9Sstevel@tonic-gate #define	bmajor(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /* get internal major part of expanded device number */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	getmajor(x)	(major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* minor part of a device internal to the kernel */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* get internal minor part of expanded device number */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	getminor(x)	(minor_t)((x) & L_MAXMIN)
1417c478bd9Sstevel@tonic-gate 
142b819cea2SGordon Ross #else	/* _KERNEL */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* major part of a device external from the kernel (same as emajor below) */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* minor part of a device external from the kernel  (same as eminor below) */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	minor(x)	(minor_t)((x) & O_MAXMIN)
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /* create old device number */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* make an new device number */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define	makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * emajor() allows kernel/driver code to print external major numbers
1657c478bd9Sstevel@tonic-gate  * eminor() allows kernel/driver code to print external minor numbers
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #define	emajor(x) \
1697c478bd9Sstevel@tonic-gate 	(major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
1707c478bd9Sstevel@tonic-gate 	    NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	eminor(x) \
1737c478bd9Sstevel@tonic-gate 	(minor_t)((x) & O_MAXMIN)
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * get external major and minor device
1777c478bd9Sstevel@tonic-gate  * components from expanded device number
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate #define	getemajor(x)	(major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
1807c478bd9Sstevel@tonic-gate 			    NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
1817c478bd9Sstevel@tonic-gate #define	geteminor(x)	(minor_t)((x) & L_MAXMIN)
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * These are versions of the kernel routines for compressing and
1857c478bd9Sstevel@tonic-gate  * expanding long device numbers that don't return errors.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #define	DEVCMPL(x)	(x)
1907c478bd9Sstevel@tonic-gate #define	DEVEXPL(x)	(x)
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate #else
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate #define	DEVCMPL(x)	\
1957c478bd9Sstevel@tonic-gate 	(dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
1967c478bd9Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
1977c478bd9Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #define	DEVEXPL(x)	\
2007c478bd9Sstevel@tonic-gate 	(((x) == NODEV32) ? NODEV : \
2017c478bd9Sstevel@tonic-gate 	makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #endif /* L_BITSMAJOR32 ... */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* convert to old (SVR3.2) dev format */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #define	cmpdev(x) \
2087c478bd9Sstevel@tonic-gate 	(o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
2097c478bd9Sstevel@tonic-gate 	    ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
2107c478bd9Sstevel@tonic-gate 	    ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* convert to new (SVR4) dev format */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	expdev(x) \
2157c478bd9Sstevel@tonic-gate 	(dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
2167c478bd9Sstevel@tonic-gate 	    ((x) & O_MAXMIN))
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Macro for checking power of 2 address alignment.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate #define	IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Macros for counting and rounding.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate #define	howmany(x, y)	(((x)+((y)-1))/(y))
2277c478bd9Sstevel@tonic-gate #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Macro to determine if value is a power of 2
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate #define	ISP2(x)		(((x) & ((x) - 1)) == 0)
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
23588b7b0f2SMatthew Ahrens  * Macros for various sorts of alignment and rounding.  The "align" must
23688b7b0f2SMatthew Ahrens  * be a power of 2.  Often times it is a block, sector, or page.
23788b7b0f2SMatthew Ahrens  */
23888b7b0f2SMatthew Ahrens 
23988b7b0f2SMatthew Ahrens /*
24088b7b0f2SMatthew Ahrens  * return x rounded down to an align boundary
24188b7b0f2SMatthew Ahrens  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
24288b7b0f2SMatthew Ahrens  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
24388b7b0f2SMatthew Ahrens  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
24488b7b0f2SMatthew Ahrens  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate #define	P2ALIGN(x, align)		((x) & -(align))
24788b7b0f2SMatthew Ahrens 
2487c478bd9Sstevel@tonic-gate /*
24988b7b0f2SMatthew Ahrens  * return x % (mod) align
25088b7b0f2SMatthew Ahrens  * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
25188b7b0f2SMatthew Ahrens  * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
25288b7b0f2SMatthew Ahrens  */
25388b7b0f2SMatthew Ahrens #define	P2PHASE(x, align)		((x) & ((align) - 1))
25488b7b0f2SMatthew Ahrens 
25588b7b0f2SMatthew Ahrens /*
25688b7b0f2SMatthew Ahrens  * return how much space is left in this block (but if it's perfectly
25788b7b0f2SMatthew Ahrens  * aligned, return 0).
25888b7b0f2SMatthew Ahrens  * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
25988b7b0f2SMatthew Ahrens  * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
26088b7b0f2SMatthew Ahrens  */
26188b7b0f2SMatthew Ahrens #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
26288b7b0f2SMatthew Ahrens 
26388b7b0f2SMatthew Ahrens /*
26488b7b0f2SMatthew Ahrens  * return x rounded up to an align boundary
26588b7b0f2SMatthew Ahrens  * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
26688b7b0f2SMatthew Ahrens  * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
26788b7b0f2SMatthew Ahrens  */
26888b7b0f2SMatthew Ahrens #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
26988b7b0f2SMatthew Ahrens 
27088b7b0f2SMatthew Ahrens /*
27188b7b0f2SMatthew Ahrens  * return the ending address of the block that x is in
27288b7b0f2SMatthew Ahrens  * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
27388b7b0f2SMatthew Ahrens  * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
27488b7b0f2SMatthew Ahrens  */
27588b7b0f2SMatthew Ahrens #define	P2END(x, align)			(-(~(x) & -(align)))
27688b7b0f2SMatthew Ahrens 
27788b7b0f2SMatthew Ahrens /*
27888b7b0f2SMatthew Ahrens  * return x rounded up to the next phase (offset) within align.
27988b7b0f2SMatthew Ahrens  * phase should be < align.
28088b7b0f2SMatthew Ahrens  * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
28188b7b0f2SMatthew Ahrens  * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
28288b7b0f2SMatthew Ahrens  */
28388b7b0f2SMatthew Ahrens #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
28488b7b0f2SMatthew Ahrens 
28588b7b0f2SMatthew Ahrens /*
28688b7b0f2SMatthew Ahrens  * return TRUE if adding len to off would cause it to cross an align
28788b7b0f2SMatthew Ahrens  * boundary.
28888b7b0f2SMatthew Ahrens  * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
28988b7b0f2SMatthew Ahrens  * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
29088b7b0f2SMatthew Ahrens  */
29188b7b0f2SMatthew Ahrens #define	P2BOUNDARY(off, len, align) \
29288b7b0f2SMatthew Ahrens 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
29388b7b0f2SMatthew Ahrens 
29488b7b0f2SMatthew Ahrens /*
29588b7b0f2SMatthew Ahrens  * Return TRUE if they have the same highest bit set.
29688b7b0f2SMatthew Ahrens  * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
29788b7b0f2SMatthew Ahrens  * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate #define	P2SAMEHIGHBIT(x, y)		(((x) ^ (y)) < ((x) & (y)))
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate  * Typed version of the P2* macros.  These macros should be used to ensure
3037c478bd9Sstevel@tonic-gate  * that the result is correctly calculated based on the data type of (x),
3047c478bd9Sstevel@tonic-gate  * which is passed in as the last argument, regardless of the data
3057c478bd9Sstevel@tonic-gate  * type of the alignment.  For example, if (x) is of type uint64_t,
3067c478bd9Sstevel@tonic-gate  * and we want to round it up to a page boundary using "PAGESIZE" as
3077c478bd9Sstevel@tonic-gate  * the alignment, we can do either
3087c478bd9Sstevel@tonic-gate  *	P2ROUNDUP(x, (uint64_t)PAGESIZE)
3097c478bd9Sstevel@tonic-gate  * or
3107c478bd9Sstevel@tonic-gate  *	P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate #define	P2ALIGN_TYPED(x, align, type)	\
3137c478bd9Sstevel@tonic-gate 	((type)(x) & -(type)(align))
3147c478bd9Sstevel@tonic-gate #define	P2PHASE_TYPED(x, align, type)	\
3157c478bd9Sstevel@tonic-gate 	((type)(x) & ((type)(align) - 1))
3167c478bd9Sstevel@tonic-gate #define	P2NPHASE_TYPED(x, align, type)	\
3177c478bd9Sstevel@tonic-gate 	(-(type)(x) & ((type)(align) - 1))
3187c478bd9Sstevel@tonic-gate #define	P2ROUNDUP_TYPED(x, align, type)	\
3197c478bd9Sstevel@tonic-gate 	(-(-(type)(x) & -(type)(align)))
3207c478bd9Sstevel@tonic-gate #define	P2END_TYPED(x, align, type)	\
3217c478bd9Sstevel@tonic-gate 	(-(~(type)(x) & -(type)(align)))
3227c478bd9Sstevel@tonic-gate #define	P2PHASEUP_TYPED(x, align, phase, type)	\
3237c478bd9Sstevel@tonic-gate 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
3247c478bd9Sstevel@tonic-gate #define	P2CROSS_TYPED(x, y, align, type)	\
3257c478bd9Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
3267c478bd9Sstevel@tonic-gate #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
3277c478bd9Sstevel@tonic-gate 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * Macros to atomically increment/decrement a variable.  mutex and var
3317c478bd9Sstevel@tonic-gate  * must be pointers.
3327c478bd9Sstevel@tonic-gate  */
3337c478bd9Sstevel@tonic-gate #define	INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
3347c478bd9Sstevel@tonic-gate #define	DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
3357c478bd9Sstevel@tonic-gate 
3362c32020fSeschrock /*
3372c32020fSeschrock  * Macros to declare bitfields - the order in the parameter list is
3382c32020fSeschrock  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
3392c32020fSeschrock  * because if a field crosses a byte boundary it's not likely to be meaningful
3402c32020fSeschrock  * without reassembly in its nonnative endianness.
3412c32020fSeschrock  */
3422c32020fSeschrock #if defined(_BIT_FIELDS_LTOH)
3432c32020fSeschrock #define	DECL_BITFIELD2(_a, _b)				\
3442c32020fSeschrock 	uint8_t _a, _b
3452c32020fSeschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3462c32020fSeschrock 	uint8_t _a, _b, _c
3472c32020fSeschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3482c32020fSeschrock 	uint8_t _a, _b, _c, _d
3492c32020fSeschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3502c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e
3512c32020fSeschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3522c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f
3532c32020fSeschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3542c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g
3552c32020fSeschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3562c32020fSeschrock 	uint8_t _a, _b, _c, _d, _e, _f, _g, _h
3572c32020fSeschrock #elif defined(_BIT_FIELDS_HTOL)
3582c32020fSeschrock #define	DECL_BITFIELD2(_a, _b)				\
3592c32020fSeschrock 	uint8_t _b, _a
3602c32020fSeschrock #define	DECL_BITFIELD3(_a, _b, _c)			\
3612c32020fSeschrock 	uint8_t _c, _b, _a
3622c32020fSeschrock #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
3632c32020fSeschrock 	uint8_t _d, _c, _b, _a
3642c32020fSeschrock #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
3652c32020fSeschrock 	uint8_t _e, _d, _c, _b, _a
3662c32020fSeschrock #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
3672c32020fSeschrock 	uint8_t _f, _e, _d, _c, _b, _a
3682c32020fSeschrock #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
3692c32020fSeschrock 	uint8_t _g, _f, _e, _d, _c, _b, _a
3702c32020fSeschrock #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
3712c32020fSeschrock 	uint8_t _h, _g, _f, _e, _d, _c, _b, _a
3722c32020fSeschrock #else
3732c32020fSeschrock #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
3742c32020fSeschrock #endif  /* _BIT_FIELDS_LTOH */
3752c32020fSeschrock 
37674ecdb51SJohn Levon #if !defined(ARRAY_SIZE)
3771d99877fSAlexey Zaytsev #define	ARRAY_SIZE(x)	(sizeof (x) / sizeof (x[0]))
37874ecdb51SJohn Levon #endif
3797c478bd9Sstevel@tonic-gate 
3802918c4a3SJohn Levon /*
3812918c4a3SJohn Levon  * Add a value to a uint64_t that saturates at UINT64_MAX instead of wrapping
3822918c4a3SJohn Levon  * around.
3832918c4a3SJohn Levon  */
3842918c4a3SJohn Levon #define	UINT64_OVERFLOW_ADD(val, add) \
3852918c4a3SJohn Levon 	((val) > ((val) + (add)) ? (UINT64_MAX) : ((val) + (add)))
3862918c4a3SJohn Levon 
3872918c4a3SJohn Levon /*
3882918c4a3SJohn Levon  * Convert to an int64, saturating at INT64_MAX.
3892918c4a3SJohn Levon  */
3902918c4a3SJohn Levon #define	UINT64_OVERFLOW_TO_INT64(uval) \
3912918c4a3SJohn Levon 	(((uval) > INT64_MAX) ? INT64_MAX : (int64_t)(uval))
3922918c4a3SJohn Levon 
3937c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate #endif
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate #endif	/* _SYS_SYSMACROS_H */
398