generic.inc 95 KB

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