memset.S 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2020, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <asm_macros.S>
  7. .syntax unified
  8. .global memset
  9. /* -----------------------------------------------------------------------
  10. * void *memset(void *dst, int val, size_t count)
  11. *
  12. * Copy the value of 'val' (converted to an unsigned char) into
  13. * each of the first 'count' characters of the object pointed to by 'dst'.
  14. *
  15. * Returns the value of 'dst'.
  16. * -----------------------------------------------------------------------
  17. */
  18. func memset
  19. mov r12, r0 /* keep r0 */
  20. tst r0, #3
  21. beq aligned /* 4-bytes aligned */
  22. /* Unaligned 'dst' */
  23. unaligned:
  24. subs r2, r2, #1
  25. strbhs r1, [r12], #1
  26. bxls lr /* return if 0 */
  27. tst r12, #3
  28. bne unaligned /* continue while unaligned */
  29. /* 4-bytes aligned */
  30. aligned:bfi r1, r1, #8, #8 /* propagate 'val' */
  31. bfi r1, r1, #16, #16
  32. mov r3, r1
  33. cmp r2, #16
  34. blo less_16 /* < 16 */
  35. push {r4, lr}
  36. mov r4, r1
  37. mov lr, r1
  38. write_32:
  39. subs r2, r2, #32
  40. stmiahs r12!, {r1, r3, r4, lr}
  41. stmiahs r12!, {r1, r3, r4, lr}
  42. bhi write_32 /* write 32 bytes in a loop */
  43. popeq {r4, pc} /* return if 0 */
  44. lsls r2, r2, #28 /* C = r2[4]; N = r2[3]; Z = r2[3:0] */
  45. stmiacs r12!, {r1, r3, r4, lr} /* write 16 bytes */
  46. popeq {r4, pc} /* return if 16 */
  47. stmiami r12!, {r1, r3} /* write 8 bytes */
  48. lsls r2, r2, #2 /* C = r2[2]; N = r2[1]; Z = r2[1:0] */
  49. strcs r1, [r12], #4 /* write 4 bytes */
  50. popeq {r4, pc} /* return if 8 or 4 */
  51. strhmi r1, [r12], #2 /* write 2 bytes */
  52. lsls r2, r2, #1 /* N = Z = r2[0] */
  53. strbmi r1, [r12] /* write 1 byte */
  54. pop {r4, pc}
  55. less_16:lsls r2, r2, #29 /* C = r2[3]; N = r2[2]; Z = r2[2:0] */
  56. stmiacs r12!, {r1, r3} /* write 8 bytes */
  57. bxeq lr /* return if 8 */
  58. strmi r1, [r12], #4 /* write 4 bytes */
  59. lsls r2, r2, #2 /* C = r2[1]; N = Z = r2[0] */
  60. strhcs r1, [r12], #2 /* write 2 bytes */
  61. strbmi r1, [r12] /* write 1 byte */
  62. bx lr
  63. endfunc memset