source: rtems-libbsd/freebsd/sys/sys/libkern.h

6-freebsd-12
Last change on this file was 705e362, checked in by Sebastian Huber <sebastian.huber@…>, on 06/05/19 at 09:35:39

Update to FreeBSD stable/12 2019-06-05

Git mirror commit 78576620f2689e23144a1cf1bf55106cc6abe2b7.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 *      @(#)libkern.h   8.1 (Berkeley) 6/10/93
32 * $FreeBSD$
33 */
34
35#ifndef _SYS_LIBKERN_H_
36#define _SYS_LIBKERN_H_
37
38#include <sys/cdefs.h>
39#include <sys/types.h>
40#ifdef _KERNEL
41#include <sys/systm.h>
42#endif
43
44#ifndef __rtems__
45#ifndef LIBKERN_INLINE
46#define LIBKERN_INLINE  static __inline
47#define LIBKERN_BODY
48#endif
49#else /* __rtems__ */
50#define LIBKERN_INLINE
51#endif /* __rtems__ */
52
53/* BCD conversions. */
54extern u_char const     bcd2bin_data[];
55extern u_char const     bin2bcd_data[];
56extern char const       hex2ascii_data[];
57
58#define LIBKERN_LEN_BCD2BIN     154
59#define LIBKERN_LEN_BIN2BCD     100
60#define LIBKERN_LEN_HEX2ASCII   36
61
62static inline u_char
63bcd2bin(int bcd)
64{
65
66        KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
67            ("invalid bcd %d", bcd));
68        return (bcd2bin_data[bcd]);
69}
70
71static inline u_char
72bin2bcd(int bin)
73{
74
75        KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
76            ("invalid bin %d", bin));
77        return (bin2bcd_data[bin]);
78}
79
80static inline char
81hex2ascii(int hex)
82{
83
84        KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
85            ("invalid hex %d", hex));
86        return (hex2ascii_data[hex]);
87}
88
89static inline bool
90validbcd(int bcd)
91{
92
93        return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
94}
95
96static __inline int imax(int a, int b) { return (a > b ? a : b); }
97static __inline int imin(int a, int b) { return (a < b ? a : b); }
98static __inline long lmax(long a, long b) { return (a > b ? a : b); }
99static __inline long lmin(long a, long b) { return (a < b ? a : b); }
100static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
101static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
102static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
103static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
104static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
105static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
106static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
107static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
108static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
109{
110
111        return (a > b ? a : b);
112}
113static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
114{
115
116        return (a < b ? a : b);
117}
118static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
119static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
120
121#ifndef __rtems__
122static __inline int abs(int a) { return (a < 0 ? -a : a); }
123static __inline long labs(long a) { return (a < 0 ? -a : a); }
124#endif /* __rtems__ */
125static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
126
127#define ARC4_ENTR_NONE  0       /* Don't have entropy yet. */
128#define ARC4_ENTR_HAVE  1       /* Have entropy. */
129#define ARC4_ENTR_SEED  2       /* Reseeding. */
130extern int arc4rand_iniseed_state;
131
132/* Prototypes for non-quad routines. */
133struct malloc_type;
134uint32_t arc4random(void);
135void     arc4random_buf(void *, size_t);
136#ifndef __rtems__
137void     arc4rand(void *, u_int, int);
138#else /* __rtems__ */
139static inline void
140arc4rand(void *ptr, u_int len, int reseed)
141{
142
143        (void)reseed;
144        arc4random_buf(ptr, len);
145}
146#endif /* __rtems__ */
147int      timingsafe_bcmp(const void *, const void *, size_t);
148void    *bsearch(const void *, const void *, size_t,
149            size_t, int (*)(const void *, const void *));
150#ifndef __rtems__
151#ifndef HAVE_INLINE_FFS
152int      ffs(int);
153#endif
154#ifndef HAVE_INLINE_FFSL
155int      ffsl(long);
156#endif
157#ifndef HAVE_INLINE_FFSLL
158int      ffsll(long long);
159#endif
160#ifndef HAVE_INLINE_FLS
161int      fls(int);
162#endif
163#ifndef HAVE_INLINE_FLSL
164int      flsl(long);
165#endif
166#ifndef HAVE_INLINE_FLSLL
167int      flsll(long long);
168#endif
169#else /* __rtems__ */
170static inline int
171builtin_fls(int x)
172{
173
174  return (x != 0 ? sizeof(x) * 8 - __builtin_clz((unsigned int)x) : 0);
175}
176
177static inline int
178builtin_flsl(long x)
179{
180
181  return (x != 0 ? sizeof(x) * 8 - __builtin_clzl((unsigned long)x) : 0);
182}
183
184static inline int
185builtin_flsll(long long x)
186{
187
188  return (x != 0 ? sizeof(x) * 8 - __builtin_clzll((unsigned long long)x) : 0);
189}
190
191#define ffs(_x)         __builtin_ffs((unsigned int)(_x))
192#define ffsl(_x)        __builtin_ffsl((unsigned long)(_x))
193#define ffsll(_x)       __builtin_ffsll((unsigned long long)(_x))
194#define fls(_x)         builtin_fls(_x)
195#define flsl(_x)        builtin_flsl(_x)
196#define flsll(_x)       builtin_flsll(_x)
197#endif /* __rtems__ */
198#define bitcount64(x)   __bitcount64((uint64_t)(x))
199#define bitcount32(x)   __bitcount32((uint32_t)(x))
200#define bitcount16(x)   __bitcount16((uint16_t)(x))
201#define bitcountl(x)    __bitcountl((u_long)(x))
202#define bitcount(x)     __bitcount((u_int)(x))
203
204int      fnmatch(const char *, const char *, int);
205int      locc(int, char *, u_int);
206void    *memchr(const void *s, int c, size_t n);
207void    *memcchr(const void *s, int c, size_t n);
208void    *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
209void     qsort(void *base, size_t nmemb, size_t size,
210            int (*compar)(const void *, const void *));
211void     qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
212            int (*compar)(void *, const void *, const void *));
213#ifndef __rtems__
214u_long   random(void);
215#else /* __rtems__ */
216#include <stdlib.h>
217u_long   _bsd_random(void);
218#define random() _bsd_random()
219#endif /* __rtems__ */
220int      scanc(u_int, const u_char *, const u_char *, int);
221#ifndef __rtems__
222void     srandom(u_long);
223#else /* __rtems__ */
224void     _bsd_srandom(u_long);
225#define srandom(_x) _bsd_srandom(_x)
226#endif /* __rtems__ */
227int      strcasecmp(const char *, const char *);
228char    *strcat(char * __restrict, const char * __restrict);
229char    *strchr(const char *, int);
230int      strcmp(const char *, const char *);
231char    *strcpy(char * __restrict, const char * __restrict);
232size_t   strcspn(const char * __restrict, const char * __restrict) __pure;
233char    *strdup_flags(const char *__restrict, struct malloc_type *, int);
234#ifdef __rtems__
235#include <string.h>
236#define strdup _bsd_strdup
237#define strndup _bsd_strndup
238#endif /* __rtems__ */
239char    *strdup(const char *__restrict, struct malloc_type *);
240char    *strncat(char *, const char *, size_t);
241char    *strndup(const char *__restrict, size_t, struct malloc_type *);
242size_t   strlcat(char *, const char *, size_t);
243size_t   strlcpy(char *, const char *, size_t);
244size_t   strlen(const char *);
245int      strncasecmp(const char *, const char *, size_t);
246int      strncmp(const char *, const char *, size_t);
247char    *strncpy(char * __restrict, const char * __restrict, size_t);
248size_t   strnlen(const char *, size_t);
249char    *strrchr(const char *, int);
250char    *strsep(char **, const char *delim);
251size_t   strspn(const char *, const char *);
252char    *strstr(const char *, const char *);
253int      strvalid(const char *, size_t);
254
255extern const uint32_t crc32_tab[];
256
257static __inline uint32_t
258crc32_raw(const void *buf, size_t size, uint32_t crc)
259{
260        const uint8_t *p = (const uint8_t *)buf;
261
262        while (size--)
263                crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
264        return (crc);
265}
266
267static __inline uint32_t
268crc32(const void *buf, size_t size)
269{
270        uint32_t crc;
271
272        crc = crc32_raw(buf, size, ~0U);
273        return (crc ^ ~0U);
274}
275
276uint32_t
277calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
278    unsigned int length);
279#ifdef _KERNEL
280#if defined(__amd64__) || defined(__i386__)
281uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
282#endif
283#if defined(__aarch64__)
284uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned int);
285#endif
286#endif
287
288#ifndef __rtems__
289static __inline char *
290index(const char *p, int ch)
291{
292
293        return (strchr(p, ch));
294}
295
296static __inline char *
297rindex(const char *p, int ch)
298{
299
300        return (strrchr(p, ch));
301}
302#endif /* __rtems__ */
303
304/* fnmatch() return values. */
305#define FNM_NOMATCH     1       /* Match failed. */
306
307/* fnmatch() flags. */
308#define FNM_NOESCAPE    0x01    /* Disable backslash escaping. */
309#define FNM_PATHNAME    0x02    /* Slash must be matched by slash. */
310#define FNM_PERIOD      0x04    /* Period must be matched by period. */
311#define FNM_LEADING_DIR 0x08    /* Ignore /<tail> after Imatch. */
312#define FNM_CASEFOLD    0x10    /* Case insensitive search. */
313#define FNM_IGNORECASE  FNM_CASEFOLD
314#define FNM_FILE_NAME   FNM_PATHNAME
315
316#endif /* !_SYS_LIBKERN_H_ */
Note: See TracBrowser for help on using the repository browser.