memset_64.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2011 Tilera Corporation. All Rights Reserved.
  3. *
  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.
  7. *
  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
  12. * more details.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. #include <arch/chip.h>
  18. #include "string-endian.h"
  19. void *memset(void *s, int c, size_t n)
  20. {
  21. uint64_t *out64;
  22. int n64, to_align64;
  23. uint64_t v64;
  24. uint8_t *out8 = s;
  25. /* Experimentation shows that a trivial tight loop is a win up until
  26. * around a size of 20, where writing a word at a time starts to win.
  27. */
  28. #define BYTE_CUTOFF 20
  29. #if BYTE_CUTOFF < 7
  30. /* This must be at least at least this big, or some code later
  31. * on doesn't work.
  32. */
  33. #error "BYTE_CUTOFF is too small"
  34. #endif
  35. if (n < BYTE_CUTOFF) {
  36. /* Strangely, this turns out to be the tightest way to
  37. * write this loop.
  38. */
  39. if (n != 0) {
  40. do {
  41. /* Strangely, combining these into one line
  42. * performs worse.
  43. */
  44. *out8 = c;
  45. out8++;
  46. } while (--n != 0);
  47. }
  48. return s;
  49. }
  50. /* Align 'out8'. We know n >= 7 so this won't write past the end. */
  51. while (((uintptr_t) out8 & 7) != 0) {
  52. *out8++ = c;
  53. --n;
  54. }
  55. /* Align 'n'. */
  56. while (n & 7)
  57. out8[--n] = c;
  58. out64 = (uint64_t *) out8;
  59. n64 = n >> 3;
  60. /* Tile input byte out to 64 bits. */
  61. v64 = copy_byte(c);
  62. /* This must be at least 8 or the following loop doesn't work. */
  63. #define CACHE_LINE_SIZE_IN_DOUBLEWORDS (CHIP_L2_LINE_SIZE() / 8)
  64. /* Determine how many words we need to emit before the 'out32'
  65. * pointer becomes aligned modulo the cache line size.
  66. */
  67. to_align64 = (-((uintptr_t)out64 >> 3)) &
  68. (CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1);
  69. /* Only bother aligning and using wh64 if there is at least
  70. * one full cache line to process. This check also prevents
  71. * overrunning the end of the buffer with alignment words.
  72. */
  73. if (to_align64 <= n64 - CACHE_LINE_SIZE_IN_DOUBLEWORDS) {
  74. int lines_left;
  75. /* Align out64 mod the cache line size so we can use wh64. */
  76. n64 -= to_align64;
  77. for (; to_align64 != 0; to_align64--) {
  78. *out64 = v64;
  79. out64++;
  80. }
  81. /* Use unsigned divide to turn this into a right shift. */
  82. lines_left = (unsigned)n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS;
  83. do {
  84. /* Only wh64 a few lines at a time, so we don't
  85. * exceed the maximum number of victim lines.
  86. */
  87. int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
  88. ? lines_left
  89. : CHIP_MAX_OUTSTANDING_VICTIMS());
  90. uint64_t *wh = out64;
  91. int i = x;
  92. int j;
  93. lines_left -= x;
  94. do {
  95. __insn_wh64(wh);
  96. wh += CACHE_LINE_SIZE_IN_DOUBLEWORDS;
  97. } while (--i);
  98. for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4);
  99. j != 0; j--) {
  100. *out64++ = v64;
  101. *out64++ = v64;
  102. *out64++ = v64;
  103. *out64++ = v64;
  104. }
  105. } while (lines_left != 0);
  106. /* We processed all full lines above, so only this many
  107. * words remain to be processed.
  108. */
  109. n64 &= CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1;
  110. }
  111. /* Now handle any leftover values. */
  112. if (n64 != 0) {
  113. do {
  114. *out64 = v64;
  115. out64++;
  116. } while (--n64 != 0);
  117. }
  118. return s;
  119. }
  120. EXPORT_SYMBOL(memset);