shithub: femtolisp

ref: 10d16c10bed72a177a99e0a9a3f3caca6c38ce94
dir: /cc.h/

View raw version
#pragma once

#ifdef __GNUC__

#define fl_unlikely(x) __builtin_expect(!!(x), 0)
#define fl_likely(x) __builtin_expect(!!(x), 1)
#define fl_printfmt(x, y) __attribute__((format(printf, x, y)))
#define fl_thread __thread
#define fl_prefetch(x) __builtin_prefetch(x)
#define fl_constfn __attribute__((const))
#define fl_purefn __attribute__((pure))
#define fl_hotfn __attribute__((hot))
#define fl_aligned(x) __attribute__((aligned(x)))
#define fl_popcount(x) __builtin_popcount(x)
#define fl_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 fl_unlikely(x) (x)
#define fl_likely(x) (x)
#define fl_printfmt(x, y)
#define fl_thread
#define fl_prefetch(x)
#define fl_constfn
#define fl_purefn
#define fl_hotfn
#define fl_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