generic.inc 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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 : byte;
  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_PCHAR_TO_SHORTSTR}
  629. function fpc_pchar_to_shortstr(p:pchar):shortstring;[public,alias:'FPC_PCHAR_TO_SHORTSTR']; compilerproc;
  630. var
  631. l : longint;
  632. s: shortstring;
  633. begin
  634. if p=nil then
  635. l:=0
  636. else
  637. l:=strlen(p);
  638. if l>255 then
  639. l:=255;
  640. if l>0 then
  641. move(p^,s[1],l);
  642. s[0]:=chr(l);
  643. fpc_pchar_to_shortstr := s;
  644. end;
  645. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_TO_SHORTSTR}
  646. {$ifndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  647. function fpc_chararray_to_shortstr(const arr: array of char; zerobased: boolean = true):shortstring;[public,alias:'FPC_CHARARRAY_TO_SHORTSTR']; compilerproc;
  648. var
  649. l: longint;
  650. index: longint;
  651. len: byte;
  652. begin
  653. l := high(arr)+1;
  654. if l>=256 then
  655. l:=255
  656. else if l<0 then
  657. l:=0;
  658. if (zerobased) then
  659. begin
  660. index:=IndexByte(arr[0],l,0);
  661. if (index < 0) then
  662. len := l
  663. else
  664. len := index;
  665. end
  666. else
  667. len := l;
  668. move(arr[0],fpc_chararray_to_shortstr[1],len);
  669. fpc_chararray_to_shortstr[0]:=chr(len);
  670. end;
  671. {$endif ndef FPC_SYSTEM_HAS_FPC_CHARARRAY_TO_SHORTSTR}
  672. {$ifndef FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  673. { inside the compiler, the resulttype is modified to that of the actual }
  674. { chararray we're converting to (JM) }
  675. function fpc_shortstr_to_chararray(arraysize: longint; const src: ShortString): fpc_big_chararray;[public,alias: 'FPC_SHORTSTR_TO_CHARARRAY']; compilerproc;
  676. var
  677. len: longint;
  678. begin
  679. len := length(src);
  680. if len > arraysize then
  681. len := arraysize;
  682. { make sure we don't access char 1 if length is 0 (JM) }
  683. if len > 0 then
  684. move(src[1],fpc_shortstr_to_chararray[0],len);
  685. fillchar(fpc_shortstr_to_chararray[len],arraysize-len,0);
  686. end;
  687. {$endif FPC_SYSTEM_HAS_FPC_SHORTSTR_TO_CHARARRAY}
  688. {$ifndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  689. function fpc_pchar_length(p:pchar):longint;[public,alias:'FPC_PCHAR_LENGTH']; compilerproc;
  690. var i : longint;
  691. begin
  692. i:=0;
  693. while p[i]<>#0 do inc(i);
  694. exit(i);
  695. end;
  696. {$endif ndef FPC_SYSTEM_HAS_FPC_PCHAR_LENGTH}
  697. {$ifndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  698. function fpc_pwidechar_length(p:pwidechar):longint;[public,alias:'FPC_PWIDECHAR_LENGTH']; compilerproc;
  699. var i : longint;
  700. begin
  701. i:=0;
  702. while p[i]<>#0 do inc(i);
  703. exit(i);
  704. end;
  705. {$endif ndef FPC_SYSTEM_HAS_FPC_PWIDECHAR_LENGTH}
  706. {****************************************************************************
  707. Caller/StackFrame Helpers
  708. ****************************************************************************}
  709. {$ifndef FPC_SYSTEM_HAS_GET_FRAME}
  710. {_$error Get_frame must be defined for each processor }
  711. {$endif ndef FPC_SYSTEM_HAS_GET_FRAME}
  712. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  713. {_$error Get_caller_addr must be defined for each processor }
  714. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  715. {$ifndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  716. {_$error Get_caller_frame must be defined for each processor }
  717. {$endif ndef FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  718. {****************************************************************************
  719. Math
  720. ****************************************************************************}
  721. {****************************************************************************
  722. Software longint/dword division
  723. ****************************************************************************}
  724. {$ifdef FPC_INCLUDE_SOFTWARE_MOD_DIV}
  725. function count_leading_zeros_32bit(l : longint) : longint;
  726. var
  727. i : longint;
  728. begin
  729. for i:=0 to 31 do
  730. begin
  731. if (l and (longint($80000000) shr i))<>0 then
  732. begin
  733. result:=i;
  734. exit;
  735. end;
  736. end;
  737. result:=i;
  738. end;
  739. {$ifndef FPC_SYSTEM_HAS_DIV_DWORD}
  740. function fpc_div_dword(n,z : dword) : dword; [public,alias: 'FPC_DIV_DWORD']; compilerproc;
  741. var
  742. shift,lzz,lzn : longint;
  743. begin
  744. result:=0;
  745. if n=0 then
  746. HandleErrorFrame(200,get_frame);
  747. lzz:=count_leading_zeros_32bit(z);
  748. lzn:=count_leading_zeros_32bit(n);
  749. { if the denominator contains less zeros
  750. then the numerator
  751. the d is greater than the n }
  752. if lzn<lzz then
  753. exit;
  754. shift:=lzn-lzz;
  755. n:=n shl shift;
  756. repeat
  757. if z>=n then
  758. begin
  759. z:=z-n;
  760. result:=result+dword(1 shl shift);
  761. end;
  762. dec(shift);
  763. n:=n shr 1;
  764. until shift<0;
  765. end;
  766. {$endif FPC_SYSTEM_HAS_DIV_DWORD}
  767. {$ifndef FPC_SYSTEM_HAS_MOD_DWORD}
  768. function fpc_mod_dword(n,z : dword) : dword; [public,alias: 'FPC_MOD_DWORD']; compilerproc;
  769. var
  770. shift,lzz,lzn : longint;
  771. begin
  772. result:=0;
  773. if n=0 then
  774. HandleErrorFrame(200,get_frame);
  775. lzz:=count_leading_zeros_32bit(z);
  776. lzn:=count_leading_zeros_32bit(n);
  777. { if the denominator contains less zeros
  778. then the numerator
  779. the d is greater than the n }
  780. if lzn<lzz then
  781. begin
  782. result:=z;
  783. exit;
  784. end;
  785. shift:=lzn-lzz;
  786. n:=n shl shift;
  787. repeat
  788. if z>=n then
  789. z:=z-n;
  790. dec(shift);
  791. n:=n shr 1;
  792. until shift<0;
  793. result:=z;
  794. end;
  795. {$endif FPC_SYSTEM_HAS_MOD_DWORD}
  796. {$ifndef FPC_SYSTEM_HAS_DIV_LONGINT}
  797. function fpc_div_longint(n,z : longint) : longint; [public,alias: 'FPC_DIV_LONGINT']; compilerproc;
  798. var
  799. sign : boolean;
  800. d1,d2 : dword;
  801. begin
  802. if n=0 then
  803. HandleErrorFrame(200,get_frame);
  804. sign:=false;
  805. if z<0 then
  806. begin
  807. sign:=not(sign);
  808. d1:=dword(-z);
  809. end
  810. else
  811. d1:=z;
  812. if n<0 then
  813. begin
  814. sign:=not(sign);
  815. d2:=dword(-n);
  816. end
  817. else
  818. d2:=n;
  819. { the div is coded by the compiler as call to divdword }
  820. if sign then
  821. result:=-(d1 div d2)
  822. else
  823. result:=d1 div d2;
  824. end;
  825. {$endif FPC_SYSTEM_HAS_DIV_LONGINT}
  826. {$ifndef FPC_SYSTEM_HAS_MOD_LONGINT}
  827. function fpc_mod_longint(n,z : longint) : longint; [public,alias: 'FPC_MOD_LONGINT']; compilerproc;
  828. var
  829. signed : boolean;
  830. r,nq,zq : dword;
  831. begin
  832. if n=0 then
  833. HandleErrorFrame(200,get_frame);
  834. nq:=abs(n);
  835. if z<0 then
  836. begin
  837. zq:=dword(-z);
  838. signed:=true;
  839. end
  840. else
  841. begin
  842. zq:=z;
  843. signed:=false;
  844. end;
  845. r:=zq mod nq;
  846. if signed then
  847. result:=-longint(r)
  848. else
  849. result:=r;
  850. end;
  851. {$endif FPC_SYSTEM_HAS_MOD_LONGINT}
  852. {$endif FPC_INCLUDE_SOFTWARE_MOD_DIV}
  853. {****************************************************************************}
  854. {$ifndef FPC_SYSTEM_HAS_ABS_LONGINT}
  855. function abs(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  856. begin
  857. if l<0 then
  858. abs:=-l
  859. else
  860. abs:=l;
  861. end;
  862. {$endif not FPC_SYSTEM_HAS_ABS_LONGINT}
  863. {$ifndef FPC_SYSTEM_HAS_ODD_LONGINT}
  864. function odd(l:longint):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  865. begin
  866. odd:=boolean(l and 1);
  867. end;
  868. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGINT}
  869. {$ifndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  870. function odd(l:longword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  871. begin
  872. odd:=boolean(l and 1);
  873. end;
  874. {$endif ndef FPC_SYSTEM_HAS_ODD_LONGWORD}
  875. {$ifndef FPC_SYSTEM_HAS_ODD_INT64}
  876. function odd(l:int64):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  877. begin
  878. odd:=boolean(longint(l) and 1);
  879. end;
  880. {$endif ndef FPC_SYSTEM_HAS_ODD_INT64}
  881. {$ifndef FPC_SYSTEM_HAS_ODD_QWORD}
  882. function odd(l:qword):boolean;{$ifdef SYSTEMINLINE}inline;{$endif}
  883. begin
  884. odd:=boolean(longint(l) and 1);
  885. end;
  886. {$endif ndef FPC_SYSTEM_HAS_ODD_QWORD}
  887. {$ifndef FPC_SYSTEM_HAS_SQR_LONGINT}
  888. function sqr(l:longint):longint;{$ifdef SYSTEMINLINE}inline;{$endif}
  889. begin
  890. sqr:=l*l;
  891. end;
  892. {$endif ndef FPC_SYSTEM_HAS_SQR_LONGINT}
  893. {$ifndef FPC_SYSTEM_HAS_ABS_INT64}
  894. function abs(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  895. begin
  896. if l < 0 then
  897. abs := -l
  898. else
  899. abs := l;
  900. end;
  901. {$endif ndef FPC_SYSTEM_HAS_ABS_INT64}
  902. {$ifndef FPC_SYSTEM_HAS_SQR_INT64}
  903. function sqr(l: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  904. begin
  905. sqr := l*l;
  906. end;
  907. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  908. {$ifndef FPC_SYSTEM_HAS_SQR_QWORD}
  909. function sqr(l: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  910. begin
  911. sqr := l*l;
  912. end;
  913. {$endif ndef FPC_SYSTEM_HAS_SQR_INT64}
  914. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  915. function declocked(var l:longint):boolean;
  916. begin
  917. Dec(l);
  918. declocked:=(l=0);
  919. end;
  920. {$endif FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  921. {$ifndef FPC_SYSTEM_HAS_DECLOCKED_INT64}
  922. function declocked(var l:int64):boolean;
  923. begin
  924. Dec(l);
  925. declocked:=(l=0);
  926. end;
  927. {$endif FPC_SYSTEM_HAS_DECLOCKED_INT64}
  928. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  929. procedure inclocked(var l:longint);
  930. begin
  931. Inc(l);
  932. end;
  933. {$endif FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  934. {$ifndef FPC_SYSTEM_HAS_INCLOCKED_INT64}
  935. procedure inclocked(var l:int64);
  936. begin
  937. Inc(l);
  938. end;
  939. {$endif FPC_SYSTEM_HAS_INCLOCKED_INT64}
  940. {$ifndef FPC_SYSTEM_HAS_SPTR}
  941. {_$error Sptr must be defined for each processor }
  942. {$endif ndef FPC_SYSTEM_HAS_SPTR}
  943. function align(addr : PtrUInt;alignment : PtrUInt) : PtrUInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  944. begin
  945. if addr mod alignment<>0 then
  946. result:=addr+(alignment-(addr mod alignment))
  947. else
  948. result:=addr;
  949. end;
  950. function align(addr : Pointer;alignment : PtrUInt) : Pointer;{$ifdef SYSTEMINLINE}inline;{$endif}
  951. begin
  952. if PtrUInt(addr) mod alignment<>0 then
  953. result:=pointer(addr+(alignment-(PtrUInt(addr) mod alignment)))
  954. else
  955. result:=addr;
  956. end;
  957. {****************************************************************************
  958. Str()
  959. ****************************************************************************}
  960. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  961. procedure int_str(l:longint;out s:string);
  962. var
  963. m,m1 : longword;
  964. pc,pc2 : pchar;
  965. hs : string[32];
  966. begin
  967. pc2:=@s[1];
  968. if (l<0) then
  969. begin
  970. pc2^:='-';
  971. inc(pc2);
  972. m:=longword(-l);
  973. end
  974. else
  975. m:=longword(l);
  976. pc:=@hs[0];
  977. repeat
  978. inc(pc);
  979. m1:=m div 10;
  980. pc^:=char(m-(m1*10)+byte('0'));
  981. m:=m1;
  982. until m=0;
  983. while (pc>pchar(@hs[0])) do
  984. begin
  985. pc2^:=pc^;
  986. dec(pc);
  987. inc(pc2);
  988. end;
  989. s[0]:=char(pc2-pchar(@s[1]));
  990. end;
  991. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGINT}
  992. {$ifndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  993. procedure int_str(l:longword;out s:string);
  994. var
  995. m1 : longword;
  996. pc,pc2 : pchar;
  997. hs : string[32];
  998. begin
  999. pc2:=@s[1];
  1000. pc:=@hs[0];
  1001. repeat
  1002. inc(pc);
  1003. m1:=l div 10;
  1004. pc^:=char(l-(m1*10)+byte('0'));
  1005. l:=m1;
  1006. until l=0;
  1007. while (pc>pchar(@hs[0])) do
  1008. begin
  1009. pc2^:=pc^;
  1010. dec(pc);
  1011. inc(pc2);
  1012. end;
  1013. s[0]:=char(pc2-pchar(@s[1]));
  1014. end;
  1015. {$endif ndef FPC_SYSTEM_HAS_INT_STR_LONGWORD}
  1016. {$ifndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1017. procedure int_str(l:int64;out s:string);
  1018. var
  1019. m,m1 : qword;
  1020. pc,pc2 : pchar;
  1021. hs : string[64];
  1022. begin
  1023. pc2:=@s[1];
  1024. if (l<0) then
  1025. begin
  1026. pc2^:='-';
  1027. inc(pc2);
  1028. m:=qword(-l);
  1029. end
  1030. else
  1031. m:=qword(l);
  1032. pc:=@hs[0];
  1033. repeat
  1034. inc(pc);
  1035. m1:=m div 10;
  1036. pc^:=char(m-(m1*10)+byte('0'));
  1037. m:=m1;
  1038. until m=0;
  1039. while (pc>pchar(@hs[0])) do
  1040. begin
  1041. pc2^:=pc^;
  1042. dec(pc);
  1043. inc(pc2);
  1044. end;
  1045. s[0]:=char(pc2-pchar(@s[1]));
  1046. end;
  1047. {$endif ndef FPC_SYSTEM_HAS_INT_STR_INT64}
  1048. {$ifndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1049. procedure int_str(l:qword;out s:string);
  1050. var
  1051. m1 : qword;
  1052. pc,pc2 : pchar;
  1053. hs : string[64];
  1054. begin
  1055. pc2:=@s[1];
  1056. pc:=@hs[0];
  1057. repeat
  1058. inc(pc);
  1059. m1:=l div 10;
  1060. pc^:=char(l-(m1*10)+byte('0'));
  1061. l:=m1;
  1062. until l=0;
  1063. while (pc>pchar(@hs[0])) do
  1064. begin
  1065. pc2^:=pc^;
  1066. dec(pc);
  1067. inc(pc2);
  1068. end;
  1069. s[0]:=char(pc2-pchar(@s[1]));
  1070. end;
  1071. {$endif ndef FPC_SYSTEM_HAS_INT_STR_QWORD}
  1072. {$ifndef FPC_SYSTEM_HAS_SYSRESETFPU}
  1073. procedure SysResetFpu;{$ifdef SYSTEMINLINE}inline;{$endif}
  1074. begin
  1075. { nothing todo }
  1076. end;
  1077. {$endif FPC_SYSTEM_HAS_SYSRESETFPU}
  1078. {$ifndef FPC_SYSTEM_HAS_SWAPENDIAN}
  1079. function SwapEndian(const AValue: SmallInt): SmallInt;
  1080. begin
  1081. Result := (AValue shr 8) or (AValue shl 8);
  1082. end;
  1083. function SwapEndian(const AValue: Word): Word;
  1084. begin
  1085. Result := (AValue shr 8) or (AValue shl 8);
  1086. end;
  1087. function SwapEndian(const AValue: LongInt): LongInt;
  1088. begin
  1089. Result := (AValue shl 24)
  1090. or ((AValue and $0000FF00) shl 8)
  1091. or ((AValue and $00FF0000) shr 8)
  1092. or (AValue shr 24);
  1093. end;
  1094. function SwapEndian(const AValue: DWord): DWord;
  1095. begin
  1096. Result := (AValue shl 24)
  1097. or ((AValue and $0000FF00) shl 8)
  1098. or ((AValue and $00FF0000) shr 8)
  1099. or (AValue shr 24);
  1100. end;
  1101. function SwapEndian(const AValue: Int64): Int64;
  1102. begin
  1103. Result := (AValue shl 56)
  1104. or ((AValue and $000000000000FF00) shl 40)
  1105. or ((AValue and $0000000000FF0000) shl 24)
  1106. or ((AValue and $00000000FF000000) shl 8)
  1107. or ((AValue and $000000FF00000000) shr 8)
  1108. or ((AValue and $0000FF0000000000) shr 24)
  1109. or ((AValue and $00FF000000000000) shr 40)
  1110. or (AValue shr 56);
  1111. end;
  1112. function SwapEndian(const AValue: QWord): QWord;
  1113. begin
  1114. Result := (AValue shl 56)
  1115. or ((AValue and $000000000000FF00) shl 40)
  1116. or ((AValue and $0000000000FF0000) shl 24)
  1117. or ((AValue and $00000000FF000000) shl 8)
  1118. or ((AValue and $000000FF00000000) shr 8)
  1119. or ((AValue and $0000FF0000000000) shr 24)
  1120. or ((AValue and $00FF000000000000) shr 40)
  1121. or (AValue shr 56);
  1122. end;
  1123. {$endif FPC_SYSTEM_HAS_SWAPENDIAN}
  1124. function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1125. begin
  1126. {$IFDEF ENDIAN_BIG}
  1127. Result := AValue;
  1128. {$ELSE}
  1129. Result := SwapEndian(AValue);
  1130. {$ENDIF}
  1131. end;
  1132. function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1133. begin
  1134. {$IFDEF ENDIAN_BIG}
  1135. Result := AValue;
  1136. {$ELSE}
  1137. Result := SwapEndian(AValue);
  1138. {$ENDIF}
  1139. end;
  1140. function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1141. begin
  1142. {$IFDEF ENDIAN_BIG}
  1143. Result := AValue;
  1144. {$ELSE}
  1145. Result := SwapEndian(AValue);
  1146. {$ENDIF}
  1147. end;
  1148. function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1149. begin
  1150. {$IFDEF ENDIAN_BIG}
  1151. Result := AValue;
  1152. {$ELSE}
  1153. Result := SwapEndian(AValue);
  1154. {$ENDIF}
  1155. end;
  1156. function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1157. begin
  1158. {$IFDEF ENDIAN_BIG}
  1159. Result := AValue;
  1160. {$ELSE}
  1161. Result := SwapEndian(AValue);
  1162. {$ENDIF}
  1163. end;
  1164. function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1165. begin
  1166. {$IFDEF ENDIAN_BIG}
  1167. Result := AValue;
  1168. {$ELSE}
  1169. Result := SwapEndian(AValue);
  1170. {$ENDIF}
  1171. end;
  1172. function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1173. begin
  1174. {$IFDEF ENDIAN_LITTLE}
  1175. Result := AValue;
  1176. {$ELSE}
  1177. Result := SwapEndian(AValue);
  1178. {$ENDIF}
  1179. end;
  1180. function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1181. begin
  1182. {$IFDEF ENDIAN_LITTLE}
  1183. Result := AValue;
  1184. {$ELSE}
  1185. Result := SwapEndian(AValue);
  1186. {$ENDIF}
  1187. end;
  1188. function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1189. begin
  1190. {$IFDEF ENDIAN_LITTLE}
  1191. Result := AValue;
  1192. {$ELSE}
  1193. Result := SwapEndian(AValue);
  1194. {$ENDIF}
  1195. end;
  1196. function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1197. begin
  1198. {$IFDEF ENDIAN_LITTLE}
  1199. Result := AValue;
  1200. {$ELSE}
  1201. Result := SwapEndian(AValue);
  1202. {$ENDIF}
  1203. end;
  1204. function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1205. begin
  1206. {$IFDEF ENDIAN_LITTLE}
  1207. Result := AValue;
  1208. {$ELSE}
  1209. Result := SwapEndian(AValue);
  1210. {$ENDIF}
  1211. end;
  1212. function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1213. begin
  1214. {$IFDEF ENDIAN_LITTLE}
  1215. Result := AValue;
  1216. {$ELSE}
  1217. Result := SwapEndian(AValue);
  1218. {$ENDIF}
  1219. end;
  1220. function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1221. begin
  1222. {$IFDEF ENDIAN_BIG}
  1223. Result := AValue;
  1224. {$ELSE}
  1225. Result := SwapEndian(AValue);
  1226. {$ENDIF}
  1227. end;
  1228. function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1229. begin
  1230. {$IFDEF ENDIAN_BIG}
  1231. Result := AValue;
  1232. {$ELSE}
  1233. Result := SwapEndian(AValue);
  1234. {$ENDIF}
  1235. end;
  1236. function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1237. begin
  1238. {$IFDEF ENDIAN_BIG}
  1239. Result := AValue;
  1240. {$ELSE}
  1241. Result := SwapEndian(AValue);
  1242. {$ENDIF}
  1243. end;
  1244. function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1245. begin
  1246. {$IFDEF ENDIAN_BIG}
  1247. Result := AValue;
  1248. {$ELSE}
  1249. Result := SwapEndian(AValue);
  1250. {$ENDIF}
  1251. end;
  1252. function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1253. begin
  1254. {$IFDEF ENDIAN_BIG}
  1255. Result := AValue;
  1256. {$ELSE}
  1257. Result := SwapEndian(AValue);
  1258. {$ENDIF}
  1259. end;
  1260. function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1261. begin
  1262. {$IFDEF ENDIAN_BIG}
  1263. Result := AValue;
  1264. {$ELSE}
  1265. Result := SwapEndian(AValue);
  1266. {$ENDIF}
  1267. end;
  1268. function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1269. begin
  1270. {$IFDEF ENDIAN_LITTLE}
  1271. Result := AValue;
  1272. {$ELSE}
  1273. Result := SwapEndian(AValue);
  1274. {$ENDIF}
  1275. end;
  1276. function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif}
  1277. begin
  1278. {$IFDEF ENDIAN_LITTLE}
  1279. Result := AValue;
  1280. {$ELSE}
  1281. Result := SwapEndian(AValue);
  1282. {$ENDIF}
  1283. end;
  1284. function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif}
  1285. begin
  1286. {$IFDEF ENDIAN_LITTLE}
  1287. Result := AValue;
  1288. {$ELSE}
  1289. Result := SwapEndian(AValue);
  1290. {$ENDIF}
  1291. end;
  1292. function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1293. begin
  1294. {$IFDEF ENDIAN_LITTLE}
  1295. Result := AValue;
  1296. {$ELSE}
  1297. Result := SwapEndian(AValue);
  1298. {$ENDIF}
  1299. end;
  1300. function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif}
  1301. begin
  1302. {$IFDEF ENDIAN_LITTLE}
  1303. Result := AValue;
  1304. {$ELSE}
  1305. Result := SwapEndian(AValue);
  1306. {$ENDIF}
  1307. end;
  1308. function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif}
  1309. begin
  1310. {$IFDEF ENDIAN_LITTLE}
  1311. Result := AValue;
  1312. {$ELSE}
  1313. Result := SwapEndian(AValue);
  1314. {$ENDIF}
  1315. end;