From: Pavel Roskin <proski@gnu.org>
To: linux-sparse@vger.kernel.org
Subject: Fixing "cast truncates bits from constant value"
Date: Mon, 17 Aug 2009 17:02:02 -0400 [thread overview]
Message-ID: <1250542922.10511.38.camel@mj> (raw)
Hello!
There was a discussion in the linux-wireless mailing list about
silencing a sparse warning in the b43 driver:
http://marc.info/?t=125020183400006&r=1&w=2
It turns out the fixes for the warnings would make the code worse, not
better. It's not what sparse should try to do.
Here's the simplest case:
static unsigned short test(void)
{
return (unsigned short)~0x8000;
}
test.c:3:32: warning: cast truncates bits from constant value (ffff7fff
becomes 7fff)
A side note - "0x" should be used with user visible output.
0x8000 could be a constant defined in a header, so just using 0x7fff is
not an option, if the code readability is to be preserved.
In my opinion, an explicit cast should be enough to suppress the
warning. But it's not. Moreover, it's a "superwarning" that cannot
even be suppressed by the "force" attribute! This source still
generates a warning:
static unsigned short test(void)
{
return (__attribute__((force)) unsigned short)~0x8000;
}
There is also an inconsistency. This source produces a warning:
static unsigned short test(void)
{
return 0x0ffff000U;
}
But this source doesn't:
static unsigned short test(void)
{
return 0xfffff000U;
}
In both cases bits are dropped and the value (a large positive number)
is changed.
I'm not currently subscribed to this list, please copy me.
--
Regards,
Pavel Roskin
next reply other threads:[~2009-08-17 21:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-17 21:02 Pavel Roskin [this message]
2009-08-19 5:36 ` Fixing "cast truncates bits from constant value" Christopher Li
2009-08-19 23:28 ` Pavel Roskin
2009-08-20 6:55 ` Christopher Li
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=1250542922.10511.38.camel@mj \
--to=proski@gnu.org \
--cc=linux-sparse@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).