generic.inc 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  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. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  13. var
  14. tmp,am1 : PtrUInt;
  15. begin
  16. am1:=alignment-1;
  17. tmp:=addr+am1;
  18. if alignment and am1=0 then
  19. { Alignment is a power of two. In practice alignments are powers of two 100% of the time. }
  20. result:=tmp and not am1
  21. else
  22. result:=tmp-(tmp mod alignment);
  23. end;
  24. {$ifndef cpujvm}
  25. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  26. var
  27. tmp,am1 : PtrUInt;
  28. begin
  29. am1:=alignment-1;
  30. tmp:=PtrUint(addr)+am1;
  31. if alignment and am1=0 then
  32. result:=pointer(tmp and not am1)
  33. else
  34. result:=pointer(ptruint(tmp-(tmp mod alignment)));
  35. end;
  36. {$endif}
  37. {****************************************************************************
  38. Primitives
  39. ****************************************************************************}
  40. type
  41. pstring = ^shortstring;
  42. {$ifndef FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  43. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  44. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  45. begin
  46. move(src[srcindex],dst[dstindex],len);
  47. end;
  48. {$endif FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  49. {$ifndef FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  50. {$define FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  51. procedure fpc_shortstr_chararray_intern_charmove(const src: shortstring; out dst: array of ansichar; const len: sizeint);
  52. begin
  53. move(src[1],PAnsiChar(@dst)^,len);
  54. end;
  55. {$endif FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  56. {$ifndef FPC_SYSTEM_HAS_MOVE}
  57. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  58. var
  59. aligncount : sizeint;
  60. pdest,psrc,pend : pbyte;
  61. begin
  62. if (@dest=@source) or (count<=0) then
  63. exit;
  64. if (@dest<@source) or (@source+count<@dest) then
  65. begin
  66. { Forward Move }
  67. psrc:=@source;
  68. pdest:=@dest;
  69. if (Count>4*sizeof(ptruint)-11)
  70. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  71. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  72. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  73. then
  74. begin
  75. { Align on native pointer size }
  76. aligncount:=(sizeof(PtrUInt)-PtrInt(pdest)) and (sizeof(PtrUInt)-1);
  77. dec(count,aligncount);
  78. pend:=psrc+aligncount;
  79. while psrc<pend do
  80. begin
  81. pdest^:=psrc^;
  82. inc(pdest);
  83. inc(psrc);
  84. end;
  85. { use sizeuint typecast to force shr optimization }
  86. pptruint(pend):=pptruint(psrc)+(sizeuint(count) div sizeof(ptruint));
  87. while psrc<pend do
  88. begin
  89. pptruint(pdest)^:=pptruint(psrc)^;
  90. inc(pptruint(pdest));
  91. inc(pptruint(psrc));
  92. end;
  93. count:=count and (sizeof(PtrUInt)-1);
  94. end;
  95. pend:=psrc+count;
  96. while psrc<pend do
  97. begin
  98. pdest^:=psrc^;
  99. inc(pdest);
  100. inc(psrc);
  101. end;
  102. end
  103. else
  104. begin
  105. { Backward Move }
  106. psrc:=@source+count;
  107. pdest:=@dest+count;
  108. if (Count>4*sizeof(ptruint)-11)
  109. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  110. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  111. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  112. then
  113. begin
  114. { Align on native pointer size }
  115. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  116. dec(count,aligncount);
  117. pend:=psrc-aligncount;
  118. while psrc>pend do
  119. begin
  120. dec(pdest);
  121. dec(psrc);
  122. pdest^:=psrc^;
  123. end;
  124. { use sizeuint typecast to force shr optimization }
  125. pptruint(pend):=pptruint(psrc)-(sizeuint(count) div sizeof(ptruint));
  126. while psrc>pend do
  127. begin
  128. dec(pptruint(pdest));
  129. dec(pptruint(psrc));
  130. pptruint(pdest)^:=pptruint(psrc)^;
  131. end;
  132. count:=count and (sizeof(PtrUInt)-1);
  133. end;
  134. pend:=psrc-count;
  135. while psrc>pend do
  136. begin
  137. dec(pdest);
  138. dec(psrc);
  139. pdest^:=psrc^;
  140. end;
  141. end;
  142. end;
  143. {$endif not FPC_SYSTEM_HAS_MOVE}
  144. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  145. Procedure FillChar(var x;count:SizeInt;value:byte);
  146. var
  147. pdest,pend : pbyte;
  148. v : ALUUInt;
  149. begin
  150. if count <= 0 then
  151. exit;
  152. pdest:=@x;
  153. if Count>4*sizeof(ptruint)-1 then
  154. begin
  155. {$if sizeof(v)>=2}
  156. v:=(value shl 8) or value;
  157. {$endif sizeof(v)>=2}
  158. {$if sizeof(v)>=4}
  159. v:=(v shl 16) or v;
  160. {$endif sizeof(v)>=4}
  161. {$if sizeof(v)=8}
  162. v:=(v shl 32) or v;
  163. {$endif sizeof(v)=8}
  164. { Align on native pointer size }
  165. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  166. dec(count,pend-pdest);
  167. while pdest<pend do
  168. begin
  169. pdest^:=value;
  170. inc(pdest);
  171. end;
  172. { use sizeuint typecast to force shr optimization }
  173. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  174. while pdest<pend do
  175. begin
  176. pptruint(pdest)^:=v;
  177. inc(pptruint(pdest));
  178. end;
  179. count:=count and (sizeof(ptruint)-1);
  180. end;
  181. pend:=pdest+count;
  182. while pdest<pend do
  183. begin
  184. pdest^:=value;
  185. inc(pdest);
  186. end;
  187. end;
  188. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  189. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  190. procedure fillword(var x;count : SizeInt;value : word);
  191. var
  192. aligncount : sizeint;
  193. pdest,pend : pword;
  194. v : ALUUInt;
  195. begin
  196. if count <= 0 then
  197. exit;
  198. pdest:=@x;
  199. if Count>4*sizeof(ptruint)-1 then
  200. begin
  201. {$if sizeof(v)>=4}
  202. v:=(value shl 16) or value;
  203. {$endif sizeof(v)>=4}
  204. {$if sizeof(v)=8}
  205. v:=(v shl 32) or v;
  206. {$endif sizeof(v)=8}
  207. { Align on native pointer size }
  208. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  209. dec(count,aligncount);
  210. pend:=pdest+aligncount;
  211. while pdest<pend do
  212. begin
  213. pdest^:=value;
  214. inc(pdest);
  215. end;
  216. { use sizeuint typecast to force shr optimization }
  217. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  218. while pdest<pend do
  219. begin
  220. pptruint(pdest)^:=v;
  221. inc(pptruint(pdest));
  222. end;
  223. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  224. end;
  225. pend:=pdest+count;
  226. while pdest<pend do
  227. begin
  228. pdest^:=value;
  229. inc(pdest);
  230. end;
  231. end;
  232. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  233. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  234. procedure filldword(var x;count : SizeInt;value : dword);
  235. var
  236. aligncount : sizeint;
  237. pdest,pend : pdword;
  238. v : ALUUInt;
  239. begin
  240. if count <= 0 then
  241. exit;
  242. pdest:=@x;
  243. if Count>4*sizeof(ptruint)-1 then
  244. begin
  245. v:=value;
  246. {$if sizeof(v)=8}
  247. v:=(v shl 32) or v;
  248. {$endif sizeof(v)=8}
  249. { Align on native pointer size }
  250. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  251. dec(count,aligncount);
  252. pend:=pdest+aligncount;
  253. while pdest<pend do
  254. begin
  255. pdest^:=value;
  256. inc(pdest);
  257. end;
  258. { use sizeuint typecast to force shr optimization }
  259. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  260. while pdest<pend do
  261. begin
  262. pptruint(pdest)^:=v;
  263. inc(pptruint(pdest));
  264. end;
  265. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  266. end;
  267. pend:=pdest+count;
  268. while pdest<pend do
  269. begin
  270. pdest^:=value;
  271. inc(pdest);
  272. end;
  273. end;
  274. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  275. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  276. procedure fillqword(var x;count : SizeInt;value : qword);
  277. var
  278. pdest,pend : pqword;
  279. begin
  280. if count <= 0 then
  281. exit;
  282. pdest:=@x;
  283. pend:=pdest+count;
  284. while pdest<pend do
  285. begin
  286. pdest^:=value;
  287. inc(pdest);
  288. end;
  289. end;
  290. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  291. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  292. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  293. var
  294. psrc,pend : pbyte;
  295. begin
  296. psrc:=@buf;
  297. pend:=psrc+len;
  298. { simulate assembler implementations behaviour, which is expected }
  299. { fpc_pchar_to_ansistr in astrings.inc }
  300. if (len < 0) or
  301. (pend < psrc) then
  302. pend:=pbyte(high(PtrUInt)-PtrUint(sizeof(byte)));
  303. while (psrc<pend) and (psrc^<>b) do
  304. inc(psrc);
  305. if psrc<pend then
  306. result:=psrc-pbyte(@buf)
  307. else
  308. result:=-1;
  309. end;
  310. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  311. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  312. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  313. var
  314. psrc,pend : pword;
  315. begin
  316. psrc:=@buf;
  317. pend:=psrc+len;
  318. { simulate assembler implementations behaviour, which is expected }
  319. { fpc_pchar_to_ansistr in astrings.inc }
  320. if not (
  321. (len >= 0) and
  322. { is this ever false? }
  323. (len <= high(PtrInt))) or
  324. (pend < psrc) then
  325. pend:=pword(high(PtrUInt)-PtrUint(sizeof(word)));
  326. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  327. if (ptruint(psrc) mod 2)<>0 then
  328. while (psrc<pend) and (unaligned(psrc^)<>b) do
  329. inc(psrc)
  330. else
  331. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  332. while (psrc<pend) and (psrc^<>b) do
  333. inc(psrc);
  334. if psrc<pend then
  335. { the result is always >=0 so avoid handling of negative values }
  336. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(word)
  337. else
  338. result:=-1;
  339. end;
  340. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  341. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  342. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  343. var
  344. psrc,pend : pdword;
  345. begin
  346. psrc:=@buf;
  347. pend:=psrc+len;
  348. { simulate assembler implementations behaviour, which is expected }
  349. { fpc_pchar_to_ansistr in astrings.inc }
  350. if not (
  351. (len >= 0) and
  352. (len <= high(PtrInt) div 2)) or
  353. (pend < psrc) then
  354. pend:=pdword(high(PtrUInt)-PtrUInt(sizeof(dword)));
  355. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  356. if (ptruint(psrc) mod 4)<>0 then
  357. while (psrc<pend) and (unaligned(psrc^)<>b) do
  358. inc(psrc)
  359. else
  360. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  361. while (psrc<pend) and (psrc^<>b) do
  362. inc(psrc);
  363. if psrc<pend then
  364. { the result is always >=0 so avoid handling of negative values }
  365. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(DWord)
  366. else
  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. pend:=psrc+len;
  377. { simulate assembler implementations behaviour, which is expected }
  378. { fpc_pchar_to_ansistr in astrings.inc }
  379. if not (
  380. (len >= 0) and
  381. (len <= high(PtrInt) div 4)) or
  382. (pend < psrc) then
  383. pend:=pqword(high(PtrUInt)-PtrUInt(sizeof(qword)));
  384. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  385. if (ptruint(psrc) mod 8)<>0 then
  386. while (psrc<pend) and (unaligned(psrc^)<>b) do
  387. inc(psrc)
  388. else
  389. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  390. while (psrc<pend) and (psrc^<>b) do
  391. inc(psrc);
  392. if psrc<pend then
  393. { the result is always >=0 so avoid handling of negative values }
  394. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(QWord)
  395. else
  396. result:=-1;
  397. end;
  398. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  399. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  400. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  401. var
  402. psrc,pdest,pend,pendpart : pbyte;
  403. begin
  404. psrc:=@buf1;
  405. pdest:=@buf2;
  406. pend:=psrc+len;
  407. if (pend<psrc) then
  408. pend:=pbyte(high(ptruint));
  409. if (len>=2*sizeof(ptruint))
  410. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  411. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  412. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  413. then
  414. begin
  415. { Align "psrc" on native pointer size. }
  416. PtrUint(pendpart):=PtrUint(psrc+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  417. if psrc<pendpart then
  418. begin
  419. while (psrc<pendpart) and (psrc^=pdest^) do
  420. begin
  421. inc(pdest);
  422. inc(psrc);
  423. end;
  424. if psrc<pendpart then
  425. exit(sizeint(psrc^)-sizeint(pdest^));
  426. end;
  427. { "pend" is the end of "psrc" and "psrc" is aligned, so aligned "pend" can be obtained this way. }
  428. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  429. while (psrc<pendpart) and (pptruint(psrc)^=pptruint(pdest)^) do
  430. begin
  431. inc(pptruint(pdest));
  432. inc(pptruint(psrc));
  433. end;
  434. if psrc<pendpart then
  435. pend:=psrc+sizeof(ptruint);
  436. end;
  437. while (psrc<pend) and (psrc^=pdest^) do
  438. begin
  439. inc(pdest);
  440. inc(psrc);
  441. end;
  442. if psrc<pend then
  443. exit(sizeint(psrc^)-sizeint(pdest^));
  444. result:=0;
  445. end;
  446. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  447. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  448. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  449. var
  450. psrc,pdest,pend,pendpart : pword;
  451. begin
  452. psrc:=@buf1;
  453. pdest:=@buf2;
  454. pend:=psrc+len;
  455. if (pend<psrc) or not ((len>=0) and (len<=High(PtrInt) div 2)) then
  456. pend:=pword(high(ptruint)-2);
  457. if (len>=2*sizeof(ptruint)) { len in words, so at least four pointers }
  458. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  459. and ((PtrUInt(pdest) xor PtrUInt(psrc)) and (sizeof(PtrUInt)-1)=0)
  460. and (PtrUInt(psrc) and 1=0)
  461. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  462. then
  463. begin
  464. { Align on native pointer size. Careful, these 'pendpart's are aligned even if 'psrc' is misaligned, so "psrc<>pendpart" must not be used. }
  465. PtrUint(pendpart):=(PtrUint(psrc)+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  466. while (psrc<pendpart) and (psrc^=pdest^) do
  467. begin
  468. inc(pdest);
  469. inc(psrc);
  470. end;
  471. if psrc<pendpart then
  472. exit(2*ord(psrc^>pdest^)-1);
  473. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  474. while (psrc<pendpart) and (pptrint(psrc)^=pptrint(pdest)^) do
  475. begin
  476. inc(pptruint(pdest));
  477. inc(pptruint(psrc));
  478. end;
  479. if psrc<pendpart then
  480. pointer(pend):=pointer(psrc)+sizeof(ptruint);
  481. end;
  482. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  483. if (PtrUInt(pdest) or PtrUInt(psrc)) and 1<>0 then
  484. begin
  485. while (psrc<pend) and (unaligned(psrc^)=unaligned(pdest^)) do
  486. begin
  487. inc(pdest);
  488. inc(psrc);
  489. end;
  490. if psrc<pend then
  491. exit(2*ord(unaligned(psrc^)>unaligned(pdest^))-1);
  492. end
  493. else
  494. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  495. begin
  496. while (psrc<pend) and (psrc^=pdest^) do
  497. begin
  498. inc(pdest);
  499. inc(psrc);
  500. end;
  501. if psrc<pend then
  502. exit(2*ord(psrc^>pdest^)-1);
  503. end;
  504. result:=0;
  505. end;
  506. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  507. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  508. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  509. var
  510. psrc,pdest,pend,pendpart : pdword;
  511. begin
  512. psrc:=@buf1;
  513. pdest:=@buf2;
  514. pend:=psrc+len;
  515. if (pend<psrc) or not ((len>=0) and (len<=High(PtrInt) div 4)) then
  516. pend:=pdword(high(ptruint)-4);
  517. {$if sizeof(ptruint)>sizeof(dword)}
  518. if (len>=sizeof(ptruint)) { len in uint32s, so at least four pointers }
  519. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  520. and ((PtrUInt(pdest) xor PtrUInt(psrc)) and (sizeof(PtrUInt)-1)=0)
  521. and (PtrUInt(psrc) and 3=0)
  522. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  523. then
  524. begin
  525. { Align on native pointer size. Careful, these 'pendpart's are aligned even if 'psrc' is misaligned, so "psrc<>pendpart" must not be used. }
  526. PtrUint(pendpart):=(PtrUint(psrc)+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  527. while (psrc<pendpart) and (psrc^=pdest^) do
  528. begin
  529. inc(pdest);
  530. inc(psrc);
  531. end;
  532. if psrc<pendpart then
  533. exit(2*ord(psrc^>pdest^)-1);
  534. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  535. while (psrc<pendpart) and (pptrint(psrc)^=pptrint(pdest)^) do
  536. begin
  537. inc(pptruint(pdest));
  538. inc(pptruint(psrc));
  539. end;
  540. if psrc<pendpart then
  541. pointer(pend):=pointer(psrc)+sizeof(ptruint);
  542. end;
  543. {$endif sizeof(ptruint)>sizeof(dword)}
  544. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  545. if (PtrUInt(pdest) or PtrUInt(psrc)) and 3<>0 then
  546. begin
  547. while (psrc<pend) and (unaligned(psrc^)=unaligned(pdest^)) do
  548. begin
  549. inc(pdest);
  550. inc(psrc);
  551. end;
  552. if psrc<pend then
  553. exit(2*ord(unaligned(psrc^)>unaligned(pdest^))-1);
  554. end
  555. else
  556. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  557. begin
  558. while (psrc<pend) and (psrc^=pdest^) do
  559. begin
  560. inc(pdest);
  561. inc(psrc);
  562. end;
  563. if psrc<pend then
  564. exit(2*ord(psrc^>pdest^)-1);
  565. end;
  566. result:=0;
  567. end;
  568. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  569. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  570. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  571. var
  572. I : SizeInt;
  573. begin
  574. if Len = 0 then
  575. exit;
  576. I:=IndexByte(Buf1,Len,0);
  577. if I<>-1 then
  578. Move(Buf1,Buf2,I)
  579. else
  580. Move(Buf1,Buf2,len);
  581. end;
  582. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  583. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  584. function IndexChar0(Const buf;len:SizeInt;b:AnsiChar):SizeInt;
  585. var
  586. psrc,pend : pbyte;
  587. begin
  588. psrc:=@buf;
  589. { simulate assembler implementations behaviour, which is expected }
  590. { fpc_pchar_to_ansistr in astrings.inc }
  591. if (len < 0) then
  592. pend:=pbyte(high(PtrUInt)-PtrUInt(sizeof(byte)))
  593. else
  594. pend:=psrc+len;
  595. while (psrc<pend) and (psrc^<>0) do
  596. begin
  597. if (psrc^=byte(b)) then
  598. begin
  599. result:=psrc-pbyte(@buf);
  600. exit;
  601. end;
  602. inc(psrc);
  603. end;
  604. result:=-1;
  605. end;
  606. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  607. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  608. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  609. var
  610. psrc,pdest,pend : pbyte;
  611. b : ptrint;
  612. begin
  613. b:=0;
  614. psrc:=@buf1;
  615. pdest:=@buf2;
  616. pend:=psrc+len;
  617. while psrc<pend do
  618. begin
  619. b:=(ptrint(psrc^)-ptrint(pdest^));
  620. if b<0 then
  621. exit(-1)
  622. else if b>0 then
  623. exit(1);
  624. if (psrc^=0) or (pdest^=0) then
  625. exit(0);
  626. inc(pdest);
  627. inc(psrc);
  628. end;
  629. result:=0;
  630. end;
  631. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  632. {****************************************************************************
  633. Object Helpers
  634. ****************************************************************************}
  635. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  636. type
  637. pobjectvmt=^tobjectvmt;
  638. tobjectvmt=record
  639. size,msize:sizeuint;
  640. parent:ppointer;
  641. end;
  642. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  643. { Note: _vmt will be reset to -1 when memory is allocated,
  644. this is needed for fpc_help_fail }
  645. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  646. var
  647. vmtcopy : pobjectvmt;
  648. begin
  649. vmtcopy:=pobjectvmt(_vmt);
  650. { Inherited call? }
  651. if vmtcopy=nil then
  652. begin
  653. fpc_help_constructor:=_self;
  654. exit;
  655. end;
  656. if (_self=nil) and
  657. (vmtcopy^.size>0) then
  658. begin
  659. getmem(_self,vmtcopy^.size);
  660. { reset vmt needed for fail }
  661. _vmt:=pointer(-1);
  662. end;
  663. if _self<>nil then
  664. begin
  665. fillchar(_self^,vmtcopy^.size,0);
  666. ppointer(_self+_vmt_pos)^:=vmtcopy;
  667. end;
  668. fpc_help_constructor:=_self;
  669. end;
  670. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  671. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  672. { Note: _self will not be reset, the compiler has to generate the reset }
  673. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  674. begin
  675. { already released? }
  676. if (_self=nil) or
  677. (_vmt<>pointer(-1)) or
  678. (ppointer(_self+vmt_pos)^=nil) then
  679. exit;
  680. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  681. (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  682. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  683. { reset vmt to nil for protection }
  684. ppointer(_self+vmt_pos)^:=nil;
  685. freemem(_self);
  686. end;
  687. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  688. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  689. { Note: _self will not be reset, the compiler has to generate the reset }
  690. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  691. begin
  692. if (_self=nil) or (_vmt=nil) then
  693. exit;
  694. { vmt=$ffffffff when memory was allocated }
  695. if ptruint(_vmt)=high(ptruint) then
  696. begin
  697. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  698. HandleError(210)
  699. else
  700. begin
  701. ppointer(_self+vmt_pos)^:=nil;
  702. freemem(_self);
  703. { reset _vmt to nil so it will not be freed a
  704. second time }
  705. _vmt:=nil;
  706. end;
  707. end
  708. else
  709. ppointer(_self+vmt_pos)^:=nil;
  710. end;
  711. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  712. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  713. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  714. begin
  715. if (_vmt=nil) or
  716. (pobjectvmt(_vmt)^.size=0) or
  717. (pobjectvmt(_vmt)^.size+pobjectvmt(_vmt)^.msize<>0) then
  718. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  719. end;
  720. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  721. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  722. { checks for a correct vmt pointer }
  723. { deeper check to see if the current object is }
  724. { really related to the true }
  725. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  726. begin
  727. if (vmt=nil) or
  728. (pobjectvmt(vmt)^.size=0) or
  729. (pobjectvmt(vmt)^.size+pobjectvmt(vmt)^.msize<>0) then
  730. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  731. while assigned(vmt) do
  732. if vmt=expvmt then
  733. exit
  734. else
  735. if assigned(pobjectvmt(vmt)^.parent) then
  736. vmt:=pobjectvmt(vmt)^.parent^
  737. else
  738. vmt:=nil;
  739. HandleErrorAddrFrameInd(219,get_pc_addr,get_frame);
  740. end;
  741. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  742. {$endif FPC_HAS_FEATURE_OBJECTS}
  743. {****************************************************************************
  744. String
  745. ****************************************************************************}
  746. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  747. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  748. var
  749. slen : byte;
  750. begin
  751. slen:=length(sstr);
  752. if slen>high(res) then
  753. slen:=high(res);
  754. move(sstr[0],res[0],slen+1);
  755. res[0]:=chr(slen);
  756. end;
  757. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  758. var
  759. slen : byte;
  760. begin
  761. slen:=length(pshortstring(sstr)^);
  762. if slen<len then
  763. len:=slen;
  764. move(sstr^,dstr^,len+1);
  765. if slen>len then
  766. PAnsiChar(dstr)^:=chr(len);
  767. end;
  768. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  769. {$push}
  770. { ensure that comparing addresses of openshortstrings with regular shortstrings
  771. doesn't cause errors }
  772. {$t-}
  773. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  774. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  775. var
  776. s1l, s2l : ObjpasInt;
  777. begin
  778. s1l:=length(s1);
  779. s2l:=length(s2);
  780. if s1l+s2l>high(dests) then
  781. begin
  782. if s1l>high(dests) then
  783. s1l:=high(dests);
  784. s2l:=high(dests)-s1l;
  785. end;
  786. { Copy s2 first, as in the case of @dests = @s2 it must be copied first and in other cases the order does not matter. }
  787. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  788. if @dests<>@s1 then
  789. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  790. dests[0]:=chr(s1l+s2l);
  791. end;
  792. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  793. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  794. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  795. var
  796. i,s2l,Len,destpos0 : ObjpasInt;
  797. p : pshortstring;
  798. begin
  799. Len:=0;
  800. i:=0;
  801. while (i<=high(sarr)) do
  802. begin
  803. p:=sarr[i];
  804. if assigned(p) then
  805. inc(Len,length(p^));
  806. inc(i);
  807. end;
  808. destpos0:=Len;
  809. { Copy strings from the last to the first, so that possible occurences of DestS read correct DestS.
  810. DestS[0] = length(DestS) must have its original value for a while! }
  811. while (destpos0>0) do
  812. begin
  813. dec(i);
  814. p:=sarr[i];
  815. if not assigned(p) then
  816. continue;
  817. s2l:=length(p^);
  818. dec(destpos0,s2l);
  819. if (destpos0=0) and (p=@dests) then { Skip moving DestS to itself when appending. This destpos0-based form also catches DestS := '' + '' + DestS. }
  820. break;
  821. if destpos0+s2l>high(dests) then
  822. begin
  823. if destpos0>=high(dests) then
  824. continue;
  825. s2l:=high(dests)-destpos0;
  826. end;
  827. fpc_shortstr_shortstr_intern_charmove(p^,1,dests,destpos0+1,s2l);
  828. end;
  829. if Len>high(dests) then
  830. Len:=high(dests);
  831. dests[0]:=Chr(Len); { Careful, loop above relies on DestS[0] having the original value. }
  832. end;
  833. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  834. {$pop}
  835. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  836. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  837. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  838. var
  839. s1l, s2l : sizeint;
  840. begin
  841. s1l:=length(s1);
  842. s2l:=length(s2);
  843. if s1l+s2l>high(s1) then
  844. s2l:=high(s1)-s1l;
  845. move(s2[1],s1[s1l+1],s2l);
  846. s1[0]:=chr(s1l+s2l);
  847. end;
  848. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  849. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  850. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  851. var
  852. s1,s2,max : byte;
  853. begin
  854. s1:=length(left);
  855. s2:=length(right);
  856. if s1<s2 then
  857. max:=s1
  858. else
  859. max:=s2;
  860. result:=CompareByte(left[1],right[1],max);
  861. if result=0 then
  862. result:=s1-s2;
  863. result:=ord(result>0)-ord(result<0);
  864. end;
  865. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  866. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  867. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  868. begin
  869. Result := ObjpasInt(left[0]) - ObjpasInt(right[0]);
  870. if Result = 0 then
  871. Result := CompareByte(left[1],right[1], ObjpasInt(left[0]));
  872. end;
  873. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  874. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  875. procedure fpc_pchar_to_shortstr(out res : shortstring;p:PAnsiChar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  876. var
  877. l : ObjpasInt;
  878. begin
  879. if p=nil then
  880. begin
  881. res[0]:=#0;
  882. exit;
  883. end;
  884. { On platforms where IndexByte with len > 0 will not read the invalid memory past the null terminator, high(res) can be used as a limit. }
  885. {$if defined(cpui386) or defined(cpux86_64)}
  886. l:=IndexByte(p^,high(res),0);
  887. if l<0 then
  888. l:=high(res);
  889. {$else IndexByte(p^,high(res),0) can crash}
  890. l:=strlen(p);
  891. if l>high(res) then
  892. l:=high(res);
  893. {$endif IndexByte(p^,high(res),0) can crash}
  894. move(p^,res[1],l);
  895. res[0]:=chr(l);
  896. end;
  897. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  898. {$ifndef cpujvm}
  899. { also define alias which can be used inside the system unit }
  900. procedure fpc_pchar_to_shortstr(out res : shortstring;p:PAnsiChar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  901. function strpas(p:PAnsiChar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  902. begin
  903. fpc_pchar_to_shortstr(result,p);
  904. end;
  905. {$endif not cpujvm}
  906. { Combining codepoints are those belonging to one of the three "Mark" general categories.
  907. UnicodeData.txt column 3 has M* for them: Mn, Mc, Me.
  908. Using the table below, codepoint %...XXXXXXXX_YYYY_ZZZZZ can be classified as combining or not with a 3-level lookup:
  909. if %...XXXXXXXX <= High(IsCombinings.L2) then
  910. begin
  911. index := IsCombinings.L2[%XXXXXXXX];
  912. index := IsCombinings.L1[index][%YYYY];
  913. IsCombining := boolean(IsCombinings.L0[index] shr %ZZZZZ and 1);
  914. end else
  915. IsCombining := false;
  916. Equivalent one-liner:
  917. IsCombining := (%...XXXXXXXX <= High(IsCombinings.L2)) and (IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[%XXXXXXXX]][%YYYY]] shr %ZZZZZ and 1 <> 0);
  918. Additionally, there is a combining range U+E0100..U+E01EF far to the right, not included into the table to save 1 level.
  919. Table built from UnicodeData.txt 15.0.0 (September 2022). }
  920. const
  921. IsCombinings: record
  922. L2: array[0 .. 244] of uint8;
  923. L1: array[0 .. 46, 0 .. 15] of uint8;
  924. L0: array[0 .. 161] of uint32;
  925. end =
  926. (
  927. L2: (
  928. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, {10} 0, 10, 11, 12, 13, 0, 14, 0, 0, 0, {20} 0, 0, 15, 0, 16, 0, 0, 0, 0, 0, {30} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  929. {40} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {50} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {60} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {70} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {80} 0,
  930. 0, 0, 17, 18, 19, 0, 0, 0, 0, {90} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {100} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {110} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {120} 0, 0,
  931. 0, 0, 0, 20, 0, 21, 22, 23, {130} 0, 0, 0, 24, 25, 26, 27, 28, 29, 30, {140} 31, 32, 33, 34, 0, 0, 0, 0, 0, 0, {150} 0, 0, 0, 0, 35, 0, 0, 0, 0, 0,
  932. {160} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {170} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {180} 0, 36, 0, 37, 0, 0, 0, 0, 0, 0, {190} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  933. {200} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {210} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {220} 0, 0, 38, 0, 0, 0, 0, 0, 0, 0, {230} 0, 39, 40, 41, 0, 0, 0, 42, 0, 0,
  934. {240} 43, 44, 45, 0, 46
  935. );
  936. L1: (
  937. {0} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 0, 0, 0, 0),
  938. {2} (0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0), (7, 0, 8, 9, 0, 0, 10, 11, 12, 13, 14, 0, 0, 15, 0, 16),
  939. {4} (17, 18, 19, 0, 20, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), (27, 28, 31, 32, 27, 28, 33, 34, 27, 28, 35, 26, 36, 37, 38, 0),
  940. {6} (39, 28, 40, 26, 27, 28, 40, 41, 23, 42, 43, 26, 27, 0, 44, 45), (0, 46, 47, 0, 0, 48, 49, 0, 50, 51, 0, 4, 52, 53, 54, 0),
  941. {8} (0, 55, 56, 57, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0, 0, 0, 0, 0),
  942. {10} (0, 0, 0, 0, 0, 0, 0, 0, 60, 61, 45, 45, 0, 62, 63, 0), (64, 0, 0, 0, 65, 66, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0),
  943. {12} (68, 0, 69, 70, 0, 13, 1, 1, 39, 62, 39, 71, 72, 73, 0, 74), (0, 75, 0, 0, 0, 0, 76, 77, 0, 0, 0, 0, 0, 0, 1, 1),
  944. {14} (0, 0, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 78, 0, 0, 0, 79, 0, 0, 0, 1),
  945. {16} (0, 80, 0, 0, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 82, 37, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0),
  946. {18} (84, 85, 0, 0, 86, 62, 87, 88, 0, 89, 90, 0, 23, 91, 92, 93), (0, 94, 95, 96, 0, 97, 98, 99, 0, 0, 0, 0, 0, 0, 0, 100),
  947. {20} (0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0, 0), (2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  948. {22} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102), (0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 103, 0, 0, 0, 0),
  949. {24} (104, 105, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0),
  950. {26} (0, 0, 0, 0, 0, 107, 0, 59, 0, 0, 15, 0, 108, 0, 0, 0), (72, 20, 109, 110, 72, 7, 36, 0, 72, 111, 65, 112, 72, 91, 113, 0),
  951. {28} (0, 114, 98, 0, 0, 0, 79, 14, 23, 42, 29, 115, 0, 0, 0, 0), (0, 116, 117, 0, 0, 13, 23, 0, 0, 0, 0, 0, 0, 118, 119, 0),
  952. {30} (0, 13, 92, 0, 0, 120, 0, 0, 59, 121, 0, 0, 0, 0, 0, 0), (0, 122, 0, 0, 0, 0, 0, 0, 0, 123, 124, 0, 0, 0, 125, 126),
  953. {32} (127, 128, 129, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 131, 0, 0, 132, 133, 0, 0, 0, 134, 135, 0, 136, 0, 0, 0),
  954. {34} (0, 0, 0, 0, 0, 0, 0, 137, 138, 139, 72, 0, 0, 0, 0, 0), (0, 0, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  955. {36} (0, 0, 0, 0, 0, 0, 0, 141, 0, 142, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 1, 144, 0, 0, 145),
  956. {38} (0, 0, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 147, 109, 0, 0, 0, 0, 0),
  957. {40} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 149, 150, 0, 0), (0, 0, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  958. {42} (1, 152, 1, 153, 154, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (156, 157, 0, 0, 158, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0),
  959. {44} (0, 0, 0, 0, 0, 159, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0),
  960. {46} (0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 161, 0, 0, 0, 0, 0)
  961. );
  962. L0: (
  963. {0} $00000000, $FFFFFFFF, $0000FFFF, $000003F8, $FFFE0000, $BFFFFFFF, $000000B6, $07FF0000, $FFFFF800, $00010000, $9FC00000, $00003D9F, $00020000,
  964. {13} $FFFF0000, $000007FF, $0001FFC0, $200FF800, $FBC00000, $00003EEF, $0E000000, $FF000000, $FFFFFC00, $FFFFFFFB, $0000000F, $DC000000, $00FEFFFF,
  965. {26} $0000000C, $0000000E, $D0000000, $0080399F, $4000000C, $00023987, $00230000, $00003BBF, $FC00000C, $00E0399F, $00000004, $C0000000, $00803DC7,
  966. {39} $0000001F, $00603DDF, $0008000C, $D8000000, $00803DDF, $FF5F8400, $000C0000, $07F20000, $00007F80, $1FF20000, $00007F00, $03000000, $C2A00000,
  967. {52} $FEFFE0DF, $1FFFFFFF, $00000040, $7FFFF800, $C3C00000, $001E3F9D, $3C00BFFC, $E0000000, $003C0000, $001C0000, $FFF00000, $200FFFFF, $0000B800,
  968. {65} $00000060, $00000200, $0FFF0FFF, $0F800000, $7FE00000, $9FFFFFFF, $000FF800, $00000007, $00003FFE, $000FFFC0, $00FFFFF0, $FFF70000, $039021FF,
  969. {78} $00038000, $80000000, $0000FC00, $06000000, $3FF78000, $00030000, $00000844, $000010F8, $00000003, $0000003F, $8003FFFF, $00003FC0, $000FFF80,
  970. {91} $FFF80000, $00000001, $00000020, $007FFE00, $00003008, $38000000, $C19D0000, $00000002, $0060F800, $000037F8, $40000000, $20000000, $07C00000,
  971. {104} $0000F06E, $87000000, $000000F0, $00001800, $0000003C, $0000007F, $80190000, $001FFF80, $00080000, $0000DE01, $40FFF000, $001F1FCC, $FFE00000,
  972. {117} $4000007F, $FF3F8000, $30000001, $00FFF800, $00000FFF, $07FFF000, $79BF0000, $0000000D, $FCFE0000, $00000011, $000007FE, $7BF80000, $0FFE0080,
  973. {130} $03FFFC00, $FF7F8000, $FFFC0000, $007FFEFF, $B47E0000, $000000BF, $00FB7C00, $00780000, $0000000B, $C7F00000, $003FFF81, $001F0000, $007F0000,
  974. {143} $FFFE8000, $000780FF, $00030010, $60000000, $FFFF3FFF, $F807E3E0, $00000FE7, $00003C00, $0000001C, $F87FFFFF, $00201FFF, $F8000010, $0000FFFE,
  975. {156} $F9FFFF7F, $000007DB, $00008000, $00004000, $0000F000, $000007F0
  976. );
  977. );
  978. function Utf8CodePointLen(P: PAnsiChar; MaxLookAhead: SizeInt; IncludeCombiningDiacriticalMarks: Boolean): SizeInt;
  979. var
  980. cp: uint32;
  981. iByte,cpLen: SizeInt;
  982. begin
  983. { see https://en.wikipedia.org/wiki/UTF-8#Description for details }
  984. result:=0;
  985. { result = 0 when scanning first character, result > 0 when scanning potential diacritical marks following it.
  986. Common case is correct UTF-8.
  987. Setting cpLen and breaking from the loop (instead of exiting) will handle invalid/incomplete cases
  988. when cpLen bytes were expected, but not all are present/valid.
  989. This keeps the code more compact, both source and binary. }
  990. repeat
  991. if MaxLookAhead<1 then
  992. exit;
  993. case ord(P[result]) of
  994. { One-byte codepoints have the form
  995. %(0)xxxxxxx. }
  996. 0..$7F {%01111111}:
  997. { There are no diacritics among them. }
  998. if not IncludeCombiningDiacriticalMarks then
  999. exit(1)
  1000. else if result=0 then
  1001. begin
  1002. result:=1;
  1003. Dec(MaxLookAhead);
  1004. end
  1005. else
  1006. exit;
  1007. { Two-byte codepoints have the form
  1008. %(110)xxxxx (10)xxxxxx.
  1009. but also minimum value of $80 = %10000000 =
  1010. %(110)00010 (10)000000. }
  1011. $C2 {%11000010}..$DF {%11011111}:
  1012. if (MaxLookAhead>=2) and
  1013. (ord(P[result+1]) and $C0=$80) then
  1014. begin
  1015. if not IncludeCombiningDiacriticalMarks then
  1016. exit(2);
  1017. if result>0 then
  1018. begin
  1019. cp:=ord(P[result]) and $1F {%11111} shl 6 or ord(P[result+1]) and $3F {%111111};
  1020. { Max possible cp value, $7FF, won't overflow L2. }
  1021. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1022. exit;
  1023. end;
  1024. Inc(result,2);
  1025. Dec(MaxLookAhead,2);
  1026. end
  1027. else
  1028. begin
  1029. cpLen:=2;
  1030. break;
  1031. end;
  1032. { Three-byte codepoints have the form
  1033. %(1110)xxxx (10)xxxxxx (10)xxxxxx
  1034. but also minimum value of $800 = %1000 00000000 =
  1035. %(1110)0000 (10)100000 (10)000000. }
  1036. $E0 {%11100000}..$EF {%11101111}:
  1037. if (MaxLookAhead>=3) and
  1038. (ord(P[result+1]) and $C0=$80) and
  1039. (ord(P[result+2]) and $C0=$80) and
  1040. ((ord(P[result])>$E0 {%11100000}) or
  1041. (ord(P[result+1])>=$A0 {%10100000})) then
  1042. begin
  1043. if not IncludeCombiningDiacriticalMarks then
  1044. exit(3);
  1045. if result>0 then
  1046. begin
  1047. cp:=ord(P[result]) and $F {%1111} shl 12 or ord(P[result+1]) and $3F {%111111} shl 6 or ord(P[result+2]) and $3F {%111111};
  1048. { Max possible cp value, $FFFF, won't overflow L2. }
  1049. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1050. exit;
  1051. end;
  1052. Inc(result,3);
  1053. Dec(MaxLookAhead,3);
  1054. end
  1055. else
  1056. begin
  1057. cpLen:=3;
  1058. break;
  1059. end;
  1060. { Four-byte codepoints have the form
  1061. %(11110)xxx (10)xxxxxx (10)xxxxxx (10)xxxxxx
  1062. but also minimum value of $10000 = %1 00000000 00000000 =
  1063. %(11110)000 (10)010000 (10)000000 (10)000000
  1064. and maximum of $10FFFF = %10000 11111111 11111111 =
  1065. %(11110)100 (10)001111 (10)111111 (10)111111. }
  1066. $F0 {%11110000}..$F4 {%11110100}:
  1067. if (MaxLookAhead>=4) and
  1068. (ord(P[result+1]) and $C0=$80) and
  1069. (ord(P[result+2]) and $C0=$80) and
  1070. (ord(P[result+3]) and $C0=$80) and
  1071. (uint16(P[result]) shl 8 or ord(P[result+1])>=$F090 {%11110000 10010000}) and
  1072. (uint16(P[result]) shl 8 or ord(P[result+1])<=$F48F {%11110100 10001111}) then
  1073. begin
  1074. if not IncludeCombiningDiacriticalMarks then
  1075. exit(4);
  1076. if result>0 then
  1077. begin
  1078. cp:=ord(P[result]) and $7 {%111} shl 18 or ord(P[result+1]) and $3F {%111111} shl 12 or ord(P[result+2]) and $3F {%111111} shl 6 or ord(P[result+3]) and $3F {%111111};
  1079. { This time, cp can overflow L2, and can have special-cased values U+E0100..U+E01EF. }
  1080. if cp<length(IsCombinings.L2) shl (5+4) then
  1081. begin
  1082. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1083. exit;
  1084. end
  1085. else if not ((cp>=$E0100) and (cp<=$E01EF)) then
  1086. exit;
  1087. end;
  1088. Inc(result,4);
  1089. Dec(MaxLookAhead,4);
  1090. end
  1091. else
  1092. begin
  1093. cpLen:=4;
  1094. break;
  1095. end;
  1096. else
  1097. begin
  1098. cpLen:=1;
  1099. break;
  1100. end;
  1101. end;
  1102. until false;
  1103. { Handle invalid or incomplete cases, when expected codepoint length is cpLen. }
  1104. for iByte:=1 to cpLen-1 do
  1105. if (iByte<MaxLookAhead) and
  1106. (ord(P[result+iByte]) and $C0 {%11000000}<>$80 {%10000000}) then
  1107. begin
  1108. if result=0 then result:=-1-iByte;
  1109. exit;
  1110. end;
  1111. if cpLen>MaxLookAhead then
  1112. result:=0 { Signal an incomplete codepoint, even if there were complete codepoints before. }
  1113. else if result=0 then
  1114. result:=-cpLen;
  1115. end;
  1116. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1117. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1118. var
  1119. l: ObjpasInt;
  1120. index: ObjpasInt;
  1121. len: byte;
  1122. begin
  1123. l:=high(arr)+1;
  1124. if l>=ObjpasInt(high(res))+1 then
  1125. l:=high(res)
  1126. else if l<0 then
  1127. l:=0;
  1128. if zerobased then
  1129. begin
  1130. index:=IndexByte(arr[0],l,0);
  1131. if index<0 then
  1132. len:=l
  1133. else
  1134. len:=index;
  1135. end
  1136. else
  1137. len:=l;
  1138. move(arr[0],res[1],len);
  1139. res[0]:=chr(len);
  1140. end;
  1141. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1142. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1143. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  1144. var
  1145. len: ObjpasInt;
  1146. begin
  1147. len := length(src);
  1148. if len > length(res) then
  1149. len := length(res);
  1150. {$push}{$r-}
  1151. { make sure we don't access AnsiChar 1 if length is 0 (JM) }
  1152. if len > 0 then
  1153. move(src[1],res[0],len);
  1154. fillchar(res[len],length(res)-len,0);
  1155. {$pop}
  1156. end;
  1157. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1158. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1159. function fpc_pchar_length(p:PAnsiChar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1160. begin
  1161. if assigned(p) then
  1162. Result:=IndexByte(p^,-1,0)
  1163. else
  1164. Result:=0;
  1165. end;
  1166. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1167. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1168. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1169. begin
  1170. if assigned(p) then
  1171. Result:=IndexWord(p^,-1,0)
  1172. else
  1173. result:=0;
  1174. end;
  1175. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1176. {****************************************************************************
  1177. Caller/StackFrame Helpers
  1178. ****************************************************************************}
  1179. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1180. {_$error Get_frame must be defined for each processor }
  1181. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1182. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1183. {_$error Get_caller_addr must be defined for each processor }
  1184. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1185. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1186. {_$error Get_caller_frame must be defined for each processor }
  1187. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1188. {****************************************************************************
  1189. Math
  1190. ****************************************************************************}
  1191. {****************************************************************************
  1192. Software multiplication
  1193. ****************************************************************************}
  1194. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1195. {$ifndef FPC_SYSTEM_HAS_MUL_SHORTINT}
  1196. function fpc_mul_shortint(f1,f2 : shortint) : shortint;[public,alias: 'FPC_MUL_SHORTINT']; compilerproc;
  1197. begin
  1198. { there's no difference between signed and unsigned multiplication,
  1199. when the destination size is equal to the source size and overflow
  1200. checking is off }
  1201. { byte(f1) * byte(f2) is coded as a call to mul_byte }
  1202. fpc_mul_shortint := shortint(byte(f1) * byte(f2));
  1203. end;
  1204. function fpc_mul_shortint_checkoverflow(f1,f2 : shortint) : shortint;[public,alias: 'FPC_MUL_SHORTINT_CHECKOVERFLOW']; compilerproc;
  1205. var
  1206. sign : boolean;
  1207. q1,q2,q3 : byte;
  1208. begin
  1209. sign:=false;
  1210. if f1 < 0 then
  1211. begin
  1212. sign := not(sign);
  1213. q1 := byte(-f1);
  1214. end
  1215. else
  1216. q1 := f1;
  1217. if f2 < 0 then
  1218. begin
  1219. sign := not(sign);
  1220. q2 := byte(-f2);
  1221. end
  1222. else
  1223. q2 := f2;
  1224. { the q1*q2 is coded as call to mul_byte }
  1225. {$push}
  1226. {$Q+}
  1227. q3 := q1 * q2;
  1228. {$pop}
  1229. if (q1 <> 0) and (q2 <> 0) and
  1230. ((q1 > q3) or (q2 > q3) or
  1231. { the bit 7 can be only set if we have $80 }
  1232. { and sign is true }
  1233. (q3 shr 7 <> 0) and
  1234. ((q3 <> byte(byte(1) shl 7)) or not(sign))
  1235. ) then
  1236. FPC_Overflow();
  1237. if sign then
  1238. fpc_mul_shortint_checkoverflow := -q3
  1239. else
  1240. fpc_mul_shortint_checkoverflow := q3;
  1241. end;
  1242. {$endif FPC_SYSTEM_HAS_MUL_SHORTINT}
  1243. {$ifndef FPC_SYSTEM_HAS_MUL_BYTE}
  1244. function fpc_mul_byte(f1,f2 : byte) : byte;[public,alias: 'FPC_MUL_BYTE']; compilerproc;
  1245. var
  1246. v1,v2,res: byte;
  1247. begin
  1248. if f1<f2 then
  1249. begin
  1250. v1:=f1;
  1251. v2:=f2;
  1252. end
  1253. else
  1254. begin
  1255. v1:=f2;
  1256. v2:=f1;
  1257. end;
  1258. res:=0;
  1259. while v1<>0 do
  1260. begin
  1261. if v1 and 1<>0 then
  1262. inc(res,v2);
  1263. v2:=v2 shl 1;
  1264. v1:=v1 shr 1;
  1265. end;
  1266. fpc_mul_byte:=res;
  1267. end;
  1268. function fpc_mul_byte_checkoverflow(f1,f2 : byte) : byte;[public,alias: 'FPC_MUL_BYTE_CHECKOVERFLOW']; compilerproc;
  1269. var
  1270. _f1, bitpos : byte;
  1271. f1overflowed : boolean;
  1272. begin
  1273. fpc_mul_byte_checkoverflow := 0;
  1274. bitpos := 1;
  1275. f1overflowed := false;
  1276. while f1<>0 do
  1277. begin
  1278. if (f2 and bitpos) <> 0 then
  1279. begin
  1280. _f1 := fpc_mul_byte_checkoverflow;
  1281. fpc_mul_byte_checkoverflow := fpc_mul_byte_checkoverflow + f1;
  1282. { if one of the operands is greater than the result an
  1283. overflow occurs }
  1284. if f1overflowed or ((_f1 <> 0) and (f1 <> 0) and
  1285. ((_f1 > fpc_mul_byte_checkoverflow) or (f1 > fpc_mul_byte_checkoverflow))) then
  1286. FPC_Overflow();
  1287. end;
  1288. { when bootstrapping, we forget about overflow checking for qword :) }
  1289. f1overflowed := f1overflowed or ((f1 and (1 shl 7)) <> 0);
  1290. f1 := f1 shl 1;
  1291. bitpos := bitpos shl 1;
  1292. end;
  1293. end;
  1294. {$endif FPC_SYSTEM_HAS_MUL_BYTE}
  1295. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1296. function fpc_mul_integer(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1297. begin
  1298. { there's no difference between signed and unsigned multiplication,
  1299. when the destination size is equal to the source size and overflow
  1300. checking is off }
  1301. { word(f1)*word(f2) is coded as a call to mulword }
  1302. fpc_mul_integer:=integer(word(f1)*word(f2));
  1303. end;
  1304. function fpc_mul_integer_checkoverflow(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER_CHECKOVERFLOW']; compilerproc;
  1305. var
  1306. sign : boolean;
  1307. q1,q2,q3 : word;
  1308. begin
  1309. sign:=false;
  1310. if f1<0 then
  1311. begin
  1312. sign:=not(sign);
  1313. q1:=word(-f1);
  1314. end
  1315. else
  1316. q1:=f1;
  1317. if f2<0 then
  1318. begin
  1319. sign:=not(sign);
  1320. q2:=word(-f2);
  1321. end
  1322. else
  1323. q2:=f2;
  1324. { the q1*q2 is coded as call to mulword }
  1325. {$push}
  1326. {$Q+}
  1327. q3:=q1*q2;
  1328. {$pop}
  1329. if (q1 <> 0) and (q2 <>0) and
  1330. ((q1>q3) or (q2>q3) or
  1331. { the bit 63 can be only set if we have $8000 }
  1332. { and sign is true }
  1333. (q3 shr 15<>0) and
  1334. ((q3<>word(word(1) shl 15)) or not(sign))
  1335. ) then
  1336. FPC_Overflow();
  1337. if sign then
  1338. fpc_mul_integer_checkoverflow:=-q3
  1339. else
  1340. fpc_mul_integer_checkoverflow:=q3;
  1341. end;
  1342. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1343. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1344. function fpc_mul_word(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1345. var
  1346. v1,v2,res: word;
  1347. begin
  1348. if f1<f2 then
  1349. begin
  1350. v1:=f1;
  1351. v2:=f2;
  1352. end
  1353. else
  1354. begin
  1355. v1:=f2;
  1356. v2:=f1;
  1357. end;
  1358. res:=0;
  1359. while v1<>0 do
  1360. begin
  1361. if ALUUInt(v1) and 1<>0 then
  1362. inc(res,v2);
  1363. v2:=v2 shl 1;
  1364. v1:=v1 shr 1;
  1365. end;
  1366. fpc_mul_word:=res;
  1367. end;
  1368. function fpc_mul_word_checkoverflow(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD_CHECKOVERFLOW']; compilerproc;
  1369. var
  1370. _f1,bitpos : word;
  1371. f1overflowed : boolean;
  1372. begin
  1373. fpc_mul_word_checkoverflow:=0;
  1374. bitpos:=1;
  1375. f1overflowed:=false;
  1376. while f1<>0 do
  1377. begin
  1378. if (f2 and bitpos)<>0 then
  1379. begin
  1380. _f1:=fpc_mul_word_checkoverflow;
  1381. fpc_mul_word_checkoverflow:=fpc_mul_word_checkoverflow+f1;
  1382. { if one of the operands is greater than the result an
  1383. overflow occurs }
  1384. if f1overflowed or ((_f1<>0) and (f1<>0) and
  1385. ((_f1>fpc_mul_word_checkoverflow) or (f1>fpc_mul_word_checkoverflow))) then
  1386. FPC_Overflow();
  1387. end;
  1388. { when bootstrapping, we forget about overflow checking for qword :) }
  1389. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1390. f1:=f1 shl 1;
  1391. bitpos:=bitpos shl 1;
  1392. end;
  1393. end;
  1394. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1395. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1396. function fpc_mul_longint(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1397. begin
  1398. { there's no difference between signed and unsigned multiplication,
  1399. when the destination size is equal to the source size and overflow
  1400. checking is off }
  1401. { dword(f1)*dword(f2) is coded as a call to muldword }
  1402. fpc_mul_longint:=longint(dword(f1)*dword(f2));
  1403. end;
  1404. function fpc_mul_longint_checkoverflow(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT_CHECKOVERFLOW']; compilerproc;
  1405. var
  1406. sign : boolean;
  1407. q1,q2,q3 : dword;
  1408. begin
  1409. sign:=false;
  1410. if f1<0 then
  1411. begin
  1412. sign:=not(sign);
  1413. q1:=dword(-f1);
  1414. end
  1415. else
  1416. q1:=f1;
  1417. if f2<0 then
  1418. begin
  1419. sign:=not(sign);
  1420. q2:=dword(-f2);
  1421. end
  1422. else
  1423. q2:=f2;
  1424. { the q1*q2 is coded as call to muldword }
  1425. {$push}
  1426. {$Q+}
  1427. q3:=q1*q2;
  1428. {$pop}
  1429. if (q1 <> 0) and (q2 <>0) and
  1430. ((q1>q3) or (q2>q3) or
  1431. { the bit 31 can be only set if we have $8000 0000 }
  1432. { and sign is true }
  1433. (q3 shr 31<>0) and
  1434. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1435. ) then
  1436. FPC_Overflow();
  1437. if sign then
  1438. fpc_mul_longint_checkoverflow:=-q3
  1439. else
  1440. fpc_mul_longint_checkoverflow:=q3;
  1441. end;
  1442. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1443. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1444. function fpc_mul_dword(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1445. var
  1446. v1,v2,res: dword;
  1447. begin
  1448. if f1<f2 then
  1449. begin
  1450. v1:=f1;
  1451. v2:=f2;
  1452. end
  1453. else
  1454. begin
  1455. v1:=f2;
  1456. v2:=f1;
  1457. end;
  1458. res:=0;
  1459. while v1<>0 do
  1460. begin
  1461. if ALUUInt(v1) and 1<>0 then
  1462. inc(res,v2);
  1463. v2:=v2 shl 1;
  1464. v1:=v1 shr 1;
  1465. end;
  1466. fpc_mul_dword:=res;
  1467. end;
  1468. function fpc_mul_dword_checkoverflow(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD_CHECKOVERFLOW']; compilerproc;
  1469. var
  1470. _f1,bitpos : dword;
  1471. f1overflowed : boolean;
  1472. begin
  1473. fpc_mul_dword_checkoverflow:=0;
  1474. bitpos:=1;
  1475. f1overflowed:=false;
  1476. while f1<>0 do
  1477. begin
  1478. if (f2 and bitpos)<>0 then
  1479. begin
  1480. _f1:=fpc_mul_dword_checkoverflow;
  1481. fpc_mul_dword_checkoverflow:=fpc_mul_dword_checkoverflow+f1;
  1482. { if one of the operands is greater than the result an
  1483. overflow occurs }
  1484. if f1overflowed or ((_f1<>0) and (f1<>0) and
  1485. ((_f1>fpc_mul_dword_checkoverflow) or (f1>fpc_mul_dword_checkoverflow))) then
  1486. FPC_Overflow();
  1487. end;
  1488. { when bootstrapping, we forget about overflow checking for qword :) }
  1489. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1490. f1:=f1 shl 1;
  1491. bitpos:=bitpos shl 1;
  1492. end;
  1493. end;
  1494. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1495. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1496. {****************************************************************************
  1497. Software longint/dword division
  1498. ****************************************************************************}
  1499. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1500. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1501. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1502. var
  1503. shift,lzz,lzn : ObjpasInt;
  1504. begin
  1505. result:=0;
  1506. if n=0 then
  1507. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1508. if z=0 then
  1509. exit;
  1510. lzz:=BsrDWord(z);
  1511. lzn:=BsrDWord(n);
  1512. { if the denominator contains less zeros
  1513. then the numerator
  1514. then d is greater than the n }
  1515. if lzn>lzz then
  1516. exit;
  1517. shift:=lzz-lzn;
  1518. n:=n shl shift;
  1519. for shift:=shift downto 0 do
  1520. begin
  1521. if z>=n then
  1522. begin
  1523. z:=z-n;
  1524. result:=result+dword(dword(1) shl shift);
  1525. end;
  1526. n:=n shr 1;
  1527. end;
  1528. end;
  1529. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1530. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1531. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1532. var
  1533. shift,lzz,lzn : ObjpasInt;
  1534. begin
  1535. result:=0;
  1536. if n=0 then
  1537. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1538. if z=0 then
  1539. exit;
  1540. lzz:=BsrDWord(z);
  1541. lzn:=BsrDWord(n);
  1542. { if the denominator contains less zeros
  1543. then the numerator
  1544. then d is greater than the n }
  1545. if lzn>lzz then
  1546. begin
  1547. result:=z;
  1548. exit;
  1549. end;
  1550. shift:=lzz-lzn;
  1551. n:=n shl shift;
  1552. for shift:=shift downto 0 do
  1553. begin
  1554. if z>=n then
  1555. z:=z-n;
  1556. n:=n shr 1;
  1557. end;
  1558. result:=z;
  1559. end;
  1560. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1561. {$ifndef FPC_SYSTEM_HAS_DIV_WORD}
  1562. function fpc_div_word(n,z : word) : word; [public,alias: 'FPC_DIV_WORD']; compilerproc;
  1563. var
  1564. shift,lzz,lzn : Byte;
  1565. begin
  1566. result:=0;
  1567. if n=0 then
  1568. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1569. if z=0 then
  1570. exit;
  1571. lzz:=BsrWord(z);
  1572. lzn:=BsrWord(n);
  1573. { if the denominator contains less zeros
  1574. then the numerator
  1575. then d is greater than the n }
  1576. if lzn>lzz then
  1577. exit;
  1578. shift:=lzz-lzn;
  1579. n:=n shl shift;
  1580. for shift:=shift downto 0 do
  1581. begin
  1582. if z>=n then
  1583. begin
  1584. z:=z-n;
  1585. result:=result+word(word(1) shl shift);
  1586. end;
  1587. n:=n shr 1;
  1588. end;
  1589. end;
  1590. {$endif FPC_SYSTEM_HAS_DIV_WORD}
  1591. {$ifndef FPC_SYSTEM_HAS_MOD_WORD}
  1592. function fpc_mod_word(n,z : word) : word; [public,alias: 'FPC_MOD_WORD']; compilerproc;
  1593. var
  1594. shift,lzz,lzn : Byte;
  1595. begin
  1596. result:=0;
  1597. if n=0 then
  1598. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1599. if z=0 then
  1600. exit;
  1601. lzz:=BsrWord(z);
  1602. lzn:=BsrWord(n);
  1603. { if the denominator contains less zeros
  1604. then the numerator
  1605. then d is greater than the n }
  1606. if lzn>lzz then
  1607. begin
  1608. result:=z;
  1609. exit;
  1610. end;
  1611. shift:=lzz-lzn;
  1612. n:=n shl shift;
  1613. for shift:=shift downto 0 do
  1614. begin
  1615. if z>=n then
  1616. z:=z-n;
  1617. n:=n shr 1;
  1618. end;
  1619. result:=z;
  1620. end;
  1621. {$endif FPC_SYSTEM_HAS_MOD_WORD}
  1622. {$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
  1623. function fpc_div_byte(n,z : byte) : byte; [public,alias: 'FPC_DIV_BYTE']; compilerproc;
  1624. var
  1625. shift,lzz,lzn : Byte;
  1626. begin
  1627. result:=0;
  1628. if n=0 then
  1629. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1630. if z=0 then
  1631. exit;
  1632. lzz:=BsrByte(z);
  1633. lzn:=BsrByte(n);
  1634. { if the denominator contains less zeros
  1635. then the numerator
  1636. then d is greater than the n }
  1637. if lzn>lzz then
  1638. exit;
  1639. shift:=lzz-lzn;
  1640. n:=n shl shift;
  1641. for shift:=shift downto 0 do
  1642. begin
  1643. if z>=n then
  1644. begin
  1645. z:=z-n;
  1646. result:=result+byte(byte(1) shl shift);
  1647. end;
  1648. n:=n shr 1;
  1649. end;
  1650. end;
  1651. {$endif FPC_SYSTEM_HAS_DIV_BYTE}
  1652. {$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
  1653. function fpc_mod_byte(n,z : byte) : byte; [public,alias: 'FPC_MOD_BYTE']; compilerproc;
  1654. var
  1655. shift,lzz,lzn : Byte;
  1656. begin
  1657. result:=0;
  1658. if n=0 then
  1659. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1660. if z=0 then
  1661. exit;
  1662. lzz:=BsrByte(z);
  1663. lzn:=BsrByte(n);
  1664. { if the denominator contains less zeros
  1665. then the numerator
  1666. then d is greater than the n }
  1667. if lzn>lzz then
  1668. begin
  1669. result:=z;
  1670. exit;
  1671. end;
  1672. shift:=lzz-lzn;
  1673. n:=n shl shift;
  1674. for shift:=shift downto 0 do
  1675. begin
  1676. if z>=n then
  1677. z:=z-n;
  1678. n:=n shr 1;
  1679. end;
  1680. result:=z;
  1681. end;
  1682. {$endif FPC_SYSTEM_HAS_MOD_BYTE}
  1683. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1684. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1685. var
  1686. sign : boolean;
  1687. d1,d2 : dword;
  1688. begin
  1689. if n=0 then
  1690. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1691. sign:=false;
  1692. if z<0 then
  1693. begin
  1694. sign:=not(sign);
  1695. d1:=dword(-z);
  1696. end
  1697. else
  1698. d1:=z;
  1699. if n<0 then
  1700. begin
  1701. sign:=not(sign);
  1702. d2:=dword(-n);
  1703. end
  1704. else
  1705. d2:=n;
  1706. { the div is coded by the compiler as call to divdword }
  1707. if sign then
  1708. result:=-(d1 div d2)
  1709. else
  1710. result:=d1 div d2;
  1711. end;
  1712. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1713. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1714. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1715. var
  1716. signed : boolean;
  1717. r,nq,zq : dword;
  1718. begin
  1719. if n=0 then
  1720. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1721. nq:=abs(n);
  1722. if z<0 then
  1723. begin
  1724. zq:=dword(-z);
  1725. signed:=true;
  1726. end
  1727. else
  1728. begin
  1729. zq:=z;
  1730. signed:=false;
  1731. end;
  1732. r:=zq mod nq;
  1733. if signed then
  1734. result:=-longint(r)
  1735. else
  1736. result:=r;
  1737. end;
  1738. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1739. {$ifndef FPC_SYSTEM_HAS_DIV_SMALLINT}
  1740. function fpc_div_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_DIV_SMALLINT']; compilerproc;
  1741. var
  1742. sign : boolean;
  1743. w1,w2 : word;
  1744. begin
  1745. if n=0 then
  1746. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1747. sign:=false;
  1748. if z<0 then
  1749. begin
  1750. sign:=not(sign);
  1751. w1:=word(-z);
  1752. end
  1753. else
  1754. w1:=z;
  1755. if n<0 then
  1756. begin
  1757. sign:=not(sign);
  1758. w2:=word(-n);
  1759. end
  1760. else
  1761. w2:=n;
  1762. { the div is coded by the compiler as call to divdword }
  1763. if sign then
  1764. result:=-(w1 div w2)
  1765. else
  1766. result:=w1 div w2;
  1767. end;
  1768. {$endif FPC_SYSTEM_HAS_DIV_SMALLINT}
  1769. {$ifndef FPC_SYSTEM_HAS_MOD_SMALLINT}
  1770. function fpc_mod_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_MOD_SMALLINT']; compilerproc;
  1771. var
  1772. signed : boolean;
  1773. r,nq,zq : word;
  1774. begin
  1775. if n=0 then
  1776. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1777. nq:=abs(n);
  1778. if z<0 then
  1779. begin
  1780. zq:=word(-z);
  1781. signed:=true;
  1782. end
  1783. else
  1784. begin
  1785. zq:=z;
  1786. signed:=false;
  1787. end;
  1788. r:=zq mod nq;
  1789. if signed then
  1790. result:=-smallint(r)
  1791. else
  1792. result:=r;
  1793. end;
  1794. {$endif FPC_SYSTEM_HAS_MOD_SMALLINT}
  1795. {$ifndef FPC_SYSTEM_HAS_DIV_SHORTINT}
  1796. function fpc_div_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_DIV_SHORTINT']; compilerproc;
  1797. var
  1798. sign : boolean;
  1799. b1,b2 : byte;
  1800. begin
  1801. if n=0 then
  1802. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1803. sign:=false;
  1804. if z<0 then
  1805. begin
  1806. sign:=not(sign);
  1807. b1:=byte(-z);
  1808. end
  1809. else
  1810. b1:=z;
  1811. if n<0 then
  1812. begin
  1813. sign:=not(sign);
  1814. b2:=byte(-n);
  1815. end
  1816. else
  1817. b2:=n;
  1818. { the div is coded by the compiler as call to divdword }
  1819. if sign then
  1820. result:=-(b1 div b2)
  1821. else
  1822. result:=b1 div b2;
  1823. end;
  1824. {$endif FPC_SYSTEM_HAS_DIV_SHORTINT}
  1825. {$ifndef FPC_SYSTEM_HAS_MOD_SHORTINT}
  1826. function fpc_mod_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_MOD_SHORTINT']; compilerproc;
  1827. var
  1828. signed : boolean;
  1829. r,nq,zq : byte;
  1830. begin
  1831. if n=0 then
  1832. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1833. nq:=abs(n);
  1834. if z<0 then
  1835. begin
  1836. zq:=byte(-z);
  1837. signed:=true;
  1838. end
  1839. else
  1840. begin
  1841. zq:=z;
  1842. signed:=false;
  1843. end;
  1844. r:=zq mod nq;
  1845. if signed then
  1846. result:=-shortint(r)
  1847. else
  1848. result:=r;
  1849. end;
  1850. {$endif FPC_SYSTEM_HAS_MOD_SHORTINT}
  1851. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1852. {****************************************************************************}
  1853. {$if defined(CPUINT8)}
  1854. {$ifndef FPC_SYSTEM_HAS_ABS_SHORTINT}
  1855. function abs(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1856. begin
  1857. if l<0 then
  1858. abs:=-l
  1859. else
  1860. abs:=l;
  1861. end;
  1862. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  1863. {$endif CPUINT8}
  1864. {$if defined(CPUINT16) or defined(CPUINT8)}
  1865. {$ifndef FPC_SYSTEM_HAS_ABS_SMALLINT}
  1866. function abs(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1867. begin
  1868. if l<0 then
  1869. abs:=-l
  1870. else
  1871. abs:=l;
  1872. end;
  1873. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  1874. {$endif CPUINT16 or CPUINT8}
  1875. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1876. { This is only needed to bootstrap on SPARC targets
  1877. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  1878. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1879. begin
  1880. if l<0 then
  1881. abs:=-l
  1882. else
  1883. abs:=l;
  1884. end;
  1885. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1886. {$if defined(CPUINT8)}
  1887. {$ifndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  1888. function odd(l:shortint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1889. begin
  1890. odd:=boolean(l and 1);
  1891. end;
  1892. {$endif ndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  1893. {$ifndef FPC_SYSTEM_HAS_ODD_BYTE}
  1894. function odd(l:byte):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1895. begin
  1896. odd:=boolean(l and 1);
  1897. end;
  1898. {$endif ndef FPC_SYSTEM_HAS_ODD_BYTE}
  1899. {$endif CPUINT8}
  1900. {$if defined(CPUINT16) or defined(CPUINT8)}
  1901. {$ifndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  1902. function odd(l:smallint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1903. begin
  1904. odd:=boolean(l and 1);
  1905. end;
  1906. {$endif ndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  1907. {$ifndef FPC_SYSTEM_HAS_ODD_WORD}
  1908. function odd(l:word):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1909. begin
  1910. odd:=boolean(l and 1);
  1911. end;
  1912. {$endif ndef FPC_SYSTEM_HAS_ODD_WORD}
  1913. {$endif CPUINT16 or CPUINT8}
  1914. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1915. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1916. begin
  1917. odd:=boolean(l and 1);
  1918. end;
  1919. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1920. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1921. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1922. begin
  1923. odd:=boolean(l and 1);
  1924. end;
  1925. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1926. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1927. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1928. begin
  1929. odd:=boolean(longint(l) and 1);
  1930. end;
  1931. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1932. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1933. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1934. begin
  1935. odd:=boolean(longint(l) and 1);
  1936. end;
  1937. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1938. {$if defined(CPUINT8)}
  1939. {$ifndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  1940. function sqr(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1941. begin
  1942. sqr:=l*l;
  1943. end;
  1944. {$endif ndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  1945. {$endif CPUINT8}
  1946. {$if defined(CPUINT16) or defined(CPUINT8)}
  1947. {$ifndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  1948. function sqr(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1949. begin
  1950. sqr:=l*l;
  1951. end;
  1952. {$endif ndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  1953. {$endif CPUINT16 or CPUINT8}
  1954. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1955. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1956. begin
  1957. sqr:=l*l;
  1958. end;
  1959. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1960. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1961. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1962. begin
  1963. if l < 0 then
  1964. abs := -l
  1965. else
  1966. abs := l;
  1967. end;
  1968. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1969. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1970. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1971. begin
  1972. sqr := l*l;
  1973. end;
  1974. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1975. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1976. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1977. begin
  1978. sqr := l*l;
  1979. end;
  1980. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1981. {$ifdef CPU16}
  1982. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1983. function declocked(var l:smallint):boolean;
  1984. begin
  1985. Dec(l);
  1986. declocked:=(l=0);
  1987. end;
  1988. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1989. {$endif CPU16}
  1990. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1991. function declocked(var l:longint):boolean;
  1992. begin
  1993. Dec(l);
  1994. declocked:=(l=0);
  1995. end;
  1996. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1997. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1998. function declocked(var l:int64):boolean;
  1999. begin
  2000. Dec(l);
  2001. declocked:=(l=0);
  2002. end;
  2003. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  2004. {$ifdef CPU16}
  2005. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2006. procedure inclocked(var l:smallint);
  2007. begin
  2008. Inc(l);
  2009. end;
  2010. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2011. {$endif CPU16}
  2012. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2013. procedure inclocked(var l:longint);
  2014. begin
  2015. Inc(l);
  2016. end;
  2017. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2018. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2019. procedure inclocked(var l:int64);
  2020. begin
  2021. Inc(l);
  2022. end;
  2023. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2024. {$ifndef FPC_SYSTEM_HAS_SPTR}
  2025. {_$error Sptr must be defined for each processor }
  2026. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  2027. {****************************************************************************
  2028. Str()
  2029. ****************************************************************************}
  2030. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2031. procedure int_str(l:longint;out s:shortstring);
  2032. var
  2033. m,m1 : longword;
  2034. pcstart,
  2035. pc2start,
  2036. pc,pc2 : PAnsiChar;
  2037. hs : string[32];
  2038. overflow : longint;
  2039. begin
  2040. pc2start:=@s[1];
  2041. pc2:=pc2start;
  2042. if (l<0) then
  2043. begin
  2044. pc2^:='-';
  2045. inc(pc2);
  2046. m:=longword(-l);
  2047. end
  2048. else
  2049. m:=longword(l);
  2050. pcstart:=PAnsiChar(@hs[0]);
  2051. pc:=pcstart;
  2052. repeat
  2053. m1:=m div 10;
  2054. inc(pc);
  2055. pc^:=AnsiChar(m-(m1*10)+byte('0'));
  2056. m:=m1;
  2057. until m=0;
  2058. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2059. if overflow>0 then
  2060. inc(pcstart,overflow);
  2061. while (pc>pcstart) do
  2062. begin
  2063. pc2^:=pc^;
  2064. inc(pc2);
  2065. dec(pc);
  2066. end;
  2067. s[0]:=AnsiChar(pc2-pc2start);
  2068. end;
  2069. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2070. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2071. procedure int_str_unsigned(l:longword;out s:shortstring);
  2072. var
  2073. m1 : longword;
  2074. pcstart,
  2075. pc2start,
  2076. pc,pc2 : PAnsiChar;
  2077. hs : string[32];
  2078. overflow : longint;
  2079. begin
  2080. pc2start:=@s[1];
  2081. pc2:=pc2start;
  2082. pcstart:=PAnsiChar(@hs[0]);
  2083. pc:=pcstart;
  2084. repeat
  2085. inc(pc);
  2086. m1:=l div 10;
  2087. pc^:=AnsiChar(l-(m1*10)+byte('0'));
  2088. l:=m1;
  2089. until l=0;
  2090. overflow:=(pc-pcstart)-high(s);
  2091. if overflow>0 then
  2092. inc(pcstart,overflow);
  2093. while (pc>pcstart) do
  2094. begin
  2095. pc2^:=pc^;
  2096. inc(pc2);
  2097. dec(pc);
  2098. end;
  2099. s[0]:=AnsiChar(pc2-pc2start);
  2100. end;
  2101. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2102. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2103. procedure int_str(l:int64;out s:shortstring);
  2104. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2105. begin
  2106. runerror(217);
  2107. end;
  2108. {$else EXCLUDE_COMPLEX_PROCS}
  2109. var
  2110. m,m1 : qword;
  2111. pcstart,
  2112. pc2start,
  2113. pc,pc2 : PAnsiChar;
  2114. hs : string[32];
  2115. overflow : longint;
  2116. begin
  2117. pc2start:=@s[1];
  2118. pc2:=pc2start;
  2119. if (l<0) then
  2120. begin
  2121. pc2^:='-';
  2122. inc(pc2);
  2123. m:=qword(-l);
  2124. end
  2125. else
  2126. m:=qword(l);
  2127. pcstart:=PAnsiChar(@hs[0]);
  2128. pc:=pcstart;
  2129. repeat
  2130. m1:=m div 10;
  2131. inc(pc);
  2132. pc^:=AnsiChar(m-(m1*10)+byte('0'));
  2133. m:=m1;
  2134. until m=0;
  2135. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2136. if overflow>0 then
  2137. inc(pcstart,overflow);
  2138. while (pc>pcstart) do
  2139. begin
  2140. pc2^:=pc^;
  2141. inc(pc2);
  2142. dec(pc);
  2143. end;
  2144. s[0]:=AnsiChar(pc2-pc2start);
  2145. end;
  2146. {$endif EXCLUDE_COMPLEX_PROCS}
  2147. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2148. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2149. procedure int_str_unsigned(l:qword;out s:shortstring);
  2150. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2151. begin
  2152. runerror(217);
  2153. end;
  2154. {$else EXCLUDE_COMPLEX_PROCS}
  2155. var
  2156. m1 : qword;
  2157. pcstart,
  2158. pc2start,
  2159. pc,pc2 : PAnsiChar;
  2160. hs : string[64];
  2161. overflow : longint;
  2162. begin
  2163. pc2start:=@s[1];
  2164. pc2:=pc2start;
  2165. pcstart:=PAnsiChar(@hs[0]);
  2166. pc:=pcstart;
  2167. repeat
  2168. inc(pc);
  2169. m1:=l div 10;
  2170. pc^:=AnsiChar(l-(m1*10)+byte('0'));
  2171. l:=m1;
  2172. until l=0;
  2173. overflow:=(pc-pcstart)-high(s);
  2174. if overflow>0 then
  2175. inc(pcstart,overflow);
  2176. while (pc>pcstart) do
  2177. begin
  2178. pc2^:=pc^;
  2179. inc(pc2);
  2180. dec(pc);
  2181. end;
  2182. s[0]:=AnsiChar(pc2-pc2start);
  2183. end;
  2184. {$endif EXCLUDE_COMPLEX_PROCS}
  2185. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2186. {$ifndef FPUNONE}
  2187. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  2188. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2189. begin
  2190. softfloat_exception_flags:=[];
  2191. {$if declared(DefaultFPUControlWord)}
  2192. SetNativeFPUControlWord(DefaultFPUControlWord);
  2193. {$endif}
  2194. end;
  2195. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  2196. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  2197. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2198. begin
  2199. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  2200. softfloat_exception_flags:=[];
  2201. end;
  2202. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  2203. {$endif}
  2204. {$ifndef FPC_SYSTEM_HAS_FPC_CPUINIT}
  2205. procedure fpc_cpuinit;
  2206. begin
  2207. {$ifndef FPUNONE}
  2208. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  2209. if not IsLibrary then
  2210. {$endif}
  2211. SysInitFPU;
  2212. {$if declared(DefaultFPUControlWord)}
  2213. DefaultFPUControlWord:=GetNativeFPUControlWord;
  2214. {$endif}
  2215. SysResetFPU;
  2216. {$endif}
  2217. end;
  2218. {$endif}
  2219. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  2220. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2221. begin
  2222. { the extra Word type cast is necessary because the "AValue shr 8" }
  2223. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  2224. { the sign bits from the upper 16 bits are shifted in rather than }
  2225. { zeroes. }
  2226. Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff);
  2227. end;
  2228. {$ifndef cpujvm}
  2229. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2230. begin
  2231. Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
  2232. end;
  2233. {$endif}
  2234. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2235. begin
  2236. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2237. Result := (Result shl 16) or (Result shr 16);
  2238. end;
  2239. {$ifndef cpujvm}
  2240. function SwapEndian(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2241. begin
  2242. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2243. Result := (Result shl 16) or (Result shr 16);
  2244. end;
  2245. {$endif}
  2246. function SwapEndian(const AValue: Int64): Int64;
  2247. begin
  2248. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2249. ((AValue shr 8) and $00FF00FF00FF00FF);
  2250. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2251. ((Result shr 16) and $0000FFFF0000FFFF);
  2252. Result := (Result shl 32) or ((Result shr 32));
  2253. end;
  2254. {$ifndef cpujvm}
  2255. function SwapEndian(const AValue: QWord): QWord;
  2256. begin
  2257. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2258. ((AValue shr 8) and $00FF00FF00FF00FF);
  2259. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2260. ((Result shr 16) and $0000FFFF0000FFFF);
  2261. Result := (Result shl 32) or ((Result shr 32));
  2262. end;
  2263. {$endif}
  2264. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  2265. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2266. begin
  2267. {$IFDEF ENDIAN_BIG}
  2268. Result := AValue;
  2269. {$ELSE}
  2270. Result := SwapEndian(AValue);
  2271. {$ENDIF}
  2272. end;
  2273. {$ifndef cpujvm}
  2274. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2275. begin
  2276. {$IFDEF ENDIAN_BIG}
  2277. Result := AValue;
  2278. {$ELSE}
  2279. Result := SwapEndian(AValue);
  2280. {$ENDIF}
  2281. end;
  2282. {$endif not cpujvm}
  2283. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2284. begin
  2285. {$IFDEF ENDIAN_BIG}
  2286. Result := AValue;
  2287. {$ELSE}
  2288. Result := SwapEndian(AValue);
  2289. {$ENDIF}
  2290. end;
  2291. {$ifndef cpujvm}
  2292. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2293. begin
  2294. {$IFDEF ENDIAN_BIG}
  2295. Result := AValue;
  2296. {$ELSE}
  2297. Result := SwapEndian(AValue);
  2298. {$ENDIF}
  2299. end;
  2300. {$endif not cpujvm}
  2301. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2302. begin
  2303. {$IFDEF ENDIAN_BIG}
  2304. Result := AValue;
  2305. {$ELSE}
  2306. Result := SwapEndian(AValue);
  2307. {$ENDIF}
  2308. end;
  2309. {$ifndef cpujvm}
  2310. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2311. begin
  2312. {$IFDEF ENDIAN_BIG}
  2313. Result := AValue;
  2314. {$ELSE}
  2315. Result := SwapEndian(AValue);
  2316. {$ENDIF}
  2317. end;
  2318. {$endif not cpujvm}
  2319. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2320. begin
  2321. {$IFDEF ENDIAN_LITTLE}
  2322. Result := AValue;
  2323. {$ELSE}
  2324. Result := SwapEndian(AValue);
  2325. {$ENDIF}
  2326. end;
  2327. {$ifndef cpujvm}
  2328. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2329. begin
  2330. {$IFDEF ENDIAN_LITTLE}
  2331. Result := AValue;
  2332. {$ELSE}
  2333. Result := SwapEndian(AValue);
  2334. {$ENDIF}
  2335. end;
  2336. {$endif not cpujvm}
  2337. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2338. begin
  2339. {$IFDEF ENDIAN_LITTLE}
  2340. Result := AValue;
  2341. {$ELSE}
  2342. Result := SwapEndian(AValue);
  2343. {$ENDIF}
  2344. end;
  2345. {$ifndef cpujvm}
  2346. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2347. begin
  2348. {$IFDEF ENDIAN_LITTLE}
  2349. Result := AValue;
  2350. {$ELSE}
  2351. Result := SwapEndian(AValue);
  2352. {$ENDIF}
  2353. end;
  2354. {$endif not cpujvm}
  2355. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2356. begin
  2357. {$IFDEF ENDIAN_LITTLE}
  2358. Result := AValue;
  2359. {$ELSE}
  2360. Result := SwapEndian(AValue);
  2361. {$ENDIF}
  2362. end;
  2363. {$ifndef cpujvm}
  2364. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2365. begin
  2366. {$IFDEF ENDIAN_LITTLE}
  2367. Result := AValue;
  2368. {$ELSE}
  2369. Result := SwapEndian(AValue);
  2370. {$ENDIF}
  2371. end;
  2372. {$endif not cpujvm}
  2373. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2374. begin
  2375. {$IFDEF ENDIAN_BIG}
  2376. Result := AValue;
  2377. {$ELSE}
  2378. Result := SwapEndian(AValue);
  2379. {$ENDIF}
  2380. end;
  2381. {$ifndef cpujvm}
  2382. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2383. begin
  2384. {$IFDEF ENDIAN_BIG}
  2385. Result := AValue;
  2386. {$ELSE}
  2387. Result := SwapEndian(AValue);
  2388. {$ENDIF}
  2389. end;
  2390. {$endif not cpujvm}
  2391. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2392. begin
  2393. {$IFDEF ENDIAN_BIG}
  2394. Result := AValue;
  2395. {$ELSE}
  2396. Result := SwapEndian(AValue);
  2397. {$ENDIF}
  2398. end;
  2399. {$ifndef cpujvm}
  2400. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2401. begin
  2402. {$IFDEF ENDIAN_BIG}
  2403. Result := AValue;
  2404. {$ELSE}
  2405. Result := SwapEndian(AValue);
  2406. {$ENDIF}
  2407. end;
  2408. {$endif not cpujvm}
  2409. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2410. begin
  2411. {$IFDEF ENDIAN_BIG}
  2412. Result := AValue;
  2413. {$ELSE}
  2414. Result := SwapEndian(AValue);
  2415. {$ENDIF}
  2416. end;
  2417. {$ifndef cpujvm}
  2418. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2419. begin
  2420. {$IFDEF ENDIAN_BIG}
  2421. Result := AValue;
  2422. {$ELSE}
  2423. Result := SwapEndian(AValue);
  2424. {$ENDIF}
  2425. end;
  2426. {$endif not cpujvm}
  2427. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2428. begin
  2429. {$IFDEF ENDIAN_LITTLE}
  2430. Result := AValue;
  2431. {$ELSE}
  2432. Result := SwapEndian(AValue);
  2433. {$ENDIF}
  2434. end;
  2435. {$ifndef cpujvm}
  2436. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2437. begin
  2438. {$IFDEF ENDIAN_LITTLE}
  2439. Result := AValue;
  2440. {$ELSE}
  2441. Result := SwapEndian(AValue);
  2442. {$ENDIF}
  2443. end;
  2444. {$endif not cpujvm}
  2445. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2446. begin
  2447. {$IFDEF ENDIAN_LITTLE}
  2448. Result := AValue;
  2449. {$ELSE}
  2450. Result := SwapEndian(AValue);
  2451. {$ENDIF}
  2452. end;
  2453. {$ifndef cpujvm}
  2454. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2455. begin
  2456. {$IFDEF ENDIAN_LITTLE}
  2457. Result := AValue;
  2458. {$ELSE}
  2459. Result := SwapEndian(AValue);
  2460. {$ENDIF}
  2461. end;
  2462. {$endif not cpujvm}
  2463. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2464. begin
  2465. {$IFDEF ENDIAN_LITTLE}
  2466. Result := AValue;
  2467. {$ELSE}
  2468. Result := SwapEndian(AValue);
  2469. {$ENDIF}
  2470. end;
  2471. {$ifndef cpujvm}
  2472. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2473. begin
  2474. {$IFDEF ENDIAN_LITTLE}
  2475. Result := AValue;
  2476. {$ELSE}
  2477. Result := SwapEndian(AValue);
  2478. {$ENDIF}
  2479. end;
  2480. {$endif not cpujvm}
  2481. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2482. procedure ReadBarrier;
  2483. begin
  2484. end;
  2485. procedure ReadDependencyBarrier;
  2486. begin
  2487. end;
  2488. procedure ReadWriteBarrier;
  2489. begin
  2490. end;
  2491. procedure WriteBarrier;
  2492. begin
  2493. end;
  2494. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2495. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2496. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2497. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2498. begin
  2499. Result:=(AValue shr 1) or (AValue shl 7);
  2500. end;
  2501. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2502. begin
  2503. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2504. end;
  2505. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2506. begin
  2507. Result:=(AValue shl 1) or (AValue shr 7);
  2508. end;
  2509. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2510. begin
  2511. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2512. end;
  2513. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2514. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2515. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2516. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2517. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2518. begin
  2519. Result:=(AValue shr 1) or (AValue shl 15);
  2520. end;
  2521. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2522. begin
  2523. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2524. end;
  2525. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2526. begin
  2527. Result:=(AValue shl 1) or (AValue shr 15);
  2528. end;
  2529. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2530. begin
  2531. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2532. end;
  2533. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2534. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2535. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2536. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2537. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2538. begin
  2539. Result:=(AValue shr 1) or (AValue shl 31);
  2540. end;
  2541. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2542. begin
  2543. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2544. end;
  2545. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2546. begin
  2547. Result:=(AValue shl 1) or (AValue shr 31);
  2548. end;
  2549. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2550. begin
  2551. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2552. end;
  2553. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2554. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2555. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2556. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2557. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2558. begin
  2559. Result:=(AValue shr 1) or (AValue shl 63);
  2560. end;
  2561. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2562. begin
  2563. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2564. end;
  2565. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2566. begin
  2567. Result:=(AValue shl 1) or (AValue shr 63);
  2568. end;
  2569. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2570. begin
  2571. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2572. end;
  2573. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2574. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2575. {$ifndef FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2576. {$ifndef FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2577. procedure fpc_ror_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_INT64']; compilerproc;
  2578. begin
  2579. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2580. end;
  2581. procedure fpc_ror_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_QWORD']; compilerproc;
  2582. begin
  2583. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2584. end;
  2585. procedure fpc_rol_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_INT64']; compilerproc;
  2586. begin
  2587. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2588. end;
  2589. procedure fpc_rol_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_QWORD']; compilerproc;
  2590. begin
  2591. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2592. end;
  2593. {$endif FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2594. {$endif FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2595. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2596. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2597. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2598. begin
  2599. 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)))));
  2600. end;
  2601. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2602. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2603. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2604. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2605. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2606. begin
  2607. 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)))));
  2608. end;
  2609. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2610. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2611. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2612. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2613. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2614. begin
  2615. 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)))));
  2616. end;
  2617. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2618. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2619. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2620. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2621. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2622. begin
  2623. 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)))));
  2624. end;
  2625. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2626. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2627. {$ifndef FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2628. {$ifndef FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2629. procedure fpc_sar_assign_int64(var AValue : Int64;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_INT64']; compilerproc;
  2630. begin
  2631. AValue:=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)))));
  2632. end;
  2633. procedure fpc_sar_assign_qword(var AValue : QWord;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_QWORD']; compilerproc;
  2634. begin
  2635. AValue:=qword(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)))));
  2636. end;
  2637. {$endif FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2638. {$endif FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2639. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2640. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2641. function BsfByte(Const AValue: Byte): Byte;
  2642. const bsf8bit: array [Byte] of Byte = (
  2643. $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,
  2644. 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,
  2645. 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,
  2646. 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,
  2647. 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,
  2648. 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,
  2649. 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,
  2650. 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
  2651. );
  2652. begin
  2653. result:=bsf8bit[AValue];
  2654. end;
  2655. {$endif}
  2656. {$endif}
  2657. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2658. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2659. function BsrByte(Const AValue: Byte): Byte;
  2660. const bsr8bit: array [Byte] of Byte = (
  2661. $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,
  2662. 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,
  2663. 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,
  2664. 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,
  2665. 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,
  2666. 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,
  2667. 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,
  2668. 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
  2669. );
  2670. begin
  2671. result:=bsr8bit[AValue];
  2672. end;
  2673. {$endif}
  2674. {$endif}
  2675. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2676. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2677. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2678. begin
  2679. result:=ord(lo(AValue)=0)*8;
  2680. result:=result or BsfByte(byte(AValue shr result));
  2681. end;
  2682. {$endif}
  2683. {$endif}
  2684. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2685. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2686. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2687. begin
  2688. result:=ord(AValue>255)*8;
  2689. result:=result or BsrByte(byte(AValue shr result));
  2690. end;
  2691. {$endif}
  2692. {$endif}
  2693. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2694. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2695. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2696. var
  2697. tmp: DWord;
  2698. begin
  2699. result:=ord(lo(AValue)=0)*16;
  2700. tmp:=AValue shr result;
  2701. result:=result or (ord((tmp and $FF)=0)*8);
  2702. tmp:=tmp shr (result and 8);
  2703. result:=result or BsfByte(byte(tmp));
  2704. end;
  2705. {$endif}
  2706. {$endif}
  2707. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2708. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2709. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2710. var
  2711. tmp: DWord;
  2712. begin
  2713. result:=ord(AValue>$FFFF)*16;
  2714. tmp:=AValue shr result;
  2715. result:=result or (ord(tmp>$FF)*8);
  2716. tmp:=tmp shr (result and 8);
  2717. result:=result or BsrByte(byte(tmp));
  2718. end;
  2719. {$endif}
  2720. {$endif}
  2721. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2722. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2723. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2724. var
  2725. tmp: DWord;
  2726. begin
  2727. result:=0;
  2728. tmp:=lo(AValue);
  2729. if (tmp=0) then
  2730. begin
  2731. tmp:=hi(AValue);
  2732. result:=32;
  2733. end;
  2734. result:=result or BsfDword(tmp);
  2735. end;
  2736. {$endif}
  2737. {$endif}
  2738. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2739. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2740. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2741. var
  2742. tmp: DWord;
  2743. begin
  2744. result:=32;
  2745. tmp:=hi(AValue);
  2746. if (tmp=0) then
  2747. begin
  2748. tmp:=lo(AValue);
  2749. result:=0;
  2750. end;
  2751. result:=result or BsrDword(tmp);
  2752. end;
  2753. {$endif}
  2754. {$endif}
  2755. const
  2756. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2757. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2758. begin
  2759. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2760. end;
  2761. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2762. var
  2763. i : SizeInt;
  2764. begin
  2765. Result:=0;
  2766. for i:=0 to 3 do
  2767. begin
  2768. inc(Result,PopCntData[AValue and $f]);
  2769. AValue:=AValue shr 4;
  2770. end;
  2771. end;
  2772. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2773. var
  2774. i : SizeInt;
  2775. begin
  2776. Result:=0;
  2777. for i:=0 to 7 do
  2778. begin
  2779. inc(Result,PopCntData[AValue and $f]);
  2780. AValue:=AValue shr 4;
  2781. end;
  2782. end;
  2783. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2784. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2785. begin
  2786. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2787. end;
  2788. {$endif}