3 Commits

4 changed files with 122 additions and 162 deletions

View File

@@ -37,7 +37,8 @@ DLIB := $(addsuffix .so, $(LIBDIR)/lib$(LIB)) # dynamic lib
BIN := fen piece move eval brchess
LIBS := -l$(LIB) -lreadline -lncurses
LIBSEXT := -lreadline -lncurses
LIBS := -l$(LIB) $(LIBSEXT)
CFLAGS := -std=gnu11
@@ -174,10 +175,10 @@ cleanbin:
$(RM) -f $(BIN) core
# TODO: find a better dependancy graph
$(BIN): $(SRCDIR)/$$@.c $(DLIB) $$(subst $(OBJDIR)/$$@.o,,$(OBJ))
$(BIN): $(SRCDIR)/$$@.c libs $$(subst $(OBJDIR)/$$@.o,,$(OBJ))
@[[ -f $(BINMARK) ]] || echo -n "generating binaries: "
@echo -n "$@... "
@$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) -I $(INCDIR) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@
@$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@
@$(TOUCH) $(BINMARK)
##################################### ccls

View File

@@ -15,10 +15,9 @@
#include <stdint.h>
#include <stdbool.h>
#include <bits/wordsize.h> /* defines __WORDSIZE: 32 or 64 */
/* next include will define __WORDSIZE: 32 or 64
*/
#include <bits/wordsize.h>
void bits_implementation(void);
#ifndef __has_builtin
#define __has_builtin(x) 0
@@ -77,15 +76,9 @@ typedef signed char schar;
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++;
@@ -98,15 +91,9 @@ static __always_inline int popcount64(u64 n)
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++;
@@ -122,21 +109,12 @@ static __always_inline int popcount32(u32 n)
static __always_inline int ctz64(u64 n)
{
# if __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin clzl.\n");
# endif
return __WORDSIZE - (__builtin_clzl(n & -n) + 1);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64((n & -n) - 1);
# endif
}
@@ -144,21 +122,12 @@ static __always_inline int ctz64(u64 n)
static __always_inline int ctz32(u32 n)
{
# if __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin clz.\n");
# endif
return __WORDSIZE - (__builtin_clz(n & -n) + 1);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32((n & -n) - 1);
# endif
}
@@ -169,15 +138,9 @@ static __always_inline int ctz32(u32 n)
static __always_inline int clz64(u64 n)
{
# if __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clzl(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u64 r, q;
r = (n > 0xFFFFFFFF) << 5; n >>= r;
@@ -193,15 +156,9 @@ static __always_inline int clz64(u64 n)
static __always_inline int clz32(u32 n)
{
# if __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clz(n);
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u32 r, q;
r = (n > 0xFFFF) << 4; n >>= r;
@@ -236,23 +193,14 @@ static __always_inline int fls32(u32 n)
static __always_inline uint ffs64(u64 n)
{
# if __has_builtin(__builtin_ffsl)
# ifdef DEBUG_BITS
log_f(1, "builtin ffsl.\n");
# endif
return __builtin_ffsl(n);
# elif __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
if (n == 0)
return (0);
return __builtin_ctzl(n) + 1;
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64(n ^ ~-n);
# endif
}
@@ -260,28 +208,19 @@ static __always_inline uint ffs64(u64 n)
static __always_inline uint ffs32(u32 n)
{
# if __has_builtin(__builtin_ffs)
# ifdef DEBUG_BITS
log_f(1, "builtin ffs.\n");
# endif
return __builtin_ffs(n);
# elif __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
if (n == 0)
return (0);
return __builtin_ctz(n) + 1;
# else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32(n ^ ~-n);
# endif
}
/* rolXX are taken from kernel's <linux/bitops.h> are are:
/* rolXX/rorXX are taken from kernel's <linux/bitops.h> are are:
* SPDX-License-Identifier: GPL-2.0
*/
@@ -292,7 +231,7 @@ static __always_inline uint ffs32(u32 n)
*/
static inline u64 rol64(u64 word, unsigned int shift)
{
return (word << (shift & 63)) | (word >> ((-shift) & 63));
return (word << (shift & 63)) | (word >> ((-shift) & 63));
}
/**
@@ -302,7 +241,7 @@ static inline u64 rol64(u64 word, unsigned int shift)
*/
static inline u64 ror64(u64 word, unsigned int shift)
{
return (word >> (shift & 63)) | (word << ((-shift) & 63));
return (word >> (shift & 63)) | (word << ((-shift) & 63));
}
/**
@@ -312,7 +251,7 @@ static inline u64 ror64(u64 word, unsigned int shift)
*/
static inline u32 rol32(u32 word, unsigned int shift)
{
return (word << (shift & 31)) | (word >> ((-shift) & 31));
return (word << (shift & 31)) | (word >> ((-shift) & 31));
}
/**
@@ -322,7 +261,7 @@ static inline u32 rol32(u32 word, unsigned int shift)
*/
static inline u32 ror32(u32 word, unsigned int shift)
{
return (word >> (shift & 31)) | (word << ((-shift) & 31));
return (word >> (shift & 31)) | (word << ((-shift) & 31));
}
/**
@@ -332,7 +271,7 @@ static inline u32 ror32(u32 word, unsigned int shift)
*/
static inline u16 rol16(u16 word, unsigned int shift)
{
return (word << (shift & 15)) | (word >> ((-shift) & 15));
return (word << (shift & 15)) | (word >> ((-shift) & 15));
}
/**
@@ -342,7 +281,7 @@ static inline u16 rol16(u16 word, unsigned int shift)
*/
static inline u16 ror16(u16 word, unsigned int shift)
{
return (word >> (shift & 15)) | (word << ((-shift) & 15));
return (word >> (shift & 15)) | (word << ((-shift) & 15));
}
/**
@@ -352,7 +291,7 @@ static inline u16 ror16(u16 word, unsigned int shift)
*/
static inline u8 rol8(u8 word, unsigned int shift)
{
return (word << (shift & 7)) | (word >> ((-shift) & 7));
return (word << (shift & 7)) | (word >> ((-shift) & 7));
}
/**
@@ -362,22 +301,25 @@ static inline u8 rol8(u8 word, unsigned int shift)
*/
static inline u8 ror8(u8 word, unsigned int shift)
{
return (word >> (shift & 7)) | (word << ((-shift) & 7));
return (word >> (shift & 7)) | (word << ((-shift) & 7));
}
/**
* ilog2 -
* __ilog2 - non-constant log of base 2 calculators
* - the arch may override these in asm/bitops.h if they can be implemented
* more efficiently than using fls() and fls64()
* - the arch is not required to handle n==0 if implementing the fallback
*/
static __always_inline __attribute__((const))
int __ilog2_u32(u32 n)
{
return fls32(n) - 1;
}
static __always_inline __attribute__((const))
int __ilog2_u64(u64 n)
{
return fls64(n) - 1;
return fls64(n) - 1;
}
static __always_inline __attribute__((const))
int __ilog2_u32(u32 n)
{
return fls32(n) - 1;
}
/**
@@ -391,7 +333,26 @@ int __ilog2_u64(u64 n)
static inline __attribute__((const))
bool is_power_of_2(unsigned long n)
{
return (n != 0 && ((n & (n - 1)) == 0));
return (n != 0 && ((n & (n - 1)) == 0));
}
/**
* __roundup_pow_of_two() - round up to nearest power of two
* @n: value to round up
*/
static inline __attribute__((const))
u64 __roundup_pow_of_two(u64 n)
{
return 1UL << fls64(n - 1);
}
/**
* __rounddown_pow_of_two() - round down to nearest power of two
* @n: value to round down
*/
static inline __attribute__((const)) u64 __rounddown_pow_of_two(u64 n)
{
return 1UL << (fls64(n) - 1);
}
/**
@@ -404,14 +365,14 @@ bool is_power_of_2(unsigned long n)
*
* selects the appropriately-sized optimised version depending on sizeof(n)
*/
#define ilog2(n) \
( \
__builtin_constant_p(n) ? \
((n) < 2 ? 0 : \
63 - __builtin_clzll(n)) : \
(sizeof(n) <= 4) ? \
__ilog2_u32(n) : \
__ilog2_u64(n) \
#define ilog2(n) \
( \
__builtin_constant_p(n) ? \
((n) < 2 ? 0 : \
63 - __builtin_clzll(n)) : \
(sizeof(n) <= 4) ? \
__ilog2_u32(n) : \
__ilog2_u64(n) \
)
/**
@@ -422,13 +383,13 @@ bool is_power_of_2(unsigned long n)
* - the result is undefined when n == 0
* - this can be used to initialise global variables from constant data
*/
#define roundup_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 1) ? 1 : \
(1UL << (ilog2((n) - 1) + 1)) \
) : \
__roundup_pow_of_two(n) \
#define roundup_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 1) ? 1 : \
(1UL << (ilog2((n) - 1) + 1)) \
) : \
__roundup_pow_of_two(n) \
)
/**
@@ -439,17 +400,16 @@ bool is_power_of_2(unsigned long n)
* - the result is undefined when n == 0
* - this can be used to initialise global variables from constant data
*/
#define rounddown_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(1UL << ilog2(n))) : \
__rounddown_pow_of_two(n) \
#define rounddown_pow_of_two(n) \
( \
__builtin_constant_p(n) ? ( \
(1UL << ilog2(n))) : \
__rounddown_pow_of_two(n) \
)
static inline __attribute_const__
int __order_base_2(unsigned long n)
static inline __attribute_const__ int __order_base_2(unsigned long n)
{
return n > 1 ? ilog2(n - 1) + 1 : 0;
return n > 1 ? ilog2(n - 1) + 1 : 0;
}
/**
@@ -465,22 +425,22 @@ int __order_base_2(unsigned long n)
* ob2(5) = 3
* ... and so on.
*/
#define order_base_2(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) ? 0 : \
ilog2((n) - 1) + 1) : \
__order_base_2(n) \
#define order_base_2(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) ? \
0 : \
ilog2((n) - 1) + 1) : \
__order_base_2(n) \
)
static inline __attribute__((const))
int __bits_per(unsigned long n)
static inline __attribute__((const)) int __bits_per(unsigned long n)
{
if (n < 2)
return 1;
if (is_power_of_2(n))
return order_base_2(n) + 1;
return order_base_2(n);
if (n < 2)
return 1;
if (is_power_of_2(n))
return order_base_2(n) + 1;
return order_base_2(n);
}
/**
@@ -498,13 +458,13 @@ int __bits_per(unsigned long n)
* bf(4) = 3
* ... and so on.
*/
#define bits_per(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) \
? 1 : ilog2(n) + 1 \
) : \
__bits_per(n) \
#define bits_per(n) \
( \
__builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) ? \
1 : \
ilog2(n) + 1 : \
__bits_per(n) \
)
/** bit_for_each - iterate over an u64/u32 bits

View File

@@ -19,7 +19,6 @@
#include <stdint.h>
#include <br.h>
#include <bits.h>
#define NANOSEC 1000000000 /* nano sec in sec */
#define MILLISEC 1000000 /* milli sec in sec */
@@ -28,28 +27,29 @@
#ifdef DEBUG_DEBUG
void debug_init(uint level, FILE *stream, bool flush);
void debug_level_set(uint level);
uint debug_level_get(void);
void debug_init(int level, FILE *stream, bool flush);
void debug_level_set(int level);
int debug_level_get(void);
void debug_stream_set(FILE *stream);
long long debug_timer_elapsed(void);
void debug_flush_set(bool flush);
void _printf debug(uint level, bool timestamp,
uint indent, const char *src,
uint line, const char *fmt, ...);
void _printf debug(int level, bool timestamp,
int indent, const char *src,
int line, const char *fmt, ...);
#else /* DEBUG_DEBUG */
static inline void debug_init(__unused uint level,
static inline void debug_init(__unused int level,
__unused FILE *stream,
_unused bool flush) {}
static inline void debug_level_set(__unused uint level) {}
static inline uint debug_level_get(void) {return 0;}
__unused bool flush) {}
static inline void debug_level_set(__unused int level) {}
static inline int debug_level_get(void) {return 0;}
static inline void debug_stream_set(__unused FILE *stream) {}
static inline long long debug_timer_elapsed(void) {return 0LL};
static inline long long debug_timer_elapsed(void) {return 0LL;}
static inline void debug_flush_set(__unused bool level) {}
static inline void _printf debug(__unused uint level, __unused bool timestamp,
__unused uint indent, __unused const char *src,
__unused uint line, __unused const char *fmt, ...) {}
static inline void _printf debug(__unused int level, __unused bool timestamp,
__unused int indent, __unused const char *src,
__unused int line, __unused const char *fmt, ...) {}
#endif /* DEBUG_DEBUG */

