generic.inc 88 KB

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