generic.inc 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  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:=(PtrUInt(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_INTEGER}
  1385. function fpc_mul_integer(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER']; 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. { word(f1)*word(f2) is coded as a call to mulword }
  1391. fpc_mul_integer:=integer(word(f1)*word(f2));
  1392. end;
  1393. function fpc_mul_integer_checkoverflow(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER_CHECKOVERFLOW']; compilerproc;
  1394. var
  1395. sign : boolean;
  1396. q1,q2,q3 : word;
  1397. begin
  1398. sign:=false;
  1399. if f1<0 then
  1400. begin
  1401. sign:=not(sign);
  1402. q1:=word(-f1);
  1403. end
  1404. else
  1405. q1:=f1;
  1406. if f2<0 then
  1407. begin
  1408. sign:=not(sign);
  1409. q2:=word(-f2);
  1410. end
  1411. else
  1412. q2:=f2;
  1413. { the q1*q2 is coded as call to mulword }
  1414. q3:=q1*q2;
  1415. if (q1 <> 0) and (q2 <>0) and
  1416. ((q1>q3) or (q2>q3) or
  1417. { the bit 63 can be only set if we have $8000 }
  1418. { and sign is true }
  1419. (q3 shr 15<>0) and
  1420. ((q3<>word(word(1) shl 15)) or not(sign))
  1421. ) then
  1422. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1423. if sign then
  1424. fpc_mul_integer_checkoverflow:=-q3
  1425. else
  1426. fpc_mul_integer_checkoverflow:=q3;
  1427. end;
  1428. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1429. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1430. function fpc_mul_word(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1431. var
  1432. _f1,bitpos : word;
  1433. b : byte;
  1434. begin
  1435. fpc_mul_word:=0;
  1436. bitpos:=1;
  1437. for b:=0 to 15 do
  1438. begin
  1439. if (f2 and bitpos)<>0 then
  1440. begin
  1441. _f1:=fpc_mul_word;
  1442. fpc_mul_word:=fpc_mul_word+f1;
  1443. end;
  1444. f1:=f1 shl 1;
  1445. bitpos:=bitpos shl 1;
  1446. end;
  1447. end;
  1448. function fpc_mul_word_checkoverflow(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD_CHECKOVERFLOW']; compilerproc;
  1449. var
  1450. _f1,bitpos : word;
  1451. b : byte;
  1452. f1overflowed : boolean;
  1453. begin
  1454. fpc_mul_word_checkoverflow:=0;
  1455. bitpos:=1;
  1456. f1overflowed:=false;
  1457. for b:=0 to 15 do
  1458. begin
  1459. if (f2 and bitpos)<>0 then
  1460. begin
  1461. _f1:=fpc_mul_word_checkoverflow;
  1462. fpc_mul_word_checkoverflow:=fpc_mul_word_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_word_checkoverflow) or (f1>fpc_mul_word_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 15))<>0);
  1471. f1:=f1 shl 1;
  1472. bitpos:=bitpos shl 1;
  1473. end;
  1474. end;
  1475. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1476. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1477. function fpc_mul_longint(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT']; 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. { dword(f1)*dword(f2) is coded as a call to muldword }
  1483. fpc_mul_longint:=longint(dword(f1)*dword(f2));
  1484. end;
  1485. function fpc_mul_longint_checkoverflow(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT_CHECKOVERFLOW']; compilerproc;
  1486. var
  1487. sign : boolean;
  1488. q1,q2,q3 : dword;
  1489. begin
  1490. sign:=false;
  1491. if f1<0 then
  1492. begin
  1493. sign:=not(sign);
  1494. q1:=dword(-f1);
  1495. end
  1496. else
  1497. q1:=f1;
  1498. if f2<0 then
  1499. begin
  1500. sign:=not(sign);
  1501. q2:=dword(-f2);
  1502. end
  1503. else
  1504. q2:=f2;
  1505. { the q1*q2 is coded as call to muldword }
  1506. q3:=q1*q2;
  1507. if (q1 <> 0) and (q2 <>0) and
  1508. ((q1>q3) or (q2>q3) or
  1509. { the bit 31 can be only set if we have $8000 0000 }
  1510. { and sign is true }
  1511. (q3 shr 15<>0) and
  1512. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1513. ) then
  1514. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1515. if sign then
  1516. fpc_mul_longint_checkoverflow:=-q3
  1517. else
  1518. fpc_mul_longint_checkoverflow:=q3;
  1519. end;
  1520. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1521. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1522. function fpc_mul_dword(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1523. var
  1524. _f1,bitpos : dword;
  1525. b : byte;
  1526. begin
  1527. fpc_mul_dword:=0;
  1528. bitpos:=1;
  1529. for b:=0 to 31 do
  1530. begin
  1531. if (f2 and bitpos)<>0 then
  1532. begin
  1533. _f1:=fpc_mul_dword;
  1534. fpc_mul_dword:=fpc_mul_dword+f1;
  1535. end;
  1536. f1:=f1 shl 1;
  1537. bitpos:=bitpos shl 1;
  1538. end;
  1539. end;
  1540. function fpc_mul_dword_checkoverflow(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD_CHECKOVERFLOW']; compilerproc;
  1541. var
  1542. _f1,bitpos : dword;
  1543. b : byte;
  1544. f1overflowed : boolean;
  1545. begin
  1546. fpc_mul_dword_checkoverflow:=0;
  1547. bitpos:=1;
  1548. f1overflowed:=false;
  1549. for b:=0 to 31 do
  1550. begin
  1551. if (f2 and bitpos)<>0 then
  1552. begin
  1553. _f1:=fpc_mul_dword_checkoverflow;
  1554. fpc_mul_dword_checkoverflow:=fpc_mul_dword_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_dword_checkoverflow) or (f1>fpc_mul_dword_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 (dword(1) shl 31))<>0);
  1563. f1:=f1 shl 1;
  1564. bitpos:=bitpos shl 1;
  1565. end;
  1566. end;
  1567. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1568. {$endif VER3_0}
  1569. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1570. {****************************************************************************
  1571. Software longint/dword division
  1572. ****************************************************************************}
  1573. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1574. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1575. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1576. var
  1577. shift,lzz,lzn : ObjpasInt;
  1578. begin
  1579. result:=0;
  1580. if n=0 then
  1581. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1582. if z=0 then
  1583. exit;
  1584. lzz:=BsrDWord(z);
  1585. lzn:=BsrDWord(n);
  1586. { if the denominator contains less zeros
  1587. then the numerator
  1588. then d is greater than the n }
  1589. if lzn>lzz then
  1590. exit;
  1591. shift:=lzz-lzn;
  1592. n:=n shl shift;
  1593. for shift:=shift downto 0 do
  1594. begin
  1595. if z>=n then
  1596. begin
  1597. z:=z-n;
  1598. result:=result+dword(dword(1) shl shift);
  1599. end;
  1600. n:=n shr 1;
  1601. end;
  1602. end;
  1603. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1604. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1605. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1606. var
  1607. shift,lzz,lzn : ObjpasInt;
  1608. begin
  1609. result:=0;
  1610. if n=0 then
  1611. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1612. if z=0 then
  1613. exit;
  1614. lzz:=BsrDWord(z);
  1615. lzn:=BsrDWord(n);
  1616. { if the denominator contains less zeros
  1617. then the numerator
  1618. then d is greater than the n }
  1619. if lzn>lzz then
  1620. begin
  1621. result:=z;
  1622. exit;
  1623. end;
  1624. shift:=lzz-lzn;
  1625. n:=n shl shift;
  1626. for shift:=shift downto 0 do
  1627. begin
  1628. if z>=n then
  1629. z:=z-n;
  1630. n:=n shr 1;
  1631. end;
  1632. result:=z;
  1633. end;
  1634. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1635. {$ifndef FPC_SYSTEM_HAS_DIV_WORD}
  1636. function fpc_div_word(n,z : word) : word; [public,alias: 'FPC_DIV_WORD']; compilerproc;
  1637. var
  1638. shift,lzz,lzn : Byte;
  1639. begin
  1640. result:=0;
  1641. if n=0 then
  1642. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1643. if z=0 then
  1644. exit;
  1645. lzz:=BsrWord(z);
  1646. lzn:=BsrWord(n);
  1647. { if the denominator contains less zeros
  1648. then the numerator
  1649. then d is greater than the n }
  1650. if lzn>lzz then
  1651. exit;
  1652. shift:=lzz-lzn;
  1653. n:=n shl shift;
  1654. for shift:=shift downto 0 do
  1655. begin
  1656. if z>=n then
  1657. begin
  1658. z:=z-n;
  1659. result:=result+word(word(1) shl shift);
  1660. end;
  1661. n:=n shr 1;
  1662. end;
  1663. end;
  1664. {$endif FPC_SYSTEM_HAS_DIV_WORD}
  1665. {$ifndef FPC_SYSTEM_HAS_MOD_WORD}
  1666. function fpc_mod_word(n,z : word) : word; [public,alias: 'FPC_MOD_WORD']; compilerproc;
  1667. var
  1668. shift,lzz,lzn : Byte;
  1669. begin
  1670. result:=0;
  1671. if n=0 then
  1672. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1673. if z=0 then
  1674. exit;
  1675. lzz:=BsrWord(z);
  1676. lzn:=BsrWord(n);
  1677. { if the denominator contains less zeros
  1678. then the numerator
  1679. then d is greater than the n }
  1680. if lzn>lzz then
  1681. begin
  1682. result:=z;
  1683. exit;
  1684. end;
  1685. shift:=lzz-lzn;
  1686. n:=n shl shift;
  1687. for shift:=shift downto 0 do
  1688. begin
  1689. if z>=n then
  1690. z:=z-n;
  1691. n:=n shr 1;
  1692. end;
  1693. result:=z;
  1694. end;
  1695. {$endif FPC_SYSTEM_HAS_MOD_WORD}
  1696. {$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
  1697. function fpc_div_byte(n,z : byte) : byte; [public,alias: 'FPC_DIV_BYTE']; compilerproc;
  1698. var
  1699. shift,lzz,lzn : Byte;
  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:=BsrByte(z);
  1707. lzn:=BsrByte(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. exit;
  1713. shift:=lzz-lzn;
  1714. n:=n shl shift;
  1715. for shift:=shift downto 0 do
  1716. begin
  1717. if z>=n then
  1718. begin
  1719. z:=z-n;
  1720. result:=result+byte(byte(1) shl shift);
  1721. end;
  1722. n:=n shr 1;
  1723. end;
  1724. end;
  1725. {$endif FPC_SYSTEM_HAS_DIV_BYTE}
  1726. {$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
  1727. function fpc_mod_byte(n,z : byte) : byte; [public,alias: 'FPC_MOD_BYTE']; compilerproc;
  1728. var
  1729. shift,lzz,lzn : Byte;
  1730. begin
  1731. result:=0;
  1732. if n=0 then
  1733. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1734. if z=0 then
  1735. exit;
  1736. lzz:=BsrByte(z);
  1737. lzn:=BsrByte(n);
  1738. { if the denominator contains less zeros
  1739. then the numerator
  1740. then d is greater than the n }
  1741. if lzn>lzz then
  1742. begin
  1743. result:=z;
  1744. exit;
  1745. end;
  1746. shift:=lzz-lzn;
  1747. n:=n shl shift;
  1748. for shift:=shift downto 0 do
  1749. begin
  1750. if z>=n then
  1751. z:=z-n;
  1752. n:=n shr 1;
  1753. end;
  1754. result:=z;
  1755. end;
  1756. {$endif FPC_SYSTEM_HAS_MOD_BYTE}
  1757. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1758. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1759. var
  1760. sign : boolean;
  1761. d1,d2 : dword;
  1762. begin
  1763. if n=0 then
  1764. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1765. sign:=false;
  1766. if z<0 then
  1767. begin
  1768. sign:=not(sign);
  1769. d1:=dword(-z);
  1770. end
  1771. else
  1772. d1:=z;
  1773. if n<0 then
  1774. begin
  1775. sign:=not(sign);
  1776. d2:=dword(-n);
  1777. end
  1778. else
  1779. d2:=n;
  1780. { the div is coded by the compiler as call to divdword }
  1781. if sign then
  1782. result:=-(d1 div d2)
  1783. else
  1784. result:=d1 div d2;
  1785. end;
  1786. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1787. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1788. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1789. var
  1790. signed : boolean;
  1791. r,nq,zq : dword;
  1792. begin
  1793. if n=0 then
  1794. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1795. nq:=abs(n);
  1796. if z<0 then
  1797. begin
  1798. zq:=dword(-z);
  1799. signed:=true;
  1800. end
  1801. else
  1802. begin
  1803. zq:=z;
  1804. signed:=false;
  1805. end;
  1806. r:=zq mod nq;
  1807. if signed then
  1808. result:=-longint(r)
  1809. else
  1810. result:=r;
  1811. end;
  1812. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1813. {$ifndef FPC_SYSTEM_HAS_DIV_SMALLINT}
  1814. function fpc_div_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_DIV_SMALLINT']; compilerproc;
  1815. var
  1816. sign : boolean;
  1817. w1,w2 : word;
  1818. begin
  1819. if n=0 then
  1820. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1821. sign:=false;
  1822. if z<0 then
  1823. begin
  1824. sign:=not(sign);
  1825. w1:=word(-z);
  1826. end
  1827. else
  1828. w1:=z;
  1829. if n<0 then
  1830. begin
  1831. sign:=not(sign);
  1832. w2:=word(-n);
  1833. end
  1834. else
  1835. w2:=n;
  1836. { the div is coded by the compiler as call to divdword }
  1837. if sign then
  1838. result:=-(w1 div w2)
  1839. else
  1840. result:=w1 div w2;
  1841. end;
  1842. {$endif FPC_SYSTEM_HAS_DIV_SMALLINT}
  1843. {$ifndef FPC_SYSTEM_HAS_MOD_SMALLINT}
  1844. function fpc_mod_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_MOD_SMALLINT']; compilerproc;
  1845. var
  1846. signed : boolean;
  1847. r,nq,zq : word;
  1848. begin
  1849. if n=0 then
  1850. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1851. nq:=abs(n);
  1852. if z<0 then
  1853. begin
  1854. zq:=word(-z);
  1855. signed:=true;
  1856. end
  1857. else
  1858. begin
  1859. zq:=z;
  1860. signed:=false;
  1861. end;
  1862. r:=zq mod nq;
  1863. if signed then
  1864. result:=-smallint(r)
  1865. else
  1866. result:=r;
  1867. end;
  1868. {$endif FPC_SYSTEM_HAS_MOD_SMALLINT}
  1869. {$ifndef FPC_SYSTEM_HAS_DIV_SHORTINT}
  1870. function fpc_div_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_DIV_SHORTINT']; compilerproc;
  1871. var
  1872. sign : boolean;
  1873. b1,b2 : byte;
  1874. begin
  1875. if n=0 then
  1876. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1877. sign:=false;
  1878. if z<0 then
  1879. begin
  1880. sign:=not(sign);
  1881. b1:=byte(-z);
  1882. end
  1883. else
  1884. b1:=z;
  1885. if n<0 then
  1886. begin
  1887. sign:=not(sign);
  1888. b2:=byte(-n);
  1889. end
  1890. else
  1891. b2:=n;
  1892. { the div is coded by the compiler as call to divdword }
  1893. if sign then
  1894. result:=-(b1 div b2)
  1895. else
  1896. result:=b1 div b2;
  1897. end;
  1898. {$endif FPC_SYSTEM_HAS_DIV_SHORTINT}
  1899. {$ifndef FPC_SYSTEM_HAS_MOD_SHORTINT}
  1900. function fpc_mod_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_MOD_SHORTINT']; compilerproc;
  1901. var
  1902. signed : boolean;
  1903. r,nq,zq : byte;
  1904. begin
  1905. if n=0 then
  1906. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1907. nq:=abs(n);
  1908. if z<0 then
  1909. begin
  1910. zq:=byte(-z);
  1911. signed:=true;
  1912. end
  1913. else
  1914. begin
  1915. zq:=z;
  1916. signed:=false;
  1917. end;
  1918. r:=zq mod nq;
  1919. if signed then
  1920. result:=-shortint(r)
  1921. else
  1922. result:=r;
  1923. end;
  1924. {$endif FPC_SYSTEM_HAS_MOD_SHORTINT}
  1925. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1926. {****************************************************************************}
  1927. {$if defined(CPUINT8)}
  1928. {$ifndef FPC_SYSTEM_HAS_ABS_SHORTINT}
  1929. function abs(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1930. begin
  1931. if l<0 then
  1932. abs:=-l
  1933. else
  1934. abs:=l;
  1935. end;
  1936. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  1937. {$endif CPUINT8}
  1938. {$if defined(CPUINT16) or defined(CPUINT8)}
  1939. {$ifndef FPC_SYSTEM_HAS_ABS_SMALLINT}
  1940. function abs(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1941. begin
  1942. if l<0 then
  1943. abs:=-l
  1944. else
  1945. abs:=l;
  1946. end;
  1947. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  1948. {$endif CPUINT16 or CPUINT8}
  1949. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1950. { This is only needed to bootstrap on SPARC targets
  1951. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  1952. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1953. begin
  1954. if l<0 then
  1955. abs:=-l
  1956. else
  1957. abs:=l;
  1958. end;
  1959. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1960. {$if defined(CPUINT8)}
  1961. {$ifndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  1962. function odd(l:shortint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1963. begin
  1964. odd:=boolean(l and 1);
  1965. end;
  1966. {$endif ndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  1967. {$ifndef FPC_SYSTEM_HAS_ODD_BYTE}
  1968. function odd(l:byte):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1969. begin
  1970. odd:=boolean(l and 1);
  1971. end;
  1972. {$endif ndef FPC_SYSTEM_HAS_ODD_BYTE}
  1973. {$endif CPUINT8}
  1974. {$if defined(CPUINT16) or defined(CPUINT8)}
  1975. {$ifndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  1976. function odd(l:smallint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1977. begin
  1978. odd:=boolean(l and 1);
  1979. end;
  1980. {$endif ndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  1981. {$ifndef FPC_SYSTEM_HAS_ODD_WORD}
  1982. function odd(l:word):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1983. begin
  1984. odd:=boolean(l and 1);
  1985. end;
  1986. {$endif ndef FPC_SYSTEM_HAS_ODD_WORD}
  1987. {$endif CPUINT16 or CPUINT8}
  1988. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1989. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1990. begin
  1991. odd:=boolean(l and 1);
  1992. end;
  1993. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1994. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1995. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1996. begin
  1997. odd:=boolean(l and 1);
  1998. end;
  1999. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  2000. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  2001. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2002. begin
  2003. odd:=boolean(longint(l) and 1);
  2004. end;
  2005. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  2006. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  2007. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2008. begin
  2009. odd:=boolean(longint(l) and 1);
  2010. end;
  2011. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  2012. {$if defined(CPUINT8)}
  2013. {$ifndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  2014. function sqr(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2015. begin
  2016. sqr:=l*l;
  2017. end;
  2018. {$endif ndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  2019. {$endif CPUINT8}
  2020. {$if defined(CPUINT16) or defined(CPUINT8)}
  2021. {$ifndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  2022. function sqr(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2023. begin
  2024. sqr:=l*l;
  2025. end;
  2026. {$endif ndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  2027. {$endif CPUINT16 or CPUINT8}
  2028. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  2029. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2030. begin
  2031. sqr:=l*l;
  2032. end;
  2033. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  2034. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  2035. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2036. begin
  2037. if l < 0 then
  2038. abs := -l
  2039. else
  2040. abs := l;
  2041. end;
  2042. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  2043. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  2044. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2045. begin
  2046. sqr := l*l;
  2047. end;
  2048. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  2049. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  2050. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2051. begin
  2052. sqr := l*l;
  2053. end;
  2054. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  2055. {$ifdef CPU16}
  2056. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  2057. function declocked(var l:smallint):boolean;
  2058. begin
  2059. Dec(l);
  2060. declocked:=(l=0);
  2061. end;
  2062. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  2063. {$endif CPU16}
  2064. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  2065. function declocked(var l:longint):boolean;
  2066. begin
  2067. Dec(l);
  2068. declocked:=(l=0);
  2069. end;
  2070. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  2071. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  2072. function declocked(var l:int64):boolean;
  2073. begin
  2074. Dec(l);
  2075. declocked:=(l=0);
  2076. end;
  2077. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  2078. {$ifdef CPU16}
  2079. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2080. procedure inclocked(var l:smallint);
  2081. begin
  2082. Inc(l);
  2083. end;
  2084. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2085. {$endif CPU16}
  2086. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2087. procedure inclocked(var l:longint);
  2088. begin
  2089. Inc(l);
  2090. end;
  2091. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2092. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2093. procedure inclocked(var l:int64);
  2094. begin
  2095. Inc(l);
  2096. end;
  2097. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2098. {$ifndef FPC_SYSTEM_HAS_SPTR}
  2099. {_$error Sptr must be defined for each processor }
  2100. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  2101. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2102. var
  2103. tmp: PtrUInt;
  2104. begin
  2105. tmp:=addr+PtrUInt(alignment-1);
  2106. result:=tmp-(tmp mod alignment)
  2107. end;
  2108. {$ifndef cpujvm}
  2109. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  2110. var
  2111. tmp: PtrUInt;
  2112. begin
  2113. tmp:=PtrUInt(addr)+(alignment-1);
  2114. result:=pointer(tmp-(tmp mod alignment));
  2115. end;
  2116. {$endif}
  2117. {****************************************************************************
  2118. Str()
  2119. ****************************************************************************}
  2120. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2121. procedure int_str(l:longint;out s:shortstring);
  2122. var
  2123. m,m1 : longword;
  2124. pcstart,
  2125. pc2start,
  2126. pc,pc2 : pchar;
  2127. hs : string[32];
  2128. overflow : longint;
  2129. begin
  2130. pc2start:=@s[1];
  2131. pc2:=pc2start;
  2132. if (l<0) then
  2133. begin
  2134. pc2^:='-';
  2135. inc(pc2);
  2136. m:=longword(-l);
  2137. end
  2138. else
  2139. m:=longword(l);
  2140. pcstart:=pchar(@hs[0]);
  2141. pc:=pcstart;
  2142. repeat
  2143. m1:=m div 10;
  2144. inc(pc);
  2145. pc^:=char(m-(m1*10)+byte('0'));
  2146. m:=m1;
  2147. until m=0;
  2148. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2149. if overflow>0 then
  2150. inc(pcstart,overflow);
  2151. while (pc>pcstart) do
  2152. begin
  2153. pc2^:=pc^;
  2154. inc(pc2);
  2155. dec(pc);
  2156. end;
  2157. s[0]:=char(pc2-pc2start);
  2158. end;
  2159. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2160. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2161. procedure int_str_unsigned(l:longword;out s:shortstring);
  2162. var
  2163. m1 : longword;
  2164. pcstart,
  2165. pc2start,
  2166. pc,pc2 : pchar;
  2167. hs : string[32];
  2168. overflow : longint;
  2169. begin
  2170. pc2start:=@s[1];
  2171. pc2:=pc2start;
  2172. pcstart:=pchar(@hs[0]);
  2173. pc:=pcstart;
  2174. repeat
  2175. inc(pc);
  2176. m1:=l div 10;
  2177. pc^:=char(l-(m1*10)+byte('0'));
  2178. l:=m1;
  2179. until l=0;
  2180. overflow:=(pc-pcstart)-high(s);
  2181. if overflow>0 then
  2182. inc(pcstart,overflow);
  2183. while (pc>pcstart) do
  2184. begin
  2185. pc2^:=pc^;
  2186. inc(pc2);
  2187. dec(pc);
  2188. end;
  2189. s[0]:=char(pc2-pc2start);
  2190. end;
  2191. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2192. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2193. procedure int_str(l:int64;out s:shortstring);
  2194. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2195. begin
  2196. runerror(217);
  2197. end;
  2198. {$else EXCLUDE_COMPLEX_PROCS}
  2199. var
  2200. m,m1 : qword;
  2201. pcstart,
  2202. pc2start,
  2203. pc,pc2 : pchar;
  2204. hs : string[32];
  2205. overflow : longint;
  2206. begin
  2207. pc2start:=@s[1];
  2208. pc2:=pc2start;
  2209. if (l<0) then
  2210. begin
  2211. pc2^:='-';
  2212. inc(pc2);
  2213. m:=qword(-l);
  2214. end
  2215. else
  2216. m:=qword(l);
  2217. pcstart:=pchar(@hs[0]);
  2218. pc:=pcstart;
  2219. repeat
  2220. m1:=m div 10;
  2221. inc(pc);
  2222. pc^:=char(m-(m1*10)+byte('0'));
  2223. m:=m1;
  2224. until m=0;
  2225. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2226. if overflow>0 then
  2227. inc(pcstart,overflow);
  2228. while (pc>pcstart) do
  2229. begin
  2230. pc2^:=pc^;
  2231. inc(pc2);
  2232. dec(pc);
  2233. end;
  2234. s[0]:=char(pc2-pc2start);
  2235. end;
  2236. {$endif EXCLUDE_COMPLEX_PROCS}
  2237. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2238. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2239. procedure int_str_unsigned(l:qword;out s:shortstring);
  2240. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2241. begin
  2242. runerror(217);
  2243. end;
  2244. {$else EXCLUDE_COMPLEX_PROCS}
  2245. var
  2246. m1 : qword;
  2247. pcstart,
  2248. pc2start,
  2249. pc,pc2 : pchar;
  2250. hs : string[64];
  2251. overflow : longint;
  2252. begin
  2253. pc2start:=@s[1];
  2254. pc2:=pc2start;
  2255. pcstart:=pchar(@hs[0]);
  2256. pc:=pcstart;
  2257. repeat
  2258. inc(pc);
  2259. m1:=l div 10;
  2260. pc^:=char(l-(m1*10)+byte('0'));
  2261. l:=m1;
  2262. until l=0;
  2263. overflow:=(pc-pcstart)-high(s);
  2264. if overflow>0 then
  2265. inc(pcstart,overflow);
  2266. while (pc>pcstart) do
  2267. begin
  2268. pc2^:=pc^;
  2269. inc(pc2);
  2270. dec(pc);
  2271. end;
  2272. s[0]:=char(pc2-pc2start);
  2273. end;
  2274. {$endif EXCLUDE_COMPLEX_PROCS}
  2275. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2276. {$ifndef FPUNONE}
  2277. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  2278. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2279. begin
  2280. softfloat_exception_flags:=[];
  2281. end;
  2282. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  2283. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  2284. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2285. begin
  2286. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  2287. end;
  2288. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  2289. {$endif}
  2290. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  2291. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2292. begin
  2293. { the extra Word type cast is necessary because the "AValue shr 8" }
  2294. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  2295. { the sign bits from the upper 16 bits are shifted in rather than }
  2296. { zeroes. }
  2297. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  2298. end;
  2299. {$ifndef cpujvm}
  2300. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2301. begin
  2302. Result := Word((AValue shr 8) or (AValue shl 8));
  2303. end;
  2304. {$endif}
  2305. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2306. begin
  2307. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2308. Result := (Result shl 16) or (Result shr 16);
  2309. end;
  2310. {$ifndef cpujvm}
  2311. function SwapEndian(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2312. begin
  2313. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2314. Result := (Result shl 16) or (Result shr 16);
  2315. end;
  2316. {$endif}
  2317. function SwapEndian(const AValue: Int64): Int64;
  2318. begin
  2319. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2320. ((AValue shr 8) and $00FF00FF00FF00FF);
  2321. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2322. ((Result shr 16) and $0000FFFF0000FFFF);
  2323. Result := (Result shl 32) or ((Result shr 32));
  2324. end;
  2325. {$ifndef cpujvm}
  2326. function SwapEndian(const AValue: QWord): QWord;
  2327. begin
  2328. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2329. ((AValue shr 8) and $00FF00FF00FF00FF);
  2330. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2331. ((Result shr 16) and $0000FFFF0000FFFF);
  2332. Result := (Result shl 32) or ((Result shr 32));
  2333. end;
  2334. {$endif}
  2335. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  2336. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2337. begin
  2338. {$IFDEF ENDIAN_BIG}
  2339. Result := AValue;
  2340. {$ELSE}
  2341. Result := SwapEndian(AValue);
  2342. {$ENDIF}
  2343. end;
  2344. {$ifndef cpujvm}
  2345. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2346. begin
  2347. {$IFDEF ENDIAN_BIG}
  2348. Result := AValue;
  2349. {$ELSE}
  2350. Result := SwapEndian(AValue);
  2351. {$ENDIF}
  2352. end;
  2353. {$endif not cpujvm}
  2354. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2355. begin
  2356. {$IFDEF ENDIAN_BIG}
  2357. Result := AValue;
  2358. {$ELSE}
  2359. Result := SwapEndian(AValue);
  2360. {$ENDIF}
  2361. end;
  2362. {$ifndef cpujvm}
  2363. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2364. begin
  2365. {$IFDEF ENDIAN_BIG}
  2366. Result := AValue;
  2367. {$ELSE}
  2368. Result := SwapEndian(AValue);
  2369. {$ENDIF}
  2370. end;
  2371. {$endif not cpujvm}
  2372. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2373. begin
  2374. {$IFDEF ENDIAN_BIG}
  2375. Result := AValue;
  2376. {$ELSE}
  2377. Result := SwapEndian(AValue);
  2378. {$ENDIF}
  2379. end;
  2380. {$ifndef cpujvm}
  2381. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2382. begin
  2383. {$IFDEF ENDIAN_BIG}
  2384. Result := AValue;
  2385. {$ELSE}
  2386. Result := SwapEndian(AValue);
  2387. {$ENDIF}
  2388. end;
  2389. {$endif not cpujvm}
  2390. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2391. begin
  2392. {$IFDEF ENDIAN_LITTLE}
  2393. Result := AValue;
  2394. {$ELSE}
  2395. Result := SwapEndian(AValue);
  2396. {$ENDIF}
  2397. end;
  2398. {$ifndef cpujvm}
  2399. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2400. begin
  2401. {$IFDEF ENDIAN_LITTLE}
  2402. Result := AValue;
  2403. {$ELSE}
  2404. Result := SwapEndian(AValue);
  2405. {$ENDIF}
  2406. end;
  2407. {$endif not cpujvm}
  2408. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2409. begin
  2410. {$IFDEF ENDIAN_LITTLE}
  2411. Result := AValue;
  2412. {$ELSE}
  2413. Result := SwapEndian(AValue);
  2414. {$ENDIF}
  2415. end;
  2416. {$ifndef cpujvm}
  2417. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2418. begin
  2419. {$IFDEF ENDIAN_LITTLE}
  2420. Result := AValue;
  2421. {$ELSE}
  2422. Result := SwapEndian(AValue);
  2423. {$ENDIF}
  2424. end;
  2425. {$endif not cpujvm}
  2426. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2427. begin
  2428. {$IFDEF ENDIAN_LITTLE}
  2429. Result := AValue;
  2430. {$ELSE}
  2431. Result := SwapEndian(AValue);
  2432. {$ENDIF}
  2433. end;
  2434. {$ifndef cpujvm}
  2435. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2436. begin
  2437. {$IFDEF ENDIAN_LITTLE}
  2438. Result := AValue;
  2439. {$ELSE}
  2440. Result := SwapEndian(AValue);
  2441. {$ENDIF}
  2442. end;
  2443. {$endif not cpujvm}
  2444. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2445. begin
  2446. {$IFDEF ENDIAN_BIG}
  2447. Result := AValue;
  2448. {$ELSE}
  2449. Result := SwapEndian(AValue);
  2450. {$ENDIF}
  2451. end;
  2452. {$ifndef cpujvm}
  2453. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2454. begin
  2455. {$IFDEF ENDIAN_BIG}
  2456. Result := AValue;
  2457. {$ELSE}
  2458. Result := SwapEndian(AValue);
  2459. {$ENDIF}
  2460. end;
  2461. {$endif not cpujvm}
  2462. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2463. begin
  2464. {$IFDEF ENDIAN_BIG}
  2465. Result := AValue;
  2466. {$ELSE}
  2467. Result := SwapEndian(AValue);
  2468. {$ENDIF}
  2469. end;
  2470. {$ifndef cpujvm}
  2471. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2472. begin
  2473. {$IFDEF ENDIAN_BIG}
  2474. Result := AValue;
  2475. {$ELSE}
  2476. Result := SwapEndian(AValue);
  2477. {$ENDIF}
  2478. end;
  2479. {$endif not cpujvm}
  2480. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2481. begin
  2482. {$IFDEF ENDIAN_BIG}
  2483. Result := AValue;
  2484. {$ELSE}
  2485. Result := SwapEndian(AValue);
  2486. {$ENDIF}
  2487. end;
  2488. {$ifndef cpujvm}
  2489. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2490. begin
  2491. {$IFDEF ENDIAN_BIG}
  2492. Result := AValue;
  2493. {$ELSE}
  2494. Result := SwapEndian(AValue);
  2495. {$ENDIF}
  2496. end;
  2497. {$endif not cpujvm}
  2498. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2499. begin
  2500. {$IFDEF ENDIAN_LITTLE}
  2501. Result := AValue;
  2502. {$ELSE}
  2503. Result := SwapEndian(AValue);
  2504. {$ENDIF}
  2505. end;
  2506. {$ifndef cpujvm}
  2507. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2508. begin
  2509. {$IFDEF ENDIAN_LITTLE}
  2510. Result := AValue;
  2511. {$ELSE}
  2512. Result := SwapEndian(AValue);
  2513. {$ENDIF}
  2514. end;
  2515. {$endif not cpujvm}
  2516. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2517. begin
  2518. {$IFDEF ENDIAN_LITTLE}
  2519. Result := AValue;
  2520. {$ELSE}
  2521. Result := SwapEndian(AValue);
  2522. {$ENDIF}
  2523. end;
  2524. {$ifndef cpujvm}
  2525. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2526. begin
  2527. {$IFDEF ENDIAN_LITTLE}
  2528. Result := AValue;
  2529. {$ELSE}
  2530. Result := SwapEndian(AValue);
  2531. {$ENDIF}
  2532. end;
  2533. {$endif not cpujvm}
  2534. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2535. begin
  2536. {$IFDEF ENDIAN_LITTLE}
  2537. Result := AValue;
  2538. {$ELSE}
  2539. Result := SwapEndian(AValue);
  2540. {$ENDIF}
  2541. end;
  2542. {$ifndef cpujvm}
  2543. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2544. begin
  2545. {$IFDEF ENDIAN_LITTLE}
  2546. Result := AValue;
  2547. {$ELSE}
  2548. Result := SwapEndian(AValue);
  2549. {$ENDIF}
  2550. end;
  2551. {$endif not cpujvm}
  2552. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2553. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2554. begin
  2555. end;
  2556. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2557. begin
  2558. end;
  2559. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2560. begin
  2561. end;
  2562. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2563. begin
  2564. end;
  2565. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2566. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2567. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2568. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2569. begin
  2570. Result:=(AValue shr 1) or (AValue shl 7);
  2571. end;
  2572. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2573. begin
  2574. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2575. end;
  2576. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2577. begin
  2578. Result:=(AValue shl 1) or (AValue shr 7);
  2579. end;
  2580. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2581. begin
  2582. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2583. end;
  2584. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2585. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2586. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2587. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2588. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2589. begin
  2590. Result:=(AValue shr 1) or (AValue shl 15);
  2591. end;
  2592. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2593. begin
  2594. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2595. end;
  2596. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2597. begin
  2598. Result:=(AValue shl 1) or (AValue shr 15);
  2599. end;
  2600. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2601. begin
  2602. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2603. end;
  2604. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2605. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2606. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2607. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2608. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2609. begin
  2610. Result:=(AValue shr 1) or (AValue shl 31);
  2611. end;
  2612. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2613. begin
  2614. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2615. end;
  2616. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2617. begin
  2618. Result:=(AValue shl 1) or (AValue shr 31);
  2619. end;
  2620. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2621. begin
  2622. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2623. end;
  2624. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2625. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2626. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2627. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2628. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2629. begin
  2630. Result:=(AValue shr 1) or (AValue shl 63);
  2631. end;
  2632. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2633. begin
  2634. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2635. end;
  2636. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2637. begin
  2638. Result:=(AValue shl 1) or (AValue shr 63);
  2639. end;
  2640. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2641. begin
  2642. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2643. end;
  2644. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2645. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2646. {$ifndef FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2647. {$ifndef FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2648. procedure fpc_ror_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_INT64']; compilerproc;
  2649. begin
  2650. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2651. end;
  2652. procedure fpc_ror_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_QWORD']; compilerproc;
  2653. begin
  2654. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2655. end;
  2656. procedure fpc_rol_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_INT64']; compilerproc;
  2657. begin
  2658. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2659. end;
  2660. procedure fpc_rol_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_QWORD']; compilerproc;
  2661. begin
  2662. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2663. end;
  2664. {$endif FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2665. {$endif FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2666. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2667. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2668. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2669. begin
  2670. 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)))));
  2671. end;
  2672. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2673. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2674. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2675. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2676. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2677. begin
  2678. 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)))));
  2679. end;
  2680. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2681. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2682. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2683. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2684. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2685. begin
  2686. 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)))));
  2687. end;
  2688. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2689. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2690. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2691. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2692. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2693. begin
  2694. 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)))));
  2695. end;
  2696. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2697. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2698. {$ifndef FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2699. {$ifndef FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2700. procedure fpc_sar_assign_int64(var AValue : Int64;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_INT64']; compilerproc;
  2701. begin
  2702. 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)))));
  2703. end;
  2704. procedure fpc_sar_assign_qword(var AValue : QWord;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_QWORD']; compilerproc;
  2705. begin
  2706. 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)))));
  2707. end;
  2708. {$endif FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2709. {$endif FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2710. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2711. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2712. function BsfByte(Const AValue: Byte): Byte;
  2713. const bsf8bit: array [Byte] of Byte = (
  2714. $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,
  2715. 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,
  2716. 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,
  2717. 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,
  2718. 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,
  2719. 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,
  2720. 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,
  2721. 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
  2722. );
  2723. begin
  2724. result:=bsf8bit[AValue];
  2725. end;
  2726. {$endif}
  2727. {$endif}
  2728. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2729. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2730. function BsrByte(Const AValue: Byte): Byte;
  2731. const bsr8bit: array [Byte] of Byte = (
  2732. $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,
  2733. 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,
  2734. 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,
  2735. 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,
  2736. 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,
  2737. 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,
  2738. 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,
  2739. 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
  2740. );
  2741. begin
  2742. result:=bsr8bit[AValue];
  2743. end;
  2744. {$endif}
  2745. {$endif}
  2746. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2747. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2748. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2749. begin
  2750. result:=ord(lo(AValue)=0)*8;
  2751. result:=result or BsfByte(byte(AValue shr result));
  2752. end;
  2753. {$endif}
  2754. {$endif}
  2755. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2756. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2757. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2758. begin
  2759. result:=ord(AValue>255)*8;
  2760. result:=result or BsrByte(byte(AValue shr result));
  2761. end;
  2762. {$endif}
  2763. {$endif}
  2764. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2765. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2766. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2767. var
  2768. tmp: DWord;
  2769. begin
  2770. result:=ord(lo(AValue)=0)*16;
  2771. tmp:=AValue shr result;
  2772. result:=result or (ord((tmp and $FF)=0)*8);
  2773. tmp:=tmp shr (result and 8);
  2774. result:=result or BsfByte(byte(tmp));
  2775. end;
  2776. {$endif}
  2777. {$endif}
  2778. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2779. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2780. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2781. var
  2782. tmp: DWord;
  2783. begin
  2784. result:=ord(AValue>$FFFF)*16;
  2785. tmp:=AValue shr result;
  2786. result:=result or (ord(tmp>$FF)*8);
  2787. tmp:=tmp shr (result and 8);
  2788. result:=result or BsrByte(byte(tmp));
  2789. end;
  2790. {$endif}
  2791. {$endif}
  2792. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2793. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2794. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2795. var
  2796. tmp: DWord;
  2797. begin
  2798. result:=0;
  2799. tmp:=lo(AValue);
  2800. if (tmp=0) then
  2801. begin
  2802. tmp:=hi(AValue);
  2803. result:=32;
  2804. end;
  2805. result:=result or BsfDword(tmp);
  2806. end;
  2807. {$endif}
  2808. {$endif}
  2809. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2810. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2811. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2812. var
  2813. tmp: DWord;
  2814. begin
  2815. result:=32;
  2816. tmp:=hi(AValue);
  2817. if (tmp=0) then
  2818. begin
  2819. tmp:=lo(AValue);
  2820. result:=0;
  2821. end;
  2822. result:=result or BsrDword(tmp);
  2823. end;
  2824. {$endif}
  2825. {$endif}
  2826. const
  2827. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2828. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2829. begin
  2830. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2831. end;
  2832. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2833. var
  2834. i : SizeInt;
  2835. begin
  2836. Result:=0;
  2837. for i:=0 to 3 do
  2838. begin
  2839. inc(Result,PopCntData[AValue and $f]);
  2840. AValue:=AValue shr 4;
  2841. end;
  2842. end;
  2843. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2844. var
  2845. i : SizeInt;
  2846. begin
  2847. Result:=0;
  2848. for i:=0 to 7 do
  2849. begin
  2850. inc(Result,PopCntData[AValue and $f]);
  2851. AValue:=AValue shr 4;
  2852. end;
  2853. end;
  2854. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2855. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2856. {$ifdef VER2_6}
  2857. var
  2858. i : SizeInt;
  2859. begin
  2860. Result:=0;
  2861. for i:=0 to 15 do
  2862. begin
  2863. inc(Result,PopCntData[AValue and $f]);
  2864. AValue:=AValue shr 4;
  2865. end;
  2866. {$else VER2_6}
  2867. begin
  2868. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2869. {$endif VER2_6}
  2870. end;
  2871. {$endif}