generic.inc 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  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. {$ifdef CPU64}
  119. v:=(v shl 32) or v;
  120. {$endif CPU64}
  121. { Align on native pointer size }
  122. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  123. dec(count,pend-pdest);
  124. while pdest<pend do
  125. begin
  126. pdest^:=value;
  127. inc(pdest);
  128. end;
  129. { use sizeuint typecast to force shr optimization }
  130. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  131. while pdest<pend do
  132. begin
  133. pptruint(pdest)^:=v;
  134. inc(pptruint(pdest));
  135. end;
  136. count:=count and (sizeof(ptruint)-1);
  137. end;
  138. pend:=pdest+count;
  139. while pdest<pend do
  140. begin
  141. pdest^:=value;
  142. inc(pdest);
  143. end;
  144. end;
  145. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  146. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  147. procedure fillword(var x;count : SizeInt;value : word);
  148. var
  149. aligncount : sizeint;
  150. pdest,pend : pword;
  151. v : ptruint;
  152. begin
  153. if count <= 0 then
  154. exit;
  155. pdest:=@x;
  156. if Count>4*sizeof(ptruint)-1 then
  157. begin
  158. v:=(value shl 16) or value;
  159. {$ifdef CPU64}
  160. v:=(v shl 32) or v;
  161. {$endif CPU64}
  162. { Align on native pointer size }
  163. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  164. dec(count,aligncount);
  165. pend:=pdest+aligncount;
  166. while pdest<pend do
  167. begin
  168. pdest^:=value;
  169. inc(pdest);
  170. end;
  171. { use sizeuint typecast to force shr optimization }
  172. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  173. while pdest<pend do
  174. begin
  175. pptruint(pdest)^:=v;
  176. inc(pptruint(pdest));
  177. end;
  178. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  179. end;
  180. pend:=pdest+count;
  181. while pdest<pend do
  182. begin
  183. pdest^:=value;
  184. inc(pdest);
  185. end;
  186. end;
  187. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  188. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  189. procedure filldword(var x;count : SizeInt;value : dword);
  190. var
  191. aligncount : sizeint;
  192. pdest,pend : pdword;
  193. v : ptruint;
  194. begin
  195. if count <= 0 then
  196. exit;
  197. pdest:=@x;
  198. if Count>4*sizeof(ptruint)-1 then
  199. begin
  200. v:=value;
  201. {$ifdef CPU64}
  202. v:=(v shl 32) or v;
  203. {$endif CPU64}
  204. { Align on native pointer size }
  205. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  206. dec(count,aligncount);
  207. pend:=pdest+aligncount;
  208. while pdest<pend do
  209. begin
  210. pdest^:=value;
  211. inc(pdest);
  212. end;
  213. { use sizeuint typecast to force shr optimization }
  214. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  215. while pdest<pend do
  216. begin
  217. pptruint(pdest)^:=v;
  218. inc(pptruint(pdest));
  219. end;
  220. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  221. end;
  222. pend:=pdest+count;
  223. while pdest<pend do
  224. begin
  225. pdest^:=value;
  226. inc(pdest);
  227. end;
  228. end;
  229. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  230. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  231. procedure fillqword(var x;count : SizeInt;value : qword);
  232. var
  233. pdest,pend : pqword;
  234. begin
  235. if count <= 0 then
  236. exit;
  237. pdest:=@x;
  238. pend:=pdest+count;
  239. while pdest<pend do
  240. begin
  241. pdest^:=value;
  242. inc(pdest);
  243. end;
  244. end;
  245. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  246. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  247. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  248. var
  249. psrc,pend : pbyte;
  250. begin
  251. psrc:=@buf;
  252. { simulate assembler implementations behaviour, which is expected }
  253. { fpc_pchar_to_ansistr in astrings.inc }
  254. if (len < 0) or
  255. (psrc+len < psrc) then
  256. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  257. else
  258. pend:=psrc+len;
  259. while (psrc<pend) do
  260. begin
  261. if psrc^=b then
  262. begin
  263. result:=psrc-pbyte(@buf);
  264. exit;
  265. end;
  266. inc(psrc);
  267. end;
  268. result:=-1;
  269. end;
  270. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  271. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  272. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  273. var
  274. psrc,pend : pword;
  275. begin
  276. psrc:=@buf;
  277. { simulate assembler implementations behaviour, which is expected }
  278. { fpc_pchar_to_ansistr in astrings.inc }
  279. if (len < 0) or
  280. { is this ever true? }
  281. (len > high(PtrInt)) or
  282. (psrc+len < psrc) then
  283. pend:=pword(high(PtrUInt)-sizeof(word))
  284. else
  285. pend:=psrc+len;
  286. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  287. if (ptruint(psrc) mod 2)<>0 then
  288. while psrc<pend do
  289. begin
  290. if unaligned(psrc^)=b then
  291. begin
  292. result:=psrc-pword(@buf);
  293. exit;
  294. end;
  295. inc(psrc);
  296. end
  297. else
  298. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  299. while psrc<pend do
  300. begin
  301. if psrc^=b then
  302. begin
  303. result:=psrc-pword(@buf);
  304. exit;
  305. end;
  306. inc(psrc);
  307. end;
  308. result:=-1;
  309. end;
  310. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  311. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  312. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  313. var
  314. psrc,pend : pdword;
  315. begin
  316. psrc:=@buf;
  317. { simulate assembler implementations behaviour, which is expected }
  318. { fpc_pchar_to_ansistr in astrings.inc }
  319. if (len < 0) or
  320. (len > high(PtrInt) div 2) or
  321. (psrc+len < psrc) then
  322. pend:=pdword(high(PtrUInt)-sizeof(dword))
  323. else
  324. pend:=psrc+len;
  325. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  326. if (ptruint(psrc) mod 4)<>0 then
  327. while psrc<pend do
  328. begin
  329. if unaligned(psrc^)=b then
  330. begin
  331. result:=psrc-pdword(@buf);
  332. exit;
  333. end;
  334. inc(psrc);
  335. end
  336. else
  337. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  338. while psrc<pend do
  339. begin
  340. if psrc^=b then
  341. begin
  342. result:=psrc-pdword(@buf);
  343. exit;
  344. end;
  345. inc(psrc);
  346. end;
  347. result:=-1;
  348. end;
  349. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  350. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  351. function IndexQWord(Const buf;len:SizeInt;b:QWord):SizeInt;
  352. var
  353. psrc,pend : pqword;
  354. begin
  355. psrc:=@buf;
  356. { simulate assembler implementations behaviour, which is expected }
  357. { fpc_pchar_to_ansistr in astrings.inc }
  358. if (len < 0) or
  359. (len > high(PtrInt) div 4) or
  360. (psrc+len < psrc) then
  361. pend:=pqword(high(PtrUInt)-sizeof(qword))
  362. else
  363. pend:=psrc+len;
  364. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  365. if (ptruint(psrc) mod 8)<>0 then
  366. while psrc<pend do
  367. begin
  368. if unaligned(psrc^)=b then
  369. begin
  370. result:=psrc-pqword(@buf);
  371. exit;
  372. end;
  373. inc(psrc);
  374. end
  375. else
  376. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  377. while psrc<pend do
  378. begin
  379. if psrc^=b then
  380. begin
  381. result:=psrc-pqword(@buf);
  382. exit;
  383. end;
  384. inc(psrc);
  385. end;
  386. result:=-1;
  387. end;
  388. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  389. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  390. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  391. var
  392. aligncount : sizeint;
  393. psrc,pdest,pend : pbyte;
  394. b : ptrint;
  395. begin
  396. b:=0;
  397. psrc:=@buf1;
  398. pdest:=@buf2;
  399. if (len>4*sizeof(ptruint)-1)
  400. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  401. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  402. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  403. then
  404. begin
  405. { Align on native pointer size }
  406. aligncount:=(sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1);
  407. dec(len,aligncount);
  408. pend:=psrc+aligncount;
  409. while psrc<pend do
  410. begin
  411. b:=(ptrint(psrc^)-ptrint(pdest^));
  412. if b<>0 then
  413. begin
  414. if b<0 then
  415. exit(-1)
  416. else
  417. exit(1);
  418. end;
  419. inc(pdest);
  420. inc(psrc);
  421. end;
  422. { use sizeuint typecast to force shr optimization }
  423. pptruint(pend):=pptruint(psrc)+(sizeuint(len) div sizeof(ptruint));
  424. len:=len and (sizeof(PtrUInt)-1);
  425. while psrc<pend do
  426. begin
  427. b:=(pptrint(psrc)^-pptrint(pdest)^);
  428. if b<>0 then
  429. begin
  430. len:=sizeof(ptruint);
  431. break;
  432. end;
  433. inc(pptruint(pdest));
  434. inc(pptruint(psrc));
  435. end;
  436. end;
  437. if (psrc+len >= psrc) then
  438. pend:=psrc+len
  439. else
  440. pend:=pbyte(high(ptruint)-1);
  441. while psrc<pend do
  442. begin
  443. b:=(ptrint(psrc^)-ptrint(pdest^));
  444. if b<>0 then
  445. begin
  446. if b<0 then
  447. exit(-1)
  448. else
  449. exit(1);
  450. end;
  451. inc(pdest);
  452. inc(psrc);
  453. end;
  454. result:=0;
  455. end;
  456. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  457. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  458. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  459. var
  460. aligncount : sizeint;
  461. psrc,pdest,pend : pword;
  462. b : ptrint;
  463. begin
  464. b:=0;
  465. psrc:=@buf1;
  466. pdest:=@buf2;
  467. if (len>4*sizeof(ptruint)-1)
  468. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  469. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  470. and (((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))=0)
  471. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  472. then
  473. begin
  474. { Align on native pointer size }
  475. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 1;
  476. dec(len,aligncount);
  477. pend:=psrc+aligncount;
  478. while psrc<pend do
  479. begin
  480. b:=(ptrint(psrc^)-ptrint(pdest^));
  481. if b<>0 then
  482. begin
  483. if b<0 then
  484. exit(-1)
  485. else
  486. exit(1);
  487. end;
  488. inc(pdest);
  489. inc(psrc);
  490. end;
  491. { use sizeuint typecast to force shr optimization }
  492. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*2 div sizeof(ptruint));
  493. len:=((len*2) and (sizeof(PtrUInt)-1)) shr 1;
  494. while psrc<pend do
  495. begin
  496. b:=(pptrint(psrc)^-pptrint(pdest)^);
  497. if b<>0 then
  498. begin
  499. len:=sizeof(ptruint) shr 1;
  500. break;
  501. end;
  502. inc(pptruint(pdest));
  503. inc(pptruint(psrc));
  504. end;
  505. end;
  506. if (len <= high(ptrint)) and
  507. (psrc+len >= psrc) then
  508. pend:=psrc+len
  509. else
  510. pend:=pword(high(ptruint)-2);
  511. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  512. if ((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))<>0 then
  513. while psrc<pend do
  514. begin
  515. b:=(ptrint(unaligned(psrc^))-ptrint(unaligned(pdest^)));
  516. if b<>0 then
  517. begin
  518. if b<0 then
  519. exit(-1)
  520. else
  521. exit(1);
  522. end;
  523. inc(pdest);
  524. inc(psrc);
  525. end
  526. else
  527. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  528. while psrc<pend do
  529. begin
  530. b:=(ptrint(psrc^)-ptrint(pdest^));
  531. if b<>0 then
  532. begin
  533. if b<0 then
  534. exit(-1)
  535. else
  536. exit(1);
  537. end;
  538. inc(pdest);
  539. inc(psrc);
  540. end;
  541. result:=0;
  542. end;
  543. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  544. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  545. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  546. var
  547. aligncount : sizeint;
  548. psrc,pdest,pend : pdword;
  549. begin
  550. psrc:=@buf1;
  551. pdest:=@buf2;
  552. if (len>4*sizeof(ptruint)-11)
  553. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  554. and (((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))=0)
  555. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  556. then
  557. begin
  558. { Align on native pointer size }
  559. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  560. dec(len,aligncount);
  561. pend:=psrc+aligncount;
  562. while psrc<pend do
  563. begin
  564. if psrc^<>pdest^ then
  565. if psrc^>pdest^ then
  566. exit(1)
  567. else
  568. exit(-1);
  569. inc(pdest);
  570. inc(psrc);
  571. end;
  572. { use sizeuint typecast to force shr optimization }
  573. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  574. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  575. while psrc<pend do
  576. begin
  577. if pptrint(psrc)^<>pptrint(pdest)^ then
  578. begin
  579. len:=sizeof(ptruint) shr 2;
  580. break;
  581. end;
  582. inc(pptruint(pdest));
  583. inc(pptruint(psrc));
  584. end;
  585. end;
  586. if (len <= high(ptrint) div 2) and
  587. (psrc+len >= psrc) then
  588. pend:=psrc+len
  589. else
  590. pend:=pdword(high(ptruint)-4);
  591. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  592. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  593. while psrc<pend do
  594. begin
  595. if unaligned(psrc^)<>unaligned(pdest^) then
  596. if unaligned(psrc^)>unaligned(pdest^) then
  597. exit(1)
  598. else
  599. exit(-1);
  600. inc(pdest);
  601. inc(psrc);
  602. end
  603. else
  604. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  605. while psrc<pend do
  606. begin
  607. if psrc^<>pdest^ then
  608. if psrc^>pdest^ then
  609. exit(1)
  610. else
  611. exit(-1);
  612. inc(pdest);
  613. inc(psrc);
  614. end;
  615. result:=0;
  616. end;
  617. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  618. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  619. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  620. var
  621. I : SizeInt;
  622. begin
  623. if Len = 0 then
  624. exit;
  625. I:=IndexByte(Buf1,Len,0);
  626. if I<>-1 then
  627. Move(Buf1,Buf2,I)
  628. else
  629. Move(Buf1,Buf2,len);
  630. end;
  631. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  632. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  633. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  634. var
  635. psrc,pend : pbyte;
  636. begin
  637. psrc:=@buf;
  638. { simulate assembler implementations behaviour, which is expected }
  639. { fpc_pchar_to_ansistr in astrings.inc }
  640. if (len < 0) then
  641. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  642. else
  643. pend:=psrc+len;
  644. while (psrc<pend) and (psrc^<>0) do
  645. begin
  646. if (psrc^=byte(b)) then
  647. begin
  648. result:=psrc-pbyte(@buf);
  649. exit;
  650. end;
  651. inc(psrc);
  652. end;
  653. result:=-1;
  654. end;
  655. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  656. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  657. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  658. var
  659. psrc,pdest,pend : pbyte;
  660. b : ptrint;
  661. begin
  662. b:=0;
  663. psrc:=@buf1;
  664. pdest:=@buf2;
  665. pend:=psrc+len;
  666. while psrc<pend do
  667. begin
  668. b:=(ptrint(psrc^)-ptrint(pdest^));
  669. if b<0 then
  670. exit(-1)
  671. else if b>0 then
  672. exit(1);
  673. if (psrc^=0) or (pdest^=0) then
  674. exit(0);
  675. inc(pdest);
  676. inc(psrc);
  677. end;
  678. result:=0;
  679. end;
  680. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  681. {****************************************************************************
  682. Object Helpers
  683. ****************************************************************************}
  684. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  685. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  686. { Note: _vmt will be reset to -1 when memory is allocated,
  687. this is needed for fpc_help_fail }
  688. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  689. type
  690. ppointer = ^pointer;
  691. pvmt = ^tvmt;
  692. tvmt=packed record
  693. size,msize:ptruint;
  694. parent:pointer;
  695. end;
  696. var
  697. vmtcopy : pointer;
  698. begin
  699. { Inherited call? }
  700. if _vmt=nil then
  701. begin
  702. fpc_help_constructor:=_self;
  703. exit;
  704. end;
  705. vmtcopy:=_vmt;
  706. if (_self=nil) and
  707. (pvmt(_vmt)^.size>0) then
  708. begin
  709. getmem(_self,pvmt(_vmt)^.size);
  710. { reset vmt needed for fail }
  711. _vmt:=pointer(-1);
  712. end;
  713. if _self<>nil then
  714. begin
  715. fillchar(_self^,pvmt(vmtcopy)^.size,0);
  716. ppointer(_self+_vmt_pos)^:=vmtcopy;
  717. end;
  718. fpc_help_constructor:=_self;
  719. end;
  720. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  721. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  722. { Note: _self will not be reset, the compiler has to generate the reset }
  723. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  724. type
  725. ppointer = ^pointer;
  726. pvmt = ^tvmt;
  727. tvmt = packed record
  728. size,msize : ptruint;
  729. parent : pointer;
  730. end;
  731. begin
  732. { already released? }
  733. if (_self=nil) or
  734. (_vmt=nil) or
  735. (ppointer(_self+vmt_pos)^=nil) then
  736. exit;
  737. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  738. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  739. RunError(210);
  740. { reset vmt to nil for protection }
  741. ppointer(_self+vmt_pos)^:=nil;
  742. freemem(_self);
  743. end;
  744. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  745. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  746. { Note: _self will not be reset, the compiler has to generate the reset }
  747. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  748. begin
  749. if (_self=nil) or (_vmt=nil) then
  750. exit;
  751. { vmt=$ffffffff when memory was allocated }
  752. if ptruint(_vmt)=high(ptruint) then
  753. begin
  754. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  755. HandleError(210)
  756. else
  757. begin
  758. ppointer(_self+vmt_pos)^:=nil;
  759. freemem(_self);
  760. { reset _vmt to nil so it will not be freed a
  761. second time }
  762. _vmt:=nil;
  763. end;
  764. end
  765. else
  766. ppointer(_self+vmt_pos)^:=nil;
  767. end;
  768. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  769. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  770. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  771. type
  772. pvmt = ^tvmt;
  773. tvmt = packed record
  774. size,msize : ptruint;
  775. parent : pointer;
  776. end;
  777. begin
  778. if (_vmt=nil) or
  779. (pvmt(_vmt)^.size=0) or
  780. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  781. RunError(210);
  782. end;
  783. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  784. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  785. { checks for a correct vmt pointer }
  786. { deeper check to see if the current object is }
  787. { really related to the true }
  788. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  789. type
  790. pvmt = ^tvmt;
  791. tvmt = packed record
  792. size,msize : ptruint;
  793. parent : pointer;
  794. end;
  795. begin
  796. if (vmt=nil) or
  797. (pvmt(vmt)^.size=0) or
  798. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  799. RunError(210);
  800. while assigned(vmt) do
  801. if vmt=expvmt then
  802. exit
  803. else
  804. vmt:=pvmt(vmt)^.parent;
  805. RunError(219);
  806. end;
  807. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  808. {$endif FPC_HAS_FEATURE_OBJECTS}
  809. {****************************************************************************
  810. String
  811. ****************************************************************************}
  812. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  813. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  814. var
  815. slen : byte;
  816. begin
  817. slen:=length(sstr);
  818. if slen>high(res) then
  819. slen:=high(res);
  820. move(sstr[0],res[0],slen+1);
  821. res[0]:=chr(slen);
  822. end;
  823. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  824. var
  825. slen : byte;
  826. type
  827. pstring = ^string;
  828. begin
  829. slen:=length(pstring(sstr)^);
  830. if slen<len then
  831. len:=slen;
  832. move(sstr^,dstr^,len+1);
  833. if slen>len then
  834. pchar(dstr)^:=chr(len);
  835. end;
  836. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  837. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  838. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  839. var
  840. s1l, s2l : longint;
  841. begin
  842. s1l:=length(s1);
  843. s2l:=length(s2);
  844. if s1l+s2l>high(dests) then
  845. begin
  846. if s1l>high(dests) then
  847. s1l:=high(dests);
  848. s2l:=high(dests)-s1l;
  849. end;
  850. if @dests=@s1 then
  851. move(s2[1],dests[s1l+1],s2l)
  852. else
  853. if @dests=@s2 then
  854. begin
  855. move(dests[1],dests[s1l+1],s2l);
  856. move(s1[1],dests[1],s1l);
  857. end
  858. else
  859. begin
  860. move(s1[1],dests[1],s1l);
  861. move(s2[1],dests[s1l+1],s2l);
  862. end;
  863. dests[0]:=chr(s1l+s2l);
  864. end;
  865. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  866. var
  867. s2l : byte;
  868. LowStart,i,
  869. Len : longint;
  870. pc : pchar;
  871. needtemp : boolean;
  872. tmpstr : shortstring;
  873. p,pdest : pshortstring;
  874. begin
  875. if high(sarr)=0 then
  876. begin
  877. DestS:='';
  878. exit;
  879. end;
  880. lowstart:=low(sarr);
  881. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  882. inc(lowstart);
  883. { Check for another reuse, then we can't use
  884. the append optimization and need to use a temp }
  885. needtemp:=false;
  886. for i:=lowstart to high(sarr) do
  887. begin
  888. if Pointer(@DestS)=Pointer(sarr[i]) then
  889. begin
  890. needtemp:=true;
  891. break;
  892. end;
  893. end;
  894. if needtemp then
  895. begin
  896. lowstart:=low(sarr);
  897. tmpstr:='';
  898. pdest:=@tmpstr
  899. end
  900. else
  901. begin
  902. { Start with empty DestS if we start with concatting
  903. the first array element }
  904. if lowstart=low(sarr) then
  905. DestS:='';
  906. pdest:=@DestS;
  907. end;
  908. { Concat all strings, except the string we already
  909. copied in DestS }
  910. Len:=length(pdest^);
  911. pc:=@pdest^[1+Length(pdest^)];
  912. for i:=lowstart to high(sarr) do
  913. begin
  914. p:=sarr[i];
  915. if assigned(p) then
  916. begin
  917. s2l:=length(p^);
  918. if Len+s2l>high(dests) then
  919. s2l:=high(dests)-Len;
  920. Move(p^[1],pc^,s2l);
  921. inc(pc,s2l);
  922. inc(Len,s2l);
  923. end;
  924. end;
  925. pdest^[0]:=Chr(Len);
  926. if needtemp then
  927. DestS:=TmpStr;
  928. end;
  929. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  930. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  931. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  932. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  933. var
  934. s1l, s2l : sizeint;
  935. begin
  936. s1l:=length(s1);
  937. s2l:=length(s2);
  938. if s1l+s2l>high(s1) then
  939. s2l:=high(s1)-s1l;
  940. move(s2[1],s1[s1l+1],s2l);
  941. s1[0]:=chr(s1l+s2l);
  942. end;
  943. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  944. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  945. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  946. var
  947. s1,s2,max,i : byte;
  948. d : longint;
  949. begin
  950. s1:=length(left);
  951. s2:=length(right);
  952. if s1<s2 then
  953. max:=s1
  954. else
  955. max:=s2;
  956. for i:=1 to max do
  957. begin
  958. d:=byte(left[i])-byte(right[i]);
  959. if d>0 then
  960. exit(1)
  961. else if d<0 then
  962. exit(-1);
  963. end;
  964. if s1>s2 then
  965. exit(1)
  966. else if s1<s2 then
  967. exit(-1)
  968. else
  969. exit(0);
  970. end;
  971. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  972. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  973. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  974. begin
  975. Result := longint(left[0]) - longint(right[0]);
  976. if Result = 0 then
  977. Result := CompareByte(left[1],right[1], longint(left[0]));
  978. end;
  979. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  980. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  981. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  982. var
  983. l : longint;
  984. s: shortstring;
  985. begin
  986. if p=nil then
  987. l:=0
  988. else
  989. l:=strlen(p);
  990. if l>high(res) then
  991. l:=high(res);
  992. if l>0 then
  993. move(p^,s[1],l);
  994. s[0]:=chr(l);
  995. res:=s;
  996. end;
  997. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  998. { also define alias which can be used inside the system unit }
  999. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1000. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1001. begin
  1002. fpc_pchar_to_shortstr(result,p);
  1003. end;
  1004. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1005. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1006. var
  1007. l: longint;
  1008. index: longint;
  1009. len: byte;
  1010. begin
  1011. l:=high(arr)+1;
  1012. if l>=high(res)+1 then
  1013. l:=high(res)
  1014. else if l<0 then
  1015. l:=0;
  1016. if zerobased then
  1017. begin
  1018. index:=IndexByte(arr[0],l,0);
  1019. if index<0 then
  1020. len:=l
  1021. else
  1022. len:=index;
  1023. end
  1024. else
  1025. len:=l;
  1026. move(arr[0],res[1],len);
  1027. res[0]:=chr(len);
  1028. end;
  1029. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1030. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1031. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1032. var
  1033. len: longint;
  1034. begin
  1035. len := length(src);
  1036. if len > length(res) then
  1037. len := length(res);
  1038. {$push}{$r-}
  1039. { make sure we don't access char 1 if length is 0 (JM) }
  1040. if len > 0 then
  1041. move(src[1],res[0],len);
  1042. fillchar(res[len],length(res)-len,0);
  1043. {$pop}
  1044. end;
  1045. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1046. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1047. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1048. var i : sizeint;
  1049. begin
  1050. i:=0;
  1051. if assigned(p) then
  1052. while p[i]<>#0 do
  1053. inc(i);
  1054. exit(i);
  1055. end;
  1056. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1057. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1058. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1059. var i : sizeint;
  1060. begin
  1061. i:=0;
  1062. if assigned(p) then
  1063. while p[i]<>#0 do
  1064. inc(i);
  1065. exit(i);
  1066. end;
  1067. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1068. {****************************************************************************
  1069. Caller/StackFrame Helpers
  1070. ****************************************************************************}
  1071. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1072. {_$error Get_frame must be defined for each processor }
  1073. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1074. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1075. {_$error Get_caller_addr must be defined for each processor }
  1076. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1077. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1078. {_$error Get_caller_frame must be defined for each processor }
  1079. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1080. {****************************************************************************
  1081. Math
  1082. ****************************************************************************}
  1083. {****************************************************************************
  1084. Software multiplication
  1085. ****************************************************************************}
  1086. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1087. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1088. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1089. var
  1090. sign : boolean;
  1091. q1,q2,q3 : word;
  1092. begin
  1093. begin
  1094. sign:=false;
  1095. if f1<0 then
  1096. begin
  1097. sign:=not(sign);
  1098. q1:=word(-f1);
  1099. end
  1100. else
  1101. q1:=f1;
  1102. if f2<0 then
  1103. begin
  1104. sign:=not(sign);
  1105. q2:=word(-f2);
  1106. end
  1107. else
  1108. q2:=f2;
  1109. { the q1*q2 is coded as call to mulqword }
  1110. q3:=q1*q2;
  1111. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1112. ((q1>q3) or (q2>q3) or
  1113. { the bit 63 can be only set if we have $8000 }
  1114. { and sign is true }
  1115. (q3 shr 15<>0) and
  1116. ((q3<>word(word(1) shl 15)) or not(sign))
  1117. ) then
  1118. HandleErrorFrame(215,get_frame);
  1119. if sign then
  1120. fpc_mul_integer:=-q3
  1121. else
  1122. fpc_mul_integer:=q3;
  1123. end;
  1124. end;
  1125. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1126. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1127. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1128. var
  1129. _f1,bitpos : word;
  1130. b : byte;
  1131. f1overflowed : boolean;
  1132. begin
  1133. fpc_mul_word:=0;
  1134. bitpos:=1;
  1135. f1overflowed:=false;
  1136. for b:=0 to 15 do
  1137. begin
  1138. if (f2 and bitpos)<>0 then
  1139. begin
  1140. _f1:=fpc_mul_word;
  1141. fpc_mul_word:=fpc_mul_word+f1;
  1142. { if one of the operands is greater than the result an
  1143. overflow occurs }
  1144. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1145. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1146. HandleErrorFrame(215,get_frame);
  1147. end;
  1148. { when bootstrapping, we forget about overflow checking for qword :) }
  1149. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1150. f1:=f1 shl 1;
  1151. bitpos:=bitpos shl 1;
  1152. end;
  1153. end;
  1154. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1155. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1156. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1157. var
  1158. sign : boolean;
  1159. q1,q2,q3 : dword;
  1160. begin
  1161. begin
  1162. sign:=false;
  1163. if f1<0 then
  1164. begin
  1165. sign:=not(sign);
  1166. q1:=dword(-f1);
  1167. end
  1168. else
  1169. q1:=f1;
  1170. if f2<0 then
  1171. begin
  1172. sign:=not(sign);
  1173. q2:=dword(-f2);
  1174. end
  1175. else
  1176. q2:=f2;
  1177. { the q1*q2 is coded as call to mulqword }
  1178. q3:=q1*q2;
  1179. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1180. ((q1>q3) or (q2>q3) or
  1181. { the bit 31 can be only set if we have $8000 0000 }
  1182. { and sign is true }
  1183. (q3 shr 15<>0) and
  1184. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1185. ) then
  1186. HandleErrorFrame(215,get_frame);
  1187. if sign then
  1188. fpc_mul_longint:=-q3
  1189. else
  1190. fpc_mul_longint:=q3;
  1191. end;
  1192. end;
  1193. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1194. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1195. { multiplies two dwords
  1196. the longbool for checkoverflow avoids a misaligned stack
  1197. }
  1198. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1199. var
  1200. _f1,bitpos : dword;
  1201. b : byte;
  1202. f1overflowed : boolean;
  1203. begin
  1204. fpc_mul_dword:=0;
  1205. bitpos:=1;
  1206. f1overflowed:=false;
  1207. for b:=0 to 31 do
  1208. begin
  1209. if (f2 and bitpos)<>0 then
  1210. begin
  1211. _f1:=fpc_mul_dword;
  1212. fpc_mul_dword:=fpc_mul_dword+f1;
  1213. { if one of the operands is greater than the result an
  1214. overflow occurs }
  1215. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1216. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1217. HandleErrorFrame(215,get_frame);
  1218. end;
  1219. { when bootstrapping, we forget about overflow checking for qword :) }
  1220. f1overflowed:=f1overflowed or ((f1 and (1 shl 31))<>0);
  1221. f1:=f1 shl 1;
  1222. bitpos:=bitpos shl 1;
  1223. end;
  1224. end;
  1225. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1226. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1227. {****************************************************************************
  1228. Software longint/dword division
  1229. ****************************************************************************}
  1230. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1231. function count_leading_zeros_32bit(l : longint) : longint;
  1232. var
  1233. i : longint;
  1234. begin
  1235. for i:=0 to 31 do
  1236. begin
  1237. if (l and (longint($80000000) shr i))<>0 then
  1238. begin
  1239. result:=i;
  1240. exit;
  1241. end;
  1242. end;
  1243. result:=i;
  1244. end;
  1245. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1246. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1247. var
  1248. shift,lzz,lzn : longint;
  1249. begin
  1250. result:=0;
  1251. if n=0 then
  1252. HandleErrorFrame(200,get_frame);
  1253. lzz:=count_leading_zeros_32bit(z);
  1254. lzn:=count_leading_zeros_32bit(n);
  1255. { if the denominator contains less zeros
  1256. then the numerator
  1257. the d is greater than the n }
  1258. if lzn<lzz then
  1259. exit;
  1260. shift:=lzn-lzz;
  1261. n:=n shl shift;
  1262. repeat
  1263. if z>=n then
  1264. begin
  1265. z:=z-n;
  1266. result:=result+dword(1 shl shift);
  1267. end;
  1268. dec(shift);
  1269. n:=n shr 1;
  1270. until shift<0;
  1271. end;
  1272. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1273. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1274. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1275. var
  1276. shift,lzz,lzn : longint;
  1277. begin
  1278. result:=0;
  1279. if n=0 then
  1280. HandleErrorFrame(200,get_frame);
  1281. lzz:=count_leading_zeros_32bit(z);
  1282. lzn:=count_leading_zeros_32bit(n);
  1283. { if the denominator contains less zeros
  1284. then the numerator
  1285. the d is greater than the n }
  1286. if lzn<lzz then
  1287. begin
  1288. result:=z;
  1289. exit;
  1290. end;
  1291. shift:=lzn-lzz;
  1292. n:=n shl shift;
  1293. repeat
  1294. if z>=n then
  1295. z:=z-n;
  1296. dec(shift);
  1297. n:=n shr 1;
  1298. until shift<0;
  1299. result:=z;
  1300. end;
  1301. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1302. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1303. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1304. var
  1305. sign : boolean;
  1306. d1,d2 : dword;
  1307. begin
  1308. if n=0 then
  1309. HandleErrorFrame(200,get_frame);
  1310. sign:=false;
  1311. if z<0 then
  1312. begin
  1313. sign:=not(sign);
  1314. d1:=dword(-z);
  1315. end
  1316. else
  1317. d1:=z;
  1318. if n<0 then
  1319. begin
  1320. sign:=not(sign);
  1321. d2:=dword(-n);
  1322. end
  1323. else
  1324. d2:=n;
  1325. { the div is coded by the compiler as call to divdword }
  1326. if sign then
  1327. result:=-(d1 div d2)
  1328. else
  1329. result:=d1 div d2;
  1330. end;
  1331. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1332. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1333. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1334. var
  1335. signed : boolean;
  1336. r,nq,zq : dword;
  1337. begin
  1338. if n=0 then
  1339. HandleErrorFrame(200,get_frame);
  1340. nq:=abs(n);
  1341. if z<0 then
  1342. begin
  1343. zq:=dword(-z);
  1344. signed:=true;
  1345. end
  1346. else
  1347. begin
  1348. zq:=z;
  1349. signed:=false;
  1350. end;
  1351. r:=zq mod nq;
  1352. if signed then
  1353. result:=-longint(r)
  1354. else
  1355. result:=r;
  1356. end;
  1357. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1358. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1359. {****************************************************************************}
  1360. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1361. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1362. begin
  1363. if l<0 then
  1364. abs:=-l
  1365. else
  1366. abs:=l;
  1367. end;
  1368. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1369. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1370. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1371. begin
  1372. odd:=boolean(l and 1);
  1373. end;
  1374. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1375. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1376. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1377. begin
  1378. odd:=boolean(l and 1);
  1379. end;
  1380. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1381. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1382. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1383. begin
  1384. odd:=boolean(longint(l) and 1);
  1385. end;
  1386. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1387. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1388. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1389. begin
  1390. odd:=boolean(longint(l) and 1);
  1391. end;
  1392. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1393. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1394. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1395. begin
  1396. sqr:=l*l;
  1397. end;
  1398. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1399. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1400. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1401. begin
  1402. if l < 0 then
  1403. abs := -l
  1404. else
  1405. abs := l;
  1406. end;
  1407. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1408. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1409. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1410. begin
  1411. sqr := l*l;
  1412. end;
  1413. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1414. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1415. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1416. begin
  1417. sqr := l*l;
  1418. end;
  1419. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1420. {$ifdef CPU16}
  1421. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1422. function declocked(var l:smallint):boolean;
  1423. begin
  1424. Dec(l);
  1425. declocked:=(l=0);
  1426. end;
  1427. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1428. {$endif CPU16}
  1429. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1430. function declocked(var l:longint):boolean;
  1431. begin
  1432. Dec(l);
  1433. declocked:=(l=0);
  1434. end;
  1435. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1436. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1437. function declocked(var l:int64):boolean;
  1438. begin
  1439. Dec(l);
  1440. declocked:=(l=0);
  1441. end;
  1442. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1443. {$ifdef CPU16}
  1444. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1445. procedure inclocked(var l:smallint);
  1446. begin
  1447. Inc(l);
  1448. end;
  1449. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1450. {$endif CPU16}
  1451. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1452. procedure inclocked(var l:longint);
  1453. begin
  1454. Inc(l);
  1455. end;
  1456. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1457. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1458. procedure inclocked(var l:int64);
  1459. begin
  1460. Inc(l);
  1461. end;
  1462. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1463. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1464. {_$error Sptr must be defined for each processor }
  1465. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1466. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1467. var
  1468. tmp: PtrUInt;
  1469. begin
  1470. tmp:=addr+alignment-1;
  1471. result:=tmp-(tmp mod alignment)
  1472. end;
  1473. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1474. var
  1475. tmp: PtrUInt;
  1476. begin
  1477. tmp:=PtrUInt(addr)+alignment-1;
  1478. result:=pointer(tmp-(tmp mod alignment));
  1479. end;
  1480. {****************************************************************************
  1481. Str()
  1482. ****************************************************************************}
  1483. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1484. procedure int_str(l:longint;out s:string);
  1485. var
  1486. m,m1 : longword;
  1487. pcstart,
  1488. pc2start,
  1489. pc,pc2 : pchar;
  1490. hs : string[32];
  1491. overflow : longint;
  1492. begin
  1493. pc2start:=@s[1];
  1494. pc2:=pc2start;
  1495. if (l<0) then
  1496. begin
  1497. pc2^:='-';
  1498. inc(pc2);
  1499. m:=longword(-l);
  1500. end
  1501. else
  1502. m:=longword(l);
  1503. pcstart:=pchar(@hs[0]);
  1504. pc:=pcstart;
  1505. repeat
  1506. m1:=m div 10;
  1507. inc(pc);
  1508. pc^:=char(m-(m1*10)+byte('0'));
  1509. m:=m1;
  1510. until m=0;
  1511. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1512. if overflow>0 then
  1513. inc(pcstart,overflow);
  1514. while (pc>pcstart) do
  1515. begin
  1516. pc2^:=pc^;
  1517. inc(pc2);
  1518. dec(pc);
  1519. end;
  1520. s[0]:=char(pc2-pc2start);
  1521. end;
  1522. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1523. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1524. procedure int_str(l:longword;out s:string);
  1525. var
  1526. m1 : longword;
  1527. pcstart,
  1528. pc2start,
  1529. pc,pc2 : pchar;
  1530. hs : string[32];
  1531. overflow : longint;
  1532. begin
  1533. pc2start:=@s[1];
  1534. pc2:=pc2start;
  1535. pcstart:=pchar(@hs[0]);
  1536. pc:=pcstart;
  1537. repeat
  1538. inc(pc);
  1539. m1:=l div 10;
  1540. pc^:=char(l-(m1*10)+byte('0'));
  1541. l:=m1;
  1542. until l=0;
  1543. overflow:=(pc-pcstart)-high(s);
  1544. if overflow>0 then
  1545. inc(pcstart,overflow);
  1546. while (pc>pcstart) do
  1547. begin
  1548. pc2^:=pc^;
  1549. inc(pc2);
  1550. dec(pc);
  1551. end;
  1552. s[0]:=char(pc2-pc2start);
  1553. end;
  1554. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1555. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1556. procedure int_str(l:int64;out s:string);
  1557. var
  1558. m,m1 : qword;
  1559. pcstart,
  1560. pc2start,
  1561. pc,pc2 : pchar;
  1562. hs : string[32];
  1563. overflow : longint;
  1564. begin
  1565. pc2start:=@s[1];
  1566. pc2:=pc2start;
  1567. if (l<0) then
  1568. begin
  1569. pc2^:='-';
  1570. inc(pc2);
  1571. m:=qword(-l);
  1572. end
  1573. else
  1574. m:=qword(l);
  1575. pcstart:=pchar(@hs[0]);
  1576. pc:=pcstart;
  1577. repeat
  1578. m1:=m div 10;
  1579. inc(pc);
  1580. pc^:=char(m-(m1*10)+byte('0'));
  1581. m:=m1;
  1582. until m=0;
  1583. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1584. if overflow>0 then
  1585. inc(pcstart,overflow);
  1586. while (pc>pcstart) do
  1587. begin
  1588. pc2^:=pc^;
  1589. inc(pc2);
  1590. dec(pc);
  1591. end;
  1592. s[0]:=char(pc2-pc2start);
  1593. end;
  1594. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1595. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1596. procedure int_str(l:qword;out s:string);
  1597. var
  1598. m1 : qword;
  1599. pcstart,
  1600. pc2start,
  1601. pc,pc2 : pchar;
  1602. hs : string[64];
  1603. overflow : longint;
  1604. begin
  1605. pc2start:=@s[1];
  1606. pc2:=pc2start;
  1607. pcstart:=pchar(@hs[0]);
  1608. pc:=pcstart;
  1609. repeat
  1610. inc(pc);
  1611. m1:=l div 10;
  1612. pc^:=char(l-(m1*10)+byte('0'));
  1613. l:=m1;
  1614. until l=0;
  1615. overflow:=(pc-pcstart)-high(s);
  1616. if overflow>0 then
  1617. inc(pcstart,overflow);
  1618. while (pc>pcstart) do
  1619. begin
  1620. pc2^:=pc^;
  1621. inc(pc2);
  1622. dec(pc);
  1623. end;
  1624. s[0]:=char(pc2-pc2start);
  1625. end;
  1626. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1627. {$ifndef FPUNONE}
  1628. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1629. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1630. begin
  1631. softfloat_exception_flags:=0;
  1632. end;
  1633. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1634. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1635. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1636. begin
  1637. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1638. end;
  1639. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1640. {$endif}
  1641. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1642. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1643. begin
  1644. { the extra Word type cast is necessary because the "AValue shr 8" }
  1645. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1646. { the sign bits from the upper 16 bits are shifted in rather than }
  1647. { zeroes. }
  1648. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1649. end;
  1650. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1651. begin
  1652. Result := Word((AValue shr 8) or (AValue shl 8));
  1653. end;
  1654. function SwapEndian(const AValue: LongInt): LongInt;
  1655. begin
  1656. Result := (AValue shl 24)
  1657. or ((AValue and $0000FF00) shl 8)
  1658. or ((AValue and $00FF0000) shr 8)
  1659. or (AValue shr 24);
  1660. end;
  1661. function SwapEndian(const AValue: DWord): DWord;
  1662. begin
  1663. Result := (AValue shl 24)
  1664. or ((AValue and $0000FF00) shl 8)
  1665. or ((AValue and $00FF0000) shr 8)
  1666. or (AValue shr 24);
  1667. end;
  1668. function SwapEndian(const AValue: Int64): Int64;
  1669. begin
  1670. Result := (AValue shl 56)
  1671. or ((AValue and $000000000000FF00) shl 40)
  1672. or ((AValue and $0000000000FF0000) shl 24)
  1673. or ((AValue and $00000000FF000000) shl 8)
  1674. or ((AValue and $000000FF00000000) shr 8)
  1675. or ((AValue and $0000FF0000000000) shr 24)
  1676. or ((AValue and $00FF000000000000) shr 40)
  1677. or (AValue shr 56);
  1678. end;
  1679. function SwapEndian(const AValue: QWord): QWord;
  1680. begin
  1681. Result := (AValue shl 56)
  1682. or ((AValue and $000000000000FF00) shl 40)
  1683. or ((AValue and $0000000000FF0000) shl 24)
  1684. or ((AValue and $00000000FF000000) shl 8)
  1685. or ((AValue and $000000FF00000000) shr 8)
  1686. or ((AValue and $0000FF0000000000) shr 24)
  1687. or ((AValue and $00FF000000000000) shr 40)
  1688. or (AValue shr 56);
  1689. end;
  1690. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1691. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1692. begin
  1693. {$IFDEF ENDIAN_BIG}
  1694. Result := AValue;
  1695. {$ELSE}
  1696. Result := SwapEndian(AValue);
  1697. {$ENDIF}
  1698. end;
  1699. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1700. begin
  1701. {$IFDEF ENDIAN_BIG}
  1702. Result := AValue;
  1703. {$ELSE}
  1704. Result := SwapEndian(AValue);
  1705. {$ENDIF}
  1706. end;
  1707. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1708. begin
  1709. {$IFDEF ENDIAN_BIG}
  1710. Result := AValue;
  1711. {$ELSE}
  1712. Result := SwapEndian(AValue);
  1713. {$ENDIF}
  1714. end;
  1715. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1716. begin
  1717. {$IFDEF ENDIAN_BIG}
  1718. Result := AValue;
  1719. {$ELSE}
  1720. Result := SwapEndian(AValue);
  1721. {$ENDIF}
  1722. end;
  1723. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1724. begin
  1725. {$IFDEF ENDIAN_BIG}
  1726. Result := AValue;
  1727. {$ELSE}
  1728. Result := SwapEndian(AValue);
  1729. {$ENDIF}
  1730. end;
  1731. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1732. begin
  1733. {$IFDEF ENDIAN_BIG}
  1734. Result := AValue;
  1735. {$ELSE}
  1736. Result := SwapEndian(AValue);
  1737. {$ENDIF}
  1738. end;
  1739. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1740. begin
  1741. {$IFDEF ENDIAN_LITTLE}
  1742. Result := AValue;
  1743. {$ELSE}
  1744. Result := SwapEndian(AValue);
  1745. {$ENDIF}
  1746. end;
  1747. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1748. begin
  1749. {$IFDEF ENDIAN_LITTLE}
  1750. Result := AValue;
  1751. {$ELSE}
  1752. Result := SwapEndian(AValue);
  1753. {$ENDIF}
  1754. end;
  1755. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1756. begin
  1757. {$IFDEF ENDIAN_LITTLE}
  1758. Result := AValue;
  1759. {$ELSE}
  1760. Result := SwapEndian(AValue);
  1761. {$ENDIF}
  1762. end;
  1763. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1764. begin
  1765. {$IFDEF ENDIAN_LITTLE}
  1766. Result := AValue;
  1767. {$ELSE}
  1768. Result := SwapEndian(AValue);
  1769. {$ENDIF}
  1770. end;
  1771. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1772. begin
  1773. {$IFDEF ENDIAN_LITTLE}
  1774. Result := AValue;
  1775. {$ELSE}
  1776. Result := SwapEndian(AValue);
  1777. {$ENDIF}
  1778. end;
  1779. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1780. begin
  1781. {$IFDEF ENDIAN_LITTLE}
  1782. Result := AValue;
  1783. {$ELSE}
  1784. Result := SwapEndian(AValue);
  1785. {$ENDIF}
  1786. end;
  1787. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1788. begin
  1789. {$IFDEF ENDIAN_BIG}
  1790. Result := AValue;
  1791. {$ELSE}
  1792. Result := SwapEndian(AValue);
  1793. {$ENDIF}
  1794. end;
  1795. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1796. begin
  1797. {$IFDEF ENDIAN_BIG}
  1798. Result := AValue;
  1799. {$ELSE}
  1800. Result := SwapEndian(AValue);
  1801. {$ENDIF}
  1802. end;
  1803. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1804. begin
  1805. {$IFDEF ENDIAN_BIG}
  1806. Result := AValue;
  1807. {$ELSE}
  1808. Result := SwapEndian(AValue);
  1809. {$ENDIF}
  1810. end;
  1811. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1812. begin
  1813. {$IFDEF ENDIAN_BIG}
  1814. Result := AValue;
  1815. {$ELSE}
  1816. Result := SwapEndian(AValue);
  1817. {$ENDIF}
  1818. end;
  1819. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1820. begin
  1821. {$IFDEF ENDIAN_BIG}
  1822. Result := AValue;
  1823. {$ELSE}
  1824. Result := SwapEndian(AValue);
  1825. {$ENDIF}
  1826. end;
  1827. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1828. begin
  1829. {$IFDEF ENDIAN_BIG}
  1830. Result := AValue;
  1831. {$ELSE}
  1832. Result := SwapEndian(AValue);
  1833. {$ENDIF}
  1834. end;
  1835. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1836. begin
  1837. {$IFDEF ENDIAN_LITTLE}
  1838. Result := AValue;
  1839. {$ELSE}
  1840. Result := SwapEndian(AValue);
  1841. {$ENDIF}
  1842. end;
  1843. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1844. begin
  1845. {$IFDEF ENDIAN_LITTLE}
  1846. Result := AValue;
  1847. {$ELSE}
  1848. Result := SwapEndian(AValue);
  1849. {$ENDIF}
  1850. end;
  1851. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1852. begin
  1853. {$IFDEF ENDIAN_LITTLE}
  1854. Result := AValue;
  1855. {$ELSE}
  1856. Result := SwapEndian(AValue);
  1857. {$ENDIF}
  1858. end;
  1859. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1860. begin
  1861. {$IFDEF ENDIAN_LITTLE}
  1862. Result := AValue;
  1863. {$ELSE}
  1864. Result := SwapEndian(AValue);
  1865. {$ENDIF}
  1866. end;
  1867. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1868. begin
  1869. {$IFDEF ENDIAN_LITTLE}
  1870. Result := AValue;
  1871. {$ELSE}
  1872. Result := SwapEndian(AValue);
  1873. {$ENDIF}
  1874. end;
  1875. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1876. begin
  1877. {$IFDEF ENDIAN_LITTLE}
  1878. Result := AValue;
  1879. {$ELSE}
  1880. Result := SwapEndian(AValue);
  1881. {$ENDIF}
  1882. end;
  1883. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1884. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1885. begin
  1886. end;
  1887. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1888. begin
  1889. end;
  1890. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1891. begin
  1892. end;
  1893. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1894. begin
  1895. end;
  1896. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  1897. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  1898. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  1899. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1900. begin
  1901. Result:=(AValue shr 1) or (AValue shl 7);
  1902. end;
  1903. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1904. begin
  1905. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  1906. end;
  1907. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1908. begin
  1909. Result:=(AValue shl 1) or (AValue shr 7);
  1910. end;
  1911. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1912. begin
  1913. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  1914. end;
  1915. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  1916. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  1917. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  1918. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  1919. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1920. begin
  1921. Result:=(AValue shr 1) or (AValue shl 15);
  1922. end;
  1923. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1924. begin
  1925. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  1926. end;
  1927. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1928. begin
  1929. Result:=(AValue shl 1) or (AValue shr 15);
  1930. end;
  1931. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1932. begin
  1933. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  1934. end;
  1935. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  1936. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  1937. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  1938. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  1939. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1940. begin
  1941. Result:=(AValue shr 1) or (AValue shl 31);
  1942. end;
  1943. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1944. begin
  1945. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  1946. end;
  1947. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1948. begin
  1949. Result:=(AValue shl 1) or (AValue shr 31);
  1950. end;
  1951. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1952. begin
  1953. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  1954. end;
  1955. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  1956. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  1957. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  1958. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  1959. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1960. begin
  1961. Result:=(AValue shr 1) or (AValue shl 63);
  1962. end;
  1963. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1964. begin
  1965. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  1966. end;
  1967. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1968. begin
  1969. Result:=(AValue shl 1) or (AValue shr 63);
  1970. end;
  1971. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1972. begin
  1973. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  1974. end;
  1975. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  1976. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  1977. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  1978. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  1979. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  1980. begin
  1981. Result:=shortint(byte(byte(byte(AValue) shr (Shift and 7)) or (byte(shortint(byte(0-byte(byte(AValue) shr 7)) and byte(shortint(0-(ord((Shift and 7)<>0){ and 1}))))) shl (8-(Shift and 7)))));
  1982. end;
  1983. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  1984. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  1985. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  1986. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  1987. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  1988. begin
  1989. Result:=smallint(word(word(word(AValue) shr (Shift and 15)) or (word(smallint(word(0-word(word(AValue) shr 15)) and word(smallint(0-(ord((Shift and 15)<>0){ and 1}))))) shl (16-(Shift and 15)))));
  1990. end;
  1991. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  1992. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  1993. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  1994. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  1995. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  1996. begin
  1997. Result:=longint(dword(dword(dword(AValue) shr (Shift and 31)) or (dword(longint(dword(0-dword(dword(AValue) shr 31)) and dword(longint(0-(ord((Shift and 31)<>0){ and 1}))))) shl (32-(Shift and 31)))));
  1998. end;
  1999. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2000. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2001. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2002. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2003. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2004. begin
  2005. Result:=int64(qword(qword(qword(AValue) shr (Shift and 63)) or (qword(int64(qword(0-qword(qword(AValue) shr 63)) and qword(int64(0-(ord((Shift and 63)<>0){ and 1}))))) shl (64-(Shift and 63)))));
  2006. end;
  2007. function SarInt64(Const AValue : Int64;const Shift : Byte = 1): Int64; [external name 'FPC_SARINT64'];
  2008. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2009. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2010. {$ifndef FPC_HAS_INTERNAL_BSX_BYTE}
  2011. {$ifndef FPC_SYSTEM_HAS_BSX_BYTE}
  2012. function BsfByte(Const AValue: Byte): Byte;
  2013. const bsf8bit: array [Byte] of Byte = (
  2014. $ff,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2015. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2016. 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2017. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2018. 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2019. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2020. 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
  2021. 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
  2022. );
  2023. begin
  2024. result:=bsf8bit[AValue];
  2025. end;
  2026. function BsrByte(Const AValue: Byte): Byte;
  2027. const bsr8bit: array [Byte] of Byte = (
  2028. $ff,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  2029. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  2030. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  2031. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  2032. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  2033. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  2034. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  2035. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  2036. );
  2037. begin
  2038. result:=bsr8bit[AValue];
  2039. end;
  2040. {$endif}
  2041. {$endif}
  2042. {$ifndef FPC_SYSTEM_HAS_BSX_WORD}
  2043. {$ifndef FPC_HAS_INTERNAL_BSX_WORD}
  2044. function BsfWord(Const AValue: Word): cardinal;
  2045. begin
  2046. if lo(AValue)<>0 then
  2047. result:=BsfByte(lo(AValue))
  2048. else
  2049. result:=BsfByte(hi(AValue))+8
  2050. end;
  2051. function BsrWord(Const AValue: Word): cardinal;
  2052. begin
  2053. if hi(AValue)<>0 then
  2054. result:=BsrByte(hi(AValue))+8
  2055. else
  2056. result:=BsrByte(lo(AValue))
  2057. end;
  2058. {$endif}
  2059. {$endif}
  2060. {$ifndef FPC_HAS_INTERNAL_BSX_DWORD}
  2061. {$ifndef FPC_SYSTEM_HAS_BSX_DWORD}
  2062. function BsfDWord(Const AValue : DWord): cardinal;
  2063. begin
  2064. if lo(AValue)<>0 then
  2065. result:=BsfWord(lo(AValue))
  2066. else
  2067. result:=BsfWord(hi(AValue))+16
  2068. end;
  2069. function BsrDWord(Const AValue : DWord): cardinal;
  2070. begin
  2071. if hi(AValue)<>0 then
  2072. result:=BsrWord(hi(AValue))+16
  2073. else
  2074. result:=BsrWord(lo(AValue))
  2075. end;
  2076. {$endif}
  2077. {$endif}
  2078. {$ifndef FPC_HAS_INTERNAL_BSX_QWORD}
  2079. {$ifndef FPC_SYSTEM_HAS_BSX_QWORD}
  2080. function BsfQWord(Const AValue : QWord): cardinal;
  2081. begin
  2082. if lo(AValue) <> 0 then
  2083. result:=BsfDWord(lo(AValue))
  2084. else
  2085. result:=BsfDWord(hi(AValue)) + 32
  2086. end;
  2087. function BsrQWord(Const AValue : QWord): cardinal;
  2088. begin
  2089. if hi(AValue) <> 0 then
  2090. result:=BsrDWord(hi(AValue)) + 32
  2091. else
  2092. result:=BsrDWord(lo(AValue))
  2093. end;
  2094. {$endif}
  2095. {$endif}