cutils.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# This unit contains some generic support functions which are used
  18. in the different parts of the compiler.
  19. }
  20. unit cutils;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. constexp;
  25. type
  26. Tcharset=set of char;
  27. var
  28. internalerrorproc : procedure(i:longint);
  29. {# Returns the minimal value between @var(a) and @var(b) }
  30. function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  31. function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  32. function min(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  33. {# Returns the maximum value between @var(a) and @var(b) }
  34. function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  35. function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  36. function max(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  37. { These functions are intenionally put here and not in the constexp unit.
  38. Since Tconstexprint may be automatically converted to int, which causes
  39. loss of data and since there are already min and max functions for ints in
  40. this unit, we put min and max for Tconstexprint as well. This way we avoid
  41. potential bugs, caused by code unintentionally calling the int versions of
  42. min/max on Tconstexprint, because of only including cutils and forgetting
  43. the constexp unit in the uses clause. }
  44. function min(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  45. function max(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  46. {# Return value @var(i) aligned on @var(a) boundary }
  47. function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
  48. function align(i,a:int64):int64;{$ifdef USEINLINE}inline;{$endif}
  49. function align(i,a:qword):qword;{$ifdef USEINLINE}inline;{$endif}
  50. { if you have an address aligned using "oldalignment" and add an
  51. offset of (a multiple of) offset to it, this function calculates
  52. the new minimally guaranteed alignment
  53. }
  54. function newalignment(oldalignment: longint; offset: int64): longint;
  55. {# Return @var(b) with the bit order reversed }
  56. function reverse_byte(b: byte): byte;
  57. {# Return @var(w) with the bit order reversed }
  58. function reverse_word(w: word): word;
  59. {# Return @var(l) with the bit order reversed }
  60. function reverse_longword(l: longword): longword;
  61. function next_prime(l: longint): longint;
  62. function used_align(varalign,minalign,maxalign:longint):longint;
  63. function isbetteralignedthan(new, org, limit: cardinal): boolean;
  64. function packedbitsloadsize(bitlen: int64) : int64;
  65. procedure Replace(var s:string;s1:string;const s2:string);
  66. procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
  67. procedure ReplaceCase(var s:string;const s1,s2:string);
  68. procedure ReplaceCase(var s:ansistring;const s1,s2:ansistring);
  69. Function MatchPattern(const pattern,what:string):boolean;
  70. function upper(const c : char) : char;
  71. function upper(const s : string) : string;
  72. function upper(const s : ansistring) : ansistring;
  73. function lower(const c : char) : char;
  74. function lower(const s : string) : string;
  75. function lower(const s : ansistring) : ansistring;
  76. function rpos(const needle: char; const haystack: shortstring): longint; overload;
  77. function rpos(const needle: shortstring; const haystack: shortstring): longint; overload;
  78. function trimspace(const s:string):string;
  79. function trimspace(const s:AnsiString):AnsiString;
  80. function space (b : longint): string;
  81. { returns the position of the first char of the set cs in s, if there is none, then it returns 0 }
  82. function PosCharset(const cs : TCharSet;const s : ansistring) : integer;
  83. function PadSpace(const s:string;len:longint):string;
  84. function PadSpace(const s:AnsiString;len:longint):AnsiString;
  85. function GetToken(var s:string;endchar:char):string;
  86. procedure uppervar(var s : string);
  87. function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
  88. function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  89. function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  90. function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  91. function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
  92. function DStr(l:longint):string;
  93. {# Returns true if the string s is a number }
  94. function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
  95. {# Returns true if value is a power of 2, the actual
  96. exponent value is returned in power.
  97. }
  98. function ispowerof2(value : int64;out power : longint) : boolean;
  99. function ispowerof2(const value : Tconstexprint;out power : longint) : boolean;
  100. {# Returns true if abs(value) is a power of 2, the actual
  101. exponent value is returned in power.
  102. }
  103. function isabspowerof2(const value : Tconstexprint; out power : longint) : boolean;
  104. { # Returns the power of 2 >= value }
  105. function nextpowerof2(value : qword; out power: longint) : qword;
  106. function backspace_quote(const s:string;const qchars:Tcharset):string;
  107. function octal_quote(const s:string;const qchars:Tcharset):string;
  108. {# If the string is quoted, in accordance with pascal, it is
  109. dequoted and returned in s, and the function returns true.
  110. If it is not quoted, or if the quoting is bad, s is not touched,
  111. and false is returned.
  112. }
  113. function DePascalQuote(var s: ansistring): Boolean;
  114. function CompareStr(const S1, S2: string): Integer;
  115. function CompareText(S1, S2: string): integer;
  116. function CompareVersionStrings(s1,s2: string): longint;
  117. { releases the string p and assignes nil to p }
  118. { if p=nil then freemem isn't called }
  119. procedure stringdispose(var p : pshortstring);{$ifdef USEINLINE}inline;{$endif}
  120. { allocates mem for a copy of s, copies s to this mem and returns }
  121. { a pointer to this mem }
  122. function stringdup(const s : shortstring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  123. function stringdup(const s : ansistring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  124. {# Allocates memory for the string @var(s) and copies s as zero
  125. terminated string to that allocated memory and returns a pointer
  126. to that mem
  127. }
  128. function strpnew(const s : string) : pchar;
  129. function strpnew(const s : ansistring) : pchar;
  130. {# makes the character @var(c) lowercase, with spanish, french and german
  131. character set
  132. }
  133. function lowercase(c : char) : char;
  134. { makes zero terminated string to a pascal string }
  135. { the data in p is modified and p is returned }
  136. function pchar2pshortstring(p : pchar) : pshortstring;
  137. { inverse of pchar2pshortstring }
  138. function pshortstring2pchar(p : pshortstring) : pchar;
  139. { allocate a new pchar with the contents of a}
  140. function ansistring2pchar(const a: ansistring) : pchar;
  141. { Ansistring (pchar+length) support }
  142. procedure ansistringdispose(var p : pchar;length : longint);
  143. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  144. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  145. {Lzw encode/decode to compress strings -> save memory.}
  146. function minilzw_encode(const s:string):string;
  147. function minilzw_decode(const s:string):string;
  148. Function nextafter(x,y:double):double;
  149. function LengthUleb128(a: qword) : byte;
  150. function LengthSleb128(a: int64) : byte;
  151. function EncodeUleb128(a: qword;out buf;len: byte) : byte;
  152. function EncodeSleb128(a: int64;out buf;len: byte) : byte;
  153. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  154. const
  155. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  156. implementation
  157. uses
  158. SysUtils;
  159. var
  160. uppertbl,
  161. lowertbl : array[char] of char;
  162. function min(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  163. {
  164. return the minimal of a and b
  165. }
  166. begin
  167. if a<=b then
  168. min:=a
  169. else
  170. min:=b;
  171. end;
  172. function min(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  173. {
  174. return the minimal of a and b
  175. }
  176. begin
  177. if a<=b then
  178. min:=a
  179. else
  180. min:=b;
  181. end;
  182. function min(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  183. {
  184. return the minimal of a and b
  185. }
  186. begin
  187. if a<=b then
  188. min:=a
  189. else
  190. min:=b;
  191. end;
  192. function min(a,b : qword) : qword;
  193. {
  194. return the minimal of a and b
  195. }
  196. begin
  197. if a<=b then
  198. min:=a
  199. else
  200. min:=b;
  201. end;
  202. function max(a,b : longint) : longint;{$ifdef USEINLINE}inline;{$endif}
  203. {
  204. return the maximum of a and b
  205. }
  206. begin
  207. if a>=b then
  208. max:=a
  209. else
  210. max:=b;
  211. end;
  212. function max(a,b : int64) : int64;{$ifdef USEINLINE}inline;{$endif}
  213. {
  214. return the maximum of a and b
  215. }
  216. begin
  217. if a>=b then
  218. max:=a
  219. else
  220. max:=b;
  221. end;
  222. function max(a,b : qword) : qword;{$ifdef USEINLINE}inline;{$endif}
  223. {
  224. return the maximum of a and b
  225. }
  226. begin
  227. if a>=b then
  228. max:=a
  229. else
  230. max:=b;
  231. end;
  232. function max(const a,b : Tconstexprint) : Tconstexprint;{$ifdef USEINLINE}inline;{$endif}
  233. {
  234. return the maximum of a and b
  235. }
  236. begin
  237. if a>=b then
  238. max:=a
  239. else
  240. max:=b;
  241. end;
  242. function newalignment(oldalignment: longint; offset: int64): longint;
  243. begin
  244. { oldalignment must be power of two.
  245. Negating two's complement number keeps its tail '100...000' and complements all bits above.
  246. "x and -x" extracts this tail of 'x'.
  247. Said tail of "oldalignment or offset" is the desired answer. }
  248. result:=oldalignment or longint(offset); { high part of offset won't matter as long as alignment is 32-bit }
  249. result:=result and -result;
  250. end;
  251. function reverse_byte(b: byte): byte;
  252. const
  253. reverse_nible:array[0..15] of 0..15 =
  254. (%0000,%1000,%0100,%1100,%0010,%1010,%0110,%1110,
  255. %0001,%1001,%0101,%1101,%0011,%1011,%0111,%1111);
  256. begin
  257. reverse_byte:=(reverse_nible[b and $f] shl 4) or reverse_nible[b shr 4];
  258. end;
  259. function reverse_word(w: word): word;
  260. type
  261. TWordRec = packed record
  262. hi, lo: Byte;
  263. end;
  264. begin
  265. TWordRec(reverse_word).hi := reverse_byte(TWordRec(w).lo);
  266. TWordRec(reverse_word).lo := reverse_byte(TWordRec(w).hi);
  267. end;
  268. function reverse_longword(l: longword): longword;
  269. type
  270. TLongWordRec = packed record
  271. b: array[0..3] of Byte;
  272. end;
  273. begin
  274. TLongWordRec(reverse_longword).b[0] := reverse_byte(TLongWordRec(l).b[3]);
  275. TLongWordRec(reverse_longword).b[1] := reverse_byte(TLongWordRec(l).b[2]);
  276. TLongWordRec(reverse_longword).b[2] := reverse_byte(TLongWordRec(l).b[1]);
  277. TLongWordRec(reverse_longword).b[3] := reverse_byte(TLongWordRec(l).b[0]);
  278. end;
  279. function align(i,a:longint):longint;{$ifdef USEINLINE}inline;{$endif}
  280. {
  281. return value <i> aligned <a> boundary. <a> must be power of two.
  282. }
  283. begin
  284. { One-line formula for i >= 0 is
  285. >>> (i + a - 1) and not (a - 1),
  286. and for i < 0 is
  287. >>> i and not (a - 1). }
  288. if a>0 then
  289. a:=a-1; { 'a' is decremented beforehand, this also allows a=0 as a synonym for a=1. }
  290. if i>=0 then
  291. i:=i+a;
  292. result:=i and not a;
  293. end;
  294. function align(i,a:int64):int64;{$ifdef USEINLINE}inline;{$endif}
  295. {
  296. return value <i> aligned <a> boundary. <a> must be power of two.
  297. }
  298. begin
  299. { Copy of 'longint' version. }
  300. if a>0 then
  301. a:=a-1;
  302. if i>=0 then
  303. i:=i+a;
  304. result:=i and not a;
  305. end;
  306. function align(i,a:qword):qword;{$ifdef USEINLINE}inline;{$endif}
  307. {
  308. return value <i> aligned <a> boundary. <a> must be power of two.
  309. }
  310. begin
  311. { No i < 0 case here. }
  312. if a>0 then
  313. a:=a-1;
  314. result:=(i+a) and not a;
  315. end;
  316. function packedbitsloadsize(bitlen: int64) : int64;
  317. begin
  318. case bitlen of
  319. 1,2,4,8:
  320. result := 1;
  321. { 10 bits can never be split over 3 bytes via 1-8-1, because it }
  322. { always starts at a multiple of 10 bits. Same for the others. }
  323. 3,5,6,7,9,10,12,16:
  324. result := 2;
  325. {$ifdef cpu64bitalu}
  326. { performance penalty for unaligned 8 byte access is much }
  327. { higher than for unaligned 4 byte access, at least on ppc, }
  328. { so use 4 bytes even in some cases where a value could }
  329. { always loaded using a single 8 byte load (e.g. in case of }
  330. { 28 bit values) }
  331. 11,13,14,15,17..32:
  332. result := 4;
  333. else
  334. result := 8;
  335. {$else cpu64bitalu}
  336. else
  337. result := 4;
  338. {$endif cpu64bitalu}
  339. end;
  340. end;
  341. function isbetteralignedthan(new, org, limit: cardinal): boolean;
  342. var
  343. cnt: cardinal;
  344. begin
  345. cnt:=2;
  346. while (cnt <= limit) do
  347. begin
  348. if (org and (cnt-1)) > (new and (cnt-1)) then
  349. begin
  350. result:=true;
  351. exit;
  352. end
  353. else if (org and (cnt-1)) < (new and (cnt-1)) then
  354. begin
  355. result:=false;
  356. exit;
  357. end;
  358. cnt:=cnt*2;
  359. end;
  360. result:=false;
  361. end;
  362. function next_prime(l: longint): longint;
  363. var
  364. check, checkbound: longint;
  365. ok: boolean;
  366. begin
  367. result:=l or 1;
  368. while l<high(longint) do
  369. begin
  370. ok:=true;
  371. checkbound:=trunc(sqrt(l));
  372. check:=3;
  373. while check<checkbound do
  374. begin
  375. if (l mod check) = 0 then
  376. begin
  377. ok:=false;
  378. break;
  379. end;
  380. inc(check,2);
  381. end;
  382. if ok then
  383. exit;
  384. inc(l);
  385. end;
  386. end;
  387. function used_align(varalign,minalign,maxalign:longint):longint;
  388. begin
  389. { varalign : minimum alignment required for the variable
  390. minalign : Minimum alignment of this structure, 0 = undefined
  391. maxalign : Maximum alignment of this structure, 0 = undefined }
  392. if (minalign>0) and
  393. (varalign<minalign) then
  394. used_align:=minalign
  395. else
  396. begin
  397. if (maxalign>0) and
  398. (varalign>maxalign) then
  399. used_align:=maxalign
  400. else
  401. used_align:=varalign;
  402. end;
  403. end;
  404. procedure Replace(var s:string;s1:string;const s2:string);
  405. var
  406. last,
  407. i : longint;
  408. begin
  409. s1:=upper(s1);
  410. last:=0;
  411. repeat
  412. i:=pos(s1,upper(s));
  413. if i=last then
  414. i:=0;
  415. if (i>0) then
  416. begin
  417. Delete(s,i,length(s1));
  418. Insert(s2,s,i);
  419. last:=i;
  420. end;
  421. until (i=0);
  422. end;
  423. procedure Replace(var s:AnsiString;s1:string;const s2:AnsiString);
  424. var
  425. last,
  426. i : longint;
  427. begin
  428. s1:=upper(s1);
  429. last:=0;
  430. repeat
  431. i:=pos(s1,upper(s));
  432. if i=last then
  433. i:=0;
  434. if (i>0) then
  435. begin
  436. Delete(s,i,length(s1));
  437. Insert(s2,s,i);
  438. last:=i;
  439. end;
  440. until (i=0);
  441. end;
  442. procedure ReplaceCase(var s:string;const s1,s2:string);
  443. var
  444. last,
  445. i : longint;
  446. begin
  447. last:=0;
  448. repeat
  449. i:=pos(s1,s);
  450. if i=last then
  451. i:=0;
  452. if (i>0) then
  453. begin
  454. Delete(s,i,length(s1));
  455. Insert(s2,s,i);
  456. last:=i;
  457. end;
  458. until (i=0);
  459. end;
  460. procedure ReplaceCase(var s: ansistring; const s1, s2: ansistring);
  461. var
  462. last,
  463. i : longint;
  464. begin
  465. last:=0;
  466. repeat
  467. i:=pos(s1,s);
  468. if i=last then
  469. i:=0;
  470. if (i>0) then
  471. begin
  472. Delete(s,i,length(s1));
  473. Insert(s2,s,i);
  474. last:=i;
  475. end;
  476. until (i=0);
  477. end;
  478. Function MatchPattern(const pattern,what:string):boolean;
  479. var
  480. found : boolean;
  481. i1,i2 : longint;
  482. begin
  483. i1:=0;
  484. i2:=0;
  485. if pattern='' then
  486. begin
  487. result:=(what='');
  488. exit;
  489. end;
  490. found:=true;
  491. repeat
  492. inc(i1);
  493. if (i1>length(pattern)) then
  494. break;
  495. inc(i2);
  496. if (i2>length(what)) then
  497. break;
  498. case pattern[i1] of
  499. '?' :
  500. found:=true;
  501. '*' :
  502. begin
  503. found:=true;
  504. if (i1=length(pattern)) then
  505. i2:=length(what)
  506. else
  507. if (i1<length(pattern)) and (pattern[i1+1]<>what[i2]) then
  508. begin
  509. if i2<length(what) then
  510. dec(i1)
  511. end
  512. else
  513. if i2>1 then
  514. dec(i2);
  515. end;
  516. else
  517. found:=(pattern[i1]=what[i2]) or (what[i2]='?');
  518. end;
  519. until not found;
  520. if found then
  521. begin
  522. found:=(i2>=length(what)) and
  523. (
  524. (i1>length(pattern)) or
  525. ((i1=length(pattern)) and
  526. (pattern[i1]='*'))
  527. );
  528. end;
  529. result:=found;
  530. end;
  531. function upper(const c : char) : char;
  532. {
  533. return uppercase of c
  534. }
  535. begin
  536. upper:=uppertbl[c];
  537. end;
  538. function upper(const s : string) : string;
  539. {
  540. return uppercased string of s
  541. }
  542. var
  543. i : longint;
  544. begin
  545. for i:=1 to length(s) do
  546. upper[i]:=uppertbl[s[i]];
  547. upper[0]:=s[0];
  548. end;
  549. function upper(const s : ansistring) : ansistring;
  550. {
  551. return uppercased string of s
  552. }
  553. var
  554. i : longint;
  555. begin
  556. Result:='';
  557. setlength(upper,length(s));
  558. for i:=1 to length(s) do
  559. upper[i]:=uppertbl[s[i]];
  560. end;
  561. function lower(const c : char) : char;
  562. {
  563. return lowercase of c
  564. }
  565. begin
  566. lower:=lowertbl[c];
  567. end;
  568. function lower(const s : string) : string;
  569. {
  570. return lowercased string of s
  571. }
  572. var
  573. i : longint;
  574. begin
  575. for i:=1 to length(s) do
  576. lower[i]:=lowertbl[s[i]];
  577. lower[0]:=s[0];
  578. end;
  579. function lower(const s : ansistring) : ansistring;
  580. {
  581. return lowercased string of s
  582. }
  583. var
  584. i : longint;
  585. begin
  586. Result:='';
  587. setlength(lower,length(s));
  588. for i:=1 to length(s) do
  589. lower[i]:=lowertbl[s[i]];
  590. end;
  591. procedure uppervar(var s : string);
  592. {
  593. uppercase string s
  594. }
  595. var
  596. i : longint;
  597. begin
  598. for i:=1 to length(s) do
  599. s[i]:=uppertbl[s[i]];
  600. end;
  601. procedure initupperlower;
  602. var
  603. c : char;
  604. begin
  605. for c:=#0 to #255 do
  606. begin
  607. lowertbl[c]:=c;
  608. uppertbl[c]:=c;
  609. case c of
  610. 'A'..'Z' :
  611. lowertbl[c]:=char(byte(c)+32);
  612. 'a'..'z' :
  613. uppertbl[c]:=char(byte(c)-32);
  614. end;
  615. end;
  616. end;
  617. function DStr(l:longint):string;
  618. var
  619. TmpStr : string[32];
  620. i : longint;
  621. begin
  622. Str(l,TmpStr);
  623. i:=Length(TmpStr);
  624. while (i>3) do
  625. begin
  626. dec(i,3);
  627. if TmpStr[i]<>'-' then
  628. insert('.',TmpStr,i+1);
  629. end;
  630. DStr:=TmpStr;
  631. end;
  632. function rpos(const needle: char; const haystack: shortstring): longint;
  633. begin
  634. result:=length(haystack);
  635. while (result>0) do
  636. begin
  637. if haystack[result]=needle then
  638. exit;
  639. dec(result);
  640. end;
  641. end;
  642. function rpos(const needle: shortstring; const haystack: shortstring): longint;
  643. begin
  644. result:=0;
  645. if (length(needle)=0) or
  646. (length(needle)>length(haystack)) then
  647. exit;
  648. result:=length(haystack)-length(needle);
  649. repeat
  650. if (haystack[result]=needle[1]) and
  651. (copy(haystack,result,length(needle))=needle) then
  652. exit;
  653. dec(result);
  654. until result=0;
  655. end;
  656. function trimspace(const s:string):string;
  657. {
  658. return s with all leading and ending spaces and tabs removed
  659. }
  660. var
  661. i,j : longint;
  662. begin
  663. i:=length(s);
  664. while (i>0) and (s[i] in [#9,' ']) do
  665. dec(i);
  666. j:=1;
  667. while (j<i) and (s[j] in [#9,' ']) do
  668. inc(j);
  669. trimspace:=Copy(s,j,i-j+1);
  670. end;
  671. function trimspace(const s:AnsiString):AnsiString;
  672. {
  673. return s with all leading and ending spaces and tabs removed
  674. }
  675. var
  676. i,j : longint;
  677. begin
  678. i:=length(s);
  679. while (i>0) and (s[i] in [#9,' ']) do
  680. dec(i);
  681. j:=1;
  682. while (j<i) and (s[j] in [#9,' ']) do
  683. inc(j);
  684. trimspace:=Copy(s,j,i-j+1);
  685. end;
  686. function space (b : longint): string;
  687. var
  688. s: string;
  689. begin
  690. space[0] := chr(b);
  691. s[0] := chr(b);
  692. FillChar (S[1],b,' ');
  693. space:=s;
  694. end;
  695. function PadSpace(const s:string;len:longint):string;
  696. {
  697. return s with spaces add to the end
  698. }
  699. begin
  700. if length(s)<len then
  701. PadSpace:=s+Space(len-length(s))
  702. else
  703. PadSpace:=s;
  704. end;
  705. function PadSpace(const s:AnsiString;len:longint):AnsiString;
  706. {
  707. return s with spaces add to the end
  708. }
  709. begin
  710. if length(s)<len then
  711. PadSpace:=s+Space(len-length(s))
  712. else
  713. PadSpace:=s;
  714. end;
  715. function GetToken(var s:string;endchar:char):string;
  716. var
  717. i : longint;
  718. quote : char;
  719. begin
  720. GetToken:='';
  721. s:=TrimSpace(s);
  722. if (length(s)>0) and
  723. (s[1] in ['''','"']) then
  724. begin
  725. quote:=s[1];
  726. i:=1;
  727. while (i<length(s)) do
  728. begin
  729. inc(i);
  730. if s[i]=quote then
  731. begin
  732. { Remove double quote }
  733. if (i<length(s)) and
  734. (s[i+1]=quote) then
  735. begin
  736. Delete(s,i,1);
  737. inc(i);
  738. end
  739. else
  740. begin
  741. GetToken:=Copy(s,2,i-2);
  742. Delete(s,1,i);
  743. exit;
  744. end;
  745. end;
  746. end;
  747. GetToken:=s;
  748. s:='';
  749. end
  750. else
  751. begin
  752. i:=pos(EndChar,s);
  753. if i=0 then
  754. begin
  755. GetToken:=s;
  756. s:='';
  757. exit;
  758. end
  759. else
  760. begin
  761. GetToken:=Copy(s,1,i-1);
  762. Delete(s,1,i);
  763. exit;
  764. end;
  765. end;
  766. end;
  767. function realtostr(e:extended):string;{$ifdef USEINLINE}inline;{$endif}
  768. begin
  769. str(e,result);
  770. end;
  771. function tostr(i : qword) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  772. {
  773. return string of value i
  774. }
  775. begin
  776. str(i,result);
  777. end;
  778. function tostr(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  779. {
  780. return string of value i
  781. }
  782. begin
  783. str(i,result);
  784. end;
  785. function tostr(i : longint) : string;{$ifdef USEINLINE}inline;{$endif}overload;
  786. {
  787. return string of value i
  788. }
  789. begin
  790. str(i,result);
  791. end;
  792. function tostr_with_plus(i : int64) : string;{$ifdef USEINLINE}inline;{$endif}
  793. {
  794. return string of value i, but always include a + when i>=0
  795. }
  796. begin
  797. str(i,result);
  798. if i>=0 then
  799. result:='+'+result;
  800. end;
  801. function is_number(const s : string) : boolean;{$ifdef USEINLINE}inline;{$endif}
  802. {
  803. is string a correct number ?
  804. }
  805. var
  806. w : integer;
  807. l : longint;
  808. begin
  809. val(s,l,w);
  810. // remove warning
  811. l:=l;
  812. is_number:=(w=0);
  813. end;
  814. function ispowerof2(value : int64;out power : longint) : boolean;
  815. {
  816. return if value is a power of 2. And if correct return the power
  817. }
  818. begin
  819. if (value <= 0) or (value and (value - 1) <> 0) then
  820. exit(false);
  821. power:=BsfQWord(value);
  822. result:=true;
  823. end;
  824. function ispowerof2(const value: Tconstexprint; out power: longint): boolean;
  825. begin
  826. if value.signed or
  827. (value.uvalue<=high(int64)) then
  828. result:=ispowerof2(value.svalue,power)
  829. else if not value.signed and
  830. (value.svalue=low(int64)) then
  831. begin
  832. result:=true;
  833. power:=63;
  834. end
  835. else
  836. result:=false;
  837. end;
  838. function isabspowerof2(const value : Tconstexprint;out power : longint) : boolean;
  839. begin
  840. if ispowerof2(value,power) then
  841. result:=true
  842. else if value.signed and (value.svalue<0) and (value.svalue<>low(int64)) and ispowerof2(-value.svalue,power) then
  843. result:=true
  844. else
  845. result:=false;
  846. end;
  847. function nextpowerof2(value : qword; out power: longint) : qword;
  848. begin
  849. power:=-1;
  850. result:=0;
  851. if (value=0) or (value>qword($8000000000000000)) then
  852. exit;
  853. power:=BsrQWord(value);
  854. result:=qword(1) shl power;
  855. if (value and (value-1))<>0 then
  856. begin
  857. inc(power);
  858. result:=result shl 1;
  859. end;
  860. end;
  861. function backspace_quote(const s:string;const qchars:Tcharset):string;
  862. var i:byte;
  863. begin
  864. backspace_quote:='';
  865. for i:=1 to length(s) do
  866. begin
  867. if (s[i]=#10) and (#10 in qchars) then
  868. backspace_quote:=backspace_quote+'\n'
  869. else if (s[i]=#13) and (#13 in qchars) then
  870. backspace_quote:=backspace_quote+'\r'
  871. else
  872. begin
  873. if s[i] in qchars then
  874. backspace_quote:=backspace_quote+'\';
  875. backspace_quote:=backspace_quote+s[i];
  876. end;
  877. end;
  878. end;
  879. function octal_quote(const s:string;const qchars:Tcharset):string;
  880. var i:byte;
  881. begin
  882. octal_quote:='';
  883. for i:=1 to length(s) do
  884. begin
  885. if s[i] in qchars then
  886. begin
  887. if ord(s[i])<64 then
  888. octal_quote:=octal_quote+'\'+octstr(ord(s[i]),3)
  889. else
  890. octal_quote:=octal_quote+'\'+octstr(ord(s[i]),4);
  891. end
  892. else
  893. octal_quote:=octal_quote+s[i];
  894. end;
  895. end;
  896. function DePascalQuote(var s: ansistring): Boolean;
  897. var
  898. destPos, sourcePos, len: Integer;
  899. t: string;
  900. ch: Char;
  901. begin
  902. t:='';
  903. DePascalQuote:= false;
  904. len:= length(s);
  905. if (len >= 1) and (s[1] = '''') then
  906. begin
  907. {Remove quotes, exchange '' against ' }
  908. destPos := 0;
  909. sourcepos:=1;
  910. while (sourcepos<len) do
  911. begin
  912. inc(sourcePos);
  913. ch := s[sourcePos];
  914. if ch = '''' then
  915. begin
  916. inc(sourcePos);
  917. if (sourcePos <= len) and (s[sourcePos] = '''') then
  918. {Add the quote as part of string}
  919. else
  920. begin
  921. SetLength(t, destPos);
  922. s:= t;
  923. Exit(true);
  924. end;
  925. end;
  926. inc(destPos);
  927. t[destPos] := ch;
  928. end;
  929. end;
  930. end;
  931. function pchar2pshortstring(p : pchar) : pshortstring;
  932. var
  933. w,i : longint;
  934. begin
  935. w:=strlen(p);
  936. for i:=w-1 downto 0 do
  937. p[i+1]:=p[i];
  938. p[0]:=chr(w);
  939. pchar2pshortstring:=pshortstring(p);
  940. end;
  941. function pshortstring2pchar(p : pshortstring) : pchar;
  942. var
  943. w,i : longint;
  944. begin
  945. w:=length(p^);
  946. for i:=1 to w do
  947. p^[i-1]:=p^[i];
  948. p^[w]:=#0;
  949. pshortstring2pchar:=pchar(p);
  950. end;
  951. function ansistring2pchar(const a: ansistring) : pchar;
  952. var
  953. len: ptrint;
  954. begin
  955. len:=length(a);
  956. getmem(result,len+1);
  957. if (len<>0) then
  958. move(a[1],result[0],len);
  959. result[len]:=#0;
  960. end;
  961. function lowercase(c : char) : char;
  962. begin
  963. case c of
  964. #65..#90 : c := chr(ord (c) + 32);
  965. #154 : c:=#129; { german }
  966. #142 : c:=#132; { german }
  967. #153 : c:=#148; { german }
  968. #144 : c:=#130; { french }
  969. #128 : c:=#135; { french }
  970. #143 : c:=#134; { swedish/norge (?) }
  971. #165 : c:=#164; { spanish }
  972. #228 : c:=#229; { greek }
  973. #226 : c:=#231; { greek }
  974. #232 : c:=#227; { greek }
  975. end;
  976. lowercase := c;
  977. end;
  978. function strpnew(const s : string) : pchar;
  979. var
  980. p : pchar;
  981. begin
  982. getmem(p,length(s)+1);
  983. move(s[1],p^,length(s));
  984. p[length(s)]:=#0;
  985. result:=p;
  986. end;
  987. function strpnew(const s: ansistring): pchar;
  988. var
  989. p : pchar;
  990. begin
  991. getmem(p,length(s)+1);
  992. move(s[1],p^,length(s)+1);
  993. result:=p;
  994. end;
  995. procedure stringdispose(var p : pshortstring);{$ifdef USEINLINE}inline;{$endif}
  996. begin
  997. if assigned(p) then
  998. begin
  999. freemem(p);
  1000. p:=nil;
  1001. end;
  1002. end;
  1003. function stringdup(const s : shortstring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  1004. begin
  1005. getmem(result,length(s)+1);
  1006. result^:=s;
  1007. end;
  1008. function stringdup(const s : ansistring) : pshortstring;{$ifdef USEINLINE}inline;{$endif}
  1009. begin
  1010. getmem(result,length(s)+1);
  1011. result^:=s;
  1012. end;
  1013. function PosCharset(const cs : TCharSet;const s : ansistring) : integer;
  1014. var
  1015. i : integer;
  1016. begin
  1017. result:=0;
  1018. for i:=1 to length(s) do
  1019. if s[i] in cs then
  1020. begin
  1021. result:=i;
  1022. exit;
  1023. end;
  1024. end;
  1025. function CompareStr(const S1, S2: string): Integer;
  1026. var
  1027. count, count1, count2: integer;
  1028. begin
  1029. result := 0;
  1030. Count1 := Length(S1);
  1031. Count2 := Length(S2);
  1032. if Count1>Count2 then
  1033. Count:=Count2
  1034. else
  1035. Count:=Count1;
  1036. result := CompareChar(S1[1],S2[1], Count);
  1037. if result=0 then
  1038. result:=Count1-Count2;
  1039. end;
  1040. function CompareText(S1, S2: string): integer;
  1041. begin
  1042. UpperVar(S1);
  1043. UpperVar(S2);
  1044. Result:=CompareStr(S1,S2);
  1045. end;
  1046. function CompareVersionStrings(s1,s2: string): longint;
  1047. var
  1048. start1, start2,
  1049. i1, i2,
  1050. num1,num2,
  1051. res,
  1052. err : longint;
  1053. begin
  1054. i1:=1;
  1055. i2:=1;
  1056. repeat
  1057. start1:=i1;
  1058. start2:=i2;
  1059. while (i1<=length(s1)) and
  1060. (s1[i1] in ['0'..'9']) do
  1061. inc(i1);
  1062. while (i2<=length(s2)) and
  1063. (s2[i2] in ['0'..'9']) do
  1064. inc(i2);
  1065. { one of the strings misses digits -> other is the largest version }
  1066. if i1=start1 then
  1067. if i2=start2 then
  1068. exit(0)
  1069. else
  1070. exit(-1)
  1071. else if i2=start2 then
  1072. exit(1);
  1073. { get version number part }
  1074. val(copy(s1,start1,i1-start1),num1,err);
  1075. val(copy(s2,start2,i2-start2),num2,err);
  1076. { different -> done }
  1077. res:=num1-num2;
  1078. if res<>0 then
  1079. exit(res);
  1080. { if one of the two is at the end while the other isn't, add a '.0' }
  1081. if (i1>length(s1)) and
  1082. (i2<=length(s2)) then
  1083. s1:=s1+'.0';
  1084. if (i2>length(s2)) and
  1085. (i1<=length(s1)) then
  1086. s2:=s2+'.0';
  1087. { compare non-numerical characters normally }
  1088. while (i1<=length(s1)) and
  1089. not(s1[i1] in ['0'..'9']) and
  1090. (i2<=length(s2)) and
  1091. not(s2[i2] in ['0'..'9']) do
  1092. begin
  1093. res:=ord(s1[i1])-ord(s2[i2]);
  1094. if res<>0 then
  1095. exit(res);
  1096. inc(i1);
  1097. inc(i2);
  1098. end;
  1099. { both should be digits again now, otherwise pick the one with the
  1100. digits as the largest (it more likely means that the input was
  1101. ill-formatted though) }
  1102. if (i1<=length(s1)) and
  1103. not(s1[i1] in ['0'..'9']) then
  1104. exit(-1);
  1105. if (i2<=length(s2)) and
  1106. not(s2[i2] in ['0'..'9']) then
  1107. exit(1);
  1108. until false;
  1109. end;
  1110. {*****************************************************************************
  1111. Ansistring (PChar+Length)
  1112. *****************************************************************************}
  1113. procedure ansistringdispose(var p : pchar;length : longint);
  1114. begin
  1115. if assigned(p) then
  1116. begin
  1117. freemem(p);
  1118. p:=nil;
  1119. end;
  1120. end;
  1121. { enable ansistring comparison }
  1122. { 0 means equal }
  1123. { 1 means p1 > p2 }
  1124. { -1 means p1 < p2 }
  1125. function compareansistrings(p1,p2 : pchar;length1,length2 : longint) : longint;
  1126. var
  1127. i,j : longint;
  1128. begin
  1129. compareansistrings:=0;
  1130. j:=min(length1,length2);
  1131. i:=0;
  1132. while (i<j) do
  1133. begin
  1134. if p1[i]>p2[i] then
  1135. begin
  1136. compareansistrings:=1;
  1137. exit;
  1138. end
  1139. else
  1140. if p1[i]<p2[i] then
  1141. begin
  1142. compareansistrings:=-1;
  1143. exit;
  1144. end;
  1145. inc(i);
  1146. end;
  1147. if length1>length2 then
  1148. compareansistrings:=1
  1149. else
  1150. if length1<length2 then
  1151. compareansistrings:=-1;
  1152. end;
  1153. function concatansistrings(p1,p2 : pchar;length1,length2 : longint) : pchar;
  1154. var
  1155. p : pchar;
  1156. begin
  1157. getmem(p,length1+length2+1);
  1158. move(p1[0],p[0],length1);
  1159. move(p2[0],p[length1],length2+1);
  1160. concatansistrings:=p;
  1161. end;
  1162. {*****************************************************************************
  1163. Ultra basic KISS Lzw (de)compressor
  1164. *****************************************************************************}
  1165. {This is an extremely basic implementation of the Lzw algorithm. It
  1166. compresses 7-bit ASCII strings into 8-bit compressed strings.
  1167. The Lzw dictionary is preinitialized with 0..127, therefore this
  1168. part of the dictionary does not need to be stored in the arrays.
  1169. The Lzw code size is allways 8 bit, so we do not need complex code
  1170. that can write partial bytes.}
  1171. function minilzw_encode(const s:string):string;
  1172. var t,u,i:byte;
  1173. c:char;
  1174. data:array[128..255] of char;
  1175. previous:array[128..255] of byte;
  1176. lzwptr:byte;
  1177. next_avail:set of 0..255;
  1178. label l1;
  1179. begin
  1180. minilzw_encode:='';
  1181. fillchar(data,sizeof(data),#0);
  1182. fillchar(previous,sizeof(previous),#0);
  1183. if s<>'' then
  1184. begin
  1185. lzwptr:=127;
  1186. t:=byte(s[1]);
  1187. i:=2;
  1188. u:=128;
  1189. next_avail:=[];
  1190. while i<=length(s) do
  1191. begin
  1192. c:=s[i];
  1193. if not(t in next_avail) or (u>lzwptr) then goto l1;
  1194. while (previous[u]<>t) or (data[u]<>c) do
  1195. begin
  1196. inc(u);
  1197. if u>lzwptr then goto l1;
  1198. end;
  1199. t:=u;
  1200. inc(i);
  1201. continue;
  1202. l1:
  1203. {It's a pity that we still need those awfull tricks
  1204. with this modern compiler. Without this performance
  1205. of the entire procedure drops about 3 times.}
  1206. inc(minilzw_encode[0]);
  1207. minilzw_encode[length(minilzw_encode)]:=char(t);
  1208. if lzwptr=255 then
  1209. begin
  1210. lzwptr:=127;
  1211. next_avail:=[];
  1212. end
  1213. else
  1214. begin
  1215. inc(lzwptr);
  1216. data[lzwptr]:=c;
  1217. previous[lzwptr]:=t;
  1218. include(next_avail,t);
  1219. end;
  1220. t:=byte(c);
  1221. u:=128;
  1222. inc(i);
  1223. end;
  1224. inc(minilzw_encode[0]);
  1225. minilzw_encode[length(minilzw_encode)]:=char(t);
  1226. end;
  1227. end;
  1228. function minilzw_decode(const s:string):string;
  1229. var oldc,newc,c:char;
  1230. i,j:byte;
  1231. data:array[128..255] of char;
  1232. previous:array[128..255] of byte;
  1233. lzwptr:byte;
  1234. t:string;
  1235. begin
  1236. minilzw_decode:='';
  1237. fillchar(data,sizeof(data),#0);
  1238. fillchar(previous,sizeof(previous),#0);
  1239. if s<>'' then
  1240. begin
  1241. lzwptr:=127;
  1242. oldc:=s[1];
  1243. c:=oldc;
  1244. i:=2;
  1245. minilzw_decode:=oldc;
  1246. while i<=length(s) do
  1247. begin
  1248. newc:=s[i];
  1249. if byte(newc)>lzwptr then
  1250. begin
  1251. t:=c;
  1252. c:=oldc;
  1253. end
  1254. else
  1255. begin
  1256. c:=newc;
  1257. t:='';
  1258. end;
  1259. while c>=#128 do
  1260. begin
  1261. inc(t[0]);
  1262. t[length(t)]:=data[byte(c)];
  1263. byte(c):=previous[byte(c)];
  1264. end;
  1265. inc(minilzw_decode[0]);
  1266. minilzw_decode[length(minilzw_decode)]:=c;
  1267. for j:=length(t) downto 1 do
  1268. begin
  1269. inc(minilzw_decode[0]);
  1270. minilzw_decode[length(minilzw_decode)]:=t[j];
  1271. end;
  1272. if lzwptr=255 then
  1273. lzwptr:=127
  1274. else
  1275. begin
  1276. inc(lzwptr);
  1277. previous[lzwptr]:=byte(oldc);
  1278. data[lzwptr]:=c;
  1279. end;
  1280. oldc:=newc;
  1281. inc(i);
  1282. end;
  1283. end;
  1284. end;
  1285. procedure defaulterror(i:longint);
  1286. begin
  1287. writeln('Internal error ',i);
  1288. runerror(255);
  1289. end;
  1290. Function Nextafter(x,y:double):double;
  1291. // Returns the double precision number closest to x in
  1292. // the direction toward y.
  1293. // Initial direct translation by Soeren Haastrup from
  1294. // www.netlib.org/fdlibm/s_nextafter.c according to
  1295. // ====================================================
  1296. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  1297. // Developed at SunSoft, a Sun Microsystems, Inc. business.
  1298. // Permission to use, copy, modify, and distribute this
  1299. // software is freely granted, provided that this notice
  1300. // is preserved.
  1301. // ====================================================
  1302. // and with all signaling policies preserved as is.
  1303. type
  1304. {$if defined(ENDIAN_LITTLE) and not defined(FPC_DOUBLE_HILO_SWAPPED)}
  1305. twoword=record
  1306. lo,hi:longword; // Little Endian split of a double.
  1307. end;
  1308. {$else}
  1309. twoword=record
  1310. hi,lo:longword; // Big Endian split of a double.
  1311. end;
  1312. {$endif}
  1313. var
  1314. hx,hy,ix,iy:longint;
  1315. lx,ly:longword;
  1316. Begin
  1317. hx:=twoword(x).hi; // high and low words of x and y
  1318. lx:=twoword(x).lo;
  1319. hy:=twoword(y).hi;
  1320. ly:=twoword(y).lo;
  1321. ix:=hx and $7fffffff; // absolute values
  1322. iy:=hy and $7fffffff;
  1323. // Case x=NAN or y=NAN
  1324. if ( (ix>=$7ff00000) and ((longword(ix-$7ff00000) or lx) <> 0) )
  1325. or ( (iy>=$7ff00000) and ((longword(iy-$7ff00000) OR ly) <> 0) )
  1326. then exit(x+y);
  1327. // Case x=y
  1328. if x=y then exit(x); // (implies Nextafter(0,-0) is 0 and not -0...)
  1329. // Case x=0
  1330. if (longword(ix) or lx)=0
  1331. then begin
  1332. twoword(x).hi:=hy and $80000000; // return +-minimalSubnormal
  1333. twoword(x).lo:=1;
  1334. y:=x*x; // set underflow flag (ignored in FPC as default)
  1335. if y=x
  1336. then exit(y)
  1337. else exit(x);
  1338. end;
  1339. // all other cases
  1340. if hx>=0 // x>0
  1341. then begin
  1342. if (hx>hy) or ( (hx=hy) and (lx>ly) ) // x>y , return x-ulp
  1343. then begin
  1344. if (lx=0) then hx:=hx-1;
  1345. lx:=lx-1;
  1346. end
  1347. else begin // x<y, return x+ulp
  1348. lx:=lx+1;
  1349. if lx=0 then hx:=hx+1;
  1350. end
  1351. end
  1352. else begin // x<0
  1353. if (hy>=0) or (hx>=hy) or ( (hx=hy) and (lx>ly)) // x<y, return x-ulp
  1354. then begin
  1355. if (lx=0) then hx:=hx-1;
  1356. lx:=lx-1;
  1357. end
  1358. else begin // x>y , return x+ulp
  1359. lx:=lx+1;
  1360. if lx=0 then hx:=hx+1;
  1361. end
  1362. end;
  1363. // finally check if overflow or underflow just happend
  1364. hy:=hx and $7ff00000;
  1365. if (hy>= $7ff00000) then exit(x+x); // overflow and signal
  1366. if (hy<$0010000) // underflow
  1367. then begin
  1368. y:=x*x; // raise underflow flag
  1369. if y<>x
  1370. then begin
  1371. twoword(y).hi:=hx;
  1372. twoword(y).lo:=lx;
  1373. exit(y);
  1374. end
  1375. end;
  1376. twoword(x).hi:=hx;
  1377. twoword(x).lo:=lx;
  1378. nextafter:=x;
  1379. end;
  1380. function LengthUleb128(a: qword) : byte;
  1381. begin
  1382. result:=0;
  1383. repeat
  1384. a := a shr 7;
  1385. inc(result);
  1386. if a=0 then
  1387. break;
  1388. until false;
  1389. end;
  1390. function LengthSleb128(a: int64) : byte;
  1391. var
  1392. b: byte;
  1393. more: boolean;
  1394. begin
  1395. more := true;
  1396. result:=0;
  1397. repeat
  1398. b := a and $7f;
  1399. a := SarInt64(a, 7);
  1400. if (
  1401. ((a = 0) and (b and $40 = 0)) or
  1402. ((a = -1) and (b and $40 <> 0))
  1403. ) then
  1404. more := false;
  1405. inc(result);
  1406. if not(more) then
  1407. break;
  1408. until false;
  1409. end;
  1410. function EncodeUleb128(a: qword;out buf;len : byte) : byte;
  1411. var
  1412. b: byte;
  1413. pbuf : pbyte;
  1414. begin
  1415. result:=0;
  1416. pbuf:=@buf;
  1417. repeat
  1418. b := a and $7f;
  1419. a := a shr 7;
  1420. if a<>0 then
  1421. b := b or $80;
  1422. pbuf^:=b;
  1423. inc(pbuf);
  1424. inc(result);
  1425. if (a=0) and (result>=len) then
  1426. break;
  1427. until false;
  1428. end;
  1429. function EncodeSleb128(a: int64;out buf;len : byte) : byte;
  1430. var
  1431. b: byte;
  1432. more: boolean;
  1433. pbuf : pbyte;
  1434. begin
  1435. more := true;
  1436. result:=0;
  1437. pbuf:=@buf;
  1438. repeat
  1439. b := a and $7f;
  1440. a := SarInt64(a, 7);
  1441. if (result+1>=len) and (
  1442. ((a = 0) and (b and $40 = 0)) or
  1443. ((a = -1) and (b and $40 <> 0))
  1444. ) then
  1445. more := false
  1446. else
  1447. b := b or $80;
  1448. pbuf^:=b;
  1449. inc(pbuf);
  1450. inc(result);
  1451. until not more;
  1452. end;
  1453. initialization
  1454. internalerrorproc:=@defaulterror;
  1455. initupperlower;
  1456. end.