generic.inc 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Processor independent implementation for the system unit
  5. (adapted for intel i386.inc file)
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {****************************************************************************
  13. Primitives
  14. ****************************************************************************}
  15. type
  16. pstring = ^shortstring;
  17. {$ifndef FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  18. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  19. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  20. begin
  21. move(src[srcindex],dst[dstindex],len);
  22. end;
  23. {$endif FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  24. {$ifndef FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  25. {$define FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  26. procedure fpc_shortstr_chararray_intern_charmove(const src: shortstring; out dst: array of char; const len: sizeint);
  27. begin
  28. move(src[1],pchar(@dst)^,len);
  29. end;
  30. {$endif FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  31. {$ifndef FPC_SYSTEM_HAS_MOVE}
  32. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  33. var
  34. aligncount : sizeint;
  35. pdest,psrc,pend : pbyte;
  36. begin
  37. if (@dest=@source) or (count<=0) then
  38. exit;
  39. if (@dest<@source) or (@source+count<@dest) then
  40. begin
  41. { Forward Move }
  42. psrc:=@source;
  43. pdest:=@dest;
  44. if (Count>4*sizeof(ptruint)-11)
  45. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  46. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  47. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  48. then
  49. begin
  50. { Align on native pointer size }
  51. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  52. dec(count,aligncount);
  53. pend:=psrc+aligncount;
  54. while psrc<pend do
  55. begin
  56. pdest^:=psrc^;
  57. inc(pdest);
  58. inc(psrc);
  59. end;
  60. { use sizeuint typecast to force shr optimization }
  61. pptruint(pend):=pptruint(psrc)+(sizeuint(count) div sizeof(ptruint));
  62. while psrc<pend do
  63. begin
  64. pptruint(pdest)^:=pptruint(psrc)^;
  65. inc(pptruint(pdest));
  66. inc(pptruint(psrc));
  67. end;
  68. count:=count and (sizeof(PtrUInt)-1);
  69. end;
  70. pend:=psrc+count;
  71. while psrc<pend do
  72. begin
  73. pdest^:=psrc^;
  74. inc(pdest);
  75. inc(psrc);
  76. end;
  77. end
  78. else
  79. begin
  80. { Backward Move }
  81. psrc:=@source+count;
  82. pdest:=@dest+count;
  83. if (Count>4*sizeof(ptruint)-11)
  84. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  85. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  86. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  87. then
  88. begin
  89. { Align on native pointer size }
  90. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  91. dec(count,aligncount);
  92. pend:=psrc-aligncount;
  93. while psrc>pend do
  94. begin
  95. dec(pdest);
  96. dec(psrc);
  97. pdest^:=psrc^;
  98. end;
  99. { use sizeuint typecast to force shr optimization }
  100. pptruint(pend):=pptruint(psrc)-(sizeuint(count) div sizeof(ptruint));
  101. while psrc>pend do
  102. begin
  103. dec(pptruint(pdest));
  104. dec(pptruint(psrc));
  105. pptruint(pdest)^:=pptruint(psrc)^;
  106. end;
  107. count:=count and (sizeof(PtrUInt)-1);
  108. end;
  109. pend:=psrc-count;
  110. while psrc>pend do
  111. begin
  112. dec(pdest);
  113. dec(psrc);
  114. pdest^:=psrc^;
  115. end;
  116. end;
  117. end;
  118. {$endif not FPC_SYSTEM_HAS_MOVE}
  119. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  120. Procedure FillChar(var x;count:SizeInt;value:byte);
  121. var
  122. pdest,pend : pbyte;
  123. v : 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)-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)-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 (psrc+len >= psrc) then
  521. pend:=psrc+len
  522. else
  523. pend:=pword(high(ptruint)-2);
  524. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  525. if ((PtrUInt(pdest) and 1) or (PtrUInt(psrc) and 1))<>0 then
  526. while psrc<pend do
  527. begin
  528. b:=(ptrint(unaligned(psrc^))-ptrint(unaligned(pdest^)));
  529. if b<>0 then
  530. begin
  531. if b<0 then
  532. exit(-1)
  533. else
  534. exit(1);
  535. end;
  536. inc(pdest);
  537. inc(psrc);
  538. end
  539. else
  540. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  541. while psrc<pend do
  542. begin
  543. b:=(ptrint(psrc^)-ptrint(pdest^));
  544. if b<>0 then
  545. begin
  546. if b<0 then
  547. exit(-1)
  548. else
  549. exit(1);
  550. end;
  551. inc(pdest);
  552. inc(psrc);
  553. end;
  554. result:=0;
  555. end;
  556. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  557. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  558. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  559. var
  560. aligncount : sizeint;
  561. psrc,pdest,pend : pdword;
  562. begin
  563. psrc:=@buf1;
  564. pdest:=@buf2;
  565. if (len>4*sizeof(ptruint)-11)
  566. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  567. and (((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))=0)
  568. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  569. then
  570. begin
  571. { Align on native pointer size }
  572. aligncount:=((sizeof(PtrUInt)-(PtrUInt(pdest) and (sizeof(PtrUInt)-1))) and (sizeof(PtrUInt)-1)) shr 2;
  573. dec(len,aligncount);
  574. pend:=psrc+aligncount;
  575. while psrc<pend do
  576. begin
  577. if psrc^<>pdest^ then
  578. if psrc^>pdest^ then
  579. exit(1)
  580. else
  581. exit(-1);
  582. inc(pdest);
  583. inc(psrc);
  584. end;
  585. { use sizeuint typecast to force shr optimization }
  586. pptruint(pend):=pptruint(psrc)+(sizeuint(len)*4 div sizeof(ptruint));
  587. len:=((len*4) and (sizeof(PtrUInt)-1)) shr 2;
  588. while psrc<pend do
  589. begin
  590. if pptrint(psrc)^<>pptrint(pdest)^ then
  591. begin
  592. len:=sizeof(ptruint) shr 2;
  593. break;
  594. end;
  595. inc(pptruint(pdest));
  596. inc(pptruint(psrc));
  597. end;
  598. end;
  599. if (len <= high(ptrint) div 2) and
  600. (psrc+len >= psrc) then
  601. pend:=psrc+len
  602. else
  603. pend:=pdword(high(ptruint)-4);
  604. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  605. if ((PtrUInt(pdest) and 3) or (PtrUInt(psrc) and 3))<>0 then
  606. while psrc<pend do
  607. begin
  608. if unaligned(psrc^)<>unaligned(pdest^) then
  609. if unaligned(psrc^)>unaligned(pdest^) then
  610. exit(1)
  611. else
  612. exit(-1);
  613. inc(pdest);
  614. inc(psrc);
  615. end
  616. else
  617. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  618. while psrc<pend do
  619. begin
  620. if psrc^<>pdest^ then
  621. if psrc^>pdest^ then
  622. exit(1)
  623. else
  624. exit(-1);
  625. inc(pdest);
  626. inc(psrc);
  627. end;
  628. result:=0;
  629. end;
  630. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  631. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  632. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  633. var
  634. I : SizeInt;
  635. begin
  636. if Len = 0 then
  637. exit;
  638. I:=IndexByte(Buf1,Len,0);
  639. if I<>-1 then
  640. Move(Buf1,Buf2,I)
  641. else
  642. Move(Buf1,Buf2,len);
  643. end;
  644. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  645. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  646. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  647. var
  648. psrc,pend : pbyte;
  649. begin
  650. psrc:=@buf;
  651. { simulate assembler implementations behaviour, which is expected }
  652. { fpc_pchar_to_ansistr in astrings.inc }
  653. if (len < 0) then
  654. pend:=pbyte(high(PtrUInt)-PtrUInt(sizeof(byte)))
  655. else
  656. pend:=psrc+len;
  657. while (psrc<pend) and (psrc^<>0) do
  658. begin
  659. if (psrc^=byte(b)) then
  660. begin
  661. result:=psrc-pbyte(@buf);
  662. exit;
  663. end;
  664. inc(psrc);
  665. end;
  666. result:=-1;
  667. end;
  668. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  669. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  670. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  671. var
  672. psrc,pdest,pend : pbyte;
  673. b : ptrint;
  674. begin
  675. b:=0;
  676. psrc:=@buf1;
  677. pdest:=@buf2;
  678. pend:=psrc+len;
  679. while psrc<pend do
  680. begin
  681. b:=(ptrint(psrc^)-ptrint(pdest^));
  682. if b<0 then
  683. exit(-1)
  684. else if b>0 then
  685. exit(1);
  686. if (psrc^=0) or (pdest^=0) then
  687. exit(0);
  688. inc(pdest);
  689. inc(psrc);
  690. end;
  691. result:=0;
  692. end;
  693. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  694. {****************************************************************************
  695. Object Helpers
  696. ****************************************************************************}
  697. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  698. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  699. { Note: _vmt will be reset to -1 when memory is allocated,
  700. this is needed for fpc_help_fail }
  701. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  702. type
  703. ppointer = ^pointer;
  704. pvmt = ^tvmt;
  705. tvmt=packed record
  706. size,msize:ptruint;
  707. parent:pointer;
  708. end;
  709. var
  710. vmtcopy : pointer;
  711. begin
  712. { Inherited call? }
  713. if _vmt=nil then
  714. begin
  715. fpc_help_constructor:=_self;
  716. exit;
  717. end;
  718. vmtcopy:=_vmt;
  719. if (_self=nil) and
  720. (pvmt(_vmt)^.size>0) then
  721. begin
  722. getmem(_self,pvmt(_vmt)^.size);
  723. { reset vmt needed for fail }
  724. _vmt:=pointer(-1);
  725. end;
  726. if _self<>nil then
  727. begin
  728. fillchar(_self^,pvmt(vmtcopy)^.size,0);
  729. ppointer(_self+_vmt_pos)^:=vmtcopy;
  730. end;
  731. fpc_help_constructor:=_self;
  732. end;
  733. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  734. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  735. { Note: _self will not be reset, the compiler has to generate the reset }
  736. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  737. type
  738. ppointer = ^pointer;
  739. pvmt = ^tvmt;
  740. tvmt = packed record
  741. size,msize : ptruint;
  742. parent : pointer;
  743. end;
  744. begin
  745. { already released? }
  746. if (_self=nil) or
  747. (_vmt=nil) or
  748. (ppointer(_self+vmt_pos)^=nil) then
  749. exit;
  750. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  751. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  752. RunError(210);
  753. { reset vmt to nil for protection }
  754. ppointer(_self+vmt_pos)^:=nil;
  755. freemem(_self);
  756. end;
  757. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  758. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  759. { Note: _self will not be reset, the compiler has to generate the reset }
  760. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  761. begin
  762. if (_self=nil) or (_vmt=nil) then
  763. exit;
  764. { vmt=$ffffffff when memory was allocated }
  765. if ptruint(_vmt)=high(ptruint) then
  766. begin
  767. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  768. HandleError(210)
  769. else
  770. begin
  771. ppointer(_self+vmt_pos)^:=nil;
  772. freemem(_self);
  773. { reset _vmt to nil so it will not be freed a
  774. second time }
  775. _vmt:=nil;
  776. end;
  777. end
  778. else
  779. ppointer(_self+vmt_pos)^:=nil;
  780. end;
  781. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  782. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  783. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  784. type
  785. pvmt = ^tvmt;
  786. tvmt = packed record
  787. size,msize : ptruint;
  788. parent : pointer;
  789. end;
  790. begin
  791. if (_vmt=nil) or
  792. (pvmt(_vmt)^.size=0) or
  793. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  794. RunError(210);
  795. end;
  796. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  797. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  798. { checks for a correct vmt pointer }
  799. { deeper check to see if the current object is }
  800. { really related to the true }
  801. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  802. type
  803. pvmt = ^tvmt;
  804. tvmt = packed record
  805. size,msize : ptruint;
  806. parent : pointer;
  807. end;
  808. begin
  809. if (vmt=nil) or
  810. (pvmt(vmt)^.size=0) or
  811. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  812. RunError(210);
  813. while assigned(vmt) do
  814. if vmt=expvmt then
  815. exit
  816. else
  817. vmt:=pvmt(vmt)^.parent;
  818. RunError(219);
  819. end;
  820. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  821. {$endif FPC_HAS_FEATURE_OBJECTS}
  822. {****************************************************************************
  823. String
  824. ****************************************************************************}
  825. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  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. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  837. var
  838. slen : byte;
  839. begin
  840. slen:=length(pshortstring(sstr)^);
  841. if slen<len then
  842. len:=slen;
  843. move(sstr^,dstr^,len+1);
  844. if slen>len then
  845. pchar(dstr)^:=chr(len);
  846. end;
  847. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  848. {$push}
  849. { ensure that comparing addresses of openshortstrings with regular shortstrings
  850. doesn't cause errors }
  851. {$t-}
  852. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  853. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  854. var
  855. s1l, s2l : longint;
  856. begin
  857. s1l:=length(s1);
  858. s2l:=length(s2);
  859. if s1l+s2l>high(dests) then
  860. begin
  861. if s1l>high(dests) then
  862. s1l:=high(dests);
  863. s2l:=high(dests)-s1l;
  864. end;
  865. if @dests=@s1 then
  866. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l)
  867. else
  868. if @dests=@s2 then
  869. begin
  870. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,s1l+1,s2l);
  871. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  872. end
  873. else
  874. begin
  875. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  876. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  877. end;
  878. dests[0]:=chr(s1l+s2l);
  879. end;
  880. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  881. var
  882. s2l : byte;
  883. LowStart,i,
  884. Len : longint;
  885. needtemp : boolean;
  886. tmpstr : shortstring;
  887. p,pdest : pshortstring;
  888. begin
  889. if high(sarr)=0 then
  890. begin
  891. DestS:='';
  892. exit;
  893. end;
  894. lowstart:=low(sarr);
  895. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  896. inc(lowstart);
  897. { Check for another reuse, then we can't use
  898. the append optimization and need to use a temp }
  899. needtemp:=false;
  900. for i:=lowstart to high(sarr) do
  901. begin
  902. if Pointer(@DestS)=Pointer(sarr[i]) then
  903. begin
  904. needtemp:=true;
  905. break;
  906. end;
  907. end;
  908. if needtemp then
  909. begin
  910. lowstart:=low(sarr);
  911. tmpstr:='';
  912. pdest:=@tmpstr
  913. end
  914. else
  915. begin
  916. { Start with empty DestS if we start with concatting
  917. the first array element }
  918. if lowstart=low(sarr) then
  919. DestS:='';
  920. pdest:=@DestS;
  921. end;
  922. { Concat all strings, except the string we already
  923. copied in DestS }
  924. Len:=length(pdest^);
  925. for i:=lowstart to high(sarr) do
  926. begin
  927. p:=sarr[i];
  928. if assigned(p) then
  929. begin
  930. s2l:=length(p^);
  931. if Len+s2l>high(dests) then
  932. s2l:=high(dests)-Len;
  933. fpc_shortstr_shortstr_intern_charmove(p^,1,pdest^,Len+1,s2l);
  934. inc(Len,s2l);
  935. end;
  936. end;
  937. pdest^[0]:=Chr(Len);
  938. if needtemp then
  939. DestS:=TmpStr;
  940. end;
  941. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  942. {$pop}
  943. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  944. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  945. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  946. var
  947. s1l, s2l : sizeint;
  948. begin
  949. s1l:=length(s1);
  950. s2l:=length(s2);
  951. if s1l+s2l>high(s1) then
  952. s2l:=high(s1)-s1l;
  953. move(s2[1],s1[s1l+1],s2l);
  954. s1[0]:=chr(s1l+s2l);
  955. end;
  956. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  957. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  958. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  959. var
  960. s1,s2,max,i : byte;
  961. d : longint;
  962. begin
  963. s1:=length(left);
  964. s2:=length(right);
  965. if s1<s2 then
  966. max:=s1
  967. else
  968. max:=s2;
  969. for i:=1 to max do
  970. begin
  971. d:=byte(left[i])-byte(right[i]);
  972. if d>0 then
  973. exit(1)
  974. else if d<0 then
  975. exit(-1);
  976. end;
  977. if s1>s2 then
  978. exit(1)
  979. else if s1<s2 then
  980. exit(-1)
  981. else
  982. exit(0);
  983. end;
  984. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  985. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  986. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  987. begin
  988. Result := longint(left[0]) - longint(right[0]);
  989. if Result = 0 then
  990. Result := CompareByte(left[1],right[1], longint(left[0]));
  991. end;
  992. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  993. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  994. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  995. var
  996. l : longint;
  997. s: shortstring;
  998. begin
  999. if p=nil then
  1000. l:=0
  1001. else
  1002. l:=strlen(p);
  1003. if l>high(res) then
  1004. l:=high(res);
  1005. if l>0 then
  1006. move(p^,s[1],l);
  1007. s[0]:=chr(l);
  1008. res:=s;
  1009. end;
  1010. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  1011. {$ifndef cpujvm}
  1012. { also define alias which can be used inside the system unit }
  1013. procedure fpc_pchar_to_shortstr(out res : shortstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  1014. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  1015. begin
  1016. fpc_pchar_to_shortstr(result,p);
  1017. end;
  1018. {$endif not cpujvm}
  1019. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1020. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of char; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1021. var
  1022. l: longint;
  1023. index: longint;
  1024. len: byte;
  1025. begin
  1026. l:=high(arr)+1;
  1027. if l>=high(res)+1 then
  1028. l:=high(res)
  1029. else if l<0 then
  1030. l:=0;
  1031. if zerobased then
  1032. begin
  1033. index:=IndexByte(arr[0],l,0);
  1034. if index<0 then
  1035. len:=l
  1036. else
  1037. len:=index;
  1038. end
  1039. else
  1040. len:=l;
  1041. move(arr[0],res[1],len);
  1042. res[0]:=chr(len);
  1043. end;
  1044. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1045. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1046. procedure fpc_shortstr_to_chararray(out res: array of char; const src: ShortString); compilerproc;
  1047. var
  1048. len: longint;
  1049. begin
  1050. len := length(src);
  1051. if len > length(res) then
  1052. len := length(res);
  1053. {$push}{$r-}
  1054. { make sure we don't access char 1 if length is 0 (JM) }
  1055. if len > 0 then
  1056. move(src[1],res[0],len);
  1057. fillchar(res[len],length(res)-len,0);
  1058. {$pop}
  1059. end;
  1060. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1061. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1062. function fpc_pchar_length(p:pchar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1063. var i : sizeint;
  1064. begin
  1065. i:=0;
  1066. if assigned(p) then
  1067. while p[i]<>#0 do
  1068. inc(i);
  1069. exit(i);
  1070. end;
  1071. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1072. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1073. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1074. var i : sizeint;
  1075. begin
  1076. i:=0;
  1077. if assigned(p) then
  1078. while p[i]<>#0 do
  1079. inc(i);
  1080. exit(i);
  1081. end;
  1082. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1083. {****************************************************************************
  1084. Caller/StackFrame Helpers
  1085. ****************************************************************************}
  1086. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1087. {_$error Get_frame must be defined for each processor }
  1088. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1089. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1090. {_$error Get_caller_addr must be defined for each processor }
  1091. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1092. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1093. {_$error Get_caller_frame must be defined for each processor }
  1094. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1095. {****************************************************************************
  1096. Math
  1097. ****************************************************************************}
  1098. {****************************************************************************
  1099. Software multiplication
  1100. ****************************************************************************}
  1101. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1102. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1103. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1104. var
  1105. sign : boolean;
  1106. q1,q2,q3 : word;
  1107. begin
  1108. begin
  1109. sign:=false;
  1110. if f1<0 then
  1111. begin
  1112. sign:=not(sign);
  1113. q1:=word(-f1);
  1114. end
  1115. else
  1116. q1:=f1;
  1117. if f2<0 then
  1118. begin
  1119. sign:=not(sign);
  1120. q2:=word(-f2);
  1121. end
  1122. else
  1123. q2:=f2;
  1124. { the q1*q2 is coded as call to mulqword }
  1125. q3:=q1*q2;
  1126. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1127. ((q1>q3) or (q2>q3) or
  1128. { the bit 63 can be only set if we have $8000 }
  1129. { and sign is true }
  1130. (q3 shr 15<>0) and
  1131. ((q3<>word(word(1) shl 15)) or not(sign))
  1132. ) then
  1133. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1134. if sign then
  1135. fpc_mul_integer:=-q3
  1136. else
  1137. fpc_mul_integer:=q3;
  1138. end;
  1139. end;
  1140. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1141. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1142. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1143. var
  1144. _f1,bitpos : word;
  1145. b : byte;
  1146. f1overflowed : boolean;
  1147. begin
  1148. fpc_mul_word:=0;
  1149. bitpos:=1;
  1150. f1overflowed:=false;
  1151. for b:=0 to 15 do
  1152. begin
  1153. if (f2 and bitpos)<>0 then
  1154. begin
  1155. _f1:=fpc_mul_word;
  1156. fpc_mul_word:=fpc_mul_word+f1;
  1157. { if one of the operands is greater than the result an
  1158. overflow occurs }
  1159. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1160. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1161. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1162. end;
  1163. { when bootstrapping, we forget about overflow checking for qword :) }
  1164. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1165. f1:=f1 shl 1;
  1166. bitpos:=bitpos shl 1;
  1167. end;
  1168. end;
  1169. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1170. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1171. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1172. var
  1173. sign : boolean;
  1174. q1,q2,q3 : dword;
  1175. begin
  1176. begin
  1177. sign:=false;
  1178. if f1<0 then
  1179. begin
  1180. sign:=not(sign);
  1181. q1:=dword(-f1);
  1182. end
  1183. else
  1184. q1:=f1;
  1185. if f2<0 then
  1186. begin
  1187. sign:=not(sign);
  1188. q2:=dword(-f2);
  1189. end
  1190. else
  1191. q2:=f2;
  1192. { the q1*q2 is coded as call to mulqword }
  1193. q3:=q1*q2;
  1194. if checkoverflow and (q1 <> 0) and (q2 <>0) and
  1195. ((q1>q3) or (q2>q3) or
  1196. { the bit 31 can be only set if we have $8000 0000 }
  1197. { and sign is true }
  1198. (q3 shr 15<>0) and
  1199. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1200. ) then
  1201. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1202. if sign then
  1203. fpc_mul_longint:=-q3
  1204. else
  1205. fpc_mul_longint:=q3;
  1206. end;
  1207. end;
  1208. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1209. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1210. { multiplies two dwords
  1211. the longbool for checkoverflow avoids a misaligned stack
  1212. }
  1213. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1214. var
  1215. _f1,bitpos : dword;
  1216. b : byte;
  1217. f1overflowed : boolean;
  1218. begin
  1219. fpc_mul_dword:=0;
  1220. bitpos:=1;
  1221. f1overflowed:=false;
  1222. for b:=0 to 31 do
  1223. begin
  1224. if (f2 and bitpos)<>0 then
  1225. begin
  1226. _f1:=fpc_mul_dword;
  1227. fpc_mul_dword:=fpc_mul_dword+f1;
  1228. { if one of the operands is greater than the result an
  1229. overflow occurs }
  1230. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1231. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1232. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1233. end;
  1234. { when bootstrapping, we forget about overflow checking for qword :) }
  1235. f1overflowed:=f1overflowed or ((f1 and (1 shl 31))<>0);
  1236. f1:=f1 shl 1;
  1237. bitpos:=bitpos shl 1;
  1238. end;
  1239. end;
  1240. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1241. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1242. {****************************************************************************
  1243. Software longint/dword division
  1244. ****************************************************************************}
  1245. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1246. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1247. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1248. var
  1249. shift,lzz,lzn : longint;
  1250. begin
  1251. result:=0;
  1252. if n=0 then
  1253. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1254. if z=0 then
  1255. exit;
  1256. lzz:=BsrDWord(z);
  1257. lzn:=BsrDWord(n);
  1258. { if the denominator contains less zeros
  1259. then the numerator
  1260. then d is greater than the n }
  1261. if lzn>lzz then
  1262. exit;
  1263. shift:=lzz-lzn;
  1264. n:=n shl shift;
  1265. for shift:=shift downto 0 do
  1266. begin
  1267. if z>=n then
  1268. begin
  1269. z:=z-n;
  1270. result:=result+dword(1 shl shift);
  1271. end;
  1272. n:=n shr 1;
  1273. end;
  1274. end;
  1275. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1276. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1277. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1278. var
  1279. shift,lzz,lzn : longint;
  1280. begin
  1281. result:=0;
  1282. if n=0 then
  1283. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1284. if z=0 then
  1285. exit;
  1286. lzz:=BsrDWord(z);
  1287. lzn:=BsrDWord(n);
  1288. { if the denominator contains less zeros
  1289. then the numerator
  1290. then d is greater than the n }
  1291. if lzn>lzz then
  1292. begin
  1293. result:=z;
  1294. exit;
  1295. end;
  1296. shift:=lzz-lzn;
  1297. n:=n shl shift;
  1298. for shift:=shift downto 0 do
  1299. begin
  1300. if z>=n then
  1301. z:=z-n;
  1302. n:=n shr 1;
  1303. end;
  1304. result:=z;
  1305. end;
  1306. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1307. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1308. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1309. var
  1310. sign : boolean;
  1311. d1,d2 : dword;
  1312. begin
  1313. if n=0 then
  1314. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1315. sign:=false;
  1316. if z<0 then
  1317. begin
  1318. sign:=not(sign);
  1319. d1:=dword(-z);
  1320. end
  1321. else
  1322. d1:=z;
  1323. if n<0 then
  1324. begin
  1325. sign:=not(sign);
  1326. d2:=dword(-n);
  1327. end
  1328. else
  1329. d2:=n;
  1330. { the div is coded by the compiler as call to divdword }
  1331. if sign then
  1332. result:=-(d1 div d2)
  1333. else
  1334. result:=d1 div d2;
  1335. end;
  1336. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1337. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1338. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1339. var
  1340. signed : boolean;
  1341. r,nq,zq : dword;
  1342. begin
  1343. if n=0 then
  1344. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1345. nq:=abs(n);
  1346. if z<0 then
  1347. begin
  1348. zq:=dword(-z);
  1349. signed:=true;
  1350. end
  1351. else
  1352. begin
  1353. zq:=z;
  1354. signed:=false;
  1355. end;
  1356. r:=zq mod nq;
  1357. if signed then
  1358. result:=-longint(r)
  1359. else
  1360. result:=r;
  1361. end;
  1362. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1363. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1364. {****************************************************************************}
  1365. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  1366. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1367. begin
  1368. if l<0 then
  1369. abs:=-l
  1370. else
  1371. abs:=l;
  1372. end;
  1373. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  1374. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1375. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1376. begin
  1377. odd:=boolean(l and 1);
  1378. end;
  1379. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  1380. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1381. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1382. begin
  1383. odd:=boolean(l and 1);
  1384. end;
  1385. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  1386. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  1387. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1388. begin
  1389. odd:=boolean(longint(l) and 1);
  1390. end;
  1391. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  1392. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  1393. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  1394. begin
  1395. odd:=boolean(longint(l) and 1);
  1396. end;
  1397. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  1398. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1399. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  1400. begin
  1401. sqr:=l*l;
  1402. end;
  1403. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  1404. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  1405. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1406. begin
  1407. if l < 0 then
  1408. abs := -l
  1409. else
  1410. abs := l;
  1411. end;
  1412. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  1413. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  1414. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1415. begin
  1416. sqr := l*l;
  1417. end;
  1418. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1419. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  1420. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1421. begin
  1422. sqr := l*l;
  1423. end;
  1424. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  1425. {$ifdef CPU16}
  1426. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1427. function declocked(var l:smallint):boolean;
  1428. begin
  1429. Dec(l);
  1430. declocked:=(l=0);
  1431. end;
  1432. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  1433. {$endif CPU16}
  1434. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1435. function declocked(var l:longint):boolean;
  1436. begin
  1437. Dec(l);
  1438. declocked:=(l=0);
  1439. end;
  1440. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  1441. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1442. function declocked(var l:int64):boolean;
  1443. begin
  1444. Dec(l);
  1445. declocked:=(l=0);
  1446. end;
  1447. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  1448. {$ifdef CPU16}
  1449. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1450. procedure inclocked(var l:smallint);
  1451. begin
  1452. Inc(l);
  1453. end;
  1454. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  1455. {$endif CPU16}
  1456. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1457. procedure inclocked(var l:longint);
  1458. begin
  1459. Inc(l);
  1460. end;
  1461. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  1462. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1463. procedure inclocked(var l:int64);
  1464. begin
  1465. Inc(l);
  1466. end;
  1467. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  1468. {$ifndef FPC_SYSTEM_HAS_SPTR}
  1469. {_$error Sptr must be defined for each processor }
  1470. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  1471. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1472. var
  1473. tmp: PtrUInt;
  1474. begin
  1475. tmp:=addr+(alignment-1);
  1476. result:=tmp-(tmp mod alignment)
  1477. end;
  1478. {$ifndef cpujvm}
  1479. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  1480. var
  1481. tmp: PtrUInt;
  1482. begin
  1483. tmp:=PtrUInt(addr)+(alignment-1);
  1484. result:=pointer(tmp-(tmp mod alignment));
  1485. end;
  1486. {$endif}
  1487. {****************************************************************************
  1488. Str()
  1489. ****************************************************************************}
  1490. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1491. procedure int_str(l:longint;out s:shortstring);
  1492. var
  1493. m,m1 : longword;
  1494. pcstart,
  1495. pc2start,
  1496. pc,pc2 : pchar;
  1497. hs : string[32];
  1498. overflow : longint;
  1499. begin
  1500. pc2start:=@s[1];
  1501. pc2:=pc2start;
  1502. if (l<0) then
  1503. begin
  1504. pc2^:='-';
  1505. inc(pc2);
  1506. m:=longword(-l);
  1507. end
  1508. else
  1509. m:=longword(l);
  1510. pcstart:=pchar(@hs[0]);
  1511. pc:=pcstart;
  1512. repeat
  1513. m1:=m div 10;
  1514. inc(pc);
  1515. pc^:=char(m-(m1*10)+byte('0'));
  1516. m:=m1;
  1517. until m=0;
  1518. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1519. if overflow>0 then
  1520. inc(pcstart,overflow);
  1521. while (pc>pcstart) do
  1522. begin
  1523. pc2^:=pc^;
  1524. inc(pc2);
  1525. dec(pc);
  1526. end;
  1527. s[0]:=char(pc2-pc2start);
  1528. end;
  1529. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1530. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1531. procedure int_str_unsigned(l:longword;out s:shortstring);
  1532. var
  1533. m1 : longword;
  1534. pcstart,
  1535. pc2start,
  1536. pc,pc2 : pchar;
  1537. hs : string[32];
  1538. overflow : longint;
  1539. begin
  1540. pc2start:=@s[1];
  1541. pc2:=pc2start;
  1542. pcstart:=pchar(@hs[0]);
  1543. pc:=pcstart;
  1544. repeat
  1545. inc(pc);
  1546. m1:=l div 10;
  1547. pc^:=char(l-(m1*10)+byte('0'));
  1548. l:=m1;
  1549. until l=0;
  1550. overflow:=(pc-pcstart)-high(s);
  1551. if overflow>0 then
  1552. inc(pcstart,overflow);
  1553. while (pc>pcstart) do
  1554. begin
  1555. pc2^:=pc^;
  1556. inc(pc2);
  1557. dec(pc);
  1558. end;
  1559. s[0]:=char(pc2-pc2start);
  1560. end;
  1561. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1562. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1563. procedure int_str(l:int64;out s:shortstring);
  1564. var
  1565. m,m1 : qword;
  1566. pcstart,
  1567. pc2start,
  1568. pc,pc2 : pchar;
  1569. hs : string[32];
  1570. overflow : longint;
  1571. begin
  1572. pc2start:=@s[1];
  1573. pc2:=pc2start;
  1574. if (l<0) then
  1575. begin
  1576. pc2^:='-';
  1577. inc(pc2);
  1578. m:=qword(-l);
  1579. end
  1580. else
  1581. m:=qword(l);
  1582. pcstart:=pchar(@hs[0]);
  1583. pc:=pcstart;
  1584. repeat
  1585. m1:=m div 10;
  1586. inc(pc);
  1587. pc^:=char(m-(m1*10)+byte('0'));
  1588. m:=m1;
  1589. until m=0;
  1590. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  1591. if overflow>0 then
  1592. inc(pcstart,overflow);
  1593. while (pc>pcstart) do
  1594. begin
  1595. pc2^:=pc^;
  1596. inc(pc2);
  1597. dec(pc);
  1598. end;
  1599. s[0]:=char(pc2-pc2start);
  1600. end;
  1601. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1602. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1603. procedure int_str_unsigned(l:qword;out s:shortstring);
  1604. var
  1605. m1 : qword;
  1606. pcstart,
  1607. pc2start,
  1608. pc,pc2 : pchar;
  1609. hs : string[64];
  1610. overflow : longint;
  1611. begin
  1612. pc2start:=@s[1];
  1613. pc2:=pc2start;
  1614. pcstart:=pchar(@hs[0]);
  1615. pc:=pcstart;
  1616. repeat
  1617. inc(pc);
  1618. m1:=l div 10;
  1619. pc^:=char(l-(m1*10)+byte('0'));
  1620. l:=m1;
  1621. until l=0;
  1622. overflow:=(pc-pcstart)-high(s);
  1623. if overflow>0 then
  1624. inc(pcstart,overflow);
  1625. while (pc>pcstart) do
  1626. begin
  1627. pc2^:=pc^;
  1628. inc(pc2);
  1629. dec(pc);
  1630. end;
  1631. s[0]:=char(pc2-pc2start);
  1632. end;
  1633. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1634. {$ifndef FPUNONE}
  1635. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1636. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1637. begin
  1638. softfloat_exception_flags:=0;
  1639. end;
  1640. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1641. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  1642. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1643. begin
  1644. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1645. end;
  1646. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  1647. {$endif}
  1648. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1649. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1650. begin
  1651. { the extra Word type cast is necessary because the "AValue shr 8" }
  1652. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  1653. { the sign bits from the upper 16 bits are shifted in rather than }
  1654. { zeroes. }
  1655. Result := SmallInt((Word(AValue) shr 8) or (Word(AValue) shl 8));
  1656. end;
  1657. {$ifndef cpujvm}
  1658. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1659. begin
  1660. Result := Word((AValue shr 8) or (AValue shl 8));
  1661. end;
  1662. {$endif}
  1663. function SwapEndian(const AValue: LongInt): LongInt;
  1664. begin
  1665. Result := (AValue shl 24)
  1666. or ((AValue and $0000FF00) shl 8)
  1667. or ((AValue and $00FF0000) shr 8)
  1668. or (AValue shr 24);
  1669. end;
  1670. {$ifndef cpujvm}
  1671. function SwapEndian(const AValue: DWord): DWord;
  1672. begin
  1673. Result := (AValue shl 24)
  1674. or ((AValue and $0000FF00) shl 8)
  1675. or ((AValue and $00FF0000) shr 8)
  1676. or (AValue shr 24);
  1677. end;
  1678. {$endif}
  1679. function SwapEndian(const AValue: Int64): Int64;
  1680. begin
  1681. Result := (AValue shl 56)
  1682. or ((AValue and $000000000000FF00) shl 40)
  1683. or ((AValue and $0000000000FF0000) shl 24)
  1684. or ((AValue and $00000000FF000000) shl 8)
  1685. or ((AValue and $000000FF00000000) shr 8)
  1686. or ((AValue and $0000FF0000000000) shr 24)
  1687. or ((AValue and $00FF000000000000) shr 40)
  1688. or (AValue shr 56);
  1689. end;
  1690. {$ifndef cpujvm}
  1691. function SwapEndian(const AValue: QWord): QWord;
  1692. begin
  1693. Result := (AValue shl 56)
  1694. or ((AValue and $000000000000FF00) shl 40)
  1695. or ((AValue and $0000000000FF0000) shl 24)
  1696. or ((AValue and $00000000FF000000) shl 8)
  1697. or ((AValue and $000000FF00000000) shr 8)
  1698. or ((AValue and $0000FF0000000000) shr 24)
  1699. or ((AValue and $00FF000000000000) shr 40)
  1700. or (AValue shr 56);
  1701. end;
  1702. {$endif}
  1703. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1704. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1705. begin
  1706. {$IFDEF ENDIAN_BIG}
  1707. Result := AValue;
  1708. {$ELSE}
  1709. Result := SwapEndian(AValue);
  1710. {$ENDIF}
  1711. end;
  1712. {$ifndef cpujvm}
  1713. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1714. begin
  1715. {$IFDEF ENDIAN_BIG}
  1716. Result := AValue;
  1717. {$ELSE}
  1718. Result := SwapEndian(AValue);
  1719. {$ENDIF}
  1720. end;
  1721. {$endif not cpujvm}
  1722. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1723. begin
  1724. {$IFDEF ENDIAN_BIG}
  1725. Result := AValue;
  1726. {$ELSE}
  1727. Result := SwapEndian(AValue);
  1728. {$ENDIF}
  1729. end;
  1730. {$ifndef cpujvm}
  1731. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1732. begin
  1733. {$IFDEF ENDIAN_BIG}
  1734. Result := AValue;
  1735. {$ELSE}
  1736. Result := SwapEndian(AValue);
  1737. {$ENDIF}
  1738. end;
  1739. {$endif not cpujvm}
  1740. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1741. begin
  1742. {$IFDEF ENDIAN_BIG}
  1743. Result := AValue;
  1744. {$ELSE}
  1745. Result := SwapEndian(AValue);
  1746. {$ENDIF}
  1747. end;
  1748. {$ifndef cpujvm}
  1749. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1750. begin
  1751. {$IFDEF ENDIAN_BIG}
  1752. Result := AValue;
  1753. {$ELSE}
  1754. Result := SwapEndian(AValue);
  1755. {$ENDIF}
  1756. end;
  1757. {$endif not cpujvm}
  1758. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1759. begin
  1760. {$IFDEF ENDIAN_LITTLE}
  1761. Result := AValue;
  1762. {$ELSE}
  1763. Result := SwapEndian(AValue);
  1764. {$ENDIF}
  1765. end;
  1766. {$ifndef cpujvm}
  1767. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1768. begin
  1769. {$IFDEF ENDIAN_LITTLE}
  1770. Result := AValue;
  1771. {$ELSE}
  1772. Result := SwapEndian(AValue);
  1773. {$ENDIF}
  1774. end;
  1775. {$endif not cpujvm}
  1776. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1777. begin
  1778. {$IFDEF ENDIAN_LITTLE}
  1779. Result := AValue;
  1780. {$ELSE}
  1781. Result := SwapEndian(AValue);
  1782. {$ENDIF}
  1783. end;
  1784. {$ifndef cpujvm}
  1785. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1786. begin
  1787. {$IFDEF ENDIAN_LITTLE}
  1788. Result := AValue;
  1789. {$ELSE}
  1790. Result := SwapEndian(AValue);
  1791. {$ENDIF}
  1792. end;
  1793. {$endif not cpujvm}
  1794. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1795. begin
  1796. {$IFDEF ENDIAN_LITTLE}
  1797. Result := AValue;
  1798. {$ELSE}
  1799. Result := SwapEndian(AValue);
  1800. {$ENDIF}
  1801. end;
  1802. {$ifndef cpujvm}
  1803. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1804. begin
  1805. {$IFDEF ENDIAN_LITTLE}
  1806. Result := AValue;
  1807. {$ELSE}
  1808. Result := SwapEndian(AValue);
  1809. {$ENDIF}
  1810. end;
  1811. {$endif not cpujvm}
  1812. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1813. begin
  1814. {$IFDEF ENDIAN_BIG}
  1815. Result := AValue;
  1816. {$ELSE}
  1817. Result := SwapEndian(AValue);
  1818. {$ENDIF}
  1819. end;
  1820. {$ifndef cpujvm}
  1821. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1822. begin
  1823. {$IFDEF ENDIAN_BIG}
  1824. Result := AValue;
  1825. {$ELSE}
  1826. Result := SwapEndian(AValue);
  1827. {$ENDIF}
  1828. end;
  1829. {$endif not cpujvm}
  1830. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1831. begin
  1832. {$IFDEF ENDIAN_BIG}
  1833. Result := AValue;
  1834. {$ELSE}
  1835. Result := SwapEndian(AValue);
  1836. {$ENDIF}
  1837. end;
  1838. {$ifndef cpujvm}
  1839. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1840. begin
  1841. {$IFDEF ENDIAN_BIG}
  1842. Result := AValue;
  1843. {$ELSE}
  1844. Result := SwapEndian(AValue);
  1845. {$ENDIF}
  1846. end;
  1847. {$endif not cpujvm}
  1848. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1849. begin
  1850. {$IFDEF ENDIAN_BIG}
  1851. Result := AValue;
  1852. {$ELSE}
  1853. Result := SwapEndian(AValue);
  1854. {$ENDIF}
  1855. end;
  1856. {$ifndef cpujvm}
  1857. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1858. begin
  1859. {$IFDEF ENDIAN_BIG}
  1860. Result := AValue;
  1861. {$ELSE}
  1862. Result := SwapEndian(AValue);
  1863. {$ENDIF}
  1864. end;
  1865. {$endif not cpujvm}
  1866. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1867. begin
  1868. {$IFDEF ENDIAN_LITTLE}
  1869. Result := AValue;
  1870. {$ELSE}
  1871. Result := SwapEndian(AValue);
  1872. {$ENDIF}
  1873. end;
  1874. {$ifndef cpujvm}
  1875. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1876. begin
  1877. {$IFDEF ENDIAN_LITTLE}
  1878. Result := AValue;
  1879. {$ELSE}
  1880. Result := SwapEndian(AValue);
  1881. {$ENDIF}
  1882. end;
  1883. {$endif not cpujvm}
  1884. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1885. begin
  1886. {$IFDEF ENDIAN_LITTLE}
  1887. Result := AValue;
  1888. {$ELSE}
  1889. Result := SwapEndian(AValue);
  1890. {$ENDIF}
  1891. end;
  1892. {$ifndef cpujvm}
  1893. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1894. begin
  1895. {$IFDEF ENDIAN_LITTLE}
  1896. Result := AValue;
  1897. {$ELSE}
  1898. Result := SwapEndian(AValue);
  1899. {$ENDIF}
  1900. end;
  1901. {$endif not cpujvm}
  1902. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1903. begin
  1904. {$IFDEF ENDIAN_LITTLE}
  1905. Result := AValue;
  1906. {$ELSE}
  1907. Result := SwapEndian(AValue);
  1908. {$ENDIF}
  1909. end;
  1910. {$ifndef cpujvm}
  1911. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1912. begin
  1913. {$IFDEF ENDIAN_LITTLE}
  1914. Result := AValue;
  1915. {$ELSE}
  1916. Result := SwapEndian(AValue);
  1917. {$ENDIF}
  1918. end;
  1919. {$endif not cpujvm}
  1920. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  1921. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1922. begin
  1923. end;
  1924. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1925. begin
  1926. end;
  1927. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1928. begin
  1929. end;
  1930. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  1931. begin
  1932. end;
  1933. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  1934. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  1935. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  1936. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1937. begin
  1938. Result:=(AValue shr 1) or (AValue shl 7);
  1939. end;
  1940. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1941. begin
  1942. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  1943. end;
  1944. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1945. begin
  1946. Result:=(AValue shl 1) or (AValue shr 7);
  1947. end;
  1948. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  1949. begin
  1950. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  1951. end;
  1952. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  1953. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  1954. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  1955. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  1956. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1957. begin
  1958. Result:=(AValue shr 1) or (AValue shl 15);
  1959. end;
  1960. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1961. begin
  1962. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  1963. end;
  1964. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1965. begin
  1966. Result:=(AValue shl 1) or (AValue shr 15);
  1967. end;
  1968. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1969. begin
  1970. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  1971. end;
  1972. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  1973. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  1974. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  1975. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  1976. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1977. begin
  1978. Result:=(AValue shr 1) or (AValue shl 31);
  1979. end;
  1980. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1981. begin
  1982. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  1983. end;
  1984. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1985. begin
  1986. Result:=(AValue shl 1) or (AValue shr 31);
  1987. end;
  1988. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1989. begin
  1990. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  1991. end;
  1992. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  1993. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  1994. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  1995. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  1996. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1997. begin
  1998. Result:=(AValue shr 1) or (AValue shl 63);
  1999. end;
  2000. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2001. begin
  2002. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2003. end;
  2004. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2005. begin
  2006. Result:=(AValue shl 1) or (AValue shr 63);
  2007. end;
  2008. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2009. begin
  2010. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2011. end;
  2012. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2013. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2014. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2015. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2016. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2017. begin
  2018. 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)))));
  2019. end;
  2020. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2021. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2022. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2023. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2024. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2025. begin
  2026. 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)))));
  2027. end;
  2028. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2029. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2030. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2031. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2032. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2033. begin
  2034. 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)))));
  2035. end;
  2036. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2037. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2038. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2039. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2040. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2041. begin
  2042. 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)))));
  2043. end;
  2044. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2045. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2046. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2047. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2048. function BsfByte(Const AValue: Byte): Byte;
  2049. const bsf8bit: array [Byte] of Byte = (
  2050. $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,
  2051. 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,
  2052. 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,
  2053. 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,
  2054. 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,
  2055. 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,
  2056. 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,
  2057. 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
  2058. );
  2059. begin
  2060. result:=bsf8bit[AValue];
  2061. end;
  2062. {$endif}
  2063. {$endif}
  2064. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2065. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2066. function BsrByte(Const AValue: Byte): Byte;
  2067. const bsr8bit: array [Byte] of Byte = (
  2068. $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,
  2069. 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,
  2070. 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,
  2071. 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,
  2072. 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,
  2073. 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,
  2074. 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,
  2075. 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
  2076. );
  2077. begin
  2078. result:=bsr8bit[AValue];
  2079. end;
  2080. {$endif}
  2081. {$endif}
  2082. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2083. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2084. function BsfWord(Const AValue: Word): cardinal;
  2085. begin
  2086. result:=$ff;
  2087. if lo(AValue)<>0 then
  2088. result:=BsfByte(lo(AValue))
  2089. else if hi(AValue) <> 0 then
  2090. result:=BsfByte(hi(AValue))+8
  2091. end;
  2092. {$endif}
  2093. {$endif}
  2094. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2095. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2096. function BsrWord(Const AValue: Word): cardinal;
  2097. begin
  2098. if hi(AValue)<>0 then
  2099. result:=BsrByte(hi(AValue))+8
  2100. else
  2101. result:=BsrByte(lo(AValue))
  2102. end;
  2103. {$endif}
  2104. {$endif}
  2105. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2106. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2107. function BsfDWord(Const AValue : DWord): cardinal;
  2108. begin
  2109. result:=$ff;
  2110. if lo(AValue)<>0 then
  2111. result:=BsfWord(lo(AValue))
  2112. else if hi(AValue) <> 0 then
  2113. result:=BsfWord(hi(AValue))+16
  2114. end;
  2115. {$endif}
  2116. {$endif}
  2117. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2118. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2119. function BsrDWord(Const AValue : DWord): cardinal;
  2120. begin
  2121. if hi(AValue)<>0 then
  2122. result:=BsrWord(hi(AValue))+16
  2123. else
  2124. result:=BsrWord(lo(AValue))
  2125. end;
  2126. {$endif}
  2127. {$endif}
  2128. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2129. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2130. function BsfQWord(Const AValue : QWord): cardinal;
  2131. begin
  2132. result:=$ff;
  2133. if lo(AValue) <> 0 then
  2134. result:=BsfDWord(lo(AValue))
  2135. else if hi(AValue) <> 0 then
  2136. result:=BsfDWord(hi(AValue)) + 32;
  2137. end;
  2138. {$endif}
  2139. {$endif}
  2140. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2141. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2142. function BsrQWord(Const AValue : QWord): cardinal;
  2143. begin
  2144. if hi(AValue) <> 0 then
  2145. result:=BsrDWord(hi(AValue)) + 32
  2146. else
  2147. result:=BsrDWord(lo(AValue))
  2148. end;
  2149. {$endif}
  2150. {$endif}
  2151. const
  2152. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2153. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2154. begin
  2155. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2156. end;
  2157. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2158. var
  2159. i : SizeInt;
  2160. begin
  2161. Result:=0;
  2162. for i:=0 to 3 do
  2163. begin
  2164. inc(Result,PopCntData[AValue and $f]);
  2165. AValue:=AValue shr 4;
  2166. end;
  2167. end;
  2168. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2169. var
  2170. i : SizeInt;
  2171. begin
  2172. Result:=0;
  2173. for i:=0 to 7 do
  2174. begin
  2175. inc(Result,PopCntData[AValue and $f]);
  2176. AValue:=AValue shr 4;
  2177. end;
  2178. end;
  2179. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2180. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2181. {$ifdef VER2_6}
  2182. var
  2183. i : SizeInt;
  2184. begin
  2185. Result:=0;
  2186. for i:=0 to 15 do
  2187. begin
  2188. inc(Result,PopCntData[AValue and $f]);
  2189. AValue:=AValue shr 4;
  2190. end;
  2191. {$else VER2_6}
  2192. begin
  2193. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2194. {$endif VER2_6}
  2195. end;
  2196. {$endif}