generic.inc 47 KB

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