generic.inc 94 KB

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