Skip to contentSkip to conversation

Add abs() macro to return absolute value

PATCH Superseded Simon Glass 3 participants 6 messages May 4, 2012 – May 10, 2012 Arm (32-bit)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

 arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
 drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
 include/common.h                        |   13 +++++++++++++
 3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {
drivers/bios_emulator/x86emu/prim_ops.c +0 -5
index 7553087..5f6c795 100644
@@ -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 ----------------------------*/
 
include/common.h +13 -0
index 74d9704..92eac2c 100644
@@ -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) || \
Signature · 1 line
1.7.7.3
#1 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

Hi Simon,

Le 04/05/2012 20:25, Simon Glass a ?crit :

Quoted 49 lines

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

  arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
  drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
  include/common.h                        |   13 +++++++++++++
  3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {
drivers/bios_emulator/x86emu/prim_ops.c +0 -5
index 7553087..5f6c795 100644
@@ -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 ----------------------------*/
 
include/common.h +0 -0
index 74d9704..92eac2c 100644

include/common.h ulong timer_get_boot_us(void); · line 235

Quoted 7 lines
include/common.h +4 -0
@@ -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.
+ */

What is the rationale for forcing a signed long return type? Such macros as max, min, abs etc usually try to avoid any forced typing. Can't you get rid of the ret variable and use __x directly as the return value?
+#define abs(x) ({ \

include/common.h ulong timer_get_boot_us(void);

Quoted 11 lines
include/common.h +8 -0
+		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) || \

Amicalement,

Signature · 1 line
Albert.
#2 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

Simon,

Quoted 77 lines

-----Original Message-----
From: Albert ARIBAUD [mailto:albert.u.boot at aribaud.net]
Sent: Friday, May 04, 2012 12:28 PM
To: Simon Glass
Cc: U-Boot Mailing List; Tom Warren
Subject: Re: [U-Boot] [PATCH v6 03/23] Add abs() macro to return absolute value

Hi Simon,

Le 04/05/2012 20:25, Simon Glass a ?crit :

Quoted 36 lines

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

  arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
  drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
  include/common.h                        |   13 +++++++++++++
  3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
index 7553087..5f6c795 100644

drivers/bios_emulator/x86emu/prim_ops.c static u32 x86emu_parity_tab[8] = · line 119

Quoted 2 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
@@ -118,11 +118,6 @@ static u32 x86emu_parity_tab[8] =
 
  #define PARITY(x)   (((x86emu_parity_tab[(x) / 32]>>  ((x) % 32))&  1)

== 0)

Quoted 14 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -5
  #define XOR2(x)	    (((x) ^ ((x)>>1))&  0x1)
-/*----------------------------- Implementation
----------------------------*/ -int abs(int v) -{
-	return (v>0)?v:-v;
-}
 
  /*----------------------------- Implementation
----------------------------*/
 
include/common.h index renamed from include/common.h +0 -0

74d9704..92eac2c 100644

include/common.h ulong timer_get_boot_us(void); · line 236

Quoted 8 lines
include/common.h +5 -0
@@ -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.
+ */

What is the rationale for forcing a signed long return type? Such macros as max, min, abs etc usually try to avoid any forced typing. Can't you get rid of the ret variable and use __x directly as the return value?
+#define abs(x) ({ \

Quoted 13 lines
Quoted 11 lines
include/common.h +8 -0
+		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)<
Quoted 1 line

CONFIG_SYS_MONITOR_BASE) || \

This patch is all that's keeping me from submitting a new pull request for u-boot-tegra/master to Albert. I've fixed the other 2 build errors w/your updated patches.

Thanks,

Tom

Quoted 4 lines

Amicalement,
--
Albert.

Signature · 1 line
nvpublic
#3 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

In reply to Albert ARIBAUD

Albert,

Quoted 77 lines

-----Original Message-----
From: Albert ARIBAUD [mailto:albert.u.boot at aribaud.net]
Sent: Friday, May 04, 2012 12:28 PM
To: Simon Glass
Cc: U-Boot Mailing List; Tom Warren
Subject: Re: [U-Boot] [PATCH v6 03/23] Add abs() macro to return absolute value

Hi Simon,

Le 04/05/2012 20:25, Simon Glass a ?crit :

Quoted 36 lines

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

  arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
  drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
  include/common.h                        |   13 +++++++++++++
  3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
index 7553087..5f6c795 100644

drivers/bios_emulator/x86emu/prim_ops.c static u32 x86emu_parity_tab[8] = · line 119

Quoted 2 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
@@ -118,11 +118,6 @@ static u32 x86emu_parity_tab[8] =
 
  #define PARITY(x)   (((x86emu_parity_tab[(x) / 32]>>  ((x) % 32))&  1)

== 0)

Quoted 14 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -5
  #define XOR2(x)	    (((x) ^ ((x)>>1))&  0x1)
-/*----------------------------- Implementation
----------------------------*/ -int abs(int v) -{
-	return (v>0)?v:-v;
-}
 
  /*----------------------------- Implementation
----------------------------*/
 
include/common.h index renamed from include/common.h +0 -0

74d9704..92eac2c 100644

include/common.h ulong timer_get_boot_us(void); · line 236

Quoted 8 lines
include/common.h +5 -0
@@ -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.
+ */

What is the rationale for forcing a signed long return type? Such macros as max, min, abs etc usually try to avoid any forced typing. Can't you get rid of the ret variable and use __x directly as the return value?
+#define abs(x) ({ \

