global: add overflow safe addition macros
This commit is contained in:
parent
4df0986635
commit
f18d5ea850
14
global.h
14
global.h
|
@ -15,7 +15,7 @@
|
|||
/** global definitions and methods (API)
|
||||
* @file global.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016-2017
|
||||
* @date 2016-2019
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
@ -30,6 +30,18 @@
|
|||
#define CAT3(x,y,z) x##y##z
|
||||
/** concatenate 4 arguments */
|
||||
#define CAT4(w,x,y,z) w##x##y##z
|
||||
/** integer underflow/overflow safe uint8_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDU8_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > UINT8_MAX - _b) ? UINT8_MAX : (_a + _b)) : ((_a < _b) ? 0 : (_a - _b)));}
|
||||
/** integer underflow/overflow safe uint16_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDU16_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > UINT16_MAX - _b) ? UINT16_MAX : (_a + _b)) : ((_a < _b) ? 0 : (_a - _b)));}
|
||||
/** integer underflow/overflow safe uint32_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDU32_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > UINT32_MAX - _b) ? UINT32_MAX : (_a + _b)) : ((_a < _b) ? 0 : (_a - _b)));}
|
||||
/** integer underflow/overflow safe int8_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDS8_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > INT8_MAX - _b) ? INT8_MAX : (_a + _b)) : ((_a < INT8_MAX + _b) ? INT8_MAX : (_a - _b)));}
|
||||
/** integer underflow/overflow safe int16_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDS16_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > INT16_MAX - _b) ? INT16_MAX : (_a + _b)) : ((_a < INT16_MIN + _b) ? INT16_MIN : (_a - _b)));}
|
||||
/** integer underflow/overflow safe int32_t addition (result to min/max on underflow/overflow) */
|
||||
#define ADDS32_SAFE(a,b) {__typeof__ (a) _a = (a); __typeof__ (b) _b = (b); a = (_b > 0 ? ((_a > UINT32_MAX - _b) ? UINT32_MAX : (_a + _b)) : ((_a < INT32_MIN + _b) ? INT32_MIN : (_a - _b)));}
|
||||
|
||||
/** build year as number */
|
||||
#define COMPUTE_BUILD_YEAR \
|
||||
|
|
Loading…
Reference in New Issue