generic.inc 74 KB

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