strrchr.S 2.7 KB

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