memset.c 868 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Simple string operations using longword operations.
  3. * The ones in newlib-nano are bytewise...
  4. */
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include "fw.h"
  9. void *memset(void *s, int c, size_t n)
  10. {
  11. xptr_t q;
  12. uint32_t cc;
  13. if (unlikely(!n))
  14. return s;
  15. cc = (uint8_t)c;
  16. cc = cc | (cc << 16);
  17. cc = cc | (cc << 8);
  18. q.b = s;
  19. if (unlikely(q.a & 1)) {
  20. *q.b++ = cc;
  21. n--;
  22. }
  23. if (likely(n >= 2)) {
  24. if (unlikely(q.a & 2)) {
  25. *q.w++ = cc;
  26. n -= 2;
  27. }
  28. while (n >= 4*8) {
  29. q.l[0] = cc;
  30. q.l[1] = cc;
  31. q.l[2] = cc;
  32. q.l[3] = cc;
  33. q.l[4] = cc;
  34. q.l[5] = cc;
  35. q.l[6] = cc;
  36. q.l[7] = cc;
  37. q.l += 8;
  38. n -= 32;
  39. }
  40. while (n >= 4) {
  41. *q.l++ = cc;
  42. n -= 4;
  43. }
  44. if (unlikely(n & 2))
  45. *q.w++ = c;
  46. }
  47. if (unlikely(n & 1))
  48. *q.b++ = c;
  49. return s;
  50. }