4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #ifndef _OPENSOLARIS_SYS_SYSMACROS_H_
32 #define _OPENSOLARIS_SYS_SYSMACROS_H_
34 #include <sys/param.h>
41 * Macro for checking power of 2 address alignment.
43 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
46 * Macro to determine if value is a power of 2
48 #define ISP2(x) (((x) & ((x) - 1)) == 0)
51 * Macros for various sorts of alignment and rounding when the alignment
52 * is known to be a power of 2.
54 #define P2ALIGN(x, align) ((x) & -(align))
55 #define P2PHASE(x, align) ((x) & ((align) - 1))
56 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
57 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
58 #define P2END(x, align) (-(~(x) & -(align)))
59 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
60 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
62 * Determine whether two numbers have the same high-order bit.
64 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
67 * Typed version of the P2* macros. These macros should be used to ensure
68 * that the result is correctly calculated based on the data type of (x),
69 * which is passed in as the last argument, regardless of the data
70 * type of the alignment. For example, if (x) is of type uint64_t,
71 * and we want to round it up to a page boundary using "PAGESIZE" as
72 * the alignment, we can do either
73 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
75 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
77 #define P2ALIGN_TYPED(x, align, type) \
78 ((type)(x) & -(type)(align))
79 #define P2PHASE_TYPED(x, align, type) \
80 ((type)(x) & ((type)(align) - 1))
81 #define P2NPHASE_TYPED(x, align, type) \
82 (-(type)(x) & ((type)(align) - 1))
83 #define P2ROUNDUP_TYPED(x, align, type) \
84 (-(-(type)(x) & -(type)(align)))
85 #define P2END_TYPED(x, align, type) \
86 (-(~(type)(x) & -(type)(align)))
87 #define P2PHASEUP_TYPED(x, align, phase, type) \
88 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
89 #define P2CROSS_TYPED(x, y, align, type) \
90 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
91 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
92 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
95 #define memmove(dst, src, size) bcopy((src), (dst), (size))
99 * Find highest one bit set.
100 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
101 * High order bit is 31 (or 63 in _LP64 kernel).
111 if (i & 0xffffffff00000000ul) {
115 if (i & 0xffff0000) {
137 #endif /* _OPENSOLARIS_SYS_SYSMACROS_H_ */