generic.inc 94 KB

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