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