memset.S 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * OpenRISC memset.S
  3. *
  4. * Hand-optimized assembler version of memset for OpenRISC.
  5. * Algorithm inspired by several other arch-specific memset routines
  6. * in the kernel tree
  7. *
  8. * Copyright (C) 2015 Olof Kindgren <olof.kindgren@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. .global memset
  16. .type memset, @function
  17. memset:
  18. /* arguments:
  19. * r3 = *s
  20. * r4 = c
  21. * r5 = n
  22. * r13, r15, r17, r19 used as temp regs
  23. */
  24. /* Exit if n == 0 */
  25. l.sfeqi r5, 0
  26. l.bf 4f
  27. /* Truncate c to char */
  28. l.andi r13, r4, 0xff
  29. /* Skip word extension if c is 0 */
  30. l.sfeqi r13, 0
  31. l.bf 1f
  32. /* Check for at least two whole words (8 bytes) */
  33. l.sfleui r5, 7
  34. /* Extend char c to 32-bit word cccc in r13 */
  35. l.slli r15, r13, 16 // r13 = 000c, r15 = 0c00
  36. l.or r13, r13, r15 // r13 = 0c0c, r15 = 0c00
  37. l.slli r15, r13, 8 // r13 = 0c0c, r15 = c0c0
  38. l.or r13, r13, r15 // r13 = cccc, r15 = c0c0
  39. 1: l.addi r19, r3, 0 // Set r19 = src
  40. /* Jump to byte copy loop if less than two words */
  41. l.bf 3f
  42. l.or r17, r5, r0 // Set r17 = n
  43. /* Mask out two LSBs to check alignment */
  44. l.andi r15, r3, 0x3
  45. /* lsb == 00, jump to word copy loop */
  46. l.sfeqi r15, 0
  47. l.bf 2f
  48. l.addi r19, r3, 0 // Set r19 = src
  49. /* lsb == 01,10 or 11 */
  50. l.sb 0(r3), r13 // *src = c
  51. l.addi r17, r17, -1 // Decrease n
  52. l.sfeqi r15, 3
  53. l.bf 2f
  54. l.addi r19, r3, 1 // src += 1
  55. /* lsb == 01 or 10 */
  56. l.sb 1(r3), r13 // *(src+1) = c
  57. l.addi r17, r17, -1 // Decrease n
  58. l.sfeqi r15, 2
  59. l.bf 2f
  60. l.addi r19, r3, 2 // src += 2
  61. /* lsb == 01 */
  62. l.sb 2(r3), r13 // *(src+2) = c
  63. l.addi r17, r17, -1 // Decrease n
  64. l.addi r19, r3, 3 // src += 3
  65. /* Word copy loop */
  66. 2: l.sw 0(r19), r13 // *src = cccc
  67. l.addi r17, r17, -4 // Decrease n
  68. l.sfgeui r17, 4
  69. l.bf 2b
  70. l.addi r19, r19, 4 // Increase src
  71. /* When n > 0, copy the remaining bytes, otherwise jump to exit */
  72. l.sfeqi r17, 0
  73. l.bf 4f
  74. /* Byte copy loop */
  75. 3: l.addi r17, r17, -1 // Decrease n
  76. l.sb 0(r19), r13 // *src = cccc
  77. l.sfnei r17, 0
  78. l.bf 3b
  79. l.addi r19, r19, 1 // Increase src
  80. 4: l.jr r9
  81. l.ori r11, r3, 0