strings.inc 7.7 KB

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