Update copyright notices with scripts/update-copyrights.
[jlayton/glibc.git] / ports / sysdeps / tile / tilepro / memset.c
1 /* Copyright (C) 2011-2013 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <string.h>
20 #include <stdint.h>
21 #include <arch/chip.h>
22
23 void *
24 __memset (void *s, int c, size_t n)
25 {
26   uint32_t *out32;
27   int n32;
28   uint32_t v16, v32;
29   uint8_t *out8 = s;
30   int to_align32;
31
32   /* Experimentation shows that a trivial tight loop is a win up until
33      around a size of 20, where writing a word at a time starts to win.  */
34 #define BYTE_CUTOFF 20
35
36 #if BYTE_CUTOFF < 3
37   /* This must be at least at least this big, or some code later
38      on doesn't work.  */
39 # error "BYTE_CUTOFF is too small."
40 #endif
41
42   if (n < BYTE_CUTOFF)
43     {
44       /* Strangely, this turns out to be the tightest way to write
45          this loop.  */
46       if (n != 0)
47         {
48           do
49             {
50               /* Strangely, combining these into one line performs worse.  */
51               *out8 = c;
52               out8++;
53             }
54           while (--n != 0);
55         }
56
57       return s;
58     }
59
60   /* Align 'out8'. We know n >= 3 so this won't write past the end. */
61   while (((uintptr_t) out8 & 3) != 0)
62     {
63       *out8++ = c;
64       --n;
65     }
66
67   /* Align 'n'. */
68   while (n & 3)
69     out8[--n] = c;
70
71   out32 = (uint32_t *) out8;
72   n32 = n >> 2;
73
74   /* Tile input byte out to 32 bits. */
75   v16 = __insn_intlb (c, c);
76   v32 = __insn_intlh (v16, v16);
77
78   /* This must be at least 8 or the following loop doesn't work. */
79 #define CACHE_LINE_SIZE_IN_WORDS (CHIP_L2_LINE_SIZE() / 4)
80
81   /* Determine how many words we need to emit before the 'out32'
82      pointer becomes aligned modulo the cache line size.  */
83   to_align32 = (-((uintptr_t) out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 1);
84
85   /* Only bother aligning and using wh64 if there is at least one full
86      cache line to process.  This check also prevents overrunning the
87      end of the buffer with alignment words.  */
88   if (to_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS)
89     {
90       int lines_left;
91
92       /* Align out32 mod the cache line size so we can use wh64. */
93       n32 -= to_align32;
94       for (; to_align32 != 0; to_align32--)
95         {
96           *out32 = v32;
97           out32++;
98         }
99
100       /* Use unsigned divide to turn this into a right shift. */
101       lines_left = (unsigned) n32 / CACHE_LINE_SIZE_IN_WORDS;
102
103       do
104         {
105           /* Only wh64 a few lines at a time, so we don't exceed the
106              maximum number of victim lines.  */
107           int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS ())? lines_left
108                    : CHIP_MAX_OUTSTANDING_VICTIMS ());
109           uint32_t *wh = out32;
110           int i = x;
111           int j;
112
113           lines_left -= x;
114
115           do
116             {
117               __insn_wh64 (wh);
118               wh += CACHE_LINE_SIZE_IN_WORDS;
119             }
120           while (--i);
121
122           for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4); j != 0; j--)
123             {
124               *out32++ = v32;
125               *out32++ = v32;
126               *out32++ = v32;
127               *out32++ = v32;
128             }
129         }
130       while (lines_left != 0);
131
132       /* We processed all full lines above, so only this many words
133          remain to be processed.  */
134       n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;
135     }
136
137   /* Now handle any leftover values. */
138   if (n32 != 0)
139     {
140       do
141         {
142           *out32 = v32;
143           out32++;
144         }
145       while (--n32 != 0);
146     }
147
148   return s;
149 }
150 weak_alias (__memset, memset)
151 libc_hidden_builtin_def (memset)