Skip to contentSkip to conversation

fix abs() macro to work with types wider than int

RFC PATCH In review Jiri Bohac 6 participants 11 messages Apr 19, 2007 – Apr 25, 2007

lore
Share & tools
lore

Jiri Bohac

author
#0 lore raw

RFC PATCH 1/1 fix abs() macro to work with types wider than int

Hi,

is there any reason to use an explicit int instead of a typeof in the abs() macro? The current implementation will return bogus results when used with longs.

How about changing the int to a typeof like this?:

Fix the abs() macro to work with wider types than int.

include/linux/kernel.h +1 -1
@@ -89,7 +89,7 @@ extern int cond_resched(void);
 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
 
 #define abs(x) ({				\
-		int __x = (x);			\
+		typeof(x) __x = (x);			\
 		(__x < 0) ? -__x : __x;		\
 	})
 
Signature · 2 lines
Jiri Bohac <[email protected]>
SUSE Labs, SUSE CZ
#1 lore raw
Quoted 5 lines · On Thu, 19 Apr 2007 11:23:39 +0200 Jiri Bohac wrote:

Hi,

is there any reason to use an explicit int instead of a typeof in the abs() macro? The current implementation will return bogus results when used with longs.

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

Quoted 25 lines

How about changing the int to a typeof like this?:

Fix the abs() macro to work with wider types than int.

include/linux/kernel.h +1 -1
@@ -89,7 +89,7 @@ extern int cond_resched(void);
 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
 
 #define abs(x) ({				\
-		int __x = (x);			\
+		typeof(x) __x = (x);			\
 		(__x < 0) ? -__x : __x;		\
 	})
 
Signature · 3 lines
-- 
Jiri Bohac <[email protected]>
SUSE Labs, SUSE CZ

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#2 lore raw
Quoted 5 lines · Randy Dunlap
Quoted 2 lines · On Thu, 19 Apr 2007 11:23:39 +0200 Jiri Bohac wrote:

Hi,

Quoted 8 lines
Quoted 4 lines

is there any reason to use an explicit int instead of a typeof in the abs() macro? The current implementation will return bogus results when used with longs.

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

We actually had labs() before (few months ago), but since it was not used, and if it would it seemed better to just fix abs(), it was removed. So I think this is the appropriate way to go.

Quoted 23 lines
Quoted 23 lines

How about changing the int to a typeof like this?:

Fix the abs() macro to work with wider types than int.

include/linux/kernel.h +1 -1
@@ -89,7 +89,7 @@ extern int cond_resched(void);
 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
 
 #define abs(x) ({				\
-		int __x = (x);			\
+		typeof(x) __x = (x);			\
 		(__x < 0) ? -__x : __x;		\
 	})
 
#3 lore raw
Quoted 11 lines · On Fri, 20 Apr 2007 14:44:38 +0200 Richard Knutsson wrote:
Quoted 10 lines · Randy Dunlap
Quoted 6 lines · On Thu, 19 Apr 2007 11:23:39 +0200 Jiri Bohac wrote:

Hi,

is there any reason to use an explicit int instead of a typeof in the abs() macro? The current implementation will return bogus results when used with longs.

Quoted 6 lines
Quoted 3 lines

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

We actually had labs() before (few months ago), but since it was not used, and if it would it seemed better to just fix abs(), it was removed. So I think this is the appropriate way to go.

Sounds like when someone actually needs labs() or llabs() they can submit a patch for however they would like to use it.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

How about changing the int to a typeof like this?:

Fix the abs() macro to work with wider types than int.

include/linux/kernel.h +0 -0
@@ -89,7 +89,7 @@ extern int cond_resched(void);
 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
 
Quoted 5 lines
Quoted 5 lines

include/linux/kernel.h extern int cond_resched(void); · line 94

Quoted 5 lines
include/linux/kernel.h +1 -1
 #define abs(x) ({				\
-		int __x = (x);			\
+		typeof(x) __x = (x);			\
 		(__x < 0) ? -__x : __x;		\
 	})

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#4 lore raw
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

We actually had labs() before (few months ago), but since it was not used, and if it would it seemed better to just fix abs(), it was removed. So I think this is the appropriate way to go.

Sounds like when someone actually needs labs() or llabs() they can submit a patch for however they would like to use it.

