source: rtems-libbsd/freebsd/sys/sys/libkern.h @ 1d99262

55-freebsd-126-freebsd-12
Last change on this file since 1d99262 was 1d99262, checked in by Sebastian Huber <sebastian.huber@…>, on 08/09/18 at 05:20:08

libkern.h: Avoid call overhead for bcmp()

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