Compare commits

...

19 Commits

Author SHA1 Message Date
180839c960 bug.h revamp, easier to use 2024-07-15 08:18:35 +02:00
a48ebf9099 Makefile: add 'build=' option. bug.h: numeric {BUG,WARN}_ON 2024-07-10 07:34:05 +02:00
553dc6bd07 del useless unlikely() 2024-06-23 07:32:22 +02:00
7bedfddfba allow bug.h re-inclusion 2024-05-18 13:30:46 +02:00
8ff163dcf5 add stdlib in brlib.h (unexplained error in __abs_choose_expr) 2024-03-07 22:51:48 +01:00
9162db31e3 add -DBUG_ON -DWARN_ON to enable/disable bug_on()/warn_on() 2024-02-26 19:09:15 +01:00
24baf3524a switch to -O3 2024-02-23 05:47:23 +01:00
f9aec10459 add bswap32/bswap64 2024-02-13 19:37:18 +01:00
0e9b25dd68 warn(): vprintf -> fprintf 2024-02-07 22:13:10 +01:00
8cb08dacb3 fix bug() args 2024-02-07 19:29:49 +01:00
e907c81b8b lowercase bug() and warn() functions 2024-02-07 19:03:50 +01:00
b637146095 br.h -> brlib.h 2024-02-05 14:08:28 +01:00
635e4c8d9e improve README, remove some Makefile compile output 2024-02-05 07:51:05 +01:00
e55ac95301 fix (another) git URI 2024-01-13 15:57:55 +01:00
2262cc7393 fix git https:// URI 2024-01-13 14:28:45 +01:00
32fd923192 move cutest_bfe data to first set 2024-01-13 10:59:24 +01:00
a8a82a3121 Makefile: fix bin generation 2024-01-13 10:58:08 +01:00
5e88bad291 clearer install instructions 2024-01-13 10:57:22 +01:00
f29821ee94 add generic-ilog2.h (for reference) 2024-01-13 10:56:48 +01:00
22 changed files with 421 additions and 196 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@ compile_commands.json
core
/.ccls-cache/
/test/test/
.lastbuild
# /test/cutest/
/tmp/
# created when building

100
Makefile
View File

