From: Miroslav Lichvar Date: Fri, 27 Nov 2015 10:03:16 +0000 (+0100) Subject: util: add macros for maximum, minimum and clamp X-Git-Tag: 2.3-pre1~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=801830df57ac866a69c0393dc58ae418788a2df9;p=thirdparty%2Fchrony.git util: add macros for maximum, minimum and clamp If MAX/MIN are defined in system headers, undefine them first. --- diff --git a/ntp_core.c b/ntp_core.c index 8987468b..26ca39b9 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -1404,8 +1404,7 @@ receive_packet(NTP_Packet *message, struct timeval *now, double now_err, NCR_Ins &sample_time, offset, delay, dispersion, root_delay, root_dispersion, - message->stratum > inst->min_stratum ? - message->stratum : inst->min_stratum, + MAX(message->stratum, inst->min_stratum), (NTP_Leap) pkt_leap); SRC_SelectSource(inst->source); diff --git a/util.h b/util.h index 78b7d71a..ebd0d208 100644 --- a/util.h +++ b/util.h @@ -148,4 +148,17 @@ extern int UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid /* Fill buffer with random bytes */ extern void UTI_GetRandomBytes(void *buf, unsigned int len); +/* Macros to get maximum and minimum of two values */ +#ifdef MAX +#undef MAX +#endif +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#ifdef MIN +#undef MIN +#endif +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + +/* Macro to clamp a value between two values */ +#define CLAMP(min, x, max) (MAX((min), MIN((x), (max)))) + #endif /* GOT_UTIL_H */