generic.inc 63 KB

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