From: Simon Glass Date: Mon, 2 Apr 2012 16:18:43 -0700 Subject: [U-Boot] [PATCH v3 03/23] Add abs() macro to return absolute value In-Reply-To: <1333408743-28720-1-git-send-email-sjg@chromium.org> References: <1333408743-28720-1-git-send-email-sjg@chromium.org> Message-ID: <1333408743-28720-4-git-send-email-sjg@chromium.org> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de This macro is generally useful to make it available in common. Signed-off-by: Simon Glass --- Changes in v3: - Add new patch to put abs() in common.h include/common.h | 13 +++++++++++++ 1 files changed, 13 insertions(+), 0 deletions(-) diff --git a/include/common.h b/include/common.h index 74d9704..92eac2c 100644 --- a/include/common.h +++ b/include/common.h @@ -229,6 +229,19 @@ ulong timer_get_boot_us(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 ints, shorts + * and chars and returns a signed long. + */ +#define abs(x) ({ \ + long ret; \ + { \ + typeof((x)) __x = (x); \ + ret = (__x < 0) ? -__x : __x; \ + } \ + ret; \ + }) + #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) || \ -- 1.7.7.3