GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/fp/type_enumerator.h Lines: 27 44 61.4 %
Date: 2021-05-22 Branches: 17 70 24.3 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Tim King, Martin Brain, Aina Niemetz
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * An enumerator for floating-point numbers.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__FP__TYPE_ENUMERATOR_H
19
#define CVC5__THEORY__FP__TYPE_ENUMERATOR_H
20
21
#include "expr/kind.h"
22
#include "expr/type_node.h"
23
#include "theory/type_enumerator.h"
24
#include "util/bitvector.h"
25
#include "util/floatingpoint.h"
26
27
namespace cvc5 {
28
namespace theory {
29
namespace fp {
30
31
28
class FloatingPointEnumerator
32
    : public TypeEnumeratorBase<FloatingPointEnumerator> {
33
 public:
34
14
  FloatingPointEnumerator(TypeNode type,
35
                          TypeEnumeratorProperties* tep = nullptr)
36
14
      : TypeEnumeratorBase<FloatingPointEnumerator>(type),
37
14
        d_e(type.getFloatingPointExponentSize()),
38
14
        d_s(type.getFloatingPointSignificandSize()),
39
14
        d_state(d_e + d_s, 0U),
40
56
        d_enumerationComplete(false) {}
41
42
  /** Throws NoMoreValuesException if the enumeration is complete. */
43
28
  Node operator*() override {
44
28
    if (d_enumerationComplete) {
45
      throw NoMoreValuesException(getType());
46
    }
47
28
    return NodeManager::currentNM()->mkConst(createFP());
48
  }
49
50
  FloatingPointEnumerator& operator++() override {
51
    const FloatingPoint current(createFP());
52
    if (current.isNaN()) {
53
      d_enumerationComplete = true;
54
    } else {
55
      d_state = d_state + BitVector(d_state.getSize(), 1U);
56
    }
57
    return *this;
58
  }
59
60
28
  bool isFinished() override { return d_enumerationComplete; }
61
62
 protected:
63
28
  FloatingPoint createFP(void) const {
64
    // Rotate the LSB into the sign so that NaN is the last value
65
28
    uint64_t vone = 1;
66
28
    uint64_t vmax = d_state.getSize() - 1;
67
    BitVector bva =
68
56
        d_state.logicalRightShift(BitVector(d_state.getSize(), vone));
69
56
    BitVector bvb = d_state.leftShift(BitVector(d_state.getSize(), vmax));
70
56
    const BitVector value = (bva | bvb);
71
72
56
    return FloatingPoint(d_e, d_s, value);
73
  }
74
75
 private:
76
  const unsigned d_e;
77
  const unsigned d_s;
78
  BitVector d_state;
79
  bool d_enumerationComplete;
80
}; /* FloatingPointEnumerator */
81
82
2
class RoundingModeEnumerator
83
    : public TypeEnumeratorBase<RoundingModeEnumerator> {
84
 public:
85
1
  RoundingModeEnumerator(TypeNode type, TypeEnumeratorProperties* tep = nullptr)
86
1
      : TypeEnumeratorBase<RoundingModeEnumerator>(type),
87
        d_rm(ROUND_NEAREST_TIES_TO_EVEN),
88
1
        d_enumerationComplete(false)
89
  {
90
1
  }
91
92
  /** Throws NoMoreValuesException if the enumeration is complete. */
93
2
  Node operator*() override {
94
2
    if (d_enumerationComplete) {
95
      throw NoMoreValuesException(getType());
96
    }
97
2
    return NodeManager::currentNM()->mkConst(d_rm);
98
  }
99
100
  RoundingModeEnumerator& operator++() override {
101
    switch (d_rm) {
102
      case ROUND_NEAREST_TIES_TO_EVEN: d_rm = ROUND_TOWARD_POSITIVE; break;
103
      case ROUND_TOWARD_POSITIVE: d_rm = ROUND_TOWARD_NEGATIVE; break;
104
      case ROUND_TOWARD_NEGATIVE: d_rm = ROUND_TOWARD_ZERO; break;
105
      case ROUND_TOWARD_ZERO: d_rm = ROUND_NEAREST_TIES_TO_AWAY; break;
106
      case ROUND_NEAREST_TIES_TO_AWAY: d_enumerationComplete = true; break;
107
      default: Unreachable() << "Unknown rounding mode?"; break;
108
    }
109
    return *this;
110
  }
111
112
2
  bool isFinished() override { return d_enumerationComplete; }
113
114
 private:
115
  RoundingMode d_rm;
116
  bool d_enumerationComplete;
117
}; /* RoundingModeEnumerator */
118
119
}  // namespace fp
120
}  // namespace theory
121
}  // namespace cvc5
122
123
#endif /* CVC5__THEORY__FP__TYPE_ENUMERATOR_H */