generic.inc 46 KB

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