generic.inc 88 KB

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