generic.inc 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  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. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  13. var
  14. tmp,am1 : PtrUInt;
  15. begin
  16. am1:=alignment-1;
  17. tmp:=addr+am1;
  18. if alignment and am1=0 then
  19. { Alignment is a power of two. In practice alignments are powers of two 100% of the time. }
  20. result:=tmp and not am1
  21. else
  22. result:=tmp-(tmp mod alignment);
  23. end;
  24. {$ifndef cpujvm}
  25. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  26. var
  27. tmp,am1 : PtrUInt;
  28. begin
  29. am1:=alignment-1;
  30. tmp:=PtrUint(addr)+am1;
  31. if alignment and am1=0 then
  32. result:=pointer(tmp and not am1)
  33. else
  34. result:=pointer(ptruint(tmp-(tmp mod alignment)));
  35. end;
  36. {$endif}
  37. {****************************************************************************
  38. Primitives
  39. ****************************************************************************}
  40. type
  41. pstring = ^shortstring;
  42. {$ifndef FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  43. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  44. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  45. begin
  46. move(src[srcindex],dst[dstindex],len);
  47. end;
  48. {$endif FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  49. {$ifndef FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  50. {$define FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  51. procedure fpc_shortstr_chararray_intern_charmove(const src: shortstring; out dst: array of ansichar; const len: sizeint);
  52. begin
  53. move(src[1],PAnsiChar(@dst)^,len);
  54. end;
  55. {$endif FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  56. {$ifndef FPC_SYSTEM_HAS_MOVE}
  57. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  58. var
  59. aligncount : sizeint;
  60. pdest,psrc,pend : pbyte;
  61. begin
  62. if (@dest=@source) or (count<=0) then
  63. exit;
  64. if (@dest<@source) or (@source+count<@dest) then
  65. begin
  66. { Forward Move }
  67. psrc:=@source;
  68. pdest:=@dest;
  69. if (Count>4*sizeof(ptruint)-11)
  70. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  71. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  72. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  73. then
  74. begin
  75. { Align on native pointer size }
  76. aligncount:=(sizeof(PtrUInt)-PtrInt(pdest)) and (sizeof(PtrUInt)-1);
  77. dec(count,aligncount);
  78. pend:=psrc+aligncount;
  79. while psrc<pend do
  80. begin
  81. pdest^:=psrc^;
  82. inc(pdest);
  83. inc(psrc);
  84. end;
  85. { use sizeuint typecast to force shr optimization }
  86. pptruint(pend):=pptruint(psrc)+(sizeuint(count) div sizeof(ptruint));
  87. while psrc<pend do
  88. begin
  89. pptruint(pdest)^:=pptruint(psrc)^;
  90. inc(pptruint(pdest));
  91. inc(pptruint(psrc));
  92. end;
  93. count:=count and (sizeof(PtrUInt)-1);
  94. end;
  95. pend:=psrc+count;
  96. while psrc<pend do
  97. begin
  98. pdest^:=psrc^;
  99. inc(pdest);
  100. inc(psrc);
  101. end;
  102. end
  103. else
  104. begin
  105. { Backward Move }
  106. psrc:=@source+count;
  107. pdest:=@dest+count;
  108. if (Count>4*sizeof(ptruint)-11)
  109. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  110. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  111. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  112. then
  113. begin
  114. { Align on native pointer size }
  115. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1));
  116. dec(count,aligncount);
  117. pend:=psrc-aligncount;
  118. while psrc>pend do
  119. begin
  120. dec(pdest);
  121. dec(psrc);
  122. pdest^:=psrc^;
  123. end;
  124. { use sizeuint typecast to force shr optimization }
  125. pptruint(pend):=pptruint(psrc)-(sizeuint(count) div sizeof(ptruint));
  126. while psrc>pend do
  127. begin
  128. dec(pptruint(pdest));
  129. dec(pptruint(psrc));
  130. pptruint(pdest)^:=pptruint(psrc)^;
  131. end;
  132. count:=count and (sizeof(PtrUInt)-1);
  133. end;
  134. pend:=psrc-count;
  135. while psrc>pend do
  136. begin
  137. dec(pdest);
  138. dec(psrc);
  139. pdest^:=psrc^;
  140. end;
  141. end;
  142. end;
  143. {$endif not FPC_SYSTEM_HAS_MOVE}
  144. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  145. Procedure FillChar(var x;count:SizeInt;value:byte);
  146. var
  147. pdest,pend : pbyte;
  148. v : ALUUInt;
  149. begin
  150. if count <= 0 then
  151. exit;
  152. pdest:=@x;
  153. if Count>4*sizeof(ptruint)-1 then
  154. begin
  155. {$if sizeof(v)>=2}
  156. v:=(value shl 8) or value;
  157. {$endif sizeof(v)>=2}
  158. {$if sizeof(v)>=4}
  159. v:=(v shl 16) or v;
  160. {$endif sizeof(v)>=4}
  161. {$if sizeof(v)=8}
  162. v:=(v shl 32) or v;
  163. {$endif sizeof(v)=8}
  164. { Align on native pointer size }
  165. pend:=pbyte(align(pdest,sizeof(PtrUInt)));
  166. dec(count,pend-pdest);
  167. while pdest<pend do
  168. begin
  169. pdest^:=value;
  170. inc(pdest);
  171. end;
  172. { use sizeuint typecast to force shr optimization }
  173. pptruint(pend):=pptruint(pdest)+(sizeuint(count) div sizeof(ptruint));
  174. while pdest<pend do
  175. begin
  176. pptruint(pdest)^:=v;
  177. inc(pptruint(pdest));
  178. end;
  179. count:=count and (sizeof(ptruint)-1);
  180. end;
  181. pend:=pdest+count;
  182. while pdest<pend do
  183. begin
  184. pdest^:=value;
  185. inc(pdest);
  186. end;
  187. end;
  188. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  189. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  190. procedure fillword(var x;count : SizeInt;value : word);
  191. var
  192. aligncount : sizeint;
  193. pdest,pend : pword;
  194. v : ALUUInt;
  195. begin
  196. if count <= 0 then
  197. exit;
  198. pdest:=@x;
  199. if Count>4*sizeof(ptruint)-1 then
  200. begin
  201. {$if sizeof(v)>=4}
  202. v:=(value shl 16) or value;
  203. {$endif sizeof(v)>=4}
  204. {$if sizeof(v)=8}
  205. v:=(v shl 32) or v;
  206. {$endif sizeof(v)=8}
  207. { Align on native pointer size }
  208. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 1;
  209. dec(count,aligncount);
  210. pend:=pdest+aligncount;
  211. while pdest<pend do
  212. begin
  213. pdest^:=value;
  214. inc(pdest);
  215. end;
  216. { use sizeuint typecast to force shr optimization }
  217. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*2) div sizeof(ptruint));
  218. while pdest<pend do
  219. begin
  220. pptruint(pdest)^:=v;
  221. inc(pptruint(pdest));
  222. end;
  223. count:=((count*2) and (sizeof(ptruint)-1)) shr 1;
  224. end;
  225. pend:=pdest+count;
  226. while pdest<pend do
  227. begin
  228. pdest^:=value;
  229. inc(pdest);
  230. end;
  231. end;
  232. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  233. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  234. procedure filldword(var x;count : SizeInt;value : dword);
  235. var
  236. aligncount : sizeint;
  237. pdest,pend : pdword;
  238. v : ALUUInt;
  239. begin
  240. if count <= 0 then
  241. exit;
  242. pdest:=@x;
  243. if Count>4*sizeof(ptruint)-1 then
  244. begin
  245. v:=value;
  246. {$if sizeof(v)=8}
  247. v:=(v shl 32) or v;
  248. {$endif sizeof(v)=8}
  249. { Align on native pointer size }
  250. aligncount:=(PtrUInt(pdest) and (sizeof(PtrUInt)-1)) shr 2;
  251. dec(count,aligncount);
  252. pend:=pdest+aligncount;
  253. while pdest<pend do
  254. begin
  255. pdest^:=value;
  256. inc(pdest);
  257. end;
  258. { use sizeuint typecast to force shr optimization }
  259. pptruint(pend):=pptruint(pdest)+((sizeuint(count)*4) div sizeof(ptruint));
  260. while pdest<pend do
  261. begin
  262. pptruint(pdest)^:=v;
  263. inc(pptruint(pdest));
  264. end;
  265. count:=((count*4) and (sizeof(ptruint)-1)) shr 2;
  266. end;
  267. pend:=pdest+count;
  268. while pdest<pend do
  269. begin
  270. pdest^:=value;
  271. inc(pdest);
  272. end;
  273. end;
  274. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  275. {$ifndef FPC_SYSTEM_HAS_FILLQWORD}
  276. procedure fillqword(var x;count : SizeInt;value : qword);
  277. var
  278. pdest,pend : pqword;
  279. begin
  280. if count <= 0 then
  281. exit;
  282. pdest:=@x;
  283. pend:=pdest+count;
  284. while pdest<pend do
  285. begin
  286. pdest^:=value;
  287. inc(pdest);
  288. end;
  289. end;
  290. {$endif FPC_SYSTEM_HAS_FILLQWORD}
  291. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  292. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  293. var
  294. psrc,pend : pbyte;
  295. begin
  296. psrc:=@buf;
  297. pend:=psrc+len;
  298. { simulate assembler implementations behaviour, which is expected }
  299. { fpc_pchar_to_ansistr in astrings.inc }
  300. if (len < 0) or
  301. (pend < psrc) then
  302. pend:=pbyte(high(PtrUInt)-PtrUint(sizeof(byte)));
  303. while (psrc<pend) and (psrc^<>b) do
  304. inc(psrc);
  305. if psrc<pend then
  306. result:=psrc-pbyte(@buf)
  307. else
  308. result:=-1;
  309. end;
  310. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  311. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  312. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  313. var
  314. psrc,pend : pword;
  315. begin
  316. psrc:=@buf;
  317. pend:=psrc+len;
  318. { simulate assembler implementations behaviour, which is expected }
  319. { fpc_pchar_to_ansistr in astrings.inc }
  320. if not (
  321. (len >= 0) and
  322. { is this ever false? }
  323. (len <= high(PtrInt))) or
  324. (pend < psrc) then
  325. pend:=pword(high(PtrUInt)-PtrUint(sizeof(word)));
  326. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  327. if (ptruint(psrc) mod 2)<>0 then
  328. while (psrc<pend) and (unaligned(psrc^)<>b) do
  329. inc(psrc)
  330. else
  331. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  332. while (psrc<pend) and (psrc^<>b) do
  333. inc(psrc);
  334. if psrc<pend then
  335. { the result is always >=0 so avoid handling of negative values }
  336. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(word)
  337. else
  338. result:=-1;
  339. end;
  340. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  341. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  342. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  343. var
  344. psrc,pend : pdword;
  345. begin
  346. psrc:=@buf;
  347. pend:=psrc+len;
  348. { simulate assembler implementations behaviour, which is expected }
  349. { fpc_pchar_to_ansistr in astrings.inc }
  350. if not (
  351. (len >= 0) and
  352. (len <= high(PtrInt) div 2)) or
  353. (pend < psrc) then
  354. pend:=pdword(high(PtrUInt)-PtrUInt(sizeof(dword)));
  355. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  356. if (ptruint(psrc) mod 4)<>0 then
  357. while (psrc<pend) and (unaligned(psrc^)<>b) do
  358. inc(psrc)
  359. else
  360. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  361. while (psrc<pend) and (psrc^<>b) do
  362. inc(psrc);
  363. if psrc<pend then
  364. { the result is always >=0 so avoid handling of negative values }
  365. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(DWord)
  366. else
  367. result:=-1;
  368. end;
  369. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  370. {$ifndef FPC_SYSTEM_HAS_INDEXQWORD}
  371. function IndexQWord(Const buf;len:SizeInt;b:QWord):SizeInt;
  372. var
  373. psrc,pend : pqword;
  374. begin
  375. psrc:=@buf;
  376. pend:=psrc+len;
  377. { simulate assembler implementations behaviour, which is expected }
  378. { fpc_pchar_to_ansistr in astrings.inc }
  379. if not (
  380. (len >= 0) and
  381. (len <= high(PtrInt) div 4)) or
  382. (pend < psrc) then
  383. pend:=pqword(high(PtrUInt)-PtrUInt(sizeof(qword)));
  384. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  385. if (ptruint(psrc) mod 8)<>0 then
  386. while (psrc<pend) and (unaligned(psrc^)<>b) do
  387. inc(psrc)
  388. else
  389. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  390. while (psrc<pend) and (psrc^<>b) do
  391. inc(psrc);
  392. if psrc<pend then
  393. { the result is always >=0 so avoid handling of negative values }
  394. result:=PtrUint(pointer(psrc)-pointer(@buf)) div sizeof(QWord)
  395. else
  396. result:=-1;
  397. end;
  398. {$endif not FPC_SYSTEM_HAS_INDEXQWORD}
  399. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  400. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  401. var
  402. psrc,pdest,pend,pendpart : pbyte;
  403. begin
  404. psrc:=@buf1;
  405. pdest:=@buf2;
  406. pend:=psrc+len;
  407. if (pend<psrc) then
  408. pend:=pbyte(high(ptruint));
  409. if (len>=2*sizeof(ptruint))
  410. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  411. and ((PtrUInt(pdest) and (sizeof(PtrUInt)-1))=(PtrUInt(psrc) and (sizeof(PtrUInt)-1)))
  412. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  413. then
  414. begin
  415. { Align "psrc" on native pointer size. }
  416. PtrUint(pendpart):=PtrUint(psrc+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  417. if psrc<pendpart then
  418. begin
  419. while (psrc<pendpart) and (psrc^=pdest^) do
  420. begin
  421. inc(pdest);
  422. inc(psrc);
  423. end;
  424. if psrc<pendpart then
  425. exit(sizeint(psrc^)-sizeint(pdest^));
  426. end;
  427. { "pend" is the end of "psrc" and "psrc" is aligned, so aligned "pend" can be obtained this way. }
  428. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  429. while (psrc<pendpart) and (pptruint(psrc)^=pptruint(pdest)^) do
  430. begin
  431. inc(pptruint(pdest));
  432. inc(pptruint(psrc));
  433. end;
  434. if psrc<pendpart then
  435. pend:=psrc+sizeof(ptruint);
  436. end;
  437. while (psrc<pend) and (psrc^=pdest^) do
  438. begin
  439. inc(pdest);
  440. inc(psrc);
  441. end;
  442. if psrc<pend then
  443. exit(sizeint(psrc^)-sizeint(pdest^));
  444. result:=0;
  445. end;
  446. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  447. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  448. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  449. var
  450. psrc,pdest,pend,pendpart : pword;
  451. begin
  452. psrc:=@buf1;
  453. pdest:=@buf2;
  454. pend:=psrc+len;
  455. if (pend<psrc) or not ((len>=0) and (len<=High(PtrInt) div 2)) then
  456. pend:=pword(high(ptruint)-2);
  457. if (len>=2*sizeof(ptruint)) { len in words, so at least four pointers }
  458. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  459. and ((PtrUInt(pdest) xor PtrUInt(psrc)) and (sizeof(PtrUInt)-1)=0)
  460. and (PtrUInt(psrc) and 1=0)
  461. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  462. then
  463. begin
  464. { Align on native pointer size. Careful, these 'pendpart's are aligned even if 'psrc' is misaligned, so "psrc<>pendpart" must not be used. }
  465. PtrUint(pendpart):=(PtrUint(psrc)+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  466. while (psrc<pendpart) and (psrc^=pdest^) do
  467. begin
  468. inc(pdest);
  469. inc(psrc);
  470. end;
  471. if psrc<pendpart then
  472. exit(2*ord(psrc^>pdest^)-1);
  473. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  474. while (psrc<pendpart) and (pptrint(psrc)^=pptrint(pdest)^) do
  475. begin
  476. inc(pptruint(pdest));
  477. inc(pptruint(psrc));
  478. end;
  479. if psrc<pendpart then
  480. pointer(pend):=pointer(psrc)+sizeof(ptruint);
  481. end;
  482. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  483. if (PtrUInt(pdest) or PtrUInt(psrc)) and 1<>0 then
  484. begin
  485. while (psrc<pend) and (unaligned(psrc^)=unaligned(pdest^)) do
  486. begin
  487. inc(pdest);
  488. inc(psrc);
  489. end;
  490. if psrc<pend then
  491. exit(2*ord(unaligned(psrc^)>unaligned(pdest^))-1);
  492. end
  493. else
  494. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  495. begin
  496. while (psrc<pend) and (psrc^=pdest^) do
  497. begin
  498. inc(pdest);
  499. inc(psrc);
  500. end;
  501. if psrc<pend then
  502. exit(2*ord(psrc^>pdest^)-1);
  503. end;
  504. result:=0;
  505. end;
  506. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  507. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  508. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  509. var
  510. psrc,pdest,pend,pendpart : pdword;
  511. begin
  512. psrc:=@buf1;
  513. pdest:=@buf2;
  514. pend:=psrc+len;
  515. if (pend<psrc) or not ((len>=0) and (len<=High(PtrInt) div 4)) then
  516. pend:=pdword(high(ptruint)-4);
  517. {$if sizeof(ptruint)>sizeof(dword)}
  518. if (len>=sizeof(ptruint)) { len in uint32s, so at least four pointers }
  519. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  520. and ((PtrUInt(pdest) xor PtrUInt(psrc)) and (sizeof(PtrUInt)-1)=0)
  521. and (PtrUInt(psrc) and 3=0)
  522. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  523. then
  524. begin
  525. { Align on native pointer size. Careful, these 'pendpart's are aligned even if 'psrc' is misaligned, so "psrc<>pendpart" must not be used. }
  526. PtrUint(pendpart):=(PtrUint(psrc)+(sizeof(PtrUint)-1)) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  527. while (psrc<pendpart) and (psrc^=pdest^) do
  528. begin
  529. inc(pdest);
  530. inc(psrc);
  531. end;
  532. if psrc<pendpart then
  533. exit(2*ord(psrc^>pdest^)-1);
  534. PtrUint(pendpart):=PtrUint(pend) and PtrUint(not PtrUint(sizeof(PtrUint)-1));
  535. while (psrc<pendpart) and (pptrint(psrc)^=pptrint(pdest)^) do
  536. begin
  537. inc(pptruint(pdest));
  538. inc(pptruint(psrc));
  539. end;
  540. if psrc<pendpart then
  541. pointer(pend):=pointer(psrc)+sizeof(ptruint);
  542. end;
  543. {$endif sizeof(ptruint)>sizeof(dword)}
  544. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  545. if (PtrUInt(pdest) or PtrUInt(psrc)) and 3<>0 then
  546. begin
  547. while (psrc<pend) and (unaligned(psrc^)=unaligned(pdest^)) do
  548. begin
  549. inc(pdest);
  550. inc(psrc);
  551. end;
  552. if psrc<pend then
  553. exit(2*ord(unaligned(psrc^)>unaligned(pdest^))-1);
  554. end
  555. else
  556. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  557. begin
  558. while (psrc<pend) and (psrc^=pdest^) do
  559. begin
  560. inc(pdest);
  561. inc(psrc);
  562. end;
  563. if psrc<pend then
  564. exit(2*ord(psrc^>pdest^)-1);
  565. end;
  566. result:=0;
  567. end;
  568. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  569. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  570. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  571. var
  572. I : SizeInt;
  573. begin
  574. if Len = 0 then
  575. exit;
  576. I:=IndexByte(Buf1,Len,0);
  577. if I<>-1 then
  578. Move(Buf1,Buf2,I)
  579. else
  580. Move(Buf1,Buf2,len);
  581. end;
  582. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  583. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  584. function IndexChar0(Const buf;len:SizeInt;b:AnsiChar):SizeInt;
  585. var
  586. psrc,pend : pbyte;
  587. begin
  588. psrc:=@buf;
  589. { simulate assembler implementations behaviour, which is expected }
  590. { fpc_pchar_to_ansistr in astrings.inc }
  591. if (len < 0) then
  592. pend:=pbyte(high(PtrUInt)-PtrUInt(sizeof(byte)))
  593. else
  594. pend:=psrc+len;
  595. while (psrc<pend) and (psrc^<>0) do
  596. begin
  597. if (psrc^=byte(b)) then
  598. begin
  599. result:=psrc-pbyte(@buf);
  600. exit;
  601. end;
  602. inc(psrc);
  603. end;
  604. result:=-1;
  605. end;
  606. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  607. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  608. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  609. var
  610. psrc,pdest,pend : pbyte;
  611. b : ptrint;
  612. begin
  613. b:=0;
  614. psrc:=@buf1;
  615. pdest:=@buf2;
  616. pend:=psrc+len;
  617. while psrc<pend do
  618. begin
  619. b:=(ptrint(psrc^)-ptrint(pdest^));
  620. if b<0 then
  621. exit(-1)
  622. else if b>0 then
  623. exit(1);
  624. if (psrc^=0) or (pdest^=0) then
  625. exit(0);
  626. inc(pdest);
  627. inc(psrc);
  628. end;
  629. result:=0;
  630. end;
  631. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  632. {****************************************************************************
  633. Object Helpers
  634. ****************************************************************************}
  635. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  636. type
  637. pobjectvmt=^tobjectvmt;
  638. tobjectvmt=record
  639. size,msize:sizeuint;
  640. parent:{$ifdef VER3_0}pointer{$else}ppointer{$endif};
  641. end;
  642. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  643. { Note: _vmt will be reset to -1 when memory is allocated,
  644. this is needed for fpc_help_fail }
  645. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  646. var
  647. vmtcopy : pobjectvmt;
  648. begin
  649. vmtcopy:=pobjectvmt(_vmt);
  650. { Inherited call? }
  651. if vmtcopy=nil then
  652. begin
  653. fpc_help_constructor:=_self;
  654. exit;
  655. end;
  656. if (_self=nil) and
  657. (vmtcopy^.size>0) then
  658. begin
  659. getmem(_self,vmtcopy^.size);
  660. { reset vmt needed for fail }
  661. _vmt:=pointer(-1);
  662. end;
  663. if _self<>nil then
  664. begin
  665. fillchar(_self^,vmtcopy^.size,0);
  666. ppointer(_self+_vmt_pos)^:=vmtcopy;
  667. end;
  668. fpc_help_constructor:=_self;
  669. end;
  670. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  671. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  672. { Note: _self will not be reset, the compiler has to generate the reset }
  673. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  674. begin
  675. { already released? }
  676. if (_self=nil) or
  677. (_vmt<>pointer(-1)) or
  678. (ppointer(_self+vmt_pos)^=nil) then
  679. exit;
  680. if (pobjectvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  681. (pobjectvmt(ppointer(_self+vmt_pos)^)^.size+pobjectvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  682. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  683. { reset vmt to nil for protection }
  684. ppointer(_self+vmt_pos)^:=nil;
  685. freemem(_self);
  686. end;
  687. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  688. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  689. { Note: _self will not be reset, the compiler has to generate the reset }
  690. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  691. begin
  692. if (_self=nil) or (_vmt=nil) then
  693. exit;
  694. { vmt=$ffffffff when memory was allocated }
  695. if ptruint(_vmt)=high(ptruint) then
  696. begin
  697. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  698. HandleError(210)
  699. else
  700. begin
  701. ppointer(_self+vmt_pos)^:=nil;
  702. freemem(_self);
  703. { reset _vmt to nil so it will not be freed a
  704. second time }
  705. _vmt:=nil;
  706. end;
  707. end
  708. else
  709. ppointer(_self+vmt_pos)^:=nil;
  710. end;
  711. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  712. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  713. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  714. begin
  715. if (_vmt=nil) or
  716. (pobjectvmt(_vmt)^.size=0) or
  717. (pobjectvmt(_vmt)^.size+pobjectvmt(_vmt)^.msize<>0) then
  718. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  719. end;
  720. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  721. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  722. { checks for a correct vmt pointer }
  723. { deeper check to see if the current object is }
  724. { really related to the true }
  725. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  726. begin
  727. if (vmt=nil) or
  728. (pobjectvmt(vmt)^.size=0) or
  729. (pobjectvmt(vmt)^.size+pobjectvmt(vmt)^.msize<>0) then
  730. HandleErrorAddrFrameInd(210,get_pc_addr,get_frame);
  731. while assigned(vmt) do
  732. if vmt=expvmt then
  733. exit
  734. else
  735. {$ifdef VER3_0}
  736. vmt:=pobjectvmt(vmt)^.parent;
  737. {$else VER3_0}
  738. if assigned(pobjectvmt(vmt)^.parent) then
  739. vmt:=pobjectvmt(vmt)^.parent^
  740. else
  741. vmt:=nil;
  742. {$endif}
  743. HandleErrorAddrFrameInd(219,get_pc_addr,get_frame);
  744. end;
  745. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  746. {$endif FPC_HAS_FEATURE_OBJECTS}
  747. {****************************************************************************
  748. String
  749. ****************************************************************************}
  750. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  751. procedure fpc_shortstr_to_shortstr(out res:shortstring; const sstr: shortstring);[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  752. var
  753. slen : byte;
  754. begin
  755. slen:=length(sstr);
  756. if slen>high(res) then
  757. slen:=high(res);
  758. move(sstr[0],res[0],slen+1);
  759. res[0]:=chr(slen);
  760. end;
  761. procedure fpc_shortstr_assign(len:{$ifdef cpu16}smallint{$else}longint{$endif};sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; compilerproc;
  762. var
  763. slen : byte;
  764. begin
  765. slen:=length(pshortstring(sstr)^);
  766. if slen<len then
  767. len:=slen;
  768. move(sstr^,dstr^,len+1);
  769. if slen>len then
  770. PAnsiChar(dstr)^:=chr(len);
  771. end;
  772. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  773. {$push}
  774. { ensure that comparing addresses of openshortstrings with regular shortstrings
  775. doesn't cause errors }
  776. {$t-}
  777. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  778. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  779. var
  780. s1l, s2l : ObjpasInt;
  781. begin
  782. s1l:=length(s1);
  783. s2l:=length(s2);
  784. if s1l+s2l>high(dests) then
  785. begin
  786. if s1l>high(dests) then
  787. s1l:=high(dests);
  788. s2l:=high(dests)-s1l;
  789. end;
  790. if @dests=@s1 then
  791. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l)
  792. else
  793. if @dests=@s2 then
  794. begin
  795. fpc_shortstr_shortstr_intern_charmove(dests,1,dests,s1l+1,s2l);
  796. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  797. end
  798. else
  799. begin
  800. fpc_shortstr_shortstr_intern_charmove(s1,1,dests,1,s1l);
  801. fpc_shortstr_shortstr_intern_charmove(s2,1,dests,s1l+1,s2l);
  802. end;
  803. dests[0]:=chr(s1l+s2l);
  804. end;
  805. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  806. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  807. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  808. var
  809. s2l : byte;
  810. LowStart,i,
  811. Len : ObjpasInt;
  812. needtemp : boolean;
  813. tmpstr : shortstring;
  814. p,pdest : pshortstring;
  815. begin
  816. if high(sarr)=0 then
  817. begin
  818. DestS:='';
  819. exit;
  820. end;
  821. lowstart:=low(sarr);
  822. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  823. inc(lowstart);
  824. { Check for another reuse, then we can't use
  825. the append optimization and need to use a temp }
  826. needtemp:=false;
  827. for i:=lowstart to high(sarr) do
  828. begin
  829. if Pointer(@DestS)=Pointer(sarr[i]) then
  830. begin
  831. needtemp:=true;
  832. break;
  833. end;
  834. end;
  835. if needtemp then
  836. begin
  837. lowstart:=low(sarr);
  838. tmpstr:='';
  839. pdest:=@tmpstr
  840. end
  841. else
  842. begin
  843. { Start with empty DestS if we start with concatting
  844. the first array element }
  845. if lowstart=low(sarr) then
  846. DestS:='';
  847. pdest:=@DestS;
  848. end;
  849. { Concat all strings, except the string we already
  850. copied in DestS }
  851. Len:=length(pdest^);
  852. for i:=lowstart to high(sarr) do
  853. begin
  854. p:=sarr[i];
  855. if assigned(p) then
  856. begin
  857. s2l:=length(p^);
  858. if Len+s2l>high(dests) then
  859. s2l:=high(dests)-Len;
  860. fpc_shortstr_shortstr_intern_charmove(p^,1,pdest^,Len+1,s2l);
  861. inc(Len,s2l);
  862. end;
  863. end;
  864. pdest^[0]:=Chr(Len);
  865. if needtemp then
  866. DestS:=TmpStr;
  867. end;
  868. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT_MULTI}
  869. {$pop}
  870. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  871. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  872. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  873. var
  874. s1l, s2l : sizeint;
  875. begin
  876. s1l:=length(s1);
  877. s2l:=length(s2);
  878. if s1l+s2l>high(s1) then
  879. s2l:=high(s1)-s1l;
  880. move(s2[1],s1[s1l+1],s2l);
  881. s1[0]:=chr(s1l+s2l);
  882. end;
  883. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  884. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  885. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  886. var
  887. s1,s2,max : byte;
  888. begin
  889. s1:=length(left);
  890. s2:=length(right);
  891. if s1<s2 then
  892. max:=s1
  893. else
  894. max:=s2;
  895. result:=CompareByte(left[1],right[1],max);
  896. if result=0 then
  897. result:=s1-s2;
  898. result:=ord(result>0)-ord(result<0);
  899. end;
  900. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  901. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  902. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  903. begin
  904. Result := ObjpasInt(left[0]) - ObjpasInt(right[0]);
  905. if Result = 0 then
  906. Result := CompareByte(left[1],right[1], ObjpasInt(left[0]));
  907. end;
  908. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  909. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  910. procedure fpc_pchar_to_shortstr(out res : shortstring;p:PAnsiChar);[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  911. var
  912. l : ObjpasInt;
  913. begin
  914. if p=nil then
  915. begin
  916. res[0]:=#0;
  917. exit;
  918. end;
  919. { On platforms where IndexByte with len > 0 will not read the invalid memory past the null terminator, high(res) can be used as a limit. }
  920. {$if defined(cpui386) or defined(cpux86_64)}
  921. l:=IndexByte(p^,high(res),0);
  922. if l<0 then
  923. l:=high(res);
  924. {$else IndexByte(p^,high(res),0) can crash}
  925. l:=strlen(p);
  926. if l>high(res) then
  927. l:=high(res);
  928. {$endif IndexByte(p^,high(res),0) can crash}
  929. move(p^,res[1],l);
  930. res[0]:=chr(l);
  931. end;
  932. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  933. {$ifndef cpujvm}
  934. { also define alias which can be used inside the system unit }
  935. procedure fpc_pchar_to_shortstr(out res : shortstring;p:PAnsiChar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  936. function strpas(p:PAnsiChar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  937. begin
  938. fpc_pchar_to_shortstr(result,p);
  939. end;
  940. {$endif not cpujvm}
  941. { Combining codepoints are those belonging to one of the three "Mark" general categories.
  942. UnicodeData.txt column 3 has M* for them: Mn, Mc, Me.
  943. Using the table below, codepoint %...XXXXXXXX_YYYY_ZZZZZ can be classified as combining or not with a 3-level lookup:
  944. if %...XXXXXXXX <= High(IsCombinings.L2) then
  945. begin
  946. index := IsCombinings.L2[%XXXXXXXX];
  947. index := IsCombinings.L1[index][%YYYY];
  948. IsCombining := boolean(IsCombinings.L0[index] shr %ZZZZZ and 1);
  949. end else
  950. IsCombining := false;
  951. Equivalent one-liner:
  952. IsCombining := (%...XXXXXXXX <= High(IsCombinings.L2)) and (IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[%XXXXXXXX]][%YYYY]] shr %ZZZZZ and 1 <> 0);
  953. Additionally, there is a combining range U+E0100..U+E01EF far to the right, not included into the table to save 1 level.
  954. Table built from UnicodeData.txt 15.0.0 (September 2022). }
  955. const
  956. IsCombinings: record
  957. L2: array[0 .. 244] of uint8;
  958. L1: array[0 .. 46, 0 .. 15] of uint8;
  959. L0: array[0 .. 161] of uint32;
  960. end =
  961. (
  962. L2: (
  963. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, {10} 0, 10, 11, 12, 13, 0, 14, 0, 0, 0, {20} 0, 0, 15, 0, 16, 0, 0, 0, 0, 0, {30} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  964. {40} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {50} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {60} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {70} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {80} 0,
  965. 0, 0, 17, 18, 19, 0, 0, 0, 0, {90} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {100} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {110} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {120} 0, 0,
  966. 0, 0, 0, 20, 0, 21, 22, 23, {130} 0, 0, 0, 24, 25, 26, 27, 28, 29, 30, {140} 31, 32, 33, 34, 0, 0, 0, 0, 0, 0, {150} 0, 0, 0, 0, 35, 0, 0, 0, 0, 0,
  967. {160} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {170} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {180} 0, 36, 0, 37, 0, 0, 0, 0, 0, 0, {190} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  968. {200} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {210} 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {220} 0, 0, 38, 0, 0, 0, 0, 0, 0, 0, {230} 0, 39, 40, 41, 0, 0, 0, 42, 0, 0,
  969. {240} 43, 44, 45, 0, 46
  970. );
  971. L1: (
  972. {0} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 0, 0, 0, 0),
  973. {2} (0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 5, 6, 0), (7, 0, 8, 9, 0, 0, 10, 11, 12, 13, 14, 0, 0, 15, 0, 16),
  974. {4} (17, 18, 19, 0, 20, 0, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), (27, 28, 31, 32, 27, 28, 33, 34, 27, 28, 35, 26, 36, 37, 38, 0),
  975. {6} (39, 28, 40, 26, 27, 28, 40, 41, 23, 42, 43, 26, 27, 0, 44, 45), (0, 46, 47, 0, 0, 48, 49, 0, 50, 51, 0, 4, 52, 53, 54, 0),
  976. {8} (0, 55, 56, 57, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0, 0, 0, 0, 0),
  977. {10} (0, 0, 0, 0, 0, 0, 0, 0, 60, 61, 45, 45, 0, 62, 63, 0), (64, 0, 0, 0, 65, 66, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0),
  978. {12} (68, 0, 69, 70, 0, 13, 1, 1, 39, 62, 39, 71, 72, 73, 0, 74), (0, 75, 0, 0, 0, 0, 76, 77, 0, 0, 0, 0, 0, 0, 1, 1),
  979. {14} (0, 0, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 78, 0, 0, 0, 79, 0, 0, 0, 1),
  980. {16} (0, 80, 0, 0, 81, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 82, 37, 0, 0, 83, 0, 0, 0, 0, 0, 0, 0, 0),
  981. {18} (84, 85, 0, 0, 86, 62, 87, 88, 0, 89, 90, 0, 23, 91, 92, 93), (0, 94, 95, 96, 0, 97, 98, 99, 0, 0, 0, 0, 0, 0, 0, 100),
  982. {20} (0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0, 0), (2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  983. {22} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102), (0, 0, 0, 0, 0, 0, 0, 92, 0, 0, 0, 103, 0, 0, 0, 0),
  984. {24} (104, 105, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 106, 0, 0, 0, 0, 0, 0),
  985. {26} (0, 0, 0, 0, 0, 107, 0, 59, 0, 0, 15, 0, 108, 0, 0, 0), (72, 20, 109, 110, 72, 7, 36, 0, 72, 111, 65, 112, 72, 91, 113, 0),
  986. {28} (0, 114, 98, 0, 0, 0, 79, 14, 23, 42, 29, 115, 0, 0, 0, 0), (0, 116, 117, 0, 0, 13, 23, 0, 0, 0, 0, 0, 0, 118, 119, 0),
  987. {30} (0, 13, 92, 0, 0, 120, 0, 0, 59, 121, 0, 0, 0, 0, 0, 0), (0, 122, 0, 0, 0, 0, 0, 0, 0, 123, 124, 0, 0, 0, 125, 126),
  988. {32} (127, 128, 129, 0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 131, 0, 0, 132, 133, 0, 0, 0, 134, 135, 0, 136, 0, 0, 0),
  989. {34} (0, 0, 0, 0, 0, 0, 0, 137, 138, 139, 72, 0, 0, 0, 0, 0), (0, 0, 140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  990. {36} (0, 0, 0, 0, 0, 0, 0, 141, 0, 142, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 1, 144, 0, 0, 145),
  991. {38} (0, 0, 0, 0, 146, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 147, 109, 0, 0, 0, 0, 0),
  992. {40} (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 149, 150, 0, 0), (0, 0, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  993. {42} (1, 152, 1, 153, 154, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (156, 157, 0, 0, 158, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0),
  994. {44} (0, 0, 0, 0, 0, 159, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 160, 0, 0, 0, 0, 0, 0, 0, 0),
  995. {46} (0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 161, 0, 0, 0, 0, 0)
  996. );
  997. L0: (
  998. {0} $00000000, $FFFFFFFF, $0000FFFF, $000003F8, $FFFE0000, $BFFFFFFF, $000000B6, $07FF0000, $FFFFF800, $00010000, $9FC00000, $00003D9F, $00020000,
  999. {13} $FFFF0000, $000007FF, $0001FFC0, $200FF800, $FBC00000, $00003EEF, $0E000000, $FF000000, $FFFFFC00, $FFFFFFFB, $0000000F, $DC000000, $00FEFFFF,
  1000. {26} $0000000C, $0000000E, $D0000000, $0080399F, $4000000C, $00023987, $00230000, $00003BBF, $FC00000C, $00E0399F, $00000004, $C0000000, $00803DC7,
  1001. {39} $0000001F, $00603DDF, $0008000C, $D8000000, $00803DDF, $FF5F8400, $000C0000, $07F20000, $00007F80, $1FF20000, $00007F00, $03000000, $C2A00000,
  1002. {52} $FEFFE0DF, $1FFFFFFF, $00000040, $7FFFF800, $C3C00000, $001E3F9D, $3C00BFFC, $E0000000, $003C0000, $001C0000, $FFF00000, $200FFFFF, $0000B800,
  1003. {65} $00000060, $00000200, $0FFF0FFF, $0F800000, $7FE00000, $9FFFFFFF, $000FF800, $00000007, $00003FFE, $000FFFC0, $00FFFFF0, $FFF70000, $039021FF,
  1004. {78} $00038000, $80000000, $0000FC00, $06000000, $3FF78000, $00030000, $00000844, $000010F8, $00000003, $0000003F, $8003FFFF, $00003FC0, $000FFF80,
  1005. {91} $FFF80000, $00000001, $00000020, $007FFE00, $00003008, $38000000, $C19D0000, $00000002, $0060F800, $000037F8, $40000000, $20000000, $07C00000,
  1006. {104} $0000F06E, $87000000, $000000F0, $00001800, $0000003C, $0000007F, $80190000, $001FFF80, $00080000, $0000DE01, $40FFF000, $001F1FCC, $FFE00000,
  1007. {117} $4000007F, $FF3F8000, $30000001, $00FFF800, $00000FFF, $07FFF000, $79BF0000, $0000000D, $FCFE0000, $00000011, $000007FE, $7BF80000, $0FFE0080,
  1008. {130} $03FFFC00, $FF7F8000, $FFFC0000, $007FFEFF, $B47E0000, $000000BF, $00FB7C00, $00780000, $0000000B, $C7F00000, $003FFF81, $001F0000, $007F0000,
  1009. {143} $FFFE8000, $000780FF, $00030010, $60000000, $FFFF3FFF, $F807E3E0, $00000FE7, $00003C00, $0000001C, $F87FFFFF, $00201FFF, $F8000010, $0000FFFE,
  1010. {156} $F9FFFF7F, $000007DB, $00008000, $00004000, $0000F000, $000007F0
  1011. );
  1012. );
  1013. function Utf8CodePointLen(P: PAnsiChar; MaxLookAhead: SizeInt; IncludeCombiningDiacriticalMarks: Boolean): SizeInt;
  1014. var
  1015. cp: uint32;
  1016. iByte,cpLen: SizeInt;
  1017. begin
  1018. { see https://en.wikipedia.org/wiki/UTF-8#Description for details }
  1019. result:=0;
  1020. { result = 0 when scanning first character, result > 0 when scanning potential diacritical marks following it.
  1021. Common case is correct UTF-8.
  1022. Setting cpLen and breaking from the loop (instead of exiting) will handle invalid/incomplete cases
  1023. when cpLen bytes were expected, but not all are present/valid.
  1024. This keeps the code more compact, both source and binary. }
  1025. repeat
  1026. if MaxLookAhead<1 then
  1027. exit;
  1028. case ord(P[result]) of
  1029. { One-byte codepoints have the form
  1030. %(0)xxxxxxx. }
  1031. 0..$7F {%01111111}:
  1032. { There are no diacritics among them. }
  1033. if not IncludeCombiningDiacriticalMarks then
  1034. exit(1)
  1035. else if result=0 then
  1036. begin
  1037. result:=1;
  1038. Dec(MaxLookAhead);
  1039. end
  1040. else
  1041. exit;
  1042. { Two-byte codepoints have the form
  1043. %(110)xxxxx (10)xxxxxx.
  1044. but also minimum value of $80 = %10000000 =
  1045. %(110)00010 (10)000000. }
  1046. $C2 {%11000010}..$DF {%11011111}:
  1047. if (MaxLookAhead>=2) and
  1048. (ord(P[result+1]) and $C0=$80) then
  1049. begin
  1050. if not IncludeCombiningDiacriticalMarks then
  1051. exit(2);
  1052. if result>0 then
  1053. begin
  1054. cp:=ord(P[result]) and $1F {%11111} shl 6 or ord(P[result+1]) and $3F {%111111};
  1055. { Max possible cp value, $7FF, won't overflow L2. }
  1056. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1057. exit;
  1058. end;
  1059. Inc(result,2);
  1060. Dec(MaxLookAhead,2);
  1061. end
  1062. else
  1063. begin
  1064. cpLen:=2;
  1065. break;
  1066. end;
  1067. { Three-byte codepoints have the form
  1068. %(1110)xxxx (10)xxxxxx (10)xxxxxx
  1069. but also minimum value of $800 = %1000 00000000 =
  1070. %(1110)0000 (10)100000 (10)000000. }
  1071. $E0 {%11100000}..$EF {%11101111}:
  1072. if (MaxLookAhead>=3) and
  1073. (ord(P[result+1]) and $C0=$80) and
  1074. (ord(P[result+2]) and $C0=$80) and
  1075. ((ord(P[result])>$E0 {%11100000}) or
  1076. (ord(P[result+1])>=$A0 {%10100000})) then
  1077. begin
  1078. if not IncludeCombiningDiacriticalMarks then
  1079. exit(3);
  1080. if result>0 then
  1081. begin
  1082. cp:=ord(P[result]) and $F {%1111} shl 12 or ord(P[result+1]) and $3F {%111111} shl 6 or ord(P[result+2]) and $3F {%111111};
  1083. { Max possible cp value, $FFFF, won't overflow L2. }
  1084. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1085. exit;
  1086. end;
  1087. Inc(result,3);
  1088. Dec(MaxLookAhead,3);
  1089. end
  1090. else
  1091. begin
  1092. cpLen:=3;
  1093. break;
  1094. end;
  1095. { Four-byte codepoints have the form
  1096. %(11110)xxx (10)xxxxxx (10)xxxxxx (10)xxxxxx
  1097. but also minimum value of $10000 = %1 00000000 00000000 =
  1098. %(11110)000 (10)010000 (10)000000 (10)000000
  1099. and maximum of $10FFFF = %10000 11111111 11111111 =
  1100. %(11110)100 (10)001111 (10)111111 (10)111111. }
  1101. $F0 {%11110000}..$F4 {%11110100}:
  1102. if (MaxLookAhead>=4) and
  1103. (ord(P[result+1]) and $C0=$80) and
  1104. (ord(P[result+2]) and $C0=$80) and
  1105. (ord(P[result+3]) and $C0=$80) and
  1106. (uint16(P[result]) shl 8 or ord(P[result+1])>=$F090 {%11110000 10010000}) and
  1107. (uint16(P[result]) shl 8 or ord(P[result+1])<=$F48F {%11110100 10001111}) then
  1108. begin
  1109. if not IncludeCombiningDiacriticalMarks then
  1110. exit(4);
  1111. if result>0 then
  1112. begin
  1113. cp:=ord(P[result]) and $7 {%111} shl 18 or ord(P[result+1]) and $3F {%111111} shl 12 or ord(P[result+2]) and $3F {%111111} shl 6 or ord(P[result+3]) and $3F {%111111};
  1114. { This time, cp can overflow L2, and can have special-cased values U+E0100..U+E01EF. }
  1115. if cp<length(IsCombinings.L2) shl (5+4) then
  1116. begin
  1117. if IsCombinings.L0[IsCombinings.L1[IsCombinings.L2[cp shr (5+4)]][cp shr 5 and (1 shl 4-1)]] shr (cp and (1 shl 5-1)) and 1=0 then
  1118. exit;
  1119. end
  1120. else if not ((cp>=$E0100) and (cp<=$E01EF)) then
  1121. exit;
  1122. end;
  1123. Inc(result,4);
  1124. Dec(MaxLookAhead,4);
  1125. end
  1126. else
  1127. begin
  1128. cpLen:=4;
  1129. break;
  1130. end;
  1131. else
  1132. begin
  1133. cpLen:=1;
  1134. break;
  1135. end;
  1136. end;
  1137. until false;
  1138. { Handle invalid or incomplete cases, when expected codepoint length is cpLen. }
  1139. for iByte:=1 to cpLen-1 do
  1140. if (iByte<MaxLookAhead) and
  1141. (ord(P[result+iByte]) and $C0 {%11000000}<>$80 {%10000000}) then
  1142. begin
  1143. if result=0 then result:=-1-iByte;
  1144. exit;
  1145. end;
  1146. if cpLen>MaxLookAhead then
  1147. result:=0 { Signal an incomplete codepoint, even if there were complete codepoints before. }
  1148. else if result=0 then
  1149. result:=-cpLen;
  1150. end;
  1151. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1152. procedure fpc_chararray_to_shortstr(out res : shortstring;const arr: array of AnsiChar; zerobased: boolean = true);[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  1153. var
  1154. l: ObjpasInt;
  1155. index: ObjpasInt;
  1156. len: byte;
  1157. begin
  1158. l:=high(arr)+1;
  1159. if l>=ObjpasInt(high(res))+1 then
  1160. l:=high(res)
  1161. else if l<0 then
  1162. l:=0;
  1163. if zerobased then
  1164. begin
  1165. index:=IndexByte(arr[0],l,0);
  1166. if index<0 then
  1167. len:=l
  1168. else
  1169. len:=index;
  1170. end
  1171. else
  1172. len:=l;
  1173. move(arr[0],res[1],len);
  1174. res[0]:=chr(len);
  1175. end;
  1176. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  1177. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1178. procedure fpc_shortstr_to_chararray(out res: array of AnsiChar; const src: ShortString); compilerproc;
  1179. var
  1180. len: ObjpasInt;
  1181. begin
  1182. len := length(src);
  1183. if len > length(res) then
  1184. len := length(res);
  1185. {$push}{$r-}
  1186. { make sure we don't access AnsiChar 1 if length is 0 (JM) }
  1187. if len > 0 then
  1188. move(src[1],res[0],len);
  1189. fillchar(res[len],length(res)-len,0);
  1190. {$pop}
  1191. end;
  1192. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  1193. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1194. function fpc_pchar_length(p:PAnsiChar):sizeint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  1195. begin
  1196. if assigned(p) then
  1197. Result:=IndexByte(p^,-1,0)
  1198. else
  1199. Result:=0;
  1200. end;
  1201. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  1202. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1203. function fpc_pwidechar_length(p:pwidechar):sizeint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  1204. var i : sizeint;
  1205. begin
  1206. i:=0;
  1207. if assigned(p) then
  1208. while p[i]<>#0 do
  1209. inc(i);
  1210. exit(i);
  1211. end;
  1212. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  1213. {****************************************************************************
  1214. Caller/StackFrame Helpers
  1215. ****************************************************************************}
  1216. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  1217. {_$error Get_frame must be defined for each processor }
  1218. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  1219. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1220. {_$error Get_caller_addr must be defined for each processor }
  1221. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  1222. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1223. {_$error Get_caller_frame must be defined for each processor }
  1224. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  1225. {****************************************************************************
  1226. Math
  1227. ****************************************************************************}
  1228. {****************************************************************************
  1229. Software multiplication
  1230. ****************************************************************************}
  1231. {$ifdef FPC_INCLUDE_SOFTWARE_MUL}
  1232. {$ifdef VER3_0}
  1233. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1234. function fpc_mul_integer(f1,f2 : integer;checkoverflow : boolean) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1235. var
  1236. sign : boolean;
  1237. q1,q2,q3 : word;
  1238. begin
  1239. { there's no difference between signed and unsigned multiplication,
  1240. when the destination size is equal to the source size and overflow
  1241. checking is off }
  1242. if not checkoverflow then
  1243. { word(f1)*word(f2) is coded as a call to mulword }
  1244. fpc_mul_integer:=integer(word(f1)*word(f2))
  1245. else
  1246. begin
  1247. sign:=false;
  1248. if f1<0 then
  1249. begin
  1250. sign:=not(sign);
  1251. q1:=word(-f1);
  1252. end
  1253. else
  1254. q1:=f1;
  1255. if f2<0 then
  1256. begin
  1257. sign:=not(sign);
  1258. q2:=word(-f2);
  1259. end
  1260. else
  1261. q2:=f2;
  1262. { the q1*q2 is coded as call to mulword }
  1263. q3:=q1*q2;
  1264. if (q1 <> 0) and (q2 <>0) and
  1265. ((q1>q3) or (q2>q3) or
  1266. { the bit 63 can be only set if we have $8000 }
  1267. { and sign is true }
  1268. (q3 shr 15<>0) and
  1269. ((q3<>word(word(1) shl 15)) or not(sign))
  1270. ) then
  1271. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1272. if sign then
  1273. fpc_mul_integer:=-q3
  1274. else
  1275. fpc_mul_integer:=q3;
  1276. end;
  1277. end;
  1278. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1279. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1280. function fpc_mul_word(f1,f2 : word;checkoverflow : boolean) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1281. var
  1282. _f1,bitpos : word;
  1283. f1overflowed : boolean;
  1284. begin
  1285. fpc_mul_word:=0;
  1286. bitpos:=1;
  1287. f1overflowed:=false;
  1288. while f1<>0 do
  1289. begin
  1290. if (f2 and bitpos)<>0 then
  1291. begin
  1292. _f1:=fpc_mul_word;
  1293. fpc_mul_word:=fpc_mul_word+f1;
  1294. { if one of the operands is greater than the result an
  1295. overflow occurs }
  1296. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1297. ((_f1>fpc_mul_word) or (f1>fpc_mul_word)))) then
  1298. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1299. end;
  1300. { when bootstrapping, we forget about overflow checking for qword :) }
  1301. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1302. f1:=f1 shl 1;
  1303. bitpos:=bitpos shl 1;
  1304. end;
  1305. end;
  1306. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1307. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1308. function fpc_mul_longint(f1,f2 : longint;checkoverflow : boolean) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1309. var
  1310. sign : boolean;
  1311. q1,q2,q3 : dword;
  1312. begin
  1313. { there's no difference between signed and unsigned multiplication,
  1314. when the destination size is equal to the source size and overflow
  1315. checking is off }
  1316. if not checkoverflow then
  1317. { dword(f1)*dword(f2) is coded as a call to muldword }
  1318. fpc_mul_longint:=longint(dword(f1)*dword(f2))
  1319. else
  1320. begin
  1321. sign:=false;
  1322. if f1<0 then
  1323. begin
  1324. sign:=not(sign);
  1325. q1:=dword(-f1);
  1326. end
  1327. else
  1328. q1:=f1;
  1329. if f2<0 then
  1330. begin
  1331. sign:=not(sign);
  1332. q2:=dword(-f2);
  1333. end
  1334. else
  1335. q2:=f2;
  1336. { the q1*q2 is coded as call to muldword }
  1337. q3:=q1*q2;
  1338. if (q1 <> 0) and (q2 <>0) and
  1339. ((q1>q3) or (q2>q3) or
  1340. { the bit 31 can be only set if we have $8000 0000 }
  1341. { and sign is true }
  1342. (q3 shr 15<>0) and
  1343. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1344. ) then
  1345. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1346. if sign then
  1347. fpc_mul_longint:=-q3
  1348. else
  1349. fpc_mul_longint:=q3;
  1350. end;
  1351. end;
  1352. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1353. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1354. { multiplies two dwords
  1355. the longbool for checkoverflow avoids a misaligned stack
  1356. }
  1357. function fpc_mul_dword(f1,f2 : dword;checkoverflow : boolean) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1358. var
  1359. _f1,bitpos : dword;
  1360. f1overflowed : boolean;
  1361. begin
  1362. fpc_mul_dword:=0;
  1363. bitpos:=1;
  1364. f1overflowed:=false;
  1365. while f1<>0 do
  1366. begin
  1367. if (f2 and bitpos)<>0 then
  1368. begin
  1369. _f1:=fpc_mul_dword;
  1370. fpc_mul_dword:=fpc_mul_dword+f1;
  1371. { if one of the operands is greater than the result an
  1372. overflow occurs }
  1373. if checkoverflow and (f1overflowed or ((_f1<>0) and (f1<>0) and
  1374. ((_f1>fpc_mul_dword) or (f1>fpc_mul_dword)))) then
  1375. HandleErrorAddrFrameInd(215,get_pc_addr,get_frame);
  1376. end;
  1377. { when bootstrapping, we forget about overflow checking for qword :) }
  1378. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1379. f1:=f1 shl 1;
  1380. bitpos:=bitpos shl 1;
  1381. end;
  1382. end;
  1383. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1384. {$else VER3_0}
  1385. {$ifndef FPC_SYSTEM_HAS_MUL_SHORTINT}
  1386. function fpc_mul_shortint(f1,f2 : shortint) : shortint;[public,alias: 'FPC_MUL_SHORTINT']; compilerproc;
  1387. begin
  1388. { there's no difference between signed and unsigned multiplication,
  1389. when the destination size is equal to the source size and overflow
  1390. checking is off }
  1391. { byte(f1) * byte(f2) is coded as a call to mul_byte }
  1392. fpc_mul_shortint := shortint(byte(f1) * byte(f2));
  1393. end;
  1394. function fpc_mul_shortint_checkoverflow(f1,f2 : shortint) : shortint;[public,alias: 'FPC_MUL_SHORTINT_CHECKOVERFLOW']; compilerproc;
  1395. var
  1396. sign : boolean;
  1397. q1,q2,q3 : byte;
  1398. begin
  1399. sign:=false;
  1400. if f1 < 0 then
  1401. begin
  1402. sign := not(sign);
  1403. q1 := byte(-f1);
  1404. end
  1405. else
  1406. q1 := f1;
  1407. if f2 < 0 then
  1408. begin
  1409. sign := not(sign);
  1410. q2 := byte(-f2);
  1411. end
  1412. else
  1413. q2 := f2;
  1414. { the q1*q2 is coded as call to mul_byte }
  1415. {$push}
  1416. {$Q+}
  1417. q3 := q1 * q2;
  1418. {$pop}
  1419. if (q1 <> 0) and (q2 <> 0) and
  1420. ((q1 > q3) or (q2 > q3) or
  1421. { the bit 7 can be only set if we have $80 }
  1422. { and sign is true }
  1423. (q3 shr 7 <> 0) and
  1424. ((q3 <> byte(byte(1) shl 7)) or not(sign))
  1425. ) then
  1426. FPC_Overflow();
  1427. if sign then
  1428. fpc_mul_shortint_checkoverflow := -q3
  1429. else
  1430. fpc_mul_shortint_checkoverflow := q3;
  1431. end;
  1432. {$endif FPC_SYSTEM_HAS_MUL_SHORTINT}
  1433. {$ifndef FPC_SYSTEM_HAS_MUL_BYTE}
  1434. function fpc_mul_byte(f1,f2 : byte) : byte;[public,alias: 'FPC_MUL_BYTE']; compilerproc;
  1435. var
  1436. v1,v2,res: byte;
  1437. begin
  1438. if f1<f2 then
  1439. begin
  1440. v1:=f1;
  1441. v2:=f2;
  1442. end
  1443. else
  1444. begin
  1445. v1:=f2;
  1446. v2:=f1;
  1447. end;
  1448. res:=0;
  1449. while v1<>0 do
  1450. begin
  1451. if v1 and 1<>0 then
  1452. inc(res,v2);
  1453. v2:=v2 shl 1;
  1454. v1:=v1 shr 1;
  1455. end;
  1456. fpc_mul_byte:=res;
  1457. end;
  1458. function fpc_mul_byte_checkoverflow(f1,f2 : byte) : byte;[public,alias: 'FPC_MUL_BYTE_CHECKOVERFLOW']; compilerproc;
  1459. var
  1460. _f1, bitpos : byte;
  1461. f1overflowed : boolean;
  1462. begin
  1463. fpc_mul_byte_checkoverflow := 0;
  1464. bitpos := 1;
  1465. f1overflowed := false;
  1466. while f1<>0 do
  1467. begin
  1468. if (f2 and bitpos) <> 0 then
  1469. begin
  1470. _f1 := fpc_mul_byte_checkoverflow;
  1471. fpc_mul_byte_checkoverflow := fpc_mul_byte_checkoverflow + f1;
  1472. { if one of the operands is greater than the result an
  1473. overflow occurs }
  1474. if f1overflowed or ((_f1 <> 0) and (f1 <> 0) and
  1475. ((_f1 > fpc_mul_byte_checkoverflow) or (f1 > fpc_mul_byte_checkoverflow))) then
  1476. FPC_Overflow();
  1477. end;
  1478. { when bootstrapping, we forget about overflow checking for qword :) }
  1479. f1overflowed := f1overflowed or ((f1 and (1 shl 7)) <> 0);
  1480. f1 := f1 shl 1;
  1481. bitpos := bitpos shl 1;
  1482. end;
  1483. end;
  1484. {$endif FPC_SYSTEM_HAS_MUL_BYTE}
  1485. {$ifndef FPC_SYSTEM_HAS_MUL_INTEGER}
  1486. function fpc_mul_integer(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER']; compilerproc;
  1487. begin
  1488. { there's no difference between signed and unsigned multiplication,
  1489. when the destination size is equal to the source size and overflow
  1490. checking is off }
  1491. { word(f1)*word(f2) is coded as a call to mulword }
  1492. fpc_mul_integer:=integer(word(f1)*word(f2));
  1493. end;
  1494. function fpc_mul_integer_checkoverflow(f1,f2 : integer) : integer;[public,alias: 'FPC_MUL_INTEGER_CHECKOVERFLOW']; compilerproc;
  1495. var
  1496. sign : boolean;
  1497. q1,q2,q3 : word;
  1498. begin
  1499. sign:=false;
  1500. if f1<0 then
  1501. begin
  1502. sign:=not(sign);
  1503. q1:=word(-f1);
  1504. end
  1505. else
  1506. q1:=f1;
  1507. if f2<0 then
  1508. begin
  1509. sign:=not(sign);
  1510. q2:=word(-f2);
  1511. end
  1512. else
  1513. q2:=f2;
  1514. { the q1*q2 is coded as call to mulword }
  1515. {$push}
  1516. {$Q+}
  1517. q3:=q1*q2;
  1518. {$pop}
  1519. if (q1 <> 0) and (q2 <>0) and
  1520. ((q1>q3) or (q2>q3) or
  1521. { the bit 63 can be only set if we have $8000 }
  1522. { and sign is true }
  1523. (q3 shr 15<>0) and
  1524. ((q3<>word(word(1) shl 15)) or not(sign))
  1525. ) then
  1526. FPC_Overflow();
  1527. if sign then
  1528. fpc_mul_integer_checkoverflow:=-q3
  1529. else
  1530. fpc_mul_integer_checkoverflow:=q3;
  1531. end;
  1532. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1533. {$ifndef FPC_SYSTEM_HAS_MUL_WORD}
  1534. function fpc_mul_word(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD']; compilerproc;
  1535. var
  1536. v1,v2,res: word;
  1537. begin
  1538. if f1<f2 then
  1539. begin
  1540. v1:=f1;
  1541. v2:=f2;
  1542. end
  1543. else
  1544. begin
  1545. v1:=f2;
  1546. v2:=f1;
  1547. end;
  1548. res:=0;
  1549. while v1<>0 do
  1550. begin
  1551. if ALUUInt(v1) and 1<>0 then
  1552. inc(res,v2);
  1553. v2:=v2 shl 1;
  1554. v1:=v1 shr 1;
  1555. end;
  1556. fpc_mul_word:=res;
  1557. end;
  1558. function fpc_mul_word_checkoverflow(f1,f2 : word) : word;[public,alias: 'FPC_MUL_WORD_CHECKOVERFLOW']; compilerproc;
  1559. var
  1560. _f1,bitpos : word;
  1561. f1overflowed : boolean;
  1562. begin
  1563. fpc_mul_word_checkoverflow:=0;
  1564. bitpos:=1;
  1565. f1overflowed:=false;
  1566. while f1<>0 do
  1567. begin
  1568. if (f2 and bitpos)<>0 then
  1569. begin
  1570. _f1:=fpc_mul_word_checkoverflow;
  1571. fpc_mul_word_checkoverflow:=fpc_mul_word_checkoverflow+f1;
  1572. { if one of the operands is greater than the result an
  1573. overflow occurs }
  1574. if f1overflowed or ((_f1<>0) and (f1<>0) and
  1575. ((_f1>fpc_mul_word_checkoverflow) or (f1>fpc_mul_word_checkoverflow))) then
  1576. FPC_Overflow();
  1577. end;
  1578. { when bootstrapping, we forget about overflow checking for qword :) }
  1579. f1overflowed:=f1overflowed or ((f1 and (1 shl 15))<>0);
  1580. f1:=f1 shl 1;
  1581. bitpos:=bitpos shl 1;
  1582. end;
  1583. end;
  1584. {$endif FPC_SYSTEM_HAS_MUL_WORD}
  1585. {$ifndef FPC_SYSTEM_HAS_MUL_LONGINT}
  1586. function fpc_mul_longint(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT']; compilerproc;
  1587. begin
  1588. { there's no difference between signed and unsigned multiplication,
  1589. when the destination size is equal to the source size and overflow
  1590. checking is off }
  1591. { dword(f1)*dword(f2) is coded as a call to muldword }
  1592. fpc_mul_longint:=longint(dword(f1)*dword(f2));
  1593. end;
  1594. function fpc_mul_longint_checkoverflow(f1,f2 : longint) : longint;[public,alias: 'FPC_MUL_LONGINT_CHECKOVERFLOW']; compilerproc;
  1595. var
  1596. sign : boolean;
  1597. q1,q2,q3 : dword;
  1598. begin
  1599. sign:=false;
  1600. if f1<0 then
  1601. begin
  1602. sign:=not(sign);
  1603. q1:=dword(-f1);
  1604. end
  1605. else
  1606. q1:=f1;
  1607. if f2<0 then
  1608. begin
  1609. sign:=not(sign);
  1610. q2:=dword(-f2);
  1611. end
  1612. else
  1613. q2:=f2;
  1614. { the q1*q2 is coded as call to muldword }
  1615. {$push}
  1616. {$Q+}
  1617. q3:=q1*q2;
  1618. {$pop}
  1619. if (q1 <> 0) and (q2 <>0) and
  1620. ((q1>q3) or (q2>q3) or
  1621. { the bit 31 can be only set if we have $8000 0000 }
  1622. { and sign is true }
  1623. (q3 shr 31<>0) and
  1624. ((q3<>dword(dword(1) shl 31)) or not(sign))
  1625. ) then
  1626. FPC_Overflow();
  1627. if sign then
  1628. fpc_mul_longint_checkoverflow:=-q3
  1629. else
  1630. fpc_mul_longint_checkoverflow:=q3;
  1631. end;
  1632. {$endif FPC_SYSTEM_HAS_MUL_INTEGER}
  1633. {$ifndef FPC_SYSTEM_HAS_MUL_DWORD}
  1634. function fpc_mul_dword(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD']; compilerproc;
  1635. var
  1636. v1,v2,res: dword;
  1637. begin
  1638. if f1<f2 then
  1639. begin
  1640. v1:=f1;
  1641. v2:=f2;
  1642. end
  1643. else
  1644. begin
  1645. v1:=f2;
  1646. v2:=f1;
  1647. end;
  1648. res:=0;
  1649. while v1<>0 do
  1650. begin
  1651. if ALUUInt(v1) and 1<>0 then
  1652. inc(res,v2);
  1653. v2:=v2 shl 1;
  1654. v1:=v1 shr 1;
  1655. end;
  1656. fpc_mul_dword:=res;
  1657. end;
  1658. function fpc_mul_dword_checkoverflow(f1,f2 : dword) : dword;[public,alias: 'FPC_MUL_DWORD_CHECKOVERFLOW']; compilerproc;
  1659. var
  1660. _f1,bitpos : dword;
  1661. f1overflowed : boolean;
  1662. begin
  1663. fpc_mul_dword_checkoverflow:=0;
  1664. bitpos:=1;
  1665. f1overflowed:=false;
  1666. while f1<>0 do
  1667. begin
  1668. if (f2 and bitpos)<>0 then
  1669. begin
  1670. _f1:=fpc_mul_dword_checkoverflow;
  1671. fpc_mul_dword_checkoverflow:=fpc_mul_dword_checkoverflow+f1;
  1672. { if one of the operands is greater than the result an
  1673. overflow occurs }
  1674. if f1overflowed or ((_f1<>0) and (f1<>0) and
  1675. ((_f1>fpc_mul_dword_checkoverflow) or (f1>fpc_mul_dword_checkoverflow))) then
  1676. FPC_Overflow();
  1677. end;
  1678. { when bootstrapping, we forget about overflow checking for qword :) }
  1679. f1overflowed:=f1overflowed or ((f1 and (dword(1) shl 31))<>0);
  1680. f1:=f1 shl 1;
  1681. bitpos:=bitpos shl 1;
  1682. end;
  1683. end;
  1684. {$endif FPC_SYSTEM_HAS_MUL_DWORD}
  1685. {$endif VER3_0}
  1686. {$endif FPC_INCLUDE_SOFTWARE_MUL}
  1687. {****************************************************************************
  1688. Software longint/dword division
  1689. ****************************************************************************}
  1690. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  1691. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  1692. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  1693. var
  1694. shift,lzz,lzn : ObjpasInt;
  1695. begin
  1696. result:=0;
  1697. if n=0 then
  1698. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1699. if z=0 then
  1700. exit;
  1701. lzz:=BsrDWord(z);
  1702. lzn:=BsrDWord(n);
  1703. { if the denominator contains less zeros
  1704. then the numerator
  1705. then d is greater than the n }
  1706. if lzn>lzz then
  1707. exit;
  1708. shift:=lzz-lzn;
  1709. n:=n shl shift;
  1710. for shift:=shift downto 0 do
  1711. begin
  1712. if z>=n then
  1713. begin
  1714. z:=z-n;
  1715. result:=result+dword(dword(1) shl shift);
  1716. end;
  1717. n:=n shr 1;
  1718. end;
  1719. end;
  1720. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  1721. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  1722. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  1723. var
  1724. shift,lzz,lzn : ObjpasInt;
  1725. begin
  1726. result:=0;
  1727. if n=0 then
  1728. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1729. if z=0 then
  1730. exit;
  1731. lzz:=BsrDWord(z);
  1732. lzn:=BsrDWord(n);
  1733. { if the denominator contains less zeros
  1734. then the numerator
  1735. then d is greater than the n }
  1736. if lzn>lzz then
  1737. begin
  1738. result:=z;
  1739. exit;
  1740. end;
  1741. shift:=lzz-lzn;
  1742. n:=n shl shift;
  1743. for shift:=shift downto 0 do
  1744. begin
  1745. if z>=n then
  1746. z:=z-n;
  1747. n:=n shr 1;
  1748. end;
  1749. result:=z;
  1750. end;
  1751. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  1752. {$ifndef FPC_SYSTEM_HAS_DIV_WORD}
  1753. function fpc_div_word(n,z : word) : word; [public,alias: 'FPC_DIV_WORD']; compilerproc;
  1754. var
  1755. shift,lzz,lzn : Byte;
  1756. begin
  1757. result:=0;
  1758. if n=0 then
  1759. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1760. if z=0 then
  1761. exit;
  1762. lzz:=BsrWord(z);
  1763. lzn:=BsrWord(n);
  1764. { if the denominator contains less zeros
  1765. then the numerator
  1766. then d is greater than the n }
  1767. if lzn>lzz then
  1768. exit;
  1769. shift:=lzz-lzn;
  1770. n:=n shl shift;
  1771. for shift:=shift downto 0 do
  1772. begin
  1773. if z>=n then
  1774. begin
  1775. z:=z-n;
  1776. result:=result+word(word(1) shl shift);
  1777. end;
  1778. n:=n shr 1;
  1779. end;
  1780. end;
  1781. {$endif FPC_SYSTEM_HAS_DIV_WORD}
  1782. {$ifndef FPC_SYSTEM_HAS_MOD_WORD}
  1783. function fpc_mod_word(n,z : word) : word; [public,alias: 'FPC_MOD_WORD']; compilerproc;
  1784. var
  1785. shift,lzz,lzn : Byte;
  1786. begin
  1787. result:=0;
  1788. if n=0 then
  1789. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1790. if z=0 then
  1791. exit;
  1792. lzz:=BsrWord(z);
  1793. lzn:=BsrWord(n);
  1794. { if the denominator contains less zeros
  1795. then the numerator
  1796. then d is greater than the n }
  1797. if lzn>lzz then
  1798. begin
  1799. result:=z;
  1800. exit;
  1801. end;
  1802. shift:=lzz-lzn;
  1803. n:=n shl shift;
  1804. for shift:=shift downto 0 do
  1805. begin
  1806. if z>=n then
  1807. z:=z-n;
  1808. n:=n shr 1;
  1809. end;
  1810. result:=z;
  1811. end;
  1812. {$endif FPC_SYSTEM_HAS_MOD_WORD}
  1813. {$ifndef FPC_SYSTEM_HAS_DIV_BYTE}
  1814. function fpc_div_byte(n,z : byte) : byte; [public,alias: 'FPC_DIV_BYTE']; compilerproc;
  1815. var
  1816. shift,lzz,lzn : Byte;
  1817. begin
  1818. result:=0;
  1819. if n=0 then
  1820. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1821. if z=0 then
  1822. exit;
  1823. lzz:=BsrByte(z);
  1824. lzn:=BsrByte(n);
  1825. { if the denominator contains less zeros
  1826. then the numerator
  1827. then d is greater than the n }
  1828. if lzn>lzz then
  1829. exit;
  1830. shift:=lzz-lzn;
  1831. n:=n shl shift;
  1832. for shift:=shift downto 0 do
  1833. begin
  1834. if z>=n then
  1835. begin
  1836. z:=z-n;
  1837. result:=result+byte(byte(1) shl shift);
  1838. end;
  1839. n:=n shr 1;
  1840. end;
  1841. end;
  1842. {$endif FPC_SYSTEM_HAS_DIV_BYTE}
  1843. {$ifndef FPC_SYSTEM_HAS_MOD_BYTE}
  1844. function fpc_mod_byte(n,z : byte) : byte; [public,alias: 'FPC_MOD_BYTE']; compilerproc;
  1845. var
  1846. shift,lzz,lzn : Byte;
  1847. begin
  1848. result:=0;
  1849. if n=0 then
  1850. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1851. if z=0 then
  1852. exit;
  1853. lzz:=BsrByte(z);
  1854. lzn:=BsrByte(n);
  1855. { if the denominator contains less zeros
  1856. then the numerator
  1857. then d is greater than the n }
  1858. if lzn>lzz then
  1859. begin
  1860. result:=z;
  1861. exit;
  1862. end;
  1863. shift:=lzz-lzn;
  1864. n:=n shl shift;
  1865. for shift:=shift downto 0 do
  1866. begin
  1867. if z>=n then
  1868. z:=z-n;
  1869. n:=n shr 1;
  1870. end;
  1871. result:=z;
  1872. end;
  1873. {$endif FPC_SYSTEM_HAS_MOD_BYTE}
  1874. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  1875. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  1876. var
  1877. sign : boolean;
  1878. d1,d2 : dword;
  1879. begin
  1880. if n=0 then
  1881. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1882. sign:=false;
  1883. if z<0 then
  1884. begin
  1885. sign:=not(sign);
  1886. d1:=dword(-z);
  1887. end
  1888. else
  1889. d1:=z;
  1890. if n<0 then
  1891. begin
  1892. sign:=not(sign);
  1893. d2:=dword(-n);
  1894. end
  1895. else
  1896. d2:=n;
  1897. { the div is coded by the compiler as call to divdword }
  1898. if sign then
  1899. result:=-(d1 div d2)
  1900. else
  1901. result:=d1 div d2;
  1902. end;
  1903. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  1904. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  1905. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  1906. var
  1907. signed : boolean;
  1908. r,nq,zq : dword;
  1909. begin
  1910. if n=0 then
  1911. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1912. nq:=abs(n);
  1913. if z<0 then
  1914. begin
  1915. zq:=dword(-z);
  1916. signed:=true;
  1917. end
  1918. else
  1919. begin
  1920. zq:=z;
  1921. signed:=false;
  1922. end;
  1923. r:=zq mod nq;
  1924. if signed then
  1925. result:=-longint(r)
  1926. else
  1927. result:=r;
  1928. end;
  1929. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  1930. {$ifndef FPC_SYSTEM_HAS_DIV_SMALLINT}
  1931. function fpc_div_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_DIV_SMALLINT']; compilerproc;
  1932. var
  1933. sign : boolean;
  1934. w1,w2 : word;
  1935. begin
  1936. if n=0 then
  1937. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1938. sign:=false;
  1939. if z<0 then
  1940. begin
  1941. sign:=not(sign);
  1942. w1:=word(-z);
  1943. end
  1944. else
  1945. w1:=z;
  1946. if n<0 then
  1947. begin
  1948. sign:=not(sign);
  1949. w2:=word(-n);
  1950. end
  1951. else
  1952. w2:=n;
  1953. { the div is coded by the compiler as call to divdword }
  1954. if sign then
  1955. result:=-(w1 div w2)
  1956. else
  1957. result:=w1 div w2;
  1958. end;
  1959. {$endif FPC_SYSTEM_HAS_DIV_SMALLINT}
  1960. {$ifndef FPC_SYSTEM_HAS_MOD_SMALLINT}
  1961. function fpc_mod_smallint(n,z : smallint) : smallint; [public,alias: 'FPC_MOD_SMALLINT']; compilerproc;
  1962. var
  1963. signed : boolean;
  1964. r,nq,zq : word;
  1965. begin
  1966. if n=0 then
  1967. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1968. nq:=abs(n);
  1969. if z<0 then
  1970. begin
  1971. zq:=word(-z);
  1972. signed:=true;
  1973. end
  1974. else
  1975. begin
  1976. zq:=z;
  1977. signed:=false;
  1978. end;
  1979. r:=zq mod nq;
  1980. if signed then
  1981. result:=-smallint(r)
  1982. else
  1983. result:=r;
  1984. end;
  1985. {$endif FPC_SYSTEM_HAS_MOD_SMALLINT}
  1986. {$ifndef FPC_SYSTEM_HAS_DIV_SHORTINT}
  1987. function fpc_div_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_DIV_SHORTINT']; compilerproc;
  1988. var
  1989. sign : boolean;
  1990. b1,b2 : byte;
  1991. begin
  1992. if n=0 then
  1993. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  1994. sign:=false;
  1995. if z<0 then
  1996. begin
  1997. sign:=not(sign);
  1998. b1:=byte(-z);
  1999. end
  2000. else
  2001. b1:=z;
  2002. if n<0 then
  2003. begin
  2004. sign:=not(sign);
  2005. b2:=byte(-n);
  2006. end
  2007. else
  2008. b2:=n;
  2009. { the div is coded by the compiler as call to divdword }
  2010. if sign then
  2011. result:=-(b1 div b2)
  2012. else
  2013. result:=b1 div b2;
  2014. end;
  2015. {$endif FPC_SYSTEM_HAS_DIV_SHORTINT}
  2016. {$ifndef FPC_SYSTEM_HAS_MOD_SHORTINT}
  2017. function fpc_mod_shortint(n,z : shortint) : shortint; [public,alias: 'FPC_MOD_SHORTINT']; compilerproc;
  2018. var
  2019. signed : boolean;
  2020. r,nq,zq : byte;
  2021. begin
  2022. if n=0 then
  2023. HandleErrorAddrFrameInd(200,get_pc_addr,get_frame);
  2024. nq:=abs(n);
  2025. if z<0 then
  2026. begin
  2027. zq:=byte(-z);
  2028. signed:=true;
  2029. end
  2030. else
  2031. begin
  2032. zq:=z;
  2033. signed:=false;
  2034. end;
  2035. r:=zq mod nq;
  2036. if signed then
  2037. result:=-shortint(r)
  2038. else
  2039. result:=r;
  2040. end;
  2041. {$endif FPC_SYSTEM_HAS_MOD_SHORTINT}
  2042. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  2043. {****************************************************************************}
  2044. {$if defined(CPUINT8)}
  2045. {$ifndef FPC_SYSTEM_HAS_ABS_SHORTINT}
  2046. function abs(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2047. begin
  2048. if l<0 then
  2049. abs:=-l
  2050. else
  2051. abs:=l;
  2052. end;
  2053. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  2054. {$endif CPUINT8}
  2055. {$if defined(CPUINT16) or defined(CPUINT8)}
  2056. {$ifndef FPC_SYSTEM_HAS_ABS_SMALLINT}
  2057. function abs(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2058. begin
  2059. if l<0 then
  2060. abs:=-l
  2061. else
  2062. abs:=l;
  2063. end;
  2064. {$endif not FPC_SYSTEM_HAS_ABS_SMALLINT}
  2065. {$endif CPUINT16 or CPUINT8}
  2066. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  2067. { This is only needed to bootstrap on SPARC targets
  2068. (MIPS and m68k too, but they have no releases, so bootstrapping is not an issue) }
  2069. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2070. begin
  2071. if l<0 then
  2072. abs:=-l
  2073. else
  2074. abs:=l;
  2075. end;
  2076. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  2077. {$if defined(CPUINT8)}
  2078. {$ifndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  2079. function odd(l:shortint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2080. begin
  2081. odd:=boolean(l and 1);
  2082. end;
  2083. {$endif ndef FPC_SYSTEM_HAS_ODD_SHORTINT}
  2084. {$ifndef FPC_SYSTEM_HAS_ODD_BYTE}
  2085. function odd(l:byte):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2086. begin
  2087. odd:=boolean(l and 1);
  2088. end;
  2089. {$endif ndef FPC_SYSTEM_HAS_ODD_BYTE}
  2090. {$endif CPUINT8}
  2091. {$if defined(CPUINT16) or defined(CPUINT8)}
  2092. {$ifndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  2093. function odd(l:smallint):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2094. begin
  2095. odd:=boolean(l and 1);
  2096. end;
  2097. {$endif ndef FPC_SYSTEM_HAS_ODD_SMALLINT}
  2098. {$ifndef FPC_SYSTEM_HAS_ODD_WORD}
  2099. function odd(l:word):Boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2100. begin
  2101. odd:=boolean(l and 1);
  2102. end;
  2103. {$endif ndef FPC_SYSTEM_HAS_ODD_WORD}
  2104. {$endif CPUINT16 or CPUINT8}
  2105. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  2106. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2107. begin
  2108. odd:=boolean(l and 1);
  2109. end;
  2110. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  2111. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  2112. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2113. begin
  2114. odd:=boolean(l and 1);
  2115. end;
  2116. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  2117. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  2118. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2119. begin
  2120. odd:=boolean(longint(l) and 1);
  2121. end;
  2122. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  2123. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  2124. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  2125. begin
  2126. odd:=boolean(longint(l) and 1);
  2127. end;
  2128. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  2129. {$if defined(CPUINT8)}
  2130. {$ifndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  2131. function sqr(l:shortint):shortint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2132. begin
  2133. sqr:=l*l;
  2134. end;
  2135. {$endif ndef FPC_SYSTEM_HAS_SQR_SHORTINT}
  2136. {$endif CPUINT8}
  2137. {$if defined(CPUINT16) or defined(CPUINT8)}
  2138. {$ifndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  2139. function sqr(l:smallint):smallint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2140. begin
  2141. sqr:=l*l;
  2142. end;
  2143. {$endif ndef FPC_SYSTEM_HAS_SQR_SMALLINT}
  2144. {$endif CPUINT16 or CPUINT8}
  2145. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  2146. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  2147. begin
  2148. sqr:=l*l;
  2149. end;
  2150. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  2151. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  2152. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2153. begin
  2154. if l < 0 then
  2155. abs := -l
  2156. else
  2157. abs := l;
  2158. end;
  2159. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  2160. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  2161. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2162. begin
  2163. sqr := l*l;
  2164. end;
  2165. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  2166. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  2167. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2168. begin
  2169. sqr := l*l;
  2170. end;
  2171. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  2172. {$ifdef CPU16}
  2173. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  2174. function declocked(var l:smallint):boolean;
  2175. begin
  2176. Dec(l);
  2177. declocked:=(l=0);
  2178. end;
  2179. {$endif FPC_SYSTEM_HAS_DECLOCKED_SMALLINT}
  2180. {$endif CPU16}
  2181. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  2182. function declocked(var l:longint):boolean;
  2183. begin
  2184. Dec(l);
  2185. declocked:=(l=0);
  2186. end;
  2187. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  2188. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  2189. function declocked(var l:int64):boolean;
  2190. begin
  2191. Dec(l);
  2192. declocked:=(l=0);
  2193. end;
  2194. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  2195. {$ifdef CPU16}
  2196. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2197. procedure inclocked(var l:smallint);
  2198. begin
  2199. Inc(l);
  2200. end;
  2201. {$endif FPC_SYSTEM_HAS_INCLOCKED_SMALLINT}
  2202. {$endif CPU16}
  2203. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2204. procedure inclocked(var l:longint);
  2205. begin
  2206. Inc(l);
  2207. end;
  2208. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  2209. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2210. procedure inclocked(var l:int64);
  2211. begin
  2212. Inc(l);
  2213. end;
  2214. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  2215. {$ifndef FPC_SYSTEM_HAS_SPTR}
  2216. {_$error Sptr must be defined for each processor }
  2217. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  2218. {****************************************************************************
  2219. Str()
  2220. ****************************************************************************}
  2221. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2222. procedure int_str(l:longint;out s:shortstring);
  2223. var
  2224. m,m1 : longword;
  2225. pcstart,
  2226. pc2start,
  2227. pc,pc2 : PAnsiChar;
  2228. hs : string[32];
  2229. overflow : longint;
  2230. begin
  2231. pc2start:=@s[1];
  2232. pc2:=pc2start;
  2233. if (l<0) then
  2234. begin
  2235. pc2^:='-';
  2236. inc(pc2);
  2237. m:=longword(-l);
  2238. end
  2239. else
  2240. m:=longword(l);
  2241. pcstart:=PAnsiChar(@hs[0]);
  2242. pc:=pcstart;
  2243. repeat
  2244. m1:=m div 10;
  2245. inc(pc);
  2246. pc^:=AnsiChar(m-(m1*10)+byte('0'));
  2247. m:=m1;
  2248. until m=0;
  2249. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2250. if overflow>0 then
  2251. inc(pcstart,overflow);
  2252. while (pc>pcstart) do
  2253. begin
  2254. pc2^:=pc^;
  2255. inc(pc2);
  2256. dec(pc);
  2257. end;
  2258. s[0]:=AnsiChar(pc2-pc2start);
  2259. end;
  2260. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  2261. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2262. procedure int_str_unsigned(l:longword;out s:shortstring);
  2263. var
  2264. m1 : longword;
  2265. pcstart,
  2266. pc2start,
  2267. pc,pc2 : PAnsiChar;
  2268. hs : string[32];
  2269. overflow : longint;
  2270. begin
  2271. pc2start:=@s[1];
  2272. pc2:=pc2start;
  2273. pcstart:=PAnsiChar(@hs[0]);
  2274. pc:=pcstart;
  2275. repeat
  2276. inc(pc);
  2277. m1:=l div 10;
  2278. pc^:=AnsiChar(l-(m1*10)+byte('0'));
  2279. l:=m1;
  2280. until l=0;
  2281. overflow:=(pc-pcstart)-high(s);
  2282. if overflow>0 then
  2283. inc(pcstart,overflow);
  2284. while (pc>pcstart) do
  2285. begin
  2286. pc2^:=pc^;
  2287. inc(pc2);
  2288. dec(pc);
  2289. end;
  2290. s[0]:=AnsiChar(pc2-pc2start);
  2291. end;
  2292. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  2293. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2294. procedure int_str(l:int64;out s:shortstring);
  2295. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2296. begin
  2297. runerror(217);
  2298. end;
  2299. {$else EXCLUDE_COMPLEX_PROCS}
  2300. var
  2301. m,m1 : qword;
  2302. pcstart,
  2303. pc2start,
  2304. pc,pc2 : PAnsiChar;
  2305. hs : string[32];
  2306. overflow : longint;
  2307. begin
  2308. pc2start:=@s[1];
  2309. pc2:=pc2start;
  2310. if (l<0) then
  2311. begin
  2312. pc2^:='-';
  2313. inc(pc2);
  2314. m:=qword(-l);
  2315. end
  2316. else
  2317. m:=qword(l);
  2318. pcstart:=PAnsiChar(@hs[0]);
  2319. pc:=pcstart;
  2320. repeat
  2321. m1:=m div 10;
  2322. inc(pc);
  2323. pc^:=AnsiChar(m-(m1*10)+byte('0'));
  2324. m:=m1;
  2325. until m=0;
  2326. overflow:=(pc-pcstart)+(pc2-pc2start)-high(s);
  2327. if overflow>0 then
  2328. inc(pcstart,overflow);
  2329. while (pc>pcstart) do
  2330. begin
  2331. pc2^:=pc^;
  2332. inc(pc2);
  2333. dec(pc);
  2334. end;
  2335. s[0]:=AnsiChar(pc2-pc2start);
  2336. end;
  2337. {$endif EXCLUDE_COMPLEX_PROCS}
  2338. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  2339. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2340. procedure int_str_unsigned(l:qword;out s:shortstring);
  2341. {$ifdef EXCLUDE_COMPLEX_PROCS}
  2342. begin
  2343. runerror(217);
  2344. end;
  2345. {$else EXCLUDE_COMPLEX_PROCS}
  2346. var
  2347. m1 : qword;
  2348. pcstart,
  2349. pc2start,
  2350. pc,pc2 : PAnsiChar;
  2351. hs : string[64];
  2352. overflow : longint;
  2353. begin
  2354. pc2start:=@s[1];
  2355. pc2:=pc2start;
  2356. pcstart:=PAnsiChar(@hs[0]);
  2357. pc:=pcstart;
  2358. repeat
  2359. inc(pc);
  2360. m1:=l div 10;
  2361. pc^:=AnsiChar(l-(m1*10)+byte('0'));
  2362. l:=m1;
  2363. until l=0;
  2364. overflow:=(pc-pcstart)-high(s);
  2365. if overflow>0 then
  2366. inc(pcstart,overflow);
  2367. while (pc>pcstart) do
  2368. begin
  2369. pc2^:=pc^;
  2370. inc(pc2);
  2371. dec(pc);
  2372. end;
  2373. s[0]:=AnsiChar(pc2-pc2start);
  2374. end;
  2375. {$endif EXCLUDE_COMPLEX_PROCS}
  2376. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  2377. {$ifndef FPUNONE}
  2378. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  2379. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2380. begin
  2381. softfloat_exception_flags:=[];
  2382. {$if declared(DefaultFPUControlWord)}
  2383. SetNativeFPUControlWord(DefaultFPUControlWord);
  2384. {$endif}
  2385. end;
  2386. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  2387. {$ifndef FPC_SYSTEM_HAS_SYSINITFPU}
  2388. procedure SysInitFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  2389. begin
  2390. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  2391. softfloat_exception_flags:=[];
  2392. end;
  2393. {$endif FPC_SYSTEM_HAS_SYSINITFPU}
  2394. {$endif}
  2395. {$ifndef FPC_SYSTEM_HAS_FPC_CPUINIT}
  2396. procedure fpc_cpuinit;
  2397. begin
  2398. {$ifndef FPUNONE}
  2399. {$ifdef FPC_HAS_FEATURE_DYNLIBS}
  2400. if not IsLibrary then
  2401. {$endif}
  2402. SysInitFPU;
  2403. {$if declared(DefaultFPUControlWord)}
  2404. DefaultFPUControlWord:=GetNativeFPUControlWord;
  2405. {$endif}
  2406. SysResetFPU;
  2407. {$endif}
  2408. end;
  2409. {$endif}
  2410. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  2411. function SwapEndian(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2412. begin
  2413. { the extra Word type cast is necessary because the "AValue shr 8" }
  2414. { is turned into "longint(AValue) shr 8", so if AValue < 0 then }
  2415. { the sign bits from the upper 16 bits are shifted in rather than }
  2416. { zeroes. }
  2417. Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff);
  2418. end;
  2419. {$ifndef cpujvm}
  2420. function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2421. begin
  2422. Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
  2423. end;
  2424. {$endif}
  2425. function SwapEndian(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2426. begin
  2427. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2428. Result := (Result shl 16) or (Result shr 16);
  2429. end;
  2430. {$ifndef cpujvm}
  2431. function SwapEndian(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2432. begin
  2433. Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
  2434. Result := (Result shl 16) or (Result shr 16);
  2435. end;
  2436. {$endif}
  2437. function SwapEndian(const AValue: Int64): Int64;
  2438. begin
  2439. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2440. ((AValue shr 8) and $00FF00FF00FF00FF);
  2441. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2442. ((Result shr 16) and $0000FFFF0000FFFF);
  2443. Result := (Result shl 32) or ((Result shr 32));
  2444. end;
  2445. {$ifndef cpujvm}
  2446. function SwapEndian(const AValue: QWord): QWord;
  2447. begin
  2448. Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
  2449. ((AValue shr 8) and $00FF00FF00FF00FF);
  2450. Result := ((Result shl 16) and $FFFF0000FFFF0000) or
  2451. ((Result shr 16) and $0000FFFF0000FFFF);
  2452. Result := (Result shl 32) or ((Result shr 32));
  2453. end;
  2454. {$endif}
  2455. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  2456. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2457. begin
  2458. {$IFDEF ENDIAN_BIG}
  2459. Result := AValue;
  2460. {$ELSE}
  2461. Result := SwapEndian(AValue);
  2462. {$ENDIF}
  2463. end;
  2464. {$ifndef cpujvm}
  2465. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2466. begin
  2467. {$IFDEF ENDIAN_BIG}
  2468. Result := AValue;
  2469. {$ELSE}
  2470. Result := SwapEndian(AValue);
  2471. {$ENDIF}
  2472. end;
  2473. {$endif not cpujvm}
  2474. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2475. begin
  2476. {$IFDEF ENDIAN_BIG}
  2477. Result := AValue;
  2478. {$ELSE}
  2479. Result := SwapEndian(AValue);
  2480. {$ENDIF}
  2481. end;
  2482. {$ifndef cpujvm}
  2483. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2484. begin
  2485. {$IFDEF ENDIAN_BIG}
  2486. Result := AValue;
  2487. {$ELSE}
  2488. Result := SwapEndian(AValue);
  2489. {$ENDIF}
  2490. end;
  2491. {$endif not cpujvm}
  2492. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2493. begin
  2494. {$IFDEF ENDIAN_BIG}
  2495. Result := AValue;
  2496. {$ELSE}
  2497. Result := SwapEndian(AValue);
  2498. {$ENDIF}
  2499. end;
  2500. {$ifndef cpujvm}
  2501. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2502. begin
  2503. {$IFDEF ENDIAN_BIG}
  2504. Result := AValue;
  2505. {$ELSE}
  2506. Result := SwapEndian(AValue);
  2507. {$ENDIF}
  2508. end;
  2509. {$endif not cpujvm}
  2510. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2511. begin
  2512. {$IFDEF ENDIAN_LITTLE}
  2513. Result := AValue;
  2514. {$ELSE}
  2515. Result := SwapEndian(AValue);
  2516. {$ENDIF}
  2517. end;
  2518. {$ifndef cpujvm}
  2519. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2520. begin
  2521. {$IFDEF ENDIAN_LITTLE}
  2522. Result := AValue;
  2523. {$ELSE}
  2524. Result := SwapEndian(AValue);
  2525. {$ENDIF}
  2526. end;
  2527. {$endif not cpujvm}
  2528. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2529. begin
  2530. {$IFDEF ENDIAN_LITTLE}
  2531. Result := AValue;
  2532. {$ELSE}
  2533. Result := SwapEndian(AValue);
  2534. {$ENDIF}
  2535. end;
  2536. {$ifndef cpujvm}
  2537. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2538. begin
  2539. {$IFDEF ENDIAN_LITTLE}
  2540. Result := AValue;
  2541. {$ELSE}
  2542. Result := SwapEndian(AValue);
  2543. {$ENDIF}
  2544. end;
  2545. {$endif not cpujvm}
  2546. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2547. begin
  2548. {$IFDEF ENDIAN_LITTLE}
  2549. Result := AValue;
  2550. {$ELSE}
  2551. Result := SwapEndian(AValue);
  2552. {$ENDIF}
  2553. end;
  2554. {$ifndef cpujvm}
  2555. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2556. begin
  2557. {$IFDEF ENDIAN_LITTLE}
  2558. Result := AValue;
  2559. {$ELSE}
  2560. Result := SwapEndian(AValue);
  2561. {$ENDIF}
  2562. end;
  2563. {$endif not cpujvm}
  2564. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2565. begin
  2566. {$IFDEF ENDIAN_BIG}
  2567. Result := AValue;
  2568. {$ELSE}
  2569. Result := SwapEndian(AValue);
  2570. {$ENDIF}
  2571. end;
  2572. {$ifndef cpujvm}
  2573. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2574. begin
  2575. {$IFDEF ENDIAN_BIG}
  2576. Result := AValue;
  2577. {$ELSE}
  2578. Result := SwapEndian(AValue);
  2579. {$ENDIF}
  2580. end;
  2581. {$endif not cpujvm}
  2582. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2583. begin
  2584. {$IFDEF ENDIAN_BIG}
  2585. Result := AValue;
  2586. {$ELSE}
  2587. Result := SwapEndian(AValue);
  2588. {$ENDIF}
  2589. end;
  2590. {$ifndef cpujvm}
  2591. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2592. begin
  2593. {$IFDEF ENDIAN_BIG}
  2594. Result := AValue;
  2595. {$ELSE}
  2596. Result := SwapEndian(AValue);
  2597. {$ENDIF}
  2598. end;
  2599. {$endif not cpujvm}
  2600. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2601. begin
  2602. {$IFDEF ENDIAN_BIG}
  2603. Result := AValue;
  2604. {$ELSE}
  2605. Result := SwapEndian(AValue);
  2606. {$ENDIF}
  2607. end;
  2608. {$ifndef cpujvm}
  2609. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2610. begin
  2611. {$IFDEF ENDIAN_BIG}
  2612. Result := AValue;
  2613. {$ELSE}
  2614. Result := SwapEndian(AValue);
  2615. {$ENDIF}
  2616. end;
  2617. {$endif not cpujvm}
  2618. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2619. begin
  2620. {$IFDEF ENDIAN_LITTLE}
  2621. Result := AValue;
  2622. {$ELSE}
  2623. Result := SwapEndian(AValue);
  2624. {$ENDIF}
  2625. end;
  2626. {$ifndef cpujvm}
  2627. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2628. begin
  2629. {$IFDEF ENDIAN_LITTLE}
  2630. Result := AValue;
  2631. {$ELSE}
  2632. Result := SwapEndian(AValue);
  2633. {$ENDIF}
  2634. end;
  2635. {$endif not cpujvm}
  2636. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  2637. begin
  2638. {$IFDEF ENDIAN_LITTLE}
  2639. Result := AValue;
  2640. {$ELSE}
  2641. Result := SwapEndian(AValue);
  2642. {$ENDIF}
  2643. end;
  2644. {$ifndef cpujvm}
  2645. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2646. begin
  2647. {$IFDEF ENDIAN_LITTLE}
  2648. Result := AValue;
  2649. {$ELSE}
  2650. Result := SwapEndian(AValue);
  2651. {$ENDIF}
  2652. end;
  2653. {$endif not cpujvm}
  2654. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  2655. begin
  2656. {$IFDEF ENDIAN_LITTLE}
  2657. Result := AValue;
  2658. {$ELSE}
  2659. Result := SwapEndian(AValue);
  2660. {$ENDIF}
  2661. end;
  2662. {$ifndef cpujvm}
  2663. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2664. begin
  2665. {$IFDEF ENDIAN_LITTLE}
  2666. Result := AValue;
  2667. {$ELSE}
  2668. Result := SwapEndian(AValue);
  2669. {$ENDIF}
  2670. end;
  2671. {$endif not cpujvm}
  2672. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  2673. procedure ReadBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2674. begin
  2675. end;
  2676. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2677. begin
  2678. end;
  2679. procedure ReadWriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2680. begin
  2681. end;
  2682. procedure WriteBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  2683. begin
  2684. end;
  2685. {$endif FPC_SYSTEM_HAS_MEM_BARRIER}
  2686. {$ifndef FPC_HAS_INTERNAL_ROX_BYTE}
  2687. {$ifndef FPC_SYSTEM_HAS_ROX_BYTE}
  2688. function RorByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2689. begin
  2690. Result:=(AValue shr 1) or (AValue shl 7);
  2691. end;
  2692. function RorByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2693. begin
  2694. Result:=(AValue shr (Dist and 7)) or (AValue shl (8-(Dist and 7)));
  2695. end;
  2696. function RolByte(Const AValue : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2697. begin
  2698. Result:=(AValue shl 1) or (AValue shr 7);
  2699. end;
  2700. function RolByte(Const AValue : Byte;const Dist : Byte): Byte;{$ifdef SYSTEMINLINE}inline;{$endif}
  2701. begin
  2702. Result:=(AValue shl (Dist and 7)) or (AValue shr (8-(Dist and 7)));
  2703. end;
  2704. {$endif FPC_SYSTEM_HAS_ROX_BYTE}
  2705. {$endif FPC_HAS_INTERNAL_ROX_BYTE}
  2706. {$ifndef FPC_HAS_INTERNAL_ROX_WORD}
  2707. {$ifndef FPC_SYSTEM_HAS_ROX_WORD}
  2708. function RorWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2709. begin
  2710. Result:=(AValue shr 1) or (AValue shl 15);
  2711. end;
  2712. function RorWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2713. begin
  2714. Result:=(AValue shr (Dist and 15)) or (AValue shl (16-(Dist and 15)));
  2715. end;
  2716. function RolWord(Const AValue : Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2717. begin
  2718. Result:=(AValue shl 1) or (AValue shr 15);
  2719. end;
  2720. function RolWord(Const AValue : Word;const Dist : Byte): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  2721. begin
  2722. Result:=(AValue shl (Dist and 15)) or (AValue shr (16-(Dist and 15)));
  2723. end;
  2724. {$endif FPC_SYSTEM_HAS_ROX_WORD}
  2725. {$endif FPC_HAS_INTERNAL_ROX_WORD}
  2726. {$ifndef FPC_HAS_INTERNAL_ROX_DWORD}
  2727. {$ifndef FPC_SYSTEM_HAS_ROX_DWORD}
  2728. function RorDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2729. begin
  2730. Result:=(AValue shr 1) or (AValue shl 31);
  2731. end;
  2732. function RorDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2733. begin
  2734. Result:=(AValue shr (Dist and 31)) or (AValue shl (32-(Dist and 31)));
  2735. end;
  2736. function RolDWord(Const AValue : DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2737. begin
  2738. Result:=(AValue shl 1) or (AValue shr 31);
  2739. end;
  2740. function RolDWord(Const AValue : DWord;const Dist : Byte): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2741. begin
  2742. Result:=(AValue shl (Dist and 31)) or (AValue shr (32-(Dist and 31)));
  2743. end;
  2744. {$endif FPC_SYSTEM_HAS_ROX_DWORD}
  2745. {$endif FPC_HAS_INTERNAL_ROX_DWORD}
  2746. {$ifndef FPC_HAS_INTERNAL_ROX_QWORD}
  2747. {$ifndef FPC_SYSTEM_HAS_ROX_QWORD}
  2748. function RorQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2749. begin
  2750. Result:=(AValue shr 1) or (AValue shl 63);
  2751. end;
  2752. function RorQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2753. begin
  2754. Result:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2755. end;
  2756. function RolQWord(Const AValue : QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2757. begin
  2758. Result:=(AValue shl 1) or (AValue shr 63);
  2759. end;
  2760. function RolQWord(Const AValue : QWord;const Dist : Byte): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  2761. begin
  2762. Result:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2763. end;
  2764. {$endif FPC_SYSTEM_HAS_ROX_QWORD}
  2765. {$endif FPC_HAS_INTERNAL_ROX_QWORD}
  2766. {$ifndef FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2767. {$ifndef FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2768. procedure fpc_ror_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_INT64']; compilerproc;
  2769. begin
  2770. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2771. end;
  2772. procedure fpc_ror_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROR_ASSIGN_QWORD']; compilerproc;
  2773. begin
  2774. AValue:=(AValue shr (Dist and 63)) or (AValue shl (64-(Dist and 63)));
  2775. end;
  2776. procedure fpc_rol_assign_int64(var AValue : int64;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_INT64']; compilerproc;
  2777. begin
  2778. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2779. end;
  2780. procedure fpc_rol_assign_qword(var AValue : QWord;const Dist : Byte); [Public,Alias:'FPC_ROL_ASSIGN_QWORD']; compilerproc;
  2781. begin
  2782. AValue:=(AValue shl (Dist and 63)) or (AValue shr (64-(Dist and 63)));
  2783. end;
  2784. {$endif FPC_SYSTEM_HAS_ROX_ASSIGN_QWORD}
  2785. {$endif FPC_HAS_INTERNAL_ROX_ASSIGN_QWORD}
  2786. {$ifndef FPC_HAS_INTERNAL_SAR_BYTE}
  2787. {$ifndef FPC_SYSTEM_HAS_SAR_BYTE}
  2788. function SarShortint(Const AValue : Shortint;const Shift : Byte): Shortint;
  2789. begin
  2790. 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)))));
  2791. end;
  2792. {$endif FPC_HAS_INTERNAL_SAR_BYTE}
  2793. {$endif FPC_SYSTEM_HAS_SAR_BYTE}
  2794. {$ifndef FPC_HAS_INTERNAL_SAR_WORD}
  2795. {$ifndef FPC_SYSTEM_HAS_SAR_WORD}
  2796. function SarSmallint(Const AValue : Smallint;const Shift : Byte): Smallint;
  2797. begin
  2798. 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)))));
  2799. end;
  2800. {$endif FPC_HAS_INTERNAL_SAR_WORD}
  2801. {$endif FPC_SYSTEM_HAS_SAR_WORD}
  2802. {$ifndef FPC_HAS_INTERNAL_SAR_DWORD}
  2803. {$ifndef FPC_SYSTEM_HAS_SAR_DWORD}
  2804. function SarLongint(Const AValue : Longint;const Shift : Byte): Longint;
  2805. begin
  2806. 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)))));
  2807. end;
  2808. {$endif FPC_HAS_INTERNAL_SAR_DWORD}
  2809. {$endif FPC_SYSTEM_HAS_SAR_DWORD}
  2810. {$ifndef FPC_HAS_INTERNAL_SAR_QWORD}
  2811. {$ifndef FPC_SYSTEM_HAS_SAR_QWORD}
  2812. function fpc_SarInt64(Const AValue : Int64;const Shift : Byte): Int64; [Public,Alias:'FPC_SARINT64']; compilerproc;
  2813. begin
  2814. 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)))));
  2815. end;
  2816. {$endif FPC_HAS_INTERNAL_SAR_QWORD}
  2817. {$endif FPC_SYSTEM_HAS_SAR_QWORD}
  2818. {$ifndef FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2819. {$ifndef FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2820. procedure fpc_sar_assign_int64(var AValue : Int64;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_INT64']; compilerproc;
  2821. begin
  2822. AValue:=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)))));
  2823. end;
  2824. procedure fpc_sar_assign_qword(var AValue : QWord;const Shift : Byte); [Public,Alias:'FPC_SAR_ASSIGN_QWORD']; compilerproc;
  2825. begin
  2826. AValue:=qword(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)))));
  2827. end;
  2828. {$endif FPC_HAS_INTERNAL_SAR_ASSIGN_QWORD}
  2829. {$endif FPC_SYSTEM_HAS_SAR_ASSIGN_QWORD}
  2830. {$ifndef FPC_HAS_INTERNAL_BSF_BYTE}
  2831. {$ifndef FPC_SYSTEM_HAS_BSF_BYTE}
  2832. function BsfByte(Const AValue: Byte): Byte;
  2833. const bsf8bit: array [Byte] of Byte = (
  2834. $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,
  2835. 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,
  2836. 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,
  2837. 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,
  2838. 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,
  2839. 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,
  2840. 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,
  2841. 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
  2842. );
  2843. begin
  2844. result:=bsf8bit[AValue];
  2845. end;
  2846. {$endif}
  2847. {$endif}
  2848. {$ifndef FPC_HAS_INTERNAL_BSR_BYTE}
  2849. {$ifndef FPC_SYSTEM_HAS_BSR_BYTE}
  2850. function BsrByte(Const AValue: Byte): Byte;
  2851. const bsr8bit: array [Byte] of Byte = (
  2852. $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,
  2853. 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,
  2854. 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,
  2855. 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,
  2856. 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,
  2857. 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,
  2858. 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,
  2859. 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
  2860. );
  2861. begin
  2862. result:=bsr8bit[AValue];
  2863. end;
  2864. {$endif}
  2865. {$endif}
  2866. {$ifndef FPC_SYSTEM_HAS_BSF_WORD}
  2867. {$ifndef FPC_HAS_INTERNAL_BSF_WORD}
  2868. function BsfWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2869. begin
  2870. result:=ord(lo(AValue)=0)*8;
  2871. result:=result or BsfByte(byte(AValue shr result));
  2872. end;
  2873. {$endif}
  2874. {$endif}
  2875. {$ifndef FPC_SYSTEM_HAS_BSR_WORD}
  2876. {$ifndef FPC_HAS_INTERNAL_BSR_WORD}
  2877. function BsrWord(Const AValue: Word): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2878. begin
  2879. result:=ord(AValue>255)*8;
  2880. result:=result or BsrByte(byte(AValue shr result));
  2881. end;
  2882. {$endif}
  2883. {$endif}
  2884. {$ifndef FPC_HAS_INTERNAL_BSF_DWORD}
  2885. {$ifndef FPC_SYSTEM_HAS_BSF_DWORD}
  2886. function BsfDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2887. var
  2888. tmp: DWord;
  2889. begin
  2890. result:=ord(lo(AValue)=0)*16;
  2891. tmp:=AValue shr result;
  2892. result:=result or (ord((tmp and $FF)=0)*8);
  2893. tmp:=tmp shr (result and 8);
  2894. result:=result or BsfByte(byte(tmp));
  2895. end;
  2896. {$endif}
  2897. {$endif}
  2898. {$ifndef FPC_HAS_INTERNAL_BSR_DWORD}
  2899. {$ifndef FPC_SYSTEM_HAS_BSR_DWORD}
  2900. function BsrDWord(Const AValue : DWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2901. var
  2902. tmp: DWord;
  2903. begin
  2904. result:=ord(AValue>$FFFF)*16;
  2905. tmp:=AValue shr result;
  2906. result:=result or (ord(tmp>$FF)*8);
  2907. tmp:=tmp shr (result and 8);
  2908. result:=result or BsrByte(byte(tmp));
  2909. end;
  2910. {$endif}
  2911. {$endif}
  2912. {$ifndef FPC_HAS_INTERNAL_BSF_QWORD}
  2913. {$ifndef FPC_SYSTEM_HAS_BSF_QWORD}
  2914. function BsfQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2915. var
  2916. tmp: DWord;
  2917. begin
  2918. result:=0;
  2919. tmp:=lo(AValue);
  2920. if (tmp=0) then
  2921. begin
  2922. tmp:=hi(AValue);
  2923. result:=32;
  2924. end;
  2925. result:=result or BsfDword(tmp);
  2926. end;
  2927. {$endif}
  2928. {$endif}
  2929. {$ifndef FPC_HAS_INTERNAL_BSR_QWORD}
  2930. {$ifndef FPC_SYSTEM_HAS_BSR_QWORD}
  2931. function BsrQWord(Const AValue : QWord): {$ifdef CPU16}byte{$else}cardinal{$endif};
  2932. var
  2933. tmp: DWord;
  2934. begin
  2935. result:=32;
  2936. tmp:=hi(AValue);
  2937. if (tmp=0) then
  2938. begin
  2939. tmp:=lo(AValue);
  2940. result:=0;
  2941. end;
  2942. result:=result or BsrDword(tmp);
  2943. end;
  2944. {$endif}
  2945. {$endif}
  2946. const
  2947. PopCntData : array[0..15] of byte = (0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4);
  2948. function fpc_PopCnt_byte(AValue : Byte): Byte;[Public,Alias:'FPC_POPCNT_BYTE'];compilerproc;
  2949. begin
  2950. Result:=PopCntData[AValue and $f]+PopCntData[(AValue shr 4) and $f];
  2951. end;
  2952. function fpc_PopCnt_word(AValue : Word): Word;[Public,Alias:'FPC_POPCNT_WORD'];compilerproc;
  2953. var
  2954. i : SizeInt;
  2955. begin
  2956. Result:=0;
  2957. for i:=0 to 3 do
  2958. begin
  2959. inc(Result,PopCntData[AValue and $f]);
  2960. AValue:=AValue shr 4;
  2961. end;
  2962. end;
  2963. function fpc_PopCnt_dword(AValue : DWord): DWord;[Public,Alias:'FPC_POPCNT_DWORD'];compilerproc;
  2964. var
  2965. i : SizeInt;
  2966. begin
  2967. Result:=0;
  2968. for i:=0 to 7 do
  2969. begin
  2970. inc(Result,PopCntData[AValue and $f]);
  2971. AValue:=AValue shr 4;
  2972. end;
  2973. end;
  2974. {$ifndef FPC_SYSTEM_HAS_POPCNT_QWORD}
  2975. function fpc_PopCnt_qword(AValue : QWord): QWord;[Public,Alias:'FPC_POPCNT_QWORD'];compilerproc;
  2976. begin
  2977. Result:=PopCnt(lo(AValue))+PopCnt(hi(AValue))
  2978. end;
  2979. {$endif}