genset.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. if @source<>@dest then
  82. move(source,dest,size);
  83. tbsetarray(dest)[b]:=1;
  84. end;
  85. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_BYTE}
  86. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  87. {
  88. suppresses the element b to the set pointed by p
  89. used for exclude(set,element)
  90. }
  91. procedure fpc_varset_unset(const source;var dest; b,size : ptrint); compilerproc;
  92. type
  93. tbsetarray = bitpacked array[0..high(sizeint)-1] of 0..1;
  94. begin
  95. if @source<>@dest then
  96. move(source,dest,size);
  97. tbsetarray(dest)[b]:=0;
  98. end;
  99. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_UNSET_BYTE}
  100. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  101. {
  102. adds the range [l..h] to the set orgset
  103. }
  104. procedure fpc_varset_set_range(const orgset; var dest;l,h,size : ptrint); compilerproc;
  105. var
  106. bp : pbyte;
  107. nbits,partbits,partbytes : sizeint;
  108. begin
  109. nbits:=h-l+1;
  110. if nbits<=0 then
  111. exit;
  112. if @orgset<>@dest then
  113. move(orgset,dest,size);
  114. bp:=pbyte(@dest)+l shr 3;
  115. partbits:=-l and 7;
  116. if partbits<>0 then { Head. }
  117. if partbits>=nbits then
  118. begin
  119. bp^:=bp^ or {$ifdef endian_little} (1 shl nbits-1) shl 8 shr partbits {$else} (1 shl nbits-1) shl partbits shr nbits {$endif};
  120. exit;
  121. end
  122. else
  123. begin
  124. bp^:=bp^ or {$ifdef endian_little} byte($FF00 shr partbits) {$else} (1 shl partbits-1) {$endif};
  125. inc(bp);
  126. nbits:=nbits-partbits;
  127. end;
  128. partbytes:=nbits shr 3;
  129. FillChar(bp^,partbytes,$FF); { Full bytes. }
  130. bp:=bp+partbytes;
  131. nbits:=nbits and 7;
  132. if nbits<>0 then { Tail. }
  133. bp^:=bp^ or {$ifdef endian_little} (1 shl nbits-1) {$else} byte($FF00 shr nbits) {$endif};
  134. end;
  135. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  136. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  137. {
  138. adds set1 and set2 into set dest
  139. }
  140. procedure fpc_varset_add_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  141. type
  142. tbytearray = array[0..maxsetsize-1] of byte;
  143. begin
  144. if (size>=sizeof(PtrUint))
  145. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  146. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  147. {$endif}
  148. then
  149. begin
  150. { Work in PtrUints from the end. }
  151. size:=size-sizeof(PtrUint);
  152. repeat
  153. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ or PPtrUint(pointer(@set2)+size)^;
  154. size:=size-sizeof(PtrUint);
  155. until size<=0;
  156. { Head, overlapping in non-existing cases of size = sizeof(PtrUint) or size mod sizeof(PtrUint) <> 0.
  157. “Or” is idempotent, so dest = set1 or set2 does not matter. }
  158. PPtrUint(@dest)^:=PPtrUint(@set1)^ or PPtrUint(@set2)^;
  159. exit;
  160. end;
  161. repeat
  162. dec(size);
  163. tbytearray(dest)[size]:=tbytearray(set1)[size] or tbytearray(set2)[size];
  164. until size=0;
  165. end;
  166. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  167. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  168. {
  169. multiplies (takes common elements of) set1 and set2 result put in dest
  170. }
  171. procedure fpc_varset_mul_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  172. type
  173. tbytearray = array[0..maxsetsize-1] of byte;
  174. begin
  175. { fpc_varset_add_sets with 'or' instead of 'and'. }
  176. if (size>=sizeof(PtrUint))
  177. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  178. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  179. {$endif}
  180. then
  181. begin
  182. size:=size-sizeof(PtrUint);
  183. repeat
  184. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ and PPtrUint(pointer(@set2)+size)^;
  185. size:=size-sizeof(PtrUint);
  186. until size<=0;
  187. PPtrUint(@dest)^:=PPtrUint(@set1)^ and PPtrUint(@set2)^;
  188. exit;
  189. end;
  190. repeat
  191. dec(size);
  192. tbytearray(dest)[size]:=tbytearray(set1)[size] and tbytearray(set2)[size];
  193. until size=0;
  194. end;
  195. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  196. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  197. {
  198. computes the diff from set1 to set2 result in dest
  199. }
  200. procedure fpc_varset_sub_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  201. type
  202. tbytearray = array[0..maxsetsize-1] of byte;
  203. var
  204. headval : ptruint;
  205. begin
  206. if (size>=sizeof(PtrUint))
  207. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  208. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  209. {$endif}
  210. then
  211. begin
  212. { Head, overlapping in non-existing cases of size = sizeof(PtrUint) or size mod sizeof(PtrUint) <> 0.
  213. “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. }
  214. headval:=PPtrUint(@set1)^ and not PPtrUint(@set2)^;
  215. { Work in PtrUints from the end. }
  216. size:=size-sizeof(PtrUint);
  217. repeat
  218. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ and not PPtrUint(pointer(@set2)+size)^;
  219. size:=size-sizeof(PtrUint);
  220. until size<=0;
  221. PPtrUint(@dest)^:=headval;
  222. exit;
  223. end;
  224. repeat
  225. dec(size);
  226. tbytearray(dest)[size]:=tbytearray(set1)[size] and not tbytearray(set2)[size];
  227. until size=0;
  228. end;
  229. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  230. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  231. {
  232. computes the symetric diff from set1 to set2 result in dest
  233. }
  234. procedure fpc_varset_symdif_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  235. type
  236. tbytearray = array[0..maxsetsize-1] of byte;
  237. var
  238. headval : ptruint;
  239. begin
  240. { fpc_varset_sub_sets with 'xor' instead of 'and not'. }
  241. if (size>=sizeof(PtrUint))
  242. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  243. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(@dest) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  244. {$endif}
  245. then
  246. begin
  247. headval:=PPtrUint(@set1)^ xor PPtrUint(@set2)^;
  248. size:=size-sizeof(PtrUint);
  249. repeat
  250. PPtrUint(pointer(@dest)+size)^:=PPtrUint(pointer(@set1)+size)^ xor PPtrUint(pointer(@set2)+size)^;
  251. size:=size-sizeof(PtrUint);
  252. until size<=0;
  253. PPtrUint(@dest)^:=headval;
  254. exit;
  255. end;
  256. repeat
  257. dec(size);
  258. tbytearray(dest)[size]:=tbytearray(set1)[size] xor tbytearray(set2)[size];
  259. until size=0;
  260. end;
  261. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  262. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  263. {
  264. compares set1 and set2 zeroflag is set if they are equal
  265. }
  266. function fpc_varset_comp_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  267. begin
  268. result:=CompareByte(set1,set2,size)=0;
  269. end;
  270. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  271. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}
  272. {
  273. on exit, zero flag is set if set1 <= set2 (set2 contains set1)
  274. }
  275. function fpc_varset_contains_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  276. var
  277. set1p,set2p,set1tail : pointer;
  278. begin
  279. result:=false;
  280. set1p:=@set1;
  281. set2p:=@set2;
  282. { Should scan left to right because first bits are more likely to differ. }
  283. if (size>=sizeof(PtrUint))
  284. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  285. and ((PtrUint(@set1) or PtrUint(@set2) or PtrUint(size)) and (sizeof(PtrUint)-1)=0)
  286. {$endif}
  287. then
  288. begin
  289. set1tail:=set1p+size-sizeof(PtrUint);
  290. repeat
  291. if PPtrUint(set1p)^ and not PPtrUint(set2p)^<>0 then
  292. exit;
  293. inc(set1p,sizeof(PtrUint));
  294. inc(set2p,sizeof(PtrUint));
  295. until set1p>=set1tail;
  296. dec(set2p,set1p-set1tail); { set2p = “set2tail” }
  297. exit(PPtrUint(set1tail)^ and not PPtrUint(set2p)^=0);
  298. end;
  299. set1tail:=set1p+size;
  300. repeat
  301. if pbyte(set1p)^ and not pbyte(set2p)^<>0 then
  302. exit;
  303. inc(set1p);
  304. inc(set2p);
  305. until set1p=set1tail;
  306. result:=true;
  307. end;
  308. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}