Question about gcc internal library

Linux specific questions.
lassar
Posts: 240
Joined: Jan 17, 2006 1:35
Contact:

Question about gcc internal library

Postby lassar » May 16, 2017 14:51

I am thinking about using assembly in the gcc internal libray to speed up gcc output binaries.

It seems to me that all the internal library is done in c code, and none in assembly.

I found out, in speeding up RadioTelephone Tutor, I had to redo the most simple routines in assembly.

And example of this would be string "equal" and "not equal".

Where is the gcc internal library located, and what is the name of it?

And how would one go about recompiling the source to the internal library?
St_W
Posts: 788
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Question about gcc internal library

Postby St_W » May 16, 2017 17:25

Do you really mean the gcc runtime? Or rather the FreeBasic runtime library (which is written in C) ?

Any way, rewriting methods in Assembler won't get you any performance improvement for the c standard library because that is already heavily optimized. It is similar for the FB runtime, although it is not optimized as much. You need very good assembly programming skills to take any advantage of coding in assembly language.

I'd rather suggest to enable compiler optimizations if you're already using the gcc backend of FreeBasic.
vdecampo
Posts: 2962
Joined: Aug 07, 2007 23:20
Location: Florida, USA
Contact:

Re: Question about gcc internal library

Postby vdecampo » May 16, 2017 17:54

St_W wrote:I'd rather suggest to enable compiler optimizations if you're already using the gcc backend of FreeBasic.


Another suggestion would be to optimize your algorithms.

-Vince
caseih
Posts: 899
Joined: Feb 26, 2007 5:32

Re: Question about gcc internal library

Postby caseih » May 16, 2017 18:01

I don't know what this "gcc internal library" is that your'e talking about. There is a library called libgcc, which might be what you're referring to. There's also the C standard runtime which all executables link against. The standard C runtime library is called libc, and the default version that GCC links against is usually, but not always, glibc. While it might be possible to make it faster using straight assembly language, the gains would be minor compared to the downsides. There's always room for improvement, but I'm not convinced assembler is the answer. There are lighter-weight standard C libraries you can use as well, such as newlib, some might even have a lot of assembler in them.

As they say, premature optimization is the root of all evil. Before rewriting anything in assembler you'll want to profile the code and find out where the execution time is spent. Then you can focus on those areas that are consuming the most amount of time. Once every ounce of speed is squeezed out of an algorithm, then maybe there's a case for assembly as an optimization.
lassar
Posts: 240
Joined: Jan 17, 2006 1:35
Contact:

Re: Question about gcc internal library

Postby lassar » May 17, 2017 17:41

I am talking about the gcc runtime library, not the freebasic runtime.

There are several programs in linux that uses lots of string compares, and string functions.

Examples of these are bash, awk, sed, and python.

Inline assembly for string operations are about 10 to 20 times faster, than regular c code.

There's also the C standard runtime which all executables link against. The standard C runtime library is called libc, and the default version that GCC links against is usually, but not always, glibc.


Caseih sounds like you know quite a bit about the gcc compiler.

Question is libc part of the executable or is it a separate library in linux?
marcov
Posts: 2298
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Question about gcc internal library

Postby marcov » May 17, 2017 18:25

(Note on terminology:

gcc runtime : libgcc
c runtime : (g)libc

libgcc is part of gcc and mostly only architecture dependent. libc is the C language runtime, and usually part of the OS. The Linux implementation is GNU's, glibc, Android has various implementations (the original one was called Bionic iirc. The various BSDs and OS X have their own libc, and Windows has msvcrt as OS C language runtime, though 3rd party libc can be installed )
)
caseih
Posts: 899
Joined: Feb 26, 2007 5:32

Re: Question about gcc internal library

Postby caseih » May 17, 2017 19:52

lassar wrote:I am talking about the gcc runtime library, not the freebasic runtime.

Again, you'll have to be more specific as to what you're referring to. See Marcov's comment. Seeing as you are talking about string manipulation, probably the routines you have in mind are in libc, not libgcc.

There are several programs in linux that uses lots of string compares, and string functions.
Examples of these are bash, awk, sed, and python.

Inline assembly for string operations are about 10 to 20 times faster, than regular c code.

I guarantee that awk, sed, grep and friends are all written in C without assembler, and they are blazingly fast, nearly as fast as is possible. Speed comes from the algorithms. C is only slightly above assembler, and the compiler is smart enough to recognize certain operations that can benefit from specific platform CPU instructions and uses them.
Caseih sounds like you know quite a bit about the gcc compiler.

Question is libc part of the executable or is it a separate library in linux?

libc is not usually part of the binary. It's almost always dynamically-linked, unless you explicitly perform a static compile, in which case libc is embedded in the binary. Like Marcov said, libc is not actually part of the compiler, though the compiler depends on it, and on some platforms it ships with the compiler, such as Visual Studio. Also you can use other libc's if you want. libc's are mostly written in C, since C is as fast as assembler when used properly.
marcov
Posts: 2298
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Question about gcc internal library

Postby marcov » May 17, 2017 20:21

And there are multiple libcs, and most libcs are a balance between performance and architecture independence.

The gigantic differences between C and assembler, are only between optimized assembler and a C implementation usually so naive that it is only written to benchmark against.

Many primitives can furthermore be reduced to a few basis primitives like

- move a block memory
- compare a block of memory (sometimes a special case of subtract blocks of memory)
- search for a byte/word/dword in a block of memory

which are more architecture dependent and typically assembler, while the actual libc calls are mostly C. Some like strlen are often not library functions at all, but intrinsics, so that e.g. strlen (literal_string) can be optimized, since known compiletime and e.g. memcpy so that for a move with small constant value, inline code can be generated. There is usually a library function that is called if these special cases don't apply though, and since it is compiler called, it is in libgcc not glibc.

glibc is more a construction kit than a clear thing though. There is an awful lot of things that can be tuned depending on minimal architecture and required compatibility.

Of course in the C case, there is a complication called null terminated strings, which means that search for a byte has to search two different values( the byte to search, but also keep an eye for \0), while most languages store the length separately.
sean_vn
Posts: 217
Joined: Aug 06, 2012 8:26

Re: Question about gcc internal library

Postby sean_vn » May 17, 2017 22:32

Typically the only situation you will do better than the c compiler ( or Java's hotspot just in time compiler) is when doing something complex/clever using the SIMD instructions like haddps xmm0,xmm1. Even there modern compilers will autovectorize simple loops with the SIMD instructions if they are straightforward enough. Sometimes though you benefit from access to specific instructions or the flags. You can always go look at the assembly outputted by FB with the -RR compile option anyway. And don't forget
-O 3.

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 2 guests