generic.inc 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  322. { Note: _vmt will be reset to -1 when memory is allocated,
  323. this is needed for fpc_help_fail }
  324. function fpc_help_constructor(_self:pointer;var _vmt:pointer;_vmt_pos:cardinal):pointer;[public,alias:'FPC_HELP_CONSTRUCTOR'];compilerproc;
  325. type
  326. ppointer = ^pointer;
  327. pvmt = ^tvmt;
  328. tvmt=packed record
  329. size,msize:ptrint;
  330. parent:pointer;
  331. end;
  332. var
  333. vmtcopy : pointer;
  334. begin
  335. { Inherited call? }
  336. if _vmt=nil then
  337. begin
  338. fpc_help_constructor:=_self;
  339. exit;
  340. end;
  341. vmtcopy:=_vmt;
  342. if (_self=nil) and
  343. (pvmt(_vmt)^.size>0) then
  344. begin
  345. getmem(_self,pvmt(_vmt)^.size);
  346. { reset vmt needed for fail }
  347. _vmt:=pointer(-1);
  348. end;
  349. if _self<>nil then
  350. begin
  351. fillchar(_self^,pvmt(vmtcopy)^.size,#0);
  352. ppointer(_self+_vmt_pos)^:=vmtcopy;
  353. end;
  354. fpc_help_constructor:=_self;
  355. end;
  356. {$endif FPC_SYSTEM_HAS_FPC_HELP_CONSTRUCTOR}
  357. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  358. { Note: _self will not be reset, the compiler has to generate the reset }
  359. procedure fpc_help_destructor(_self,_vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_DESTRUCTOR']; compilerproc;
  360. type
  361. ppointer = ^pointer;
  362. pvmt = ^tvmt;
  363. tvmt = packed record
  364. size,msize : ptrint;
  365. parent : pointer;
  366. end;
  367. begin
  368. { already released? }
  369. if (_self=nil) or
  370. (_vmt=nil) or
  371. (ppointer(_self+vmt_pos)^=nil) then
  372. exit;
  373. if (pvmt(ppointer(_self+vmt_pos)^)^.size=0) or
  374. (pvmt(ppointer(_self+vmt_pos)^)^.size+pvmt(ppointer(_self+vmt_pos)^)^.msize<>0) then
  375. RunError(210);
  376. { reset vmt to nil for protection }
  377. ppointer(_self+vmt_pos)^:=nil;
  378. freemem(_self);
  379. end;
  380. {$endif FPC_SYSTEM_HAS_FPC_HELP_DESTRUCTOR}
  381. {$ifndef FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  382. { Note: _self will not be reset, the compiler has to generate the reset }
  383. procedure fpc_help_fail(_self:pointer;var _vmt:pointer;vmt_pos:cardinal);[public,alias:'FPC_HELP_FAIL'];compilerproc;
  384. type
  385. ppointer = ^pointer;
  386. pvmt = ^tvmt;
  387. tvmt = packed record
  388. size,msize : ptrint;
  389. parent : pointer;
  390. end;
  391. begin
  392. if (_self=nil) or (_vmt=nil) then
  393. exit;
  394. { vmt=-1 when memory was allocated }
  395. if ptrint(_vmt)=-1 then
  396. begin
  397. if (_self=nil) or (ppointer(_self+vmt_pos)^=nil) then
  398. HandleError(210)
  399. else
  400. begin
  401. ppointer(_self+vmt_pos)^:=nil;
  402. freemem(_self);
  403. { reset _vmt to nil so it will not be freed a
  404. second time }
  405. _vmt:=nil;
  406. end;
  407. end
  408. else
  409. ppointer(_self+vmt_pos)^:=nil;
  410. end;
  411. {$endif FPC_SYSTEM_HAS_FPC_HELP_FAIL}
  412. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  413. procedure fpc_check_object(_vmt : pointer); [public,alias:'FPC_CHECK_OBJECT']; compilerproc;
  414. type
  415. pvmt = ^tvmt;
  416. tvmt = packed record
  417. size,msize : ptrint;
  418. parent : pointer;
  419. end;
  420. begin
  421. if (_vmt=nil) or
  422. (pvmt(_vmt)^.size=0) or
  423. (pvmt(_vmt)^.size+pvmt(_vmt)^.msize<>0) then
  424. RunError(210);
  425. end;
  426. {$endif ndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT}
  427. {$ifndef FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  428. { checks for a correct vmt pointer }
  429. { deeper check to see if the current object is }
  430. { really related to the true }
  431. procedure fpc_check_object_ext(vmt, expvmt : pointer); [public,alias:'FPC_CHECK_OBJECT_EXT']; compilerproc;
  432. type
  433. pvmt = ^tvmt;
  434. tvmt = packed record
  435. size,msize : ptrint;
  436. parent : pointer;
  437. end;
  438. begin
  439. if (vmt=nil) or
  440. (pvmt(vmt)^.size=0) or
  441. (pvmt(vmt)^.size+pvmt(vmt)^.msize<>0) then
  442. RunError(210);
  443. while assigned(vmt) do
  444. if vmt=expvmt then
  445. exit
  446. else
  447. vmt:=pvmt(vmt)^.parent;
  448. RunError(219);
  449. end;
  450. {$endif not FPC_SYSTEM_HAS_FPC_CHECK_OBJECT_EXT}
  451. {****************************************************************************
  452. String
  453. ****************************************************************************}
  454. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  455. function fpc_shortstr_to_shortstr(len:longint;const sstr:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_TO_SHORTSTR']; compilerproc;
  456. var
  457. slen : byte;
  458. begin
  459. slen:=length(sstr);
  460. if slen<len then
  461. len:=slen;
  462. move(sstr[0],result[0],len+1);
  463. if slen>len then
  464. result[0]:=chr(len);
  465. end;
  466. procedure fpc_shortstr_assign(len:longint;sstr,dstr:pointer);[public,alias:'FPC_SHORTSTR_ASSIGN']; {$ifdef HAS_COMPILER_PROC} compilerproc; {$endif}
  467. var
  468. slen : byte;
  469. type
  470. pstring = ^string;
  471. begin
  472. slen:=length(pstring(sstr)^);
  473. if slen<len then
  474. len:=slen;
  475. move(sstr^,dstr^,len+1);
  476. if slen>len then
  477. pchar(dstr)^:=chr(len);
  478. end;
  479. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_ASSIGN}
  480. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  481. {$ifndef STR_CONCAT_PROCS}
  482. function fpc_shortstr_concat(const s1,s2:shortstring): shortstring;[public,alias:'FPC_SHORTSTR_CONCAT']; compilerproc;
  483. var
  484. s1l, s2l : byte;
  485. begin
  486. s1l:=length(s1);
  487. s2l:=length(s2);
  488. if s1l+s2l>255 then
  489. s2l:=255-s1l;
  490. move(s1[1],fpc_shortstr_concat[1],s1l);
  491. move(s2[1],fpc_shortstr_concat[s1l+1],s2l);
  492. fpc_shortstr_concat[0]:=chr(s1l+s2l);
  493. end;
  494. {$else STR_CONCAT_PROCS}
  495. procedure fpc_shortstr_concat(var dests:shortstring;const s1,s2:shortstring);compilerproc;
  496. var
  497. s1l, s2l : byte;
  498. begin
  499. s1l:=length(s1);
  500. s2l:=length(s2);
  501. if s1l+s2l>high(dests) then
  502. s2l:=high(dests)-s1l;
  503. if @dests=@s1 then
  504. move(s2[1],dests[s1l+1],s2l)
  505. else
  506. if @dests=@s2 then
  507. begin
  508. move(dests[1],dests[s1l+1],s2l);
  509. move(s1[1],dests[1],s1l);
  510. end
  511. else
  512. begin
  513. move(s1[1],dests[1],s1l);
  514. move(s2[1],dests[s1l+1],s2l);
  515. end;
  516. dests[0]:=chr(s1l+s2l);
  517. end;
  518. procedure fpc_shortstr_concat_multi(var dests:shortstring;const sarr:array of pshortstring);compilerproc;
  519. var
  520. s2l : byte;
  521. LowStart,i,
  522. Len : longint;
  523. pc : pchar;
  524. needtemp : boolean;
  525. tmpstr : shortstring;
  526. p,pdest : pshortstring;
  527. begin
  528. if high(sarr)=0 then
  529. begin
  530. DestS:='';
  531. exit;
  532. end;
  533. lowstart:=low(sarr);
  534. if Pointer(@DestS)=Pointer(sarr[lowstart]) then
  535. inc(lowstart);
  536. { Check for another reuse, then we can't use
  537. the append optimization and need to use a temp }
  538. needtemp:=false;
  539. for i:=lowstart to high(sarr) do
  540. begin
  541. if Pointer(@DestS)=Pointer(sarr[i]) then
  542. begin
  543. needtemp:=true;
  544. break;
  545. end;
  546. end;
  547. if needtemp then
  548. begin
  549. lowstart:=low(sarr);
  550. tmpstr:='';
  551. pdest:=@tmpstr
  552. end
  553. else
  554. begin
  555. { Start with empty DestS if we start with concatting
  556. the first array element }
  557. if lowstart=low(sarr) then
  558. DestS:='';
  559. pdest:=@DestS;
  560. end;
  561. { Concat all strings, except the string we already
  562. copied in DestS }
  563. Len:=length(pdest^);
  564. pc:=@pdest^[1+Length(pdest^)];
  565. for i:=lowstart to high(sarr) do
  566. begin
  567. p:=sarr[i];
  568. if assigned(p) then
  569. begin
  570. s2l:=length(p^);
  571. if Len+s2l>high(dests) then
  572. s2l:=high(dests)-Len;
  573. Move(p^[1],pc^,s2l);
  574. inc(pc,s2l);
  575. inc(Len,s2l);
  576. end;
  577. end;
  578. pdest^[0]:=Chr(Len);
  579. if needtemp then
  580. DestS:=TmpStr;
  581. end;
  582. {$endif STR_CONCAT_PROCS}
  583. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_CONCAT}
  584. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  585. procedure fpc_shortstr_append_shortstr(var s1:shortstring;const s2:shortstring);compilerproc;
  586. [public,alias:'FPC_SHORTSTR_APPEND_SHORTSTR'];
  587. var
  588. s1l, s2l : integer;
  589. begin
  590. s1l:=length(s1);
  591. s2l:=length(s2);
  592. if s1l+s2l>high(s1) then
  593. s2l:=high(s1)-s1l;
  594. move(s2[1],s1[s1l+1],s2l);
  595. s1[0]:=chr(s1l+s2l);
  596. end;
  597. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_APPEND_SHORTSTR}
  598. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  599. function fpc_shortstr_compare(const left,right:shortstring) : longint;[public,alias:'FPC_SHORTSTR_COMPARE']; compilerproc;
  600. var
  601. s1,s2,max,i : byte;
  602. d : longint;
  603. begin
  604. s1:=length(left);
  605. s2:=length(right);
  606. if s1<s2 then
  607. max:=s1
  608. else
  609. max:=s2;
  610. for i:=1 to max do
  611. begin
  612. d:=byte(left[i])-byte(right[i]);
  613. if d>0 then
  614. exit(1)
  615. else if d<0 then
  616. exit(-1);
  617. end;
  618. if s1>s2 then
  619. exit(1)
  620. else if s1<s2 then
  621. exit(-1)
  622. else
  623. exit(0);
  624. end;
  625. {$endif ndef FPC_SYSTEM_HAS_FPC_SHORTSTR_COMPARE}
  626. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  627. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  628. var
  629. l : longint;
  630. s: shortstring;
  631. begin
  632. if p=nil then
  633. l:=0
  634. else
  635. l:=strlen(p);
  636. if l>255 then
  637. l:=255;
  638. if l>0 then
  639. move(p^,s[1],l);
  640. s[0]:=chr(l);
  641. fpc_pchar_to_shortstr := s;
  642. end;
  643. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  644. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  645. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  646. var
  647. l: longint;
  648. index: longint;
  649. len: byte;
  650. begin
  651. l := high(arr)+1;
  652. if l>=256 then
  653. l:=255
  654. else if l<0 then
  655. l:=0;
  656. if (zerobased) then
  657. begin
  658. index:=IndexByte(arr[0],l,0);
  659. if (index < 0) then
  660. len := l
  661. else
  662. len := index;
  663. end
  664. else
  665. len := l;
  666. move(arr[0],fpc_chararray_to_shortstr[1],len);
  667. fpc_chararray_to_shortstr[0]:=chr(len);
  668. end;
  669. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  670. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  671. { inside the compiler, the resulttype is modified to that of the actual }
  672. { chararray we're converting to (JM) }
  673. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  674. var
  675. len: longint;
  676. begin
  677. len := length(src);
  678. if len > arraysize then
  679. len := arraysize;
  680. { make sure we don't access char 1 if length is 0 (JM) }
  681. if len > 0 then
  682. move(src[1],fpc_shortstr_to_chararray[0],len);
  683. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  684. end;
  685. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  686. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  687. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  688. var i : longint;
  689. begin
  690. i:=0;
  691. while p[i]<>#0 do inc(i);
  692. exit(i);
  693. end;
  694. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  695. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  696. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  697. var i : longint;
  698. begin
  699. i:=0;
  700. while p[i]<>#0 do inc(i);
  701. exit(i);
  702. end;
  703. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  704. {****************************************************************************
  705. Caller/StackFrame Helpers
  706. ****************************************************************************}
  707. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  708. {_$error Get_frame must be defined for each processor }
  709. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  710. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  711. {_$error Get_caller_addr must be defined for each processor }
  712. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  713. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  714. {_$error Get_caller_frame must be defined for each processor }
  715. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  716. {****************************************************************************
  717. Math
  718. ****************************************************************************}
  719. {****************************************************************************
  720. Software longint/dword division
  721. ****************************************************************************}
  722. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  723. function count_leading_zeros_32bit(l : longint) : longint;
  724. var
  725. i : longint;
  726. begin
  727. for i:=0 to 31 do
  728. begin
  729. if (l and (longint($80000000) shr i))<>0 then
  730. begin
  731. result:=i;
  732. exit;
  733. end;
  734. end;
  735. result:=i;
  736. end;
  737. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  738. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  739. var
  740. shift,lzz,lzn : longint;
  741. begin
  742. result:=0;
  743. if n=0 then
  744. HandleErrorFrame(200,get_frame);
  745. lzz:=count_leading_zeros_32bit(z);
  746. lzn:=count_leading_zeros_32bit(n);
  747. { if the denominator contains less zeros
  748. then the numerator
  749. the d is greater than the n }
  750. if lzn<lzz then
  751. exit;
  752. shift:=lzn-lzz;
  753. n:=n shl shift;
  754. repeat
  755. if z>=n then
  756. begin
  757. z:=z-n;
  758. result:=result+dword(1 shl shift);
  759. end;
  760. dec(shift);
  761. n:=n shr 1;
  762. until shift<0;
  763. end;
  764. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  765. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  766. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  767. var
  768. shift,lzz,lzn : longint;
  769. begin
  770. result:=0;
  771. if n=0 then
  772. HandleErrorFrame(200,get_frame);
  773. lzz:=count_leading_zeros_32bit(z);
  774. lzn:=count_leading_zeros_32bit(n);
  775. { if the denominator contains less zeros
  776. then the numerator
  777. the d is greater than the n }
  778. if lzn<lzz then
  779. begin
  780. result:=z;
  781. exit;
  782. end;
  783. shift:=lzn-lzz;
  784. n:=n shl shift;
  785. repeat
  786. if z>=n then
  787. z:=z-n;
  788. dec(shift);
  789. n:=n shr 1;
  790. until shift<0;
  791. result:=z;
  792. end;
  793. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  794. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  795. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  796. var
  797. sign : boolean;
  798. d1,d2 : dword;
  799. begin
  800. if n=0 then
  801. HandleErrorFrame(200,get_frame);
  802. sign:=false;
  803. if z<0 then
  804. begin
  805. sign:=not(sign);
  806. d1:=dword(-z);
  807. end
  808. else
  809. d1:=z;
  810. if n<0 then
  811. begin
  812. sign:=not(sign);
  813. d2:=dword(-n);
  814. end
  815. else
  816. d2:=n;
  817. { the div is coded by the compiler as call to divdword }
  818. if sign then
  819. result:=-(d1 div d2)
  820. else
  821. result:=d1 div d2;
  822. end;
  823. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  824. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  825. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  826. var
  827. signed : boolean;
  828. r,nq,zq : dword;
  829. begin
  830. if n=0 then
  831. HandleErrorFrame(200,get_frame);
  832. nq:=abs(n);
  833. if z<0 then
  834. begin
  835. zq:=dword(-z);
  836. signed:=true;
  837. end
  838. else
  839. begin
  840. zq:=z;
  841. signed:=false;
  842. end;
  843. r:=zq mod nq;
  844. if signed then
  845. result:=-longint(r)
  846. else
  847. result:=r;
  848. end;
  849. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  850. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  851. {****************************************************************************}
  852. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  853. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  854. begin
  855. if l<0 then
  856. abs:=-l
  857. else
  858. abs:=l;
  859. end;
  860. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  861. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  862. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  863. begin
  864. odd:=boolean(l and 1);
  865. end;
  866. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  867. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  868. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  869. begin
  870. odd:=boolean(l and 1);
  871. end;
  872. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  873. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  874. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  875. begin
  876. odd:=boolean(longint(l) and 1);
  877. end;
  878. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  879. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  880. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  881. begin
  882. odd:=boolean(longint(l) and 1);
  883. end;
  884. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  885. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  886. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  887. begin
  888. sqr:=l*l;
  889. end;
  890. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  891. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  892. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  893. begin
  894. if l < 0 then
  895. abs := -l
  896. else
  897. abs := l;
  898. end;
  899. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  900. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  901. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  902. begin
  903. sqr := l*l;
  904. end;
  905. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  906. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  907. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  908. begin
  909. sqr := l*l;
  910. end;
  911. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  912. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  913. function declocked(var l:longint):boolean;
  914. begin
  915. Dec(l);
  916. declocked:=(l=0);
  917. end;
  918. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  919. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  920. function declocked(var l:int64):boolean;
  921. begin
  922. Dec(l);
  923. declocked:=(l=0);
  924. end;
  925. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  926. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  927. procedure inclocked(var l:longint);
  928. begin
  929. Inc(l);
  930. end;
  931. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  932. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  933. procedure inclocked(var l:int64);
  934. begin
  935. Inc(l);
  936. end;
  937. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  938. {$ifndef FPC_SYSTEM_HAS_SPTR}
  939. {_$error Sptr must be defined for each processor }
  940. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  941. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  942. begin
  943. if addr mod alignment<>0 then
  944. result:=addr+(alignment-(addr mod alignment))
  945. else
  946. result:=addr;
  947. end;
  948. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  949. begin
  950. if PtrUInt(addr) mod alignment<>0 then
  951. result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
  952. else
  953. result:=addr;
  954. end;
  955. {****************************************************************************
  956. Str()
  957. ****************************************************************************}
  958. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  959. procedure int_str(l:longint;out s:string);
  960. var
  961. m,m1 : longword;
  962. pc,pc2 : pchar;
  963. hs : string[32];
  964. begin
  965. pc2:=@s[1];
  966. if (l<0) then
  967. begin
  968. pc2^:='-';
  969. inc(pc2);
  970. m:=longword(-l);
  971. end
  972. else
  973. m:=longword(l);
  974. pc:=@hs[0];
  975. repeat
  976. inc(pc);
  977. m1:=m div 10;
  978. pc^:=char(m-(m1*10)+byte('0'));
  979. m:=m1;
  980. until m=0;
  981. while (pc>pchar(@hs[0])) do
  982. begin
  983. pc2^:=pc^;
  984. dec(pc);
  985. inc(pc2);
  986. end;
  987. s[0]:=char(pc2-pchar(@s[1]));
  988. end;
  989. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  990. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  991. procedure int_str(l:longword;out s:string);
  992. var
  993. m1 : longword;
  994. pc,pc2 : pchar;
  995. hs : string[32];
  996. begin
  997. pc2:=@s[1];
  998. pc:=@hs[0];
  999. repeat
  1000. inc(pc);
  1001. m1:=l div 10;
  1002. pc^:=char(l-(m1*10)+byte('0'));
  1003. l:=m1;
  1004. until l=0;
  1005. while (pc>pchar(@hs[0])) do
  1006. begin
  1007. pc2^:=pc^;
  1008. dec(pc);
  1009. inc(pc2);
  1010. end;
  1011. s[0]:=char(pc2-pchar(@s[1]));
  1012. end;
  1013. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1014. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1015. procedure int_str(l:int64;out s:string);
  1016. var
  1017. m,m1 : qword;
  1018. pc,pc2 : pchar;
  1019. hs : string[64];
  1020. begin
  1021. pc2:=@s[1];
  1022. if (l<0) then
  1023. begin
  1024. pc2^:='-';
  1025. inc(pc2);
  1026. m:=qword(-l);
  1027. end
  1028. else
  1029. m:=qword(l);
  1030. pc:=@hs[0];
  1031. repeat
  1032. inc(pc);
  1033. m1:=m div 10;
  1034. pc^:=char(m-(m1*10)+byte('0'));
  1035. m:=m1;
  1036. until m=0;
  1037. while (pc>pchar(@hs[0])) do
  1038. begin
  1039. pc2^:=pc^;
  1040. dec(pc);
  1041. inc(pc2);
  1042. end;
  1043. s[0]:=char(pc2-pchar(@s[1]));
  1044. end;
  1045. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1046. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1047. procedure int_str(l:qword;out s:string);
  1048. var
  1049. m1 : qword;
  1050. pc,pc2 : pchar;
  1051. hs : string[64];
  1052. begin
  1053. pc2:=@s[1];
  1054. pc:=@hs[0];
  1055. repeat
  1056. inc(pc);
  1057. m1:=l div 10;
  1058. pc^:=char(l-(m1*10)+byte('0'));
  1059. l:=m1;
  1060. until l=0;
  1061. while (pc>pchar(@hs[0])) do
  1062. begin
  1063. pc2^:=pc^;
  1064. dec(pc);
  1065. inc(pc2);
  1066. end;
  1067. s[0]:=char(pc2-pchar(@s[1]));
  1068. end;
  1069. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1070. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1071. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1072. begin
  1073. { nothing todo }
  1074. end;
  1075. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1076. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1077. function SwapEndian(const AValue: SmallInt): SmallInt;
  1078. begin
  1079. Result := (AValue shr 8) or (AValue shl 8);
  1080. end;
  1081. function SwapEndian(const AValue: Word): Word;
  1082. begin
  1083. Result := (AValue shr 8) or (AValue shl 8);
  1084. end;
  1085. function SwapEndian(const AValue: LongInt): LongInt;
  1086. begin
  1087. Result := (AValue shl 24)
  1088. or ((AValue and $0000FF00) shl 8)
  1089. or ((AValue and $00FF0000) shr 8)
  1090. or (AValue shr 24);
  1091. end;
  1092. function SwapEndian(const AValue: DWord): DWord;
  1093. begin
  1094. Result := (AValue shl 24)
  1095. or ((AValue and $0000FF00) shl 8)
  1096. or ((AValue and $00FF0000) shr 8)
  1097. or (AValue shr 24);
  1098. end;
  1099. function SwapEndian(const AValue: Int64): Int64;
  1100. begin
  1101. Result := (AValue shl 56)
  1102. or ((AValue and $000000000000FF00) shl 40)
  1103. or ((AValue and $0000000000FF0000) shl 24)
  1104. or ((AValue and $00000000FF000000) shl 8)
  1105. or ((AValue and $000000FF00000000) shr 8)
  1106. or ((AValue and $0000FF0000000000) shr 24)
  1107. or ((AValue and $00FF000000000000) shr 40)
  1108. or (AValue shr 56);
  1109. end;
  1110. function SwapEndian(const AValue: QWord): QWord;
  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. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1122. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1123. begin
  1124. {$IFDEF ENDIAN_BIG}
  1125. Result := AValue;
  1126. {$ELSE}
  1127. Result := SwapEndian(AValue);
  1128. {$ENDIF}
  1129. end;
  1130. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1131. begin
  1132. {$IFDEF ENDIAN_BIG}
  1133. Result := AValue;
  1134. {$ELSE}
  1135. Result := SwapEndian(AValue);
  1136. {$ENDIF}
  1137. end;
  1138. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1139. begin
  1140. {$IFDEF ENDIAN_BIG}
  1141. Result := AValue;
  1142. {$ELSE}
  1143. Result := SwapEndian(AValue);
  1144. {$ENDIF}
  1145. end;
  1146. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1147. begin
  1148. {$IFDEF ENDIAN_BIG}
  1149. Result := AValue;
  1150. {$ELSE}
  1151. Result := SwapEndian(AValue);
  1152. {$ENDIF}
  1153. end;
  1154. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1155. begin
  1156. {$IFDEF ENDIAN_BIG}
  1157. Result := AValue;
  1158. {$ELSE}
  1159. Result := SwapEndian(AValue);
  1160. {$ENDIF}
  1161. end;
  1162. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1163. begin
  1164. {$IFDEF ENDIAN_BIG}
  1165. Result := AValue;
  1166. {$ELSE}
  1167. Result := SwapEndian(AValue);
  1168. {$ENDIF}
  1169. end;
  1170. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1171. begin
  1172. {$IFDEF ENDIAN_LITTLE}
  1173. Result := AValue;
  1174. {$ELSE}
  1175. Result := SwapEndian(AValue);
  1176. {$ENDIF}
  1177. end;
  1178. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1179. begin
  1180. {$IFDEF ENDIAN_LITTLE}
  1181. Result := AValue;
  1182. {$ELSE}
  1183. Result := SwapEndian(AValue);
  1184. {$ENDIF}
  1185. end;
  1186. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1187. begin
  1188. {$IFDEF ENDIAN_LITTLE}
  1189. Result := AValue;
  1190. {$ELSE}
  1191. Result := SwapEndian(AValue);
  1192. {$ENDIF}
  1193. end;
  1194. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1195. begin
  1196. {$IFDEF ENDIAN_LITTLE}
  1197. Result := AValue;
  1198. {$ELSE}
  1199. Result := SwapEndian(AValue);
  1200. {$ENDIF}
  1201. end;
  1202. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1203. begin
  1204. {$IFDEF ENDIAN_LITTLE}
  1205. Result := AValue;
  1206. {$ELSE}
  1207. Result := SwapEndian(AValue);
  1208. {$ENDIF}
  1209. end;
  1210. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1211. begin
  1212. {$IFDEF ENDIAN_LITTLE}
  1213. Result := AValue;
  1214. {$ELSE}
  1215. Result := SwapEndian(AValue);
  1216. {$ENDIF}
  1217. end;
  1218. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1219. begin
  1220. {$IFDEF ENDIAN_BIG}
  1221. Result := AValue;
  1222. {$ELSE}
  1223. Result := SwapEndian(AValue);
  1224. {$ENDIF}
  1225. end;
  1226. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1227. begin
  1228. {$IFDEF ENDIAN_BIG}
  1229. Result := AValue;
  1230. {$ELSE}
  1231. Result := SwapEndian(AValue);
  1232. {$ENDIF}
  1233. end;
  1234. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1235. begin
  1236. {$IFDEF ENDIAN_BIG}
  1237. Result := AValue;
  1238. {$ELSE}
  1239. Result := SwapEndian(AValue);
  1240. {$ENDIF}
  1241. end;
  1242. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1243. begin
  1244. {$IFDEF ENDIAN_BIG}
  1245. Result := AValue;
  1246. {$ELSE}
  1247. Result := SwapEndian(AValue);
  1248. {$ENDIF}
  1249. end;
  1250. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1251. begin
  1252. {$IFDEF ENDIAN_BIG}
  1253. Result := AValue;
  1254. {$ELSE}
  1255. Result := SwapEndian(AValue);
  1256. {$ENDIF}
  1257. end;
  1258. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1259. begin
  1260. {$IFDEF ENDIAN_BIG}
  1261. Result := AValue;
  1262. {$ELSE}
  1263. Result := SwapEndian(AValue);
  1264. {$ENDIF}
  1265. end;
  1266. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1267. begin
  1268. {$IFDEF ENDIAN_LITTLE}
  1269. Result := AValue;
  1270. {$ELSE}
  1271. Result := SwapEndian(AValue);
  1272. {$ENDIF}
  1273. end;
  1274. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1275. begin
  1276. {$IFDEF ENDIAN_LITTLE}
  1277. Result := AValue;
  1278. {$ELSE}
  1279. Result := SwapEndian(AValue);
  1280. {$ENDIF}
  1281. end;
  1282. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1283. begin
  1284. {$IFDEF ENDIAN_LITTLE}
  1285. Result := AValue;
  1286. {$ELSE}
  1287. Result := SwapEndian(AValue);
  1288. {$ENDIF}
  1289. end;
  1290. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1291. begin
  1292. {$IFDEF ENDIAN_LITTLE}
  1293. Result := AValue;
  1294. {$ELSE}
  1295. Result := SwapEndian(AValue);
  1296. {$ENDIF}
  1297. end;
  1298. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1299. begin
  1300. {$IFDEF ENDIAN_LITTLE}
  1301. Result := AValue;
  1302. {$ELSE}
  1303. Result := SwapEndian(AValue);
  1304. {$ENDIF}
  1305. end;
  1306. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1307. begin
  1308. {$IFDEF ENDIAN_LITTLE}
  1309. Result := AValue;
  1310. {$ELSE}
  1311. Result := SwapEndian(AValue);
  1312. {$ENDIF}
  1313. end;