strlen.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. Processor specific implementation of strlen
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. Implemented using the code from glibc: libc/sysdeps/x86_64/strlen.S Version 1.2
  13. }
  14. {$ifdef win64}
  15. var
  16. rdi : qword;
  17. {$endif win64}
  18. asm
  19. { win64 has different calling conventions }
  20. {$ifdef win64}
  21. movq %rdi,rdi
  22. movq %rcx, %rdi { Duplicate source pointer. }
  23. {$else win64}
  24. movq %rdi, %rcx { Duplicate source pointer. }
  25. {$endif win64}
  26. andl $7, %ecx { mask alignment bits }
  27. movq %rdi, %rax { duplicate destination. }
  28. jz .LFPC_STRLEN_1 { aligned => start loop }
  29. neg %ecx { We need to align to 8 bytes. }
  30. addl $8,%ecx
  31. { Search the first bytes directly. }
  32. .LFPC_STRLEN_0:
  33. cmpb $0x0,(%rax) { is byte NUL? }
  34. je .LFPC_STRLEN_2 { yes => return }
  35. incq %rax { increment pointer }
  36. decl %ecx
  37. jnz .LFPC_STRLEN_0
  38. .LFPC_STRLEN_1:
  39. movq $0xfefefefefefefeff,%r8 { Save magic. }
  40. .p2align 4 { Align loop. }
  41. .LFPC_STRLEN_4: { Main Loop is unrolled 4 times. }
  42. { First unroll. }
  43. movq (%rax), %rcx { get double word (= 8 bytes) in question }
  44. addq $8,%rax { adjust pointer for next word }
  45. movq %r8, %rdx { magic value }
  46. addq %rcx, %rdx { add the magic value to the word. We get
  47. carry bits reported for each byte which
  48. is *not* 0 }
  49. jnc .LFPC_STRLEN_3 { highest byte is NUL => return pointer }
  50. xorq %rcx, %rdx { (word+magic)^word }
  51. orq %r8, %rdx { set all non-carry bits }
  52. incq %rdx { add 1: if one carry bit was *not* set
  53. the addition will not result in 0. }
  54. jnz .LFPC_STRLEN_3 { found NUL => return pointer }
  55. { Second unroll. }
  56. movq (%rax), %rcx { get double word (= 8 bytes) in question }
  57. addq $8,%rax { adjust pointer for next word }
  58. movq %r8, %rdx { magic value }
  59. addq %rcx, %rdx { add the magic value to the word. We get
  60. carry bits reported for each byte which
  61. is *not* 0 }
  62. jnc .LFPC_STRLEN_3 { highest byte is NUL => return pointer }
  63. xorq %rcx, %rdx { (word+magic)^word }
  64. orq %r8, %rdx { set all non-carry bits }
  65. incq %rdx { add 1: if one carry bit was *not* set
  66. the addition will not result in 0. }
  67. jnz .LFPC_STRLEN_3 { found NUL => return pointer }
  68. { Third unroll. }
  69. movq (%rax), %rcx { get double word (= 8 bytes) in question }
  70. addq $8,%rax { adjust pointer for next word }
  71. movq %r8, %rdx { magic value }
  72. addq %rcx, %rdx { add the magic value to the word. We get
  73. carry bits reported for each byte which
  74. is *not* 0 }
  75. jnc .LFPC_STRLEN_3 { highest byte is NUL => return pointer }
  76. xorq %rcx, %rdx { (word+magic)^word }
  77. orq %r8, %rdx { set all non-carry bits }
  78. incq %rdx { add 1: if one carry bit was *not* set
  79. the addition will not result in 0. }
  80. jnz .LFPC_STRLEN_3 { found NUL => return pointer }
  81. { Fourth unroll. }
  82. movq (%rax), %rcx { get double word (= 8 bytes) in question }
  83. addq $8,%rax { adjust pointer for next word }
  84. movq %r8, %rdx { magic value }
  85. addq %rcx, %rdx { add the magic value to the word. We get
  86. carry bits reported for each byte which
  87. is *not* 0 }
  88. jnc .LFPC_STRLEN_3 { highest byte is NUL => return pointer }
  89. xorq %rcx, %rdx { (word+magic)^word }
  90. orq %r8, %rdx { set all non-carry bits }
  91. incq %rdx { add 1: if one carry bit was *not* set
  92. the addition will not result in 0. }
  93. jz .LFPC_STRLEN_4 { no NUL found => continue loop }
  94. .p2align 4 { Align, it's a jump target. }
  95. .LFPC_STRLEN_3:
  96. subq $8,%rax { correct pointer increment. }
  97. testb %cl, %cl { is first byte NUL? }
  98. jz .LFPC_STRLEN_2 { yes => return }
  99. incq %rax { increment pointer }
  100. testb %ch, %ch { is second byte NUL? }
  101. jz .LFPC_STRLEN_2 { yes => return }
  102. incq %rax { increment pointer }
  103. testl $0x00ff0000, %ecx { is third byte NUL? }
  104. jz .LFPC_STRLEN_2 { yes => return pointer }
  105. incq %rax { increment pointer }
  106. testl $0xff000000, %ecx { is fourth byte NUL? }
  107. jz .LFPC_STRLEN_2 { yes => return pointer }
  108. incq %rax { increment pointer }
  109. shrq $32, %rcx { look at other half. }
  110. testb %cl, %cl { is first byte NUL? }
  111. jz .LFPC_STRLEN_2 { yes => return }
  112. incq %rax { increment pointer }
  113. testb %ch, %ch { is second byte NUL? }
  114. jz .LFPC_STRLEN_2 { yes => return }
  115. incq %rax { increment pointer }
  116. testl $0xff0000, %ecx { is third byte NUL? }
  117. jz .LFPC_STRLEN_2 { yes => return pointer }
  118. incq %rax { increment pointer }
  119. .LFPC_STRLEN_2:
  120. subq %rdi, %rax { compute difference to string start }
  121. {$ifdef win64}
  122. movq rdi,%rdi
  123. {$endif win64}
  124. end;