generic.inc 88 KB

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