generic.inc 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  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. type
  699. pobjectvmt=^tobjectvmt;
  700. tobjectvmt=record
  701. size,msize:sizeuint;
  702. parent:pointer;
  703. end;
  704. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  705. { Note: _vmt will be reset to -1 when memory is allocated,
  706. this is needed for fpc_help_fail }
  707. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  708. var
  709. vmtcopy : pobjectvmt;
  710. begin
  711. vmtcopy:=pobjectvmt(_vmt);
  712. { Inherited call? }
  713. if vmtcopy=nil then
  714. begin
  715. fpc_help_constructor:=_self;
  716. exit;
  717. end;
  718. if (_self=nil) and
  719. (vmtcopy^.size>0) then
  720. begin
  721. getmem(_self,vmtcopy^.size);
  722. { reset vmt needed for fail }
  723. _vmt:=pointer(-1);
  724. end;
  725. if _self<>nil then
  726. begin
  727. fillchar(_self^,vmtcopy^.size,0);
  728. ppointer(_self+_vmt_pos)^:=vmtcopy;
  729. end;
  730. fpc_help_constructor:=_self;
  731. end;
  732. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  733. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  734. { Note: _self will not be reset, the compiler has to generate the reset }
  735. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  736. begin
  737. { already released? }
  738. if (_self=nil) or
  739. (_vmt<>pointer(-1)) or
  740. (ppointer(_self+vmt_pos)^=nil) then
  741. exit;
  742. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  743. (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  744. RunError(210);
  745. { reset vmt to nil for protection }
  746. ppointer(_self+vmt_pos)^:=nil;
  747. freemem(_self);
  748. end;
  749. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  750. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  751. { Note: _self will not be reset, the compiler has to generate the reset }
  752. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  753. begin
  754. if (_self=nil) or (_vmt=nil) then
  755. exit;
  756. { vmt=$ffffffff when memory was allocated }
  757. if ptruint(_vmt)=high(ptruint) then
  758. begin
  759. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  760. HandleError(210)
  761. else
  762. begin
  763. ppointer(_self+vmt_pos)^:=nil;
  764. freemem(_self);
  765. { reset _vmt to nil so it will not be freed a
  766. second time }
  767. _vmt:=nil;
  768. end;
  769. end
  770. else
  771. ppointer(_self+vmt_pos)^:=nil;
  772. end;
  773. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  774. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  775. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  776. begin
  777. if (_vmt=nil) or
  778. (pobjectvmt(_vmt)^.size=0) or
  779. (pobjectvmt(_vmt)^.size+pobjectvmt(_vmt)^.msize<>0) then
  780. RunError(210);
  781. end;
  782. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  783. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  784. { checks for a correct vmt pointer }
  785. { deeper check to see if the current object is }
  786. { really related to the true }
  787. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  788. begin
  789. if (vmt=nil) or
  790. (pobjectvmt(vmt)^.size=0) or
  791. (pobjectvmt(vmt)^.size+pobjectvmt(vmt)^.msize<>0) then
  792. RunError(210);
  793. while assigned(vmt) do
  794. if vmt=expvmt then
  795. exit
  796. else
  797. vmt:=pobjectvmt(vmt)^.parent;
  798. RunError(219);
  799. end;
  800. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  801. {$endif FPC_HAS_FEATURE_OBJECTS}
  802. {****************************************************************************
  803. String
  804. ****************************************************************************}
  805. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  806. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  807. var
  808. slen : byte;
  809. begin
  810. slen:=length(sstr);
  811. if slen>high(res) then
  812. slen:=high(res);
  813. move(sstr[0],res[0],slen+1);
  814. res[0]:=chr(slen);
  815. end;
  816. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  817. var
  818. slen : byte;
  819. begin
  820. slen:=length(pshortstring(sstr)^);
  821. if slen<len then
  822. len:=slen;
  823. move(sstr^,dstr^,len+1);
  824. if slen>len then
  825. pchar(dstr)^:=chr(len);
  826. end;
  827. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  828. {$push}
  829. { ensure that comparing addresses of openshortstrings with regular shortstrings
  830. doesn't cause errors }
  831. {$t-}
  832. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  833. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  834. var
  835. s1l, s2l : longint;
  836. begin
  837. s1l:=length(s1);
  838. s2l:=length(s2);
  839. if s1l+s2l>high(dests) then
  840. begin
  841. if s1l>high(dests) then
  842. s1l:=high(dests);
  843. s2l:=high(dests)-s1l;
  844. end;
  845. if @dests=@s1 then
  846. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l)
  847. else
  848. if @dests=@s2 then
  849. begin
  850. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,s1l+1,s2l);
  851. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  852. end
  853. else
  854. begin
  855. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  856. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  857. end;
  858. dests[0]:=chr(s1l+s2l);
  859. end;
  860. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  861. var
  862. s2l : byte;
  863. LowStart,i,
  864. Len : longint;
  865. needtemp : boolean;
  866. tmpstr : shortstring;
  867. p,pdest : pshortstring;
  868. begin
  869. if high(sarr)=0 then
  870. begin
  871. DestS:='';
  872. exit;
  873. end;
  874. lowstart:=low(sarr);
  875. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  876. inc(lowstart);
  877. { Check for another reuse, then we can't use
  878. the append optimization and need to use a temp }
  879. needtemp:=false;
  880. for i:=lowstart to high(sarr) do
  881. begin
  882. if Pointer(@DestS)=Pointer(sarr[i]) then
  883. begin
  884. needtemp:=true;
  885. break;
  886. end;
  887. end;
  888. if needtemp then
  889. begin
  890. lowstart:=low(sarr);
  891. tmpstr:='';
  892. pdest:=@tmpstr
  893. end
  894. else
  895. begin
  896. { Start with empty DestS if we start with concatting
  897. the first array element }
  898. if lowstart=low(sarr) then
  899. DestS:='';
  900. pdest:=@DestS;
  901. end;
  902. { Concat all strings, except the string we already
  903. copied in DestS }
  904. Len:=length(pdest^);
  905. for i:=lowstart to high(sarr) do
  906. begin
  907. p:=sarr[i];
  908. if assigned(p) then
  909. begin
  910. s2l:=length(p^);
  911. if Len+s2l>high(dests) then
  912. s2l:=high(dests)-Len;
  913. fpc_shortstr_shortstr_intern_charmove(p^,1,pdest^,Len+1,s2l);
  914. inc(Len,s2l);
  915. end;
  916. end;
  917. pdest^[0]:=Chr(Len);
  918. if needtemp then
  919. DestS:=TmpStr;
  920. end;
  921. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  922. {$pop}
  923. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  924. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  925. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  926. var
  927. s1l, s2l : sizeint;
  928. begin
  929. s1l:=length(s1);
  930. s2l:=length(s2);
  931. if s1l+s2l>high(s1) then
  932. s2l:=high(s1)-s1l;
  933. move(s2[1],s1[s1l+1],s2l);
  934. s1[0]:=chr(s1l+s2l);
  935. end;
  936. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  937. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  938. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  939. var
  940. s1,s2,max,i : byte;
  941. d : longint;
  942. begin
  943. s1:=length(left);
  944. s2:=length(right);
  945. if s1<s2 then
  946. max:=s1
  947. else
  948. max:=s2;
  949. for i:=1 to max do
  950. begin
  951. d:=byte(left[i])-byte(right[i]);
  952. if d>0 then
  953. exit(1)
  954. else if d<0 then
  955. exit(-1);
  956. end;
  957. if s1>s2 then
  958. exit(1)
  959. else if s1<s2 then
  960. exit(-1)
  961. else
  962. exit(0);
  963. end;
  964. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  965. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  966. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  967. begin
  968. Result := longint(left[0]) - longint(right[0]);
  969. if Result = 0 then
  970. Result := CompareByte(left[1],right[1], longint(left[0]));
  971. end;
  972. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  973. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  974. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  975. var
  976. l : longint;
  977. s: shortstring;
  978. begin
  979. if p=nil then
  980. l:=0
  981. else
  982. l:=strlen(p);
  983. if l>high(res) then
  984. l:=high(res);
  985. if l>0 then
  986. move(p^,s[1],l);
  987. s[0]:=chr(l);
  988. res:=s;
  989. end;
  990. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  991. {$ifndef cpujvm}
  992. { also define alias which can be used inside the system unit }
  993. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  994. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  995. begin
  996. fpc_pchar_to_shortstr(result,p);
  997. end;
  998. {$endif not cpujvm}
  999. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1000. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1001. var
  1002. l: longint;
  1003. index: longint;
  1004. len: byte;
  1005. begin
  1006. l:=high(arr)+1;
  1007. if l>=high(res)+1 then
  1008. l:=high(res)
  1009. else if l<0 then
  1010. l:=0;
  1011. if zerobased then
  1012. begin
  1013. index:=IndexByte(arr[0],l,0);
  1014. if index<0 then
  1015. len:=l
  1016. else
  1017. len:=index;
  1018. end
  1019. else
  1020. len:=l;
  1021. move(arr[0],res[1],len);
  1022. res[0]:=chr(len);
  1023. end;
  1024. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1025. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1026. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1027. var
  1028. len: longint;
  1029. begin
  1030. len := length(src);
  1031. if len > length(res) then
  1032. len := length(res);
  1033. {$push}{$r-}
  1034. { make sure we don't access char 1 if length is 0 (JM) }
  1035. if len > 0 then
  1036. move(src[1],res[0],len);
  1037. fillchar(res[len],length(res)-len,0);
  1038. {$pop}
  1039. end;
  1040. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1041. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1042. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1043. var i : sizeint;
  1044. begin
  1045. i:=0;
  1046. if assigned(p) then
  1047. while p[i]<>#0 do
  1048. inc(i);
  1049. exit(i);
  1050. end;
  1051. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1052. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1053. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1054. var i : sizeint;
  1055. begin
  1056. i:=0;
  1057. if assigned(p) then
  1058. while p[i]<>#0 do
  1059. inc(i);
  1060. exit(i);
  1061. end;
  1062. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1063. {****************************************************************************
  1064. Caller/StackFrame Helpers
  1065. ****************************************************************************}
  1066. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1067. {_$error Get_frame must be defined for each processor }
  1068. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1069. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1070. {_$error Get_caller_addr must be defined for each processor }
  1071. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1072. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1073. {_$error Get_caller_frame must be defined for each processor }
  1074. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1075. {****************************************************************************
  1076. Math
  1077. ****************************************************************************}
  1078. {****************************************************************************
  1079. Software multiplication
  1080. ****************************************************************************}
  1081. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1082. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1083. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1084. var
  1085. sign : boolean;
  1086. q1,q2,q3 : word;
  1087. begin
  1088. { there's no difference between signed and unsigned multiplication,
  1089. when the destination size is equal to the source size and overflow
  1090. checking is off }
  1091. if not checkoverflow then
  1092. { word(f1)*word(f2) is coded as a call to mulword }
  1093. fpc_mul_integer:=integer(word(f1)*word(f2))
  1094. else
  1095. begin
  1096. sign:=false;
  1097. if f1<0 then
  1098. begin
  1099. sign:=not(sign);
  1100. q1:=word(-f1);
  1101. end
  1102. else
  1103. q1:=f1;
  1104. if f2<0 then
  1105. begin
  1106. sign:=not(sign);
  1107. q2:=word(-f2);
  1108. end
  1109. else
  1110. q2:=f2;
  1111. { the q1*q2 is coded as call to mulword }
  1112. q3:=q1*q2;
  1113. if (q1 <> 0) and (q2 <>0) and
  1114. ((q1>q3) or (q2>q3) or
  1115. { the bit 63 can be only set if we have $8000 }
  1116. { and sign is true }
  1117. (q3 shr 15<>0) and
  1118. ((q3<>word(word(1) shl 15)) or not(sign))
  1119. ) then
  1120. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1121. if sign then
  1122. fpc_mul_integer:=-q3
  1123. else
  1124. fpc_mul_integer:=q3;
  1125. end;
  1126. end;
  1127. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1128. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1129. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1130. var
  1131. _f1,bitpos : word;
  1132. b : byte;
  1133. f1overflowed : boolean;
  1134. begin
  1135. fpc_mul_word:=0;
  1136. bitpos:=1;
  1137. f1overflowed:=false;
  1138. for b:=0 to 15 do
  1139. begin
  1140. if (f2 and bitpos)<>0 then
  1141. begin
  1142. _f1:=fpc_mul_word;
  1143. fpc_mul_word:=fpc_mul_word+f1;
  1144. { if one of the operands is greater than the result an
  1145. overflow occurs }
  1146. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1147. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1148. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1149. end;
  1150. { when bootstrapping, we forget about overflow checking for qword :) }
  1151. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1152. f1:=f1 shl 1;
  1153. bitpos:=bitpos shl 1;
  1154. end;
  1155. end;
  1156. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1157. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1158. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1159. var
  1160. sign : boolean;
  1161. q1,q2,q3 : dword;
  1162. begin
  1163. { there's no difference between signed and unsigned multiplication,
  1164. when the destination size is equal to the source size and overflow
  1165. checking is off }
  1166. if not checkoverflow then
  1167. { dword(f1)*dword(f2) is coded as a call to muldword }
  1168. fpc_mul_longint:=longint(dword(f1)*dword(f2))
  1169. else
  1170. begin
  1171. sign:=false;
  1172. if f1<0 then
  1173. begin
  1174. sign:=not(sign);
  1175. q1:=dword(-f1);
  1176. end
  1177. else
  1178. q1:=f1;
  1179. if f2<0 then
  1180. begin
  1181. sign:=not(sign);
  1182. q2:=dword(-f2);
  1183. end
  1184. else
  1185. q2:=f2;
  1186. { the q1*q2 is coded as call to muldword }
  1187. q3:=q1*q2;
  1188. if (q1 <> 0) and (q2 <>0) and
  1189. ((q1>q3) or (q2>q3) or
  1190. { the bit 31 can be only set if we have $8000 0000 }
  1191. { and sign is true }
  1192. (q3 shr 15<>0) and
  1193. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1194. ) then
  1195. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1196. if sign then
  1197. fpc_mul_longint:=-q3
  1198. else
  1199. fpc_mul_longint:=q3;
  1200. end;
  1201. end;
  1202. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1203. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1204. { multiplies two dwords
  1205. the longbool for checkoverflow avoids a misaligned stack
  1206. }
  1207. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1208. var
  1209. _f1,bitpos : dword;
  1210. b : byte;
  1211. f1overflowed : boolean;
  1212. begin
  1213. fpc_mul_dword:=0;
  1214. bitpos:=1;
  1215. f1overflowed:=false;
  1216. for b:=0 to 31 do
  1217. begin
  1218. if (f2 and bitpos)<>0 then
  1219. begin
  1220. _f1:=fpc_mul_dword;
  1221. fpc_mul_dword:=fpc_mul_dword+f1;
  1222. { if one of the operands is greater than the result an
  1223. overflow occurs }
  1224. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1225. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1226. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1227. end;
  1228. { when bootstrapping, we forget about overflow checking for qword :) }
  1229. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1230. f1:=f1 shl 1;
  1231. bitpos:=bitpos shl 1;
  1232. end;
  1233. end;
  1234. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1235. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1236. {****************************************************************************
  1237. Software longint/dword division
  1238. ****************************************************************************}
  1239. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1240. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1241. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1242. var
  1243. shift,lzz,lzn : ObjpasInt;
  1244. begin
  1245. result:=0;
  1246. if n=0 then
  1247. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1248. if z=0 then
  1249. exit;
  1250. lzz:=BsrDWord(z);
  1251. lzn:=BsrDWord(n);
  1252. { if the denominator contains less zeros
  1253. then the numerator
  1254. then d is greater than the n }
  1255. if lzn>lzz then
  1256. exit;
  1257. shift:=lzz-lzn;
  1258. n:=n shl shift;
  1259. for shift:=shift downto 0 do
  1260. begin
  1261. if z>=n then
  1262. begin
  1263. z:=z-n;
  1264. result:=result+dword(dword(1) shl shift);
  1265. end;
  1266. n:=n shr 1;
  1267. end;
  1268. end;
  1269. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1270. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1271. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1272. var
  1273. shift,lzz,lzn : ObjpasInt;
  1274. begin
  1275. result:=0;
  1276. if n=0 then
  1277. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1278. if z=0 then
  1279. exit;
  1280. lzz:=BsrDWord(z);
  1281. lzn:=BsrDWord(n);
  1282. { if the denominator contains less zeros
  1283. then the numerator
  1284. then d is greater than the n }
  1285. if lzn>lzz then
  1286. begin
  1287. result:=z;
  1288. exit;
  1289. end;
  1290. shift:=lzz-lzn;
  1291. n:=n shl shift;
  1292. for shift:=shift downto 0 do
  1293. begin
  1294. if z>=n then
  1295. z:=z-n;
  1296. n:=n shr 1;
  1297. end;
  1298. result:=z;
  1299. end;
  1300. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1301. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1302. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1303. var
  1304. sign : boolean;
  1305. d1,d2 : dword;
  1306. begin
  1307. if n=0 then
  1308. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1309. sign:=false;
  1310. if z<0 then
  1311. begin
  1312. sign:=not(sign);
  1313. d1:=dword(-z);
  1314. end
  1315. else
  1316. d1:=z;
  1317. if n<0 then
  1318. begin
  1319. sign:=not(sign);
  1320. d2:=dword(-n);
  1321. end
  1322. else
  1323. d2:=n;
  1324. { the div is coded by the compiler as call to divdword }
  1325. if sign then
  1326. result:=-(d1 div d2)
  1327. else
  1328. result:=d1 div d2;
  1329. end;
  1330. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1331. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1332. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1333. var
  1334. signed : boolean;
  1335. r,nq,zq : dword;
  1336. begin
  1337. if n=0 then
  1338. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1339. nq:=abs(n);
  1340. if z<0 then
  1341. begin
  1342. zq:=dword(-z);
  1343. signed:=true;
  1344. end
  1345. else
  1346. begin
  1347. zq:=z;
  1348. signed:=false;
  1349. end;
  1350. r:=zq mod nq;
  1351. if signed then
  1352. result:=-longint(r)
  1353. else
  1354. result:=r;
  1355. end;
  1356. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1357. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1358. {****************************************************************************}
  1359. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1360. { This is only needed to bootstrap on SPARC targets
  1361. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  1362. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1363. begin
  1364. if l<0 then
  1365. abs:=-l
  1366. else
  1367. abs:=l;
  1368. end;
  1369. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1370. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1371. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1372. begin
  1373. odd:=boolean(l and 1);
  1374. end;
  1375. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1376. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1377. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1378. begin
  1379. odd:=boolean(l and 1);
  1380. end;
  1381. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1382. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1383. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1384. begin
  1385. odd:=boolean(longint(l) and 1);
  1386. end;
  1387. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1388. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1389. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1390. begin
  1391. odd:=boolean(longint(l) and 1);
  1392. end;
  1393. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1394. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1395. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1396. begin
  1397. sqr:=l*l;
  1398. end;
  1399. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1400. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1401. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1402. begin
  1403. if l < 0 then
  1404. abs := -l
  1405. else
  1406. abs := l;
  1407. end;
  1408. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1409. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1410. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1411. begin
  1412. sqr := l*l;
  1413. end;
  1414. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1415. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1416. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1417. begin
  1418. sqr := l*l;
  1419. end;
  1420. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1421. {$ifdef CPU16}
  1422. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1423. function declocked(var l:smallint):boolean;
  1424. begin
  1425. Dec(l);
  1426. declocked:=(l=0);
  1427. end;
  1428. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1429. {$endif CPU16}
  1430. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1431. function declocked(var l:longint):boolean;
  1432. begin
  1433. Dec(l);
  1434. declocked:=(l=0);
  1435. end;
  1436. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1437. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1438. function declocked(var l:int64):boolean;
  1439. begin
  1440. Dec(l);
  1441. declocked:=(l=0);
  1442. end;
  1443. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1444. {$ifdef CPU16}
  1445. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1446. procedure inclocked(var l:smallint);
  1447. begin
  1448. Inc(l);
  1449. end;
  1450. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1451. {$endif CPU16}
  1452. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1453. procedure inclocked(var l:longint);
  1454. begin
  1455. Inc(l);
  1456. end;
  1457. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1458. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1459. procedure inclocked(var l:int64);
  1460. begin
  1461. Inc(l);
  1462. end;
  1463. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1464. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1465. {_$error Sptr must be defined for each processor }
  1466. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1467. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1468. var
  1469. tmp: PtrUInt;
  1470. begin
  1471. tmp:=addr+(alignment-1);
  1472. result:=tmp-(tmp mod alignment)
  1473. end;
  1474. {$ifndef cpujvm}
  1475. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1476. var
  1477. tmp: PtrUInt;
  1478. begin
  1479. tmp:=PtrUInt(addr)+(alignment-1);
  1480. result:=pointer(tmp-(tmp mod alignment));
  1481. end;
  1482. {$endif}
  1483. {****************************************************************************
  1484. Str()
  1485. ****************************************************************************}
  1486. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1487. procedure int_str(l:longint;out s:shortstring);
  1488. var
  1489. m,m1 : longword;
  1490. pcstart,
  1491. pc2start,
  1492. pc,pc2 : pchar;
  1493. hs : string[32];
  1494. overflow : longint;
  1495. begin
  1496. pc2start:=@s[1];
  1497. pc2:=pc2start;
  1498. if (l<0) then
  1499. begin
  1500. pc2^:='-';
  1501. inc(pc2);
  1502. m:=longword(-l);
  1503. end
  1504. else
  1505. m:=longword(l);
  1506. pcstart:=pchar(@hs[0]);
  1507. pc:=pcstart;
  1508. repeat
  1509. m1:=m div 10;
  1510. inc(pc);
  1511. pc^:=char(m-(m1*10)+byte('0'));
  1512. m:=m1;
  1513. until m=0;
  1514. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1515. if overflow>0 then
  1516. inc(pcstart,overflow);
  1517. while (pc>pcstart) do
  1518. begin
  1519. pc2^:=pc^;
  1520. inc(pc2);
  1521. dec(pc);
  1522. end;
  1523. s[0]:=char(pc2-pc2start);
  1524. end;
  1525. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1526. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1527. procedure int_str_unsigned(l:longword;out s:shortstring);
  1528. var
  1529. m1 : longword;
  1530. pcstart,
  1531. pc2start,
  1532. pc,pc2 : pchar;
  1533. hs : string[32];
  1534. overflow : longint;
  1535. begin
  1536. pc2start:=@s[1];
  1537. pc2:=pc2start;
  1538. pcstart:=pchar(@hs[0]);
  1539. pc:=pcstart;
  1540. repeat
  1541. inc(pc);
  1542. m1:=l div 10;
  1543. pc^:=char(l-(m1*10)+byte('0'));
  1544. l:=m1;
  1545. until l=0;
  1546. overflow:=(pc-pcstart)-high(s);
  1547. if overflow>0 then
  1548. inc(pcstart,overflow);
  1549. while (pc>pcstart) do
  1550. begin
  1551. pc2^:=pc^;
  1552. inc(pc2);
  1553. dec(pc);
  1554. end;
  1555. s[0]:=char(pc2-pc2start);
  1556. end;
  1557. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1558. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1559. procedure int_str(l:int64;out s:shortstring);
  1560. var
  1561. m,m1 : qword;
  1562. pcstart,
  1563. pc2start,
  1564. pc,pc2 : pchar;
  1565. hs : string[32];
  1566. overflow : longint;
  1567. begin
  1568. pc2start:=@s[1];
  1569. pc2:=pc2start;
  1570. if (l<0) then
  1571. begin
  1572. pc2^:='-';
  1573. inc(pc2);
  1574. m:=qword(-l);
  1575. end
  1576. else
  1577. m:=qword(l);
  1578. pcstart:=pchar(@hs[0]);
  1579. pc:=pcstart;
  1580. repeat
  1581. m1:=m div 10;
  1582. inc(pc);
  1583. pc^:=char(m-(m1*10)+byte('0'));
  1584. m:=m1;
  1585. until m=0;
  1586. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1587. if overflow>0 then
  1588. inc(pcstart,overflow);
  1589. while (pc>pcstart) do
  1590. begin
  1591. pc2^:=pc^;
  1592. inc(pc2);
  1593. dec(pc);
  1594. end;
  1595. s[0]:=char(pc2-pc2start);
  1596. end;
  1597. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1598. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1599. procedure int_str_unsigned(l:qword;out s:shortstring);
  1600. var
  1601. m1 : qword;
  1602. pcstart,
  1603. pc2start,
  1604. pc,pc2 : pchar;
  1605. hs : string[64];
  1606. overflow : longint;
  1607. begin
  1608. pc2start:=@s[1];
  1609. pc2:=pc2start;
  1610. pcstart:=pchar(@hs[0]);
  1611. pc:=pcstart;
  1612. repeat
  1613. inc(pc);
  1614. m1:=l div 10;
  1615. pc^:=char(l-(m1*10)+byte('0'));
  1616. l:=m1;
  1617. until l=0;
  1618. overflow:=(pc-pcstart)-high(s);
  1619. if overflow>0 then
  1620. inc(pcstart,overflow);
  1621. while (pc>pcstart) do
  1622. begin
  1623. pc2^:=pc^;
  1624. inc(pc2);
  1625. dec(pc);
  1626. end;
  1627. s[0]:=char(pc2-pc2start);
  1628. end;
  1629. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1630. {$ifndef FPUNONE}
  1631. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1632. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1633. begin
  1634. softfloat_exception_flags:=[];
  1635. end;
  1636. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1637. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1638. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1639. begin
  1640. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  1641. end;
  1642. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1643. {$endif}
  1644. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1645. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1646. begin
  1647. { the extra Word type cast is necessary because the "AValue shr 8" }
  1648. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1649. { the sign bits from the upper 16 bits are shifted in rather than }
  1650. { zeroes. }
  1651. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1652. end;
  1653. {$ifndef cpujvm}
  1654. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1655. begin
  1656. Result := Word((AValue shr 8) or (AValue shl 8));
  1657. end;
  1658. {$endif}
  1659. function SwapEndian(const AValue: LongInt): LongInt;
  1660. begin
  1661. Result := (AValue shl 24)
  1662. or ((AValue and $0000FF00) shl 8)
  1663. or ((AValue and $00FF0000) shr 8)
  1664. or (AValue shr 24);
  1665. end;
  1666. {$ifndef cpujvm}
  1667. function SwapEndian(const AValue: DWord): DWord;
  1668. begin
  1669. Result := (AValue shl 24)
  1670. or ((AValue and $0000FF00) shl 8)
  1671. or ((AValue and $00FF0000) shr 8)
  1672. or (AValue shr 24);
  1673. end;
  1674. {$endif}
  1675. function SwapEndian(const AValue: Int64): Int64;
  1676. begin
  1677. Result := (AValue shl 56)
  1678. or ((AValue and $000000000000FF00) shl 40)
  1679. or ((AValue and $0000000000FF0000) shl 24)
  1680. or ((AValue and $00000000FF000000) shl 8)
  1681. or ((AValue and $000000FF00000000) shr 8)
  1682. or ((AValue and $0000FF0000000000) shr 24)
  1683. or ((AValue and $00FF000000000000) shr 40)
  1684. or (AValue shr 56);
  1685. end;
  1686. {$ifndef cpujvm}
  1687. function SwapEndian(const AValue: QWord): QWord;
  1688. begin
  1689. Result := (AValue shl 56)
  1690. or ((AValue and $000000000000FF00) shl 40)
  1691. or ((AValue and $0000000000FF0000) shl 24)
  1692. or ((AValue and $00000000FF000000) shl 8)
  1693. or ((AValue and $000000FF00000000) shr 8)
  1694. or ((AValue and $0000FF0000000000) shr 24)
  1695. or ((AValue and $00FF000000000000) shr 40)
  1696. or (AValue shr 56);
  1697. end;
  1698. {$endif}
  1699. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1700. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1701. begin
  1702. {$IFDEF ENDIAN_BIG}
  1703. Result := AValue;
  1704. {$ELSE}
  1705. Result := SwapEndian(AValue);
  1706. {$ENDIF}
  1707. end;
  1708. {$ifndef cpujvm}
  1709. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1710. begin
  1711. {$IFDEF ENDIAN_BIG}
  1712. Result := AValue;
  1713. {$ELSE}
  1714. Result := SwapEndian(AValue);
  1715. {$ENDIF}
  1716. end;
  1717. {$endif not cpujvm}
  1718. function BEtoN(const AValue: LongInt): LongInt;{$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: DWord): DWord;{$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: Int64): Int64;{$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: QWord): QWord;{$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 LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1755. begin
  1756. {$IFDEF ENDIAN_LITTLE}
  1757. Result := AValue;
  1758. {$ELSE}
  1759. Result := SwapEndian(AValue);
  1760. {$ENDIF}
  1761. end;
  1762. {$ifndef cpujvm}
  1763. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1764. begin
  1765. {$IFDEF ENDIAN_LITTLE}
  1766. Result := AValue;
  1767. {$ELSE}
  1768. Result := SwapEndian(AValue);
  1769. {$ENDIF}
  1770. end;
  1771. {$endif not cpujvm}
  1772. function LEtoN(const AValue: LongInt): LongInt;{$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: DWord): DWord;{$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: Int64): Int64;{$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: QWord): QWord;{$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 NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1809. begin
  1810. {$IFDEF ENDIAN_BIG}
  1811. Result := AValue;
  1812. {$ELSE}
  1813. Result := SwapEndian(AValue);
  1814. {$ENDIF}
  1815. end;
  1816. {$ifndef cpujvm}
  1817. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1818. begin
  1819. {$IFDEF ENDIAN_BIG}
  1820. Result := AValue;
  1821. {$ELSE}
  1822. Result := SwapEndian(AValue);
  1823. {$ENDIF}
  1824. end;
  1825. {$endif not cpujvm}
  1826. function NtoBE(const AValue: LongInt): LongInt;{$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: DWord): DWord;{$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: Int64): Int64;{$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: QWord): QWord;{$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 NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1863. begin
  1864. {$IFDEF ENDIAN_LITTLE}
  1865. Result := AValue;
  1866. {$ELSE}
  1867. Result := SwapEndian(AValue);
  1868. {$ENDIF}
  1869. end;
  1870. {$ifndef cpujvm}
  1871. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1872. begin
  1873. {$IFDEF ENDIAN_LITTLE}
  1874. Result := AValue;
  1875. {$ELSE}
  1876. Result := SwapEndian(AValue);
  1877. {$ENDIF}
  1878. end;
  1879. {$endif not cpujvm}
  1880. function NtoLE(const AValue: LongInt): LongInt;{$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: DWord): DWord;{$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: Int64): Int64;{$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: QWord): QWord;{$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. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1917. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1918. begin
  1919. end;
  1920. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1921. begin
  1922. end;
  1923. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1924. begin
  1925. end;
  1926. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1927. begin
  1928. end;
  1929. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  1930. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  1931. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  1932. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1933. begin
  1934. Result:=(AValue shr 1) or (AValue shl 7);
  1935. end;
  1936. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1937. begin
  1938. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  1939. end;
  1940. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1941. begin
  1942. Result:=(AValue shl 1) or (AValue shr 7);
  1943. end;
  1944. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1945. begin
  1946. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  1947. end;
  1948. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  1949. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  1950. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  1951. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  1952. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1953. begin
  1954. Result:=(AValue shr 1) or (AValue shl 15);
  1955. end;
  1956. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1957. begin
  1958. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  1959. end;
  1960. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1961. begin
  1962. Result:=(AValue shl 1) or (AValue shr 15);
  1963. end;
  1964. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1965. begin
  1966. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  1967. end;
  1968. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  1969. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  1970. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  1971. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  1972. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1973. begin
  1974. Result:=(AValue shr 1) or (AValue shl 31);
  1975. end;
  1976. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1977. begin
  1978. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  1979. end;
  1980. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1981. begin
  1982. Result:=(AValue shl 1) or (AValue shr 31);
  1983. end;
  1984. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1985. begin
  1986. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  1987. end;
  1988. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  1989. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  1990. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  1991. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  1992. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1993. begin
  1994. Result:=(AValue shr 1) or (AValue shl 63);
  1995. end;
  1996. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1997. begin
  1998. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  1999. end;
  2000. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2001. begin
  2002. Result:=(AValue shl 1) or (AValue shr 63);
  2003. end;
  2004. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2005. begin
  2006. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2007. end;
  2008. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2009. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2010. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2011. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2012. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2013. begin
  2014. 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)))));
  2015. end;
  2016. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2017. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2018. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2019. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2020. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2021. begin
  2022. 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)))));
  2023. end;
  2024. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2025. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2026. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2027. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2028. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2029. begin
  2030. 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)))));
  2031. end;
  2032. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2033. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2034. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2035. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2036. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2037. begin
  2038. 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)))));
  2039. end;
  2040. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2041. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2042. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2043. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2044. function BsfByte(Const AValue: Byte): Byte;
  2045. const bsf8bit: array [Byte] of Byte = (
  2046. $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,
  2047. 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,
  2048. 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,
  2049. 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,
  2050. 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,
  2051. 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,
  2052. 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,
  2053. 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
  2054. );
  2055. begin
  2056. result:=bsf8bit[AValue];
  2057. end;
  2058. {$endif}
  2059. {$endif}
  2060. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2061. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2062. function BsrByte(Const AValue: Byte): Byte;
  2063. const bsr8bit: array [Byte] of Byte = (
  2064. $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,
  2065. 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,
  2066. 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,
  2067. 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,
  2068. 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,
  2069. 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,
  2070. 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,
  2071. 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
  2072. );
  2073. begin
  2074. result:=bsr8bit[AValue];
  2075. end;
  2076. {$endif}
  2077. {$endif}
  2078. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2079. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2080. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2081. begin
  2082. result:=$ff;
  2083. if lo(AValue)<>0 then
  2084. result:=BsfByte(lo(AValue))
  2085. else if hi(AValue) <> 0 then
  2086. result:=BsfByte(hi(AValue))+8
  2087. end;
  2088. {$endif}
  2089. {$endif}
  2090. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2091. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2092. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2093. begin
  2094. if hi(AValue)<>0 then
  2095. result:=BsrByte(hi(AValue))+8
  2096. else
  2097. result:=BsrByte(lo(AValue))
  2098. end;
  2099. {$endif}
  2100. {$endif}
  2101. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2102. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2103. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2104. begin
  2105. result:=$ff;
  2106. if lo(AValue)<>0 then
  2107. result:=BsfWord(lo(AValue))
  2108. else if hi(AValue) <> 0 then
  2109. result:=BsfWord(hi(AValue))+16
  2110. end;
  2111. {$endif}
  2112. {$endif}
  2113. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2114. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2115. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2116. begin
  2117. if hi(AValue)<>0 then
  2118. result:=BsrWord(hi(AValue))+16
  2119. else
  2120. result:=BsrWord(lo(AValue))
  2121. end;
  2122. {$endif}
  2123. {$endif}
  2124. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2125. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2126. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2127. begin
  2128. result:=$ff;
  2129. if lo(AValue) <> 0 then
  2130. result:=BsfDWord(lo(AValue))
  2131. else if hi(AValue) <> 0 then
  2132. result:=BsfDWord(hi(AValue)) + 32;
  2133. end;
  2134. {$endif}
  2135. {$endif}
  2136. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2137. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2138. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2139. begin
  2140. if hi(AValue) <> 0 then
  2141. result:=BsrDWord(hi(AValue)) + 32
  2142. else
  2143. result:=BsrDWord(lo(AValue))
  2144. end;
  2145. {$endif}
  2146. {$endif}
  2147. const
  2148. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2149. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2150. begin
  2151. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2152. end;
  2153. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2154. var
  2155. i : SizeInt;
  2156. begin
  2157. Result:=0;
  2158. for i:=0 to 3 do
  2159. begin
  2160. inc(Result,PopCntData[AValue and $f]);
  2161. AValue:=AValue shr 4;
  2162. end;
  2163. end;
  2164. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2165. var
  2166. i : SizeInt;
  2167. begin
  2168. Result:=0;
  2169. for i:=0 to 7 do
  2170. begin
  2171. inc(Result,PopCntData[AValue and $f]);
  2172. AValue:=AValue shr 4;
  2173. end;
  2174. end;
  2175. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2176. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2177. {$ifdef VER2_6}
  2178. var
  2179. i : SizeInt;
  2180. begin
  2181. Result:=0;
  2182. for i:=0 to 15 do
  2183. begin
  2184. inc(Result,PopCntData[AValue and $f]);
  2185. AValue:=AValue shr 4;
  2186. end;
  2187. {$else VER2_6}
  2188. begin
  2189. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2190. {$endif VER2_6}
  2191. end;
  2192. {$endif}