However they would like to use *abs()? What different ways are possible to take the arithmetic absolute value? I see record of many cases where dozens of authors have macros that then get collapsed to include files. So why not avoid that annoyance this time and -start- with it in the include files?

Can there even be any reason beyond unnecessary pedantics to have [l[l]]abs?

#5 lore raw
Quoted 10 lines · John Anthony Kazos Jr.
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

We actually had labs() before (few months ago), but since it was not used, and if it would it seemed better to just fix abs(), it was removed. So I think this is the appropriate way to go.

Sounds like when someone actually needs labs() or llabs() they can submit a patch for however they would like to use it.

However they would like to use *abs()? What different ways are possible to take the arithmetic absolute value? I see record of many cases where dozens of authors have macros that then get collapsed to include files. So why not avoid that annoyance this time and -start- with it in the include files?

Can there even be any reason beyond unnecessary pedantics to have [l[l]]abs?

See Paragraph 1 above. We do lots of functions in a manner that is like C (or libc) so that we don't confuse developers.

Signature · 2 lines
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***
#6 lore raw
Quoted 4 lines
Quoted 1 line

Can there even be any reason beyond unnecessary pedantics to have [l[l]]abs?

See Paragraph 1 above. We do lots of functions in a manner that is like C (or libc) so that we don't confuse developers.

I have the perfect solution that includes compatibility while avoiding stupidity.

Macro "abs" which evaluates its argument once and uses typeof().

#define labs(n) abs(n)
#define llabs(n) labs(n)

Leave that in the kernel for a year or two. At the end of that time, grep the kernel to show that only two silly people actually use them. Change those, and remove the aliases. Sense restored.

As far as I'm concerned, the only reason those functions exist like that is because standard C doesn't support function overloading and doesn't want to promote every argument to the biggest width and -also- can't define it as a macro because there's no typeof() in standard C. So since we do have it...

...and why do we want people hacking the kernel who get confused by a single "abs" function? Isn't that quite a lot like saying "Do not use hairdryer while sleeping."?

#7 lore raw
In reply to John Anthony Kazos Jr.
Quoted 10 lines · John Anthony Kazos Jr.
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I think it's like it is just to be consistent with abs() in C, which also contains labs() and llabs().

We actually had labs() before (few months ago), but since it was not used, and if it would it seemed better to just fix abs(), it was removed. So I think this is the appropriate way to go.

Sounds like when someone actually needs labs() or llabs() they can submit a patch for however they would like to use it.

Quoted 8 lines

However they would like to use *abs()? What different ways are possible to take the arithmetic absolute value? I see record of many cases where dozens of authors have macros that then get collapsed to include files. So why not avoid that annoyance this time and -start- with it in the include files?

Can there even be any reason beyond unnecessary pedantics to have [l[l]]abs?

Isn't any macro that looks for "< 0" insensitive to the word length? I think it's only when people try tricks with "sign-bits," that they get into trouble. I think this works, regardless of the length of the integers:

#define abs(x) (((x)<0)?-(x):(x))

Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.62 BogoMips). New book: http://www.AbominableFirebug.com/
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#8 lore raw
Quoted 3 lines · linux-os (Dick Johnson)

I think this works, regardless of the length of the integers:

#define abs(x) (((x)<0)?-(x):(x))

But it evaluates its argument more than once.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#9 lore raw
Quoted 10 lines · Andreas Schwab
Quoted 3 lines · linux-os (Dick Johnson)

I think this works, regardless of the length of the integers:

#define abs(x) (((x)<0)?-(x):(x))

But it evaluates its argument more than once.

Andreas.

Sure. Macros do that. If that's not okay, then you need either a temporary variable or a function. The original query was for a macro. Any macro that tests something then returns a value based upon the test is going to have that problem. Any attempt to just AND off a sign bit is going to have width problems. You can't have your cake and eat it too. Even whacking off the sign- bit may fail because there may be two variable accesses which could mean two function calls, destroying the logic.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.16.24 on an i686 machine (5592.62 BogoMips). New book: http://www.AbominableFirebug.com/
_


****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#10 lore raw
Quoted 1 line · linux-os (Dick Johnson)

The original query was for a macro.

A macro that does not evaluate its argument more than once. If you want it to match the behaviour as defined by the C standard then it is not allowed to do so.

Andreas.

Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected]
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."