From: Denis Zaitsev <zzz@anda.ru>
To: Zack Weinberg <zack@codesourcery.com>
Cc: Richard Henderson <rth@redhat.com>, Andreas Jaeger <aj@suse.de>,
libc-alpha@sources.redhat.com, linux-gcc@vger.kernel.org,
gcc@gcc.gnu.org
Subject: Re: i386 inline-asm string functions - some questions
Date: Sat, 27 Dec 2003 16:35:40 +0500 [thread overview]
Message-ID: <20031227163540.B6728@zzz.ward.six> (raw)
In-Reply-To: <87fzf6mubo.fsf@egil.codesourcery.com>; from zack@codesourcery.com on Sat, Dec 27, 2003 at 02:24:59AM -0800
On Sat, Dec 27, 2003 at 02:24:59AM -0800, Zack Weinberg wrote:
> Denis' original example doesn't quote actual code but I think it's
> talking about stuff like this (from libc cvs,
> sysdeps/i386/i486/bits/string.h) -
Exactly. But I've mentioned that...
> __STRING_INLINE void *
> __memcpy_g (void *__dest, __const void *__src, size_t __n)
> {
> register unsigned long int __d0, __d1, __d2;
> register void *__tmp = __dest;
> __asm__ __volatile__
> ("cld\n\t"
> "shrl $1,%%ecx\n\t"
> "jnc 1f\n\t"
> "movsb\n"
> "1:\n\t"
> "shrl $1,%%ecx\n\t"
> "jnc 2f\n\t"
> "movsw\n"
> "2:\n\t"
> "rep; movsl"
> : "=&c" (__d0), "=&D" (__d1), "=&S" (__d2),
> "=m" ( *(struct { __extension__ char __x[__n]; } *)__dest)
> : "0" (__n), "1" (__tmp), "2" (__src),
> "m" ( *(struct { __extension__ char __x[__n]; } *)__src)
> : "cc");
> return __dest;
> }
>
> so, first off, I don't think this kind of optimization is libc's
> business; we have the tools to do a better job over here in the
> compiler.
Should the compiler implement all the string functions? Very probably
not. But anyway, then these problem will be inside the compiler
(again). And anyway, there should be the right way to make such
inline solutions, if so...
> And furthermore I think it's buggy - if the block to be copied is
> large and not aligned, it will overwrite memory past the end of the
> destination.
Why do you think so? The code looks ok. I don't think it's the
fastest one, but it's correct.
> But let's suppose /arguendo/ that there is a legitimate use for a
> construct like this: the notation is frankly appalling. Let me try
> to make up some better notation, using C99 variably-modified arrays
> and GNU forward parameter declarations (we have the blasted things,
> we might as well get some use out of them...) Note I am not
> attempting to fix the bugs in the assembly.
>
> __STRING_INLINE void *
> __memcpy_g (size_t __n; char __dest[restrict static __n],
> const char __src[restrict static __n], size_t __n)
> {
> void *savedest = __dest;
> __asm__ __volatile__
> ("cld\n\t"
> "shrl $1,%%ecx\n\t"
> "jnc 1f\n\t"
> "movsb\n"
> "1:\n\t"
> "shrl $1,%%ecx\n\t"
> "jnc 2f\n\t"
> "movsw\n"
> "2:\n\t"
> "rep; movsl"
> : "+c" (__n), "+@S" (__src), "+@D" (__dest));
> return savedest;
> }
>
> @ is a character not otherwise used in constraints; it means 'the
> value here is a pointer and the memory pointed to will be accessed'.
Why isn't it documented? Is it a kind of "new" one?
(The only remark is - it must be "+@&S" etc., there are the
earlyclobbered operands.)
> Exactly how much memory, and the nature of the access, are
> determined by the type of the pointer. Here, both pointers are
> restrict- qualified and point to memory blocks of known size (that's
> what "static __n" in the brackets means). Furthermore, __src points
> to constant memory, so that block is only read, whereas __dest is
> not constant so the compiler shall assume it's written.
Does this "@" behaves as well with unrestricted pointers like just
(char *s)?
> I had to change the types from void to char so the size expressions
> would be meaningful; if you were actually to use this to implement
> memcpy, you'd wrap it in another inline function that casted the
> arguments.
>
> I think that's all that should be needed. Thoughts?
I've just tried this (on mine examples, with unrestricted pointers).
The things seem to be fine. Not ideal (reloading suffers sometimes,
but this is not the @-specific problem), but completely free of the
problems introduced by "m".
next prev parent reply other threads:[~2003-12-27 11:35 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-25 0:20 i386 inline-asm string functions - some questions Denis Zaitsev
2003-12-25 0:38 ` Richard Henderson
2003-12-25 1:15 ` Denis Zaitsev
2003-12-25 1:21 ` Zack Weinberg
2003-12-25 1:45 ` Denis Zaitsev
2003-12-26 3:40 ` Zack Weinberg
2003-12-27 4:58 ` Richard Henderson
2003-12-27 10:24 ` Zack Weinberg
2003-12-27 11:35 ` Denis Zaitsev [this message]
2003-12-27 18:38 ` Zack Weinberg
2003-12-28 20:58 ` Denis Zaitsev
2003-12-29 2:22 ` Zack Weinberg
2003-12-29 2:44 ` Denis Zaitsev
2003-12-29 2:46 ` Zack Weinberg
2003-12-29 2:53 ` Denis Zaitsev
2003-12-29 3:35 ` Ulrich Drepper
2003-12-29 3:54 ` Andrew Pinski
2003-12-29 6:57 ` Jakub Jelinek
2003-12-29 3:56 ` Zack Weinberg
2003-12-29 5:31 ` Daniel Jacobowitz
2003-12-29 5:55 ` Zack Weinberg
2003-12-29 18:37 ` Denis Zaitsev
2003-12-29 19:09 ` Zack Weinberg
2003-12-29 19:31 ` Denis Zaitsev
2003-12-29 19:37 ` Denis Zaitsev
2003-12-29 18:51 ` Denis Zaitsev
2003-12-29 19:15 ` Zack Weinberg
2003-12-27 10:52 ` Denis Zaitsev
[not found] ` <20031225060850.C7419@zzz.ward.six>
[not found] ` <20031225012711.GD13447@redhat.com>
2003-12-25 1:38 ` Denis Zaitsev
2003-12-25 1:53 ` Richard Henderson
2003-12-25 2:08 ` Denis Zaitsev
2003-12-25 0:39 ` Roland McGrath
2003-12-25 1:13 ` Denis Zaitsev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20031227163540.B6728@zzz.ward.six \
--to=zzz@anda.ru \
--cc=aj@suse.de \
--cc=gcc@gcc.gnu.org \
--cc=libc-alpha@sources.redhat.com \
--cc=linux-gcc@vger.kernel.org \
--cc=rth@redhat.com \
--cc=zack@codesourcery.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).