generic.inc 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  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 FPC_STRTOSHORTSTRINGPROC}
  1047. { also define alias which can be used inside the system unit }
  1048. function fpc_pchar_to_shortstr(p:pchar):shortstring;[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1049. {$else FPC_STRTOSHORTSTRINGPROC}
  1050. { also define alias which can be used inside the system unit }
  1051. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1052. {$endif FPC_STRTOSHORTSTRINGPROC}
  1053. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1054. begin
  1055. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1056. result:=fpc_pchar_to_shortstr(p);
  1057. {$else FPC_STRTOSHORTSTRINGPROC}
  1058. fpc_pchar_to_shortstr(result,p);
  1059. {$endif FPC_STRTOSHORTSTRINGPROC}
  1060. end;
  1061. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1062. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  1063. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1064. var
  1065. l: longint;
  1066. index: longint;
  1067. len: byte;
  1068. begin
  1069. l := high(arr)+1;
  1070. if l>=256 then
  1071. l:=255
  1072. else if l<0 then
  1073. l:=0;
  1074. if (zerobased) then
  1075. begin
  1076. index:=IndexByte(arr[0],l,0);
  1077. if (index < 0) then
  1078. len := l
  1079. else
  1080. len := index;
  1081. end
  1082. else
  1083. len := l;
  1084. move(arr[0],fpc_chararray_to_shortstr[1],len);
  1085. fpc_chararray_to_shortstr[0]:=chr(len);
  1086. end;
  1087. {$else FPC_STRTOSHORTSTRINGPROC}
  1088. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1089. var
  1090. l: longint;
  1091. index: longint;
  1092. len: byte;
  1093. begin
  1094. l:=high(arr)+1;
  1095. if l>=high(res)+1 then
  1096. l:=high(res)
  1097. else if l<0 then
  1098. l:=0;
  1099. if zerobased then
  1100. begin
  1101. index:=IndexByte(arr[0],l,0);
  1102. if index<0 then
  1103. len:=l
  1104. else
  1105. len:=index;
  1106. end
  1107. else
  1108. len:=l;
  1109. move(arr[0],res[1],len);
  1110. res[0]:=chr(len);
  1111. end;
  1112. {$endif FPC_STRTOSHORTSTRINGPROC}
  1113. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1114. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1115. {$ifndef FPC_STRTOCHARARRAYPROC}
  1116. { inside the compiler, the resulttype is modified to that of the actual }
  1117. { chararray we're converting to (JM) }
  1118. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  1119. var
  1120. len: longint;
  1121. begin
  1122. len := length(src);
  1123. if len > arraysize then
  1124. len := arraysize;
  1125. {$r-}
  1126. { make sure we don't access char 1 if length is 0 (JM) }
  1127. if len > 0 then
  1128. move(src[1],fpc_shortstr_to_chararray[0],len);
  1129. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  1130. {$ifdef RangeCheckWasOn}
  1131. {$r+}
  1132. {$endif}
  1133. end;
  1134. {$else ndef FPC_STRTOCHARARRAYPROC}
  1135. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1136. var
  1137. len: longint;
  1138. begin
  1139. len := length(src);
  1140. if len > length(res) then
  1141. len := length(res);
  1142. {$r-}
  1143. { make sure we don't access char 1 if length is 0 (JM) }
  1144. if len > 0 then
  1145. move(src[1],res[0],len);
  1146. fillchar(res[len],length(res)-len,0);
  1147. {$ifdef RangeCheckWasOn}
  1148. {$r+}
  1149. {$endif}
  1150. end;
  1151. {$endif ndef FPC_STRTOCHARARRAYPROC}
  1152. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1153. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1154. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1155. var i : sizeint;
  1156. begin
  1157. i:=0;
  1158. if assigned(p) then
  1159. while p[i]<>#0 do
  1160. inc(i);
  1161. exit(i);
  1162. end;
  1163. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1164. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1165. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1166. var i : sizeint;
  1167. begin
  1168. i:=0;
  1169. if assigned(p) then
  1170. while p[i]<>#0 do
  1171. inc(i);
  1172. exit(i);
  1173. end;
  1174. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1175. {****************************************************************************
  1176. Caller/StackFrame Helpers
  1177. ****************************************************************************}
  1178. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1179. {_$error Get_frame must be defined for each processor }
  1180. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1181. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1182. {_$error Get_caller_addr must be defined for each processor }
  1183. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1184. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1185. {_$error Get_caller_frame must be defined for each processor }
  1186. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1187. {****************************************************************************
  1188. Math
  1189. ****************************************************************************}
  1190. {****************************************************************************
  1191. Software multiplication
  1192. ****************************************************************************}
  1193. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1194. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1195. var
  1196. sign : boolean;
  1197. q1,q2,q3 : word;
  1198. begin
  1199. begin
  1200. sign:=false;
  1201. if f1<0 then
  1202. begin
  1203. sign:=not(sign);
  1204. q1:=word(-f1);
  1205. end
  1206. else
  1207. q1:=f1;
  1208. if f2<0 then
  1209. begin
  1210. sign:=not(sign);
  1211. q2:=word(-f2);
  1212. end
  1213. else
  1214. q2:=f2;
  1215. { the q1*q2 is coded as call to mulqword }
  1216. q3:=q1*q2;
  1217. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1218. ((q1>q3) or (q2>q3) or
  1219. { the bit 63 can be only set if we have $8000 }
  1220. { and sign is true }
  1221. (q3 shr 15<>0) and
  1222. ((q3<>word(word(1) shl 15)) or not(sign))
  1223. ) then
  1224. HandleErrorFrame(215,get_frame);
  1225. if sign then
  1226. fpc_mul_integer:=-q3
  1227. else
  1228. fpc_mul_integer:=q3;
  1229. end;
  1230. end;
  1231. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1232. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1233. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1234. var
  1235. _f1,bitpos : word;
  1236. b : byte;
  1237. f1overflowed : boolean;
  1238. begin
  1239. fpc_mul_word:=0;
  1240. bitpos:=1;
  1241. f1overflowed:=false;
  1242. for b:=0 to 15 do
  1243. begin
  1244. if (f2 and bitpos)<>0 then
  1245. begin
  1246. _f1:=fpc_mul_word;
  1247. fpc_mul_word:=fpc_mul_word+f1;
  1248. { if one of the operands is greater than the result an
  1249. overflow occurs }
  1250. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1251. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1252. HandleErrorFrame(215,get_frame);
  1253. end;
  1254. { when bootstrapping, we forget about overflow checking for qword :) }
  1255. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1256. f1:=f1 shl 1;
  1257. bitpos:=bitpos shl 1;
  1258. end;
  1259. end;
  1260. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1261. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1262. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1263. var
  1264. sign : boolean;
  1265. q1,q2,q3 : dword;
  1266. begin
  1267. begin
  1268. sign:=false;
  1269. if f1<0 then
  1270. begin
  1271. sign:=not(sign);
  1272. q1:=dword(-f1);
  1273. end
  1274. else
  1275. q1:=f1;
  1276. if f2<0 then
  1277. begin
  1278. sign:=not(sign);
  1279. q2:=dword(-f2);
  1280. end
  1281. else
  1282. q2:=f2;
  1283. { the q1*q2 is coded as call to mulqword }
  1284. q3:=q1*q2;
  1285. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1286. ((q1>q3) or (q2>q3) or
  1287. { the bit 31 can be only set if we have $8000 0000 }
  1288. { and sign is true }
  1289. (q3 shr 15<>0) and
  1290. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1291. ) then
  1292. HandleErrorFrame(215,get_frame);
  1293. if sign then
  1294. fpc_mul_longint:=-q3
  1295. else
  1296. fpc_mul_longint:=q3;
  1297. end;
  1298. end;
  1299. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1300. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1301. { multiplies two dwords
  1302. the longbool for checkoverflow avoids a misaligned stack
  1303. }
  1304. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1305. var
  1306. _f1,bitpos : dword;
  1307. b : byte;
  1308. f1overflowed : boolean;
  1309. begin
  1310. fpc_mul_dword:=0;
  1311. bitpos:=1;
  1312. f1overflowed:=false;
  1313. for b:=0 to 31 do
  1314. begin
  1315. if (f2 and bitpos)<>0 then
  1316. begin
  1317. _f1:=fpc_mul_dword;
  1318. fpc_mul_dword:=fpc_mul_dword+f1;
  1319. { if one of the operands is greater than the result an
  1320. overflow occurs }
  1321. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1322. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1323. HandleErrorFrame(215,get_frame);
  1324. end;
  1325. { when bootstrapping, we forget about overflow checking for qword :) }
  1326. f1overflowed:=f1overflowed or ((f1 and (1 shl 31))<>0);
  1327. f1:=f1 shl 1;
  1328. bitpos:=bitpos shl 1;
  1329. end;
  1330. end;
  1331. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1332. {****************************************************************************
  1333. Software longint/dword division
  1334. ****************************************************************************}
  1335. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1336. function count_leading_zeros_32bit(l : longint) : longint;
  1337. var
  1338. i : longint;
  1339. begin
  1340. for i:=0 to 31 do
  1341. begin
  1342. if (l and (longint($80000000) shr i))<>0 then
  1343. begin
  1344. result:=i;
  1345. exit;
  1346. end;
  1347. end;
  1348. result:=i;
  1349. end;
  1350. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1351. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1352. var
  1353. shift,lzz,lzn : longint;
  1354. begin
  1355. result:=0;
  1356. if n=0 then
  1357. HandleErrorFrame(200,get_frame);
  1358. lzz:=count_leading_zeros_32bit(z);
  1359. lzn:=count_leading_zeros_32bit(n);
  1360. { if the denominator contains less zeros
  1361. then the numerator
  1362. the d is greater than the n }
  1363. if lzn<lzz then
  1364. exit;
  1365. shift:=lzn-lzz;
  1366. n:=n shl shift;
  1367. repeat
  1368. if z>=n then
  1369. begin
  1370. z:=z-n;
  1371. result:=result+dword(1 shl shift);
  1372. end;
  1373. dec(shift);
  1374. n:=n shr 1;
  1375. until shift<0;
  1376. end;
  1377. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1378. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1379. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1380. var
  1381. shift,lzz,lzn : longint;
  1382. begin
  1383. result:=0;
  1384. if n=0 then
  1385. HandleErrorFrame(200,get_frame);
  1386. lzz:=count_leading_zeros_32bit(z);
  1387. lzn:=count_leading_zeros_32bit(n);
  1388. { if the denominator contains less zeros
  1389. then the numerator
  1390. the d is greater than the n }
  1391. if lzn<lzz then
  1392. begin
  1393. result:=z;
  1394. exit;
  1395. end;
  1396. shift:=lzn-lzz;
  1397. n:=n shl shift;
  1398. repeat
  1399. if z>=n then
  1400. z:=z-n;
  1401. dec(shift);
  1402. n:=n shr 1;
  1403. until shift<0;
  1404. result:=z;
  1405. end;
  1406. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1407. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1408. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1409. var
  1410. sign : boolean;
  1411. d1,d2 : dword;
  1412. begin
  1413. if n=0 then
  1414. HandleErrorFrame(200,get_frame);
  1415. sign:=false;
  1416. if z<0 then
  1417. begin
  1418. sign:=not(sign);
  1419. d1:=dword(-z);
  1420. end
  1421. else
  1422. d1:=z;
  1423. if n<0 then
  1424. begin
  1425. sign:=not(sign);
  1426. d2:=dword(-n);
  1427. end
  1428. else
  1429. d2:=n;
  1430. { the div is coded by the compiler as call to divdword }
  1431. if sign then
  1432. result:=-(d1 div d2)
  1433. else
  1434. result:=d1 div d2;
  1435. end;
  1436. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1437. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1438. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1439. var
  1440. signed : boolean;
  1441. r,nq,zq : dword;
  1442. begin
  1443. if n=0 then
  1444. HandleErrorFrame(200,get_frame);
  1445. nq:=abs(n);
  1446. if z<0 then
  1447. begin
  1448. zq:=dword(-z);
  1449. signed:=true;
  1450. end
  1451. else
  1452. begin
  1453. zq:=z;
  1454. signed:=false;
  1455. end;
  1456. r:=zq mod nq;
  1457. if signed then
  1458. result:=-longint(r)
  1459. else
  1460. result:=r;
  1461. end;
  1462. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1463. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1464. {****************************************************************************}
  1465. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1466. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1467. begin
  1468. if l<0 then
  1469. abs:=-l
  1470. else
  1471. abs:=l;
  1472. end;
  1473. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1474. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1475. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1476. begin
  1477. odd:=boolean(l and 1);
  1478. end;
  1479. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1480. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1481. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1482. begin
  1483. odd:=boolean(l and 1);
  1484. end;
  1485. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1486. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1487. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1488. begin
  1489. odd:=boolean(longint(l) and 1);
  1490. end;
  1491. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1492. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1493. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1494. begin
  1495. odd:=boolean(longint(l) and 1);
  1496. end;
  1497. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1498. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1499. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1500. begin
  1501. sqr:=l*l;
  1502. end;
  1503. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1504. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1505. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1506. begin
  1507. if l < 0 then
  1508. abs := -l
  1509. else
  1510. abs := l;
  1511. end;
  1512. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1513. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1514. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1515. begin
  1516. sqr := l*l;
  1517. end;
  1518. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1519. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1520. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1521. begin
  1522. sqr := l*l;
  1523. end;
  1524. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1525. {$ifdef CPU16}
  1526. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1527. function declocked(var l:smallint):boolean;
  1528. begin
  1529. Dec(l);
  1530. declocked:=(l=0);
  1531. end;
  1532. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1533. {$endif CPU16}
  1534. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1535. function declocked(var l:longint):boolean;
  1536. begin
  1537. Dec(l);
  1538. declocked:=(l=0);
  1539. end;
  1540. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1541. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1542. function declocked(var l:int64):boolean;
  1543. begin
  1544. Dec(l);
  1545. declocked:=(l=0);
  1546. end;
  1547. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1548. {$ifdef CPU16}
  1549. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1550. procedure inclocked(var l:smallint);
  1551. begin
  1552. Inc(l);
  1553. end;
  1554. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1555. {$endif CPU16}
  1556. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1557. procedure inclocked(var l:longint);
  1558. begin
  1559. Inc(l);
  1560. end;
  1561. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1562. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1563. procedure inclocked(var l:int64);
  1564. begin
  1565. Inc(l);
  1566. end;
  1567. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1568. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1569. {_$error Sptr must be defined for each processor }
  1570. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1571. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1572. var
  1573. tmp: PtrUInt;
  1574. begin
  1575. tmp:=addr+alignment-1;
  1576. result:=tmp-(tmp mod alignment)
  1577. end;
  1578. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1579. var
  1580. tmp: PtrUInt;
  1581. begin
  1582. tmp:=PtrUInt(addr)+alignment-1;
  1583. result:=pointer(tmp-(tmp mod alignment));
  1584. end;
  1585. {****************************************************************************
  1586. Str()
  1587. ****************************************************************************}
  1588. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1589. procedure int_str(l:longint;out s:string);
  1590. var
  1591. m,m1 : longword;
  1592. pcstart,
  1593. pc2start,
  1594. pc,pc2 : pchar;
  1595. hs : string[32];
  1596. overflow : longint;
  1597. begin
  1598. pc2start:=@s[1];
  1599. pc2:=pc2start;
  1600. if (l<0) then
  1601. begin
  1602. pc2^:='-';
  1603. inc(pc2);
  1604. m:=longword(-l);
  1605. end
  1606. else
  1607. m:=longword(l);
  1608. pcstart:=pchar(@hs[0]);
  1609. pc:=pcstart;
  1610. repeat
  1611. m1:=m div 10;
  1612. inc(pc);
  1613. pc^:=char(m-(m1*10)+byte('0'));
  1614. m:=m1;
  1615. until m=0;
  1616. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1617. if overflow>0 then
  1618. inc(pcstart,overflow);
  1619. while (pc>pcstart) do
  1620. begin
  1621. pc2^:=pc^;
  1622. inc(pc2);
  1623. dec(pc);
  1624. end;
  1625. s[0]:=char(pc2-pc2start);
  1626. end;
  1627. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1628. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1629. procedure int_str(l:longword;out s:string);
  1630. var
  1631. m1 : longword;
  1632. pcstart,
  1633. pc2start,
  1634. pc,pc2 : pchar;
  1635. hs : string[32];
  1636. overflow : longint;
  1637. begin
  1638. pc2start:=@s[1];
  1639. pc2:=pc2start;
  1640. pcstart:=pchar(@hs[0]);
  1641. pc:=pcstart;
  1642. repeat
  1643. inc(pc);
  1644. m1:=l div 10;
  1645. pc^:=char(l-(m1*10)+byte('0'));
  1646. l:=m1;
  1647. until l=0;
  1648. overflow:=(pc-pcstart)-high(s);
  1649. if overflow>0 then
  1650. inc(pcstart,overflow);
  1651. while (pc>pcstart) do
  1652. begin
  1653. pc2^:=pc^;
  1654. inc(pc2);
  1655. dec(pc);
  1656. end;
  1657. s[0]:=char(pc2-pc2start);
  1658. end;
  1659. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1660. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1661. procedure int_str(l:int64;out s:string);
  1662. var
  1663. m,m1 : qword;
  1664. pcstart,
  1665. pc2start,
  1666. pc,pc2 : pchar;
  1667. hs : string[32];
  1668. overflow : longint;
  1669. begin
  1670. pc2start:=@s[1];
  1671. pc2:=pc2start;
  1672. if (l<0) then
  1673. begin
  1674. pc2^:='-';
  1675. inc(pc2);
  1676. m:=qword(-l);
  1677. end
  1678. else
  1679. m:=qword(l);
  1680. pcstart:=pchar(@hs[0]);
  1681. pc:=pcstart;
  1682. repeat
  1683. m1:=m div 10;
  1684. inc(pc);
  1685. pc^:=char(m-(m1*10)+byte('0'));
  1686. m:=m1;
  1687. until m=0;
  1688. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1689. if overflow>0 then
  1690. inc(pcstart,overflow);
  1691. while (pc>pcstart) do
  1692. begin
  1693. pc2^:=pc^;
  1694. inc(pc2);
  1695. dec(pc);
  1696. end;
  1697. s[0]:=char(pc2-pc2start);
  1698. end;
  1699. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1700. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1701. procedure int_str(l:qword;out s:string);
  1702. var
  1703. m1 : qword;
  1704. pcstart,
  1705. pc2start,
  1706. pc,pc2 : pchar;
  1707. hs : string[64];
  1708. overflow : longint;
  1709. begin
  1710. pc2start:=@s[1];
  1711. pc2:=pc2start;
  1712. pcstart:=pchar(@hs[0]);
  1713. pc:=pcstart;
  1714. repeat
  1715. inc(pc);
  1716. m1:=l div 10;
  1717. pc^:=char(l-(m1*10)+byte('0'));
  1718. l:=m1;
  1719. until l=0;
  1720. overflow:=(pc-pcstart)-high(s);
  1721. if overflow>0 then
  1722. inc(pcstart,overflow);
  1723. while (pc>pcstart) do
  1724. begin
  1725. pc2^:=pc^;
  1726. inc(pc2);
  1727. dec(pc);
  1728. end;
  1729. s[0]:=char(pc2-pc2start);
  1730. end;
  1731. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1732. {$ifndef FPUNONE}
  1733. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1734. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1735. begin
  1736. softfloat_exception_flags:=0;
  1737. end;
  1738. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1739. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1740. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1741. begin
  1742. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1743. end;
  1744. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1745. {$endif}
  1746. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1747. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1748. begin
  1749. { the extra Word type cast is necessary because the "AValue shr 8" }
  1750. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1751. { the sign bits from the upper 16 bits are shifted in rather than }
  1752. { zeroes. }
  1753. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1754. end;
  1755. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1756. begin
  1757. Result := Word((AValue shr 8) or (AValue shl 8));
  1758. end;
  1759. function SwapEndian(const AValue: LongInt): LongInt;
  1760. begin
  1761. Result := (AValue shl 24)
  1762. or ((AValue and $0000FF00) shl 8)
  1763. or ((AValue and $00FF0000) shr 8)
  1764. or (AValue shr 24);
  1765. end;
  1766. function SwapEndian(const AValue: DWord): DWord;
  1767. begin
  1768. Result := (AValue shl 24)
  1769. or ((AValue and $0000FF00) shl 8)
  1770. or ((AValue and $00FF0000) shr 8)
  1771. or (AValue shr 24);
  1772. end;
  1773. function SwapEndian(const AValue: Int64): Int64;
  1774. begin
  1775. Result := (AValue shl 56)
  1776. or ((AValue and $000000000000FF00) shl 40)
  1777. or ((AValue and $0000000000FF0000) shl 24)
  1778. or ((AValue and $00000000FF000000) shl 8)
  1779. or ((AValue and $000000FF00000000) shr 8)
  1780. or ((AValue and $0000FF0000000000) shr 24)
  1781. or ((AValue and $00FF000000000000) shr 40)
  1782. or (AValue shr 56);
  1783. end;
  1784. function SwapEndian(const AValue: QWord): QWord;
  1785. begin
  1786. Result := (AValue shl 56)
  1787. or ((AValue and $000000000000FF00) shl 40)
  1788. or ((AValue and $0000000000FF0000) shl 24)
  1789. or ((AValue and $00000000FF000000) shl 8)
  1790. or ((AValue and $000000FF00000000) shr 8)
  1791. or ((AValue and $0000FF0000000000) shr 24)
  1792. or ((AValue and $00FF000000000000) shr 40)
  1793. or (AValue shr 56);
  1794. end;
  1795. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1796. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1797. begin
  1798. {$IFDEF ENDIAN_BIG}
  1799. Result := AValue;
  1800. {$ELSE}
  1801. Result := SwapEndian(AValue);
  1802. {$ENDIF}
  1803. end;
  1804. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1805. begin
  1806. {$IFDEF ENDIAN_BIG}
  1807. Result := AValue;
  1808. {$ELSE}
  1809. Result := SwapEndian(AValue);
  1810. {$ENDIF}
  1811. end;
  1812. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1813. begin
  1814. {$IFDEF ENDIAN_BIG}
  1815. Result := AValue;
  1816. {$ELSE}
  1817. Result := SwapEndian(AValue);
  1818. {$ENDIF}
  1819. end;
  1820. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1821. begin
  1822. {$IFDEF ENDIAN_BIG}
  1823. Result := AValue;
  1824. {$ELSE}
  1825. Result := SwapEndian(AValue);
  1826. {$ENDIF}
  1827. end;
  1828. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1829. begin
  1830. {$IFDEF ENDIAN_BIG}
  1831. Result := AValue;
  1832. {$ELSE}
  1833. Result := SwapEndian(AValue);
  1834. {$ENDIF}
  1835. end;
  1836. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1837. begin
  1838. {$IFDEF ENDIAN_BIG}
  1839. Result := AValue;
  1840. {$ELSE}
  1841. Result := SwapEndian(AValue);
  1842. {$ENDIF}
  1843. end;
  1844. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1845. begin
  1846. {$IFDEF ENDIAN_LITTLE}
  1847. Result := AValue;
  1848. {$ELSE}
  1849. Result := SwapEndian(AValue);
  1850. {$ENDIF}
  1851. end;
  1852. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1853. begin
  1854. {$IFDEF ENDIAN_LITTLE}
  1855. Result := AValue;
  1856. {$ELSE}
  1857. Result := SwapEndian(AValue);
  1858. {$ENDIF}
  1859. end;
  1860. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1861. begin
  1862. {$IFDEF ENDIAN_LITTLE}
  1863. Result := AValue;
  1864. {$ELSE}
  1865. Result := SwapEndian(AValue);
  1866. {$ENDIF}
  1867. end;
  1868. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1869. begin
  1870. {$IFDEF ENDIAN_LITTLE}
  1871. Result := AValue;
  1872. {$ELSE}
  1873. Result := SwapEndian(AValue);
  1874. {$ENDIF}
  1875. end;
  1876. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1877. begin
  1878. {$IFDEF ENDIAN_LITTLE}
  1879. Result := AValue;
  1880. {$ELSE}
  1881. Result := SwapEndian(AValue);
  1882. {$ENDIF}
  1883. end;
  1884. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1885. begin
  1886. {$IFDEF ENDIAN_LITTLE}
  1887. Result := AValue;
  1888. {$ELSE}
  1889. Result := SwapEndian(AValue);
  1890. {$ENDIF}
  1891. end;
  1892. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1893. begin
  1894. {$IFDEF ENDIAN_BIG}
  1895. Result := AValue;
  1896. {$ELSE}
  1897. Result := SwapEndian(AValue);
  1898. {$ENDIF}
  1899. end;
  1900. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1901. begin
  1902. {$IFDEF ENDIAN_BIG}
  1903. Result := AValue;
  1904. {$ELSE}
  1905. Result := SwapEndian(AValue);
  1906. {$ENDIF}
  1907. end;
  1908. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1909. begin
  1910. {$IFDEF ENDIAN_BIG}
  1911. Result := AValue;
  1912. {$ELSE}
  1913. Result := SwapEndian(AValue);
  1914. {$ENDIF}
  1915. end;
  1916. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1917. begin
  1918. {$IFDEF ENDIAN_BIG}
  1919. Result := AValue;
  1920. {$ELSE}
  1921. Result := SwapEndian(AValue);
  1922. {$ENDIF}
  1923. end;
  1924. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1925. begin
  1926. {$IFDEF ENDIAN_BIG}
  1927. Result := AValue;
  1928. {$ELSE}
  1929. Result := SwapEndian(AValue);
  1930. {$ENDIF}
  1931. end;
  1932. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1933. begin
  1934. {$IFDEF ENDIAN_BIG}
  1935. Result := AValue;
  1936. {$ELSE}
  1937. Result := SwapEndian(AValue);
  1938. {$ENDIF}
  1939. end;
  1940. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1941. begin
  1942. {$IFDEF ENDIAN_LITTLE}
  1943. Result := AValue;
  1944. {$ELSE}
  1945. Result := SwapEndian(AValue);
  1946. {$ENDIF}
  1947. end;
  1948. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1949. begin
  1950. {$IFDEF ENDIAN_LITTLE}
  1951. Result := AValue;
  1952. {$ELSE}
  1953. Result := SwapEndian(AValue);
  1954. {$ENDIF}
  1955. end;
  1956. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1957. begin
  1958. {$IFDEF ENDIAN_LITTLE}
  1959. Result := AValue;
  1960. {$ELSE}
  1961. Result := SwapEndian(AValue);
  1962. {$ENDIF}
  1963. end;
  1964. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1965. begin
  1966. {$IFDEF ENDIAN_LITTLE}
  1967. Result := AValue;
  1968. {$ELSE}
  1969. Result := SwapEndian(AValue);
  1970. {$ENDIF}
  1971. end;
  1972. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1973. begin
  1974. {$IFDEF ENDIAN_LITTLE}
  1975. Result := AValue;
  1976. {$ELSE}
  1977. Result := SwapEndian(AValue);
  1978. {$ENDIF}
  1979. end;
  1980. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1981. begin
  1982. {$IFDEF ENDIAN_LITTLE}
  1983. Result := AValue;
  1984. {$ELSE}
  1985. Result := SwapEndian(AValue);
  1986. {$ENDIF}
  1987. end;
  1988. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1989. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1990. begin
  1991. end;
  1992. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1993. begin
  1994. end;
  1995. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1996. begin
  1997. end;
  1998. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1999. begin
  2000. end;
  2001. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2002. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2003. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2004. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2005. begin
  2006. Result:=(AValue shr 1) or (AValue shl 7);
  2007. end;
  2008. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2009. begin
  2010. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2011. end;
  2012. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2013. begin
  2014. Result:=(AValue shl 1) or (AValue shr 7);
  2015. end;
  2016. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2017. begin
  2018. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2019. end;
  2020. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2021. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2022. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2023. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2024. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2025. begin
  2026. Result:=(AValue shr 1) or (AValue shl 15);
  2027. end;
  2028. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2029. begin
  2030. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2031. end;
  2032. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2033. begin
  2034. Result:=(AValue shl 1) or (AValue shr 15);
  2035. end;
  2036. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2037. begin
  2038. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2039. end;
  2040. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2041. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2042. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2043. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2044. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2045. begin
  2046. Result:=(AValue shr 1) or (AValue shl 31);
  2047. end;
  2048. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2049. begin
  2050. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2051. end;
  2052. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2053. begin
  2054. Result:=(AValue shl 1) or (AValue shr 31);
  2055. end;
  2056. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2057. begin
  2058. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2059. end;
  2060. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2061. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2062. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2063. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2064. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2065. begin
  2066. Result:=(AValue shr 1) or (AValue shl 63);
  2067. end;
  2068. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2069. begin
  2070. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2071. end;
  2072. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2073. begin
  2074. Result:=(AValue shl 1) or (AValue shr 63);
  2075. end;
  2076. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2077. begin
  2078. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2079. end;
  2080. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2081. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2082. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2083. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2084. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2085. begin
  2086. 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)))));
  2087. end;
  2088. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2089. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2090. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2091. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2092. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2093. begin
  2094. 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)))));
  2095. end;
  2096. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2097. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2098. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2099. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2100. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2101. begin
  2102. 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)))));
  2103. end;
  2104. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2105. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2106. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2107. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2108. function SarInt64(Const AValue : Int64;const Shift : Byte): Int64;
  2109. begin
  2110. 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)))));
  2111. end;
  2112. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2113. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2114. {$ifndef FPC_HAS_INTERNAL_BSX_BYTE}
  2115. {$ifndef FPC_SYSTEM_HAS_BSX_BYTE}
  2116. function BsfByte(Const AValue: Byte): Byte;
  2117. const bsf8bit: array [Byte] of Byte = (
  2118. $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,
  2119. 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,
  2120. 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,
  2121. 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,
  2122. 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,
  2123. 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,
  2124. 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,
  2125. 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
  2126. );
  2127. begin
  2128. result:=bsf8bit[AValue];
  2129. end;
  2130. function BsrByte(Const AValue: Byte): Byte;
  2131. const bsr8bit: array [Byte] of Byte = (
  2132. $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,
  2133. 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,
  2134. 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,
  2135. 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,
  2136. 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,
  2137. 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,
  2138. 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,
  2139. 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
  2140. );
  2141. begin
  2142. result:=bsr8bit[AValue];
  2143. end;
  2144. {$endif}
  2145. {$endif}
  2146. {$ifndef FPC_SYSTEM_HAS_BSX_WORD}
  2147. {$ifndef FPC_HAS_INTERNAL_BSX_WORD}
  2148. function BsfWord(Const AValue: Word): cardinal;
  2149. begin
  2150. if lo(AValue)<>0 then
  2151. result:=BsfByte(lo(AValue))
  2152. else
  2153. result:=BsfByte(hi(AValue))+8
  2154. end;
  2155. function BsrWord(Const AValue: Word): cardinal;
  2156. begin
  2157. if hi(AValue)<>0 then
  2158. result:=BsrByte(hi(AValue))+8
  2159. else
  2160. result:=BsrByte(lo(AValue))
  2161. end;
  2162. {$endif}
  2163. {$endif}
  2164. {$ifndef FPC_HAS_INTERNAL_BSX_DWORD}
  2165. {$ifndef FPC_SYSTEM_HAS_BSX_DWORD}
  2166. function BsfDWord(Const AValue : DWord): cardinal;
  2167. begin
  2168. if lo(AValue)<>0 then
  2169. result:=BsfWord(lo(AValue))
  2170. else
  2171. result:=BsfWord(hi(AValue))+16
  2172. end;
  2173. function BsrDWord(Const AValue : DWord): cardinal;
  2174. begin
  2175. if hi(AValue)<>0 then
  2176. result:=BsrWord(hi(AValue))+16
  2177. else
  2178. result:=BsrWord(lo(AValue))
  2179. end;
  2180. {$endif}
  2181. {$endif}
  2182. {$ifndef FPC_HAS_INTERNAL_BSX_QWORD}
  2183. {$ifndef FPC_SYSTEM_HAS_BSX_QWORD}
  2184. function BsfQWord(Const AValue : QWord): cardinal;
  2185. begin
  2186. if lo(AValue) <> 0 then
  2187. result:=BsfDWord(lo(AValue))
  2188. else
  2189. result:=BsfDWord(hi(AValue)) + 32
  2190. end;
  2191. function BsrQWord(Const AValue : QWord): cardinal;
  2192. begin
  2193. if hi(AValue) <> 0 then
  2194. result:=BsrDWord(hi(AValue)) + 32
  2195. else
  2196. result:=BsrDWord(lo(AValue))
  2197. end;
  2198. {$endif}
  2199. {$endif}