summaryrefslogtreecommitdiff
path: root/include/util.h
diff options
context:
space:
mode:
authorDaisuke Nojiri <dnojiri@chromium.org>2018-12-06 12:42:28 -0800
committerchrome-bot <chrome-bot@chromium.org>2018-12-10 16:00:33 -0800
commiteb3ac28cb5a634cf15770e1f79558cdfa4a6923b (patch)
tree2892c3dfcdf15e062b19b7a23a381c589f21363f /include/util.h
parent080075c076d9c3daabb050abfe9d71b55b585656 (diff)
downloadchrome-ec-eb3ac28cb5a634cf15770e1f79558cdfa4a6923b.tar.gz
util: Add macro to swap two variables
This patch adds swap(a,b), which swaps the values in two variables. It requires c99 for typeof. Swapping composites (e.g. a+b, x++) doesn't make sense. So, <a> and <b> can only be a variable (x) or a pointer reference (*x) without an operator. Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org> BUG=none BRANCH=none TEST=buildall Change-Id: Id656e173d372dfff759d9aee9314a008a6d91786 Reviewed-on: https://chromium-review.googlesource.com/1366306 Commit-Ready: Daisuke Nojiri <dnojiri@chromium.org> Tested-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'include/util.h')
-rw-r--r--include/util.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/include/util.h b/include/util.h
index 5c2a7f38a6..aa15ba4fa8 100644
--- a/include/util.h
+++ b/include/util.h
@@ -62,7 +62,22 @@ extern "C" {
#define DIV_ROUND_UP(x, y) (((x) + ((y) - 1)) / (y))
#define DIV_ROUND_NEAREST(x, y) (((x) + ((y) / 2)) / (y))
+/*
+ * Swap two variables (requires c99)
+ *
+ * Swapping composites (e.g. a+b, x++) doesn't make sense. So, <a> and <b>
+ * can only be a variable (x) or a pointer reference (*x) without operator.
+ */
+#define swap(a, b) \
+ do { \
+ typeof(a) __t__; \
+ __t__ = a; \
+ a = b; \
+ b = __t__; \
+ } while (0)
+
#ifndef HIDE_EC_STDLIB
+
/* Standard library functions */
__stdlib_compat int atoi(const char *nptr);
__stdlib_compat int isdigit(int c);