x86_64.inc 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2002 by Florian Klaempfl and Sergei Gorelkin
  4. Members of the Free Pascal development team
  5. Processor dependent implementation for the system unit for
  6. the x86-64 architecture
  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. {$asmmode GAS}
  14. {****************************************************************************
  15. Primitives
  16. ****************************************************************************}
  17. {$ifndef win64}
  18. {$define use_fast_repmovstos} { REP STOS uses nonvolatile RDI and would require a stack frame on Win64 to be SEH-compliant. }
  19. {$endif}
  20. {$ifdef use_fast_repmovstos}
  21. var
  22. fast_large_repmovstosb : boolean; { Enhanced REP MOVSB and STOSB (ERMSB) feature @ CPUID(7).ebx[9]. }
  23. {$endif}
  24. var
  25. has_sse41_support,fpc_cpuinit_performed : boolean;
  26. {$define FPC_SYSTEM_HAS_SPTR}
  27. Function Sptr : Pointer;assembler;nostackframe;
  28. asm
  29. movq %rsp,%rax
  30. end;
  31. {$IFNDEF INTERNAL_BACKTRACE}
  32. {$define FPC_SYSTEM_HAS_GET_FRAME}
  33. function get_frame:pointer;assembler;nostackframe;
  34. asm
  35. movq %rbp,%rax
  36. end;
  37. {$ENDIF not INTERNAL_BACKTRACE}
  38. {$define FPC_SYSTEM_HAS_GET_PC_ADDR}
  39. function get_pc_addr:pointer;assembler;nostackframe;
  40. asm
  41. movq (%rsp),%rax
  42. end;
  43. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  44. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;
  45. begin
  46. get_caller_addr:=framebp;
  47. if assigned(framebp) then
  48. get_caller_addr:=PPointer(framebp)[1];
  49. end;
  50. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  51. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;
  52. begin
  53. get_caller_frame:=framebp;
  54. if assigned(framebp) then
  55. get_caller_frame:=PPointer(framebp)^;
  56. end;
  57. // The following assembler procedures are disabled for FreeBSD due to
  58. // multiple issues with its old GNU assembler (Mantis #19188).
  59. // Even after fixing them, it can be enabled only for the trunk version,
  60. // otherwise bootstrapping won't be possible.
  61. // Modified to use oldbinutils as in cpu.pp source, to allow easier use for other targets.
  62. {$ifdef freebsd}
  63. {$ifndef overridebinutils}
  64. {$define oldbinutils}
  65. {$endif}
  66. {$endif freebsd}
  67. {$ifndef oldbinutils}
  68. {$ifndef FPC_SYSTEM_HAS_MOVE}
  69. {$define FPC_SYSTEM_HAS_MOVE}
  70. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];assembler;nostackframe;
  71. { Linux: rdi source, rsi dest, rdx count
  72. win64: rcx source, rdx dest, r8 count }
  73. const
  74. NtThreshold = 256 * 1024; { this limit must be processor-specific (1/2 L2 cache size) }
  75. PrefetchDistance = 512;
  76. asm
  77. {$ifndef win64}
  78. mov %rdx, %r8
  79. mov %rsi, %rdx
  80. mov %rdi, %rcx
  81. {$endif win64}
  82. cmp $3, %r8
  83. jle .L3OrLess
  84. cmp $8, %r8
  85. jle .L4to8
  86. cmp $16, %r8
  87. jle .L9to16
  88. movups (%rcx), %xmm4 { First and last 16 bytes, used both in .L33OrMore and 17–32 branch. }
  89. movups -16(%rcx,%r8), %xmm5
  90. cmp $32, %r8
  91. jg .L33OrMore
  92. movups %xmm4, (%rdx) { 17–32 bytes }
  93. movups %xmm5, -16(%rdx,%r8)
  94. ret
  95. .balign 16
  96. .L3OrLess:
  97. cmp $1, %r8
  98. jl .LZero
  99. movzbl (%rcx), %eax
  100. je .LOne
  101. movzwl -2(%rcx,%r8), %r9d
  102. mov %r9w, -2(%rdx,%r8)
  103. .LOne:
  104. mov %al, (%rdx)
  105. .LZero:
  106. ret
  107. .L4to8:
  108. mov (%rcx), %eax
  109. mov -4(%rcx,%r8), %r9d
  110. mov %eax, (%rdx)
  111. mov %r9d, -4(%rdx,%r8)
  112. ret
  113. .L9to16:
  114. mov (%rcx), %rax
  115. mov -8(%rcx,%r8), %r9
  116. mov %rax, (%rdx)
  117. mov %r9, -8(%rdx,%r8)
  118. .Lquit:
  119. ret
  120. .byte 102,102,102,102,102,102,102,102,102,102,102,144 { Turns .balign 16 before .Lloop32f into a no-op. }
  121. .L33OrMore:
  122. movups -32(%rcx,%r8), %xmm3 { Second vector from the end. Wasted read if .Lback branch is taken (it uses second vector from the start instead), }
  123. { but -32(%rcx,%r8) is about to become not accessible so easily, .Lback is rare, and small .Lback is even rarer / matters even less. }
  124. sub %rdx, %rcx { rcx = src - dest }
  125. jz .Lquit { exit if src=dest }
  126. mov %rcx, %rax
  127. neg %rax
  128. cmp %rax, %r8
  129. ja .Lback { count (r8) > unsigned(dest - src) (rax) if regions overlap }
  130. mov %rdx, %r9 { remember original dest to write first 16 bytes }
  131. add %rdx, %r8 { Move dest to the next 16-byte boundary. +16 if already aligned, as first 16 bytes will be writen separately anyway. }
  132. add $16, %rdx
  133. and $-16, %rdx
  134. sub %rdx, %r8
  135. .LRestAfterNTf:
  136. sub $32, %r8 { During the N× loop, r8 is N bytes less than actually remained to allow sub N+jae .LLoop instead of sub N+cmp N+jae .LLoop. }
  137. jbe .LPost32f
  138. cmp $NtThreshold-32, %r8
  139. jae .Lntf { might jump back right away after more checks, but the branch is taken only on huge moves so it's better to take these checks out of here... }
  140. .balign 16 { no-op }
  141. .Lloop32f:
  142. movups (%rcx,%rdx), %xmm0
  143. movaps %xmm0, (%rdx)
  144. movups 16(%rcx,%rdx), %xmm0
  145. movaps %xmm0, 16(%rdx)
  146. add $32, %rdx
  147. sub $32, %r8
  148. ja .Lloop32f
  149. .LPost32f: { +32 fixup not applied after 32× loop, r8 = remaining - 32 here. }
  150. movups %xmm3, (%rdx, %r8)
  151. movups %xmm5, 16(%rdx,%r8) { Write first and last 16 bytes after everything else. }
  152. movups %xmm4, (%r9) { Important for <16-byte step between src and dest. }
  153. ret
  154. .balign 16
  155. .Lntf:
  156. cmp $NtThreshold, %rcx { Maybe change mind: don't bother bypassing cache if src and dest are close to each other }
  157. jb .Lloop32f { (this check is performed here to not stand in the way of smaller counts) }
  158. sub $PrefetchDistance+32, %r8 { r8 = remaining - prefetch distance - bytes per loop (64), but 32 was subtracted already. }
  159. .balign 16 { no-op }
  160. .Lntloop64f:
  161. prefetchnta 0+PrefetchDistance(%rcx,%rdx,1)
  162. movups (%rcx,%rdx,1), %xmm0
  163. movntps %xmm0, (%rdx)
  164. movups 16(%rcx,%rdx,1), %xmm0
  165. movntps %xmm0, 16(%rdx)
  166. movups 32(%rcx,%rdx,1), %xmm0
  167. movntps %xmm0, 32(%rdx)
  168. movups 48(%rcx,%rdx,1), %xmm0
  169. movntps %xmm0, 48(%rdx)
  170. add $64, %rdx
  171. sub $64, %r8
  172. jae .Lntloop64f
  173. sfence
  174. add $PrefetchDistance+64, %r8
  175. jmpq .LRestAfterNTf { go handle remaining bytes }
  176. .byte 102,102,102,102,102,102,144 { Turns .balign 16 before .Lloop32b into a no-op. }
  177. { backwards move }
  178. .Lback:
  179. movups 16(%rcx,%rdx), %xmm3 { Second vector from the start. }
  180. lea (%rdx,%r8), %r9 { points to the end of dest; remember to write last 16 bytes }
  181. lea -1(%r9), %r8 { move dest to the previous 16-byte boundary... }
  182. and $-16, %r8
  183. sub %rdx, %r8
  184. add %r8, %rdx
  185. .LRestAfterNTb:
  186. sub $32, %r8
  187. jbe .LPost32b
  188. cmp $NtThreshold-32, %r8
  189. jae .Lntb
  190. .balign 16 { no-op }
  191. .Lloop32b:
  192. sub $32, %rdx
  193. movups 16(%rcx,%rdx), %xmm0
  194. movaps %xmm0, 16(%rdx)
  195. movups (%rcx,%rdx), %xmm0
  196. movaps %xmm0, (%rdx)
  197. sub $32, %r8
  198. ja .Lloop32b
  199. .LPost32b:
  200. sub %r8, %rdx
  201. movups %xmm3, -16(%rdx)
  202. movups %xmm4, -32(%rdx)
  203. movups %xmm5, -16(%r9)
  204. ret
  205. .balign 16
  206. .Lntb:
  207. cmp $-NtThreshold,%rcx
  208. jnb .Lloop32b
  209. sub $PrefetchDistance+32, %r8
  210. .balign 16 { no-op }
  211. .Lntloop64b:
  212. prefetchnta -PrefetchDistance(%rcx,%rdx,1)
  213. sub $64, %rdx
  214. movups 48(%rcx,%rdx,1), %xmm0
  215. movntps %xmm0, 48(%rdx)
  216. movups 32(%rcx,%rdx,1), %xmm0
  217. movntps %xmm0, 32(%rdx)
  218. movups 16(%rcx,%rdx,1), %xmm0
  219. movntps %xmm0, 16(%rdx)
  220. movups (%rcx,%rdx,1), %xmm0
  221. movntps %xmm0, (%rdx)
  222. sub $64, %r8
  223. jae .Lntloop64b
  224. sfence
  225. add $PrefetchDistance+64, %r8
  226. jmpq .LRestAfterNTb
  227. end;
  228. {$endif FPC_SYSTEM_HAS_MOVE}
  229. {$if not defined(FPC_SYSTEM_HAS_FILLCHAR)
  230. or not defined(FPC_SYSTEM_HAS_FILLWORD)
  231. or not defined(FPC_SYSTEM_HAS_FILLDWORD)
  232. or not defined(FPC_SYSTEM_HAS_FILLQWORD)}
  233. procedure FillXxxx_MoreThanTwoXmms; assembler; nostackframe;
  234. { Input:
  235. rcx = 'x'
  236. rdx = byte count
  237. xmm0 = pattern for ALIGNED writes
  238. First and last 16 bytes are written. }
  239. const
  240. {$ifdef use_fast_repmovstos}
  241. ErmsThreshold = 1536;
  242. {$endif}
  243. NtThreshold = 4 * 1024 * 1024;
  244. asm
  245. { x can start and end misaligned on the vector boundary:
  246. x = ~~][H1][H2][...][T2][T1]~
  247. [UH] [UT]
  248. UH (“unaligned head”) potentially overlaps with H1 and is already written with 'movdqu' by the caller.
  249. At least 1 of its bytes is exclusive to it, i.e. if x is already aligned, H1 starts at byte 16.
  250. H1 and so on are called “aligned heads” or just “heads”.
  251. T1 and so on are called “aligned tails” or just “tails”.
  252. UT (“unaligned tail”) is written by the caller as well.
  253. At least 1 of its bytes is exclusive to it as well, that’s why 65 is subtracted below instead of 64. }
  254. lea -65(%rcx,%rdx), %rax
  255. and $-16, %rax { rax = “T4” (possibly fictive). }
  256. mov %rax, %rdx { Remember T4 to rdx. }
  257. and $-16, %rcx { rcx = H1 − 16. }
  258. sub %rcx, %rax { rax = aligned byte count − 48. }
  259. movdqa %xmm0, 16(%rcx) { Write H1. }
  260. cmp $32-48, %rax
  261. jle .LOneAlignedTailWrite
  262. movdqa %xmm0, 32(%rcx) { Write H2. }
  263. cmp $64-48, %rax
  264. jle .LTwoAlignedTailWrites
  265. sub $48, %rax { rax = aligned byte count − 96 (32 bytes already written + 64 bytes written after loop). }
  266. jle .LFourAlignedTailWrites
  267. add $48, %rcx { rcx = H3. }
  268. {$ifdef use_fast_repmovstos}
  269. cmp $ErmsThreshold-64, %rax { Need to write aligned byte count − 32 bytes already written. rax = aligned byte count − 96, so compare rax + 64 to ErmsThreshold, or rax to ErmsThreshold − 64. }
  270. jae .LRepStos
  271. {$else}
  272. cmp $NtThreshold, %rax
  273. jae .L64xNT_Body
  274. {$endif}
  275. .balign 16
  276. .L64x_Body:
  277. movdqa %xmm0, (%rcx)
  278. movdqa %xmm0, 16(%rcx)
  279. movdqa %xmm0, 32(%rcx)
  280. movdqa %xmm0, 48(%rcx)
  281. add $64, %rcx
  282. sub $64, %rax
  283. ja .L64x_Body
  284. .LFourAlignedTailWrites:
  285. movdqa %xmm0, (%rdx) { T4 }
  286. movdqa %xmm0, 16(%rdx) { T3 }
  287. .LTwoAlignedTailWrites:
  288. movdqa %xmm0, 32(%rdx) { T2 }
  289. .LOneAlignedTailWrite:
  290. movdqa %xmm0, 48(%rdx) { T1 }
  291. ret
  292. {$ifdef use_fast_repmovstos}
  293. .LRepStos:
  294. {$ifdef FPC_PIC}
  295. movq fast_large_repmovstosb@GOTPCREL(%rip), %r8
  296. cmpb $1, (%r8)
  297. {$else FPC_PIC}
  298. cmpb $1, fast_large_repmovstosb(%rip)
  299. {$endif FPC_PIC}
  300. jne .LRepStosIsNotBetter
  301. {$ifdef win64}
  302. push %rdi { For tests on Windows; however this is SEH incompliant so the entire use_fast_repmovstos branch is disabled by default! }
  303. {$endif}
  304. mov %rcx, %rdi { rdi = REP STOS destination. }
  305. lea 64(%rax), %rcx
  306. shr $3, %rcx { rcx = count of REP STOSQ blocks up to T1 (might be 1 more than strictly required if T1 and UT overlap is 8 or more, don’t care). }
  307. movq %xmm0, %rax { recover pattern for aligned writes back to GPR :) }
  308. rep stosq
  309. {$ifdef win64}
  310. pop %rdi
  311. {$endif}
  312. ret
  313. {$endif}
  314. .LRepStosIsNotBetter:
  315. cmp $NtThreshold-64, %rax
  316. jb .L64x_Body
  317. .balign 16
  318. .L64xNT_Body:
  319. movntdq %xmm0, (%rcx)
  320. movntdq %xmm0, 16(%rcx)
  321. movntdq %xmm0, 32(%rcx)
  322. movntdq %xmm0, 48(%rcx)
  323. add $64, %rcx
  324. sub $64, %rax
  325. ja .L64xNT_Body
  326. sfence
  327. jmp .LFourAlignedTailWrites
  328. end;
  329. {$endif FPC_SYSTEM_HAS_FILLxxxx}
  330. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  331. {$define FPC_SYSTEM_HAS_FILLCHAR}
  332. Procedure FillChar(var x;count:SizeInt;value:byte);assembler;nostackframe;
  333. asm
  334. { win64: rcx dest, rdx count, r8b value
  335. linux: rdi dest, rsi count, rdx value }
  336. movzbl {$ifdef win64} %r8b {$else} %dl {$endif}, %eax
  337. imul $0x01010101, %eax
  338. {$ifndef win64}
  339. mov %rsi, %rdx
  340. mov %rdi, %rcx
  341. {$endif win64}
  342. cmp $3, %rdx
  343. jle .L3OrLess
  344. cmp $16, %rdx
  345. jl .L4to15
  346. movd %eax, %xmm0
  347. pshufd $0, %xmm0, %xmm0
  348. movdqu %xmm0, (%rcx)
  349. movdqu %xmm0, -16(%rcx,%rdx)
  350. cmp $32, %rdx
  351. jg FillXxxx_MoreThanTwoXmms
  352. ret
  353. .L4to15:
  354. mov %eax, (%rcx)
  355. cmp $8, %edx
  356. jle .LLast4
  357. mov %eax, 4(%rcx)
  358. mov %eax, -8(%rcx,%rdx)
  359. .LLast4:
  360. mov %eax, -4(%rcx,%rdx)
  361. ret
  362. .L3OrLess:
  363. test %rdx, %rdx
  364. jle .LQuit
  365. mov %al, (%rcx)
  366. mov %al, -1(%rcx,%rdx)
  367. shr $1, %edx
  368. mov %al, (%rcx,%rdx)
  369. .LQuit:
  370. end;
  371. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  372. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  373. {$define FPC_SYSTEM_HAS_FILLWORD}
  374. procedure FillWord(var x;count:SizeInt;value:word);assembler;nostackframe;
  375. asm
  376. {$ifdef win64}
  377. movzwl %r8w, %eax
  378. shl $16, %r8d
  379. or %r8d, %eax
  380. {$else}
  381. movzwl %dx, %eax
  382. shl $16, %edx
  383. or %edx, %eax
  384. mov %rsi, %rdx
  385. mov %rdi, %rcx
  386. {$endif}
  387. cmp $3, %rdx
  388. jle .L3OrLess
  389. cmp $8, %rdx
  390. jle .L4to8
  391. movd %eax, %xmm0
  392. pshufd $0, %xmm0, %xmm0 { xmm0 = pattern for unaligned writes }
  393. movdqu %xmm0, (%rcx)
  394. movdqu %xmm0, -16(%rcx,%rdx,2)
  395. cmp $16, %rdx
  396. jg .LMoreThanTwoXMMs
  397. ret
  398. .LMoreThanTwoXMMs:
  399. shl $1, %rdx { rdx = byte count }
  400. mov %rcx, %r8
  401. shl $3, %ecx
  402. rol %cl, %eax { misalign the pattern by the misalignment of x }
  403. mov %r8, %rcx
  404. movd %eax, %xmm0
  405. pshufd $0, %xmm0, %xmm0 { xmm0 = pattern for aligned writes }
  406. jmp FillXxxx_MoreThanTwoXmms
  407. .L4to8:
  408. mov %eax, %r8d
  409. shl $32, %r8
  410. or %r8, %rax
  411. mov %rax, (%rcx)
  412. mov %rax, -8(%rcx,%rdx,2)
  413. ret
  414. .L3OrLess:
  415. test %rdx, %rdx
  416. jle .LQuit
  417. mov %ax, (%rcx)
  418. mov %ax, -2(%rcx,%rdx,2)
  419. shr $1, %edx
  420. mov %ax, (%rcx,%rdx,2)
  421. .LQuit:
  422. end;
  423. {$endif FPC_SYSTEM_HAS_FILLWORD}
  424. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  425. {$define FPC_SYSTEM_HAS_FILLDWORD}
  426. procedure FillDWord(var x;count:SizeInt;value:DWord);assembler;nostackframe;
  427. asm
  428. {$ifdef win64}
  429. mov %r8d, %eax
  430. {$else}
  431. mov %edx, %eax
  432. mov %rsi, %rdx
  433. mov %rdi, %rcx
  434. {$endif win64}
  435. cmp $3, %rdx
  436. jle .L3OrLess
  437. cmp $8, %rdx
  438. jle .L4to8
  439. movd %eax, %xmm0
  440. pshufd $0, %xmm0, %xmm0 { xmm0 = pattern for unaligned writes }
  441. movdqu %xmm0, (%rcx)
  442. movdqu %xmm0, -16(%rcx,%rdx,4)
  443. shl $2, %rdx { rdx = byte count }
  444. mov %rcx, %r8
  445. shl $3, %ecx
  446. rol %cl, %eax { misalign the pattern by the misalignment of x }
  447. mov %r8, %rcx
  448. movd %eax, %xmm0
  449. pshufd $0, %xmm0, %xmm0 { xmm0 = pattern for aligned writes }
  450. jmp FillXxxx_MoreThanTwoXmms
  451. .L4to8:
  452. {$ifndef win64} { on win64, eax = r8d already. }
  453. mov %eax, %r8d
  454. {$endif}
  455. shl $32, %r8
  456. or %r8, %rax
  457. mov %rax, (%rcx)
  458. mov %rax, 8(%rcx)
  459. mov %rax, -16(%rcx,%rdx,4)
  460. mov %rax, -8(%rcx,%rdx,4)
  461. ret
  462. .L3OrLess:
  463. test %rdx, %rdx
  464. jle .LQuit
  465. mov %eax, (%rcx)
  466. mov %eax, -4(%rcx,%rdx,4)
  467. shr $1, %edx
  468. mov %eax, (%rcx,%rdx,4)
  469. .LQuit:
  470. end;
  471. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  472. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  473. {$define FPC_SYSTEM_HAS_FILLQWORD}
  474. procedure FillQWord(var x;count:SizeInt;value:QWord);assembler;nostackframe;
  475. asm
  476. {$ifdef win64}
  477. mov %r8, %rax
  478. {$else}
  479. mov %rdx, %rax
  480. mov %rsi, %rdx
  481. mov %rdi, %rcx
  482. {$endif win64}
  483. cmp $2, %rdx
  484. jle .L2OrLess
  485. cmp $6, %rdx
  486. jle .L3to6
  487. movq %rax, %xmm0
  488. punpcklqdq %xmm0, %xmm0 { xmm0 = pattern for unaligned writes }
  489. movdqu %xmm0, (%rcx)
  490. movdqu %xmm0, -16(%rcx,%rdx,8)
  491. shl $3, %rdx { rdx = byte count }
  492. mov %rcx, %r8
  493. shl $3, %ecx
  494. rol %cl, %rax { misalign the pattern by the misalignment of x }
  495. mov %r8, %rcx
  496. movq %rax, %xmm0
  497. punpcklqdq %xmm0, %xmm0 { xmm0 = pattern for aligned writes }
  498. jmp FillXxxx_MoreThanTwoXmms
  499. .L3to6:
  500. mov %rax, (%rcx)
  501. mov %rax, 8(%rcx)
  502. mov %rax, 16(%rcx)
  503. mov %rax, -24(%rcx,%rdx,8)
  504. mov %rax, -16(%rcx,%rdx,8)
  505. mov %rax, -8(%rcx,%rdx,8)
  506. ret
  507. .L2OrLess:
  508. test %rdx, %rdx
  509. jle .LQuit
  510. mov %rax, (%rcx)
  511. mov %rax, -8(%rcx,%rdx,8)
  512. .LQuit:
  513. end;
  514. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  515. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  516. {$define FPC_SYSTEM_HAS_INDEXBYTE}
  517. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt; assembler; nostackframe;
  518. { win64: rcx buf, rdx len, r8b word
  519. linux: rdi buf, rsi len, rdx word }
  520. asm
  521. test len, len
  522. jz .Lnotfound { exit if len=0 }
  523. movd {$ifdef win64} %r8d {$else} %edx {$endif}, %xmm1
  524. {$ifdef win64}
  525. mov %rcx, %r8 { r8 = original ptr, rcx = buf + 16 for aligning & shifts. }
  526. add $16, %rcx
  527. {$else}
  528. lea 16(%rdi), %rcx { rdi = original ptr, rcx = buf + 16 for aligning & shifts. }
  529. {$endif}
  530. punpcklbw %xmm1, %xmm1
  531. and $-0x10, %rcx { first aligned address after buf }
  532. punpcklbw %xmm1, %xmm1
  533. pshufd $0, %xmm1, %xmm1
  534. movdqa -16(%rcx), %xmm0 { Fetch first 16 bytes (up to 15 bytes before target) }
  535. sub {$ifdef win64} %r8 {$else} %rdi {$endif}, %rcx { rcx=number of valid bytes, r8/rdi=original ptr }
  536. pcmpeqb %xmm1, %xmm0 { compare with pattern and get bitmask }
  537. pmovmskb %xmm0, %eax
  538. shl %cl, %eax { shift valid bits into high word }
  539. and $0xffff0000, %eax { clear low word containing invalid bits }
  540. shr %cl, %eax { shift back }
  541. jz .Lcontinue
  542. .Lmatch:
  543. bsf %eax, %eax
  544. lea -16(%rcx,%rax), %rax
  545. cmp %rax, len { check against the buffer length }
  546. jbe .Lnotfound
  547. ret
  548. .balign 16
  549. .Lloop:
  550. movdqa ({$ifdef win64} %r8 {$else} %rdi {$endif},%rcx), %xmm0 { r8/rdi and rcx may have any values, }
  551. add $16, %rcx { but their sum is evenly divisible by 16. }
  552. pcmpeqb %xmm1, %xmm0
  553. pmovmskb %xmm0, %eax
  554. test %eax, %eax
  555. jnz .Lmatch
  556. .Lcontinue:
  557. cmp %rcx, len
  558. ja .Lloop
  559. .Lnotfound:
  560. or $-1, %rax
  561. end;
  562. {$endif FPC_SYSTEM_HAS_INDEXBYTE}
  563. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  564. {$define FPC_SYSTEM_HAS_INDEXWORD}
  565. function IndexWord(Const buf;len:SizeInt;b:word):SizeInt; assembler; nostackframe;
  566. { win64: rcx buf, rdx len, r8b word
  567. linux: rdi buf, rsi len, rdx word }
  568. asm
  569. test len, len
  570. jz .Lnotfound { exit if len=0 }
  571. movd {$ifdef win64} %r8d {$else} %edx {$endif}, %xmm1
  572. {$ifdef win64}
  573. mov %rcx, %r8 { r8 = original ptr, rcx = buf + 16 for aligning & shifts. }
  574. add $16, %rcx
  575. {$else}
  576. lea 16(%rdi), %rcx { rdi = original ptr, rcx = buf + 16 for aligning & shifts. }
  577. {$endif}
  578. punpcklwd %xmm1, %xmm1
  579. and $-0x10, %rcx
  580. pshufd $0, %xmm1, %xmm1
  581. movdqa -16(%rcx), %xmm0 { Fetch first 16 bytes (up to 14 bytes before target) }
  582. sub {$ifdef win64} %r8 {$else} %rdi {$endif}, %rcx { rcx=number of valid bytes }
  583. test $1, {$ifdef win64} %r8b {$else} %dil {$endif} { if buffer isn't aligned to word boundary, }
  584. jnz .Lunaligned { use a different algorithm }
  585. pcmpeqw %xmm1, %xmm0
  586. pmovmskb %xmm0, %eax
  587. shl %cl, %eax
  588. and $0xffff0000, %eax
  589. shr %cl, %eax
  590. shr $1, %ecx { bytes->words }
  591. test %eax, %eax
  592. jz .Lcontinue
  593. .Lmatch:
  594. bsf %eax, %eax
  595. shr $1, %eax { in words }
  596. lea -8(%rcx,%rax), %rax
  597. cmp %rax, len
  598. jbe .Lnotfound { if match is after the specified length, ignore it }
  599. retq
  600. .balign 16
  601. .Lloop:
  602. movdqa ({$ifdef win64} %r8 {$else} %rdi {$endif},%rcx,2), %xmm0
  603. add $8, %rcx
  604. pcmpeqw %xmm1, %xmm0
  605. pmovmskb %xmm0, %eax
  606. test %eax, %eax
  607. jnz .Lmatch
  608. .Lcontinue:
  609. cmp %rcx, len
  610. ja .Lloop
  611. .Lnotfound:
  612. or $-1, %rax
  613. retq
  614. .Lunaligned:
  615. movdqa %xmm1, %xmm2 { (mis)align the pattern (in this particular case: }
  616. psllw $8, %xmm1 { swap bytes of each word of pattern) }
  617. psrlw $8, %xmm2
  618. por %xmm2, %xmm1
  619. pcmpeqb %xmm1, %xmm0
  620. pmovmskb %xmm0, %eax
  621. shl %cl, %eax
  622. and $0xffff0000, %eax
  623. shr %cl, %eax
  624. add len, len { length words -> bytes }
  625. xor %r10d, %r10d { nothing to merge yet }
  626. jmp .Lcontinue_u
  627. .balign 16
  628. .Lloop_u:
  629. movdqa ({$ifdef win64} %r8 {$else} %rdi {$endif},%rcx), %xmm0
  630. add $16, %rcx
  631. pcmpeqb %xmm1, %xmm0 { compare by bytes }
  632. shr $16, %r10d { bit 16 shifts into 0 }
  633. pmovmskb %xmm0, %eax
  634. .Lcontinue_u:
  635. shl $1, %eax { 15:0 -> 16:1 }
  636. or %r10d, %eax { merge bit 0 from previous round }
  637. mov %eax, %r10d
  638. shr $1, %eax { now AND together adjacent pairs of bits }
  639. and %r10d, %eax
  640. and $0x5555, %eax { also reset odd bits }
  641. jnz .Lmatch_u
  642. cmpq %rcx, len
  643. ja .Lloop_u
  644. .Lnotfound_u:
  645. or $-1, %rax
  646. retq
  647. .Lmatch_u:
  648. bsf %eax, %eax
  649. lea -16(%rcx,%rax), %rax
  650. cmp %rax, len
  651. jbe .Lnotfound_u { if match is after the specified length, ignore it }
  652. sar $1, %rax { in words }
  653. end;
  654. {$endif FPC_SYSTEM_HAS_INDEXWORD}
  655. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  656. {$define FPC_SYSTEM_HAS_INDEXDWORD}
  657. function IndexDWord(Const buf;len:SizeInt;b:dword):SizeInt; assembler; nostackframe;
  658. asm
  659. {$ifdef win64}
  660. mov %rcx, %rax
  661. {$else}
  662. mov %rdx, %r8
  663. mov %rsi, %rdx
  664. mov %rdi, %rax
  665. {$endif}
  666. cmp $4, %rdx
  667. jle .LDwordwise_Prepare
  668. sub $4, %rdx
  669. movd %r8d, %xmm1
  670. pshufd $0, %xmm1, %xmm1
  671. .balign 16
  672. .L4x_Body:
  673. movdqu (%rax), %xmm0
  674. pcmpeqd %xmm1, %xmm0
  675. pmovmskb %xmm0, %r8d
  676. test %r8d, %r8d
  677. jnz .LFoundAtMask
  678. add $16, %rax
  679. sub $4, %rdx
  680. jg .L4x_Body
  681. lea (%rax,%rdx,4), %rax
  682. movdqu (%rax), %xmm0
  683. pcmpeqd %xmm1, %xmm0
  684. pmovmskb %xmm0, %r8d
  685. test %r8d, %r8d
  686. jnz .LFoundAtMask
  687. or $-1, %rax
  688. ret
  689. .balign 16 { no-op }
  690. .LDwordwise_Body:
  691. cmp (%rax), %r8d
  692. je .LFoundAtRax
  693. add $4, %rax
  694. .LDwordwise_Prepare:
  695. sub $1, %rdx
  696. jae .LDwordwise_Body
  697. or $-1, %rax
  698. ret
  699. .LFoundAtMask:
  700. bsf %r8d, %r8d
  701. add %r8, %rax
  702. .LFoundAtRax:
  703. sub {$ifdef win64} %rcx {$else} %rdi {$endif}, %rax
  704. shr $2, %rax
  705. end;
  706. {$endif FPC_SYSTEM_HAS_INDEXDWORD}
  707. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  708. {$define FPC_SYSTEM_HAS_INDEXQWORD}
  709. function IndexQWord_Plain(Const buf;len:SizeInt;b:QWord):SizeInt; assembler; nostackframe;
  710. { win64: rcx=buf, rdx=len, r8=b
  711. else: rdi=buf, rsi=len, rdx=b }
  712. asm
  713. mov buf, %rax
  714. sub $8, %rax
  715. .balign 16
  716. .LQwordwise_Next:
  717. add $8, %rax
  718. sub $1, len
  719. jb .LNothing
  720. cmpq b, (%rax)
  721. jne .LQwordwise_Next
  722. sub buf, %rax
  723. shr $3, %rax
  724. ret
  725. .LNothing:
  726. mov $-1, %rax
  727. end;
  728. function IndexQWord_SSE41(Const buf;len:SizeInt;b:QWord):SizeInt; assembler; nostackframe;
  729. { win64: rcx=buf, rdx=len, r8=b
  730. else: rdi=buf, rsi=len, rdx=b }
  731. asm
  732. cmp $6, len
  733. jle IndexQWord_Plain
  734. mov buf, %rax
  735. movq {$ifdef win64} %r8 {$else} %rdx {$endif}, %xmm0
  736. punpcklqdq %xmm0, %xmm0 { xmm0 = pattern of 'b's. }
  737. sub $6, len
  738. .balign 16
  739. .L6x_Loop:
  740. movdqu (%rax), %xmm1
  741. pcmpeqq %xmm0, %xmm1 { xmm1 = cmpeq(vec 0, pattern) }
  742. movdqu 16(%rax), %xmm2
  743. pcmpeqq %xmm0, %xmm2
  744. por %xmm1, %xmm2 { xmm2 = cmpeq(vec 0, pattern) or cmpeq(vec 1, pattern) }
  745. movdqu 32(%rax), %xmm3
  746. pcmpeqq %xmm0, %xmm3
  747. por %xmm2, %xmm3 { xmm3 = cmpeq(vec 0, pattern) or cmpeq(vec 1, pattern) or cmpeq(vec 2, pattern) }
  748. ptest %xmm3, %xmm3
  749. jnz .LFound
  750. add $48, %rax
  751. sub $6, len
  752. jge .L6x_Loop
  753. lea (%rax,{$ifdef win64} %rdx {$else} %rsi {$endif},8), %rax { Point to last 3 vectors. }
  754. cmp $-5, len
  755. jge .L6x_Loop { Reuse .L6x_Loop to compare last 3 vectors, if not compared already. }
  756. mov $-1, %rax
  757. ret
  758. .LFound:
  759. sub buf, %rax
  760. ptest %xmm1, %xmm1
  761. jnz .LFoundAtXmm1
  762. ptest %xmm2, %xmm2
  763. jnz .LFoundAtXmm2
  764. add $16, %rax
  765. movdqa %xmm3, %xmm2
  766. .LFoundAtXmm2:
  767. add $16, %rax
  768. movdqa %xmm2, %xmm1
  769. .LFoundAtXmm1:
  770. pmovmskb %xmm1, %ecx
  771. bsf %ecx, %ecx
  772. add %rcx, %rax
  773. shr $3, %rax
  774. end;
  775. function IndexQWord_Dispatch(const buf;len:SizeInt;b:QWord):SizeInt; forward;
  776. var
  777. IndexQWord_Impl: function(const buf;len:SizeInt;b:QWord):SizeInt = @IndexQWord_Dispatch;
  778. function IndexQWord_Dispatch(const buf;len:SizeInt;b:QWord):SizeInt;
  779. begin
  780. if not fpc_cpuinit_performed then
  781. exit(IndexQWord_Plain(buf,len,b));
  782. if has_sse41_support then
  783. IndexQWord_Impl:=@IndexQWord_SSE41
  784. else
  785. IndexQWord_Impl:=@IndexQWord_Plain;
  786. result:=IndexQWord_Impl(buf,len,b);
  787. end;
  788. function IndexQWord(const buf;len:SizeInt;b:QWord):SizeInt;
  789. begin
  790. result:=IndexQWord_Impl(buf,len,b);
  791. end;
  792. {$endif FPC_SYSTEM_HAS_INDEXQWORD}
  793. {$endif freebsd}
  794. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  795. {$define FPC_SYSTEM_HAS_COMPAREBYTE}
  796. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt; assembler; nostackframe;
  797. { win64: rcx buf, rdx buf, r8 len
  798. linux: rdi buf, rsi buf, rdx len }
  799. asm
  800. {$ifndef win64}
  801. mov %rdx, %r8
  802. mov %rsi, %rdx
  803. mov %rdi, %rcx
  804. {$endif win64}
  805. { rcx = buf1, rdx = buf2, r8 = len }
  806. cmp $1, %r8
  807. jle .L1OrLess
  808. cmp $16, %r8
  809. jae .LVecOrMore
  810. { 2 to 15 bytes: check for page cross. Pessimistic variant that has false positives, but is faster. }
  811. mov %ecx, %eax
  812. or %edx, %eax
  813. and $4095, %eax
  814. cmp $4080, %eax
  815. ja .LCantOverReadBoth
  816. { Over-read both as XMMs. }
  817. movdqu (%rcx), %xmm0
  818. movdqu (%rdx), %xmm1
  819. pcmpeqb %xmm1, %xmm0
  820. pmovmskb %xmm0, %eax
  821. inc %ax
  822. jz .LNothing
  823. bsf %eax, %eax
  824. cmp %r8d, %eax { Ignore garbage beyond 'len'. }
  825. jae .LNothing
  826. movzbl (%rdx,%rax), %edx
  827. movzbl (%rcx,%rax), %eax
  828. sub %rdx, %rax
  829. ret
  830. .balign 16
  831. .LNothing:
  832. xor %eax, %eax
  833. ret
  834. .LAligned32xLoop_TwoVectorsDiffer:
  835. add %rcx, %rdx { restore rdx = buf2 }
  836. pmovmskb %xmm0, %r8d { Is there a difference in the first vector? }
  837. inc %r8w
  838. jz .LVec1Differs { No difference in the first vector, xmm0 is all ones, eax = pmovmskb(pcmpeqb(buf1 + 16, buf2 + 16)) from the loop body. }
  839. mov %r8d, %eax
  840. .LVec0Differs:
  841. bsf %eax, %eax
  842. movzbl (%rdx,%rax), %edx
  843. movzbl (%rcx,%rax), %eax
  844. sub %rdx, %rax
  845. ret
  846. .byte 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00 { Turn .balign 16 before .LAligned32xLoop_Body into a no-op. }
  847. .LVecOrMore:
  848. { Compare first vectors. }
  849. movdqu (%rcx), %xmm0
  850. movdqu (%rdx), %xmm1
  851. pcmpeqb %xmm1, %xmm0
  852. pmovmskb %xmm0, %eax
  853. inc %ax
  854. jnz .LVec0Differs
  855. sub $32, %r8
  856. jbe .LLastVec
  857. { Compare second vectors. }
  858. movdqu 16(%rcx), %xmm0
  859. movdqu 16(%rdx), %xmm1
  860. pcmpeqb %xmm1, %xmm0
  861. pmovmskb %xmm0, %eax
  862. inc %ax
  863. jnz .LVec1Differs
  864. cmp $32, %r8
  865. jbe .LLastTwoVectors
  866. { More than four vectors: aligned loop. }
  867. lea -32(%rcx,%r8), %r8 { buffer end - last two vectors handled separately - first two vectors already analyzed (by the fact ecx was still len - 32). }
  868. sub %rcx, %rdx { rdx = buf2 - buf1 }
  869. and $-16, %rcx { Align buf1. First two vectors already analyzed are skipped by +32 on the first loop iteration. }
  870. sub %rcx, %r8 { r8 = count to be handled with loop }
  871. .balign 16 { no-op }
  872. .LAligned32xLoop_Body:
  873. add $32, %rcx
  874. { Compare two XMMs, reduce the result with 'and'. }
  875. movdqu (%rdx,%rcx), %xmm0
  876. pcmpeqb (%rcx), %xmm0 { xmm0 = pcmpeqb(buf1, buf2) }
  877. movdqu 16(%rdx,%rcx), %xmm1
  878. pcmpeqb 16(%rcx), %xmm1
  879. pand %xmm0, %xmm1 { xmm1 = xmm0 and pcmpeqb(buf1 + 16, buf2 + 16) }
  880. pmovmskb %xmm1, %eax
  881. inc %ax
  882. jnz .LAligned32xLoop_TwoVectorsDiffer
  883. sub $32, %r8
  884. ja .LAligned32xLoop_Body
  885. add %rcx, %rdx { restore rdx = buf2 }
  886. add $32, %r8
  887. .LLastTwoVectors:
  888. movdqu (%rcx,%r8), %xmm0
  889. movdqu (%rdx,%r8), %xmm1
  890. pcmpeqb %xmm1, %xmm0
  891. pmovmskb %xmm0, %eax
  892. inc %ax
  893. jnz .LVecEm2Differs
  894. .LLastVec:
  895. movdqu 16(%rcx,%r8), %xmm0
  896. movdqu 16(%rdx,%r8), %xmm1
  897. pcmpeqb %xmm1, %xmm0
  898. pmovmskb %xmm0, %eax
  899. inc %ax
  900. jnz .LVecEm1Differs
  901. xor %eax, %eax
  902. ret
  903. .LVec1Differs:
  904. xor %r8d, %r8d
  905. .LVecEm1Differs:
  906. add $16, %r8
  907. .LVecEm2Differs:
  908. bsf %eax, %eax
  909. add %r8, %rax
  910. movzbl (%rdx,%rax), %edx
  911. movzbl (%rcx,%rax), %eax
  912. sub %rdx, %rax
  913. ret
  914. .LCantOverReadBoth:
  915. cmp $8, %r8d
  916. ja .L9to15
  917. cmp $3, %r8d
  918. jle .L2to3
  919. mov (%rcx), %eax
  920. mov (%rdx), %r9d
  921. cmp %r9d, %eax
  922. jne .L4xOr8xDiffer
  923. mov -4(%rcx,%r8), %eax
  924. mov -4(%rdx,%r8), %r9d
  925. cmp %r9d, %eax
  926. jne .L4xOr8xDiffer
  927. xor %eax, %eax
  928. ret
  929. .L9to15:
  930. mov (%rcx), %rax
  931. mov (%rdx), %r9
  932. cmp %r9, %rax
  933. jne .L4xOr8xDiffer
  934. mov -8(%rcx,%r8), %rax
  935. mov -8(%rdx,%r8), %r9
  936. cmp %r9, %rax
  937. jne .L4xOr8xDiffer
  938. xor %eax, %eax
  939. ret
  940. .L4xOr8xDiffer:
  941. bswap %r9
  942. bswap %rax
  943. cmp %r9, %rax
  944. sbb %rax, %rax
  945. or $1, %rax
  946. ret
  947. .L2to3:
  948. movzwl (%rcx), %eax
  949. bswap %eax
  950. shr $1, %eax
  951. mov -1(%rcx,%r8), %al
  952. movzwl (%rdx), %ecx
  953. bswap %ecx
  954. shr $1, %ecx
  955. mov -1(%rdx,%r8), %cl
  956. sub %rcx, %rax
  957. ret
  958. .L1OrLess:
  959. jl .LUnbounded_Prepare
  960. movzbl (%rcx), %eax
  961. movzbl (%rdx), %edx
  962. sub %rdx, %rax
  963. ret
  964. .LUnbounded_Prepare:
  965. sub %rcx, %rdx { rdx = buf2 - buf1 }
  966. test %r8, %r8
  967. jnz .LUnbounded_Body
  968. xor %eax, %eax
  969. ret
  970. .balign 16
  971. .LUnbounded_Next:
  972. add $1, %rcx
  973. .LUnbounded_Body:
  974. movzbl (%rdx,%rcx), %eax
  975. cmp %al, (%rcx)
  976. je .LUnbounded_Next
  977. sbb %rax, %rax
  978. or $1, %rax
  979. end;
  980. {$endif FPC_SYSTEM_HAS_COMPAREBYTE}
  981. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  982. {$define FPC_SYSTEM_HAS_COMPAREWORD}
  983. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt; assembler; nostackframe;
  984. asm
  985. {$ifndef win64}
  986. mov %rdx, %r8
  987. mov %rsi, %rdx
  988. mov %rdi, %rcx
  989. {$endif win64}
  990. sub %rcx, %rdx { rdx = buf2 - buf1 }
  991. cmp $1, %r8
  992. jle .LWordwise_Prepare
  993. mov %r8, %rax
  994. shr $62, %rax
  995. jnz .LWordwise_Prepare
  996. cmp $8, %r8
  997. jge .LVecOrMore
  998. lea (%rdx,%rcx), %eax
  999. or %ecx, %eax
  1000. and $4095, %eax
  1001. cmp $4080, %eax
  1002. ja .LWordwise_Prepare
  1003. movdqu (%rdx,%rcx), %xmm0
  1004. movdqu (%rcx), %xmm1
  1005. pcmpeqw %xmm1, %xmm0
  1006. pmovmskb %xmm0, %eax
  1007. shl $1, %r8 { convert to bytes }
  1008. inc %ax
  1009. jz .LNothing
  1010. bsf %eax, %eax
  1011. cmp %r8d, %eax
  1012. jb .LSubtractWords
  1013. .LNothing:
  1014. xor %eax, %eax
  1015. ret
  1016. .balign 16
  1017. .LWordwise_Body:
  1018. movzwl (%rdx,%rcx), %eax
  1019. cmp %ax, (%rcx)
  1020. jne .LDoSbb
  1021. add $2, %rcx
  1022. .LWordwise_Prepare:
  1023. sub $1, %r8
  1024. jae .LWordwise_Body
  1025. xor %eax, %eax
  1026. ret
  1027. .LDoSbb:
  1028. sbb %rax, %rax
  1029. or $1, %rax
  1030. ret
  1031. .LVec0Differs:
  1032. bsf %eax, %eax
  1033. .LSubtractWords:
  1034. add %rcx, %rdx { recover rdx = buf2 }
  1035. movzwl (%rdx,%rax), %edx
  1036. movzwl (%rcx,%rax), %eax
  1037. sub %rdx, %rax
  1038. ret
  1039. .LVecOrMore:
  1040. movdqu (%rdx,%rcx), %xmm0 { Compare first vectors. }
  1041. movdqu (%rcx), %xmm1
  1042. pcmpeqw %xmm1, %xmm0
  1043. pmovmskb %xmm0, %eax
  1044. inc %ax
  1045. jnz .LVec0Differs
  1046. shl $1, %r8 { convert to bytes }
  1047. sub $32, %r8 { first 16 bytes already analyzed + last 16 bytes analyzed separately }
  1048. jle .LLastVec
  1049. mov %rcx, %r9 { save original buf1 to recover word position if byte mismatch found (aligned loop works in bytes to support misaligned buf1). }
  1050. add %rcx, %r8
  1051. and $-16, %rcx { align buf1; +16 is performed by the loop. }
  1052. sub %rcx, %r8
  1053. .balign 16
  1054. .LAligned8xLoop_Body:
  1055. add $16, %rcx
  1056. movdqu (%rdx,%rcx), %xmm0
  1057. pcmpeqb (%rcx), %xmm0
  1058. pmovmskb %xmm0, %eax
  1059. inc %ax
  1060. jnz .LAligned8xLoop_VecDiffers
  1061. sub $16, %r8
  1062. ja .LAligned8xLoop_Body
  1063. .LLastVec:
  1064. lea 16(%rcx,%r8), %rcx { point to the last 16 bytes }
  1065. movdqu (%rdx,%rcx), %xmm0
  1066. movdqu (%rcx), %xmm1
  1067. pcmpeqw %xmm1, %xmm0
  1068. pmovmskb %xmm0, %eax
  1069. inc %ax
  1070. jnz .LVec0Differs
  1071. xor %eax, %eax
  1072. ret
  1073. .LAligned8xLoop_VecDiffers:
  1074. bsf %eax, %eax
  1075. add %rax, %rcx
  1076. sub %r9, %rcx
  1077. and $-2, %rcx
  1078. add %r9, %rcx
  1079. movzwl (%rdx,%rcx), %edx
  1080. movzwl (%rcx), %eax
  1081. sub %rdx, %rax
  1082. end;
  1083. {$endif FPC_SYSTEM_HAS_COMPAREWORD}
  1084. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  1085. {$define FPC_SYSTEM_HAS_COMPAREDWORD}
  1086. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt; assembler; nostackframe;
  1087. asm
  1088. {$ifndef win64}
  1089. mov %rdx, %r8
  1090. mov %rsi, %rdx
  1091. mov %rdi, %rcx
  1092. {$endif win64}
  1093. sub %rcx, %rdx { rdx = buf2 - buf1 }
  1094. cmp $4, %r8
  1095. jle .LDwordwise_Prepare
  1096. mov %r8, %rax
  1097. shr $61, %rax
  1098. jnz .LDwordwise_Prepare
  1099. movdqu (%rdx,%rcx), %xmm0 { Compare first vectors. }
  1100. movdqu (%rcx), %xmm1
  1101. pcmpeqd %xmm1, %xmm0
  1102. pmovmskb %xmm0, %eax
  1103. inc %ax
  1104. jnz .LVec0Differs
  1105. shl $2, %r8 { convert to bytes }
  1106. sub $32, %r8 { first 16 bytes already analyzed + last 16 bytes analyzed separately }
  1107. jle .LLastVec
  1108. mov %rcx, %r9 { save original buf1 to recover word position if byte mismatch found (aligned loop works in bytes to support misaligned buf1). }
  1109. add %rcx, %r8
  1110. and $-16, %rcx { align buf1; +16 is performed by the loop. }
  1111. sub %rcx, %r8
  1112. .balign 16
  1113. .LAligned4xLoop_Body:
  1114. add $16, %rcx
  1115. movdqu (%rdx,%rcx), %xmm0
  1116. pcmpeqb (%rcx), %xmm0
  1117. pmovmskb %xmm0, %eax
  1118. inc %ax
  1119. jnz .LAligned4xLoop_VecDiffers
  1120. sub $16, %r8
  1121. ja .LAligned4xLoop_Body
  1122. .LLastVec:
  1123. lea 16(%rcx,%r8), %rcx { point to the last 16 bytes }
  1124. movdqu (%rdx,%rcx), %xmm0
  1125. movdqu (%rcx), %xmm1
  1126. pcmpeqd %xmm1, %xmm0
  1127. pmovmskb %xmm0, %eax
  1128. inc %ax
  1129. jnz .LVec0Differs
  1130. xor %eax, %eax
  1131. ret
  1132. .LVec0Differs:
  1133. bsf %eax, %eax
  1134. add %rcx, %rdx { recover rdx = buf2 }
  1135. mov (%rdx,%rax), %edx
  1136. cmp %edx, (%rcx,%rax)
  1137. sbb %rax, %rax
  1138. or $1, %rax
  1139. ret
  1140. .LAligned4xLoop_VecDiffers:
  1141. bsf %eax, %eax
  1142. add %rax, %rcx
  1143. sub %r9, %rcx
  1144. and $-4, %rcx
  1145. add %r9, %rcx
  1146. mov (%rdx,%rcx), %edx
  1147. cmp %edx, (%rcx)
  1148. .LDoSbb:
  1149. sbb %rax, %rax
  1150. or $1, %rax
  1151. ret
  1152. .balign 16
  1153. .LDwordwise_Body:
  1154. mov (%rdx,%rcx), %eax
  1155. cmp %eax, (%rcx)
  1156. jne .LDoSbb
  1157. add $4, %rcx
  1158. .LDwordwise_Prepare:
  1159. sub $1, %r8
  1160. jae .LDwordwise_Body
  1161. xor %eax, %eax
  1162. end;
  1163. {$endif FPC_SYSTEM_HAS_COMPAREDWORD}
  1164. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1165. { does a thread save inc/dec }
  1166. function declocked(var l : longint) : boolean;assembler; nostackframe;
  1167. asm
  1168. { this check should be done because a lock takes a lot }
  1169. { of time! }
  1170. {$ifdef FPC_PIC}
  1171. movq IsMultithread@GOTPCREL(%rip),%rax
  1172. cmpl $0,(%rax)
  1173. {$else FPC_PIC}
  1174. cmpl $0,IsMultithread(%rip)
  1175. {$endif FPC_PIC}
  1176. jz .Ldeclockedskiplock
  1177. .byte 0xF0 // LOCK prefix.
  1178. .Ldeclockedskiplock:
  1179. decl {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1180. setzb %al
  1181. end;
  1182. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1183. function declocked(var l : int64) : boolean;assembler; nostackframe;
  1184. asm
  1185. { this check should be done because a lock takes a lot }
  1186. { of time! }
  1187. {$ifdef FPC_PIC}
  1188. movq IsMultithread@GOTPCREL(%rip),%rax
  1189. cmpl $0,(%rax)
  1190. {$else FPC_PIC}
  1191. cmpl $0,IsMultithread(%rip)
  1192. {$endif FPC_PIC}
  1193. jz .Ldeclockedskiplock
  1194. .byte 0xF0 // LOCK prefix.
  1195. .Ldeclockedskiplock:
  1196. decq {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1197. setzb %al
  1198. end;
  1199. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1200. procedure inclocked(var l : longint);assembler; nostackframe;
  1201. asm
  1202. { this check should be done because a lock takes a lot }
  1203. { of time! }
  1204. {$ifdef FPC_PIC}
  1205. movq IsMultithread@GOTPCREL(%rip),%rax
  1206. cmpl $0,(%rax)
  1207. {$else FPC_PIC}
  1208. cmpl $0,IsMultithread(%rip)
  1209. {$endif FPC_PIC}
  1210. jz .Linclockedskiplock
  1211. .byte 0xF0 // LOCK prefix.
  1212. .Linclockedskiplock:
  1213. incl {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1214. end;
  1215. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1216. procedure inclocked(var l : int64);assembler; nostackframe;
  1217. asm
  1218. { this check should be done because a lock takes a lot }
  1219. { of time! }
  1220. {$ifdef FPC_PIC}
  1221. movq IsMultithread@GOTPCREL(%rip),%rax
  1222. cmpl $0,(%rax)
  1223. {$else FPC_PIC}
  1224. cmpl $0,IsMultithread(%rip)
  1225. {$endif FPC_PIC}
  1226. jz .Linclockedskiplock
  1227. .byte 0xF0 // LOCK prefix.
  1228. .Linclockedskiplock:
  1229. incq {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1230. end;
  1231. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  1232. asm
  1233. movl $-1,%eax
  1234. lock
  1235. xaddl %eax, {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1236. decl %eax
  1237. end;
  1238. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  1239. asm
  1240. movl $1,%eax
  1241. lock
  1242. xaddl %eax, {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1243. incl %eax
  1244. end;
  1245. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  1246. asm
  1247. {$ifdef win64}
  1248. xchgl (%rcx),%edx
  1249. movl %edx,%eax
  1250. {$else win64}
  1251. xchgl (%rdi),%esi
  1252. movl %esi,%eax
  1253. {$endif win64}
  1254. end;
  1255. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  1256. asm
  1257. {$ifdef win64}
  1258. lock
  1259. xaddl %edx, (%rcx)
  1260. movl %edx,%eax
  1261. {$else win64}
  1262. lock
  1263. xaddl %esi, (%rdi)
  1264. movl %esi,%eax
  1265. {$endif win64}
  1266. end;
  1267. function InterLockedCompareExchange(var Target: longint; NewValue, Comperand : longint): longint; assembler; nostackframe;
  1268. asm
  1269. {$ifdef win64}
  1270. movl %r8d,%eax
  1271. lock
  1272. cmpxchgl %edx,(%rcx)
  1273. {$else win64}
  1274. movl %edx,%eax
  1275. lock
  1276. cmpxchgl %esi,(%rdi)
  1277. {$endif win64}
  1278. end;
  1279. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  1280. asm
  1281. movq $-1,%rax
  1282. lock
  1283. xaddq %rax, {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1284. decq %rax
  1285. end;
  1286. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  1287. asm
  1288. movq $1,%rax
  1289. lock
  1290. xaddq %rax, {$ifdef win64} (%rcx) {$else} (%rdi) {$endif}
  1291. incq %rax
  1292. end;
  1293. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  1294. asm
  1295. {$ifdef win64}
  1296. xchgq (%rcx),%rdx
  1297. movq %rdx,%rax
  1298. {$else win64}
  1299. xchgq (%rdi),%rsi
  1300. movq %rsi,%rax
  1301. {$endif win64}
  1302. end;
  1303. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  1304. asm
  1305. {$ifdef win64}
  1306. lock
  1307. xaddq %rdx, (%rcx)
  1308. movq %rdx,%rax
  1309. {$else win64}
  1310. lock
  1311. xaddq %rsi, (%rdi)
  1312. movq %rsi,%rax
  1313. {$endif win64}
  1314. end;
  1315. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  1316. asm
  1317. {$ifdef win64}
  1318. movq %r8,%rax
  1319. lock
  1320. cmpxchgq %rdx,(%rcx)
  1321. {$else win64}
  1322. movq %rdx,%rax
  1323. lock
  1324. cmpxchgq %rsi,(%rdi)
  1325. {$endif win64}
  1326. end;
  1327. {****************************************************************************
  1328. FPU
  1329. ****************************************************************************}
  1330. const
  1331. { Internal constants for use in system unit }
  1332. FPU_Invalid = 1;
  1333. FPU_Denormal = 2;
  1334. FPU_DivisionByZero = 4;
  1335. FPU_Overflow = 8;
  1336. FPU_Underflow = $10;
  1337. FPU_StackUnderflow = $20;
  1338. FPU_StackOverflow = $40;
  1339. FPU_ExceptionMask = $ff;
  1340. MM_Invalid = 1;
  1341. MM_Denormal = 2;
  1342. MM_DivisionByZero = 4;
  1343. MM_Overflow = 8;
  1344. MM_Underflow = $10;
  1345. MM_Precicion = $20;
  1346. MM_ExceptionMask = $3f;
  1347. MM_MaskInvalidOp = %0000000010000000;
  1348. MM_MaskDenorm = %0000000100000000;
  1349. MM_MaskDivZero = %0000001000000000;
  1350. MM_MaskOverflow = %0000010000000000;
  1351. MM_MaskUnderflow = %0000100000000000;
  1352. MM_MaskPrecision = %0001000000000000;
  1353. {$define FPC_SYSTEM_HAS_FPC_CPUINIT}
  1354. procedure fpc_cpuinit;
  1355. var
  1356. _eax,cpuid7_ebx,cpuid1_ecx : dword;
  1357. begin
  1358. { don't let libraries influence the FPU cw set by the host program }
  1359. if IsLibrary then
  1360. begin
  1361. Default8087CW:=Get8087CW;
  1362. DefaultMXCSR:=GetMXCSR;
  1363. end;
  1364. SysResetFPU;
  1365. asm
  1366. xorl %eax,%eax
  1367. cpuid
  1368. movl %eax,_eax
  1369. movl $1,%eax
  1370. xorl %ecx,%ecx
  1371. cpuid
  1372. movl %ecx,cpuid1_ecx
  1373. end;
  1374. has_sse41_support:=boolean(cpuid1_ecx shr 19 and 1);
  1375. if _eax>=7 then
  1376. begin
  1377. asm
  1378. movl $7,%eax
  1379. xorl %ecx,%ecx
  1380. cpuid
  1381. movl %ebx,cpuid7_ebx
  1382. end;
  1383. {$ifdef use_fast_repmovstos}
  1384. fast_large_repmovstosb:=cpuid7_ebx and (1 shl 9)<>0;
  1385. {$endif}
  1386. { XGETBV support? }
  1387. if (cpuid1_ecx and $8000000)<>0 then
  1388. begin
  1389. asm
  1390. xorl %ecx,%ecx
  1391. .byte 0x0f,0x01,0xd0 { xgetbv }
  1392. movl %eax,_eax
  1393. end;
  1394. if (_eax and 6)=6 then
  1395. begin
  1396. has_avx_support:=(cpuid1_ecx and $10000000)<>0;
  1397. has_avx2_support:=(cpuid7_ebx and $20)<>0;
  1398. end;
  1399. end;
  1400. end;
  1401. fpc_cpuinit_performed:=true;
  1402. end;
  1403. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  1404. Procedure SysInitFPU;
  1405. begin
  1406. end;
  1407. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  1408. Procedure SysResetFPU;
  1409. var
  1410. { these locals are so we don't have to hack pic code in the assembler }
  1411. localmxcsr: dword;
  1412. localfpucw: word;
  1413. begin
  1414. localfpucw:=Default8087CW;
  1415. localmxcsr:=DefaultMXCSR;
  1416. asm
  1417. fninit
  1418. fwait
  1419. fldcw localfpucw
  1420. ldmxcsr localmxcsr
  1421. end;
  1422. end;
  1423. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1424. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  1425. procedure ReadBarrier;assembler;nostackframe;
  1426. asm
  1427. lfence
  1428. end;
  1429. procedure ReadDependencyBarrier;assembler;nostackframe;
  1430. asm
  1431. { reads imply barrier on earlier reads depended on }
  1432. end;
  1433. procedure ReadWriteBarrier;assembler;nostackframe;
  1434. asm
  1435. mfence
  1436. end;
  1437. procedure WriteBarrier;assembler;nostackframe;
  1438. asm
  1439. sfence
  1440. end;
  1441. {$endif}
  1442. {****************************************************************************
  1443. Math Routines
  1444. ****************************************************************************}
  1445. {$define FPC_SYSTEM_HAS_SWAPENDIAN}
  1446. { SwapEndian(<16 Bit>) being inlined is faster than using assembler }
  1447. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1448. begin
  1449. { the extra Word type cast is necessary because the "AValue shr 8" }
  1450. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1451. { the sign bits from the upper 16 bits are shifted in rather than }
  1452. { zeroes. }
  1453. Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff);
  1454. end;
  1455. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1456. begin
  1457. Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
  1458. end;
  1459. function SwapEndian(const AValue: LongInt): LongInt; assembler; nostackframe;
  1460. asm
  1461. {$ifdef win64}
  1462. movl %ecx, %eax
  1463. {$else win64}
  1464. movl %edi, %eax
  1465. {$endif win64}
  1466. bswap %eax
  1467. end;
  1468. function SwapEndian(const AValue: DWord): DWord; assembler; nostackframe;
  1469. asm
  1470. {$ifdef win64}
  1471. movl %ecx, %eax
  1472. {$else win64}
  1473. movl %edi, %eax
  1474. {$endif win64}
  1475. bswap %eax
  1476. end;
  1477. function SwapEndian(const AValue: Int64): Int64; assembler; nostackframe;
  1478. asm
  1479. {$ifdef win64}
  1480. movq %rcx, %rax
  1481. {$else win64}
  1482. movq %rdi, %rax
  1483. {$endif win64}
  1484. bswap %rax
  1485. end;
  1486. function SwapEndian(const AValue: QWord): QWord; assembler; nostackframe;
  1487. asm
  1488. {$ifdef win64}
  1489. movq %rcx, %rax
  1490. {$else win64}
  1491. movq %rdi, %rax
  1492. {$endif win64}
  1493. bswap %rax
  1494. end;
  1495. {$ifndef win64}
  1496. {$define FPC_SYSTEM_HAS_U128_DIV_U64_TO_U64}
  1497. function u128_div_u64_to_u64( const xh, xl: qword; const y: qword; out quotient, remainder: qword ): boolean;nostackframe;assembler;
  1498. {
  1499. SysV:
  1500. xh: RDI
  1501. xl: RSI
  1502. y: RDX
  1503. quotient: RCX
  1504. remainder: R8
  1505. }
  1506. label
  1507. dodiv;
  1508. asm
  1509. cmpq %rdi,%rdx
  1510. ja dodiv
  1511. xorl %eax,%eax
  1512. ret
  1513. dodiv:
  1514. movq %rdx,%r9
  1515. movq %rsi,%rax
  1516. movq %rdi,%rdx
  1517. divq %r9
  1518. movq %rax,(%rcx)
  1519. movq %rdx,(%r8)
  1520. movl $1,%eax
  1521. end;
  1522. {$endif win64}