19 bvt round_to_plus_inf=
21 bvt round_to_minus_inf=
72 std::size_t dest_width)
79 std::size_t dest_width)
86 std::size_t dest_width,
102 if(dest_width > fraction.size())
105 bv_utils.build_constant(0U, dest_width - fraction.size());
107 fraction.begin(), lsb_extension.begin(), lsb_extension.end());
116 const std::size_t distance_width =
118 bvt offset =
bv_utils.build_constant(fraction.size() - 1, distance_width);
125 bvt result = shift_result;
128 for(std::size_t i = 0; i < result.size(); i++)
129 result[i] =
prop.land(result[i], !exponent_sign);
132 if(result.size() > dest_width)
134 result.resize(dest_width);
138 result.size() == dest_width,
139 "result bitvector width should equal the destination bitvector width");
171 auto unpacked =
unpack(src);
172 auto is_special =
prop.lor({unpacked.zero, unpacked.NaN, unpacked.infinity});
184 magic_number_bv.back() = src.back();
186 auto tmp1 =
add_sub(src, magic_number_bv,
false);
188 auto tmp2 =
add_sub(tmp1, magic_number_bv,
true);
191 tmp2.back() = src.back();
193 return bv_utils.select(
prop.lor(is_special, ge_magic_number), src, tmp2);
213 int sourceSmallestNormalExponent=-((1 << (
spec.e - 1)) - 1);
214 int sourceSmallestDenormalExponent =
215 sourceSmallestNormalExponent -
spec.f;
219 int destSmallestNormalExponent=-((1 << (dest_spec.
e - 1)) - 1);
221 if(dest_spec.
e>=
spec.e &&
222 dest_spec.
f>=
spec.f &&
223 !(sourceSmallestDenormalExponent < destSmallestNormalExponent))
229 std::size_t padding=dest_spec.
f-
spec.f;
239 if(dest_spec.
e >
spec.e)
250 result.
NaN=unpacked_src.
NaN;
280 bvt extended_exponent1=
282 bvt extended_exponent2=
285 PRECONDITION(extended_exponent1.size() == extended_exponent2.size());
288 return bv_utils.sub(extended_exponent1, extended_exponent2);
305 literalt src2_bigger=exponent_difference.back();
307 const bvt bigger_exponent=
311 const bvt new_fraction1=
314 const bvt new_fraction2=
318 const bvt distance=
bv_utils.absolute_value(exponent_difference);
324 const bvt fraction1_padded=
326 const bvt fraction2_padded=
331 const bvt fraction1_shifted=fraction1_padded;
333 fraction2_padded, limited_dist, sticky_bit);
336 bvt fraction2_stickied=fraction2_shifted;
337 fraction2_stickied[0]=
prop.lor(fraction2_shifted[0], sticky_bit);
340 const bvt fraction1_ext=
341 bv_utils.zero_extension(fraction1_shifted, fraction1_shifted.size()+2);
342 const bvt fraction2_ext=
343 bv_utils.zero_extension(fraction2_stickied, fraction2_stickied.size()+2);
350 bv_utils.add_sub(fraction1_ext, fraction2_ext, subtract_lit);
423 for(std::size_t i=0; i<result.
fraction.size(); i++)
424 result.
fraction[i]=new_fraction2[i];
440 upper_bits.erase(upper_bits.begin(), upper_bits.begin()+nb_bits);
444 lower_bits.resize(nb_bits);
447 result.resize(lower_bits.size());
450 for(std::size_t i=0; i<result.size(); i++)
451 result[i]=
prop.lor(lower_bits[i], or_upper_bits);
479 bvt added_exponent=
bv_utils.add(exponent1, exponent2);
495 NaN_cond.push_back(
is_NaN(src1));
496 NaN_cond.push_back(
is_NaN(src2));
509 const bvt &multiply_lhs,
510 const bvt &multiply_rhs,
522 const std::size_t frac_size = unpacked_lhs.
fraction.size();
537 prod_exponent =
bv_utils.inc(prod_exponent);
547 -(
mp_integer(1) << (prod_exponent.size() - 2)), prod_exponent.size());
548 prod_exponent =
bv_utils.select(product_is_zero, min_exponent, prod_exponent);
556 const std::size_t prod_width = prod_fraction.size();
557 const std::size_t c_pad = prod_width - frac_size;
564 bvt exp_diff =
bv_utils.sub(prod_exponent, c_exponent);
565 literalt c_bigger = exp_diff.back();
567 bvt bigger_exp =
bv_utils.select(c_bigger, c_exponent, prod_exponent);
568 bvt big_frac =
bv_utils.select(c_bigger, c_fraction, prod_fraction);
569 bvt small_frac =
bv_utils.select(c_bigger, prod_fraction, c_fraction);
580 small_shifted[0] =
prop.lor(small_shifted[0], sticky_bit);
582 bvt big_ext =
bv_utils.zero_extension(big_padded, big_padded.size() + 2);
584 bv_utils.zero_extension(small_shifted, small_shifted.size() + 2);
587 bvt sum =
bv_utils.add_sub(big_ext, small_ext, subtract_lit);
589 literalt fraction_sign = sum.back();
595 bv_utils.sign_extension(bigger_exp, bigger_exp.size() + 1),
596 bv_utils.build_constant(2, bigger_exp.size() + 1));
600 prop.lselect(c_bigger, unpacked_add.
sign, prod_sign), fraction_sign);
612 prop.lxor(prod_sign, unpacked_add.
sign))});
620 literalt infinity_sign =
prop.lselect(prod_inf, prod_sign, unpacked_add.
sign);
623 prop.lor(prod_sign, unpacked_add.
sign),
624 prop.land(prod_sign, unpacked_add.
sign));
629 prop.lselect(result.
zero, zero_sign, add_sub_sign));
640 std::size_t div_width=unpacked1.
fraction.size()*2+1;
644 fraction1.reserve(div_width);
645 while(fraction1.size()<div_width)
663 result.
fraction.begin(), have_remainder);
674 bvt added_exponent=
bv_utils.sub(exponent1, exponent2);
679 bv_utils.build_constant(
spec.f, added_exponent.size()));
789 prop.lselect(signs_different,
796 and_bv.push_back(less_than3);
797 and_bv.push_back(!bitwise_equal);
798 and_bv.push_back(!both_zero);
799 and_bv.push_back(!NaN);
801 return prop.land(and_bv);
806 or_bv.push_back(less_than3);
807 or_bv.push_back(both_zero);
808 or_bv.push_back(bitwise_equal);
810 return prop.land(
prop.lor(or_bv), !NaN);
820 prop.lor(bitwise_equal, both_zero),
834 all_but_sign.resize(all_but_sign.size()-1);
835 return bv_utils.is_zero(all_but_sign);
844 return prop.land(and_bv);
872 return prop.land(and_bv);
886 exponent.erase(exponent.begin(), exponent.begin()+
spec.f);
889 exponent.resize(
spec.e);
891 return bv_utils.is_all_ones(exponent);
899 exponent.erase(exponent.begin(), exponent.begin()+
spec.f);
902 exponent.resize(
spec.e);
922 bvt new_fraction=
prop.new_variables(fraction.size());
923 bvt new_exponent=
prop.new_variables(exponent.size());
926 for(std::size_t i=0; i<fraction.size(); i++)
931 for(std::size_t j=0; j<i; j++)
933 !fraction[fraction.size()-1-j]);
936 equal.push_back(fraction[fraction.size()-1-i]);
943 bv_utils.cond_implies_equal(shift, shifted_fraction, new_fraction);
946 bvt adjustment=
bv_utils.build_constant(-i, exponent.size());
947 bvt added_exponent=
bv_utils.add(exponent, adjustment);
948 bv_utils.cond_implies_equal(shift, added_exponent, new_exponent);
956 bv_utils.cond_implies_equal(fraction_all_zero, zero_fraction, new_fraction);
958 fraction=new_fraction;
959 exponent=new_exponent;
971 bv_utils.sign_extension(exponent, std::max(depth, exponent.size() + 1));
973 bvt exponent_delta=
bv_utils.zeros(exponent.size());
976 std::size_t max_distance = 1 << (depth - 1);
978 if(fraction.size() < max_distance)
979 fraction =
bv_utils.zero_extension(fraction, max_distance);
981 for(
int d=depth-1; d>=0; d--)
983 std::size_t distance=(1<<d);
986 fraction.size() >= distance,
"fraction must be larger or equal distance");
989 const bvt prefix=
bv_utils.extract_msb(fraction, distance);
998 bv_utils.select(prefix_is_zero, shifted, fraction);
1002 d < (
signed)exponent_delta.size(),
1003 "depth must be smaller than exponent size");
1004 exponent_delta[d]=prefix_is_zero;
1007 exponent=
bv_utils.sub(exponent, exponent_delta);
1032 exponent=
bv_utils.sign_extension(exponent, exponent.size() + 1);
1036 bv_utils.build_constant(-
bias+1, exponent.size()), exponent);
1046 if(fraction.size() < (
spec.f + 3))
1054 bvt denormalisedFraction=fraction;
1057 denormalisedFraction =
1059 denormalisedFraction[0]=
prop.lor(denormalisedFraction[0], sticky_bit);
1064 denormalisedFraction,
1071 bv_utils.shift(fraction, bv_utilst::LRIGHT, distance),
1094 if(aligned_exponent.size()<exponent_bits)
1098 bv_utils.sign_extension(aligned_exponent, exponent_bits);
1126 const std::size_t dest_bits,
1128 const bvt &fraction)
1133 std::size_t extra_bits=fraction.size()-dest_bits;
1144 bvt tail=
bv_utils.extract(fraction, 0, extra_bits-2);
1145 sticky_bit=
prop.lor(tail);
1150 extra_bits >= 1,
"the extra bits include at least the rounding bit");
1151 literalt rounding_bit=fraction[extra_bits-1];
1154 literalt rounding_least=fraction[extra_bits];
1158 prop.land(rounding_bit,
1159 prop.lor(rounding_least, sticky_bit));
1164 prop.lor(rounding_bit, sticky_bit));
1169 prop.lor(rounding_bit, sticky_bit));
1176 literalt round_to_away = rounding_bit;
1185 prop.new_variable())))));
1191 std::size_t fraction_size=
spec.f+1;
1194 if(result.
fraction.size()<fraction_size)
1197 std::size_t padding=fraction_size-result.
fraction.size();
1204 result.
fraction.size() == fraction_size,
1205 "sizes should be equal as result.fraction was zero-padded");
1207 else if(result.
fraction.size()==fraction_size)
1213 std::size_t extra_bits=result.
fraction.size()-fraction_size;
1216 "the extra bits should at least include the rounding bit");
1227 result.
fraction.size() == fraction_size,
1228 "sizes should be equal as extra bits were chopped off from "
1249 literalt new_integer_part=
prop.lor(integer_part1, integer_part0);
1252 result.
fraction.back()=new_integer_part;
1278 prop.lor(overflow, subnormal_to_normal));
1306 spec.max_exponent()-
spec.bias(), old_exponent.size());
1312 !
bv_utils.signed_less_than(old_exponent, max_exponent),
1326 prop.land(exponent_too_large, !overflow_to_inf);
1329 bvt largest_normal_exponent=
1342 prop.land(exponent_too_large,
1374 for(std::size_t i=0; i<result.
exponent.size(); i++)
1434 result.resize(
spec.width());
1438 result[result.size()-1]=
1445 for(std::size_t i=0; i<
spec.f; i++)
1446 result[i]=
prop.land(src.
fraction[i], !infinity_or_NaN);
1448 result[0]=
prop.lor(result[0], src.
NaN);
1451 for(std::size_t i=0; i<
spec.e; i++)
1463 for(std::size_t i=0; i<src.size(); i++)
1464 int_value+=
power(2, i)*
prop.l_get(src[i]).is_true();
1468 result.
unpack(int_value);
1482 for(std::size_t stage=0; stage<dist.size(); stage++)
1490 if(d<=result.size())
1491 lost_bits=
bv_utils.extract(result, 0, d-1);
1496 prop.land(dist[stage],
prop.lor(lost_bits)),
1499 result=
bv_utils.select(dist[stage], tmp, result);
unbiased_floatt rounder(const unbiased_floatt &)
bvt to_integer(const bvt &src, std::size_t int_width, bool is_signed)
literalt is_NaN(const bvt &)
virtual void normalization_shift(bvt &fraction, bvt &exponent)
normalize fraction/exponent pair returns 'zero' if fraction is zero
bvt debug2(const bvt &op0, const bvt &op1)
virtual bvt rem(const bvt &src1, const bvt &src2)
bvt round_to_integral(const bvt &)
literalt is_plus_inf(const bvt &)
ieee_float_valuet get(const bvt &) const
literalt is_infinity(const bvt &)
void set_rounding_mode(const bvt &)
void round_exponent(unbiased_floatt &result)
void round_fraction(unbiased_floatt &result)
bvt sticky_right_shift(const bvt &op, const bvt &dist, literalt &sticky)
unbiased_floatt unpack(const bvt &)
bvt from_unsigned_integer(const bvt &)
virtual bvt mul(const bvt &src1, const bvt &src2)
bvt debug1(const bvt &op0, const bvt &op1)
bvt add_bias(const bvt &exponent)
bvt round_and_pack(const unbiased_floatt &)
bvt subtract_exponents(const unbiased_floatt &src1, const unbiased_floatt &src2)
Subtracts the exponents.
bvt get_fraction(const bvt &)
Gets the fraction without hidden bit in a floating-point bit-vector src.
literalt is_minus_inf(const bvt &)
literalt fraction_rounding_decision(const std::size_t dest_bits, const literalt sign, const bvt &fraction)
rounding decision for fraction using sticky bit
bvt get_exponent(const bvt &)
Gets the unbiased exponent in a floating-point bit-vector.
void denormalization_shift(bvt &fraction, bvt &exponent)
make sure exponent is not too small; the exponent is unbiased
bvt to_unsigned_integer(const bvt &src, std::size_t int_width)
bvt build_constant(const ieee_float_valuet &)
virtual bvt div(const bvt &src1, const bvt &src2)
literalt exponent_all_zeros(const bvt &)
literalt fraction_all_zeros(const bvt &)
bvt fma(const bvt &multiply_lhs, const bvt &multiply_rhs, const bvt &addend)
Fused multiply-add: round(multiply_lhs * multiply_rhs + addend) with a single rounding step.
bvt from_signed_integer(const bvt &)
literalt is_zero(const bvt &)
bvt sub(const bvt &src1, const bvt &src2)
bvt sub_bias(const bvt &exponent)
bvt limit_distance(const bvt &dist, mp_integer limit)
Limits the shift distance.
bvt conversion(const bvt &src, const ieee_float_spect &dest_spec)
bvt pack(const biased_floatt &)
virtual bvt add_sub(const bvt &src1, const bvt &src2, bool subtract)
static literalt sign_bit(const bvt &src)
literalt exponent_all_ones(const bvt &)
bvt to_signed_integer(const bvt &src, std::size_t int_width)
literalt is_normal(const bvt &)
literalt relation(const bvt &src1, relt rel, const bvt &src2)
rounding_mode_bitst rounding_mode_bits
biased_floatt bias(const unbiased_floatt &)
takes an unbiased float, and applies the bias
An IEEE 754 floating-point value, including specificiation.
const mp_integer & get_fraction() const
void unpack(const mp_integer &)
const mp_integer & get_exponent() const
An IEEE 754 value plus a rounding mode, enabling operations with rounding on values.
std::vector< literalt > bvt
literalt const_literal(bool value)
#define CHECK_RETURN(CONDITION)
#define UNREACHABLE
This should be used to mark dead code.
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
bool is_signed(const typet &t)
Convenience function – is the type signed?