Static Value-Flow Analysis
Loading...
Searching...
No Matches
IntervalValue.h
Go to the documentation of this file.
1//===- IntervalValue.h ----Interval Value for Abstract Domain-------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2022> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22/*
23 * IntervalValue.h
24 *
25 * Created on: Aug 4, 2022
26 * Author: Jiawei Wang, Xiao Cheng
27 *
28 */
29// The implementation is based on
30// Xiao Cheng, Jiawei Wang and Yulei Sui. Precise Sparse Abstract Execution via Cross-Domain Interaction.
31// 46th International Conference on Software Engineering. (ICSE24)
32
33#ifndef Z3_EXAMPLE_IntervalValue_H
34#define Z3_EXAMPLE_IntervalValue_H
35
36#include <sstream>
38
39namespace SVF
40{
41
46{
47private:
48 // Lower bound
50
51 // Upper bound
53
54 // Invariant: isBottom() <=> _lb = +inf && _ub = -inf
55public:
56 friend IntervalValue operator+(const IntervalValue &lhs, const IntervalValue &rhs);
57 friend IntervalValue operator-(const IntervalValue &lhs, const IntervalValue &rhs);
58 friend IntervalValue operator*(const IntervalValue &lhs, const IntervalValue &rhs);
59 friend IntervalValue operator/(const IntervalValue &lhs, const IntervalValue &rhs);
60 friend IntervalValue operator<<(const IntervalValue &lhs, const IntervalValue &rhs);
61 friend IntervalValue operator>>(const IntervalValue &lhs, const IntervalValue &rhs);
62 friend IntervalValue operator&(const IntervalValue &lhs, const IntervalValue &rhs);
63 friend IntervalValue operator|(const IntervalValue &lhs, const IntervalValue &rhs);
64 friend IntervalValue operator^(const IntervalValue &lhs, const IntervalValue &rhs);
65
66 bool isTop() const
67 {
68 return _lb.is_minus_infinity() && _ub.is_plus_infinity();
69 }
70
71 bool isBottom() const
72 {
73 return _lb.is_plus_infinity() && _ub.is_minus_infinity();
74 }
75
78 {
80 }
81
84 {
86 }
87
88 static bool is_infinite(const BoundedInt &e)
89 {
90 return e.is_infinity();
91 }
92
95 {
97 }
98
101 {
103 }
104
107
109 explicit IntervalValue(s64_t n) : _lb(n), _ub(n) {}
110
112
114
115 explicit IntervalValue(double n) : _lb(n), _ub(n) {}
116
118
120 explicit IntervalValue(BoundedInt lb, BoundedInt ub) : _lb(std::move(lb)), _ub(std::move(ub))
121 {
122 assert((isBottom() || _lb.leq(_ub)) && "lower bound should be less than or equal to upper bound");
123 }
124
126
127 explicit IntervalValue(double lb, double ub) : IntervalValue(BoundedInt(lb), BoundedInt(ub)) {}
128
129 explicit IntervalValue(float lb, float ub) : IntervalValue(BoundedInt(lb), BoundedInt(ub)) {}
130
132
134
136
138 IntervalValue(const IntervalValue &) = default;
139
142
145
148
151 {
152 if (this->isBottom() || other.isBottom())
153 {
154 return bottom();
155 }
156 else if (this->isTop() || other.isTop())
157 {
158 return top();
159 }
160 else
161 {
162 if (this->is_numeral() && other.is_numeral())
163 {
164 return eq(this->_lb, other._lb) ? IntervalValue(1, 1) : IntervalValue(0, 0);
165 }
166 else
167 {
168 IntervalValue value = *this;
169 value.meet_with(other);
170 if (value.isBottom())
171 {
172 return IntervalValue(0, 0);
173 }
174 else
175 {
176 // return top();
177 return IntervalValue(0, 1);
178 }
179 }
180 }
181 }
182
185 {
186 if (this->isBottom() || other.isBottom())
187 {
188 return bottom();
189 }
190 else if (this->isTop() || other.isTop())
191 {
192 return top();
193 }
194 else
195 {
196 if (this->is_numeral() && other.is_numeral())
197 {
198 return eq(this->_lb, other._lb) ? IntervalValue(0, 0) : IntervalValue(1, 1);
199 }
200 else
201 {
202 IntervalValue value = *this;
203 value.meet_with(other);
204 if (!value.isBottom())
205 {
206 return IntervalValue(0, 1);
207 }
208 else
209 {
210 return IntervalValue(1, 1);
211 }
212 }
213 }
214 }
215
217 ~IntervalValue() = default;
218
220 const BoundedInt &lb() const
221 {
222 assert(!this->isBottom() && "bottom interval does not have lower bound");
223 return this->_lb;
224 }
225
227 const BoundedInt &ub() const
228 {
229 assert(!this->isBottom() && "bottom interval does not have upper bound");
230 return this->_ub;
231 }
232
234 bool is_zero() const
235 {
236 return _lb.is_zero() && _ub.is_zero();
237 }
238
240 bool is_infinite() const
241 {
242 return _lb.is_infinity() || _ub.is_infinity();
243 }
244
245 bool is_int() const
246 {
247 return !is_real();
248 }
249
250 bool is_real() const
251 {
252 bool lb_real = _lb.is_real();
253 bool ub_real = _ub.is_real();
254 return lb_real || ub_real;
255 }
256
259 {
260 assert(is_numeral() && "this IntervalValue is not numeral");
261 return _lb.getNumeral();
262 }
263
265 {
266 assert(is_numeral() && "this IntervalValue is not numeral");
267 return _lb.getIntNumeral();
268 }
269
270 double getRealNumeral() const
271 {
272 assert(is_numeral() && "this IntervalValue is not numeral");
273 return _lb.getRealNumeral();
274 }
275
277 bool is_numeral() const
278 {
279 return eq(_lb, _ub);
280 }
281
284 {
285 this->_lb = plus_infinity();
286 this->_ub = minus_infinity();
287 }
288
291 {
292 this->_lb = minus_infinity();
293 this->_ub = plus_infinity();
294 }
295
300 bool containedWithin(const IntervalValue &other) const
301 {
302 if (this->isBottom())
303 {
304 return true;
305 }
306 else if (other.isBottom())
307 {
308 return false;
309 }
310 else
311 {
312 return other._lb.leq(this->_lb) && this->_ub.leq(other._ub);
313 }
314
315 }
316
321 bool contain(const IntervalValue &other) const
322 {
323 if (this->isBottom())
324 {
325 return true;
326 }
327 else if (other.isBottom())
328 {
329 return false;
330 }
331 else
332 {
333 return other._lb.geq(this->_lb) && this->_ub.geq(other._ub);
334 }
335 }
336
339 bool leq(const IntervalValue &other) const
340 {
341 if (this->isBottom())
342 {
343 return true;
344 }
345 else if (other.isBottom())
346 {
347 return false;
348 }
349 else
350 {
351 return this->_ub.leq(other._lb);
352 }
353 }
354
357 bool geq(const IntervalValue &other) const
358 {
359 if (this->isBottom())
360 {
361 return true;
362 }
363 else if (other.isBottom())
364 {
365 return false;
366 }
367 else
368 {
369 return this->_lb.geq(other._ub);
370 }
371 }
372
373
375 bool equals(const IntervalValue &other) const
376 {
377 if (this->isBottom())
378 {
379 return other.isBottom();
380 }
381 else if (other.isBottom())
382 {
383 return false;
384 }
385 else
386 {
387 if (this->is_real() && other.is_real())
388 {
389 return this->_lb.equal(other._lb) && this->_ub.equal(other._ub);
390 }
391 else if (this->is_int() && other.is_int())
392 {
393 return this->_lb.equal(other._lb) && this->_ub.equal(other._ub);
394 }
395 else if (this->is_int())
396 {
397 double thislb = this->_lb.getIntNumeral();
398 double thisub = this->_ub.getIntNumeral();
399 double otherlb = other._lb.getRealNumeral();
400 double otherub = other._ub.getRealNumeral();
401 return thislb == otherlb && thisub == otherub;
402 }
403 else
404 {
405 double thislb = this->_lb.getRealNumeral();
406 double thisub = this->_ub.getRealNumeral();
407 double otherlb = other._lb.getIntNumeral();
408 double otherub = other._ub.getIntNumeral();
409 return thislb == otherlb && thisub == otherub;
410 }
411 assert(false && "not implemented");
412 }
413 }
414
416 void join_with(const IntervalValue &other)
417 {
418 if (this->isBottom())
419 {
420 if (other.isBottom())
421 {
422 return;
423 }
424 else
425 {
426 setValue(other.lb(), other.ub());
427 }
428 }
429 else if (other.isBottom())
430 {
431 return;
432 }
433 else
434 {
435 setValue(min(this->lb(), other.lb()), max(this->ub(), other.ub()));
436 }
437 }
438
440 void widen_with(const IntervalValue &other)
441 {
442 if (this->isBottom())
443 {
444 this->_lb = other._lb;
445 this->_ub = other._ub;
446 }
447 else if (other.isBottom())
448 {
449 return;
450 }
451 else
452 {
453 setValue(!lb().leq(other.lb()) ? minus_infinity() : this->lb(), !ub().geq(other.ub()) ? plus_infinity() : this->ub());
454 }
455 }
456
458 void narrow_with(const IntervalValue &other)
459 {
460 if (this->isBottom() || other.isBottom())
461 {
462 this->set_to_bottom();
463 }
464 else if (other.isBottom())
465 {
466 return;
467 }
468 else
469 {
470 setValue(is_infinite(this->lb()) ? other._lb : this->_lb, is_infinite(this->ub()) ? other._ub : this->_ub);
471 }
472 }
473
475 void meet_with(const IntervalValue &other)
476 {
477 if (this->isBottom() || other.isBottom())
478 {
479 this->set_to_bottom();
480 }
481 else
482 {
483 if (!(max(this->_lb, other.lb()).leq(min(this->_ub, other.ub()))))
484 {
485 this->set_to_bottom();
486 }
487 else
488 {
489 setValue(max(this->_lb, other.lb()), min(this->_ub, other.ub()));
490 }
491 }
492 }
493
495 bool contains(int n) const
496 {
497 return this->_lb.leq(n) && this->_ub.geq(n);
498 }
499
500 void dump(std::ostream &o) const
501 {
502 if (this->isBottom())
503 {
504 o << "⊥";
505 }
506 else
507 {
508 o << "[" << this->_lb << ", " << this->_ub << "]";
509 }
510 }
511
512 const std::string toString() const
513 {
514 std::string str;
515 std::stringstream rawStr(str);
516 if (this->isBottom())
517 {
518 rawStr << "⊥";
519 }
520 else
521 {
522 rawStr << "[" << lb().to_string() << ", " << ub().to_string() << "]";
523 }
524 return rawStr.str();
525 }
526private:
528 void setValue(const BoundedInt &lb, const BoundedInt &ub)
529 {
530 assert(((lb.is_plus_infinity() && ub.is_minus_infinity()) ||
531 lb.leq(ub)) &&
532 "lower bound should be less than or equal to upper bound");
533 this->_lb = lb;
534 this->_ub = ub;
535 }
536
537private:
538 // internal use for create bottom-tolerant IntervalValue
540 {
541 if (!lb.leq(ub))
542 return IntervalValue::bottom();
543 else
544 return IntervalValue(lb, ub);
545 }
546}; // end class IntervalValue
547
550 const IntervalValue &rhs)
551{
552 if (lhs.isBottom() || rhs.isBottom())
553 {
554 return IntervalValue::bottom();
555 }
556 else if (lhs.isTop() || rhs.isTop())
557 {
558 return IntervalValue::top();
559 }
560 else
561 {
562 return IntervalValue(lhs.lb() + rhs.lb(), lhs.ub() + rhs.ub());
563 }
564}
565
568 const IntervalValue &rhs)
569{
570 if (lhs.isBottom() || rhs.isBottom())
571 {
572 return IntervalValue::bottom();
573 }
574 else if (lhs.isTop() || rhs.isTop())
575 {
576 return IntervalValue::top();
577 }
578 else
579 {
580 return IntervalValue(lhs.lb() - rhs.ub(), lhs.ub() - rhs.lb());
581 }
582}
583
586 const IntervalValue &rhs)
587{
588 if (lhs.isBottom() || rhs.isBottom())
589 {
590 return IntervalValue::bottom();
591 }
592 else
593 {
594 BoundedInt ll = lhs.lb() * rhs.lb();
595 BoundedInt lu = lhs.lb() * rhs.ub();
596 BoundedInt ul = lhs.ub() * rhs.lb();
597 BoundedInt uu = lhs.ub() * rhs.ub();
598 std::vector<BoundedInt> vec{ll, lu, ul, uu};
599 return IntervalValue(BoundedInt::min(vec),
600 BoundedInt::max(vec));
601 }
602}
603
606 const IntervalValue &rhs)
607{
608 if (lhs.isBottom() || rhs.isBottom())
609 {
610 return IntervalValue::bottom();
611 }
612 else if (rhs.contains(0))
613 {
616 IntervalValue l_res = lhs / lb;
617 IntervalValue r_res = lhs / ub;
618 l_res.join_with(r_res);
619 return l_res;
620 }
621 else if (lhs.contains(0))
622 {
625 IntervalValue l_res = lb / rhs;
626 IntervalValue r_res = ub / rhs;
627 l_res.join_with(r_res);
628 l_res.join_with(IntervalValue(0));
629 return l_res;
630 }
631 else
632 {
633 // Neither the dividend nor the divisor contains 0
634 BoundedInt ll = lhs.lb() / rhs.lb();
635 BoundedInt lu = lhs.lb() / rhs.ub();
636 BoundedInt ul = lhs.ub() / rhs.lb();
637 BoundedInt uu = lhs.ub() / rhs.ub();
638 std::vector<BoundedInt> vec{ll, lu, ul, uu};
639
641 BoundedInt::max(vec));
642 return res;
643 }
644}
645
648 const IntervalValue &rhs)
649{
650 if (lhs.isBottom() || rhs.isBottom())
651 {
652 return IntervalValue::bottom();
653 }
654 else if (rhs.contains(0))
655 {
656 return lhs.is_zero() ? IntervalValue(0, 0) : IntervalValue::top();
657 }
658 else if (lhs.is_numeral() && rhs.is_numeral())
659 {
660 return IntervalValue(lhs.lb() % rhs.lb(), lhs.lb() % rhs.lb());
661 }
662 else
663 {
664 BoundedInt n_ub = max(abs(lhs.lb()), abs(lhs.ub()));
665 BoundedInt d_ub = max(abs(rhs.lb()), rhs.ub()) - 1;
666 BoundedInt ub = min(n_ub, d_ub);
667
668 if (lhs.lb().getNumeral() < 0)
669 {
670 if (lhs.ub().getNumeral() > 0)
671 {
672 return IntervalValue(-ub, ub);
673 }
674 else
675 {
676 return IntervalValue(-ub, 0);
677 }
678 }
679 else
680 {
681 return IntervalValue(0, ub);
682 }
683 }
684}
685
686// Compare two IntervalValues for greater than
688{
689 // If either lhs or rhs is bottom, the result is bottom
690 if (lhs.isBottom() || rhs.isBottom())
691 {
692 return IntervalValue::bottom();
693 }
694 // If either lhs or rhs is top, the result is top
695 else if (lhs.isTop() || rhs.isTop())
696 {
697 return IntervalValue::top();
698 }
699 else
700 {
701 // If both lhs and rhs are numerals (lb = ub), directly compare their values
702 if (lhs.is_numeral() && rhs.is_numeral())
703 {
704 // It means lhs.lb() > rhs.lb()? true: false
705 return lhs.lb().leq(rhs.lb()) ? IntervalValue(0, 0) : IntervalValue(1, 1);
706 }
707 else
708 {
709 // Return [1,1] means lhs is totally greater than rhs
710 // When lhs.lb > rhs.ub, e.g., lhs:[3, 4] rhs:[1, 2]
711 // lhs.lb(3) > rhs.ub(2)
712 // It means lhs.lb() > rhs.ub()
713 if (!lhs.lb().leq(rhs.ub()))
714 {
715 return IntervalValue(1, 1);
716 }
717 // Return [0,0] means lhs is totally impossible to be greater than rhs
718 // i.e., lhs is totally less than or equal to rhs
719 // When lhs.ub <= rhs.lb, e.g., lhs:[3, 4] rhs:[4,5]
720 // lhs.ub(4) <= rhs.lb(4)
721 else if (lhs.ub().leq(rhs.lb()))
722 {
723 return IntervalValue(0, 0);
724 }
725 // For other cases, lhs can be greater than or not, depending on the values
726 // e.g., lhs: [2,4], rhs: [1,3],
727 // lhs can be greater than rhs if lhs is 4 and rhs is 1.
728 // lhs can also not be greater than rhs if lhs is 2 and rhs is 3
729 else
730 {
731 return IntervalValue(0, 1);
732 }
733 }
734 }
735}
736
737// Compare two IntervalValues for less than
739{
740 // If either lhs or rhs is bottom, the result is bottom
741 if (lhs.isBottom() || rhs.isBottom())
742 {
743 return IntervalValue::bottom();
744 }
745 // If either lhs or rhs is top, the result is top
746 else if (lhs.isTop() || rhs.isTop())
747 {
748 return IntervalValue::top();
749 }
750 else
751 {
752 // If both lhs and rhs are numerals (lb = ub), directly compare their values
753 if (lhs.is_numeral() && rhs.is_numeral())
754 {
755 // It means lhs.lb() < rhs.lb()? true: false
756 return lhs.lb().geq(rhs.lb()) ? IntervalValue(0, 0) : IntervalValue(1, 1);
757 }
758 else
759 {
760 // Return [1,1] means lhs is totally less than rhs
761 // When lhs.ub < rhs.lb, e.g., lhs:[1, 2] rhs:[3, 4]
762 // lhs.ub(2) < rhs.lb(3)
763 // It means lhs.ub() < rhs.lb()
764 if (!lhs.ub().geq(rhs.lb()))
765 {
766 return IntervalValue(1, 1);
767 }
768 // Return [0,0] means lhs is totally impossible to be less than rhs
769 // i.e., lhs is totally greater than or equal to rhs
770 // When lhs.lb >= rhs.ub, e.g., lhs:[4,5] rhs:[3,4]
771 // lhs.lb(4) >= rhs.ub(4)
772 else if (lhs.lb().geq(rhs.ub()))
773 {
774 return IntervalValue(0, 0);
775 }
776 // For other cases, lhs can be less than rhs or not, depending on the values
777 // e.g., lhs: [2,4], rhs: [1,3],
778 // lhs can be less than rhs if lhs is 2, rhs is 3.
779 // lhs can also not be less than rhs if lhs is 4 and rhs is 1
780 else
781 {
782 return IntervalValue(0, 1);
783 }
784 }
785 }
786}
787
788
789// Compare two IntervalValues for greater than or equal to
791{
792 // If either lhs or rhs is bottom, the result is bottom
793 if (lhs.isBottom() || rhs.isBottom())
794 {
795 return IntervalValue::bottom();
796 }
797 // If either lhs or rhs is top, the result is top
798 else if (lhs.isTop() || rhs.isTop())
799 {
800 return IntervalValue::top();
801 }
802 else
803 {
804 // If both lhs and rhs are numerals (lb = ub), directly compare their values
805 if (lhs.is_numeral() && rhs.is_numeral())
806 {
807 return lhs.lb().geq(rhs.lb()) ? IntervalValue(1, 1) : IntervalValue(0, 0);
808 }
809 else
810 {
811 // Return [1,1] means lhs is totally greater than or equal to rhs
812 // When lhs.lb >= rhs.ub, e.g., lhs:[2, 3] rhs:[1, 2]
813 // lhs.lb(2) >= rhs.ub(2)
814 if (lhs.lb().geq(rhs.ub()))
815 {
816 return IntervalValue(1, 1);
817 }
818 // Return [0,0] means lhs is totally impossible to be greater than or equal to rhs
819 // i.e., lhs is totally less than rhs
820 // When lhs.ub < rhs.lb, e.g., lhs:[1, 2] rhs:[3, 4]
821 // lhs.ub(2) < rhs.lb(3)
822 // It means lhs.ub() < rhs.lb()
823 else if (!lhs.ub().geq(rhs.lb()))
824 {
825 return IntervalValue(0, 0);
826 }
827 // For other cases, lhs can be greater than or equal to rhs or not, depending on the values
828 // e.g., lhs: [2,4], rhs: [1,3],
829 // lhs can be greater than or equal to rhs if lhs is 3, rhs is 2.
830 // lhs can also not be greater than or equal to rhs if lhs is 2 and rhs is 3
831 else
832 {
833 return IntervalValue(0, 1);
834 }
835 }
836 }
837}
838
839// Compare two IntervalValues for less than or equal to
841{
842 // If either lhs or rhs is bottom, the result is bottom
843 if (lhs.isBottom() || rhs.isBottom())
844 {
845 return IntervalValue::bottom();
846 }
847 // If either lhs or rhs is top, the result is top
848 else if (lhs.isTop() || rhs.isTop())
849 {
850 return IntervalValue::top();
851 }
852 else
853 {
854 // If both lhs and rhs are numerals (lb = ub), directly compare their values
855 if (lhs.is_numeral() && rhs.is_numeral())
856 {
857 return lhs.lb().leq(rhs.lb()) ? IntervalValue(1, 1) : IntervalValue(0, 0);
858 }
859 else
860 {
861 // Return [1,1] means lhs is totally less than or equal to rhs
862 // When lhs.ub <= rhs.lb, e.g., lhs:[1, 2] rhs:[2, 3]
863 // lhs.ub(2) <= rhs.lb(2)
864 if (lhs.ub().leq(rhs.lb()))
865 {
866 return IntervalValue(1, 1);
867 }
868 // Return [0,0] means lhs is totally impossible to be less than or equal to rhs
869 // i.e., lhs is totally greater than rhs
870 // When lhs.lb > rhs.ub, e.g., lhs:[3, 4] rhs:[1, 2]
871 // lhs.lb(3) > rhs.ub(2)
872 // It means lhs.lb() > rhs.ub()
873 else if (!lhs.lb().leq(rhs.ub()))
874 {
875 return IntervalValue(0, 0);
876 }
877 // For other cases, lhs can be less than or equal to rhs or not, depending on the values
878 // e.g., lhs: [2,4], rhs: [1,3],
879 // lhs can be less than or equal to rhs if lhs is 3, rhs is 3.
880 // lhs can also not be less than or equal to rhs if lhs is 3 and rhs is 2
881 else
882 {
883 return IntervalValue(0, 1);
884 }
885 }
886 }
887}
888
891{
892 if (lhs.isBottom() || rhs.isBottom())
893 return IntervalValue::bottom();
894 if (lhs.isTop() && rhs.isTop())
895 return IntervalValue::top();
896 else
897 {
898 IntervalValue shift = rhs;
900 if (shift.isBottom())
901 return IntervalValue::bottom();
902 // Zero stays zero for any admitted shift, even an unbounded one.
903 if (lhs.equals(IntervalValue(0)))
904 return lhs;
905 // 1 << 31 overflows a signed 32-bit coefficient and may create an
906 // invalid interval whose lower bound is greater than its upper bound.
907 if (shift.ub().is_infinity() || shift.ub().getIntNumeral() >= 31)
908 return IntervalValue::top();
909 const s32_t low = static_cast<s32_t>(shift.lb().getIntNumeral());
910 const s32_t high = static_cast<s32_t>(shift.ub().getIntNumeral());
911 return lhs * IntervalValue(s64_t(1) << low, s64_t(1) << high);
912 }
913}
914
917{
918 if (lhs.isBottom() || rhs.isBottom())
919 return IntervalValue::bottom();
920 else if (lhs.isTop() && rhs.isTop())
921 return IntervalValue::top();
922 else
923 {
924 IntervalValue shift = rhs;
926 if (shift.isBottom())
927 return IntervalValue::bottom();
928 if (lhs.contains(0))
929 {
932 IntervalValue tmp = l >> rhs;
933 tmp.join_with(u >> rhs);
934 tmp.join_with(IntervalValue(0));
935 return tmp;
936 }
937 else
938 {
939 BoundedInt ll = lhs.lb() >> shift.lb();
940 BoundedInt lu = lhs.lb() >> shift.ub();
941 BoundedInt ul = lhs.ub() >> shift.lb();
942 BoundedInt uu = lhs.ub() >> shift.ub();
943 std::vector<BoundedInt> vec{ll, lu, ul, uu};
944 return IntervalValue(BoundedInt::min(vec),
945 BoundedInt::max(vec));
946 }
947 }
948}
949
952{
953 if (lhs.isBottom() || rhs.isBottom())
954 return IntervalValue::bottom();
955 else if (lhs.is_numeral() && rhs.is_numeral())
956 {
957 return IntervalValue(lhs.lb() & rhs.lb());
958 }
959 else if (lhs.lb().getNumeral() >= 0 && rhs.lb().getNumeral() >= 0)
960 {
961 return IntervalValue((s64_t) 0, min(lhs.ub(), rhs.ub()));
962 }
963 else if (lhs.lb().getNumeral() >= 0)
964 {
965 return IntervalValue((s64_t) 0, lhs.ub());
966 }
967 else if (rhs.lb().getNumeral() >= 0)
968 {
969 return IntervalValue((s64_t) 0, rhs.ub());
970 }
971 else
972 {
973 return IntervalValue::top();
974 }
975}
976
979{
980 auto next_power_of_2 = [](s64_t num)
981 {
982 int i = 1;
983 while ((num >> i) != 0)
984 {
985 ++i;
986 }
987 return 1 << i;
988 };
989 if (lhs.isBottom() || rhs.isBottom())
990 return IntervalValue::bottom();
991 else if (lhs.is_numeral() && rhs.is_numeral())
992 return IntervalValue(lhs.lb() | rhs.lb());
993 else if (lhs.lb().getNumeral() >= 0 && !lhs.ub().is_infinity() &&
994 rhs.lb().getNumeral() >= 0 && !rhs.ub().is_infinity())
995 {
996 s64_t m = std::max(lhs.ub().getNumeral(), rhs.ub().getNumeral());
997 s64_t ub = next_power_of_2(s64_t(m)) - 1;
998 return IntervalValue((s64_t) 0, (s64_t) ub);
999 }
1000 else
1001 {
1002 return IntervalValue::top();
1003 }
1004}
1005
1008{
1009 auto next_power_of_2 = [](s64_t num)
1010 {
1011 int i = 1;
1012 while ((num >> i) != 0)
1013 {
1014 ++i;
1015 }
1016 return 1 << i;
1017 };
1018 if (lhs.isBottom() || rhs.isBottom())
1019 return IntervalValue::bottom();
1020 else if (lhs.is_numeral() && rhs.is_numeral())
1021 return IntervalValue(lhs.lb() ^ rhs.lb());
1022 else if (lhs.lb().getNumeral() >= 0 && !lhs.ub().is_infinity() &&
1023 rhs.lb().getNumeral() >= 0 && !rhs.ub().is_infinity())
1024 {
1025 s64_t m = std::max(lhs.ub().getNumeral(), rhs.ub().getNumeral());
1026 s64_t ub = next_power_of_2(s64_t(m)) - 1;
1027 return IntervalValue((s64_t) 0, (s64_t) ub);
1028 }
1029 else
1030 {
1031 return IntervalValue::top();
1032 }
1033}
1034
1036inline std::ostream &operator<<(std::ostream &o,
1038{
1040 return o;
1041}
1042
1043} // end namespace SVF
1044#endif //Z3_EXAMPLE_IntervalValue_H
cJSON * a
Definition cJSON.cpp:2560
cJSON * n
Definition cJSON.cpp:2558
A class representing a bounded 64-bit integer.
static BoundedInt plus_infinity()
s64_t getNumeral() const
Retrieves the numeral value of the BoundedInt object.
bool is_infinity() const
friend BoundedInt max(const BoundedInt &lhs, const BoundedInt &rhs)
s64_t getIntNumeral() const
static BoundedInt minus_infinity()
friend BoundedInt min(const BoundedInt &lhs, const BoundedInt &rhs)
bool geq(const BoundedInt &rhs) const
bool equal(const BoundedInt &rhs) const
bool leq(const BoundedInt &rhs) const
double getRealNumeral() const
virtual const std::string to_string() const
IntervalValue & operator=(const IntervalValue &a)=default
Copy assignment operator.
void meet_with(const IntervalValue &other)
Return a intersected IntervalValue.
IntervalValue operator!=(const IntervalValue &other) const
Equality comparison.
friend IntervalValue operator/(const IntervalValue &lhs, const IntervalValue &rhs)
Divide IntervalValues.
static BoundedInt minus_infinity()
Get minus infinity -inf.
bool isTop() const
bool is_int() const
IntervalValue(s64_t n)
Create the IntervalValue [n, n].
bool leq(const IntervalValue &other) const
IntervalValue(u32_t lb, u32_t ub)
bool geq(const IntervalValue &other) const
const BoundedInt & ub() const
Return the upper bound.
IntervalValue(u64_t lb, u64_t ub)
void join_with(const IntervalValue &other)
Current IntervalValue joins with another IntervalValue.
IntervalValue & operator=(IntervalValue &&)=default
Move assignment operator.
IntervalValue operator==(const IntervalValue &other) const
Equality comparison.
friend IntervalValue operator&(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise AND of IntervalValues.
s64_t getIntNumeral() const
IntervalValue(double lb, double ub)
bool is_numeral() const
Return true if the IntervalValue is a number [num, num].
friend IntervalValue operator>>(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.
const std::string toString() const
friend IntervalValue operator^(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise XOR of IntervalValues.
bool isBottom() const
void narrow_with(const IntervalValue &other)
Current IntervalValue narrow with another IntervalValue.
IntervalValue(s64_t lb, s64_t ub)
bool is_infinite() const
Return true if the IntervalValue is infinite IntervalValue.
IntervalValue(BoundedInt n)
void set_to_top()
Set current IntervalValue as top.
friend IntervalValue operator|(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise OR of IntervalValues.
~IntervalValue()=default
Destructor.
friend IntervalValue operator*(const IntervalValue &lhs, const IntervalValue &rhs)
Multiply IntervalValues.
friend IntervalValue operator<<(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.
bool is_zero() const
Return true if the IntervalValue is [0, 0].
static IntervalValue bottom()
Create the bottom IntervalValue [+inf, -inf].
double getRealNumeral() const
bool contain(const IntervalValue &other) const
friend IntervalValue operator-(const IntervalValue &lhs, const IntervalValue &rhs)
Subtract IntervalValues.
static BoundedInt plus_infinity()
Get plus infinity +inf.
void widen_with(const IntervalValue &other)
Current IntervalValue widen with another IntervalValue.
IntervalValue(float lb, float ub)
static IntervalValue create(const BoundedInt &lb, const BoundedInt &ub)
bool containedWithin(const IntervalValue &other) const
bool equals(const IntervalValue &other) const
Equality comparison.
IntervalValue(s32_t lb, s32_t ub)
void dump(std::ostream &o) const
void set_to_bottom()
Set current IntervalValue as bottom.
void setValue(const BoundedInt &lb, const BoundedInt &ub)
Set the lower bound.
IntervalValue()
Create default IntervalValue.
IntervalValue(BoundedInt lb, BoundedInt ub)
Create the IntervalValue [lb, ub].
static IntervalValue top()
Create the IntervalValue [-inf, +inf].
s64_t getNumeral() const
Return.
IntervalValue(IntervalValue &&)=default
Move constructor.
friend IntervalValue operator+(const IntervalValue &lhs, const IntervalValue &rhs)
Add IntervalValues.
static bool is_infinite(const BoundedInt &e)
bool is_real() const
const BoundedInt & lb() const
Return the lower bound.
IntervalValue(const IntervalValue &)=default
Copy constructor.
bool contains(int n) const
Return true if the IntervalValue contains n.
for isBitcode
Definition BasicTypes.h:70
IntervalValue operator>=(const IntervalValue &lhs, const IntervalValue &rhs)
IntervalValue operator<=(const IntervalValue &lhs, const IntervalValue &rhs)
IntervalValue operator/(const IntervalValue &lhs, const IntervalValue &rhs)
Divide IntervalValues.
IntervalValue operator%(const IntervalValue &lhs, const IntervalValue &rhs)
Divide IntervalValues.
unsigned long long u64_t
Definition GeneralType.h:69
IntervalValue operator>>(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.
IntervalValue operator-(const IntervalValue &lhs, const IntervalValue &rhs)
Subtract IntervalValues.
IntervalValue operator^(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise XOR of IntervalValues.
IntervalValue operator&(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise AND of IntervalValues.
signed s32_t
Definition GeneralType.h:68
IntervalValue operator|(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise OR of IntervalValues.
IntervalValue operator*(const IntervalValue &lhs, const IntervalValue &rhs)
Multiply IntervalValues.
unsigned u32_t
Definition GeneralType.h:67
signed long long s64_t
Definition GeneralType.h:70
IntervalValue operator+(const IntervalValue &lhs, const IntervalValue &rhs)
Add IntervalValues.
IntervalValue operator<(const IntervalValue &lhs, const IntervalValue &rhs)
IntervalValue operator>(const IntervalValue &lhs, const IntervalValue &rhs)
IntervalValue operator<<(const IntervalValue &lhs, const IntervalValue &rhs)
Left binary shift of IntervalValues.