-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfloat_bv.cpp
1511 lines (1249 loc) · 43.9 KB
/
float_bv.cpp
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module:
Author: Daniel Kroening, kroening@kroening.com
\*******************************************************************/
#include "float_bv.h"
#include <algorithm>
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/bitvector_types.h>
#include <util/floatbv_expr.h>
#include <util/std_expr.h>
exprt float_bvt::convert(const exprt &expr) const
{
if(expr.id()==ID_abs)
return abs(to_abs_expr(expr).op(), get_spec(expr));
else if(expr.id()==ID_unary_minus)
return negation(to_unary_minus_expr(expr).op(), get_spec(expr));
else if(expr.id()==ID_ieee_float_equal)
{
const auto &equal_expr = to_ieee_float_equal_expr(expr);
return is_equal(
equal_expr.lhs(), equal_expr.rhs(), get_spec(equal_expr.lhs()));
}
else if(expr.id()==ID_ieee_float_notequal)
{
const auto ¬equal_expr = to_ieee_float_notequal_expr(expr);
return not_exprt(is_equal(
notequal_expr.lhs(), notequal_expr.rhs(), get_spec(notequal_expr.lhs())));
}
else if(expr.id()==ID_floatbv_typecast)
{
const auto &floatbv_typecast_expr = to_floatbv_typecast_expr(expr);
const auto &op = floatbv_typecast_expr.op();
const typet &src_type = floatbv_typecast_expr.op().type();
const typet &dest_type = floatbv_typecast_expr.type();
const auto &rounding_mode = floatbv_typecast_expr.rounding_mode();
if(dest_type.id()==ID_signedbv &&
src_type.id()==ID_floatbv) // float -> signed
return to_signed_integer(
op,
to_signedbv_type(dest_type).get_width(),
rounding_mode,
get_spec(op));
else if(dest_type.id()==ID_unsignedbv &&
src_type.id()==ID_floatbv) // float -> unsigned
return to_unsigned_integer(
op,
to_unsignedbv_type(dest_type).get_width(),
rounding_mode,
get_spec(op));
else if(src_type.id()==ID_signedbv &&
dest_type.id()==ID_floatbv) // signed -> float
return from_signed_integer(op, rounding_mode, get_spec(expr));
else if(src_type.id()==ID_unsignedbv &&
dest_type.id()==ID_floatbv) // unsigned -> float
{
return from_unsigned_integer(op, rounding_mode, get_spec(expr));
}
else if(dest_type.id()==ID_floatbv &&
src_type.id()==ID_floatbv) // float -> float
{
return conversion(op, rounding_mode, get_spec(op), get_spec(expr));
}
else
return nil_exprt();
}
else if(
expr.id() == ID_typecast && expr.is_boolean() &&
to_typecast_expr(expr).op().type().id() == ID_floatbv) // float -> bool
{
return not_exprt(is_zero(to_typecast_expr(expr).op()));
}
else if(
expr.id() == ID_typecast && expr.type().id() == ID_bv &&
to_typecast_expr(expr).op().type().id() == ID_floatbv) // float -> raw bv
{
const typecast_exprt &tc = to_typecast_expr(expr);
const bitvector_typet &dest_type = to_bitvector_type(expr.type());
const floatbv_typet &src_type = to_floatbv_type(tc.op().type());
if(
dest_type.get_width() != src_type.get_width() ||
dest_type.get_width() == 0)
{
return nil_exprt{};
}
return extractbits_exprt{to_typecast_expr(expr).op(), 0, dest_type};
}
else if(expr.id()==ID_floatbv_plus)
{
const auto &float_expr = to_ieee_float_op_expr(expr);
return add_sub(
false,
float_expr.lhs(),
float_expr.rhs(),
float_expr.rounding_mode(),
get_spec(expr));
}
else if(expr.id()==ID_floatbv_minus)
{
const auto &float_expr = to_ieee_float_op_expr(expr);
return add_sub(
true,
float_expr.lhs(),
float_expr.rhs(),
float_expr.rounding_mode(),
get_spec(expr));
}
else if(expr.id()==ID_floatbv_mult)
{
const auto &float_expr = to_ieee_float_op_expr(expr);
return mul(
float_expr.lhs(),
float_expr.rhs(),
float_expr.rounding_mode(),
get_spec(expr));
}
else if(expr.id()==ID_floatbv_div)
{
const auto &float_expr = to_ieee_float_op_expr(expr);
return div(
float_expr.lhs(),
float_expr.rhs(),
float_expr.rounding_mode(),
get_spec(expr));
}
else if(expr.id()==ID_isnan)
{
const auto &op = to_unary_expr(expr).op();
return isnan(op, get_spec(op));
}
else if(expr.id()==ID_isfinite)
{
const auto &op = to_unary_expr(expr).op();
return isfinite(op, get_spec(op));
}
else if(expr.id()==ID_isinf)
{
const auto &op = to_unary_expr(expr).op();
return isinf(op, get_spec(op));
}
else if(expr.id()==ID_isnormal)
{
const auto &op = to_unary_expr(expr).op();
return isnormal(op, get_spec(op));
}
else if(expr.id()==ID_lt)
{
const auto &rel_expr = to_binary_relation_expr(expr);
return relation(
rel_expr.lhs(), relt::LT, rel_expr.rhs(), get_spec(rel_expr.lhs()));
}
else if(expr.id()==ID_gt)
{
const auto &rel_expr = to_binary_relation_expr(expr);
return relation(
rel_expr.lhs(), relt::GT, rel_expr.rhs(), get_spec(rel_expr.lhs()));
}
else if(expr.id()==ID_le)
{
const auto &rel_expr = to_binary_relation_expr(expr);
return relation(
rel_expr.lhs(), relt::LE, rel_expr.rhs(), get_spec(rel_expr.lhs()));
}
else if(expr.id()==ID_ge)
{
const auto &rel_expr = to_binary_relation_expr(expr);
return relation(
rel_expr.lhs(), relt::GE, rel_expr.rhs(), get_spec(rel_expr.lhs()));
}
else if(expr.id()==ID_sign)
return sign_bit(to_unary_expr(expr).op());
return nil_exprt();
}
ieee_float_spect float_bvt::get_spec(const exprt &expr)
{
const floatbv_typet &type=to_floatbv_type(expr.type());
return ieee_float_spect(type);
}
exprt float_bvt::abs(const exprt &op, const ieee_float_spect &spec)
{
// we mask away the sign bit, which is the most significant bit
const mp_integer v = power(2, spec.width() - 1) - 1;
const constant_exprt mask(integer2bvrep(v, spec.width()), op.type());
return bitand_exprt(op, mask);
}
exprt float_bvt::negation(const exprt &op, const ieee_float_spect &spec)
{
// we flip the sign bit with an xor
const mp_integer v = power(2, spec.width() - 1);
constant_exprt mask(integer2bvrep(v, spec.width()), op.type());
return bitxor_exprt(op, mask);
}
exprt float_bvt::is_equal(
const exprt &src0,
const exprt &src1,
const ieee_float_spect &spec)
{
// special cases: -0 and 0 are equal
const exprt is_zero0 = is_zero(src0);
const exprt is_zero1 = is_zero(src1);
const and_exprt both_zero(is_zero0, is_zero1);
// NaN compares to nothing
exprt isnan0=isnan(src0, spec);
exprt isnan1=isnan(src1, spec);
const or_exprt nan(isnan0, isnan1);
const equal_exprt bitwise_equal(src0, src1);
return and_exprt(
or_exprt(bitwise_equal, both_zero),
not_exprt(nan));
}
exprt float_bvt::is_zero(const exprt &src)
{
// we mask away the sign bit, which is the most significant bit
const floatbv_typet &type=to_floatbv_type(src.type());
std::size_t width=type.get_width();
const mp_integer v = power(2, width - 1) - 1;
constant_exprt mask(integer2bvrep(v, width), src.type());
ieee_float_valuet z(type);
z.make_zero();
return equal_exprt(bitand_exprt(src, mask), z.to_expr());
}
exprt float_bvt::exponent_all_ones(
const exprt &src,
const ieee_float_spect &spec)
{
exprt exponent=get_exponent(src, spec);
exprt all_ones=to_unsignedbv_type(exponent.type()).largest_expr();
return equal_exprt(exponent, all_ones);
}
exprt float_bvt::exponent_all_zeros(
const exprt &src,
const ieee_float_spect &spec)
{
exprt exponent=get_exponent(src, spec);
exprt all_zeros=to_unsignedbv_type(exponent.type()).zero_expr();
return equal_exprt(exponent, all_zeros);
}
exprt float_bvt::fraction_all_zeros(
const exprt &src,
const ieee_float_spect &spec)
{
// does not include hidden bit
exprt fraction=get_fraction(src, spec);
exprt all_zeros=to_unsignedbv_type(fraction.type()).zero_expr();
return equal_exprt(fraction, all_zeros);
}
void float_bvt::rounding_mode_bitst::get(const exprt &rm)
{
exprt round_to_even_const=from_integer(ieee_floatt::ROUND_TO_EVEN, rm.type());
exprt round_to_plus_inf_const=
from_integer(ieee_floatt::ROUND_TO_PLUS_INF, rm.type());
exprt round_to_minus_inf_const=
from_integer(ieee_floatt::ROUND_TO_MINUS_INF, rm.type());
exprt round_to_zero_const=from_integer(ieee_floatt::ROUND_TO_ZERO, rm.type());
exprt round_to_away_const =
from_integer(ieee_floatt::ROUND_TO_AWAY, rm.type());
round_to_even=equal_exprt(rm, round_to_even_const);
round_to_plus_inf=equal_exprt(rm, round_to_plus_inf_const);
round_to_minus_inf=equal_exprt(rm, round_to_minus_inf_const);
round_to_zero=equal_exprt(rm, round_to_zero_const);
round_to_away = equal_exprt(rm, round_to_away_const);
}
exprt float_bvt::sign_bit(const exprt &op)
{
const bitvector_typet &bv_type=to_bitvector_type(op.type());
std::size_t width=bv_type.get_width();
return extractbit_exprt(op, width-1);
}
exprt float_bvt::from_signed_integer(
const exprt &src,
const exprt &rm,
const ieee_float_spect &spec) const
{
std::size_t src_width=to_signedbv_type(src.type()).get_width();
unbiased_floatt result;
// we need to adjust for negative integers
result.sign=sign_bit(src);
result.fraction=
typecast_exprt(abs_exprt(src), unsignedbv_typet(src_width));
// build an exponent (unbiased) -- this is signed!
result.exponent=
from_integer(
src_width-1,
signedbv_typet(address_bits(src_width - 1) + 1));
return rounder(result, rm, spec);
}
exprt float_bvt::from_unsigned_integer(
const exprt &src,
const exprt &rm,
const ieee_float_spect &spec) const
{
unbiased_floatt result;
result.fraction=src;
std::size_t src_width=to_unsignedbv_type(src.type()).get_width();
// build an exponent (unbiased) -- this is signed!
result.exponent=
from_integer(
src_width-1,
signedbv_typet(address_bits(src_width - 1) + 1));
result.sign=false_exprt();
return rounder(result, rm, spec);
}
exprt float_bvt::to_signed_integer(
const exprt &src,
std::size_t dest_width,
const exprt &rm,
const ieee_float_spect &spec)
{
return to_integer(src, dest_width, true, rm, spec);
}
exprt float_bvt::to_unsigned_integer(
const exprt &src,
std::size_t dest_width,
const exprt &rm,
const ieee_float_spect &spec)
{
return to_integer(src, dest_width, false, rm, spec);
}
exprt float_bvt::to_integer(
const exprt &src,
std::size_t dest_width,
bool is_signed,
const exprt &rm,
const ieee_float_spect &spec)
{
const unbiased_floatt unpacked=unpack(src, spec);
rounding_mode_bitst rounding_mode_bits(rm);
// Right now hard-wired to round-to-zero, which is
// the usual case in ANSI-C.
// if the exponent is positive, shift right
exprt offset=from_integer(spec.f, signedbv_typet(spec.e));
const minus_exprt distance(offset, unpacked.exponent);
const lshr_exprt shift_result(unpacked.fraction, distance);
// if the exponent is negative, we have zero anyways
exprt result=shift_result;
const sign_exprt exponent_sign(unpacked.exponent);
result=
if_exprt(exponent_sign, from_integer(0, result.type()), result);
// chop out the right number of bits from the result
typet result_type=
is_signed?static_cast<typet>(signedbv_typet(dest_width)):
static_cast<typet>(unsignedbv_typet(dest_width));
result=typecast_exprt(result, result_type);
// if signed, apply sign.
if(is_signed)
{
result=if_exprt(
unpacked.sign, unary_minus_exprt(result), result);
}
else
{
// It's unclear what the behaviour for negative floats
// to integer shall be.
}
return result;
}
exprt float_bvt::conversion(
const exprt &src,
const exprt &rm,
const ieee_float_spect &src_spec,
const ieee_float_spect &dest_spec) const
{
// Catch the special case in which we extend,
// e.g. single to double.
// In this case, rounding can be avoided,
// but a denormal number may be come normal.
// Be careful to exclude the difficult case
// when denormalised numbers in the old format
// can be converted to denormalised numbers in the
// new format. Note that this is rare and will only
// happen with very non-standard formats.
int sourceSmallestNormalExponent = -((1 << (src_spec.e - 1)) - 1);
int sourceSmallestDenormalExponent =
sourceSmallestNormalExponent - src_spec.f;
// Using the fact that f doesn't include the hidden bit
int destSmallestNormalExponent = -((1 << (dest_spec.e - 1)) - 1);
if(dest_spec.e>=src_spec.e &&
dest_spec.f>=src_spec.f &&
!(sourceSmallestDenormalExponent < destSmallestNormalExponent))
{
unbiased_floatt unpacked_src=unpack(src, src_spec);
unbiased_floatt result;
// the fraction gets zero-padded
std::size_t padding=dest_spec.f-src_spec.f;
result.fraction=
concatenation_exprt(
unpacked_src.fraction,
from_integer(0, unsignedbv_typet(padding)),
unsignedbv_typet(dest_spec.f+1));
// the exponent gets sign-extended
INVARIANT(
unpacked_src.exponent.type().id() == ID_signedbv,
"the exponent needs to have a signed type");
result.exponent=
typecast_exprt(unpacked_src.exponent, signedbv_typet(dest_spec.e));
// if the number was denormal and is normal in the new format,
// normalise it!
if(dest_spec.e > src_spec.e)
{
normalization_shift(result.fraction, result.exponent);
// normalization_shift unconditionally extends the exponent size to avoid
// arithmetic overflow, but this cannot have happened here as the exponent
// had already been extended to dest_spec's size
result.exponent =
typecast_exprt(result.exponent, signedbv_typet(dest_spec.e));
}
// the flags get copied
result.sign=unpacked_src.sign;
result.NaN=unpacked_src.NaN;
result.infinity=unpacked_src.infinity;
// no rounding needed!
return pack(bias(result, dest_spec), dest_spec);
}
else
{
// we actually need to round
unbiased_floatt result=unpack(src, src_spec);
return rounder(result, rm, dest_spec);
}
}
exprt float_bvt::isnormal(
const exprt &src,
const ieee_float_spect &spec)
{
return and_exprt(
not_exprt(exponent_all_zeros(src, spec)),
not_exprt(exponent_all_ones(src, spec)));
}
/// Subtracts the exponents
exprt float_bvt::subtract_exponents(
const unbiased_floatt &src1,
const unbiased_floatt &src2)
{
// extend both by one bit
std::size_t old_width1=to_signedbv_type(src1.exponent.type()).get_width();
std::size_t old_width2=to_signedbv_type(src2.exponent.type()).get_width();
PRECONDITION(old_width1 == old_width2);
const typecast_exprt extended_exponent1(
src1.exponent, signedbv_typet(old_width1 + 1));
const typecast_exprt extended_exponent2(
src2.exponent, signedbv_typet(old_width2 + 1));
// compute shift distance (here is the subtraction)
return minus_exprt(extended_exponent1, extended_exponent2);
}
exprt float_bvt::add_sub(
bool subtract,
const exprt &op0,
const exprt &op1,
const exprt &rm,
const ieee_float_spect &spec) const
{
unbiased_floatt unpacked1=unpack(op0, spec);
unbiased_floatt unpacked2=unpack(op1, spec);
// subtract?
if(subtract)
unpacked2.sign=not_exprt(unpacked2.sign);
// figure out which operand has the bigger exponent
const exprt exponent_difference=subtract_exponents(unpacked1, unpacked2);
const sign_exprt src2_bigger(exponent_difference);
const exprt bigger_exponent=
if_exprt(src2_bigger, unpacked2.exponent, unpacked1.exponent);
// swap fractions as needed
const exprt new_fraction1=
if_exprt(src2_bigger, unpacked2.fraction, unpacked1.fraction);
const exprt new_fraction2=
if_exprt(src2_bigger, unpacked1.fraction, unpacked2.fraction);
// compute distance
const exprt distance=
typecast_exprt(abs_exprt(exponent_difference), unsignedbv_typet(spec.e));
// limit the distance: shifting more than f+3 bits is unnecessary
const exprt limited_dist=limit_distance(distance, spec.f+3);
// pad fractions with 3 zeros from below
exprt three_zeros=from_integer(0, unsignedbv_typet(3));
// add 4 to spec.f because unpacked new_fraction has the hidden bit
const exprt fraction1_padded=
concatenation_exprt(new_fraction1, three_zeros, unsignedbv_typet(spec.f+4));
const exprt fraction2_padded=
concatenation_exprt(new_fraction2, three_zeros, unsignedbv_typet(spec.f+4));
// shift new_fraction2
exprt sticky_bit;
const exprt fraction1_shifted=fraction1_padded;
const exprt fraction2_shifted=sticky_right_shift(
fraction2_padded, limited_dist, sticky_bit);
// sticky bit: 'or' of the bits lost by the right-shift
const bitor_exprt fraction2_stickied(
fraction2_shifted,
concatenation_exprt(
from_integer(0, unsignedbv_typet(spec.f + 3)),
sticky_bit,
fraction2_shifted.type()));
// need to have two extra fraction bits for addition and rounding
const exprt fraction1_ext=
typecast_exprt(fraction1_shifted, unsignedbv_typet(spec.f+4+2));
const exprt fraction2_ext=
typecast_exprt(fraction2_stickied, unsignedbv_typet(spec.f+4+2));
unbiased_floatt result;
// now add/sub them
const notequal_exprt subtract_lit(unpacked1.sign, unpacked2.sign);
result.fraction=
if_exprt(subtract_lit,
minus_exprt(fraction1_ext, fraction2_ext),
plus_exprt(fraction1_ext, fraction2_ext));
// sign of result
std::size_t width=to_bitvector_type(result.fraction.type()).get_width();
const sign_exprt fraction_sign(
typecast_exprt(result.fraction, signedbv_typet(width)));
result.fraction=
typecast_exprt(
abs_exprt(typecast_exprt(result.fraction, signedbv_typet(width))),
unsignedbv_typet(width));
result.exponent=bigger_exponent;
// adjust the exponent for the fact that we added two bits to the fraction
result.exponent=
plus_exprt(typecast_exprt(result.exponent, signedbv_typet(spec.e+1)),
from_integer(2, signedbv_typet(spec.e+1)));
// NaN?
result.NaN=or_exprt(
and_exprt(and_exprt(unpacked1.infinity, unpacked2.infinity),
notequal_exprt(unpacked1.sign, unpacked2.sign)),
or_exprt(unpacked1.NaN, unpacked2.NaN));
// infinity?
result.infinity=and_exprt(
not_exprt(result.NaN),
or_exprt(unpacked1.infinity, unpacked2.infinity));
// zero?
// Note that:
// 1. The zero flag isn't used apart from in divide and
// is only set on unpack
// 2. Subnormals mean that addition or subtraction can't round to 0,
// thus we can perform this test now
// 3. The rules for sign are different for zero
result.zero=
and_exprt(
not_exprt(or_exprt(result.infinity, result.NaN)),
equal_exprt(
result.fraction,
from_integer(0, result.fraction.type())));
// sign
const notequal_exprt add_sub_sign(
if_exprt(src2_bigger, unpacked2.sign, unpacked1.sign), fraction_sign);
const if_exprt infinity_sign(
unpacked1.infinity, unpacked1.sign, unpacked2.sign);
#if 1
rounding_mode_bitst rounding_mode_bits(rm);
const if_exprt zero_sign(
rounding_mode_bits.round_to_minus_inf,
or_exprt(unpacked1.sign, unpacked2.sign),
and_exprt(unpacked1.sign, unpacked2.sign));
result.sign=if_exprt(
result.infinity,
infinity_sign,
if_exprt(result.zero,
zero_sign,
add_sub_sign));
#else
result.sign=if_exprt(
result.infinity,
infinity_sign,
add_sub_sign);
#endif
return rounder(result, rm, spec);
}
/// Limits the shift distance
exprt float_bvt::limit_distance(
const exprt &dist,
mp_integer limit)
{
std::size_t nb_bits = address_bits(limit);
std::size_t dist_width=to_unsignedbv_type(dist.type()).get_width();
if(dist_width<=nb_bits)
return dist;
const extractbits_exprt upper_bits(
dist, nb_bits, unsignedbv_typet(dist_width - nb_bits));
const equal_exprt upper_bits_zero(
upper_bits, from_integer(0, upper_bits.type()));
const extractbits_exprt lower_bits(dist, 0, unsignedbv_typet(nb_bits));
return if_exprt(
upper_bits_zero,
lower_bits,
unsignedbv_typet(nb_bits).largest_expr());
}
exprt float_bvt::mul(
const exprt &src1,
const exprt &src2,
const exprt &rm,
const ieee_float_spect &spec) const
{
// unpack
const unbiased_floatt unpacked1=unpack(src1, spec);
const unbiased_floatt unpacked2=unpack(src2, spec);
// zero-extend the fractions (unpacked fraction has the hidden bit)
typet new_fraction_type=unsignedbv_typet((spec.f+1)*2);
const exprt fraction1 =
zero_extend_exprt{unpacked1.fraction, new_fraction_type};
const exprt fraction2 =
zero_extend_exprt{unpacked2.fraction, new_fraction_type};
// multiply the fractions
unbiased_floatt result;
result.fraction=mult_exprt(fraction1, fraction2);
// extend exponents to account for overflow
// add two bits, as we do extra arithmetic on it later
typet new_exponent_type=signedbv_typet(spec.e+2);
const exprt exponent1=typecast_exprt(unpacked1.exponent, new_exponent_type);
const exprt exponent2=typecast_exprt(unpacked2.exponent, new_exponent_type);
const plus_exprt added_exponent(exponent1, exponent2);
// Adjust exponent; we are thowing in an extra fraction bit,
// it has been extended above.
result.exponent=
plus_exprt(added_exponent, from_integer(1, new_exponent_type));
// new sign
result.sign=notequal_exprt(unpacked1.sign, unpacked2.sign);
// infinity?
result.infinity=or_exprt(unpacked1.infinity, unpacked2.infinity);
// NaN?
result.NaN = disjunction(
{isnan(src1, spec),
isnan(src2, spec),
// infinity * 0 is NaN!
and_exprt(unpacked1.zero, unpacked2.infinity),
and_exprt(unpacked2.zero, unpacked1.infinity)});
return rounder(result, rm, spec);
}
exprt float_bvt::div(
const exprt &src1,
const exprt &src2,
const exprt &rm,
const ieee_float_spect &spec) const
{
// unpack
const unbiased_floatt unpacked1=unpack(src1, spec);
const unbiased_floatt unpacked2=unpack(src2, spec);
std::size_t fraction_width=
to_unsignedbv_type(unpacked1.fraction.type()).get_width();
std::size_t div_width=fraction_width*2+1;
// pad fraction1 with zeros
const concatenation_exprt fraction1(
unpacked1.fraction,
from_integer(0, unsignedbv_typet(div_width - fraction_width)),
unsignedbv_typet(div_width));
// zero-extend fraction2 to match fraction1
const zero_extend_exprt fraction2{unpacked2.fraction, fraction1.type()};
// divide fractions
unbiased_floatt result;
exprt rem;
// the below should be merged somehow
result.fraction=div_exprt(fraction1, fraction2);
rem=mod_exprt(fraction1, fraction2);
// is there a remainder?
const notequal_exprt have_remainder(rem, from_integer(0, rem.type()));
// we throw this into the result, as least-significant bit,
// to get the right rounding decision
result.fraction=
concatenation_exprt(
result.fraction, have_remainder, unsignedbv_typet(div_width+1));
// We will subtract the exponents;
// to account for overflow, we add a bit.
const typecast_exprt exponent1(
unpacked1.exponent, signedbv_typet(spec.e + 1));
const typecast_exprt exponent2(
unpacked2.exponent, signedbv_typet(spec.e + 1));
// subtract exponents
const minus_exprt added_exponent(exponent1, exponent2);
// adjust, as we have thown in extra fraction bits
result.exponent=plus_exprt(
added_exponent,
from_integer(spec.f, added_exponent.type()));
// new sign
result.sign=notequal_exprt(unpacked1.sign, unpacked2.sign);
// Infinity? This happens when
// 1) dividing a non-nan/non-zero by zero, or
// 2) first operand is inf and second is non-nan and non-zero
// In particular, inf/0=inf.
result.infinity=
or_exprt(
and_exprt(not_exprt(unpacked1.zero),
and_exprt(not_exprt(unpacked1.NaN),
unpacked2.zero)),
and_exprt(unpacked1.infinity,
and_exprt(not_exprt(unpacked2.NaN),
not_exprt(unpacked2.zero))));
// NaN?
result.NaN=or_exprt(unpacked1.NaN,
or_exprt(unpacked2.NaN,
or_exprt(and_exprt(unpacked1.zero, unpacked2.zero),
and_exprt(unpacked1.infinity, unpacked2.infinity))));
// Division by infinity produces zero, unless we have NaN
const and_exprt force_zero(not_exprt(unpacked1.NaN), unpacked2.infinity);
result.fraction=
if_exprt(
force_zero,
from_integer(0, result.fraction.type()),
result.fraction);
return rounder(result, rm, spec);
}
exprt float_bvt::relation(
const exprt &src1,
relt rel,
const exprt &src2,
const ieee_float_spect &spec)
{
if(rel==relt::GT)
return relation(src2, relt::LT, src1, spec); // swapped
else if(rel==relt::GE)
return relation(src2, relt::LE, src1, spec); // swapped
INVARIANT(
rel == relt::EQ || rel == relt::LT || rel == relt::LE,
"relation should be equality, less-than, or less-or-equal");
// special cases: -0 and 0 are equal
const exprt is_zero1 = is_zero(src1);
const exprt is_zero2 = is_zero(src2);
const and_exprt both_zero(is_zero1, is_zero2);
// NaN compares to nothing
exprt isnan1=isnan(src1, spec);
exprt isnan2=isnan(src2, spec);
const or_exprt nan(isnan1, isnan2);
if(rel==relt::LT || rel==relt::LE)
{
const equal_exprt bitwise_equal(src1, src2);
// signs different? trivial! Unless Zero.
const notequal_exprt signs_different(sign_bit(src1), sign_bit(src2));
// as long as the signs match: compare like unsigned numbers
// this works due to the BIAS
const binary_relation_exprt less_than1(
typecast_exprt(
typecast_exprt(src1, bv_typet(spec.width())),
unsignedbv_typet(spec.width())),
ID_lt,
typecast_exprt(
typecast_exprt(src2, bv_typet(spec.width())),
unsignedbv_typet(spec.width())));
// if both are negative (and not the same), need to turn around!
const notequal_exprt less_than2(
less_than1, and_exprt(sign_bit(src1), sign_bit(src2)));
const if_exprt less_than3(signs_different, sign_bit(src1), less_than2);
if(rel==relt::LT)
{
and_exprt and_bv{{less_than3,
// for the case of two negative numbers
not_exprt(bitwise_equal),
not_exprt(both_zero),
not_exprt(nan)}};
return std::move(and_bv);
}
else if(rel==relt::LE)
{
or_exprt or_bv{{less_than3, both_zero, bitwise_equal}};
return and_exprt(or_bv, not_exprt(nan));
}
else
UNREACHABLE;
}
else if(rel==relt::EQ)
{
const equal_exprt bitwise_equal(src1, src2);
return and_exprt(
or_exprt(bitwise_equal, both_zero),
not_exprt(nan));
}
UNREACHABLE;
return false_exprt();
}
exprt float_bvt::isinf(
const exprt &src,
const ieee_float_spect &spec)
{
return and_exprt(
exponent_all_ones(src, spec),
fraction_all_zeros(src, spec));
}
exprt float_bvt::isfinite(
const exprt &src,
const ieee_float_spect &spec)
{
return not_exprt(or_exprt(isinf(src, spec), isnan(src, spec)));
}
/// Gets the unbiased exponent in a floating-point bit-vector
exprt float_bvt::get_exponent(
const exprt &src,
const ieee_float_spect &spec)
{
return extractbits_exprt(src, spec.f, unsignedbv_typet(spec.e));
}
/// Gets the fraction without hidden bit in a floating-point bit-vector src
exprt float_bvt::get_fraction(
const exprt &src,
const ieee_float_spect &spec)
{
return extractbits_exprt(src, 0, unsignedbv_typet(spec.f));
}
exprt float_bvt::isnan(
const exprt &src,
const ieee_float_spect &spec)
{
return and_exprt(exponent_all_ones(src, spec),
not_exprt(fraction_all_zeros(src, spec)));
}
/// normalize fraction/exponent pair returns 'zero' if fraction is zero
void float_bvt::normalization_shift(
exprt &fraction,
exprt &exponent)
{
// n-log-n alignment shifter.
// The worst-case shift is the number of fraction
// bits minus one, in case the fraction is one exactly.
std::size_t fraction_bits=to_unsignedbv_type(fraction.type()).get_width();
std::size_t exponent_bits=to_signedbv_type(exponent.type()).get_width();
PRECONDITION(fraction_bits != 0);
std::size_t depth = address_bits(fraction_bits - 1);
exponent = typecast_exprt(
exponent, signedbv_typet(std::max(depth, exponent_bits + 1)));
exprt exponent_delta=from_integer(0, exponent.type());
for(int d=depth-1; d>=0; d--)
{
unsigned distance=(1<<d);
INVARIANT(
fraction_bits > distance,
"distance must be within the range of fraction bits");
// check if first 'distance'-many bits are zeros
const extractbits_exprt prefix(
fraction, fraction_bits - distance, unsignedbv_typet(distance));
const equal_exprt prefix_is_zero(prefix, from_integer(0, prefix.type()));
// If so, shift the zeros out left by 'distance'.
// Otherwise, leave as is.
const shl_exprt shifted(fraction, distance);
fraction=
if_exprt(prefix_is_zero, shifted, fraction);
// add corresponding weight to exponent
INVARIANT(
d < (signed int)exponent_bits,
"depth must be smaller than exponent bits");
exponent_delta=
bitor_exprt(exponent_delta,
shl_exprt(typecast_exprt(prefix_is_zero, exponent_delta.type()), d));
}
exponent=minus_exprt(exponent, exponent_delta);
}
/// make sure exponent is not too small; the exponent is unbiased