strings.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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) : SizeInt;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. end;
  124. {$define FPC_UNIT_HAS_STRCOMP}
  125. { Created from glibc: libc/sysdeps/x86_64/strcmp.S Version 1.2 }
  126. function StrComp(Str1, Str2: PChar): SizeInt;assembler;
  127. asm
  128. .LFPC_STRCMP_LOOP:
  129. movb (%rdi), %al
  130. cmpb (%rsi), %al
  131. jne .LFPC_STRCMP_NEG
  132. incq %rdi
  133. incq %rsi
  134. testb %al, %al
  135. jnz .LFPC_STRCMP_LOOP
  136. xorq %rax, %rax
  137. jmp .Lexit
  138. .LFPC_STRCMP_NEG:
  139. movq $1, %rax
  140. movq $-1, %rcx
  141. cmovbq %rcx, %rax
  142. .Lexit:
  143. end;
  144. {
  145. $Log$
  146. Revision 1.5 2004-11-02 15:34:10 florian
  147. * fixed strcomp
  148. Revision 1.4 2004/05/01 23:55:18 peter
  149. * replace strlenint with sizeint
  150. Revision 1.3 2004/02/18 22:00:59 peter
  151. * use SizeInt instead of longint
  152. Revision 1.2 2003/04/30 22:11:06 florian
  153. + for a lot of x86-64 dependend files mostly dummies added
  154. Revision 1.1 2003/04/30 16:36:39 florian
  155. + support for generic pchar routines added
  156. + some basic rtl stuff for x86-64 added
  157. }