mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Denys Vlasenko <vda.linux@googlemail.com>
To: Rich Felker <dalias@libc.org>
Cc: musl <musl@lists.openwall.com>
Subject: Re: [PATCH] x86_64/memset: use "small block" code for blocks up to 30 bytes long
Date: Sun, 15 Feb 2015 22:44:59 +0100	[thread overview]
Message-ID: <CAK1hOcMgM5j-EtOk2aPao6ma=M7PVyA_3U=22+8HbPu+S9GXdw@mail.gmail.com> (raw)
In-Reply-To: <20150215150313.GO23507@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 1466 bytes --]

On Sun, Feb 15, 2015 at 4:03 PM, Rich Felker <dalias@libc.org> wrote:
>> Just because we don't personally see a hit from 6-cycle imul of AMD CPUs,
>> it does not mean people who do use those CPUs don't exist. Have heart...
>
> Did you test the version I attached? I think there should be at least
> 4-5 cycles between when the imul is launched and when the result is
> used, so I'm failing to see how the latency is a big deal.

Okay, I won't insist.
Your version works good. The "rep stosq" setup time is still noticeable
even when we switch to it after 126:

129 byte block: 10.37 bytes/ns
128 byte block: 10.65 bytes/ns
127 byte block: 10.58 bytes/ns
126 byte block: 18.44 bytes/ns
125 byte block: 18.30 bytes/ns
124 byte block: 18.15 bytes/ns

but I don't think we should do anything about this.


Here

        lea -1(%rdx),%rcx
        cmp $126,%rcx
        jae 2f

you'd have a stall, since cmp needs the result of lea. why not this?

        lea -1(%rdx),%rcx
        cmp $127,%rdx
        jae 2f

then you can even move lea to "big buf" code part
(no point doing it in "small buf" code where it is not used).


Possible bug: this check seems misplaced:

2:      test %rdx,%rdx
        jz 1b

it should be before byte stores:
        mov %sil,(%rdi)
        mov %sil,-1(%rdi,%rdx)
        cmp $2,%edx
        jbe 1f
otherwise memset of zero length will fill two bytes, at buf[0] and buf[-1]


"sub $8,%rcx" can be folded into lea.

Please see attached file.

[-- Attachment #2: vda1.s --]
[-- Type: application/octet-stream, Size: 1056 bytes --]

.global memset
.type memset,@function
memset:
	movzbq %sil,%rax
	mov $0x101010101010101,%r8
	imul %r8,%rax

	cmp $127,%rdx
	jae 2f

	test %rdx,%rdx
	jz 1f

	mov %sil,(%rdi)
	mov %sil,-1(%rdi,%rdx)
	cmp $2,%edx
	jbe 1f

	mov %ax,1(%rdi)
	mov %ax,(-1-2)(%rdi,%rdx)
	cmp $6,%edx
	jbe 1f

	mov %eax,(1+2)(%rdi)
	mov %eax,(-1-2-4)(%rdi,%rdx)
	cmp $14,%edx
	jbe 1f

	mov %rax,(1+2+4)(%rdi)
	mov %rax,(-1-2-4-8)(%rdi,%rdx)
	cmp $30,%edx
	jbe 1f

	mov %rax,(1+2+4+8)(%rdi)
	mov %rax,(1+2+4+8+8)(%rdi)
	mov %rax,(-1-2-4-8-16)(%rdi,%rdx)
	mov %rax,(-1-2-4-8-8)(%rdi,%rdx)
	cmp $62,%edx
	jbe 1f

	mov %rax,(1+2+4+8+16)(%rdi)
	mov %rax,(1+2+4+8+16+8)(%rdi)
	mov %rax,(1+2+4+8+16+16)(%rdi)
	mov %rax,(1+2+4+8+16+24)(%rdi)
	mov %rax,(-1-2-4-8-16-32)(%rdi,%rdx)
	mov %rax,(-1-2-4-8-16-24)(%rdi,%rdx)
	mov %rax,(-1-2-4-8-16-16)(%rdi,%rdx)
	mov %rax,(-1-2-4-8-16-8)(%rdi,%rdx)

1:	mov %rdi,%rax
	ret

2:	lea -9(%rdx),%rcx
	mov %rdi,%r8
	shr $3,%rcx
	mov %rax,(%rdi)
	mov %rax,-16(%rdi,%rdx)
	mov %rax,-8(%rdi,%rdx)
	add $8,%rdi
	and $-8,%rdi
	rep
	stosq
	mov %r8,%rax
	ret

  reply	other threads:[~2015-02-15 21:44 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-13 16:39 Denys Vlasenko
2015-02-14 19:35 ` Rich Felker
2015-02-15  4:06   ` Rich Felker
2015-02-15 14:07     ` Denys Vlasenko
2015-02-15 15:03       ` Rich Felker
2015-02-15 21:44         ` Denys Vlasenko [this message]
2015-02-15 22:55           ` Rich Felker
2015-02-16 10:09             ` Denys Vlasenko
2015-02-16 15:12               ` Rich Felker
2015-02-16 17:36           ` Rich Felker
2015-02-17 13:08             ` Denys Vlasenko
2015-02-17 16:12               ` Rich Felker
2015-02-17 16:51                 ` Denys Vlasenko
2015-02-17 17:30                   ` Denys Vlasenko
2015-02-17 17:40                   ` Rich Felker
2015-02-17 18:53                     ` Denys Vlasenko
2015-02-17 21:12                       ` Rich Felker
2015-02-18  9:05                         ` Denys Vlasenko

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='CAK1hOcMgM5j-EtOk2aPao6ma=M7PVyA_3U=22+8HbPu+S9GXdw@mail.gmail.com' \
    --to=vda.linux@googlemail.com \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).