generic.inc 89 KB

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