strrchr.S 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * arch/alpha/lib/strrchr.S
  4. * Contributed by Richard Henderson ([email protected])
  5. *
  6. * Return the address of the last occurrence of a given character
  7. * within a null-terminated string, or null if it is not found.
  8. */
  9. #include <asm/export.h>
  10. #include <asm/regdef.h>
  11. .set noreorder
  12. .set noat
  13. .align 3
  14. .ent strrchr
  15. .globl strrchr
  16. strrchr:
  17. .frame sp, 0, ra
  18. .prologue 0
  19. zapnot a1, 1, a1 # e0 : zero extend our test character
  20. mov zero, t6 # .. e1 : t6 is last match aligned addr
  21. sll a1, 8, t5 # e0 : replicate our test character
  22. mov zero, t8 # .. e1 : t8 is last match byte compare mask
  23. or t5, a1, a1 # e0 :
  24. ldq_u t0, 0(a0) # .. e1 : load first quadword
  25. sll a1, 16, t5 # e0 :
  26. andnot a0, 7, v0 # .. e1 : align source addr
  27. or t5, a1, a1 # e0 :
  28. lda t4, -1 # .. e1 : build garbage mask
  29. sll a1, 32, t5 # e0 :
  30. cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
  31. mskqh t4, a0, t4 # e0 :
  32. or t5, a1, a1 # .. e1 : character replication complete
  33. xor t0, a1, t2 # e0 : make bytes == c zero
  34. cmpbge zero, t4, t4 # .. e1 : bits set iff byte is garbage
  35. cmpbge zero, t2, t3 # e0 : bits set iff byte == c
  36. andnot t1, t4, t1 # .. e1 : clear garbage from null test
  37. andnot t3, t4, t3 # e0 : clear garbage from char test
  38. bne t1, $eos # .. e1 : did we already hit the terminator?
  39. /* Character search main loop */
  40. $loop:
  41. ldq t0, 8(v0) # e0 : load next quadword
  42. cmovne t3, v0, t6 # .. e1 : save previous comparisons match
  43. cmovne t3, t3, t8 # e0 :
  44. addq v0, 8, v0 # .. e1 :
  45. xor t0, a1, t2 # e0 :
  46. cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
  47. cmpbge zero, t2, t3 # e0 : bits set iff byte == c
  48. beq t1, $loop # .. e1 : if we havnt seen a null, loop
  49. /* Mask out character matches after terminator */
  50. $eos:
  51. negq t1, t4 # e0 : isolate first null byte match
  52. and t1, t4, t4 # e1 :
  53. subq t4, 1, t5 # e0 : build a mask of the bytes up to...
  54. or t4, t5, t4 # e1 : ... and including the null
  55. and t3, t4, t3 # e0 : mask out char matches after null
  56. cmovne t3, t3, t8 # .. e1 : save it, if match found
  57. cmovne t3, v0, t6 # e0 :
  58. /* Locate the address of the last matched character */
  59. /* Retain the early exit for the ev4 -- the ev5 mispredict penalty
  60. is 5 cycles -- the same as just falling through. */
  61. beq t8, $retnull # .. e1 :
  62. and t8, 0xf0, t2 # e0 : binary search for the high bit set
  63. cmovne t2, t2, t8 # .. e1 (zdb)
  64. cmovne t2, 4, t2 # e0 :
  65. and t8, 0xcc, t1 # .. e1 :
  66. cmovne t1, t1, t8 # e0 :
  67. cmovne t1, 2, t1 # .. e1 :
  68. and t8, 0xaa, t0 # e0 :
  69. cmovne t0, 1, t0 # .. e1 (zdb)
  70. addq t2, t1, t1 # e0 :
  71. addq t6, t0, v0 # .. e1 : add our aligned base ptr to the mix
  72. addq v0, t1, v0 # e0 :
  73. ret # .. e1 :
  74. $retnull:
  75. mov zero, v0 # e0 :
  76. ret # .. e1 :
  77. .end strrchr
  78. EXPORT_SYMBOL(strrchr)