bits.h -> bitops.h, start bitops refactor: target all implem. visible

This commit is contained in:
2024-01-03 19:13:44 +01:00
parent b63a5cceda
commit 0dcca10559
13 changed files with 55 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
/* bits.h - bits functions. /* bitops.h - bits functions.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -44,6 +44,26 @@ void print_bitops_impl(void);
/* count set bits: 10101000 -> 3 /* count set bits: 10101000 -> 3
* ^ ^ ^ * ^ ^ ^
*/ */
#if __has_builtin(__builtin_popcountll)
#define ___popcount64_native(n) __builtin_popcountll(n)
#endif
#if __has_builtin(__builtin_popcount)
#define ___popcount32_native(n) __builtin_popcount(n)
#endif
#define ___popcount_emulated(n) ({ \
int ___count = 0; \
while (n) { \
___count++; \
n &= (n - 1); \
} \
___count; })
#ifdef ___popcount64_native
#define ppcount64(n) ___popcount64_native(n)
#else
#define ppcount64(n) ___popcount_emulated(n)
#endif
static __always_inline int popcount64(u64 n) static __always_inline int popcount64(u64 n)
{ {
# if __has_builtin(__builtin_popcountll) # if __has_builtin(__builtin_popcountll)

View File

@@ -1,6 +1,6 @@
/* br.h - misc macros. /* br.h - misc macros.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *

View File

@@ -20,9 +20,6 @@
* can somehow continue operating, perhaps with reduced functionality, * can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy. * it's probably not BUG-worthy.
* *
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly.
*/ */
#define BUG() do { \ #define BUG() do { \
fprintf(stderr, "BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ fprintf(stderr, "BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
@@ -32,9 +29,8 @@
#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0) #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
/* /*
* WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report * WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report significant
* significant kernel issues that need prompt attention if they should ever * issues that need prompt attention if they should ever appear at runtime.
* appear at runtime.
* *
* Do not use these macros when checking for invalid external inputs * Do not use these macros when checking for invalid external inputs
* (e.g. invalid system call arguments, or invalid data coming from * (e.g. invalid system call arguments, or invalid data coming from
@@ -50,6 +46,7 @@
#define __WARN() do { \ #define __WARN() do { \
fprintf(stderr, "WARNING: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ fprintf(stderr, "WARNING: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
} while (0) } while (0)
#define __WARN_printf(arg...) do { \ #define __WARN_printf(arg...) do { \
vfprintf(stderr, arg); \ vfprintf(stderr, arg); \
} while (0) } while (0)

View File

@@ -9,8 +9,9 @@
(C) 2002 Nadia Yvette Chambers, IBM */ (C) 2002 Nadia Yvette Chambers, IBM */
#include <asm/bitsperlong.h> #include <asm/bitsperlong.h>
#include "bits.h"
#include "br.h" #include "br.h"
#include "bitops.h"
/* /*
* The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and

View File

@@ -7,8 +7,6 @@
#ifndef _BR_LIST_SORT_H #ifndef _BR_LIST_SORT_H
#define _BR_LIST_SORT_H #define _BR_LIST_SORT_H
//#include <linux/types.h>
struct list_head; struct list_head;
typedef int __attribute__((nonnull(2,3))) (*list_cmp_func_t)(void *, typedef int __attribute__((nonnull(2,3))) (*list_cmp_func_t)(void *,

View File

@@ -1,6 +1,6 @@
/* pjwhash-inline.h - PJW hash function, inline version. /* pjwhash-inline.h - PJW hash function, inline version.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -14,7 +14,7 @@
#ifndef _PJWHASH_INLINE_H #ifndef _PJWHASH_INLINE_H
#define _PJWHASH_INLINE_H #define _PJWHASH_INLINE_H
#include "bits.h" #include "br.h"
#define THREE_QUARTERS ((int) ((BITS_PER_INT * 3) / 4)) #define THREE_QUARTERS ((int) ((BITS_PER_INT * 3) / 4))
#define ONE_EIGHTH ((int) (BITS_PER_INT / 8)) #define ONE_EIGHTH ((int) (BITS_PER_INT / 8))

View File

@@ -1,6 +1,6 @@
/* pjwhash.h - PJW hash function, extern version. /* pjwhash.h - PJW hash function, extern version.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -13,7 +13,7 @@
#ifndef _PJWHASH_H #ifndef _PJWHASH_H
#define _PJWHASH_H #define _PJWHASH_H
#include "bits.h" #include "br.h"
/** /**
* unsigned int pjwhash - PJW hash function * unsigned int pjwhash - PJW hash function

View File

@@ -1,6 +1,6 @@
/* pool.h - A simple memory pool manager. /* pool.h - A simple memory pool manager.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -16,8 +16,9 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "br.h"
#include "list.h" #include "list.h"
#include "bits.h"
#define POOL_NAME_LENGTH (16) /* max name length including trailing \0 */ #define POOL_NAME_LENGTH (16) /* max name length including trailing \0 */

View File

@@ -1,6 +1,6 @@
/* struct-group.h - mirrored structure macros. /* struct-group.h - mirrored structure macros.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -33,10 +33,10 @@
* as both having struct attributes appended. * as both having struct attributes appended.
*/ */
#define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \ #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
union { \ union { \
struct { MEMBERS } ATTRS; \ struct { MEMBERS } ATTRS; \
struct TAG { MEMBERS } ATTRS NAME; \ struct TAG { MEMBERS } ATTRS NAME; \
} }
/** /**
* DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
@@ -48,11 +48,11 @@
* struct, it needs to be wrapped in an anonymous struct with at least 1 * struct, it needs to be wrapped in an anonymous struct with at least 1
* named member, but that member can be empty. * named member, but that member can be empty.
*/ */
#define DECLARE_FLEX_ARRAY(TYPE, NAME) \ #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
struct { \ struct { \
struct { } __empty_ ## NAME; \ struct { } __empty_ ## NAME; \
TYPE NAME[]; \ TYPE NAME[]; \
} }
/** /**
* struct_group() - Wrap a set of declarations in a mirrored struct * struct_group() - Wrap a set of declarations in a mirrored struct
@@ -67,7 +67,7 @@
* struct members. * struct members.
*/ */
#define struct_group(NAME, MEMBERS...) \ #define struct_group(NAME, MEMBERS...) \
__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS) __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
/** /**
* struct_group_attr() - Create a struct_group() with trailing attributes * struct_group_attr() - Create a struct_group() with trailing attributes
@@ -83,7 +83,7 @@
* struct members. Includes structure attributes argument. * struct members. Includes structure attributes argument.
*/ */
#define struct_group_attr(NAME, ATTRS, MEMBERS...) \ #define struct_group_attr(NAME, ATTRS, MEMBERS...) \
__struct_group(/* no tag */, NAME, ATTRS, MEMBERS) __struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
/** /**
* struct_group_tagged() - Create a struct_group with a reusable tag * struct_group_tagged() - Create a struct_group with a reusable tag
@@ -100,6 +100,6 @@
* so the specified layout can be reused later. * so the specified layout can be reused later.
*/ */
#define struct_group_tagged(TAG, NAME, MEMBERS...) \ #define struct_group_tagged(TAG, NAME, MEMBERS...) \
__struct_group(TAG, NAME, /* no attrs */, MEMBERS) __struct_group(TAG, NAME, /* no attrs */, MEMBERS)
#endif /* _STRUCT_GROUP_H */ #endif /* _STRUCT_GROUP_H */

View File

@@ -1,6 +1,6 @@
/* bits.c - information about bitops implementation. /* bitops.c - information about bitops implementation.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -11,7 +11,7 @@
* *
*/ */
#include "bits.h" #include "bitops.h"
#include "debug.h" #include "debug.h"
void print_bitops_impl(void) void print_bitops_impl(void)

View File

@@ -3,9 +3,9 @@
/* /*
* Taken from linux kernel: lib/list_sort.c * Taken from linux kernel: lib/list_sort.c
*/ */
#include "br.h"
#include "list_sort.h" #include "list_sort.h"
#include "list.h" #include "list.h"
#include "bits.h"
#include "likely.h" #include "likely.h"
/* /*

View File

@@ -1,6 +1,6 @@
/* pjwhash.c - PJW hash function. /* pjwhash.c - PJW hash function.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -13,8 +13,5 @@
#define _pjw_inline extern #define _pjw_inline extern
//#include "bits.h"
//extern unsigned int pjwhash (const void* key, uint length);
#include "pjwhash.h" #include "pjwhash.h"
#include "pjwhash-inline.h" #include "pjwhash-inline.h"

View File

@@ -1,6 +1,6 @@
/* pool.c - A simple pool manager. /* pool.c - A simple pool manager.
* *
* Copyright (C) 2021-2022 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -17,10 +17,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include "br.h"
#include "list.h" #include "list.h"
#include "pool.h" #include "pool.h"
#include "debug.h" #include "debug.h"
#include "bits.h"
void pool_stats(pool_t *pool) void pool_stats(pool_t *pool)
{ {