]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/cddl/compat/opensolaris/sys/sysmacros.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / cddl / compat / opensolaris / sys / sysmacros.h
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  *
22  * $FreeBSD$
23  */
24 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
25 /*        All Rights Reserved   */
26
27
28 /*
29  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32
33 #ifndef _OPENSOLARIS_SYS_SYSMACROS_H_
34 #define _OPENSOLARIS_SYS_SYSMACROS_H_
35
36 #include <sys/param.h>
37
38 #ifdef  __cplusplus
39 extern "C" {
40 #endif
41
42 #ifndef ABS
43 #define ABS(a)  ((a) < 0 ? -(a) : (a))
44 #endif
45
46 #ifndef SIGNOF
47 #define SIGNOF(a)       ((a) < 0 ? -1 : (a) > 0)
48 #endif
49
50 /*
51  * Macro for checking power of 2 address alignment.
52  */
53 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
54
55 /*
56  * Macro to determine if value is a power of 2
57  */
58 #define ISP2(x)         (((x) & ((x) - 1)) == 0)
59
60 /*
61  * Macros for various sorts of alignment and rounding when the alignment
62  * is known to be a power of 2.
63  */
64 #define P2ALIGN(x, align)               ((x) & -(align))
65 #define P2PHASE(x, align)               ((x) & ((align) - 1))
66 #define P2NPHASE(x, align)              (-(x) & ((align) - 1))
67 #define P2ROUNDUP(x, align)             (-(-(x) & -(align)))
68 #define P2END(x, align)                 (-(~(x) & -(align)))
69 #define P2PHASEUP(x, align, phase)      ((phase) - (((phase) - (x)) & -(align)))
70 #define P2BOUNDARY(off, len, align)     (((off) ^ ((off) + (len) - 1)) > (align) - 1)
71 /*
72  * Determine whether two numbers have the same high-order bit.
73  */
74 #define P2SAMEHIGHBIT(x, y)             (((x) ^ (y)) < ((x) & (y)))
75
76 /*
77  * Typed version of the P2* macros.  These macros should be used to ensure
78  * that the result is correctly calculated based on the data type of (x),
79  * which is passed in as the last argument, regardless of the data
80  * type of the alignment.  For example, if (x) is of type uint64_t,
81  * and we want to round it up to a page boundary using "PAGESIZE" as
82  * the alignment, we can do either
83  *      P2ROUNDUP(x, (uint64_t)PAGESIZE)
84  * or
85  *      P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
86  */
87 #define P2ALIGN_TYPED(x, align, type)   \
88         ((type)(x) & -(type)(align))
89 #define P2PHASE_TYPED(x, align, type)   \
90         ((type)(x) & ((type)(align) - 1))
91 #define P2NPHASE_TYPED(x, align, type)  \
92         (-(type)(x) & ((type)(align) - 1))
93 #define P2ROUNDUP_TYPED(x, align, type) \
94         (-(-(type)(x) & -(type)(align)))
95 #define P2END_TYPED(x, align, type)     \
96         (-(~(type)(x) & -(type)(align)))
97 #define P2PHASEUP_TYPED(x, align, phase, type)  \
98         ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
99 #define P2CROSS_TYPED(x, y, align, type)        \
100         (((type)(x) ^ (type)(y)) > (type)(align) - 1)
101 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
102         (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
103
104 /*
105  * Find highest one bit set.
106  *      Returns bit number + 1 of highest bit that is set, otherwise returns 0.
107  * High order bit is 31 (or 63 in _LP64 kernel).
108  */
109 static __inline int
110 highbit(ulong_t i)
111 {
112         register int h = 1;
113
114         if (i == 0)
115                 return (0);
116 #ifdef _LP64
117         if (i & 0xffffffff00000000ul) {
118                 h += 32; i >>= 32;
119         }
120 #endif
121         if (i & 0xffff0000) {
122                 h += 16; i >>= 16;
123         }
124         if (i & 0xff00) {
125                 h += 8; i >>= 8;
126         }
127         if (i & 0xf0) {
128                 h += 4; i >>= 4;
129         }
130         if (i & 0xc) {
131                 h += 2; i >>= 2;
132         }
133         if (i & 0x2) {
134                 h += 1;
135         }
136         return (h);
137 }
138
139 #ifdef  __cplusplus
140 }
141 #endif
142
143 #endif  /* _OPENSOLARIS_SYS_SYSMACROS_H_ */