Skip to contentSkip to conversation

strlen("literal string") -> (sizeof("literal string")-1)

TRIVIAL Andi Kleen 2 participants 3 messages Aug 29, 2002 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20020829031008.T7920@devserv.devel.redhat.com.suse.lists.linux.kernel

A message that is not in the archive (sent off-list or not yet received): Pine.LNX.4.44.0208290955280.2070-100000@home.transmeta.com.suse.lists.linux.kernel

Andi Kleen

author
#2 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 4 lines · Jakub Jelinek

Well, IMHO at least for the more recent GCC versions kernel should leave the job to GCC (ie. either just prototype str* functions, or define them to __builtin_str* variants).

I agree. That x86 strlen() inline is from 1991 with fixes ever after, and pre-dates gcc having any support for inline at all. We're much more likely to be better off just removing it these days. Is somebody willing to compare code quality? I wouldn't be in the least surprised if gcc did a better job these days..

It does a better job for near all the string.h stuff. x86-64 just uses the builtins. Only exception is memcpy, where it likes to call out of line memcpy when it is not absolutely sure about all the alignments (especally lots of casting causes that)

Still having an empty (or the one from x86-64 ;) string.h should be fine.

-Andi

#3 lore raw
Quoted 4 lines · Andi Kleen

It does a better job for near all the string.h stuff. x86-64 just uses the builtins. Only exception is memcpy, where it likes to call out of line memcpy when it is not absolutely sure about all the alignments (especally lots of casting causes that)

memcpy() on x86 includes uses of mmx_memcpy(). Not likely to be done by gcc...

author
#4 lore raw
Quoted 10 lines · Alexander Viro
Quoted 4 lines · Andi Kleen

It does a better job for near all the string.h stuff. x86-64 just uses the builtins. Only exception is memcpy, where it likes to call out of line memcpy when it is not absolutely sure about all the alignments (especally lots of casting causes that)

memcpy() on x86 includes uses of mmx_memcpy(). Not likely to be done by gcc...

Only for big arguments. That is why it calls the out of line function. Basically it only inlines when the limit is known and small and the alignments are known. IMHO the alignment check is overkill for x86, but that is what it does. That is why I wrapped it a bit (see include/asm-x86_64/string.h)

-Andi