shithub: femtolisp

ref: 73063b8a1eb2647abff04cd4af49d04239a5222a
dir: /overflows.h/

View raw version

#define addof_int64(c,a,b) ( \
  (b < 1)? \
  ((INT64_MIN-(b) <= (a))?((c=(a)+(b))?0:1):1): \
  ((INT64_MAX-(b) >= (a))?((c=(a)+(b))?0:1):1) \
)

#define subof_int64(c,a,b) ( \
  (b < 1)? \
  ((INT64_MAX+(b) >= (a))?((c=(a)-(b))?0:1):1): \
  ((INT64_MIN+(b) <= (a))?((c=(a)-(b))?0:1):1) \
)

#define mulof_int64(c,a,b) ( \
  (((a) != 0) && ((c=(a)*(b))/(a) != (b)))?1:0 \
)

#define addof_uint64(c,a,b) ( \
  (b < 1)? \
  ((0-(b) <= (a))?((c=(a)+(b))?0:1):1): \
  ((UINT64_MAX-(b) >= (a))?((c=(a)+(b))?0:1):1) \
)

#define subof_uint64(c,a,b) ( \
  (b < 1)? \
  ((UINT64_MAX+(b) >= (a))?((c=(a)-(b))?0:1):1): \
  (((b) <= (a))?((c=(a)-(b))?0:1):1) \
)

#define mulof_uint64(c,a,b) ( \
  (((a) != 0) && ((c=(a)*(b))/(a) != (b)))?1:0 \
)