1 /* $NetBSD: libkern.h,v 1.91 2009/05/13 02:50:32 pgoyette Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)libkern.h 8.2 (Berkeley) 8/5/94
34 #ifndef _LIB_LIBKERN_LIBKERN_H_
35 #define _LIB_LIBKERN_LIBKERN_H_
37 #include <sys/types.h>
38 #include <sys/inttypes.h>
41 #ifndef LIBKERN_INLINE
42 #define LIBKERN_INLINE static __inline
46 LIBKERN_INLINE
int imax(int, int) __unused
;
47 LIBKERN_INLINE
int imin(int, int) __unused
;
48 LIBKERN_INLINE u_int
max(u_int
, u_int
) __unused
;
49 LIBKERN_INLINE u_int
min(u_int
, u_int
) __unused
;
50 LIBKERN_INLINE
long lmax(long, long) __unused
;
51 LIBKERN_INLINE
long lmin(long, long) __unused
;
52 LIBKERN_INLINE u_long
ulmax(u_long
, u_long
) __unused
;
53 LIBKERN_INLINE u_long
ulmin(u_long
, u_long
) __unused
;
54 LIBKERN_INLINE
int abs(int) __unused
;
56 LIBKERN_INLINE
int isspace(int) __unused
;
57 LIBKERN_INLINE
int isascii(int) __unused
;
58 LIBKERN_INLINE
int isupper(int) __unused
;
59 LIBKERN_INLINE
int islower(int) __unused
;
60 LIBKERN_INLINE
int isalpha(int) __unused
;
61 LIBKERN_INLINE
int isdigit(int) __unused
;
62 LIBKERN_INLINE
int isxdigit(int) __unused
;
63 LIBKERN_INLINE
int toupper(int) __unused
;
64 LIBKERN_INLINE
int tolower(int) __unused
;
70 return (a
> b
? a
: b
);
75 return (a
< b
? a
: b
);
80 return (a
> b
? a
: b
);
85 return (a
< b
? a
: b
);
90 return (a
> b
? a
: b
);
95 return (a
< b
? a
: b
);
98 ulmax(u_long a
, u_long b
)
100 return (a
> b
? a
: b
);
102 LIBKERN_INLINE u_long
103 ulmin(u_long a
, u_long b
)
105 return (a
< b
? a
: b
);
111 return(j
< 0 ? -j
: j
);
117 return (ch
== ' ' || (ch
>= '\t' && ch
<= '\r'));
123 return ((ch
& ~0x7f) == 0);
129 return (ch
>= 'A' && ch
<= 'Z');
135 return (ch
>= 'a' && ch
<= 'z');
141 return (isupper(ch
) || islower(ch
));
147 return (ch
>= '0' && ch
<= '9');
153 return (isdigit(ch
) ||
154 (ch
>= 'A' && ch
<= 'F') ||
155 (ch
>= 'a' && ch
<= 'f'));
175 #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
177 #ifdef NDEBUG /* tradition! */
178 #define assert(e) ((void)0)
181 #define assert(e) (__predict_true((e)) ? (void)0 : \
182 __kernassert("", __FILE__, __LINE__, #e))
184 #define assert(e) (__predict_true((e)) ? (void)0 : \
185 __kernassert("", __FILE__, __LINE__, "e"))
195 #define CTASSERT(x) _CTASSERT(x, __LINE__)
196 #define _CTASSERT(x, y) __CTASSERT(x, y)
197 #define __CTASSERT(x, y) typedef char __ctassert ## y[(x) ? 1 : -1];
200 #define _DIAGASSERT(a) (void)0
202 #define KASSERTMSG(e, msg) /* NOTHING */
203 #define KASSERT(e) /* NOTHING */
205 #define KASSERTMSG(e, msg) ((void)0)
206 #define KASSERT(e) ((void)0)
208 #else /* DIAGNOSTIC */
209 #define _DIAGASSERT(a) assert(a)
210 #define KASSERTMSG(e, msg) do { \
211 if (__predict_false((e))) \
213 } while (/*CONSTCOND*/ 0)
215 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
216 __kernassert("diagnostic ", __FILE__, __LINE__, #e))
218 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
219 __kernassert("diagnostic ", __FILE__, __LINE__,"e"))
225 #define KDASSERT(e) /* NOTHING */
227 #define KDASSERT(e) ((void)0)
231 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
232 __kernassert("debugging ", __FILE__, __LINE__, #e))
234 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
235 __kernassert("debugging ", __FILE__, __LINE__, "e"))
239 * XXX: For compatibility we use SMALL_RANDOM by default.
244 #define offsetof(type, member) \
245 ((size_t)(unsigned long)(&(((type *)0)->member)))
248 #define MTPRNG_RLEN 624
249 struct mtprng_state
{
251 uint32_t mt_elem
[MTPRNG_RLEN
];
253 uint32_t mt_sparse
[3];
256 /* Prototypes for which GCC built-ins exist. */
257 void *memcpy(void *, const void *, size_t);
258 int memcmp(const void *, const void *, size_t);
259 void *memset(void *, int, size_t);
260 #if __GNUC_PREREQ__(2, 95) && (__GNUC_PREREQ__(4, 0) || !defined(__vax__)) && \
261 !defined(_STANDALONE)
262 #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
263 #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
265 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) && !defined(_STANDALONE)
266 #define memset(d, v, l) __builtin_memset(d, v, l)
269 char *strcpy(char *, const char *);
270 int strcmp(const char *, const char *);
271 size_t strlen(const char *);
272 char *strsep(char **, const char *);
273 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
274 #define strcpy(d, s) __builtin_strcpy(d, s)
275 #define strcmp(a, b) __builtin_strcmp(a, b)
276 #define strlen(a) __builtin_strlen(a)
279 /* Functions for which we always use built-ins. */
281 #define alloca(s) __builtin_alloca(s)
284 /* These exist in GCC 3.x, but we don't bother. */
285 char *strcat(char *, const char *);
286 char *strncpy(char *, const char *, size_t);
287 int strncmp(const char *, const char *, size_t);
288 char *strchr(const char *, int);
289 char *strrchr(const char *, int);
291 char *strstr(const char *, const char *);
294 * ffs is an instruction on vax.
297 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
298 #define ffs(x) __builtin_ffs(x)
301 void __kernassert(const char *, const char *, int, const char *);
303 bcdtobin(unsigned int);
305 bintobcd(unsigned int);
307 inet_addr(const char *);
309 int inet_aton(const char *, struct in_addr
*);
310 char *intoa(u_int32_t
);
311 #define inet_ntoa(a) intoa((a).s_addr)
312 void *memchr(const void *, int, size_t);
313 void *memmove(void *, const void *, size_t);
314 int pmatch(const char *, const char *, const char **);
315 u_int32_t
arc4random(void);
316 void arc4randbytes(void *, size_t);
318 void srandom(unsigned long);
319 char *initstate(unsigned long, char *, size_t);
320 char *setstate(char *);
321 #endif /* SMALL_RANDOM */
323 void mtprng_init32(struct mtprng_state
*, uint32_t);
324 void mtprng_initarray(struct mtprng_state
*, const uint32_t *, size_t);
325 uint32_t mtprng_rawrandom(struct mtprng_state
*);
326 uint32_t mtprng_random(struct mtprng_state
*);
327 int scanc(u_int
, const u_char
*, const u_char
*, int);
328 int skpc(int, size_t, u_char
*);
329 int strcasecmp(const char *, const char *);
330 size_t strlcpy(char *, const char *, size_t);
331 size_t strlcat(char *, const char *, size_t);
332 int strncasecmp(const char *, const char *, size_t);
333 u_long
strtoul(const char *, char **, int);
334 long long strtoll(const char *, char **, int);
335 unsigned long long strtoull(const char *, char **, int);
336 uintmax_t strtoumax(const char *, char **, int);
337 int snprintb(char *, size_t, const char *, uint64_t);
338 int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
339 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
341 uint32_t crc32(uint32_t, const uint8_t *, size_t);
342 unsigned int popcount(unsigned int) __constfunc
;
343 unsigned int popcountl(unsigned long) __constfunc
;
344 unsigned int popcountll(unsigned long long) __constfunc
;
345 unsigned int popcount32(uint32_t) __constfunc
;
346 unsigned int popcount64(uint64_t) __constfunc
;
347 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */