genset.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. type
  104. tbsetarray = bitpacked array[0..high(sizeint)-1] of 0..1;
  105. var
  106. i : ptrint;
  107. begin
  108. move(orgset,dest,size);
  109. for i:=l to h do
  110. tbsetarray(dest)[i]:=1;
  111. end;
  112. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SET_RANGE}
  113. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  114. {
  115. adds set1 and set2 into set dest
  116. }
  117. procedure fpc_varset_add_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  118. type
  119. tbytearray = array[0..maxsetsize-1] of byte;
  120. var
  121. i : ptrint;
  122. begin
  123. for i:=0 to size-1 do
  124. tbytearray(dest)[i]:=tbytearray(set1)[i] or tbytearray(set2)[i];
  125. end;
  126. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_ADD_SETS}
  127. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  128. {
  129. multiplies (takes common elements of) set1 and set2 result put in dest
  130. }
  131. procedure fpc_varset_mul_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  132. type
  133. tbytearray = array[0..maxsetsize-1] of byte;
  134. var
  135. i : ptrint;
  136. begin
  137. for i:=0 to size-1 do
  138. tbytearray(dest)[i]:=tbytearray(set1)[i] and tbytearray(set2)[i];
  139. end;
  140. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_MUL_SETS}
  141. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  142. {
  143. computes the diff from set1 to set2 result in dest
  144. }
  145. procedure fpc_varset_sub_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  146. type
  147. tbytearray = array[0..maxsetsize-1] of byte;
  148. var
  149. i : ptrint;
  150. begin
  151. for i:=0 to size-1 do
  152. tbytearray(dest)[i]:=tbytearray(set1)[i] and not tbytearray(set2)[i];
  153. end;
  154. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SUB_SETS}
  155. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  156. {
  157. computes the symetric diff from set1 to set2 result in dest
  158. }
  159. procedure fpc_varset_symdif_sets(const set1,set2; var dest;size : ptrint); compilerproc;
  160. type
  161. tbytearray = array[0..maxsetsize-1] of byte;
  162. var
  163. i : ptrint;
  164. begin
  165. for i:=0 to size-1 do
  166. tbytearray(dest)[i]:=tbytearray(set1)[i] xor tbytearray(set2)[i];
  167. end;
  168. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_SYMDIF_SETS}
  169. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  170. {
  171. compares set1 and set2 zeroflag is set if they are equal
  172. }
  173. function fpc_varset_comp_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  174. type
  175. tbytearray = array[0..maxsetsize-1] of byte;
  176. var
  177. i : ptrint;
  178. begin
  179. fpc_varset_comp_sets:= false;
  180. for i:=0 to size-1 do
  181. if tbytearray(set1)[i]<>tbytearray(set2)[i] then
  182. exit;
  183. fpc_varset_comp_sets:=true;
  184. end;
  185. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_COMP_SETS}
  186. {$ifndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}
  187. {
  188. on exit, zero flag is set if set1 <= set2 (set2 contains set1)
  189. }
  190. function fpc_varset_contains_sets(const set1,set2;size : ptrint):boolean; compilerproc;
  191. type
  192. tbytearray = array[0..maxsetsize-1] of byte;
  193. var
  194. i : ptrint;
  195. begin
  196. fpc_varset_contains_sets:= false;
  197. for i:=0 to size-1 do
  198. if (tbytearray(set1)[i] and not tbytearray(set2)[i])<>0 then
  199. exit;
  200. fpc_varset_contains_sets:=true;
  201. end;
  202. {$endif ndef FPC_SYSTEM_HAS_FPC_VARSET_CONTAINS_SET}