generic.inc 95 KB

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