Can anyone point me to the definition of strlen()
in GCC? I've been grepping release 4.4.2 for about a half hour now (while Googling like crazy) and I can't seem to find where strlen()
is actually implemented.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Is this what you are looking for? strlen() source. See the git repository for more information. The glibc resources page has links to the git repositories if you want to grab them rather than looking at the web view.
glibc 2.26 has several hand optimized assembly implementations of
strlen
As of
glibc-2.26
, a quick:in the glibc tree shows a dozen of assembly hand-optimized implementations for all major archs and variations.
In particular, x86_64 alone has 3 variations:
A quick and dirty way to determine which one is used, is to step debug a test program:
compiled with:
Off the bat:
contains:
so the libc version is being called.
After a few
si
instruction level steps into that, GDB reaches:which tells me that
strlen-avx2.S
was used.Then, I further confirm with:
and compare the disassembly with the glibc source.
It is not surprising that the AVX2 version was used, since I have an i7-7820HQ CPU with launch date Q1 2017 and AVX2 support, and AVX2 is the most advanced of the assembly implementations, with launch date Q2 2013, while SSE2 is much more ancient from 2004.
This is where a great part of the hardcoreness of glibc comes from: it has a lot of arch optimized hand written assembly code.
Tested in Ubuntu 17.10, gcc 7.2.0, glibc 2.26.
-O3
TODO: with
-O3
, gcc does not use glibc'sstrlen
, it just generates inline assembly, which is mentioned at: https://stackoverflow.com/a/19885891/895245Is it because it can optimize even better? But its output does not contain AVX2 instructions, so I feel that this is not the case.
https://www.gnu.org/software/gcc/projects/optimize.html mentions:
My simple tests show that the
-O3
version is actually faster, so GCC made the right choice.Asked at: https://www.quora.com/unanswered/How-does-GCC-know-that-its-builtin-implementation-of-strlen-is-faster-than-glibcs-when-using-optimization-level-O3
defined in glibc/string/strlen.c
You should be looking in glibc, not GCC -- it seems to be defined in
strlen.c
-- here's a link to strlen.c for glibc version 2.7... And here is a link to the glibc SVN repository online for strlen.c.The reason you should be looking at glibc and not gcc is: