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