Skip to contentSkip to conversation

Add kabs()

PATCH Superseded Alexey Dobriyan 3 participants 4 messages Apr 23, 2012 – Apr 25, 2012 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add kabs()

There is abs() and abs64().
They have following unwanted or suprising properties:
1) They return signed value
in math norms et al are unsigned

2) In finest Unix tradition they do not work reliably.
Quoting abs(3).
"Trying to take the absolute value of the most negative integer is not defined."

3) They expand type needlessly

4) There are 2 of them with different names
with gcc extensions __builtin_choose_expr(), __builtin_types_compatible_p() we can mimic type dispatch as found in modern programming languages.

Enter kabs().

kabs() has the following nice properties which should have been there from day 1.

kabs() return unsigned value.
kabs() works for all integer types and return correct result. sizeof(kabs(x)) == sizeof(x)

Mention kabs() in checkpatch.pl so people would gradually convert. abs() and abs64() will eventually dissapear.

---

 include/linux/kernel.h |   29 +++++++++++++++++++++++++++++
 scripts/checkpatch.pl  |    6 ++++++
 2 files changed, 35 insertions(+)
include/linux/kernel.h +29 -0
@@ -188,6 +188,35 @@ extern int _cond_resched(void);
 		(__x < 0) ? -__x : __x;		\
 	})
 
+#define kabs(x)								\
+({									\
+	typeof(x) _x = (x);						\
+									\
+	/*								\
+	 * "char", "signed char", "unsigned char" aren't compatible	\
+	 * regardless of -f{un,}signed-char.				\
+	 */
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), signed char),	\
+		(unsigned char)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), char),		\
+		(unsigned char)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), short),	\
+		(unsigned short)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), int),		\
+		(unsigned int)({ _x < 0 ? -_x : _x; }),			\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), long),		\
+		(unsigned long)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), long long),	\
+		(unsigned long long)({ _x < 0 ? -_x : _x; }),		\
+	_x))))));							\
+})
+
 #ifdef CONFIG_PROVE_LOCKING
 void might_fault(void);
 #else
scripts/checkpatch.pl +6 -0
@@ -3347,6 +3347,12 @@ sub process {
 			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
 		}
 
+# check for abs(), abs64()
+		if ($line =~ /\b(abs64|abs)\s*\(/) {
+			WARN("CONSIDER_KABS",
+			     "$1 is obsolete, use kabs\n" . $herecurr);
+		}
+
 # check for use of yield()
 		if ($line =~ /\byield\s*\(\s*\)/) {
 			WARN("YIELD",
#1 lore raw

PATCH v2 1/1 Add kabs()

There is abs() and abs64().
They have following unwanted or suprising properties:
1) They return signed value
in math norms et al are unsigned

2) In finest Unix tradition they do not work reliably.
Quoting abs(3).
"Trying to take the absolute value of the most negative integer is not defined."

3) They expand type needlessly

4) There are 2 of them with different names
with gcc extensions __builtin_choose_expr(), __builtin_types_compatible_p() we can mimic type dispatch as found in modern programming languages.

Enter kabs().

kabs() has the following nice properties which should have been there from day 1.

kabs() return unsigned value.
kabs() works for all integer types and return correct result. sizeof(kabs(x)) == sizeof(x)

Mention kabs() in checkpatch.pl so people would gradually convert. abs() and abs64() will eventually dissapear.

---

This one will actually compile.

 include/linux/kernel.h |   29 +++++++++++++++++++++++++++++
 scripts/checkpatch.pl  |    6 ++++++
 2 files changed, 35 insertions(+)
include/linux/kernel.h +29 -0
@@ -188,6 +188,35 @@ extern int _cond_resched(void);
 		(__x < 0) ? -__x : __x;		\
 	})
 
+#define kabs(x)								\
+({									\
+	typeof(x) _x = (x);						\
+									\
+	/*								\
+	 * "char", "signed char", "unsigned char" aren't compatible	\
+	 * regardless of -f{un,}signed-char.				\
+	 */								\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), signed char),	\
+		(unsigned char)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), char),		\
+		(unsigned char)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), short),	\
+		(unsigned short)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), int),		\
+		(unsigned int)({ _x < 0 ? -_x : _x; }),			\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), long),		\
+		(unsigned long)({ _x < 0 ? -_x : _x; }),		\
+	__builtin_choose_expr(						\
+		__builtin_types_compatible_p(typeof(_x), long long),	\
+		(unsigned long long)({ _x < 0 ? -_x : _x; }),		\
+	_x))))));							\
+})
+
 #ifdef CONFIG_PROVE_LOCKING
 void might_fault(void);
 #else
scripts/checkpatch.pl +6 -0
@@ -3347,6 +3347,12 @@ sub process {
 			     "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr);
 		}
 
+# check for abs(), abs64()
+		if ($line =~ /\b(abs64|abs)\s*\(/) {
+			WARN("CONSIDER_KABS",
+			     "$1 is obsolete, use kabs\n" . $herecurr);
+		}
+
 # check for use of yield()
 		if ($line =~ /\byield\s*\(\s*\)/) {
 			WARN("YIELD",
#2 lore raw
Quoted 2 lines · Alexey Dobriyan

There is abs() and abs64().
They have following unwanted or suprising properties:

[...]

Quoted 3 lines

2) In finest Unix tradition they do not work reliably.
Quoting abs(3).
"Trying to take the absolute value of the most negative integer is not defined."

Unforunately, your version does not actually fix this problem, because...

Quoted 4 lines
include/linux/kernel.h +4 -0
+#define kabs(x)								\
+({									\
+	typeof(x) _x = (x);						\
+									\

[...]

include/linux/kernel.h extern int _cond_resched(void); · line 210

Quoted 2 lines
include/linux/kernel.h +2 -0
+		__builtin_types_compatible_p(typeof(_x), int),		\
+		(unsigned int)({ _x < 0 ? -_x : _x; }),			\

... here, if x == INT_MIN, then evaluating -x results in signed overflow and thus undefined behaviour. The problem is that the conversion to unsigned int happens too late.

It needs to be (untested):

_x == INT_MIN ? (unsigned)_x : (unsigned)(_x < 0 ? -_x : _x)

This version assumes a couple things about the representation of signed/unsigned types, in particular that INT_MIN is exactly one less than -INT_MAX, and that UINT_MAX is exactly one more than 2u*INT_MAX. I presume this is true for everything that Linux targets.

Ditto for the other types, although types "smaller" than int will be promoted to int before being negated, so the overflow probably won't happen for them.

[...]

include/linux/kernel.h extern int _cond_resched(void); · line 218

Quoted 2 lines
include/linux/kernel.h +2 -0
+	_x))))));							\
+})

Cheers,

Signature · 1 line
Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)
#3 lore raw
In reply to Alexey Dobriyan
Quoted 6 lines · Alexey Dobriyan

kabs() has the following nice properties which should have been there from day 1.

kabs() return unsigned value.
kabs() works for all integer types and return correct result. sizeof(kabs(x)) == sizeof(x)

It would be nice to document these properties in a source code comment, as well as in the commit message.