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