1 1.7 matt /* $NetBSD: memset.S,v 1.7 2011/01/17 07:07:36 matt Exp $ */ 2 1.1 matt 3 1.1 matt /*- 4 1.1 matt * Copyright (C) 2003 Matt Thomas <matt (at) 3am-software.com> 5 1.3 keihan * Copyright (C) 2001 Martin J. Laubach <mjl (at) NetBSD.org> 6 1.1 matt * All rights reserved. 7 1.1 matt * 8 1.1 matt * Redistribution and use in source and binary forms, with or without 9 1.1 matt * modification, are permitted provided that the following conditions 10 1.1 matt * are met: 11 1.1 matt * 1. Redistributions of source code must retain the above copyright 12 1.1 matt * notice, this list of conditions and the following disclaimer. 13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 matt * notice, this list of conditions and the following disclaimer in the 15 1.1 matt * documentation and/or other materials provided with the distribution. 16 1.1 matt * 3. The name of the author may not be used to endorse or promote products 17 1.1 matt * derived from this software without specific prior written permission. 18 1.1 matt * 19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 1.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 1.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 1.1 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 1.1 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 1.1 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 1.1 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 1.1 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 1.1 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 1.1 matt * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 1.1 matt */ 30 1.1 matt /*----------------------------------------------------------------------*/ 31 1.1 matt 32 1.1 matt #include <machine/asm.h> 33 1.1 matt 34 1.1 matt /*----------------------------------------------------------------------*/ 35 1.1 matt /* 36 1.1 matt void bzero(void *b r3, size_t len r4); 37 1.6 christos void *memset(void *b r3, int c r4, size_t len r5); 38 1.1 matt */ 39 1.1 matt /*----------------------------------------------------------------------*/ 40 1.1 matt 41 1.1 matt #define r_dst %r4 42 1.1 matt #define r_len %r5 43 1.1 matt #define r_tmp %r6 44 1.1 matt #define r_val %r0 45 1.1 matt 46 1.1 matt ENTRY(bzero) 47 1.1 matt li r_val, 0 /* Value to fill with */ 48 1.1 matt mr r_len, %r4 49 1.1 matt b cb_memset 50 1.1 matt 51 1.1 matt ENTRY(memset) 52 1.1 matt mr r_val, %r4 /* Value to fill with */ 53 1.1 matt 54 1.1 matt cb_memset: 55 1.1 matt cmplwi r_len, 0 /* is the length 0? */ 56 1.1 matt beqlr- /* nothing to do */ 57 1.1 matt mr r_dst, %r3 58 1.1 matt /* 59 1.1 matt * r3=start, r4=dstptr, r5=length, r0=fill-val 60 1.1 matt */ 61 1.1 matt cmplwi r_len, 6 /* more than 6 bytes? */ 62 1.1 matt bgt complex_fill /* do it the complex way */ 63 1.1 matt subi r_dst, r_dst, 1 /* presubtract for stbu */ 64 1.1 matt simple_fill: mtctr r_len /* set CTR */ 65 1.1 matt 1: stbu r_val, 1(r_dst) /* update memory */ 66 1.1 matt bdnz+ 1b /* until CTR is 0 */ 67 1.1 matt blr /* return */ 68 1.1 matt 69 1.1 matt complex_fill: 70 1.1 matt rlwimi r_val, r_val, 8, 16, 23 /* word extend fill value */ 71 1.1 matt rlwimi r_val, r_val, 16, 0, 15 72 1.4 perry andi. r_tmp, r_dst, 0x03 73 1.1 matt beq+ word_fill /* already aligned to word? */ 74 1.4 perry andi. r_tmp, r_dst, 0x01 75 1.1 matt beq half_fill /* aligned to halfword? */ 76 1.1 matt stb r_val, 0(r_dst) 77 1.1 matt addi r_dst, r_dst, 1 78 1.1 matt subi r_len, r_len, 1 /* subtract byte */ 79 1.1 matt andi. r_tmp, r_dst, 0x02 80 1.1 matt beq word_fill /* aligned to word? */ 81 1.1 matt half_fill: 82 1.2 matt sth r_val, 0(r_dst) 83 1.1 matt addi r_dst, r_dst, 2 84 1.1 matt subi r_len, r_len, 2 /* subtract halfword */ 85 1.1 matt 86 1.1 matt word_fill: 87 1.1 matt cmplwi r_len, 4 /* we have more than 4 bytes? */ 88 1.1 matt blt- trailing_bytes /* no? finish writing */ 89 1.1 matt srwi r_tmp, r_len, 2 /* get word count */ 90 1.1 matt mtctr r_tmp 91 1.1 matt subi r_dst, r_dst, 4 92 1.1 matt 1: stwu r_val, 4(r_dst) 93 1.1 matt bdnz+ 1b 94 1.1 matt 95 1.1 matt trailing_bytes: 96 1.1 matt andi. r_len, r_len, 0x03 /* how much left? */ 97 1.1 matt beqlr+ /* nothing? return */ 98 1.1 matt addi r_dst, r_dst, 3 99 1.1 matt b simple_fill 100