generic.inc 74 KB

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