From: Andrew Morton <akpm@linux-foundation.org>
To: Rolf Eike Beer <eike-kernel@sf-tec.de>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32
Date: Thu, 10 Sep 2009 13:39:13 -0700 [thread overview]
Message-ID: <20090910133913.097bb9a3.akpm@linux-foundation.org> (raw)
In-Reply-To: <200909032312.10418.eike-kernel@sf-tec.de>
On Thu, 3 Sep 2009 23:12:01 +0200
Rolf Eike Beer <eike-kernel@sf-tec.de> wrote:
> I was just digging a bit around in linux/kernel.h and stumbled over the abs()
> makro. For me it looks as it would return wrong results on 64 bit platforms
> if the input value is greater than 2^32.
>
Yes, it will truncate.
>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index d6320a3..1e6eb66 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -145,7 +145,7 @@ extern int _cond_resched(void);
> #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
>
> #define abs(x) ({ \
> - int __x = (x); \
> + long __x = (x); \
> (__x < 0) ? -__x : __x; \
> })
I wonder if that ends up producing worse code in the normal case.
We could use typeof to address that but what about
unsigned foo = -1;
signed bar = abs(foo);
?
That'll currently return 1 but if we use typeof it'll return
0xffffffffU which gets turned into -1.
hrm.
next prev parent reply other threads:[~2009-09-10 20:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-03 21:12 [PATCH] Make sure the value in abs() does not get truncated if it is greater than 2^32 Rolf Eike Beer
2009-09-10 20:39 ` Andrew Morton [this message]
2009-09-11 5:28 ` Rolf Eike Beer
2009-09-11 22:54 ` Jesper Juhl
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=20090910133913.097bb9a3.akpm@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=eike-kernel@sf-tec.de \
--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