9#ifndef FORTRAN_EVALUATE_LOGICAL_H_
10#define FORTRAN_EVALUATE_LOGICAL_H_
15namespace Fortran::evaluate::value {
17template <
int BITS,
bool IS_LIKE_C = true>
class Logical {
19 static constexpr int bits{BITS};
20 using Word = Integer<bits>;
24 static constexpr bool IsLikeC{BITS <= 8 || IS_LIKE_C};
27 template <
int B,
bool C>
29 constexpr Logical(
bool truth) : word_{Represent(truth)} {}
31 constexpr Logical(Word &&w) : word_{std::move(w)} {}
34 word_ = Represent(x.IsTrue());
38 Word word()
const {
return word_; }
39 bool IsCanonical()
const {
40 return word_ == canonicalFalse || word_ == canonicalTrue;
47 template <
int B,
bool C>
49 return !IsTrue() && that.IsTrue();
51 template <
int B,
bool C>
55 template <
int B,
bool C>
57 return IsTrue() == that.IsTrue();
59 template <
int B,
bool C>
61 return IsTrue() != that.IsTrue();
63 template <
int B,
bool C>
67 template <
int B,
bool C>
69 return IsTrue() && !that.IsTrue();
72 constexpr bool IsTrue()
const {
73 if constexpr (IsLikeC) {
74 return !word_.IsZero();
76 return word_.BTEST(0);
80 constexpr Logical NOT()
const {
return {word_.IEOR(canonicalTrue)}; }
83 return {word_.IAND(that.word_)};
87 return {word_.IOR(that.word_)};
90 constexpr Logical EQV(
const Logical &that)
const {
return NEQV(that).NOT(); }
93 return {word_.IEOR(that.word_)};
97 static constexpr Word canonicalTrue{IsLikeC ? 1 : -std::uint64_t{1}};
98 static constexpr Word canonicalFalse{0};
99 static constexpr Word Represent(
bool x) {
100 return x ? canonicalTrue : canonicalFalse;