shithub: sl

ref: 48299e12fec43f510d42016b22dadd3823b7462f
dir: /src/cc.h/

View raw version
#pragma once

#ifdef __GNUC__

typedef uint8_t u8int;
typedef uint16_t u16int;
typedef uint32_t u32int;
typedef uint64_t u64int;
typedef uintptr_t uintptr;

typedef int8_t s8int;
typedef int16_t s16int;
typedef int32_t s32int;
typedef int64_t s64int;
typedef intptr_t intptr;

typedef ssize_t ssize;
typedef size_t usize;
typedef off_t soffset;

#define sl_unlikely(x) __builtin_expect(!!(x), 0)
#define sl_likely(x) __builtin_expect(!!(x), 1)
#define sl_printfmt(x, y) __attribute__((format(printf, x, y)))
#if defined(NDEBUG) && !defined(__macos__) && !defined(__dos__)
#define sl_thread(x) __thread x
#else
#define sl_thread(x) x
#endif
#define sl_prefetch(x) __builtin_prefetch(x)
#define sl_constfn __attribute__((const))
#define sl_purefn __attribute__((pure))
#define sl_hotfn __attribute__((hot))
#define sl_aligned(x) __attribute__((aligned(x)))
#define sl_popcount(x) __builtin_popcount(x)
#define sl_clz(x) __builtin_clz(x)
#define sadd_overflow __builtin_add_overflow
#define sadd_overflow_64 __builtin_add_overflow
#define smul_overflow_64 __builtin_mul_overflow

#else

#define sl_unlikely(x) (x)
#define sl_likely(x) (x)
#define sl_printfmt(x, y)
#define sl_thread(x) x
#define sl_prefetch(x)
#define sl_constfn
#define sl_purefn
#define sl_hotfn
#define sl_aligned(x)

/* FIXME(sigrid): s*_overflow_* can be more optimal */
#define sadd_overflow_64(a, b, c) ( \
  (b < 1) ? \
  ((INT64_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
  ((INT64_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
)
#define smul_overflow_64(a, b, c) ( \
	((a)>0 ? ((b)>0 ? (a)>INT64_MAX/(b) : (b)<INT64_MIN/(a)) \
	       : ((b)>0 ? (a)<INT64_MIN/(b) : ((a)!=0 && (b)<INT64_MAX/(a)))) \
	? 1 \
	: ((*(c)=(a)*(b)), 0) \
)
#if defined(BITS64)
#define sadd_overflow(a, b, c) sadd_overflow_64(a, b, c)
#else
#define sadd_overflow(a, b, c) ( \
  (b < 1) ? \
  ((INT32_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \
  ((INT32_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \
)
#endif

#endif