X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=qdefs.h;h=9371f06eecc703be1efc0afb15305e2ffcf729f8;hp=463761e9b43a311993b6b182edf5f657c07b9a72;hb=7fabd73462146de1729bb3ce64a38ae706fd387e;hpb=1ff568a91b88652d1a12165607f5230e71e210e6 diff --git a/qdefs.h b/qdefs.h index 463761e9..9371f06e 100644 --- a/qdefs.h +++ b/qdefs.h @@ -9,16 +9,16 @@ #define DP_FUNC_PRINTF(n) __attribute__ ((format (printf, n, n+1))) #define DP_FUNC_PURE __attribute__ ((pure)) #define DP_FUNC_NORETURN __attribute__ ((noreturn)) +#define DP_FUNC_ALWAYS_INLINE inline __attribute__((always_inline)) #else #define DP_FUNC_PRINTF(n) #define DP_FUNC_PURE #define DP_FUNC_NORETURN -#endif - -#ifdef DP_GCC_COMPATIBLE -#define Q_typeof(var) typeof(var) -#elif defined (MSVC) -#define Q_typeof(var) decltype(var) +# if defined (_MSC_VER) +# define DP_FUNC_ALWAYS_INLINE __forceinline +# else +# define DP_FUNC_ALWAYS_INLINE inline +# endif #endif #define MAX_NUM_ARGVS 50 @@ -186,15 +186,13 @@ // This also includes extended characters, and ALL control chars #define ISWHITESPACEORCONTROL(ch) ((signed char) (ch) <= (signed char) ' ') -#ifdef PRVM_64 -#define FLOAT_IS_TRUE_FOR_INT(x) ((x) & 0x7FFFFFFFFFFFFFFF) // also match "negative zero" doubles of value 0x8000000000000000 -#define FLOAT_LOSSLESS_FORMAT "%.17g" -#define VECTOR_LOSSLESS_FORMAT "%.17g %.17g %.17g" -#else +#define DOUBLE_IS_TRUE_FOR_INT(x) ((x) & 0x7FFFFFFFFFFFFFFF) // also match "negative zero" doubles of value 0x8000000000000000 +#define DOUBLE_LOSSLESS_FORMAT "%.17g" +#define DOUBLE_VECTOR_LOSSLESS_FORMAT "%.17g %.17g %.17g" + #define FLOAT_IS_TRUE_FOR_INT(x) ((x) & 0x7FFFFFFF) // also match "negative zero" floats of value 0x80000000 #define FLOAT_LOSSLESS_FORMAT "%.9g" -#define VECTOR_LOSSLESS_FORMAT "%.9g %.9g %.9g" -#endif +#define FLOAT_VECTOR_LOSSLESS_FORMAT "%.9g %.9g %.9g" // originally this was _MSC_VER // but here we want to test the system libc, which on win32 is borked, and NOT the compiler @@ -208,4 +206,22 @@ #define INT_LOSSLESS_FORMAT_CONVERT_U(x) ((uintmax_t)(x)) #endif +// simple safe library to handle integer overflows when doing buffer size calculations +// Usage: +// - calculate data size using INTOVERFLOW_??? macros +// - compare: calculated-size <= INTOVERFLOW_NORMALIZE(buffersize) +// Functionality: +// - all overflows (values > INTOVERFLOW_MAX) and errors are mapped to INTOVERFLOW_MAX +// - if any input of an operation is INTOVERFLOW_MAX, INTOVERFLOW_MAX will be returned +// - otherwise, regular arithmetics apply + +#define INTOVERFLOW_MAX 2147483647 + +#define INTOVERFLOW_ADD(a,b) (((a) < INTOVERFLOW_MAX && (b) < INTOVERFLOW_MAX && (a) < INTOVERFLOW_MAX - (b)) ? ((a) + (b)) : INTOVERFLOW_MAX) +#define INTOVERFLOW_SUB(a,b) (((a) < INTOVERFLOW_MAX && (b) < INTOVERFLOW_MAX && (b) <= (a)) ? ((a) - (b)) : INTOVERFLOW_MAX) +#define INTOVERFLOW_MUL(a,b) (((a) < INTOVERFLOW_MAX && (b) < INTOVERFLOW_MAX && (a) < INTOVERFLOW_MAX / (b)) ? ((a) * (b)) : INTOVERFLOW_MAX) +#define INTOVERFLOW_DIV(a,b) (((a) < INTOVERFLOW_MAX && (b) < INTOVERFLOW_MAX && (b) > 0) ? ((a) / (b)) : INTOVERFLOW_MAX) + +#define INTOVERFLOW_NORMALIZE(a) (((a) < INTOVERFLOW_MAX) ? (a) : (INTOVERFLOW_MAX - 1)) + #endif