generic.inc 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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. {****************************************************************************
  13. Primitives
  14. ****************************************************************************}
  15. type
  16. pstring = ^shortstring;
  17. {$ifndef FPC_SYSTEM_HAS_MOVE}
  18. procedure Move(const source;var dest;count:SizeInt);[public, alias: 'FPC_MOVE'];
  19. type
  20. bytearray = array [0..high(sizeint)-1] of byte;
  21. var
  22. i:longint;
  23. begin
  24. if count <= 0 then exit;
  25. Dec(count);
  26. if @source<@dest then
  27. begin
  28. for i:=count downto 0 do
  29. bytearray(dest)[i]:=bytearray(source)[i];
  30. end
  31. else
  32. begin
  33. for i:=0 to count do
  34. bytearray(dest)[i]:=bytearray(source)[i];
  35. end;
  36. end;
  37. {$endif not FPC_SYSTEM_HAS_MOVE}
  38. {$ifndef FPC_SYSTEM_HAS_FILLCHAR}
  39. Procedure FillChar(var x;count:SizeInt;value:byte);
  40. type
  41. longintarray = array [0..high(sizeint) div 4-1] of longint;
  42. bytearray = array [0..high(sizeint)-1] of byte;
  43. var
  44. i,v : longint;
  45. begin
  46. if count <= 0 then exit;
  47. v := 0;
  48. { aligned? }
  49. if (PtrUInt(@x) mod sizeof(PtrUInt))<>0 then
  50. begin
  51. for i:=0 to count-1 do
  52. bytearray(x)[i]:=value;
  53. end
  54. else
  55. begin
  56. v:=(value shl 8) or (value and $FF);
  57. v:=(v shl 16) or (v and $ffff);
  58. for i:=0 to (count div 4)-1 do
  59. longintarray(x)[i]:=v;
  60. for i:=(count div 4)*4 to count-1 do
  61. bytearray(x)[i]:=value;
  62. end;
  63. end;
  64. {$endif FPC_SYSTEM_HAS_FILLCHAR}
  65. {$ifndef INTERNALMOVEFILLCHAR}
  66. {$ifndef FPC_SYSTEM_HAS_FILLBYTE}
  67. procedure FillByte (var x;count : SizeInt;value : byte );
  68. begin
  69. FillChar (X,Count,CHR(VALUE));
  70. end;
  71. {$endif not FPC_SYSTEM_HAS_FILLBYTE}
  72. {$endif INTERNALMOVEFILLCHAR}
  73. {$ifndef FPC_SYSTEM_HAS_FILLWORD}
  74. procedure fillword(var x;count : SizeInt;value : word);
  75. type
  76. longintarray = array [0..high(sizeint) div 4-1] of longint;
  77. wordarray = array [0..high(sizeint) div 2-1] of word;
  78. var
  79. i,v : longint;
  80. begin
  81. if Count <= 0 then exit;
  82. { aligned? }
  83. if (PtrUInt(@x) mod sizeof(PtrUInt))<>0 then
  84. begin
  85. for i:=0 to count-1 do
  86. wordarray(x)[i]:=value;
  87. end
  88. else
  89. begin
  90. v:=value*$10000+value;
  91. for i:=0 to (count div 2) -1 do
  92. longintarray(x)[i]:=v;
  93. for i:=(count div 2)*2 to count-1 do
  94. wordarray(x)[i]:=value;
  95. end;
  96. end;
  97. {$endif not FPC_SYSTEM_HAS_FILLWORD}
  98. {$ifndef FPC_SYSTEM_HAS_FILLDWORD}
  99. procedure FillDWord(var x;count : SizeInt;value : DWord);
  100. type
  101. longintarray = array [0..high(sizeint) div 4-1] of longint;
  102. begin
  103. if count <= 0 then exit;
  104. while Count<>0 do
  105. begin
  106. { range checking must be disabled here }
  107. longintarray(x)[count-1]:=longint(value);
  108. Dec(count);
  109. end;
  110. end;
  111. {$endif FPC_SYSTEM_HAS_FILLDWORD}
  112. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR}
  113. function IndexChar(Const buf;len:SizeInt;b:char):SizeInt;
  114. begin
  115. IndexChar:=IndexByte(Buf,Len,byte(B));
  116. end;
  117. {$endif not FPC_SYSTEM_HAS_INDEXCHAR}
  118. {$ifndef FPC_SYSTEM_HAS_INDEXBYTE}
  119. function IndexByte(Const buf;len:SizeInt;b:byte):SizeInt;
  120. type
  121. bytearray = array [0..high(sizeint)-1] of byte;
  122. var
  123. I : longint;
  124. begin
  125. I:=0;
  126. { simulate assembler implementations behaviour, which is expected }
  127. { fpc_pchar_to_ansistr in astrings.inc }
  128. if (len < 0) then
  129. len := high(longint);
  130. while (I<Len) and (bytearray(buf)[I]<>b) do
  131. inc(I);
  132. if (i=Len) then
  133. i:=-1; {Can't use 0, since it is a possible value}
  134. IndexByte:=I;
  135. end;
  136. {$endif not FPC_SYSTEM_HAS_INDEXBYTE}
  137. {$ifndef FPC_SYSTEM_HAS_INDEXWORD}
  138. function Indexword(Const buf;len:SizeInt;b:word):SizeInt;
  139. type
  140. wordarray = array [0..high(sizeint) div 2-1] of word;
  141. var
  142. I : longint;
  143. begin
  144. I:=0;
  145. if (len < 0) then
  146. len := high(longint);
  147. while (I<Len) and (wordarray(buf)[I]<>b) do
  148. inc(I);
  149. if (i=Len) then
  150. i:=-1; {Can't use 0, since it is a possible value for index}
  151. Indexword:=I;
  152. end;
  153. {$endif not FPC_SYSTEM_HAS_INDEXWORD}
  154. {$ifndef FPC_SYSTEM_HAS_INDEXDWORD}
  155. function IndexDWord(Const buf;len:SizeInt;b:DWord):SizeInt;
  156. type
  157. dwordarray = array [0..high(sizeint) div 4-1] of dword;
  158. var
  159. I : longint;
  160. begin
  161. I:=0;
  162. if (len < 0) then
  163. len := high(longint);
  164. while (I<Len) and (dwordarray(buf)[I]<>b) do
  165. inc(I);
  166. if (i=Len) then
  167. i:=-1; {Can't use 0, since it is a possible value for index}
  168. IndexDWord:=I;
  169. end;
  170. {$endif not FPC_SYSTEM_HAS_INDEXDWORD}
  171. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR}
  172. function CompareChar(Const buf1,buf2;len:SizeInt):SizeInt;
  173. begin
  174. CompareChar:=CompareByte(buf1,buf2,len);
  175. end;
  176. {$endif not FPC_SYSTEM_HAS_COMPARECHAR}
  177. {$ifndef FPC_SYSTEM_HAS_COMPAREBYTE}
  178. function CompareByte(Const buf1,buf2;len:SizeInt):SizeInt;
  179. type
  180. bytearray = array [0..high(sizeint)-1] of byte;
  181. var
  182. I : longint;
  183. begin
  184. I:=0;
  185. if (Len<>0) and (@Buf1<>@Buf2) then
  186. begin
  187. while (bytearray(Buf1)[I]=bytearray(Buf2)[I]) and (I<Len) do
  188. inc(I);
  189. if I=Len then {No difference}
  190. I:=0
  191. else
  192. begin
  193. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  194. if I>0 then
  195. I:=1
  196. else
  197. if I<0 then
  198. I:=-1;
  199. end;
  200. end;
  201. CompareByte:=I;
  202. end;
  203. {$endif not FPC_SYSTEM_HAS_COMPAREBYTE}
  204. {$ifndef FPC_SYSTEM_HAS_COMPAREWORD}
  205. function CompareWord(Const buf1,buf2;len:SizeInt):SizeInt;
  206. type
  207. wordarray = array [0..high(sizeint) div 2-1] of word;
  208. var
  209. I : longint;
  210. begin
  211. I:=0;
  212. if (Len<>0) and (@Buf1<>@Buf2) then
  213. begin
  214. while (wordarray(Buf1)[I]=wordarray(Buf2)[I]) and (I<Len) do
  215. inc(I);
  216. if I=Len then {No difference}
  217. I:=0
  218. else
  219. begin
  220. I:=wordarray(Buf1)[I]-wordarray(Buf2)[I];
  221. if I>0 then
  222. I:=1
  223. else
  224. if I<0 then
  225. I:=-1;
  226. end;
  227. end;
  228. CompareWord:=I;
  229. end;
  230. {$endif not FPC_SYSTEM_HAS_COMPAREWORD}
  231. {$ifndef FPC_SYSTEM_HAS_COMPAREDWORD}
  232. function CompareDWord(Const buf1,buf2;len:SizeInt):SizeInt;
  233. type
  234. longintarray = array [0..high(sizeint) div 4-1] of longint;
  235. var
  236. I : longint;
  237. begin
  238. I:=0;
  239. if (Len<>0) and (@Buf1<>@Buf2) then
  240. begin
  241. while (longintarray(Buf1)[I]=longintarray(Buf2)[I]) and (I<Len) do
  242. inc(I);
  243. if I=Len then {No difference}
  244. I:=0
  245. else
  246. begin
  247. I:=longintarray(Buf1)[I]-longintarray(Buf2)[I];
  248. if I>0 then
  249. I:=1
  250. else
  251. if I<0 then
  252. I:=-1;
  253. end;
  254. end;
  255. CompareDWord:=I;
  256. end;
  257. {$endif ndef FPC_SYSTEM_HAS_COMPAREDWORD}
  258. {$ifndef FPC_SYSTEM_HAS_MOVECHAR0}
  259. procedure MoveChar0(Const buf1;var buf2;len:SizeInt);
  260. var
  261. I : longint;
  262. begin
  263. if Len = 0 then exit;
  264. I:=IndexByte(Buf1,Len,0);
  265. if I<>-1 then
  266. Move(Buf1,Buf2,I)
  267. else
  268. Move(Buf1,Buf2,len);
  269. end;
  270. {$endif ndef FPC_SYSTEM_HAS_MOVECHAR0}
  271. {$ifndef FPC_SYSTEM_HAS_INDEXCHAR0}
  272. function IndexChar0(Const buf;len:SizeInt;b:Char):SizeInt;
  273. var
  274. I : longint;
  275. begin
  276. if Len<>0 then
  277. begin
  278. I:=IndexByte(Buf,Len,0);
  279. If (I=-1) then
  280. I:=Len;
  281. IndexChar0:=IndexByte(Buf,I,byte(b));
  282. end
  283. else
  284. IndexChar0:=0;
  285. end;
  286. {$endif ndef FPC_SYSTEM_HAS_INDEXCHAR0}
  287. {$ifndef FPC_SYSTEM_HAS_COMPARECHAR0}
  288. function CompareChar0(Const buf1,buf2;len:SizeInt):SizeInt;
  289. type
  290. bytearray = array [0..high(sizeint)-1] of byte;
  291. var
  292. i : longint;
  293. begin
  294. I:=0;
  295. if (Len<>0) and (@Buf1<>@Buf2) then
  296. begin
  297. while (I<Len) And
  298. ((Pbyte(@Buf1)[i]<>0) and (PByte(@buf2)[i]<>0)) and
  299. (pbyte(@Buf1)[I]=pbyte(@Buf2)[I]) do
  300. inc(I);
  301. if (I=Len) or
  302. (PByte(@Buf1)[i]=0) or
  303. (PByte(@buf2)[I]=0) then {No difference or 0 reached }
  304. I:=0
  305. else
  306. begin
  307. I:=bytearray(Buf1)[I]-bytearray(Buf2)[I];
  308. if I>0 then
  309. I:=1
  310. else
  311. if I<0 then
  312. I:=-1;
  313. end;
  314. end;
  315. CompareChar0:=I;
  316. end;
  317. {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
  318. {****************************************************************************
  319. Object Helpers
  320. ****************************************************************************}
  321. {$ifdef FPC_HAS_FEATURE_OBJECTS}
  322. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  323. { Note: _vmt will be reset to -1 when memory is allocated,
  324. this is needed for fpc_help_fail }
  325. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  326. type
  327. ppointer = ^pointer;
  328. pvmt = ^tvmt;
  329. tvmt=packed record
  330. size,msize:ptrint;
  331. parent:pointer;
  332. end;
  333. var
  334. vmtcopy : pointer;
  335. begin
  336. { Inherited call? }
  337. if _vmt=nil then
  338. begin
  339. fpc_help_constructor:=_self;
  340. exit;
  341. end;
  342. vmtcopy:=_vmt;
  343. if (_self=nil) and
  344. (pvmt(_vmt)^.size>0) then
  345. begin
  346. getmem(_self,pvmt(_vmt)^.size);
  347. { reset vmt needed for fail }
  348. _vmt:=pointer(-1);
  349. end;
  350. if _self<>nil then
  351. begin
  352. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  353. ppointer(_self+_vmt_pos)^:=vmtcopy;
  354. end;
  355. fpc_help_constructor:=_self;
  356. end;
  357. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  358. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  359. { Note: _self will not be reset, the compiler has to generate the reset }
  360. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  361. type
  362. ppointer = ^pointer;
  363. pvmt = ^tvmt;
  364. tvmt = packed record
  365. size,msize : ptrint;
  366. parent : pointer;
  367. end;
  368. begin
  369. { already released? }
  370. if (_self=nil) or
  371. (_vmt=nil) or
  372. (ppointer(_self+vmt_pos)^=nil) then
  373. exit;
  374. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  375. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  376. RunError(210);
  377. { reset vmt to nil for protection }
  378. ppointer(_self+vmt_pos)^:=nil;
  379. freemem(_self);
  380. end;
  381. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  382. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  383. { Note: _self will not be reset, the compiler has to generate the reset }
  384. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  385. type
  386. ppointer = ^pointer;
  387. pvmt = ^tvmt;
  388. tvmt = packed record
  389. size,msize : ptrint;
  390. parent : pointer;
  391. end;
  392. begin
  393. if (_self=nil) or (_vmt=nil) then
  394. exit;
  395. { vmt=-1 when memory was allocated }
  396. if ptrint(_vmt)=-1 then
  397. begin
  398. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  399. HandleError(210)
  400. else
  401. begin
  402. ppointer(_self+vmt_pos)^:=nil;
  403. freemem(_self);
  404. { reset _vmt to nil so it will not be freed a
  405. second time }
  406. _vmt:=nil;
  407. end;
  408. end
  409. else
  410. ppointer(_self+vmt_pos)^:=nil;
  411. end;
  412. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  413. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  414. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  415. type
  416. pvmt = ^tvmt;
  417. tvmt = packed record
  418. size,msize : ptrint;
  419. parent : pointer;
  420. end;
  421. begin
  422. if (_vmt=nil) or
  423. (pvmt(_vmt)^.size=0) or
  424. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  425. RunError(210);
  426. end;
  427. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  428. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  429. { checks for a correct vmt pointer }
  430. { deeper check to see if the current object is }
  431. { really related to the true }
  432. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  433. type
  434. pvmt = ^tvmt;
  435. tvmt = packed record
  436. size,msize : ptrint;
  437. parent : pointer;
  438. end;
  439. begin
  440. if (vmt=nil) or
  441. (pvmt(vmt)^.size=0) or
  442. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  443. RunError(210);
  444. while assigned(vmt) do
  445. if vmt=expvmt then
  446. exit
  447. else
  448. vmt:=pvmt(vmt)^.parent;
  449. RunError(219);
  450. end;
  451. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  452. {$endif FPC_HAS_FEATURE_OBJECTS}
  453. {****************************************************************************
  454. String
  455. ****************************************************************************}
  456. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  457. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  458. var
  459. slen : byte;
  460. begin
  461. slen:=length(sstr);
  462. if slen<len then
  463. len:=slen;
  464. move(sstr[0],result[0],len+1);
  465. if slen>len then
  466. result[0]:=chr(len);
  467. end;
  468. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  469. var
  470. slen : byte;
  471. type
  472. pstring = ^string;
  473. begin
  474. slen:=length(pstring(sstr)^);
  475. if slen<len then
  476. len:=slen;
  477. move(sstr^,dstr^,len+1);
  478. if slen>len then
  479. pchar(dstr)^:=chr(len);
  480. end;
  481. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  482. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  483. {$ifndef STR_CONCAT_PROCS}
  484. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; compilerproc;
  485. var
  486. s1l, s2l : byte;
  487. begin
  488. s1l:=length(s1);
  489. s2l:=length(s2);
  490. if s1l+s2l>255 then
  491. s2l:=255-s1l;
  492. move(s1[1],fpc_shortstr_concat[1],s1l);
  493. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  494. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  495. end;
  496. {$else STR_CONCAT_PROCS}
  497. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  498. var
  499. s1l, s2l : longint;
  500. begin
  501. s1l:=length(s1);
  502. s2l:=length(s2);
  503. if s1l+s2l>high(dests) then
  504. s2l:=high(dests)-s1l;
  505. if @dests=@s1 then
  506. move(s2[1],dests[s1l+1],s2l)
  507. else
  508. if @dests=@s2 then
  509. begin
  510. move(dests[1],dests[s1l+1],s2l);
  511. move(s1[1],dests[1],s1l);
  512. end
  513. else
  514. begin
  515. move(s1[1],dests[1],s1l);
  516. move(s2[1],dests[s1l+1],s2l);
  517. end;
  518. dests[0]:=chr(s1l+s2l);
  519. end;
  520. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  521. var
  522. s2l : byte;
  523. LowStart,i,
  524. Len : longint;
  525. pc : pchar;
  526. needtemp : boolean;
  527. tmpstr : shortstring;
  528. p,pdest : pshortstring;
  529. begin
  530. if high(sarr)=0 then
  531. begin
  532. DestS:='';
  533. exit;
  534. end;
  535. lowstart:=low(sarr);
  536. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  537. inc(lowstart);
  538. { Check for another reuse, then we can't use
  539. the append optimization and need to use a temp }
  540. needtemp:=false;
  541. for i:=lowstart to high(sarr) do
  542. begin
  543. if Pointer(@DestS)=Pointer(sarr[i]) then
  544. begin
  545. needtemp:=true;
  546. break;
  547. end;
  548. end;
  549. if needtemp then
  550. begin
  551. lowstart:=low(sarr);
  552. tmpstr:='';
  553. pdest:=@tmpstr
  554. end
  555. else
  556. begin
  557. { Start with empty DestS if we start with concatting
  558. the first array element }
  559. if lowstart=low(sarr) then
  560. DestS:='';
  561. pdest:=@DestS;
  562. end;
  563. { Concat all strings, except the string we already
  564. copied in DestS }
  565. Len:=length(pdest^);
  566. pc:=@pdest^[1+Length(pdest^)];
  567. for i:=lowstart to high(sarr) do
  568. begin
  569. p:=sarr[i];
  570. if assigned(p) then
  571. begin
  572. s2l:=length(p^);
  573. if Len+s2l>high(dests) then
  574. s2l:=high(dests)-Len;
  575. Move(p^[1],pc^,s2l);
  576. inc(pc,s2l);
  577. inc(Len,s2l);
  578. end;
  579. end;
  580. pdest^[0]:=Chr(Len);
  581. if needtemp then
  582. DestS:=TmpStr;
  583. end;
  584. {$endif STR_CONCAT_PROCS}
  585. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  586. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  587. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  588. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  589. var
  590. s1l, s2l : integer;
  591. begin
  592. s1l:=length(s1);
  593. s2l:=length(s2);
  594. if s1l+s2l>high(s1) then
  595. s2l:=high(s1)-s1l;
  596. move(s2[1],s1[s1l+1],s2l);
  597. s1[0]:=chr(s1l+s2l);
  598. end;
  599. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  600. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  601. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  602. var
  603. s1,s2,max,i : byte;
  604. d : longint;
  605. begin
  606. s1:=length(left);
  607. s2:=length(right);
  608. if s1<s2 then
  609. max:=s1
  610. else
  611. max:=s2;
  612. for i:=1 to max do
  613. begin
  614. d:=byte(left[i])-byte(right[i]);
  615. if d>0 then
  616. exit(1)
  617. else if d<0 then
  618. exit(-1);
  619. end;
  620. if s1>s2 then
  621. exit(1)
  622. else if s1<s2 then
  623. exit(-1)
  624. else
  625. exit(0);
  626. end;
  627. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  628. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  629. function fpc_shortstr_compare_equal(const left,right:shortstring): longint; [public,alias:'FPC_SHORTSTR_COMPARE_EQUAL']; compilerproc;
  630. begin
  631. Result := longint(left[0]) - longint(right[0]);
  632. if Result = 0 then
  633. Result := CompareByte(left[1],right[1], longint(left[0]));
  634. end;
  635. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE_EQUAL}
  636. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  637. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  638. var
  639. l : longint;
  640. s: shortstring;
  641. begin
  642. if p=nil then
  643. l:=0
  644. else
  645. l:=strlen(p);
  646. if l>255 then
  647. l:=255;
  648. if l>0 then
  649. move(p^,s[1],l);
  650. s[0]:=chr(l);
  651. fpc_pchar_to_shortstr := s;
  652. end;
  653. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  654. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  655. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  656. var
  657. l: longint;
  658. index: longint;
  659. len: byte;
  660. begin
  661. l := high(arr)+1;
  662. if l>=256 then
  663. l:=255
  664. else if l<0 then
  665. l:=0;
  666. if (zerobased) then
  667. begin
  668. index:=IndexByte(arr[0],l,0);
  669. if (index < 0) then
  670. len := l
  671. else
  672. len := index;
  673. end
  674. else
  675. len := l;
  676. move(arr[0],fpc_chararray_to_shortstr[1],len);
  677. fpc_chararray_to_shortstr[0]:=chr(len);
  678. end;
  679. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  680. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  681. { inside the compiler, the resulttype is modified to that of the actual }
  682. { chararray we're converting to (JM) }
  683. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  684. var
  685. len: longint;
  686. begin
  687. len := length(src);
  688. if len > arraysize then
  689. len := arraysize;
  690. { make sure we don't access char 1 if length is 0 (JM) }
  691. if len > 0 then
  692. move(src[1],fpc_shortstr_to_chararray[0],len);
  693. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  694. end;
  695. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  696. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  697. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  698. var i : longint;
  699. begin
  700. i:=0;
  701. while p[i]<>#0 do inc(i);
  702. exit(i);
  703. end;
  704. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  705. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  706. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  707. var i : longint;
  708. begin
  709. i:=0;
  710. while p[i]<>#0 do inc(i);
  711. exit(i);
  712. end;
  713. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  714. {****************************************************************************
  715. Caller/StackFrame Helpers
  716. ****************************************************************************}
  717. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  718. {_$error Get_frame must be defined for each processor }
  719. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  720. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  721. {_$error Get_caller_addr must be defined for each processor }
  722. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  723. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  724. {_$error Get_caller_frame must be defined for each processor }
  725. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  726. {****************************************************************************
  727. Math
  728. ****************************************************************************}
  729. {****************************************************************************
  730. Software longint/dword division
  731. ****************************************************************************}
  732. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  733. function count_leading_zeros_32bit(l : longint) : longint;
  734. var
  735. i : longint;
  736. begin
  737. for i:=0 to 31 do
  738. begin
  739. if (l and (longint($80000000) shr i))<>0 then
  740. begin
  741. result:=i;
  742. exit;
  743. end;
  744. end;
  745. result:=i;
  746. end;
  747. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  748. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  749. var
  750. shift,lzz,lzn : longint;
  751. begin
  752. result:=0;
  753. if n=0 then
  754. HandleErrorFrame(200,get_frame);
  755. lzz:=count_leading_zeros_32bit(z);
  756. lzn:=count_leading_zeros_32bit(n);
  757. { if the denominator contains less zeros
  758. then the numerator
  759. the d is greater than the n }
  760. if lzn<lzz then
  761. exit;
  762. shift:=lzn-lzz;
  763. n:=n shl shift;
  764. repeat
  765. if z>=n then
  766. begin
  767. z:=z-n;
  768. result:=result+dword(1 shl shift);
  769. end;
  770. dec(shift);
  771. n:=n shr 1;
  772. until shift<0;
  773. end;
  774. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  775. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  776. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  777. var
  778. shift,lzz,lzn : longint;
  779. begin
  780. result:=0;
  781. if n=0 then
  782. HandleErrorFrame(200,get_frame);
  783. lzz:=count_leading_zeros_32bit(z);
  784. lzn:=count_leading_zeros_32bit(n);
  785. { if the denominator contains less zeros
  786. then the numerator
  787. the d is greater than the n }
  788. if lzn<lzz then
  789. begin
  790. result:=z;
  791. exit;
  792. end;
  793. shift:=lzn-lzz;
  794. n:=n shl shift;
  795. repeat
  796. if z>=n then
  797. z:=z-n;
  798. dec(shift);
  799. n:=n shr 1;
  800. until shift<0;
  801. result:=z;
  802. end;
  803. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  804. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  805. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  806. var
  807. sign : boolean;
  808. d1,d2 : dword;
  809. begin
  810. if n=0 then
  811. HandleErrorFrame(200,get_frame);
  812. sign:=false;
  813. if z<0 then
  814. begin
  815. sign:=not(sign);
  816. d1:=dword(-z);
  817. end
  818. else
  819. d1:=z;
  820. if n<0 then
  821. begin
  822. sign:=not(sign);
  823. d2:=dword(-n);
  824. end
  825. else
  826. d2:=n;
  827. { the div is coded by the compiler as call to divdword }
  828. if sign then
  829. result:=-(d1 div d2)
  830. else
  831. result:=d1 div d2;
  832. end;
  833. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  834. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  835. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  836. var
  837. signed : boolean;
  838. r,nq,zq : dword;
  839. begin
  840. if n=0 then
  841. HandleErrorFrame(200,get_frame);
  842. nq:=abs(n);
  843. if z<0 then
  844. begin
  845. zq:=dword(-z);
  846. signed:=true;
  847. end
  848. else
  849. begin
  850. zq:=z;
  851. signed:=false;
  852. end;
  853. r:=zq mod nq;
  854. if signed then
  855. result:=-longint(r)
  856. else
  857. result:=r;
  858. end;
  859. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  860. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  861. {****************************************************************************}
  862. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  863. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  864. begin
  865. if l<0 then
  866. abs:=-l
  867. else
  868. abs:=l;
  869. end;
  870. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  871. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  872. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  873. begin
  874. odd:=boolean(l and 1);
  875. end;
  876. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  877. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  878. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  879. begin
  880. odd:=boolean(l and 1);
  881. end;
  882. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  883. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  884. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  885. begin
  886. odd:=boolean(longint(l) and 1);
  887. end;
  888. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  889. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  890. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  891. begin
  892. odd:=boolean(longint(l) and 1);
  893. end;
  894. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  895. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  896. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  897. begin
  898. sqr:=l*l;
  899. end;
  900. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  901. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  902. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  903. begin
  904. if l < 0 then
  905. abs := -l
  906. else
  907. abs := l;
  908. end;
  909. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  910. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  911. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  912. begin
  913. sqr := l*l;
  914. end;
  915. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  916. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  917. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  918. begin
  919. sqr := l*l;
  920. end;
  921. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  922. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  923. function declocked(var l:longint):boolean;
  924. begin
  925. Dec(l);
  926. declocked:=(l=0);
  927. end;
  928. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  929. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  930. function declocked(var l:int64):boolean;
  931. begin
  932. Dec(l);
  933. declocked:=(l=0);
  934. end;
  935. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  936. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  937. procedure inclocked(var l:longint);
  938. begin
  939. Inc(l);
  940. end;
  941. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  942. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  943. procedure inclocked(var l:int64);
  944. begin
  945. Inc(l);
  946. end;
  947. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  948. {$ifndef FPC_SYSTEM_HAS_SPTR}
  949. {_$error Sptr must be defined for each processor }
  950. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  951. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  952. begin
  953. if addr mod alignment<>0 then
  954. result:=addr+(alignment-(addr mod alignment))
  955. else
  956. result:=addr;
  957. end;
  958. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  959. begin
  960. if PtrUInt(addr) mod alignment<>0 then
  961. result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
  962. else
  963. result:=addr;
  964. end;
  965. {****************************************************************************
  966. Str()
  967. ****************************************************************************}
  968. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  969. procedure int_str(l:longint;out s:string);
  970. var
  971. m,m1 : longword;
  972. pc,pc2 : pchar;
  973. hs : string[32];
  974. begin
  975. pc2:=@s[1];
  976. if (l<0) then
  977. begin
  978. pc2^:='-';
  979. inc(pc2);
  980. m:=longword(-l);
  981. end
  982. else
  983. m:=longword(l);
  984. pc:=@hs[0];
  985. repeat
  986. inc(pc);
  987. m1:=m div 10;
  988. pc^:=char(m-(m1*10)+byte('0'));
  989. m:=m1;
  990. until m=0;
  991. while (pc>pchar(@hs[0])) do
  992. begin
  993. pc2^:=pc^;
  994. dec(pc);
  995. inc(pc2);
  996. end;
  997. s[0]:=char(pc2-pchar(@s[1]));
  998. end;
  999. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  1000. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1001. procedure int_str(l:longword;out s:string);
  1002. var
  1003. m1 : longword;
  1004. pc,pc2 : pchar;
  1005. hs : string[32];
  1006. begin
  1007. pc2:=@s[1];
  1008. pc:=@hs[0];
  1009. repeat
  1010. inc(pc);
  1011. m1:=l div 10;
  1012. pc^:=char(l-(m1*10)+byte('0'));
  1013. l:=m1;
  1014. until l=0;
  1015. while (pc>pchar(@hs[0])) do
  1016. begin
  1017. pc2^:=pc^;
  1018. dec(pc);
  1019. inc(pc2);
  1020. end;
  1021. s[0]:=char(pc2-pchar(@s[1]));
  1022. end;
  1023. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1024. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1025. procedure int_str(l:int64;out s:string);
  1026. var
  1027. m,m1 : qword;
  1028. pc,pc2 : pchar;
  1029. hs : string[64];
  1030. begin
  1031. pc2:=@s[1];
  1032. if (l<0) then
  1033. begin
  1034. pc2^:='-';
  1035. inc(pc2);
  1036. m:=qword(-l);
  1037. end
  1038. else
  1039. m:=qword(l);
  1040. pc:=@hs[0];
  1041. repeat
  1042. inc(pc);
  1043. m1:=m div 10;
  1044. pc^:=char(m-(m1*10)+byte('0'));
  1045. m:=m1;
  1046. until m=0;
  1047. while (pc>pchar(@hs[0])) do
  1048. begin
  1049. pc2^:=pc^;
  1050. dec(pc);
  1051. inc(pc2);
  1052. end;
  1053. s[0]:=char(pc2-pchar(@s[1]));
  1054. end;
  1055. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1056. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1057. procedure int_str(l:qword;out s:string);
  1058. var
  1059. m1 : qword;
  1060. pc,pc2 : pchar;
  1061. hs : string[64];
  1062. begin
  1063. pc2:=@s[1];
  1064. pc:=@hs[0];
  1065. repeat
  1066. inc(pc);
  1067. m1:=l div 10;
  1068. pc^:=char(l-(m1*10)+byte('0'));
  1069. l:=m1;
  1070. until l=0;
  1071. while (pc>pchar(@hs[0])) do
  1072. begin
  1073. pc2^:=pc^;
  1074. dec(pc);
  1075. inc(pc2);
  1076. end;
  1077. s[0]:=char(pc2-pchar(@s[1]));
  1078. end;
  1079. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1080. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1081. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1082. begin
  1083. softfloat_exception_flags:=0;
  1084. softfloat_exception_mask:=float_flag_underflow or float_flag_inexact or float_flag_denormal;
  1085. end;
  1086. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1087. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1088. function SwapEndian(const AValue: SmallInt): SmallInt;
  1089. begin
  1090. Result := (AValue shr 8) or (AValue shl 8);
  1091. end;
  1092. function SwapEndian(const AValue: Word): Word;
  1093. begin
  1094. Result := (AValue shr 8) or (AValue shl 8);
  1095. end;
  1096. function SwapEndian(const AValue: LongInt): LongInt;
  1097. begin
  1098. Result := (AValue shl 24)
  1099. or ((AValue and $0000FF00) shl 8)
  1100. or ((AValue and $00FF0000) shr 8)
  1101. or (AValue shr 24);
  1102. end;
  1103. function SwapEndian(const AValue: DWord): DWord;
  1104. begin
  1105. Result := (AValue shl 24)
  1106. or ((AValue and $0000FF00) shl 8)
  1107. or ((AValue and $00FF0000) shr 8)
  1108. or (AValue shr 24);
  1109. end;
  1110. function SwapEndian(const AValue: Int64): Int64;
  1111. begin
  1112. Result := (AValue shl 56)
  1113. or ((AValue and $000000000000FF00) shl 40)
  1114. or ((AValue and $0000000000FF0000) shl 24)
  1115. or ((AValue and $00000000FF000000) shl 8)
  1116. or ((AValue and $000000FF00000000) shr 8)
  1117. or ((AValue and $0000FF0000000000) shr 24)
  1118. or ((AValue and $00FF000000000000) shr 40)
  1119. or (AValue shr 56);
  1120. end;
  1121. function SwapEndian(const AValue: QWord): QWord;
  1122. begin
  1123. Result := (AValue shl 56)
  1124. or ((AValue and $000000000000FF00) shl 40)
  1125. or ((AValue and $0000000000FF0000) shl 24)
  1126. or ((AValue and $00000000FF000000) shl 8)
  1127. or ((AValue and $000000FF00000000) shr 8)
  1128. or ((AValue and $0000FF0000000000) shr 24)
  1129. or ((AValue and $00FF000000000000) shr 40)
  1130. or (AValue shr 56);
  1131. end;
  1132. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1133. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1134. begin
  1135. {$IFDEF ENDIAN_BIG}
  1136. Result := AValue;
  1137. {$ELSE}
  1138. Result := SwapEndian(AValue);
  1139. {$ENDIF}
  1140. end;
  1141. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1142. begin
  1143. {$IFDEF ENDIAN_BIG}
  1144. Result := AValue;
  1145. {$ELSE}
  1146. Result := SwapEndian(AValue);
  1147. {$ENDIF}
  1148. end;
  1149. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1150. begin
  1151. {$IFDEF ENDIAN_BIG}
  1152. Result := AValue;
  1153. {$ELSE}
  1154. Result := SwapEndian(AValue);
  1155. {$ENDIF}
  1156. end;
  1157. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1158. begin
  1159. {$IFDEF ENDIAN_BIG}
  1160. Result := AValue;
  1161. {$ELSE}
  1162. Result := SwapEndian(AValue);
  1163. {$ENDIF}
  1164. end;
  1165. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1166. begin
  1167. {$IFDEF ENDIAN_BIG}
  1168. Result := AValue;
  1169. {$ELSE}
  1170. Result := SwapEndian(AValue);
  1171. {$ENDIF}
  1172. end;
  1173. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1174. begin
  1175. {$IFDEF ENDIAN_BIG}
  1176. Result := AValue;
  1177. {$ELSE}
  1178. Result := SwapEndian(AValue);
  1179. {$ENDIF}
  1180. end;
  1181. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1182. begin
  1183. {$IFDEF ENDIAN_LITTLE}
  1184. Result := AValue;
  1185. {$ELSE}
  1186. Result := SwapEndian(AValue);
  1187. {$ENDIF}
  1188. end;
  1189. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1190. begin
  1191. {$IFDEF ENDIAN_LITTLE}
  1192. Result := AValue;
  1193. {$ELSE}
  1194. Result := SwapEndian(AValue);
  1195. {$ENDIF}
  1196. end;
  1197. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1198. begin
  1199. {$IFDEF ENDIAN_LITTLE}
  1200. Result := AValue;
  1201. {$ELSE}
  1202. Result := SwapEndian(AValue);
  1203. {$ENDIF}
  1204. end;
  1205. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1206. begin
  1207. {$IFDEF ENDIAN_LITTLE}
  1208. Result := AValue;
  1209. {$ELSE}
  1210. Result := SwapEndian(AValue);
  1211. {$ENDIF}
  1212. end;
  1213. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1214. begin
  1215. {$IFDEF ENDIAN_LITTLE}
  1216. Result := AValue;
  1217. {$ELSE}
  1218. Result := SwapEndian(AValue);
  1219. {$ENDIF}
  1220. end;
  1221. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1222. begin
  1223. {$IFDEF ENDIAN_LITTLE}
  1224. Result := AValue;
  1225. {$ELSE}
  1226. Result := SwapEndian(AValue);
  1227. {$ENDIF}
  1228. end;
  1229. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1230. begin
  1231. {$IFDEF ENDIAN_BIG}
  1232. Result := AValue;
  1233. {$ELSE}
  1234. Result := SwapEndian(AValue);
  1235. {$ENDIF}
  1236. end;
  1237. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1238. begin
  1239. {$IFDEF ENDIAN_BIG}
  1240. Result := AValue;
  1241. {$ELSE}
  1242. Result := SwapEndian(AValue);
  1243. {$ENDIF}
  1244. end;
  1245. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1246. begin
  1247. {$IFDEF ENDIAN_BIG}
  1248. Result := AValue;
  1249. {$ELSE}
  1250. Result := SwapEndian(AValue);
  1251. {$ENDIF}
  1252. end;
  1253. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1254. begin
  1255. {$IFDEF ENDIAN_BIG}
  1256. Result := AValue;
  1257. {$ELSE}
  1258. Result := SwapEndian(AValue);
  1259. {$ENDIF}
  1260. end;
  1261. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1262. begin
  1263. {$IFDEF ENDIAN_BIG}
  1264. Result := AValue;
  1265. {$ELSE}
  1266. Result := SwapEndian(AValue);
  1267. {$ENDIF}
  1268. end;
  1269. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1270. begin
  1271. {$IFDEF ENDIAN_BIG}
  1272. Result := AValue;
  1273. {$ELSE}
  1274. Result := SwapEndian(AValue);
  1275. {$ENDIF}
  1276. end;
  1277. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1278. begin
  1279. {$IFDEF ENDIAN_LITTLE}
  1280. Result := AValue;
  1281. {$ELSE}
  1282. Result := SwapEndian(AValue);
  1283. {$ENDIF}
  1284. end;
  1285. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1286. begin
  1287. {$IFDEF ENDIAN_LITTLE}
  1288. Result := AValue;
  1289. {$ELSE}
  1290. Result := SwapEndian(AValue);
  1291. {$ENDIF}
  1292. end;
  1293. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1294. begin
  1295. {$IFDEF ENDIAN_LITTLE}
  1296. Result := AValue;
  1297. {$ELSE}
  1298. Result := SwapEndian(AValue);
  1299. {$ENDIF}
  1300. end;
  1301. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1302. begin
  1303. {$IFDEF ENDIAN_LITTLE}
  1304. Result := AValue;
  1305. {$ELSE}
  1306. Result := SwapEndian(AValue);
  1307. {$ENDIF}
  1308. end;
  1309. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1310. begin
  1311. {$IFDEF ENDIAN_LITTLE}
  1312. Result := AValue;
  1313. {$ELSE}
  1314. Result := SwapEndian(AValue);
  1315. {$ENDIF}
  1316. end;
  1317. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1318. begin
  1319. {$IFDEF ENDIAN_LITTLE}
  1320. Result := AValue;
  1321. {$ELSE}
  1322. Result := SwapEndian(AValue);
  1323. {$ENDIF}
  1324. end;