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/>. */
21 #include <arch/chip.h>
24 __memset (void *s
, int c
, size_t n
)
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
37 /* This must be at least at least this big, or some code later
39 # error "BYTE_CUTOFF is too small."
44 /* Strangely, this turns out to be the tightest way to write
50 /* Strangely, combining these into one line performs worse. */
60 /* Align 'out8'. We know n >= 3 so this won't write past the end. */
61 while (((uintptr_t) out8
& 3) != 0)
71 out32
= (uint32_t *) out8
;
74 /* Tile input byte out to 32 bits. */
75 v16
= __insn_intlb (c
, c
);
76 v32
= __insn_intlh (v16
, v16
);
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)
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);
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
)
92 /* Align out32 mod the cache line size so we can use wh64. */
94 for (; to_align32
!= 0; to_align32
--)
100 /* Use unsigned divide to turn this into a right shift. */
101 lines_left
= (unsigned) n32
/ CACHE_LINE_SIZE_IN_WORDS
;
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
;
118 wh
+= CACHE_LINE_SIZE_IN_WORDS
;
122 for (j
= x
* (CACHE_LINE_SIZE_IN_WORDS
/ 4); j
!= 0; j
--)
130 while (lines_left
!= 0);
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;
137 /* Now handle any leftover values. */
150 weak_alias (__memset
, memset
)
151 libc_hidden_builtin_def (memset
)