flat assembler
Message board for the users of flat assembler.

Index > Main > optimizing c lib functions

Author
Thread Post new topic Reply to topic
FlashBurn



Joined: 06 Jan 2005
Posts: 87
FlashBurn
I want to use optimized functions of the most used c lib functions for different cpus (amd, intel) and families (p5,p6 and so on).

1st thing I need to know is, what are the most used c functions where optimizing is a good idea (like string and mem functions)?

Next thing is, I need to know for which cpu it is a good idea to optimize for.

For example, I take the memcpy function.
Code:
;in ecx is the count of bytes to copy, in esi is the source and in edi is the destination
rep movsb
    


A optimized variant would be, f.e.
Code:
;same as above
align 4
.loop
 dec ecx
 mov al,[esi+ecx]
 mov [edi+ecx],al
 test ecx,ecx
 jnz .loop
    


I hope you get what I want. Maybe this could also be a thread for collecting the most optimized functions.

The background is that I want to use modules for my kernel so that it can use optimized functions for the cpu it is running on.
Post 14 Mar 2009, 20:18
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
which implementation of libc do you plan to optimize?
Post 14 Mar 2009, 21:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
FlashBurn wrote:
I need to know for which cpu it is a good idea to optimize for.


Compilers such as GCC or OpenWatcom do a pretty good job for 586 and 686, so your best bet is to target either small size or something less utilized by most C libs (e.g. MMX, 3dnow!, SSE[1234]). Of course, the real defining factor is what your movitation is, how much skill and persistence you have, and what cpus you intend to use / test on. So really, the choice has already been made (almost) for you! Laughing
Post 15 Mar 2009, 01:39
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2915
Location: [RSP+8*5]
bitRAKE
It might be advisable to look at a project like GMP and see how they've organized the optimizations for multiple CPUs. Although we are concerned with x86 variants many of the same ideas are applicable, and ease adoption of future CPUs. In general algorithm selection is based on CPU identification and performance tests while allowing specific selection where the programmer knows better.

So, I suggest creating a framework to plug implementations into and not selecting a specific CPU. Or maybe a solid implementation will give better guidance as to what the framework should consist of?
Post 15 Mar 2009, 01:55
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
FlashBurn wrote:
optimized functions of the most used c lib functions


COOL.

Quote:
For example, I take the memcpy function.
Code:
;in ecx is the count of bytes to copy, in esi is the source and in edi is the destination
rep movsb
    

A optimized variant would be, f.e.
Code:
;same as above
align 4
.loop
 dec ecx
 mov al,[esi+ecx]
 mov [edi+ecx],al
 test ecx,ecx
 jnz .loop
    



And MOVSD can't be used since it's exclusively possessed by King Hutch and the lower code is faster Shocked

Quote:
I hope you get what I want. Maybe this could also be a thread for collecting the most optimized functions.


Indeed. Some info about how to decrypt C syntax and translate it to FASM would be nice.

Vid wrote:

Quote:
which implementation of libc do you plan to optimize?


What is around at all ?
Post 15 Mar 2009, 02:45
View user's profile Send private message Reply with quote
FlashBurn



Joined: 06 Jan 2005
Posts: 87
FlashBurn
@vid

1st I plan to have optimized variants of the string and memory functions.

@rugxulo

I don´t like the code that compilers produce and I like to use mmx and sse instruction where it is faster, so I have to write it myself/use others code Wink
Post 15 Mar 2009, 08:38
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
1st I plan to have optimized variants of the string and memory functions.

That's not what I asked about. I asked which implementation of libc you want to optimize. GNU libc implementation, MS libc implementation, tinylibc, etc???
Post 15 Mar 2009, 14:32
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
FlashBurn



Joined: 06 Jan 2005
Posts: 87
FlashBurn
I have to say that I don´t know if there would be any difference. I thought it is equal which libc I like to optimize, because I have to port it to my os and for the kernel I do not want to use a whole libc, but I only want to write/use the functions I need. So for my kernel it makes no difference which libc I will use and for my os I think I will use glibc, because it makes porting other apps easier.

At the moment I use my own c code, but as I said, I want to use asm code and want to have optimized versions.

If that doesn´t answer your question I don´t get the point Wink
Post 15 Mar 2009, 21:26
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
FlashBurn wrote:
for my os I think I will use glibc, because it makes porting other apps easier.


No offense, but I think GLIBC is extremely huge and bloated, way too complex for your needs. And there are many other C libs that are more portable, even. Perhaps newlib, but I've never investigated it honestly, so maybe I'm way off-base. (I think GNU libc mainly focuses on Linux variants and Hurd, nothing else.)
Post 17 Mar 2009, 03:09
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
If you're writing a kernel, you probably don't want to use libc for it - large parts of libc doesn't make sense inside the kernel, and you definitely want to avoid str* functions like the plague.

So, decide on what functionality your kernel needs, then write routines that make sense, and optimize as necessary. No reason to spend time optimizing a c-style strlen if you can use counted strings and do a "mov eax, [string.size]".
Post 19 Mar 2009, 12:55
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.