#pragma once /* 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
#pragma once
/* 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