memset.c 815 B

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