generic.inc 95 KB

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