generic.inc 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  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 : ALUUInt;
  124. begin
  125. if count <= 0 then
  126. exit;
  127. pdest:=@x;
  128. if Count>4*sizeof(ptruint)-1 then
  129. begin
  130. {$if sizeof(v)>=2}
  131. v:=(value shl 8) or value;
  132. {$endif sizeof(v)>=2}
  133. {$if sizeof(v)>=4}
  134. v:=(v shl 16) or v;
  135. {$endif sizeof(v)>=4}
  136. {$if sizeof(v)=8}
  137. v:=(v shl 32) or v;
  138. {$endif sizeof(v)=8}
  139. { Align on native pointer size }
  140. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  141. dec(count,pend-pdest);
  142. while pdest<pend do
  143. begin
  144. pdest^:=value;
  145. inc(pdest);
  146. end;
  147. { use sizeuint typecast to force shr optimization }
  148. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  149. while pdest<pend do
  150. begin
  151. pptruint(pdest)^:=v;
  152. inc(pptruint(pdest));
  153. end;
  154. count:=count and (sizeof(ptruint)-1);
  155. end;
  156. pend:=pdest+count;
  157. while pdest<pend do
  158. begin
  159. pdest^:=value;
  160. inc(pdest);
  161. end;
  162. end;
  163. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  164. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  165. procedure fillword(var x;count : SizeInt;value : word);
  166. var
  167. aligncount : sizeint;
  168. pdest,pend : pword;
  169. v : ALUUInt;
  170. begin
  171. if count <= 0 then
  172. exit;
  173. pdest:=@x;
  174. if Count>4*sizeof(ptruint)-1 then
  175. begin
  176. {$if sizeof(v)>=4}
  177. v:=(value shl 16) or value;
  178. {$endif sizeof(v)>=4}
  179. {$if sizeof(v)=8}
  180. v:=(v shl 32) or v;
  181. {$endif sizeof(v)=8}
  182. { Align on native pointer size }
  183. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  184. dec(count,aligncount);
  185. pend:=pdest+aligncount;
  186. while pdest<pend do
  187. begin
  188. pdest^:=value;
  189. inc(pdest);
  190. end;
  191. { use sizeuint typecast to force shr optimization }
  192. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  193. while pdest<pend do
  194. begin
  195. pptruint(pdest)^:=v;
  196. inc(pptruint(pdest));
  197. end;
  198. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  199. end;
  200. pend:=pdest+count;
  201. while pdest<pend do
  202. begin
  203. pdest^:=value;
  204. inc(pdest);
  205. end;
  206. end;
  207. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  208. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  209. procedure filldword(var x;count : SizeInt;value : dword);
  210. var
  211. aligncount : sizeint;
  212. pdest,pend : pdword;
  213. v : ALUUInt;
  214. begin
  215. if count <= 0 then
  216. exit;
  217. pdest:=@x;
  218. if Count>4*sizeof(ptruint)-1 then
  219. begin
  220. v:=value;
  221. {$if sizeof(v)=8}
  222. v:=(v shl 32) or v;
  223. {$endif sizeof(v)=8}
  224. { Align on native pointer size }
  225. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  226. dec(count,aligncount);
  227. pend:=pdest+aligncount;
  228. while pdest<pend do
  229. begin
  230. pdest^:=value;
  231. inc(pdest);
  232. end;
  233. { use sizeuint typecast to force shr optimization }
  234. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  235. while pdest<pend do
  236. begin
  237. pptruint(pdest)^:=v;
  238. inc(pptruint(pdest));
  239. end;
  240. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  241. end;
  242. pend:=pdest+count;
  243. while pdest<pend do
  244. begin
  245. pdest^:=value;
  246. inc(pdest);
  247. end;
  248. end;
  249. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  250. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  251. procedure fillqword(var x;count : SizeInt;value : qword);
  252. var
  253. pdest,pend : pqword;
  254. begin
  255. if count <= 0 then
  256. exit;
  257. pdest:=@x;
  258. pend:=pdest+count;
  259. while pdest<pend do
  260. begin
  261. pdest^:=value;
  262. inc(pdest);
  263. end;
  264. end;
  265. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  266. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  267. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  268. var
  269. psrc,pend : pbyte;
  270. begin
  271. psrc:=@buf;
  272. { simulate assembler implementations behaviour, which is expected }
  273. { fpc_pchar_to_ansistr in astrings.inc }
  274. if (len < 0) or
  275. (psrc+len < psrc) then
  276. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  277. else
  278. pend:=psrc+len;
  279. while (psrc<pend) do
  280. begin
  281. if psrc^=b then
  282. begin
  283. result:=psrc-pbyte(@buf);
  284. exit;
  285. end;
  286. inc(psrc);
  287. end;
  288. result:=-1;
  289. end;
  290. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  291. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  292. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  293. var
  294. psrc,pend : pword;
  295. begin
  296. psrc:=@buf;
  297. { simulate assembler implementations behaviour, which is expected }
  298. { fpc_pchar_to_ansistr in astrings.inc }
  299. if (len < 0) or
  300. { is this ever true? }
  301. (len > high(PtrInt)) or
  302. (psrc+len < psrc) then
  303. pend:=pword(high(PtrUInt)-sizeof(word))
  304. else
  305. pend:=psrc+len;
  306. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  307. if (ptruint(psrc) mod 2)<>0 then
  308. while psrc<pend do
  309. begin
  310. if unaligned(psrc^)=b then
  311. begin
  312. result:=psrc-pword(@buf);
  313. exit;
  314. end;
  315. inc(psrc);
  316. end
  317. else
  318. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  319. while psrc<pend do
  320. begin
  321. if psrc^=b then
  322. begin
  323. result:=psrc-pword(@buf);
  324. exit;
  325. end;
  326. inc(psrc);
  327. end;
  328. result:=-1;
  329. end;
  330. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  331. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  332. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  333. var
  334. psrc,pend : pdword;
  335. begin
  336. psrc:=@buf;
  337. { simulate assembler implementations behaviour, which is expected }
  338. { fpc_pchar_to_ansistr in astrings.inc }
  339. if (len < 0) or
  340. (len > high(PtrInt) div 2) or
  341. (psrc+len < psrc) then
  342. pend:=pdword(high(PtrUInt)-PtrUInt(sizeof(dword)))
  343. else
  344. pend:=psrc+len;
  345. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  346. if (ptruint(psrc) mod 4)<>0 then
  347. while psrc<pend do
  348. begin
  349. if unaligned(psrc^)=b then
  350. begin
  351. result:=psrc-pdword(@buf);
  352. exit;
  353. end;
  354. inc(psrc);
  355. end
  356. else
  357. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  358. while psrc<pend do
  359. begin
  360. if psrc^=b then
  361. begin
  362. result:=psrc-pdword(@buf);
  363. exit;
  364. end;
  365. inc(psrc);
  366. end;
  367. result:=-1;
  368. end;
  369. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  370. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  371. function IndexQWord(Const buf;len:SizeInt;b:QWord):SizeInt;
  372. var
  373. psrc,pend : pqword;
  374. begin
  375. psrc:=@buf;
  376. { simulate assembler implementations behaviour, which is expected }
  377. { fpc_pchar_to_ansistr in astrings.inc }
  378. if (len < 0) or
  379. (len > high(PtrInt) div 4) or
  380. (psrc+len < psrc) then
  381. pend:=pqword(high(PtrUInt)-PtrUInt(sizeof(qword)))
  382. else
  383. pend:=psrc+len;
  384. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  385. if (ptruint(psrc) mod 8)<>0 then
  386. while psrc<pend do
  387. begin
  388. if unaligned(psrc^)=b then
  389. begin
  390. result:=psrc-pqword(@buf);
  391. exit;
  392. end;
  393. inc(psrc);
  394. end
  395. else
  396. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  397. while psrc<pend do
  398. begin
  399. if psrc^=b then
  400. begin
  401. result:=psrc-pqword(@buf);
  402. exit;
  403. end;
  404. inc(psrc);
  405. end;
  406. result:=-1;
  407. end;
  408. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  409. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  410. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  411. var
  412. aligncount : sizeint;
  413. psrc,pdest,pend : pbyte;
  414. b : ptrint;
  415. begin
  416. b:=0;
  417. psrc:=@buf1;
  418. pdest:=@buf2;
  419. if (len>4*sizeof(ptruint)-1)
  420. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  421. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  422. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  423. then
  424. begin
  425. { Align on native pointer size }
  426. aligncount:=(sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1);
  427. dec(len,aligncount);
  428. pend:=psrc+aligncount;
  429. while psrc<pend do
  430. begin
  431. b:=(ptrint(psrc^)-ptrint(pdest^));
  432. if b<>0 then
  433. begin
  434. if b<0 then
  435. exit(-1)
  436. else
  437. exit(1);
  438. end;
  439. inc(pdest);
  440. inc(psrc);
  441. end;
  442. { use sizeuint typecast to force shr optimization }
  443. pptruint(pend):=pptruint(psrc)+(sizeuint(len) div sizeof(ptruint));
  444. len:=len and (sizeof(PtrUInt)-1);
  445. while psrc<pend do
  446. begin
  447. b:=(pptrint(psrc)^-pptrint(pdest)^);
  448. if b<>0 then
  449. begin
  450. len:=sizeof(ptruint);
  451. break;
  452. end;
  453. inc(pptruint(pdest));
  454. inc(pptruint(psrc));
  455. end;
  456. end;
  457. if (psrc+len >= psrc) then
  458. pend:=psrc+len
  459. else
  460. pend:=pbyte(high(ptruint)-1);
  461. while psrc<pend do
  462. begin
  463. b:=(ptrint(psrc^)-ptrint(pdest^));
  464. if b<>0 then
  465. begin
  466. if b<0 then
  467. exit(-1)
  468. else
  469. exit(1);
  470. end;
  471. inc(pdest);
  472. inc(psrc);
  473. end;
  474. result:=0;
  475. end;
  476. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  477. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  478. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  479. var
  480. aligncount : sizeint;
  481. psrc,pdest,pend : pword;
  482. b : ptrint;
  483. begin
  484. b:=0;
  485. psrc:=@buf1;
  486. pdest:=@buf2;
  487. if (len>4*sizeof(ptruint)-1)
  488. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  489. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  490. and (((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))=0)
  491. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  492. then
  493. begin
  494. { Align on native pointer size }
  495. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 1;
  496. dec(len,aligncount);
  497. pend:=psrc+aligncount;
  498. while psrc<pend do
  499. begin
  500. b:=(ptrint(psrc^)-ptrint(pdest^));
  501. if b<>0 then
  502. begin
  503. if b<0 then
  504. exit(-1)
  505. else
  506. exit(1);
  507. end;
  508. inc(pdest);
  509. inc(psrc);
  510. end;
  511. { use sizeuint typecast to force shr optimization }
  512. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*2 div sizeof(ptruint));
  513. len:=((len*2) and (sizeof(PtrUInt)-1)) shr 1;
  514. while psrc<pend do
  515. begin
  516. b:=(pptrint(psrc)^-pptrint(pdest)^);
  517. if b<>0 then
  518. begin
  519. len:=sizeof(ptruint) shr 1;
  520. break;
  521. end;
  522. inc(pptruint(pdest));
  523. inc(pptruint(psrc));
  524. end;
  525. end;
  526. if (psrc+len >= psrc) then
  527. pend:=psrc+len
  528. else
  529. pend:=pword(high(ptruint)-2);
  530. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  531. if ((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))<>0 then
  532. while psrc<pend do
  533. begin
  534. b:=(ptrint(unaligned(psrc^))-ptrint(unaligned(pdest^)));
  535. if b<>0 then
  536. begin
  537. if b<0 then
  538. exit(-1)
  539. else
  540. exit(1);
  541. end;
  542. inc(pdest);
  543. inc(psrc);
  544. end
  545. else
  546. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  547. while psrc<pend do
  548. begin
  549. b:=(ptrint(psrc^)-ptrint(pdest^));
  550. if b<>0 then
  551. begin
  552. if b<0 then
  553. exit(-1)
  554. else
  555. exit(1);
  556. end;
  557. inc(pdest);
  558. inc(psrc);
  559. end;
  560. result:=0;
  561. end;
  562. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  563. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  564. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  565. var
  566. aligncount : sizeint;
  567. psrc,pdest,pend : pdword;
  568. begin
  569. psrc:=@buf1;
  570. pdest:=@buf2;
  571. if (len>4*sizeof(ptruint)-11)
  572. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  573. and (((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))=0)
  574. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  575. then
  576. begin
  577. { Align on native pointer size }
  578. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  579. dec(len,aligncount);
  580. pend:=psrc+aligncount;
  581. while psrc<pend do
  582. begin
  583. if psrc^<>pdest^ then
  584. if psrc^>pdest^ then
  585. exit(1)
  586. else
  587. exit(-1);
  588. inc(pdest);
  589. inc(psrc);
  590. end;
  591. { use sizeuint typecast to force shr optimization }
  592. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  593. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  594. while psrc<pend do
  595. begin
  596. if pptrint(psrc)^<>pptrint(pdest)^ then
  597. begin
  598. len:=sizeof(ptruint) shr 2;
  599. break;
  600. end;
  601. inc(pptruint(pdest));
  602. inc(pptruint(psrc));
  603. end;
  604. end;
  605. if (len <= high(ptrint) div 2) and
  606. (psrc+len >= psrc) then
  607. pend:=psrc+len
  608. else
  609. pend:=pdword(high(ptruint)-4);
  610. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  611. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  612. while psrc<pend do
  613. begin
  614. if unaligned(psrc^)<>unaligned(pdest^) then
  615. if unaligned(psrc^)>unaligned(pdest^) then
  616. exit(1)
  617. else
  618. exit(-1);
  619. inc(pdest);
  620. inc(psrc);
  621. end
  622. else
  623. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  624. while psrc<pend do
  625. begin
  626. if psrc^<>pdest^ then
  627. if psrc^>pdest^ then
  628. exit(1)
  629. else
  630. exit(-1);
  631. inc(pdest);
  632. inc(psrc);
  633. end;
  634. result:=0;
  635. end;
  636. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  637. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  638. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  639. var
  640. I : SizeInt;
  641. begin
  642. if Len = 0 then
  643. exit;
  644. I:=IndexByte(Buf1,Len,0);
  645. if I<>-1 then
  646. Move(Buf1,Buf2,I)
  647. else
  648. Move(Buf1,Buf2,len);
  649. end;
  650. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  651. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  652. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  653. var
  654. psrc,pend : pbyte;
  655. begin
  656. psrc:=@buf;
  657. { simulate assembler implementations behaviour, which is expected }
  658. { fpc_pchar_to_ansistr in astrings.inc }
  659. if (len < 0) then
  660. pend:=pbyte(high(PtrUInt)-PtrUInt(sizeof(byte)))
  661. else
  662. pend:=psrc+len;
  663. while (psrc<pend) and (psrc^<>0) do
  664. begin
  665. if (psrc^=byte(b)) then
  666. begin
  667. result:=psrc-pbyte(@buf);
  668. exit;
  669. end;
  670. inc(psrc);
  671. end;
  672. result:=-1;
  673. end;
  674. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  675. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  676. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  677. var
  678. psrc,pdest,pend : pbyte;
  679. b : ptrint;
  680. begin
  681. b:=0;
  682. psrc:=@buf1;
  683. pdest:=@buf2;
  684. pend:=psrc+len;
  685. while psrc<pend do
  686. begin
  687. b:=(ptrint(psrc^)-ptrint(pdest^));
  688. if b<0 then
  689. exit(-1)
  690. else if b>0 then
  691. exit(1);
  692. if (psrc^=0) or (pdest^=0) then
  693. exit(0);
  694. inc(pdest);
  695. inc(psrc);
  696. end;
  697. result:=0;
  698. end;
  699. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  700. {****************************************************************************
  701. Object Helpers
  702. ****************************************************************************}
  703. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  704. type
  705. pobjectvmt=^tobjectvmt;
  706. tobjectvmt=record
  707. size,msize:sizeuint;
  708. parent:pointer;
  709. end;
  710. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  711. { Note: _vmt will be reset to -1 when memory is allocated,
  712. this is needed for fpc_help_fail }
  713. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  714. var
  715. vmtcopy : pobjectvmt;
  716. begin
  717. vmtcopy:=pobjectvmt(_vmt);
  718. { Inherited call? }
  719. if vmtcopy=nil then
  720. begin
  721. fpc_help_constructor:=_self;
  722. exit;
  723. end;
  724. if (_self=nil) and
  725. (vmtcopy^.size>0) then
  726. begin
  727. getmem(_self,vmtcopy^.size);
  728. { reset vmt needed for fail }
  729. _vmt:=pointer(-1);
  730. end;
  731. if _self<>nil then
  732. begin
  733. fillchar(_self^,vmtcopy^.size,0);
  734. ppointer(_self+_vmt_pos)^:=vmtcopy;
  735. end;
  736. fpc_help_constructor:=_self;
  737. end;
  738. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  739. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  740. { Note: _self will not be reset, the compiler has to generate the reset }
  741. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  742. begin
  743. { already released? }
  744. if (_self=nil) or
  745. (_vmt<>pointer(-1)) or
  746. (ppointer(_self+vmt_pos)^=nil) then
  747. exit;
  748. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  749. (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  750. RunError(210);
  751. { reset vmt to nil for protection }
  752. ppointer(_self+vmt_pos)^:=nil;
  753. freemem(_self);
  754. end;
  755. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  756. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  757. { Note: _self will not be reset, the compiler has to generate the reset }
  758. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  759. begin
  760. if (_self=nil) or (_vmt=nil) then
  761. exit;
  762. { vmt=$ffffffff when memory was allocated }
  763. if ptruint(_vmt)=high(ptruint) then
  764. begin
  765. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  766. HandleError(210)
  767. else
  768. begin
  769. ppointer(_self+vmt_pos)^:=nil;
  770. freemem(_self);
  771. { reset _vmt to nil so it will not be freed a
  772. second time }
  773. _vmt:=nil;
  774. end;
  775. end
  776. else
  777. ppointer(_self+vmt_pos)^:=nil;
  778. end;
  779. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  780. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  781. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  782. begin
  783. if (_vmt=nil) or
  784. (pobjectvmt(_vmt)^.size=0) or
  785. (pobjectvmt(_vmt)^.size+pobjectvmt(_vmt)^.msize<>0) then
  786. RunError(210);
  787. end;
  788. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  789. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  790. { checks for a correct vmt pointer }
  791. { deeper check to see if the current object is }
  792. { really related to the true }
  793. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  794. begin
  795. if (vmt=nil) or
  796. (pobjectvmt(vmt)^.size=0) or
  797. (pobjectvmt(vmt)^.size+pobjectvmt(vmt)^.msize<>0) then
  798. RunError(210);
  799. while assigned(vmt) do
  800. if vmt=expvmt then
  801. exit
  802. else
  803. vmt:=pobjectvmt(vmt)^.parent;
  804. RunError(219);
  805. end;
  806. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  807. {$endif FPC_HAS_FEATURE_OBJECTS}
  808. {****************************************************************************
  809. String
  810. ****************************************************************************}
  811. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  812. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  813. var
  814. slen : byte;
  815. begin
  816. slen:=length(sstr);
  817. if slen>high(res) then
  818. slen:=high(res);
  819. move(sstr[0],res[0],slen+1);
  820. res[0]:=chr(slen);
  821. end;
  822. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  823. var
  824. slen : byte;
  825. begin
  826. slen:=length(pshortstring(sstr)^);
  827. if slen<len then
  828. len:=slen;
  829. move(sstr^,dstr^,len+1);
  830. if slen>len then
  831. pchar(dstr)^:=chr(len);
  832. end;
  833. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  834. {$push}
  835. { ensure that comparing addresses of openshortstrings with regular shortstrings
  836. doesn't cause errors }
  837. {$t-}
  838. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  839. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  840. var
  841. s1l, s2l : ObjpasInt;
  842. begin
  843. s1l:=length(s1);
  844. s2l:=length(s2);
  845. if s1l+s2l>high(dests) then
  846. begin
  847. if s1l>high(dests) then
  848. s1l:=high(dests);
  849. s2l:=high(dests)-s1l;
  850. end;
  851. if @dests=@s1 then
  852. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l)
  853. else
  854. if @dests=@s2 then
  855. begin
  856. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,s1l+1,s2l);
  857. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  858. end
  859. else
  860. begin
  861. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  862. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  863. end;
  864. dests[0]:=chr(s1l+s2l);
  865. end;
  866. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  867. var
  868. s2l : byte;
  869. LowStart,i,
  870. Len : ObjpasInt;
  871. needtemp : boolean;
  872. tmpstr : shortstring;
  873. p,pdest : pshortstring;
  874. begin
  875. if high(sarr)=0 then
  876. begin
  877. DestS:='';
  878. exit;
  879. end;
  880. lowstart:=low(sarr);
  881. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  882. inc(lowstart);
  883. { Check for another reuse, then we can't use
  884. the append optimization and need to use a temp }
  885. needtemp:=false;
  886. for i:=lowstart to high(sarr) do
  887. begin
  888. if Pointer(@DestS)=Pointer(sarr[i]) then
  889. begin
  890. needtemp:=true;
  891. break;
  892. end;
  893. end;
  894. if needtemp then
  895. begin
  896. lowstart:=low(sarr);
  897. tmpstr:='';
  898. pdest:=@tmpstr
  899. end
  900. else
  901. begin
  902. { Start with empty DestS if we start with concatting
  903. the first array element }
  904. if lowstart=low(sarr) then
  905. DestS:='';
  906. pdest:=@DestS;
  907. end;
  908. { Concat all strings, except the string we already
  909. copied in DestS }
  910. Len:=length(pdest^);
  911. for i:=lowstart to high(sarr) do
  912. begin
  913. p:=sarr[i];
  914. if assigned(p) then
  915. begin
  916. s2l:=length(p^);
  917. if Len+s2l>high(dests) then
  918. s2l:=high(dests)-Len;
  919. fpc_shortstr_shortstr_intern_charmove(p^,1,pdest^,Len+1,s2l);
  920. inc(Len,s2l);
  921. end;
  922. end;
  923. pdest^[0]:=Chr(Len);
  924. if needtemp then
  925. DestS:=TmpStr;
  926. end;
  927. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  928. {$pop}
  929. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  930. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  931. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  932. var
  933. s1l, s2l : sizeint;
  934. begin
  935. s1l:=length(s1);
  936. s2l:=length(s2);
  937. if s1l+s2l>high(s1) then
  938. s2l:=high(s1)-s1l;
  939. move(s2[1],s1[s1l+1],s2l);
  940. s1[0]:=chr(s1l+s2l);
  941. end;
  942. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  943. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  944. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  945. var
  946. s1,s2,max,i : byte;
  947. d : ObjpasInt;
  948. begin
  949. s1:=length(left);
  950. s2:=length(right);
  951. if s1<s2 then
  952. max:=s1
  953. else
  954. max:=s2;
  955. for i:=1 to max do
  956. begin
  957. d:=byte(left[i])-byte(right[i]);
  958. if d>0 then
  959. exit(1)
  960. else if d<0 then
  961. exit(-1);
  962. end;
  963. if s1>s2 then
  964. exit(1)
  965. else if s1<s2 then
  966. exit(-1)
  967. else
  968. exit(0);
  969. end;
  970. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  971. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  972. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  973. begin
  974. Result := ObjpasInt(left[0]) - ObjpasInt(right[0]);
  975. if Result = 0 then
  976. Result := CompareByte(left[1],right[1], ObjpasInt(left[0]));
  977. end;
  978. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  979. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  980. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  981. var
  982. l : ObjpasInt;
  983. s: shortstring;
  984. begin
  985. if p=nil then
  986. l:=0
  987. else
  988. l:=strlen(p);
  989. if l>high(res) then
  990. l:=high(res);
  991. if l>0 then
  992. move(p^,s[1],l);
  993. s[0]:=chr(l);
  994. res:=s;
  995. end;
  996. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  997. {$ifndef cpujvm}
  998. { also define alias which can be used inside the system unit }
  999. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1000. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1001. begin
  1002. fpc_pchar_to_shortstr(result,p);
  1003. end;
  1004. {$endif not cpujvm}
  1005. function Utf8CodePointLen(P: PAnsiChar; MaxLookAhead: SizeInt; IncludeCombiningDiacriticalMarks: Boolean): SizeInt;
  1006. var
  1007. bytes: sizeint;
  1008. firstzerobit: byte;
  1009. begin
  1010. { see https://en.wikipedia.org/wiki/UTF-8#Description for details }
  1011. if maxlookahead<=0 then
  1012. begin
  1013. { incomplete }
  1014. result:=0;
  1015. exit;
  1016. end;
  1017. { include the first byte }
  1018. result:=1;
  1019. { multiple byte utf-8 code point? }
  1020. if p[0]>#127 then
  1021. begin
  1022. { bsr searches for the leftmost 1 bit. We are interested in the
  1023. leftmost 0 bit, so first invert the value
  1024. }
  1025. firstzerobit:=bsrbyte(not(byte(p[0])));
  1026. { if there is no zero bit or the first zero bit is the rightmost bit
  1027. (bit 0), this is an invalid UTF-8 byte ($ff cannot appear in an
  1028. UTF-8-encoded string, and in the worst case bit 1 has to be zero)
  1029. Additionally, 5-byte UTF-8 sequences don't exist either, so bit 1
  1030. cannot be the first zero-bit either. And bits 6 and 7 can't be 0
  1031. either in the first byte.
  1032. }
  1033. if (firstzerobit<=1) or (firstzerobit>=6) then
  1034. begin
  1035. result:=-result;
  1036. exit;
  1037. end;
  1038. { the number of bytes belonging to this code point is
  1039. 7-(pos first 0-bit). Subtract 1 since we're already at the first
  1040. byte. All subsequent bytes of the same sequence must have their
  1041. highest bit set and the next one unset. We stop when we detect an
  1042. invalid sequence.
  1043. }
  1044. bytes:=6-firstzerobit;
  1045. while (result<maxlookahead) and
  1046. (bytes>0) and
  1047. ((ord(p[result]) and %11000000)=%10000000) do
  1048. begin
  1049. inc(result);
  1050. dec(bytes);
  1051. end;
  1052. { stopped because of invalid/incomplete sequence -> exit }
  1053. if bytes<>0 then
  1054. begin
  1055. if result>=maxlookahead then
  1056. result:=0
  1057. else
  1058. result:=-result;
  1059. exit;
  1060. end;
  1061. end;
  1062. if includecombiningdiacriticalmarks then
  1063. begin
  1064. { combining diacritical marks?
  1065. 1) U+0300 - U+036F in UTF-8 = %11001100 10000000 - %11001101 10101111
  1066. 2) U+1AB0 - U+1AFF in UTF-8 = %11100001 10101010 10110000 - %11100001 10101011 10111111
  1067. 3) U+1DC0 - U+1DFF in UTF-8 = %11100001 10110111 10000000 - %11100001 10110111 10111111
  1068. 4) U+20D0 - U+20FF in UTF-8 = %11100010 10000011 10010000 - %11100010 10000011 10111111
  1069. 5) U+FE20 - U+FE2F in UTF-8 = %11101111 10111000 10100000 - %11101111 10111000 10101111
  1070. }
  1071. repeat
  1072. bytes:=result;
  1073. if result+1<maxlookahead then
  1074. begin
  1075. { case 1) }
  1076. if ((ord(p[result]) and %11001100=%11001100)) and
  1077. (ord(p[result+1])>=%10000000) and
  1078. (ord(p[result+1])<=%10101111) then
  1079. inc(result,2)
  1080. { case 2), 3), 4), 5) }
  1081. else if (result+2<maxlookahead) and
  1082. (ord(p[result])>=%11100001) then
  1083. begin
  1084. { case 2) }
  1085. if ((ord(p[result])=%11100001) and
  1086. (ord(p[result+1])=%10101010) and
  1087. (ord(p[result+2])>=%10110000) and
  1088. (ord(p[result+2])<=%10111111)) or
  1089. { case 3) }
  1090. ((ord(p[result])=%11100001) and
  1091. (ord(p[result+1])=%10110111) and
  1092. (ord(p[result+2])>=%10000000) and
  1093. (ord(p[result+2])<=%10111111)) or
  1094. { case 4) }
  1095. ((ord(p[result])=%11100010) and
  1096. (ord(p[result+1])=%10000011) and
  1097. (ord(p[result+2])>=%10010000) and
  1098. (ord(p[result+2])<=%10111111)) or
  1099. { case 5) }
  1100. ((ord(p[result])=%11101111) and
  1101. (ord(p[result+1])=%10111000) and
  1102. (ord(p[result+2])>=%10100000) and
  1103. (ord(p[result+2])<=%10101111)) then
  1104. inc(result,3);
  1105. end;
  1106. end;
  1107. until bytes=result;
  1108. { is there an incomplete diacritical mark? (invalid makes little sense:
  1109. either a sequence is a combining diacritical mark, or it's not ; if
  1110. it's invalid, it may also not have been a combining diacritical mark)
  1111. }
  1112. if result<maxlookahead then
  1113. begin
  1114. { case 1) }
  1115. if (((ord(p[result]) and %11001100=%11001100)) and
  1116. (result+1>=maxlookahead)) or
  1117. { case 2) and 3)}
  1118. ((ord(p[result])=%11100001) and
  1119. ((result+1>=maxlookahead) or
  1120. (((ord(p[result+1])=%10101010) or
  1121. (ord(p[result+1])=%10110111)) and
  1122. (result+2>=maxlookahead)))) or
  1123. { case 4 }
  1124. ((ord(p[result])=%11100010) and
  1125. ((result+1>=maxlookahead) or
  1126. ((ord(p[result+1])=%10000011) and
  1127. (result+2>=maxlookahead)))) or
  1128. { case 5 }
  1129. ((ord(p[result])=%11101111) and
  1130. ((result+1>=maxlookahead) or
  1131. ((ord(p[result+1])=%10111000) and
  1132. (result+2>=maxlookahead)))) then
  1133. begin
  1134. result:=0;
  1135. exit;
  1136. end;
  1137. end;
  1138. end;
  1139. end;
  1140. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1141. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1142. var
  1143. l: ObjpasInt;
  1144. index: ObjpasInt;
  1145. len: byte;
  1146. begin
  1147. l:=high(arr)+1;
  1148. if l>=high(res)+1 then
  1149. l:=high(res)
  1150. else if l<0 then
  1151. l:=0;
  1152. if zerobased then
  1153. begin
  1154. index:=IndexByte(arr[0],l,0);
  1155. if index<0 then
  1156. len:=l
  1157. else
  1158. len:=index;
  1159. end
  1160. else
  1161. len:=l;
  1162. move(arr[0],res[1],len);
  1163. res[0]:=chr(len);
  1164. end;
  1165. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1166. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1167. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1168. var
  1169. len: ObjpasInt;
  1170. begin
  1171. len := length(src);
  1172. if len > length(res) then
  1173. len := length(res);
  1174. {$push}{$r-}
  1175. { make sure we don't access char 1 if length is 0 (JM) }
  1176. if len > 0 then
  1177. move(src[1],res[0],len);
  1178. fillchar(res[len],length(res)-len,0);
  1179. {$pop}
  1180. end;
  1181. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1182. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1183. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1184. begin
  1185. if assigned(p) then
  1186. Result:=IndexByte(p^,high(Result),0)
  1187. else
  1188. Result:=0;
  1189. end;
  1190. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1191. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1192. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1193. var i : sizeint;
  1194. begin
  1195. i:=0;
  1196. if assigned(p) then
  1197. while p[i]<>#0 do
  1198. inc(i);
  1199. exit(i);
  1200. end;
  1201. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1202. {****************************************************************************
  1203. Caller/StackFrame Helpers
  1204. ****************************************************************************}
  1205. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1206. {_$error Get_frame must be defined for each processor }
  1207. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1208. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1209. {_$error Get_caller_addr must be defined for each processor }
  1210. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1211. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1212. {_$error Get_caller_frame must be defined for each processor }
  1213. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1214. {****************************************************************************
  1215. Math
  1216. ****************************************************************************}
  1217. {****************************************************************************
  1218. Software multiplication
  1219. ****************************************************************************}
  1220. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1221. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1222. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1223. var
  1224. sign : boolean;
  1225. q1,q2,q3 : word;
  1226. begin
  1227. { there's no difference between signed and unsigned multiplication,
  1228. when the destination size is equal to the source size and overflow
  1229. checking is off }
  1230. if not checkoverflow then
  1231. { word(f1)*word(f2) is coded as a call to mulword }
  1232. fpc_mul_integer:=integer(word(f1)*word(f2))
  1233. else
  1234. begin
  1235. sign:=false;
  1236. if f1<0 then
  1237. begin
  1238. sign:=not(sign);
  1239. q1:=word(-f1);
  1240. end
  1241. else
  1242. q1:=f1;
  1243. if f2<0 then
  1244. begin
  1245. sign:=not(sign);
  1246. q2:=word(-f2);
  1247. end
  1248. else
  1249. q2:=f2;
  1250. { the q1*q2 is coded as call to mulword }
  1251. q3:=q1*q2;
  1252. if (q1 <> 0) and (q2 <>0) and
  1253. ((q1>q3) or (q2>q3) or
  1254. { the bit 63 can be only set if we have $8000 }
  1255. { and sign is true }
  1256. (q3 shr 15<>0) and
  1257. ((q3<>word(word(1) shl 15)) or not(sign))
  1258. ) then
  1259. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1260. if sign then
  1261. fpc_mul_integer:=-q3
  1262. else
  1263. fpc_mul_integer:=q3;
  1264. end;
  1265. end;
  1266. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1267. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1268. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1269. var
  1270. _f1,bitpos : word;
  1271. b : byte;
  1272. f1overflowed : boolean;
  1273. begin
  1274. fpc_mul_word:=0;
  1275. bitpos:=1;
  1276. f1overflowed:=false;
  1277. for b:=0 to 15 do
  1278. begin
  1279. if (f2 and bitpos)<>0 then
  1280. begin
  1281. _f1:=fpc_mul_word;
  1282. fpc_mul_word:=fpc_mul_word+f1;
  1283. { if one of the operands is greater than the result an
  1284. overflow occurs }
  1285. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1286. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1287. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1288. end;
  1289. { when bootstrapping, we forget about overflow checking for qword :) }
  1290. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1291. f1:=f1 shl 1;
  1292. bitpos:=bitpos shl 1;
  1293. end;
  1294. end;
  1295. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1296. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1297. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1298. var
  1299. sign : boolean;
  1300. q1,q2,q3 : dword;
  1301. begin
  1302. { there's no difference between signed and unsigned multiplication,
  1303. when the destination size is equal to the source size and overflow
  1304. checking is off }
  1305. if not checkoverflow then
  1306. { dword(f1)*dword(f2) is coded as a call to muldword }
  1307. fpc_mul_longint:=longint(dword(f1)*dword(f2))
  1308. else
  1309. begin
  1310. sign:=false;
  1311. if f1<0 then
  1312. begin
  1313. sign:=not(sign);
  1314. q1:=dword(-f1);
  1315. end
  1316. else
  1317. q1:=f1;
  1318. if f2<0 then
  1319. begin
  1320. sign:=not(sign);
  1321. q2:=dword(-f2);
  1322. end
  1323. else
  1324. q2:=f2;
  1325. { the q1*q2 is coded as call to muldword }
  1326. q3:=q1*q2;
  1327. if (q1 <> 0) and (q2 <>0) and
  1328. ((q1>q3) or (q2>q3) or
  1329. { the bit 31 can be only set if we have $8000 0000 }
  1330. { and sign is true }
  1331. (q3 shr 15<>0) and
  1332. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1333. ) then
  1334. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1335. if sign then
  1336. fpc_mul_longint:=-q3
  1337. else
  1338. fpc_mul_longint:=q3;
  1339. end;
  1340. end;
  1341. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1342. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1343. { multiplies two dwords
  1344. the longbool for checkoverflow avoids a misaligned stack
  1345. }
  1346. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1347. var
  1348. _f1,bitpos : dword;
  1349. b : byte;
  1350. f1overflowed : boolean;
  1351. begin
  1352. fpc_mul_dword:=0;
  1353. bitpos:=1;
  1354. f1overflowed:=false;
  1355. for b:=0 to 31 do
  1356. begin
  1357. if (f2 and bitpos)<>0 then
  1358. begin
  1359. _f1:=fpc_mul_dword;
  1360. fpc_mul_dword:=fpc_mul_dword+f1;
  1361. { if one of the operands is greater than the result an
  1362. overflow occurs }
  1363. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1364. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1365. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1366. end;
  1367. { when bootstrapping, we forget about overflow checking for qword :) }
  1368. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1369. f1:=f1 shl 1;
  1370. bitpos:=bitpos shl 1;
  1371. end;
  1372. end;
  1373. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1374. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1375. {****************************************************************************
  1376. Software longint/dword division
  1377. ****************************************************************************}
  1378. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1379. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1380. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1381. var
  1382. shift,lzz,lzn : ObjpasInt;
  1383. begin
  1384. result:=0;
  1385. if n=0 then
  1386. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1387. if z=0 then
  1388. exit;
  1389. lzz:=BsrDWord(z);
  1390. lzn:=BsrDWord(n);
  1391. { if the denominator contains less zeros
  1392. then the numerator
  1393. then d is greater than the n }
  1394. if lzn>lzz then
  1395. exit;
  1396. shift:=lzz-lzn;
  1397. n:=n shl shift;
  1398. for shift:=shift downto 0 do
  1399. begin
  1400. if z>=n then
  1401. begin
  1402. z:=z-n;
  1403. result:=result+dword(dword(1) shl shift);
  1404. end;
  1405. n:=n shr 1;
  1406. end;
  1407. end;
  1408. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1409. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1410. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1411. var
  1412. shift,lzz,lzn : ObjpasInt;
  1413. begin
  1414. result:=0;
  1415. if n=0 then
  1416. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1417. if z=0 then
  1418. exit;
  1419. lzz:=BsrDWord(z);
  1420. lzn:=BsrDWord(n);
  1421. { if the denominator contains less zeros
  1422. then the numerator
  1423. then d is greater than the n }
  1424. if lzn>lzz then
  1425. begin
  1426. result:=z;
  1427. exit;
  1428. end;
  1429. shift:=lzz-lzn;
  1430. n:=n shl shift;
  1431. for shift:=shift downto 0 do
  1432. begin
  1433. if z>=n then
  1434. z:=z-n;
  1435. n:=n shr 1;
  1436. end;
  1437. result:=z;
  1438. end;
  1439. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1440. {$ifndef FPC_SYSTEM_HAS_DIV_WORD}
  1441. function fpc_div_word(n,z : word) : word; [public,alias: 'FPC_DIV_WORD']; compilerproc;
  1442. var
  1443. shift,lzz,lzn : Byte;
  1444. begin
  1445. result:=0;
  1446. if n=0 then
  1447. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1448. if z=0 then
  1449. exit;
  1450. lzz:=BsrWord(z);
  1451. lzn:=BsrWord(n);
  1452. { if the denominator contains less zeros
  1453. then the numerator
  1454. then d is greater than the n }
  1455. if lzn>lzz then
  1456. exit;
  1457. shift:=lzz-lzn;
  1458. n:=n shl shift;
  1459. for shift:=shift downto 0 do
  1460. begin
  1461. if z>=n then
  1462. begin
  1463. z:=z-n;
  1464. result:=result+word(word(1) shl shift);
  1465. end;
  1466. n:=n shr 1;
  1467. end;
  1468. end;
  1469. {$endif FPC_SYSTEM_HAS_DIV_WORD}
  1470. {$ifndef FPC_SYSTEM_HAS_MOD_WORD}
  1471. function fpc_mod_word(n,z : word) : word; [public,alias: 'FPC_MOD_WORD']; compilerproc;
  1472. var
  1473. shift,lzz,lzn : Byte;
  1474. begin
  1475. result:=0;
  1476. if n=0 then
  1477. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1478. if z=0 then
  1479. exit;
  1480. lzz:=BsrWord(z);
  1481. lzn:=BsrWord(n);
  1482. { if the denominator contains less zeros
  1483. then the numerator
  1484. then d is greater than the n }
  1485. if lzn>lzz then
  1486. begin
  1487. result:=z;
  1488. exit;
  1489. end;
  1490. shift:=lzz-lzn;
  1491. n:=n shl shift;
  1492. for shift:=shift downto 0 do
  1493. begin
  1494. if z>=n then
  1495. z:=z-n;
  1496. n:=n shr 1;
  1497. end;
  1498. result:=z;
  1499. end;
  1500. {$endif FPC_SYSTEM_HAS_MOD_WORD}
  1501. {$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
  1502. function fpc_div_byte(n,z : byte) : byte; [public,alias: 'FPC_DIV_BYTE']; compilerproc;
  1503. var
  1504. shift,lzz,lzn : Byte;
  1505. begin
  1506. result:=0;
  1507. if n=0 then
  1508. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1509. if z=0 then
  1510. exit;
  1511. lzz:=BsrByte(z);
  1512. lzn:=BsrByte(n);
  1513. { if the denominator contains less zeros
  1514. then the numerator
  1515. then d is greater than the n }
  1516. if lzn>lzz then
  1517. exit;
  1518. shift:=lzz-lzn;
  1519. n:=n shl shift;
  1520. for shift:=shift downto 0 do
  1521. begin
  1522. if z>=n then
  1523. begin
  1524. z:=z-n;
  1525. result:=result+byte(byte(1) shl shift);
  1526. end;
  1527. n:=n shr 1;
  1528. end;
  1529. end;
  1530. {$endif FPC_SYSTEM_HAS_DIV_BYTE}
  1531. {$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
  1532. function fpc_mod_byte(n,z : byte) : byte; [public,alias: 'FPC_MOD_BYTE']; compilerproc;
  1533. var
  1534. shift,lzz,lzn : Byte;
  1535. begin
  1536. result:=0;
  1537. if n=0 then
  1538. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1539. if z=0 then
  1540. exit;
  1541. lzz:=BsrByte(z);
  1542. lzn:=BsrByte(n);
  1543. { if the denominator contains less zeros
  1544. then the numerator
  1545. then d is greater than the n }
  1546. if lzn>lzz then
  1547. begin
  1548. result:=z;
  1549. exit;
  1550. end;
  1551. shift:=lzz-lzn;
  1552. n:=n shl shift;
  1553. for shift:=shift downto 0 do
  1554. begin
  1555. if z>=n then
  1556. z:=z-n;
  1557. n:=n shr 1;
  1558. end;
  1559. result:=z;
  1560. end;
  1561. {$endif FPC_SYSTEM_HAS_MOD_BYTE}
  1562. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1563. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1564. var
  1565. sign : boolean;
  1566. d1,d2 : dword;
  1567. begin
  1568. if n=0 then
  1569. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1570. sign:=false;
  1571. if z<0 then
  1572. begin
  1573. sign:=not(sign);
  1574. d1:=dword(-z);
  1575. end
  1576. else
  1577. d1:=z;
  1578. if n<0 then
  1579. begin
  1580. sign:=not(sign);
  1581. d2:=dword(-n);
  1582. end
  1583. else
  1584. d2:=n;
  1585. { the div is coded by the compiler as call to divdword }
  1586. if sign then
  1587. result:=-(d1 div d2)
  1588. else
  1589. result:=d1 div d2;
  1590. end;
  1591. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1592. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1593. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1594. var
  1595. signed : boolean;
  1596. r,nq,zq : dword;
  1597. begin
  1598. if n=0 then
  1599. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1600. nq:=abs(n);
  1601. if z<0 then
  1602. begin
  1603. zq:=dword(-z);
  1604. signed:=true;
  1605. end
  1606. else
  1607. begin
  1608. zq:=z;
  1609. signed:=false;
  1610. end;
  1611. r:=zq mod nq;
  1612. if signed then
  1613. result:=-longint(r)
  1614. else
  1615. result:=r;
  1616. end;
  1617. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1618. {$ifndef FPC_SYSTEM_HAS_DIV_SMALLINT}
  1619. function fpc_div_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_DIV_SMALLINT']; compilerproc;
  1620. var
  1621. sign : boolean;
  1622. w1,w2 : word;
  1623. begin
  1624. if n=0 then
  1625. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1626. sign:=false;
  1627. if z<0 then
  1628. begin
  1629. sign:=not(sign);
  1630. w1:=word(-z);
  1631. end
  1632. else
  1633. w1:=z;
  1634. if n<0 then
  1635. begin
  1636. sign:=not(sign);
  1637. w2:=word(-n);
  1638. end
  1639. else
  1640. w2:=n;
  1641. { the div is coded by the compiler as call to divdword }
  1642. if sign then
  1643. result:=-(w1 div w2)
  1644. else
  1645. result:=w1 div w2;
  1646. end;
  1647. {$endif FPC_SYSTEM_HAS_DIV_SMALLINT}
  1648. {$ifndef FPC_SYSTEM_HAS_MOD_SMALLINT}
  1649. function fpc_mod_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_MOD_SMALLINT']; compilerproc;
  1650. var
  1651. signed : boolean;
  1652. r,nq,zq : word;
  1653. begin
  1654. if n=0 then
  1655. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1656. nq:=abs(n);
  1657. if z<0 then
  1658. begin
  1659. zq:=word(-z);
  1660. signed:=true;
  1661. end
  1662. else
  1663. begin
  1664. zq:=z;
  1665. signed:=false;
  1666. end;
  1667. r:=zq mod nq;
  1668. if signed then
  1669. result:=-smallint(r)
  1670. else
  1671. result:=r;
  1672. end;
  1673. {$endif FPC_SYSTEM_HAS_MOD_SMALLINT}
  1674. {$ifndef FPC_SYSTEM_HAS_DIV_SHORTINT}
  1675. function fpc_div_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_DIV_SHORTINT']; compilerproc;
  1676. var
  1677. sign : boolean;
  1678. b1,b2 : byte;
  1679. begin
  1680. if n=0 then
  1681. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1682. sign:=false;
  1683. if z<0 then
  1684. begin
  1685. sign:=not(sign);
  1686. b1:=byte(-z);
  1687. end
  1688. else
  1689. b1:=z;
  1690. if n<0 then
  1691. begin
  1692. sign:=not(sign);
  1693. b2:=byte(-n);
  1694. end
  1695. else
  1696. b2:=n;
  1697. { the div is coded by the compiler as call to divdword }
  1698. if sign then
  1699. result:=-(b1 div b2)
  1700. else
  1701. result:=b1 div b2;
  1702. end;
  1703. {$endif FPC_SYSTEM_HAS_DIV_SHORTINT}
  1704. {$ifndef FPC_SYSTEM_HAS_MOD_SHORTINT}
  1705. function fpc_mod_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_MOD_SHORTINT']; compilerproc;
  1706. var
  1707. signed : boolean;
  1708. r,nq,zq : byte;
  1709. begin
  1710. if n=0 then
  1711. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1712. nq:=abs(n);
  1713. if z<0 then
  1714. begin
  1715. zq:=byte(-z);
  1716. signed:=true;
  1717. end
  1718. else
  1719. begin
  1720. zq:=z;
  1721. signed:=false;
  1722. end;
  1723. r:=zq mod nq;
  1724. if signed then
  1725. result:=-shortint(r)
  1726. else
  1727. result:=r;
  1728. end;
  1729. {$endif FPC_SYSTEM_HAS_MOD_SHORTINT}
  1730. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1731. {****************************************************************************}
  1732. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1733. { This is only needed to bootstrap on SPARC targets
  1734. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  1735. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1736. begin
  1737. if l<0 then
  1738. abs:=-l
  1739. else
  1740. abs:=l;
  1741. end;
  1742. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1743. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1744. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1745. begin
  1746. odd:=boolean(l and 1);
  1747. end;
  1748. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1749. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1750. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1751. begin
  1752. odd:=boolean(l and 1);
  1753. end;
  1754. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1755. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1756. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1757. begin
  1758. odd:=boolean(longint(l) and 1);
  1759. end;
  1760. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1761. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1762. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1763. begin
  1764. odd:=boolean(longint(l) and 1);
  1765. end;
  1766. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1767. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1768. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1769. begin
  1770. sqr:=l*l;
  1771. end;
  1772. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1773. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1774. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1775. begin
  1776. if l < 0 then
  1777. abs := -l
  1778. else
  1779. abs := l;
  1780. end;
  1781. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1782. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1783. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1784. begin
  1785. sqr := l*l;
  1786. end;
  1787. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1788. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1789. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1790. begin
  1791. sqr := l*l;
  1792. end;
  1793. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1794. {$ifdef CPU16}
  1795. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1796. function declocked(var l:smallint):boolean;
  1797. begin
  1798. Dec(l);
  1799. declocked:=(l=0);
  1800. end;
  1801. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1802. {$endif CPU16}
  1803. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1804. function declocked(var l:longint):boolean;
  1805. begin
  1806. Dec(l);
  1807. declocked:=(l=0);
  1808. end;
  1809. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1810. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1811. function declocked(var l:int64):boolean;
  1812. begin
  1813. Dec(l);
  1814. declocked:=(l=0);
  1815. end;
  1816. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1817. {$ifdef CPU16}
  1818. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1819. procedure inclocked(var l:smallint);
  1820. begin
  1821. Inc(l);
  1822. end;
  1823. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1824. {$endif CPU16}
  1825. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1826. procedure inclocked(var l:longint);
  1827. begin
  1828. Inc(l);
  1829. end;
  1830. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1831. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1832. procedure inclocked(var l:int64);
  1833. begin
  1834. Inc(l);
  1835. end;
  1836. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1837. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1838. {_$error Sptr must be defined for each processor }
  1839. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1840. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1841. var
  1842. tmp: PtrUInt;
  1843. begin
  1844. tmp:=addr+(alignment-1);
  1845. result:=tmp-(tmp mod alignment)
  1846. end;
  1847. {$ifndef cpujvm}
  1848. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1849. var
  1850. tmp: PtrUInt;
  1851. begin
  1852. tmp:=PtrUInt(addr)+(alignment-1);
  1853. result:=pointer(tmp-(tmp mod alignment));
  1854. end;
  1855. {$endif}
  1856. {****************************************************************************
  1857. Str()
  1858. ****************************************************************************}
  1859. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1860. procedure int_str(l:longint;out s:shortstring);
  1861. var
  1862. m,m1 : longword;
  1863. pcstart,
  1864. pc2start,
  1865. pc,pc2 : pchar;
  1866. hs : string[32];
  1867. overflow : longint;
  1868. begin
  1869. pc2start:=@s[1];
  1870. pc2:=pc2start;
  1871. if (l<0) then
  1872. begin
  1873. pc2^:='-';
  1874. inc(pc2);
  1875. m:=longword(-l);
  1876. end
  1877. else
  1878. m:=longword(l);
  1879. pcstart:=pchar(@hs[0]);
  1880. pc:=pcstart;
  1881. repeat
  1882. m1:=m div 10;
  1883. inc(pc);
  1884. pc^:=char(m-(m1*10)+byte('0'));
  1885. m:=m1;
  1886. until m=0;
  1887. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1888. if overflow>0 then
  1889. inc(pcstart,overflow);
  1890. while (pc>pcstart) do
  1891. begin
  1892. pc2^:=pc^;
  1893. inc(pc2);
  1894. dec(pc);
  1895. end;
  1896. s[0]:=char(pc2-pc2start);
  1897. end;
  1898. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1899. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1900. procedure int_str_unsigned(l:longword;out s:shortstring);
  1901. var
  1902. m1 : longword;
  1903. pcstart,
  1904. pc2start,
  1905. pc,pc2 : pchar;
  1906. hs : string[32];
  1907. overflow : longint;
  1908. begin
  1909. pc2start:=@s[1];
  1910. pc2:=pc2start;
  1911. pcstart:=pchar(@hs[0]);
  1912. pc:=pcstart;
  1913. repeat
  1914. inc(pc);
  1915. m1:=l div 10;
  1916. pc^:=char(l-(m1*10)+byte('0'));
  1917. l:=m1;
  1918. until l=0;
  1919. overflow:=(pc-pcstart)-high(s);
  1920. if overflow>0 then
  1921. inc(pcstart,overflow);
  1922. while (pc>pcstart) do
  1923. begin
  1924. pc2^:=pc^;
  1925. inc(pc2);
  1926. dec(pc);
  1927. end;
  1928. s[0]:=char(pc2-pc2start);
  1929. end;
  1930. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1931. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1932. procedure int_str(l:int64;out s:shortstring);
  1933. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1934. begin
  1935. runerror(217);
  1936. end;
  1937. {$else EXCLUDE_COMPLEX_PROCS}
  1938. var
  1939. m,m1 : qword;
  1940. pcstart,
  1941. pc2start,
  1942. pc,pc2 : pchar;
  1943. hs : string[32];
  1944. overflow : longint;
  1945. begin
  1946. pc2start:=@s[1];
  1947. pc2:=pc2start;
  1948. if (l<0) then
  1949. begin
  1950. pc2^:='-';
  1951. inc(pc2);
  1952. m:=qword(-l);
  1953. end
  1954. else
  1955. m:=qword(l);
  1956. pcstart:=pchar(@hs[0]);
  1957. pc:=pcstart;
  1958. repeat
  1959. m1:=m div 10;
  1960. inc(pc);
  1961. pc^:=char(m-(m1*10)+byte('0'));
  1962. m:=m1;
  1963. until m=0;
  1964. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1965. if overflow>0 then
  1966. inc(pcstart,overflow);
  1967. while (pc>pcstart) do
  1968. begin
  1969. pc2^:=pc^;
  1970. inc(pc2);
  1971. dec(pc);
  1972. end;
  1973. s[0]:=char(pc2-pc2start);
  1974. end;
  1975. {$endif EXCLUDE_COMPLEX_PROCS}
  1976. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1977. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1978. procedure int_str_unsigned(l:qword;out s:shortstring);
  1979. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1980. begin
  1981. runerror(217);
  1982. end;
  1983. {$else EXCLUDE_COMPLEX_PROCS}
  1984. var
  1985. m1 : qword;
  1986. pcstart,
  1987. pc2start,
  1988. pc,pc2 : pchar;
  1989. hs : string[64];
  1990. overflow : longint;
  1991. begin
  1992. pc2start:=@s[1];
  1993. pc2:=pc2start;
  1994. pcstart:=pchar(@hs[0]);
  1995. pc:=pcstart;
  1996. repeat
  1997. inc(pc);
  1998. m1:=l div 10;
  1999. pc^:=char(l-(m1*10)+byte('0'));
  2000. l:=m1;
  2001. until l=0;
  2002. overflow:=(pc-pcstart)-high(s);
  2003. if overflow>0 then
  2004. inc(pcstart,overflow);
  2005. while (pc>pcstart) do
  2006. begin
  2007. pc2^:=pc^;
  2008. inc(pc2);
  2009. dec(pc);
  2010. end;
  2011. s[0]:=char(pc2-pc2start);
  2012. end;
  2013. {$endif EXCLUDE_COMPLEX_PROCS}
  2014. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2015. {$ifndef FPUNONE}
  2016. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  2017. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2018. begin
  2019. softfloat_exception_flags:=[];
  2020. end;
  2021. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  2022. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  2023. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2024. begin
  2025. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  2026. end;
  2027. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  2028. {$endif}
  2029. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  2030. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2031. begin
  2032. { the extra Word type cast is necessary because the "AValue shr 8" }
  2033. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  2034. { the sign bits from the upper 16 bits are shifted in rather than }
  2035. { zeroes. }
  2036. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  2037. end;
  2038. {$ifndef cpujvm}
  2039. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2040. begin
  2041. Result := Word((AValue shr 8) or (AValue shl 8));
  2042. end;
  2043. {$endif}
  2044. function SwapEndian(const AValue: LongInt): LongInt;
  2045. begin
  2046. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2047. Result := (Result shl 16) or (Result shr 16);
  2048. end;
  2049. {$ifndef cpujvm}
  2050. function SwapEndian(const AValue: DWord): DWord;
  2051. begin
  2052. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2053. Result := (Result shl 16) or (Result shr 16);
  2054. end;
  2055. {$endif}
  2056. function SwapEndian(const AValue: Int64): Int64;
  2057. begin
  2058. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2059. ((AValue shr 8) and $00FF00FF00FF00FF);
  2060. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2061. ((Result shr 16) and $0000FFFF0000FFFF);
  2062. Result := (Result shl 32) or ((Result shr 32));
  2063. end;
  2064. {$ifndef cpujvm}
  2065. function SwapEndian(const AValue: QWord): QWord;
  2066. begin
  2067. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2068. ((AValue shr 8) and $00FF00FF00FF00FF);
  2069. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2070. ((Result shr 16) and $0000FFFF0000FFFF);
  2071. Result := (Result shl 32) or ((Result shr 32));
  2072. end;
  2073. {$endif}
  2074. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  2075. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2076. begin
  2077. {$IFDEF ENDIAN_BIG}
  2078. Result := AValue;
  2079. {$ELSE}
  2080. Result := SwapEndian(AValue);
  2081. {$ENDIF}
  2082. end;
  2083. {$ifndef cpujvm}
  2084. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2085. begin
  2086. {$IFDEF ENDIAN_BIG}
  2087. Result := AValue;
  2088. {$ELSE}
  2089. Result := SwapEndian(AValue);
  2090. {$ENDIF}
  2091. end;
  2092. {$endif not cpujvm}
  2093. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2094. begin
  2095. {$IFDEF ENDIAN_BIG}
  2096. Result := AValue;
  2097. {$ELSE}
  2098. Result := SwapEndian(AValue);
  2099. {$ENDIF}
  2100. end;
  2101. {$ifndef cpujvm}
  2102. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2103. begin
  2104. {$IFDEF ENDIAN_BIG}
  2105. Result := AValue;
  2106. {$ELSE}
  2107. Result := SwapEndian(AValue);
  2108. {$ENDIF}
  2109. end;
  2110. {$endif not cpujvm}
  2111. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2112. begin
  2113. {$IFDEF ENDIAN_BIG}
  2114. Result := AValue;
  2115. {$ELSE}
  2116. Result := SwapEndian(AValue);
  2117. {$ENDIF}
  2118. end;
  2119. {$ifndef cpujvm}
  2120. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2121. begin
  2122. {$IFDEF ENDIAN_BIG}
  2123. Result := AValue;
  2124. {$ELSE}
  2125. Result := SwapEndian(AValue);
  2126. {$ENDIF}
  2127. end;
  2128. {$endif not cpujvm}
  2129. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2130. begin
  2131. {$IFDEF ENDIAN_LITTLE}
  2132. Result := AValue;
  2133. {$ELSE}
  2134. Result := SwapEndian(AValue);
  2135. {$ENDIF}
  2136. end;
  2137. {$ifndef cpujvm}
  2138. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2139. begin
  2140. {$IFDEF ENDIAN_LITTLE}
  2141. Result := AValue;
  2142. {$ELSE}
  2143. Result := SwapEndian(AValue);
  2144. {$ENDIF}
  2145. end;
  2146. {$endif not cpujvm}
  2147. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2148. begin
  2149. {$IFDEF ENDIAN_LITTLE}
  2150. Result := AValue;
  2151. {$ELSE}
  2152. Result := SwapEndian(AValue);
  2153. {$ENDIF}
  2154. end;
  2155. {$ifndef cpujvm}
  2156. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2157. begin
  2158. {$IFDEF ENDIAN_LITTLE}
  2159. Result := AValue;
  2160. {$ELSE}
  2161. Result := SwapEndian(AValue);
  2162. {$ENDIF}
  2163. end;
  2164. {$endif not cpujvm}
  2165. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2166. begin
  2167. {$IFDEF ENDIAN_LITTLE}
  2168. Result := AValue;
  2169. {$ELSE}
  2170. Result := SwapEndian(AValue);
  2171. {$ENDIF}
  2172. end;
  2173. {$ifndef cpujvm}
  2174. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2175. begin
  2176. {$IFDEF ENDIAN_LITTLE}
  2177. Result := AValue;
  2178. {$ELSE}
  2179. Result := SwapEndian(AValue);
  2180. {$ENDIF}
  2181. end;
  2182. {$endif not cpujvm}
  2183. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2184. begin
  2185. {$IFDEF ENDIAN_BIG}
  2186. Result := AValue;
  2187. {$ELSE}
  2188. Result := SwapEndian(AValue);
  2189. {$ENDIF}
  2190. end;
  2191. {$ifndef cpujvm}
  2192. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2193. begin
  2194. {$IFDEF ENDIAN_BIG}
  2195. Result := AValue;
  2196. {$ELSE}
  2197. Result := SwapEndian(AValue);
  2198. {$ENDIF}
  2199. end;
  2200. {$endif not cpujvm}
  2201. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2202. begin
  2203. {$IFDEF ENDIAN_BIG}
  2204. Result := AValue;
  2205. {$ELSE}
  2206. Result := SwapEndian(AValue);
  2207. {$ENDIF}
  2208. end;
  2209. {$ifndef cpujvm}
  2210. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2211. begin
  2212. {$IFDEF ENDIAN_BIG}
  2213. Result := AValue;
  2214. {$ELSE}
  2215. Result := SwapEndian(AValue);
  2216. {$ENDIF}
  2217. end;
  2218. {$endif not cpujvm}
  2219. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2220. begin
  2221. {$IFDEF ENDIAN_BIG}
  2222. Result := AValue;
  2223. {$ELSE}
  2224. Result := SwapEndian(AValue);
  2225. {$ENDIF}
  2226. end;
  2227. {$ifndef cpujvm}
  2228. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2229. begin
  2230. {$IFDEF ENDIAN_BIG}
  2231. Result := AValue;
  2232. {$ELSE}
  2233. Result := SwapEndian(AValue);
  2234. {$ENDIF}
  2235. end;
  2236. {$endif not cpujvm}
  2237. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2238. begin
  2239. {$IFDEF ENDIAN_LITTLE}
  2240. Result := AValue;
  2241. {$ELSE}
  2242. Result := SwapEndian(AValue);
  2243. {$ENDIF}
  2244. end;
  2245. {$ifndef cpujvm}
  2246. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2247. begin
  2248. {$IFDEF ENDIAN_LITTLE}
  2249. Result := AValue;
  2250. {$ELSE}
  2251. Result := SwapEndian(AValue);
  2252. {$ENDIF}
  2253. end;
  2254. {$endif not cpujvm}
  2255. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2256. begin
  2257. {$IFDEF ENDIAN_LITTLE}
  2258. Result := AValue;
  2259. {$ELSE}
  2260. Result := SwapEndian(AValue);
  2261. {$ENDIF}
  2262. end;
  2263. {$ifndef cpujvm}
  2264. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2265. begin
  2266. {$IFDEF ENDIAN_LITTLE}
  2267. Result := AValue;
  2268. {$ELSE}
  2269. Result := SwapEndian(AValue);
  2270. {$ENDIF}
  2271. end;
  2272. {$endif not cpujvm}
  2273. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2274. begin
  2275. {$IFDEF ENDIAN_LITTLE}
  2276. Result := AValue;
  2277. {$ELSE}
  2278. Result := SwapEndian(AValue);
  2279. {$ENDIF}
  2280. end;
  2281. {$ifndef cpujvm}
  2282. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2283. begin
  2284. {$IFDEF ENDIAN_LITTLE}
  2285. Result := AValue;
  2286. {$ELSE}
  2287. Result := SwapEndian(AValue);
  2288. {$ENDIF}
  2289. end;
  2290. {$endif not cpujvm}
  2291. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2292. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2293. begin
  2294. end;
  2295. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2296. begin
  2297. end;
  2298. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2299. begin
  2300. end;
  2301. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2302. begin
  2303. end;
  2304. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2305. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2306. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2307. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2308. begin
  2309. Result:=(AValue shr 1) or (AValue shl 7);
  2310. end;
  2311. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2312. begin
  2313. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2314. end;
  2315. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2316. begin
  2317. Result:=(AValue shl 1) or (AValue shr 7);
  2318. end;
  2319. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2320. begin
  2321. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2322. end;
  2323. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2324. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2325. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2326. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2327. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2328. begin
  2329. Result:=(AValue shr 1) or (AValue shl 15);
  2330. end;
  2331. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2332. begin
  2333. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2334. end;
  2335. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2336. begin
  2337. Result:=(AValue shl 1) or (AValue shr 15);
  2338. end;
  2339. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2340. begin
  2341. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2342. end;
  2343. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2344. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2345. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2346. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2347. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2348. begin
  2349. Result:=(AValue shr 1) or (AValue shl 31);
  2350. end;
  2351. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2352. begin
  2353. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2354. end;
  2355. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2356. begin
  2357. Result:=(AValue shl 1) or (AValue shr 31);
  2358. end;
  2359. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2360. begin
  2361. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2362. end;
  2363. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2364. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2365. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2366. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2367. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2368. begin
  2369. Result:=(AValue shr 1) or (AValue shl 63);
  2370. end;
  2371. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2372. begin
  2373. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2374. end;
  2375. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2376. begin
  2377. Result:=(AValue shl 1) or (AValue shr 63);
  2378. end;
  2379. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2380. begin
  2381. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2382. end;
  2383. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2384. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2385. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2386. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2387. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2388. begin
  2389. 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)))));
  2390. end;
  2391. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2392. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2393. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2394. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2395. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2396. begin
  2397. 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)))));
  2398. end;
  2399. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2400. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2401. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2402. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2403. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2404. begin
  2405. 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)))));
  2406. end;
  2407. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2408. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2409. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2410. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2411. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2412. begin
  2413. 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)))));
  2414. end;
  2415. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2416. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2417. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2418. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2419. function BsfByte(Const AValue: Byte): Byte;
  2420. const bsf8bit: array [Byte] of Byte = (
  2421. $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,
  2422. 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,
  2423. 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,
  2424. 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,
  2425. 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,
  2426. 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,
  2427. 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,
  2428. 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
  2429. );
  2430. begin
  2431. result:=bsf8bit[AValue];
  2432. end;
  2433. {$endif}
  2434. {$endif}
  2435. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2436. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2437. function BsrByte(Const AValue: Byte): Byte;
  2438. const bsr8bit: array [Byte] of Byte = (
  2439. $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,
  2440. 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,
  2441. 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,
  2442. 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,
  2443. 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,
  2444. 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,
  2445. 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,
  2446. 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
  2447. );
  2448. begin
  2449. result:=bsr8bit[AValue];
  2450. end;
  2451. {$endif}
  2452. {$endif}
  2453. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2454. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2455. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2456. begin
  2457. result:=ord(lo(AValue)=0)*8;
  2458. result:=result or BsfByte(byte(AValue shr result));
  2459. end;
  2460. {$endif}
  2461. {$endif}
  2462. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2463. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2464. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2465. begin
  2466. result:=ord(AValue>255)*8;
  2467. result:=result or BsrByte(byte(AValue shr result));
  2468. end;
  2469. {$endif}
  2470. {$endif}
  2471. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2472. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2473. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2474. var
  2475. tmp: DWord;
  2476. begin
  2477. result:=ord(lo(AValue)=0)*16;
  2478. tmp:=AValue shr result;
  2479. result:=result or (ord((tmp and $FF)=0)*8);
  2480. tmp:=tmp shr (result and 8);
  2481. result:=result or BsfByte(byte(tmp));
  2482. end;
  2483. {$endif}
  2484. {$endif}
  2485. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2486. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2487. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2488. var
  2489. tmp: DWord;
  2490. begin
  2491. result:=ord(AValue>$FFFF)*16;
  2492. tmp:=AValue shr result;
  2493. result:=result or (ord(tmp>$FF)*8);
  2494. tmp:=tmp shr (result and 8);
  2495. result:=result or BsrByte(byte(tmp));
  2496. end;
  2497. {$endif}
  2498. {$endif}
  2499. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2500. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2501. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2502. var
  2503. tmp: DWord;
  2504. begin
  2505. result:=0;
  2506. tmp:=lo(AValue);
  2507. if (tmp=0) then
  2508. begin
  2509. tmp:=hi(AValue);
  2510. result:=32;
  2511. end;
  2512. result:=result or BsfDword(tmp);
  2513. end;
  2514. {$endif}
  2515. {$endif}
  2516. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2517. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2518. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2519. var
  2520. tmp: DWord;
  2521. begin
  2522. result:=32;
  2523. tmp:=hi(AValue);
  2524. if (tmp=0) then
  2525. begin
  2526. tmp:=lo(AValue);
  2527. result:=0;
  2528. end;
  2529. result:=result or BsrDword(tmp);
  2530. end;
  2531. {$endif}
  2532. {$endif}
  2533. const
  2534. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2535. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2536. begin
  2537. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2538. end;
  2539. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2540. var
  2541. i : SizeInt;
  2542. begin
  2543. Result:=0;
  2544. for i:=0 to 3 do
  2545. begin
  2546. inc(Result,PopCntData[AValue and $f]);
  2547. AValue:=AValue shr 4;
  2548. end;
  2549. end;
  2550. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2551. var
  2552. i : SizeInt;
  2553. begin
  2554. Result:=0;
  2555. for i:=0 to 7 do
  2556. begin
  2557. inc(Result,PopCntData[AValue and $f]);
  2558. AValue:=AValue shr 4;
  2559. end;
  2560. end;
  2561. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2562. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2563. {$ifdef VER2_6}
  2564. var
  2565. i : SizeInt;
  2566. begin
  2567. Result:=0;
  2568. for i:=0 to 15 do
  2569. begin
  2570. inc(Result,PopCntData[AValue and $f]);
  2571. AValue:=AValue shr 4;
  2572. end;
  2573. {$else VER2_6}
  2574. begin
  2575. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2576. {$endif VER2_6}
  2577. end;
  2578. {$endif}