generic.inc 104 KB

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