strncat.S 1.8 KB

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