The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

660 lines
18 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Returns a subset of minterms from a boolean function.
  5. @author Balakrishna Kumthekar
  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. /*---------------------------------------------------------------------------*/
  40. /* Type declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Structure declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static DdNode * selectMintermsFromUniverse (DdManager *manager, int *varSeen, double n);
  56. static DdNode * mintermsFromUniverse (DdManager *manager, DdNode **vars, int numVars, double n, int index);
  57. static double bddAnnotateMintermCount (DdManager *manager, DdNode *node, double max, st_table *table);
  58. /** \endcond */
  59. /*---------------------------------------------------------------------------*/
  60. /* Definition of exported functions */
  61. /*---------------------------------------------------------------------------*/
  62. /**
  63. @brief Returns m minterms from a %BDD.
  64. @details Returns <code>m</code> minterms from a %BDD whose
  65. support has <code>n</code> variables at most. The procedure tries
  66. to create as few extra nodes as possible. The function represented
  67. by <code>S</code> depends on at most <code>n</code> of the variables
  68. in <code>xVars</code>.
  69. @return a %BDD with <code>m</code> minterms of the on-set of S if
  70. successful; NULL otherwise.
  71. @sideeffect None
  72. */
  73. DdNode *
  74. Cudd_SplitSet(
  75. DdManager * manager,
  76. DdNode * S,
  77. DdNode ** xVars,
  78. int n,
  79. double m)
  80. {
  81. DdNode *result;
  82. DdNode *zero, *one;
  83. double max, num;
  84. st_table *mtable;
  85. int *varSeen;
  86. int i,index, size;
  87. size = manager->size;
  88. one = DD_ONE(manager);
  89. zero = Cudd_Not(one);
  90. /* Trivial cases. */
  91. if (m == 0.0) {
  92. return(zero);
  93. }
  94. if (S == zero) {
  95. return(NULL);
  96. }
  97. max = pow(2.0,(double)n);
  98. if (m > max)
  99. return(NULL);
  100. do {
  101. manager->reordered = 0;
  102. /* varSeen is used to mark the variables that are encountered
  103. ** while traversing the BDD S.
  104. */
  105. varSeen = ALLOC(int, size);
  106. if (varSeen == NULL) {
  107. manager->errorCode = CUDD_MEMORY_OUT;
  108. return(NULL);
  109. }
  110. for (i = 0; i < size; i++) {
  111. varSeen[i] = -1;
  112. }
  113. for (i = 0; i < n; i++) {
  114. index = (xVars[i])->index;
  115. varSeen[manager->invperm[index]] = 0;
  116. }
  117. if (S == one) {
  118. if (m == max) {
  119. FREE(varSeen);
  120. return(S);
  121. }
  122. result = selectMintermsFromUniverse(manager,varSeen,m);
  123. if (result)
  124. cuddRef(result);
  125. FREE(varSeen);
  126. } else {
  127. mtable = st_init_table(st_ptrcmp,st_ptrhash);
  128. if (mtable == NULL) {
  129. (void) fprintf(manager->out,
  130. "Cudd_SplitSet: out-of-memory.\n");
  131. FREE(varSeen);
  132. manager->errorCode = CUDD_MEMORY_OUT;
  133. return(NULL);
  134. }
  135. /* The nodes of BDD S are annotated by the number of minterms
  136. ** in their onset. The node and the number of minterms in its
  137. ** onset are stored in mtable.
  138. */
  139. num = bddAnnotateMintermCount(manager,S,max,mtable);
  140. if (m == num) {
  141. st_foreach(mtable,cuddStCountfree,NIL(void));
  142. st_free_table(mtable);
  143. FREE(varSeen);
  144. return(S);
  145. }
  146. result = cuddSplitSetRecur(manager,mtable,varSeen,S,m,max,0);
  147. if (result)
  148. cuddRef(result);
  149. st_foreach(mtable,cuddStCountfree,NULL);
  150. st_free_table(mtable);
  151. FREE(varSeen);
  152. }
  153. } while (manager->reordered == 1);
  154. if (manager->errorCode == CUDD_TIMEOUT_EXPIRED && manager->timeoutHandler) {
  155. manager->timeoutHandler(manager, manager->tohArg);
  156. }
  157. cuddDeref(result);
  158. return(result);
  159. } /* end of Cudd_SplitSet */
  160. /*---------------------------------------------------------------------------*/
  161. /* Definition of internal functions */
  162. /*---------------------------------------------------------------------------*/
  163. /**
  164. @brief Implements the recursive step of Cudd_SplitSet.
  165. @details The procedure recursively traverses the %BDD and checks to
  166. see if any node satisfies the minterm requirements as specified by
  167. 'n'. At any node X, n is compared to the number of minterms in the
  168. onset of X's children. If either of the child nodes have exactly n
  169. minterms, then that node is returned; else, if n is greater than the
  170. onset of one of the child nodes, that node is retained and the
  171. difference in the number of minterms is extracted from the other
  172. child. In case n minterms can be extracted from constant 1, the
  173. algorithm returns the result with at most log(n) nodes.
  174. @sideeffect The array 'varSeen' is updated at every recursive call
  175. to set the variables traversed by the procedure.
  176. */
  177. DdNode*
  178. cuddSplitSetRecur(
  179. DdManager * manager,
  180. st_table * mtable,
  181. int * varSeen,
  182. DdNode * p,
  183. double n,
  184. double max,
  185. int index)
  186. {
  187. DdNode *one, *zero, *N, *Nv;
  188. DdNode *Nnv, *q, *r, *v;
  189. DdNode *result;
  190. double *dummy, numT, numE;
  191. int variable, positive;
  192. statLine(manager);
  193. one = DD_ONE(manager);
  194. zero = Cudd_Not(one);
  195. /* If p is constant, extract n minterms from constant 1. The procedure by
  196. ** construction guarantees that minterms will not be extracted from
  197. ** constant 0.
  198. */
  199. if (Cudd_IsConstantInt(p)) {
  200. q = selectMintermsFromUniverse(manager,varSeen,n);
  201. return(q);
  202. }
  203. N = Cudd_Regular(p);
  204. /* Set variable as seen. */
  205. variable = N->index;
  206. varSeen[manager->invperm[variable]] = -1;
  207. Nv = cuddT(N);
  208. Nnv = cuddE(N);
  209. if (Cudd_IsComplement(p)) {
  210. Nv = Cudd_Not(Nv);
  211. Nnv = Cudd_Not(Nnv);
  212. }
  213. /* If both the children of 'p' are constants, extract n minterms from a
  214. ** constant node.
  215. */
  216. if (Cudd_IsConstantInt(Nv) && Cudd_IsConstantInt(Nnv)) {
  217. q = selectMintermsFromUniverse(manager,varSeen,n);
  218. if (q == NULL) {
  219. return(NULL);
  220. }
  221. cuddRef(q);
  222. r = cuddBddAndRecur(manager,p,q);
  223. if (r == NULL) {
  224. Cudd_RecursiveDeref(manager,q);
  225. return(NULL);
  226. }
  227. cuddRef(r);
  228. Cudd_RecursiveDeref(manager,q);
  229. cuddDeref(r);
  230. return(r);
  231. }
  232. /* Lookup the # of minterms in the onset of the node from the table. */
  233. if (!Cudd_IsConstantInt(Nv)) {
  234. if (!st_lookup(mtable, Nv, (void **) &dummy)) return(NULL);
  235. numT = *dummy/(2*(1U<<index));
  236. } else if (Nv == one) {
  237. numT = max/(2*(1U<<index));
  238. } else {
  239. numT = 0;
  240. }
  241. if (!Cudd_IsConstantInt(Nnv)) {
  242. if (!st_lookup(mtable, Nnv, (void **) &dummy)) return(NULL);
  243. numE = *dummy/(2*(1U<<index));
  244. } else if (Nnv == one) {
  245. numE = max/(2*(1U<<index));
  246. } else {
  247. numE = 0;
  248. }
  249. v = cuddUniqueInter(manager,variable,one,zero);
  250. cuddRef(v);
  251. /* If perfect match. */
  252. if (numT == n) {
  253. q = cuddBddAndRecur(manager,v,Nv);
  254. if (q == NULL) {
  255. Cudd_RecursiveDeref(manager,v);
  256. return(NULL);
  257. }
  258. cuddRef(q);
  259. Cudd_RecursiveDeref(manager,v);
  260. cuddDeref(q);
  261. return(q);
  262. }
  263. if (numE == n) {
  264. q = cuddBddAndRecur(manager,Cudd_Not(v),Nnv);
  265. if (q == NULL) {
  266. Cudd_RecursiveDeref(manager,v);
  267. return(NULL);
  268. }
  269. cuddRef(q);
  270. Cudd_RecursiveDeref(manager,v);
  271. cuddDeref(q);
  272. return(q);
  273. }
  274. /* If n is greater than numT, extract the difference from the ELSE child
  275. ** and retain the function represented by the THEN branch.
  276. */
  277. if (numT < n) {
  278. q = cuddSplitSetRecur(manager,mtable,varSeen,
  279. Nnv,(n-numT),max,index+1);
  280. if (q == NULL) {
  281. Cudd_RecursiveDeref(manager,v);
  282. return(NULL);
  283. }
  284. cuddRef(q);
  285. r = cuddBddIteRecur(manager,v,Nv,q);
  286. if (r == NULL) {
  287. Cudd_RecursiveDeref(manager,q);
  288. Cudd_RecursiveDeref(manager,v);
  289. return(NULL);
  290. }
  291. cuddRef(r);
  292. Cudd_RecursiveDeref(manager,q);
  293. Cudd_RecursiveDeref(manager,v);
  294. cuddDeref(r);
  295. return(r);
  296. }
  297. /* If n is greater than numE, extract the difference from the THEN child
  298. ** and retain the function represented by the ELSE branch.
  299. */
  300. if (numE < n) {
  301. q = cuddSplitSetRecur(manager,mtable,varSeen,
  302. Nv, (n-numE),max,index+1);
  303. if (q == NULL) {
  304. Cudd_RecursiveDeref(manager,v);
  305. return(NULL);
  306. }
  307. cuddRef(q);
  308. r = cuddBddIteRecur(manager,v,q,Nnv);
  309. if (r == NULL) {
  310. Cudd_RecursiveDeref(manager,q);
  311. Cudd_RecursiveDeref(manager,v);
  312. return(NULL);
  313. }
  314. cuddRef(r);
  315. Cudd_RecursiveDeref(manager,q);
  316. Cudd_RecursiveDeref(manager,v);
  317. cuddDeref(r);
  318. return(r);
  319. }
  320. /* None of the above cases; (n < numT and n < numE) and either of
  321. ** the Nv, Nnv or both are not constants. If possible extract the
  322. ** required minterms the constant branch.
  323. */
  324. if (Cudd_IsConstantInt(Nv) && !Cudd_IsConstantInt(Nnv)) {
  325. q = selectMintermsFromUniverse(manager,varSeen,n);
  326. if (q == NULL) {
  327. Cudd_RecursiveDeref(manager,v);
  328. return(NULL);
  329. }
  330. cuddRef(q);
  331. result = cuddBddAndRecur(manager,v,q);
  332. if (result == NULL) {
  333. Cudd_RecursiveDeref(manager,q);
  334. Cudd_RecursiveDeref(manager,v);
  335. return(NULL);
  336. }
  337. cuddRef(result);
  338. Cudd_RecursiveDeref(manager,q);
  339. Cudd_RecursiveDeref(manager,v);
  340. cuddDeref(result);
  341. return(result);
  342. } else if (!Cudd_IsConstantInt(Nv) && Cudd_IsConstantInt(Nnv)) {
  343. q = selectMintermsFromUniverse(manager,varSeen,n);
  344. if (q == NULL) {
  345. Cudd_RecursiveDeref(manager,v);
  346. return(NULL);
  347. }
  348. cuddRef(q);
  349. result = cuddBddAndRecur(manager,Cudd_Not(v),q);
  350. if (result == NULL) {
  351. Cudd_RecursiveDeref(manager,q);
  352. Cudd_RecursiveDeref(manager,v);
  353. return(NULL);
  354. }
  355. cuddRef(result);
  356. Cudd_RecursiveDeref(manager,q);
  357. Cudd_RecursiveDeref(manager,v);
  358. cuddDeref(result);
  359. return(result);
  360. }
  361. /* Both Nv and Nnv are not constants. So choose the one which
  362. ** has fewer minterms in its onset.
  363. */
  364. positive = 0;
  365. if (numT < numE) {
  366. q = cuddSplitSetRecur(manager,mtable,varSeen,
  367. Nv,n,max,index+1);
  368. positive = 1;
  369. } else {
  370. q = cuddSplitSetRecur(manager,mtable,varSeen,
  371. Nnv,n,max,index+1);
  372. }
  373. if (q == NULL) {
  374. Cudd_RecursiveDeref(manager,v);
  375. return(NULL);
  376. }
  377. cuddRef(q);
  378. if (positive) {
  379. result = cuddBddAndRecur(manager,v,q);
  380. } else {
  381. result = cuddBddAndRecur(manager,Cudd_Not(v),q);
  382. }
  383. if (result == NULL) {
  384. Cudd_RecursiveDeref(manager,q);
  385. Cudd_RecursiveDeref(manager,v);
  386. return(NULL);
  387. }
  388. cuddRef(result);
  389. Cudd_RecursiveDeref(manager,q);
  390. Cudd_RecursiveDeref(manager,v);
  391. cuddDeref(result);
  392. return(result);
  393. } /* end of cuddSplitSetRecur */
  394. /*---------------------------------------------------------------------------*/
  395. /* Definition of static functions */
  396. /*---------------------------------------------------------------------------*/
  397. /**
  398. @brief This function prepares an array of variables which have not been
  399. encountered so far when traversing the procedure cuddSplitSetRecur.
  400. @details This array is then used to extract the required number of
  401. minterms from a constant 1. The algorithm guarantees that the size
  402. of %BDD will be at most log(n).
  403. @sideeffect None
  404. */
  405. static DdNode *
  406. selectMintermsFromUniverse(
  407. DdManager * manager,
  408. int * varSeen,
  409. double n)
  410. {
  411. int numVars;
  412. int i, size, j;
  413. DdNode *one, *zero, *result;
  414. DdNode **vars;
  415. numVars = 0;
  416. size = manager->size;
  417. one = DD_ONE(manager);
  418. zero = Cudd_Not(one);
  419. /* Count the number of variables not encountered so far in procedure
  420. ** cuddSplitSetRecur.
  421. */
  422. for (i = size-1; i >= 0; i--) {
  423. if(varSeen[i] == 0)
  424. numVars++;
  425. }
  426. vars = ALLOC(DdNode *, numVars);
  427. if (!vars) {
  428. manager->errorCode = CUDD_MEMORY_OUT;
  429. return(NULL);
  430. }
  431. j = 0;
  432. for (i = size-1; i >= 0; i--) {
  433. if(varSeen[i] == 0) {
  434. vars[j] = cuddUniqueInter(manager,manager->perm[i],one,zero);
  435. cuddRef(vars[j]);
  436. j++;
  437. }
  438. }
  439. /* Compute a function which has n minterms and depends on at most
  440. ** numVars variables.
  441. */
  442. result = mintermsFromUniverse(manager,vars,numVars,n, 0);
  443. if (result)
  444. cuddRef(result);
  445. for (i = 0; i < numVars; i++)
  446. Cudd_RecursiveDeref(manager,vars[i]);
  447. FREE(vars);
  448. return(result);
  449. } /* end of selectMintermsFromUniverse */
  450. /**
  451. @brief Recursive procedure to extract n mintems from constant 1.
  452. @sideeffect None
  453. */
  454. static DdNode *
  455. mintermsFromUniverse(
  456. DdManager * manager,
  457. DdNode ** vars,
  458. int numVars,
  459. double n,
  460. int index)
  461. {
  462. DdNode *one, *zero;
  463. DdNode *q, *result;
  464. double max, max2;
  465. statLine(manager);
  466. one = DD_ONE(manager);
  467. zero = Cudd_Not(one);
  468. max = pow(2.0, (double)numVars);
  469. max2 = max / 2.0;
  470. if (n == max)
  471. return(one);
  472. if (n == 0.0)
  473. return(zero);
  474. /* if n == 2^(numVars-1), return a single variable */
  475. if (n == max2)
  476. return vars[index];
  477. else if (n > max2) {
  478. /* When n > 2^(numVars-1), a single variable vars[index]
  479. ** contains 2^(numVars-1) minterms. The rest are extracted
  480. ** from a constant with 1 less variable.
  481. */
  482. q = mintermsFromUniverse(manager,vars,numVars-1,(n-max2),index+1);
  483. if (q == NULL)
  484. return(NULL);
  485. cuddRef(q);
  486. result = cuddBddIteRecur(manager,vars[index],one,q);
  487. } else {
  488. /* When n < 2^(numVars-1), a literal of variable vars[index]
  489. ** is selected. The required n minterms are extracted from a
  490. ** constant with 1 less variable.
  491. */
  492. q = mintermsFromUniverse(manager,vars,numVars-1,n,index+1);
  493. if (q == NULL)
  494. return(NULL);
  495. cuddRef(q);
  496. result = cuddBddAndRecur(manager,vars[index],q);
  497. }
  498. if (result == NULL) {
  499. Cudd_RecursiveDeref(manager,q);
  500. return(NULL);
  501. }
  502. cuddRef(result);
  503. Cudd_RecursiveDeref(manager,q);
  504. cuddDeref(result);
  505. return(result);
  506. } /* end of mintermsFromUniverse */
  507. /**
  508. @brief Annotates every node in the %BDD node with its minterm count.
  509. @details In this function, every node and the minterm count
  510. represented by it are stored in a hash table.
  511. @sideeffect Fills up 'table' with the pair <node,minterm_count>.
  512. */
  513. static double
  514. bddAnnotateMintermCount(
  515. DdManager * manager,
  516. DdNode * node,
  517. double max,
  518. st_table * table)
  519. {
  520. DdNode *N,*Nv,*Nnv;
  521. double min_v,min_nv;
  522. double min_N;
  523. double *pmin;
  524. double *dummy;
  525. statLine(manager);
  526. N = Cudd_Regular(node);
  527. if (cuddIsConstant(N)) {
  528. if (node == DD_ONE(manager)) {
  529. return(max);
  530. } else {
  531. return(0.0);
  532. }
  533. }
  534. if (st_lookup(table, node, (void **) &dummy)) {
  535. return(*dummy);
  536. }
  537. Nv = cuddT(N);
  538. Nnv = cuddE(N);
  539. if (N != node) {
  540. Nv = Cudd_Not(Nv);
  541. Nnv = Cudd_Not(Nnv);
  542. }
  543. /* Recur on the two branches. */
  544. min_v = bddAnnotateMintermCount(manager,Nv,max,table) / 2.0;
  545. if (min_v == (double)CUDD_OUT_OF_MEM)
  546. return ((double)CUDD_OUT_OF_MEM);
  547. min_nv = bddAnnotateMintermCount(manager,Nnv,max,table) / 2.0;
  548. if (min_nv == (double)CUDD_OUT_OF_MEM)
  549. return ((double)CUDD_OUT_OF_MEM);
  550. min_N = min_v + min_nv;
  551. pmin = ALLOC(double,1);
  552. if (pmin == NULL) {
  553. manager->errorCode = CUDD_MEMORY_OUT;
  554. return((double)CUDD_OUT_OF_MEM);
  555. }
  556. *pmin = min_N;
  557. if (st_insert(table, node, pmin) == ST_OUT_OF_MEM) {
  558. FREE(pmin);
  559. return((double)CUDD_OUT_OF_MEM);
  560. }
  561. return(min_N);
  562. } /* end of bddAnnotateMintermCount */