-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRangeAnalysis.qll
More file actions
697 lines (674 loc) · 22.8 KB
/
Copy pathRangeAnalysis.qll
File metadata and controls
697 lines (674 loc) · 22.8 KB
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import go
Expr getAUse(SsaDefinition def) {
result = def.getVariable().getAUse().(IR::EvalInstruction).getExpr()
}
/**
* Gets the upper bound of expression `expr`.
*/
float getUpperBound(Expr expr) { result = max(getAnUpperBound(expr)) }
/**
* Gets the lower bound of expression `expr`.
*/
float getLowerBound(Expr expr) { result = min(getALowerBound(expr)) }
/**
* Gets the upper bound of SSA definition `def`.
*/
float getSsaUpperBound(SsaDefinition def) { result = max(getAnSsaUpperBound(def)) }
/**
* Gets the lower bound of SSA definition `def`.
*/
float getSsaLowerBound(SsaDefinition def) { result = min(getAnSsaLowerBound(def)) }
/**
* Gets a possible upper bound of expression `expr`.
*/
float getAnUpperBound(Expr expr) {
if expr.isConst()
then result = expr.getNumericValue()
else (
//if this expression is an identifier
exists(SsaVariable v, Ident identifier |
identifier = expr and
identifier.refersTo(v.getSourceVariable()) and
v.getAUse() = IR::evalExprInstruction(identifier) and
(
if
//if a condition expression exists before and one of the operand happens to be the identifier, we use this condition expression to narrow down the range.
exists(
ControlFlow::ConditionGuardNode n, DataFlow::Node lesser, DataFlow::Node greater,
ReachableBasicBlock bb
|
n.ensuresLeq(lesser, greater, _) and
IR::evalExprInstruction(lesser.asExpr()) = v.getAUse() and
n.dominates(bb) and
bb.getANode() = IR::evalExprInstruction(identifier) and
not exists(Expr e |
e = v.getAUse().(IR::EvalInstruction).getExpr() and
e.getParent*() = greater.asExpr()
)
)
then
exists(
ControlFlow::ConditionGuardNode n, ReachableBasicBlock bb, DataFlow::Node lesser,
DataFlow::Node greater, int bias
|
n.dominates(bb) and
bb.getANode() = IR::evalExprInstruction(identifier) and
n.ensuresLeq(lesser, greater, bias) and
v.getAUse() = IR::evalExprInstruction(lesser.asExpr()) and
not exists(Expr e |
e = v.getAUse().(IR::EvalInstruction).getExpr() and
e.getParent*() = greater.asExpr()
) and
result = getAnUpperBound(greater.asExpr()) + bias
)
else
//If not, find the corresponding `SsaDefinition`, then call `getAnSsaUpperBound` on it.
result = getAnSsaUpperBound(v.getDefinition())
)
)
or
//if this expression is an add expression
exists(AddExpr add, float lhsUB, float rhsUB |
add = expr and
lhsUB = getAnUpperBound(add.getLeftOperand()) and
rhsUB = getAnUpperBound(add.getRightOperand()) and
result = addRoundingUp(lhsUB, rhsUB)
)
or
//if this expression is an sub expression
exists(SubExpr sub, float lhsUB, float rhsLB |
sub = expr and
lhsUB = getAnUpperBound(sub.getLeftOperand()) and
rhsLB = getALowerBound(sub.getRightOperand()) and
result = addRoundingUp(lhsUB, -rhsLB)
)
or
//if this expression is an remainder operation
exists(RemExpr rem | rem = expr |
result = 0
or
exists(float lhsUB, float rhsLB, float rhsUB |
lhsUB = getAnUpperBound(rem.getLeftOperand()) and
lhsUB > 0 and
rhsLB = getALowerBound(rem.getRightOperand()) and
rhsUB = getAnUpperBound(rem.getRightOperand())
|
result = rhsLB.abs() or
result = rhsUB.abs()
)
)
or
//if this expression is an unary plus expression
exists(PlusExpr plus |
plus = expr and
result = getAnUpperBound(plus.getOperand())
)
or
//if this expression is an unary minus expression
exists(MinusExpr minus |
minus = expr and
result = -getALowerBound(minus.getOperand())
)
or
//if this expression is an multiply expression and one of the operator is a constant integer.
exists(MulExpr mul |
mul = expr and
exists(mul.getAnOperand().getIntValue()) and
if exists(mul.getLeftOperand().getIntValue())
then
exists(float lhs |
lhs = mul.getLeftOperand().getIntValue() and
(
result = lhs * getAnUpperBound(mul.getRightOperand()) or
result = lhs * getALowerBound(mul.getRightOperand())
)
)
else
exists(float rhs |
rhs = mul.getRightOperand().getIntValue() and
(
result = rhs * getAnUpperBound(mul.getLeftOperand()) or
result = rhs * getALowerBound(mul.getLeftOperand())
)
)
)
or
//if this expression is a selector expression, simply returns maximum value which the expression can represent.
exists(SelectorExpr sel, Entity e |
sel = expr and
sel.getSelector().refersTo(e) and
result = typeMaxValue(e.getType())
)
or
//if this expression is a conversion
exists(ConversionExpr conv |
conv = expr and
result = typeMaxValue(conv.getType())
)
or
//if this expression is an bitwise-and expression and one of the operator is an constant integer.
exists(AndExpr bitAnd, int i, int j |
bitAnd = expr and
i = bitAnd.getAnOperand().getIntValue() and
j in [0 .. 31] and
i = 2.pow(j) - 1 and
result = i
)
or
//all other kind of expressions which we cannot determine the range
(
expr instanceof IndexExpr or
expr instanceof CallExpr or
expr instanceof StarExpr or
expr instanceof DerefExpr
) and
result = typeMaxValue(expr.getType())
)
}
/**
* Gets a possible lower bound of expression `expr`.
*/
float getALowerBound(Expr expr) {
if expr.isConst()
then
result = expr.getFloatValue() or
result = expr.getIntValue() or
result = expr.getExactValue().toFloat()
else (
//if this expression is an identifer
exists(SsaVariable v, Ident identifier |
identifier = expr and
identifier.refersTo(v.getSourceVariable()) and
v.getAUse() = IR::evalExprInstruction(identifier) and
(
//if exists a condition expression before this identifier
if
exists(
ControlFlow::ConditionGuardNode n, DataFlow::Node greater, DataFlow::Node lesser,
ReachableBasicBlock bb
|
n.ensuresLeq(lesser, greater, _) and
IR::evalExprInstruction(greater.asExpr()) = v.getAUse() and
n.dominates(bb) and
bb.getANode() = IR::evalExprInstruction(identifier) and
not exists(Expr e |
e = v.getAUse().(IR::EvalInstruction).getExpr() and
e.getParent*() = lesser.asExpr()
)
)
then
exists(
ControlFlow::ConditionGuardNode n, ReachableBasicBlock bb, DataFlow::Node lesser,
DataFlow::Node greater, int bias, float lbs
|
n.dominates(bb) and
bb.getANode() = IR::evalExprInstruction(identifier) and
n.ensuresLeq(lesser, greater, bias) and
v.getAUse() = IR::evalExprInstruction(greater.asExpr()) and
not exists(Expr e |
e = v.getAUse().(IR::EvalInstruction).getExpr() and
e.getParent*() = lesser.asExpr()
) and
lbs = getALowerBound(lesser.asExpr()) and
result = lbs - bias
)
else
//find corresponding SSA definition and calls `getAnSsaLowerBound` on it.
result = getAnSsaLowerBound(v.getDefinition())
)
)
or
//add expr
exists(AddExpr add, float lhsLB, float rhsLB |
add = expr and
lhsLB = getALowerBound(add.getLeftOperand()) and
rhsLB = getALowerBound(add.getRightOperand()) and
result = addRoundingDown(lhsLB, rhsLB)
)
or
//sub expr
exists(SubExpr sub, float lhsLB, float rhsUB |
sub = expr and
lhsLB = getALowerBound(sub.getLeftOperand()) and
rhsUB = getAnUpperBound(sub.getRightOperand()) and
result = addRoundingDown(lhsLB, -rhsUB)
)
or
//remainder expr
exists(RemExpr rem | rem = expr |
result = 0
or
exists(float lhsLB, float rhsLB, float rhsUB |
lhsLB = getALowerBound(rem.getLeftOperand()) and
lhsLB < 0 and
rhsLB = getALowerBound(rem.getRightOperand()) and
rhsUB = getAnUpperBound(rem.getRightOperand()) and
(
result = -rhsLB.abs() or
result = -rhsUB.abs()
)
)
)
or
//unary plus expr
exists(PlusExpr plus |
plus = expr and
result = getALowerBound(plus.getOperand())
)
or
//unary minus expr
exists(MinusExpr minus |
minus = expr and
result = -getAnUpperBound(minus.getOperand())
)
or
//multiply expression and one of the operator is an constant integer
exists(MulExpr mul |
mul = expr and
exists(mul.getAnOperand().getIntValue()) and
if exists(mul.getLeftOperand().getIntValue())
then
exists(float lhs |
lhs = mul.getLeftOperand().getIntValue() and
(
result = lhs * getAnUpperBound(mul.getRightOperand()) or
result = lhs * getALowerBound(mul.getRightOperand())
)
)
else
exists(float rhs |
rhs = mul.getRightOperand().getIntValue() and
(
result = rhs * getAnUpperBound(mul.getLeftOperand()) or
result = rhs * getALowerBound(mul.getLeftOperand())
)
)
)
or
//selector expression
exists(SelectorExpr sel, Entity e |
sel = expr and
sel.getSelector().refersTo(e) and
result = typeMinValue(e.getType())
)
or
//conversion expr
exists(ConversionExpr conv |
conv = expr and
result = typeMinValue(conv.getType())
)
or
//call expression when the function is builtin function `len`
exists(CallExpr call |
call = expr and
if call.getTarget() = Builtin::len()
then result = 0
else result = typeMinValue(call.getType())
)
or
//bitwise-and expression and one of the operator is a constant integer
exists(AndExpr bitAnd, int i, int j |
bitAnd = expr and
i = bitAnd.getAnOperand().getIntValue() and
j in [0 .. 31] and
i = 2.pow(j) - 1 and
result = 0
)
or
//other kind of expression which we cannot determine the range
(
expr instanceof IndexExpr or
expr instanceof StarExpr or
expr instanceof DerefExpr
) and
result = typeMinValue(expr.getType())
)
}
/**
* Gets a possible upper bound of SSA definition `def`.
*/
float getAnSsaUpperBound(SsaDefinition def) {
not recursiveSelfDef(def) and
(
def instanceof SsaExplicitDefinition and
exists(SsaExplicitDefinition explicitDef | explicitDef = def |
//SSA definition corresponding to a `SimpleAssignStmt`
if explicitDef.getInstruction() instanceof IR::AssignInstruction
then
exists(IR::AssignInstruction assignInstr, SimpleAssignStmt simpleAssign |
assignInstr = explicitDef.getInstruction() and
assignInstr.getRhs().(IR::EvalInstruction).getExpr() = simpleAssign.getRhs() and
result = getAnUpperBound(simpleAssign.getRhs())
)
or
//SSA definition corresponding to a ValueSpec(used in a variable declaration)
exists(IR::AssignInstruction declInstr, ValueSpec vs, int i, Expr init |
declInstr = explicitDef.getInstruction() and
declInstr = IR::initInstruction(vs, i) and
init = vs.getInit(i) and
result = getAnUpperBound(init)
)
or
//SSA definition corresponding to an `AddAssignStmt` (x += y) or `SubAssignStmt` (x -= y)
exists(
IR::AssignInstruction assignInstr, SsaExplicitDefinition prevDef,
CompoundAssignStmt compoundAssign, float prevBound, float delta
|
assignInstr = explicitDef.getInstruction() and
getAUse(prevDef) = compoundAssign.getLhs() and
assignInstr = IR::assignInstruction(compoundAssign, 0) and
prevBound = getAnSsaUpperBound(prevDef) and
(
compoundAssign instanceof AddAssignStmt and
delta = getAnUpperBound(compoundAssign.getRhs()) and
result = addRoundingUp(prevBound, delta)
or
compoundAssign instanceof SubAssignStmt and
delta = getALowerBound(compoundAssign.getRhs()) and
result = addRoundingUp(prevBound, -delta)
)
)
else
//SSA definition corresponding to an `IncDecStmt`
if explicitDef.getInstruction() instanceof IR::IncDecInstruction
then
exists(IncDecStmt incOrDec, IR::IncDecInstruction instr, float exprLB |
instr = explicitDef.getInstruction() and
exprLB = getAnUpperBound(incOrDec.getOperand()) and
instr.getRhs().(IR::EvalIncDecRhsInstruction).getStmt() = incOrDec and
(
//IncStmt(x++)
exists(IncStmt inc |
inc = incOrDec and
result = addRoundingUp(exprLB, 1)
)
or
//DecStmt(x--)
exists(DecStmt dec |
dec = incOrDec and
result = addRoundingUp(exprLB, -1)
)
)
)
else (
//SSA definition coreponding to the init of the parameter
explicitDef.getInstruction() instanceof IR::InitParameterInstruction and
exists(IR::InitParameterInstruction instr, Parameter p |
instr = explicitDef.getInstruction() and
IR::initParamInstruction(p) = instr and
result = typeMaxValue(p.getType())
)
)
)
or
//this SSA definition is a phi node.
def instanceof SsaPhiNode and
exists(SsaPhiNode phi |
phi = def and
result = getAnSsaUpperBound(phi.getAnInput().getDefinition())
)
)
}
/**
* Gets a possible lower bound of SSA definition `def`.
*/
float getAnSsaLowerBound(SsaDefinition def) {
not recursiveSelfDef(def) and
(
def instanceof SsaExplicitDefinition and
exists(SsaExplicitDefinition explicitDef | explicitDef = def |
if explicitDef.getInstruction() instanceof IR::AssignInstruction
then
//SimpleAssignStmt
exists(IR::AssignInstruction assignInstr, SimpleAssignStmt simpleAssign |
assignInstr = explicitDef.getInstruction() and
assignInstr.getRhs().(IR::EvalInstruction).getExpr() = simpleAssign.getRhs() and
result = getALowerBound(simpleAssign.getRhs())
)
or
//ValueSpec
exists(IR::AssignInstruction declInstr, ValueSpec vs, int i, Expr init |
declInstr = explicitDef.getInstruction() and
declInstr = IR::initInstruction(vs, i) and
init = vs.getInit(i) and
result = getALowerBound(init)
)
or
//AddAssignStmt(x += y)
exists(
IR::AssignInstruction assignInstr, SsaExplicitDefinition prevDef,
CompoundAssignStmt compoundAssign, float prevBound, float delta
|
assignInstr = explicitDef.getInstruction() and
getAUse(prevDef) = compoundAssign.getLhs() and
assignInstr = IR::assignInstruction(compoundAssign, 0) and
prevBound = getAnSsaLowerBound(prevDef) and
(
compoundAssign instanceof AddAssignStmt and
delta = getALowerBound(compoundAssign.getRhs()) and
result = addRoundingDown(prevBound, delta)
or
compoundAssign instanceof SubAssignStmt and
delta = getAnUpperBound(compoundAssign.getRhs()) and
result = addRoundingDown(prevBound, -delta)
)
)
else
//IncDecStmt
if explicitDef.getInstruction() instanceof IR::IncDecInstruction
then
exists(IncDecStmt incOrDec, IR::IncDecInstruction instr, float exprLB |
instr = explicitDef.getInstruction() and
exprLB = getALowerBound(incOrDec.getOperand()) and
instr.getRhs().(IR::EvalIncDecRhsInstruction).getStmt() = incOrDec and
(
//IncStmt(x++)
exists(IncStmt inc |
inc = incOrDec and
result = addRoundingDown(exprLB, 1)
)
or
//DecStmt(x--)
exists(DecStmt dec |
dec = incOrDec and
result = addRoundingDown(exprLB, -1)
)
)
)
else (
//init of the function parameter
explicitDef.getInstruction() instanceof IR::InitParameterInstruction and
exists(IR::InitParameterInstruction instr, Parameter p |
instr = explicitDef.getInstruction() and
IR::initParamInstruction(p) = instr and
result = typeMinValue(p.getType())
)
)
)
or
//phi node
def instanceof SsaPhiNode and
exists(SsaPhiNode phi |
phi = def and
result = getAnSsaLowerBound(phi.getAnInput().getDefinition())
)
)
}
/**
* Checks if SSA definition `nextDef` depends on SSA definition `prevDef` directly.
*
* The structure of this function needs to be same as `getAnSsaLowerBound`
*/
predicate ssaDependsOnSsa(SsaDefinition nextDef, SsaDefinition prevDef) {
//SSA definition corresponding to a `SimpleAssignStmt`
exists(SimpleAssignStmt simpleAssign |
nextDef.(SsaExplicitDefinition).getInstruction() = IR::assignInstruction(simpleAssign, _) and
ssaDependsOnExpr(prevDef, simpleAssign.getRhs())
)
or
//SSA definition corresponding to a `ValueSpec`(used in variable declaration)
exists(IR::AssignInstruction declInstr, ValueSpec vs, int i, Expr init |
declInstr = nextDef.(SsaExplicitDefinition).getInstruction() and
declInstr = IR::initInstruction(vs, i) and
init = vs.getInit(i) and
ssaDependsOnExpr(prevDef, init)
)
or
//SSA definition corresponding to a `AddAssignStmt` or `SubAssignStmt`
exists(CompoundAssignStmt compoundAssign |
(compoundAssign instanceof AddAssignStmt or compoundAssign instanceof SubAssignStmt) and
nextDef.(SsaExplicitDefinition).getInstruction() = IR::assignInstruction(compoundAssign, 0) and
(
getAUse(prevDef) = compoundAssign.getLhs() or
ssaDependsOnExpr(prevDef, compoundAssign.getRhs())
)
)
or
//SSA definition corresponding to a `IncDecStmt`
exists(IncDecStmt incDec |
nextDef
.(SsaExplicitDefinition)
.getInstruction()
.(IR::IncDecInstruction)
.getRhs()
.(IR::EvalIncDecRhsInstruction)
.getStmt() = incDec and
ssaDependsOnExpr(prevDef, incDec.getOperand())
)
or
//if `nextDef` corresponding to the init of a parameter, there is no corresponding `prevDef`
//if `nextDef` is a phi node and `prevDef` is one of the input of the phi node, then `nextDef` depends on `prevDef` directly.
exists(SsaPhiNode phi | nextDef = phi and phi.getAnInput().getDefinition() = prevDef)
}
/**
* Checks if SSA definition `def` depends on the value of `expr`.
*
* The structure of this function needs to be same as `getAnUpperBound`
*/
predicate ssaDependsOnExpr(SsaDefinition def, Expr expr) {
if expr.isConst()
then none()
else (
exists(Ident ident |
ident = expr and
getAUse(def) = ident
)
or
exists(AddExpr add | add = expr and ssaDependsOnExpr(def, add.getAnOperand()))
or
exists(SubExpr sub | sub = expr and ssaDependsOnExpr(def, sub.getAnOperand()))
or
exists(RemExpr rem | rem = expr and ssaDependsOnExpr(def, rem.getAnOperand()))
or
exists(PlusExpr plus |
plus = expr and
ssaDependsOnExpr(def, plus.getOperand())
)
or
exists(MinusExpr minus |
minus = expr and
ssaDependsOnExpr(def, minus.getOperand())
)
or
exists(MulExpr mul |
mul = expr and
ssaDependsOnExpr(def, mul.getAnOperand())
)
or
//if the expr is a selector expression, we currently do not support analyze it.
//if the expr is a conversion
exists(ConversionExpr conv |
conv = expr and
ssaDependsOnExpr(def, conv.getOperand())
)
or
exists(AndExpr bitAnd |
bitAnd = expr and
ssaDependsOnExpr(def, bitAnd.getAnOperand())
)
)
}
/**
* Checks if SSA definition `def` depends on self.
*/
predicate recursiveSelfDef(SsaDefinition def) { ssaDependsOnSsa+(def, def) }
/**
* Gets the maximum value expression `expr` can represent.
*/
float getMaxRepresentableValue(Expr expr) { result = typeMaxValue(expr.getType()) }
/**
* Gets the minimum value expression `expr` can represent.
*/
float getMinRepresentableValue(Expr expr) { result = typeMinValue(expr.getType()) }
/**
* Gets the maximum value type `t` can represent.
*/
float typeMaxValue(Type t) {
exists(IntegerType integerTp, int bits |
integerTp = t and
not integerTp instanceof UintptrType and
bits = min(integerTp.getASize()) and
if integerTp instanceof SignedIntegerType
then result = 2.pow(bits - 1) - 1
else result = 2.pow(bits) - 1
)
or
t instanceof FloatType and
result = 1.0 / 0.0
or
t instanceof UintptrType and
result = 2.pow(64) - 1
}
/**
* Gets the minimum value type `t` can represent.
*/
float typeMinValue(Type t) {
exists(IntegerType integerTp, int bits |
integerTp = t and
not integerTp instanceof UintptrType and
bits = min(integerTp.getASize()) and
if integerTp instanceof SignedIntegerType then result = -2.pow(bits - 1) else result = 0
)
or
t instanceof FloatType and
result = -1.0 / 0.0
or
t instanceof UintptrType and
result = 0
}
/*
* decide if an expression may overflow
*/
predicate exprMayOverflow(Expr expr) { getUpperBound(expr) > getMaxRepresentableValue(expr) }
/*
* decide if an expression may underflow
*/
predicate exprMayUnderflow(Expr expr) { getLowerBound(expr) < getMinRepresentableValue(expr) }
/**
* Computes a normal form of `x` where -0.0 has changed to +0.0. This can be
* needed on the lesser side of a floating-point comparison or on both sides of
* a floating point equality because QL does not follow IEEE in floating-point
* comparisons but instead defines -0.0 to be less than and distinct from 0.0.
*/
bindingset[x]
private float normalizeFloatUp(float x) { result = x + 0.0 }
/**
* Computes `x + y`, rounded towards +Inf. This is the general case where both
* `x` and `y` may be large numbers.
*/
bindingset[x, y]
float addRoundingUp(float x, float y) {
if normalizeFloatUp((x + y) - x) < y or normalizeFloatUp((x + y) - y) < x
then result = (x + y).nextUp()
else result = (x + y)
}
/**
* Computes `x + y`, rounded towards -Inf. This is the general case where both
* `x` and `y` may be large numbers.
*/
bindingset[x, y]
float addRoundingDown(float x, float y) {
if (x + y) - x > normalizeFloatUp(y) or (x + y) - y > normalizeFloatUp(x)
then result = (x + y).nextDown()
else result = (x + y)
}