-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAbstractFloatingPointFormulaManager.java
514 lines (407 loc) · 17.8 KB
/
AbstractFloatingPointFormulaManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2020 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.basicimpl;
import static org.sosy_lab.java_smt.basicimpl.FormulaCreator.escapeName;
import com.google.common.base.Preconditions;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import org.sosy_lab.common.rationals.Rational;
import org.sosy_lab.java_smt.api.BitvectorFormula;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.FloatingPointFormula;
import org.sosy_lab.java_smt.api.FloatingPointFormulaManager;
import org.sosy_lab.java_smt.api.FloatingPointRoundingMode;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.api.FormulaType;
import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType;
/**
* Similar to the other Abstract*FormulaManager classes in this package, this class serves as a
* helper for implementing {@link FloatingPointFormulaManager}. It handles all the unwrapping and
* wrapping from and to the {@link Formula} instances, such that the concrete class needs to handle
* only its own internal types.
*
* <p>For {@link #multiply(FloatingPointFormula, FloatingPointFormula)}, and {@link
* #divide(FloatingPointFormula, FloatingPointFormula)} this class even offers an implementation
* based on UFs. Sub-classes are supposed to override them if they can implement these operations
* more precisely (for example multiplication with constants should be supported by all solvers and
* implemented by all sub-classes).
*/
@SuppressWarnings("ClassTypeParameterName")
public abstract class AbstractFloatingPointFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl>
extends AbstractBaseFormulaManager<TFormulaInfo, TType, TEnv, TFuncDecl>
implements FloatingPointFormulaManager {
private final Map<FloatingPointRoundingMode, TFormulaInfo> roundingModes;
protected AbstractFloatingPointFormulaManager(
FormulaCreator<TFormulaInfo, TType, TEnv, TFuncDecl> pCreator) {
super(pCreator);
roundingModes = new HashMap<>();
}
protected abstract TFormulaInfo getDefaultRoundingMode();
protected abstract TFormulaInfo getRoundingModeImpl(
FloatingPointRoundingMode pFloatingPointRoundingMode);
private TFormulaInfo getRoundingMode(FloatingPointRoundingMode pFloatingPointRoundingMode) {
return roundingModes.computeIfAbsent(pFloatingPointRoundingMode, this::getRoundingModeImpl);
}
protected FloatingPointFormula wrap(TFormulaInfo pTerm) {
return getFormulaCreator().encapsulateFloatingPoint(pTerm);
}
@Override
public FloatingPointFormula makeNumber(Rational n, FormulaType.FloatingPointType type) {
return wrap(makeNumberImpl(n.toString(), type, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula makeNumber(
Rational n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(makeNumberImpl(n.toString(), type, getRoundingMode(pFloatingPointRoundingMode)));
}
@Override
public FloatingPointFormula makeNumber(double n, FormulaType.FloatingPointType type) {
return wrap(makeNumberImpl(n, type, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula makeNumber(
double n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(makeNumberImpl(n, type, getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo makeNumberImpl(
double n, FormulaType.FloatingPointType type, TFormulaInfo pFloatingPointRoundingMode);
@Override
public FloatingPointFormula makeNumber(BigDecimal n, FormulaType.FloatingPointType type) {
return wrap(makeNumberImpl(n, type, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula makeNumber(
BigDecimal n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(makeNumberImpl(n, type, getRoundingMode(pFloatingPointRoundingMode)));
}
protected TFormulaInfo makeNumberImpl(
BigDecimal n, FormulaType.FloatingPointType type, TFormulaInfo pFloatingPointRoundingMode) {
return makeNumberImpl(n.toPlainString(), type, pFloatingPointRoundingMode);
}
@Override
public FloatingPointFormula makeNumber(String n, FormulaType.FloatingPointType type) {
return wrap(makeNumberImpl(n, type, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula makeNumber(
String n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(makeNumberImpl(n, type, getRoundingMode(pFloatingPointRoundingMode)));
}
/** directly catch the most common special String constants. */
protected TFormulaInfo makeNumberImpl(
String n, FormulaType.FloatingPointType type, TFormulaInfo pFloatingPointRoundingMode) {
if (n.startsWith("+")) {
n = n.substring(1);
}
switch (n) {
case "NaN":
case "-NaN":
return makeNaNImpl(type);
case "Infinity":
return makePlusInfinityImpl(type);
case "-Infinity":
return makeMinusInfinityImpl(type);
default:
return makeNumberAndRound(n, type, pFloatingPointRoundingMode);
}
}
@Override
public FloatingPointFormula makeNumber(
BigInteger exponent, BigInteger mantissa, boolean signBit, FloatingPointType type) {
return wrap(makeNumberImpl(exponent, mantissa, signBit, type));
}
protected abstract TFormulaInfo makeNumberImpl(
BigInteger exponent, BigInteger mantissa, boolean signBit, FloatingPointType type);
protected static boolean isNegativeZero(Double pN) {
Preconditions.checkNotNull(pN);
return Double.valueOf("-0.0").equals(pN);
}
protected abstract TFormulaInfo makeNumberAndRound(
String pN, FloatingPointType pType, TFormulaInfo pFloatingPointRoundingMode);
@Override
public FloatingPointFormula makeVariable(String pVar, FormulaType.FloatingPointType pType) {
return wrap(makeVariableImpl(escapeName(pVar), pType));
}
protected abstract TFormulaInfo makeVariableImpl(
String pVar, FormulaType.FloatingPointType pType);
@Override
public FloatingPointFormula makePlusInfinity(FormulaType.FloatingPointType pType) {
return wrap(makePlusInfinityImpl(pType));
}
protected abstract TFormulaInfo makePlusInfinityImpl(FormulaType.FloatingPointType pType);
@Override
public FloatingPointFormula makeMinusInfinity(FormulaType.FloatingPointType pType) {
return wrap(makeMinusInfinityImpl(pType));
}
protected abstract TFormulaInfo makeMinusInfinityImpl(FormulaType.FloatingPointType pType);
@Override
public FloatingPointFormula makeNaN(FormulaType.FloatingPointType pType) {
return wrap(makeNaNImpl(pType));
}
protected abstract TFormulaInfo makeNaNImpl(FormulaType.FloatingPointType pType);
@Override
public <T extends Formula> T castTo(
FloatingPointFormula pNumber, boolean pSigned, FormulaType<T> pTargetType) {
return getFormulaCreator()
.encapsulate(
pTargetType,
castToImpl(extractInfo(pNumber), pSigned, pTargetType, getDefaultRoundingMode()));
}
@Override
public <T extends Formula> T castTo(
FloatingPointFormula number,
boolean pSigned,
FormulaType<T> targetType,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
return getFormulaCreator()
.encapsulate(
targetType,
castToImpl(
extractInfo(number),
pSigned,
targetType,
getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo castToImpl(
TFormulaInfo pNumber,
boolean pSigned,
FormulaType<?> pTargetType,
TFormulaInfo pRoundingMode);
@Override
public FloatingPointFormula castFrom(
Formula pNumber, boolean pSigned, FormulaType.FloatingPointType pTargetType) {
return wrap(castFromImpl(extractInfo(pNumber), pSigned, pTargetType, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula castFrom(
Formula number,
boolean signed,
FloatingPointType targetType,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(
castFromImpl(
extractInfo(number), signed, targetType, getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo castFromImpl(
TFormulaInfo pNumber,
boolean pSigned,
FormulaType.FloatingPointType pTargetType,
TFormulaInfo pRoundingMode);
@Override
public FloatingPointFormula fromIeeeBitvector(
BitvectorFormula pNumber, FloatingPointType pTargetType) {
return wrap(fromIeeeBitvectorImpl(extractInfo(pNumber), pTargetType));
}
protected abstract TFormulaInfo fromIeeeBitvectorImpl(
TFormulaInfo pNumber, FloatingPointType pTargetType);
@Override
public BitvectorFormula toIeeeBitvector(FloatingPointFormula pNumber) {
return getFormulaCreator().encapsulateBitvector(toIeeeBitvectorImpl(extractInfo(pNumber)));
}
protected abstract TFormulaInfo toIeeeBitvectorImpl(TFormulaInfo pNumber);
@Override
public FloatingPointFormula negate(FloatingPointFormula pNumber) {
TFormulaInfo param1 = extractInfo(pNumber);
return wrap(negate(param1));
}
protected abstract TFormulaInfo negate(TFormulaInfo pParam1);
@Override
public FloatingPointFormula abs(FloatingPointFormula pNumber) {
TFormulaInfo param1 = extractInfo(pNumber);
return wrap(abs(param1));
}
protected abstract TFormulaInfo abs(TFormulaInfo pParam1);
@Override
public FloatingPointFormula max(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
return wrap(max(extractInfo(pNumber1), extractInfo(pNumber2)));
}
protected abstract TFormulaInfo max(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public FloatingPointFormula min(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
return wrap(min(extractInfo(pNumber1), extractInfo(pNumber2)));
}
protected abstract TFormulaInfo min(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public FloatingPointFormula sqrt(FloatingPointFormula pNumber) {
return wrap(sqrt(extractInfo(pNumber), getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula sqrt(
FloatingPointFormula number, FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(sqrt(extractInfo(number), getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo sqrt(TFormulaInfo pNumber, TFormulaInfo pRoundingMode);
@Override
public FloatingPointFormula add(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrap(add(param1, param2, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula add(
FloatingPointFormula number1,
FloatingPointFormula number2,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
return wrap(
add(
extractInfo(number1),
extractInfo(number2),
getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo add(
TFormulaInfo pParam1, TFormulaInfo pParam2, TFormulaInfo pRoundingMode);
@Override
public FloatingPointFormula subtract(
FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrap(subtract(param1, param2, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula subtract(
FloatingPointFormula number1,
FloatingPointFormula number2,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
TFormulaInfo param1 = extractInfo(number1);
TFormulaInfo param2 = extractInfo(number2);
return wrap(subtract(param1, param2, getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo subtract(
TFormulaInfo pParam1, TFormulaInfo pParam2, TFormulaInfo pFloatingPointRoundingMode);
@Override
public FloatingPointFormula divide(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrap(divide(param1, param2, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula divide(
FloatingPointFormula number1,
FloatingPointFormula number2,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
TFormulaInfo param1 = extractInfo(number1);
TFormulaInfo param2 = extractInfo(number2);
return wrap(divide(param1, param2, getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo divide(
TFormulaInfo pParam1, TFormulaInfo pParam2, TFormulaInfo pFloatingPointRoundingMode);
@Override
public FloatingPointFormula multiply(
FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrap(multiply(param1, param2, getDefaultRoundingMode()));
}
@Override
public FloatingPointFormula multiply(
FloatingPointFormula number1,
FloatingPointFormula number2,
FloatingPointRoundingMode pFloatingPointRoundingMode) {
TFormulaInfo param1 = extractInfo(number1);
TFormulaInfo param2 = extractInfo(number2);
return wrap(multiply(param1, param2, getRoundingMode(pFloatingPointRoundingMode)));
}
protected abstract TFormulaInfo multiply(
TFormulaInfo pParam1, TFormulaInfo pParam2, TFormulaInfo pFloatingPointRoundingMode);
@Override
public FloatingPointFormula remainder(
FloatingPointFormula number1, FloatingPointFormula number2) {
return wrap(remainder(extractInfo(number1), extractInfo(number2)));
}
protected abstract TFormulaInfo remainder(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula assignment(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(assignment(param1, param2));
}
protected abstract TFormulaInfo assignment(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula equalWithFPSemantics(
FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(equalWithFPSemantics(param1, param2));
}
protected abstract TFormulaInfo equalWithFPSemantics(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula greaterThan(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(greaterThan(param1, param2));
}
protected abstract TFormulaInfo greaterThan(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula greaterOrEquals(
FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(greaterOrEquals(param1, param2));
}
protected abstract TFormulaInfo greaterOrEquals(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula lessThan(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(lessThan(param1, param2));
}
protected abstract TFormulaInfo lessThan(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula lessOrEquals(FloatingPointFormula pNumber1, FloatingPointFormula pNumber2) {
TFormulaInfo param1 = extractInfo(pNumber1);
TFormulaInfo param2 = extractInfo(pNumber2);
return wrapBool(lessOrEquals(param1, param2));
}
protected abstract TFormulaInfo lessOrEquals(TFormulaInfo pParam1, TFormulaInfo pParam2);
@Override
public BooleanFormula isNaN(FloatingPointFormula pNumber) {
return wrapBool(isNaN(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isNaN(TFormulaInfo pParam);
@Override
public BooleanFormula isInfinity(FloatingPointFormula pNumber) {
return wrapBool(isInfinity(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isInfinity(TFormulaInfo pParam);
@Override
public BooleanFormula isZero(FloatingPointFormula pNumber) {
return wrapBool(isZero(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isZero(TFormulaInfo pParam);
@Override
public BooleanFormula isSubnormal(FloatingPointFormula pNumber) {
return wrapBool(isSubnormal(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isSubnormal(TFormulaInfo pParam);
@Override
public BooleanFormula isNormal(FloatingPointFormula pNumber) {
return wrapBool(isNormal(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isNormal(TFormulaInfo pParam);
@Override
public BooleanFormula isNegative(FloatingPointFormula pNumber) {
return wrapBool(isNegative(extractInfo(pNumber)));
}
protected abstract TFormulaInfo isNegative(TFormulaInfo pParam);
@Override
public FloatingPointFormula round(
FloatingPointFormula pFormula, FloatingPointRoundingMode pRoundingMode) {
return wrap(round(extractInfo(pFormula), pRoundingMode));
}
protected abstract TFormulaInfo round(
TFormulaInfo pFormula, FloatingPointRoundingMode pRoundingMode);
protected static String getBvRepresentation(BigInteger integer, int size) {
char[] values = new char[size];
for (int i = 0; i < size; i++) {
values[size - 1 - i] = integer.testBit(i) ? '1' : '0';
}
return String.copyValueOf(values);
}
}