strncat.S 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * arch/alpha/lib/strncat.S
  4. * Contributed by Richard Henderson ([email protected])
  5. *
  6. * Append no more than COUNT characters from the null-terminated string SRC
  7. * to the null-terminated string DST. Always null-terminate the new DST.
  8. *
  9. * This differs slightly from the semantics in libc in that we never write
  10. * past count, whereas libc may write to count+1. This follows the generic
  11. * implementation in lib/string.c and is, IMHO, more sensible.
  12. */
  13. #include <asm/export.h>
  14. .text
  15. .align 3
  16. .globl strncat
  17. .ent strncat
  18. strncat:
  19. .frame $30, 0, $26
  20. .prologue 0
  21. mov $16, $0 # set up return value
  22. beq $18, $zerocount
  23. /* Find the end of the string. */
  24. ldq_u $1, 0($16) # load first quadword ($16 may be misaligned)
  25. lda $2, -1($31)
  26. insqh $2, $16, $2
  27. andnot $16, 7, $16
  28. or $2, $1, $1
  29. cmpbge $31, $1, $2 # bits set iff byte == 0
  30. bne $2, $found
  31. $loop: ldq $1, 8($16)
  32. addq $16, 8, $16
  33. cmpbge $31, $1, $2
  34. beq $2, $loop
  35. $found: negq $2, $3 # clear all but least set bit
  36. and $2, $3, $2
  37. and $2, 0xf0, $3 # binary search for that set bit
  38. and $2, 0xcc, $4
  39. and $2, 0xaa, $5
  40. cmovne $3, 4, $3
  41. cmovne $4, 2, $4
  42. cmovne $5, 1, $5
  43. addq $3, $4, $3
  44. addq $16, $5, $16
  45. addq $16, $3, $16
  46. /* Now do the append. */
  47. bsr $23, __stxncpy
  48. /* Worry about the null termination. */
  49. zapnot $1, $27, $2 # was last byte a null?
  50. bne $2, 0f
  51. ret
  52. 0: cmplt $27, $24, $2 # did we fill the buffer completely?
  53. or $2, $18, $2
  54. bne $2, 2f
  55. and $24, 0x80, $2 # no zero next byte
  56. bne $2, 1f
  57. /* Here there are bytes left in the current word. Clear one. */
  58. addq $24, $24, $24 # end-of-count bit <<= 1
  59. 2: zap $1, $24, $1
  60. stq_u $1, 0($16)
  61. ret
  62. 1: /* Here we must read the next DST word and clear the first byte. */
  63. ldq_u $1, 8($16)
  64. zap $1, 1, $1
  65. stq_u $1, 8($16)
  66. $zerocount:
  67. ret
  68. .end strncat
  69. EXPORT_SYMBOL(strncat)