2
0

generic.inc 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  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'];
  824. var
  825. slen : byte;
  826. begin
  827. slen:=length(pshortstring(sstr)^);
  828. if slen<len then
  829. len:=slen;
  830. move(sstr^,dstr^,len+1);
  831. if slen>len then
  832. pchar(dstr)^:=chr(len);
  833. end;
  834. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  835. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  836. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  837. var
  838. s1l, s2l : longint;
  839. begin
  840. s1l:=length(s1);
  841. s2l:=length(s2);
  842. if s1l+s2l>high(dests) then
  843. begin
  844. if s1l>high(dests) then
  845. s1l:=high(dests);
  846. s2l:=high(dests)-s1l;
  847. end;
  848. if @dests=@s1 then
  849. move(s2[1],dests[s1l+1],s2l)
  850. else
  851. if @dests=@s2 then
  852. begin
  853. move(dests[1],dests[s1l+1],s2l);
  854. move(s1[1],dests[1],s1l);
  855. end
  856. else
  857. begin
  858. move(s1[1],dests[1],s1l);
  859. move(s2[1],dests[s1l+1],s2l);
  860. end;
  861. dests[0]:=chr(s1l+s2l);
  862. end;
  863. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  864. var
  865. s2l : byte;
  866. LowStart,i,
  867. Len : longint;
  868. pc : pchar;
  869. needtemp : boolean;
  870. tmpstr : shortstring;
  871. p,pdest : pshortstring;
  872. begin
  873. if high(sarr)=0 then
  874. begin
  875. DestS:='';
  876. exit;
  877. end;
  878. lowstart:=low(sarr);
  879. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  880. inc(lowstart);
  881. { Check for another reuse, then we can't use
  882. the append optimization and need to use a temp }
  883. needtemp:=false;
  884. for i:=lowstart to high(sarr) do
  885. begin
  886. if Pointer(@DestS)=Pointer(sarr[i]) then
  887. begin
  888. needtemp:=true;
  889. break;
  890. end;
  891. end;
  892. if needtemp then
  893. begin
  894. lowstart:=low(sarr);
  895. tmpstr:='';
  896. pdest:=@tmpstr
  897. end
  898. else
  899. begin
  900. { Start with empty DestS if we start with concatting
  901. the first array element }
  902. if lowstart=low(sarr) then
  903. DestS:='';
  904. pdest:=@DestS;
  905. end;
  906. { Concat all strings, except the string we already
  907. copied in DestS }
  908. Len:=length(pdest^);
  909. pc:=@pdest^[1+Length(pdest^)];
  910. for i:=lowstart to high(sarr) do
  911. begin
  912. p:=sarr[i];
  913. if assigned(p) then
  914. begin
  915. s2l:=length(p^);
  916. if Len+s2l>high(dests) then
  917. s2l:=high(dests)-Len;
  918. Move(p^[1],pc^,s2l);
  919. inc(pc,s2l);
  920. inc(Len,s2l);
  921. end;
  922. end;
  923. pdest^[0]:=Chr(Len);
  924. if needtemp then
  925. DestS:=TmpStr;
  926. end;
  927. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  928. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  929. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  930. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  931. var
  932. s1l, s2l : sizeint;
  933. begin
  934. s1l:=length(s1);
  935. s2l:=length(s2);
  936. if s1l+s2l>high(s1) then
  937. s2l:=high(s1)-s1l;
  938. move(s2[1],s1[s1l+1],s2l);
  939. s1[0]:=chr(s1l+s2l);
  940. end;
  941. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  942. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  943. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  944. var
  945. s1,s2,max,i : byte;
  946. d : longint;
  947. begin
  948. s1:=length(left);
  949. s2:=length(right);
  950. if s1<s2 then
  951. max:=s1
  952. else
  953. max:=s2;
  954. for i:=1 to max do
  955. begin
  956. d:=byte(left[i])-byte(right[i]);
  957. if d>0 then
  958. exit(1)
  959. else if d<0 then
  960. exit(-1);
  961. end;
  962. if s1>s2 then
  963. exit(1)
  964. else if s1<s2 then
  965. exit(-1)
  966. else
  967. exit(0);
  968. end;
  969. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  970. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  971. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  972. begin
  973. Result := longint(left[0]) - longint(right[0]);
  974. if Result = 0 then
  975. Result := CompareByte(left[1],right[1], longint(left[0]));
  976. end;
  977. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  978. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  979. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  980. var
  981. l : longint;
  982. s: shortstring;
  983. begin
  984. if p=nil then
  985. l:=0
  986. else
  987. l:=strlen(p);
  988. if l>high(res) then
  989. l:=high(res);
  990. if l>0 then
  991. move(p^,s[1],l);
  992. s[0]:=chr(l);
  993. res:=s;
  994. end;
  995. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  996. { also define alias which can be used inside the system unit }
  997. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  998. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  999. begin
  1000. fpc_pchar_to_shortstr(result,p);
  1001. end;
  1002. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1003. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1004. var
  1005. l: longint;
  1006. index: longint;
  1007. len: byte;
  1008. begin
  1009. l:=high(arr)+1;
  1010. if l>=high(res)+1 then
  1011. l:=high(res)
  1012. else if l<0 then
  1013. l:=0;
  1014. if zerobased then
  1015. begin
  1016. index:=IndexByte(arr[0],l,0);
  1017. if index<0 then
  1018. len:=l
  1019. else
  1020. len:=index;
  1021. end
  1022. else
  1023. len:=l;
  1024. move(arr[0],res[1],len);
  1025. res[0]:=chr(len);
  1026. end;
  1027. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1028. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1029. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1030. var
  1031. len: longint;
  1032. begin
  1033. len := length(src);
  1034. if len > length(res) then
  1035. len := length(res);
  1036. {$push}{$r-}
  1037. { make sure we don't access char 1 if length is 0 (JM) }
  1038. if len > 0 then
  1039. move(src[1],res[0],len);
  1040. fillchar(res[len],length(res)-len,0);
  1041. {$pop}
  1042. end;
  1043. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1044. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1045. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1046. var i : sizeint;
  1047. begin
  1048. i:=0;
  1049. if assigned(p) then
  1050. while p[i]<>#0 do
  1051. inc(i);
  1052. exit(i);
  1053. end;
  1054. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1055. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1056. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1057. var i : sizeint;
  1058. begin
  1059. i:=0;
  1060. if assigned(p) then
  1061. while p[i]<>#0 do
  1062. inc(i);
  1063. exit(i);
  1064. end;
  1065. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1066. {****************************************************************************
  1067. Caller/StackFrame Helpers
  1068. ****************************************************************************}
  1069. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1070. {_$error Get_frame must be defined for each processor }
  1071. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1072. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1073. {_$error Get_caller_addr must be defined for each processor }
  1074. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1075. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1076. {_$error Get_caller_frame must be defined for each processor }
  1077. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1078. {****************************************************************************
  1079. Math
  1080. ****************************************************************************}
  1081. {****************************************************************************
  1082. Software multiplication
  1083. ****************************************************************************}
  1084. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1085. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1086. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1087. var
  1088. sign : boolean;
  1089. q1,q2,q3 : word;
  1090. begin
  1091. begin
  1092. sign:=false;
  1093. if f1<0 then
  1094. begin
  1095. sign:=not(sign);
  1096. q1:=word(-f1);
  1097. end
  1098. else
  1099. q1:=f1;
  1100. if f2<0 then
  1101. begin
  1102. sign:=not(sign);
  1103. q2:=word(-f2);
  1104. end
  1105. else
  1106. q2:=f2;
  1107. { the q1*q2 is coded as call to mulqword }
  1108. q3:=q1*q2;
  1109. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1110. ((q1>q3) or (q2>q3) or
  1111. { the bit 63 can be only set if we have $8000 }
  1112. { and sign is true }
  1113. (q3 shr 15<>0) and
  1114. ((q3<>word(word(1) shl 15)) or not(sign))
  1115. ) then
  1116. HandleErrorFrame(215,get_frame);
  1117. if sign then
  1118. fpc_mul_integer:=-q3
  1119. else
  1120. fpc_mul_integer:=q3;
  1121. end;
  1122. end;
  1123. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1124. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1125. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1126. var
  1127. _f1,bitpos : word;
  1128. b : byte;
  1129. f1overflowed : boolean;
  1130. begin
  1131. fpc_mul_word:=0;
  1132. bitpos:=1;
  1133. f1overflowed:=false;
  1134. for b:=0 to 15 do
  1135. begin
  1136. if (f2 and bitpos)<>0 then
  1137. begin
  1138. _f1:=fpc_mul_word;
  1139. fpc_mul_word:=fpc_mul_word+f1;
  1140. { if one of the operands is greater than the result an
  1141. overflow occurs }
  1142. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1143. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1144. HandleErrorFrame(215,get_frame);
  1145. end;
  1146. { when bootstrapping, we forget about overflow checking for qword :) }
  1147. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1148. f1:=f1 shl 1;
  1149. bitpos:=bitpos shl 1;
  1150. end;
  1151. end;
  1152. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1153. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1154. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1155. var
  1156. sign : boolean;
  1157. q1,q2,q3 : dword;
  1158. begin
  1159. begin
  1160. sign:=false;
  1161. if f1<0 then
  1162. begin
  1163. sign:=not(sign);
  1164. q1:=dword(-f1);
  1165. end
  1166. else
  1167. q1:=f1;
  1168. if f2<0 then
  1169. begin
  1170. sign:=not(sign);
  1171. q2:=dword(-f2);
  1172. end
  1173. else
  1174. q2:=f2;
  1175. { the q1*q2 is coded as call to mulqword }
  1176. q3:=q1*q2;
  1177. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1178. ((q1>q3) or (q2>q3) or
  1179. { the bit 31 can be only set if we have $8000 0000 }
  1180. { and sign is true }
  1181. (q3 shr 15<>0) and
  1182. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1183. ) then
  1184. HandleErrorFrame(215,get_frame);
  1185. if sign then
  1186. fpc_mul_longint:=-q3
  1187. else
  1188. fpc_mul_longint:=q3;
  1189. end;
  1190. end;
  1191. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1192. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1193. { multiplies two dwords
  1194. the longbool for checkoverflow avoids a misaligned stack
  1195. }
  1196. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1197. var
  1198. _f1,bitpos : dword;
  1199. b : byte;
  1200. f1overflowed : boolean;
  1201. begin
  1202. fpc_mul_dword:=0;
  1203. bitpos:=1;
  1204. f1overflowed:=false;
  1205. for b:=0 to 31 do
  1206. begin
  1207. if (f2 and bitpos)<>0 then
  1208. begin
  1209. _f1:=fpc_mul_dword;
  1210. fpc_mul_dword:=fpc_mul_dword+f1;
  1211. { if one of the operands is greater than the result an
  1212. overflow occurs }
  1213. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1214. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1215. HandleErrorFrame(215,get_frame);
  1216. end;
  1217. { when bootstrapping, we forget about overflow checking for qword :) }
  1218. f1overflowed:=f1overflowed or ((f1 and (1 shl 31))<>0);
  1219. f1:=f1 shl 1;
  1220. bitpos:=bitpos shl 1;
  1221. end;
  1222. end;
  1223. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1224. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1225. {****************************************************************************
  1226. Software longint/dword division
  1227. ****************************************************************************}
  1228. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1229. function count_leading_zeros_32bit(l : longint) : longint;
  1230. var
  1231. i : longint;
  1232. begin
  1233. for i:=0 to 31 do
  1234. begin
  1235. if (l and (longint($80000000) shr i))<>0 then
  1236. begin
  1237. result:=i;
  1238. exit;
  1239. end;
  1240. end;
  1241. result:=i;
  1242. end;
  1243. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1244. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1245. var
  1246. shift,lzz,lzn : longint;
  1247. begin
  1248. result:=0;
  1249. if n=0 then
  1250. HandleErrorFrame(200,get_frame);
  1251. lzz:=count_leading_zeros_32bit(z);
  1252. lzn:=count_leading_zeros_32bit(n);
  1253. { if the denominator contains less zeros
  1254. then the numerator
  1255. the d is greater than the n }
  1256. if lzn<lzz then
  1257. exit;
  1258. shift:=lzn-lzz;
  1259. n:=n shl shift;
  1260. repeat
  1261. if z>=n then
  1262. begin
  1263. z:=z-n;
  1264. result:=result+dword(1 shl shift);
  1265. end;
  1266. dec(shift);
  1267. n:=n shr 1;
  1268. until shift<0;
  1269. end;
  1270. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1271. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1272. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1273. var
  1274. shift,lzz,lzn : longint;
  1275. begin
  1276. result:=0;
  1277. if n=0 then
  1278. HandleErrorFrame(200,get_frame);
  1279. lzz:=count_leading_zeros_32bit(z);
  1280. lzn:=count_leading_zeros_32bit(n);
  1281. { if the denominator contains less zeros
  1282. then the numerator
  1283. the d is greater than the n }
  1284. if lzn<lzz then
  1285. begin
  1286. result:=z;
  1287. exit;
  1288. end;
  1289. shift:=lzn-lzz;
  1290. n:=n shl shift;
  1291. repeat
  1292. if z>=n then
  1293. z:=z-n;
  1294. dec(shift);
  1295. n:=n shr 1;
  1296. until shift<0;
  1297. result:=z;
  1298. end;
  1299. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1300. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1301. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1302. var
  1303. sign : boolean;
  1304. d1,d2 : dword;
  1305. begin
  1306. if n=0 then
  1307. HandleErrorFrame(200,get_frame);
  1308. sign:=false;
  1309. if z<0 then
  1310. begin
  1311. sign:=not(sign);
  1312. d1:=dword(-z);
  1313. end
  1314. else
  1315. d1:=z;
  1316. if n<0 then
  1317. begin
  1318. sign:=not(sign);
  1319. d2:=dword(-n);
  1320. end
  1321. else
  1322. d2:=n;
  1323. { the div is coded by the compiler as call to divdword }
  1324. if sign then
  1325. result:=-(d1 div d2)
  1326. else
  1327. result:=d1 div d2;
  1328. end;
  1329. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1330. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1331. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1332. var
  1333. signed : boolean;
  1334. r,nq,zq : dword;
  1335. begin
  1336. if n=0 then
  1337. HandleErrorFrame(200,get_frame);
  1338. nq:=abs(n);
  1339. if z<0 then
  1340. begin
  1341. zq:=dword(-z);
  1342. signed:=true;
  1343. end
  1344. else
  1345. begin
  1346. zq:=z;
  1347. signed:=false;
  1348. end;
  1349. r:=zq mod nq;
  1350. if signed then
  1351. result:=-longint(r)
  1352. else
  1353. result:=r;
  1354. end;
  1355. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1356. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1357. {****************************************************************************}
  1358. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1359. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1360. begin
  1361. if l<0 then
  1362. abs:=-l
  1363. else
  1364. abs:=l;
  1365. end;
  1366. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1367. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1368. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1369. begin
  1370. odd:=boolean(l and 1);
  1371. end;
  1372. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1373. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1374. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1375. begin
  1376. odd:=boolean(l and 1);
  1377. end;
  1378. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1379. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1380. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1381. begin
  1382. odd:=boolean(longint(l) and 1);
  1383. end;
  1384. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1385. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1386. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1387. begin
  1388. odd:=boolean(longint(l) and 1);
  1389. end;
  1390. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1391. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1392. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1393. begin
  1394. sqr:=l*l;
  1395. end;
  1396. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1397. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1398. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1399. begin
  1400. if l < 0 then
  1401. abs := -l
  1402. else
  1403. abs := l;
  1404. end;
  1405. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1406. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1407. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1408. begin
  1409. sqr := l*l;
  1410. end;
  1411. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1412. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1413. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1414. begin
  1415. sqr := l*l;
  1416. end;
  1417. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1418. {$ifdef CPU16}
  1419. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1420. function declocked(var l:smallint):boolean;
  1421. begin
  1422. Dec(l);
  1423. declocked:=(l=0);
  1424. end;
  1425. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1426. {$endif CPU16}
  1427. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1428. function declocked(var l:longint):boolean;
  1429. begin
  1430. Dec(l);
  1431. declocked:=(l=0);
  1432. end;
  1433. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1434. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1435. function declocked(var l:int64):boolean;
  1436. begin
  1437. Dec(l);
  1438. declocked:=(l=0);
  1439. end;
  1440. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1441. {$ifdef CPU16}
  1442. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1443. procedure inclocked(var l:smallint);
  1444. begin
  1445. Inc(l);
  1446. end;
  1447. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1448. {$endif CPU16}
  1449. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1450. procedure inclocked(var l:longint);
  1451. begin
  1452. Inc(l);
  1453. end;
  1454. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1455. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1456. procedure inclocked(var l:int64);
  1457. begin
  1458. Inc(l);
  1459. end;
  1460. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1461. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1462. {_$error Sptr must be defined for each processor }
  1463. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1464. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1465. var
  1466. tmp: PtrUInt;
  1467. begin
  1468. tmp:=addr+(alignment-1);
  1469. result:=tmp-(tmp mod alignment)
  1470. end;
  1471. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1472. var
  1473. tmp: PtrUInt;
  1474. begin
  1475. tmp:=PtrUInt(addr)+(alignment-1);
  1476. result:=pointer(tmp-(tmp mod alignment));
  1477. end;
  1478. {****************************************************************************
  1479. Str()
  1480. ****************************************************************************}
  1481. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1482. procedure int_str(l:longint;out s:string);
  1483. var
  1484. m,m1 : longword;
  1485. pcstart,
  1486. pc2start,
  1487. pc,pc2 : pchar;
  1488. hs : string[32];
  1489. overflow : longint;
  1490. begin
  1491. pc2start:=@s[1];
  1492. pc2:=pc2start;
  1493. if (l<0) then
  1494. begin
  1495. pc2^:='-';
  1496. inc(pc2);
  1497. m:=longword(-l);
  1498. end
  1499. else
  1500. m:=longword(l);
  1501. pcstart:=pchar(@hs[0]);
  1502. pc:=pcstart;
  1503. repeat
  1504. m1:=m div 10;
  1505. inc(pc);
  1506. pc^:=char(m-(m1*10)+byte('0'));
  1507. m:=m1;
  1508. until m=0;
  1509. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1510. if overflow>0 then
  1511. inc(pcstart,overflow);
  1512. while (pc>pcstart) do
  1513. begin
  1514. pc2^:=pc^;
  1515. inc(pc2);
  1516. dec(pc);
  1517. end;
  1518. s[0]:=char(pc2-pc2start);
  1519. end;
  1520. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1521. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1522. procedure int_str(l:longword;out s:string);
  1523. var
  1524. m1 : longword;
  1525. pcstart,
  1526. pc2start,
  1527. pc,pc2 : pchar;
  1528. hs : string[32];
  1529. overflow : longint;
  1530. begin
  1531. pc2start:=@s[1];
  1532. pc2:=pc2start;
  1533. pcstart:=pchar(@hs[0]);
  1534. pc:=pcstart;
  1535. repeat
  1536. inc(pc);
  1537. m1:=l div 10;
  1538. pc^:=char(l-(m1*10)+byte('0'));
  1539. l:=m1;
  1540. until l=0;
  1541. overflow:=(pc-pcstart)-high(s);
  1542. if overflow>0 then
  1543. inc(pcstart,overflow);
  1544. while (pc>pcstart) do
  1545. begin
  1546. pc2^:=pc^;
  1547. inc(pc2);
  1548. dec(pc);
  1549. end;
  1550. s[0]:=char(pc2-pc2start);
  1551. end;
  1552. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1553. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1554. procedure int_str(l:int64;out s:string);
  1555. var
  1556. m,m1 : qword;
  1557. pcstart,
  1558. pc2start,
  1559. pc,pc2 : pchar;
  1560. hs : string[32];
  1561. overflow : longint;
  1562. begin
  1563. pc2start:=@s[1];
  1564. pc2:=pc2start;
  1565. if (l<0) then
  1566. begin
  1567. pc2^:='-';
  1568. inc(pc2);
  1569. m:=qword(-l);
  1570. end
  1571. else
  1572. m:=qword(l);
  1573. pcstart:=pchar(@hs[0]);
  1574. pc:=pcstart;
  1575. repeat
  1576. m1:=m div 10;
  1577. inc(pc);
  1578. pc^:=char(m-(m1*10)+byte('0'));
  1579. m:=m1;
  1580. until m=0;
  1581. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1582. if overflow>0 then
  1583. inc(pcstart,overflow);
  1584. while (pc>pcstart) do
  1585. begin
  1586. pc2^:=pc^;
  1587. inc(pc2);
  1588. dec(pc);
  1589. end;
  1590. s[0]:=char(pc2-pc2start);
  1591. end;
  1592. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1593. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1594. procedure int_str(l:qword;out s:string);
  1595. var
  1596. m1 : qword;
  1597. pcstart,
  1598. pc2start,
  1599. pc,pc2 : pchar;
  1600. hs : string[64];
  1601. overflow : longint;
  1602. begin
  1603. pc2start:=@s[1];
  1604. pc2:=pc2start;
  1605. pcstart:=pchar(@hs[0]);
  1606. pc:=pcstart;
  1607. repeat
  1608. inc(pc);
  1609. m1:=l div 10;
  1610. pc^:=char(l-(m1*10)+byte('0'));
  1611. l:=m1;
  1612. until l=0;
  1613. overflow:=(pc-pcstart)-high(s);
  1614. if overflow>0 then
  1615. inc(pcstart,overflow);
  1616. while (pc>pcstart) do
  1617. begin
  1618. pc2^:=pc^;
  1619. inc(pc2);
  1620. dec(pc);
  1621. end;
  1622. s[0]:=char(pc2-pc2start);
  1623. end;
  1624. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1625. {$ifndef FPUNONE}
  1626. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1627. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1628. begin
  1629. softfloat_exception_flags:=0;
  1630. end;
  1631. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1632. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1633. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1634. begin
  1635. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1636. end;
  1637. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1638. {$endif}
  1639. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1640. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1641. begin
  1642. { the extra Word type cast is necessary because the "AValue shr 8" }
  1643. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1644. { the sign bits from the upper 16 bits are shifted in rather than }
  1645. { zeroes. }
  1646. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1647. end;
  1648. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1649. begin
  1650. Result := Word((AValue shr 8) or (AValue shl 8));
  1651. end;
  1652. function SwapEndian(const AValue: LongInt): LongInt;
  1653. begin
  1654. Result := (AValue shl 24)
  1655. or ((AValue and $0000FF00) shl 8)
  1656. or ((AValue and $00FF0000) shr 8)
  1657. or (AValue shr 24);
  1658. end;
  1659. function SwapEndian(const AValue: DWord): DWord;
  1660. begin
  1661. Result := (AValue shl 24)
  1662. or ((AValue and $0000FF00) shl 8)
  1663. or ((AValue and $00FF0000) shr 8)
  1664. or (AValue shr 24);
  1665. end;
  1666. function SwapEndian(const AValue: Int64): Int64;
  1667. begin
  1668. Result := (AValue shl 56)
  1669. or ((AValue and $000000000000FF00) shl 40)
  1670. or ((AValue and $0000000000FF0000) shl 24)
  1671. or ((AValue and $00000000FF000000) shl 8)
  1672. or ((AValue and $000000FF00000000) shr 8)
  1673. or ((AValue and $0000FF0000000000) shr 24)
  1674. or ((AValue and $00FF000000000000) shr 40)
  1675. or (AValue shr 56);
  1676. end;
  1677. function SwapEndian(const AValue: QWord): QWord;
  1678. begin
  1679. Result := (AValue shl 56)
  1680. or ((AValue and $000000000000FF00) shl 40)
  1681. or ((AValue and $0000000000FF0000) shl 24)
  1682. or ((AValue and $00000000FF000000) shl 8)
  1683. or ((AValue and $000000FF00000000) shr 8)
  1684. or ((AValue and $0000FF0000000000) shr 24)
  1685. or ((AValue and $00FF000000000000) shr 40)
  1686. or (AValue shr 56);
  1687. end;
  1688. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1689. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1690. begin
  1691. {$IFDEF ENDIAN_BIG}
  1692. Result := AValue;
  1693. {$ELSE}
  1694. Result := SwapEndian(AValue);
  1695. {$ENDIF}
  1696. end;
  1697. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1698. begin
  1699. {$IFDEF ENDIAN_BIG}
  1700. Result := AValue;
  1701. {$ELSE}
  1702. Result := SwapEndian(AValue);
  1703. {$ENDIF}
  1704. end;
  1705. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1706. begin
  1707. {$IFDEF ENDIAN_BIG}
  1708. Result := AValue;
  1709. {$ELSE}
  1710. Result := SwapEndian(AValue);
  1711. {$ENDIF}
  1712. end;
  1713. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1714. begin
  1715. {$IFDEF ENDIAN_BIG}
  1716. Result := AValue;
  1717. {$ELSE}
  1718. Result := SwapEndian(AValue);
  1719. {$ENDIF}
  1720. end;
  1721. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1722. begin
  1723. {$IFDEF ENDIAN_BIG}
  1724. Result := AValue;
  1725. {$ELSE}
  1726. Result := SwapEndian(AValue);
  1727. {$ENDIF}
  1728. end;
  1729. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1730. begin
  1731. {$IFDEF ENDIAN_BIG}
  1732. Result := AValue;
  1733. {$ELSE}
  1734. Result := SwapEndian(AValue);
  1735. {$ENDIF}
  1736. end;
  1737. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1738. begin
  1739. {$IFDEF ENDIAN_LITTLE}
  1740. Result := AValue;
  1741. {$ELSE}
  1742. Result := SwapEndian(AValue);
  1743. {$ENDIF}
  1744. end;
  1745. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1746. begin
  1747. {$IFDEF ENDIAN_LITTLE}
  1748. Result := AValue;
  1749. {$ELSE}
  1750. Result := SwapEndian(AValue);
  1751. {$ENDIF}
  1752. end;
  1753. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1754. begin
  1755. {$IFDEF ENDIAN_LITTLE}
  1756. Result := AValue;
  1757. {$ELSE}
  1758. Result := SwapEndian(AValue);
  1759. {$ENDIF}
  1760. end;
  1761. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1762. begin
  1763. {$IFDEF ENDIAN_LITTLE}
  1764. Result := AValue;
  1765. {$ELSE}
  1766. Result := SwapEndian(AValue);
  1767. {$ENDIF}
  1768. end;
  1769. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1770. begin
  1771. {$IFDEF ENDIAN_LITTLE}
  1772. Result := AValue;
  1773. {$ELSE}
  1774. Result := SwapEndian(AValue);
  1775. {$ENDIF}
  1776. end;
  1777. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1778. begin
  1779. {$IFDEF ENDIAN_LITTLE}
  1780. Result := AValue;
  1781. {$ELSE}
  1782. Result := SwapEndian(AValue);
  1783. {$ENDIF}
  1784. end;
  1785. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1786. begin
  1787. {$IFDEF ENDIAN_BIG}
  1788. Result := AValue;
  1789. {$ELSE}
  1790. Result := SwapEndian(AValue);
  1791. {$ENDIF}
  1792. end;
  1793. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1794. begin
  1795. {$IFDEF ENDIAN_BIG}
  1796. Result := AValue;
  1797. {$ELSE}
  1798. Result := SwapEndian(AValue);
  1799. {$ENDIF}
  1800. end;
  1801. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1802. begin
  1803. {$IFDEF ENDIAN_BIG}
  1804. Result := AValue;
  1805. {$ELSE}
  1806. Result := SwapEndian(AValue);
  1807. {$ENDIF}
  1808. end;
  1809. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1810. begin
  1811. {$IFDEF ENDIAN_BIG}
  1812. Result := AValue;
  1813. {$ELSE}
  1814. Result := SwapEndian(AValue);
  1815. {$ENDIF}
  1816. end;
  1817. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1818. begin
  1819. {$IFDEF ENDIAN_BIG}
  1820. Result := AValue;
  1821. {$ELSE}
  1822. Result := SwapEndian(AValue);
  1823. {$ENDIF}
  1824. end;
  1825. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1826. begin
  1827. {$IFDEF ENDIAN_BIG}
  1828. Result := AValue;
  1829. {$ELSE}
  1830. Result := SwapEndian(AValue);
  1831. {$ENDIF}
  1832. end;
  1833. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1834. begin
  1835. {$IFDEF ENDIAN_LITTLE}
  1836. Result := AValue;
  1837. {$ELSE}
  1838. Result := SwapEndian(AValue);
  1839. {$ENDIF}
  1840. end;
  1841. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1842. begin
  1843. {$IFDEF ENDIAN_LITTLE}
  1844. Result := AValue;
  1845. {$ELSE}
  1846. Result := SwapEndian(AValue);
  1847. {$ENDIF}
  1848. end;
  1849. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1850. begin
  1851. {$IFDEF ENDIAN_LITTLE}
  1852. Result := AValue;
  1853. {$ELSE}
  1854. Result := SwapEndian(AValue);
  1855. {$ENDIF}
  1856. end;
  1857. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1858. begin
  1859. {$IFDEF ENDIAN_LITTLE}
  1860. Result := AValue;
  1861. {$ELSE}
  1862. Result := SwapEndian(AValue);
  1863. {$ENDIF}
  1864. end;
  1865. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1866. begin
  1867. {$IFDEF ENDIAN_LITTLE}
  1868. Result := AValue;
  1869. {$ELSE}
  1870. Result := SwapEndian(AValue);
  1871. {$ENDIF}
  1872. end;
  1873. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1874. begin
  1875. {$IFDEF ENDIAN_LITTLE}
  1876. Result := AValue;
  1877. {$ELSE}
  1878. Result := SwapEndian(AValue);
  1879. {$ENDIF}
  1880. end;
  1881. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1882. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1883. begin
  1884. end;
  1885. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1886. begin
  1887. end;
  1888. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1889. begin
  1890. end;
  1891. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1892. begin
  1893. end;
  1894. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  1895. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  1896. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  1897. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1898. begin
  1899. Result:=(AValue shr 1) or (AValue shl 7);
  1900. end;
  1901. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1902. begin
  1903. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  1904. end;
  1905. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1906. begin
  1907. Result:=(AValue shl 1) or (AValue shr 7);
  1908. end;
  1909. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1910. begin
  1911. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  1912. end;
  1913. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  1914. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  1915. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  1916. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  1917. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1918. begin
  1919. Result:=(AValue shr 1) or (AValue shl 15);
  1920. end;
  1921. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1922. begin
  1923. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  1924. end;
  1925. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1926. begin
  1927. Result:=(AValue shl 1) or (AValue shr 15);
  1928. end;
  1929. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1930. begin
  1931. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  1932. end;
  1933. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  1934. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  1935. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  1936. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  1937. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1938. begin
  1939. Result:=(AValue shr 1) or (AValue shl 31);
  1940. end;
  1941. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1942. begin
  1943. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  1944. end;
  1945. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1946. begin
  1947. Result:=(AValue shl 1) or (AValue shr 31);
  1948. end;
  1949. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1950. begin
  1951. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  1952. end;
  1953. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  1954. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  1955. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  1956. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  1957. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1958. begin
  1959. Result:=(AValue shr 1) or (AValue shl 63);
  1960. end;
  1961. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1962. begin
  1963. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  1964. end;
  1965. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1966. begin
  1967. Result:=(AValue shl 1) or (AValue shr 63);
  1968. end;
  1969. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1970. begin
  1971. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  1972. end;
  1973. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  1974. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  1975. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  1976. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  1977. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  1978. begin
  1979. 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)))));
  1980. end;
  1981. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  1982. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  1983. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  1984. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  1985. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  1986. begin
  1987. 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)))));
  1988. end;
  1989. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  1990. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  1991. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  1992. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  1993. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  1994. begin
  1995. 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)))));
  1996. end;
  1997. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  1998. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  1999. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2000. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2001. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2002. begin
  2003. 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)))));
  2004. end;
  2005. function SarInt64(Const AValue : Int64;const Shift : Byte = 1): Int64; [external name 'FPC_SARINT64'];
  2006. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2007. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2008. {$ifndef FPC_HAS_INTERNAL_BSX_BYTE}
  2009. {$ifndef FPC_SYSTEM_HAS_BSX_BYTE}
  2010. function BsfByte(Const AValue: Byte): Byte;
  2011. const bsf8bit: array [Byte] of Byte = (
  2012. $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,
  2013. 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,
  2014. 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,
  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. 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,
  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. 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,
  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. );
  2021. begin
  2022. result:=bsf8bit[AValue];
  2023. end;
  2024. function BsrByte(Const AValue: Byte): Byte;
  2025. const bsr8bit: array [Byte] of Byte = (
  2026. $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,
  2027. 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,
  2028. 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,
  2029. 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,
  2030. 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,
  2031. 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,
  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. );
  2035. begin
  2036. result:=bsr8bit[AValue];
  2037. end;
  2038. {$endif}
  2039. {$endif}
  2040. {$ifndef FPC_SYSTEM_HAS_BSX_WORD}
  2041. {$ifndef FPC_HAS_INTERNAL_BSX_WORD}
  2042. function BsfWord(Const AValue: Word): cardinal;
  2043. begin
  2044. if lo(AValue)<>0 then
  2045. result:=BsfByte(lo(AValue))
  2046. else
  2047. result:=BsfByte(hi(AValue))+8
  2048. end;
  2049. function BsrWord(Const AValue: Word): cardinal;
  2050. begin
  2051. if hi(AValue)<>0 then
  2052. result:=BsrByte(hi(AValue))+8
  2053. else
  2054. result:=BsrByte(lo(AValue))
  2055. end;
  2056. {$endif}
  2057. {$endif}
  2058. {$ifndef FPC_HAS_INTERNAL_BSX_DWORD}
  2059. {$ifndef FPC_SYSTEM_HAS_BSX_DWORD}
  2060. function BsfDWord(Const AValue : DWord): cardinal;
  2061. begin
  2062. if lo(AValue)<>0 then
  2063. result:=BsfWord(lo(AValue))
  2064. else
  2065. result:=BsfWord(hi(AValue))+16
  2066. end;
  2067. function BsrDWord(Const AValue : DWord): cardinal;
  2068. begin
  2069. if hi(AValue)<>0 then
  2070. result:=BsrWord(hi(AValue))+16
  2071. else
  2072. result:=BsrWord(lo(AValue))
  2073. end;
  2074. {$endif}
  2075. {$endif}
  2076. {$ifndef FPC_HAS_INTERNAL_BSX_QWORD}
  2077. {$ifndef FPC_SYSTEM_HAS_BSX_QWORD}
  2078. function BsfQWord(Const AValue : QWord): cardinal;
  2079. begin
  2080. if lo(AValue) <> 0 then
  2081. result:=BsfDWord(lo(AValue))
  2082. else
  2083. result:=BsfDWord(hi(AValue)) + 32
  2084. end;
  2085. function BsrQWord(Const AValue : QWord): cardinal;
  2086. begin
  2087. if hi(AValue) <> 0 then
  2088. result:=BsrDWord(hi(AValue)) + 32
  2089. else
  2090. result:=BsrDWord(lo(AValue))
  2091. end;
  2092. {$endif}
  2093. {$endif}