Static Value-Flow Analysis
Loading...
Searching...
No Matches
NumericValue.h
Go to the documentation of this file.
1//===- NumericValue.h ----Numeric Value-------------------------//
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 * Numeri Value.h
24 *
25 * Created on: May 11, 2024
26 * Author: Xiao Cheng, Jiawei Ren
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 SVF_NUMERICVALUE_H
34#define SVF_NUMERICVALUE_H
35
36#include <cfloat> // For DBL_MAX
37#include <cmath>
38#include <cstdlib>
39#include <limits>
40#include <utility>
41
42#include "Util/GeneralType.h"
43
44#define epsilon std::numeric_limits<double>::epsilon();
45namespace SVF
46{
47
58{
59protected:
60 s64_t _iVal; // The 64-bit integer value.
61 bool _isInf; // True if the value is infinite. If true, _iVal == 1
62 // represents positive infinity and _iVal == -1 represents
63 // negative infinity.
64
65 // Default constructor is protected to prevent creating an object without
66 // initializing _iVal and _isInf.
67 BoundedInt() = default;
68
69public:
70 // Constructs a BoundedInt with the given 64-bit integer value. The value is
71 // not infinite.
72 BoundedInt(s64_t fVal) : _iVal(fVal), _isInf(false) {}
73
74 // Constructs a BoundedInt with the given 64-bit integer value and infinity
75 // flag.
76 BoundedInt(s64_t fVal, bool isInf) : _iVal(fVal), _isInf(isInf) {}
77
78 // Copy constructor.
79 BoundedInt(const BoundedInt& rhs) : _iVal(rhs._iVal), _isInf(rhs._isInf) {}
80
81 // Copy assignment operator.
83 {
84 _iVal = rhs._iVal;
85 _isInf = rhs._isInf;
86 return *this;
87 }
88
89 // Move constructor.
91
92 // Move assignment operator.
94 {
95 _iVal = rhs._iVal;
96 _isInf = rhs._isInf;
97 return *this;
98 }
99
100 // Virtual destructor.
101 virtual ~BoundedInt() {}
102
103 // Checks if the BoundedInt represents positive infinity.
104 bool is_plus_infinity() const
105 {
106 return _isInf && _iVal == 1;
107 }
108
109 // Checks if the BoundedInt represents negative infinity.
110 bool is_minus_infinity() const
111 {
112 return _isInf && _iVal == -1;
113 }
114
115 // Checks if the BoundedInt represents either positive or negative infinity.
116 bool is_infinity() const
117 {
119 }
120
121 // Sets the BoundedInt to represent positive infinity.
123 {
124 *this = plus_infinity();
125 }
126
127 // Sets the BoundedInt to represent negative infinity.
129 {
130 *this = minus_infinity();
131 }
132
133 // Returns a BoundedInt representing positive infinity.
135 {
136 return {1, true};
137 }
138
139 // Returns a BoundedInt representing negative infinity.
141 {
142 return {-1, true};
143 }
144
145 // Checks if the BoundedInt represents zero.
146 bool is_zero() const
147 {
148 return _iVal == 0;
149 }
150
151 // Checks if the given BoundedInt represents zero.
152 static bool isZero(const BoundedInt& expr)
153 {
154 return expr._iVal == 0;
155 }
156
157 // Checks if the BoundedInt is equal to another BoundedInt.
158 bool equal(const BoundedInt& rhs) const
159 {
160 return _iVal == rhs._iVal && _isInf == rhs._isInf;
161 }
162
163 // Checks if the BoundedInt is less than or equal to another BoundedInt.
164 bool leq(const BoundedInt& rhs) const
165 {
166 // If only one of the two BoundedInts is infinite.
167 if (is_infinity() ^ rhs.is_infinity())
168 {
169 if (is_infinity())
170 {
171 return is_minus_infinity();
172 }
173 else
174 {
175 return rhs.is_plus_infinity();
176 }
177 }
178 // If both BoundedInts are infinite.
179 if (is_infinity() && rhs.is_infinity())
180 {
181 if (is_minus_infinity())
182 return true;
183 else
184 return rhs.is_plus_infinity();
185 }
186 // If neither BoundedInt is infinite.
187 else
188 return _iVal <= rhs._iVal;
189 }
190
191 // Checks if the BoundedInt is greater than or equal to another BoundedInt.
192 bool geq(const BoundedInt& rhs) const
193 {
194 // If only one of the two BoundedInts is infinite.
195 if (is_infinity() ^ rhs.is_infinity())
196 {
197 if (is_infinity())
198 {
199 return is_plus_infinity();
200 }
201 else
202 {
203 return rhs.is_minus_infinity();
204 }
205 }
206 // If both BoundedInts are infinite.
207 if (is_infinity() && rhs.is_infinity())
208 {
209 if (is_plus_infinity())
210 return true;
211 else
212 return rhs.is_minus_infinity();
213 }
214 // If neither BoundedInt is infinite.
215 else
216 return _iVal >= rhs._iVal;
217 }
218
220 //{%
221 // Overloads the equality operator to compare two BoundedInt objects.
222 friend bool operator==(const BoundedInt& lhs, const BoundedInt& rhs)
223 {
224 return lhs.equal(rhs);
225 }
226
227 // Overloads the inequality operator to compare two BoundedInt objects.
228 friend bool operator!=(const BoundedInt& lhs, const BoundedInt& rhs)
229 {
230 return !lhs.equal(rhs);
231 }
232
233 // Overloads the greater than operator to compare two BoundedInt objects.
234 friend bool operator>(const BoundedInt& lhs, const BoundedInt& rhs)
235 {
236 return !lhs.leq(rhs);
237 }
238
239 // Overloads the less than operator to compare two BoundedInt objects.
240 friend bool operator<(const BoundedInt& lhs, const BoundedInt& rhs)
241 {
242 return !lhs.geq(rhs);
243 }
244
245 // Overloads the less than or equal to operator to compare two BoundedInt
246 // objects.
247 friend bool operator<=(const BoundedInt& lhs, const BoundedInt& rhs)
248 {
249 return lhs.leq(rhs);
250 }
251
252 // Overloads the greater than or equal to operator to compare two BoundedInt
253 // objects.
254 friend bool operator>=(const BoundedInt& lhs, const BoundedInt& rhs)
255 {
256 return lhs.geq(rhs);
257 }
258
283 static BoundedInt safeNeg(const BoundedInt& value)
284 {
285 if (value.is_plus_infinity())
286 return minus_infinity();
287 if (value.is_minus_infinity())
288 return plus_infinity();
289 if (value._iVal == std::numeric_limits<s64_t>::min())
290 return plus_infinity();
291 return -value._iVal;
292 }
293
294 static BoundedInt safeAdd(const BoundedInt& lhs, const BoundedInt& rhs)
295 {
296 if ((lhs.is_plus_infinity() && rhs.is_minus_infinity()) ||
297 (lhs.is_minus_infinity() && rhs.is_plus_infinity()))
298 {
299 assert(false && "invalid add");
300 abort();
301 }
302 if (lhs.is_plus_infinity() || rhs.is_plus_infinity())
303 return plus_infinity();
304 if (lhs.is_minus_infinity() || rhs.is_minus_infinity())
305 return minus_infinity();
306
307 const s64_t min = std::numeric_limits<s64_t>::min();
308 const s64_t max = std::numeric_limits<s64_t>::max();
309 if (rhs._iVal > 0 && lhs._iVal > max - rhs._iVal)
310 return plus_infinity();
311 if (rhs._iVal < 0 && lhs._iVal < min - rhs._iVal)
312 return minus_infinity();
313 return lhs._iVal + rhs._iVal;
314 }
315
316 static BoundedInt safeSub(const BoundedInt& lhs, const BoundedInt& rhs)
317 {
318 if ((lhs.is_plus_infinity() && rhs.is_plus_infinity()) ||
319 (lhs.is_minus_infinity() && rhs.is_minus_infinity()))
320 {
321 assert(false && "invalid subtract");
322 abort();
323 }
324 if (lhs.is_plus_infinity() || rhs.is_minus_infinity())
325 return plus_infinity();
326 if (lhs.is_minus_infinity() || rhs.is_plus_infinity())
327 return minus_infinity();
328
329 const s64_t min = std::numeric_limits<s64_t>::min();
330 const s64_t max = std::numeric_limits<s64_t>::max();
331 if (rhs._iVal < 0 && lhs._iVal > max + rhs._iVal)
332 return plus_infinity();
333 if (rhs._iVal > 0 && lhs._iVal < min + rhs._iVal)
334 return minus_infinity();
335 return lhs._iVal - rhs._iVal;
336 }
337
338 static BoundedInt safeMul(const BoundedInt& lhs, const BoundedInt& rhs)
339 {
340 if (lhs.is_zero() || rhs.is_zero())
341 return 0;
342
343 if (lhs.is_infinity() || rhs.is_infinity())
344 {
345 const bool negative = (lhs._iVal < 0) != (rhs._iVal < 0);
346 return negative ? minus_infinity() : plus_infinity();
347 }
348
349 const s64_t min = std::numeric_limits<s64_t>::min();
350 const s64_t max = std::numeric_limits<s64_t>::max();
351 if (lhs._iVal > 0)
352 {
353 if (rhs._iVal > 0 && lhs._iVal > max / rhs._iVal)
354 return plus_infinity();
355 if (rhs._iVal < 0 && rhs._iVal < min / lhs._iVal)
356 return minus_infinity();
357 }
358 else
359 {
360 if (rhs._iVal > 0 && lhs._iVal < min / rhs._iVal)
361 return minus_infinity();
362 if (rhs._iVal < 0 && lhs._iVal < max / rhs._iVal)
363 return plus_infinity();
364 }
365 return lhs._iVal * rhs._iVal;
366 }
367
368 static BoundedInt safeDiv(const BoundedInt& lhs, const BoundedInt& rhs)
369 {
370 if (rhs.is_zero())
371 {
372 assert(false && "divide by zero");
373 abort();
374 }
375 if (!lhs.is_infinity() && !rhs.is_infinity())
376 {
377 if (lhs._iVal == std::numeric_limits<s64_t>::min() &&
378 rhs._iVal == -1)
379 return plus_infinity();
380 return lhs._iVal / rhs._iVal;
381 }
382 if (!lhs.is_infinity())
383 return 0;
384
385 const bool negative = (lhs._iVal < 0) != (rhs._iVal < 0);
386 return negative ? minus_infinity() : plus_infinity();
387 }
388
389 static BoundedInt safeRem(const BoundedInt& lhs, const BoundedInt& rhs)
390 {
391 if (rhs.is_zero())
392 {
393 assert(false && "divide by zero");
394 abort();
395 }
396 if (!lhs.is_infinity() && !rhs.is_infinity())
397 {
398 if (lhs._iVal == std::numeric_limits<s64_t>::min() &&
399 rhs._iVal == -1)
400 return 0;
401 return lhs._iVal % rhs._iVal;
402 }
403 if (!lhs.is_infinity())
404 return 0;
405 if (!rhs.is_infinity())
406 return rhs._iVal > 0 ? lhs : safeNeg(lhs);
407 return lhs.equal(rhs) ? plus_infinity() : minus_infinity();
408 }
409
410 friend BoundedInt operator-(const BoundedInt& value)
411 {
412 return safeNeg(value);
413 }
414
415 friend BoundedInt operator+(const BoundedInt& lhs, const BoundedInt& rhs)
416 {
417 return safeAdd(lhs, rhs);
418 }
419
420 friend BoundedInt operator-(const BoundedInt& lhs, const BoundedInt& rhs)
421 {
422 return safeSub(lhs, rhs);
423 }
424
425 friend BoundedInt operator*(const BoundedInt& lhs, const BoundedInt& rhs)
426 {
427 return safeMul(lhs, rhs);
428 }
429
430 friend BoundedInt operator/(const BoundedInt& lhs, const BoundedInt& rhs)
431 {
432 return safeDiv(lhs, rhs);
433 }
434
435 friend BoundedInt operator%(const BoundedInt& lhs, const BoundedInt& rhs)
436 {
437 return safeRem(lhs, rhs);
438 }
439
440 // Overload bitwise operators for BoundedInt objects. These operators
441 // directly apply the corresponding bitwise operators to the internal
442 // integer values of the BoundedInt objects.
443
444 // Overloads the bitwise XOR operator for BoundedInt objects.
445 friend BoundedInt operator^(const BoundedInt& lhs, const BoundedInt& rhs)
446 {
447 return lhs._iVal ^ rhs._iVal;
448 }
449
450 // Overloads the bitwise AND operator for BoundedInt objects.
451 friend BoundedInt operator&(const BoundedInt& lhs, const BoundedInt& rhs)
452 {
453 return lhs._iVal & rhs._iVal;
454 }
455
456 // Overloads the bitwise OR operator for BoundedInt objects.
457 friend BoundedInt operator|(const BoundedInt& lhs, const BoundedInt& rhs)
458 {
459 return lhs._iVal | rhs._iVal;
460 }
461
462 // Overload logical operators for BoundedInt objects. These operators
463 // directly apply the corresponding logical operators to the internal
464 // integer values of the BoundedInt objects.
465
466 // Overloads the logical AND operator for BoundedInt objects.
467 friend BoundedInt operator&&(const BoundedInt& lhs, const BoundedInt& rhs)
468 {
469 return lhs._iVal && rhs._iVal;
470 }
471
472 // Overloads the logical OR operator for BoundedInt objects.
473 friend BoundedInt operator||(const BoundedInt& lhs, const BoundedInt& rhs)
474 {
475 return lhs._iVal || rhs._iVal;
476 }
477
478 // Overloads the logical NOT operator for BoundedInt objects.
479 friend BoundedInt operator!(const BoundedInt& lhs)
480 {
481 return !lhs._iVal;
482 }
483
484 // Overloads the right shift operator for BoundedInt objects.
485 // This operation is safe as long as the right-hand side is non-negative.
486 // If the left-hand side is zero or infinity, the result is the same as the
487 // left-hand side. If the right-hand side is infinity, the result depends on
488 // the sign of the left-hand side.
489 friend BoundedInt operator>>(const BoundedInt& lhs, const BoundedInt& rhs)
490 {
491 assert(rhs.geq(0) && "rhs should be greater or equal than 0");
492 if (lhs.is_zero())
493 return lhs;
494 else if (lhs.is_infinity())
495 return lhs;
496 else if (rhs.is_infinity())
497 return lhs.geq(0) ? 0 : -1;
498 else
499 return lhs._iVal >> rhs._iVal;
500 }
501
502 // Overloads the left shift operator for BoundedInt objects.
503 // This operation is safe as long as the right-hand side is non-negative.
504 // If the left-hand side is zero or infinity, the result is the same as the
505 // left-hand side. If the right-hand side is infinity, the result depends on
506 // the sign of the left-hand side.
507 friend BoundedInt operator<<(const BoundedInt& lhs, const BoundedInt& rhs)
508 {
509 assert(rhs.geq(0) && "rhs should be greater or equal than 0");
510 if (lhs.is_zero())
511 return lhs;
512 else if (lhs.is_infinity())
513 return lhs;
514 else if (rhs.is_infinity())
515 return lhs.geq(0) ? plus_infinity() : minus_infinity();
516 else
517 return lhs._iVal << rhs._iVal;
518 }
519
520 // Overloads the ternary if-then-else operator for BoundedInt objects.
521 // The condition is evaluated as a boolean, and the result is either the
522 // second or third argument depending on the condition.
523 friend BoundedInt ite(const BoundedInt& cond, const BoundedInt& lhs,
524 const BoundedInt& rhs)
525 {
526 return cond._iVal != 0 ? lhs : rhs;
527 }
528
529 // Overloads the stream insertion operator for BoundedInt objects.
530 // This allows BoundedInt objects to be printed directly using std::cout or
531 // other output streams.
532 friend std::ostream& operator<<(std::ostream& out, const BoundedInt& expr)
533 {
534 out << expr._iVal;
535 return out;
536 }
537
538 // Defines a function to compare two BoundedInt objects for equality.
539 // This function directly compares the internal integer values of the
540 // BoundedInt objects.
541 friend bool eq(const BoundedInt& lhs, const BoundedInt& rhs)
542 {
543 return lhs._iVal == rhs._iVal && lhs._isInf == rhs._isInf;
544 }
545
546 // Defines a function to find the minimum of two BoundedInt objects.
547 // This function directly compares the internal integer values of the BoundedInt objects,
548 // and also checks if either of them represents infinity.
549 friend BoundedInt min(const BoundedInt& lhs, const BoundedInt& rhs)
550 {
551 if (lhs.is_minus_infinity() || rhs.is_minus_infinity())
552 return minus_infinity();
553 else if(lhs.is_plus_infinity())
554 return rhs;
555 else if(rhs.is_plus_infinity())
556 return lhs;
557 else
558 return BoundedInt(std::min(lhs._iVal, rhs._iVal));
559 }
560
561
562 // Defines a function to find the maximum of two BoundedInt objects.
563 // This function directly compares the internal integer values of the BoundedInt objects,
564 // and also checks if either of them represents infinity.
565 friend BoundedInt max(const BoundedInt& lhs, const BoundedInt& rhs)
566 {
567 if (lhs.is_plus_infinity() || rhs.is_plus_infinity())
568 return plus_infinity();
569 else if(lhs.is_minus_infinity())
570 return rhs;
571 else if(rhs.is_minus_infinity())
572 return lhs;
573 else
574 return BoundedInt(std::max(lhs._iVal, rhs._iVal));
575 }
576
577
578 // Defines a function to find the minimum of a vector of BoundedInt objects.
579 // This function iterates over the vector and returns the smallest
580 // BoundedInt object.
581 static BoundedInt min(std::vector<BoundedInt>& _l)
582 {
584 for (const auto& it : _l)
585 {
586 if (it.is_minus_infinity())
587 return minus_infinity();
588 else if (!it.geq(ret))
589 {
590 ret = it;
591 }
592 }
593 return ret;
594 }
595
596 // Defines a function to find the maximum of a vector of BoundedInt objects.
597 // This function iterates over the vector and returns the largest BoundedInt
598 // object.
599 static BoundedInt max(std::vector<BoundedInt>& _l)
600 {
602 for (const auto& it : _l)
603 {
604 if (it.is_plus_infinity())
605 return plus_infinity();
606 else if (!it.leq(ret))
607 {
608 ret = it;
609 }
610 }
611 return ret;
612 }
613
614 // Defines a function to find the absolute value of a BoundedInt object.
615 // This function directly applies the unary minus operator if the BoundedInt
616 // object is negative.
617 friend BoundedInt abs(const BoundedInt& lhs)
618 {
619 return lhs.leq(0) ? -lhs : lhs;
620 }
621
622 // Defines a method to check if a BoundedInt object is true.
623 // A BoundedInt object is considered true if its internal integer value is
624 // non-zero.
625 inline bool is_true() const
626 {
627 return _iVal != 0;
628 }
629
641 inline s64_t getNumeral() const
642 {
643 // If the object represents negative infinity, return the minimum
644 // representable 64-bit integer.
645 if (is_minus_infinity())
646 {
647 return std::numeric_limits<s64_t>::min();
648 }
649 // If the object represents positive infinity, return the maximum
650 // representable 64-bit integer.
651 else if (is_plus_infinity())
652 {
653 return std::numeric_limits<s64_t>::max();
654 }
655 // Otherwise, return the actual 64-bit integer value of the object.
656 else
657 {
658 return _iVal;
659 }
660 }
661
662 inline virtual const std::string to_string() const
663 {
664 if (is_minus_infinity())
665 {
666 return "-oo";
667 }
668 if (is_plus_infinity())
669 {
670 return "+oo";
671 }
672 else
673 return std::to_string(_iVal);
674 }
675
676//%}
677
678 bool is_real() const
679 {
680 return false;
681 }
682
683 inline s64_t getIntNumeral() const
684 {
685 return getNumeral();
686 }
687
688 inline double getRealNumeral() const
689 {
690 assert(false && "cannot get real number for integer!");
691 abort();
692 }
693
694 const double getFVal() const
695 {
696 assert(false && "cannot get real number for integer!");
697 abort();
698 }
699};
700
704{
705protected:
706 double _fVal;
707
708 BoundedDouble() = default;
709
710public:
711 BoundedDouble(double fVal) : _fVal(fVal) {}
712
714
716 {
717 _fVal = rhs._fVal;
718 return *this;
719 }
720
721 BoundedDouble(BoundedDouble&& rhs) : _fVal(std::move(rhs._fVal)) {}
722
724 {
725 _fVal = std::move(rhs._fVal);
726 return *this;
727 }
728
729 virtual ~BoundedDouble() {}
730
731 static bool doubleEqual(double a, double b)
732 {
733 if (std::isinf(a) && std::isinf(b))
734 return a == b;
735 return std::fabs(a - b) < epsilon;
736 }
737
738 const double getFVal() const
739 {
740 return _fVal;
741 }
742
743 bool is_plus_infinity() const
744 {
745 return _fVal == std::numeric_limits<double>::infinity();
746 }
747
748 bool is_minus_infinity() const
749 {
750 return _fVal == -std::numeric_limits<double>::infinity();
751 }
752
753 bool is_infinity() const
754 {
756 }
757
759 {
760 *this = plus_infinity();
761 }
762
764 {
765 *this = minus_infinity();
766 }
767
769 {
770 return std::numeric_limits<double>::infinity();
771 }
772
774 {
775 return -std::numeric_limits<double>::infinity();
776 }
777
778 bool is_zero() const
779 {
780 return doubleEqual(_fVal, 0.0f);
781 }
782
783 static bool isZero(const BoundedDouble& expr)
784 {
785 return doubleEqual(expr.getFVal(), 0.0f);
786 }
787
788 bool equal(const BoundedDouble& rhs) const
789 {
790 return doubleEqual(_fVal, rhs._fVal);
791 }
792
793 bool leq(const BoundedDouble& rhs) const
794 {
795 if (is_infinity() ^ rhs.is_infinity())
796 {
797 if (is_infinity())
798 {
799 return is_minus_infinity();
800 }
801 else
802 {
803 return rhs.is_plus_infinity();
804 }
805 }
806 if (is_infinity() && rhs.is_infinity())
807 {
808 if (is_minus_infinity())
809 return true;
810 else
811 return rhs.is_plus_infinity();
812 }
813 else
814 return _fVal <= rhs._fVal;
815 }
816
817 bool geq(const BoundedDouble& rhs) const
818 {
819 if (is_infinity() ^ rhs.is_infinity())
820 {
821 if (is_infinity())
822 {
823 return is_plus_infinity();
824 }
825 else
826 {
827 return rhs.is_minus_infinity();
828 }
829 }
830 if (is_infinity() && rhs.is_infinity())
831 {
832 if (is_plus_infinity())
833 return true;
834 else
835 return rhs.is_minus_infinity();
836 }
837 else
838 return _fVal >= rhs._fVal;
839 }
840
842 //{%
843 friend bool operator==(const BoundedDouble& lhs,
844 const BoundedDouble& rhs)
845 {
846 return lhs.equal(rhs);
847 }
848
849 friend bool operator!=(const BoundedDouble& lhs,
850 const BoundedDouble& rhs)
851 {
852 return !lhs.equal(rhs);
853 }
854
855 friend bool operator>(const BoundedDouble& lhs,
856 const BoundedDouble& rhs)
857 {
858 return !lhs.leq(rhs);
859 }
860
861 friend bool operator<(const BoundedDouble& lhs,
862 const BoundedDouble& rhs)
863 {
864 return !lhs.geq(rhs);
865 }
866
867 friend bool operator<=(const BoundedDouble& lhs,
868 const BoundedDouble& rhs)
869 {
870 return lhs.leq(rhs);
871 }
872
873 friend bool operator>=(const BoundedDouble& lhs,
874 const BoundedDouble& rhs)
875 {
876 return lhs.geq(rhs);
877 }
878
888 static double safeAdd(double lhs, double rhs)
889 {
890 if ((lhs == std::numeric_limits<double>::infinity() &&
891 rhs == -std::numeric_limits<double>::infinity()) ||
892 (lhs == -std::numeric_limits<double>::infinity() &&
893 rhs == std::numeric_limits<double>::infinity()))
894 {
895 assert(false && "invalid add");
896 }
897 double res =
898 lhs + rhs; // Perform the addition and store the result in 'res'
899
900 // Check if the result is positive infinity due to overflow
901 if (res == std::numeric_limits<double>::infinity())
902 {
903 return res; // Positive overflow has occurred, return positive
904 // infinity
905 }
906
907 // Check if the result is negative infinity, which can indicate a large
908 // negative overflow
909 if (res == -std::numeric_limits<double>::infinity())
910 {
911 return res; // Negative "overflow", effectively an underflow to
912 // negative infinity
913 }
914
915 // Check for positive overflow: verify if both operands are positive and
916 // their sum exceeds the maximum double value
917 if (lhs > 0 && rhs > 0 &&
918 (std::numeric_limits<double>::max() - lhs) < rhs)
919 {
920 res = std::numeric_limits<double>::infinity(); // Set result to
921 // positive infinity to
922 // indicate overflow
923 return res;
924 }
925
926 // Check for an underflow scenario: both numbers are negative and their
927 // sum is more negative than what double can represent
928 if (lhs < 0 && rhs < 0 &&
929 (-std::numeric_limits<double>::max() - lhs) > rhs)
930 {
931 res = -std::numeric_limits<
932 double>::infinity(); // Set result to negative infinity to
933 // clarify extreme negative sum
934 return res;
935 }
936
937 // If none of the above conditions are met, return the result of the
938 // addition
939 return res;
940 }
941
943 const BoundedDouble& rhs)
944 {
945 return safeAdd(lhs._fVal, rhs._fVal);
946 }
947
949 {
950 return -lhs._fVal;
951 }
952
954 const BoundedDouble& rhs)
955 {
956 return safeAdd(lhs._fVal, -rhs._fVal);
957 }
958
968 static double safeMul(double lhs, double rhs)
969 {
970 if (doubleEqual(lhs, 0.0f) || doubleEqual(rhs, 0.0f))
971 return 0.0f;
972 double res = lhs * rhs;
973 // Check if the result is positive infinity due to overflow
974 if (res == std::numeric_limits<double>::infinity())
975 {
976 return res; // Positive overflow has occurred, return positive
977 // infinity
978 }
979
980 // Check if the result is negative infinity, which can indicate a large
981 // negative overflow
982 if (res == -std::numeric_limits<double>::infinity())
983 {
984 return res; // Negative "overflow", effectively an underflow to
985 // negative infinity
986 }
987 // Check for overflow scenarios
988 if (lhs > 0 && rhs > 0 &&
989 lhs > std::numeric_limits<double>::max() / rhs)
990 {
991 return std::numeric_limits<double>::infinity();
992 }
993 if (lhs < 0 && rhs < 0 &&
994 lhs < std::numeric_limits<double>::max() / rhs)
995 {
996 return std::numeric_limits<double>::infinity();
997 }
998
999 // Check for "underflow" scenarios (negative overflow)
1000 if (lhs > 0 && rhs < 0 &&
1001 rhs < std::numeric_limits<double>::lowest() / lhs)
1002 {
1003 return -std::numeric_limits<double>::infinity();
1004 }
1005 if (lhs < 0 && rhs > 0 &&
1006 lhs < std::numeric_limits<double>::lowest() / rhs)
1007 {
1008 return -std::numeric_limits<double>::infinity();
1009 }
1010
1011 return res; // If no overflow or underflow, return the product
1012 }
1013
1015 const BoundedDouble& rhs)
1016 {
1017 return safeMul(lhs._fVal, rhs._fVal);
1018 }
1019
1029 static double safeDiv(double lhs, double rhs)
1030 {
1031 // Check for division by zero
1032 if (doubleEqual(rhs, 0.0f))
1033 {
1034 return (lhs >= 0.0f) ? std::numeric_limits<double>::infinity()
1035 : -std::numeric_limits<double>::infinity();
1036 }
1037 double res = lhs / rhs;
1038 // Check if the result is positive infinity due to overflow
1039 if (res == std::numeric_limits<double>::infinity())
1040 {
1041 return res; // Positive overflow has occurred, return positive
1042 // infinity
1043 }
1044
1045 // Check if the result is negative infinity, which can indicate a large
1046 // negative overflow
1047 if (res == -std::numeric_limits<double>::infinity())
1048 {
1049 return res; // Negative "overflow", effectively an underflow to
1050 // negative infinity
1051 }
1052
1053 // Check for overflow when dividing small numbers
1054 if (rhs > 0 && rhs < std::numeric_limits<double>::min() &&
1055 lhs > std::numeric_limits<double>::max() * rhs)
1056 {
1057 return std::numeric_limits<double>::infinity();
1058 }
1059 if (rhs < 0 && rhs > -std::numeric_limits<double>::min() &&
1060 lhs > std::numeric_limits<double>::max() * rhs)
1061 {
1062 return -std::numeric_limits<double>::infinity();
1063 }
1064
1065 return res; // If no special cases, return the quotient
1066 }
1067
1069 const BoundedDouble& rhs)
1070 {
1071 return safeDiv(lhs._fVal, rhs._fVal);
1072 }
1073
1075 const BoundedDouble& rhs)
1076 {
1077 if (rhs.is_zero())
1078 assert(false && "divide by zero");
1079 else if (!lhs.is_infinity() && !rhs.is_infinity())
1080 return std::fmod(lhs._fVal, rhs._fVal);
1081 else if (!lhs.is_infinity() && rhs.is_infinity())
1082 return 0.0f;
1083 // TODO: not sure
1084 else if (lhs.is_infinity() && !rhs.is_infinity())
1085 return ite(rhs._fVal > 0.0f, lhs, -lhs);
1086 else
1087 // TODO: +oo/-oo L'Hôpital's rule?
1088 return eq(lhs, rhs) ? plus_infinity() : minus_infinity();
1089 abort();
1090 }
1091
1092 inline bool is_int() const
1093 {
1094 return _fVal == std::round(_fVal);
1095 }
1096 inline bool is_real() const
1097 {
1098 return !is_int();
1099 }
1100
1102 const BoundedDouble& rhs)
1103 {
1104 int lInt = std::round(lhs._fVal), rInt = std::round(rhs._fVal);
1105 return lInt ^ rInt;
1106 }
1107
1109 const BoundedDouble& rhs)
1110 {
1111 int lInt = std::round(lhs._fVal), rInt = std::round(rhs._fVal);
1112 return lInt & rInt;
1113 }
1114
1116 const BoundedDouble& rhs)
1117 {
1118 int lInt = std::round(lhs._fVal), rInt = std::round(rhs._fVal);
1119 return lInt | rInt;
1120 }
1121
1123 const BoundedDouble& rhs)
1124 {
1125 return lhs._fVal && rhs._fVal;
1126 }
1127
1129 const BoundedDouble& rhs)
1130 {
1131 return lhs._fVal || rhs._fVal;
1132 }
1133
1135 {
1136 return !lhs._fVal;
1137 }
1138
1140 const BoundedDouble& rhs)
1141 {
1142 assert(rhs.geq(0) && "rhs should be greater or equal than 0");
1143 if (lhs.is_zero())
1144 return lhs;
1145 else if (lhs.is_infinity())
1146 return lhs;
1147 else if (rhs.is_infinity())
1148 return lhs.geq(0) ? 0 : -1;
1149 else
1150 return (s32_t)lhs.getNumeral() >> (s32_t)rhs.getNumeral();
1151 }
1152
1154 const BoundedDouble& rhs)
1155 {
1156 assert(rhs.geq(0) && "rhs should be greater or equal than 0");
1157 if (lhs.is_zero())
1158 return lhs;
1159 else if (lhs.is_infinity())
1160 return lhs;
1161 else if (rhs.is_infinity())
1162 return lhs.geq(0) ? plus_infinity() : minus_infinity();
1163 else
1164 return (s32_t)lhs.getNumeral() << (s32_t)rhs.getNumeral();
1165 }
1166
1167 friend BoundedDouble ite(const BoundedDouble& cond,
1168 const BoundedDouble& lhs, const BoundedDouble& rhs)
1169 {
1170 return cond._fVal != 0.0f ? lhs._fVal : rhs._fVal;
1171 }
1172
1173 friend std::ostream& operator<<(std::ostream& out,
1174 const BoundedDouble& expr)
1175 {
1176 out << expr._fVal;
1177 return out;
1178 }
1179
1180 friend bool eq(const BoundedDouble& lhs, const BoundedDouble& rhs)
1181 {
1182 return doubleEqual(lhs._fVal, rhs._fVal);
1183 }
1184
1185 friend BoundedDouble min(const BoundedDouble& lhs, const BoundedDouble& rhs)
1186 {
1187 return std::min(lhs._fVal, rhs._fVal);
1188 }
1189
1190 friend BoundedDouble max(const BoundedDouble& lhs, const BoundedDouble& rhs)
1191 {
1192 return std::max(lhs._fVal, rhs._fVal);
1193 }
1194
1195 static BoundedDouble min(std::vector<BoundedDouble>& _l)
1196 {
1198 for (const auto& it : _l)
1199 {
1200 if (it.is_minus_infinity())
1201 return minus_infinity();
1202 else if (!it.geq(ret))
1203 {
1204 ret = it;
1205 }
1206 }
1207 return ret;
1208 }
1209
1210 static BoundedDouble max(std::vector<BoundedDouble>& _l)
1211 {
1213 for (const auto& it : _l)
1214 {
1215 if (it.is_plus_infinity())
1216 return plus_infinity();
1217 else if (!it.leq(ret))
1218 {
1219 ret = it;
1220 }
1221 }
1222 return ret;
1223 }
1224
1226 {
1227 return lhs.leq(0) ? -lhs : lhs;
1228 }
1229
1230 inline bool is_true() const
1231 {
1232 return _fVal != 0.0f;
1233 }
1234
1236 inline s64_t getNumeral() const
1237 {
1238 if (is_minus_infinity())
1239 {
1240 return INT64_MIN;
1241 }
1242 else if (is_plus_infinity())
1243 {
1244 return INT64_MAX;
1245 }
1246 else
1247 {
1248 return std::round(_fVal);
1249 }
1250 }
1251
1252 inline s64_t getIntNumeral() const
1253 {
1254 return getNumeral();
1255 }
1256
1257 inline double getRealNumeral() const
1258 {
1259 return _fVal;
1260 }
1261
1262 inline virtual const std::string to_string() const
1263 {
1264 return std::to_string(_fVal);
1265 }
1266
1267 //%}
1268}; // end class BoundedDouble
1269
1270} // end namespace SVF
1271
1272#endif // SVF_NUMERICVALUE_H
#define epsilon
#define false
Definition cJSON.cpp:70
cJSON * a
Definition cJSON.cpp:2560
const cJSON *const b
Definition cJSON.h:255
BoundedDouble & operator=(const BoundedDouble &rhs)
friend BoundedDouble max(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend bool operator!=(const BoundedDouble &lhs, const BoundedDouble &rhs)
bool is_int() const
friend bool operator<=(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend BoundedDouble operator%(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend bool operator<(const BoundedDouble &lhs, const BoundedDouble &rhs)
const double getFVal() const
BoundedDouble()=default
friend BoundedDouble operator>>(const BoundedDouble &lhs, const BoundedDouble &rhs)
static double safeDiv(double lhs, double rhs)
friend BoundedDouble operator*(const BoundedDouble &lhs, const BoundedDouble &rhs)
virtual ~BoundedDouble()
static double safeMul(double lhs, double rhs)
static BoundedDouble minus_infinity()
friend BoundedDouble operator!(const BoundedDouble &lhs)
friend BoundedDouble ite(const BoundedDouble &cond, const BoundedDouble &lhs, const BoundedDouble &rhs)
static BoundedDouble plus_infinity()
friend BoundedDouble operator&&(const BoundedDouble &lhs, const BoundedDouble &rhs)
bool equal(const BoundedDouble &rhs) const
BoundedDouble & operator=(BoundedDouble &&rhs)
s64_t getIntNumeral() const
static bool isZero(const BoundedDouble &expr)
static bool doubleEqual(double a, double b)
bool is_real() const
bool geq(const BoundedDouble &rhs) const
friend BoundedDouble operator-(const BoundedDouble &lhs)
friend BoundedDouble operator-(const BoundedDouble &lhs, const BoundedDouble &rhs)
s64_t getNumeral() const
Return Numeral.
friend BoundedDouble operator|(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend BoundedDouble min(const BoundedDouble &lhs, const BoundedDouble &rhs)
bool is_true() const
friend BoundedDouble operator^(const BoundedDouble &lhs, const BoundedDouble &rhs)
static double safeAdd(double lhs, double rhs)
friend BoundedDouble abs(const BoundedDouble &lhs)
friend BoundedDouble operator<<(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend BoundedDouble operator/(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend bool eq(const BoundedDouble &lhs, const BoundedDouble &rhs)
static BoundedDouble min(std::vector< BoundedDouble > &_l)
friend BoundedDouble operator+(const BoundedDouble &lhs, const BoundedDouble &rhs)
BoundedDouble(const BoundedDouble &rhs)
bool is_minus_infinity() const
BoundedDouble(BoundedDouble &&rhs)
bool leq(const BoundedDouble &rhs) const
bool is_plus_infinity() const
virtual const std::string to_string() const
static BoundedDouble max(std::vector< BoundedDouble > &_l)
friend BoundedDouble operator&(const BoundedDouble &lhs, const BoundedDouble &rhs)
double getRealNumeral() const
friend bool operator>=(const BoundedDouble &lhs, const BoundedDouble &rhs)
BoundedDouble(double fVal)
friend BoundedDouble operator||(const BoundedDouble &lhs, const BoundedDouble &rhs)
friend bool operator==(const BoundedDouble &lhs, const BoundedDouble &rhs)
Reload operator.
bool is_zero() const
friend std::ostream & operator<<(std::ostream &out, const BoundedDouble &expr)
friend bool operator>(const BoundedDouble &lhs, const BoundedDouble &rhs)
bool is_infinity() const
BoundedInt()=default
static BoundedInt plus_infinity()
friend BoundedInt operator||(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt ite(const BoundedInt &cond, const BoundedInt &lhs, const BoundedInt &rhs)
friend bool operator<=(const BoundedInt &lhs, const BoundedInt &rhs)
bool is_minus_infinity() const
friend bool operator<(const BoundedInt &lhs, const BoundedInt &rhs)
s64_t getNumeral() const
Retrieves the numeral value of the BoundedInt object.
friend BoundedInt operator-(const BoundedInt &value)
virtual ~BoundedInt()
bool is_plus_infinity() const
friend BoundedInt operator%(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt safeNeg(const BoundedInt &value)
friend BoundedInt operator<<(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt min(std::vector< BoundedInt > &_l)
friend BoundedInt operator&(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt safeMul(const BoundedInt &lhs, const BoundedInt &rhs)
bool is_infinity() const
const double getFVal() const
friend BoundedInt operator*(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt safeRem(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt abs(const BoundedInt &lhs)
BoundedInt(BoundedInt &&rhs)
friend BoundedInt operator|(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt operator/(const BoundedInt &lhs, const BoundedInt &rhs)
friend bool operator!=(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt max(const BoundedInt &lhs, const BoundedInt &rhs)
s64_t getIntNumeral() const
friend bool operator>(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt operator+(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt operator>>(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt operator!(const BoundedInt &lhs)
friend BoundedInt operator^(const BoundedInt &lhs, const BoundedInt &rhs)
bool is_zero() const
static BoundedInt safeDiv(const BoundedInt &lhs, const BoundedInt &rhs)
friend BoundedInt operator&&(const BoundedInt &lhs, const BoundedInt &rhs)
bool is_true() const
BoundedInt & operator=(const BoundedInt &rhs)
friend std::ostream & operator<<(std::ostream &out, const BoundedInt &expr)
static BoundedInt max(std::vector< BoundedInt > &_l)
friend bool eq(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt safeSub(const BoundedInt &lhs, const BoundedInt &rhs)
static BoundedInt safeAdd(const BoundedInt &lhs, const BoundedInt &rhs)
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
BoundedInt & operator=(BoundedInt &&rhs)
bool leq(const BoundedInt &rhs) const
static bool isZero(const BoundedInt &expr)
double getRealNumeral() const
BoundedInt(s64_t fVal, bool isInf)
void set_minus_infinity()
bool is_real() const
void set_plus_infinity()
friend BoundedInt operator-(const BoundedInt &lhs, const BoundedInt &rhs)
friend bool operator>=(const BoundedInt &lhs, const BoundedInt &rhs)
BoundedInt(const BoundedInt &rhs)
virtual const std::string to_string() const
friend bool operator==(const BoundedInt &lhs, const BoundedInt &rhs)
Reload operator.
BoundedInt(s64_t fVal)
for isBitcode
Definition BasicTypes.h:70
signed s32_t
Definition GeneralType.h:68
signed long long s64_t
Definition GeneralType.h:70