generic.inc 62 KB

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