coreutils-6.11-1 in release-2 area

Eric Blake
Fri May 23 01:46:00 GMT 2008

Hash: SHA1

According to Corinna Vinschen on 5/22/2008 2:41 PM:
|>> Oh, surprise!  I thought Cygwin would use Eric's version now.
|> It will export it but, in my testing, it won't use it.

Actually, it will.  Build cygwin1.dbg, then 'gdb cygwin1.dll',
'disassemble strchr', and you will see my implementation - gcc only
implements the builtin as a wrapper to notice where it can avoid using the
library, and NOT as a full-blown replacement that would work even in the
absence of libc (after all, how do you write a generic builtin in
intermediate language that will compile to optimal code across all the
various architectures that gcc targets?).

|>  But that's no
|> different than any other program which uses strchr() and specifies the
|> right -O optimization level.

Actually, what gcc does with __builtin_strchr is that IF it can optimize
at compile time, it does (so strchr("abc","b") is turned into the
compile-time constant "bc", without a function call).  But WHEN gcc can't
optimize, it calls the library's strchr() instead.

|> strchr() and strlen() are among a group of functions that gcc inlines
|> automatically when you compile a file with optimization.
| But then... is the
|   #define strlen __builtin_strlen
|   [...]
| stuff at the top of winsup.h just old cruft which isn't needed anymore?

Adding #defines that defer to the builtin is still a useful idiom - it
allows the builtin to do optimizations even when compiled with -std=c89,
or when compiling with -fno-builtin that would inhibit the normal
recognition of plain names as special functions.  But when you don't
specifically request such a strict compilation environment, the #define
doesn't really buy much, since gcc automatically recognizes the original
name as one of its builtins.

To see for yourself, compile a program consisting of:

#include <stdio.h>
int main(int argc, char**argv)
~  printf ("%d\n", strlen("abc"));
~  printf ("%d\n", __builtin_strlen("abc"));
~  printf ("%d\n", strlen(argv[0]));
~  printf ("%d\n", __builtin_strlen(argv[0]));
~  return 0;

With 'gcc -O2', then 'gdb program', 'disassemble main', you can see the
first two instances are hard-coded to 3, the last two result in 'call
<strlen>'.  With 'gcc -O2 -fno-builtin', only the second instance is
hard-coded to three.  But hopefully this goes to show that where the
compiler can't optimize at compile time, the library function is used.

- --
Don't work too hard, make some time for fun as well!

Eric Blake   
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at
Comment: Using GnuPG with Mozilla -


More information about the Cygwin-apps mailing list