From 94705d042a94d40585414d83d446889a087dae05 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Wed, 11 Jan 2023 10:55:47 +0100 Subject: uitils: add floored and euclidian modulo functions C/C++ only implements a so called "truncated modulo" function. Lets also add a floored and an euclidian modulo function to be more complete. The functions will be used to generalize the following Change: I5fb2b0ada8d409730ac22963741fb4ab0026abdd Change-Id: If61cd54f43643325c45f64531c57fe4c5802a9cf --- include/osmocom/core/utils.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'include') diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index d0e2e9bf..ee7cfa49 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -182,6 +182,18 @@ int osmo_print_n(char *buf, size_t bufsize, const char *str, size_t n); uint32_t osmo_isqrt32(uint32_t x); +/*! Floored Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). + * \param[in] x dividend. + * \param[in] y divisor. + * \returns remainder of x divided by y. */ +#define OSMO_MOD_FLR(x, y) (((x) > 0 && (y) < 0) || ((x) < 0 && (y) > 0) ? (x) % (y) + (y) : (x) % (y)) + +/*! Euclidean Modulo (See also: Daan Leijen, Division and Modulus for Computer Scientists). + * \param[in] x dividend. + * \param[in] y divisor. + * \returns remainder of x divided by y. */ +#define OSMO_MOD_EUC(x, y) ((x) % (y) < 0 ? (y) > 0 ? (x) % (y) + (y) : (x) % (y) - (y) : (x) % (y)) + char osmo_luhn(const char* in, int in_len); /*! State for OSMO_STRBUF_APPEND() and OSMO_STRBUF_PRINTF(). See there for examples. */ -- cgit v1.2.3