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 BIN := fen piece move eval brchess
LIBS := -l$(LIB) -lreadline -lncurses LIBSEXT := -lreadline -lncurses
LIBS := -l$(LIB) $(LIBSEXT)
CFLAGS := -std=gnu11 CFLAGS := -std=gnu11
@@ -174,10 +175,10 @@ cleanbin:
$(RM) -f $(BIN) core $(RM) -f $(BIN) core
# TODO: find a better dependancy graph # 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: " @[[ -f $(BINMARK) ]] || echo -n "generating binaries: "
@echo -n "$@... " @echo -n "$@... "
@$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) -I $(INCDIR) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@ @$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@
@$(TOUCH) $(BINMARK) @$(TOUCH) $(BINMARK)
##################################### ccls ##################################### ccls

View File

@@ -15,10 +15,9 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <bits/wordsize.h> /* defines __WORDSIZE: 32 or 64 */
/* next include will define __WORDSIZE: 32 or 64 void bits_implementation(void);
*/
#include <bits/wordsize.h>
#ifndef __has_builtin #ifndef __has_builtin
#define __has_builtin(x) 0 #define __has_builtin(x) 0
@@ -77,15 +76,9 @@ typedef signed char schar;
static __always_inline int popcount64(u64 n) static __always_inline int popcount64(u64 n)
{ {
# if __has_builtin(__builtin_popcountl) # if __has_builtin(__builtin_popcountl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcountl(n); return __builtin_popcountl(n);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0; int count = 0;
while (n) { while (n) {
count++; count++;
@@ -98,15 +91,9 @@ static __always_inline int popcount64(u64 n)
static __always_inline int popcount32(u32 n) static __always_inline int popcount32(u32 n)
{ {
# if __has_builtin(__builtin_popcount) # if __has_builtin(__builtin_popcount)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_popcount(n); return __builtin_popcount(n);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
int count = 0; int count = 0;
while (n) { while (n) {
count++; count++;
@@ -122,21 +109,12 @@ static __always_inline int popcount32(u32 n)
static __always_inline int ctz64(u64 n) static __always_inline int ctz64(u64 n)
{ {
# if __has_builtin(__builtin_ctzl) # if __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
return __builtin_ctzl(n); return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clzl) # elif __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin clzl.\n");
# endif
return __WORDSIZE - (__builtin_clzl(n & -n) + 1); return __WORDSIZE - (__builtin_clzl(n & -n) + 1);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64((n & -n) - 1); return popcount64((n & -n) - 1);
# endif # endif
} }
@@ -144,21 +122,12 @@ static __always_inline int ctz64(u64 n)
static __always_inline int ctz32(u32 n) static __always_inline int ctz32(u32 n)
{ {
# if __has_builtin(__builtin_ctz) # if __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
return __builtin_ctzl(n); return __builtin_ctzl(n);
# elif __has_builtin(__builtin_clz) # elif __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin clz.\n");
# endif
return __WORDSIZE - (__builtin_clz(n & -n) + 1); return __WORDSIZE - (__builtin_clz(n & -n) + 1);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32((n & -n) - 1); return popcount32((n & -n) - 1);
# endif # endif
} }
@@ -169,15 +138,9 @@ static __always_inline int ctz32(u32 n)
static __always_inline int clz64(u64 n) static __always_inline int clz64(u64 n)
{ {
# if __has_builtin(__builtin_clzl) # if __has_builtin(__builtin_clzl)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clzl(n); return __builtin_clzl(n);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u64 r, q; u64 r, q;
r = (n > 0xFFFFFFFF) << 5; n >>= r; r = (n > 0xFFFFFFFF) << 5; n >>= r;
@@ -193,15 +156,9 @@ static __always_inline int clz64(u64 n)
static __always_inline int clz32(u32 n) static __always_inline int clz32(u32 n)
{ {
# if __has_builtin(__builtin_clz) # if __has_builtin(__builtin_clz)
# ifdef DEBUG_BITS
log_f(1, "builtin.\n");
# endif
return __builtin_clz(n); return __builtin_clz(n);
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
u32 r, q; u32 r, q;
r = (n > 0xFFFF) << 4; n >>= r; r = (n > 0xFFFF) << 4; n >>= r;
@@ -236,23 +193,14 @@ static __always_inline int fls32(u32 n)
static __always_inline uint ffs64(u64 n) static __always_inline uint ffs64(u64 n)
{ {
# if __has_builtin(__builtin_ffsl) # if __has_builtin(__builtin_ffsl)
# ifdef DEBUG_BITS
log_f(1, "builtin ffsl.\n");
# endif
return __builtin_ffsl(n); return __builtin_ffsl(n);
# elif __has_builtin(__builtin_ctzl) # elif __has_builtin(__builtin_ctzl)
# ifdef DEBUG_BITS
log_f(1, "builtin ctzl.\n");
# endif
if (n == 0) if (n == 0)
return (0); return (0);
return __builtin_ctzl(n) + 1; return __builtin_ctzl(n) + 1;
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount64(n ^ ~-n); return popcount64(n ^ ~-n);
# endif # endif
} }
@@ -260,28 +208,19 @@ static __always_inline uint ffs64(u64 n)
static __always_inline uint ffs32(u32 n) static __always_inline uint ffs32(u32 n)
{ {
# if __has_builtin(__builtin_ffs) # if __has_builtin(__builtin_ffs)
# ifdef DEBUG_BITS
log_f(1, "builtin ffs.\n");
# endif
return __builtin_ffs(n); return __builtin_ffs(n);
# elif __has_builtin(__builtin_ctz) # elif __has_builtin(__builtin_ctz)
# ifdef DEBUG_BITS
log_f(1, "builtin ctz.\n");
# endif
if (n == 0) if (n == 0)
return (0); return (0);
return __builtin_ctz(n) + 1; return __builtin_ctz(n) + 1;
# else # else
# ifdef DEBUG_BITS
log_f(1, "emulated.\n");
# endif
return popcount32(n ^ ~-n); return popcount32(n ^ ~-n);
# endif # 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 * SPDX-License-Identifier: GPL-2.0
*/ */
@@ -366,20 +305,23 @@ static inline u8 ror8(u8 word, unsigned int shift)
} }
/** /**
* 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)) static __always_inline __attribute__((const))
int __ilog2_u64(u64 n) 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;
}
/** /**
* is_power_of_2() - check if a value is a power of two * is_power_of_2() - check if a value is a power of two
* @n: the value to check * @n: the value to check
@@ -394,6 +336,25 @@ 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);
}
/** /**
* ilog2 - log base 2 of 32-bit or a 64-bit unsigned value * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
* @n: parameter * @n: parameter
@@ -446,8 +407,7 @@ bool is_power_of_2(unsigned long n)
__rounddown_pow_of_two(n) \ __rounddown_pow_of_two(n) \
) )
static inline __attribute_const__ static inline __attribute_const__ int __order_base_2(unsigned long n)
int __order_base_2(unsigned long n)
{ {
return n > 1 ? ilog2(n - 1) + 1 : 0; return n > 1 ? ilog2(n - 1) + 1 : 0;
} }
@@ -468,13 +428,13 @@ int __order_base_2(unsigned long n)
#define order_base_2(n) \ #define order_base_2(n) \
( \ ( \
__builtin_constant_p(n) ? ( \ __builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) ? 0 : \ ((n) == 0 || (n) == 1) ? \
0 : \
ilog2((n) - 1) + 1) : \ ilog2((n) - 1) + 1) : \
__order_base_2(n) \ __order_base_2(n) \
) )
static inline __attribute__((const)) static inline __attribute__((const)) int __bits_per(unsigned long n)
int __bits_per(unsigned long n)
{ {
if (n < 2) if (n < 2)
return 1; return 1;
@@ -501,9 +461,9 @@ int __bits_per(unsigned long n)
#define bits_per(n) \ #define bits_per(n) \
( \ ( \
__builtin_constant_p(n) ? ( \ __builtin_constant_p(n) ? ( \
((n) == 0 || (n) == 1) \ ((n) == 0 || (n) == 1) ? \
? 1 : ilog2(n) + 1 \ 1 : \
) : \ ilog2(n) + 1 : \
__bits_per(n) \ __bits_per(n) \
) )

View File

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

View File

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