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.

1419 lines
39 KiB

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