generic.inc 47 KB

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