2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
15 #include <arch/chip.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/module.h>
23 void *memset(void *s, int c, size_t n)
30 /* Experimentation shows that a trivial tight loop is a win up until
31 * around a size of 20, where writing a word at a time starts to win.
33 #define BYTE_CUTOFF 20
36 /* This must be at least at least this big, or some code later
39 #error "BYTE_CUTOFF is too small"
42 if (n < BYTE_CUTOFF) {
43 /* Strangely, this turns out to be the tightest way to
48 /* Strangely, combining these into one line
59 /* Align 'out8'. We know n >= 7 so this won't write past the end. */
60 while (((uintptr_t) out8 & 7) != 0) {
69 out64 = (uint64_t *) out8;
72 /* Tile input byte out to 64 bits. */
74 v64 = 0x0101010101010101ULL * (uint8_t)c;
76 /* This must be at least 8 or the following loop doesn't work. */
77 #define CACHE_LINE_SIZE_IN_DOUBLEWORDS (CHIP_L2_LINE_SIZE() / 8)
79 /* Determine how many words we need to emit before the 'out32'
80 * 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.
89 if (to_align64 <= n64 - CACHE_LINE_SIZE_IN_DOUBLEWORDS) {
92 /* Align out64 mod the cache line size so we can use wh64. */
94 for (; to_align64 != 0; to_align64--) {
99 /* Use unsigned divide to turn this into a right shift. */
100 lines_left = (unsigned)n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS;
103 /* Only wh64 a few lines at a time, so we don't
104 * exceed the maximum number of victim lines.
106 int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
108 : CHIP_MAX_OUTSTANDING_VICTIMS());
109 uint64_t *wh = out64;
117 wh += CACHE_LINE_SIZE_IN_DOUBLEWORDS;
120 for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4);
127 } while (lines_left != 0);
129 /* We processed all full lines above, so only this many
130 * words remain to be processed.
132 n64 &= CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1;
135 /* Now handle any leftover values. */
140 } while (--n64 != 0);
145 EXPORT_SYMBOL(memset);