generic.inc 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Processor independent implementation for the system unit
  5. (adapted for intel i386.inc file)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. Primitives
  14. ****************************************************************************}
  15. type
  16. pstring = ^shortstring;
  17. {$ifndef FPC_SYSTEM_HAS_MOVE}
  18. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  19. var
  20. aligncount : sizeint;
  21. pdest,psrc,pend : pbyte;
  22. begin
  23. if (@dest=@source) or (count<=0) then
  24. exit;
  25. if (@dest<@source) or (@source+count<@dest) then
  26. begin
  27. { Forward Move }
  28. psrc:=@source;
  29. pdest:=@dest;
  30. if (Count>4*sizeof(ptruint)-11)
  31. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  32. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  33. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  34. then
  35. begin
  36. { Align on native pointer size }
  37. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  38. dec(count,aligncount);
  39. pend:=psrc+aligncount;
  40. while psrc<pend do
  41. begin
  42. pdest^:=psrc^;
  43. inc(pdest);
  44. inc(psrc);
  45. end;
  46. { use sizeuint typecast to force shr optimization }
  47. pptruint(pend):=pptruint(psrc)+(sizeuint(count) div sizeof(ptruint));
  48. while psrc<pend do
  49. begin
  50. pptruint(pdest)^:=pptruint(psrc)^;
  51. inc(pptruint(pdest));
  52. inc(pptruint(psrc));
  53. end;
  54. count:=count and (sizeof(PtrUInt)-1);
  55. end;
  56. pend:=psrc+count;
  57. while psrc<pend do
  58. begin
  59. pdest^:=psrc^;
  60. inc(pdest);
  61. inc(psrc);
  62. end;
  63. end
  64. else
  65. begin
  66. { Backward Move }
  67. psrc:=@source+count;
  68. pdest:=@dest+count;
  69. if (Count>4*sizeof(ptruint)-11)
  70. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  71. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  72. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  73. then
  74. begin
  75. { Align on native pointer size }
  76. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  77. dec(count,aligncount);
  78. pend:=psrc-aligncount;
  79. while psrc>pend do
  80. begin
  81. dec(pdest);
  82. dec(psrc);
  83. pdest^:=psrc^;
  84. end;
  85. { use sizeuint typecast to force shr optimization }
  86. pptruint(pend):=pptruint(psrc)-(sizeuint(count) div sizeof(ptruint));
  87. while psrc>pend do
  88. begin
  89. dec(pptruint(pdest));
  90. dec(pptruint(psrc));
  91. pptruint(pdest)^:=pptruint(psrc)^;
  92. end;
  93. count:=count and (sizeof(PtrUInt)-1);
  94. end;
  95. pend:=psrc-count;
  96. while psrc>pend do
  97. begin
  98. dec(pdest);
  99. dec(psrc);
  100. pdest^:=psrc^;
  101. end;
  102. end;
  103. end;
  104. {$endif not FPC_SYSTEM_HAS_MOVE}
  105. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  106. Procedure FillChar(var x;count:SizeInt;value:byte);
  107. var
  108. pdest,pend : pbyte;
  109. v : ptruint;
  110. begin
  111. if count <= 0 then
  112. exit;
  113. pdest:=@x;
  114. if Count>4*sizeof(ptruint)-1 then
  115. begin
  116. v:=(value shl 8) or value;
  117. v:=(v shl 16) or v;
  118. {$ifdef CPU64}
  119. v:=(v shl 32) or v;
  120. {$endif CPU64}
  121. { Align on native pointer size }
  122. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  123. dec(count,pend-pdest);
  124. while pdest<pend do
  125. begin
  126. pdest^:=value;
  127. inc(pdest);
  128. end;
  129. { use sizeuint typecast to force shr optimization }
  130. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  131. while pdest<pend do
  132. begin
  133. pptruint(pdest)^:=v;
  134. inc(pptruint(pdest));
  135. end;
  136. count:=count and (sizeof(ptruint)-1);
  137. end;
  138. pend:=pdest+count;
  139. while pdest<pend do
  140. begin
  141. pdest^:=value;
  142. inc(pdest);
  143. end;
  144. end;
  145. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  146. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  147. procedure fillword(var x;count : SizeInt;value : word);
  148. var
  149. aligncount : sizeint;
  150. pdest,pend : pword;
  151. v : ptruint;
  152. begin
  153. if count <= 0 then
  154. exit;
  155. pdest:=@x;
  156. if Count>4*sizeof(ptruint)-1 then
  157. begin
  158. v:=(value shl 16) or value;
  159. {$ifdef CPU64}
  160. v:=(v shl 32) or v;
  161. {$endif CPU64}
  162. { Align on native pointer size }
  163. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  164. dec(count,aligncount);
  165. pend:=pdest+aligncount;
  166. while pdest<pend do
  167. begin
  168. pdest^:=value;
  169. inc(pdest);
  170. end;
  171. { use sizeuint typecast to force shr optimization }
  172. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  173. while pdest<pend do
  174. begin
  175. pptruint(pdest)^:=v;
  176. inc(pptruint(pdest));
  177. end;
  178. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  179. end;
  180. pend:=pdest+count;
  181. while pdest<pend do
  182. begin
  183. pdest^:=value;
  184. inc(pdest);
  185. end;
  186. end;
  187. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  188. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  189. procedure filldword(var x;count : SizeInt;value : dword);
  190. var
  191. aligncount : sizeint;
  192. pdest,pend : pdword;
  193. v : ptruint;
  194. begin
  195. if count <= 0 then
  196. exit;
  197. pdest:=@x;
  198. if Count>4*sizeof(ptruint)-1 then
  199. begin
  200. v:=value;
  201. {$ifdef CPU64}
  202. v:=(v shl 32) or v;
  203. {$endif CPU64}
  204. { Align on native pointer size }
  205. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  206. dec(count,aligncount);
  207. pend:=pdest+aligncount;
  208. while pdest<pend do
  209. begin
  210. pdest^:=value;
  211. inc(pdest);
  212. end;
  213. { use sizeuint typecast to force shr optimization }
  214. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  215. while pdest<pend do
  216. begin
  217. pptruint(pdest)^:=v;
  218. inc(pptruint(pdest));
  219. end;
  220. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  221. end;
  222. pend:=pdest+count;
  223. while pdest<pend do
  224. begin
  225. pdest^:=value;
  226. inc(pdest);
  227. end;
  228. end;
  229. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  230. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  231. procedure fillqword(var x;count : SizeInt;value : qword);
  232. var
  233. pdest,pend : pqword;
  234. begin
  235. if count <= 0 then
  236. exit;
  237. pdest:=@x;
  238. pend:=pdest+count;
  239. while pdest<pend do
  240. begin
  241. pdest^:=value;
  242. inc(pdest);
  243. end;
  244. end;
  245. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  246. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  247. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  248. var
  249. psrc,pend : pbyte;
  250. begin
  251. psrc:=@buf;
  252. { simulate assembler implementations behaviour, which is expected }
  253. { fpc_pchar_to_ansistr in astrings.inc }
  254. if (len < 0) or
  255. (psrc+len < psrc) then
  256. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  257. else
  258. pend:=psrc+len;
  259. while (psrc<pend) do
  260. begin
  261. if psrc^=b then
  262. begin
  263. result:=psrc-pbyte(@buf);
  264. exit;
  265. end;
  266. inc(psrc);
  267. end;
  268. result:=-1;
  269. end;
  270. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  271. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  272. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  273. var
  274. psrc,pend : pword;
  275. begin
  276. psrc:=@buf;
  277. { simulate assembler implementations behaviour, which is expected }
  278. { fpc_pchar_to_ansistr in astrings.inc }
  279. if (len < 0) or
  280. { is this ever true? }
  281. (len > high(PtrInt)) or
  282. (psrc+len < psrc) then
  283. pend:=pword(high(PtrUInt)-sizeof(word))
  284. else
  285. pend:=psrc+len;
  286. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  287. if (ptruint(psrc) mod 2)<>0 then
  288. while psrc<pend do
  289. begin
  290. if unaligned(psrc^)=b then
  291. begin
  292. result:=psrc-pword(@buf);
  293. exit;
  294. end;
  295. inc(psrc);
  296. end
  297. else
  298. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  299. while psrc<pend do
  300. begin
  301. if psrc^=b then
  302. begin
  303. result:=psrc-pword(@buf);
  304. exit;
  305. end;
  306. inc(psrc);
  307. end;
  308. result:=-1;
  309. end;
  310. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  311. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  312. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  313. var
  314. psrc,pend : pdword;
  315. begin
  316. psrc:=@buf;
  317. { simulate assembler implementations behaviour, which is expected }
  318. { fpc_pchar_to_ansistr in astrings.inc }
  319. if (len < 0) or
  320. (len > high(PtrInt) div 2) or
  321. (psrc+len < psrc) then
  322. pend:=pdword(high(PtrUInt)-sizeof(dword))
  323. else
  324. pend:=psrc+len;
  325. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  326. if (ptruint(psrc) mod 4)<>0 then
  327. while psrc<pend do
  328. begin
  329. if unaligned(psrc^)=b then
  330. begin
  331. result:=psrc-pdword(@buf);
  332. exit;
  333. end;
  334. inc(psrc);
  335. end
  336. else
  337. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  338. while psrc<pend do
  339. begin
  340. if psrc^=b then
  341. begin
  342. result:=psrc-pdword(@buf);
  343. exit;
  344. end;
  345. inc(psrc);
  346. end;
  347. result:=-1;
  348. end;
  349. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  350. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  351. function IndexQWord(Const buf;len:SizeInt;b:QWord):SizeInt;
  352. var
  353. psrc,pend : pqword;
  354. begin
  355. psrc:=@buf;
  356. { simulate assembler implementations behaviour, which is expected }
  357. { fpc_pchar_to_ansistr in astrings.inc }
  358. if (len < 0) or
  359. (len > high(PtrInt) div 4) or
  360. (psrc+len < psrc) then
  361. pend:=pqword(high(PtrUInt)-sizeof(qword))
  362. else
  363. pend:=psrc+len;
  364. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  365. if (ptruint(psrc) mod 8)<>0 then
  366. while psrc<pend do
  367. begin
  368. if unaligned(psrc^)=b then
  369. begin
  370. result:=psrc-pqword(@buf);
  371. exit;
  372. end;
  373. inc(psrc);
  374. end
  375. else
  376. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  377. while psrc<pend do
  378. begin
  379. if psrc^=b then
  380. begin
  381. result:=psrc-pqword(@buf);
  382. exit;
  383. end;
  384. inc(psrc);
  385. end;
  386. result:=-1;
  387. end;
  388. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  389. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  390. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  391. var
  392. aligncount : sizeint;
  393. psrc,pdest,pend : pbyte;
  394. b : ptrint;
  395. begin
  396. b:=0;
  397. psrc:=@buf1;
  398. pdest:=@buf2;
  399. if (len>4*sizeof(ptruint)-1)
  400. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  401. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  402. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  403. then
  404. begin
  405. { Align on native pointer size }
  406. aligncount:=(sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1);
  407. dec(len,aligncount);
  408. pend:=psrc+aligncount;
  409. while psrc<pend do
  410. begin
  411. b:=(ptrint(psrc^)-ptrint(pdest^));
  412. if b<>0 then
  413. begin
  414. if b<0 then
  415. exit(-1)
  416. else
  417. exit(1);
  418. end;
  419. inc(pdest);
  420. inc(psrc);
  421. end;
  422. { use sizeuint typecast to force shr optimization }
  423. pptruint(pend):=pptruint(psrc)+(sizeuint(len) div sizeof(ptruint));
  424. len:=len and (sizeof(PtrUInt)-1);
  425. while psrc<pend do
  426. begin
  427. b:=(pptrint(psrc)^-pptrint(pdest)^);
  428. if b<>0 then
  429. begin
  430. len:=sizeof(ptruint);
  431. break;
  432. end;
  433. inc(pptruint(pdest));
  434. inc(pptruint(psrc));
  435. end;
  436. end;
  437. if (psrc+len >= psrc) then
  438. pend:=psrc+len
  439. else
  440. pend:=pbyte(high(ptruint)-1);
  441. while psrc<pend do
  442. begin
  443. b:=(ptrint(psrc^)-ptrint(pdest^));
  444. if b<>0 then
  445. begin
  446. if b<0 then
  447. exit(-1)
  448. else
  449. exit(1);
  450. end;
  451. inc(pdest);
  452. inc(psrc);
  453. end;
  454. result:=0;
  455. end;
  456. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  457. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  458. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  459. var
  460. aligncount : sizeint;
  461. psrc,pdest,pend : pword;
  462. b : ptrint;
  463. begin
  464. b:=0;
  465. psrc:=@buf1;
  466. pdest:=@buf2;
  467. if (len>4*sizeof(ptruint)-1)
  468. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  469. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  470. and (((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))=0)
  471. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  472. then
  473. begin
  474. { Align on native pointer size }
  475. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 1;
  476. dec(len,aligncount);
  477. pend:=psrc+aligncount;
  478. while psrc<pend do
  479. begin
  480. b:=(ptrint(psrc^)-ptrint(pdest^));
  481. if b<>0 then
  482. begin
  483. if b<0 then
  484. exit(-1)
  485. else
  486. exit(1);
  487. end;
  488. inc(pdest);
  489. inc(psrc);
  490. end;
  491. { use sizeuint typecast to force shr optimization }
  492. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*2 div sizeof(ptruint));
  493. len:=((len*2) and (sizeof(PtrUInt)-1)) shr 1;
  494. while psrc<pend do
  495. begin
  496. b:=(pptrint(psrc)^-pptrint(pdest)^);
  497. if b<>0 then
  498. begin
  499. len:=sizeof(ptruint) shr 1;
  500. break;
  501. end;
  502. inc(pptruint(pdest));
  503. inc(pptruint(psrc));
  504. end;
  505. end;
  506. if (len <= high(ptrint)) and
  507. (psrc+len >= psrc) then
  508. pend:=psrc+len
  509. else
  510. pend:=pword(high(ptruint)-2);
  511. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  512. if ((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))<>0 then
  513. while psrc<pend do
  514. begin
  515. b:=(ptrint(unaligned(psrc^))-ptrint(unaligned(pdest^)));
  516. if b<>0 then
  517. begin
  518. if b<0 then
  519. exit(-1)
  520. else
  521. exit(1);
  522. end;
  523. inc(pdest);
  524. inc(psrc);
  525. end
  526. else
  527. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  528. while psrc<pend do
  529. begin
  530. b:=(ptrint(psrc^)-ptrint(pdest^));
  531. if b<>0 then
  532. begin
  533. if b<0 then
  534. exit(-1)
  535. else
  536. exit(1);
  537. end;
  538. inc(pdest);
  539. inc(psrc);
  540. end;
  541. result:=0;
  542. end;
  543. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  544. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  545. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  546. var
  547. aligncount : sizeint;
  548. psrc,pdest,pend : pdword;
  549. begin
  550. psrc:=@buf1;
  551. pdest:=@buf2;
  552. if (len>4*sizeof(ptruint)-11)
  553. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  554. and (((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))=0)
  555. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  556. then
  557. begin
  558. { Align on native pointer size }
  559. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  560. dec(len,aligncount);
  561. pend:=psrc+aligncount;
  562. while psrc<pend do
  563. begin
  564. if psrc^<>pdest^ then
  565. if psrc^>pdest^ then
  566. exit(1)
  567. else
  568. exit(-1);
  569. inc(pdest);
  570. inc(psrc);
  571. end;
  572. { use sizeuint typecast to force shr optimization }
  573. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  574. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  575. while psrc<pend do
  576. begin
  577. if pptrint(psrc)^<>pptrint(pdest)^ then
  578. begin
  579. len:=sizeof(ptruint) shr 2;
  580. break;
  581. end;
  582. inc(pptruint(pdest));
  583. inc(pptruint(psrc));
  584. end;
  585. end;
  586. if (len <= high(ptrint) div 2) and
  587. (psrc+len >= psrc) then
  588. pend:=psrc+len
  589. else
  590. pend:=pdword(high(ptruint)-4);
  591. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  592. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  593. while psrc<pend do
  594. begin
  595. if unaligned(psrc^)<>unaligned(pdest^) then
  596. if unaligned(psrc^)>unaligned(pdest^) then
  597. exit(1)
  598. else
  599. exit(-1);
  600. inc(pdest);
  601. inc(psrc);
  602. end
  603. else
  604. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  605. while psrc<pend do
  606. begin
  607. if psrc^<>pdest^ then
  608. if psrc^>pdest^ then
  609. exit(1)
  610. else
  611. exit(-1);
  612. inc(pdest);
  613. inc(psrc);
  614. end;
  615. result:=0;
  616. end;
  617. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  618. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  619. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  620. var
  621. I : SizeInt;
  622. begin
  623. if Len = 0 then
  624. exit;
  625. I:=IndexByte(Buf1,Len,0);
  626. if I<>-1 then
  627. Move(Buf1,Buf2,I)
  628. else
  629. Move(Buf1,Buf2,len);
  630. end;
  631. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  632. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  633. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  634. var
  635. psrc,pend : pbyte;
  636. begin
  637. psrc:=@buf;
  638. { simulate assembler implementations behaviour, which is expected }
  639. { fpc_pchar_to_ansistr in astrings.inc }
  640. if (len < 0) then
  641. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  642. else
  643. pend:=psrc+len;
  644. while (psrc<pend) and (psrc^<>0) do
  645. begin
  646. if (psrc^=byte(b)) then
  647. begin
  648. result:=psrc-pbyte(@buf);
  649. exit;
  650. end;
  651. inc(psrc);
  652. end;
  653. result:=-1;
  654. end;
  655. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  656. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  657. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  658. var
  659. psrc,pdest,pend : pbyte;
  660. b : ptrint;
  661. begin
  662. b:=0;
  663. psrc:=@buf1;
  664. pdest:=@buf2;
  665. pend:=psrc+len;
  666. while psrc<pend do
  667. begin
  668. b:=(ptrint(psrc^)-ptrint(pdest^));
  669. if b<0 then
  670. exit(-1)
  671. else if b>0 then
  672. exit(1);
  673. if (psrc^=0) or (pdest^=0) then
  674. exit(0);
  675. inc(pdest);
  676. inc(psrc);
  677. end;
  678. result:=0;
  679. end;
  680. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  681. {****************************************************************************
  682. Object Helpers
  683. ****************************************************************************}
  684. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  685. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  686. { Note: _vmt will be reset to -1 when memory is allocated,
  687. this is needed for fpc_help_fail }
  688. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  689. type
  690. ppointer = ^pointer;
  691. pvmt = ^tvmt;
  692. tvmt=packed record
  693. size,msize:ptruint;
  694. parent:pointer;
  695. end;
  696. var
  697. vmtcopy : pointer;
  698. begin
  699. { Inherited call? }
  700. if _vmt=nil then
  701. begin
  702. fpc_help_constructor:=_self;
  703. exit;
  704. end;
  705. vmtcopy:=_vmt;
  706. if (_self=nil) and
  707. (pvmt(_vmt)^.size>0) then
  708. begin
  709. getmem(_self,pvmt(_vmt)^.size);
  710. { reset vmt needed for fail }
  711. _vmt:=pointer(-1);
  712. end;
  713. if _self<>nil then
  714. begin
  715. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  716. ppointer(_self+_vmt_pos)^:=vmtcopy;
  717. end;
  718. fpc_help_constructor:=_self;
  719. end;
  720. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  721. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  722. { Note: _self will not be reset, the compiler has to generate the reset }
  723. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  724. type
  725. ppointer = ^pointer;
  726. pvmt = ^tvmt;
  727. tvmt = packed record
  728. size,msize : ptruint;
  729. parent : pointer;
  730. end;
  731. begin
  732. { already released? }
  733. if (_self=nil) or
  734. (_vmt=nil) or
  735. (ppointer(_self+vmt_pos)^=nil) then
  736. exit;
  737. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  738. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  739. RunError(210);
  740. { reset vmt to nil for protection }
  741. ppointer(_self+vmt_pos)^:=nil;
  742. freemem(_self);
  743. end;
  744. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  745. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  746. { Note: _self will not be reset, the compiler has to generate the reset }
  747. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  748. begin
  749. if (_self=nil) or (_vmt=nil) then
  750. exit;
  751. { vmt=$ffffffff when memory was allocated }
  752. if ptruint(_vmt)=high(ptruint) then
  753. begin
  754. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  755. HandleError(210)
  756. else
  757. begin
  758. ppointer(_self+vmt_pos)^:=nil;
  759. freemem(_self);
  760. { reset _vmt to nil so it will not be freed a
  761. second time }
  762. _vmt:=nil;
  763. end;
  764. end
  765. else
  766. ppointer(_self+vmt_pos)^:=nil;
  767. end;
  768. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  769. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  770. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  771. type
  772. pvmt = ^tvmt;
  773. tvmt = packed record
  774. size,msize : ptruint;
  775. parent : pointer;
  776. end;
  777. begin
  778. if (_vmt=nil) or
  779. (pvmt(_vmt)^.size=0) or
  780. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  781. RunError(210);
  782. end;
  783. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  784. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  785. { checks for a correct vmt pointer }
  786. { deeper check to see if the current object is }
  787. { really related to the true }
  788. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  789. type
  790. pvmt = ^tvmt;
  791. tvmt = packed record
  792. size,msize : ptruint;
  793. parent : pointer;
  794. end;
  795. begin
  796. if (vmt=nil) or
  797. (pvmt(vmt)^.size=0) or
  798. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  799. RunError(210);
  800. while assigned(vmt) do
  801. if vmt=expvmt then
  802. exit
  803. else
  804. vmt:=pvmt(vmt)^.parent;
  805. RunError(219);
  806. end;
  807. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  808. {$endif FPC_HAS_FEATURE_OBJECTS}
  809. {****************************************************************************
  810. String
  811. ****************************************************************************}
  812. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  813. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  814. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  815. var
  816. slen : byte;
  817. begin
  818. slen:=length(sstr);
  819. if slen<len then
  820. len:=slen;
  821. move(sstr[0],result[0],len+1);
  822. if slen>len then
  823. result[0]:=chr(len);
  824. end;
  825. {$else FPC_STRTOSHORTSTRINGPROC}
  826. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  827. var
  828. slen : byte;
  829. begin
  830. slen:=length(sstr);
  831. if slen>high(res) then
  832. slen:=high(res);
  833. move(sstr[0],res[0],slen+1);
  834. res[0]:=chr(slen);
  835. end;
  836. {$endif FPC_STRTOSHORTSTRINGPROC}
  837. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  838. var
  839. slen : byte;
  840. type
  841. pstring = ^string;
  842. begin
  843. slen:=length(pstring(sstr)^);
  844. if slen<len then
  845. len:=slen;
  846. move(sstr^,dstr^,len+1);
  847. if slen>len then
  848. pchar(dstr)^:=chr(len);
  849. end;
  850. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  851. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  852. {$ifndef STR_CONCAT_PROCS}
  853. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; compilerproc;
  854. var
  855. s1l, s2l : byte;
  856. begin
  857. s1l:=length(s1);
  858. s2l:=length(s2);
  859. if s1l+s2l>255 then
  860. s2l:=255-s1l;
  861. move(s1[1],fpc_shortstr_concat[1],s1l);
  862. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  863. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  864. end;
  865. {$else STR_CONCAT_PROCS}
  866. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  867. var
  868. s1l, s2l : longint;
  869. begin
  870. s1l:=length(s1);
  871. s2l:=length(s2);
  872. if s1l+s2l>high(dests) then
  873. s2l:=high(dests)-s1l;
  874. if @dests=@s1 then
  875. move(s2[1],dests[s1l+1],s2l)
  876. else
  877. if @dests=@s2 then
  878. begin
  879. move(dests[1],dests[s1l+1],s2l);
  880. move(s1[1],dests[1],s1l);
  881. end
  882. else
  883. begin
  884. move(s1[1],dests[1],s1l);
  885. move(s2[1],dests[s1l+1],s2l);
  886. end;
  887. dests[0]:=chr(s1l+s2l);
  888. end;
  889. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  890. var
  891. s2l : byte;
  892. LowStart,i,
  893. Len : longint;
  894. pc : pchar;
  895. needtemp : boolean;
  896. tmpstr : shortstring;
  897. p,pdest : pshortstring;
  898. begin
  899. if high(sarr)=0 then
  900. begin
  901. DestS:='';
  902. exit;
  903. end;
  904. lowstart:=low(sarr);
  905. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  906. inc(lowstart);
  907. { Check for another reuse, then we can't use
  908. the append optimization and need to use a temp }
  909. needtemp:=false;
  910. for i:=lowstart to high(sarr) do
  911. begin
  912. if Pointer(@DestS)=Pointer(sarr[i]) then
  913. begin
  914. needtemp:=true;
  915. break;
  916. end;
  917. end;
  918. if needtemp then
  919. begin
  920. lowstart:=low(sarr);
  921. tmpstr:='';
  922. pdest:=@tmpstr
  923. end
  924. else
  925. begin
  926. { Start with empty DestS if we start with concatting
  927. the first array element }
  928. if lowstart=low(sarr) then
  929. DestS:='';
  930. pdest:=@DestS;
  931. end;
  932. { Concat all strings, except the string we already
  933. copied in DestS }
  934. Len:=length(pdest^);
  935. pc:=@pdest^[1+Length(pdest^)];
  936. for i:=lowstart to high(sarr) do
  937. begin
  938. p:=sarr[i];
  939. if assigned(p) then
  940. begin
  941. s2l:=length(p^);
  942. if Len+s2l>high(dests) then
  943. s2l:=high(dests)-Len;
  944. Move(p^[1],pc^,s2l);
  945. inc(pc,s2l);
  946. inc(Len,s2l);
  947. end;
  948. end;
  949. pdest^[0]:=Chr(Len);
  950. if needtemp then
  951. DestS:=TmpStr;
  952. end;
  953. {$endif STR_CONCAT_PROCS}
  954. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  955. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  956. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  957. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  958. var
  959. s1l, s2l : integer;
  960. begin
  961. s1l:=length(s1);
  962. s2l:=length(s2);
  963. if s1l+s2l>high(s1) then
  964. s2l:=high(s1)-s1l;
  965. move(s2[1],s1[s1l+1],s2l);
  966. s1[0]:=chr(s1l+s2l);
  967. end;
  968. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  969. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  970. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  971. var
  972. s1,s2,max,i : byte;
  973. d : longint;
  974. begin
  975. s1:=length(left);
  976. s2:=length(right);
  977. if s1<s2 then
  978. max:=s1
  979. else
  980. max:=s2;
  981. for i:=1 to max do
  982. begin
  983. d:=byte(left[i])-byte(right[i]);
  984. if d>0 then
  985. exit(1)
  986. else if d<0 then
  987. exit(-1);
  988. end;
  989. if s1>s2 then
  990. exit(1)
  991. else if s1<s2 then
  992. exit(-1)
  993. else
  994. exit(0);
  995. end;
  996. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  997. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  998. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  999. begin
  1000. Result := longint(left[0]) - longint(right[0]);
  1001. if Result = 0 then
  1002. Result := CompareByte(left[1],right[1], longint(left[0]));
  1003. end;
  1004. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  1005. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1006. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1007. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  1008. var
  1009. l : longint;
  1010. s: shortstring;
  1011. begin
  1012. if p=nil then
  1013. l:=0
  1014. else
  1015. l:=strlen(p);
  1016. if l>255 then
  1017. l:=255;
  1018. if l>0 then
  1019. move(p^,s[1],l);
  1020. s[0]:=chr(l);
  1021. fpc_pchar_to_shortstr := s;
  1022. end;
  1023. {$else FPC_STRTOSHORTSTRINGPROC}
  1024. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  1025. var
  1026. l : longint;
  1027. s: shortstring;
  1028. begin
  1029. if p=nil then
  1030. l:=0
  1031. else
  1032. l:=strlen(p);
  1033. if l>high(res) then
  1034. l:=high(res);
  1035. if l>0 then
  1036. move(p^,s[1],l);
  1037. s[0]:=chr(l);
  1038. res:=s;
  1039. end;
  1040. {$endif FPC_STRTOSHORTSTRINGPROC}
  1041. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1042. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1043. { also define alias which can be used inside the system unit }
  1044. function fpc_pchar_to_shortstr(p:pchar):shortstring;[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1045. {$else FPC_STRTOSHORTSTRINGPROC}
  1046. { also define alias which can be used inside the system unit }
  1047. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1048. {$endif FPC_STRTOSHORTSTRINGPROC}
  1049. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1050. begin
  1051. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1052. result:=fpc_pchar_to_shortstr(p);
  1053. {$else FPC_STRTOSHORTSTRINGPROC}
  1054. fpc_pchar_to_shortstr(result,p);
  1055. {$endif FPC_STRTOSHORTSTRINGPROC}
  1056. end;
  1057. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1058. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1059. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1060. var
  1061. l: longint;
  1062. index: longint;
  1063. len: byte;
  1064. begin
  1065. l := high(arr)+1;
  1066. if l>=256 then
  1067. l:=255
  1068. else if l<0 then
  1069. l:=0;
  1070. if (zerobased) then
  1071. begin
  1072. index:=IndexByte(arr[0],l,0);
  1073. if (index < 0) then
  1074. len := l
  1075. else
  1076. len := index;
  1077. end
  1078. else
  1079. len := l;
  1080. move(arr[0],fpc_chararray_to_shortstr[1],len);
  1081. fpc_chararray_to_shortstr[0]:=chr(len);
  1082. end;
  1083. {$else FPC_STRTOSHORTSTRINGPROC}
  1084. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1085. var
  1086. l: longint;
  1087. index: longint;
  1088. len: byte;
  1089. begin
  1090. l:=high(arr)+1;
  1091. if l>=high(res)+1 then
  1092. l:=high(res)
  1093. else if l<0 then
  1094. l:=0;
  1095. if zerobased then
  1096. begin
  1097. index:=IndexByte(arr[0],l,0);
  1098. if index<0 then
  1099. len:=l
  1100. else
  1101. len:=index;
  1102. end
  1103. else
  1104. len:=l;
  1105. move(arr[0],res[1],len);
  1106. res[0]:=chr(len);
  1107. end;
  1108. {$endif FPC_STRTOSHORTSTRINGPROC}
  1109. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1110. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1111. {$ifndef FPC_STRTOCHARARRAYPROC}
  1112. { inside the compiler, the resulttype is modified to that of the actual }
  1113. { chararray we're converting to (JM) }
  1114. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  1115. var
  1116. len: longint;
  1117. begin
  1118. len := length(src);
  1119. if len > arraysize then
  1120. len := arraysize;
  1121. {$r-}
  1122. { make sure we don't access char 1 if length is 0 (JM) }
  1123. if len > 0 then
  1124. move(src[1],fpc_shortstr_to_chararray[0],len);
  1125. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  1126. {$ifdef RangeCheckWasOn}
  1127. {$r+}
  1128. {$endif}
  1129. end;
  1130. {$else ndef FPC_STRTOCHARARRAYPROC}
  1131. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1132. var
  1133. len: longint;
  1134. begin
  1135. len := length(src);
  1136. if len > length(res) then
  1137. len := length(res);
  1138. {$r-}
  1139. { make sure we don't access char 1 if length is 0 (JM) }
  1140. if len > 0 then
  1141. move(src[1],res[0],len);
  1142. fillchar(res[len],length(res)-len,0);
  1143. {$ifdef RangeCheckWasOn}
  1144. {$r+}
  1145. {$endif}
  1146. end;
  1147. {$endif ndef FPC_STRTOCHARARRAYPROC}
  1148. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1149. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1150. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1151. var i : longint;
  1152. begin
  1153. i:=0;
  1154. if assigned(p) then
  1155. while p[i]<>#0 do
  1156. inc(i);
  1157. exit(i);
  1158. end;
  1159. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1160. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1161. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1162. var i : longint;
  1163. begin
  1164. i:=0;
  1165. while p[i]<>#0 do inc(i);
  1166. exit(i);
  1167. end;
  1168. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1169. {****************************************************************************
  1170. Caller/StackFrame Helpers
  1171. ****************************************************************************}
  1172. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1173. {_$error Get_frame must be defined for each processor }
  1174. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1175. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1176. {_$error Get_caller_addr must be defined for each processor }
  1177. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1178. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1179. {_$error Get_caller_frame must be defined for each processor }
  1180. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1181. {****************************************************************************
  1182. Math
  1183. ****************************************************************************}
  1184. {****************************************************************************
  1185. Software longint/dword division
  1186. ****************************************************************************}
  1187. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1188. function count_leading_zeros_32bit(l : longint) : longint;
  1189. var
  1190. i : longint;
  1191. begin
  1192. for i:=0 to 31 do
  1193. begin
  1194. if (l and (longint($80000000) shr i))<>0 then
  1195. begin
  1196. result:=i;
  1197. exit;
  1198. end;
  1199. end;
  1200. result:=i;
  1201. end;
  1202. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1203. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1204. var
  1205. shift,lzz,lzn : longint;
  1206. begin
  1207. result:=0;
  1208. if n=0 then
  1209. HandleErrorFrame(200,get_frame);
  1210. lzz:=count_leading_zeros_32bit(z);
  1211. lzn:=count_leading_zeros_32bit(n);
  1212. { if the denominator contains less zeros
  1213. then the numerator
  1214. the d is greater than the n }
  1215. if lzn<lzz then
  1216. exit;
  1217. shift:=lzn-lzz;
  1218. n:=n shl shift;
  1219. repeat
  1220. if z>=n then
  1221. begin
  1222. z:=z-n;
  1223. result:=result+dword(1 shl shift);
  1224. end;
  1225. dec(shift);
  1226. n:=n shr 1;
  1227. until shift<0;
  1228. end;
  1229. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1230. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1231. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1232. var
  1233. shift,lzz,lzn : longint;
  1234. begin
  1235. result:=0;
  1236. if n=0 then
  1237. HandleErrorFrame(200,get_frame);
  1238. lzz:=count_leading_zeros_32bit(z);
  1239. lzn:=count_leading_zeros_32bit(n);
  1240. { if the denominator contains less zeros
  1241. then the numerator
  1242. the d is greater than the n }
  1243. if lzn<lzz then
  1244. begin
  1245. result:=z;
  1246. exit;
  1247. end;
  1248. shift:=lzn-lzz;
  1249. n:=n shl shift;
  1250. repeat
  1251. if z>=n then
  1252. z:=z-n;
  1253. dec(shift);
  1254. n:=n shr 1;
  1255. until shift<0;
  1256. result:=z;
  1257. end;
  1258. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1259. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1260. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1261. var
  1262. sign : boolean;
  1263. d1,d2 : dword;
  1264. begin
  1265. if n=0 then
  1266. HandleErrorFrame(200,get_frame);
  1267. sign:=false;
  1268. if z<0 then
  1269. begin
  1270. sign:=not(sign);
  1271. d1:=dword(-z);
  1272. end
  1273. else
  1274. d1:=z;
  1275. if n<0 then
  1276. begin
  1277. sign:=not(sign);
  1278. d2:=dword(-n);
  1279. end
  1280. else
  1281. d2:=n;
  1282. { the div is coded by the compiler as call to divdword }
  1283. if sign then
  1284. result:=-(d1 div d2)
  1285. else
  1286. result:=d1 div d2;
  1287. end;
  1288. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1289. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1290. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1291. var
  1292. signed : boolean;
  1293. r,nq,zq : dword;
  1294. begin
  1295. if n=0 then
  1296. HandleErrorFrame(200,get_frame);
  1297. nq:=abs(n);
  1298. if z<0 then
  1299. begin
  1300. zq:=dword(-z);
  1301. signed:=true;
  1302. end
  1303. else
  1304. begin
  1305. zq:=z;
  1306. signed:=false;
  1307. end;
  1308. r:=zq mod nq;
  1309. if signed then
  1310. result:=-longint(r)
  1311. else
  1312. result:=r;
  1313. end;
  1314. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1315. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1316. {****************************************************************************}
  1317. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1318. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1319. begin
  1320. if l<0 then
  1321. abs:=-l
  1322. else
  1323. abs:=l;
  1324. end;
  1325. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1326. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1327. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1328. begin
  1329. odd:=boolean(l and 1);
  1330. end;
  1331. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1332. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1333. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1334. begin
  1335. odd:=boolean(l and 1);
  1336. end;
  1337. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1338. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1339. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1340. begin
  1341. odd:=boolean(longint(l) and 1);
  1342. end;
  1343. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1344. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1345. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1346. begin
  1347. odd:=boolean(longint(l) and 1);
  1348. end;
  1349. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1350. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1351. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1352. begin
  1353. sqr:=l*l;
  1354. end;
  1355. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1356. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1357. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1358. begin
  1359. if l < 0 then
  1360. abs := -l
  1361. else
  1362. abs := l;
  1363. end;
  1364. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1365. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1366. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1367. begin
  1368. sqr := l*l;
  1369. end;
  1370. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1371. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1372. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1373. begin
  1374. sqr := l*l;
  1375. end;
  1376. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1377. {$ifdef CPU16}
  1378. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1379. function declocked(var l:smallint):boolean;
  1380. begin
  1381. Dec(l);
  1382. declocked:=(l=0);
  1383. end;
  1384. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1385. {$endif CPU16}
  1386. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1387. function declocked(var l:longint):boolean;
  1388. begin
  1389. Dec(l);
  1390. declocked:=(l=0);
  1391. end;
  1392. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1393. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1394. function declocked(var l:int64):boolean;
  1395. begin
  1396. Dec(l);
  1397. declocked:=(l=0);
  1398. end;
  1399. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1400. {$ifdef CPU16}
  1401. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1402. procedure inclocked(var l:smallint);
  1403. begin
  1404. Inc(l);
  1405. end;
  1406. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1407. {$endif CPU16}
  1408. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1409. procedure inclocked(var l:longint);
  1410. begin
  1411. Inc(l);
  1412. end;
  1413. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1414. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1415. procedure inclocked(var l:int64);
  1416. begin
  1417. Inc(l);
  1418. end;
  1419. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1420. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1421. {_$error Sptr must be defined for each processor }
  1422. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1423. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1424. var
  1425. tmp: PtrUInt;
  1426. begin
  1427. tmp:=addr+alignment-1;
  1428. result:=tmp-(tmp mod alignment)
  1429. end;
  1430. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1431. var
  1432. tmp: PtrUInt;
  1433. begin
  1434. tmp:=PtrUInt(addr)+alignment-1;
  1435. result:=pointer(tmp-(tmp mod alignment));
  1436. end;
  1437. {****************************************************************************
  1438. Str()
  1439. ****************************************************************************}
  1440. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1441. procedure int_str(l:longint;out s:string);
  1442. var
  1443. m,m1 : longword;
  1444. pcstart,
  1445. pc2start,
  1446. pc,pc2 : pchar;
  1447. hs : string[32];
  1448. overflow : longint;
  1449. begin
  1450. pc2start:=@s[1];
  1451. pc2:=pc2start;
  1452. if (l<0) then
  1453. begin
  1454. pc2^:='-';
  1455. inc(pc2);
  1456. m:=longword(-l);
  1457. end
  1458. else
  1459. m:=longword(l);
  1460. pcstart:=pchar(@hs[0]);
  1461. pc:=pcstart;
  1462. repeat
  1463. m1:=m div 10;
  1464. inc(pc);
  1465. pc^:=char(m-(m1*10)+byte('0'));
  1466. m:=m1;
  1467. until m=0;
  1468. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1469. if overflow>0 then
  1470. inc(pcstart,overflow);
  1471. while (pc>pcstart) do
  1472. begin
  1473. pc2^:=pc^;
  1474. inc(pc2);
  1475. dec(pc);
  1476. end;
  1477. s[0]:=char(pc2-pc2start);
  1478. end;
  1479. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1480. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1481. procedure int_str(l:longword;out s:string);
  1482. var
  1483. m1 : longword;
  1484. pcstart,
  1485. pc2start,
  1486. pc,pc2 : pchar;
  1487. hs : string[32];
  1488. overflow : longint;
  1489. begin
  1490. pc2start:=@s[1];
  1491. pc2:=pc2start;
  1492. pcstart:=pchar(@hs[0]);
  1493. pc:=pcstart;
  1494. repeat
  1495. inc(pc);
  1496. m1:=l div 10;
  1497. pc^:=char(l-(m1*10)+byte('0'));
  1498. l:=m1;
  1499. until l=0;
  1500. overflow:=(pc-pcstart)-high(s);
  1501. if overflow>0 then
  1502. inc(pcstart,overflow);
  1503. while (pc>pcstart) do
  1504. begin
  1505. pc2^:=pc^;
  1506. inc(pc2);
  1507. dec(pc);
  1508. end;
  1509. s[0]:=char(pc2-pc2start);
  1510. end;
  1511. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1512. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1513. procedure int_str(l:int64;out s:string);
  1514. var
  1515. m,m1 : qword;
  1516. pcstart,
  1517. pc2start,
  1518. pc,pc2 : pchar;
  1519. hs : string[32];
  1520. overflow : longint;
  1521. begin
  1522. pc2start:=@s[1];
  1523. pc2:=pc2start;
  1524. if (l<0) then
  1525. begin
  1526. pc2^:='-';
  1527. inc(pc2);
  1528. m:=qword(-l);
  1529. end
  1530. else
  1531. m:=qword(l);
  1532. pcstart:=pchar(@hs[0]);
  1533. pc:=pcstart;
  1534. repeat
  1535. m1:=m div 10;
  1536. inc(pc);
  1537. pc^:=char(m-(m1*10)+byte('0'));
  1538. m:=m1;
  1539. until m=0;
  1540. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1541. if overflow>0 then
  1542. inc(pcstart,overflow);
  1543. while (pc>pcstart) do
  1544. begin
  1545. pc2^:=pc^;
  1546. inc(pc2);
  1547. dec(pc);
  1548. end;
  1549. s[0]:=char(pc2-pc2start);
  1550. end;
  1551. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1552. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1553. procedure int_str(l:qword;out s:string);
  1554. var
  1555. m1 : qword;
  1556. pcstart,
  1557. pc2start,
  1558. pc,pc2 : pchar;
  1559. hs : string[64];
  1560. overflow : longint;
  1561. begin
  1562. pc2start:=@s[1];
  1563. pc2:=pc2start;
  1564. pcstart:=pchar(@hs[0]);
  1565. pc:=pcstart;
  1566. repeat
  1567. inc(pc);
  1568. m1:=l div 10;
  1569. pc^:=char(l-(m1*10)+byte('0'));
  1570. l:=m1;
  1571. until l=0;
  1572. overflow:=(pc-pcstart)-high(s);
  1573. if overflow>0 then
  1574. inc(pcstart,overflow);
  1575. while (pc>pcstart) do
  1576. begin
  1577. pc2^:=pc^;
  1578. inc(pc2);
  1579. dec(pc);
  1580. end;
  1581. s[0]:=char(pc2-pc2start);
  1582. end;
  1583. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1584. {$ifndef FPUNONE}
  1585. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1586. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1587. begin
  1588. softfloat_exception_flags:=0;
  1589. end;
  1590. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1591. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1592. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1593. begin
  1594. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1595. end;
  1596. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1597. {$endif}
  1598. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1599. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1600. begin
  1601. { the extra Word type cast is necessary because the "AValue shr 8" }
  1602. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1603. { the sign bits from the upper 16 bits are shifted in rather than }
  1604. { zeroes. }
  1605. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1606. end;
  1607. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1608. begin
  1609. Result := Word((AValue shr 8) or (AValue shl 8));
  1610. end;
  1611. function SwapEndian(const AValue: LongInt): LongInt;
  1612. begin
  1613. Result := (AValue shl 24)
  1614. or ((AValue and $0000FF00) shl 8)
  1615. or ((AValue and $00FF0000) shr 8)
  1616. or (AValue shr 24);
  1617. end;
  1618. function SwapEndian(const AValue: DWord): DWord;
  1619. begin
  1620. Result := (AValue shl 24)
  1621. or ((AValue and $0000FF00) shl 8)
  1622. or ((AValue and $00FF0000) shr 8)
  1623. or (AValue shr 24);
  1624. end;
  1625. function SwapEndian(const AValue: Int64): Int64;
  1626. begin
  1627. Result := (AValue shl 56)
  1628. or ((AValue and $000000000000FF00) shl 40)
  1629. or ((AValue and $0000000000FF0000) shl 24)
  1630. or ((AValue and $00000000FF000000) shl 8)
  1631. or ((AValue and $000000FF00000000) shr 8)
  1632. or ((AValue and $0000FF0000000000) shr 24)
  1633. or ((AValue and $00FF000000000000) shr 40)
  1634. or (AValue shr 56);
  1635. end;
  1636. function SwapEndian(const AValue: QWord): QWord;
  1637. begin
  1638. Result := (AValue shl 56)
  1639. or ((AValue and $000000000000FF00) shl 40)
  1640. or ((AValue and $0000000000FF0000) shl 24)
  1641. or ((AValue and $00000000FF000000) shl 8)
  1642. or ((AValue and $000000FF00000000) shr 8)
  1643. or ((AValue and $0000FF0000000000) shr 24)
  1644. or ((AValue and $00FF000000000000) shr 40)
  1645. or (AValue shr 56);
  1646. end;
  1647. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1648. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1649. begin
  1650. {$IFDEF ENDIAN_BIG}
  1651. Result := AValue;
  1652. {$ELSE}
  1653. Result := SwapEndian(AValue);
  1654. {$ENDIF}
  1655. end;
  1656. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1657. begin
  1658. {$IFDEF ENDIAN_BIG}
  1659. Result := AValue;
  1660. {$ELSE}
  1661. Result := SwapEndian(AValue);
  1662. {$ENDIF}
  1663. end;
  1664. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1665. begin
  1666. {$IFDEF ENDIAN_BIG}
  1667. Result := AValue;
  1668. {$ELSE}
  1669. Result := SwapEndian(AValue);
  1670. {$ENDIF}
  1671. end;
  1672. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1673. begin
  1674. {$IFDEF ENDIAN_BIG}
  1675. Result := AValue;
  1676. {$ELSE}
  1677. Result := SwapEndian(AValue);
  1678. {$ENDIF}
  1679. end;
  1680. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1681. begin
  1682. {$IFDEF ENDIAN_BIG}
  1683. Result := AValue;
  1684. {$ELSE}
  1685. Result := SwapEndian(AValue);
  1686. {$ENDIF}
  1687. end;
  1688. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1689. begin
  1690. {$IFDEF ENDIAN_BIG}
  1691. Result := AValue;
  1692. {$ELSE}
  1693. Result := SwapEndian(AValue);
  1694. {$ENDIF}
  1695. end;
  1696. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1697. begin
  1698. {$IFDEF ENDIAN_LITTLE}
  1699. Result := AValue;
  1700. {$ELSE}
  1701. Result := SwapEndian(AValue);
  1702. {$ENDIF}
  1703. end;
  1704. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1705. begin
  1706. {$IFDEF ENDIAN_LITTLE}
  1707. Result := AValue;
  1708. {$ELSE}
  1709. Result := SwapEndian(AValue);
  1710. {$ENDIF}
  1711. end;
  1712. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1713. begin
  1714. {$IFDEF ENDIAN_LITTLE}
  1715. Result := AValue;
  1716. {$ELSE}
  1717. Result := SwapEndian(AValue);
  1718. {$ENDIF}
  1719. end;
  1720. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1721. begin
  1722. {$IFDEF ENDIAN_LITTLE}
  1723. Result := AValue;
  1724. {$ELSE}
  1725. Result := SwapEndian(AValue);
  1726. {$ENDIF}
  1727. end;
  1728. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1729. begin
  1730. {$IFDEF ENDIAN_LITTLE}
  1731. Result := AValue;
  1732. {$ELSE}
  1733. Result := SwapEndian(AValue);
  1734. {$ENDIF}
  1735. end;
  1736. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1737. begin
  1738. {$IFDEF ENDIAN_LITTLE}
  1739. Result := AValue;
  1740. {$ELSE}
  1741. Result := SwapEndian(AValue);
  1742. {$ENDIF}
  1743. end;
  1744. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1745. begin
  1746. {$IFDEF ENDIAN_BIG}
  1747. Result := AValue;
  1748. {$ELSE}
  1749. Result := SwapEndian(AValue);
  1750. {$ENDIF}
  1751. end;
  1752. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1753. begin
  1754. {$IFDEF ENDIAN_BIG}
  1755. Result := AValue;
  1756. {$ELSE}
  1757. Result := SwapEndian(AValue);
  1758. {$ENDIF}
  1759. end;
  1760. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1761. begin
  1762. {$IFDEF ENDIAN_BIG}
  1763. Result := AValue;
  1764. {$ELSE}
  1765. Result := SwapEndian(AValue);
  1766. {$ENDIF}
  1767. end;
  1768. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1769. begin
  1770. {$IFDEF ENDIAN_BIG}
  1771. Result := AValue;
  1772. {$ELSE}
  1773. Result := SwapEndian(AValue);
  1774. {$ENDIF}
  1775. end;
  1776. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1777. begin
  1778. {$IFDEF ENDIAN_BIG}
  1779. Result := AValue;
  1780. {$ELSE}
  1781. Result := SwapEndian(AValue);
  1782. {$ENDIF}
  1783. end;
  1784. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1785. begin
  1786. {$IFDEF ENDIAN_BIG}
  1787. Result := AValue;
  1788. {$ELSE}
  1789. Result := SwapEndian(AValue);
  1790. {$ENDIF}
  1791. end;
  1792. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1793. begin
  1794. {$IFDEF ENDIAN_LITTLE}
  1795. Result := AValue;
  1796. {$ELSE}
  1797. Result := SwapEndian(AValue);
  1798. {$ENDIF}
  1799. end;
  1800. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1801. begin
  1802. {$IFDEF ENDIAN_LITTLE}
  1803. Result := AValue;
  1804. {$ELSE}
  1805. Result := SwapEndian(AValue);
  1806. {$ENDIF}
  1807. end;
  1808. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1809. begin
  1810. {$IFDEF ENDIAN_LITTLE}
  1811. Result := AValue;
  1812. {$ELSE}
  1813. Result := SwapEndian(AValue);
  1814. {$ENDIF}
  1815. end;
  1816. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1817. begin
  1818. {$IFDEF ENDIAN_LITTLE}
  1819. Result := AValue;
  1820. {$ELSE}
  1821. Result := SwapEndian(AValue);
  1822. {$ENDIF}
  1823. end;
  1824. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1825. begin
  1826. {$IFDEF ENDIAN_LITTLE}
  1827. Result := AValue;
  1828. {$ELSE}
  1829. Result := SwapEndian(AValue);
  1830. {$ENDIF}
  1831. end;
  1832. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1833. begin
  1834. {$IFDEF ENDIAN_LITTLE}
  1835. Result := AValue;
  1836. {$ELSE}
  1837. Result := SwapEndian(AValue);
  1838. {$ENDIF}
  1839. end;
  1840. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1841. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1842. begin
  1843. end;
  1844. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1845. begin
  1846. end;
  1847. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1848. begin
  1849. end;
  1850. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1851. begin
  1852. end;
  1853. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  1854. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  1855. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  1856. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1857. begin
  1858. Result:=(AValue shr 1) or (AValue shl 7);
  1859. end;
  1860. function RorByte(Const AValue : Byte;Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1861. begin
  1862. Dist:=Dist and 7;
  1863. Result:=(AValue shr Dist) or (AValue shl (8-Dist));
  1864. end;
  1865. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1866. begin
  1867. Result:=(AValue shl 1) or (AValue shr 7);
  1868. end;
  1869. function RolByte(Const AValue : Byte;Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1870. begin
  1871. Dist:=Dist and 7;
  1872. Result:=(AValue shl Dist) or (AValue shr (8-Dist));
  1873. end;
  1874. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  1875. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  1876. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  1877. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  1878. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1879. begin
  1880. Result:=(AValue shr 1) or (AValue shl 15);
  1881. end;
  1882. function RorWord(Const AValue : Word;Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1883. begin
  1884. Dist:=Dist and 15;
  1885. Result:=(AValue shr Dist) or (AValue shl (16-Dist));
  1886. end;
  1887. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1888. begin
  1889. Result:=(AValue shl 1) or (AValue shr 15);
  1890. end;
  1891. function RolWord(Const AValue : Word;Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1892. begin
  1893. Dist:=Dist and 15;
  1894. Result:=(AValue shl Dist) or (AValue shr (16-Dist));
  1895. end;
  1896. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  1897. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  1898. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  1899. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  1900. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1901. begin
  1902. Result:=(AValue shr 1) or (AValue shl 31);
  1903. end;
  1904. function RorDWord(Const AValue : DWord;Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1905. begin
  1906. Dist:=Dist and 31;
  1907. Result:=(AValue shr Dist) or (AValue shl (32-Dist));
  1908. end;
  1909. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1910. begin
  1911. Result:=(AValue shl 1) or (AValue shr 31);
  1912. end;
  1913. function RolDWord(Const AValue : DWord;Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1914. begin
  1915. Dist:=Dist and 31;
  1916. Result:=(AValue shl Dist) or (AValue shr (32-Dist));
  1917. end;
  1918. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  1919. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  1920. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  1921. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  1922. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1923. begin
  1924. Result:=(AValue shr 1) or (AValue shl 63);
  1925. end;
  1926. function RorQWord(Const AValue : QWord;Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1927. begin
  1928. Dist:=Dist and 63;
  1929. Result:=(AValue shr Dist) or (AValue shl (64-Dist));
  1930. end;
  1931. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1932. begin
  1933. Result:=(AValue shl 1) or (AValue shr 63);
  1934. end;
  1935. function RolQWord(Const AValue : QWord;Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1936. begin
  1937. Dist:=Dist and 63;
  1938. Result:=(AValue shl Dist) or (AValue shr (64-Dist));
  1939. end;
  1940. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  1941. {$endif FPC_HAS_INTERNAL_ROX_QWORD}