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.

686 lines
19 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddSplit.c]
  3. PackageName [cudd]
  4. Synopsis [Returns a subset of minterms from a boolean function.]
  5. Description [External functions included in this modoule:
  6. <ul>
  7. <li> Cudd_SplitSet()
  8. </ul>
  9. Internal functions included in this module:
  10. <ul>
  11. <li> cuddSplitSetRecur()
  12. </u>
  13. Static functions included in this module:
  14. <ul>
  15. <li> selectMintermsFromUniverse()
  16. <li> mintermsFromUniverse()
  17. <li> bddAnnotateMintermCount()
  18. </ul> ]
  19. SeeAlso []
  20. Author [Balakrishna Kumthekar]
  21. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions
  25. are met:
  26. Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. Redistributions in binary form must reproduce the above copyright
  29. notice, this list of conditions and the following disclaimer in the
  30. documentation and/or other materials provided with the distribution.
  31. Neither the name of the University of Colorado nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  37. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  38. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  41. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  44. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45. POSSIBILITY OF SUCH DAMAGE.]
  46. ******************************************************************************/
  47. #include "util.h"
  48. #include "cuddInt.h"
  49. /*---------------------------------------------------------------------------*/
  50. /* Constant declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /*---------------------------------------------------------------------------*/
  53. /* Type declarations */
  54. /*---------------------------------------------------------------------------*/
  55. /*---------------------------------------------------------------------------*/
  56. /* Structure declarations */
  57. /*---------------------------------------------------------------------------*/
  58. /*---------------------------------------------------------------------------*/
  59. /* Variable declarations */
  60. /*---------------------------------------------------------------------------*/
  61. /*---------------------------------------------------------------------------*/
  62. /* Macro declarations */
  63. /*---------------------------------------------------------------------------*/
  64. /**AutomaticStart*************************************************************/
  65. /*---------------------------------------------------------------------------*/
  66. /* Static function prototypes */
  67. /*---------------------------------------------------------------------------*/
  68. static DdNode * selectMintermsFromUniverse (DdManager *manager, int *varSeen, double n);
  69. static DdNode * mintermsFromUniverse (DdManager *manager, DdNode **vars, int numVars, double n, int index);
  70. static double bddAnnotateMintermCount (DdManager *manager, DdNode *node, double max, st_table *table);
  71. /**AutomaticEnd***************************************************************/
  72. /*---------------------------------------------------------------------------*/
  73. /* Definition of exported functions */
  74. /*---------------------------------------------------------------------------*/
  75. /**Function********************************************************************
  76. Synopsis [Returns m minterms from a BDD.]
  77. Description [Returns <code>m</code> minterms from a BDD whose
  78. support has <code>n</code> variables at most. The procedure tries
  79. to create as few extra nodes as possible. The function represented
  80. by <code>S</code> depends on at most <code>n</code> of the variables
  81. in <code>xVars</code>. Returns a BDD with <code>m</code> minterms
  82. of the on-set of S if successful; NULL otherwise.]
  83. SideEffects [None]
  84. SeeAlso []
  85. ******************************************************************************/
  86. DdNode *
  87. Cudd_SplitSet(
  88. DdManager * manager,
  89. DdNode * S,
  90. DdNode ** xVars,
  91. int n,
  92. double m)
  93. {
  94. DdNode *result;
  95. DdNode *zero, *one;
  96. double max, num;
  97. st_table *mtable;
  98. int *varSeen;
  99. int i,index, size;
  100. size = manager->size;
  101. one = DD_ONE(manager);
  102. zero = Cudd_Not(one);
  103. /* Trivial cases. */
  104. if (m == 0.0) {
  105. return(zero);
  106. }
  107. if (S == zero) {
  108. return(NULL);
  109. }
  110. max = pow(2.0,(double)n);
  111. if (m > max)
  112. return(NULL);
  113. do {
  114. manager->reordered = 0;
  115. /* varSeen is used to mark the variables that are encountered
  116. ** while traversing the BDD S.
  117. */
  118. varSeen = ALLOC(int, size);
  119. if (varSeen == NULL) {
  120. manager->errorCode = CUDD_MEMORY_OUT;
  121. return(NULL);
  122. }
  123. for (i = 0; i < size; i++) {
  124. varSeen[i] = -1;
  125. }
  126. for (i = 0; i < n; i++) {
  127. index = (xVars[i])->index;
  128. varSeen[manager->invperm[index]] = 0;
  129. }
  130. if (S == one) {
  131. if (m == max) {
  132. FREE(varSeen);
  133. return(S);
  134. }
  135. result = selectMintermsFromUniverse(manager,varSeen,m);
  136. if (result)
  137. cuddRef(result);
  138. FREE(varSeen);
  139. } else {
  140. mtable = st_init_table(st_ptrcmp,st_ptrhash);
  141. if (mtable == NULL) {
  142. (void) fprintf(manager->out,
  143. "Cudd_SplitSet: out-of-memory.\n");
  144. FREE(varSeen);
  145. manager->errorCode = CUDD_MEMORY_OUT;
  146. return(NULL);
  147. }
  148. /* The nodes of BDD S are annotated by the number of minterms
  149. ** in their onset. The node and the number of minterms in its
  150. ** onset are stored in mtable.
  151. */
  152. num = bddAnnotateMintermCount(manager,S,max,mtable);
  153. if (m == num) {
  154. st_foreach(mtable,cuddStCountfree,NIL(char));
  155. st_free_table(mtable);
  156. FREE(varSeen);
  157. return(S);
  158. }
  159. result = cuddSplitSetRecur(manager,mtable,varSeen,S,m,max,0);
  160. if (result)
  161. cuddRef(result);
  162. st_foreach(mtable,cuddStCountfree,NULL);
  163. st_free_table(mtable);
  164. FREE(varSeen);
  165. }
  166. } while (manager->reordered == 1);
  167. cuddDeref(result);
  168. return(result);
  169. } /* end of Cudd_SplitSet */
  170. /*---------------------------------------------------------------------------*/
  171. /* Definition of internal functions */
  172. /*---------------------------------------------------------------------------*/
  173. /**Function********************************************************************
  174. Synopsis [Implements the recursive step of Cudd_SplitSet.]
  175. Description [Implements the recursive step of Cudd_SplitSet. The
  176. procedure recursively traverses the BDD and checks to see if any
  177. node satisfies the minterm requirements as specified by 'n'. At any
  178. node X, n is compared to the number of minterms in the onset of X's
  179. children. If either of the child nodes have exactly n minterms, then
  180. that node is returned; else, if n is greater than the onset of one
  181. of the child nodes, that node is retained and the difference in the
  182. number of minterms is extracted from the other child. In case n
  183. minterms can be extracted from constant 1, the algorithm returns the
  184. result with at most log(n) nodes.]
  185. SideEffects [The array 'varSeen' is updated at every recursive call
  186. to set the variables traversed by the procedure.]
  187. SeeAlso []
  188. ******************************************************************************/
  189. DdNode*
  190. cuddSplitSetRecur(
  191. DdManager * manager,
  192. st_table * mtable,
  193. int * varSeen,
  194. DdNode * p,
  195. double n,
  196. double max,
  197. int index)
  198. {
  199. DdNode *one, *zero, *N, *Nv;
  200. DdNode *Nnv, *q, *r, *v;
  201. DdNode *result;
  202. double *dummy, numT, numE;
  203. int variable, positive;
  204. statLine(manager);
  205. one = DD_ONE(manager);
  206. zero = Cudd_Not(one);
  207. /* If p is constant, extract n minterms from constant 1. The procedure by
  208. ** construction guarantees that minterms will not be extracted from
  209. ** constant 0.
  210. */
  211. if (Cudd_IsConstant(p)) {
  212. q = selectMintermsFromUniverse(manager,varSeen,n);
  213. return(q);
  214. }
  215. N = Cudd_Regular(p);
  216. /* Set variable as seen. */
  217. variable = N->index;
  218. varSeen[manager->invperm[variable]] = -1;
  219. Nv = cuddT(N);
  220. Nnv = cuddE(N);
  221. if (Cudd_IsComplement(p)) {
  222. Nv = Cudd_Not(Nv);
  223. Nnv = Cudd_Not(Nnv);
  224. }
  225. /* If both the children of 'p' are constants, extract n minterms from a
  226. ** constant node.
  227. */
  228. if (Cudd_IsConstant(Nv) && Cudd_IsConstant(Nnv)) {
  229. q = selectMintermsFromUniverse(manager,varSeen,n);
  230. if (q == NULL) {
  231. return(NULL);
  232. }
  233. cuddRef(q);
  234. r = cuddBddAndRecur(manager,p,q);
  235. if (r == NULL) {
  236. Cudd_RecursiveDeref(manager,q);
  237. return(NULL);
  238. }
  239. cuddRef(r);
  240. Cudd_RecursiveDeref(manager,q);
  241. cuddDeref(r);
  242. return(r);
  243. }
  244. /* Lookup the # of minterms in the onset of the node from the table. */
  245. if (!Cudd_IsConstant(Nv)) {
  246. if (!st_lookup(mtable, Nv, &dummy)) return(NULL);
  247. numT = *dummy/(2*(1<<index));
  248. } else if (Nv == one) {
  249. numT = max/(2*(1<<index));
  250. } else {
  251. numT = 0;
  252. }
  253. if (!Cudd_IsConstant(Nnv)) {
  254. if (!st_lookup(mtable, Nnv, &dummy)) return(NULL);
  255. numE = *dummy/(2*(1<<index));
  256. } else if (Nnv == one) {
  257. numE = max/(2*(1<<index));
  258. } else {
  259. numE = 0;
  260. }
  261. v = cuddUniqueInter(manager,variable,one,zero);
  262. cuddRef(v);
  263. /* If perfect match. */
  264. if (numT == n) {
  265. q = cuddBddAndRecur(manager,v,Nv);
  266. if (q == NULL) {
  267. Cudd_RecursiveDeref(manager,v);
  268. return(NULL);
  269. }
  270. cuddRef(q);
  271. Cudd_RecursiveDeref(manager,v);
  272. cuddDeref(q);
  273. return(q);
  274. }
  275. if (numE == n) {
  276. q = cuddBddAndRecur(manager,Cudd_Not(v),Nnv);
  277. if (q == NULL) {
  278. Cudd_RecursiveDeref(manager,v);
  279. return(NULL);
  280. }
  281. cuddRef(q);
  282. Cudd_RecursiveDeref(manager,v);
  283. cuddDeref(q);
  284. return(q);
  285. }
  286. /* If n is greater than numT, extract the difference from the ELSE child
  287. ** and retain the function represented by the THEN branch.
  288. */
  289. if (numT < n) {
  290. q = cuddSplitSetRecur(manager,mtable,varSeen,
  291. Nnv,(n-numT),max,index+1);
  292. if (q == NULL) {
  293. Cudd_RecursiveDeref(manager,v);
  294. return(NULL);
  295. }
  296. cuddRef(q);
  297. r = cuddBddIteRecur(manager,v,Nv,q);
  298. if (r == NULL) {
  299. Cudd_RecursiveDeref(manager,q);
  300. Cudd_RecursiveDeref(manager,v);
  301. return(NULL);
  302. }
  303. cuddRef(r);
  304. Cudd_RecursiveDeref(manager,q);
  305. Cudd_RecursiveDeref(manager,v);
  306. cuddDeref(r);
  307. return(r);
  308. }
  309. /* If n is greater than numE, extract the difference from the THEN child
  310. ** and retain the function represented by the ELSE branch.
  311. */
  312. if (numE < n) {
  313. q = cuddSplitSetRecur(manager,mtable,varSeen,
  314. Nv, (n-numE),max,index+1);
  315. if (q == NULL) {
  316. Cudd_RecursiveDeref(manager,v);
  317. return(NULL);
  318. }
  319. cuddRef(q);
  320. r = cuddBddIteRecur(manager,v,q,Nnv);
  321. if (r == NULL) {
  322. Cudd_RecursiveDeref(manager,q);
  323. Cudd_RecursiveDeref(manager,v);
  324. return(NULL);
  325. }
  326. cuddRef(r);
  327. Cudd_RecursiveDeref(manager,q);
  328. Cudd_RecursiveDeref(manager,v);
  329. cuddDeref(r);
  330. return(r);
  331. }
  332. /* None of the above cases; (n < numT and n < numE) and either of
  333. ** the Nv, Nnv or both are not constants. If possible extract the
  334. ** required minterms the constant branch.
  335. */
  336. if (Cudd_IsConstant(Nv) && !Cudd_IsConstant(Nnv)) {
  337. q = selectMintermsFromUniverse(manager,varSeen,n);
  338. if (q == NULL) {
  339. Cudd_RecursiveDeref(manager,v);
  340. return(NULL);
  341. }
  342. cuddRef(q);
  343. result = cuddBddAndRecur(manager,v,q);
  344. if (result == NULL) {
  345. Cudd_RecursiveDeref(manager,q);
  346. Cudd_RecursiveDeref(manager,v);
  347. return(NULL);
  348. }
  349. cuddRef(result);
  350. Cudd_RecursiveDeref(manager,q);
  351. Cudd_RecursiveDeref(manager,v);
  352. cuddDeref(result);
  353. return(result);
  354. } else if (!Cudd_IsConstant(Nv) && Cudd_IsConstant(Nnv)) {
  355. q = selectMintermsFromUniverse(manager,varSeen,n);
  356. if (q == NULL) {
  357. Cudd_RecursiveDeref(manager,v);
  358. return(NULL);
  359. }
  360. cuddRef(q);
  361. result = cuddBddAndRecur(manager,Cudd_Not(v),q);
  362. if (result == NULL) {
  363. Cudd_RecursiveDeref(manager,q);
  364. Cudd_RecursiveDeref(manager,v);
  365. return(NULL);
  366. }
  367. cuddRef(result);
  368. Cudd_RecursiveDeref(manager,q);
  369. Cudd_RecursiveDeref(manager,v);
  370. cuddDeref(result);
  371. return(result);
  372. }
  373. /* Both Nv and Nnv are not constants. So choose the one which
  374. ** has fewer minterms in its onset.
  375. */
  376. positive = 0;
  377. if (numT < numE) {
  378. q = cuddSplitSetRecur(manager,mtable,varSeen,
  379. Nv,n,max,index+1);
  380. positive = 1;
  381. } else {
  382. q = cuddSplitSetRecur(manager,mtable,varSeen,
  383. Nnv,n,max,index+1);
  384. }
  385. if (q == NULL) {
  386. Cudd_RecursiveDeref(manager,v);
  387. return(NULL);
  388. }
  389. cuddRef(q);
  390. if (positive) {
  391. result = cuddBddAndRecur(manager,v,q);
  392. } else {
  393. result = cuddBddAndRecur(manager,Cudd_Not(v),q);
  394. }
  395. if (result == NULL) {
  396. Cudd_RecursiveDeref(manager,q);
  397. Cudd_RecursiveDeref(manager,v);
  398. return(NULL);
  399. }
  400. cuddRef(result);
  401. Cudd_RecursiveDeref(manager,q);
  402. Cudd_RecursiveDeref(manager,v);
  403. cuddDeref(result);
  404. return(result);
  405. } /* end of cuddSplitSetRecur */
  406. /*---------------------------------------------------------------------------*/
  407. /* Definition of static functions */
  408. /*---------------------------------------------------------------------------*/
  409. /**Function********************************************************************
  410. Synopsis [This function prepares an array of variables which have not been
  411. encountered so far when traversing the procedure cuddSplitSetRecur.]
  412. Description [This function prepares an array of variables which have not been
  413. encountered so far when traversing the procedure cuddSplitSetRecur. This
  414. array is then used to extract the required number of minterms from a constant
  415. 1. The algorithm guarantees that the size of BDD will be utmost \log(n).]
  416. SideEffects [None]
  417. ******************************************************************************/
  418. static DdNode *
  419. selectMintermsFromUniverse(
  420. DdManager * manager,
  421. int * varSeen,
  422. double n)
  423. {
  424. int numVars;
  425. int i, size, j;
  426. DdNode *one, *zero, *result;
  427. DdNode **vars;
  428. numVars = 0;
  429. size = manager->size;
  430. one = DD_ONE(manager);
  431. zero = Cudd_Not(one);
  432. /* Count the number of variables not encountered so far in procedure
  433. ** cuddSplitSetRecur.
  434. */
  435. for (i = size-1; i >= 0; i--) {
  436. if(varSeen[i] == 0)
  437. numVars++;
  438. }
  439. vars = ALLOC(DdNode *, numVars);
  440. if (!vars) {
  441. manager->errorCode = CUDD_MEMORY_OUT;
  442. return(NULL);
  443. }
  444. j = 0;
  445. for (i = size-1; i >= 0; i--) {
  446. if(varSeen[i] == 0) {
  447. vars[j] = cuddUniqueInter(manager,manager->perm[i],one,zero);
  448. cuddRef(vars[j]);
  449. j++;
  450. }
  451. }
  452. /* Compute a function which has n minterms and depends on at most
  453. ** numVars variables.
  454. */
  455. result = mintermsFromUniverse(manager,vars,numVars,n, 0);
  456. if (result)
  457. cuddRef(result);
  458. for (i = 0; i < numVars; i++)
  459. Cudd_RecursiveDeref(manager,vars[i]);
  460. FREE(vars);
  461. return(result);
  462. } /* end of selectMintermsFromUniverse */
  463. /**Function********************************************************************
  464. Synopsis [Recursive procedure to extract n mintems from constant 1.]
  465. Description [Recursive procedure to extract n mintems from constant 1.]
  466. SideEffects [None]
  467. ******************************************************************************/
  468. static DdNode *
  469. mintermsFromUniverse(
  470. DdManager * manager,
  471. DdNode ** vars,
  472. int numVars,
  473. double n,
  474. int index)
  475. {
  476. DdNode *one, *zero;
  477. DdNode *q, *result;
  478. double max, max2;
  479. statLine(manager);
  480. one = DD_ONE(manager);
  481. zero = Cudd_Not(one);
  482. max = pow(2.0, (double)numVars);
  483. max2 = max / 2.0;
  484. if (n == max)
  485. return(one);
  486. if (n == 0.0)
  487. return(zero);
  488. /* if n == 2^(numVars-1), return a single variable */
  489. if (n == max2)
  490. return vars[index];
  491. else if (n > max2) {
  492. /* When n > 2^(numVars-1), a single variable vars[index]
  493. ** contains 2^(numVars-1) minterms. The rest are extracted
  494. ** from a constant with 1 less variable.
  495. */
  496. q = mintermsFromUniverse(manager,vars,numVars-1,(n-max2),index+1);
  497. if (q == NULL)
  498. return(NULL);
  499. cuddRef(q);
  500. result = cuddBddIteRecur(manager,vars[index],one,q);
  501. } else {
  502. /* When n < 2^(numVars-1), a literal of variable vars[index]
  503. ** is selected. The required n minterms are extracted from a
  504. ** constant with 1 less variable.
  505. */
  506. q = mintermsFromUniverse(manager,vars,numVars-1,n,index+1);
  507. if (q == NULL)
  508. return(NULL);
  509. cuddRef(q);
  510. result = cuddBddAndRecur(manager,vars[index],q);
  511. }
  512. if (result == NULL) {
  513. Cudd_RecursiveDeref(manager,q);
  514. return(NULL);
  515. }
  516. cuddRef(result);
  517. Cudd_RecursiveDeref(manager,q);
  518. cuddDeref(result);
  519. return(result);
  520. } /* end of mintermsFromUniverse */
  521. /**Function********************************************************************
  522. Synopsis [Annotates every node in the BDD node with its minterm count.]
  523. Description [Annotates every node in the BDD node with its minterm count.
  524. In this function, every node and the minterm count represented by it are
  525. stored in a hash table.]
  526. SideEffects [Fills up 'table' with the pair <node,minterm_count>.]
  527. ******************************************************************************/
  528. static double
  529. bddAnnotateMintermCount(
  530. DdManager * manager,
  531. DdNode * node,
  532. double max,
  533. st_table * table)
  534. {
  535. DdNode *N,*Nv,*Nnv;
  536. register double min_v,min_nv;
  537. register double min_N;
  538. double *pmin;
  539. double *dummy;
  540. statLine(manager);
  541. N = Cudd_Regular(node);
  542. if (cuddIsConstant(N)) {
  543. if (node == DD_ONE(manager)) {
  544. return(max);
  545. } else {
  546. return(0.0);
  547. }
  548. }
  549. if (st_lookup(table, node, &dummy)) {
  550. return(*dummy);
  551. }
  552. Nv = cuddT(N);
  553. Nnv = cuddE(N);
  554. if (N != node) {
  555. Nv = Cudd_Not(Nv);
  556. Nnv = Cudd_Not(Nnv);
  557. }
  558. /* Recur on the two branches. */
  559. min_v = bddAnnotateMintermCount(manager,Nv,max,table) / 2.0;
  560. if (min_v == (double)CUDD_OUT_OF_MEM)
  561. return ((double)CUDD_OUT_OF_MEM);
  562. min_nv = bddAnnotateMintermCount(manager,Nnv,max,table) / 2.0;
  563. if (min_nv == (double)CUDD_OUT_OF_MEM)
  564. return ((double)CUDD_OUT_OF_MEM);
  565. min_N = min_v + min_nv;
  566. pmin = ALLOC(double,1);
  567. if (pmin == NULL) {
  568. manager->errorCode = CUDD_MEMORY_OUT;
  569. return((double)CUDD_OUT_OF_MEM);
  570. }
  571. *pmin = min_N;
  572. if (st_insert(table,(char *)node, (char *)pmin) == ST_OUT_OF_MEM) {
  573. FREE(pmin);
  574. return((double)CUDD_OUT_OF_MEM);
  575. }
  576. return(min_N);
  577. } /* end of bddAnnotateMintermCount */