memset.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <[email protected]>
  3. * Copyright (C) 2008-2009 PetaLogix
  4. * Copyright (C) 2007 John Williams
  5. *
  6. * Reasonably optimised generic C-code for memset on Microblaze
  7. * This is generic C code to do efficient, alignment-aware memcpy.
  8. *
  9. * It is based on demo code originally Copyright 2001 by Intel Corp, taken from
  10. * http://www.embedded.com/showArticle.jhtml?articleID=19205567
  11. *
  12. * Attempts were made, unsuccessfully, to contact the original
  13. * author of this code (Michael Morrow, Intel). Below is the original
  14. * copyright notice.
  15. *
  16. * This software has been developed by Intel Corporation.
  17. * Intel specifically disclaims all warranties, express or
  18. * implied, and all liability, including consequential and
  19. * other indirect damages, for the use of this program, including
  20. * liability for infringement of any proprietary rights,
  21. * and including the warranties of merchantability and fitness
  22. * for a particular purpose. Intel does not assume any
  23. * responsibility for and errors which may appear in this program
  24. * not any responsibility to update it.
  25. */
  26. #include <linux/export.h>
  27. #include <linux/types.h>
  28. #include <linux/stddef.h>
  29. #include <linux/compiler.h>
  30. #include <linux/string.h>
  31. #ifdef CONFIG_OPT_LIB_FUNCTION
  32. void *memset(void *v_src, int c, __kernel_size_t n)
  33. {
  34. char *src = v_src;
  35. uint32_t *i_src;
  36. uint32_t w32 = 0;
  37. /* Truncate c to 8 bits */
  38. c = (c & 0xFF);
  39. if (unlikely(c)) {
  40. /* Make a repeating word out of it */
  41. w32 = c;
  42. w32 |= w32 << 8;
  43. w32 |= w32 << 16;
  44. }
  45. if (likely(n >= 4)) {
  46. /* Align the destination to a word boundary */
  47. /* This is done in an endian independent manner */
  48. switch ((unsigned) src & 3) {
  49. case 1:
  50. *src++ = c;
  51. --n;
  52. fallthrough;
  53. case 2:
  54. *src++ = c;
  55. --n;
  56. fallthrough;
  57. case 3:
  58. *src++ = c;
  59. --n;
  60. }
  61. i_src = (void *)src;
  62. /* Do as many full-word copies as we can */
  63. for (; n >= 4; n -= 4)
  64. *i_src++ = w32;
  65. src = (void *)i_src;
  66. }
  67. /* Simple, byte oriented memset or the rest of count. */
  68. switch (n) {
  69. case 3:
  70. *src++ = c;
  71. fallthrough;
  72. case 2:
  73. *src++ = c;
  74. fallthrough;
  75. case 1:
  76. *src++ = c;
  77. break;
  78. default:
  79. break;
  80. }
  81. return v_src;
  82. }
  83. EXPORT_SYMBOL(memset);
  84. #endif /* CONFIG_OPT_LIB_FUNCTION */