generic.inc 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920
  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 3) or (PtrUInt(psrc) and 3))=0)
  574. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  575. then
  576. begin
  577. { Align on native pointer size }
  578. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  579. dec(len,aligncount);
  580. pend:=psrc+aligncount;
  581. while psrc<pend do
  582. begin
  583. if psrc^<>pdest^ then
  584. if psrc^>pdest^ then
  585. exit(1)
  586. else
  587. exit(-1);
  588. inc(pdest);
  589. inc(psrc);
  590. end;
  591. { use sizeuint typecast to force shr optimization }
  592. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  593. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  594. while psrc<pend do
  595. begin
  596. if pptrint(psrc)^<>pptrint(pdest)^ then
  597. begin
  598. len:=sizeof(ptruint) shr 2;
  599. break;
  600. end;
  601. inc(pptruint(pdest));
  602. inc(pptruint(psrc));
  603. end;
  604. end;
  605. if (len <= high(ptrint) div 2) and
  606. (psrc+len >= psrc) then
  607. pend:=psrc+len
  608. else
  609. pend:=pdword(high(ptruint)-4);
  610. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  611. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  612. while psrc<pend do
  613. begin
  614. if unaligned(psrc^)<>unaligned(pdest^) then
  615. if unaligned(psrc^)>unaligned(pdest^) then
  616. exit(1)
  617. else
  618. exit(-1);
  619. inc(pdest);
  620. inc(psrc);
  621. end
  622. else
  623. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  624. while psrc<pend do
  625. begin
  626. if psrc^<>pdest^ then
  627. if psrc^>pdest^ then
  628. exit(1)
  629. else
  630. exit(-1);
  631. inc(pdest);
  632. inc(psrc);
  633. end;
  634. result:=0;
  635. end;
  636. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  637. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  638. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  639. var
  640. I : SizeInt;
  641. begin
  642. if Len = 0 then
  643. exit;
  644. I:=IndexByte(Buf1,Len,0);
  645. if I<>-1 then
  646. Move(Buf1,Buf2,I)
  647. else
  648. Move(Buf1,Buf2,len);
  649. end;
  650. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  651. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  652. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  653. var
  654. psrc,pend : pbyte;
  655. begin
  656. psrc:=@buf;
  657. { simulate assembler implementations behaviour, which is expected }
  658. { fpc_pchar_to_ansistr in astrings.inc }
  659. if (len < 0) then
  660. pend:=pbyte(high(PtrUInt)-PtrUInt(sizeof(byte)))
  661. else
  662. pend:=psrc+len;
  663. while (psrc<pend) and (psrc^<>0) do
  664. begin
  665. if (psrc^=byte(b)) then
  666. begin
  667. result:=psrc-pbyte(@buf);
  668. exit;
  669. end;
  670. inc(psrc);
  671. end;
  672. result:=-1;
  673. end;
  674. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  675. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  676. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  677. var
  678. psrc,pdest,pend : pbyte;
  679. b : ptrint;
  680. begin
  681. b:=0;
  682. psrc:=@buf1;
  683. pdest:=@buf2;
  684. pend:=psrc+len;
  685. while psrc<pend do
  686. begin
  687. b:=(ptrint(psrc^)-ptrint(pdest^));
  688. if b<0 then
  689. exit(-1)
  690. else if b>0 then
  691. exit(1);
  692. if (psrc^=0) or (pdest^=0) then
  693. exit(0);
  694. inc(pdest);
  695. inc(psrc);
  696. end;
  697. result:=0;
  698. end;
  699. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  700. {****************************************************************************
  701. Object Helpers
  702. ****************************************************************************}
  703. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  704. type
  705. pobjectvmt=^tobjectvmt;
  706. tobjectvmt=record
  707. size,msize:sizeuint;
  708. parent:{$ifdef VER3_0}pointer{$else}ppointer{$endif};
  709. end;
  710. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  711. { Note: _vmt will be reset to -1 when memory is allocated,
  712. this is needed for fpc_help_fail }
  713. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  714. var
  715. vmtcopy : pobjectvmt;
  716. begin
  717. vmtcopy:=pobjectvmt(_vmt);
  718. { Inherited call? }
  719. if vmtcopy=nil then
  720. begin
  721. fpc_help_constructor:=_self;
  722. exit;
  723. end;
  724. if (_self=nil) and
  725. (vmtcopy^.size>0) then
  726. begin
  727. getmem(_self,vmtcopy^.size);
  728. { reset vmt needed for fail }
  729. _vmt:=pointer(-1);
  730. end;
  731. if _self<>nil then
  732. begin
  733. fillchar(_self^,vmtcopy^.size,0);
  734. ppointer(_self+_vmt_pos)^:=vmtcopy;
  735. end;
  736. fpc_help_constructor:=_self;
  737. end;
  738. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  739. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  740. { Note: _self will not be reset, the compiler has to generate the reset }
  741. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  742. begin
  743. { already released? }
  744. if (_self=nil) or
  745. (_vmt<>pointer(-1)) or
  746. (ppointer(_self+vmt_pos)^=nil) then
  747. exit;
  748. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  749. (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  750. RunError(210);
  751. { reset vmt to nil for protection }
  752. ppointer(_self+vmt_pos)^:=nil;
  753. freemem(_self);
  754. end;
  755. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  756. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  757. { Note: _self will not be reset, the compiler has to generate the reset }
  758. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  759. begin
  760. if (_self=nil) or (_vmt=nil) then
  761. exit;
  762. { vmt=$ffffffff when memory was allocated }
  763. if ptruint(_vmt)=high(ptruint) then
  764. begin
  765. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  766. HandleError(210)
  767. else
  768. begin
  769. ppointer(_self+vmt_pos)^:=nil;
  770. freemem(_self);
  771. { reset _vmt to nil so it will not be freed a
  772. second time }
  773. _vmt:=nil;
  774. end;
  775. end
  776. else
  777. ppointer(_self+vmt_pos)^:=nil;
  778. end;
  779. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  780. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  781. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  782. begin
  783. if (_vmt=nil) or
  784. (pobjectvmt(_vmt)^.size=0) or
  785. (pobjectvmt(_vmt)^.size+pobjectvmt(_vmt)^.msize<>0) then
  786. RunError(210);
  787. end;
  788. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  789. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  790. { checks for a correct vmt pointer }
  791. { deeper check to see if the current object is }
  792. { really related to the true }
  793. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  794. begin
  795. if (vmt=nil) or
  796. (pobjectvmt(vmt)^.size=0) or
  797. (pobjectvmt(vmt)^.size+pobjectvmt(vmt)^.msize<>0) then
  798. RunError(210);
  799. while assigned(vmt) do
  800. if vmt=expvmt then
  801. exit
  802. else
  803. {$ifdef VER3_0}
  804. vmt:=pobjectvmt(vmt)^.parent;
  805. {$else VER3_0}
  806. if assigned(pobjectvmt(vmt)^.parent) then
  807. vmt:=pobjectvmt(vmt)^.parent^
  808. else
  809. vmt:=nil;
  810. {$endif}
  811. RunError(219);
  812. end;
  813. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  814. {$endif FPC_HAS_FEATURE_OBJECTS}
  815. {****************************************************************************
  816. String
  817. ****************************************************************************}
  818. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  819. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  820. var
  821. slen : byte;
  822. begin
  823. slen:=length(sstr);
  824. if slen>high(res) then
  825. slen:=high(res);
  826. move(sstr[0],res[0],slen+1);
  827. res[0]:=chr(slen);
  828. end;
  829. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  830. var
  831. slen : byte;
  832. begin
  833. slen:=length(pshortstring(sstr)^);
  834. if slen<len then
  835. len:=slen;
  836. move(sstr^,dstr^,len+1);
  837. if slen>len then
  838. pchar(dstr)^:=chr(len);
  839. end;
  840. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  841. {$push}
  842. { ensure that comparing addresses of openshortstrings with regular shortstrings
  843. doesn't cause errors }
  844. {$t-}
  845. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  846. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  847. var
  848. s1l, s2l : ObjpasInt;
  849. begin
  850. s1l:=length(s1);
  851. s2l:=length(s2);
  852. if s1l+s2l>high(dests) then
  853. begin
  854. if s1l>high(dests) then
  855. s1l:=high(dests);
  856. s2l:=high(dests)-s1l;
  857. end;
  858. if @dests=@s1 then
  859. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l)
  860. else
  861. if @dests=@s2 then
  862. begin
  863. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,s1l+1,s2l);
  864. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  865. end
  866. else
  867. begin
  868. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  869. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  870. end;
  871. dests[0]:=chr(s1l+s2l);
  872. end;
  873. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  874. var
  875. s2l : byte;
  876. LowStart,i,
  877. Len : ObjpasInt;
  878. needtemp : boolean;
  879. tmpstr : shortstring;
  880. p,pdest : pshortstring;
  881. begin
  882. if high(sarr)=0 then
  883. begin
  884. DestS:='';
  885. exit;
  886. end;
  887. lowstart:=low(sarr);
  888. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  889. inc(lowstart);
  890. { Check for another reuse, then we can't use
  891. the append optimization and need to use a temp }
  892. needtemp:=false;
  893. for i:=lowstart to high(sarr) do
  894. begin
  895. if Pointer(@DestS)=Pointer(sarr[i]) then
  896. begin
  897. needtemp:=true;
  898. break;
  899. end;
  900. end;
  901. if needtemp then
  902. begin
  903. lowstart:=low(sarr);
  904. tmpstr:='';
  905. pdest:=@tmpstr
  906. end
  907. else
  908. begin
  909. { Start with empty DestS if we start with concatting
  910. the first array element }
  911. if lowstart=low(sarr) then
  912. DestS:='';
  913. pdest:=@DestS;
  914. end;
  915. { Concat all strings, except the string we already
  916. copied in DestS }
  917. Len:=length(pdest^);
  918. for i:=lowstart to high(sarr) do
  919. begin
  920. p:=sarr[i];
  921. if assigned(p) then
  922. begin
  923. s2l:=length(p^);
  924. if Len+s2l>high(dests) then
  925. s2l:=high(dests)-Len;
  926. fpc_shortstr_shortstr_intern_charmove(p^,1,pdest^,Len+1,s2l);
  927. inc(Len,s2l);
  928. end;
  929. end;
  930. pdest^[0]:=Chr(Len);
  931. if needtemp then
  932. DestS:=TmpStr;
  933. end;
  934. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  935. {$pop}
  936. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  937. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  938. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  939. var
  940. s1l, s2l : sizeint;
  941. begin
  942. s1l:=length(s1);
  943. s2l:=length(s2);
  944. if s1l+s2l>high(s1) then
  945. s2l:=high(s1)-s1l;
  946. move(s2[1],s1[s1l+1],s2l);
  947. s1[0]:=chr(s1l+s2l);
  948. end;
  949. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  950. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  951. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  952. var
  953. s1,s2,max,i : byte;
  954. d : ObjpasInt;
  955. begin
  956. s1:=length(left);
  957. s2:=length(right);
  958. if s1<s2 then
  959. max:=s1
  960. else
  961. max:=s2;
  962. for i:=1 to max do
  963. begin
  964. d:=byte(left[i])-byte(right[i]);
  965. if d>0 then
  966. exit(1)
  967. else if d<0 then
  968. exit(-1);
  969. end;
  970. if s1>s2 then
  971. exit(1)
  972. else if s1<s2 then
  973. exit(-1)
  974. else
  975. exit(0);
  976. end;
  977. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  978. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  979. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  980. begin
  981. Result := ObjpasInt(left[0]) - ObjpasInt(right[0]);
  982. if Result = 0 then
  983. Result := CompareByte(left[1],right[1], ObjpasInt(left[0]));
  984. end;
  985. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  986. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  987. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  988. var
  989. l : ObjpasInt;
  990. s: shortstring;
  991. begin
  992. if p=nil then
  993. l:=0
  994. else
  995. l:=strlen(p);
  996. if l>high(res) then
  997. l:=high(res);
  998. if l>0 then
  999. move(p^,s[1],l);
  1000. s[0]:=chr(l);
  1001. res:=s;
  1002. end;
  1003. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1004. {$ifndef cpujvm}
  1005. { also define alias which can be used inside the system unit }
  1006. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1007. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1008. begin
  1009. fpc_pchar_to_shortstr(result,p);
  1010. end;
  1011. {$endif not cpujvm}
  1012. function Utf8CodePointLen(P: PAnsiChar; MaxLookAhead: SizeInt; IncludeCombiningDiacriticalMarks: Boolean): SizeInt;
  1013. var
  1014. bytes: sizeint;
  1015. firstzerobit: byte;
  1016. begin
  1017. { see https://en.wikipedia.org/wiki/UTF-8#Description for details }
  1018. if maxlookahead<=0 then
  1019. begin
  1020. { incomplete }
  1021. result:=0;
  1022. exit;
  1023. end;
  1024. { include the first byte }
  1025. result:=1;
  1026. { multiple byte utf-8 code point? }
  1027. if p[0]>#127 then
  1028. begin
  1029. { bsr searches for the leftmost 1 bit. We are interested in the
  1030. leftmost 0 bit, so first invert the value
  1031. }
  1032. firstzerobit:=bsrbyte(not(byte(p[0])));
  1033. { if there is no zero bit or the first zero bit is the rightmost bit
  1034. (bit 0), this is an invalid UTF-8 byte ($ff cannot appear in an
  1035. UTF-8-encoded string, and in the worst case bit 1 has to be zero)
  1036. Additionally, 5-byte UTF-8 sequences don't exist either, so bit 1
  1037. cannot be the first zero-bit either. And bits 6 and 7 can't be 0
  1038. either in the first byte.
  1039. }
  1040. if (firstzerobit<=1) or (firstzerobit>=6) then
  1041. begin
  1042. result:=-result;
  1043. exit;
  1044. end;
  1045. { the number of bytes belonging to this code point is
  1046. 7-(pos first 0-bit). Subtract 1 since we're already at the first
  1047. byte. All subsequent bytes of the same sequence must have their
  1048. highest bit set and the next one unset. We stop when we detect an
  1049. invalid sequence.
  1050. }
  1051. bytes:=6-firstzerobit;
  1052. while (result<maxlookahead) and
  1053. (bytes>0) and
  1054. ((ord(p[result]) and %11000000)=%10000000) do
  1055. begin
  1056. inc(result);
  1057. dec(bytes);
  1058. end;
  1059. { stopped because of invalid/incomplete sequence -> exit }
  1060. if bytes<>0 then
  1061. begin
  1062. if result>=maxlookahead then
  1063. result:=0
  1064. else
  1065. result:=-result;
  1066. exit;
  1067. end;
  1068. end;
  1069. if includecombiningdiacriticalmarks then
  1070. begin
  1071. { combining diacritical marks?
  1072. 1) U+0300 - U+036F in UTF-8 = %11001100 10000000 - %11001101 10101111
  1073. 2) U+1AB0 - U+1AFF in UTF-8 = %11100001 10101010 10110000 - %11100001 10101011 10111111
  1074. 3) U+1DC0 - U+1DFF in UTF-8 = %11100001 10110111 10000000 - %11100001 10110111 10111111
  1075. 4) U+20D0 - U+20FF in UTF-8 = %11100010 10000011 10010000 - %11100010 10000011 10111111
  1076. 5) U+FE20 - U+FE2F in UTF-8 = %11101111 10111000 10100000 - %11101111 10111000 10101111
  1077. }
  1078. repeat
  1079. bytes:=result;
  1080. if result+1<maxlookahead then
  1081. begin
  1082. { case 1) }
  1083. if ((ord(p[result]) and %11001100=%11001100)) and
  1084. (ord(p[result+1])>=%10000000) and
  1085. (ord(p[result+1])<=%10101111) then
  1086. inc(result,2)
  1087. { case 2), 3), 4), 5) }
  1088. else if (result+2<maxlookahead) and
  1089. (ord(p[result])>=%11100001) then
  1090. begin
  1091. { case 2) }
  1092. if ((ord(p[result])=%11100001) and
  1093. (ord(p[result+1])=%10101010) and
  1094. (ord(p[result+2])>=%10110000) and
  1095. (ord(p[result+2])<=%10111111)) or
  1096. { case 3) }
  1097. ((ord(p[result])=%11100001) and
  1098. (ord(p[result+1])=%10110111) and
  1099. (ord(p[result+2])>=%10000000) and
  1100. (ord(p[result+2])<=%10111111)) or
  1101. { case 4) }
  1102. ((ord(p[result])=%11100010) and
  1103. (ord(p[result+1])=%10000011) and
  1104. (ord(p[result+2])>=%10010000) and
  1105. (ord(p[result+2])<=%10111111)) or
  1106. { case 5) }
  1107. ((ord(p[result])=%11101111) and
  1108. (ord(p[result+1])=%10111000) and
  1109. (ord(p[result+2])>=%10100000) and
  1110. (ord(p[result+2])<=%10101111)) then
  1111. inc(result,3);
  1112. end;
  1113. end;
  1114. until bytes=result;
  1115. { is there an incomplete diacritical mark? (invalid makes little sense:
  1116. either a sequence is a combining diacritical mark, or it's not ; if
  1117. it's invalid, it may also not have been a combining diacritical mark)
  1118. }
  1119. if result<maxlookahead then
  1120. begin
  1121. { case 1) }
  1122. if (((ord(p[result]) and %11001100=%11001100)) and
  1123. (result+1>=maxlookahead)) or
  1124. { case 2) and 3)}
  1125. ((ord(p[result])=%11100001) and
  1126. ((result+1>=maxlookahead) or
  1127. (((ord(p[result+1])=%10101010) or
  1128. (ord(p[result+1])=%10110111)) and
  1129. (result+2>=maxlookahead)))) or
  1130. { case 4 }
  1131. ((ord(p[result])=%11100010) and
  1132. ((result+1>=maxlookahead) or
  1133. ((ord(p[result+1])=%10000011) and
  1134. (result+2>=maxlookahead)))) or
  1135. { case 5 }
  1136. ((ord(p[result])=%11101111) and
  1137. ((result+1>=maxlookahead) or
  1138. ((ord(p[result+1])=%10111000) and
  1139. (result+2>=maxlookahead)))) then
  1140. begin
  1141. result:=0;
  1142. exit;
  1143. end;
  1144. end;
  1145. end;
  1146. end;
  1147. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1148. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1149. var
  1150. l: ObjpasInt;
  1151. index: ObjpasInt;
  1152. len: byte;
  1153. begin
  1154. l:=high(arr)+1;
  1155. if l>=high(res)+1 then
  1156. l:=high(res)
  1157. else if l<0 then
  1158. l:=0;
  1159. if zerobased then
  1160. begin
  1161. index:=IndexByte(arr[0],l,0);
  1162. if index<0 then
  1163. len:=l
  1164. else
  1165. len:=index;
  1166. end
  1167. else
  1168. len:=l;
  1169. move(arr[0],res[1],len);
  1170. res[0]:=chr(len);
  1171. end;
  1172. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1173. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1174. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1175. var
  1176. len: ObjpasInt;
  1177. begin
  1178. len := length(src);
  1179. if len > length(res) then
  1180. len := length(res);
  1181. {$push}{$r-}
  1182. { make sure we don't access char 1 if length is 0 (JM) }
  1183. if len > 0 then
  1184. move(src[1],res[0],len);
  1185. fillchar(res[len],length(res)-len,0);
  1186. {$pop}
  1187. end;
  1188. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1189. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1190. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1191. begin
  1192. if assigned(p) then
  1193. Result:=IndexByte(p^,high(Result),0)
  1194. else
  1195. Result:=0;
  1196. end;
  1197. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1198. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1199. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1200. var i : sizeint;
  1201. begin
  1202. i:=0;
  1203. if assigned(p) then
  1204. while p[i]<>#0 do
  1205. inc(i);
  1206. exit(i);
  1207. end;
  1208. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1209. {****************************************************************************
  1210. Caller/StackFrame Helpers
  1211. ****************************************************************************}
  1212. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1213. {_$error Get_frame must be defined for each processor }
  1214. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1215. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1216. {_$error Get_caller_addr must be defined for each processor }
  1217. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1218. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1219. {_$error Get_caller_frame must be defined for each processor }
  1220. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1221. {****************************************************************************
  1222. Math
  1223. ****************************************************************************}
  1224. {****************************************************************************
  1225. Software multiplication
  1226. ****************************************************************************}
  1227. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1228. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1229. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1230. var
  1231. sign : boolean;
  1232. q1,q2,q3 : word;
  1233. begin
  1234. { there's no difference between signed and unsigned multiplication,
  1235. when the destination size is equal to the source size and overflow
  1236. checking is off }
  1237. if not checkoverflow then
  1238. { word(f1)*word(f2) is coded as a call to mulword }
  1239. fpc_mul_integer:=integer(word(f1)*word(f2))
  1240. else
  1241. begin
  1242. sign:=false;
  1243. if f1<0 then
  1244. begin
  1245. sign:=not(sign);
  1246. q1:=word(-f1);
  1247. end
  1248. else
  1249. q1:=f1;
  1250. if f2<0 then
  1251. begin
  1252. sign:=not(sign);
  1253. q2:=word(-f2);
  1254. end
  1255. else
  1256. q2:=f2;
  1257. { the q1*q2 is coded as call to mulword }
  1258. q3:=q1*q2;
  1259. if (q1 <> 0) and (q2 <>0) and
  1260. ((q1>q3) or (q2>q3) or
  1261. { the bit 63 can be only set if we have $8000 }
  1262. { and sign is true }
  1263. (q3 shr 15<>0) and
  1264. ((q3<>word(word(1) shl 15)) or not(sign))
  1265. ) then
  1266. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1267. if sign then
  1268. fpc_mul_integer:=-q3
  1269. else
  1270. fpc_mul_integer:=q3;
  1271. end;
  1272. end;
  1273. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1274. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1275. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1276. var
  1277. _f1,bitpos : word;
  1278. b : byte;
  1279. f1overflowed : boolean;
  1280. begin
  1281. fpc_mul_word:=0;
  1282. bitpos:=1;
  1283. f1overflowed:=false;
  1284. for b:=0 to 15 do
  1285. begin
  1286. if (f2 and bitpos)<>0 then
  1287. begin
  1288. _f1:=fpc_mul_word;
  1289. fpc_mul_word:=fpc_mul_word+f1;
  1290. { if one of the operands is greater than the result an
  1291. overflow occurs }
  1292. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1293. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1294. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1295. end;
  1296. { when bootstrapping, we forget about overflow checking for qword :) }
  1297. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1298. f1:=f1 shl 1;
  1299. bitpos:=bitpos shl 1;
  1300. end;
  1301. end;
  1302. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1303. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1304. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1305. var
  1306. sign : boolean;
  1307. q1,q2,q3 : dword;
  1308. begin
  1309. { there's no difference between signed and unsigned multiplication,
  1310. when the destination size is equal to the source size and overflow
  1311. checking is off }
  1312. if not checkoverflow then
  1313. { dword(f1)*dword(f2) is coded as a call to muldword }
  1314. fpc_mul_longint:=longint(dword(f1)*dword(f2))
  1315. else
  1316. begin
  1317. sign:=false;
  1318. if f1<0 then
  1319. begin
  1320. sign:=not(sign);
  1321. q1:=dword(-f1);
  1322. end
  1323. else
  1324. q1:=f1;
  1325. if f2<0 then
  1326. begin
  1327. sign:=not(sign);
  1328. q2:=dword(-f2);
  1329. end
  1330. else
  1331. q2:=f2;
  1332. { the q1*q2 is coded as call to muldword }
  1333. q3:=q1*q2;
  1334. if (q1 <> 0) and (q2 <>0) and
  1335. ((q1>q3) or (q2>q3) or
  1336. { the bit 31 can be only set if we have $8000 0000 }
  1337. { and sign is true }
  1338. (q3 shr 15<>0) and
  1339. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1340. ) then
  1341. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1342. if sign then
  1343. fpc_mul_longint:=-q3
  1344. else
  1345. fpc_mul_longint:=q3;
  1346. end;
  1347. end;
  1348. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1349. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1350. { multiplies two dwords
  1351. the longbool for checkoverflow avoids a misaligned stack
  1352. }
  1353. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1354. var
  1355. _f1,bitpos : dword;
  1356. b : byte;
  1357. f1overflowed : boolean;
  1358. begin
  1359. fpc_mul_dword:=0;
  1360. bitpos:=1;
  1361. f1overflowed:=false;
  1362. for b:=0 to 31 do
  1363. begin
  1364. if (f2 and bitpos)<>0 then
  1365. begin
  1366. _f1:=fpc_mul_dword;
  1367. fpc_mul_dword:=fpc_mul_dword+f1;
  1368. { if one of the operands is greater than the result an
  1369. overflow occurs }
  1370. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1371. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1372. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1373. end;
  1374. { when bootstrapping, we forget about overflow checking for qword :) }
  1375. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1376. f1:=f1 shl 1;
  1377. bitpos:=bitpos shl 1;
  1378. end;
  1379. end;
  1380. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1381. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1382. {****************************************************************************
  1383. Software longint/dword division
  1384. ****************************************************************************}
  1385. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1386. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1387. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1388. var
  1389. shift,lzz,lzn : ObjpasInt;
  1390. begin
  1391. result:=0;
  1392. if n=0 then
  1393. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1394. if z=0 then
  1395. exit;
  1396. lzz:=BsrDWord(z);
  1397. lzn:=BsrDWord(n);
  1398. { if the denominator contains less zeros
  1399. then the numerator
  1400. then d is greater than the n }
  1401. if lzn>lzz then
  1402. exit;
  1403. shift:=lzz-lzn;
  1404. n:=n shl shift;
  1405. for shift:=shift downto 0 do
  1406. begin
  1407. if z>=n then
  1408. begin
  1409. z:=z-n;
  1410. result:=result+dword(dword(1) shl shift);
  1411. end;
  1412. n:=n shr 1;
  1413. end;
  1414. end;
  1415. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1416. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1417. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1418. var
  1419. shift,lzz,lzn : ObjpasInt;
  1420. begin
  1421. result:=0;
  1422. if n=0 then
  1423. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1424. if z=0 then
  1425. exit;
  1426. lzz:=BsrDWord(z);
  1427. lzn:=BsrDWord(n);
  1428. { if the denominator contains less zeros
  1429. then the numerator
  1430. then d is greater than the n }
  1431. if lzn>lzz then
  1432. begin
  1433. result:=z;
  1434. exit;
  1435. end;
  1436. shift:=lzz-lzn;
  1437. n:=n shl shift;
  1438. for shift:=shift downto 0 do
  1439. begin
  1440. if z>=n then
  1441. z:=z-n;
  1442. n:=n shr 1;
  1443. end;
  1444. result:=z;
  1445. end;
  1446. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1447. {$ifndef FPC_SYSTEM_HAS_DIV_WORD}
  1448. function fpc_div_word(n,z : word) : word; [public,alias: 'FPC_DIV_WORD']; compilerproc;
  1449. var
  1450. shift,lzz,lzn : Byte;
  1451. begin
  1452. result:=0;
  1453. if n=0 then
  1454. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1455. if z=0 then
  1456. exit;
  1457. lzz:=BsrWord(z);
  1458. lzn:=BsrWord(n);
  1459. { if the denominator contains less zeros
  1460. then the numerator
  1461. then d is greater than the n }
  1462. if lzn>lzz then
  1463. exit;
  1464. shift:=lzz-lzn;
  1465. n:=n shl shift;
  1466. for shift:=shift downto 0 do
  1467. begin
  1468. if z>=n then
  1469. begin
  1470. z:=z-n;
  1471. result:=result+word(word(1) shl shift);
  1472. end;
  1473. n:=n shr 1;
  1474. end;
  1475. end;
  1476. {$endif FPC_SYSTEM_HAS_DIV_WORD}
  1477. {$ifndef FPC_SYSTEM_HAS_MOD_WORD}
  1478. function fpc_mod_word(n,z : word) : word; [public,alias: 'FPC_MOD_WORD']; compilerproc;
  1479. var
  1480. shift,lzz,lzn : Byte;
  1481. begin
  1482. result:=0;
  1483. if n=0 then
  1484. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1485. if z=0 then
  1486. exit;
  1487. lzz:=BsrWord(z);
  1488. lzn:=BsrWord(n);
  1489. { if the denominator contains less zeros
  1490. then the numerator
  1491. then d is greater than the n }
  1492. if lzn>lzz then
  1493. begin
  1494. result:=z;
  1495. exit;
  1496. end;
  1497. shift:=lzz-lzn;
  1498. n:=n shl shift;
  1499. for shift:=shift downto 0 do
  1500. begin
  1501. if z>=n then
  1502. z:=z-n;
  1503. n:=n shr 1;
  1504. end;
  1505. result:=z;
  1506. end;
  1507. {$endif FPC_SYSTEM_HAS_MOD_WORD}
  1508. {$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
  1509. function fpc_div_byte(n,z : byte) : byte; [public,alias: 'FPC_DIV_BYTE']; compilerproc;
  1510. var
  1511. shift,lzz,lzn : Byte;
  1512. begin
  1513. result:=0;
  1514. if n=0 then
  1515. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1516. if z=0 then
  1517. exit;
  1518. lzz:=BsrByte(z);
  1519. lzn:=BsrByte(n);
  1520. { if the denominator contains less zeros
  1521. then the numerator
  1522. then d is greater than the n }
  1523. if lzn>lzz then
  1524. exit;
  1525. shift:=lzz-lzn;
  1526. n:=n shl shift;
  1527. for shift:=shift downto 0 do
  1528. begin
  1529. if z>=n then
  1530. begin
  1531. z:=z-n;
  1532. result:=result+byte(byte(1) shl shift);
  1533. end;
  1534. n:=n shr 1;
  1535. end;
  1536. end;
  1537. {$endif FPC_SYSTEM_HAS_DIV_BYTE}
  1538. {$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
  1539. function fpc_mod_byte(n,z : byte) : byte; [public,alias: 'FPC_MOD_BYTE']; compilerproc;
  1540. var
  1541. shift,lzz,lzn : Byte;
  1542. begin
  1543. result:=0;
  1544. if n=0 then
  1545. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1546. if z=0 then
  1547. exit;
  1548. lzz:=BsrByte(z);
  1549. lzn:=BsrByte(n);
  1550. { if the denominator contains less zeros
  1551. then the numerator
  1552. then d is greater than the n }
  1553. if lzn>lzz then
  1554. begin
  1555. result:=z;
  1556. exit;
  1557. end;
  1558. shift:=lzz-lzn;
  1559. n:=n shl shift;
  1560. for shift:=shift downto 0 do
  1561. begin
  1562. if z>=n then
  1563. z:=z-n;
  1564. n:=n shr 1;
  1565. end;
  1566. result:=z;
  1567. end;
  1568. {$endif FPC_SYSTEM_HAS_MOD_BYTE}
  1569. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1570. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1571. var
  1572. sign : boolean;
  1573. d1,d2 : dword;
  1574. begin
  1575. if n=0 then
  1576. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1577. sign:=false;
  1578. if z<0 then
  1579. begin
  1580. sign:=not(sign);
  1581. d1:=dword(-z);
  1582. end
  1583. else
  1584. d1:=z;
  1585. if n<0 then
  1586. begin
  1587. sign:=not(sign);
  1588. d2:=dword(-n);
  1589. end
  1590. else
  1591. d2:=n;
  1592. { the div is coded by the compiler as call to divdword }
  1593. if sign then
  1594. result:=-(d1 div d2)
  1595. else
  1596. result:=d1 div d2;
  1597. end;
  1598. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1599. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1600. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1601. var
  1602. signed : boolean;
  1603. r,nq,zq : dword;
  1604. begin
  1605. if n=0 then
  1606. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1607. nq:=abs(n);
  1608. if z<0 then
  1609. begin
  1610. zq:=dword(-z);
  1611. signed:=true;
  1612. end
  1613. else
  1614. begin
  1615. zq:=z;
  1616. signed:=false;
  1617. end;
  1618. r:=zq mod nq;
  1619. if signed then
  1620. result:=-longint(r)
  1621. else
  1622. result:=r;
  1623. end;
  1624. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1625. {$ifndef FPC_SYSTEM_HAS_DIV_SMALLINT}
  1626. function fpc_div_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_DIV_SMALLINT']; compilerproc;
  1627. var
  1628. sign : boolean;
  1629. w1,w2 : word;
  1630. begin
  1631. if n=0 then
  1632. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1633. sign:=false;
  1634. if z<0 then
  1635. begin
  1636. sign:=not(sign);
  1637. w1:=word(-z);
  1638. end
  1639. else
  1640. w1:=z;
  1641. if n<0 then
  1642. begin
  1643. sign:=not(sign);
  1644. w2:=word(-n);
  1645. end
  1646. else
  1647. w2:=n;
  1648. { the div is coded by the compiler as call to divdword }
  1649. if sign then
  1650. result:=-(w1 div w2)
  1651. else
  1652. result:=w1 div w2;
  1653. end;
  1654. {$endif FPC_SYSTEM_HAS_DIV_SMALLINT}
  1655. {$ifndef FPC_SYSTEM_HAS_MOD_SMALLINT}
  1656. function fpc_mod_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_MOD_SMALLINT']; compilerproc;
  1657. var
  1658. signed : boolean;
  1659. r,nq,zq : word;
  1660. begin
  1661. if n=0 then
  1662. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1663. nq:=abs(n);
  1664. if z<0 then
  1665. begin
  1666. zq:=word(-z);
  1667. signed:=true;
  1668. end
  1669. else
  1670. begin
  1671. zq:=z;
  1672. signed:=false;
  1673. end;
  1674. r:=zq mod nq;
  1675. if signed then
  1676. result:=-smallint(r)
  1677. else
  1678. result:=r;
  1679. end;
  1680. {$endif FPC_SYSTEM_HAS_MOD_SMALLINT}
  1681. {$ifndef FPC_SYSTEM_HAS_DIV_SHORTINT}
  1682. function fpc_div_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_DIV_SHORTINT']; compilerproc;
  1683. var
  1684. sign : boolean;
  1685. b1,b2 : byte;
  1686. begin
  1687. if n=0 then
  1688. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1689. sign:=false;
  1690. if z<0 then
  1691. begin
  1692. sign:=not(sign);
  1693. b1:=byte(-z);
  1694. end
  1695. else
  1696. b1:=z;
  1697. if n<0 then
  1698. begin
  1699. sign:=not(sign);
  1700. b2:=byte(-n);
  1701. end
  1702. else
  1703. b2:=n;
  1704. { the div is coded by the compiler as call to divdword }
  1705. if sign then
  1706. result:=-(b1 div b2)
  1707. else
  1708. result:=b1 div b2;
  1709. end;
  1710. {$endif FPC_SYSTEM_HAS_DIV_SHORTINT}
  1711. {$ifndef FPC_SYSTEM_HAS_MOD_SHORTINT}
  1712. function fpc_mod_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_MOD_SHORTINT']; compilerproc;
  1713. var
  1714. signed : boolean;
  1715. r,nq,zq : byte;
  1716. begin
  1717. if n=0 then
  1718. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1719. nq:=abs(n);
  1720. if z<0 then
  1721. begin
  1722. zq:=byte(-z);
  1723. signed:=true;
  1724. end
  1725. else
  1726. begin
  1727. zq:=z;
  1728. signed:=false;
  1729. end;
  1730. r:=zq mod nq;
  1731. if signed then
  1732. result:=-shortint(r)
  1733. else
  1734. result:=r;
  1735. end;
  1736. {$endif FPC_SYSTEM_HAS_MOD_SHORTINT}
  1737. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1738. {****************************************************************************}
  1739. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1740. { This is only needed to bootstrap on SPARC targets
  1741. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  1742. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1743. begin
  1744. if l<0 then
  1745. abs:=-l
  1746. else
  1747. abs:=l;
  1748. end;
  1749. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1750. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1751. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1752. begin
  1753. odd:=boolean(l and 1);
  1754. end;
  1755. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1756. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1757. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1758. begin
  1759. odd:=boolean(l and 1);
  1760. end;
  1761. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1762. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1763. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1764. begin
  1765. odd:=boolean(longint(l) and 1);
  1766. end;
  1767. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1768. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1769. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1770. begin
  1771. odd:=boolean(longint(l) and 1);
  1772. end;
  1773. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1774. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1775. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1776. begin
  1777. sqr:=l*l;
  1778. end;
  1779. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1780. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1781. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1782. begin
  1783. if l < 0 then
  1784. abs := -l
  1785. else
  1786. abs := l;
  1787. end;
  1788. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1789. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1790. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1791. begin
  1792. sqr := l*l;
  1793. end;
  1794. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1795. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1796. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1797. begin
  1798. sqr := l*l;
  1799. end;
  1800. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1801. {$ifdef CPU16}
  1802. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1803. function declocked(var l:smallint):boolean;
  1804. begin
  1805. Dec(l);
  1806. declocked:=(l=0);
  1807. end;
  1808. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1809. {$endif CPU16}
  1810. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1811. function declocked(var l:longint):boolean;
  1812. begin
  1813. Dec(l);
  1814. declocked:=(l=0);
  1815. end;
  1816. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1817. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1818. function declocked(var l:int64):boolean;
  1819. begin
  1820. Dec(l);
  1821. declocked:=(l=0);
  1822. end;
  1823. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1824. {$ifdef CPU16}
  1825. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1826. procedure inclocked(var l:smallint);
  1827. begin
  1828. Inc(l);
  1829. end;
  1830. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1831. {$endif CPU16}
  1832. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1833. procedure inclocked(var l:longint);
  1834. begin
  1835. Inc(l);
  1836. end;
  1837. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1838. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1839. procedure inclocked(var l:int64);
  1840. begin
  1841. Inc(l);
  1842. end;
  1843. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1844. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1845. {_$error Sptr must be defined for each processor }
  1846. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1847. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1848. var
  1849. tmp: PtrUInt;
  1850. begin
  1851. tmp:=addr+(alignment-1);
  1852. result:=tmp-(tmp mod alignment)
  1853. end;
  1854. {$ifndef cpujvm}
  1855. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1856. var
  1857. tmp: PtrUInt;
  1858. begin
  1859. tmp:=PtrUInt(addr)+(alignment-1);
  1860. result:=pointer(tmp-(tmp mod alignment));
  1861. end;
  1862. {$endif}
  1863. {****************************************************************************
  1864. Str()
  1865. ****************************************************************************}
  1866. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1867. procedure int_str(l:longint;out s:shortstring);
  1868. var
  1869. m,m1 : longword;
  1870. pcstart,
  1871. pc2start,
  1872. pc,pc2 : pchar;
  1873. hs : string[32];
  1874. overflow : longint;
  1875. begin
  1876. pc2start:=@s[1];
  1877. pc2:=pc2start;
  1878. if (l<0) then
  1879. begin
  1880. pc2^:='-';
  1881. inc(pc2);
  1882. m:=longword(-l);
  1883. end
  1884. else
  1885. m:=longword(l);
  1886. pcstart:=pchar(@hs[0]);
  1887. pc:=pcstart;
  1888. repeat
  1889. m1:=m div 10;
  1890. inc(pc);
  1891. pc^:=char(m-(m1*10)+byte('0'));
  1892. m:=m1;
  1893. until m=0;
  1894. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1895. if overflow>0 then
  1896. inc(pcstart,overflow);
  1897. while (pc>pcstart) do
  1898. begin
  1899. pc2^:=pc^;
  1900. inc(pc2);
  1901. dec(pc);
  1902. end;
  1903. s[0]:=char(pc2-pc2start);
  1904. end;
  1905. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1906. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1907. procedure int_str_unsigned(l:longword;out s:shortstring);
  1908. var
  1909. m1 : longword;
  1910. pcstart,
  1911. pc2start,
  1912. pc,pc2 : pchar;
  1913. hs : string[32];
  1914. overflow : longint;
  1915. begin
  1916. pc2start:=@s[1];
  1917. pc2:=pc2start;
  1918. pcstart:=pchar(@hs[0]);
  1919. pc:=pcstart;
  1920. repeat
  1921. inc(pc);
  1922. m1:=l div 10;
  1923. pc^:=char(l-(m1*10)+byte('0'));
  1924. l:=m1;
  1925. until l=0;
  1926. overflow:=(pc-pcstart)-high(s);
  1927. if overflow>0 then
  1928. inc(pcstart,overflow);
  1929. while (pc>pcstart) do
  1930. begin
  1931. pc2^:=pc^;
  1932. inc(pc2);
  1933. dec(pc);
  1934. end;
  1935. s[0]:=char(pc2-pc2start);
  1936. end;
  1937. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1938. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1939. procedure int_str(l:int64;out s:shortstring);
  1940. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1941. begin
  1942. runerror(217);
  1943. end;
  1944. {$else EXCLUDE_COMPLEX_PROCS}
  1945. var
  1946. m,m1 : qword;
  1947. pcstart,
  1948. pc2start,
  1949. pc,pc2 : pchar;
  1950. hs : string[32];
  1951. overflow : longint;
  1952. begin
  1953. pc2start:=@s[1];
  1954. pc2:=pc2start;
  1955. if (l<0) then
  1956. begin
  1957. pc2^:='-';
  1958. inc(pc2);
  1959. m:=qword(-l);
  1960. end
  1961. else
  1962. m:=qword(l);
  1963. pcstart:=pchar(@hs[0]);
  1964. pc:=pcstart;
  1965. repeat
  1966. m1:=m div 10;
  1967. inc(pc);
  1968. pc^:=char(m-(m1*10)+byte('0'));
  1969. m:=m1;
  1970. until m=0;
  1971. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1972. if overflow>0 then
  1973. inc(pcstart,overflow);
  1974. while (pc>pcstart) do
  1975. begin
  1976. pc2^:=pc^;
  1977. inc(pc2);
  1978. dec(pc);
  1979. end;
  1980. s[0]:=char(pc2-pc2start);
  1981. end;
  1982. {$endif EXCLUDE_COMPLEX_PROCS}
  1983. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1984. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1985. procedure int_str_unsigned(l:qword;out s:shortstring);
  1986. {$ifdef EXCLUDE_COMPLEX_PROCS}
  1987. begin
  1988. runerror(217);
  1989. end;
  1990. {$else EXCLUDE_COMPLEX_PROCS}
  1991. var
  1992. m1 : qword;
  1993. pcstart,
  1994. pc2start,
  1995. pc,pc2 : pchar;
  1996. hs : string[64];
  1997. overflow : longint;
  1998. begin
  1999. pc2start:=@s[1];
  2000. pc2:=pc2start;
  2001. pcstart:=pchar(@hs[0]);
  2002. pc:=pcstart;
  2003. repeat
  2004. inc(pc);
  2005. m1:=l div 10;
  2006. pc^:=char(l-(m1*10)+byte('0'));
  2007. l:=m1;
  2008. until l=0;
  2009. overflow:=(pc-pcstart)-high(s);
  2010. if overflow>0 then
  2011. inc(pcstart,overflow);
  2012. while (pc>pcstart) do
  2013. begin
  2014. pc2^:=pc^;
  2015. inc(pc2);
  2016. dec(pc);
  2017. end;
  2018. s[0]:=char(pc2-pc2start);
  2019. end;
  2020. {$endif EXCLUDE_COMPLEX_PROCS}
  2021. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2022. {$ifndef FPUNONE}
  2023. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  2024. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2025. begin
  2026. softfloat_exception_flags:=[];
  2027. end;
  2028. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  2029. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  2030. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2031. begin
  2032. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  2033. end;
  2034. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  2035. {$endif}
  2036. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  2037. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2038. begin
  2039. { the extra Word type cast is necessary because the "AValue shr 8" }
  2040. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  2041. { the sign bits from the upper 16 bits are shifted in rather than }
  2042. { zeroes. }
  2043. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  2044. end;
  2045. {$ifndef cpujvm}
  2046. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2047. begin
  2048. Result := Word((AValue shr 8) or (AValue shl 8));
  2049. end;
  2050. {$endif}
  2051. function SwapEndian(const AValue: LongInt): LongInt;
  2052. begin
  2053. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2054. Result := (Result shl 16) or (Result shr 16);
  2055. end;
  2056. {$ifndef cpujvm}
  2057. function SwapEndian(const AValue: DWord): DWord;
  2058. begin
  2059. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2060. Result := (Result shl 16) or (Result shr 16);
  2061. end;
  2062. {$endif}
  2063. function SwapEndian(const AValue: Int64): Int64;
  2064. begin
  2065. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2066. ((AValue shr 8) and $00FF00FF00FF00FF);
  2067. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2068. ((Result shr 16) and $0000FFFF0000FFFF);
  2069. Result := (Result shl 32) or ((Result shr 32));
  2070. end;
  2071. {$ifndef cpujvm}
  2072. function SwapEndian(const AValue: QWord): QWord;
  2073. begin
  2074. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2075. ((AValue shr 8) and $00FF00FF00FF00FF);
  2076. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2077. ((Result shr 16) and $0000FFFF0000FFFF);
  2078. Result := (Result shl 32) or ((Result shr 32));
  2079. end;
  2080. {$endif}
  2081. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  2082. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2083. begin
  2084. {$IFDEF ENDIAN_BIG}
  2085. Result := AValue;
  2086. {$ELSE}
  2087. Result := SwapEndian(AValue);
  2088. {$ENDIF}
  2089. end;
  2090. {$ifndef cpujvm}
  2091. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2092. begin
  2093. {$IFDEF ENDIAN_BIG}
  2094. Result := AValue;
  2095. {$ELSE}
  2096. Result := SwapEndian(AValue);
  2097. {$ENDIF}
  2098. end;
  2099. {$endif not cpujvm}
  2100. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2101. begin
  2102. {$IFDEF ENDIAN_BIG}
  2103. Result := AValue;
  2104. {$ELSE}
  2105. Result := SwapEndian(AValue);
  2106. {$ENDIF}
  2107. end;
  2108. {$ifndef cpujvm}
  2109. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2110. begin
  2111. {$IFDEF ENDIAN_BIG}
  2112. Result := AValue;
  2113. {$ELSE}
  2114. Result := SwapEndian(AValue);
  2115. {$ENDIF}
  2116. end;
  2117. {$endif not cpujvm}
  2118. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2119. begin
  2120. {$IFDEF ENDIAN_BIG}
  2121. Result := AValue;
  2122. {$ELSE}
  2123. Result := SwapEndian(AValue);
  2124. {$ENDIF}
  2125. end;
  2126. {$ifndef cpujvm}
  2127. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2128. begin
  2129. {$IFDEF ENDIAN_BIG}
  2130. Result := AValue;
  2131. {$ELSE}
  2132. Result := SwapEndian(AValue);
  2133. {$ENDIF}
  2134. end;
  2135. {$endif not cpujvm}
  2136. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2137. begin
  2138. {$IFDEF ENDIAN_LITTLE}
  2139. Result := AValue;
  2140. {$ELSE}
  2141. Result := SwapEndian(AValue);
  2142. {$ENDIF}
  2143. end;
  2144. {$ifndef cpujvm}
  2145. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2146. begin
  2147. {$IFDEF ENDIAN_LITTLE}
  2148. Result := AValue;
  2149. {$ELSE}
  2150. Result := SwapEndian(AValue);
  2151. {$ENDIF}
  2152. end;
  2153. {$endif not cpujvm}
  2154. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2155. begin
  2156. {$IFDEF ENDIAN_LITTLE}
  2157. Result := AValue;
  2158. {$ELSE}
  2159. Result := SwapEndian(AValue);
  2160. {$ENDIF}
  2161. end;
  2162. {$ifndef cpujvm}
  2163. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2164. begin
  2165. {$IFDEF ENDIAN_LITTLE}
  2166. Result := AValue;
  2167. {$ELSE}
  2168. Result := SwapEndian(AValue);
  2169. {$ENDIF}
  2170. end;
  2171. {$endif not cpujvm}
  2172. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2173. begin
  2174. {$IFDEF ENDIAN_LITTLE}
  2175. Result := AValue;
  2176. {$ELSE}
  2177. Result := SwapEndian(AValue);
  2178. {$ENDIF}
  2179. end;
  2180. {$ifndef cpujvm}
  2181. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2182. begin
  2183. {$IFDEF ENDIAN_LITTLE}
  2184. Result := AValue;
  2185. {$ELSE}
  2186. Result := SwapEndian(AValue);
  2187. {$ENDIF}
  2188. end;
  2189. {$endif not cpujvm}
  2190. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2191. begin
  2192. {$IFDEF ENDIAN_BIG}
  2193. Result := AValue;
  2194. {$ELSE}
  2195. Result := SwapEndian(AValue);
  2196. {$ENDIF}
  2197. end;
  2198. {$ifndef cpujvm}
  2199. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2200. begin
  2201. {$IFDEF ENDIAN_BIG}
  2202. Result := AValue;
  2203. {$ELSE}
  2204. Result := SwapEndian(AValue);
  2205. {$ENDIF}
  2206. end;
  2207. {$endif not cpujvm}
  2208. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2209. begin
  2210. {$IFDEF ENDIAN_BIG}
  2211. Result := AValue;
  2212. {$ELSE}
  2213. Result := SwapEndian(AValue);
  2214. {$ENDIF}
  2215. end;
  2216. {$ifndef cpujvm}
  2217. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2218. begin
  2219. {$IFDEF ENDIAN_BIG}
  2220. Result := AValue;
  2221. {$ELSE}
  2222. Result := SwapEndian(AValue);
  2223. {$ENDIF}
  2224. end;
  2225. {$endif not cpujvm}
  2226. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2227. begin
  2228. {$IFDEF ENDIAN_BIG}
  2229. Result := AValue;
  2230. {$ELSE}
  2231. Result := SwapEndian(AValue);
  2232. {$ENDIF}
  2233. end;
  2234. {$ifndef cpujvm}
  2235. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2236. begin
  2237. {$IFDEF ENDIAN_BIG}
  2238. Result := AValue;
  2239. {$ELSE}
  2240. Result := SwapEndian(AValue);
  2241. {$ENDIF}
  2242. end;
  2243. {$endif not cpujvm}
  2244. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2245. begin
  2246. {$IFDEF ENDIAN_LITTLE}
  2247. Result := AValue;
  2248. {$ELSE}
  2249. Result := SwapEndian(AValue);
  2250. {$ENDIF}
  2251. end;
  2252. {$ifndef cpujvm}
  2253. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2254. begin
  2255. {$IFDEF ENDIAN_LITTLE}
  2256. Result := AValue;
  2257. {$ELSE}
  2258. Result := SwapEndian(AValue);
  2259. {$ENDIF}
  2260. end;
  2261. {$endif not cpujvm}
  2262. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2263. begin
  2264. {$IFDEF ENDIAN_LITTLE}
  2265. Result := AValue;
  2266. {$ELSE}
  2267. Result := SwapEndian(AValue);
  2268. {$ENDIF}
  2269. end;
  2270. {$ifndef cpujvm}
  2271. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2272. begin
  2273. {$IFDEF ENDIAN_LITTLE}
  2274. Result := AValue;
  2275. {$ELSE}
  2276. Result := SwapEndian(AValue);
  2277. {$ENDIF}
  2278. end;
  2279. {$endif not cpujvm}
  2280. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2281. begin
  2282. {$IFDEF ENDIAN_LITTLE}
  2283. Result := AValue;
  2284. {$ELSE}
  2285. Result := SwapEndian(AValue);
  2286. {$ENDIF}
  2287. end;
  2288. {$ifndef cpujvm}
  2289. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2290. begin
  2291. {$IFDEF ENDIAN_LITTLE}
  2292. Result := AValue;
  2293. {$ELSE}
  2294. Result := SwapEndian(AValue);
  2295. {$ENDIF}
  2296. end;
  2297. {$endif not cpujvm}
  2298. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2299. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2300. begin
  2301. end;
  2302. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2303. begin
  2304. end;
  2305. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2306. begin
  2307. end;
  2308. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2309. begin
  2310. end;
  2311. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2312. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2313. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2314. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2315. begin
  2316. Result:=(AValue shr 1) or (AValue shl 7);
  2317. end;
  2318. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2319. begin
  2320. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2321. end;
  2322. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2323. begin
  2324. Result:=(AValue shl 1) or (AValue shr 7);
  2325. end;
  2326. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2327. begin
  2328. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2329. end;
  2330. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2331. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2332. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2333. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2334. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2335. begin
  2336. Result:=(AValue shr 1) or (AValue shl 15);
  2337. end;
  2338. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2339. begin
  2340. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2341. end;
  2342. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2343. begin
  2344. Result:=(AValue shl 1) or (AValue shr 15);
  2345. end;
  2346. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2347. begin
  2348. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2349. end;
  2350. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2351. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2352. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2353. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2354. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2355. begin
  2356. Result:=(AValue shr 1) or (AValue shl 31);
  2357. end;
  2358. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2359. begin
  2360. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2361. end;
  2362. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2363. begin
  2364. Result:=(AValue shl 1) or (AValue shr 31);
  2365. end;
  2366. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2367. begin
  2368. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2369. end;
  2370. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2371. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2372. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2373. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2374. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2375. begin
  2376. Result:=(AValue shr 1) or (AValue shl 63);
  2377. end;
  2378. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2379. begin
  2380. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2381. end;
  2382. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2383. begin
  2384. Result:=(AValue shl 1) or (AValue shr 63);
  2385. end;
  2386. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2387. begin
  2388. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2389. end;
  2390. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2391. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2392. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2393. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2394. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2395. begin
  2396. 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)))));
  2397. end;
  2398. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2399. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2400. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2401. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2402. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2403. begin
  2404. 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)))));
  2405. end;
  2406. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2407. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2408. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2409. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2410. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2411. begin
  2412. 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)))));
  2413. end;
  2414. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2415. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2416. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2417. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2418. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2419. begin
  2420. 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)))));
  2421. end;
  2422. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2423. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2424. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2425. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2426. function BsfByte(Const AValue: Byte): Byte;
  2427. const bsf8bit: array [Byte] of Byte = (
  2428. $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,
  2429. 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,
  2430. 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,
  2431. 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,
  2432. 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,
  2433. 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,
  2434. 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,
  2435. 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
  2436. );
  2437. begin
  2438. result:=bsf8bit[AValue];
  2439. end;
  2440. {$endif}
  2441. {$endif}
  2442. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2443. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2444. function BsrByte(Const AValue: Byte): Byte;
  2445. const bsr8bit: array [Byte] of Byte = (
  2446. $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,
  2447. 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,
  2448. 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,
  2449. 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,
  2450. 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,
  2451. 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,
  2452. 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,
  2453. 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
  2454. );
  2455. begin
  2456. result:=bsr8bit[AValue];
  2457. end;
  2458. {$endif}
  2459. {$endif}
  2460. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2461. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2462. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2463. begin
  2464. result:=ord(lo(AValue)=0)*8;
  2465. result:=result or BsfByte(byte(AValue shr result));
  2466. end;
  2467. {$endif}
  2468. {$endif}
  2469. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2470. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2471. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2472. begin
  2473. result:=ord(AValue>255)*8;
  2474. result:=result or BsrByte(byte(AValue shr result));
  2475. end;
  2476. {$endif}
  2477. {$endif}
  2478. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2479. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2480. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2481. var
  2482. tmp: DWord;
  2483. begin
  2484. result:=ord(lo(AValue)=0)*16;
  2485. tmp:=AValue shr result;
  2486. result:=result or (ord((tmp and $FF)=0)*8);
  2487. tmp:=tmp shr (result and 8);
  2488. result:=result or BsfByte(byte(tmp));
  2489. end;
  2490. {$endif}
  2491. {$endif}
  2492. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2493. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2494. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2495. var
  2496. tmp: DWord;
  2497. begin
  2498. result:=ord(AValue>$FFFF)*16;
  2499. tmp:=AValue shr result;
  2500. result:=result or (ord(tmp>$FF)*8);
  2501. tmp:=tmp shr (result and 8);
  2502. result:=result or BsrByte(byte(tmp));
  2503. end;
  2504. {$endif}
  2505. {$endif}
  2506. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2507. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2508. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2509. var
  2510. tmp: DWord;
  2511. begin
  2512. result:=0;
  2513. tmp:=lo(AValue);
  2514. if (tmp=0) then
  2515. begin
  2516. tmp:=hi(AValue);
  2517. result:=32;
  2518. end;
  2519. result:=result or BsfDword(tmp);
  2520. end;
  2521. {$endif}
  2522. {$endif}
  2523. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2524. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2525. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2526. var
  2527. tmp: DWord;
  2528. begin
  2529. result:=32;
  2530. tmp:=hi(AValue);
  2531. if (tmp=0) then
  2532. begin
  2533. tmp:=lo(AValue);
  2534. result:=0;
  2535. end;
  2536. result:=result or BsrDword(tmp);
  2537. end;
  2538. {$endif}
  2539. {$endif}
  2540. const
  2541. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2542. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2543. begin
  2544. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2545. end;
  2546. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2547. var
  2548. i : SizeInt;
  2549. begin
  2550. Result:=0;
  2551. for i:=0 to 3 do
  2552. begin
  2553. inc(Result,PopCntData[AValue and $f]);
  2554. AValue:=AValue shr 4;
  2555. end;
  2556. end;
  2557. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2558. var
  2559. i : SizeInt;
  2560. begin
  2561. Result:=0;
  2562. for i:=0 to 7 do
  2563. begin
  2564. inc(Result,PopCntData[AValue and $f]);
  2565. AValue:=AValue shr 4;
  2566. end;
  2567. end;
  2568. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2569. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2570. {$ifdef VER2_6}
  2571. var
  2572. i : SizeInt;
  2573. begin
  2574. Result:=0;
  2575. for i:=0 to 15 do
  2576. begin
  2577. inc(Result,PopCntData[AValue and $f]);
  2578. AValue:=AValue shr 4;
  2579. end;
  2580. {$else VER2_6}
  2581. begin
  2582. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2583. {$endif VER2_6}
  2584. end;
  2585. {$endif}