source: rtems-libbsd/freebsd/sys/sys/libkern.h @ 9bc7b96

55-freebsd-126-freebsd-12
Last change on this file since 9bc7b96 was 9128d20, checked in by Sebastian Huber <sebastian.huber@…>, on 05/10/17 at 12:39:19

libkern.h: Add ffsll()

  • Property mode set to 100644
File size: 9.5 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__ */
139int      bcmp(const void *, const void *, size_t);
140int      timingsafe_bcmp(const void *, const void *, size_t);
141void    *bsearch(const void *, const void *, size_t,
142            size_t, int (*)(const void *, const void *));
143#ifndef __rtems__
144#ifndef HAVE_INLINE_FFS
145int      ffs(int);
146#endif
147#ifndef HAVE_INLINE_FFSL
148int      ffsl(long);
149#endif
150#ifndef HAVE_INLINE_FFSLL
151int      ffsll(long long);
152#endif
153#ifndef HAVE_INLINE_FLS
154int      fls(int);
155#endif
156#ifndef HAVE_INLINE_FLSL
157int      flsl(long);
158#endif
159#ifndef HAVE_INLINE_FLSLL
160int      flsll(long long);
161#endif
162#else /* __rtems__ */
163static inline int
164builtin_fls(int x)
165{
166
167  return (x != 0 ? sizeof(x) * 8 - __builtin_clz((unsigned int)x) : 0);
168}
169
170static inline int
171builtin_flsl(long x)
172{
173
174  return (x != 0 ? sizeof(x) * 8 - __builtin_clzl((unsigned long)x) : 0);
175}
176
177static inline int
178builtin_flsll(long long x)
179{
180
181  return (x != 0 ? sizeof(x) * 8 - __builtin_clzll((unsigned long long)x) : 0);
182}
183
184#define ffs(_x)         __builtin_ffs((unsigned int)(_x))
185#define ffsl(_x)        __builtin_ffsl((unsigned long)(_x))
186#define ffsll(_x)       __builtin_ffsll((unsigned long long)(_x))
187#define fls(_x)         builtin_fls(_x)
188#define flsl(_x)        builtin_flsl(_x)
189#define flsll(_x)       builtin_flsll(_x)
190#endif /* __rtems__ */
191#define bitcount64(x)   __bitcount64((uint64_t)(x))
192#define bitcount32(x)   __bitcount32((uint32_t)(x))
193#define bitcount16(x)   __bitcount16((uint16_t)(x))
194#define bitcountl(x)    __bitcountl((u_long)(x))
195#define bitcount(x)     __bitcount((u_int)(x))
196
197int      fnmatch(const char *, const char *, int);
198int      locc(int, char *, u_int);
199void    *memchr(const void *s, int c, size_t n);
200void    *memcchr(const void *s, int c, size_t n);
201int      memcmp(const void *b1, const void *b2, size_t len);
202void    *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
203void     qsort(void *base, size_t nmemb, size_t size,
204            int (*compar)(const void *, const void *));
205void     qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
206            int (*compar)(void *, const void *, const void *));
207#ifndef __rtems__
208u_long   random(void);
209#else /* __rtems__ */
210#include <stdlib.h>
211u_long   _bsd_random(void);
212#define random() _bsd_random()
213#endif /* __rtems__ */
214int      scanc(u_int, const u_char *, const u_char *, int);
215#ifndef __rtems__
216void     srandom(u_long);
217#else /* __rtems__ */
218void     _bsd_srandom(u_long);
219#define srandom(_x) _bsd_srandom(_x)
220#endif /* __rtems__ */
221int      strcasecmp(const char *, const char *);
222char    *strcat(char * __restrict, const char * __restrict);
223char    *strchr(const char *, int);
224int      strcmp(const char *, const char *);
225char    *strcpy(char * __restrict, const char * __restrict);
226size_t   strcspn(const char * __restrict, const char * __restrict) __pure;
227#ifdef __rtems__
228#include <string.h>
229#define strdup _bsd_strdup
230#define strndup _bsd_strndup
231#endif /* __rtems__ */
232char    *strdup(const char *__restrict, struct malloc_type *);
233char    *strncat(char *, const char *, size_t);
234char    *strndup(const char *__restrict, size_t, struct malloc_type *);
235size_t   strlcat(char *, const char *, size_t);
236size_t   strlcpy(char *, const char *, size_t);
237size_t   strlen(const char *);
238int      strncasecmp(const char *, const char *, size_t);
239int      strncmp(const char *, const char *, size_t);
240char    *strncpy(char * __restrict, const char * __restrict, size_t);
241size_t   strnlen(const char *, size_t);
242char    *strrchr(const char *, int);
243char    *strsep(char **, const char *delim);
244size_t   strspn(const char *, const char *);
245char    *strstr(const char *, const char *);
246int      strvalid(const char *, size_t);
247
248extern const uint32_t crc32_tab[];
249
250static __inline uint32_t
251crc32_raw(const void *buf, size_t size, uint32_t crc)
252{
253        const uint8_t *p = (const uint8_t *)buf;
254
255        while (size--)
256                crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
257        return (crc);
258}
259
260static __inline uint32_t
261crc32(const void *buf, size_t size)
262{
263        uint32_t crc;
264
265        crc = crc32_raw(buf, size, ~0U);
266        return (crc ^ ~0U);
267}
268
269uint32_t
270calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
271    unsigned int length);
272#ifdef _KERNEL
273#if defined(__amd64__) || defined(__i386__)
274uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
275#endif
276#endif
277
278
279LIBKERN_INLINE void *memset(void *, int, size_t);
280#ifdef LIBKERN_BODY
281LIBKERN_INLINE void *
282memset(void *b, int c, size_t len)
283{
284        char *bb;
285
286        if (c == 0)
287                bzero(b, len);
288        else
289                for (bb = (char *)b; len--; )
290                        *bb++ = c;
291        return (b);
292}
293#endif
294
295#ifndef __rtems__
296static __inline char *
297index(const char *p, int ch)
298{
299
300        return (strchr(p, ch));
301}
302
303static __inline char *
304rindex(const char *p, int ch)
305{
306
307        return (strrchr(p, ch));
308}
309#endif /* __rtems__ */
310
311/* fnmatch() return values. */
312#define FNM_NOMATCH     1       /* Match failed. */
313
314/* fnmatch() flags. */
315#define FNM_NOESCAPE    0x01    /* Disable backslash escaping. */
316#define FNM_PATHNAME    0x02    /* Slash must be matched by slash. */
317#define FNM_PERIOD      0x04    /* Period must be matched by period. */
318#define FNM_LEADING_DIR 0x08    /* Ignore /<tail> after Imatch. */
319#define FNM_CASEFOLD    0x10    /* Case insensitive search. */
320#define FNM_IGNORECASE  FNM_CASEFOLD
321#define FNM_FILE_NAME   FNM_PATHNAME
322
323#endif /* !_SYS_LIBKERN_H_ */
Note: See TracBrowser for help on using the repository browser.