memset.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <asm.h>
  24. #include <arch/arm/cores.h>
  25. .text
  26. .align 2
  27. /* void bzero(void *s, size_t n); */
  28. FUNCTION(bzero)
  29. mov r2, r1
  30. mov r1, #0
  31. /* void *memset(void *s, int c, size_t n); */
  32. FUNCTION(memset)
  33. // check for zero length
  34. cmp r2, #0
  35. bxeq lr
  36. // save the original pointer
  37. mov r12, r0
  38. // short memsets aren't worth optimizing
  39. cmp r2, #(32 + 16)
  40. blt .L_bytewise
  41. // fill a 32 bit register with the 8 bit value
  42. and r1, r1, #0xff
  43. orr r1, r1, r1, lsl #8
  44. orr r1, r1, r1, lsl #16
  45. // check for 16 byte alignment
  46. tst r0, #15
  47. bne .L_not16bytealigned
  48. .L_bigset:
  49. // dump some registers to make space for our values
  50. stmfd sp!, { r4-r5 }
  51. // fill a bunch of registers with the set value
  52. mov r3, r1
  53. mov r4, r1
  54. mov r5, r1
  55. // prepare the count register so we can avoid an extra compare
  56. sub r2, r2, #32
  57. // 32 bytes at a time
  58. .L_bigset_loop:
  59. stmia r0!, { r1, r3, r4, r5 }
  60. subs r2, r2, #32
  61. stmia r0!, { r1, r3, r4, r5 }
  62. bge .L_bigset_loop
  63. // restore our dumped registers
  64. ldmfd sp!, { r4-r5 }
  65. // see if we're done
  66. adds r2, r2, #32
  67. beq .L_done
  68. .L_bytewise:
  69. // bytewise memset
  70. subs r2, r2, #1
  71. strb r1, [r0], #1
  72. bgt .L_bytewise
  73. .L_done:
  74. // restore the base pointer as return value
  75. mov r0, r12
  76. bx lr
  77. .L_not16bytealigned:
  78. // dst is not 16 byte aligned, so we will set up to 15 bytes to get it aligned.
  79. // set the condition flags based on the alignment.
  80. lsl r3, r0, #28
  81. rsb r3, r3, #0
  82. msr CPSR_f, r3 // move into NZCV fields in CPSR
  83. // move as many bytes as necessary to get the dst aligned
  84. strvsb r1, [r0], #1 // V set
  85. strcsh r1, [r0], #2 // C set
  86. streq r1, [r0], #4 // Z set
  87. strmi r1, [r0], #4 // N set
  88. strmi r1, [r0], #4 // N set
  89. // fix the remaining len
  90. sub r2, r2, r3, lsr #28
  91. // do the large memset
  92. b .L_bigset