generic.inc 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  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 then
  616. exit(-1)
  617. else if b>0 then
  618. exit(1);
  619. if (psrc^=0) or (pdest^=0) then
  620. exit(0);
  621. inc(pdest);
  622. inc(psrc);
  623. end;
  624. result:=0;
  625. end;
  626. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  627. {****************************************************************************
  628. Object Helpers
  629. ****************************************************************************}
  630. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  631. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  632. { Note: _vmt will be reset to -1 when memory is allocated,
  633. this is needed for fpc_help_fail }
  634. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  635. type
  636. ppointer = ^pointer;
  637. pvmt = ^tvmt;
  638. tvmt=packed record
  639. size,msize:ptruint;
  640. parent:pointer;
  641. end;
  642. var
  643. vmtcopy : pointer;
  644. begin
  645. { Inherited call? }
  646. if _vmt=nil then
  647. begin
  648. fpc_help_constructor:=_self;
  649. exit;
  650. end;
  651. vmtcopy:=_vmt;
  652. if (_self=nil) and
  653. (pvmt(_vmt)^.size>0) then
  654. begin
  655. getmem(_self,pvmt(_vmt)^.size);
  656. { reset vmt needed for fail }
  657. _vmt:=pointer(-1);
  658. end;
  659. if _self<>nil then
  660. begin
  661. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  662. ppointer(_self+_vmt_pos)^:=vmtcopy;
  663. end;
  664. fpc_help_constructor:=_self;
  665. end;
  666. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  667. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  668. { Note: _self will not be reset, the compiler has to generate the reset }
  669. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  670. type
  671. ppointer = ^pointer;
  672. pvmt = ^tvmt;
  673. tvmt = packed record
  674. size,msize : ptruint;
  675. parent : pointer;
  676. end;
  677. begin
  678. { already released? }
  679. if (_self=nil) or
  680. (_vmt=nil) or
  681. (ppointer(_self+vmt_pos)^=nil) then
  682. exit;
  683. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  684. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  685. RunError(210);
  686. { reset vmt to nil for protection }
  687. ppointer(_self+vmt_pos)^:=nil;
  688. freemem(_self);
  689. end;
  690. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  691. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  692. { Note: _self will not be reset, the compiler has to generate the reset }
  693. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  694. begin
  695. if (_self=nil) or (_vmt=nil) then
  696. exit;
  697. { vmt=$ffffffff when memory was allocated }
  698. if ptruint(_vmt)=high(ptruint) then
  699. begin
  700. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  701. HandleError(210)
  702. else
  703. begin
  704. ppointer(_self+vmt_pos)^:=nil;
  705. freemem(_self);
  706. { reset _vmt to nil so it will not be freed a
  707. second time }
  708. _vmt:=nil;
  709. end;
  710. end
  711. else
  712. ppointer(_self+vmt_pos)^:=nil;
  713. end;
  714. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  715. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  716. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  717. type
  718. pvmt = ^tvmt;
  719. tvmt = packed record
  720. size,msize : ptruint;
  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. end;
  729. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  730. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  731. { checks for a correct vmt pointer }
  732. { deeper check to see if the current object is }
  733. { really related to the true }
  734. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  735. type
  736. pvmt = ^tvmt;
  737. tvmt = packed record
  738. size,msize : ptruint;
  739. parent : pointer;
  740. end;
  741. begin
  742. if (vmt=nil) or
  743. (pvmt(vmt)^.size=0) or
  744. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  745. RunError(210);
  746. while assigned(vmt) do
  747. if vmt=expvmt then
  748. exit
  749. else
  750. vmt:=pvmt(vmt)^.parent;
  751. RunError(219);
  752. end;
  753. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  754. {$endif FPC_HAS_FEATURE_OBJECTS}
  755. {****************************************************************************
  756. String
  757. ****************************************************************************}
  758. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  759. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  760. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  761. var
  762. slen : byte;
  763. begin
  764. slen:=length(sstr);
  765. if slen<len then
  766. len:=slen;
  767. move(sstr[0],result[0],len+1);
  768. if slen>len then
  769. result[0]:=chr(len);
  770. end;
  771. {$else FPC_STRTOSHORTSTRINGPROC}
  772. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  773. var
  774. slen : byte;
  775. begin
  776. slen:=length(sstr);
  777. if slen>high(res) then
  778. slen:=high(res);
  779. move(sstr[0],res[0],slen+1);
  780. res[0]:=chr(slen);
  781. end;
  782. {$endif FPC_STRTOSHORTSTRINGPROC}
  783. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  784. var
  785. slen : byte;
  786. type
  787. pstring = ^string;
  788. begin
  789. slen:=length(pstring(sstr)^);
  790. if slen<len then
  791. len:=slen;
  792. move(sstr^,dstr^,len+1);
  793. if slen>len then
  794. pchar(dstr)^:=chr(len);
  795. end;
  796. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  797. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  798. {$ifndef STR_CONCAT_PROCS}
  799. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; compilerproc;
  800. var
  801. s1l, s2l : byte;
  802. begin
  803. s1l:=length(s1);
  804. s2l:=length(s2);
  805. if s1l+s2l>255 then
  806. s2l:=255-s1l;
  807. move(s1[1],fpc_shortstr_concat[1],s1l);
  808. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  809. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  810. end;
  811. {$else STR_CONCAT_PROCS}
  812. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  813. var
  814. s1l, s2l : longint;
  815. begin
  816. s1l:=length(s1);
  817. s2l:=length(s2);
  818. if s1l+s2l>high(dests) then
  819. s2l:=high(dests)-s1l;
  820. if @dests=@s1 then
  821. move(s2[1],dests[s1l+1],s2l)
  822. else
  823. if @dests=@s2 then
  824. begin
  825. move(dests[1],dests[s1l+1],s2l);
  826. move(s1[1],dests[1],s1l);
  827. end
  828. else
  829. begin
  830. move(s1[1],dests[1],s1l);
  831. move(s2[1],dests[s1l+1],s2l);
  832. end;
  833. dests[0]:=chr(s1l+s2l);
  834. end;
  835. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  836. var
  837. s2l : byte;
  838. LowStart,i,
  839. Len : longint;
  840. pc : pchar;
  841. needtemp : boolean;
  842. tmpstr : shortstring;
  843. p,pdest : pshortstring;
  844. begin
  845. if high(sarr)=0 then
  846. begin
  847. DestS:='';
  848. exit;
  849. end;
  850. lowstart:=low(sarr);
  851. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  852. inc(lowstart);
  853. { Check for another reuse, then we can't use
  854. the append optimization and need to use a temp }
  855. needtemp:=false;
  856. for i:=lowstart to high(sarr) do
  857. begin
  858. if Pointer(@DestS)=Pointer(sarr[i]) then
  859. begin
  860. needtemp:=true;
  861. break;
  862. end;
  863. end;
  864. if needtemp then
  865. begin
  866. lowstart:=low(sarr);
  867. tmpstr:='';
  868. pdest:=@tmpstr
  869. end
  870. else
  871. begin
  872. { Start with empty DestS if we start with concatting
  873. the first array element }
  874. if lowstart=low(sarr) then
  875. DestS:='';
  876. pdest:=@DestS;
  877. end;
  878. { Concat all strings, except the string we already
  879. copied in DestS }
  880. Len:=length(pdest^);
  881. pc:=@pdest^[1+Length(pdest^)];
  882. for i:=lowstart to high(sarr) do
  883. begin
  884. p:=sarr[i];
  885. if assigned(p) then
  886. begin
  887. s2l:=length(p^);
  888. if Len+s2l>high(dests) then
  889. s2l:=high(dests)-Len;
  890. Move(p^[1],pc^,s2l);
  891. inc(pc,s2l);
  892. inc(Len,s2l);
  893. end;
  894. end;
  895. pdest^[0]:=Chr(Len);
  896. if needtemp then
  897. DestS:=TmpStr;
  898. end;
  899. {$endif STR_CONCAT_PROCS}
  900. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  901. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  902. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  903. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  904. var
  905. s1l, s2l : integer;
  906. begin
  907. s1l:=length(s1);
  908. s2l:=length(s2);
  909. if s1l+s2l>high(s1) then
  910. s2l:=high(s1)-s1l;
  911. move(s2[1],s1[s1l+1],s2l);
  912. s1[0]:=chr(s1l+s2l);
  913. end;
  914. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  915. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  916. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  917. var
  918. s1,s2,max,i : byte;
  919. d : longint;
  920. begin
  921. s1:=length(left);
  922. s2:=length(right);
  923. if s1<s2 then
  924. max:=s1
  925. else
  926. max:=s2;
  927. for i:=1 to max do
  928. begin
  929. d:=byte(left[i])-byte(right[i]);
  930. if d>0 then
  931. exit(1)
  932. else if d<0 then
  933. exit(-1);
  934. end;
  935. if s1>s2 then
  936. exit(1)
  937. else if s1<s2 then
  938. exit(-1)
  939. else
  940. exit(0);
  941. end;
  942. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  943. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  944. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  945. begin
  946. Result := longint(left[0]) - longint(right[0]);
  947. if Result = 0 then
  948. Result := CompareByte(left[1],right[1], longint(left[0]));
  949. end;
  950. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  951. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  952. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  953. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  954. var
  955. l : longint;
  956. s: shortstring;
  957. begin
  958. if p=nil then
  959. l:=0
  960. else
  961. l:=strlen(p);
  962. if l>255 then
  963. l:=255;
  964. if l>0 then
  965. move(p^,s[1],l);
  966. s[0]:=chr(l);
  967. fpc_pchar_to_shortstr := s;
  968. end;
  969. {$else FPC_STRTOSHORTSTRINGPROC}
  970. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  971. var
  972. l : longint;
  973. s: shortstring;
  974. begin
  975. if p=nil then
  976. l:=0
  977. else
  978. l:=strlen(p);
  979. if l>high(res) then
  980. l:=high(res);
  981. if l>0 then
  982. move(p^,s[1],l);
  983. s[0]:=chr(l);
  984. res:=s;
  985. end;
  986. {$endif FPC_STRTOSHORTSTRINGPROC}
  987. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  988. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  989. { also define alias which can be used inside the system unit }
  990. function fpc_pchar_to_shortstr(p:pchar):shortstring;[external name 'FPC_PCHAR_TO_SHORTSTR'];
  991. {$else FPC_STRTOSHORTSTRINGPROC}
  992. { also define alias which can be used inside the system unit }
  993. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  994. {$endif FPC_STRTOSHORTSTRINGPROC}
  995. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  996. begin
  997. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  998. result:=fpc_pchar_to_shortstr(p);
  999. {$else FPC_STRTOSHORTSTRINGPROC}
  1000. fpc_pchar_to_shortstr(result,p);
  1001. {$endif FPC_STRTOSHORTSTRINGPROC}
  1002. end;
  1003. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1004. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1005. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1006. var
  1007. l: longint;
  1008. index: longint;
  1009. len: byte;
  1010. begin
  1011. l := high(arr)+1;
  1012. if l>=256 then
  1013. l:=255
  1014. else if l<0 then
  1015. l:=0;
  1016. if (zerobased) then
  1017. begin
  1018. index:=IndexByte(arr[0],l,0);
  1019. if (index < 0) then
  1020. len := l
  1021. else
  1022. len := index;
  1023. end
  1024. else
  1025. len := l;
  1026. move(arr[0],fpc_chararray_to_shortstr[1],len);
  1027. fpc_chararray_to_shortstr[0]:=chr(len);
  1028. end;
  1029. {$else FPC_STRTOSHORTSTRINGPROC}
  1030. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1031. var
  1032. l: longint;
  1033. index: longint;
  1034. len: byte;
  1035. begin
  1036. l:=high(arr)+1;
  1037. if l>=high(res)+1 then
  1038. l:=high(res)
  1039. else if l<0 then
  1040. l:=0;
  1041. if zerobased then
  1042. begin
  1043. index:=IndexByte(arr[0],l,0);
  1044. if index<0 then
  1045. len:=l
  1046. else
  1047. len:=index;
  1048. end
  1049. else
  1050. len:=l;
  1051. move(arr[0],res[1],len);
  1052. res[0]:=chr(len);
  1053. end;
  1054. {$endif FPC_STRTOSHORTSTRINGPROC}
  1055. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1056. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1057. {$ifndef FPC_STRTOCHARARRAYPROC}
  1058. { inside the compiler, the resulttype is modified to that of the actual }
  1059. { chararray we're converting to (JM) }
  1060. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  1061. var
  1062. len: longint;
  1063. begin
  1064. len := length(src);
  1065. if len > arraysize then
  1066. len := arraysize;
  1067. {$r-}
  1068. { make sure we don't access char 1 if length is 0 (JM) }
  1069. if len > 0 then
  1070. move(src[1],fpc_shortstr_to_chararray[0],len);
  1071. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  1072. {$ifdef RangeCheckWasOn}
  1073. {$r+}
  1074. {$endif}
  1075. end;
  1076. {$else ndef FPC_STRTOCHARARRAYPROC}
  1077. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1078. var
  1079. len: longint;
  1080. begin
  1081. len := length(src);
  1082. if len > length(res) then
  1083. len := length(res);
  1084. {$r-}
  1085. { make sure we don't access char 1 if length is 0 (JM) }
  1086. if len > 0 then
  1087. move(src[1],res[0],len);
  1088. fillchar(res[len],length(res)-len,0);
  1089. {$ifdef RangeCheckWasOn}
  1090. {$r+}
  1091. {$endif}
  1092. end;
  1093. {$endif ndef FPC_STRTOCHARARRAYPROC}
  1094. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1095. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1096. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1097. var i : longint;
  1098. begin
  1099. i:=0;
  1100. while p[i]<>#0 do inc(i);
  1101. exit(i);
  1102. end;
  1103. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1104. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1105. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1106. var i : longint;
  1107. begin
  1108. i:=0;
  1109. while p[i]<>#0 do inc(i);
  1110. exit(i);
  1111. end;
  1112. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1113. {****************************************************************************
  1114. Caller/StackFrame Helpers
  1115. ****************************************************************************}
  1116. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1117. {_$error Get_frame must be defined for each processor }
  1118. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1119. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1120. {_$error Get_caller_addr must be defined for each processor }
  1121. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1122. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1123. {_$error Get_caller_frame must be defined for each processor }
  1124. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1125. {****************************************************************************
  1126. Math
  1127. ****************************************************************************}
  1128. {****************************************************************************
  1129. Software longint/dword division
  1130. ****************************************************************************}
  1131. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1132. function count_leading_zeros_32bit(l : longint) : longint;
  1133. var
  1134. i : longint;
  1135. begin
  1136. for i:=0 to 31 do
  1137. begin
  1138. if (l and (longint($80000000) shr i))<>0 then
  1139. begin
  1140. result:=i;
  1141. exit;
  1142. end;
  1143. end;
  1144. result:=i;
  1145. end;
  1146. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1147. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1148. var
  1149. shift,lzz,lzn : longint;
  1150. begin
  1151. result:=0;
  1152. if n=0 then
  1153. HandleErrorFrame(200,get_frame);
  1154. lzz:=count_leading_zeros_32bit(z);
  1155. lzn:=count_leading_zeros_32bit(n);
  1156. { if the denominator contains less zeros
  1157. then the numerator
  1158. the d is greater than the n }
  1159. if lzn<lzz then
  1160. exit;
  1161. shift:=lzn-lzz;
  1162. n:=n shl shift;
  1163. repeat
  1164. if z>=n then
  1165. begin
  1166. z:=z-n;
  1167. result:=result+dword(1 shl shift);
  1168. end;
  1169. dec(shift);
  1170. n:=n shr 1;
  1171. until shift<0;
  1172. end;
  1173. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1174. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1175. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1176. var
  1177. shift,lzz,lzn : longint;
  1178. begin
  1179. result:=0;
  1180. if n=0 then
  1181. HandleErrorFrame(200,get_frame);
  1182. lzz:=count_leading_zeros_32bit(z);
  1183. lzn:=count_leading_zeros_32bit(n);
  1184. { if the denominator contains less zeros
  1185. then the numerator
  1186. the d is greater than the n }
  1187. if lzn<lzz then
  1188. begin
  1189. result:=z;
  1190. exit;
  1191. end;
  1192. shift:=lzn-lzz;
  1193. n:=n shl shift;
  1194. repeat
  1195. if z>=n then
  1196. z:=z-n;
  1197. dec(shift);
  1198. n:=n shr 1;
  1199. until shift<0;
  1200. result:=z;
  1201. end;
  1202. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1203. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1204. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1205. var
  1206. sign : boolean;
  1207. d1,d2 : dword;
  1208. begin
  1209. if n=0 then
  1210. HandleErrorFrame(200,get_frame);
  1211. sign:=false;
  1212. if z<0 then
  1213. begin
  1214. sign:=not(sign);
  1215. d1:=dword(-z);
  1216. end
  1217. else
  1218. d1:=z;
  1219. if n<0 then
  1220. begin
  1221. sign:=not(sign);
  1222. d2:=dword(-n);
  1223. end
  1224. else
  1225. d2:=n;
  1226. { the div is coded by the compiler as call to divdword }
  1227. if sign then
  1228. result:=-(d1 div d2)
  1229. else
  1230. result:=d1 div d2;
  1231. end;
  1232. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1233. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1234. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1235. var
  1236. signed : boolean;
  1237. r,nq,zq : dword;
  1238. begin
  1239. if n=0 then
  1240. HandleErrorFrame(200,get_frame);
  1241. nq:=abs(n);
  1242. if z<0 then
  1243. begin
  1244. zq:=dword(-z);
  1245. signed:=true;
  1246. end
  1247. else
  1248. begin
  1249. zq:=z;
  1250. signed:=false;
  1251. end;
  1252. r:=zq mod nq;
  1253. if signed then
  1254. result:=-longint(r)
  1255. else
  1256. result:=r;
  1257. end;
  1258. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1259. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1260. {****************************************************************************}
  1261. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1262. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1263. begin
  1264. if l<0 then
  1265. abs:=-l
  1266. else
  1267. abs:=l;
  1268. end;
  1269. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1270. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1271. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1272. begin
  1273. odd:=boolean(l and 1);
  1274. end;
  1275. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1276. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1277. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1278. begin
  1279. odd:=boolean(l and 1);
  1280. end;
  1281. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1282. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1283. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1284. begin
  1285. odd:=boolean(longint(l) and 1);
  1286. end;
  1287. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1288. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1289. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1290. begin
  1291. odd:=boolean(longint(l) and 1);
  1292. end;
  1293. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1294. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1295. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1296. begin
  1297. sqr:=l*l;
  1298. end;
  1299. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1300. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1301. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1302. begin
  1303. if l < 0 then
  1304. abs := -l
  1305. else
  1306. abs := l;
  1307. end;
  1308. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1309. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1310. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1311. begin
  1312. sqr := l*l;
  1313. end;
  1314. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1315. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1316. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1317. begin
  1318. sqr := l*l;
  1319. end;
  1320. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1321. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1322. function declocked(var l:longint):boolean;
  1323. begin
  1324. Dec(l);
  1325. declocked:=(l=0);
  1326. end;
  1327. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1328. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1329. function declocked(var l:int64):boolean;
  1330. begin
  1331. Dec(l);
  1332. declocked:=(l=0);
  1333. end;
  1334. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1335. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1336. procedure inclocked(var l:longint);
  1337. begin
  1338. Inc(l);
  1339. end;
  1340. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1341. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1342. procedure inclocked(var l:int64);
  1343. begin
  1344. Inc(l);
  1345. end;
  1346. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1347. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1348. {_$error Sptr must be defined for each processor }
  1349. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1350. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1351. begin
  1352. if addr mod alignment<>0 then
  1353. result:=addr+(alignment-(addr mod alignment))
  1354. else
  1355. result:=addr;
  1356. end;
  1357. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1358. begin
  1359. if PtrUInt(addr) mod alignment<>0 then
  1360. result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
  1361. else
  1362. result:=addr;
  1363. end;
  1364. {****************************************************************************
  1365. Str()
  1366. ****************************************************************************}
  1367. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1368. procedure int_str(l:longint;out s:string);
  1369. var
  1370. m,m1 : longword;
  1371. pc,pc2 : pchar;
  1372. hs : string[32];
  1373. b : longint;
  1374. begin
  1375. pc2:=@s[1];
  1376. if (l<0) then
  1377. begin
  1378. b:=1;
  1379. pc2^:='-';
  1380. inc(pc2);
  1381. m:=longword(-l);
  1382. end
  1383. else
  1384. begin
  1385. b:=0;
  1386. m:=longword(l);
  1387. end;
  1388. pc:=@hs[0];
  1389. repeat
  1390. inc(pc);
  1391. m1:=m div 10;
  1392. pc^:=char(m-(m1*10)+byte('0'));
  1393. m:=m1;
  1394. until m=0;
  1395. while (pc>pchar(@hs[0])) and
  1396. (b<high(s)) do
  1397. begin
  1398. pc2^:=pc^;
  1399. dec(pc);
  1400. inc(pc2);
  1401. inc(b);
  1402. end;
  1403. s[0]:=chr(b);
  1404. end;
  1405. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1406. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1407. procedure int_str(l:longword;out s:string);
  1408. var
  1409. m1 : longword;
  1410. b: longint;
  1411. pc,pc2 : pchar;
  1412. hs : string[32];
  1413. begin
  1414. pc2:=@s[1];
  1415. pc:=@hs[0];
  1416. repeat
  1417. inc(pc);
  1418. m1:=l div 10;
  1419. pc^:=char(l-(m1*10)+byte('0'));
  1420. l:=m1;
  1421. until l=0;
  1422. b:=0;
  1423. while (pc>pchar(@hs[0])) and
  1424. (b<high(s)) do
  1425. begin
  1426. pc2^:=pc^;
  1427. dec(pc);
  1428. inc(pc2);
  1429. inc(b);
  1430. end;
  1431. s[0]:=chr(b);
  1432. end;
  1433. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1434. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1435. procedure int_str(l:int64;out s:string);
  1436. var
  1437. m,m1 : qword;
  1438. pc,pc2 : pchar;
  1439. b: longint;
  1440. hs : string[64];
  1441. begin
  1442. pc2:=@s[1];
  1443. if (l<0) then
  1444. begin
  1445. b:=1;
  1446. pc2^:='-';
  1447. inc(pc2);
  1448. m:=qword(-l);
  1449. end
  1450. else
  1451. begin
  1452. b:=0;
  1453. m:=qword(l);
  1454. end;
  1455. pc:=@hs[0];
  1456. repeat
  1457. inc(pc);
  1458. m1:=m div 10;
  1459. pc^:=char(m-(m1*10)+byte('0'));
  1460. m:=m1;
  1461. until m=0;
  1462. while (pc>pchar(@hs[0])) and
  1463. (b < high(s)) do
  1464. begin
  1465. pc2^:=pc^;
  1466. dec(pc);
  1467. inc(pc2);
  1468. inc(b);
  1469. end;
  1470. s[0]:=chr(b);
  1471. end;
  1472. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1473. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1474. procedure int_str(l:qword;out s:string);
  1475. var
  1476. m1 : qword;
  1477. pc,pc2 : pchar;
  1478. b: longint;
  1479. hs : string[64];
  1480. begin
  1481. pc2:=@s[1];
  1482. pc:=@hs[0];
  1483. repeat
  1484. inc(pc);
  1485. m1:=l div 10;
  1486. pc^:=char(l-(m1*10)+byte('0'));
  1487. l:=m1;
  1488. until l=0;
  1489. b:=0;
  1490. while (pc>pchar(@hs[0])) and
  1491. (b<high(s)) do
  1492. begin
  1493. pc2^:=pc^;
  1494. dec(pc);
  1495. inc(pc2);
  1496. inc(b);
  1497. end;
  1498. s[0]:=chr(b);
  1499. end;
  1500. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1501. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1502. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1503. begin
  1504. softfloat_exception_flags:=0;
  1505. end;
  1506. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1507. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1508. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1509. begin
  1510. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1511. end;
  1512. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1513. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1514. function SwapEndian(const AValue: SmallInt): SmallInt;
  1515. begin
  1516. Result := (AValue shr 8) or (AValue shl 8);
  1517. end;
  1518. function SwapEndian(const AValue: Word): Word;
  1519. begin
  1520. Result := (AValue shr 8) or (AValue shl 8);
  1521. end;
  1522. function SwapEndian(const AValue: LongInt): LongInt;
  1523. begin
  1524. Result := (AValue shl 24)
  1525. or ((AValue and $0000FF00) shl 8)
  1526. or ((AValue and $00FF0000) shr 8)
  1527. or (AValue shr 24);
  1528. end;
  1529. function SwapEndian(const AValue: DWord): DWord;
  1530. begin
  1531. Result := (AValue shl 24)
  1532. or ((AValue and $0000FF00) shl 8)
  1533. or ((AValue and $00FF0000) shr 8)
  1534. or (AValue shr 24);
  1535. end;
  1536. function SwapEndian(const AValue: Int64): Int64;
  1537. begin
  1538. Result := (AValue shl 56)
  1539. or ((AValue and $000000000000FF00) shl 40)
  1540. or ((AValue and $0000000000FF0000) shl 24)
  1541. or ((AValue and $00000000FF000000) shl 8)
  1542. or ((AValue and $000000FF00000000) shr 8)
  1543. or ((AValue and $0000FF0000000000) shr 24)
  1544. or ((AValue and $00FF000000000000) shr 40)
  1545. or (AValue shr 56);
  1546. end;
  1547. function SwapEndian(const AValue: QWord): QWord;
  1548. begin
  1549. Result := (AValue shl 56)
  1550. or ((AValue and $000000000000FF00) shl 40)
  1551. or ((AValue and $0000000000FF0000) shl 24)
  1552. or ((AValue and $00000000FF000000) shl 8)
  1553. or ((AValue and $000000FF00000000) shr 8)
  1554. or ((AValue and $0000FF0000000000) shr 24)
  1555. or ((AValue and $00FF000000000000) shr 40)
  1556. or (AValue shr 56);
  1557. end;
  1558. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1559. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1560. begin
  1561. {$IFDEF ENDIAN_BIG}
  1562. Result := AValue;
  1563. {$ELSE}
  1564. Result := SwapEndian(AValue);
  1565. {$ENDIF}
  1566. end;
  1567. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1568. begin
  1569. {$IFDEF ENDIAN_BIG}
  1570. Result := AValue;
  1571. {$ELSE}
  1572. Result := SwapEndian(AValue);
  1573. {$ENDIF}
  1574. end;
  1575. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1576. begin
  1577. {$IFDEF ENDIAN_BIG}
  1578. Result := AValue;
  1579. {$ELSE}
  1580. Result := SwapEndian(AValue);
  1581. {$ENDIF}
  1582. end;
  1583. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1584. begin
  1585. {$IFDEF ENDIAN_BIG}
  1586. Result := AValue;
  1587. {$ELSE}
  1588. Result := SwapEndian(AValue);
  1589. {$ENDIF}
  1590. end;
  1591. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1592. begin
  1593. {$IFDEF ENDIAN_BIG}
  1594. Result := AValue;
  1595. {$ELSE}
  1596. Result := SwapEndian(AValue);
  1597. {$ENDIF}
  1598. end;
  1599. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1600. begin
  1601. {$IFDEF ENDIAN_BIG}
  1602. Result := AValue;
  1603. {$ELSE}
  1604. Result := SwapEndian(AValue);
  1605. {$ENDIF}
  1606. end;
  1607. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1608. begin
  1609. {$IFDEF ENDIAN_LITTLE}
  1610. Result := AValue;
  1611. {$ELSE}
  1612. Result := SwapEndian(AValue);
  1613. {$ENDIF}
  1614. end;
  1615. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1616. begin
  1617. {$IFDEF ENDIAN_LITTLE}
  1618. Result := AValue;
  1619. {$ELSE}
  1620. Result := SwapEndian(AValue);
  1621. {$ENDIF}
  1622. end;
  1623. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1624. begin
  1625. {$IFDEF ENDIAN_LITTLE}
  1626. Result := AValue;
  1627. {$ELSE}
  1628. Result := SwapEndian(AValue);
  1629. {$ENDIF}
  1630. end;
  1631. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1632. begin
  1633. {$IFDEF ENDIAN_LITTLE}
  1634. Result := AValue;
  1635. {$ELSE}
  1636. Result := SwapEndian(AValue);
  1637. {$ENDIF}
  1638. end;
  1639. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1640. begin
  1641. {$IFDEF ENDIAN_LITTLE}
  1642. Result := AValue;
  1643. {$ELSE}
  1644. Result := SwapEndian(AValue);
  1645. {$ENDIF}
  1646. end;
  1647. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1648. begin
  1649. {$IFDEF ENDIAN_LITTLE}
  1650. Result := AValue;
  1651. {$ELSE}
  1652. Result := SwapEndian(AValue);
  1653. {$ENDIF}
  1654. end;
  1655. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1656. begin
  1657. {$IFDEF ENDIAN_BIG}
  1658. Result := AValue;
  1659. {$ELSE}
  1660. Result := SwapEndian(AValue);
  1661. {$ENDIF}
  1662. end;
  1663. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1664. begin
  1665. {$IFDEF ENDIAN_BIG}
  1666. Result := AValue;
  1667. {$ELSE}
  1668. Result := SwapEndian(AValue);
  1669. {$ENDIF}
  1670. end;
  1671. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1672. begin
  1673. {$IFDEF ENDIAN_BIG}
  1674. Result := AValue;
  1675. {$ELSE}
  1676. Result := SwapEndian(AValue);
  1677. {$ENDIF}
  1678. end;
  1679. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1680. begin
  1681. {$IFDEF ENDIAN_BIG}
  1682. Result := AValue;
  1683. {$ELSE}
  1684. Result := SwapEndian(AValue);
  1685. {$ENDIF}
  1686. end;
  1687. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1688. begin
  1689. {$IFDEF ENDIAN_BIG}
  1690. Result := AValue;
  1691. {$ELSE}
  1692. Result := SwapEndian(AValue);
  1693. {$ENDIF}
  1694. end;
  1695. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1696. begin
  1697. {$IFDEF ENDIAN_BIG}
  1698. Result := AValue;
  1699. {$ELSE}
  1700. Result := SwapEndian(AValue);
  1701. {$ENDIF}
  1702. end;
  1703. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1704. begin
  1705. {$IFDEF ENDIAN_LITTLE}
  1706. Result := AValue;
  1707. {$ELSE}
  1708. Result := SwapEndian(AValue);
  1709. {$ENDIF}
  1710. end;
  1711. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1712. begin
  1713. {$IFDEF ENDIAN_LITTLE}
  1714. Result := AValue;
  1715. {$ELSE}
  1716. Result := SwapEndian(AValue);
  1717. {$ENDIF}
  1718. end;
  1719. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1720. begin
  1721. {$IFDEF ENDIAN_LITTLE}
  1722. Result := AValue;
  1723. {$ELSE}
  1724. Result := SwapEndian(AValue);
  1725. {$ENDIF}
  1726. end;
  1727. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1728. begin
  1729. {$IFDEF ENDIAN_LITTLE}
  1730. Result := AValue;
  1731. {$ELSE}
  1732. Result := SwapEndian(AValue);
  1733. {$ENDIF}
  1734. end;
  1735. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1736. begin
  1737. {$IFDEF ENDIAN_LITTLE}
  1738. Result := AValue;
  1739. {$ELSE}
  1740. Result := SwapEndian(AValue);
  1741. {$ENDIF}
  1742. end;
  1743. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1744. begin
  1745. {$IFDEF ENDIAN_LITTLE}
  1746. Result := AValue;
  1747. {$ELSE}
  1748. Result := SwapEndian(AValue);
  1749. {$ENDIF}
  1750. end;
  1751. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1752. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1753. begin
  1754. end;
  1755. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1756. begin
  1757. end;
  1758. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1759. begin
  1760. end;
  1761. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1762. begin
  1763. end;
  1764. {$endif}