strings.inc 7.9 KB

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