generic.inc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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_SYSTEM_HAS_MOVE}
  18. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  19. var
  20. aligncount : sizeint;
  21. pdest,psrc,pend : pbyte;
  22. begin
  23. if (@dest=@source) or (count<=0) then
  24. exit;
  25. if (@dest<@source) or (@source+count<@dest) then
  26. begin
  27. { Forward Move }
  28. psrc:=@source;
  29. pdest:=@dest;
  30. if (Count>4*sizeof(ptruint)-11)
  31. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  32. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  33. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  34. then
  35. begin
  36. { Align on native pointer size }
  37. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  38. dec(count,aligncount);
  39. pend:=psrc+aligncount;
  40. while psrc<pend do
  41. begin
  42. pdest^:=psrc^;
  43. inc(pdest);
  44. inc(psrc);
  45. end;
  46. { use sizeuint typecast to force shr optimization }
  47. pptruint(pend):=pptruint(psrc)+(sizeuint(count) div sizeof(ptruint));
  48. while psrc<pend do
  49. begin
  50. pptruint(pdest)^:=pptruint(psrc)^;
  51. inc(pptruint(pdest));
  52. inc(pptruint(psrc));
  53. end;
  54. count:=count and (sizeof(PtrUInt)-1);
  55. end;
  56. pend:=psrc+count;
  57. while psrc<pend do
  58. begin
  59. pdest^:=psrc^;
  60. inc(pdest);
  61. inc(psrc);
  62. end;
  63. end
  64. else
  65. begin
  66. { Backward Move }
  67. psrc:=@source+count;
  68. pdest:=@dest+count;
  69. if (Count>4*sizeof(ptruint)-11)
  70. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  71. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  72. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  73. then
  74. begin
  75. { Align on native pointer size }
  76. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  77. dec(count,aligncount);
  78. pend:=psrc-aligncount;
  79. while psrc>pend do
  80. begin
  81. dec(pdest);
  82. dec(psrc);
  83. pdest^:=psrc^;
  84. end;
  85. { use sizeuint typecast to force shr optimization }
  86. pptruint(pend):=pptruint(psrc)-(sizeuint(count) div sizeof(ptruint));
  87. while psrc>pend do
  88. begin
  89. dec(pptruint(pdest));
  90. dec(pptruint(psrc));
  91. pptruint(pdest)^:=pptruint(psrc)^;
  92. end;
  93. count:=count and (sizeof(PtrUInt)-1);
  94. end;
  95. pend:=psrc-count;
  96. while psrc>pend do
  97. begin
  98. dec(pdest);
  99. dec(psrc);
  100. pdest^:=psrc^;
  101. end;
  102. end;
  103. end;
  104. {$endif not FPC_SYSTEM_HAS_MOVE}
  105. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  106. Procedure FillChar(var x;count:SizeInt;value:byte);
  107. var
  108. pdest,pend : pbyte;
  109. v : ptruint;
  110. begin
  111. if count <= 0 then
  112. exit;
  113. pdest:=@x;
  114. if Count>4*sizeof(ptruint)-1 then
  115. begin
  116. v:=(value shl 8) or value;
  117. v:=(v shl 16) or v;
  118. {$ifdef CPU64}
  119. v:=(v shl 32) or v;
  120. {$endif CPU64}
  121. { Align on native pointer size }
  122. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  123. dec(count,pend-pdest);
  124. while pdest<pend do
  125. begin
  126. pdest^:=value;
  127. inc(pdest);
  128. end;
  129. { use sizeuint typecast to force shr optimization }
  130. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  131. while pdest<pend do
  132. begin
  133. pptruint(pdest)^:=v;
  134. inc(pptruint(pdest));
  135. end;
  136. count:=count and (sizeof(ptruint)-1);
  137. end;
  138. pend:=pdest+count;
  139. while pdest<pend do
  140. begin
  141. pdest^:=value;
  142. inc(pdest);
  143. end;
  144. end;
  145. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  146. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  147. procedure fillword(var x;count : SizeInt;value : word);
  148. var
  149. aligncount : sizeint;
  150. pdest,pend : pword;
  151. v : ptruint;
  152. begin
  153. if count <= 0 then
  154. exit;
  155. pdest:=@x;
  156. if Count>4*sizeof(ptruint)-1 then
  157. begin
  158. v:=(value shl 16) or value;
  159. {$ifdef CPU64}
  160. v:=(v shl 32) or v;
  161. {$endif CPU64}
  162. { Align on native pointer size }
  163. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  164. dec(count,aligncount);
  165. pend:=pdest+aligncount;
  166. while pdest<pend do
  167. begin
  168. pdest^:=value;
  169. inc(pdest);
  170. end;
  171. { use sizeuint typecast to force shr optimization }
  172. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  173. while pdest<pend do
  174. begin
  175. pptruint(pdest)^:=v;
  176. inc(pptruint(pdest));
  177. end;
  178. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  179. end;
  180. pend:=pdest+count;
  181. while pdest<pend do
  182. begin
  183. pdest^:=value;
  184. inc(pdest);
  185. end;
  186. end;
  187. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  188. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  189. procedure filldword(var x;count : SizeInt;value : dword);
  190. var
  191. aligncount : sizeint;
  192. pdest,pend : pdword;
  193. v : ptruint;
  194. begin
  195. if count <= 0 then
  196. exit;
  197. pdest:=@x;
  198. if Count>4*sizeof(ptruint)-1 then
  199. begin
  200. v:=value;
  201. {$ifdef CPU64}
  202. v:=(v shl 32) or v;
  203. {$endif CPU64}
  204. { Align on native pointer size }
  205. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  206. dec(count,aligncount);
  207. pend:=pdest+aligncount;
  208. while pdest<pend do
  209. begin
  210. pdest^:=value;
  211. inc(pdest);
  212. end;
  213. { use sizeuint typecast to force shr optimization }
  214. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  215. while pdest<pend do
  216. begin
  217. pptruint(pdest)^:=v;
  218. inc(pptruint(pdest));
  219. end;
  220. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  221. end;
  222. pend:=pdest+count;
  223. while pdest<pend do
  224. begin
  225. pdest^:=value;
  226. inc(pdest);
  227. end;
  228. end;
  229. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  230. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  231. procedure fillqword(var x;count : SizeInt;value : qword);
  232. var
  233. pdest,pend : pqword;
  234. begin
  235. if count <= 0 then
  236. exit;
  237. pdest:=@x;
  238. pend:=pdest+count;
  239. while pdest<pend do
  240. begin
  241. pdest^:=value;
  242. inc(pdest);
  243. end;
  244. end;
  245. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  246. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  247. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  248. var
  249. psrc,pend : pbyte;
  250. begin
  251. psrc:=@buf;
  252. { simulate assembler implementations behaviour, which is expected }
  253. { fpc_pchar_to_ansistr in astrings.inc }
  254. if (len < 0) or
  255. (psrc+len < psrc) then
  256. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  257. else
  258. pend:=psrc+len;
  259. while (psrc<pend) do
  260. begin
  261. if psrc^=b then
  262. begin
  263. result:=psrc-pbyte(@buf);
  264. exit;
  265. end;
  266. inc(psrc);
  267. end;
  268. result:=-1;
  269. end;
  270. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  271. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  272. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  273. var
  274. psrc,pend : pword;
  275. begin
  276. psrc:=@buf;
  277. { simulate assembler implementations behaviour, which is expected }
  278. { fpc_pchar_to_ansistr in astrings.inc }
  279. if (len < 0) or
  280. { is this ever true? }
  281. (len > high(PtrInt)) or
  282. (psrc+len < psrc) then
  283. pend:=pword(high(PtrUInt)-sizeof(word))
  284. else
  285. pend:=psrc+len;
  286. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  287. if (ptruint(psrc) mod 2)<>0 then
  288. while psrc<pend do
  289. begin
  290. if unaligned(psrc^)=b then
  291. begin
  292. result:=psrc-pword(@buf);
  293. exit;
  294. end;
  295. inc(psrc);
  296. end
  297. else
  298. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  299. while psrc<pend do
  300. begin
  301. if psrc^=b then
  302. begin
  303. result:=psrc-pword(@buf);
  304. exit;
  305. end;
  306. inc(psrc);
  307. end;
  308. result:=-1;
  309. end;
  310. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  311. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  312. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  313. var
  314. psrc,pend : pdword;
  315. begin
  316. psrc:=@buf;
  317. { simulate assembler implementations behaviour, which is expected }
  318. { fpc_pchar_to_ansistr in astrings.inc }
  319. if (len < 0) or
  320. (len > high(PtrInt) div 2) or
  321. (psrc+len < psrc) then
  322. pend:=pdword(high(PtrUInt)-sizeof(dword))
  323. else
  324. pend:=psrc+len;
  325. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  326. if (ptruint(psrc) mod 4)<>0 then
  327. while psrc<pend do
  328. begin
  329. if unaligned(psrc^)=b then
  330. begin
  331. result:=psrc-pdword(@buf);
  332. exit;
  333. end;
  334. inc(psrc);
  335. end
  336. else
  337. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  338. while psrc<pend do
  339. begin
  340. if psrc^=b then
  341. begin
  342. result:=psrc-pdword(@buf);
  343. exit;
  344. end;
  345. inc(psrc);
  346. end;
  347. result:=-1;
  348. end;
  349. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  350. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  351. function IndexQWord(Const buf;len:SizeInt;b:QWord):SizeInt;
  352. var
  353. psrc,pend : pqword;
  354. begin
  355. psrc:=@buf;
  356. { simulate assembler implementations behaviour, which is expected }
  357. { fpc_pchar_to_ansistr in astrings.inc }
  358. if (len < 0) or
  359. (len > high(PtrInt) div 4) or
  360. (psrc+len < psrc) then
  361. pend:=pqword(high(PtrUInt)-sizeof(qword))
  362. else
  363. pend:=psrc+len;
  364. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  365. if (ptruint(psrc) mod 8)<>0 then
  366. while psrc<pend do
  367. begin
  368. if unaligned(psrc^)=b then
  369. begin
  370. result:=psrc-pqword(@buf);
  371. exit;
  372. end;
  373. inc(psrc);
  374. end
  375. else
  376. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  377. while psrc<pend do
  378. begin
  379. if psrc^=b then
  380. begin
  381. result:=psrc-pqword(@buf);
  382. exit;
  383. end;
  384. inc(psrc);
  385. end;
  386. result:=-1;
  387. end;
  388. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  389. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  390. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  391. var
  392. aligncount : sizeint;
  393. psrc,pdest,pend : pbyte;
  394. b : ptrint;
  395. begin
  396. b:=0;
  397. psrc:=@buf1;
  398. pdest:=@buf2;
  399. if (len>4*sizeof(ptruint)-1)
  400. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  401. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  402. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  403. then
  404. begin
  405. { Align on native pointer size }
  406. aligncount:=(sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1);
  407. dec(len,aligncount);
  408. pend:=psrc+aligncount;
  409. while psrc<pend do
  410. begin
  411. b:=(ptrint(psrc^)-ptrint(pdest^));
  412. if b<>0 then
  413. begin
  414. if b<0 then
  415. exit(-1)
  416. else
  417. exit(1);
  418. end;
  419. inc(pdest);
  420. inc(psrc);
  421. end;
  422. { use sizeuint typecast to force shr optimization }
  423. pptruint(pend):=pptruint(psrc)+(sizeuint(len) div sizeof(ptruint));
  424. len:=len and (sizeof(PtrUInt)-1);
  425. while psrc<pend do
  426. begin
  427. b:=(pptrint(psrc)^-pptrint(pdest)^);
  428. if b<>0 then
  429. begin
  430. len:=sizeof(ptruint);
  431. break;
  432. end;
  433. inc(pptruint(pdest));
  434. inc(pptruint(psrc));
  435. end;
  436. end;
  437. if (psrc+len >= psrc) then
  438. pend:=psrc+len
  439. else
  440. pend:=pbyte(high(ptruint)-1);
  441. while psrc<pend do
  442. begin
  443. b:=(ptrint(psrc^)-ptrint(pdest^));
  444. if b<>0 then
  445. begin
  446. if b<0 then
  447. exit(-1)
  448. else
  449. exit(1);
  450. end;
  451. inc(pdest);
  452. inc(psrc);
  453. end;
  454. result:=0;
  455. end;
  456. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  457. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  458. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  459. var
  460. aligncount : sizeint;
  461. psrc,pdest,pend : pword;
  462. b : ptrint;
  463. begin
  464. b:=0;
  465. psrc:=@buf1;
  466. pdest:=@buf2;
  467. if (len>4*sizeof(ptruint)-1)
  468. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  469. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  470. and (((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))=0)
  471. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  472. then
  473. begin
  474. { Align on native pointer size }
  475. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 1;
  476. dec(len,aligncount);
  477. pend:=psrc+aligncount;
  478. while psrc<pend do
  479. begin
  480. b:=(ptrint(psrc^)-ptrint(pdest^));
  481. if b<>0 then
  482. begin
  483. if b<0 then
  484. exit(-1)
  485. else
  486. exit(1);
  487. end;
  488. inc(pdest);
  489. inc(psrc);
  490. end;
  491. { use sizeuint typecast to force shr optimization }
  492. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*2 div sizeof(ptruint));
  493. len:=((len*2) and (sizeof(PtrUInt)-1)) shr 1;
  494. while psrc<pend do
  495. begin
  496. b:=(pptrint(psrc)^-pptrint(pdest)^);
  497. if b<>0 then
  498. begin
  499. len:=sizeof(ptruint) shr 1;
  500. break;
  501. end;
  502. inc(pptruint(pdest));
  503. inc(pptruint(psrc));
  504. end;
  505. end;
  506. if (len <= high(ptrint)) and
  507. (psrc+len >= psrc) then
  508. pend:=psrc+len
  509. else
  510. pend:=pword(high(ptruint)-2);
  511. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  512. if ((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))<>0 then
  513. while psrc<pend do
  514. begin
  515. b:=(ptrint(unaligned(psrc^))-ptrint(unaligned(pdest^)));
  516. if b<>0 then
  517. begin
  518. if b<0 then
  519. exit(-1)
  520. else
  521. exit(1);
  522. end;
  523. inc(pdest);
  524. inc(psrc);
  525. end
  526. else
  527. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  528. while psrc<pend do
  529. begin
  530. b:=(ptrint(psrc^)-ptrint(pdest^));
  531. if b<>0 then
  532. begin
  533. if b<0 then
  534. exit(-1)
  535. else
  536. exit(1);
  537. end;
  538. inc(pdest);
  539. inc(psrc);
  540. end;
  541. result:=0;
  542. end;
  543. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  544. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  545. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  546. var
  547. aligncount : sizeint;
  548. psrc,pdest,pend : pdword;
  549. begin
  550. psrc:=@buf1;
  551. pdest:=@buf2;
  552. if (len>4*sizeof(ptruint)-11)
  553. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  554. and (((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))=0)
  555. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  556. then
  557. begin
  558. { Align on native pointer size }
  559. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  560. dec(len,aligncount);
  561. pend:=psrc+aligncount;
  562. while psrc<pend do
  563. begin
  564. if psrc^<>pdest^ then
  565. if psrc^>pdest^ then
  566. exit(1)
  567. else
  568. exit(-1);
  569. inc(pdest);
  570. inc(psrc);
  571. end;
  572. { use sizeuint typecast to force shr optimization }
  573. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  574. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  575. while psrc<pend do
  576. begin
  577. if pptrint(psrc)^<>pptrint(pdest)^ then
  578. begin
  579. len:=sizeof(ptruint) shr 2;
  580. break;
  581. end;
  582. inc(pptruint(pdest));
  583. inc(pptruint(psrc));
  584. end;
  585. end;
  586. if (len <= high(ptrint) div 2) and
  587. (psrc+len >= psrc) then
  588. pend:=psrc+len
  589. else
  590. pend:=pdword(high(ptruint)-4);
  591. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  592. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  593. while psrc<pend do
  594. begin
  595. if unaligned(psrc^)<>unaligned(pdest^) then
  596. if unaligned(psrc^)>unaligned(pdest^) then
  597. exit(1)
  598. else
  599. exit(-1);
  600. inc(pdest);
  601. inc(psrc);
  602. end
  603. else
  604. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  605. while psrc<pend do
  606. begin
  607. if psrc^<>pdest^ then
  608. if psrc^>pdest^ then
  609. exit(1)
  610. else
  611. exit(-1);
  612. inc(pdest);
  613. inc(psrc);
  614. end;
  615. result:=0;
  616. end;
  617. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  618. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  619. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  620. var
  621. I : SizeInt;
  622. begin
  623. if Len = 0 then
  624. exit;
  625. I:=IndexByte(Buf1,Len,0);
  626. if I<>-1 then
  627. Move(Buf1,Buf2,I)
  628. else
  629. Move(Buf1,Buf2,len);
  630. end;
  631. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  632. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  633. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  634. var
  635. psrc,pend : pbyte;
  636. begin
  637. psrc:=@buf;
  638. { simulate assembler implementations behaviour, which is expected }
  639. { fpc_pchar_to_ansistr in astrings.inc }
  640. if (len < 0) then
  641. pend:=pbyte(high(PtrUInt)-sizeof(byte))
  642. else
  643. pend:=psrc+len;
  644. while (psrc<pend) and (psrc^<>0) do
  645. begin
  646. if (psrc^=byte(b)) then
  647. begin
  648. result:=psrc-pbyte(@buf);
  649. exit;
  650. end;
  651. inc(psrc);
  652. end;
  653. result:=-1;
  654. end;
  655. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  656. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  657. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  658. var
  659. psrc,pdest,pend : pbyte;
  660. b : ptrint;
  661. begin
  662. b:=0;
  663. psrc:=@buf1;
  664. pdest:=@buf2;
  665. pend:=psrc+len;
  666. while psrc<pend do
  667. begin
  668. b:=(ptrint(psrc^)-ptrint(pdest^));
  669. if b<0 then
  670. exit(-1)
  671. else if b>0 then
  672. exit(1);
  673. if (psrc^=0) or (pdest^=0) then
  674. exit(0);
  675. inc(pdest);
  676. inc(psrc);
  677. end;
  678. result:=0;
  679. end;
  680. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  681. {****************************************************************************
  682. Object Helpers
  683. ****************************************************************************}
  684. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  685. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  686. { Note: _vmt will be reset to -1 when memory is allocated,
  687. this is needed for fpc_help_fail }
  688. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  689. type
  690. ppointer = ^pointer;
  691. pvmt = ^tvmt;
  692. tvmt=packed record
  693. size,msize:ptruint;
  694. parent:pointer;
  695. end;
  696. var
  697. vmtcopy : pointer;
  698. begin
  699. { Inherited call? }
  700. if _vmt=nil then
  701. begin
  702. fpc_help_constructor:=_self;
  703. exit;
  704. end;
  705. vmtcopy:=_vmt;
  706. if (_self=nil) and
  707. (pvmt(_vmt)^.size>0) then
  708. begin
  709. getmem(_self,pvmt(_vmt)^.size);
  710. { reset vmt needed for fail }
  711. _vmt:=pointer(-1);
  712. end;
  713. if _self<>nil then
  714. begin
  715. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  716. ppointer(_self+_vmt_pos)^:=vmtcopy;
  717. end;
  718. fpc_help_constructor:=_self;
  719. end;
  720. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  721. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  722. { Note: _self will not be reset, the compiler has to generate the reset }
  723. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  724. type
  725. ppointer = ^pointer;
  726. pvmt = ^tvmt;
  727. tvmt = packed record
  728. size,msize : ptruint;
  729. parent : pointer;
  730. end;
  731. begin
  732. { already released? }
  733. if (_self=nil) or
  734. (_vmt=nil) or
  735. (ppointer(_self+vmt_pos)^=nil) then
  736. exit;
  737. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  738. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  739. RunError(210);
  740. { reset vmt to nil for protection }
  741. ppointer(_self+vmt_pos)^:=nil;
  742. freemem(_self);
  743. end;
  744. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  745. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  746. { Note: _self will not be reset, the compiler has to generate the reset }
  747. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  748. begin
  749. if (_self=nil) or (_vmt=nil) then
  750. exit;
  751. { vmt=$ffffffff when memory was allocated }
  752. if ptruint(_vmt)=high(ptruint) then
  753. begin
  754. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  755. HandleError(210)
  756. else
  757. begin
  758. ppointer(_self+vmt_pos)^:=nil;
  759. freemem(_self);
  760. { reset _vmt to nil so it will not be freed a
  761. second time }
  762. _vmt:=nil;
  763. end;
  764. end
  765. else
  766. ppointer(_self+vmt_pos)^:=nil;
  767. end;
  768. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  769. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  770. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  771. type
  772. pvmt = ^tvmt;
  773. tvmt = packed record
  774. size,msize : ptruint;
  775. parent : pointer;
  776. end;
  777. begin
  778. if (_vmt=nil) or
  779. (pvmt(_vmt)^.size=0) or
  780. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  781. RunError(210);
  782. end;
  783. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  784. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  785. { checks for a correct vmt pointer }
  786. { deeper check to see if the current object is }
  787. { really related to the true }
  788. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  789. type
  790. pvmt = ^tvmt;
  791. tvmt = packed record
  792. size,msize : ptruint;
  793. parent : pointer;
  794. end;
  795. begin
  796. if (vmt=nil) or
  797. (pvmt(vmt)^.size=0) or
  798. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  799. RunError(210);
  800. while assigned(vmt) do
  801. if vmt=expvmt then
  802. exit
  803. else
  804. vmt:=pvmt(vmt)^.parent;
  805. RunError(219);
  806. end;
  807. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  808. {$endif FPC_HAS_FEATURE_OBJECTS}
  809. {****************************************************************************
  810. String
  811. ****************************************************************************}
  812. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  813. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  814. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  815. var
  816. slen : byte;
  817. begin
  818. slen:=length(sstr);
  819. if slen<len then
  820. len:=slen;
  821. move(sstr[0],result[0],len+1);
  822. if slen>len then
  823. result[0]:=chr(len);
  824. end;
  825. {$else FPC_STRTOSHORTSTRINGPROC}
  826. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  827. var
  828. slen : byte;
  829. begin
  830. slen:=length(sstr);
  831. if slen>high(res) then
  832. slen:=high(res);
  833. move(sstr[0],res[0],slen+1);
  834. res[0]:=chr(slen);
  835. end;
  836. {$endif FPC_STRTOSHORTSTRINGPROC}
  837. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  838. var
  839. slen : byte;
  840. type
  841. pstring = ^string;
  842. begin
  843. slen:=length(pstring(sstr)^);
  844. if slen<len then
  845. len:=slen;
  846. move(sstr^,dstr^,len+1);
  847. if slen>len then
  848. pchar(dstr)^:=chr(len);
  849. end;
  850. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  851. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  852. {$ifndef STR_CONCAT_PROCS}
  853. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; compilerproc;
  854. var
  855. s1l, s2l : byte;
  856. begin
  857. s1l:=length(s1);
  858. s2l:=length(s2);
  859. if s1l+s2l>255 then
  860. s2l:=255-s1l;
  861. move(s1[1],fpc_shortstr_concat[1],s1l);
  862. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  863. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  864. end;
  865. {$else STR_CONCAT_PROCS}
  866. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  867. var
  868. s1l, s2l : longint;
  869. begin
  870. s1l:=length(s1);
  871. s2l:=length(s2);
  872. if s1l+s2l>high(dests) then
  873. begin
  874. if s1l>high(dests) then
  875. s1l:=high(dests);
  876. s2l:=high(dests)-s1l;
  877. end;
  878. if @dests=@s1 then
  879. move(s2[1],dests[s1l+1],s2l)
  880. else
  881. if @dests=@s2 then
  882. begin
  883. move(dests[1],dests[s1l+1],s2l);
  884. move(s1[1],dests[1],s1l);
  885. end
  886. else
  887. begin
  888. move(s1[1],dests[1],s1l);
  889. move(s2[1],dests[s1l+1],s2l);
  890. end;
  891. dests[0]:=chr(s1l+s2l);
  892. end;
  893. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  894. var
  895. s2l : byte;
  896. LowStart,i,
  897. Len : longint;
  898. pc : pchar;
  899. needtemp : boolean;
  900. tmpstr : shortstring;
  901. p,pdest : pshortstring;
  902. begin
  903. if high(sarr)=0 then
  904. begin
  905. DestS:='';
  906. exit;
  907. end;
  908. lowstart:=low(sarr);
  909. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  910. inc(lowstart);
  911. { Check for another reuse, then we can't use
  912. the append optimization and need to use a temp }
  913. needtemp:=false;
  914. for i:=lowstart to high(sarr) do
  915. begin
  916. if Pointer(@DestS)=Pointer(sarr[i]) then
  917. begin
  918. needtemp:=true;
  919. break;
  920. end;
  921. end;
  922. if needtemp then
  923. begin
  924. lowstart:=low(sarr);
  925. tmpstr:='';
  926. pdest:=@tmpstr
  927. end
  928. else
  929. begin
  930. { Start with empty DestS if we start with concatting
  931. the first array element }
  932. if lowstart=low(sarr) then
  933. DestS:='';
  934. pdest:=@DestS;
  935. end;
  936. { Concat all strings, except the string we already
  937. copied in DestS }
  938. Len:=length(pdest^);
  939. pc:=@pdest^[1+Length(pdest^)];
  940. for i:=lowstart to high(sarr) do
  941. begin
  942. p:=sarr[i];
  943. if assigned(p) then
  944. begin
  945. s2l:=length(p^);
  946. if Len+s2l>high(dests) then
  947. s2l:=high(dests)-Len;
  948. Move(p^[1],pc^,s2l);
  949. inc(pc,s2l);
  950. inc(Len,s2l);
  951. end;
  952. end;
  953. pdest^[0]:=Chr(Len);
  954. if needtemp then
  955. DestS:=TmpStr;
  956. end;
  957. {$endif STR_CONCAT_PROCS}
  958. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  959. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  960. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  961. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  962. var
  963. s1l, s2l : integer;
  964. begin
  965. s1l:=length(s1);
  966. s2l:=length(s2);
  967. if s1l+s2l>high(s1) then
  968. s2l:=high(s1)-s1l;
  969. move(s2[1],s1[s1l+1],s2l);
  970. s1[0]:=chr(s1l+s2l);
  971. end;
  972. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  973. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  974. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  975. var
  976. s1,s2,max,i : byte;
  977. d : longint;
  978. begin
  979. s1:=length(left);
  980. s2:=length(right);
  981. if s1<s2 then
  982. max:=s1
  983. else
  984. max:=s2;
  985. for i:=1 to max do
  986. begin
  987. d:=byte(left[i])-byte(right[i]);
  988. if d>0 then
  989. exit(1)
  990. else if d<0 then
  991. exit(-1);
  992. end;
  993. if s1>s2 then
  994. exit(1)
  995. else if s1<s2 then
  996. exit(-1)
  997. else
  998. exit(0);
  999. end;
  1000. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  1001. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  1002. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  1003. begin
  1004. Result := longint(left[0]) - longint(right[0]);
  1005. if Result = 0 then
  1006. Result := CompareByte(left[1],right[1], longint(left[0]));
  1007. end;
  1008. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  1009. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1010. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1011. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  1012. var
  1013. l : longint;
  1014. s: shortstring;
  1015. begin
  1016. if p=nil then
  1017. l:=0
  1018. else
  1019. l:=strlen(p);
  1020. if l>255 then
  1021. l:=255;
  1022. if l>0 then
  1023. move(p^,s[1],l);
  1024. s[0]:=chr(l);
  1025. fpc_pchar_to_shortstr := s;
  1026. end;
  1027. {$else FPC_STRTOSHORTSTRINGPROC}
  1028. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  1029. var
  1030. l : longint;
  1031. s: shortstring;
  1032. begin
  1033. if p=nil then
  1034. l:=0
  1035. else
  1036. l:=strlen(p);
  1037. if l>high(res) then
  1038. l:=high(res);
  1039. if l>0 then
  1040. move(p^,s[1],l);
  1041. s[0]:=chr(l);
  1042. res:=s;
  1043. end;
  1044. {$endif FPC_STRTOSHORTSTRINGPROC}
  1045. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1046. {$ifndef JVM}
  1047. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1048. { also define alias which can be used inside the system unit }
  1049. function fpc_pchar_to_shortstr(p:pchar):shortstring;[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1050. {$else FPC_STRTOSHORTSTRINGPROC}
  1051. { also define alias which can be used inside the system unit }
  1052. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1053. {$endif FPC_STRTOSHORTSTRINGPROC}
  1054. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1055. begin
  1056. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1057. result:=fpc_pchar_to_shortstr(p);
  1058. {$else FPC_STRTOSHORTSTRINGPROC}
  1059. fpc_pchar_to_shortstr(result,p);
  1060. {$endif FPC_STRTOSHORTSTRINGPROC}
  1061. end;
  1062. {$endif not JVM}
  1063. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1064. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1065. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1066. var
  1067. l: longint;
  1068. index: longint;
  1069. len: byte;
  1070. begin
  1071. l := high(arr)+1;
  1072. if l>=256 then
  1073. l:=255
  1074. else if l<0 then
  1075. l:=0;
  1076. if (zerobased) then
  1077. begin
  1078. index:=IndexByte(arr[0],l,0);
  1079. if (index < 0) then
  1080. len := l
  1081. else
  1082. len := index;
  1083. end
  1084. else
  1085. len := l;
  1086. move(arr[0],fpc_chararray_to_shortstr[1],len);
  1087. fpc_chararray_to_shortstr[0]:=chr(len);
  1088. end;
  1089. {$else FPC_STRTOSHORTSTRINGPROC}
  1090. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1091. var
  1092. l: longint;
  1093. index: longint;
  1094. len: byte;
  1095. begin
  1096. l:=high(arr)+1;
  1097. if l>=high(res)+1 then
  1098. l:=high(res)
  1099. else if l<0 then
  1100. l:=0;
  1101. if zerobased then
  1102. begin
  1103. index:=IndexByte(arr[0],l,0);
  1104. if index<0 then
  1105. len:=l
  1106. else
  1107. len:=index;
  1108. end
  1109. else
  1110. len:=l;
  1111. move(arr[0],res[1],len);
  1112. res[0]:=chr(len);
  1113. end;
  1114. {$endif FPC_STRTOSHORTSTRINGPROC}
  1115. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1116. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1117. {$ifndef FPC_STRTOCHARARRAYPROC}
  1118. { inside the compiler, the resulttype is modified to that of the actual }
  1119. { chararray we're converting to (JM) }
  1120. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  1121. var
  1122. len: longint;
  1123. begin
  1124. len := length(src);
  1125. if len > arraysize then
  1126. len := arraysize;
  1127. {$r-}
  1128. { make sure we don't access char 1 if length is 0 (JM) }
  1129. if len > 0 then
  1130. move(src[1],fpc_shortstr_to_chararray[0],len);
  1131. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  1132. {$ifdef RangeCheckWasOn}
  1133. {$r+}
  1134. {$endif}
  1135. end;
  1136. {$else ndef FPC_STRTOCHARARRAYPROC}
  1137. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1138. var
  1139. len: longint;
  1140. begin
  1141. len := length(src);
  1142. if len > length(res) then
  1143. len := length(res);
  1144. {$r-}
  1145. { make sure we don't access char 1 if length is 0 (JM) }
  1146. if len > 0 then
  1147. move(src[1],res[0],len);
  1148. fillchar(res[len],length(res)-len,0);
  1149. {$ifdef RangeCheckWasOn}
  1150. {$r+}
  1151. {$endif}
  1152. end;
  1153. {$endif ndef FPC_STRTOCHARARRAYPROC}
  1154. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1155. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1156. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1157. var i : sizeint;
  1158. begin
  1159. i:=0;
  1160. if assigned(p) then
  1161. while p[i]<>#0 do
  1162. inc(i);
  1163. exit(i);
  1164. end;
  1165. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1166. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1167. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1168. var i : sizeint;
  1169. begin
  1170. i:=0;
  1171. if assigned(p) then
  1172. while p[i]<>#0 do
  1173. inc(i);
  1174. exit(i);
  1175. end;
  1176. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1177. {****************************************************************************
  1178. Caller/StackFrame Helpers
  1179. ****************************************************************************}
  1180. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1181. {_$error Get_frame must be defined for each processor }
  1182. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1183. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1184. {_$error Get_caller_addr must be defined for each processor }
  1185. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1186. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1187. {_$error Get_caller_frame must be defined for each processor }
  1188. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1189. {****************************************************************************
  1190. Math
  1191. ****************************************************************************}
  1192. {****************************************************************************
  1193. Software multiplication
  1194. ****************************************************************************}
  1195. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1196. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1197. var
  1198. sign : boolean;
  1199. q1,q2,q3 : word;
  1200. begin
  1201. begin
  1202. sign:=false;
  1203. if f1<0 then
  1204. begin
  1205. sign:=not(sign);
  1206. q1:=word(-f1);
  1207. end
  1208. else
  1209. q1:=f1;
  1210. if f2<0 then
  1211. begin
  1212. sign:=not(sign);
  1213. q2:=word(-f2);
  1214. end
  1215. else
  1216. q2:=f2;
  1217. { the q1*q2 is coded as call to mulqword }
  1218. q3:=q1*q2;
  1219. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1220. ((q1>q3) or (q2>q3) or
  1221. { the bit 63 can be only set if we have $8000 }
  1222. { and sign is true }
  1223. (q3 shr 15<>0) and
  1224. ((q3<>word(word(1) shl 15)) or not(sign))
  1225. ) then
  1226. HandleErrorFrame(215,get_frame);
  1227. if sign then
  1228. fpc_mul_integer:=-q3
  1229. else
  1230. fpc_mul_integer:=q3;
  1231. end;
  1232. end;
  1233. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1234. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1235. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1236. var
  1237. _f1,bitpos : word;
  1238. b : byte;
  1239. f1overflowed : boolean;
  1240. begin
  1241. fpc_mul_word:=0;
  1242. bitpos:=1;
  1243. f1overflowed:=false;
  1244. for b:=0 to 15 do
  1245. begin
  1246. if (f2 and bitpos)<>0 then
  1247. begin
  1248. _f1:=fpc_mul_word;
  1249. fpc_mul_word:=fpc_mul_word+f1;
  1250. { if one of the operands is greater than the result an
  1251. overflow occurs }
  1252. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1253. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1254. HandleErrorFrame(215,get_frame);
  1255. end;
  1256. { when bootstrapping, we forget about overflow checking for qword :) }
  1257. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1258. f1:=f1 shl 1;
  1259. bitpos:=bitpos shl 1;
  1260. end;
  1261. end;
  1262. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1263. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1264. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1265. var
  1266. sign : boolean;
  1267. q1,q2,q3 : dword;
  1268. begin
  1269. begin
  1270. sign:=false;
  1271. if f1<0 then
  1272. begin
  1273. sign:=not(sign);
  1274. q1:=dword(-f1);
  1275. end
  1276. else
  1277. q1:=f1;
  1278. if f2<0 then
  1279. begin
  1280. sign:=not(sign);
  1281. q2:=dword(-f2);
  1282. end
  1283. else
  1284. q2:=f2;
  1285. { the q1*q2 is coded as call to mulqword }
  1286. q3:=q1*q2;
  1287. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1288. ((q1>q3) or (q2>q3) or
  1289. { the bit 31 can be only set if we have $8000 0000 }
  1290. { and sign is true }
  1291. (q3 shr 15<>0) and
  1292. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1293. ) then
  1294. HandleErrorFrame(215,get_frame);
  1295. if sign then
  1296. fpc_mul_longint:=-q3
  1297. else
  1298. fpc_mul_longint:=q3;
  1299. end;
  1300. end;
  1301. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1302. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1303. { multiplies two dwords
  1304. the longbool for checkoverflow avoids a misaligned stack
  1305. }
  1306. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1307. var
  1308. _f1,bitpos : dword;
  1309. b : byte;
  1310. f1overflowed : boolean;
  1311. begin
  1312. fpc_mul_dword:=0;
  1313. bitpos:=1;
  1314. f1overflowed:=false;
  1315. for b:=0 to 31 do
  1316. begin
  1317. if (f2 and bitpos)<>0 then
  1318. begin
  1319. _f1:=fpc_mul_dword;
  1320. fpc_mul_dword:=fpc_mul_dword+f1;
  1321. { if one of the operands is greater than the result an
  1322. overflow occurs }
  1323. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1324. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1325. HandleErrorFrame(215,get_frame);
  1326. end;
  1327. { when bootstrapping, we forget about overflow checking for qword :) }
  1328. f1overflowed:=f1overflowed or ((f1 and (1 shl 31))<>0);
  1329. f1:=f1 shl 1;
  1330. bitpos:=bitpos shl 1;
  1331. end;
  1332. end;
  1333. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1334. {****************************************************************************
  1335. Software longint/dword division
  1336. ****************************************************************************}
  1337. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1338. function count_leading_zeros_32bit(l : longint) : longint;
  1339. var
  1340. i : longint;
  1341. begin
  1342. for i:=0 to 31 do
  1343. begin
  1344. if (l and (longint($80000000) shr i))<>0 then
  1345. begin
  1346. result:=i;
  1347. exit;
  1348. end;
  1349. end;
  1350. result:=i;
  1351. end;
  1352. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1353. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1354. var
  1355. shift,lzz,lzn : longint;
  1356. begin
  1357. result:=0;
  1358. if n=0 then
  1359. HandleErrorFrame(200,get_frame);
  1360. lzz:=count_leading_zeros_32bit(z);
  1361. lzn:=count_leading_zeros_32bit(n);
  1362. { if the denominator contains less zeros
  1363. then the numerator
  1364. the d is greater than the n }
  1365. if lzn<lzz then
  1366. exit;
  1367. shift:=lzn-lzz;
  1368. n:=n shl shift;
  1369. repeat
  1370. if z>=n then
  1371. begin
  1372. z:=z-n;
  1373. result:=result+dword(1 shl shift);
  1374. end;
  1375. dec(shift);
  1376. n:=n shr 1;
  1377. until shift<0;
  1378. end;
  1379. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1380. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1381. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1382. var
  1383. shift,lzz,lzn : longint;
  1384. begin
  1385. result:=0;
  1386. if n=0 then
  1387. HandleErrorFrame(200,get_frame);
  1388. lzz:=count_leading_zeros_32bit(z);
  1389. lzn:=count_leading_zeros_32bit(n);
  1390. { if the denominator contains less zeros
  1391. then the numerator
  1392. the d is greater than the n }
  1393. if lzn<lzz then
  1394. begin
  1395. result:=z;
  1396. exit;
  1397. end;
  1398. shift:=lzn-lzz;
  1399. n:=n shl shift;
  1400. repeat
  1401. if z>=n then
  1402. z:=z-n;
  1403. dec(shift);
  1404. n:=n shr 1;
  1405. until shift<0;
  1406. result:=z;
  1407. end;
  1408. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1409. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1410. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1411. var
  1412. sign : boolean;
  1413. d1,d2 : dword;
  1414. begin
  1415. if n=0 then
  1416. HandleErrorFrame(200,get_frame);
  1417. sign:=false;
  1418. if z<0 then
  1419. begin
  1420. sign:=not(sign);
  1421. d1:=dword(-z);
  1422. end
  1423. else
  1424. d1:=z;
  1425. if n<0 then
  1426. begin
  1427. sign:=not(sign);
  1428. d2:=dword(-n);
  1429. end
  1430. else
  1431. d2:=n;
  1432. { the div is coded by the compiler as call to divdword }
  1433. if sign then
  1434. result:=-(d1 div d2)
  1435. else
  1436. result:=d1 div d2;
  1437. end;
  1438. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1439. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1440. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1441. var
  1442. signed : boolean;
  1443. r,nq,zq : dword;
  1444. begin
  1445. if n=0 then
  1446. HandleErrorFrame(200,get_frame);
  1447. nq:=abs(n);
  1448. if z<0 then
  1449. begin
  1450. zq:=dword(-z);
  1451. signed:=true;
  1452. end
  1453. else
  1454. begin
  1455. zq:=z;
  1456. signed:=false;
  1457. end;
  1458. r:=zq mod nq;
  1459. if signed then
  1460. result:=-longint(r)
  1461. else
  1462. result:=r;
  1463. end;
  1464. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1465. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1466. {****************************************************************************}
  1467. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1468. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1469. begin
  1470. if l<0 then
  1471. abs:=-l
  1472. else
  1473. abs:=l;
  1474. end;
  1475. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1476. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1477. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1478. begin
  1479. odd:=boolean(l and 1);
  1480. end;
  1481. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1482. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1483. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1484. begin
  1485. odd:=boolean(l and 1);
  1486. end;
  1487. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1488. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1489. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1490. begin
  1491. odd:=boolean(longint(l) and 1);
  1492. end;
  1493. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1494. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1495. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1496. begin
  1497. odd:=boolean(longint(l) and 1);
  1498. end;
  1499. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1500. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1501. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1502. begin
  1503. sqr:=l*l;
  1504. end;
  1505. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1506. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1507. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1508. begin
  1509. if l < 0 then
  1510. abs := -l
  1511. else
  1512. abs := l;
  1513. end;
  1514. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1515. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1516. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1517. begin
  1518. sqr := l*l;
  1519. end;
  1520. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1521. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1522. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1523. begin
  1524. sqr := l*l;
  1525. end;
  1526. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1527. {$ifdef CPU16}
  1528. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1529. function declocked(var l:smallint):boolean;
  1530. begin
  1531. Dec(l);
  1532. declocked:=(l=0);
  1533. end;
  1534. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1535. {$endif CPU16}
  1536. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1537. function declocked(var l:longint):boolean;
  1538. begin
  1539. Dec(l);
  1540. declocked:=(l=0);
  1541. end;
  1542. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1543. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1544. function declocked(var l:int64):boolean;
  1545. begin
  1546. Dec(l);
  1547. declocked:=(l=0);
  1548. end;
  1549. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1550. {$ifdef CPU16}
  1551. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1552. procedure inclocked(var l:smallint);
  1553. begin
  1554. Inc(l);
  1555. end;
  1556. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1557. {$endif CPU16}
  1558. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1559. procedure inclocked(var l:longint);
  1560. begin
  1561. Inc(l);
  1562. end;
  1563. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1564. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1565. procedure inclocked(var l:int64);
  1566. begin
  1567. Inc(l);
  1568. end;
  1569. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1570. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1571. {_$error Sptr must be defined for each processor }
  1572. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1573. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1574. var
  1575. tmp: PtrUInt;
  1576. begin
  1577. tmp:=addr+alignment-1;
  1578. result:=tmp-(tmp mod alignment)
  1579. end;
  1580. {$ifndef JVM}
  1581. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1582. var
  1583. tmp: PtrUInt;
  1584. begin
  1585. tmp:=PtrUInt(addr)+alignment-1;
  1586. result:=pointer(tmp-(tmp mod alignment));
  1587. end;
  1588. {$endif}
  1589. {****************************************************************************
  1590. Str()
  1591. ****************************************************************************}
  1592. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1593. procedure int_str(l:longint;out s:shortstring);
  1594. var
  1595. m,m1 : longword;
  1596. pcstart,
  1597. pc2start,
  1598. pc,pc2 : pchar;
  1599. hs : string[32];
  1600. overflow : longint;
  1601. begin
  1602. pc2start:=@s[1];
  1603. pc2:=pc2start;
  1604. if (l<0) then
  1605. begin
  1606. pc2^:='-';
  1607. inc(pc2);
  1608. m:=longword(-l);
  1609. end
  1610. else
  1611. m:=longword(l);
  1612. pcstart:=pchar(@hs[0]);
  1613. pc:=pcstart;
  1614. repeat
  1615. m1:=m div 10;
  1616. inc(pc);
  1617. pc^:=char(m-(m1*10)+byte('0'));
  1618. m:=m1;
  1619. until m=0;
  1620. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1621. if overflow>0 then
  1622. inc(pcstart,overflow);
  1623. while (pc>pcstart) do
  1624. begin
  1625. pc2^:=pc^;
  1626. inc(pc2);
  1627. dec(pc);
  1628. end;
  1629. s[0]:=char(pc2-pc2start);
  1630. end;
  1631. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1632. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1633. procedure int_str_unsigned(l:longword;out s:shortstring);
  1634. var
  1635. m1 : longword;
  1636. pcstart,
  1637. pc2start,
  1638. pc,pc2 : pchar;
  1639. hs : string[32];
  1640. overflow : longint;
  1641. begin
  1642. pc2start:=@s[1];
  1643. pc2:=pc2start;
  1644. pcstart:=pchar(@hs[0]);
  1645. pc:=pcstart;
  1646. repeat
  1647. inc(pc);
  1648. m1:=l div 10;
  1649. pc^:=char(l-(m1*10)+byte('0'));
  1650. l:=m1;
  1651. until l=0;
  1652. overflow:=(pc-pcstart)-high(s);
  1653. if overflow>0 then
  1654. inc(pcstart,overflow);
  1655. while (pc>pcstart) do
  1656. begin
  1657. pc2^:=pc^;
  1658. inc(pc2);
  1659. dec(pc);
  1660. end;
  1661. s[0]:=char(pc2-pc2start);
  1662. end;
  1663. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1664. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1665. procedure int_str(l:int64;out s:shortstring);
  1666. var
  1667. m,m1 : qword;
  1668. pcstart,
  1669. pc2start,
  1670. pc,pc2 : pchar;
  1671. hs : string[32];
  1672. overflow : longint;
  1673. begin
  1674. pc2start:=@s[1];
  1675. pc2:=pc2start;
  1676. if (l<0) then
  1677. begin
  1678. pc2^:='-';
  1679. inc(pc2);
  1680. m:=qword(-l);
  1681. end
  1682. else
  1683. m:=qword(l);
  1684. pcstart:=pchar(@hs[0]);
  1685. pc:=pcstart;
  1686. repeat
  1687. m1:=m div 10;
  1688. inc(pc);
  1689. pc^:=char(m-(m1*10)+byte('0'));
  1690. m:=m1;
  1691. until m=0;
  1692. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1693. if overflow>0 then
  1694. inc(pcstart,overflow);
  1695. while (pc>pcstart) do
  1696. begin
  1697. pc2^:=pc^;
  1698. inc(pc2);
  1699. dec(pc);
  1700. end;
  1701. s[0]:=char(pc2-pc2start);
  1702. end;
  1703. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1704. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1705. procedure int_str_unsigned(l:qword;out s:shortstring);
  1706. var
  1707. m1 : qword;
  1708. pcstart,
  1709. pc2start,
  1710. pc,pc2 : pchar;
  1711. hs : string[64];
  1712. overflow : longint;
  1713. begin
  1714. pc2start:=@s[1];
  1715. pc2:=pc2start;
  1716. pcstart:=pchar(@hs[0]);
  1717. pc:=pcstart;
  1718. repeat
  1719. inc(pc);
  1720. m1:=l div 10;
  1721. pc^:=char(l-(m1*10)+byte('0'));
  1722. l:=m1;
  1723. until l=0;
  1724. overflow:=(pc-pcstart)-high(s);
  1725. if overflow>0 then
  1726. inc(pcstart,overflow);
  1727. while (pc>pcstart) do
  1728. begin
  1729. pc2^:=pc^;
  1730. inc(pc2);
  1731. dec(pc);
  1732. end;
  1733. s[0]:=char(pc2-pc2start);
  1734. end;
  1735. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1736. {$ifndef FPUNONE}
  1737. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1738. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1739. begin
  1740. softfloat_exception_flags:=0;
  1741. end;
  1742. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1743. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1744. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1745. begin
  1746. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1747. end;
  1748. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1749. {$endif}
  1750. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1751. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1752. begin
  1753. { the extra Word type cast is necessary because the "AValue shr 8" }
  1754. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1755. { the sign bits from the upper 16 bits are shifted in rather than }
  1756. { zeroes. }
  1757. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1758. end;
  1759. {$ifndef cpujvm}
  1760. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1761. begin
  1762. Result := Word((AValue shr 8) or (AValue shl 8));
  1763. end;
  1764. {$endif}
  1765. function SwapEndian(const AValue: LongInt): LongInt;
  1766. begin
  1767. Result := (AValue shl 24)
  1768. or ((AValue and $0000FF00) shl 8)
  1769. or ((AValue and $00FF0000) shr 8)
  1770. or (AValue shr 24);
  1771. end;
  1772. {$ifndef cpujvm}
  1773. function SwapEndian(const AValue: DWord): DWord;
  1774. begin
  1775. Result := (AValue shl 24)
  1776. or ((AValue and $0000FF00) shl 8)
  1777. or ((AValue and $00FF0000) shr 8)
  1778. or (AValue shr 24);
  1779. end;
  1780. {$endif}
  1781. function SwapEndian(const AValue: Int64): Int64;
  1782. begin
  1783. Result := (AValue shl 56)
  1784. or ((AValue and $000000000000FF00) shl 40)
  1785. or ((AValue and $0000000000FF0000) shl 24)
  1786. or ((AValue and $00000000FF000000) shl 8)
  1787. or ((AValue and $000000FF00000000) shr 8)
  1788. or ((AValue and $0000FF0000000000) shr 24)
  1789. or ((AValue and $00FF000000000000) shr 40)
  1790. or (AValue shr 56);
  1791. end;
  1792. {$ifndef cpujvm}
  1793. function SwapEndian(const AValue: QWord): QWord;
  1794. begin
  1795. Result := (AValue shl 56)
  1796. or ((AValue and $000000000000FF00) shl 40)
  1797. or ((AValue and $0000000000FF0000) shl 24)
  1798. or ((AValue and $00000000FF000000) shl 8)
  1799. or ((AValue and $000000FF00000000) shr 8)
  1800. or ((AValue and $0000FF0000000000) shr 24)
  1801. or ((AValue and $00FF000000000000) shr 40)
  1802. or (AValue shr 56);
  1803. end;
  1804. {$endif}
  1805. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1806. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1807. begin
  1808. {$IFDEF ENDIAN_BIG}
  1809. Result := AValue;
  1810. {$ELSE}
  1811. Result := SwapEndian(AValue);
  1812. {$ENDIF}
  1813. end;
  1814. {$ifndef cpujvm}
  1815. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1816. begin
  1817. {$IFDEF ENDIAN_BIG}
  1818. Result := AValue;
  1819. {$ELSE}
  1820. Result := SwapEndian(AValue);
  1821. {$ENDIF}
  1822. end;
  1823. {$endif not cpujvm}
  1824. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1825. begin
  1826. {$IFDEF ENDIAN_BIG}
  1827. Result := AValue;
  1828. {$ELSE}
  1829. Result := SwapEndian(AValue);
  1830. {$ENDIF}
  1831. end;
  1832. {$ifndef cpujvm}
  1833. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1834. begin
  1835. {$IFDEF ENDIAN_BIG}
  1836. Result := AValue;
  1837. {$ELSE}
  1838. Result := SwapEndian(AValue);
  1839. {$ENDIF}
  1840. end;
  1841. {$endif not cpujvm}
  1842. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1843. begin
  1844. {$IFDEF ENDIAN_BIG}
  1845. Result := AValue;
  1846. {$ELSE}
  1847. Result := SwapEndian(AValue);
  1848. {$ENDIF}
  1849. end;
  1850. {$ifndef cpujvm}
  1851. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1852. begin
  1853. {$IFDEF ENDIAN_BIG}
  1854. Result := AValue;
  1855. {$ELSE}
  1856. Result := SwapEndian(AValue);
  1857. {$ENDIF}
  1858. end;
  1859. {$endif not cpujvm}
  1860. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1861. begin
  1862. {$IFDEF ENDIAN_LITTLE}
  1863. Result := AValue;
  1864. {$ELSE}
  1865. Result := SwapEndian(AValue);
  1866. {$ENDIF}
  1867. end;
  1868. {$ifndef cpujvm}
  1869. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1870. begin
  1871. {$IFDEF ENDIAN_LITTLE}
  1872. Result := AValue;
  1873. {$ELSE}
  1874. Result := SwapEndian(AValue);
  1875. {$ENDIF}
  1876. end;
  1877. {$endif not cpujvm}
  1878. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1879. begin
  1880. {$IFDEF ENDIAN_LITTLE}
  1881. Result := AValue;
  1882. {$ELSE}
  1883. Result := SwapEndian(AValue);
  1884. {$ENDIF}
  1885. end;
  1886. {$ifndef cpujvm}
  1887. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1888. begin
  1889. {$IFDEF ENDIAN_LITTLE}
  1890. Result := AValue;
  1891. {$ELSE}
  1892. Result := SwapEndian(AValue);
  1893. {$ENDIF}
  1894. end;
  1895. {$endif not cpujvm}
  1896. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1897. begin
  1898. {$IFDEF ENDIAN_LITTLE}
  1899. Result := AValue;
  1900. {$ELSE}
  1901. Result := SwapEndian(AValue);
  1902. {$ENDIF}
  1903. end;
  1904. {$ifndef cpujvm}
  1905. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1906. begin
  1907. {$IFDEF ENDIAN_LITTLE}
  1908. Result := AValue;
  1909. {$ELSE}
  1910. Result := SwapEndian(AValue);
  1911. {$ENDIF}
  1912. end;
  1913. {$endif not cpujvm}
  1914. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1915. begin
  1916. {$IFDEF ENDIAN_BIG}
  1917. Result := AValue;
  1918. {$ELSE}
  1919. Result := SwapEndian(AValue);
  1920. {$ENDIF}
  1921. end;
  1922. {$ifndef cpujvm}
  1923. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1924. begin
  1925. {$IFDEF ENDIAN_BIG}
  1926. Result := AValue;
  1927. {$ELSE}
  1928. Result := SwapEndian(AValue);
  1929. {$ENDIF}
  1930. end;
  1931. {$endif not cpujvm}
  1932. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1933. begin
  1934. {$IFDEF ENDIAN_BIG}
  1935. Result := AValue;
  1936. {$ELSE}
  1937. Result := SwapEndian(AValue);
  1938. {$ENDIF}
  1939. end;
  1940. {$ifndef cpujvm}
  1941. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1942. begin
  1943. {$IFDEF ENDIAN_BIG}
  1944. Result := AValue;
  1945. {$ELSE}
  1946. Result := SwapEndian(AValue);
  1947. {$ENDIF}
  1948. end;
  1949. {$endif not cpujvm}
  1950. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1951. begin
  1952. {$IFDEF ENDIAN_BIG}
  1953. Result := AValue;
  1954. {$ELSE}
  1955. Result := SwapEndian(AValue);
  1956. {$ENDIF}
  1957. end;
  1958. {$ifndef cpujvm}
  1959. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1960. begin
  1961. {$IFDEF ENDIAN_BIG}
  1962. Result := AValue;
  1963. {$ELSE}
  1964. Result := SwapEndian(AValue);
  1965. {$ENDIF}
  1966. end;
  1967. {$endif not cpujvm}
  1968. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1969. begin
  1970. {$IFDEF ENDIAN_LITTLE}
  1971. Result := AValue;
  1972. {$ELSE}
  1973. Result := SwapEndian(AValue);
  1974. {$ENDIF}
  1975. end;
  1976. {$ifndef cpujvm}
  1977. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1978. begin
  1979. {$IFDEF ENDIAN_LITTLE}
  1980. Result := AValue;
  1981. {$ELSE}
  1982. Result := SwapEndian(AValue);
  1983. {$ENDIF}
  1984. end;
  1985. {$endif not cpujvm}
  1986. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1987. begin
  1988. {$IFDEF ENDIAN_LITTLE}
  1989. Result := AValue;
  1990. {$ELSE}
  1991. Result := SwapEndian(AValue);
  1992. {$ENDIF}
  1993. end;
  1994. {$ifndef cpujvm}
  1995. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1996. begin
  1997. {$IFDEF ENDIAN_LITTLE}
  1998. Result := AValue;
  1999. {$ELSE}
  2000. Result := SwapEndian(AValue);
  2001. {$ENDIF}
  2002. end;
  2003. {$endif not cpujvm}
  2004. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2005. begin
  2006. {$IFDEF ENDIAN_LITTLE}
  2007. Result := AValue;
  2008. {$ELSE}
  2009. Result := SwapEndian(AValue);
  2010. {$ENDIF}
  2011. end;
  2012. {$ifndef cpujvm}
  2013. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2014. begin
  2015. {$IFDEF ENDIAN_LITTLE}
  2016. Result := AValue;
  2017. {$ELSE}
  2018. Result := SwapEndian(AValue);
  2019. {$ENDIF}
  2020. end;
  2021. {$endif not cpujvm}
  2022. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2023. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2024. begin
  2025. end;
  2026. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2027. begin
  2028. end;
  2029. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2030. begin
  2031. end;
  2032. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2033. begin
  2034. end;
  2035. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2036. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2037. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2038. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2039. begin
  2040. Result:=(AValue shr 1) or (AValue shl 7);
  2041. end;
  2042. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2043. begin
  2044. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2045. end;
  2046. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2047. begin
  2048. Result:=(AValue shl 1) or (AValue shr 7);
  2049. end;
  2050. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2051. begin
  2052. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2053. end;
  2054. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2055. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2056. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2057. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2058. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2059. begin
  2060. Result:=(AValue shr 1) or (AValue shl 15);
  2061. end;
  2062. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2063. begin
  2064. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2065. end;
  2066. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2067. begin
  2068. Result:=(AValue shl 1) or (AValue shr 15);
  2069. end;
  2070. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2071. begin
  2072. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2073. end;
  2074. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2075. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2076. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2077. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2078. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2079. begin
  2080. Result:=(AValue shr 1) or (AValue shl 31);
  2081. end;
  2082. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2083. begin
  2084. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2085. end;
  2086. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2087. begin
  2088. Result:=(AValue shl 1) or (AValue shr 31);
  2089. end;
  2090. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2091. begin
  2092. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2093. end;
  2094. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2095. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2096. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2097. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2098. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2099. begin
  2100. Result:=(AValue shr 1) or (AValue shl 63);
  2101. end;
  2102. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2103. begin
  2104. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2105. end;
  2106. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2107. begin
  2108. Result:=(AValue shl 1) or (AValue shr 63);
  2109. end;
  2110. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2111. begin
  2112. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2113. end;
  2114. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2115. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2116. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2117. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2118. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2119. begin
  2120. 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)))));
  2121. end;
  2122. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2123. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2124. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2125. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2126. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2127. begin
  2128. 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)))));
  2129. end;
  2130. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2131. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2132. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2133. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2134. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2135. begin
  2136. 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)))));
  2137. end;
  2138. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2139. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2140. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2141. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2142. function SarInt64(Const AValue : Int64;const Shift : Byte): Int64;
  2143. begin
  2144. 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)))));
  2145. end;
  2146. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2147. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2148. {$ifndef FPC_HAS_INTERNAL_BSX_BYTE}
  2149. {$ifndef FPC_SYSTEM_HAS_BSX_BYTE}
  2150. function BsfByte(Const AValue: Byte): Byte;
  2151. const bsf8bit: array [Byte] of Byte = (
  2152. $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,
  2153. 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,
  2154. 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,
  2155. 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,
  2156. 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,
  2157. 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,
  2158. 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,
  2159. 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
  2160. );
  2161. begin
  2162. result:=bsf8bit[AValue];
  2163. end;
  2164. function BsrByte(Const AValue: Byte): Byte;
  2165. const bsr8bit: array [Byte] of Byte = (
  2166. $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,
  2167. 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,
  2168. 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,
  2169. 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,
  2170. 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,
  2171. 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,
  2172. 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,
  2173. 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
  2174. );
  2175. begin
  2176. result:=bsr8bit[AValue];
  2177. end;
  2178. {$endif}
  2179. {$endif}
  2180. {$ifndef FPC_SYSTEM_HAS_BSX_WORD}
  2181. {$ifndef FPC_HAS_INTERNAL_BSX_WORD}
  2182. function BsfWord(Const AValue: Word): cardinal;
  2183. begin
  2184. if lo(AValue)<>0 then
  2185. result:=BsfByte(lo(AValue))
  2186. else
  2187. result:=BsfByte(hi(AValue))+8
  2188. end;
  2189. function BsrWord(Const AValue: Word): cardinal;
  2190. begin
  2191. if hi(AValue)<>0 then
  2192. result:=BsrByte(hi(AValue))+8
  2193. else
  2194. result:=BsrByte(lo(AValue))
  2195. end;
  2196. {$endif}
  2197. {$endif}
  2198. {$ifndef FPC_HAS_INTERNAL_BSX_DWORD}
  2199. {$ifndef FPC_SYSTEM_HAS_BSX_DWORD}
  2200. function BsfDWord(Const AValue : DWord): cardinal;
  2201. begin
  2202. if lo(AValue)<>0 then
  2203. result:=BsfWord(lo(AValue))
  2204. else
  2205. result:=BsfWord(hi(AValue))+16
  2206. end;
  2207. function BsrDWord(Const AValue : DWord): cardinal;
  2208. begin
  2209. if hi(AValue)<>0 then
  2210. result:=BsrWord(hi(AValue))+16
  2211. else
  2212. result:=BsrWord(lo(AValue))
  2213. end;
  2214. {$endif}
  2215. {$endif}
  2216. {$ifndef FPC_HAS_INTERNAL_BSX_QWORD}
  2217. {$ifndef FPC_SYSTEM_HAS_BSX_QWORD}
  2218. function BsfQWord(Const AValue : QWord): cardinal;
  2219. begin
  2220. if lo(AValue) <> 0 then
  2221. result:=BsfDWord(lo(AValue))
  2222. else
  2223. result:=BsfDWord(hi(AValue)) + 32
  2224. end;
  2225. function BsrQWord(Const AValue : QWord): cardinal;
  2226. begin
  2227. if hi(AValue) <> 0 then
  2228. result:=BsrDWord(hi(AValue)) + 32
  2229. else
  2230. result:=BsrDWord(lo(AValue))
  2231. end;
  2232. {$endif}
  2233. {$endif}