1 /* $NetBSD: memset.S,v 1.5 2014/05/22 16:47:31 pooka Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <machine/asm.h>
34 #if defined(LIBC_SCCS)
35 RCSID("$NetBSD: memset.S,v 1.5 2014/05/22 16:47:31 pooka Exp $")
39 /* bzero, %rdi is buffer, %rsi length */
42 mov %rsi,%rdx /* length */
43 xor %eax,%eax /* value to write */
48 /* memset, %rdi is buffer, %rsi char to fill, %rdx length */
51 movzbq %sil,%rax /* byte value to fill */
52 mov %rdx,%rsi /* copy of length */
53 mov $0x0101010101010101,%r9
54 imul %r9,%rax /* fill value in all bytes */
57 mov %rdi,%r9 /* Need to return buffer address */
58 or %edi,%edx /* address | length */
61 jbe 10f /* jump if short fill */
62 test $7,%dl /* check for misaligned fill */
63 jnz 20f /* jump if misaligned */
65 /* Target aligned and length multiple of 8 */
73 * Short transfer, any faffing here will generate mispredicted branches.
74 * So we keep it simple.
81 * Buffer or length misaligned.
82 * Write pattern to first and last word of buffer, then fill middle.
83 * (This writes to some bytes more than once - possibly three times!.)
87 movzbq %dil,%rdx /* low address for alignment */
88 mov %rax,-8(%rcx,%rdi)
89 and $7,%dl /* offset in word */
90 sub %rdx,%rcx /* adjust length ... */
91 add %rdx,%rdi /* ... and target */