generic.inc 62 KB

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