diff options
Diffstat (limited to 'src/kernel/util.h')
-rw-r--r-- | src/kernel/util.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/kernel/util.h b/src/kernel/util.h index 9b8cb3d..0e57d67 100644 --- a/src/kernel/util.h +++ b/src/kernel/util.h @@ -1,5 +1,6 @@ #pragma once #include <stddef.h> +#include <stdint.h> #define __NUM2STR(x) #x #define NUM2STR(x) __NUM2STR(x) @@ -13,3 +14,8 @@ typeof (a) _a = (a); \ typeof (b) _b = (b); \ _a > _b ? _a : _b; }) + +/* casts an uint32_t to int32_t, capping it at INT32_MAX instead of overflowing */ +static inline int32_t capped_cast32(uint32_t u) { + return (int32_t)min(u, (uint32_t)INT32_MAX); +} |