]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/libkern.h
Update DTS files from Linux 4.12
[FreeBSD/FreeBSD.git] / sys / sys / libkern.h
1 /*-
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)libkern.h   8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_LIBKERN_H_
34 #define _SYS_LIBKERN_H_
35
36 #include <sys/cdefs.h>
37 #include <sys/types.h>
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #endif
41
42 #ifndef LIBKERN_INLINE
43 #define LIBKERN_INLINE  static __inline
44 #define LIBKERN_BODY
45 #endif
46
47 /* BCD conversions. */
48 extern u_char const     bcd2bin_data[];
49 extern u_char const     bin2bcd_data[];
50 extern char const       hex2ascii_data[];
51
52 #define LIBKERN_LEN_BCD2BIN     154
53 #define LIBKERN_LEN_BIN2BCD     100
54 #define LIBKERN_LEN_HEX2ASCII   36
55
56 static inline u_char
57 bcd2bin(int bcd)
58 {
59
60         KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
61             ("invalid bcd %d", bcd));
62         return (bcd2bin_data[bcd]);
63 }
64
65 static inline u_char
66 bin2bcd(int bin)
67 {
68
69         KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
70             ("invalid bin %d", bin));
71         return (bin2bcd_data[bin]);
72 }
73
74 static inline char
75 hex2ascii(int hex)
76 {
77
78         KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
79             ("invalid hex %d", hex));
80         return (hex2ascii_data[hex]);
81 }
82
83 static __inline int imax(int a, int b) { return (a > b ? a : b); }
84 static __inline int imin(int a, int b) { return (a < b ? a : b); }
85 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
86 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
87 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
88 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
89 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
90 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
91 static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
92 static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
93 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
94 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
95 static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
96 {
97
98         return (a > b ? a : b);
99 }
100 static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
101 {
102
103         return (a < b ? a : b);
104 }
105 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
106 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
107
108 static __inline int abs(int a) { return (a < 0 ? -a : a); }
109 static __inline long labs(long a) { return (a < 0 ? -a : a); }
110 static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
111
112 #define ARC4_ENTR_NONE  0       /* Don't have entropy yet. */
113 #define ARC4_ENTR_HAVE  1       /* Have entropy. */
114 #define ARC4_ENTR_SEED  2       /* Reseeding. */
115 extern int arc4rand_iniseed_state;
116
117 /* Prototypes for non-quad routines. */
118 struct malloc_type;
119 uint32_t arc4random(void);
120 void     arc4random_buf(void *, size_t);
121 void     arc4rand(void *, u_int, int);
122 int      bcmp(const void *, const void *, size_t);
123 int      timingsafe_bcmp(const void *, const void *, size_t);
124 void    *bsearch(const void *, const void *, size_t,
125             size_t, int (*)(const void *, const void *));
126 #ifndef HAVE_INLINE_FFS
127 int      ffs(int);
128 #endif
129 #ifndef HAVE_INLINE_FFSL
130 int      ffsl(long);
131 #endif
132 #ifndef HAVE_INLINE_FFSLL
133 int      ffsll(long long);
134 #endif
135 #ifndef HAVE_INLINE_FLS
136 int      fls(int);
137 #endif
138 #ifndef HAVE_INLINE_FLSL
139 int      flsl(long);
140 #endif
141 #ifndef HAVE_INLINE_FLSLL
142 int      flsll(long long);
143 #endif
144 #define bitcount64(x)   __bitcount64((uint64_t)(x))
145 #define bitcount32(x)   __bitcount32((uint32_t)(x))
146 #define bitcount16(x)   __bitcount16((uint16_t)(x))
147 #define bitcountl(x)    __bitcountl((u_long)(x))
148 #define bitcount(x)     __bitcount((u_int)(x))
149
150 int      fnmatch(const char *, const char *, int);
151 int      locc(int, char *, u_int);
152 void    *memchr(const void *s, int c, size_t n);
153 void    *memcchr(const void *s, int c, size_t n);
154 int      memcmp(const void *b1, const void *b2, size_t len);
155 void    *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
156 void     qsort(void *base, size_t nmemb, size_t size,
157             int (*compar)(const void *, const void *));
158 void     qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
159             int (*compar)(void *, const void *, const void *));
160 u_long   random(void);
161 int      scanc(u_int, const u_char *, const u_char *, int);
162 void     srandom(u_long);
163 int      strcasecmp(const char *, const char *);
164 char    *strcat(char * __restrict, const char * __restrict);
165 char    *strchr(const char *, int);
166 int      strcmp(const char *, const char *);
167 char    *strcpy(char * __restrict, const char * __restrict);
168 size_t   strcspn(const char * __restrict, const char * __restrict) __pure;
169 char    *strdup(const char *__restrict, struct malloc_type *);
170 char    *strncat(char *, const char *, size_t);
171 char    *strndup(const char *__restrict, size_t, struct malloc_type *);
172 size_t   strlcat(char *, const char *, size_t);
173 size_t   strlcpy(char *, const char *, size_t);
174 size_t   strlen(const char *);
175 int      strncasecmp(const char *, const char *, size_t);
176 int      strncmp(const char *, const char *, size_t);
177 char    *strncpy(char * __restrict, const char * __restrict, size_t);
178 size_t   strnlen(const char *, size_t);
179 char    *strrchr(const char *, int);
180 char    *strsep(char **, const char *delim);
181 size_t   strspn(const char *, const char *);
182 char    *strstr(const char *, const char *);
183 int      strvalid(const char *, size_t);
184
185 extern const uint32_t crc32_tab[];
186
187 static __inline uint32_t
188 crc32_raw(const void *buf, size_t size, uint32_t crc)
189 {
190         const uint8_t *p = (const uint8_t *)buf;
191
192         while (size--)
193                 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
194         return (crc);
195 }
196
197 static __inline uint32_t
198 crc32(const void *buf, size_t size)
199 {
200         uint32_t crc;
201
202         crc = crc32_raw(buf, size, ~0U);
203         return (crc ^ ~0U);
204 }
205
206 uint32_t
207 calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
208     unsigned int length);
209 #ifdef _KERNEL
210 #if defined(__amd64__) || defined(__i386__)
211 uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
212 #endif
213 #if defined(__aarch64__)
214 uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned int);
215 #endif
216 #endif
217
218
219 LIBKERN_INLINE void *memset(void *, int, size_t);
220 #ifdef LIBKERN_BODY
221 LIBKERN_INLINE void *
222 memset(void *b, int c, size_t len)
223 {
224         char *bb;
225
226         if (c == 0)
227                 bzero(b, len);
228         else
229                 for (bb = (char *)b; len--; )
230                         *bb++ = c;
231         return (b);
232 }
233 #endif
234
235 static __inline char *
236 index(const char *p, int ch)
237 {
238
239         return (strchr(p, ch));
240 }
241
242 static __inline char *
243 rindex(const char *p, int ch)
244 {
245
246         return (strrchr(p, ch));
247 }
248
249 /* fnmatch() return values. */
250 #define FNM_NOMATCH     1       /* Match failed. */
251
252 /* fnmatch() flags. */
253 #define FNM_NOESCAPE    0x01    /* Disable backslash escaping. */
254 #define FNM_PATHNAME    0x02    /* Slash must be matched by slash. */
255 #define FNM_PERIOD      0x04    /* Period must be matched by period. */
256 #define FNM_LEADING_DIR 0x08    /* Ignore /<tail> after Imatch. */
257 #define FNM_CASEFOLD    0x10    /* Case insensitive search. */
258 #define FNM_IGNORECASE  FNM_CASEFOLD
259 #define FNM_FILE_NAME   FNM_PATHNAME
260
261 #endif /* !_SYS_LIBKERN_H_ */