genset.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2001 by the Free Pascal development team
  4. Include file with set operations called by the compiler
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  12. {$ifndef FPC_SYSTEM_HAS_FPC_SET_LOAD_SMALL}
  13. { Error No pascal version of FPC_SET_LOAD_SMALL}
  14. { THIS DEPENDS ON THE ENDIAN OF THE ARCHITECTURE!
  15. Not anymore PM}
  16. function fpc_set_load_small(l: fpc_small_set): fpc_normal_set; [public,alias:'FPC_SET_LOAD_SMALL']; compilerproc;
  17. {
  18. load a normal set p from a smallset l
  19. }
  20. begin
  21. FillDWord(fpc_set_load_small,sizeof(fpc_set_load_small) div 4,0);
  22. move(l,fpc_set_load_small,sizeof(l));
  23. end;
  24. {$endif FPC_SYSTEM_HAS_FPC_SET_LOAD_SMALL}
  25. {$ifndef FPC_SYSTEM_HAS_FPC_SET_CREATE_ELEMENT}
  26. function fpc_set_create_element(b : byte): fpc_normal_set;[public,alias:'FPC_SET_CREATE_ELEMENT']; compilerproc;
  27. {
  28. create a new set in p from an element b
  29. }
  30. begin
  31. FillDWord(fpc_set_create_element,SizeOf(fpc_set_create_element) div 4,0);
  32. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  33. fpc_set_create_element[b div 32] := 1 shl (b mod 32);
  34. {$else}
  35. fpc_set_create_element[b] := 1;
  36. {$endif}
  37. end;
  38. {$endif FPC_SYSTEM_HAS_FPC_SET_CREATE_ELEMENT}
  39. {$ifndef FPC_SYSTEM_HAS_FPC_SET_SET_BYTE}
  40. function fpc_set_set_byte(const source: fpc_normal_set; b : byte): fpc_normal_set; compilerproc;
  41. {
  42. add the element b to the set "source"
  43. }
  44. var
  45. c: longint;
  46. begin
  47. move(source,fpc_set_set_byte,sizeof(source));
  48. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  49. c := fpc_set_set_byte[b div 32];
  50. c := (1 shl (b mod 32)) or c;
  51. fpc_set_set_byte[b div 32] := c;
  52. {$else}
  53. fpc_set_set_byte[b] := 1;
  54. {$endif}
  55. end;
  56. {$endif FPC_SYSTEM_HAS_FPC_SET_SET_BYTE}
  57. {$ifndef FPC_SYSTEM_HAS_FPC_SET_UNSET_BYTE}
  58. function fpc_set_unset_byte(const source: fpc_normal_set; b : byte): fpc_normal_set; compilerproc;
  59. {
  60. suppresses the element b to the set pointed by p
  61. used for exclude(set,element)
  62. }
  63. var
  64. c: longint;
  65. begin
  66. move(source,fpc_set_unset_byte,sizeof(source));
  67. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  68. c := fpc_set_unset_byte[b div 32];
  69. c := c and not (1 shl (b mod 32));
  70. fpc_set_unset_byte[b div 32] := c;
  71. {$else}
  72. fpc_set_unset_byte[b] := 0;
  73. {$endif}
  74. end;
  75. {$endif FPC_SYSTEM_HAS_FPC_SET_UNSET_BYTE}
  76. {$ifndef FPC_SYSTEM_HAS_FPC_SET_SET_RANGE}
  77. function fpc_set_set_range(const orgset: fpc_normal_set; l,h : byte): fpc_normal_set; compilerproc;
  78. {
  79. adds the range [l..h] to the set orgset
  80. }
  81. var
  82. i: integer;
  83. c: longint;
  84. begin
  85. move(orgset,fpc_set_set_range,sizeof(orgset));
  86. for i:=l to h do
  87. begin
  88. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  89. c := fpc_set_set_range[i div 32];
  90. c := (1 shl (i mod 32)) or c;
  91. fpc_set_set_range[i div 32] := c;
  92. {$else}
  93. fpc_set_set_range[i] := 1;
  94. {$endif}
  95. end;
  96. end;
  97. {$endif ndef FPC_SYSTEM_HAS_FPC_SET_SET_RANGE}
  98. {$ifndef FPC_SYSTEM_HAS_FPC_SET_ADD_SETS}
  99. function fpc_set_add_sets(const set1,set2: fpc_normal_set): fpc_normal_set;[public,alias:'FPC_SET_ADD_SETS']; compilerproc;
  100. var
  101. src1: fpc_normal_set_long absolute set1;
  102. src2: fpc_normal_set_long absolute set2;
  103. dest: fpc_normal_set_long absolute fpc_set_add_sets;
  104. {
  105. adds set1 and set2 into set dest
  106. }
  107. var
  108. i: integer;
  109. begin
  110. for i:=0 to 7 do
  111. dest[i] := src1[i] or src2[i];
  112. end;
  113. {$endif}
  114. {$ifndef FPC_SYSTEM_HAS_FPC_SET_MUL_SETS}
  115. function fpc_set_mul_sets(const set1,set2: fpc_normal_set): fpc_normal_set;[public,alias:'FPC_SET_MUL_SETS']; compilerproc;
  116. var
  117. src1: fpc_normal_set_long absolute set1;
  118. src2: fpc_normal_set_long absolute set2;
  119. dest: fpc_normal_set_long absolute fpc_set_mul_sets;
  120. {
  121. multiplies (takes common elements of) set1 and set2 result put in dest
  122. }
  123. var
  124. i: integer;
  125. begin
  126. for i:=0 to 7 do
  127. dest[i] := src1[i] and src2[i];
  128. end;
  129. {$endif}
  130. {$ifndef FPC_SYSTEM_HAS_FPC_SET_SUB_SETS}
  131. function fpc_set_sub_sets(const set1,set2: fpc_normal_set): fpc_normal_set;[public,alias:'FPC_SET_SUB_SETS']; compilerproc;
  132. var
  133. src1: fpc_normal_set_long absolute set1;
  134. src2: fpc_normal_set_long absolute set2;
  135. dest: fpc_normal_set_long absolute fpc_set_sub_sets;
  136. {
  137. computes the diff from set1 to set2 result in dest
  138. }
  139. var
  140. i: integer;
  141. begin
  142. for i:=0 to 7 do
  143. dest[i] := src1[i] and not src2[i];
  144. end;
  145. {$endif}
  146. {$ifndef FPC_SYSTEM_HAS_FPC_SET_SYMDIF_SETS}
  147. function fpc_set_symdif_sets(const set1,set2: fpc_normal_set): fpc_normal_set;[public,alias:'FPC_SET_SYMDIF_SETS']; compilerproc;
  148. var
  149. src1: fpc_normal_set_long absolute set1;
  150. src2: fpc_normal_set_long absolute set2;
  151. dest: fpc_normal_set_long absolute fpc_set_symdif_sets;
  152. {
  153. computes the symetric diff from set1 to set2 result in dest
  154. }
  155. var
  156. i: integer;
  157. begin
  158. for i:=0 to 7 do
  159. dest[i] := src1[i] xor src2[i];
  160. end;
  161. {$endif}
  162. {$ifndef FPC_SYSTEM_HAS_FPC_SET_COMP_SETS}
  163. function fpc_set_comp_sets(const set1,set2 : fpc_normal_set):boolean;[public,alias:'FPC_SET_COMP_SETS'];compilerproc;
  164. {
  165. compares set1 and set2 zeroflag is set if they are equal
  166. }
  167. var
  168. i: integer;
  169. src1: fpc_normal_set_long absolute set1;
  170. src2: fpc_normal_set_long absolute set2;
  171. begin
  172. fpc_set_comp_sets:= false;
  173. for i:=0 to 7 do
  174. if src1[i] <> src2[i] then
  175. exit;
  176. fpc_set_comp_sets:= true;
  177. end;
  178. {$endif}
  179. {$ifndef FPC_SYSTEM_HAS_FPC_SET_CONTAINS_SET}
  180. function fpc_set_contains_sets(const set1,set2 : fpc_normal_set):boolean;[public,alias:'FPC_SET_CONTAINS_SETS'];compilerproc;
  181. {
  182. on exit, zero flag is set if set1 <= set2 (set2 contains set1)
  183. }
  184. var
  185. i : integer;
  186. src1: fpc_normal_set_long absolute set1;
  187. src2: fpc_normal_set_long absolute set2;
  188. begin
  189. fpc_set_contains_sets:= false;
  190. for i:=0 to 7 do
  191. if (src1[i] and not src2[i]) <> 0 then
  192. exit;
  193. fpc_set_contains_sets:= true;
  194. end;
  195. {$endif}
  196. {$endif ndef FPC_NEW_BIGENDIAN_SETS}
  197. {****************************************************************************
  198. Var sets
  199. ****************************************************************************}
  200. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_LOAD_SMALL}
  201. {
  202. convert sets
  203. }
  204. {$ifdef FPC_SETBASE_USED}
  205. procedure fpc_varset_load(const l;sourcesize : longint;var dest;size,srcminusdstbase : ptrint); compilerproc;
  206. var
  207. srcptr, dstptr: pointer;
  208. begin
  209. srcptr:=@l;
  210. dstptr:=@dest;
  211. { going from a higher base to a lower base, e.g.
  212. src: 001f0000, base=2,size=4 -> 0000001f0000 in base 0
  213. dstr in base = 1 (-> srcminusdstbase = 1) -> to
  214. 00001f0000, base=1 -> need to prepend "srcminusdstbase" zero bytes
  215. }
  216. if (srcminusdstbase>0) then
  217. begin
  218. { fill the skipped part with 0 }
  219. fillchar(dstptr^,srcminusdstbase,0);
  220. inc(dstptr,srcminusdstbase);
  221. dec(size,srcminusdstbase);
  222. end
  223. else if (srcminusdstbase<0) then
  224. begin
  225. { inc/dec switched since srcminusdstbase < 0 }
  226. dec(srcptr,srcminusdstbase);
  227. inc(sourcesize,srcminusdstbase);
  228. end;
  229. if sourcesize>size then
  230. sourcesize:=size;
  231. move(srcptr^,dstptr^,sourcesize);
  232. { fill the leftover (if any) with 0 }
  233. FillChar((dstptr+sourcesize)^,size-sourcesize,0);
  234. end;
  235. {$else FPC_SETBASE_USED}
  236. procedure fpc_varset_load(const l;sourcesize : longint;var dest;size : ptrint); compilerproc;
  237. begin
  238. if sourcesize>size then
  239. sourcesize:=size;
  240. move(l,plongint(@dest)^,sourcesize);
  241. FillChar((@dest+sourcesize)^,size-sourcesize,0);
  242. end;
  243. {$endif FPC_SETBASE_USED}
  244. {$endif ndef FPC_SYSTEM_HAS_FPC_SET_LOAD_SMALL}
  245. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CREATE_ELEMENT}
  246. {
  247. create a new set in p from an element b
  248. }
  249. procedure fpc_varset_create_element(b,size : ptrint; var data); compilerproc;
  250. type
  251. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  252. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  253. {$else}
  254. tbsetarray = bitpacked array[0..sizeof(sizeint)-1] of 0..1;
  255. {$endif}
  256. begin
  257. FillChar(data,size,0);
  258. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  259. tbytearray(data)[b div 8]:=1 shl (b mod 8);
  260. {$else}
  261. tbsetarray(data)[b]:=1;
  262. {$endif}
  263. end;
  264. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CREATE_ELEMENT}
  265. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SET_BYTE}
  266. {
  267. add the element b to the set "source"
  268. }
  269. procedure fpc_varset_set(const source;var dest; b,size : ptrint); compilerproc;
  270. type
  271. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  272. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  273. {$else}
  274. tbsetarray = bitpacked array[0..sizeof(sizeint)-1] of 0..1;
  275. {$endif}
  276. begin
  277. move(source,dest,size);
  278. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  279. tbytearray(dest)[b div 8]:=tbytearray(dest)[b div 8] or (1 shl (b mod 8));
  280. {$else}
  281. tbsetarray(dest)[b]:=1;
  282. {$endif}
  283. end;
  284. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_BYTE}
  285. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  286. {
  287. suppresses the element b to the set pointed by p
  288. used for exclude(set,element)
  289. }
  290. procedure fpc_varset_unset(const source;var dest; b,size : ptrint); compilerproc;
  291. type
  292. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  293. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  294. {$else}
  295. tbsetarray = bitpacked array[0..sizeof(sizeint)-1] of 0..1;
  296. {$endif}
  297. begin
  298. move(source,dest,size);
  299. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  300. tbytearray(dest)[b div 8]:=tbytearray(dest)[b div 8] and not (1 shl (b mod 8));
  301. {$else}
  302. tbsetarray(dest)[b]:=0;
  303. {$endif}
  304. end;
  305. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  306. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  307. {
  308. adds the range [l..h] to the set orgset
  309. }
  310. procedure fpc_varset_set_range(const orgset; var dest;l,h,size : ptrint); compilerproc;
  311. type
  312. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  313. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  314. {$else}
  315. tbsetarray = bitpacked array[0..sizeof(sizeint)-1] of 0..1;
  316. {$endif}
  317. var
  318. i : ptrint;
  319. begin
  320. move(orgset,dest,size);
  321. for i:=l to h do
  322. {$ifndef FPC_NEW_BIGENDIAN_SETS}
  323. tbytearray(dest)[i div 8]:=(1 shl (i mod 8)) or tbytearray(dest)[i div 8];
  324. {$else}
  325. tbsetarray(dest)[i]:=1;
  326. {$endif}
  327. end;
  328. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  329. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  330. {
  331. adds set1 and set2 into set dest
  332. }
  333. procedure fpc_varset_add_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  334. type
  335. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  336. var
  337. i : ptrint;
  338. begin
  339. for i:=0 to size-1 do
  340. tbytearray(dest)[i]:=tbytearray(set1)[i] or tbytearray(set2)[i];
  341. end;
  342. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  343. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  344. {
  345. multiplies (takes common elements of) set1 and set2 result put in dest
  346. }
  347. procedure fpc_varset_mul_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  348. type
  349. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  350. var
  351. i : ptrint;
  352. begin
  353. for i:=0 to size-1 do
  354. tbytearray(dest)[i]:=tbytearray(set1)[i] and tbytearray(set2)[i];
  355. end;
  356. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  357. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  358. {
  359. computes the diff from set1 to set2 result in dest
  360. }
  361. procedure fpc_varset_sub_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  362. type
  363. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  364. var
  365. i : ptrint;
  366. begin
  367. for i:=0 to size-1 do
  368. tbytearray(dest)[i]:=tbytearray(set1)[i] and not tbytearray(set2)[i];
  369. end;
  370. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  371. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  372. {
  373. computes the symetric diff from set1 to set2 result in dest
  374. }
  375. procedure fpc_varset_symdif_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  376. type
  377. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  378. var
  379. i : ptrint;
  380. begin
  381. for i:=0 to size-1 do
  382. tbytearray(dest)[i]:=tbytearray(set1)[i] xor tbytearray(set2)[i];
  383. end;
  384. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  385. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  386. {
  387. compares set1 and set2 zeroflag is set if they are equal
  388. }
  389. function fpc_varset_comp_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  390. type
  391. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  392. var
  393. i : ptrint;
  394. begin
  395. fpc_varset_comp_sets:= false;
  396. for i:=0 to size-1 do
  397. if tbytearray(set1)[i]<>tbytearray(set2)[i] then
  398. exit;
  399. fpc_varset_comp_sets:=true;
  400. end;
  401. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  402. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}
  403. {
  404. on exit, zero flag is set if set1 <= set2 (set2 contains set1)
  405. }
  406. function fpc_varset_contains_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  407. type
  408. tbytearray = array[0..sizeof(sizeint)-1] of byte;
  409. var
  410. i : ptrint;
  411. begin
  412. fpc_varset_contains_sets:= false;
  413. for i:=0 to size-1 do
  414. if (tbytearray(set1)[i] and not tbytearray(set2)[i])<>0 then
  415. exit;
  416. fpc_varset_contains_sets:=true;
  417. end;
  418. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}