@@ -44,30 +44,88 @@ BIN := $(addprefix $(BINDIR)/,$(TEST_FN:.c=))
CCLSCMDS := compile_commands.json
##################################### Check for compiler and requested build
BUILDS := release dev perf debug
# last compilation build
BUILDFILE := .lastbuild
lastbuild := $(file < $(BUILDFILE))
$(info brlib last:$(lastbuild))
# default to gcc
CC ?= cc
ifeq ($(CC),cc)
CC = gcc
endif
# if no build specified, use last one
ifeq ($(build),)
build := $(lastbuild)
endif
# if build is still undefined, set a default
ifeq ($(build),)
build := release
endif
$(info brlib build=$(build))
# check for valid build
ifeq ($(filter $(build),$(BUILDS)),)
$(error Error: Unknown build=`$(build)`. Possible builds are: $(BUILDS))
endif
# if new build, rewrite BUILDFILE
ifneq ($(build),$(lastbuild))
$(info New build:`$(build)` (was:$(lastbuild)))
$(file >$(BUILDFILE),$(build))
endif
##################################### pre-processor flags
CPPFLAGS := -I $(INCDIR)
#CPPFLAGS += -DDEBUG # global
#CPPFLAGS += -DDEBUG_DEBUG_C # log() funcs debug
CPPFLAGS += -DDEBUG_DEBUG # activate logs funcs
CPPFLAGS += -DDEBUG_POOL # mem pools
override CPPFLAGS += -I $(INCDIR)
ifeq ($(BUILD),release)
CPPFLAGS += -DNDEBUG # assert (unused)
CPPFLAGS += -DBUG_ON=0 CPPFLAGS += -DWARN_ON=0 else # ifeq ($(BUILD),dev)
#CPPFLAGS += -DDEBUG #CPPFLAGS += -DDEBUG_DEBUG_C CPPFLAGS += -DDEBUG_DEBUG CPPFLAGS += -DDEBUG_POOL CPPFLAGS += -DBUG_ON=1 CPPFLAGS += -DWARN_ON=1 # warn_on in bug.h
endif
# remove extraneous spaces (due to spaces before comments)
CPPFLAGS := $(strip $(CPPFLAGS))
##################################### compiler flags
CFLAGS := -std=gnu11
CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
CFLAGS += -Wno-unused-result
# TODO: specific to dynamic
CFLAGS += -fPIC
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
### dev OR release
ifeq ($(BUILD),release)
CFLAGS += -O3
CFLAGS += -funroll-loops
CFLAGS += -flto
else ifeq ($(BUILD),dev)
CFLAGS += -Og
CFLAGS += -g
CFLAGS += -ginline-points # inlined funcs debug info
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (some invalid instructions)
# CFLAGS += -mno-tbm
else ifeq ($(BUILD),perf)
CFLAGS += -O3
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops
else ifeq ($(BUILD),debug)
CFLAGS += -O0
CFLAGS += -g # symbols (gdb, perf, etc.)
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
#CFLAGS += -mno-tbm
endif
CFLAGS := $(strip $(CFLAGS))
@@ -77,14 +135,21 @@ LDFLAGS := -L$(LIBDIR)
LIBS := -l$(LIB)
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d
ifeq ($(BUILD),release)
LDFLAGS += -flto
endif
##################################### General targets
.PHONY: all libs compile test emacs ccls bear clean cleanall cleanallall
.PHONY: all libs lib-static lib-dynamic compile test emacs ccls bear clean \
cleanall cleanallall
# default: build libraries
all: libs
# default: build libraries
libs: $(DLIB) $(SLIB)
# libraries
libs: lib-static lib-dynamic
lib-static: $(SLIB)
lib-dynamic: $(DLIB)
# build objects
compile: $(OBJ)
@@ -178,7 +243,8 @@ cleanobj:
cleanobjdir:
$(call rmdir,$(OBJDIR),brlib objects)
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
# $(OBJDIR)/%.o: $(BUILDFILE)
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(BUILDFILE) | $(OBJDIR) $(DEPDIR)
@echo compiling $< "->" $@.
$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
@@ -212,7 +278,7 @@ cleanbin:
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
$(BINDIR)/%: $(TESTDIR)/%.c libs | $(BINDIR)
$(BINDIR)/%: $(TESTDIR)/%.c $(SLIB) $(DLIB) | $(BINDIR)
$(CC) $(CPPFLAGS) $(CFLAGS) $< $(CUTESTSRC) $(LDFLAGS) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output
@@ -248,7 +314,7 @@ cleanccls:
# maybe run cleanobj cleanlibobj in commands ?
$(CCLSCMDS): cleanobj $(SRC) | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- make test
@$(BEAR) -- $(MAKE) test
##################################### valgrind (mem check)
.PHONY: memcheck

View File