View File

@@ -19,21 +19,20 @@
#define DEBUG_DEBUG
#endif
#include "bits.h"
#include "debug.h"
static long long timer_start; /* in nanosecond */
static uint debug_level = 0;
static bool debug_flush = false;
static FILE *stream = NULL;
static int level = 0; /* output log when < level */
static int flush = false; /* force flush after logs */
static FILE *stream = NULL; /* stream to use */
/**
* debug_level_set() - set debug level.
* @level: unsigned debug level.
* @_level: debug level (integer).
*/
void debug_level_set(uint level)
void debug_level_set(int _level)
{
debug_level = level;
level = _level;
# ifdef DEBUG_DEBUG_C
log(0, "debug level set to %u\n", level);
# endif
@@ -41,11 +40,11 @@ void debug_level_set(uint level)
/**
* debug_level_get() - get debug level.
* @return: current level debug (unsigned integer).
* @return: current level debug (integer).
*/
uint debug_level_get(void)
int debug_level_get(void)
{
return debug_level;
return level;
}
void debug_stream_set(FILE *_stream)
@@ -56,21 +55,21 @@ void debug_stream_set(FILE *_stream)
# endif
}
void debug_flush_set(bool flush)
void debug_flush_set(bool _flush)
{
debug_flush = flush;
flush = _flush;
# ifdef DEBUG_DEBUG_C
log(0, "debug flush %s.\n", flush? "set": "unset");
# endif
}
void debug_init(uint level, FILE *_stream, bool flush)
void debug_init(int _level, FILE *_stream, bool _flush)
{
struct timespec timer;
debug_stream_set(_stream);
debug_level_set(level);
debug_flush_set(flush);
debug_level_set(_level);
debug_flush_set(_flush);
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec;
}
@@ -90,16 +89,16 @@ long long debug_timer_elapsed(void)
/**
* debug() - log function
* @level: log level
* @lev: log level
* @timestamp: boolean, print timestamp if true
* @indent: indent level (2 spaces each)
* @src: source file/func name (or NULL)
* @line: line number
*/
void debug(uint level, bool timestamp, uint indent, const char *src,
uint line, const char *fmt, ...)
void debug(int lev, bool timestamp, int indent, const char *src,
int line, const char *fmt, ...)
{
if (!stream || level > debug_level)
if (!stream || lev > level)
return;
va_list ap;
@@ -122,7 +121,7 @@ void debug(uint level, bool timestamp, uint indent, const char *src,
va_start(ap, fmt);
vfprintf(stream, fmt, ap);
va_end(ap);
if (debug_flush)
if (flush)
fflush(stream);
}