shithub: femtolisp

ref: 92980154dee84747ca798bafe6e05d87519ab665
dir: /llt/llt.h/

View raw version
#ifndef __LLT_H_
#define __LLT_H_

#include "platform.h"
#include "utf8.h"
#include "ios.h"
#include "bitvector.h"

#include "htableh.inc"
HTPROT(ptrhash)

#ifdef __GNUC__
#define __unlikely(x) __builtin_expect(!!(x), 0)
#define __likely(x) __builtin_expect(!!(x), 1)
#else
#define __unlikely(x) (x)
#define __likely(x) (x)
#endif

#ifdef BOEHM_GC /* boehm GC allocator */
#include <gc.h>
#define LLT_ALLOC(n) GC_MALLOC(n)
#define LLT_REALLOC(p,n) GC_REALLOC((p),(n))
#define LLT_FREE(x) USED(x)
#else /* standard allocator */
#define LLT_ALLOC(n) malloc(n)
#define LLT_REALLOC(p,n) realloc((p),(n))
#define LLT_FREE(x) free(x)
#endif

#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
#define bswap_32(x) \
    ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
    (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
#define bswap_64(x) \
    (uint64_t)bswap_32((x) & 0xffffffffULL)<<32 | \
    (uint64_t)bswap_32(((x)>>32) & 0xffffffffULL)

#define DBL_MAXINT (1LL<<53)
#define FLT_MAXINT (1<<24)
#define BIT63 0x8000000000000000ULL
#define BIT31 0x80000000UL

#ifdef BITS64
#define NBITS 64
#define TOP_BIT BIT63
typedef uint64_t lltuint_t;
typedef int64_t lltint_t;
#else
#define NBITS 32
#define TOP_BIT BIT31
typedef uint32_t lltuint_t;
typedef int32_t lltint_t;
#endif

#define LOG2_10 3.3219280948873626
#define rel_zero(a, b) (fabs((a)/(b)) < DBL_EPSILON)
#define sign_bit(r) ((*(uint64_t*)&(r)) & BIT63)
#define LABS(n) (((n)^((n)>>(NBITS-1))) - ((n)>>(NBITS-1)))
#define NBABS(n,nb) (((n)^((n)>>((nb)-1))) - ((n)>>((nb)-1)))
#define DFINITE(d) (((*(uint64_t*)&(d))&0x7ff0000000000000ULL)!=0x7ff0000000000000ULL)
#define LLT_ALIGN(x, sz) (((x) + (sz-1)) & (-sz))

// a mask with n set lo or hi bits
#define lomask(n) (uint32_t)((((uint32_t)1)<<(n))-1)

extern double D_PNAN, D_NNAN, D_PINF, D_NINF;
extern float F_PNAN, F_NNAN, F_PINF, F_NINF;

/* timefuncs.c */
uint64_t i64time(void);
double clock_now(void);
void timestring(double seconds, char *buffer, size_t len);
double parsetime(const char *str);
void sleep_ms(int ms);

/* hashing.c */
lltuint_t nextipow2(lltuint_t i);
uint32_t int32hash(uint32_t a);
uint64_t int64hash(uint64_t key);
uint32_t int64to32hash(uint64_t key);
uint64_t memhash(const char* buf, size_t n);
uint32_t memhash32(const char* buf, size_t n);

/* random.c */
#define random() genrand_int32()
#define srandom(n) init_genrand(n)
double rand_double(void);
float rand_float(void);
double randn(void);
void randomize(void);
uint32_t genrand_int32(void);
void init_genrand(uint32_t s);
uint64_t i64time(void);

/* utils.c */
char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base);
int str2int(char *str, size_t len, int64_t *res, uint32_t base);
int isdigit_base(char c, int base);

void llt_init(void);

#endif