memset_64.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <arch/chip.h>
  15. #include <linux/types.h>
  16. #include <linux/string.h>
  17. #include <linux/module.h>
  18. #undef memset
  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. /* KLUDGE */
  62. v64 = 0x0101010101010101ULL * (uint8_t)c;
  63. /* This must be at least 8 or the following loop doesn't work. */
  64. #define CACHE_LINE_SIZE_IN_DOUBLEWORDS (CHIP_L2_LINE_SIZE() / 8)
  65. /* Determine how many words we need to emit before the 'out32'
  66. * pointer becomes aligned modulo the cache line size.
  67. */
  68. to_align64 = (-((uintptr_t)out64 >> 3)) &
  69. (CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1);
  70. /* Only bother aligning and using wh64 if there is at least
  71. * one full cache line to process. This check also prevents
  72. * overrunning the end of the buffer with alignment words.
  73. */
  74. if (to_align64 <= n64 - CACHE_LINE_SIZE_IN_DOUBLEWORDS) {
  75. int lines_left;
  76. /* Align out64 mod the cache line size so we can use wh64. */
  77. n64 -= to_align64;
  78. for (; to_align64 != 0; to_align64--) {
  79. *out64 = v64;
  80. out64++;
  81. }
  82. /* Use unsigned divide to turn this into a right shift. */
  83. lines_left = (unsigned)n64 / CACHE_LINE_SIZE_IN_DOUBLEWORDS;
  84. do {
  85. /* Only wh64 a few lines at a time, so we don't
  86. * exceed the maximum number of victim lines.
  87. */
  88. int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
  89. ? lines_left
  90. : CHIP_MAX_OUTSTANDING_VICTIMS());
  91. uint64_t *wh = out64;
  92. int i = x;
  93. int j;
  94. lines_left -= x;
  95. do {
  96. __insn_wh64(wh);
  97. wh += CACHE_LINE_SIZE_IN_DOUBLEWORDS;
  98. } while (--i);
  99. for (j = x * (CACHE_LINE_SIZE_IN_DOUBLEWORDS / 4);
  100. j != 0; j--) {
  101. *out64++ = v64;
  102. *out64++ = v64;
  103. *out64++ = v64;
  104. *out64++ = v64;
  105. }
  106. } while (lines_left != 0);
  107. /* We processed all full lines above, so only this many
  108. * words remain to be processed.
  109. */
  110. n64 &= CACHE_LINE_SIZE_IN_DOUBLEWORDS - 1;
  111. }
  112. /* Now handle any leftover values. */
  113. if (n64 != 0) {
  114. do {
  115. *out64 = v64;
  116. out64++;
  117. } while (--n64 != 0);
  118. }
  119. return s;
  120. }
  121. EXPORT_SYMBOL(memset);