GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/floatingpoint_literal_symfpu.h Lines: 9 9 100.0 %
Date: 2021-09-15 Branches: 2 4 50.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Andres Noetzli
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
 * SymFPU glue code for floating-point values.
14
 *
15
 * !!! This header is not to be included in any other headers !!!
16
 */
17
#include "cvc5_public.h"
18
19
#ifndef CVC5__UTIL__FLOATINGPOINT_LITERAL_SYMFPU_H
20
#define CVC5__UTIL__FLOATINGPOINT_LITERAL_SYMFPU_H
21
22
#include "util/bitvector.h"
23
#include "util/floatingpoint_literal_symfpu_traits.h"
24
#include "util/floatingpoint_size.h"
25
#include "util/roundingmode.h"
26
27
/* -------------------------------------------------------------------------- */
28
29
namespace cvc5 {
30
31
using SymFPUUnpackedFloatLiteral =
32
    ::symfpu::unpackedFloat<symfpuLiteral::traits>;
33
34
2817
class FloatingPointLiteral
35
{
36
  friend class FloatingPoint;
37
38
 public:
39
  /**
40
   * The kind of floating-point special constants that can be created via the
41
   * corresponding constructor.
42
   * These are prefixed with FP because of name clashes with NAN.
43
   */
44
  enum SpecialConstKind
45
  {
46
    FPINF,   // +-oo
47
    FPNAN,   // NaN
48
    FPZERO,  // +-zero
49
  };
50
51
  /**
52
   * Get the number of exponent bits in the unpacked format corresponding to a
53
   * given packed format.  This is the unpacked counter-part of
54
   * FloatingPointSize::exponentWidth().
55
   */
56
  static uint32_t getUnpackedExponentWidth(FloatingPointSize& size);
57
  /**
58
   * Get the number of exponent bits in the unpacked format corresponding to a
59
   * given packed format.  This is the unpacked counter-part of
60
   * FloatingPointSize::significandWidth().
61
   */
62
  static uint32_t getUnpackedSignificandWidth(FloatingPointSize& size);
63
64
  /** Constructors. */
65
66
  /** Create a FP literal from its IEEE bit-vector representation. */
67
  FloatingPointLiteral(uint32_t exp_size,
68
                       uint32_t sig_size,
69
                       const BitVector& bv);
70
  FloatingPointLiteral(const FloatingPointSize& size, const BitVector& bv);
71
72
  /** Create a FP literal that represents a special constants. */
73
  FloatingPointLiteral(const FloatingPointSize& size, SpecialConstKind kind);
74
  FloatingPointLiteral(const FloatingPointSize& size,
75
                       SpecialConstKind kind,
76
                       bool sign);
77
78
  /**
79
   * Create a FP literal from a signed or unsigned bit-vector value with
80
   * respect to given rounding mode.
81
   */
82
  FloatingPointLiteral(const FloatingPointSize& size,
83
                       const RoundingMode& rm,
84
                       const BitVector& bv,
85
                       bool signedBV);
86
87
4894
  ~FloatingPointLiteral() {}
88
89
  /** Return the size of this FP literal. */
90
6430
  const FloatingPointSize& getSize() const { return d_fp_size; };
91
92
  /** Return the packed (IEEE-754) representation of this literal. */
93
  BitVector pack(void) const;
94
95
  /** Floating-point absolute value literal. */
96
  FloatingPointLiteral absolute(void) const;
97
  /** Floating-point negation literal. */
98
  FloatingPointLiteral negate(void) const;
99
  /** Floating-point addition literal. */
100
  FloatingPointLiteral add(const RoundingMode& rm,
101
                           const FloatingPointLiteral& arg) const;
102
  /** Floating-point subtraction literal. */
103
  FloatingPointLiteral sub(const RoundingMode& rm,
104
                           const FloatingPointLiteral& arg) const;
105
  /** Floating-point multiplication literal. */
106
  FloatingPointLiteral mult(const RoundingMode& rm,
107
                            const FloatingPointLiteral& arg) const;
108
  /** Floating-point division literal. */
109
  FloatingPointLiteral div(const RoundingMode& rm,
110
                           const FloatingPointLiteral& arg) const;
111
  /** Floating-point fused multiplication and addition literal. */
112
  FloatingPointLiteral fma(const RoundingMode& rm,
113
                           const FloatingPointLiteral& arg1,
114
                           const FloatingPointLiteral& arg2) const;
115
  /** Floating-point square root literal. */
116
  FloatingPointLiteral sqrt(const RoundingMode& rm) const;
117
  /** Floating-point round to integral literal. */
118
  FloatingPointLiteral rti(const RoundingMode& rm) const;
119
  /** Floating-point remainder literal. */
120
  FloatingPointLiteral rem(const FloatingPointLiteral& arg) const;
121
122
  /**
123
   * Floating-point max literal (total version).
124
   * zeroCase: true to return the left (rather than the right operand) in case
125
   *           of max(-0,+0) or max(+0,-0).
126
   */
127
  FloatingPointLiteral maxTotal(const FloatingPointLiteral& arg,
128
                                bool zeroCaseLeft) const;
129
  /**
130
   * Floating-point min literal (total version).
131
   * zeroCase: true to return the left (rather than the right operand) in case
132
   *           of min(-0,+0) or min(+0,-0).
133
   */
134
  FloatingPointLiteral minTotal(const FloatingPointLiteral& arg,
135
                                bool zeroCaseLeft) const;
136
137
  /** Equality literal (NOT: fp.eq but =) over floating-point values. */
138
  bool operator==(const FloatingPointLiteral& fp) const;
139
  /** Floating-point less or equal than literal. */
140
  bool operator<=(const FloatingPointLiteral& arg) const;
141
  /** Floating-point less than literal. */
142
  bool operator<(const FloatingPointLiteral& arg) const;
143
144
  /** Get the exponent of this floating-point value. */
145
  BitVector getExponent() const;
146
  /** Get the significand of this floating-point value. */
147
  BitVector getSignificand() const;
148
  /** True if this value is a negative value. */
149
  bool getSign() const;
150
151
  /** Return true if this literal represents a normal value. */
152
  bool isNormal(void) const;
153
  /** Return true if this  literal represents a subnormal value. */
154
  bool isSubnormal(void) const;
155
  /** Return true if this literal represents a zero value. */
156
  bool isZero(void) const;
157
  /** Return true if this literal represents an infinite value. */
158
  bool isInfinite(void) const;
159
  /** Return true if this literal represents a NaN value. */
160
  bool isNaN(void) const;
161
  /** Return true if this literal represents a negative value. */
162
  bool isNegative(void) const;
163
  /** Return true if this literal represents a positive value. */
164
  bool isPositive(void) const;
165
166
  /**
167
   * Convert this floating-point literal to a literal of given size, with
168
   * respect to given rounding mode.
169
   */
170
  FloatingPointLiteral convert(const FloatingPointSize& target,
171
                               const RoundingMode& rm) const;
172
173
  /**
174
   * Convert this floating-point literal to a signed bit-vector of given size,
175
   * with respect to given rounding mode (total version).
176
   * Returns given bit-vector 'undefinedCase' in the undefined case.
177
   */
178
  BitVector convertToSBVTotal(BitVectorSize width,
179
                              const RoundingMode& rm,
180
                              BitVector undefinedCase) const;
181
  /**
182
   * Convert this floating-point literal to an unsigned bit-vector of given
183
   * size, with respect to given rounding mode (total version).
184
   * Returns given bit-vector 'undefinedCase' in the undefined case.
185
   */
186
  BitVector convertToUBVTotal(BitVectorSize width,
187
                              const RoundingMode& rm,
188
                              BitVector undefinedCase) const;
189
  /** Return wrapped floating-point literal. */
190
259
  const SymFPUUnpackedFloatLiteral& getSymUF() const { return d_symuf; }
191
192
 private:
193
  /**
194
   * Create a FP literal from unpacked representation.
195
   *
196
   * This unpacked representation accounts for additional bits required for the
197
   * exponent to allow subnormals to be normalized.
198
   *
199
   * This should NOT be used to create a literal from its IEEE bit-vector
200
   * representation -- for this, the above constructor is to be used while
201
   * creating a SymFPUUnpackedFloatLiteral via symfpu::unpack.
202
   */
203
28
  FloatingPointLiteral(const FloatingPointSize& size,
204
                       const bool sign,
205
                       const BitVector& exp,
206
                       const BitVector& sig)
207
28
      : d_fp_size(size), d_symuf(SymFPUUnpackedFloatLiteral(sign, exp, sig))
208
  {
209
28
  }
210
211
  /** Create a FP literal from a symFPU unpacked float. */
212
1509
  FloatingPointLiteral(const FloatingPointSize& size,
213
                       SymFPUUnpackedFloatLiteral symuf)
214
1509
      : d_fp_size(size), d_symuf(symuf){};
215
216
  /** The floating-point size of this floating-point literal. */
217
  FloatingPointSize d_fp_size;
218
  /** The actual floating-point value, a SymFPU unpackedFloat. */
219
  SymFPUUnpackedFloatLiteral d_symuf;
220
};
221
222
/* -------------------------------------------------------------------------- */
223
224
}  // namespace cvc5
225
226
#endif