You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1467 lines
44 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddEssent.c]
  3. PackageName [cudd]
  4. Synopsis [Functions for the detection of essential variables.]
  5. Description [External procedures included in this file:
  6. <ul>
  7. <li> Cudd_FindEssential()
  8. <li> Cudd_bddIsVarEssential()
  9. <li> Cudd_FindTwoLiteralClauses()
  10. <li> Cudd_ReadIthClause()
  11. <li> Cudd_PrintTwoLiteralClauses()
  12. <li> Cudd_tlcInfoFree()
  13. </ul>
  14. Static procedures included in this module:
  15. <ul>
  16. <li> ddFindEssentialRecur()
  17. <li> ddFindTwoLiteralClausesRecur()
  18. <li> computeClauses()
  19. <li> computeClausesWithUniverse()
  20. <li> emptyClauseSet()
  21. <li> sentinelp()
  22. <li> equalp()
  23. <li> beforep()
  24. <li> oneliteralp()
  25. <li> impliedp()
  26. <li> bitVectorAlloc()
  27. <li> bitVectorClear()
  28. <li> bitVectorFree()
  29. <li> bitVectorRead()
  30. <li> bitVectorSet()
  31. <li> tlcInfoAlloc()
  32. </ul>]
  33. Author [Fabio Somenzi]
  34. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  35. All rights reserved.
  36. Redistribution and use in source and binary forms, with or without
  37. modification, are permitted provided that the following conditions
  38. are met:
  39. Redistributions of source code must retain the above copyright
  40. notice, this list of conditions and the following disclaimer.
  41. Redistributions in binary form must reproduce the above copyright
  42. notice, this list of conditions and the following disclaimer in the
  43. documentation and/or other materials provided with the distribution.
  44. Neither the name of the University of Colorado nor the names of its
  45. contributors may be used to endorse or promote products derived from
  46. this software without specific prior written permission.
  47. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  48. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  49. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  51. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  52. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  53. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  55. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  57. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  58. POSSIBILITY OF SUCH DAMAGE.]
  59. ******************************************************************************/
  60. #include "util.h"
  61. #include "cuddInt.h"
  62. /*---------------------------------------------------------------------------*/
  63. /* Constant declarations */
  64. /*---------------------------------------------------------------------------*/
  65. /* These definitions are for the bit vectors. */
  66. #if SIZEOF_LONG == 8
  67. #define BPL 64
  68. #define LOGBPL 6
  69. #else
  70. #define BPL 32
  71. #define LOGBPL 5
  72. #endif
  73. /*---------------------------------------------------------------------------*/
  74. /* Stucture declarations */
  75. /*---------------------------------------------------------------------------*/
  76. /* This structure holds the set of clauses for a node. Each clause consists
  77. ** of two literals. For one-literal clauses, the second lietral is FALSE.
  78. ** Each literal is composed of a variable and a phase. A variable is a node
  79. ** index, and requires sizeof(DdHalfWord) bytes. The constant literals use
  80. ** CUDD_MAXINDEX as variable indicator. Each phase is a bit: 0 for positive
  81. ** phase, and 1 for negative phase.
  82. ** Variables and phases are stored separately for the sake of compactness.
  83. ** The variables are stored in an array of DdHalfWord's terminated by a
  84. ** sentinel (a pair of zeroes). The phases are stored in a bit vector.
  85. ** The cnt field holds, at the end, the number of clauses.
  86. ** The clauses of the set are kept sorted. For each clause, the first literal
  87. ** is the one of least index. So, the clause with literals +2 and -4 is stored
  88. ** as (+2,-4). A one-literal clause with literal +3 is stored as
  89. ** (+3,-CUDD_MAXINDEX). Clauses are sorted in decreasing order as follows:
  90. ** (+5,-7)
  91. ** (+5,+6)
  92. ** (-5,+7)
  93. ** (-4,FALSE)
  94. ** (-4,+8)
  95. ** ...
  96. ** That is, one first looks at the variable of the first literal, then at the
  97. ** phase of the first litral, then at the variable of the second literal,
  98. ** and finally at the phase of the second literal.
  99. */
  100. struct DdTlcInfo {
  101. DdHalfWord *vars;
  102. long *phases;
  103. DdHalfWord cnt;
  104. };
  105. /* This structure is for temporary representation of sets of clauses. It is
  106. ** meant to be used in link lists, when the number of clauses is not yet
  107. ** known. The encoding of a clause is the same as in DdTlcInfo, though
  108. ** the phase information is not stored in a bit array. */
  109. struct TlClause {
  110. DdHalfWord v1, v2;
  111. short p1, p2;
  112. struct TlClause *next;
  113. };
  114. /*---------------------------------------------------------------------------*/
  115. /* Type declarations */
  116. /*---------------------------------------------------------------------------*/
  117. typedef long BitVector;
  118. typedef struct TlClause TlClause;
  119. /*---------------------------------------------------------------------------*/
  120. /* Variable declarations */
  121. /*---------------------------------------------------------------------------*/
  122. #ifndef lint
  123. static char rcsid[] DD_UNUSED = "$Id: cuddEssent.c,v 1.25 2012/02/05 01:07:18 fabio Exp $";
  124. #endif
  125. static BitVector *Tolv;
  126. static BitVector *Tolp;
  127. static BitVector *Eolv;
  128. static BitVector *Eolp;
  129. /*---------------------------------------------------------------------------*/
  130. /* Macro declarations */
  131. /*---------------------------------------------------------------------------*/
  132. /**AutomaticStart*************************************************************/
  133. /*---------------------------------------------------------------------------*/
  134. /* Static function prototypes */
  135. /*---------------------------------------------------------------------------*/
  136. static DdNode * ddFindEssentialRecur (DdManager *dd, DdNode *f);
  137. static DdTlcInfo * ddFindTwoLiteralClausesRecur (DdManager * dd, DdNode * f, st_table *table);
  138. static DdTlcInfo * computeClauses (DdTlcInfo *Tres, DdTlcInfo *Eres, DdHalfWord label, int size);
  139. static DdTlcInfo * computeClausesWithUniverse (DdTlcInfo *Cres, DdHalfWord label, short phase);
  140. static DdTlcInfo * emptyClauseSet (void);
  141. static int sentinelp (DdHalfWord var1, DdHalfWord var2);
  142. static int equalp (DdHalfWord var1a, short phase1a, DdHalfWord var1b, short phase1b, DdHalfWord var2a, short phase2a, DdHalfWord var2b, short phase2b);
  143. static int beforep (DdHalfWord var1a, short phase1a, DdHalfWord var1b, short phase1b, DdHalfWord var2a, short phase2a, DdHalfWord var2b, short phase2b);
  144. static int oneliteralp (DdHalfWord var);
  145. static int impliedp (DdHalfWord var1, short phase1, DdHalfWord var2, short phase2, BitVector *olv, BitVector *olp);
  146. static BitVector * bitVectorAlloc (int size);
  147. DD_INLINE static void bitVectorClear (BitVector *vector, int size);
  148. static void bitVectorFree (BitVector *vector);
  149. DD_INLINE static short bitVectorRead (BitVector *vector, int i);
  150. DD_INLINE static void bitVectorSet (BitVector * vector, int i, short val);
  151. static DdTlcInfo * tlcInfoAlloc (void);
  152. /**AutomaticEnd***************************************************************/
  153. /*---------------------------------------------------------------------------*/
  154. /* Definition of exported functions */
  155. /*---------------------------------------------------------------------------*/
  156. /**Function********************************************************************
  157. Synopsis [Finds the essential variables of a DD.]
  158. Description [Returns the cube of the essential variables. A positive
  159. literal means that the variable must be set to 1 for the function to be
  160. 1. A negative literal means that the variable must be set to 0 for the
  161. function to be 1. Returns a pointer to the cube BDD if successful;
  162. NULL otherwise.]
  163. SideEffects [None]
  164. SeeAlso [Cudd_bddIsVarEssential]
  165. ******************************************************************************/
  166. DdNode *
  167. Cudd_FindEssential(
  168. DdManager * dd,
  169. DdNode * f)
  170. {
  171. DdNode *res;
  172. do {
  173. dd->reordered = 0;
  174. res = ddFindEssentialRecur(dd,f);
  175. } while (dd->reordered == 1);
  176. return(res);
  177. } /* end of Cudd_FindEssential */
  178. /**Function********************************************************************
  179. Synopsis [Determines whether a given variable is essential with a
  180. given phase in a BDD.]
  181. Description [Determines whether a given variable is essential with a
  182. given phase in a BDD. Uses Cudd_bddIteConstant. Returns 1 if phase == 1
  183. and f-->x_id, or if phase == 0 and f-->x_id'.]
  184. SideEffects [None]
  185. SeeAlso [Cudd_FindEssential]
  186. ******************************************************************************/
  187. int
  188. Cudd_bddIsVarEssential(
  189. DdManager * manager,
  190. DdNode * f,
  191. int id,
  192. int phase)
  193. {
  194. DdNode *var;
  195. int res;
  196. var = Cudd_bddIthVar(manager, id);
  197. var = Cudd_NotCond(var,phase == 0);
  198. res = Cudd_bddLeq(manager, f, var);
  199. return(res);
  200. } /* end of Cudd_bddIsVarEssential */
  201. /**Function********************************************************************
  202. Synopsis [Finds the two literal clauses of a DD.]
  203. Description [Returns the one- and two-literal clauses of a DD.
  204. Returns a pointer to the structure holding the clauses if
  205. successful; NULL otherwise. For a constant DD, the empty set of clauses
  206. is returned. This is obviously correct for a non-zero constant. For the
  207. constant zero, it is based on the assumption that only those clauses
  208. containing variables in the support of the function are considered. Since
  209. the support of a constant function is empty, no clauses are returned.]
  210. SideEffects [None]
  211. SeeAlso [Cudd_FindEssential]
  212. ******************************************************************************/
  213. DdTlcInfo *
  214. Cudd_FindTwoLiteralClauses(
  215. DdManager * dd,
  216. DdNode * f)
  217. {
  218. DdTlcInfo *res;
  219. st_table *table;
  220. st_generator *gen;
  221. DdTlcInfo *tlc;
  222. DdNode *node;
  223. int size = dd->size;
  224. if (Cudd_IsConstant(f)) {
  225. res = emptyClauseSet();
  226. return(res);
  227. }
  228. table = st_init_table(st_ptrcmp,st_ptrhash);
  229. if (table == NULL) return(NULL);
  230. Tolv = bitVectorAlloc(size);
  231. if (Tolv == NULL) {
  232. st_free_table(table);
  233. return(NULL);
  234. }
  235. Tolp = bitVectorAlloc(size);
  236. if (Tolp == NULL) {
  237. st_free_table(table);
  238. bitVectorFree(Tolv);
  239. return(NULL);
  240. }
  241. Eolv = bitVectorAlloc(size);
  242. if (Eolv == NULL) {
  243. st_free_table(table);
  244. bitVectorFree(Tolv);
  245. bitVectorFree(Tolp);
  246. return(NULL);
  247. }
  248. Eolp = bitVectorAlloc(size);
  249. if (Eolp == NULL) {
  250. st_free_table(table);
  251. bitVectorFree(Tolv);
  252. bitVectorFree(Tolp);
  253. bitVectorFree(Eolv);
  254. return(NULL);
  255. }
  256. res = ddFindTwoLiteralClausesRecur(dd,f,table);
  257. /* Dispose of table contents and free table. */
  258. st_foreach_item(table, gen, &node, &tlc) {
  259. if (node != f) {
  260. Cudd_tlcInfoFree(tlc);
  261. }
  262. }
  263. st_free_table(table);
  264. bitVectorFree(Tolv);
  265. bitVectorFree(Tolp);
  266. bitVectorFree(Eolv);
  267. bitVectorFree(Eolp);
  268. if (res != NULL) {
  269. int i;
  270. for (i = 0; !sentinelp(res->vars[i], res->vars[i+1]); i += 2);
  271. res->cnt = i >> 1;
  272. }
  273. return(res);
  274. } /* end of Cudd_FindTwoLiteralClauses */
  275. /**Function********************************************************************
  276. Synopsis [Accesses the i-th clause of a DD.]
  277. Description [Accesses the i-th clause of a DD given the clause set which
  278. must be already computed. Returns 1 if successful; 0 if i is out of range,
  279. or in case of error.]
  280. SideEffects [the four components of a clause are returned as side effects.]
  281. SeeAlso [Cudd_FindTwoLiteralClauses]
  282. ******************************************************************************/
  283. int
  284. Cudd_ReadIthClause(
  285. DdTlcInfo * tlc,
  286. int i,
  287. DdHalfWord *var1,
  288. DdHalfWord *var2,
  289. int *phase1,
  290. int *phase2)
  291. {
  292. if (tlc == NULL) return(0);
  293. if (tlc->vars == NULL || tlc->phases == NULL) return(0);
  294. if (i < 0 || (unsigned) i >= tlc->cnt) return(0);
  295. *var1 = tlc->vars[2*i];
  296. *var2 = tlc->vars[2*i+1];
  297. *phase1 = (int) bitVectorRead(tlc->phases, 2*i);
  298. *phase2 = (int) bitVectorRead(tlc->phases, 2*i+1);
  299. return(1);
  300. } /* end of Cudd_ReadIthClause */
  301. /**Function********************************************************************
  302. Synopsis [Prints the two literal clauses of a DD.]
  303. Description [Prints the one- and two-literal clauses. Returns 1 if
  304. successful; 0 otherwise. The argument "names" can be NULL, in which case
  305. the variable indices are printed.]
  306. SideEffects [None]
  307. SeeAlso [Cudd_FindTwoLiteralClauses]
  308. ******************************************************************************/
  309. int
  310. Cudd_PrintTwoLiteralClauses(
  311. DdManager * dd,
  312. DdNode * f,
  313. char **names,
  314. FILE *fp)
  315. {
  316. DdHalfWord *vars;
  317. BitVector *phases;
  318. int i;
  319. DdTlcInfo *res = Cudd_FindTwoLiteralClauses(dd, f);
  320. FILE *ifp = fp == NULL ? dd->out : fp;
  321. if (res == NULL) return(0);
  322. vars = res->vars;
  323. phases = res->phases;
  324. for (i = 0; !sentinelp(vars[i], vars[i+1]); i += 2) {
  325. if (names != NULL) {
  326. if (vars[i+1] == CUDD_MAXINDEX) {
  327. (void) fprintf(ifp, "%s%s\n",
  328. bitVectorRead(phases, i) ? "~" : " ",
  329. names[vars[i]]);
  330. } else {
  331. (void) fprintf(ifp, "%s%s | %s%s\n",
  332. bitVectorRead(phases, i) ? "~" : " ",
  333. names[vars[i]],
  334. bitVectorRead(phases, i+1) ? "~" : " ",
  335. names[vars[i+1]]);
  336. }
  337. } else {
  338. if (vars[i+1] == CUDD_MAXINDEX) {
  339. (void) fprintf(ifp, "%s%d\n",
  340. bitVectorRead(phases, i) ? "~" : " ",
  341. (int) vars[i]);
  342. } else {
  343. (void) fprintf(ifp, "%s%d | %s%d\n",
  344. bitVectorRead(phases, i) ? "~" : " ",
  345. (int) vars[i],
  346. bitVectorRead(phases, i+1) ? "~" : " ",
  347. (int) vars[i+1]);
  348. }
  349. }
  350. }
  351. Cudd_tlcInfoFree(res);
  352. return(1);
  353. } /* end of Cudd_PrintTwoLiteralClauses */
  354. /**Function********************************************************************
  355. Synopsis [Frees a DdTlcInfo Structure.]
  356. Description [Frees a DdTlcInfo Structure as well as the memory pointed
  357. by it.]
  358. SideEffects [None]
  359. SeeAlso []
  360. ******************************************************************************/
  361. void
  362. Cudd_tlcInfoFree(
  363. DdTlcInfo * t)
  364. {
  365. if (t->vars != NULL) FREE(t->vars);
  366. if (t->phases != NULL) FREE(t->phases);
  367. FREE(t);
  368. } /* end of Cudd_tlcInfoFree */
  369. /*---------------------------------------------------------------------------*/
  370. /* Definition of internal functions */
  371. /*---------------------------------------------------------------------------*/
  372. /*---------------------------------------------------------------------------*/
  373. /* Definition of static functions */
  374. /*---------------------------------------------------------------------------*/
  375. /**Function********************************************************************
  376. Synopsis [Implements the recursive step of Cudd_FindEssential.]
  377. Description [Implements the recursive step of Cudd_FindEssential.
  378. Returns a pointer to the cube BDD if successful; NULL otherwise.]
  379. SideEffects [None]
  380. ******************************************************************************/
  381. static DdNode *
  382. ddFindEssentialRecur(
  383. DdManager * dd,
  384. DdNode * f)
  385. {
  386. DdNode *T, *E, *F;
  387. DdNode *essT, *essE, *res;
  388. int index;
  389. DdNode *one, *lzero, *azero;
  390. one = DD_ONE(dd);
  391. F = Cudd_Regular(f);
  392. /* If f is constant the set of essential variables is empty. */
  393. if (cuddIsConstant(F)) return(one);
  394. res = cuddCacheLookup1(dd,Cudd_FindEssential,f);
  395. if (res != NULL) {
  396. return(res);
  397. }
  398. lzero = Cudd_Not(one);
  399. azero = DD_ZERO(dd);
  400. /* Find cofactors: here f is non-constant. */
  401. T = cuddT(F);
  402. E = cuddE(F);
  403. if (Cudd_IsComplement(f)) {
  404. T = Cudd_Not(T); E = Cudd_Not(E);
  405. }
  406. index = F->index;
  407. if (Cudd_IsConstant(T) && T != lzero && T != azero) {
  408. /* if E is zero, index is essential, otherwise there are no
  409. ** essentials, because index is not essential and no other variable
  410. ** can be, since setting index = 1 makes the function constant and
  411. ** different from 0.
  412. */
  413. if (E == lzero || E == azero) {
  414. res = dd->vars[index];
  415. } else {
  416. res = one;
  417. }
  418. } else if (T == lzero || T == azero) {
  419. if (Cudd_IsConstant(E)) { /* E cannot be zero here */
  420. res = Cudd_Not(dd->vars[index]);
  421. } else { /* E == non-constant */
  422. /* find essentials in the else branch */
  423. essE = ddFindEssentialRecur(dd,E);
  424. if (essE == NULL) {
  425. return(NULL);
  426. }
  427. cuddRef(essE);
  428. /* add index to the set with negative phase */
  429. res = cuddUniqueInter(dd,index,one,Cudd_Not(essE));
  430. if (res == NULL) {
  431. Cudd_RecursiveDeref(dd,essE);
  432. return(NULL);
  433. }
  434. res = Cudd_Not(res);
  435. cuddDeref(essE);
  436. }
  437. } else { /* T == non-const */
  438. if (E == lzero || E == azero) {
  439. /* find essentials in the then branch */
  440. essT = ddFindEssentialRecur(dd,T);
  441. if (essT == NULL) {
  442. return(NULL);
  443. }
  444. cuddRef(essT);
  445. /* add index to the set with positive phase */
  446. /* use And because essT may be complemented */
  447. res = cuddBddAndRecur(dd,dd->vars[index],essT);
  448. if (res == NULL) {
  449. Cudd_RecursiveDeref(dd,essT);
  450. return(NULL);
  451. }
  452. cuddDeref(essT);
  453. } else if (!Cudd_IsConstant(E)) {
  454. /* if E is a non-zero constant there are no essentials
  455. ** because T is non-constant.
  456. */
  457. essT = ddFindEssentialRecur(dd,T);
  458. if (essT == NULL) {
  459. return(NULL);
  460. }
  461. if (essT == one) {
  462. res = one;
  463. } else {
  464. cuddRef(essT);
  465. essE = ddFindEssentialRecur(dd,E);
  466. if (essE == NULL) {
  467. Cudd_RecursiveDeref(dd,essT);
  468. return(NULL);
  469. }
  470. cuddRef(essE);
  471. /* res = intersection(essT, essE) */
  472. res = cuddBddLiteralSetIntersectionRecur(dd,essT,essE);
  473. if (res == NULL) {
  474. Cudd_RecursiveDeref(dd,essT);
  475. Cudd_RecursiveDeref(dd,essE);
  476. return(NULL);
  477. }
  478. cuddRef(res);
  479. Cudd_RecursiveDeref(dd,essT);
  480. Cudd_RecursiveDeref(dd,essE);
  481. cuddDeref(res);
  482. }
  483. } else { /* E is a non-zero constant */
  484. res = one;
  485. }
  486. }
  487. cuddCacheInsert1(dd,Cudd_FindEssential, f, res);
  488. return(res);
  489. } /* end of ddFindEssentialRecur */
  490. /**Function********************************************************************
  491. Synopsis [Implements the recursive step of Cudd_FindTwoLiteralClauses.]
  492. Description [Implements the recursive step of
  493. Cudd_FindTwoLiteralClauses. The DD node is assumed to be not
  494. constant. Returns a pointer to a set of clauses if successful; NULL
  495. otherwise.]
  496. SideEffects [None]
  497. SeeAlso [Cudd_FindTwoLiteralClauses]
  498. ******************************************************************************/
  499. static DdTlcInfo *
  500. ddFindTwoLiteralClausesRecur(
  501. DdManager * dd,
  502. DdNode * f,
  503. st_table *table)
  504. {
  505. DdNode *T, *E, *F;
  506. DdNode *one, *lzero, *azero;
  507. DdTlcInfo *res, *Tres, *Eres;
  508. DdHalfWord index;
  509. F = Cudd_Regular(f);
  510. assert(!cuddIsConstant(F));
  511. /* Check computed table. Separate entries are necessary for
  512. ** a node and its complement. We should update the counter here. */
  513. if (st_lookup(table, f, &res)) {
  514. return(res);
  515. }
  516. /* Easy access to the constants for BDDs and ADDs. */
  517. one = DD_ONE(dd);
  518. lzero = Cudd_Not(one);
  519. azero = DD_ZERO(dd);
  520. /* Find cofactors and variable labeling the top node. */
  521. T = cuddT(F); E = cuddE(F);
  522. if (Cudd_IsComplement(f)) {
  523. T = Cudd_Not(T); E = Cudd_Not(E);
  524. }
  525. index = F->index;
  526. if (Cudd_IsConstant(T) && T != lzero && T != azero) {
  527. /* T is a non-zero constant. If E is zero, then this node's index
  528. ** is a one-literal clause. Otherwise, if E is a non-zero
  529. ** constant, there are no clauses for this node. Finally,
  530. ** if E is not constant, we recursively compute its clauses, and then
  531. ** merge using the empty set for T. */
  532. if (E == lzero || E == azero) {
  533. /* Create the clause (index + 0). */
  534. res = tlcInfoAlloc();
  535. if (res == NULL) return(NULL);
  536. res->vars = ALLOC(DdHalfWord,4);
  537. if (res->vars == NULL) {
  538. FREE(res);
  539. return(NULL);
  540. }
  541. res->phases = bitVectorAlloc(2);
  542. if (res->phases == NULL) {
  543. FREE(res->vars);
  544. FREE(res);
  545. return(NULL);
  546. }
  547. res->vars[0] = index;
  548. res->vars[1] = CUDD_MAXINDEX;
  549. res->vars[2] = 0;
  550. res->vars[3] = 0;
  551. bitVectorSet(res->phases, 0, 0); /* positive phase */
  552. bitVectorSet(res->phases, 1, 1); /* negative phase */
  553. } else if (Cudd_IsConstant(E)) {
  554. /* If E is a non-zero constant, no clauses. */
  555. res = emptyClauseSet();
  556. } else {
  557. /* E is non-constant */
  558. Tres = emptyClauseSet();
  559. if (Tres == NULL) return(NULL);
  560. Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
  561. if (Eres == NULL) {
  562. Cudd_tlcInfoFree(Tres);
  563. return(NULL);
  564. }
  565. res = computeClauses(Tres, Eres, index, dd->size);
  566. Cudd_tlcInfoFree(Tres);
  567. }
  568. } else if (T == lzero || T == azero) {
  569. /* T is zero. If E is a non-zero constant, then the
  570. ** complement of this node's index is a one-literal clause.
  571. ** Otherwise, if E is not constant, we recursively compute its
  572. ** clauses, and then merge using the universal set for T. */
  573. if (Cudd_IsConstant(E)) { /* E cannot be zero here */
  574. /* Create the clause (!index + 0). */
  575. res = tlcInfoAlloc();
  576. if (res == NULL) return(NULL);
  577. res->vars = ALLOC(DdHalfWord,4);
  578. if (res->vars == NULL) {
  579. FREE(res);
  580. return(NULL);
  581. }
  582. res->phases = bitVectorAlloc(2);
  583. if (res->phases == NULL) {
  584. FREE(res->vars);
  585. FREE(res);
  586. return(NULL);
  587. }
  588. res->vars[0] = index;
  589. res->vars[1] = CUDD_MAXINDEX;
  590. res->vars[2] = 0;
  591. res->vars[3] = 0;
  592. bitVectorSet(res->phases, 0, 1); /* negative phase */
  593. bitVectorSet(res->phases, 1, 1); /* negative phase */
  594. } else { /* E == non-constant */
  595. Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
  596. if (Eres == NULL) return(NULL);
  597. res = computeClausesWithUniverse(Eres, index, 1);
  598. }
  599. } else { /* T == non-const */
  600. Tres = ddFindTwoLiteralClausesRecur(dd, T, table);
  601. if (Tres == NULL) return(NULL);
  602. if (Cudd_IsConstant(E)) {
  603. if (E == lzero || E == azero) {
  604. res = computeClausesWithUniverse(Tres, index, 0);
  605. } else {
  606. Eres = emptyClauseSet();
  607. if (Eres == NULL) return(NULL);
  608. res = computeClauses(Tres, Eres, index, dd->size);
  609. Cudd_tlcInfoFree(Eres);
  610. }
  611. } else {
  612. Eres = ddFindTwoLiteralClausesRecur(dd, E, table);
  613. if (Eres == NULL) return(NULL);
  614. res = computeClauses(Tres, Eres, index, dd->size);
  615. }
  616. }
  617. /* Cache results. */
  618. if (st_add_direct(table, (char *)f, (char *)res) == ST_OUT_OF_MEM) {
  619. FREE(res);
  620. return(NULL);
  621. }
  622. return(res);
  623. } /* end of ddFindTwoLiteralClausesRecur */
  624. /**Function********************************************************************
  625. Synopsis [Computes the two-literal clauses for a node.]
  626. Description [Computes the two-literal clauses for a node given the
  627. clauses for its children and the label of the node. Returns a
  628. pointer to a TclInfo structure if successful; NULL otherwise.]
  629. SideEffects [None]
  630. SeeAlso [computeClausesWithUniverse]
  631. ******************************************************************************/
  632. static DdTlcInfo *
  633. computeClauses(
  634. DdTlcInfo *Tres /* list of clauses for T child */,
  635. DdTlcInfo *Eres /* list of clauses for E child */,
  636. DdHalfWord label /* variable labeling the current node */,
  637. int size /* number of variables in the manager */)
  638. {
  639. DdHalfWord *Tcv = Tres->vars; /* variables of clauses for the T child */
  640. BitVector *Tcp = Tres->phases; /* phases of clauses for the T child */
  641. DdHalfWord *Ecv = Eres->vars; /* variables of clauses for the E child */
  642. BitVector *Ecp = Eres->phases; /* phases of clauses for the E child */
  643. DdHalfWord *Vcv = NULL; /* pointer to variables of the clauses for v */
  644. BitVector *Vcp = NULL; /* pointer to phases of the clauses for v */
  645. DdTlcInfo *res = NULL; /* the set of clauses to be returned */
  646. int pt = 0; /* index in the list of clauses of T */
  647. int pe = 0; /* index in the list of clauses of E */
  648. int cv = 0; /* counter of the clauses for this node */
  649. TlClause *iclauses = NULL; /* list of inherited clauses */
  650. TlClause *tclauses = NULL; /* list of 1-literal clauses of T */
  651. TlClause *eclauses = NULL; /* list of 1-literal clauses of E */
  652. TlClause *nclauses = NULL; /* list of new (non-inherited) clauses */
  653. TlClause *lnclause = NULL; /* pointer to last new clause */
  654. TlClause *newclause; /* temporary pointer to new clauses */
  655. /* Initialize sets of one-literal clauses. The one-literal clauses
  656. ** are stored redundantly. These sets allow constant-time lookup, which
  657. ** we need when we check for implication of a two-literal clause by a
  658. ** one-literal clause. The linked lists allow fast sequential
  659. ** processing. */
  660. bitVectorClear(Tolv, size);
  661. bitVectorClear(Tolp, size);
  662. bitVectorClear(Eolv, size);
  663. bitVectorClear(Eolp, size);
  664. /* Initialize result structure. */
  665. res = tlcInfoAlloc();
  666. if (res == NULL) goto cleanup;
  667. /* Scan the two input list. Extract inherited two-literal clauses
  668. ** and set aside one-literal clauses from each list. The incoming lists
  669. ** are sorted in the order defined by beforep. The three linked list
  670. ** produced by this loop are sorted in the reverse order because we
  671. ** always append to the front of the lists.
  672. ** The inherited clauses are those clauses (both one- and two-literal)
  673. ** that are common to both children; and the two-literal clauses of
  674. ** one child that are implied by a one-literal clause of the other
  675. ** child. */
  676. while (!sentinelp(Tcv[pt], Tcv[pt+1]) || !sentinelp(Ecv[pe], Ecv[pe+1])) {
  677. if (equalp(Tcv[pt], bitVectorRead(Tcp, pt),
  678. Tcv[pt+1], bitVectorRead(Tcp, pt+1),
  679. Ecv[pe], bitVectorRead(Ecp, pe),
  680. Ecv[pe+1], bitVectorRead(Ecp, pe+1))) {
  681. /* Add clause to inherited list. */
  682. newclause = ALLOC(TlClause,1);
  683. if (newclause == NULL) goto cleanup;
  684. newclause->v1 = Tcv[pt];
  685. newclause->v2 = Tcv[pt+1];
  686. newclause->p1 = bitVectorRead(Tcp, pt);
  687. newclause->p2 = bitVectorRead(Tcp, pt+1);
  688. newclause->next = iclauses;
  689. iclauses = newclause;
  690. pt += 2; pe += 2; cv++;
  691. } else if (beforep(Tcv[pt], bitVectorRead(Tcp, pt),
  692. Tcv[pt+1], bitVectorRead(Tcp, pt+1),
  693. Ecv[pe], bitVectorRead(Ecp, pe),
  694. Ecv[pe+1], bitVectorRead(Ecp, pe+1))) {
  695. if (oneliteralp(Tcv[pt+1])) {
  696. /* Add this one-literal clause to the T set. */
  697. newclause = ALLOC(TlClause,1);
  698. if (newclause == NULL) goto cleanup;
  699. newclause->v1 = Tcv[pt];
  700. newclause->v2 = CUDD_MAXINDEX;
  701. newclause->p1 = bitVectorRead(Tcp, pt);
  702. newclause->p2 = 1;
  703. newclause->next = tclauses;
  704. tclauses = newclause;
  705. bitVectorSet(Tolv, Tcv[pt], 1);
  706. bitVectorSet(Tolp, Tcv[pt], bitVectorRead(Tcp, pt));
  707. } else {
  708. if (impliedp(Tcv[pt], bitVectorRead(Tcp, pt),
  709. Tcv[pt+1], bitVectorRead(Tcp, pt+1),
  710. Eolv, Eolp)) {
  711. /* Add clause to inherited list. */
  712. newclause = ALLOC(TlClause,1);
  713. if (newclause == NULL) goto cleanup;
  714. newclause->v1 = Tcv[pt];
  715. newclause->v2 = Tcv[pt+1];
  716. newclause->p1 = bitVectorRead(Tcp, pt);
  717. newclause->p2 = bitVectorRead(Tcp, pt+1);
  718. newclause->next = iclauses;
  719. iclauses = newclause;
  720. cv++;
  721. }
  722. }
  723. pt += 2;
  724. } else { /* !beforep() */
  725. if (oneliteralp(Ecv[pe+1])) {
  726. /* Add this one-literal clause to the E set. */
  727. newclause = ALLOC(TlClause,1);
  728. if (newclause == NULL) goto cleanup;
  729. newclause->v1 = Ecv[pe];
  730. newclause->v2 = CUDD_MAXINDEX;
  731. newclause->p1 = bitVectorRead(Ecp, pe);
  732. newclause->p2 = 1;
  733. newclause->next = eclauses;
  734. eclauses = newclause;
  735. bitVectorSet(Eolv, Ecv[pe], 1);
  736. bitVectorSet(Eolp, Ecv[pe], bitVectorRead(Ecp, pe));
  737. } else {
  738. if (impliedp(Ecv[pe], bitVectorRead(Ecp, pe),
  739. Ecv[pe+1], bitVectorRead(Ecp, pe+1),
  740. Tolv, Tolp)) {
  741. /* Add clause to inherited list. */
  742. newclause = ALLOC(TlClause,1);
  743. if (newclause == NULL) goto cleanup;
  744. newclause->v1 = Ecv[pe];
  745. newclause->v2 = Ecv[pe+1];
  746. newclause->p1 = bitVectorRead(Ecp, pe);
  747. newclause->p2 = bitVectorRead(Ecp, pe+1);
  748. newclause->next = iclauses;
  749. iclauses = newclause;
  750. cv++;
  751. }
  752. }
  753. pe += 2;
  754. }
  755. }
  756. /* Add one-literal clauses for the label variable to the front of
  757. ** the two lists. */
  758. newclause = ALLOC(TlClause,1);
  759. if (newclause == NULL) goto cleanup;
  760. newclause->v1 = label;
  761. newclause->v2 = CUDD_MAXINDEX;
  762. newclause->p1 = 0;
  763. newclause->p2 = 1;
  764. newclause->next = tclauses;
  765. tclauses = newclause;
  766. newclause = ALLOC(TlClause,1);
  767. if (newclause == NULL) goto cleanup;
  768. newclause->v1 = label;
  769. newclause->v2 = CUDD_MAXINDEX;
  770. newclause->p1 = 1;
  771. newclause->p2 = 1;
  772. newclause->next = eclauses;
  773. eclauses = newclause;
  774. /* Produce the non-inherited clauses. We preserve the "reverse"
  775. ** order of the two input lists by appending to the end of the
  776. ** list. In this way, iclauses and nclauses are consistent. */
  777. while (tclauses != NULL && eclauses != NULL) {
  778. if (beforep(eclauses->v1, eclauses->p1, eclauses->v2, eclauses->p2,
  779. tclauses->v1, tclauses->p1, tclauses->v2, tclauses->p2)) {
  780. TlClause *nextclause = tclauses->next;
  781. TlClause *otherclauses = eclauses;
  782. while (otherclauses != NULL) {
  783. if (tclauses->v1 != otherclauses->v1) {
  784. newclause = ALLOC(TlClause,1);
  785. if (newclause == NULL) goto cleanup;
  786. newclause->v1 = tclauses->v1;
  787. newclause->v2 = otherclauses->v1;
  788. newclause->p1 = tclauses->p1;
  789. newclause->p2 = otherclauses->p1;
  790. newclause->next = NULL;
  791. if (nclauses == NULL) {
  792. nclauses = newclause;
  793. lnclause = newclause;
  794. } else {
  795. lnclause->next = newclause;
  796. lnclause = newclause;
  797. }
  798. cv++;
  799. }
  800. otherclauses = otherclauses->next;
  801. }
  802. FREE(tclauses);
  803. tclauses = nextclause;
  804. } else {
  805. TlClause *nextclause = eclauses->next;
  806. TlClause *otherclauses = tclauses;
  807. while (otherclauses != NULL) {
  808. if (eclauses->v1 != otherclauses->v1) {
  809. newclause = ALLOC(TlClause,1);
  810. if (newclause == NULL) goto cleanup;
  811. newclause->v1 = eclauses->v1;
  812. newclause->v2 = otherclauses->v1;
  813. newclause->p1 = eclauses->p1;
  814. newclause->p2 = otherclauses->p1;
  815. newclause->next = NULL;
  816. if (nclauses == NULL) {
  817. nclauses = newclause;
  818. lnclause = newclause;
  819. } else {
  820. lnclause->next = newclause;
  821. lnclause = newclause;
  822. }
  823. cv++;
  824. }
  825. otherclauses = otherclauses->next;
  826. }
  827. FREE(eclauses);
  828. eclauses = nextclause;
  829. }
  830. }
  831. while (tclauses != NULL) {
  832. TlClause *nextclause = tclauses->next;
  833. FREE(tclauses);
  834. tclauses = nextclause;
  835. }
  836. while (eclauses != NULL) {
  837. TlClause *nextclause = eclauses->next;
  838. FREE(eclauses);
  839. eclauses = nextclause;
  840. }
  841. /* Merge inherited and non-inherited clauses. Now that we know the
  842. ** total number, we allocate the arrays, and we fill them bottom-up
  843. ** to restore the proper ordering. */
  844. Vcv = ALLOC(DdHalfWord, 2*(cv+1));
  845. if (Vcv == NULL) goto cleanup;
  846. if (cv > 0) {
  847. Vcp = bitVectorAlloc(2*cv);
  848. if (Vcp == NULL) goto cleanup;
  849. } else {
  850. Vcp = NULL;
  851. }
  852. res->vars = Vcv;
  853. res->phases = Vcp;
  854. /* Add sentinel. */
  855. Vcv[2*cv] = 0;
  856. Vcv[2*cv+1] = 0;
  857. while (iclauses != NULL || nclauses != NULL) {
  858. TlClause *nextclause;
  859. cv--;
  860. if (nclauses == NULL || (iclauses != NULL &&
  861. beforep(nclauses->v1, nclauses->p1, nclauses->v2, nclauses->p2,
  862. iclauses->v1, iclauses->p1, iclauses->v2, iclauses->p2))) {
  863. Vcv[2*cv] = iclauses->v1;
  864. Vcv[2*cv+1] = iclauses->v2;
  865. bitVectorSet(Vcp, 2*cv, iclauses->p1);
  866. bitVectorSet(Vcp, 2*cv+1, iclauses->p2);
  867. nextclause = iclauses->next;
  868. FREE(iclauses);
  869. iclauses = nextclause;
  870. } else {
  871. Vcv[2*cv] = nclauses->v1;
  872. Vcv[2*cv+1] = nclauses->v2;
  873. bitVectorSet(Vcp, 2*cv, nclauses->p1);
  874. bitVectorSet(Vcp, 2*cv+1, nclauses->p2);
  875. nextclause = nclauses->next;
  876. FREE(nclauses);
  877. nclauses = nextclause;
  878. }
  879. }
  880. assert(cv == 0);
  881. return(res);
  882. cleanup:
  883. if (res != NULL) Cudd_tlcInfoFree(res);
  884. while (iclauses != NULL) {
  885. TlClause *nextclause = iclauses->next;
  886. FREE(iclauses);
  887. iclauses = nextclause;
  888. }
  889. while (nclauses != NULL) {
  890. TlClause *nextclause = nclauses->next;
  891. FREE(nclauses);
  892. nclauses = nextclause;
  893. }
  894. while (tclauses != NULL) {
  895. TlClause *nextclause = tclauses->next;
  896. FREE(tclauses);
  897. tclauses = nextclause;
  898. }
  899. while (eclauses != NULL) {
  900. TlClause *nextclause = eclauses->next;
  901. FREE(eclauses);
  902. eclauses = nextclause;
  903. }
  904. return(NULL);
  905. } /* end of computeClauses */
  906. /**Function********************************************************************
  907. Synopsis [Computes the two-literal clauses for a node.]
  908. Description [Computes the two-literal clauses for a node with a zero
  909. child, given the clauses for its other child and the label of the
  910. node. Returns a pointer to a TclInfo structure if successful; NULL
  911. otherwise.]
  912. SideEffects [None]
  913. SeeAlso [computeClauses]
  914. ******************************************************************************/
  915. static DdTlcInfo *
  916. computeClausesWithUniverse(
  917. DdTlcInfo *Cres /* list of clauses for child */,
  918. DdHalfWord label /* variable labeling the current node */,
  919. short phase /* 0 if E child is zero; 1 if T child is zero */)
  920. {
  921. DdHalfWord *Ccv = Cres->vars; /* variables of clauses for child */
  922. BitVector *Ccp = Cres->phases; /* phases of clauses for child */
  923. DdHalfWord *Vcv = NULL; /* pointer to the variables of the clauses for v */
  924. BitVector *Vcp = NULL; /* pointer to the phases of the clauses for v */
  925. DdTlcInfo *res = NULL; /* the set of clauses to be returned */
  926. int i;
  927. /* Initialize result. */
  928. res = tlcInfoAlloc();
  929. if (res == NULL) goto cleanup;
  930. /* Count entries for new list and allocate accordingly. */
  931. for (i = 0; !sentinelp(Ccv[i], Ccv[i+1]); i += 2);
  932. /* At this point, i is twice the number of clauses in the child's
  933. ** list. We need four more entries for this node: 2 for the one-literal
  934. ** clause for the label, and 2 for the sentinel. */
  935. Vcv = ALLOC(DdHalfWord,i+4);
  936. if (Vcv == NULL) goto cleanup;
  937. Vcp = bitVectorAlloc(i+4);
  938. if (Vcp == NULL) goto cleanup;
  939. res->vars = Vcv;
  940. res->phases = Vcp;
  941. /* Copy old list into new. */
  942. for (i = 0; !sentinelp(Ccv[i], Ccv[i+1]); i += 2) {
  943. Vcv[i] = Ccv[i];
  944. Vcv[i+1] = Ccv[i+1];
  945. bitVectorSet(Vcp, i, bitVectorRead(Ccp, i));
  946. bitVectorSet(Vcp, i+1, bitVectorRead(Ccp, i+1));
  947. }
  948. /* Add clause corresponding to label. */
  949. Vcv[i] = label;
  950. bitVectorSet(Vcp, i, phase);
  951. i++;
  952. Vcv[i] = CUDD_MAXINDEX;
  953. bitVectorSet(Vcp, i, 1);
  954. i++;
  955. /* Add sentinel. */
  956. Vcv[i] = 0;
  957. Vcv[i+1] = 0;
  958. bitVectorSet(Vcp, i, 0);
  959. bitVectorSet(Vcp, i+1, 0);
  960. return(res);
  961. cleanup:
  962. /* Vcp is guaranteed to be NULL here. Hence, we do not try to free it. */
  963. if (Vcv != NULL) FREE(Vcv);
  964. if (res != NULL) Cudd_tlcInfoFree(res);
  965. return(NULL);
  966. } /* end of computeClausesWithUniverse */
  967. /**Function********************************************************************
  968. Synopsis [Returns an enpty set of clauses.]
  969. Description [Returns a pointer to an empty set of clauses if
  970. successful; NULL otherwise. No bit vector for the phases is
  971. allocated.]
  972. SideEffects [None]
  973. SeeAlso []
  974. ******************************************************************************/
  975. static DdTlcInfo *
  976. emptyClauseSet(void)
  977. {
  978. DdTlcInfo *eset;
  979. eset = ALLOC(DdTlcInfo,1);
  980. if (eset == NULL) return(NULL);
  981. eset->vars = ALLOC(DdHalfWord,2);
  982. if (eset->vars == NULL) {
  983. FREE(eset);
  984. return(NULL);
  985. }
  986. /* Sentinel */
  987. eset->vars[0] = 0;
  988. eset->vars[1] = 0;
  989. eset->phases = NULL; /* does not matter */
  990. eset->cnt = 0;
  991. return(eset);
  992. } /* end of emptyClauseSet */
  993. /**Function********************************************************************
  994. Synopsis [Returns true iff the argument is the sentinel clause.]
  995. Description [Returns true iff the argument is the sentinel clause.
  996. A sentinel clause has both variables equal to 0.]
  997. SideEffects [None]
  998. SeeAlso []
  999. ******************************************************************************/
  1000. static int
  1001. sentinelp(
  1002. DdHalfWord var1,
  1003. DdHalfWord var2)
  1004. {
  1005. return(var1 == 0 && var2 == 0);
  1006. } /* end of sentinelp */
  1007. /**Function********************************************************************
  1008. Synopsis [Returns true iff the two arguments are identical clauses.]
  1009. Description [Returns true iff the two arguments are identical
  1010. clauses. Since literals are sorted, we only need to compare
  1011. literals in the same position.]
  1012. SideEffects [None]
  1013. SeeAlso [beforep]
  1014. ******************************************************************************/
  1015. static int
  1016. equalp(
  1017. DdHalfWord var1a,
  1018. short phase1a,
  1019. DdHalfWord var1b,
  1020. short phase1b,
  1021. DdHalfWord var2a,
  1022. short phase2a,
  1023. DdHalfWord var2b,
  1024. short phase2b)
  1025. {
  1026. return(var1a == var2a && phase1a == phase2a &&
  1027. var1b == var2b && phase1b == phase2b);
  1028. } /* end of equalp */
  1029. /**Function********************************************************************
  1030. Synopsis [Returns true iff the first argument precedes the second in
  1031. the clause order.]
  1032. Description [Returns true iff the first argument precedes the second
  1033. in the clause order. A clause precedes another if its first lieral
  1034. precedes the first literal of the other, or if the first literals
  1035. are the same, and its second literal precedes the second literal of
  1036. the other clause. A literal precedes another if it has a higher
  1037. index, of if it has the same index, but it has lower phase. Phase 0
  1038. is the positive phase, and it is lower than Phase 1 (negative
  1039. phase).]
  1040. SideEffects [None]
  1041. SeeAlso [equalp]
  1042. ******************************************************************************/
  1043. static int
  1044. beforep(
  1045. DdHalfWord var1a,
  1046. short phase1a,
  1047. DdHalfWord var1b,
  1048. short phase1b,
  1049. DdHalfWord var2a,
  1050. short phase2a,
  1051. DdHalfWord var2b,
  1052. short phase2b)
  1053. {
  1054. return(var1a > var2a || (var1a == var2a &&
  1055. (phase1a < phase2a || (phase1a == phase2a &&
  1056. (var1b > var2b || (var1b == var2b && phase1b < phase2b))))));
  1057. } /* end of beforep */
  1058. /**Function********************************************************************
  1059. Synopsis [Returns true iff the argument is a one-literal clause.]
  1060. Description [Returns true iff the argument is a one-literal clause.
  1061. A one-litaral clause has the constant FALSE as second literal.
  1062. Since the constant TRUE is never used, it is sufficient to test for
  1063. a constant.]
  1064. SideEffects [None]
  1065. SeeAlso []
  1066. ******************************************************************************/
  1067. static int
  1068. oneliteralp(
  1069. DdHalfWord var)
  1070. {
  1071. return(var == CUDD_MAXINDEX);
  1072. } /* end of oneliteralp */
  1073. /**Function********************************************************************
  1074. Synopsis [Returns true iff either literal of a clause is in a set of
  1075. literals.]
  1076. Description [Returns true iff either literal of a clause is in a set
  1077. of literals. The first four arguments specify the clause. The
  1078. remaining two arguments specify the literal set.]
  1079. SideEffects [None]
  1080. SeeAlso []
  1081. ******************************************************************************/
  1082. static int
  1083. impliedp(
  1084. DdHalfWord var1,
  1085. short phase1,
  1086. DdHalfWord var2,
  1087. short phase2,
  1088. BitVector *olv,
  1089. BitVector *olp)
  1090. {
  1091. return((bitVectorRead(olv, var1) &&
  1092. bitVectorRead(olp, var1) == phase1) ||
  1093. (bitVectorRead(olv, var2) &&
  1094. bitVectorRead(olp, var2) == phase2));
  1095. } /* end of impliedp */
  1096. /**Function********************************************************************
  1097. Synopsis [Allocates a bit vector.]
  1098. Description [Allocates a bit vector. The parameter size gives the
  1099. number of bits. This procedure allocates enough long's to hold the
  1100. specified number of bits. Returns a pointer to the allocated vector
  1101. if successful; NULL otherwise.]
  1102. SideEffects [None]
  1103. SeeAlso [bitVectorClear bitVectorFree]
  1104. ******************************************************************************/
  1105. static BitVector *
  1106. bitVectorAlloc(
  1107. int size)
  1108. {
  1109. int allocSize;
  1110. BitVector *vector;
  1111. /* Find out how many long's we need.
  1112. ** There are sizeof(long) * 8 bits in a long.
  1113. ** The ceiling of the ratio of two integers m and n is given
  1114. ** by ((n-1)/m)+1. Putting all this together, we get... */
  1115. allocSize = ((size - 1) / (sizeof(BitVector) * 8)) + 1;
  1116. vector = ALLOC(BitVector, allocSize);
  1117. if (vector == NULL) return(NULL);
  1118. /* Clear the whole array. */
  1119. (void) memset(vector, 0, allocSize * sizeof(BitVector));
  1120. return(vector);
  1121. } /* end of bitVectorAlloc */
  1122. /**Function********************************************************************
  1123. Synopsis [Clears a bit vector.]
  1124. Description [Clears a bit vector. The parameter size gives the
  1125. number of bits.]
  1126. SideEffects [None]
  1127. SeeAlso [bitVectorAlloc]
  1128. ******************************************************************************/
  1129. DD_INLINE
  1130. static void
  1131. bitVectorClear(
  1132. BitVector *vector,
  1133. int size)
  1134. {
  1135. int allocSize;
  1136. /* Find out how many long's we need.
  1137. ** There are sizeof(long) * 8 bits in a long.
  1138. ** The ceiling of the ratio of two integers m and n is given
  1139. ** by ((n-1)/m)+1. Putting all this together, we get... */
  1140. allocSize = ((size - 1) / (sizeof(BitVector) * 8)) + 1;
  1141. /* Clear the whole array. */
  1142. (void) memset(vector, 0, allocSize * sizeof(BitVector));
  1143. return;
  1144. } /* end of bitVectorClear */
  1145. /**Function********************************************************************
  1146. Synopsis [Frees a bit vector.]
  1147. Description [Frees a bit vector.]
  1148. SideEffects [None]
  1149. SeeAlso [bitVectorAlloc]
  1150. ******************************************************************************/
  1151. static void
  1152. bitVectorFree(
  1153. BitVector *vector)
  1154. {
  1155. FREE(vector);
  1156. } /* end of bitVectorFree */
  1157. /**Function********************************************************************
  1158. Synopsis [Returns the i-th entry of a bit vector.]
  1159. Description [Returns the i-th entry of a bit vector.]
  1160. SideEffects [None]
  1161. SeeAlso [bitVectorSet]
  1162. ******************************************************************************/
  1163. DD_INLINE
  1164. static short
  1165. bitVectorRead(
  1166. BitVector *vector,
  1167. int i)
  1168. {
  1169. int word, bit;
  1170. short result;
  1171. if (vector == NULL) return((short) 0);
  1172. word = i >> LOGBPL;
  1173. bit = i & (BPL - 1);
  1174. result = (short) ((vector[word] >> bit) & 1L);
  1175. return(result);
  1176. } /* end of bitVectorRead */
  1177. /**Function********************************************************************
  1178. Synopsis [Sets the i-th entry of a bit vector to a value.]
  1179. Description [Sets the i-th entry of a bit vector to a value.]
  1180. SideEffects [None]
  1181. SeeAlso [bitVectorRead]
  1182. ******************************************************************************/
  1183. DD_INLINE
  1184. static void
  1185. bitVectorSet(
  1186. BitVector * vector,
  1187. int i,
  1188. short val)
  1189. {
  1190. int word, bit;
  1191. word = i >> LOGBPL;
  1192. bit = i & (BPL - 1);
  1193. vector[word] &= ~(1L << bit);
  1194. vector[word] |= (((long) val) << bit);
  1195. } /* end of bitVectorSet */
  1196. /**Function********************************************************************
  1197. Synopsis [Allocates a DdTlcInfo Structure.]
  1198. Description [Returns a pointer to a DdTlcInfo Structure if successful;
  1199. NULL otherwise.]
  1200. SideEffects [None]
  1201. SeeAlso [Cudd_tlcInfoFree]
  1202. ******************************************************************************/
  1203. static DdTlcInfo *
  1204. tlcInfoAlloc(void)
  1205. {
  1206. DdTlcInfo *res = ALLOC(DdTlcInfo,1);
  1207. if (res == NULL) return(NULL);
  1208. res->vars = NULL;
  1209. res->phases = NULL;
  1210. res->cnt = 0;
  1211. return(res);
  1212. } /* end of tlcInfoAlloc */