strrchr.S 2.7 KB

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