update lib from tools repo (some fixed for 32 bits)

This commit is contained in:
2022-12-07 13:20:48 +01:00
parent ca06a4a355
commit 81a58c6266
3 changed files with 67 additions and 70 deletions

View File

@@ -10,8 +10,8 @@
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html> * SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
* *
*/ */
#ifndef BITS_H #ifndef _BITS_H
#define BITS_H #define _BITS_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@@ -25,10 +25,10 @@
#endif #endif
/* no plan to support 32bits for now... /* no plan to support 32bits for now...
* #if __WORDSIZE != 64
* #error "Only 64 bits word size supported."
* #endif
*/ */
#if __WORDSIZE != 64
#error "Only 64 bits word size supported."
#endif
/* fixed-size types /* fixed-size types
*/ */
@@ -49,6 +49,51 @@ typedef unsigned int uint;
typedef unsigned short ushort; typedef unsigned short ushort;
typedef unsigned char uchar; typedef unsigned char uchar;
/* count set bits: 10101000 -> 3
* ^ ^ ^
*/
static __always_inline int popcount64(u64 n)
{
# if __has_builtin(__builtin_popcountl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcountl(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
static __always_inline int popcount32(u32 n)
{
# if __has_builtin(__builtin_popcount)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcount(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
/* char is a special case, as it can be signed or unsigned /* char is a special case, as it can be signed or unsigned
*/ */
typedef signed char schar; typedef signed char schar;
@@ -74,7 +119,7 @@ static __always_inline int ctz64(u64 n)
# ifdef DEBUG_BITS # ifdef DEBUG_BITS
log_f(1, "emulated.\n"); log_f(1, "emulated.\n");
# endif # endif
return popcount64((n & n) 1); return popcount64((n & -n) - 1);
# endif # endif
} }
@@ -96,7 +141,7 @@ static __always_inline int ctz32(u32 n)
# ifdef DEBUG_BITS # ifdef DEBUG_BITS
log_f(1, "emulated.\n"); log_f(1, "emulated.\n");
# endif # endif
return popcount32((n & n) 1); return popcount32((n & -n) - 1);
# endif # endif
} }
@@ -218,51 +263,6 @@ static __always_inline uint ffs32(u32 n)
# endif # endif
} }
/* count set bits: 10101000 -> 3
* ^ ^ ^
*/
static __always_inline int popcount64(u64 n)
{
# if __has_builtin(__builtin_popcountl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcountl(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
static __always_inline int popcount32(u32 n)
{
# if __has_builtin(__builtin_popcount)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcount(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0;
while (n) {
count++;
n &= (n - 1);
}
return count;
# endif
}
/* rolXX are taken from kernel's <linux/bitops.h> are are: /* rolXX are taken from kernel's <linux/bitops.h> are are:
* SPDX-License-Identifier: GPL-2.0 * SPDX-License-Identifier: GPL-2.0
*/ */
@@ -518,4 +518,4 @@ int __bits_per(unsigned long n)
#define bit_for_each32_2(pos, tmp, ul) \ #define bit_for_each32_2(pos, tmp, ul) \
for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp)) for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp))
#endif /* BITS_H */ #endif /* _BITS_H */

View File

@@ -19,12 +19,13 @@
#define DEBUG_DEBUG #define DEBUG_DEBUG
#endif #endif
#include "bits.h"
#include "debug.h" #include "debug.h"
#define NANOSEC 1000000000 /* nano sec in sec */ #define NANOSEC 1000000000 /* nano sec in sec */
#define MILLISEC 1000000 /* milli sec in sec */ #define MILLISEC 1000000 /* milli sec in sec */
static s64 timer_start; /* in nanosecond */ static long long timer_start; /* in nanosecond */
static u32 debug_level=0; static u32 debug_level=0;
void debug_level_set(u32 level) void debug_level_set(u32 level)
@@ -48,7 +49,7 @@ void debug_init(u32 level)
log(0, "timer started.\n"); log(0, "timer started.\n");
} }
inline static s64 timer_elapsed() inline static long long timer_elapsed()
{ {
struct timespec timer; struct timespec timer;
@@ -56,7 +57,6 @@ inline static s64 timer_elapsed()
return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start; return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start;
} }
/* void debug - log function /* void debug - log function
* @timestamp : boolean * @timestamp : boolean
* @indent : indent level (2 spaces each) * @indent : indent level (2 spaces each)
@@ -75,9 +75,9 @@ void debug(u32 level, bool timestamp, u32 indent, const char *src,
printf("%*s", 2*(indent-1), ""); printf("%*s", 2*(indent-1), "");
if (timestamp) { if (timestamp) {
s64 diff = timer_elapsed(); long long diff = timer_elapsed();
printf("%ld.%03ld ", diff/NANOSEC, (diff/1000000)%1000); printf("%lld.%03lld ", diff/NANOSEC, (diff/1000000)%1000);
printf("%010ld ", diff); printf("%010lld ", diff);
} }
if (src) { if (src) {

View File

@@ -27,9 +27,9 @@ void pool_stats(pool_t *pool)
if (pool) { if (pool) {
block_t *block; block_t *block;
log_f(1, "[%s] pool [%p]: blocks:%u avail:%u alloc:%u grow:%u eltsize:%lu\n", log_f(1, "[%s] pool [%p]: blocks:%u avail:%u alloc:%u grow:%u eltsize:%zu\n",
pool->name, (void *)pool, pool->nblocks, pool->available, pool->allocated, pool->name, (void *)pool, pool->nblocks, pool->available,
pool->growsize, pool->eltsize); pool->allocated, pool->growsize, pool->eltsize);
log(5, "\tblocks: "); log(5, "\tblocks: ");
list_for_each_entry(block, &pool->list_blocks, list_blocks) { list_for_each_entry(block, &pool->list_blocks, list_blocks) {
log(5, "%p ", block); log(5, "%p ", block);
@@ -43,14 +43,13 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
pool_t *pool; pool_t *pool;
# ifdef DEBUG_POOL # ifdef DEBUG_POOL
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n", log_f(1, "name=[%s] growsize=%u eltsize=%zu\n", name, growsize, eltsize);
name, growsize, eltsize);
# endif # endif
/* we need at least sizeof(struct list_head) space in pool elements /* we need at least sizeof(struct list_head) space in pool elements
*/ */
if (eltsize < sizeof (struct list_head)) { if (eltsize < sizeof (struct list_head)) {
# ifdef DEBUG_POOL # ifdef DEBUG_POOL
log_f(1, "[%s]: structure size too small (%lu < %lu), adjusting to %lu.\n", log_f(1, "[%s]: structure size too small (%zu < %zu), adjusting to %zu.\n",
name, eltsize, sizeof(struct list_head), sizeof(struct list_head)); name, eltsize, sizeof(struct list_head), sizeof(struct list_head));
# endif # endif
eltsize = sizeof(struct list_head); eltsize = sizeof(struct list_head);
@@ -74,11 +73,9 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
static u32 _pool_add(pool_t *pool, struct list_head *elt) static u32 _pool_add(pool_t *pool, struct list_head *elt)
{ {
# ifdef DEBUG_POOL # ifdef DEBUG_POOL
log_f(6, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n", log_f(6, "pool=%p &head=%p elt=%p off1=%zu off2=%zu\n",
(void *)pool, (void *)pool, (void *)&pool->list_available, (void *)elt,
(void *)&pool->list_available, (void *)&pool->list_available - (void *)pool,
(void *)elt,
(void *)&pool->list_available-(void *)pool,
offsetof(pool_t, list_available)); offsetof(pool_t, list_available));
# endif # endif