strings.inc 7.3 KB

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