generic.inc 63 KB

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