/* SPDX-License-Identifier: GPL-2.0-or-later * Copyright Red Hat * Author: David Gibson * * Definitions used by both passt/pasta and other tools */ #ifndef COMMON_H #define COMMON_H #include #include #define VERSION_BLOB \ VERSION "\n" \ "Copyright Red Hat\n" \ "GNU General Public License, version 2 or later\n" \ " \n" \ "This is free software: you are free to change and redistribute it.\n" \ "There is NO WARRANTY, to the extent permitted by law.\n\n" /* FPRINTF() intentionally silences cert-err33-c clang-tidy warnings */ #define FPRINTF(f, ...) (void)fprintf(f, __VA_ARGS__) #define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0]))) #ifndef MIN #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #endif #ifndef MAX #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #endif #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #define DIV_ROUND_CLOSEST(n, d) (((n) + (d) / 2) / (d)) #define ROUND_DOWN(x, y) ((x) & ~((y) - 1)) #define ROUND_UP(x, y) (((x) + (y) - 1) & ~((y) - 1)) #define BIT(n) (1UL << (n)) #define MAX_FROM_BITS(n) (((1U << (n)) - 1)) #ifndef __bswap_constant_16 #define __bswap_constant_16(x) \ ((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))) #endif #ifndef __bswap_constant_32 #define __bswap_constant_32(x) \ ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) #endif #ifndef __bswap_constant_32 #define __bswap_constant_32(x) \ ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) #endif #ifndef __bswap_constant_64 #define __bswap_constant_64(x) \ ((((x) & 0xff00000000000000ULL) >> 56) | \ (((x) & 0x00ff000000000000ULL) >> 40) | \ (((x) & 0x0000ff0000000000ULL) >> 24) | \ (((x) & 0x000000ff00000000ULL) >> 8) | \ (((x) & 0x00000000ff000000ULL) << 8) | \ (((x) & 0x0000000000ff0000ULL) << 24) | \ (((x) & 0x000000000000ff00ULL) << 40) | \ (((x) & 0x00000000000000ffULL) << 56)) #endif #if __BYTE_ORDER == __BIG_ENDIAN #define htons_constant(x) (x) #define htonl_constant(x) (x) #define htonll_constant(x) (x) #define ntohs_constant(x) (x) #define ntohl_constant(x) (x) #define ntohll_constant(x) (x) #else #define htons_constant(x) (__bswap_constant_16(x)) #define htonl_constant(x) (__bswap_constant_32(x)) #define htonll_constant(x) (__bswap_constant_64(x)) #define ntohs_constant(x) (__bswap_constant_16(x)) #define ntohl_constant(x) (__bswap_constant_32(x)) #define ntohll_constant(x) (__bswap_constant_64(x)) #endif #define ntohll(x) (be64toh((x))) #define htonll(x) (htobe64((x))) #define BITMAP_BIT(n) (BIT((n) % (sizeof(long) * 8))) #define BITMAP_WORD(n) (n / (sizeof(long) * 8)) /** * bitmap_set() - Set single bit in bitmap * @map: Pointer to bitmap * @bit: Bit number to set */ static inline void bitmap_set(uint8_t *map, unsigned bit) { unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); *word |= BITMAP_BIT(bit); } /** * bitmap_clear() - Clear single bit in bitmap * @map: Pointer to bitmap * @bit: Bit number to clear */ /* cppcheck-suppress unusedFunction */ static inline void bitmap_clear(uint8_t *map, unsigned bit) { unsigned long *word = (unsigned long *)map + BITMAP_WORD(bit); *word &= ~BITMAP_BIT(bit); } /** * bitmap_isset() - Check for set bit in bitmap * @map: Pointer to bitmap * @bit: Bit number to check * * Return: true if given bit is set, false if it's not */ static inline bool bitmap_isset(const uint8_t *map, unsigned bit) { const unsigned long *word = (const unsigned long *)map + BITMAP_WORD(bit); return !!(*word & BITMAP_BIT(bit)); } /** * bitmap_or() - Logical disjunction (OR) of two bitmaps * @dst: Pointer to result bitmap * @size: Size of bitmaps, in bytes * @a: First operand * @b: Second operand */ /* cppcheck-suppress unusedFunction */ static inline void bitmap_or(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b) { unsigned long *dw = (unsigned long *)dst; unsigned long *aw = (unsigned long *)a; unsigned long *bw = (unsigned long *)b; size_t i; for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) *dw = *aw | *bw; for (i = size / sizeof(long) * sizeof(long); i < size; i++) dst[i] = a[i] | b[i]; } /** * bitmap_and_not() - Logical conjunction with complement (AND NOT) of bitmap * @dst: Pointer to result bitmap * @size: Size of bitmaps, in bytes * @a: First operand * @b: Second operand */ static inline void bitmap_and_not(uint8_t *dst, size_t size, const uint8_t *a, const uint8_t *b) { unsigned long *dw = (unsigned long *)dst; unsigned long *aw = (unsigned long *)a; unsigned long *bw = (unsigned long *)b; size_t i; for (i = 0; i < size / sizeof(long); i++, dw++, aw++, bw++) *dw = *aw & ~*bw; for (i = size / sizeof(long) * sizeof(long); i < size; i++) dst[i] = a[i] & ~b[i]; } #endif /* _COMMON_H */