Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / tile / tilegx / memset.c
blobd41b205a3c96bbf96c897941cfb73ae9bdb1a40b
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
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.
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.
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/>. */
19 #include <arch/chip.h>
20 #include <string.h>
21 #include <stdint.h>
22 #include "string-endian.h"
24 void *
25 __memset (void *s, int c, size_t n)
27 uint64_t *out64;
28 int n64, to_align64;
29 uint64_t v64;
30 uint8_t *out8 = s;
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
36 #if BYTE_CUTOFF < 7
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
42 if (n < BYTE_CUTOFF)
44 /* Strangely, this turns out to be the tightest way to write
45 this loop. */
46 if (n != 0)
50 /* Strangely, combining these into one line performs worse. */
51 *out8 = c;
52 out8++;
54 while (--n != 0);
57 return s;
60 /* Align 'out8'. We know n >= 7 so this won't write past the end. */
61 while (((uintptr_t) out8 & 7) != 0)
63 *out8++ = c;
64 --n;
67 /* Align 'n'. */
68 while (n & 7)
69 out8[--n] = c;
71 out64 = (uint64_t *) out8;
72 n64 = n >> 3;
74 /* Tile input byte out to 64 bits. */
75 v64 = copy_byte(c);
77 /* This must be at least 8 or the following loop doesn't work. */
78 #define CACHE_LINE_SIZE_IN_DOUBLEWORDS (CHIP_L2_LINE_SIZE() / 8)
80 /* Determine how many words we need to emit before the 'out32'
81 pointer becomes aligned modulo the cache line size. */
82 to_align64 = (-((uintptr_t) out64 >> 3)) &
83 (CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1);
85 /* Only bother aligning and using wh64 if there is at least
86 one full cache line to process. This check also prevents
87 overrunning the end of the buffer with alignment words. */
88 if (to_align64 <= n64 - CACHE_LINE_SIZE_IN_DOUBLEWORDS)
90 int lines_left;
92 /* Align out64 mod the cache line size so we can use wh64. */
93 n64 -= to_align64;
94 for (; to_align64 != 0; to_align64--)
96 *out64 = v64;
97 out64++;
100 /* Use unsigned divide to turn this into a right shift. */
101 lines_left = (unsigned) n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS;
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 uint64_t *wh = out64;
110 int i = x;
111 int j;
113 lines_left -= x;
117 __insn_wh64 (wh);
118 wh += CACHE_LINE_SIZE_IN_DOUBLEWORDS;
120 while (--i);
122 for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4); j != 0; j--)
124 *out64++ = v64;
125 *out64++ = v64;
126 *out64++ = v64;
127 *out64++ = v64;
130 while (lines_left != 0);
132 /* We processed all full lines above, so only this many
133 words remain to be processed. */
134 n64 &= CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1;
137 /* Now handle any leftover values. */
138 if (n64 != 0)
142 *out64 = v64;
143 out64++;
145 while (--n64 != 0);
148 return s;
150 weak_alias (__memset, memset)
151 libc_hidden_builtin_def (memset)