generic.inc 61 KB

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