memset.S revision 1.1
1/* $NetBSD: memset.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */ 2 3/*- 4 * Copyright (c) 2014 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Thomas of 3am Software Foundry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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. 30 */ 31 32#include <machine/asm.h> 33 34 35#if defined(LIBC_SCCS) && !defined(lint) 36__RCSID("$NetBSD: memset.S,v 1.1 2014/09/03 19:34:25 matt Exp $") 37#endif /* LIBC_SCCS && !lint */ 38 39/*----------------------------------------------------------------------*/ 40/* 41 void bzero(void *b r3, size_t len r4); 42 void * memset(void *b r3, int c r4, size_t len r5); 43*/ 44/*----------------------------------------------------------------------*/ 45 46 47#ifdef _BZERO 48#define r_fill r0 49ENTRY(bzero) 50#else 51#define r_fill r4 52ENTRY(memset) 53#endif 54#ifdef _BZERO 55 l.ori r5, r4, 0 56 l.or r4, r0, r0 57#else 58 l.or r11, r3, r0 /* move start to return value */ 59#endif 60 l.sfeqi r5, 0 /* anything to do? */ 61 l.bf .Lret /* no, just return */ 62 l.nop 63 64 l.sfgeui r5, 7 /* small buffer? */ 65 l.add r5, r5, r3 /* r5 is end pointer */ 66 l.bnf .Lbyte_fill /* yes. just byte fill */ 67 l.nop 68 69#ifndef _BZERO 70 // Let's see the fill type 71 l.sfeqi r4, 0 /* filling with 0? */ 72 l.bf .Lalignment_check /* don't to replicate */ 73 l.nop 74 l.extbz r4, r4 /* truncate to 8 bits */ 75 l.slli r13, r4, 8 /* shift left 8 bits */ 76 l.or r4, r4, r13 /* merge the two bytes */ 77 l.slli r13, r4, 16 /* shift left 16 bits */ 78 l.or r4, r4, r13 /* merge the two halves */ 79 80.Lalignment_check: 81#endif 82 l.andi r13, r3, 3 /* get low bits of start */ 83 l.sfeqi r13, 0 /* word aligned? */ 84 l.bf .Lword_fill /* yes, start setting */ 85 l.nop 86 87 l.add r5, r5, r13 /* increase length */ 88 l.sub r3, r3, r13 /* mask word aligned */ 89 l.slli r13, r13, 3 /* bytes to bits */ 90 l.addi r15, r13, -8 /* minus one byte */ 91 92 l.lwz r6, 0(r3) /* get first word */ 93 l.movhi r7, 0xff00 /* 0xff000000 */ 94 l.sra r7, r7, r13 /* shift right align bytes */ 95 l.and r6, r6, r7 /* clear bytes to be filled */ 96#ifndef _BZERO 97 l.srl r7, r_fill, r13 /* clear bytes to preserve */ 98 l.or r6, r6, r7 /* merge existing with new */ 99#endif 100 l.sw 0(r3), r6 /* store first word */ 101 l.addi r3, r3, 4 /* advance to next word */ 102 l.addi r5, r5, -4 /* one less word to do */ 103 104.Lword_aligned: 105 l.srli r6, r5, 2 /* clear low two bits of len */ 106 l.srli r6, r6, 2 /* ... */ 107 l.sfgeu r3, r6 /* any more full words? */ 108 l.bf .Lend_fill /* no, handle the last bytes */ 109 l.nop 110 111.Lword_fill: 112 l.sw 0(r3), r_fill /* store a word */ 113 l.addi r3, r3, 4 /* advance */ 114 l.sfgeu r3, r6 /* any more full words? */ 115 l.bnf .Lword_fill /* yes, fill next word */ 116 l.nop 117 l.j .Lend_fill /* fill any leftover bytes */ 118 l.nop 119 120.Lbyte_fill: 121 l.sb 0(r3), r_fill /* store a byte */ 122 l.addi r3, r3, 1 /* advance */ 123.Lend_fill: 124 l.sfeq r3, r5 /* at the end? */ 125 l.bnf .Lbyte_fill /* no, fill next byte */ 126 l.nop 127 128.Lret: 129 l.jr lr /* return */ 130 l.nop 131#ifdef _BZERO 132END(bzero) 133#else 134END(memset) 135#endif 136