generic.inc 51 KB

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