]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 2011-2014 Free Software Foundation, Inc. |
63d143a2 CM |
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 | |
ab84e3ff PE |
16 | License along with the GNU C Library. If not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
63d143a2 CM |
18 | |
19 | #include <arch/chip.h> | |
20 | #include <string.h> | |
21 | #include <stdint.h> | |
64d76ca0 | 22 | #include "string-endian.h" |
63d143a2 CM |
23 | |
24 | void * | |
25 | __memset (void *s, int c, size_t n) | |
26 | { | |
27 | uint64_t *out64; | |
28 | int n64, to_align64; | |
29 | uint64_t v64; | |
30 | uint8_t *out8 = s; | |
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 < 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 | |
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 >= 7 so this won't write past the end. */ | |
61 | while (((uintptr_t) out8 & 7) != 0) | |
62 | { | |
63 | *out8++ = c; | |
64 | --n; | |
65 | } | |
66 | ||
67 | /* Align 'n'. */ | |
68 | while (n & 7) | |
69 | out8[--n] = c; | |
70 | ||
71 | out64 = (uint64_t *) out8; | |
72 | n64 = n >> 3; | |
73 | ||
74 | /* Tile input byte out to 64 bits. */ | |
64d76ca0 | 75 | v64 = copy_byte(c); |
63d143a2 CM |
76 | |
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) | |
79 | ||
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); | |
84 | ||
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) | |
89 | { | |
90 | int lines_left; | |
91 | ||
92 | /* Align out64 mod the cache line size so we can use wh64. */ | |
93 | n64 -= to_align64; | |
94 | for (; to_align64 != 0; to_align64--) | |
95 | { | |
96 | *out64 = v64; | |
97 | out64++; | |
98 | } | |
99 | ||
100 | /* Use unsigned divide to turn this into a right shift. */ | |
101 | lines_left = (unsigned) n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS; | |
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 | uint64_t *wh = out64; | |
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_DOUBLEWORDS; | |
119 | } | |
120 | while (--i); | |
121 | ||
122 | for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4); j != 0; j--) | |
123 | { | |
124 | *out64++ = v64; | |
125 | *out64++ = v64; | |
126 | *out64++ = v64; | |
127 | *out64++ = v64; | |
128 | } | |
129 | } | |
130 | while (lines_left != 0); | |
131 | ||
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; | |
135 | } | |
136 | ||
137 | /* Now handle any leftover values. */ | |
138 | if (n64 != 0) | |
139 | { | |
140 | do | |
141 | { | |
142 | *out64 = v64; | |
143 | out64++; | |
144 | } | |
145 | while (--n64 != 0); | |
146 | } | |
147 | ||
148 | return s; | |
149 | } | |
150 | weak_alias (__memset, memset) | |
151 | libc_hidden_builtin_def (memset) |