generic.inc 36 KB

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