1 /*
2 * Copyright 2009, Colin Günther, coling@gmx.de.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5 #ifndef _FBSD_COMPAT_SYS_LIBKERN_H_
6 #define _FBSD_COMPAT_SYS_LIBKERN_H_
7
8
9 #include <strings.h>
10 #include <sys/cdefs.h>
11 #include <sys/types.h>
12
13
14 __BEGIN_DECLS
15
16
17 extern int random(void);
18 u_int read_random(void *, u_int);
19 void arc4rand(void *ptr, u_int len, int reseed);
20 uint32_t arc4random(void);
21 void arc4random_buf(void *ptr, size_t len);
22
23 int timingsafe_bcmp(const void *, const void *, size_t);
24
imax(int a,int b)25 static __inline int imax(int a, int b) { return (a > b ? a : b); }
imin(int a,int b)26 static __inline int imin(int a, int b) { return (a < b ? a : b); }
27
28 extern int abs(int a);
29
30
31 static __inline __pure2 int
ffsl(long mask)32 ffsl(long mask)
33 {
34 return (__builtin_ffsl((u_long)mask));
35 }
36
37 static __inline __pure2 int
ffsll(long long mask)38 ffsll(long long mask)
39 {
40 return (__builtin_ffsll((unsigned long long)mask));
41 }
42
43 static __inline __pure2 int
fls(int mask)44 fls(int mask)
45 {
46 return (mask == 0 ? 0 :
47 8 * sizeof(mask) - __builtin_clz((u_int)mask));
48 }
49
50 static __inline __pure2 int
flsl(long mask)51 flsl(long mask)
52 {
53 return (mask == 0 ? 0 :
54 8 * sizeof(mask) - __builtin_clzl((u_long)mask));
55 }
56
57 static __inline __pure2 int
flsll(long long mask)58 flsll(long long mask)
59 {
60 return (mask == 0 ? 0 :
61 8 * sizeof(mask) - __builtin_clzll((unsigned long long)mask));
62 }
63
64 __END_DECLS
65
66 #endif /* _FBSD_COMPAT_SYS_LIBKERN_H_ */
67