memset.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  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 __HAVE_ARCH_MEMSET
  32. #ifndef CONFIG_OPT_LIB_FUNCTION
  33. void *memset(void *v_src, int c, __kernel_size_t n)
  34. {
  35. char *src = v_src;
  36. /* Truncate c to 8 bits */
  37. c = (c & 0xFF);
  38. /* Simple, byte oriented memset or the rest of count. */
  39. while (n--)
  40. *src++ = c;
  41. return v_src;
  42. }
  43. #else /* CONFIG_OPT_LIB_FUNCTION */
  44. void *memset(void *v_src, int c, __kernel_size_t n)
  45. {
  46. char *src = v_src;
  47. uint32_t *i_src;
  48. uint32_t w32 = 0;
  49. /* Truncate c to 8 bits */
  50. c = (c & 0xFF);
  51. if (unlikely(c)) {
  52. /* Make a repeating word out of it */
  53. w32 = c;
  54. w32 |= w32 << 8;
  55. w32 |= w32 << 16;
  56. }
  57. if (likely(n >= 4)) {
  58. /* Align the destination to a word boundary */
  59. /* This is done in an endian independent manner */
  60. switch ((unsigned) src & 3) {
  61. case 1:
  62. *src++ = c;
  63. --n;
  64. case 2:
  65. *src++ = c;
  66. --n;
  67. case 3:
  68. *src++ = c;
  69. --n;
  70. }
  71. i_src = (void *)src;
  72. /* Do as many full-word copies as we can */
  73. for (; n >= 4; n -= 4)
  74. *i_src++ = w32;
  75. src = (void *)i_src;
  76. }
  77. /* Simple, byte oriented memset or the rest of count. */
  78. while (n--)
  79. *src++ = c;
  80. return v_src;
  81. }
  82. #endif /* CONFIG_OPT_LIB_FUNCTION */
  83. EXPORT_SYMBOL(memset);
  84. #endif /* __HAVE_ARCH_MEMSET */