strings.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by Florian Klaempfl, member of the
  4. Free Pascal development team
  5. Processor dependent part of strings.pp, that can be shared with
  6. sysutils unit.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$ASMMODE GAS}
  14. {$ifndef FPC_UNIT_HAS_STRCOPY}
  15. {$define FPC_UNIT_HAS_STRCOPY}
  16. { Created from glibc: libc/sysdeps/x86_64/strcpy.S Version 1.2 }
  17. function strcopy(dest,source : pchar) : pchar;assembler;
  18. {$ifdef win64}
  19. var
  20. rdi,rsi : int64;
  21. {$endif win64}
  22. asm
  23. {$ifdef win64}
  24. movq %rsi,rsi
  25. movq %rdi,rdi
  26. movq %rdx, %rsi
  27. movq %rcx, %rdi
  28. {$endif win64}
  29. movq %rsi, %rcx { Source register. }
  30. andl $7, %ecx { mask alignment bits }
  31. movq %rdi, %rdx { Duplicate destination pointer. }
  32. jz .LFPC_STRCOPY_5 { aligned => start loop }
  33. neg %ecx { We need to align to 8 bytes. }
  34. addl $8,%ecx
  35. { Search the first bytes directly. }
  36. .LFPC_STRCOPY_0:
  37. movb (%rsi), %al { Fetch a byte }
  38. testb %al, %al { Is it NUL? }
  39. movb %al, (%rdx) { Store it }
  40. jz .LFPC_STRCOPY_4 { If it was NUL, done! }
  41. incq %rsi
  42. incq %rdx
  43. decl %ecx
  44. jnz .LFPC_STRCOPY_0
  45. .LFPC_STRCOPY_5:
  46. movq $0xfefefefefefefeff,%r8
  47. { Now the sources is aligned. Unfortunatly we cannot force
  48. to have both source and destination aligned, so ignore the
  49. alignment of the destination. }
  50. .p2align 4
  51. .LFPC_STRCOPY_1:
  52. { 1st unroll. }
  53. movq (%rsi), %rax { Read double word (8 bytes). }
  54. addq $8, %rsi { Adjust pointer for next word. }
  55. movq %rax, %r9 { Save a copy for NUL finding. }
  56. addq %r8, %r9 { add the magic value to the word. We get
  57. carry bits reported for each byte which
  58. is *not* 0 }
  59. jnc .LFPC_STRCOPY_3 { highest byte is NUL => return pointer }
  60. xorq %rax, %r9 { (word+magic)^word }
  61. orq %r8, %r9 { set all non-carry bits }
  62. incq %r9 { add 1: if one carry bit was *not* set
  63. the addition will not result in 0. }
  64. jnz .LFPC_STRCOPY_3 { found NUL => return pointer }
  65. movq %rax, (%rdx) { Write value to destination. }
  66. addq $8, %rdx { Adjust pointer. }
  67. { 2nd unroll. }
  68. movq (%rsi), %rax { Read double word (8 bytes). }
  69. addq $8, %rsi { Adjust pointer for next word. }
  70. movq %rax, %r9 { Save a copy for NUL finding. }
  71. addq %r8, %r9 { add the magic value to the word. We get
  72. carry bits reported for each byte which
  73. is *not* 0 }
  74. jnc .LFPC_STRCOPY_3 { highest byte is NUL => return pointer }
  75. xorq %rax, %r9 { (word+magic)^word }
  76. orq %r8, %r9 { set all non-carry bits }
  77. incq %r9 { add 1: if one carry bit was *not* set
  78. the addition will not result in 0. }
  79. jnz .LFPC_STRCOPY_3 { found NUL => return pointer }
  80. movq %rax, (%rdx) { Write value to destination. }
  81. addq $8, %rdx { Adjust pointer. }
  82. { 3rd unroll. }
  83. movq (%rsi), %rax { Read double word (8 bytes). }
  84. addq $8, %rsi { Adjust pointer for next word. }
  85. movq %rax, %r9 { Save a copy for NUL finding. }
  86. addq %r8, %r9 { add the magic value to the word. We get
  87. carry bits reported for each byte which
  88. is *not* 0 }
  89. jnc .LFPC_STRCOPY_3 { highest byte is NUL => return pointer }
  90. xorq %rax, %r9 { (word+magic)^word }
  91. orq %r8, %r9 { set all non-carry bits }
  92. incq %r9 { add 1: if one carry bit was *not* set
  93. the addition will not result in 0. }
  94. jnz .LFPC_STRCOPY_3 { found NUL => return pointer }
  95. movq %rax, (%rdx) { Write value to destination. }
  96. addq $8, %rdx { Adjust pointer. }
  97. { 4th unroll. }
  98. movq (%rsi), %rax { Read double word (8 bytes). }
  99. addq $8, %rsi { Adjust pointer for next word. }
  100. movq %rax, %r9 { Save a copy for NUL finding. }
  101. addq %r8, %r9 { add the magic value to the word. We get
  102. carry bits reported for each byte which
  103. is *not* 0 }
  104. jnc .LFPC_STRCOPY_3 { highest byte is NUL => return pointer }
  105. xorq %rax, %r9 { (word+magic)^word }
  106. orq %r8, %r9 { set all non-carry bits }
  107. incq %r9 { add 1: if one carry bit was *not* set
  108. the addition will not result in 0. }
  109. jnz .LFPC_STRCOPY_3 { found NUL => return pointer }
  110. movq %rax, (%rdx) { Write value to destination. }
  111. addq $8, %rdx { Adjust pointer. }
  112. jmp .LFPC_STRCOPY_1 { Next iteration. }
  113. { Do the last few bytes. %rax contains the value to write.
  114. The loop is unrolled twice. }
  115. .p2align 4
  116. .LFPC_STRCOPY_3:
  117. { Note that stpcpy needs to return with the value of the NUL
  118. byte. }
  119. movb %al, (%rdx) { 1st byte. }
  120. testb %al, %al { Is it NUL. }
  121. jz .LFPC_STRCOPY_4 { yes, finish. }
  122. incq %rdx { Increment destination. }
  123. movb %ah, (%rdx) { 2nd byte. }
  124. testb %ah, %ah { Is it NUL?. }
  125. jz .LFPC_STRCOPY_4 { yes, finish. }
  126. incq %rdx { Increment destination. }
  127. shrq $16, %rax { Shift... }
  128. jmp .LFPC_STRCOPY_3 { and look at next two bytes in %rax. }
  129. .LFPC_STRCOPY_4:
  130. movq %rdi, %rax { Source is return value. }
  131. {$ifdef win64}
  132. movq rsi,%rsi
  133. movq rdi,%rdi
  134. {$endif win64}
  135. end;
  136. {$endif FPC_UNIT_HAS_STRCOPY}
  137. {$ifndef FPC_UNIT_HAS_STRCOMP}
  138. {$define FPC_UNIT_HAS_STRCOMP}
  139. { Created from glibc: libc/sysdeps/x86_64/strcmp.S Version 1.2 }
  140. function StrComp(Str1, Str2: PChar): SizeInt;assembler;
  141. {$ifdef win64}
  142. var
  143. rdi,rsi : int64;
  144. {$endif win64}
  145. asm
  146. {$ifdef win64}
  147. movq %rsi,rsi
  148. movq %rdi,rdi
  149. movq %rdx, %rsi
  150. movq %rcx, %rdi
  151. {$endif win64}
  152. .LFPC_STRCMP_LOOP:
  153. movb (%rdi), %al
  154. cmpb (%rsi), %al
  155. jne .LFPC_STRCMP_NEG
  156. incq %rdi
  157. incq %rsi
  158. testb %al, %al
  159. jnz .LFPC_STRCMP_LOOP
  160. xorq %rax, %rax
  161. jmp .Lexit
  162. .LFPC_STRCMP_NEG:
  163. movq $1, %rax
  164. movq $-1, %rcx
  165. cmovbq %rcx, %rax
  166. .Lexit:
  167. {$ifdef win64}
  168. movq rsi,%rsi
  169. movq rdi,%rdi
  170. {$endif win64}
  171. end;
  172. {$endif FPC_UNIT_HAS_STRCOMP}