memset_32.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2010 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. void *memset(void *s, int c, size_t n)
  19. {
  20. uint32_t *out32;
  21. int n32;
  22. uint32_t v16, v32;
  23. uint8_t *out8 = s;
  24. int to_align32;
  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 < 3
  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 >= 3 so this won't write past the end. */
  51. while (((uintptr_t) out8 & 3) != 0) {
  52. *out8++ = c;
  53. --n;
  54. }
  55. /* Align 'n'. */
  56. while (n & 3)
  57. out8[--n] = c;
  58. out32 = (uint32_t *) out8;
  59. n32 = n >> 2;
  60. /* Tile input byte out to 32 bits. */
  61. v16 = __insn_intlb(c, c);
  62. v32 = __insn_intlh(v16, v16);
  63. /* This must be at least 8 or the following loop doesn't work. */
  64. #define CACHE_LINE_SIZE_IN_WORDS (CHIP_L2_LINE_SIZE() / 4)
  65. /* Determine how many words we need to emit before the 'out32'
  66. * pointer becomes aligned modulo the cache line size.
  67. */
  68. to_align32 =
  69. (-((uintptr_t)out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 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_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS) {
  75. int lines_left;
  76. /* Align out32 mod the cache line size so we can use wh64. */
  77. n32 -= to_align32;
  78. for (; to_align32 != 0; to_align32--) {
  79. *out32 = v32;
  80. out32++;
  81. }
  82. /* Use unsigned divide to turn this into a right shift. */
  83. lines_left = (unsigned)n32 / CACHE_LINE_SIZE_IN_WORDS;
  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. uint32_t *wh = out32;
  92. int i = x;
  93. int j;
  94. lines_left -= x;
  95. do {
  96. __insn_wh64(wh);
  97. wh += CACHE_LINE_SIZE_IN_WORDS;
  98. } while (--i);
  99. for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4);
  100. j != 0; j--) {
  101. *out32++ = v32;
  102. *out32++ = v32;
  103. *out32++ = v32;
  104. *out32++ = v32;
  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. n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;
  111. }
  112. /* Now handle any leftover values. */
  113. if (n32 != 0) {
  114. do {
  115. *out32 = v32;
  116. out32++;
  117. } while (--n32 != 0);
  118. }
  119. return s;
  120. }
  121. EXPORT_SYMBOL(memset);