mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: Optimized C memset
Date: Tue, 27 Aug 2013 05:17:30 -0400	[thread overview]
Message-ID: <20130827091730.GN20515@brightrain.aerifal.cx> (raw)
In-Reply-To: <1377593575.25996.289.camel@eris.loria.fr>

On Tue, Aug 27, 2013 at 10:52:55AM +0200, Jens Gustedt wrote:
> Hello,
> 
> Am Dienstag, den 27.08.2013, 04:30 -0400 schrieb Rich Felker:
> > One aspect of this code that may appear ugly at first is the usage of
> > the __GNUC__ macro.
> 
> It not only *appears* to be ugly :)
> 
> You already have a slight inconsistency in that at one point the code
> depends on a particular version of "gcc" and other part only depends
> on the fact that the __GNUC__ macro is set.

That's intentional. The __may_alias__ attribute has existed since GCC
3.2 (I did the research just a little bit ago). Earlier versions of
GCC certainly aren't capable of making optimization that could see
cross-translation-unit aliasing, so it's "safe" to do without the
attribute on such versions.

> There are a lot of compilers out there faking to be a gcc of some
> version and that are not always feature consistent with the gcc
> version that they are claming to fake. ICC is notorious for that. So
> to my opinion this is a dangerous path to follow. (I don't know about
> any problems with the __may_alias__ attribute, though)

So far, my experience is that compilers which advertise themseleves as
"GNU C" compilers have all the semantic features of GCC 3 at the very
least. Is this an invalid assumption?

Note that even if we "wrongly" detect a compiler as supporting
__may_alias__, as long as it doesn't give errors on unknown GCC
attributes, the situation is no worse than what we have right now:
aliasing violations that the compiler can't see without performing
cross-translation-unit analysis. So the code couldn't break without a
very fancy compiler, and I would expect any compiler providing such
fancy optimization and claiming it's "GNU C" to support the GNU C
feature for allowing aliasing in certain places.

> To make this easier to maintain, I'd suggest to introduce a special
> feature test macro, something like __has_may_alias__, and have that
> set at the beginning of the file in a section that is clearly
> dedicated to compile time feature detection. Other compilers may have
> different syntax for such a feature (a _Pragma comes in mind) and may
> be detected quite differently than by gcc version numbering.

Where would you suggest it be added? The result of the check is only
used in one place, so I don't see how

#if ....
#define __has_may_alias__
#endif

#ifdef __has_may_alias__
[use it]
#endif

is better than just

#if ....
[use it]
#endif

in any practical sense.

If on the other hand you're suggesting that I should be more
conservative and check not just for __GNUC__, but also for the
availability of __may_alias__, in the actual code body (rather than
just the typedefs), then it may make more sense.

> Such specific feature test macros is the way that clang goes, and from
> my experience this is much easier to maintain and to understand when
> you stumble on such #ifdef'ed code. You not only know that this needs
> a special version of a compiler, but also for what reason.

Oh, I agree it's much cleaner. Sadly, though, GCC doesn't seem to give
us that information...

Rich


  reply	other threads:[~2013-08-27  9:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-27  8:30 Rich Felker
2013-08-27  8:52 ` Jens Gustedt
2013-08-27  9:17   ` Rich Felker [this message]
2013-08-27  9:50     ` Jens Gustedt
2013-08-27 14:21       ` Rich Felker
2013-08-27 14:34         ` Luca Barbato
2013-08-27 14:39           ` Rich Felker
2013-08-27 15:20         ` John Spencer
2013-08-27 15:34           ` Rich Felker
2013-08-27 16:22 ` Optimized C memset [v2] Rich Felker
2013-08-27 17:28   ` Jeremy Huntwork
2013-08-27 21:27     ` Rich Felker
2013-08-28  0:05   ` Andre Renaud
2013-08-28  1:24     ` Rich Felker

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=20130827091730.GN20515@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).