1 /* Floating point range operators.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
3 Contributed by Aldy Hernandez <aldyh@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-iterator.h"
41 #include "gimple-fold.h"
43 #include "gimple-walk.h"
46 #include "value-relation.h"
48 #include "range-op-mixed.h"
50 // Default definitions for floating point operators.
53 range_operator::fold_range (frange
&r
, tree type
,
54 const frange
&op1
, const frange
&op2
,
55 relation_trio trio
) const
57 if (empty_range_varying (r
, type
, op1
, op2
))
59 if (op1
.known_isnan () || op2
.known_isnan ())
65 REAL_VALUE_TYPE lb
, ub
;
67 rv_fold (lb
, ub
, maybe_nan
, type
,
68 op1
.lower_bound (), op1
.upper_bound (),
69 op2
.lower_bound (), op2
.upper_bound (), trio
.op1_op2 ());
71 // Handle possible NANs by saturating to the appropriate INF if only
72 // one end is a NAN. If both ends are a NAN, just return a NAN.
73 bool lb_nan
= real_isnan (&lb
);
74 bool ub_nan
= real_isnan (&ub
);
87 if (lb_nan
|| ub_nan
|| maybe_nan
89 || op2
.maybe_isnan ())
90 // Keep the default NAN (with a varying sign) set by the setter.
95 // If the result has overflowed and flag_trapping_math, folding this
96 // operation could elide an overflow or division by zero exception.
97 // Avoid returning a singleton +-INF, to keep the propagators (DOM
98 // and substitute_and_fold_engine) from folding. See PR107608.
99 if (flag_trapping_math
100 && MODE_HAS_INFINITIES (TYPE_MODE (type
))
101 && r
.known_isinf () && !op1
.known_isinf () && !op2
.known_isinf ())
103 REAL_VALUE_TYPE inf
= r
.lower_bound ();
104 if (real_isneg (&inf
))
106 REAL_VALUE_TYPE min
= real_min_representable (type
);
107 r
.set (type
, inf
, min
);
111 REAL_VALUE_TYPE max
= real_max_representable (type
);
112 r
.set (type
, max
, inf
);
116 r
.flush_denormals_to_zero ();
121 // For a given operation, fold two sets of ranges into [lb, ub].
122 // MAYBE_NAN is set to TRUE if, in addition to any result in LB or
123 // UB, the final range has the possibility of a NAN.
125 range_operator::rv_fold (REAL_VALUE_TYPE
&lb
,
128 tree type ATTRIBUTE_UNUSED
,
129 const REAL_VALUE_TYPE
&lh_lb ATTRIBUTE_UNUSED
,
130 const REAL_VALUE_TYPE
&lh_ub ATTRIBUTE_UNUSED
,
131 const REAL_VALUE_TYPE
&rh_lb ATTRIBUTE_UNUSED
,
132 const REAL_VALUE_TYPE
&rh_ub ATTRIBUTE_UNUSED
,
141 range_operator::fold_range (irange
&r ATTRIBUTE_UNUSED
,
142 tree type ATTRIBUTE_UNUSED
,
143 const frange
&lh ATTRIBUTE_UNUSED
,
144 const irange
&rh ATTRIBUTE_UNUSED
,
151 range_operator::fold_range (irange
&r ATTRIBUTE_UNUSED
,
152 tree type ATTRIBUTE_UNUSED
,
153 const frange
&lh ATTRIBUTE_UNUSED
,
154 const frange
&rh ATTRIBUTE_UNUSED
,
161 range_operator::fold_range (frange
&r ATTRIBUTE_UNUSED
,
162 tree type ATTRIBUTE_UNUSED
,
163 const irange
&lh ATTRIBUTE_UNUSED
,
164 const irange
&rh ATTRIBUTE_UNUSED
,
171 range_operator::op1_range (frange
&r ATTRIBUTE_UNUSED
,
172 tree type ATTRIBUTE_UNUSED
,
173 const frange
&lhs ATTRIBUTE_UNUSED
,
174 const frange
&op2 ATTRIBUTE_UNUSED
,
181 range_operator::op1_range (frange
&r ATTRIBUTE_UNUSED
,
182 tree type ATTRIBUTE_UNUSED
,
183 const irange
&lhs ATTRIBUTE_UNUSED
,
184 const frange
&op2 ATTRIBUTE_UNUSED
,
191 range_operator::op2_range (frange
&r ATTRIBUTE_UNUSED
,
192 tree type ATTRIBUTE_UNUSED
,
193 const frange
&lhs ATTRIBUTE_UNUSED
,
194 const frange
&op1 ATTRIBUTE_UNUSED
,
201 range_operator::op2_range (frange
&r ATTRIBUTE_UNUSED
,
202 tree type ATTRIBUTE_UNUSED
,
203 const irange
&lhs ATTRIBUTE_UNUSED
,
204 const frange
&op1 ATTRIBUTE_UNUSED
,
211 range_operator::lhs_op1_relation (const frange
&lhs ATTRIBUTE_UNUSED
,
212 const frange
&op1 ATTRIBUTE_UNUSED
,
213 const frange
&op2 ATTRIBUTE_UNUSED
,
220 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
221 const frange
&op1 ATTRIBUTE_UNUSED
,
222 const frange
&op2 ATTRIBUTE_UNUSED
,
229 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
230 const frange
&op1 ATTRIBUTE_UNUSED
,
231 const frange
&op2 ATTRIBUTE_UNUSED
,
238 range_operator::lhs_op2_relation (const frange
&lhs ATTRIBUTE_UNUSED
,
239 const frange
&op1 ATTRIBUTE_UNUSED
,
240 const frange
&op2 ATTRIBUTE_UNUSED
,
247 range_operator::op1_op2_relation (const frange
&lhs ATTRIBUTE_UNUSED
) const
252 // Return TRUE if OP1 and OP2 may be a NAN.
255 maybe_isnan (const frange
&op1
, const frange
&op2
)
257 return op1
.maybe_isnan () || op2
.maybe_isnan ();
260 // Floating version of relop_early_resolve that takes into account NAN
261 // and -ffinite-math-only.
264 frelop_early_resolve (irange
&r
, tree type
,
265 const frange
&op1
, const frange
&op2
,
266 relation_trio rel
, relation_kind my_rel
)
268 // If either operand is undefined, return VARYING.
269 if (empty_range_varying (r
, type
, op1
, op2
))
272 // We can fold relations from the oracle when we know both operands
273 // are free of NANs, or when -ffinite-math-only.
274 return (!maybe_isnan (op1
, op2
)
275 && relop_early_resolve (r
, type
, op1
, op2
, rel
, my_rel
));
278 // Set VALUE to its next real value, or INF if the operation overflows.
281 frange_nextafter (enum machine_mode mode
,
282 REAL_VALUE_TYPE
&value
,
283 const REAL_VALUE_TYPE
&inf
)
285 if (MODE_COMPOSITE_P (mode
)
286 && (real_isdenormal (&value
, mode
) || real_iszero (&value
)))
288 // IBM extended denormals only have DFmode precision.
289 REAL_VALUE_TYPE tmp
, tmp2
;
290 real_convert (&tmp2
, DFmode
, &value
);
291 real_nextafter (&tmp
, REAL_MODE_FORMAT (DFmode
), &tmp2
, &inf
);
292 real_convert (&value
, mode
, &tmp
);
297 real_nextafter (&tmp
, REAL_MODE_FORMAT (mode
), &value
, &inf
);
302 // Like real_arithmetic, but round the result to INF if the operation
303 // produced inexact results.
305 // ?? There is still one problematic case, i387. With
306 // -fexcess-precision=standard we perform most SF/DFmode arithmetic in
307 // XFmode (long_double_type_node), so that case is OK. But without
308 // -mfpmath=sse, all the SF/DFmode computations are in XFmode
309 // precision (64-bit mantissa) and only occasionally rounded to
310 // SF/DFmode (when storing into memory from the 387 stack). Maybe
311 // this is ok as well though it is just occasionally more precise. ??
314 frange_arithmetic (enum tree_code code
, tree type
,
315 REAL_VALUE_TYPE
&result
,
316 const REAL_VALUE_TYPE
&op1
,
317 const REAL_VALUE_TYPE
&op2
,
318 const REAL_VALUE_TYPE
&inf
)
320 REAL_VALUE_TYPE value
;
321 enum machine_mode mode
= TYPE_MODE (type
);
322 bool mode_composite
= MODE_COMPOSITE_P (mode
);
324 bool inexact
= real_arithmetic (&value
, code
, &op1
, &op2
);
325 real_convert (&result
, mode
, &value
);
327 // Be extra careful if there may be discrepancies between the
328 // compile and runtime results.
334 bool low
= real_isneg (&inf
);
335 round
= (low
? !real_less (&result
, &value
)
336 : !real_less (&value
, &result
));
337 if (real_isinf (&result
, !low
)
338 && !real_isinf (&value
)
339 && !flag_rounding_math
)
341 // Use just [+INF, +INF] rather than [MAX, +INF]
342 // even if value is larger than MAX and rounds to
343 // nearest to +INF. Similarly just [-INF, -INF]
344 // rather than [-INF, +MAX] even if value is smaller
345 // than -MAX and rounds to nearest to -INF.
346 // Unless INEXACT is true, in that case we need some
352 REAL_VALUE_TYPE tmp
= result
, tmp2
;
353 frange_nextafter (mode
, tmp
, inf
);
354 // TMP is at this point the maximum representable
356 real_arithmetic (&tmp2
, MINUS_EXPR
, &value
, &tmp
);
357 if (real_isneg (&tmp2
) != low
358 && (REAL_EXP (&tmp2
) - REAL_EXP (&tmp
)
359 >= 2 - REAL_MODE_FORMAT (mode
)->p
))
364 if (round
&& (inexact
|| !real_identical (&result
, &value
)))
367 && (real_isdenormal (&result
, mode
) || real_iszero (&result
)))
369 // IBM extended denormals only have DFmode precision.
370 REAL_VALUE_TYPE tmp
, tmp2
;
371 real_convert (&tmp2
, DFmode
, &value
);
372 real_nextafter (&tmp
, REAL_MODE_FORMAT (DFmode
), &tmp2
, &inf
);
373 real_convert (&result
, mode
, &tmp
);
376 frange_nextafter (mode
, result
, inf
);
383 // ibm-ldouble-format documents 1ulp for + and -.
384 frange_nextafter (mode
, result
, inf
);
387 // ibm-ldouble-format documents 2ulps for *.
388 frange_nextafter (mode
, result
, inf
);
389 frange_nextafter (mode
, result
, inf
);
392 // ibm-ldouble-format documents 3ulps for /.
393 frange_nextafter (mode
, result
, inf
);
394 frange_nextafter (mode
, result
, inf
);
395 frange_nextafter (mode
, result
, inf
);
402 // Crop R to [-INF, MAX] where MAX is the maximum representable number
406 frange_drop_inf (frange
&r
, tree type
)
408 REAL_VALUE_TYPE max
= real_max_representable (type
);
409 frange
tmp (type
, r
.lower_bound (), max
);
413 // Crop R to [MIN, +INF] where MIN is the minimum representable number
417 frange_drop_ninf (frange
&r
, tree type
)
419 REAL_VALUE_TYPE min
= real_min_representable (type
);
420 frange
tmp (type
, min
, r
.upper_bound ());
424 // Crop R to [MIN, MAX] where MAX is the maximum representable number
425 // for TYPE and MIN the minimum representable number for TYPE.
428 frange_drop_infs (frange
&r
, tree type
)
430 REAL_VALUE_TYPE max
= real_max_representable (type
);
431 REAL_VALUE_TYPE min
= real_min_representable (type
);
432 frange
tmp (type
, min
, max
);
436 // If zero is in R, make sure both -0.0 and +0.0 are in the range.
439 frange_add_zeros (frange
&r
, tree type
)
441 if (r
.undefined_p () || r
.known_isnan ())
444 if (HONOR_SIGNED_ZEROS (type
)
445 && (real_iszero (&r
.lower_bound ()) || real_iszero (&r
.upper_bound ())))
448 zero
.set_zero (type
);
453 // Build a range that is <= VAL and store it in R. Return TRUE if
454 // further changes may be needed for R, or FALSE if R is in its final
458 build_le (frange
&r
, tree type
, const frange
&val
)
460 gcc_checking_assert (!val
.known_isnan ());
462 REAL_VALUE_TYPE ninf
= frange_val_min (type
);
463 r
.set (type
, ninf
, val
.upper_bound ());
465 // Add both zeros if there's the possibility of zero equality.
466 frange_add_zeros (r
, type
);
471 // Build a range that is < VAL and store it in R. Return TRUE if
472 // further changes may be needed for R, or FALSE if R is in its final
476 build_lt (frange
&r
, tree type
, const frange
&val
)
478 gcc_checking_assert (!val
.known_isnan ());
480 // < -INF is outside the range.
481 if (real_isinf (&val
.upper_bound (), 1))
483 if (HONOR_NANS (type
))
490 REAL_VALUE_TYPE ninf
= frange_val_min (type
);
491 REAL_VALUE_TYPE prev
= val
.upper_bound ();
492 machine_mode mode
= TYPE_MODE (type
);
493 // Default to the conservatively correct closed ranges for
494 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
495 // !HONOR_INFINITIES, nextafter will yield -INF, but frange::set()
496 // will crop the range appropriately.
497 if (!MODE_COMPOSITE_P (mode
))
498 frange_nextafter (mode
, prev
, ninf
);
499 r
.set (type
, ninf
, prev
);
503 // Build a range that is >= VAL and store it in R. Return TRUE if
504 // further changes may be needed for R, or FALSE if R is in its final
508 build_ge (frange
&r
, tree type
, const frange
&val
)
510 gcc_checking_assert (!val
.known_isnan ());
512 REAL_VALUE_TYPE inf
= frange_val_max (type
);
513 r
.set (type
, val
.lower_bound (), inf
);
515 // Add both zeros if there's the possibility of zero equality.
516 frange_add_zeros (r
, type
);
521 // Build a range that is > VAL and store it in R. Return TRUE if
522 // further changes may be needed for R, or FALSE if R is in its final
526 build_gt (frange
&r
, tree type
, const frange
&val
)
528 gcc_checking_assert (!val
.known_isnan ());
530 // > +INF is outside the range.
531 if (real_isinf (&val
.lower_bound (), 0))
533 if (HONOR_NANS (type
))
540 REAL_VALUE_TYPE inf
= frange_val_max (type
);
541 REAL_VALUE_TYPE next
= val
.lower_bound ();
542 machine_mode mode
= TYPE_MODE (type
);
543 // Default to the conservatively correct closed ranges for
544 // MODE_COMPOSITE_P, otherwise use nextafter. Note that for
545 // !HONOR_INFINITIES, nextafter will yield +INF, but frange::set()
546 // will crop the range appropriately.
547 if (!MODE_COMPOSITE_P (mode
))
548 frange_nextafter (mode
, next
, inf
);
549 r
.set (type
, next
, inf
);
555 operator_identity::fold_range (frange
&r
, tree
, const frange
&op1
,
556 const frange
&, relation_trio
) const
563 operator_identity::op1_range (frange
&r
, tree
, const frange
&lhs
,
564 const frange
&, relation_trio
) const
571 operator_cst::fold_range (frange
&r
, tree
, const frange
&op1
,
572 const frange
&, relation_trio
) const
579 operator_equal::op2_range (frange
&r
, tree type
,
580 const irange
&lhs
, const frange
&op1
,
581 relation_trio rel
) const
583 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
587 operator_equal::fold_range (irange
&r
, tree type
,
588 const frange
&op1
, const frange
&op2
,
589 relation_trio rel
) const
591 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
594 if (op1
.known_isnan () || op2
.known_isnan ())
595 r
= range_false (type
);
596 // We can be sure the values are always equal or not if both ranges
597 // consist of a single value, and then compare them.
598 else if (op1
.singleton_p () && op2
.singleton_p ())
601 r
= range_true (type
);
602 // If one operand is -0.0 and other 0.0, they are still equal.
603 else if (real_iszero (&op1
.lower_bound ())
604 && real_iszero (&op2
.lower_bound ()))
605 r
= range_true (type
);
607 r
= range_false (type
);
609 else if (real_iszero (&op1
.lower_bound ())
610 && real_iszero (&op1
.upper_bound ())
611 && real_iszero (&op2
.lower_bound ())
612 && real_iszero (&op2
.upper_bound ())
613 && !maybe_isnan (op1
, op2
))
614 // [-0.0, 0.0] == [-0.0, 0.0] or similar.
615 r
= range_true (type
);
618 // If ranges do not intersect, we know the range is not equal,
619 // otherwise we don't know anything for sure.
622 if (tmp
.undefined_p ())
624 // If one range is [whatever, -0.0] and another
625 // [0.0, whatever2], we don't know anything either,
626 // because -0.0 == 0.0.
627 if ((real_iszero (&op1
.upper_bound ())
628 && real_iszero (&op2
.lower_bound ()))
629 || (real_iszero (&op1
.lower_bound ())
630 && real_iszero (&op2
.upper_bound ())))
631 r
= range_true_and_false (type
);
633 r
= range_false (type
);
636 r
= range_true_and_false (type
);
642 operator_equal::op1_range (frange
&r
, tree type
,
645 relation_trio trio
) const
647 relation_kind rel
= trio
.op1_op2 ();
648 switch (get_bool_state (r
, lhs
, type
))
651 // The TRUE side of x == NAN is unreachable.
652 if (op2
.known_isnan ())
656 // If it's true, the result is the same as OP2.
658 // Add both zeros if there's the possibility of zero equality.
659 frange_add_zeros (r
, type
);
660 // The TRUE side of op1 == op2 implies op1 is !NAN.
666 // The FALSE side of op1 == op1 implies op1 is a NAN.
669 // On the FALSE side of x == NAN, we know nothing about x.
670 else if (op2
.known_isnan ())
671 r
.set_varying (type
);
672 // If the result is false, the only time we know anything is
673 // if OP2 is a constant.
674 else if (op2
.singleton_p ()
675 || (!op2
.maybe_isnan () && op2
.zero_p ()))
677 REAL_VALUE_TYPE tmp
= op2
.lower_bound ();
678 r
.set (type
, tmp
, tmp
, VR_ANTI_RANGE
);
681 r
.set_varying (type
);
691 operator_not_equal::fold_range (irange
&r
, tree type
,
692 const frange
&op1
, const frange
&op2
,
693 relation_trio rel
) const
695 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_NE
))
698 // x != NAN is always TRUE.
699 if (op1
.known_isnan () || op2
.known_isnan ())
700 r
= range_true (type
);
701 // We can be sure the values are always equal or not if both ranges
702 // consist of a single value, and then compare them.
703 else if (op1
.singleton_p () && op2
.singleton_p ())
706 r
= range_false (type
);
707 // If one operand is -0.0 and other 0.0, they are still equal.
708 else if (real_iszero (&op1
.lower_bound ())
709 && real_iszero (&op2
.lower_bound ()))
710 r
= range_false (type
);
712 r
= range_true (type
);
714 else if (real_iszero (&op1
.lower_bound ())
715 && real_iszero (&op1
.upper_bound ())
716 && real_iszero (&op2
.lower_bound ())
717 && real_iszero (&op2
.upper_bound ())
718 && !maybe_isnan (op1
, op2
))
719 // [-0.0, 0.0] != [-0.0, 0.0] or similar.
720 r
= range_false (type
);
723 // If ranges do not intersect, we know the range is not equal,
724 // otherwise we don't know anything for sure.
727 if (tmp
.undefined_p ())
729 // If one range is [whatever, -0.0] and another
730 // [0.0, whatever2], we don't know anything either,
731 // because -0.0 == 0.0.
732 if ((real_iszero (&op1
.upper_bound ())
733 && real_iszero (&op2
.lower_bound ()))
734 || (real_iszero (&op1
.lower_bound ())
735 && real_iszero (&op2
.upper_bound ())))
736 r
= range_true_and_false (type
);
738 r
= range_true (type
);
741 r
= range_true_and_false (type
);
747 operator_not_equal::op1_range (frange
&r
, tree type
,
750 relation_trio trio
) const
752 relation_kind rel
= trio
.op1_op2 ();
753 switch (get_bool_state (r
, lhs
, type
))
756 // If the result is true, the only time we know anything is if
757 // OP2 is a constant.
758 if (op2
.singleton_p ())
760 // This is correct even if op1 is NAN, because the following
761 // range would be ~[tmp, tmp] with the NAN property set to
763 REAL_VALUE_TYPE tmp
= op2
.lower_bound ();
764 r
.set (type
, tmp
, tmp
, VR_ANTI_RANGE
);
766 // The TRUE side of op1 != op1 implies op1 is NAN.
767 else if (rel
== VREL_EQ
)
770 r
.set_varying (type
);
774 // The FALSE side of x != NAN is impossible.
775 if (op2
.known_isnan ())
779 // If it's false, the result is the same as OP2.
781 // Add both zeros if there's the possibility of zero equality.
782 frange_add_zeros (r
, type
);
783 // The FALSE side of op1 != op2 implies op1 is !NAN.
795 operator_lt::fold_range (irange
&r
, tree type
,
796 const frange
&op1
, const frange
&op2
,
797 relation_trio rel
) const
799 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LT
))
802 if (op1
.known_isnan ()
803 || op2
.known_isnan ()
804 || !real_less (&op1
.lower_bound (), &op2
.upper_bound ()))
805 r
= range_false (type
);
806 else if (!maybe_isnan (op1
, op2
)
807 && real_less (&op1
.upper_bound (), &op2
.lower_bound ()))
808 r
= range_true (type
);
810 r
= range_true_and_false (type
);
815 operator_lt::op1_range (frange
&r
,
821 switch (get_bool_state (r
, lhs
, type
))
824 // The TRUE side of x < NAN is unreachable.
825 if (op2
.known_isnan ())
827 else if (op2
.undefined_p ())
829 else if (build_lt (r
, type
, op2
))
832 // x < y implies x is not +INF.
833 frange_drop_inf (r
, type
);
838 // On the FALSE side of x < NAN, we know nothing about x.
839 if (op2
.known_isnan () || op2
.maybe_isnan ())
840 r
.set_varying (type
);
842 build_ge (r
, type
, op2
);
852 operator_lt::op2_range (frange
&r
,
858 switch (get_bool_state (r
, lhs
, type
))
861 // The TRUE side of NAN < x is unreachable.
862 if (op1
.known_isnan ())
864 else if (op1
.undefined_p ())
866 else if (build_gt (r
, type
, op1
))
869 // x < y implies y is not -INF.
870 frange_drop_ninf (r
, type
);
875 // On the FALSE side of NAN < x, we know nothing about x.
876 if (op1
.known_isnan () || op1
.maybe_isnan ())
877 r
.set_varying (type
);
879 build_le (r
, type
, op1
);
889 operator_le::fold_range (irange
&r
, tree type
,
890 const frange
&op1
, const frange
&op2
,
891 relation_trio rel
) const
893 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
896 if (op1
.known_isnan ()
897 || op2
.known_isnan ()
898 || !real_compare (LE_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
899 r
= range_false (type
);
900 else if (!maybe_isnan (op1
, op2
)
901 && real_compare (LE_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
902 r
= range_true (type
);
904 r
= range_true_and_false (type
);
909 operator_le::op1_range (frange
&r
,
915 switch (get_bool_state (r
, lhs
, type
))
918 // The TRUE side of x <= NAN is unreachable.
919 if (op2
.known_isnan ())
921 else if (op2
.undefined_p ())
923 else if (build_le (r
, type
, op2
))
928 // On the FALSE side of x <= NAN, we know nothing about x.
929 if (op2
.known_isnan () || op2
.maybe_isnan ())
930 r
.set_varying (type
);
932 build_gt (r
, type
, op2
);
942 operator_le::op2_range (frange
&r
,
948 switch (get_bool_state (r
, lhs
, type
))
951 // The TRUE side of NAN <= x is unreachable.
952 if (op1
.known_isnan ())
954 else if (op1
.undefined_p ())
956 else if (build_ge (r
, type
, op1
))
961 // On the FALSE side of NAN <= x, we know nothing about x.
962 if (op1
.known_isnan () || op1
.maybe_isnan ())
963 r
.set_varying (type
);
964 else if (op1
.undefined_p ())
967 build_lt (r
, type
, op1
);
977 operator_gt::fold_range (irange
&r
, tree type
,
978 const frange
&op1
, const frange
&op2
,
979 relation_trio rel
) const
981 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GT
))
984 if (op1
.known_isnan ()
985 || op2
.known_isnan ()
986 || !real_compare (GT_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
987 r
= range_false (type
);
988 else if (!maybe_isnan (op1
, op2
)
989 && real_compare (GT_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
990 r
= range_true (type
);
992 r
= range_true_and_false (type
);
997 operator_gt::op1_range (frange
&r
,
1001 relation_trio
) const
1003 switch (get_bool_state (r
, lhs
, type
))
1006 // The TRUE side of x > NAN is unreachable.
1007 if (op2
.known_isnan ())
1009 else if (op2
.undefined_p ())
1011 else if (build_gt (r
, type
, op2
))
1014 // x > y implies x is not -INF.
1015 frange_drop_ninf (r
, type
);
1020 // On the FALSE side of x > NAN, we know nothing about x.
1021 if (op2
.known_isnan () || op2
.maybe_isnan ())
1022 r
.set_varying (type
);
1023 else if (op2
.undefined_p ())
1026 build_le (r
, type
, op2
);
1036 operator_gt::op2_range (frange
&r
,
1040 relation_trio
) const
1042 switch (get_bool_state (r
, lhs
, type
))
1045 // The TRUE side of NAN > x is unreachable.
1046 if (op1
.known_isnan ())
1048 else if (op1
.undefined_p ())
1050 else if (build_lt (r
, type
, op1
))
1053 // x > y implies y is not +INF.
1054 frange_drop_inf (r
, type
);
1059 // On The FALSE side of NAN > x, we know nothing about x.
1060 if (op1
.known_isnan () || op1
.maybe_isnan ())
1061 r
.set_varying (type
);
1062 else if (op1
.undefined_p ())
1065 build_ge (r
, type
, op1
);
1075 operator_ge::fold_range (irange
&r
, tree type
,
1076 const frange
&op1
, const frange
&op2
,
1077 relation_trio rel
) const
1079 if (frelop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1082 if (op1
.known_isnan ()
1083 || op2
.known_isnan ()
1084 || !real_compare (GE_EXPR
, &op1
.upper_bound (), &op2
.lower_bound ()))
1085 r
= range_false (type
);
1086 else if (!maybe_isnan (op1
, op2
)
1087 && real_compare (GE_EXPR
, &op1
.lower_bound (), &op2
.upper_bound ()))
1088 r
= range_true (type
);
1090 r
= range_true_and_false (type
);
1095 operator_ge::op1_range (frange
&r
,
1099 relation_trio
) const
1101 switch (get_bool_state (r
, lhs
, type
))
1104 // The TRUE side of x >= NAN is unreachable.
1105 if (op2
.known_isnan ())
1107 else if (op2
.undefined_p ())
1109 else if (build_ge (r
, type
, op2
))
1114 // On the FALSE side of x >= NAN, we know nothing about x.
1115 if (op2
.known_isnan () || op2
.maybe_isnan ())
1116 r
.set_varying (type
);
1117 else if (op2
.undefined_p ())
1120 build_lt (r
, type
, op2
);
1130 operator_ge::op2_range (frange
&r
, tree type
,
1133 relation_trio
) const
1135 switch (get_bool_state (r
, lhs
, type
))
1138 // The TRUE side of NAN >= x is unreachable.
1139 if (op1
.known_isnan ())
1141 else if (op1
.undefined_p ())
1143 else if (build_le (r
, type
, op1
))
1148 // On the FALSE side of NAN >= x, we know nothing about x.
1149 if (op1
.known_isnan () || op1
.maybe_isnan ())
1150 r
.set_varying (type
);
1151 else if (op1
.undefined_p ())
1154 build_gt (r
, type
, op1
);
1163 // UNORDERED_EXPR comparison.
1165 class foperator_unordered
: public range_operator
1167 using range_operator::fold_range
;
1168 using range_operator::op1_range
;
1169 using range_operator::op2_range
;
1171 bool fold_range (irange
&r
, tree type
,
1172 const frange
&op1
, const frange
&op2
,
1173 relation_trio
= TRIO_VARYING
) const final override
;
1174 bool op1_range (frange
&r
, tree type
,
1175 const irange
&lhs
, const frange
&op2
,
1176 relation_trio
= TRIO_VARYING
) const final override
;
1177 bool op2_range (frange
&r
, tree type
,
1178 const irange
&lhs
, const frange
&op1
,
1179 relation_trio rel
= TRIO_VARYING
) const final override
1181 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1186 foperator_unordered::fold_range (irange
&r
, tree type
,
1187 const frange
&op1
, const frange
&op2
,
1188 relation_trio
) const
1190 // UNORDERED is TRUE if either operand is a NAN.
1191 if (op1
.known_isnan () || op2
.known_isnan ())
1192 r
= range_true (type
);
1193 // UNORDERED is FALSE if neither operand is a NAN.
1194 else if (!op1
.maybe_isnan () && !op2
.maybe_isnan ())
1195 r
= range_false (type
);
1197 r
= range_true_and_false (type
);
1202 foperator_unordered::op1_range (frange
&r
, tree type
,
1205 relation_trio trio
) const
1207 relation_kind rel
= trio
.op1_op2 ();
1208 switch (get_bool_state (r
, lhs
, type
))
1211 // Since at least one operand must be NAN, if one of them is
1212 // not, the other must be.
1213 if (rel
== VREL_EQ
|| !op2
.maybe_isnan ())
1216 r
.set_varying (type
);
1220 // A false UNORDERED means both operands are !NAN, so it's
1221 // impossible for op2 to be a NAN.
1222 if (op2
.known_isnan ())
1226 r
.set_varying (type
);
1237 // ORDERED_EXPR comparison.
1239 class foperator_ordered
: public range_operator
1241 using range_operator::fold_range
;
1242 using range_operator::op1_range
;
1243 using range_operator::op2_range
;
1245 bool fold_range (irange
&r
, tree type
,
1246 const frange
&op1
, const frange
&op2
,
1247 relation_trio
= TRIO_VARYING
) const final override
;
1248 bool op1_range (frange
&r
, tree type
,
1249 const irange
&lhs
, const frange
&op2
,
1250 relation_trio
= TRIO_VARYING
) const final override
;
1251 bool op2_range (frange
&r
, tree type
,
1252 const irange
&lhs
, const frange
&op1
,
1253 relation_trio rel
= TRIO_VARYING
) const final override
1255 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1260 foperator_ordered::fold_range (irange
&r
, tree type
,
1261 const frange
&op1
, const frange
&op2
,
1262 relation_trio
) const
1264 if (op1
.known_isnan () || op2
.known_isnan ())
1265 r
= range_false (type
);
1266 else if (!op1
.maybe_isnan () && !op2
.maybe_isnan ())
1267 r
= range_true (type
);
1269 r
= range_true_and_false (type
);
1274 foperator_ordered::op1_range (frange
&r
, tree type
,
1277 relation_trio trio
) const
1279 relation_kind rel
= trio
.op1_op2 ();
1280 switch (get_bool_state (r
, lhs
, type
))
1283 // The TRUE side of ORDERED means both operands are !NAN, so
1284 // it's impossible for op2 to be a NAN.
1285 if (op2
.known_isnan ())
1289 r
.set_varying (type
);
1295 // The FALSE side of op1 ORDERED op1 implies op1 is NAN.
1299 r
.set_varying (type
);
1309 operator_negate::fold_range (frange
&r
, tree type
,
1310 const frange
&op1
, const frange
&op2
,
1311 relation_trio
) const
1313 if (empty_range_varying (r
, type
, op1
, op2
))
1315 if (op1
.known_isnan ())
1318 if (op1
.nan_signbit_p (sign
))
1319 r
.set_nan (type
, !sign
);
1325 REAL_VALUE_TYPE lh_lb
= op1
.lower_bound ();
1326 REAL_VALUE_TYPE lh_ub
= op1
.upper_bound ();
1327 lh_lb
= real_value_negate (&lh_lb
);
1328 lh_ub
= real_value_negate (&lh_ub
);
1329 r
.set (type
, lh_ub
, lh_lb
);
1330 if (op1
.maybe_isnan ())
1333 if (op1
.nan_signbit_p (sign
))
1334 r
.update_nan (!sign
);
1344 operator_negate::op1_range (frange
&r
, tree type
,
1345 const frange
&lhs
, const frange
&op2
,
1346 relation_trio rel
) const
1348 return fold_range (r
, type
, lhs
, op2
, rel
);
1352 operator_abs::fold_range (frange
&r
, tree type
,
1353 const frange
&op1
, const frange
&op2
,
1354 relation_trio
) const
1356 if (empty_range_varying (r
, type
, op1
, op2
))
1358 if (op1
.known_isnan ())
1360 r
.set_nan (type
, /*sign=*/false);
1364 const REAL_VALUE_TYPE lh_lb
= op1
.lower_bound ();
1365 const REAL_VALUE_TYPE lh_ub
= op1
.upper_bound ();
1366 // Handle the easy case where everything is positive.
1367 if (real_compare (GE_EXPR
, &lh_lb
, &dconst0
)
1368 && !real_iszero (&lh_lb
, /*sign=*/true)
1369 && !op1
.maybe_isnan (/*sign=*/true))
1375 REAL_VALUE_TYPE min
= real_value_abs (&lh_lb
);
1376 REAL_VALUE_TYPE max
= real_value_abs (&lh_ub
);
1377 // If the range contains zero then we know that the minimum value in the
1378 // range will be zero.
1379 if (real_compare (LE_EXPR
, &lh_lb
, &dconst0
)
1380 && real_compare (GE_EXPR
, &lh_ub
, &dconst0
))
1382 if (real_compare (GT_EXPR
, &min
, &max
))
1388 // If the range was reversed, swap MIN and MAX.
1389 if (real_compare (GT_EXPR
, &min
, &max
))
1390 std::swap (min
, max
);
1393 r
.set (type
, min
, max
);
1394 if (op1
.maybe_isnan ())
1395 r
.update_nan (/*sign=*/false);
1402 operator_abs::op1_range (frange
&r
, tree type
,
1403 const frange
&lhs
, const frange
&op2
,
1404 relation_trio
) const
1406 if (empty_range_varying (r
, type
, lhs
, op2
))
1408 if (lhs
.known_isnan ())
1414 // Start with the positives because negatives are an impossible result.
1415 frange
positives (type
, dconst0
, frange_val_max (type
));
1416 positives
.update_nan (/*sign=*/false);
1417 positives
.intersect (lhs
);
1419 // Add -NAN if relevant.
1420 if (r
.maybe_isnan ())
1423 neg_nan
.set_nan (type
, true);
1426 if (r
.known_isnan () || r
.undefined_p ())
1428 // Then add the negative of each pair:
1429 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
1430 frange
negatives (type
, real_value_negate (&positives
.upper_bound ()),
1431 real_value_negate (&positives
.lower_bound ()));
1432 negatives
.clear_nan ();
1433 r
.union_ (negatives
);
1437 class foperator_unordered_lt
: public range_operator
1439 using range_operator::fold_range
;
1440 using range_operator::op1_range
;
1441 using range_operator::op2_range
;
1443 bool fold_range (irange
&r
, tree type
,
1444 const frange
&op1
, const frange
&op2
,
1445 relation_trio rel
= TRIO_VARYING
) const final override
1447 if (op1
.known_isnan () || op2
.known_isnan ())
1449 r
= range_true (type
);
1452 frange op1_no_nan
= op1
;
1453 frange op2_no_nan
= op2
;
1454 if (op1
.maybe_isnan ())
1455 op1_no_nan
.clear_nan ();
1456 if (op2
.maybe_isnan ())
1457 op2_no_nan
.clear_nan ();
1458 if (!range_op_handler (LT_EXPR
).fold_range (r
, type
, op1_no_nan
,
1461 // The result is the same as the ordered version when the
1462 // comparison is true or when the operands cannot be NANs.
1463 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1467 r
= range_true_and_false (type
);
1471 bool op1_range (frange
&r
, tree type
,
1474 relation_trio trio
) const final override
;
1475 bool op2_range (frange
&r
, tree type
,
1478 relation_trio trio
) const final override
;
1482 foperator_unordered_lt::op1_range (frange
&r
, tree type
,
1485 relation_trio
) const
1487 switch (get_bool_state (r
, lhs
, type
))
1490 if (op2
.known_isnan () || op2
.maybe_isnan ())
1491 r
.set_varying (type
);
1492 else if (op2
.undefined_p ())
1495 build_lt (r
, type
, op2
);
1499 // A false UNORDERED_LT means both operands are !NAN, so it's
1500 // impossible for op2 to be a NAN.
1501 if (op2
.known_isnan ())
1503 else if (op2
.undefined_p ())
1505 else if (build_ge (r
, type
, op2
))
1516 foperator_unordered_lt::op2_range (frange
&r
, tree type
,
1519 relation_trio
) const
1521 switch (get_bool_state (r
, lhs
, type
))
1524 if (op1
.known_isnan () || op1
.maybe_isnan ())
1525 r
.set_varying (type
);
1526 else if (op1
.undefined_p ())
1529 build_gt (r
, type
, op1
);
1533 // A false UNORDERED_LT means both operands are !NAN, so it's
1534 // impossible for op1 to be a NAN.
1535 if (op1
.known_isnan ())
1537 else if (op1
.undefined_p ())
1539 else if (build_le (r
, type
, op1
))
1549 class foperator_unordered_le
: public range_operator
1551 using range_operator::fold_range
;
1552 using range_operator::op1_range
;
1553 using range_operator::op2_range
;
1555 bool fold_range (irange
&r
, tree type
,
1556 const frange
&op1
, const frange
&op2
,
1557 relation_trio rel
= TRIO_VARYING
) const final override
1559 if (op1
.known_isnan () || op2
.known_isnan ())
1561 r
= range_true (type
);
1564 frange op1_no_nan
= op1
;
1565 frange op2_no_nan
= op2
;
1566 if (op1
.maybe_isnan ())
1567 op1_no_nan
.clear_nan ();
1568 if (op2
.maybe_isnan ())
1569 op2_no_nan
.clear_nan ();
1570 if (!range_op_handler (LE_EXPR
).fold_range (r
, type
, op1_no_nan
,
1573 // The result is the same as the ordered version when the
1574 // comparison is true or when the operands cannot be NANs.
1575 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1579 r
= range_true_and_false (type
);
1583 bool op1_range (frange
&r
, tree type
,
1584 const irange
&lhs
, const frange
&op2
,
1585 relation_trio
= TRIO_VARYING
) const final override
;
1586 bool op2_range (frange
&r
, tree type
,
1587 const irange
&lhs
, const frange
&op1
,
1588 relation_trio
= TRIO_VARYING
) const final override
;
1592 foperator_unordered_le::op1_range (frange
&r
, tree type
,
1593 const irange
&lhs
, const frange
&op2
,
1594 relation_trio
) const
1596 switch (get_bool_state (r
, lhs
, type
))
1599 if (op2
.known_isnan () || op2
.maybe_isnan ())
1600 r
.set_varying (type
);
1601 else if (op2
.undefined_p ())
1604 build_le (r
, type
, op2
);
1608 // A false UNORDERED_LE means both operands are !NAN, so it's
1609 // impossible for op2 to be a NAN.
1610 if (op2
.known_isnan ())
1612 else if (build_gt (r
, type
, op2
))
1623 foperator_unordered_le::op2_range (frange
&r
,
1627 relation_trio
) const
1629 switch (get_bool_state (r
, lhs
, type
))
1632 if (op1
.known_isnan () || op1
.maybe_isnan ())
1633 r
.set_varying (type
);
1634 else if (op1
.undefined_p ())
1637 build_ge (r
, type
, op1
);
1641 // A false UNORDERED_LE means both operands are !NAN, so it's
1642 // impossible for op1 to be a NAN.
1643 if (op1
.known_isnan ())
1645 else if (op1
.undefined_p ())
1647 else if (build_lt (r
, type
, op1
))
1657 class foperator_unordered_gt
: public range_operator
1659 using range_operator::fold_range
;
1660 using range_operator::op1_range
;
1661 using range_operator::op2_range
;
1663 bool fold_range (irange
&r
, tree type
,
1664 const frange
&op1
, const frange
&op2
,
1665 relation_trio rel
= TRIO_VARYING
) const final override
1667 if (op1
.known_isnan () || op2
.known_isnan ())
1669 r
= range_true (type
);
1672 frange op1_no_nan
= op1
;
1673 frange op2_no_nan
= op2
;
1674 if (op1
.maybe_isnan ())
1675 op1_no_nan
.clear_nan ();
1676 if (op2
.maybe_isnan ())
1677 op2_no_nan
.clear_nan ();
1678 if (!range_op_handler (GT_EXPR
).fold_range (r
, type
, op1_no_nan
,
1681 // The result is the same as the ordered version when the
1682 // comparison is true or when the operands cannot be NANs.
1683 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1687 r
= range_true_and_false (type
);
1691 bool op1_range (frange
&r
, tree type
,
1692 const irange
&lhs
, const frange
&op2
,
1693 relation_trio
= TRIO_VARYING
) const final override
;
1694 bool op2_range (frange
&r
, tree type
,
1695 const irange
&lhs
, const frange
&op1
,
1696 relation_trio
= TRIO_VARYING
) const final override
;
1700 foperator_unordered_gt::op1_range (frange
&r
,
1704 relation_trio
) const
1706 switch (get_bool_state (r
, lhs
, type
))
1709 if (op2
.known_isnan () || op2
.maybe_isnan ())
1710 r
.set_varying (type
);
1711 else if (op2
.undefined_p ())
1714 build_gt (r
, type
, op2
);
1718 // A false UNORDERED_GT means both operands are !NAN, so it's
1719 // impossible for op2 to be a NAN.
1720 if (op2
.known_isnan ())
1722 else if (op2
.undefined_p ())
1724 else if (build_le (r
, type
, op2
))
1735 foperator_unordered_gt::op2_range (frange
&r
,
1739 relation_trio
) const
1741 switch (get_bool_state (r
, lhs
, type
))
1744 if (op1
.known_isnan () || op1
.maybe_isnan ())
1745 r
.set_varying (type
);
1746 else if (op1
.undefined_p ())
1749 build_lt (r
, type
, op1
);
1753 // A false UNORDERED_GT means both operands are !NAN, so it's
1754 // impossible for op1 to be a NAN.
1755 if (op1
.known_isnan ())
1757 else if (op1
.undefined_p ())
1759 else if (build_ge (r
, type
, op1
))
1769 class foperator_unordered_ge
: public range_operator
1771 using range_operator::fold_range
;
1772 using range_operator::op1_range
;
1773 using range_operator::op2_range
;
1775 bool fold_range (irange
&r
, tree type
,
1776 const frange
&op1
, const frange
&op2
,
1777 relation_trio rel
= TRIO_VARYING
) const final override
1779 if (op1
.known_isnan () || op2
.known_isnan ())
1781 r
= range_true (type
);
1784 frange op1_no_nan
= op1
;
1785 frange op2_no_nan
= op2
;
1786 if (op1
.maybe_isnan ())
1787 op1_no_nan
.clear_nan ();
1788 if (op2
.maybe_isnan ())
1789 op2_no_nan
.clear_nan ();
1790 if (!range_op_handler (GE_EXPR
).fold_range (r
, type
, op1_no_nan
,
1793 // The result is the same as the ordered version when the
1794 // comparison is true or when the operands cannot be NANs.
1795 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1799 r
= range_true_and_false (type
);
1803 bool op1_range (frange
&r
, tree type
,
1804 const irange
&lhs
, const frange
&op2
,
1805 relation_trio
= TRIO_VARYING
) const final override
;
1806 bool op2_range (frange
&r
, tree type
,
1807 const irange
&lhs
, const frange
&op1
,
1808 relation_trio
= TRIO_VARYING
) const final override
;
1812 foperator_unordered_ge::op1_range (frange
&r
,
1816 relation_trio
) const
1818 switch (get_bool_state (r
, lhs
, type
))
1821 if (op2
.known_isnan () || op2
.maybe_isnan ())
1822 r
.set_varying (type
);
1823 else if (op2
.undefined_p ())
1826 build_ge (r
, type
, op2
);
1830 // A false UNORDERED_GE means both operands are !NAN, so it's
1831 // impossible for op2 to be a NAN.
1832 if (op2
.known_isnan ())
1834 else if (op2
.undefined_p ())
1836 else if (build_lt (r
, type
, op2
))
1847 foperator_unordered_ge::op2_range (frange
&r
, tree type
,
1850 relation_trio
) const
1852 switch (get_bool_state (r
, lhs
, type
))
1855 if (op1
.known_isnan () || op1
.maybe_isnan ())
1856 r
.set_varying (type
);
1857 else if (op1
.undefined_p ())
1860 build_le (r
, type
, op1
);
1864 // A false UNORDERED_GE means both operands are !NAN, so it's
1865 // impossible for op1 to be a NAN.
1866 if (op1
.known_isnan ())
1868 else if (op1
.undefined_p ())
1870 else if (build_gt (r
, type
, op1
))
1880 class foperator_unordered_equal
: public range_operator
1882 using range_operator::fold_range
;
1883 using range_operator::op1_range
;
1884 using range_operator::op2_range
;
1886 bool fold_range (irange
&r
, tree type
,
1887 const frange
&op1
, const frange
&op2
,
1888 relation_trio rel
= TRIO_VARYING
) const final override
1890 if (op1
.known_isnan () || op2
.known_isnan ())
1892 r
= range_true (type
);
1895 frange op1_no_nan
= op1
;
1896 frange op2_no_nan
= op2
;
1897 if (op1
.maybe_isnan ())
1898 op1_no_nan
.clear_nan ();
1899 if (op2
.maybe_isnan ())
1900 op2_no_nan
.clear_nan ();
1901 if (!range_op_handler (EQ_EXPR
).fold_range (r
, type
, op1_no_nan
,
1904 // The result is the same as the ordered version when the
1905 // comparison is true or when the operands cannot be NANs.
1906 if (!maybe_isnan (op1
, op2
) || r
== range_true (type
))
1910 r
= range_true_and_false (type
);
1914 bool op1_range (frange
&r
, tree type
,
1915 const irange
&lhs
, const frange
&op2
,
1916 relation_trio
= TRIO_VARYING
) const final override
;
1917 bool op2_range (frange
&r
, tree type
,
1918 const irange
&lhs
, const frange
&op1
,
1919 relation_trio rel
= TRIO_VARYING
) const final override
1921 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1923 } fop_unordered_equal
;
1926 foperator_unordered_equal::op1_range (frange
&r
, tree type
,
1929 relation_trio
) const
1931 switch (get_bool_state (r
, lhs
, type
))
1934 // If it's true, the result is the same as OP2 plus a NAN.
1936 // Add both zeros if there's the possibility of zero equality.
1937 frange_add_zeros (r
, type
);
1938 // Add the possibility of a NAN.
1943 // A false UNORDERED_EQ means both operands are !NAN, so it's
1944 // impossible for op2 to be a NAN.
1945 if (op2
.known_isnan ())
1949 // The false side indicates !NAN and not equal. We can at least
1951 r
.set_varying (type
);
1962 class foperator_ltgt
: public range_operator
1964 using range_operator::fold_range
;
1965 using range_operator::op1_range
;
1966 using range_operator::op2_range
;
1968 bool fold_range (irange
&r
, tree type
,
1969 const frange
&op1
, const frange
&op2
,
1970 relation_trio rel
= TRIO_VARYING
) const final override
1972 if (op1
.known_isnan () || op2
.known_isnan ())
1974 r
= range_false (type
);
1977 frange op1_no_nan
= op1
;
1978 frange op2_no_nan
= op2
;
1979 if (op1
.maybe_isnan ())
1980 op1_no_nan
.clear_nan ();
1981 if (op2
.maybe_isnan ())
1982 op2_no_nan
.clear_nan ();
1983 if (!range_op_handler (NE_EXPR
).fold_range (r
, type
, op1_no_nan
,
1986 // The result is the same as the ordered version when the
1987 // comparison is true or when the operands cannot be NANs.
1988 if (!maybe_isnan (op1
, op2
) || r
== range_false (type
))
1992 r
= range_true_and_false (type
);
1996 bool op1_range (frange
&r
, tree type
,
1997 const irange
&lhs
, const frange
&op2
,
1998 relation_trio
= TRIO_VARYING
) const final override
;
1999 bool op2_range (frange
&r
, tree type
,
2000 const irange
&lhs
, const frange
&op1
,
2001 relation_trio rel
= TRIO_VARYING
) const final override
2003 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
2008 foperator_ltgt::op1_range (frange
&r
, tree type
,
2011 relation_trio
) const
2013 switch (get_bool_state (r
, lhs
, type
))
2016 // A true LTGT means both operands are !NAN, so it's
2017 // impossible for op2 to be a NAN.
2018 if (op2
.known_isnan ())
2022 // The true side indicates !NAN and not equal. We can at least
2024 r
.set_varying (type
);
2030 // If it's false, the result is the same as OP2 plus a NAN.
2032 // Add both zeros if there's the possibility of zero equality.
2033 frange_add_zeros (r
, type
);
2034 // Add the possibility of a NAN.
2044 // Final tweaks for float binary op op1_range/op2_range.
2045 // Return TRUE if the operation is performed and a valid range is available.
2048 float_binary_op_range_finish (bool ret
, frange
&r
, tree type
,
2049 const frange
&lhs
, bool div_op2
= false)
2054 // If we get a known NAN from reverse op, it means either that
2055 // the other operand was known NAN (in that case we know nothing),
2056 // or the reverse operation introduced a known NAN.
2057 // Say for lhs = op1 * op2 if lhs is [-0, +0] and op2 is too,
2058 // 0 / 0 is known NAN. Just punt in that case.
2059 // If NANs aren't honored, we get for 0 / 0 UNDEFINED, so punt as well.
2060 // Or if lhs is a known NAN, we also don't know anything.
2061 if (r
.known_isnan () || lhs
.known_isnan () || r
.undefined_p ())
2063 r
.set_varying (type
);
2067 // If lhs isn't NAN, then neither operand could be NAN,
2068 // even if the reverse operation does introduce a maybe_nan.
2069 if (!lhs
.maybe_isnan ())
2073 ? !(real_compare (LE_EXPR
, &lhs
.lower_bound (), &dconst0
)
2074 && real_compare (GE_EXPR
, &lhs
.upper_bound (), &dconst0
))
2075 : !(real_isinf (&lhs
.lower_bound ())
2076 || real_isinf (&lhs
.upper_bound ())))
2077 // For reverse + or - or * or op1 of /, if result is finite, then
2078 // r must be finite too, as X + INF or X - INF or X * INF or
2079 // INF / X is always +-INF or NAN. For op2 of /, if result is
2080 // non-zero and not NAN, r must be finite, as X / INF is always
2082 frange_drop_infs (r
, type
);
2084 // If lhs is a maybe or known NAN, the operand could be
2091 // True if [lb, ub] is [+-0, +-0].
2093 zero_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2095 return real_iszero (&lb
) && real_iszero (&ub
);
2098 // True if +0 or -0 is in [lb, ub] range.
2100 contains_zero_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2102 return (real_compare (LE_EXPR
, &lb
, &dconst0
)
2103 && real_compare (GE_EXPR
, &ub
, &dconst0
));
2106 // True if [lb, ub] is [-INF, -INF] or [+INF, +INF].
2108 singleton_inf_p (const REAL_VALUE_TYPE
&lb
, const REAL_VALUE_TYPE
&ub
)
2110 return real_isinf (&lb
) && real_isinf (&ub
, real_isneg (&lb
));
2113 // Return -1 if binary op result must have sign bit set,
2114 // 1 if binary op result must have sign bit clear,
2116 // Sign bit of binary op result is exclusive or of the
2117 // operand's sign bits.
2119 signbit_known_p (const REAL_VALUE_TYPE
&lh_lb
, const REAL_VALUE_TYPE
&lh_ub
,
2120 const REAL_VALUE_TYPE
&rh_lb
, const REAL_VALUE_TYPE
&rh_ub
)
2122 if (real_isneg (&lh_lb
) == real_isneg (&lh_ub
)
2123 && real_isneg (&rh_lb
) == real_isneg (&rh_ub
))
2125 if (real_isneg (&lh_lb
) == real_isneg (&rh_ub
))
2133 // Set [lb, ub] to [-0, -0], [-0, +0] or [+0, +0] depending on
2136 zero_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2139 if (signbit_known
<= 0)
2141 if (signbit_known
< 0)
2145 // Set [lb, ub] to [-INF, -INF], [-INF, +INF] or [+INF, +INF] depending on
2148 inf_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2150 if (signbit_known
> 0)
2151 ub
= lb
= dconstinf
;
2152 else if (signbit_known
< 0)
2153 ub
= lb
= dconstninf
;
2161 // Set [lb, ub] to [-INF, -0], [-INF, +INF] or [+0, +INF] depending on
2164 zero_to_inf_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, int signbit_known
)
2166 if (signbit_known
> 0)
2171 else if (signbit_known
< 0)
2183 /* Extend the LHS range by 1ulp in each direction. For op1_range
2184 or op2_range of binary operations just computing the inverse
2185 operation on ranges isn't sufficient. Consider e.g.
2186 [1., 1.] = op1 + [1., 1.]. op1's range is not [0., 0.], but
2187 [-0x1.0p-54, 0x1.0p-53] (when not -frounding-math), any value for
2188 which adding 1. to it results in 1. after rounding to nearest.
2189 So, for op1_range/op2_range extend the lhs range by 1ulp (or 0.5ulp)
2190 in each direction. See PR109008 for more details. */
2193 float_widen_lhs_range (tree type
, const frange
&lhs
)
2196 if (lhs
.known_isnan ())
2198 REAL_VALUE_TYPE lb
= lhs
.lower_bound ();
2199 REAL_VALUE_TYPE ub
= lhs
.upper_bound ();
2200 if (real_isfinite (&lb
))
2202 frange_nextafter (TYPE_MODE (type
), lb
, dconstninf
);
2203 if (real_isinf (&lb
))
2205 /* For -DBL_MAX, instead of -Inf use
2206 nexttoward (-DBL_MAX, -LDBL_MAX) in a hypothetical
2207 wider type with the same mantissa precision but larger
2208 exponent range; it is outside of range of double values,
2209 but makes it clear it is just one ulp larger rather than
2210 infinite amount larger. */
2212 SET_REAL_EXP (&lb
, FLOAT_MODE_FORMAT (TYPE_MODE (type
))->emax
+ 1);
2214 if (!flag_rounding_math
&& !MODE_COMPOSITE_P (TYPE_MODE (type
)))
2216 /* If not -frounding-math nor IBM double double, actually widen
2217 just by 0.5ulp rather than 1ulp. */
2218 REAL_VALUE_TYPE tem
;
2219 real_arithmetic (&tem
, PLUS_EXPR
, &lhs
.lower_bound (), &lb
);
2220 real_arithmetic (&lb
, RDIV_EXPR
, &tem
, &dconst2
);
2223 if (real_isfinite (&ub
))
2225 frange_nextafter (TYPE_MODE (type
), ub
, dconstinf
);
2226 if (real_isinf (&ub
))
2228 /* For DBL_MAX similarly. */
2230 SET_REAL_EXP (&ub
, FLOAT_MODE_FORMAT (TYPE_MODE (type
))->emax
+ 1);
2232 if (!flag_rounding_math
&& !MODE_COMPOSITE_P (TYPE_MODE (type
)))
2234 /* If not -frounding-math nor IBM double double, actually widen
2235 just by 0.5ulp rather than 1ulp. */
2236 REAL_VALUE_TYPE tem
;
2237 real_arithmetic (&tem
, PLUS_EXPR
, &lhs
.upper_bound (), &ub
);
2238 real_arithmetic (&ub
, RDIV_EXPR
, &tem
, &dconst2
);
2241 /* Temporarily disable -ffinite-math-only, so that frange::set doesn't
2242 reduce the range back to real_min_representable (type) as lower bound
2243 or real_max_representable (type) as upper bound. */
2244 bool save_flag_finite_math_only
= flag_finite_math_only
;
2245 flag_finite_math_only
= false;
2246 ret
.set (type
, lb
, ub
, lhs
.get_nan_state ());
2247 flag_finite_math_only
= save_flag_finite_math_only
;
2252 operator_plus::op1_range (frange
&r
, tree type
, const frange
&lhs
,
2253 const frange
&op2
, relation_trio
) const
2255 if (lhs
.undefined_p ())
2257 range_op_handler
minus (MINUS_EXPR
);
2260 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2261 return float_binary_op_range_finish (minus
.fold_range (r
, type
, wlhs
, op2
),
2266 operator_plus::op2_range (frange
&r
, tree type
,
2267 const frange
&lhs
, const frange
&op1
,
2268 relation_trio
) const
2270 return op1_range (r
, type
, lhs
, op1
);
2274 operator_plus::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2275 bool &maybe_nan
, tree type
,
2276 const REAL_VALUE_TYPE
&lh_lb
,
2277 const REAL_VALUE_TYPE
&lh_ub
,
2278 const REAL_VALUE_TYPE
&rh_lb
,
2279 const REAL_VALUE_TYPE
&rh_ub
,
2280 relation_kind
) const
2282 frange_arithmetic (PLUS_EXPR
, type
, lb
, lh_lb
, rh_lb
, dconstninf
);
2283 frange_arithmetic (PLUS_EXPR
, type
, ub
, lh_ub
, rh_ub
, dconstinf
);
2285 // [-INF] + [+INF] = NAN
2286 if (real_isinf (&lh_lb
, true) && real_isinf (&rh_ub
, false))
2288 // [+INF] + [-INF] = NAN
2289 else if (real_isinf (&lh_ub
, false) && real_isinf (&rh_lb
, true))
2297 operator_minus::op1_range (frange
&r
, tree type
,
2298 const frange
&lhs
, const frange
&op2
,
2299 relation_trio
) const
2301 if (lhs
.undefined_p ())
2303 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2304 return float_binary_op_range_finish (
2305 range_op_handler (PLUS_EXPR
).fold_range (r
, type
, wlhs
, op2
),
2310 operator_minus::op2_range (frange
&r
, tree type
,
2311 const frange
&lhs
, const frange
&op1
,
2312 relation_trio
) const
2314 if (lhs
.undefined_p ())
2316 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2317 return float_binary_op_range_finish (fold_range (r
, type
, op1
, wlhs
),
2322 operator_minus::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2323 bool &maybe_nan
, tree type
,
2324 const REAL_VALUE_TYPE
&lh_lb
,
2325 const REAL_VALUE_TYPE
&lh_ub
,
2326 const REAL_VALUE_TYPE
&rh_lb
,
2327 const REAL_VALUE_TYPE
&rh_ub
,
2328 relation_kind
) const
2330 frange_arithmetic (MINUS_EXPR
, type
, lb
, lh_lb
, rh_ub
, dconstninf
);
2331 frange_arithmetic (MINUS_EXPR
, type
, ub
, lh_ub
, rh_lb
, dconstinf
);
2333 // [+INF] - [+INF] = NAN
2334 if (real_isinf (&lh_ub
, false) && real_isinf (&rh_ub
, false))
2336 // [-INF] - [-INF] = NAN
2337 else if (real_isinf (&lh_lb
, true) && real_isinf (&rh_lb
, true))
2344 // Given CP[0] to CP[3] floating point values rounded to -INF,
2345 // set LB to the smallest of them (treating -0 as smaller to +0).
2346 // Given CP[4] to CP[7] floating point values rounded to +INF,
2347 // set UB to the largest of them (treating -0 as smaller to +0).
2350 find_range (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2351 const REAL_VALUE_TYPE (&cp
)[8])
2355 for (int i
= 1; i
< 4; ++i
)
2357 if (real_less (&cp
[i
], &lb
)
2358 || (real_iszero (&lb
) && real_isnegzero (&cp
[i
])))
2360 if (real_less (&ub
, &cp
[i
+ 4])
2361 || (real_isnegzero (&ub
) && real_iszero (&cp
[i
+ 4])))
2368 operator_mult::op1_range (frange
&r
, tree type
,
2369 const frange
&lhs
, const frange
&op2
,
2370 relation_trio
) const
2372 if (lhs
.undefined_p ())
2374 range_op_handler
rdiv (RDIV_EXPR
);
2377 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2378 bool ret
= rdiv
.fold_range (r
, type
, wlhs
, op2
);
2381 if (wlhs
.known_isnan () || op2
.known_isnan () || op2
.undefined_p ())
2382 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2383 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2384 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2385 const REAL_VALUE_TYPE
&op2_lb
= op2
.lower_bound ();
2386 const REAL_VALUE_TYPE
&op2_ub
= op2
.upper_bound ();
2387 if ((contains_zero_p (lhs_lb
, lhs_ub
) && contains_zero_p (op2_lb
, op2_ub
))
2388 || ((real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))
2389 && (real_isinf (&op2_lb
) || real_isinf (&op2_ub
))))
2391 // If both lhs and op2 could be zeros or both could be infinities,
2392 // we don't know anything about op1 except maybe for the sign
2393 // and perhaps if it can be NAN or not.
2394 REAL_VALUE_TYPE lb
, ub
;
2395 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op2_lb
, op2_ub
);
2396 zero_to_inf_range (lb
, ub
, signbit_known
);
2397 r
.set (type
, lb
, ub
);
2399 // Otherwise, if op2 is a singleton INF and lhs doesn't include INF,
2400 // or if lhs must be zero and op2 doesn't include zero, it would be
2401 // UNDEFINED, while rdiv.fold_range computes a zero or singleton INF
2402 // range. Those are supersets of UNDEFINED, so let's keep that way.
2403 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2407 operator_mult::op2_range (frange
&r
, tree type
,
2408 const frange
&lhs
, const frange
&op1
,
2409 relation_trio
) const
2411 return op1_range (r
, type
, lhs
, op1
);
2415 operator_mult::rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
,
2416 bool &maybe_nan
, tree type
,
2417 const REAL_VALUE_TYPE
&lh_lb
,
2418 const REAL_VALUE_TYPE
&lh_ub
,
2419 const REAL_VALUE_TYPE
&rh_lb
,
2420 const REAL_VALUE_TYPE
&rh_ub
,
2421 relation_kind kind
) const
2425 && real_equal (&lh_lb
, &rh_lb
)
2426 && real_equal (&lh_ub
, &rh_ub
)
2427 && real_isneg (&lh_lb
) == real_isneg (&rh_lb
)
2428 && real_isneg (&lh_ub
) == real_isneg (&rh_ub
));
2431 // x * x never produces a new NAN and we only multiply the same
2432 // values, so the 0 * INF problematic cases never appear there.
2435 // [+-0, +-0] * [+INF,+INF] (or [-INF,-INF] or swapped is a known NAN.
2436 if ((zero_p (lh_lb
, lh_ub
) && singleton_inf_p (rh_lb
, rh_ub
))
2437 || (zero_p (rh_lb
, rh_ub
) && singleton_inf_p (lh_lb
, lh_ub
)))
2439 real_nan (&lb
, "", 0, TYPE_MODE (type
));
2445 // Otherwise, if one range includes zero and the other ends with +-INF,
2446 // it is a maybe NAN.
2447 if ((contains_zero_p (lh_lb
, lh_ub
)
2448 && (real_isinf (&rh_lb
) || real_isinf (&rh_ub
)))
2449 || (contains_zero_p (rh_lb
, rh_ub
)
2450 && (real_isinf (&lh_lb
) || real_isinf (&lh_ub
))))
2454 int signbit_known
= signbit_known_p (lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2456 // If one of the ranges that includes INF is singleton
2457 // and the other range includes zero, the resulting
2458 // range is INF and NAN, because the 0 * INF boundary
2459 // case will be NAN, but already nextafter (0, 1) * INF
2461 if (singleton_inf_p (lh_lb
, lh_ub
)
2462 || singleton_inf_p (rh_lb
, rh_ub
))
2463 return inf_range (lb
, ub
, signbit_known
);
2465 // If one of the multiplicands must be zero, the resulting
2466 // range is +-0 and NAN.
2467 if (zero_p (lh_lb
, lh_ub
) || zero_p (rh_lb
, rh_ub
))
2468 return zero_range (lb
, ub
, signbit_known
);
2470 // Otherwise one of the multiplicands could be
2471 // [0.0, nextafter (0.0, 1.0)] and the [DBL_MAX, INF]
2472 // or similarly with different signs. 0.0 * DBL_MAX
2473 // is still 0.0, nextafter (0.0, 1.0) * INF is still INF,
2474 // so if the signs are always the same or always different,
2475 // result is [+0.0, +INF] or [-INF, -0.0], otherwise VARYING.
2476 return zero_to_inf_range (lb
, ub
, signbit_known
);
2480 REAL_VALUE_TYPE cp
[8];
2481 // Do a cross-product. At this point none of the multiplications
2482 // should produce a NAN.
2483 frange_arithmetic (MULT_EXPR
, type
, cp
[0], lh_lb
, rh_lb
, dconstninf
);
2484 frange_arithmetic (MULT_EXPR
, type
, cp
[4], lh_lb
, rh_lb
, dconstinf
);
2487 // For x * x we can just do max (lh_lb * lh_lb, lh_ub * lh_ub)
2488 // as maximum and -0.0 as minimum if 0.0 is in the range,
2489 // otherwise min (lh_lb * lh_lb, lh_ub * lh_ub).
2490 // -0.0 rather than 0.0 because VREL_EQ doesn't prove that
2491 // x and y are bitwise equal, just that they compare equal.
2492 if (contains_zero_p (lh_lb
, lh_ub
))
2494 if (real_isneg (&lh_lb
) == real_isneg (&lh_ub
))
2507 frange_arithmetic (MULT_EXPR
, type
, cp
[1], lh_lb
, rh_ub
, dconstninf
);
2508 frange_arithmetic (MULT_EXPR
, type
, cp
[5], lh_lb
, rh_ub
, dconstinf
);
2509 frange_arithmetic (MULT_EXPR
, type
, cp
[2], lh_ub
, rh_lb
, dconstninf
);
2510 frange_arithmetic (MULT_EXPR
, type
, cp
[6], lh_ub
, rh_lb
, dconstinf
);
2512 frange_arithmetic (MULT_EXPR
, type
, cp
[3], lh_ub
, rh_ub
, dconstninf
);
2513 frange_arithmetic (MULT_EXPR
, type
, cp
[7], lh_ub
, rh_ub
, dconstinf
);
2515 find_range (lb
, ub
, cp
);
2519 class foperator_div
: public range_operator
2521 using range_operator::op1_range
;
2522 using range_operator::op2_range
;
2524 virtual bool op1_range (frange
&r
, tree type
,
2527 relation_trio
= TRIO_VARYING
) const final override
2529 if (lhs
.undefined_p ())
2531 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2532 bool ret
= range_op_handler (MULT_EXPR
).fold_range (r
, type
, wlhs
, op2
);
2535 if (wlhs
.known_isnan () || op2
.known_isnan () || op2
.undefined_p ())
2536 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2537 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2538 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2539 const REAL_VALUE_TYPE
&op2_lb
= op2
.lower_bound ();
2540 const REAL_VALUE_TYPE
&op2_ub
= op2
.upper_bound ();
2541 if ((contains_zero_p (lhs_lb
, lhs_ub
)
2542 && (real_isinf (&op2_lb
) || real_isinf (&op2_ub
)))
2543 || ((contains_zero_p (op2_lb
, op2_ub
))
2544 && (real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))))
2546 // If both lhs could be zero and op2 infinity or vice versa,
2547 // we don't know anything about op1 except maybe for the sign
2548 // and perhaps if it can be NAN or not.
2549 REAL_VALUE_TYPE lb
, ub
;
2550 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op2_lb
, op2_ub
);
2551 zero_to_inf_range (lb
, ub
, signbit_known
);
2552 r
.set (type
, lb
, ub
);
2554 return float_binary_op_range_finish (ret
, r
, type
, wlhs
);
2556 virtual bool op2_range (frange
&r
, tree type
,
2559 relation_trio
= TRIO_VARYING
) const final override
2561 if (lhs
.undefined_p ())
2563 frange wlhs
= float_widen_lhs_range (type
, lhs
);
2564 bool ret
= fold_range (r
, type
, op1
, wlhs
);
2567 if (wlhs
.known_isnan () || op1
.known_isnan () || op1
.undefined_p ())
2568 return float_binary_op_range_finish (ret
, r
, type
, wlhs
, true);
2569 const REAL_VALUE_TYPE
&lhs_lb
= wlhs
.lower_bound ();
2570 const REAL_VALUE_TYPE
&lhs_ub
= wlhs
.upper_bound ();
2571 const REAL_VALUE_TYPE
&op1_lb
= op1
.lower_bound ();
2572 const REAL_VALUE_TYPE
&op1_ub
= op1
.upper_bound ();
2573 if ((contains_zero_p (lhs_lb
, lhs_ub
) && contains_zero_p (op1_lb
, op1_ub
))
2574 || ((real_isinf (&lhs_lb
) || real_isinf (&lhs_ub
))
2575 && (real_isinf (&op1_lb
) || real_isinf (&op1_ub
))))
2577 // If both lhs and op1 could be zeros or both could be infinities,
2578 // we don't know anything about op2 except maybe for the sign
2579 // and perhaps if it can be NAN or not.
2580 REAL_VALUE_TYPE lb
, ub
;
2581 int signbit_known
= signbit_known_p (lhs_lb
, lhs_ub
, op1_lb
, op1_ub
);
2582 zero_to_inf_range (lb
, ub
, signbit_known
);
2583 r
.set (type
, lb
, ub
);
2585 return float_binary_op_range_finish (ret
, r
, type
, wlhs
, true);
2588 void rv_fold (REAL_VALUE_TYPE
&lb
, REAL_VALUE_TYPE
&ub
, bool &maybe_nan
,
2590 const REAL_VALUE_TYPE
&lh_lb
,
2591 const REAL_VALUE_TYPE
&lh_ub
,
2592 const REAL_VALUE_TYPE
&rh_lb
,
2593 const REAL_VALUE_TYPE
&rh_ub
,
2594 relation_kind
) const final override
2596 // +-0.0 / +-0.0 or +-INF / +-INF is a known NAN.
2597 if ((zero_p (lh_lb
, lh_ub
) && zero_p (rh_lb
, rh_ub
))
2598 || (singleton_inf_p (lh_lb
, lh_ub
) && singleton_inf_p (rh_lb
, rh_ub
)))
2600 real_nan (&lb
, "", 0, TYPE_MODE (type
));
2606 // If +-0.0 is in both ranges, it is a maybe NAN.
2607 if (contains_zero_p (lh_lb
, lh_ub
) && contains_zero_p (rh_lb
, rh_ub
))
2609 // If +-INF is in both ranges, it is a maybe NAN.
2610 else if ((real_isinf (&lh_lb
) || real_isinf (&lh_ub
))
2611 && (real_isinf (&rh_lb
) || real_isinf (&rh_ub
)))
2616 int signbit_known
= signbit_known_p (lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2618 // If dividend must be zero, the range is just +-0
2619 // (including if the divisor is +-INF).
2620 // If divisor must be +-INF, the range is just +-0
2621 // (including if the dividend is zero).
2622 if (zero_p (lh_lb
, lh_ub
) || singleton_inf_p (rh_lb
, rh_ub
))
2623 return zero_range (lb
, ub
, signbit_known
);
2625 // If divisor must be zero, the range is just +-INF
2626 // (including if the dividend is +-INF).
2627 // If dividend must be +-INF, the range is just +-INF
2628 // (including if the dividend is zero).
2629 if (zero_p (rh_lb
, rh_ub
) || singleton_inf_p (lh_lb
, lh_ub
))
2630 return inf_range (lb
, ub
, signbit_known
);
2632 // Otherwise if both operands may be zero, divisor could be
2633 // nextafter(0.0, +-1.0) and dividend +-0.0
2634 // in which case result is going to INF or vice versa and
2635 // result +0.0. So, all we can say for that case is if the
2636 // signs of divisor and dividend are always the same we have
2637 // [+0.0, +INF], if they are always different we have
2638 // [-INF, -0.0]. If they vary, VARYING.
2639 // If both may be +-INF, divisor could be INF and dividend FLT_MAX,
2640 // in which case result is going to INF or vice versa and
2641 // result +0.0. So, all we can say for that case is if the
2642 // signs of divisor and dividend are always the same we have
2643 // [+0.0, +INF], if they are always different we have
2644 // [-INF, -0.0]. If they vary, VARYING.
2646 return zero_to_inf_range (lb
, ub
, signbit_known
);
2648 REAL_VALUE_TYPE cp
[8];
2649 // Do a cross-division. At this point none of the divisions should
2651 frange_arithmetic (RDIV_EXPR
, type
, cp
[0], lh_lb
, rh_lb
, dconstninf
);
2652 frange_arithmetic (RDIV_EXPR
, type
, cp
[1], lh_lb
, rh_ub
, dconstninf
);
2653 frange_arithmetic (RDIV_EXPR
, type
, cp
[2], lh_ub
, rh_lb
, dconstninf
);
2654 frange_arithmetic (RDIV_EXPR
, type
, cp
[3], lh_ub
, rh_ub
, dconstninf
);
2655 frange_arithmetic (RDIV_EXPR
, type
, cp
[4], lh_lb
, rh_lb
, dconstinf
);
2656 frange_arithmetic (RDIV_EXPR
, type
, cp
[5], lh_lb
, rh_ub
, dconstinf
);
2657 frange_arithmetic (RDIV_EXPR
, type
, cp
[6], lh_ub
, rh_lb
, dconstinf
);
2658 frange_arithmetic (RDIV_EXPR
, type
, cp
[7], lh_ub
, rh_ub
, dconstinf
);
2660 find_range (lb
, ub
, cp
);
2662 // If divisor may be zero (but is not known to be only zero),
2663 // and dividend can't be zero, the range can go up to -INF or +INF
2664 // depending on the signs.
2665 if (contains_zero_p (rh_lb
, rh_ub
))
2667 if (signbit_known
<= 0)
2668 real_inf (&lb
, true);
2669 if (signbit_known
>= 0)
2670 real_inf (&ub
, false);
2676 // Initialize any float operators to the primary table
2679 range_op_table::initialize_float_ops ()
2681 set (UNLE_EXPR
, fop_unordered_le
);
2682 set (UNLT_EXPR
, fop_unordered_lt
);
2683 set (UNGE_EXPR
, fop_unordered_ge
);
2684 set (UNGT_EXPR
, fop_unordered_gt
);
2685 set (UNEQ_EXPR
, fop_unordered_equal
);
2686 set (ORDERED_EXPR
, fop_ordered
);
2687 set (UNORDERED_EXPR
, fop_unordered
);
2688 set (LTGT_EXPR
, fop_ltgt
);
2689 set (RDIV_EXPR
, fop_div
);
2693 #include "selftest.h"
2698 // Build an frange from string endpoints.
2700 static inline frange
2701 frange_float (const char *lb
, const char *ub
, tree type
= float_type_node
)
2703 REAL_VALUE_TYPE min
, max
;
2704 gcc_assert (real_from_string (&min
, lb
) == 0);
2705 gcc_assert (real_from_string (&max
, ub
) == 0);
2706 return frange (type
, min
, max
);
2710 range_op_float_tests ()
2713 frange
trange (float_type_node
);
2715 // negate([-5, +10]) => [-10, 5]
2716 r0
= frange_float ("-5", "10");
2717 range_op_handler (NEGATE_EXPR
).fold_range (r
, float_type_node
, r0
, trange
);
2718 ASSERT_EQ (r
, frange_float ("-10", "5"));
2720 // negate([0, 1] -NAN) => [-1, -0] +NAN
2721 r0
= frange_float ("0", "1");
2722 r0
.update_nan (true);
2723 range_op_handler (NEGATE_EXPR
).fold_range (r
, float_type_node
, r0
, trange
);
2724 r1
= frange_float ("-1", "-0");
2725 r1
.update_nan (false);
2728 // [-INF,+INF] + [-INF,+INF] could be a NAN.
2729 range_op_handler
plus (PLUS_EXPR
);
2730 r0
.set_varying (float_type_node
);
2731 r1
.set_varying (float_type_node
);
2734 plus
.fold_range (r
, float_type_node
, r0
, r1
);
2735 if (HONOR_NANS (float_type_node
))
2736 ASSERT_TRUE (r
.maybe_isnan ());
2739 } // namespace selftest
2741 #endif // CHECKING_P