update from main archive 961105
[jlayton/glibc.git] / sysdeps / alpha / memset.S
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2    Contributed by Richard Henderson (rth@tamu.edu)
3
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB.  If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA.  */
20
21 /* Fill a block of memory with a character.  Optimized for the Alpha
22    architecture:
23
24    - memory accessed as aligned quadwords only
25    - destination memory not read unless needed for good cache behaviour
26    - basic blocks arranged to optimize branch prediction for full-quadword
27      aligned memory blocks.
28    - partial head and tail quadwords constructed with byte-mask instructions
29
30    This is generally scheduled for the EV5 (got to look out for my own
31    interests :-), but with EV4 needs in mind.  There *should* be no more
32    stalls for the EV4 than there are for the EV5.
33 */
34
35
36 #include <sysdep.h>
37
38         .set noat
39         .set noreorder
40
41         .text
42
43 /* There is a problem with either gdb (as of 4.16) or gas (as of 2.7) that
44    doesn't like putting the entry point for a procedure somewhere in the
45    middle of the procedure descriptor.  Work around this by putting the main
46    loop in its own procedure descriptor.  */
47
48         /* On entry to this basic block:
49            t3 == loop counter
50            t4 == bytes in partial final word
51            a0 == possibly misaligned destination pointer
52            a1 == replicated source character  */
53
54         .ent memset_loop
55         .align 3
56 memset_loop:
57         .frame sp, 0, ra, 0
58         .prologue 0
59
60         beq     t3, $tail
61         blbc    t3, 0f          # skip single store if count even
62
63         stq_u   a1, 0(a0)       # e0    : store one word
64         subq    t3, 1, t3       # .. e1 :
65         addq    a0, 8, a0       # e0    :
66         beq     t3, $tail       # .. e1 :
67
68 0:      stq_u   a1, 0(a0)       # e0    : store two words
69         subq    t3, 2, t3       # .. e1 :
70         stq_u   a1, 8(a0)       # e0    :
71         addq    a0, 16, a0      # .. e1 :
72         bne     t3, 0b          # e1    :
73
74 $tail:  bne     t4, 1f          # is there a tail to do?
75         ret                     # no
76
77         .align 3
78 1:      ldq_u   t0, 0(a0)       # e1    : yes, load original data
79         mskql   a1, t4, t1      # .. e0 :
80         mskqh   t0, t4, t0      # e0    :
81         or      t0, t1, t0      # e1 (stall)
82         stq_u   t0, 0(a0)       # e0    :
83         ret                     # .. e1 :
84
85         .end memset_loop
86
87 ENTRY(memset)
88 #ifdef PROF
89         ldgp    gp, 0(pv)
90         lda     AT, _mcount
91         jsr     AT, (AT), _mcount
92         .prologue 1
93 #else
94         .prologue 0
95 #endif
96
97         zapnot  a1, 1, a1       # e0    : zero extend input character
98         mov     a0, v0          # .. e1 : move return value in place
99         sll     a1, 8, t0       # e0    : begin replicating the char
100         beq     a2, $done       # .. e1 : early exit for zero-length store
101         or      t0, a1, a1      # e0    :
102         and     a0, 7, t1       # .. e1 : dest misalignment
103         sll     a1, 16, t0      # e0    :
104         addq    a2, t1, a2      # .. e1 : add dest misalignment to count
105         or      t0, a1, a1      # e0    :
106         srl     a2, 3, t3       # .. e1 : loop = count >> 3
107         sll     a1, 32, t0      # e0    :
108         and     a2, 7, t4       # .. e1 : find number of bytes in tail
109         or      t0, a1, a1      # e0    : character replication done
110
111         beq     t1, memset_loop # .. e1 : aligned head, jump right in
112
113         ldq_u   t0, 0(a0)       # e1    : load original data to mask into
114         mskqh   a1, a0, t1      # .. e0 :
115
116         cmpult  a2, 8, t2       # e0    : is this a sub-word set?
117         bne     t2, $oneq       # .. e1 (zdb)
118
119         mskql   t0, a0, t0      # e0    : we span words.  finish this partial
120         subq    t3, 1, t3       # .. e1 :
121         addq    a0, 8, a0       # e0    :
122         or      t0, t1, t0      # .. e1 :
123         stq_u   t0, -8(a0)      # e0    :
124         br      memset_loop     # .. e1 :
125
126         .align 3
127 $oneq:
128         mskql   t1, a2, t1      # e0    : entire operation within one word
129         mskql   t0, a0, t2      # e0    :
130         mskqh   t0, a2, t3      # e0    :
131         or      t1, t2, t0      # .. e1 :
132         or      t0, t3, t0      # e1    :
133         stq_u   t0, 0(a0)       # e0 (stall)
134
135 $done:  ret
136
137         END(memset)