generic.inc 47 KB

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