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);
545 const std::size_t prod_width = prod_fraction.size();
546 const std::size_t c_pad = prod_width - frac_size;
553 bvt exp_diff =
bv_utils.sub(prod_exponent, c_exponent);
554 literalt c_bigger = exp_diff.back();
556 bvt bigger_exp =
bv_utils.select(c_bigger, c_exponent, prod_exponent);
557 bvt big_frac =
bv_utils.select(c_bigger, c_fraction, prod_fraction);
558 bvt small_frac =
bv_utils.select(c_bigger, prod_fraction, c_fraction);
569 small_shifted[0] =
prop.lor(small_shifted[0], sticky_bit);
571 bvt big_ext =
bv_utils.zero_extension(big_padded, big_padded.size() + 2);
573 bv_utils.zero_extension(small_shifted, small_shifted.size() + 2);
576 bvt sum =
bv_utils.add_sub(big_ext, small_ext, subtract_lit);
578 literalt fraction_sign = sum.back();
584 bv_utils.sign_extension(bigger_exp, bigger_exp.size() + 1),
585 bv_utils.build_constant(2, bigger_exp.size() + 1));
589 prop.lselect(c_bigger, unpacked_add.
sign, prod_sign), fraction_sign);
601 prop.lxor(prod_sign, unpacked_add.
sign))});
609 literalt infinity_sign =
prop.lselect(prod_inf, prod_sign, unpacked_add.
sign);
612 prop.lor(prod_sign, unpacked_add.
sign),
613 prop.land(prod_sign, unpacked_add.
sign));
618 prop.lselect(result.
zero, zero_sign, add_sub_sign));
629 std::size_t div_width=unpacked1.
fraction.size()*2+1;
633 fraction1.reserve(div_width);
634 while(fraction1.size()<div_width)
652 result.
fraction.begin(), have_remainder);
663 bvt added_exponent=
bv_utils.sub(exponent1, exponent2);
668 bv_utils.build_constant(
spec.f, added_exponent.size()));
778 prop.lselect(signs_different,
785 and_bv.push_back(less_than3);
786 and_bv.push_back(!bitwise_equal);
787 and_bv.push_back(!both_zero);
788 and_bv.push_back(!NaN);
790 return prop.land(and_bv);
795 or_bv.push_back(less_than3);
796 or_bv.push_back(both_zero);
797 or_bv.push_back(bitwise_equal);
799 return prop.land(
prop.lor(or_bv), !NaN);
809 prop.lor(bitwise_equal, both_zero),
823 all_but_sign.resize(all_but_sign.size()-1);
824 return bv_utils.is_zero(all_but_sign);
833 return prop.land(and_bv);
861 return prop.land(and_bv);
875 exponent.erase(exponent.begin(), exponent.begin()+
spec.f);
878 exponent.resize(
spec.e);
880 return bv_utils.is_all_ones(exponent);
888 exponent.erase(exponent.begin(), exponent.begin()+
spec.f);
891 exponent.resize(
spec.e);
911 bvt new_fraction=
prop.new_variables(fraction.size());
912 bvt new_exponent=
prop.new_variables(exponent.size());
915 for(std::size_t i=0; i<fraction.size(); i++)
920 for(std::size_t j=0; j<i; j++)
922 !fraction[fraction.size()-1-j]);
925 equal.push_back(fraction[fraction.size()-1-i]);
932 bv_utils.cond_implies_equal(shift, shifted_fraction, new_fraction);
935 bvt adjustment=
bv_utils.build_constant(-i, exponent.size());
936 bvt added_exponent=
bv_utils.add(exponent, adjustment);
937 bv_utils.cond_implies_equal(shift, added_exponent, new_exponent);
945 bv_utils.cond_implies_equal(fraction_all_zero, zero_fraction, new_fraction);
947 fraction=new_fraction;
948 exponent=new_exponent;
960 bv_utils.sign_extension(exponent, std::max(depth, exponent.size() + 1));
962 bvt exponent_delta=
bv_utils.zeros(exponent.size());
965 std::size_t max_distance = 1 << (depth - 1);
967 if(fraction.size() < max_distance)
968 fraction =
bv_utils.zero_extension(fraction, max_distance);
970 for(
int d=depth-1; d>=0; d--)
972 std::size_t distance=(1<<d);
975 fraction.size() >= distance,
"fraction must be larger or equal distance");
978 const bvt prefix=
bv_utils.extract_msb(fraction, distance);
987 bv_utils.select(prefix_is_zero, shifted, fraction);
991 d < (
signed)exponent_delta.size(),
992 "depth must be smaller than exponent size");
993 exponent_delta[d]=prefix_is_zero;
996 exponent=
bv_utils.sub(exponent, exponent_delta);
1021 exponent=
bv_utils.sign_extension(exponent, exponent.size() + 1);
1025 bv_utils.build_constant(-
bias+1, exponent.size()), exponent);
1035 if(fraction.size() < (
spec.f + 3))
1043 bvt denormalisedFraction=fraction;
1046 denormalisedFraction =
1048 denormalisedFraction[0]=
prop.lor(denormalisedFraction[0], sticky_bit);
1053 denormalisedFraction,
1060 bv_utils.shift(fraction, bv_utilst::LRIGHT, distance),
1083 if(aligned_exponent.size()<exponent_bits)
1087 bv_utils.sign_extension(aligned_exponent, exponent_bits);
1115 const std::size_t dest_bits,
1117 const bvt &fraction)
1122 std::size_t extra_bits=fraction.size()-dest_bits;
1133 bvt tail=
bv_utils.extract(fraction, 0, extra_bits-2);
1134 sticky_bit=
prop.lor(tail);
1139 extra_bits >= 1,
"the extra bits include at least the rounding bit");
1140 literalt rounding_bit=fraction[extra_bits-1];
1143 literalt rounding_least=fraction[extra_bits];
1147 prop.land(rounding_bit,
1148 prop.lor(rounding_least, sticky_bit));
1153 prop.lor(rounding_bit, sticky_bit));
1158 prop.lor(rounding_bit, sticky_bit));
1165 literalt round_to_away = rounding_bit;
1174 prop.new_variable())))));
1180 std::size_t fraction_size=
spec.f+1;
1183 if(result.
fraction.size()<fraction_size)
1186 std::size_t padding=fraction_size-result.
fraction.size();
1193 result.
fraction.size() == fraction_size,
1194 "sizes should be equal as result.fraction was zero-padded");
1196 else if(result.
fraction.size()==fraction_size)
1202 std::size_t extra_bits=result.
fraction.size()-fraction_size;
1205 "the extra bits should at least include the rounding bit");
1216 result.
fraction.size() == fraction_size,
1217 "sizes should be equal as extra bits were chopped off from "
1238 literalt new_integer_part=
prop.lor(integer_part1, integer_part0);
1241 result.
fraction.back()=new_integer_part;
1267 prop.lor(overflow, subnormal_to_normal));
1295 spec.max_exponent()-
spec.bias(), old_exponent.size());
1301 !
bv_utils.signed_less_than(old_exponent, max_exponent),
1315 prop.land(exponent_too_large, !overflow_to_inf);
1318 bvt largest_normal_exponent=
1331 prop.land(exponent_too_large,
1363 for(std::size_t i=0; i<result.
exponent.size(); i++)
1423 result.resize(
spec.width());
1427 result[result.size()-1]=
1434 for(std::size_t i=0; i<
spec.f; i++)
1435 result[i]=
prop.land(src.
fraction[i], !infinity_or_NaN);
1437 result[0]=
prop.lor(result[0], src.
NaN);
1440 for(std::size_t i=0; i<
spec.e; i++)
1452 for(std::size_t i=0; i<src.size(); i++)
1453 int_value+=
power(2, i)*
prop.l_get(src[i]).is_true();
1457 result.
unpack(int_value);
1471 for(std::size_t stage=0; stage<dist.size(); stage++)
1479 if(d<=result.size())
1480 lost_bits=
bv_utils.extract(result, 0, d-1);
1485 prop.land(dist[stage],
prop.lor(lost_bits)),
1488 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?