mymemset.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 *memset(void *s, int c, size_t n); */
  28. .global mymemset
  29. mymemset:
  30. // check for zero length
  31. cmp r2, #0
  32. bxeq lr
  33. // save the original pointer
  34. mov r12, r0
  35. // short memsets aren't worth optimizing
  36. cmp r2, #(32 + 16)
  37. blt .L_bytewise
  38. // fill a 32 bit register with the 8 bit value
  39. and r1, r1, #0xff
  40. orr r1, r1, r1, lsl #8
  41. orr r1, r1, r1, lsl #16
  42. // check for 16 byte alignment
  43. tst r0, #15
  44. bne .L_not16bytealigned
  45. .L_bigset:
  46. // dump some registers to make space for our values
  47. stmfd sp!, { r4-r5 }
  48. // fill a bunch of registers with the set value
  49. mov r3, r1
  50. mov r4, r1
  51. mov r5, r1
  52. // prepare the count register so we can avoid an extra compare
  53. sub r2, r2, #32
  54. // 32 bytes at a time
  55. .L_bigset_loop:
  56. stmia r0!, { r1, r3, r4, r5 }
  57. subs r2, r2, #32
  58. stmia r0!, { r1, r3, r4, r5 }
  59. bge .L_bigset_loop
  60. // restore our dumped registers
  61. ldmfd sp!, { r4-r5 }
  62. // see if we're done
  63. adds r2, r2, #32
  64. beq .L_done
  65. .L_bytewise:
  66. // bytewise memset
  67. subs r2, r2, #1
  68. strb r1, [r0], #1
  69. bgt .L_bytewise
  70. .L_done:
  71. // restore the base pointer as return value
  72. mov r0, r12
  73. bx lr
  74. .L_not16bytealigned:
  75. // dst is not 16 byte aligned, so we will set up to 15 bytes to get it aligned.
  76. // set the condition flags based on the alignment.
  77. lsl r3, r0, #28
  78. rsb r3, r3, #0
  79. msr CPSR_f, r3 // move into NZCV fields in CPSR
  80. // move as many bytes as necessary to get the dst aligned
  81. strvsb r1, [r0], #1 // V set
  82. strcsh r1, [r0], #2 // C set
  83. streq r1, [r0], #4 // Z set
  84. strmi r1, [r0], #4 // N set
  85. strmi r1, [r0], #4 // N set
  86. // fix the remaining len
  87. sub r2, r2, r3, lsr #28
  88. // do the large memset
  89. b .L_bigset