Skip to content

Commit

Permalink
add type-safe MIN() and MAX() macros (#2112)
Browse files Browse the repository at this point in the history
* add type-safe MIN() and MAX() macros

https://stackoverflow.com/questions/3437404/min-and-max-in-c

* add a CMake check for __typeof__() availability

* extend CMake check for statement expressions

* Revert "extend CMake check for statement expressions"

This reverts commit 52a24bc.

* Revert "add a CMake check for __typeof__() availability"

This reverts commit 24d14f5.
  • Loading branch information
fabiangreffrath authored Jan 1, 2025
1 parent fc6d14c commit 3171589
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/doomtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,25 @@ typedef byte lighttable_t;

#define arrlen(array) (sizeof(array) / sizeof(*array))

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#if defined(__GNUC__) || defined(__clang__)
#define MIN(a,b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
#define MAX(a,b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#else
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#define BETWEEN(l, u, x) ((l) > (x) ? (l) : (x) > (u) ? (u) : (x))
#define BETWEEN(l, u, x) (MAX((l), (MIN((u), (x)))))

#define DIV_ROUND_FLOOR(n, d) (((n) - (d) / 2) / (d))

Expand Down

0 comments on commit 3171589

Please sign in to comment.