generic.inc 95 KB

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