aboutsummaryrefslogtreecommitdiff
path: root/include/common.h
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2012-05-10 11:37:35 +0000
committerAlbert ARIBAUD <albert.u.boot@aribaud.net>2012-05-15 08:31:37 +0200
commita35925b8c10c99a7020bfcda74c8a6c72ed90cf5 (patch)
tree897969251fd788554a574d944fc173cb61891b2c /include/common.h
parent3ddecfc74086aa185a2f671cc07cb826b72d35f0 (diff)
Add abs() macro to return absolute value
This macro is generally useful to make it available in common. Signed-off-by: Simon Glass <sjg@chromium.org> Signed-off-by: Tom Warren <twarren@nvidia.com> Acked-by: Tom Rini <trini@ti.com> Acked-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'include/common.h')
-rw-r--r--include/common.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h
index 4b5841ef4..ff7126d35 100644
--- a/include/common.h
+++ b/include/common.h
@@ -222,6 +222,31 @@ typedef void (interrupt_handler_t)(void *);
#define MIN(x, y) min(x, y)
#define MAX(x, y) max(x, y)
+/*
+ * Return the absolute value of a number.
+ *
+ * This handles unsigned and signed longs, ints, shorts and chars. For all
+ * input types abs() returns a signed long.
+ *
+ * For 64-bit types, use abs64()
+ */
+#define abs(x) ({ \
+ long ret; \
+ if (sizeof(x) == sizeof(long)) { \
+ long __x = (x); \
+ ret = (__x < 0) ? -__x : __x; \
+ } else { \
+ int __x = (x); \
+ ret = (__x < 0) ? -__x : __x; \
+ } \
+ ret; \
+ })
+
+#define abs64(x) ({ \
+ s64 __x = (x); \
+ (__x < 0) ? -__x : __x; \
+ })
+
#if defined(CONFIG_ENV_IS_EMBEDDED)
#define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
#elif ( ((CONFIG_ENV_ADDR+CONFIG_ENV_SIZE) < CONFIG_SYS_MONITOR_BASE) || \