Quoted 13 lines
Quoted 11 lines
include/common.h +8 -0
+		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)<
Quoted 1 line

CONFIG_SYS_MONITOR_BASE) || \

This is the last fix needed to get my pull request to compile w/o errors/warnings w/MAKEALL.

To get this unstuck, how about if I change the macro to:

#define abs(X)				\
	({ typeof (X) __x = (X);		\
		(__x<  0) ? -__x : __x; })

And put it locally in drivers/power/tps6586x.c (the only file that uses it in that patchset). That way I get a clean build w/o the OMAP4 warnings of a double-defined abs(), and we can move everybody to use abs() in include/common.h@a later date?

Tom

Quoted 4 lines

Amicalement,
--
Albert.

Signature · 1 line
nvpublic
#4 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

Hi Tom,

Le 10/05/2012 00:56, Tom Warren a ?crit :

Quoted 96 lines

Albert,

Quoted 77 lines

-----Original Message-----
From: Albert ARIBAUD [mailto:albert.u.boot at aribaud.net]
Sent: Friday, May 04, 2012 12:28 PM
To: Simon Glass
Cc: U-Boot Mailing List; Tom Warren
Subject: Re: [U-Boot] [PATCH v6 03/23] Add abs() macro to return absolute value

Hi Simon,

Le 04/05/2012 20:25, Simon Glass a ?crit :

Quoted 36 lines

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

   arch/arm/cpu/armv7/omap4/clocks.c       |    2 --
   drivers/bios_emulator/x86emu/prim_ops.c |    5 -----
   include/common.h                        |   13 +++++++++++++
   3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
index 7553087..5f6c795 100644

drivers/bios_emulator/x86emu/prim_ops.c static u32 x86emu_parity_tab[8] = · line 119

Quoted 2 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -0
@@ -118,11 +118,6 @@ static u32 x86emu_parity_tab[8] =
 
   #define PARITY(x)   (((x86emu_parity_tab[(x) / 32]>>   ((x) % 32))&   1)

== 0)

Quoted 14 lines
drivers/bios_emulator/x86emu/prim_ops.c +0 -5
   #define XOR2(x)	    (((x) ^ ((x)>>1))&   0x1)
-/*----------------------------- Implementation
----------------------------*/ -int abs(int v) -{
-	return (v>0)?v:-v;
-}
 
   /*----------------------------- Implementation
----------------------------*/
 
include/common.h index renamed from include/common.h +0 -0

74d9704..92eac2c 100644

include/common.h ulong timer_get_boot_us(void); · line 236

Quoted 8 lines
include/common.h +5 -0
@@ -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.
+ */

What is the rationale for forcing a signed long return type? Such macros as max, min, abs etc usually try to avoid any forced typing. Can't you get rid of the ret variable and use __x directly as the return value?
+#define abs(x) ({ \

Quoted 13 lines
Quoted 11 lines
include/common.h +8 -0
+		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)<
Quoted 1 line

CONFIG_SYS_MONITOR_BASE) || \

This is the last fix needed to get my pull request to compile w/o errors/warnings w/MAKEALL.

To get this unstuck, how about if I change the macro to:

Quoted 8 lines
#define abs(X)				\
	({ typeof (X) __x = (X);		\
		(__x<   0) ? -__x : __x; })

And put it locally in drivers/power/tps6586x.c (the only file that uses it in that patchset). That way I get a clean build w/o the OMAP4 warnings of a double-defined abs(), and we can move everybody to use abs() in include/common.h at a later date?

I don't have the final word on drivers/power :) but the best is you send out a patch and if no one rejects it, then it should go in.

Quoted 1 line

Tom

Amicalement,

Signature · 1 line
Albert.
#5 lore raw

PATCH v6 3/23 Add abs() macro to return absolute value

In reply to Albert ARIBAUD

Hi Albert,

Quoted 63 lines · Albert ARIBAUD

Hi Simon,

Le 04/05/2012 20:25, Simon Glass a ?crit :

Quoted 59 lines

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

---
Changes in v3:
- Add new patch to put abs() in common.h

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

 arch/arm/cpu/armv7/omap4/**clocks.c       |    2 --
 drivers/bios_emulator/x86emu/**prim_ops.c |    5 -----
 include/common.h                        |   13 +++++++++++++
 3 files changed, 13 insertions(+), 7 deletions(-)
arch/arm/cpu/armv7/omap4/**clocks.c +0 -2
index e2189f7..ce3f59c 100644
@@ -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] = {

drivers/bios_emulator/**x86emu/prim_ops.c +0 -7
index 7553087..5f6c795 100644
@@ -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
----------------------------*/
include/common.h +0 -0
index 74d9704..92eac2c 100644
@@ -229,6 +229,19 @@ ulong timer_get_boot_us(void);
 #define MIN(x, y)  min(x, y)
 #define MAX(x, y)  max(x, y)
 
Quoted 10 lines
Quoted 6 lines
include/common.h +2 -0
+/*
+ * Return the absolute value of a number. This handles unsigned ints,

shorts
+ * and chars and returns a signed long.
+ */

What is the rationale for forcing a signed long return type? Such macros as max, min, abs etc usually try to avoid any forced typing. Can't you get rid of the ret variable and use __x directly as the return value?

Sorry I am not sure about that, not my code... I will submit a patch with a really simply implementation.

Quoted 18 lines

+#define abs(x) ({ \

Quoted 11 lines
include/common.h +8 -0
+               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)
Quoted 2 lines

|| \

Quoted 4 lines

Amicalement,
--
Albert.

Regards,
Simon