PageRenderTime 28ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/rice/src/ima2-4.11.11/src/xtrapbits.h

https://github.com/goshng/Peach
C Header | 356 lines | 196 code | 44 blank | 116 comment | 32 complexity | 75935f8b440c279cb9ab5688f64375e9 MD5 | raw file
  1. /* $XFree86$ */
  2. /*
  3. * This include file is designed to be a portable way for systems to define
  4. * bit field manipulation of arrays of bits.
  5. */
  6. #ifndef __XTRAPBITS__
  7. #define __XTRAPBITS__ "@(#)xtrapbits.h 1.6 - 90/09/18 "
  8. /*****************************************************************************
  9. Copyright 1987, 1988, 1989, 1990, 1994 by Digital Equipment Corporation,
  10. Maynard, MA
  11. Permission to use, copy, modify, and distribute this software and its
  12. documentation for any purpose and without fee is hereby granted,
  13. provided that the above copyright notice appear in all copies and that
  14. both that copyright notice and this permission notice appear in
  15. supporting documentation, and that the name of Digital not be
  16. used in advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. *****************************************************************************/
  26. /*
  27. *
  28. * CONTRIBUTORS:
  29. *
  30. * Dick Annicchiarico
  31. * Robert Chesler
  32. * Dan Coutu
  33. * Gene Durso
  34. * Marc Evans
  35. * Alan Jamison
  36. * Mark Henry
  37. * Ken Miller
  38. *
  39. */
  40. typedef unsigned char *UByteP; /* Pointer to an unsigned byte array */
  41. #define BitsInByte 8L /* The number of bits in a byte */
  42. #define BitInByte(bit) /* Returns the bit mask of a byte */ \
  43. (1L << (((bit) % BitsInByte)))
  44. #define BitInWord(bit) /* Returns the bit mask of a word */ \
  45. (1L << (((bit) % (BitsInByte * 2L))))
  46. #define BitInLong(bit) /* Returns the bit mask of a long */ \
  47. (1L << (((bit) % (BitsInByte * 4L))))
  48. #define ByteInArray(array,bit) /* Returns the byte offset to get to a bit */ \
  49. (((UByteP)(array))[(bit) / BitsInByte])
  50. #define BitIsTrue(array,bit) /* Test to see if a specific bit is True */ \
  51. (ByteInArray(array,bit) & BitInByte(bit))
  52. #define BitIsFalse(array,bit) /* Test to see if a specific bit is False */ \
  53. (!(BitIsTrue(array,bit)))
  54. #define BitTrue(array,bit) /* Set a specific bit to be True */ \
  55. (ByteInArray(array,bit) |= BitInByte(bit))
  56. #define BitFalse(array,bit) /* Set a specific bit to be False */ \
  57. (ByteInArray(array,bit) &= ~BitInByte(bit))
  58. #define BitToggle(array,bit) /* Toggle a specific bit */ \
  59. (ByteInArray(array,bit) ^= BitInByte(bit))
  60. #define BitCopy(dest,src,bit) /* Copy a specific bit */ \
  61. BitIsTrue((src),(bit)) ? BitTrue((dest),(bit)) : BitFalse((dest),(bit))
  62. #define BitValue(array,bit) /* Return True or False depending on bit */ \
  63. (BitIsTrue((array),(bit)) ? True : False)
  64. #define BitSet(array,bit,value) /* Set bit to given value in array */ \
  65. (value) ? BitTrue((array),(bit)) : BitFalse((array),(bit))
  66. #define BitAssign(A,B,size) /* A = B */ \
  67. memcpy ((A), (B), (size) * sizeof(unsigned char))
  68. /*
  69. for ( (i) = 0; (i) < (size); (i)++) \
  70. ((UByteP)(A))[(i)] = ((UByteP)(B))[(i)]
  71. */
  72. #define BitUnion(A,B,C,size,i) /* A = B union C */ \
  73. for ( (i) = 0; (i) < (size); (i)++) \
  74. ((UByteP)(A))[(i)] = ((UByteP)(B))[(i)] | ((UByteP)(C))[(i)]
  75. #define BitIntersection(A,B,C,size,i) /* A = B intersection C */ \
  76. for ( (i) = 0; (i) < (size); (i)++) \
  77. ((UByteP)(A))[(i)] = ((UByteP)(B))[(i)] & ((UByteP)(C))[(i)]
  78. #define BitDifference(A,B,C,size,i) /* A = B - C, or B intersection !C */ \
  79. for ( (i) = 0; (i) < (size); (i)++) \
  80. ((UByteP)(A))[(i)] = ((UByteP)(B))[(i)] & ~((UByteP)(C))[(i)]
  81. #define BitEmpty(v,A,size,i) /* if A is empty, v = 0 */ \
  82. (v) = 0; \
  83. for ( (i) = 0; (i) < (size); (i)++) \
  84. (v) += ((UByteP)(A))[(i)] | 0
  85. #define BitZero(A,size) /* A = 0 */ \
  86. memset ((A), 0, (size) * sizeof(unsigned char))
  87. /*
  88. for ( (i) = 0; (i) < (size); (i)++) \
  89. ((UByteP)(A))[(i)] = 0
  90. */
  91. #define BitOne(A,size) /* A */ \
  92. memset ((A), '\xff', (size) * sizeof(unsigned char))
  93. #define BitSingleton(A,a,size) /* A = {a} */ \
  94. BitZero((A),(size)); \
  95. BitTrue((A),(a))
  96. #define BitNumberTrue(v,A,size,i) /* Returns the number of 1's */ \
  97. (v) = 0; \
  98. for ((i) = 0; (i) < (size)*8; (i)++) \
  99. { \
  100. if ((BitIsTrue((A), (i)))) \
  101. { \
  102. (v) = (v) + 1; \
  103. } \
  104. }
  105. #define BitNTrues(v,A,size,i) /* Returns the number of 1's */ \
  106. (v) = 0; \
  107. for ((i) = 0; (i) < (size)*8; (i)++) \
  108. { \
  109. if ((BitIsTrue((A), (i)))) \
  110. { \
  111. (v) = (v) + 1; \
  112. } \
  113. }
  114. /*
  115. (v) = 0; \
  116. for ((i) = 0; (i) < (size); (i)++) \
  117. { \
  118. (v) += BITNUMBERTRUE[((UByteP)(A))[(i)]]; \
  119. }
  120. */
  121. /*
  122. (v) = 0; \
  123. for ((i) = 0; (i) < (size)*8; (i)++) \
  124. { \
  125. if ((BitIsTrue((A), (i)))) \
  126. { \
  127. (v) = (v) + 1; \
  128. } \
  129. }
  130. */
  131. #define Bit1s(v,a,A,size,i) \
  132. (v) = 0; \
  133. for ((i) = 0; (i) < (size)*8; (i)++) \
  134. { \
  135. (a)[(i)] = -1; \
  136. if ((BitIsTrue((A), (i)))) \
  137. { \
  138. (a)[(v)] = (i); \
  139. (v) += 1; \
  140. } \
  141. }
  142. /* v = 1 if N(A) is less than N(B). 0 otherwise. */
  143. #define BitIsSmaller(v,A,B,size,i,a,b) /* N(A) < N(B) */ \
  144. BitNumberTrue((a),(A),(size),(i)); \
  145. BitNumberTrue((b),(B),(size),(i)); \
  146. (v) = 0; \
  147. if ((a) < (b)) \
  148. { \
  149. (v) = 1; \
  150. }
  151. /* v = 1 if A is a subset of B. 0 otherwise. */
  152. /* A - B = empty */
  153. #define BitIsSubset(v,A,B,X,size,i) /* A is a subset of B */ \
  154. BitDifference((X),(A),(B),(size),(i)); \
  155. BitNTrues((v),(X),(size),(i)); \
  156. if ((v) == 0) \
  157. { \
  158. (v) = 1; \
  159. } \
  160. else \
  161. { \
  162. (v) = 0; \
  163. }
  164. /*
  165. BitDifference((C),(A),(B),(size),(i)); \
  166. BitNumberTrue((v),(C),(size),(i)); \
  167. if ((v) == 0) \
  168. { \
  169. (v) = 1; \
  170. } \
  171. else \
  172. { \
  173. (v) = 0; \
  174. }
  175. */
  176. /* v = 1 if A is a proper subset of B. 0 otherwise. */
  177. #define BitIsProperSubset(v,A,B,X,size,i,a,b) /* A is a subset of B */ \
  178. BitIsSmaller ((v),(A),(B),(size),(i),(a),(b)); \
  179. if ((v) == 1) \
  180. { \
  181. BitIsSubset ((v),(A),(B),(X),(size),(i)); \
  182. }
  183. /* 0010 < 0110: 0 0 -> 0, 1 1 -> 0, 0 1 -> 0, 1 0 -> 1 */
  184. /* 1101 < 0110: 1 0 -> 0, 0 1 -> 0, 1 1 -> 0, 0 0 -> 1 */
  185. /* A < B == ~(~A or B) and N(A) < N(B) */
  186. /* 0010 1101 */
  187. /* ---- -or- */
  188. /* 0110 0110 */
  189. /* 1111 */
  190. #define BitPrint(A,size,i) /* print true bit */ \
  191. for ((i) = 0; (i) < (size)*8; (i)++) \
  192. { \
  193. if ((BitIsTrue((A), (i)))) \
  194. { \
  195. fprintf (stdout, "%d ", (i)); \
  196. } \
  197. }
  198. //fprintf (stderr, "\n");
  199. //printf ("\n");
  200. #define BitBitPrint(A,size,i) /* print true bit */ \
  201. for ((i) = 0; (i) < (size)*8; (i)++) \
  202. { \
  203. printf("%d", (i)%10); \
  204. } \
  205. printf ("\n"); \
  206. for ((i) = 0; (i) < (size)*8; (i)++) \
  207. { \
  208. if ((BitIsTrue((A), (i)))) \
  209. { \
  210. printf ("1"); \
  211. } \
  212. else \
  213. { \
  214. printf ("0"); \
  215. } \
  216. } \
  217. printf ("\n");
  218. #define BitSetNew(A,size) /* */ \
  219. (A) = (UByteP) malloc ((size) * sizeof(unsigned char)); \
  220. memset ((A), 0, (size) * sizeof(unsigned char))
  221. #define BitSetDelete(A) /* */ \
  222. free ((A)); \
  223. (A) = NULL
  224. #define BitSetZero(A,size) /* A = 0 */ \
  225. memset ((A), 0, (size) * sizeof(unsigned char))
  226. /**
  227. * A: a power set
  228. * size: set element size
  229. * n: a power set size
  230. * i: dummy variable
  231. */
  232. #define BitPowerSetNew(A,size,n,i) /* */ \
  233. (A) = (UByteP *) malloc ((n) * sizeof(UByteP)); \
  234. for ( (i) = 0; (i) < (n); (i)++) \
  235. { \
  236. (A)[(i)] = (UByteP) malloc ((size) * sizeof(unsigned char)); \
  237. memset ((A)[(i)], 0, (size) * sizeof(unsigned char)); \
  238. }
  239. #define BitPowerSetZero(A,size,n,i) /* */ \
  240. for ( (i) = 0; (i) < (n); (i)++) \
  241. { \
  242. memset ((A)[(i)], 0, (size) * sizeof(unsigned char)); \
  243. }
  244. /**
  245. * A: a power set
  246. * n: a power set size
  247. * i: dummy variable
  248. */
  249. #define BitPowerSetDelete(A,n,i) /* */ \
  250. for ( (i) = 0; (i) < (n); (i)++) \
  251. { \
  252. free ((A)[(i)]); \
  253. (A)[(i)] = NULL; \
  254. } \
  255. free ((A)); \
  256. (A) = NULL;
  257. /**
  258. * A: a set of power sets
  259. * size: set element size
  260. * n: the size of the set of power sets
  261. * m: a power set size
  262. * i: dummy variable
  263. * j: dummy variable
  264. */
  265. #define BitSetPowerSetNew(A,size,n,m,i,j) /* */ \
  266. (A) = (UByteP **) malloc ((n) * sizeof(UByteP *)); \
  267. for ( (i) = 0; (i) < (n); (i)++) \
  268. { \
  269. (A)[(i)] = (UByteP *) malloc ((m) * sizeof(UByteP)); \
  270. for ( (j) = 0; (j) < (m); (j)++) \
  271. { \
  272. (A)[(i)][(j)] = (UByteP) malloc ((size) * sizeof(unsigned char)); \
  273. memset ((A)[(i)][(j)], 0, (size) * sizeof(unsigned char)); \
  274. } \
  275. }
  276. #define BitSetPowerSetZero(A,size,n,m,i,j) /* */ \
  277. for ( (i) = 0; (i) < (n); (i)++) \
  278. { \
  279. for ( (j) = 0; (j) < (m); (j)++) \
  280. { \
  281. memset ((A)[(i)][(j)], 0, (size) * sizeof(unsigned char)); \
  282. } \
  283. }
  284. /**
  285. * A: a set of power sets
  286. * n: the size of the set of power sets
  287. * m: a power set size
  288. * i: dummy variable
  289. * j: dummy variable
  290. */
  291. #define BitSetPowerSetDelete(A,n,m,i,j) /* */ \
  292. for ( (i) = 0; (i) < (n); (i)++) \
  293. { \
  294. for ( (j) = 0; (j) < (m); (j)++) \
  295. { \
  296. free ((A)[(i)][(j)]); \
  297. (A)[(i)][(j)] = NULL; \
  298. } \
  299. free ((A)[(i)]); \
  300. (A)[(i)] = NULL; \
  301. } \
  302. free ((A)); \
  303. (A) = NULL;
  304. #define ArrayCopy(A,B,n,i) \
  305. for ( (i) = 0; (i) < (n); (i)++) \
  306. { \
  307. (A)[(i)] = (B)[(i)]; \
  308. }
  309. #endif /* __XTRAPBITS__ */