Documentation/releases: Add 4.15 release notes template
[coreboot.git] / src / include / lib.h
blob359626c9648add81e6e2157e9f7a3f97af3a70e7
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /* This file is for "nuisance prototypes" that have no other home. */
5 #ifndef __LIB_H__
6 #define __LIB_H__
8 #include <types.h>
10 /* Defined in src/lib/lzma.c. Returns decompressed size or 0 on error. */
11 size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn);
13 /* Defined in src/lib/ramtest.c */
14 /* Assumption is 32-bit addressable UC memory. */
15 void ram_check(uintptr_t start);
16 int ram_check_nodie(uintptr_t start);
17 int ram_check_noprint_nodie(uintptr_t start);
18 void quick_ram_check_or_die(uintptr_t dst);
20 /* Defined in primitive_memtest.c */
21 int primitive_memtest(uintptr_t base, uintptr_t size);
23 /* Defined in src/lib/stack.c */
24 int checkstack(void *top_of_stack, int core);
27 * Defined in src/lib/hexdump.c
28 * Use the Linux command "xxd" for matching output. xxd is found in package
29 * https://packages.debian.org/jessie/amd64/vim-common/filelist
31 void hexdump(const void *memory, size_t length);
32 void hexdump32(char LEVEL, const void *d, size_t len);
35 * hexstrtobin - Turn a string of ASCII hex characters into binary
37 * @str: String of hex characters to parse
38 * @buf: Buffer to store the resulting bytes into
39 * @len: Maximum length of buffer to fill
41 * Defined in src/lib/hexstrtobin.c
42 * Ignores non-hex characters in the string.
43 * Ignores the last hex character if the number of hex characters in the string
44 * is odd.
45 * Returns the number of bytes that have been put in the buffer.
47 size_t hexstrtobin(const char *str, uint8_t *buf, size_t len);
49 /* Population Count: number of bits that are one */
50 static inline int popcnt(u32 x) { return __builtin_popcount(x); }
51 /* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
52 static inline int clz(u32 x) { return x ? __builtin_clz(x) : sizeof(x) * 8; }
53 /* Integer binary logarithm (rounding down): log2(0) == -1, log2(5) == 2 */
54 static inline int log2(u32 x) { return sizeof(x) * 8 - clz(x) - 1; }
55 /* Find First Set: __ffs(1) == 0, __ffs(0) == -1, __ffs(1<<31) == 31 */
56 static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
58 /* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2(5) == 3 */
59 static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x * 2 - 1); }
61 static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
62 static inline int clz64(u64 x) { return x ? __builtin_clzll(x) : sizeof(x) * 8; }
63 static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
64 static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
66 #endif /* __LIB_H__ */