All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH v8 03/23] Add abs() macro to return absolute value
@ 2012-05-12  0:08 Simon Glass
  2012-05-14  5:40 ` Mike Frysinger
  0 siblings, 1 reply; 2+ messages in thread
From: Simon Glass @ 2012-05-12  0:08 UTC (permalink / raw)
  To: u-boot

This macro is generally useful to make it available in common.

This version is taken directly from the Linux kernel include/linux/kernel.h

Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v3:
- Add new patch to put abs() in common.h

Changes in v6:
- Update x86emu and omap4 to use the abs() macro

Changes in v7:
- Use a really simple abs() macro for now

Changes in v8:
- Go back to the complex abs() macro, but use the kernel version now

 arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
 drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
 include/common.h                        |   25 +++++++++++++++++++++++++
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/arch/arm/cpu/armv7/omap4/clocks.c b/arch/arm/cpu/armv7/omap4/clocks.c
index e2189f7..ce3f59c 100644
--- a/arch/arm/cpu/armv7/omap4/clocks.c
+++ b/arch/arm/cpu/armv7/omap4/clocks.c
@@ -46,8 +46,6 @@
 #define puts(s)
 #endif
 
-#define abs(x) (((x) < 0) ? ((x)*-1) : (x))
-
 struct omap4_prcm_regs *const prcm = (struct omap4_prcm_regs *)0x4A004100;
 
 const u32 sys_clk_array[8] = {
diff --git a/drivers/bios_emulator/x86emu/prim_ops.c b/drivers/bios_emulator/x86emu/prim_ops.c
index 7553087..5f6c795 100644
--- a/drivers/bios_emulator/x86emu/prim_ops.c
+++ b/drivers/bios_emulator/x86emu/prim_ops.c
@@ -118,11 +118,6 @@ static u32 x86emu_parity_tab[8] =
 
 #define PARITY(x)   (((x86emu_parity_tab[(x) / 32] >> ((x) % 32)) & 1) == 0)
 #define XOR2(x)	    (((x) ^ ((x)>>1)) & 0x1)
-/*----------------------------- Implementation ----------------------------*/
-int abs(int v)
-{
-	return (v>0)?v:-v;
-}
 
 /*----------------------------- Implementation ----------------------------*/
 
diff --git a/include/common.h b/include/common.h
index 4b5841e..ff7126d 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) || \
-- 
1.7.7.3

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [U-Boot] [PATCH v8 03/23] Add abs() macro to return absolute value
  2012-05-12  0:08 [U-Boot] [PATCH v8 03/23] Add abs() macro to return absolute value Simon Glass
@ 2012-05-14  5:40 ` Mike Frysinger
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Frysinger @ 2012-05-14  5:40 UTC (permalink / raw)
  To: u-boot

On Friday 11 May 2012 20:08:16 Simon Glass wrote:
> This macro is generally useful to make it available in common.
> 
> This version is taken directly from the Linux kernel include/linux/kernel.h
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

Acked-by: Mike Frysinger <vapier@gentoo.org>
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120514/a4059b84/attachment.pgp>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-05-14  5:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-12  0:08 [U-Boot] [PATCH v8 03/23] Add abs() macro to return absolute value Simon Glass
2012-05-14  5:40 ` Mike Frysinger

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.