generic.inc 88 KB

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