generic.inc 59 KB

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