blob: 359626c9648add81e6e2157e9f7a3f97af3a70e7 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Myles Watson58170782009-10-28 16:13:28 +00002
3/* This file is for "nuisance prototypes" that have no other home. */
4
Myles Watson34261952010-03-19 02:33:40 +00005#ifndef __LIB_H__
6#define __LIB_H__
Elyes HAOUAS0c154af2020-05-28 08:54:42 +02007
Alexandru Gagniuc3dd0e722013-12-26 22:53:52 -05008#include <types.h>
Myles Watson34261952010-03-19 02:33:40 +00009
Julius Wernera25b5d22016-02-08 11:46:22 -080010/* Defined in src/lib/lzma.c. Returns decompressed size or 0 on error. */
Julius Wernera25b5d22016-02-08 11:46:22 -080011size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn);
Myles Watson58170782009-10-28 16:13:28 +000012
Myles Watson34261952010-03-19 02:33:40 +000013/* Defined in src/lib/ramtest.c */
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020014/* Assumption is 32-bit addressable UC memory. */
Kyösti Mälkkie1aa9832019-03-23 10:00:31 +020015void ram_check(uintptr_t start);
16int ram_check_nodie(uintptr_t start);
17int ram_check_noprint_nodie(uintptr_t start);
Kyösti Mälkkif5cf60f2019-03-18 15:26:48 +020018void quick_ram_check_or_die(uintptr_t dst);
Myles Watson34261952010-03-19 02:33:40 +000019
David Hendricks560c6432014-02-13 13:07:50 -080020/* Defined in primitive_memtest.c */
21int primitive_memtest(uintptr_t base, uintptr_t size);
22
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -070023/* Defined in src/lib/stack.c */
Stefan Reinauer75dbc382012-10-15 15:19:43 -070024int checkstack(void *top_of_stack, int core);
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -070025
Lee Leahy1b0ab812015-05-14 14:50:42 -070026/*
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
30 */
Alexandru Gagniuc3dd0e722013-12-26 22:53:52 -050031void hexdump(const void *memory, size_t length);
Paul Menzel60ec2ff2014-05-03 16:21:34 +020032void hexdump32(char LEVEL, const void *d, size_t len);
Stefan Reinauer2f38b072013-07-18 16:24:08 -070033
Duncan Laurieb9552842016-05-09 10:58:03 -070034/*
35 * hexstrtobin - Turn a string of ASCII hex characters into binary
36 *
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
40 *
41 * Defined in src/lib/hexstrtobin.c
42 * Ignores non-hex characters in the string.
Anna Karascb01c2a2020-07-02 15:32:00 +020043 * Ignores the last hex character if the number of hex characters in the string
44 * is odd.
Duncan Laurieb9552842016-05-09 10:58:03 -070045 * Returns the number of bytes that have been put in the buffer.
46 */
47size_t hexstrtobin(const char *str, uint8_t *buf, size_t len);
48
Angel Ponse0ce60c2020-10-14 17:48:05 +020049/* Population Count: number of bits that are one */
50static inline int popcnt(u32 x) { return __builtin_popcount(x); }
Julius Werner7a8a4ab2015-05-22 16:26:40 -070051/* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
52static 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 */
54static 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 */
56static inline int __ffs(u32 x) { return log2(x & (u32)(-(s32)x)); }
Julius Werner7a8a4ab2015-05-22 16:26:40 -070057
58/* Integer binary logarithm (rounding up): log2_ceil(0) == -1, log2(5) == 3 */
59static inline int log2_ceil(u32 x) { return (x == 0) ? -1 : log2(x * 2 - 1); }
60
Angel Ponse0ce60c2020-10-14 17:48:05 +020061static inline int popcnt64(u64 x) { return __builtin_popcountll(x); }
Tim Wawrzynczak7ded1af2020-10-01 15:36:42 -060062static inline int clz64(u64 x) { return x ? __builtin_clzll(x) : sizeof(x) * 8; }
63static inline int log2_64(u64 x) { return sizeof(x) * 8 - clz64(x) - 1; }
64static inline int __ffs64(u64 x) { return log2_64(x & (u64)(-(s64)x)); }
65
Myles Watson34261952010-03-19 02:33:40 +000066#endif /* __LIB_H__ */