svn commit: r325765 - head/lib/libc/string

Mark Millard markmi at dsl-only.net
Thu Nov 16 09:20:03 UTC 2017


On 2017-Nov-15, at 11:15 PM, Bruce Evans <brde at optusnet.com.au> wrote:

> On Wed, 15 Nov 2017, Mark Millard wrote:
> 
>> Bruce Evans brde at optusnet.com.au wrote on
>> Tue Nov 14 12:41:50 UTC 2017 :
>> 
>>> . . .
> 
>> head/sys/arm/arm/support.S ( -r283366 ) has
>> 
>> 394	ENTRY(bcopy)
>> 395	        /* switch the source and destination registers */
>> 396	        eor     r0, r1, r0
>> 397	        eor     r1, r0, r1
>> 398	        eor     r0, r1, r0
>> 399	EENTRY(memmove)
>> 400	        /* Do the buffers overlap? */
>> 401	        cmp     r0, r1
>> 402	        RETeq           /* Bail now if src/dst are the same */
>> 403	        subcc   r3, r0, r1      /* if (dst > src) r3 = dst - src */
>> 404	        subcs   r3, r1, r0      /* if (src > dsr) r3 = src - dst */
>> 405	        cmp     r3, r2          /* if (r3 < len) we have an overlap */
>> 406	        bcc     PIC_SYM(_C_LABEL(memcpy), PLT)
>> . . .
> 
> . . .
> 
>> head/lib/libc/arm/string/memmove.S ( -r288373 ) has:
>> 
>> 37	#ifndef _BCOPY
>> 38	/* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */
>> 39	ENTRY(memmove)
>> 40	#else
>> 41	/* bcopy = memcpy/memmove with arguments reversed. */
>> 42	/* LINTSTUB: Func: void bcopy(void *, void *, size_t) */
>> 43	ENTRY(bcopy)
>> 44	        /* switch the source and destination registers */
>> 45	        eor     r0, r1, r0
>> 46	        eor     r1, r0, r1
>> 47	        eor     r0, r1, r0
>> 48	#endif
> 
> . . .
> 
>> 49	        /* Do the buffers overlap? */
>> 50	        cmp     r0, r1
>> 51	        it      eq
>> 52	        RETeq           /* Bail now if src/dst are the same */
>> 53	        ite     cc
>> 54	        subcc   r3, r0, r1      /* if (dst > src) r3 = dst - src */
>> 55	        subcs   r3, r1, r0      /* if (src > dsr) r3 = src - dst */
>> 56	        cmp     r3, r2          /* if (r3 < len) we have an overlap */
>> 57	        bcc     PIC_SYM(_C_LABEL(memcpy), PLT)
>> . . .
>> 
>> . . .
> 
> The above only shows memmove() calling bcopy(), only in the kernel.
> . . .

I was referring to the "bcc PIC_SYM(_C_LABEL(memcpy), PLT)", which
jumps to memcpy, not to bcopy. In both bcopy and memcopy there
is the conditional jump to memcpy, in both libc/arm/string/memmove.S
and sys/arm/arm/support.S .

(I claim that the selection code is wrong and should follow the
later netbsd update. Netbsd used to have code like the above
for those details. But that is not the primary point here. Also:
This note is from my memory of the issue, not a recent recheck.)

>> ...
>> Another issue is if compilers (clang, gcc) are well
>> controlled for code substitutions to preserve
>> supposed FreeBSD rules about where overlaps
>> are well defined. The library code might not be all
>> there is to it as far as behavior goes for
>> compiled C/C++ source code.
> 
> Do you mean the namespace stuff?

No, I meant what you later referred to with:

QUOTE
 Without -ffreestanding,
compilers can and do often implement memcpy inline.  Then overlapping
copies are not supported.  The BUGS section in memcpy.3 should just be
deleted.
END QUOTE

Sufficient memcpy behavior documentation for C/C++/. . .
has to consider compiler code generation as well, not
just what the library code does if called.

>> head/sys/arm64/arm64/ is similar for bcopy/memmove
>> calling memcpy (but head/lib/libc/amd64/string/ is
>> not):
> 
> It is reasonable, and possibly best, to implement the usual
> non-overlapping case by a call to memcpy() and not try hard to
> optimize the overlapping case (or jump far away to it to keep
> caches cleaner for the usual case).

arm (32-bit) has code attempting to pick when to
do a "bcc PIC_SYM(_C_LABEL(memcpy), PLT)" from
memmove and from bcopy.

>> head/sys/arm64/arm64/memmove.S ( -r307909 ) has:
>> ...
>> head/lib/libc/amd64/string/bcopy.S has:
>> ...

My switch to looking at libc/amd64/'s bcopy.S was
unintentional. I was thinking of arm64 when looking
at amd64 --even though there was a libc/aarch64/
right there to see as well.

head/contrib/cortex-strings/src/aarch64/memmove.S's
code has memmove use memcpy for backwards copies
with overlapping objects and for forward copies with
small-enough overlapping objects.

(head/contrib/cortex-strings/src/aarch64/memcpy.S
handles overlaps in small-enough objects by reading
the object in completely before writing any of it
back out. memmove depends on such details.)

The details here are different from the 32-bit arm
handling of memmove and memcpy.

===
Mark Millard
markmi at dsl-only.net



More information about the svn-src-head mailing list