generic.inc 87 KB

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