aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2023-01-11 10:55:47 +0100
committerdexter <pmaier@sysmocom.de>2023-01-12 09:33:07 +0000
commit94705d042a94d40585414d83d446889a087dae05 (patch)
tree0f4d349a86793b9e7facb4376bb00c18a57c03ff /include
parente709bd4814020146baff2538ea50dbbc96432ff0 (diff)
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
Diffstat (limited to 'include')
-rw-r--r--include/osmocom/core/utils.h12
1 files changed, 12 insertions, 0 deletions
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. */