linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] Add kabs()
Date: Mon, 23 Apr 2012 16:11:06 +0300	[thread overview]
Message-ID: <20120423131106.GA27574@p183.telecom.by> (raw)

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.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 include/linux/kernel.h |   29 +++++++++++++++++++++++++++++
 scripts/checkpatch.pl  |    6 ++++++
 2 files changed, 35 insertions(+)

--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -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
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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",

             reply	other threads:[~2012-04-23 13:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-23 13:11 Alexey Dobriyan [this message]
2012-04-23 13:22 ` [PATCH v2] Add kabs() Alexey Dobriyan
2012-04-23 18:18   ` Nick Bowler
2012-04-25 17:20 ` [PATCH] " Ben Pfaff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120423131106.GA27574@p183.telecom.by \
    --to=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).