1 // Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FORTRAN_EVALUATE_ROUNDING_BITS_H_
16 #define FORTRAN_EVALUATE_ROUNDING_BITS_H_
17 
18 // A helper class used by Real<> to determine rounding of rational results
19 // to floating-point values.  Bits lost from intermediate computations by
20 // being shifted rightward are accumulated in instances of this class.
21 
22 namespace Fortran::evaluate::value {
23 
24 class RoundingBits {
25 public:
26   constexpr RoundingBits(
27       bool guard = false, bool round = false, bool sticky = false)
28     : guard_{guard}, round_{round}, sticky_{sticky} {}
29 
30   template<typename FRACTION>
RoundingBits(const FRACTION & fraction,int rshift)31   constexpr RoundingBits(const FRACTION &fraction, int rshift) {
32     if (rshift > 0 && rshift < fraction.bits + 1) {
33       guard_ = fraction.BTEST(rshift - 1);
34     }
35     if (rshift > 1 && rshift < fraction.bits + 2) {
36       round_ = fraction.BTEST(rshift - 2);
37     }
38     if (rshift > 2) {
39       if (rshift >= fraction.bits + 2) {
40         sticky_ = !fraction.IsZero();
41       } else {
42         auto mask{fraction.MASKR(rshift - 2)};
43         sticky_ = !fraction.IAND(mask).IsZero();
44       }
45     }
46   }
47 
guard()48   constexpr bool guard() const { return guard_; }
round()49   constexpr bool round() const { return round_; }
sticky()50   constexpr bool sticky() const { return sticky_; }
empty()51   constexpr bool empty() const { return !(guard_ | round_ | sticky_); }
52 
Negate()53   constexpr bool Negate() {
54     bool carry{!sticky_};
55     if (carry) {
56       carry = !round_;
57     } else {
58       round_ = !round_;
59     }
60     if (carry) {
61       carry = !guard_;
62     } else {
63       guard_ = !guard_;
64     }
65     return carry;
66   }
67 
ShiftLeft()68   constexpr bool ShiftLeft() {
69     bool oldGuard{guard_};
70     guard_ = round_;
71     round_ = sticky_;
72     return oldGuard;
73   }
74 
ShiftRight(bool newGuard)75   constexpr void ShiftRight(bool newGuard) {
76     sticky_ |= round_;
77     round_ = guard_;
78     guard_ = newGuard;
79   }
80 
81   // Determines whether a value should be rounded by increasing its
82   // fraction, given a rounding mode and a summary of the lost bits.
MustRound(Rounding rounding,bool isNegative,bool isOdd)83   constexpr bool MustRound(
84       Rounding rounding, bool isNegative, bool isOdd) const {
85     bool round{false};  // to dodge bogus g++ warning about missing return
86     switch (rounding.mode) {
87     case RoundingMode::TiesToEven:
88       round = guard_ && (round_ | sticky_ | isOdd);
89       break;
90     case RoundingMode::ToZero: break;
91     case RoundingMode::Down: round = isNegative && !empty(); break;
92     case RoundingMode::Up: round = !isNegative && !empty(); break;
93     case RoundingMode::TiesAwayFromZero: round = guard_; break;
94     }
95     return round;
96   }
97 
98 private:
99   bool guard_{false};  // 0.5 * ulp (unit in lowest place)
100   bool round_{false};  // 0.25 * ulp
101   bool sticky_{false};  // true if any lesser-valued bit would be set
102 };
103 }
104 #endif  // FORTRAN_EVALUATE_ROUNDING_BITS_H_
105