strings.inc 7.8 KB

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