@@ -1,35 +1,43 @@
#+title: brlib - A small personal C library
#+OPTIONS: toc:nil
#+OPTIONS: num:2
#+startup: num
** License
~SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>~~
This work is, with exceptions below, Copyright (C) 2021-2024 Bruno Raoult
("br"), and licensed under the GNU General Public License v3.0 or later.
Some rights reserved. See COPYING.
*The licence exceptions are:**
_Cutest testing framework.__
You can find the original work on
[[https://sourceforge.net/projects/cutest/files/cutest/][sourceforge]].
*** The licence exceptions are:*
**** _Cutest testing framework.__
You can find the original work on [[https://sourceforge.net/projects/cutest/files/cutest/][sourceforge]].
This software is (C) 2000-2003 Asim Jalis, under the zlib License.
See [[test/cutest/license.txt][license local copy]] or
<https://spdx.org/licenses/Zlib.html>.
See [[test/cutest/license.txt][license local copy]] or <https://spdx.org/licenses/Zlib.html>.
** Installation:
*** user...
*** clone repository
**** user...
#+BEGIN_EXAMPLE
$ git clone https://git.raoult.com:bruno/brlib.git
or
$ git clone https://github.com/braoult/brlib.git
or
$ git clone https://git.raoult.com/bruno/brlib.git
#+END_EXAMPLE
*** ...or developer
**** OR ...developer
#+BEGIN_EXAMPLE
$ git clone git@git.raoult.com:bruno/brlib.git
$ cd brlib
$ git remote add github git@github.com:braoult/brlib.git
$ git fetch --all
#+END_EXAMPLE
*Add CuTest submodule (optional)*
#+BEGIN_EXAMPLE
$ cd brlib
$ git submodule init
$ git submodule update
#+END_EXAMPLE

View File

@@ -0,0 +1,38 @@
/* generic-bswap.h - generic bswap implementations.
*
* Copyright (C) 2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef _GENERIC_BSWAP_H_
#define _GENERIC_BSWAP_H_
#include "brlib.h"
/* Adapted from: http://www-graphics.stanford.edu/%7Eseander/bithacks.html
*/
static __always_inline u32 __bswap32_emulated(u32 n)
{
const u32 k = 0x00FF00FF;
n = ((n >> 8) & k) | ((n & k) << 8);
n = ( n >> 16) | ( n << 16);
return n;
}
static __always_inline u64 __bswap64_emulated(u64 n)
{
const u64 k1 = 0x00FF00FF00FF00FFull;
const u64 k2 = 0x0000FFFF0000FFFFull;
n = ((n >> 8) & k1) | ((n & k1) << 8);
n = ((n >> 16) & k2) | ((n & k2) << 16);
n = ( n >> 32) | ( n << 32);
return n;
}
#endif /* _GENERIC_BSWAP_H_ */

View File

@@ -13,7 +13,7 @@
#ifndef _GENERIC_CLZ_H_
#define _GENERIC_CLZ_H_
#include "br.h"
#include "brlib.h"
/* Adapted from: http://www-graphics.stanford.edu/%7Eseander/bithacks.html
*/

View File

@@ -13,7 +13,7 @@
#ifndef _GENERIC_CTZ_H_
#define _GENERIC_CTZ_H_
#include "br.h"
#include "brlib.h"
/* Adapted from: http://www-graphics.stanford.edu/%7Eseander/bithacks.html
*/

View File

@@ -0,0 +1,60 @@
/* generic-ilog2.h - generic ilog2 implementations.
*
* Copyright (C) 2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef _GENERIC_ILOG2_H_
#define _GENERIC_ILOG2_H_
#include "brlib.h"
/*
* See https://stackoverflow.com/a/11398748/3079831
*/
static const int __ilog2_32_table[32] = {
0, 9, 1, 10, 13, 21, 2, 29,
11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7,
19, 27, 23, 6, 26, 5, 4, 31
};
static const int __ilog2_64_table[64] = {
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
static __always_inline __const int __ilog2_32_emulated (u32 n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return __ilog2_32_table[(n*0x07C4ACDD) >> 27];
}
static __always_inline __const int __ilog2_64_emulated (u64 n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n |= n >> 32;
return __ilog2_64_table[((n - (n >> 1))*0x07EDD5E59A4E28C2LL) >> 58];
}
#endif /* _GENERIC_ILOG2_H_ */

View File

@@ -13,9 +13,10 @@
#ifndef _BITS_H
#define _BITS_H
#include "br.h"
#include "brlib.h"
#include "bitops-emulated/generic-ctz.h"
#include "bitops-emulated/generic-clz.h"
#include "bitops-emulated/generic-bswap.h"
#ifndef __has_builtin
#define __has_builtin(x) 0
@@ -35,7 +36,9 @@
#if __has_builtin(__builtin_ffs)
# define HAS_FFS
#endif
#if __has_builtin(__builtin_bswap32)
# define HAS_BSWAP
#endif
/**
* print_bitops_impl() - print bitops implementation.
@@ -174,6 +177,24 @@ void print_bitops_impl(void);
#define ffz32(n) ffs32(~(n))
#define ffz64(n) ffs64(~(n))
/**
* bswap32, bswap64 - reverse bytes: 0x01020304 -> 0x04030201
* @n: unsigned 32 or 64 bits integer.
*
* ffs(n) is similar to ctz(n) + 1, but returns 0 if n == 0 (except
* for ctz version, where ffs(0) is undefined).
* ffz(n) is ffz(~n), with undefine value if n = 0.
*/
#if defined(HAS_BSWAP)
# define __bswap32_native(n) __builtin_bswap32(n)
# define __bswap64_native(n) __builtin_bswap64(n)
# define bswap32(n) __bswap32_native(n)
# define bswap64(n) __bswap64_native(n)
#else
# define bswap32(n) __bswap32_emulated(n)
# define bswap64(n) __bswap64_emulated(n)
#endif
/**
* fls32, fls64 - return one plus MSB index: 00101000 -> 6
* @n: unsigned 32 or 64 bits integer.

View File

@@ -1,4 +1,4 @@
/* br.h - misc macros.
/* brlib.h - misc types/macros.
*
* Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
@@ -15,9 +15,10 @@
* This header contains generic stuff.
*/
#ifndef _BR_H
#define _BR_H
#ifndef _BRLIB_H
#define _BRLIB_H
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <bits/wordsize.h> /* defines __WORDSIZE: 32 or 64 */
@@ -267,4 +268,4 @@ typedef signed char schar;
({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
#endif /* _BR_H */
#endif /* _BRLIB_H */

View File

@@ -1,68 +1,61 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BR_BUG_H
#define _BR_BUG_H
/* bug.h - bug_on/warn_on/warn functions.
*
* Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef BUG_H
#define BUG_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include "likely.h"
#include "debug.h"
/* BUG functions inspired by Linux kernel's <asm/bug.h>
*/
#define panic() exit(0xff)
/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
*/
#define BUG() do { \
fprintf(stderr, "BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
panic(); \
#define bug_on_always(expr) do { \
if (expr) { \
fprintf(stderr, \
"** BUG ON %s[%s:%d]: assertion '%s' failed.\n", \
__func__, __FILE__,__LINE__, #expr); \
abort(); \
/* not reached */ \
} \
} 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 significant
* issues that need prompt attention if they should ever appear at runtime.
*
* Do not use these macros when checking for invalid external inputs
* (e.g. invalid system call arguments, or invalid data coming from
* network/devices), and on transient conditions like ENOMEM or EAGAIN.
* These macros should be used for recoverable kernel issues only.
* For invalid external inputs, transient conditions, etc use
* pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
* Do not include "BUG"/"WARNING" in format strings manually to make these
* conditions distinguishable from kernel issues.
*
* Use the versions with printk format strings to provide better diagnostics.
*/
#define __WARN() do { \
fprintf(stderr, "WARNING: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
} while (0)
#define __WARN_printf(arg...) do { \
vfprintf(stderr, arg); \
} while (0)
#define WARN_ON(condition) ({ \
int __ret_warn_on = !!(condition); \
if (unlikely(__ret_warn_on)) \
__WARN(); \
unlikely(__ret_warn_on); \
#define warn(expr, args...) ({ \
int _ret = !!(expr); \
if (_ret) \
fprintf(stderr, ##args); \
_ret; \
})
#define WARN(condition, format...) ({ \
int __ret_warn_on = !!(condition); \
if (unlikely(__ret_warn_on)) \
__WARN_printf(format); \
unlikely(__ret_warn_on); \
#define warn_on_always(expr) ({ \
warn(expr, \
"** WARN ON %s[%s:%d]: assertion '%s' failed.\n", \
__func__, __FILE__,__LINE__, #expr); \
})
#endif /* _BR_BUG_H */
#ifdef BUG_ON
#define bug_on(expr) bug_on_always(expr)
#define warn_on(expr) warn_on_always(expr)
#define warn_on_or_eval(expr) warn_on(expr)
#else
#define bug_on(expr)
#define warn_on(expr)
#define warn_on_or_eval(expr) (expr)
#endif /* BUG_ON */
#endif /* BUG_H */

View File

@@ -17,7 +17,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <br.h>
#include "brlib.h"
#define NANOSEC 1000000000 /* nano sec in sec */
#define MILLISEC 1000000 /* milli sec in sec */

View File

@@ -10,7 +10,7 @@
#include <asm/bitsperlong.h>
#include "br.h"
#include "brlib.h"
#include "bitops.h"
/*

View File

@@ -14,7 +14,7 @@
#ifndef _PJWHASH_INLINE_H
#define _PJWHASH_INLINE_H
#include "br.h"
#include "brlib.h"
#define THREE_QUARTERS ((int) ((BITS_PER_INT * 3) / 4))
#define ONE_EIGHTH ((int) (BITS_PER_INT / 8))

View File

@@ -13,7 +13,7 @@
#ifndef _PJWHASH_H
#define _PJWHASH_H
#include "br.h"
#include "brlib.h"
/**
* unsigned int pjwhash - PJW hash function

View File

@@ -279,7 +279,7 @@ static inline int plist_node_empty(const struct plist_node *node)
#ifdef CONFIG_DEBUG_PLIST
# define plist_first_entry(head, type, member) \
({ \
WARN_ON(plist_head_empty(head)); \
warn_on(plist_head_empty(head)); \
container_of(plist_first(head), type, member); \
})
#else
@@ -296,7 +296,7 @@ static inline int plist_node_empty(const struct plist_node *node)
#ifdef CONFIG_DEBUG_PLIST
# define plist_last_entry(head, type, member) \
({ \
WARN_ON(plist_head_empty(head)); \
warn_on(plist_head_empty(head)); \
container_of(plist_last(head), type, member); \
})
#else

View File

@@ -17,7 +17,7 @@
#include <stdint.h>
#include <stddef.h>
#include "br.h"
#include "brlib.h"
#include "list.h"
#define POOL_NAME_LENGTH (16) /* max name length including trailing \0 */

View File

@@ -18,13 +18,15 @@
if [[ ! -v _BRLIB_ENV_ ]]; then
export _BRLIB_ENV_=1 BRLIB_ROOT LD_LIBRARY_PATH
BRLIB_SCRIPTDIR=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")")
BRLIB_ROOT=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")/..")
BRLIB_LIBDIR="$BRLIB_ROOT/lib"
BRLIB_SCRIPTDIR="$BRLIB_ROOT/scripts"
BRLIB_BINDIR="$BRLIB_ROOT/bin"
LD_LIBRARY_PATH="${BRLIB_LIBDIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
PATH="$PATH:$BRLIB_BINDIR:$BRLIB_SCRIPTDIR"
BRLIB_LIBDIR="$BRLIB_ROOT/lib"
LD_LIBRARY_PATH="${BRLIB_LIBDIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
#printf "R=%s L=%s LD=%s\n" "$BRLIB_ROOT" "$BRLIB_DIR" "$LD_LIBRARY_PATH"
unset BRLIB_LIBDIR BRLIB_BINDIR BRLIB_SCRIPTDIR
unset BRLIB_SCRIPTDIR BRLIB_BINDIR BRLIB_LIBDIR
printf "brlib environment complete.\n"
fi

View File

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

View File

@@ -32,7 +32,7 @@ static struct plist_head test_head;
static void plist_check_prev_next(struct list_head *t, struct list_head *p,
struct list_head *n)
{
WARN(n->prev != p || p->next != n,
warn(n->prev != p || p->next != n,
"top: %p, n: %p, p: %p\n"
"prev: %p, n: %p, p: %p\n"
"next: %p, n: %p, p: %p\n",
@@ -76,8 +76,8 @@ void plist_add(struct plist_node *node, struct plist_head *head)
struct list_head *node_next = &head->node_list;
plist_check_head(head);
WARN_ON(!plist_node_empty(node));
WARN_ON(!list_empty(&node->prio_list));
warn_on(!plist_node_empty(node));
warn_on(!list_empty(&node->prio_list));
if (plist_head_empty(head))
goto ins_node;
@@ -148,8 +148,8 @@ void plist_requeue(struct plist_node *node, struct plist_head *head)
struct list_head *node_next = &head->node_list;
plist_check_head(head);
BUG_ON(plist_head_empty(head));
BUG_ON(plist_node_empty(node));
bug_on(plist_head_empty(head));
bug_on(plist_node_empty(node));
if (node == plist_last(head))
return;

View File

@@ -17,7 +17,7 @@
#include <stdlib.h>
#include <errno.h>
#include "br.h"
#include "brlib.h"
#include "list.h"
#include "pool.h"
#include "debug.h"

View File

@@ -14,12 +14,13 @@
#include <stdio.h>
#include <stdlib.h>
#include "br.h"
#include "brlib.h"
#include "bitops.h"
#include "cutest/CuTest.h"
static const struct test32_1 {
u32 t32; /* input */
u32 bswap;
int popc;
int ctz;
int clz;
@@ -27,31 +28,65 @@ static const struct test32_1 {
int ffz;
int fls;
int ilog2;
struct {
int size;
uchar bfe[32];
};
} test32_1[] = {
{ 0x00000000, 0, 32, 32, 0, 1, 0, 0 }, /* C undefined for some values */
{ 0xffffffff, 32, 0, 0, 1, 0, 32, 31 },
{ 0x00000001, 1, 0, 31, 1, 2, 1, 0 },
{ 0x80000000, 1, 31, 0, 32, 1, 32, 31 },
{ 0x71800718, 10, 3, 1, 4, 1, 31, 30 },
{ 0x07eeeef7, 22, 0, 5, 1, 4, 27, 26 },
{ 0x00000000, 0x00000000, 0, 32, 32, 0, 1, 0, 0, /* sometimes undefined */
{ 0, { 0 } } },
{ 0xffffffff, 0xffffffff, 32, 0, 0, 1, 0, 32, 31,
{ 32, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 } } },
{ 0x00000001, 0x01000000, 1, 0, 31, 1, 2, 1, 0,
{ 1, { 0 } } },
{ 0x80000000, 0x00000080, 1, 31, 0, 32, 1, 32, 31,
{ 1, { 31 } } },
{ 0x71800718, 0x18078071, 10, 3, 1, 4, 1, 31, 30,
/* 0111 0001 1000 0000 0000 0111 0001 1000 */
{ 10, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30 } } },
{ 0x07eeeef7, 0xf7eeee07, 22, 0, 5, 1, 4, 27, 26,
/* 0000 0111 1110 1110 1110 1110 0111 1111*/
{ 22, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13,
14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26 } } },
};
static const struct test64_1 {
u64 t64; /* input */
int popc;
u64 bswap;
int popc; /* some values may be undefined */
int ctz;
int clz;
int ffs;
int ffz;
int fls;
int ilog2;
struct {
int size;
uchar bfe[64];
};
} test64_1[] = {
{ 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0 }, /* undefined for some values */
{ 0xffffffffffffffff, 64, 0, 0, 1, 0, 64, 63 },
{ 0x0000000100000001, 2, 0, 31, 1, 2, 33, 32 },
{ 0x8000000000000000, 1, 63, 0, 64, 1, 64, 63 },
{ 0x7180071871800718, 20, 3, 1, 4, 1, 63, 62 },
{ 0x07eeeef707eeeef7, 44, 0, 5, 1, 4, 59, 58 },
{ 0x0000000000000000, 0x0000000000000000, 0, 64, 64, 0, 1, 0, 0,
{ 0, { 0 } } },
{ 0xffffffffffffffff, 0xffffffffffffffff, 64, 0, 0, 1, 0, 64, 63,
{ 64, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 } } },
{ 0x0000000100000001, 0x0100000001000000, 2, 0, 31, 1, 2, 33, 32,
{ 2, { 0, 32 } } },
{ 0x8000000000000000, 0x0000000000000080, 1, 63, 0, 64, 1, 64, 63,
{ 1, { 63 } } },
{ 0x7180071871800718, 0x1807807118078071, 20, 3, 1, 4, 1, 63, 62,
/* 2 x 0111 0001 1000 0000 0000 0111 0001 1000 */
{ 20, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30,
35, 36, 40, 41, 42, 55, 56, 60, 61, 62 } } },
{ 0x07eeeef707eeeef7, 0xf7eeee07f7eeee07, 44, 0, 5, 1, 4, 59, 58 ,
/* 2 x 0000 0111 1110 1110 1110 1110 0111 1111*/
{ 44, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13,
14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26,
32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 45,
46, 47, 49, 50, 51, 53, 54, 55, 56, 57, 58 } } },
};
static void cutest_popcount(CuTest *tc)
@@ -134,6 +169,20 @@ static void cutest_fls(CuTest *tc)
}
}
static void cutest_bswap(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
int res = bswap32(test32_1[i].t32);
//printf("fls32 t=%#x r=%d e=%d\n", test32_1[i].t32, res, test32_1[i].fls);
CuAssertIntEquals(tc, test32_1[i].bswap, res);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
int res = bswap64(test64_1[i].t64);
//printf("fls64 t=%#llx r=%d e=%d\n", test64_1[i].t64, res, test64_1[i].fls);
CuAssertIntEquals(tc, test64_1[i].bswap, res);
}
}
static void cutest_ilog(CuTest *tc)
{
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
@@ -158,6 +207,54 @@ static void cutest_ilog(CuTest *tc)
}
}
static void cutest_bfe(CuTest *tc)
{
char s[64];
for (uint i = 0; i < ARRAY_SIZE(test32_1); ++i) {
u32 tmp;
int cur, nb = 0;
char bfe_ffs[32];
/* create bit_for_each32_ffs values */
for (int j = 0; j < test32_1[i].size; ++j)
bfe_ffs[j] = test32_1[i].bfe[j] + 1;
bit_for_each32(cur, tmp, test32_1[i].t32) {
s[nb++] = cur;
}
CuAssertIntEquals(tc, test32_1[i].size, nb);
CuAssertMemEquals(tc, test32_1[i].bfe, s, test32_1[i].size);
nb = 0;
bit_for_each32_ffs(cur, tmp, test32_1[i].t32) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test32_1[i].size);
}
for (uint i = 0; i < ARRAY_SIZE(test64_1); ++i) {
u64 tmp;
int cur, nb = 0;
char bfe_ffs[64];
/* create bit_for_each64_ffs values */
for (int j = 0; j < test64_1[i].size; ++j)
bfe_ffs[j] = test64_1[i].bfe[j] + 1;
bit_for_each64(cur, tmp, test64_1[i].t64) {
s[nb++] = cur;
}
//printf("\n64 nb=%d size=%d\n", nb, test64_1[i].size);
CuAssertIntEquals(tc, test64_1[i].size, nb);
CuAssertMemEquals(tc, test64_1[i].bfe, s, test64_1[i].size);
nb = 0;
bit_for_each64_ffs(cur, tmp, test64_1[i].t64) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test64_1[i].size);
}
}
struct test32_2 {
u32 t32; /* input */
u32 arg;
@@ -189,28 +286,13 @@ struct test64_2 {
u32 arg;
u64 rol;
u64 ror;
struct {
int size;
uchar bfe[64];
};
} test64_2[] = {
{ 0x0000000000000000, 0, 0, 0,
{ 0, { 0 } } },
{ 0x0000000000000000, 1, 0, 0,
{ 0, { 0 } } },
{ 0x1000000110000001, 2, 0x4000000440000004, 0x4400000044000000,
{ 4, { 0, 28, 32, 60 } } },
{ 0x8000000880000008, 3, 0x0000004400000044, 0x1000000110000001,
{ 4, { 3, 31, 35, 63 } } },
{ 0x7180071871800718, 8, 0x8007187180071871, 0x1871800718718007,
{ 20, { 3, 4, 8, 9, 10, 23, 24, 28, 29, 30,
35, 36, 40, 41, 42, 55, 56, 60, 61, 62 } } },
{ 0x07eeeef707eeeef7, 4, 0x7eeeef707eeeef70, 0x707eeeef707eeeef,
{ 44, { 0, 1, 2, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19,
21, 22, 23, 24, 25, 26,
32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 45, 46, 47, 49, 50, 51,
53, 54, 55, 56, 57, 58 } } },
{ 0x0000000000000000, 0, 0, 0 },
{ 0x0000000000000000, 1, 0, 0 },
{ 0x1000000110000001, 2, 0x4000000440000004, 0x4400000044000000 },
{ 0x8000000880000008, 3, 0x0000004400000044, 0x1000000110000001 },
{ 0x7180071871800718, 8, 0x8007187180071871, 0x1871800718718007 },
{ 0x07eeeef707eeeef7, 4, 0x7eeeef707eeeef70, 0x707eeeef707eeeef }
};
static void cutest_rol(CuTest *tc)
@@ -245,54 +327,6 @@ static void cutest_ror(CuTest *tc)
}
}
static void cutest_bfe(CuTest *tc)
{
char s[64];
for (uint i = 0; i < ARRAY_SIZE(test32_2); ++i) {
u32 tmp;
int cur, nb = 0;
char bfe_ffs[32];
/* create bit_for_each32_ffs values */
for (int j = 0; j < test32_2[i].size; ++j)
bfe_ffs[j] = test32_2[i].bfe[j] + 1;
bit_for_each32(cur, tmp, test32_2[i].t32) {
s[nb++] = cur;
}
CuAssertIntEquals(tc, test32_2[i].size, nb);
CuAssertMemEquals(tc, test32_2[i].bfe, s, test32_2[i].size);
nb = 0;
bit_for_each32_ffs(cur, tmp, test32_2[i].t32) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test32_2[i].size);
}
for (uint i = 0; i < ARRAY_SIZE(test64_2); ++i) {
u64 tmp;
int cur, nb = 0;
char bfe_ffs[64];
/* create bit_for_each64_ffs values */
for (int j = 0; j < test64_2[i].size; ++j)
bfe_ffs[j] = test64_2[i].bfe[j] + 1;
bit_for_each64(cur, tmp, test64_2[i].t64) {
s[nb++] = cur;
}
//printf("\n64 nb=%d size=%d\n", nb, test64_2[i].size);
CuAssertIntEquals(tc, test64_2[i].size, nb);
CuAssertMemEquals(tc, test64_2[i].bfe, s, test64_2[i].size);
nb = 0;
bit_for_each64_ffs(cur, tmp, test64_2[i].t64) {
s[nb++] = cur;
}
CuAssertMemEquals(tc, bfe_ffs, s, test64_2[i].size);
}
}
static CuSuite *bitops_GetSuite()
{
CuSuite* suite = CuSuiteNew();
@@ -303,10 +337,11 @@ static CuSuite *bitops_GetSuite()
SUITE_ADD_TEST(suite, cutest_ffz);
SUITE_ADD_TEST(suite, cutest_fls);
SUITE_ADD_TEST(suite, cutest_ilog);
SUITE_ADD_TEST(suite, cutest_bfe);
SUITE_ADD_TEST(suite, cutest_bswap);
SUITE_ADD_TEST(suite, cutest_rol);
SUITE_ADD_TEST(suite, cutest_ror);
SUITE_ADD_TEST(suite, cutest_bfe);
return suite;
}

View File

@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "br.h"
#include "brlib.h"
#include "pool.h"
#include "debug.h"