genset.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. {****************************************************************************
  12. Var sets
  13. ****************************************************************************}
  14. const
  15. maxsetsize = 32;
  16. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_LOAD_SMALL}
  17. {
  18. convert sets
  19. }
  20. {$ifdef FPC_SETBASE_USED}
  21. procedure fpc_varset_load(const l;sourcesize : longint;var dest;size,srcminusdstbase : ptrint); compilerproc;
  22. var
  23. srcptr, dstptr: pointer;
  24. begin
  25. srcptr:=@l;
  26. dstptr:=@dest;
  27. { going from a higher base to a lower base, e.g.
  28. src: 001f0000, base=2,size=4 -> 0000001f0000 in base 0
  29. dstr in base = 1 (-> srcminusdstbase = 1) -> to
  30. 00001f0000, base=1 -> need to prepend "srcminusdstbase" zero bytes
  31. }
  32. if (srcminusdstbase>0) then
  33. begin
  34. { fill the skipped part with 0 }
  35. fillchar(dstptr^,srcminusdstbase,0);
  36. inc(dstptr,srcminusdstbase);
  37. dec(size,srcminusdstbase);
  38. end
  39. else if (srcminusdstbase<0) then
  40. begin
  41. { inc/dec switched since srcminusdstbase < 0 }
  42. dec(srcptr,srcminusdstbase);
  43. inc(sourcesize,srcminusdstbase);
  44. end;
  45. if sourcesize>size then
  46. sourcesize:=size;
  47. move(srcptr^,dstptr^,sourcesize);
  48. { fill the leftover (if any) with 0 }
  49. FillChar((dstptr+sourcesize)^,size-sourcesize,0);
  50. end;
  51. {$else FPC_SETBASE_USED}
  52. procedure fpc_varset_load(const l;sourcesize : longint;var dest;size : ptrint); compilerproc;
  53. begin
  54. if sourcesize>size then
  55. sourcesize:=size;
  56. move(l,plongint(@dest)^,sourcesize);
  57. FillChar((@dest+sourcesize)^,size-sourcesize,0);
  58. end;
  59. {$endif FPC_SETBASE_USED}
  60. {$endif ndef FPC_SYSTEM_HAS_FPC_SET_LOAD_SMALL}
  61. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CREATE_ELEMENT}
  62. {
  63. create a new set in p from an element b
  64. }
  65. procedure fpc_varset_create_element(b,size : ptrint; var data); compilerproc;
  66. type
  67. tbsetarray = bitpacked array[0..high(sizeint)-1] of 0..1;
  68. begin
  69. FillChar(data,size,0);
  70. tbsetarray(data)[b]:=1;
  71. end;
  72. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CREATE_ELEMENT}
  73. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SET_BYTE}
  74. {
  75. add the element b to the set "source"
  76. }
  77. procedure fpc_varset_set(const source;var dest; b,size : ptrint); compilerproc;
  78. type
  79. tbsetarray = bitpacked array[0..high(sizeint)-1] of 0..1;
  80. begin
  81. move(source,dest,size);
  82. tbsetarray(dest)[b]:=1;
  83. end;
  84. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_BYTE}
  85. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  86. {
  87. suppresses the element b to the set pointed by p
  88. used for exclude(set,element)
  89. }
  90. procedure fpc_varset_unset(const source;var dest; b,size : ptrint); compilerproc;
  91. type
  92. tbsetarray = bitpacked array[0..high(sizeint)-1] of 0..1;
  93. begin
  94. move(source,dest,size);
  95. tbsetarray(dest)[b]:=0;
  96. end;
  97. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  98. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  99. {
  100. adds the range [l..h] to the set orgset
  101. }
  102. procedure fpc_varset_set_range(const orgset; var dest;l,h,size : ptrint); compilerproc;
  103. var
  104. bp : pbyte;
  105. nbits,partbits,partbytes : sizeint;
  106. begin
  107. nbits:=h-l+1;
  108. if nbits<=0 then
  109. exit;
  110. if @orgset<>@dest then
  111. move(orgset,dest,size);
  112. bp:=pbyte(@dest)+l shr 3;
  113. partbits:=-l and 7;
  114. if partbits<>0 then { Head. }
  115. if partbits>=nbits then
  116. begin
  117. bp^:=bp^ or {$ifdef endian_little} (1 shl nbits-1) shl 8 shr partbits {$else} (1 shl nbits-1) shl partbits shr nbits {$endif};
  118. exit;
  119. end
  120. else
  121. begin
  122. bp^:=bp^ or {$ifdef endian_little} byte($FF00 shr partbits) {$else} (1 shl partbits-1) {$endif};
  123. inc(bp);
  124. nbits:=nbits-partbits;
  125. end;
  126. partbytes:=nbits shr 3;
  127. FillChar(bp^,partbytes,$FF); { Full bytes. }
  128. bp:=bp+partbytes;
  129. nbits:=nbits and 7;
  130. if nbits<>0 then { Tail. }
  131. bp^:=bp^ or {$ifdef endian_little} (1 shl nbits-1) {$else} byte($FF00 shr nbits) {$endif};
  132. end;
  133. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  134. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  135. {
  136. adds set1 and set2 into set dest
  137. }
  138. procedure fpc_varset_add_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  139. type
  140. tbytearray = array[0..maxsetsize-1] of byte;
  141. begin
  142. if (size>=sizeof(PtrUint))
  143. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  144. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  145. {$endif}
  146. then
  147. begin
  148. { Work in PtrUints from the end. }
  149. size:=size-sizeof(PtrUint);
  150. repeat
  151. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ or PPtrUint(pointer(@set2)+size)^;
  152. size:=size-sizeof(PtrUint);
  153. until size<=0;
  154. { Head, overlapping in non-existing cases of size = sizeof(PtrUint) or size mod sizeof(PtrUint) <> 0.
  155. “Or” is idempotent, so dest = set1 or set2 does not matter. }
  156. PPtrUint(@dest)^:=PPtrUint(@set1)^ or PPtrUint(@set2)^;
  157. exit;
  158. end;
  159. repeat
  160. dec(size);
  161. tbytearray(dest)[size]:=tbytearray(set1)[size] or tbytearray(set2)[size];
  162. until size=0;
  163. end;
  164. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  165. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  166. {
  167. multiplies (takes common elements of) set1 and set2 result put in dest
  168. }
  169. procedure fpc_varset_mul_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  170. type
  171. tbytearray = array[0..maxsetsize-1] of byte;
  172. begin
  173. { fpc_varset_add_sets with 'or' instead of 'and'. }
  174. if (size>=sizeof(PtrUint))
  175. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  176. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  177. {$endif}
  178. then
  179. begin
  180. size:=size-sizeof(PtrUint);
  181. repeat
  182. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ and PPtrUint(pointer(@set2)+size)^;
  183. size:=size-sizeof(PtrUint);
  184. until size<=0;
  185. PPtrUint(@dest)^:=PPtrUint(@set1)^ and PPtrUint(@set2)^;
  186. exit;
  187. end;
  188. repeat
  189. dec(size);
  190. tbytearray(dest)[size]:=tbytearray(set1)[size] and tbytearray(set2)[size];
  191. until size=0;
  192. end;
  193. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  194. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  195. {
  196. computes the diff from set1 to set2 result in dest
  197. }
  198. procedure fpc_varset_sub_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  199. type
  200. tbytearray = array[0..maxsetsize-1] of byte;
  201. var
  202. headval : ptruint;
  203. begin
  204. if (size>=sizeof(PtrUint))
  205. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  206. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  207. {$endif}
  208. then
  209. begin
  210. { Head, overlapping in non-existing cases of size = sizeof(PtrUint) or size mod sizeof(PtrUint) <> 0.
  211. “And not” is not idempotent, so head must be calculated in advance to work correctly when, in this non-existing case, dest = set1 or set2. }
  212. headval:=PPtrUint(@set1)^ and not PPtrUint(@set2)^;
  213. { Work in PtrUints from the end. }
  214. size:=size-sizeof(PtrUint);
  215. repeat
  216. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ and not PPtrUint(pointer(@set2)+size)^;
  217. size:=size-sizeof(PtrUint);
  218. until size<=0;
  219. PPtrUint(@dest)^:=headval;
  220. exit;
  221. end;
  222. repeat
  223. dec(size);
  224. tbytearray(dest)[size]:=tbytearray(set1)[size] and not tbytearray(set2)[size];
  225. until size=0;
  226. end;
  227. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  228. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  229. {
  230. computes the symetric diff from set1 to set2 result in dest
  231. }
  232. procedure fpc_varset_symdif_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  233. type
  234. tbytearray = array[0..maxsetsize-1] of byte;
  235. var
  236. headval : ptruint;
  237. begin
  238. { fpc_varset_sub_sets with 'xor' instead of 'and not'. }
  239. if (size>=sizeof(PtrUint))
  240. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  241. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  242. {$endif}
  243. then
  244. begin
  245. headval:=PPtrUint(@set1)^ xor PPtrUint(@set2)^;
  246. size:=size-sizeof(PtrUint);
  247. repeat
  248. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ xor PPtrUint(pointer(@set2)+size)^;
  249. size:=size-sizeof(PtrUint);
  250. until size<=0;
  251. PPtrUint(@dest)^:=headval;
  252. exit;
  253. end;
  254. repeat
  255. dec(size);
  256. tbytearray(dest)[size]:=tbytearray(set1)[size] xor tbytearray(set2)[size];
  257. until size=0;
  258. end;
  259. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  260. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  261. {
  262. compares set1 and set2 zeroflag is set if they are equal
  263. }
  264. function fpc_varset_comp_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  265. begin
  266. result:=CompareByte(set1,set2,size)=0;
  267. end;
  268. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  269. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}
  270. {
  271. on exit, zero flag is set if set1 <= set2 (set2 contains set1)
  272. }
  273. function fpc_varset_contains_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  274. var
  275. set1p,set2p,set1tail : pointer;
  276. begin
  277. result:=false;
  278. set1p:=@set1;
  279. set2p:=@set2;
  280. { Should scan left to right because first bits are more likely to differ. }
  281. if (size>=sizeof(PtrUint))
  282. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  283. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  284. {$endif}
  285. then
  286. begin
  287. set1tail:=set1p+size-sizeof(PtrUint);
  288. repeat
  289. if PPtrUint(set1p)^ and not PPtrUint(set2p)^<>0 then
  290. exit;
  291. inc(set1p,sizeof(PtrUint));
  292. inc(set2p,sizeof(PtrUint));
  293. until set1p>=set1tail;
  294. dec(set2p,set1p-set1tail); { set2p = “set2tail” }
  295. exit(PPtrUint(set1tail)^ and not PPtrUint(set2p)^=0);
  296. end;
  297. set1tail:=set1p+size;
  298. repeat
  299. if pbyte(set1p)^ and not pbyte(set2p)^<>0 then
  300. exit;
  301. inc(set1p);
  302. inc(set2p);
  303. until set1p=set1tail;
  304. result:=true;
  305. end;
  306. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}