1*84d9c625SLionel Sambuc/*- 2*84d9c625SLionel Sambuc * Copyright (c) 2013 The NetBSD Foundation, Inc. 3*84d9c625SLionel Sambuc * All rights reserved. 4*84d9c625SLionel Sambuc * 5*84d9c625SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation 6*84d9c625SLionel Sambuc * by Matt Thomas of 3am Software Foundry. 7*84d9c625SLionel Sambuc * 8*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without 9*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions 10*84d9c625SLionel Sambuc * are met: 11*84d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 12*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer. 13*84d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 14*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 15*84d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution. 16*84d9c625SLionel Sambuc * 17*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18*84d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19*84d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20*84d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21*84d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*84d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*84d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*84d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*84d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*84d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 28*84d9c625SLionel Sambuc */ 29*84d9c625SLionel Sambuc 30*84d9c625SLionel Sambuc#include <machine/asm.h> 31*84d9c625SLionel Sambuc 32*84d9c625SLionel SambucRCSID("$NetBSD: memset_naive.S,v 1.1 2013/01/08 20:15:00 matt Exp $") 33*84d9c625SLionel Sambuc 34*84d9c625SLionel Sambuc/* 35*84d9c625SLionel Sambuc * This isn't quite as simple/short as it could be but the truly trivial 36*84d9c625SLionel Sambuc * memset was an order of magnitude slower than this. 37*84d9c625SLionel Sambuc */ 38*84d9c625SLionel Sambuc 39*84d9c625SLionel SambucENTRY(memset) 40*84d9c625SLionel Sambuc/* LINTSTUB: void *memset(void *, int, size_t) */ 41*84d9c625SLionel Sambuc mov ip, r0 /* need to preserve r0 */ 42*84d9c625SLionel Sambuc cmp r2, #10 /* 10 bytes or less? */ 43*84d9c625SLionel Sambuc ble .Lbyte_by_byte /* yes, bytewise is faster */ 44*84d9c625SLionel Sambuc ands r3, r1, #0xff /* we are dealing with bytes */ 45*84d9c625SLionel Sambuc orrne r3, r3, r3, lsl #8 /* move value into 2nd byte lane */ 46*84d9c625SLionel Sambuc orrne r3, r3, r3, lsl #16 /* move value into all byte lanes */ 47*84d9c625SLionel Sambuc mov r1, r2 /* move count */ 48*84d9c625SLionel Sambuc ands r2, ip, #7 /* are we dword aligned? */ 49*84d9c625SLionel Sambuc beq 1f /* yes we are */ 50*84d9c625SLionel Sambuc rsb r2, r2, #8 /* how many bytes until aligned? */ 51*84d9c625SLionel Sambuc sub r1, r1, r2 /* subtract from count */ 52*84d9c625SLionel Sambuc tst ip, #1 /* halfword aligned? */ 53*84d9c625SLionel Sambuc strneb r3, [ip], #1 /* nope, write a byte */ 54*84d9c625SLionel Sambuc tst ip, #2 /* word aligned? */ 55*84d9c625SLionel Sambuc strneh r3, [ip], #2 /* nope, write a halfword */ 56*84d9c625SLionel Sambuc tst ip, #4 /* dword aligned? */ 57*84d9c625SLionel Sambuc strne r3, [ip], #4 /* nope, write a word */ 58*84d9c625SLionel Sambuc /* 59*84d9c625SLionel Sambuc * At this point, we are dword aligned. 60*84d9c625SLionel Sambuc */ 61*84d9c625SLionel Sambuc1: mov r2, r3 /* duplicate fill value */ 62*84d9c625SLionel Sambuc2: subs r1, r1, #16 /* can we write 16 bytes? */ 63*84d9c625SLionel Sambuc stmgeia ip!, {r2,r3} /* yes, write the first 8 of them */ 64*84d9c625SLionel Sambuc stmgeia ip!, {r2,r3} /* yes, write the second 8 of them */ 65*84d9c625SLionel Sambuc bgt 2b /* more left to fill */ 66*84d9c625SLionel Sambuc RETc(eq) /* no, return */ 67*84d9c625SLionel Sambuc /* 68*84d9c625SLionel Sambuc * Our count went negative but the bits below 16 haven't changed. 69*84d9c625SLionel Sambuc * So we are effectively testing modulo 16. 70*84d9c625SLionel Sambuc */ 71*84d9c625SLionel Sambuc tst r1, #8 /* can we write at least 8 bytes? */ 72*84d9c625SLionel Sambuc stmneia ip!, {r2,r3} /* so do it */ 73*84d9c625SLionel Sambuc tst r1, #4 /* can we write at least 4 bytes? */ 74*84d9c625SLionel Sambuc strne r3, [ip], #4 /* so do it */ 75*84d9c625SLionel Sambuc tst r1, #2 /* can we write at least 2 bytes? */ 76*84d9c625SLionel Sambuc strneh r3, [ip], #2 /* so do it */ 77*84d9c625SLionel Sambuc tst r1, #1 /* can we write 1 bytes? */ 78*84d9c625SLionel Sambuc strneb r3, [ip], #1 /* so do it */ 79*84d9c625SLionel Sambuc RET /* return */ 80*84d9c625SLionel Sambuc 81*84d9c625SLionel Sambuc.Lbyte_by_byte: 82*84d9c625SLionel Sambuc subs r2, r2, #1 /* can we write a byte? */ 83*84d9c625SLionel Sambuc RETc(lt) /* no, return */ 84*84d9c625SLionel Sambuc strb r3, [ip], #1 /* write a byte */ 85*84d9c625SLionel Sambuc b .Lbyte_by_byte /* do next byte */ 86*84d9c625SLionel SambucEND(memset) 87