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
|
/*******************************************************************\
Module: Unit tests for util/ieee_float
Author: Daniel Kroening, dkr@amazon.com
\*******************************************************************/
#include <util/ieee_float.h>
#include <testing-utils/use_catch.h>
#include <limits>
TEST_CASE("Make an IEEE 754 one", "[core][util][ieee_float]")
{
auto spec = ieee_float_spect::single_precision();
REQUIRE(ieee_floatt::one(spec) == 1);
}
TEST_CASE("round_to_integral", "[unit][util][ieee_float]")
{
auto from_double = [](double d) -> ieee_float_valuet
{
ieee_float_valuet v;
v.from_double(d);
return v;
};
auto round_to_integral =
[](ieee_float_valuet op, ieee_floatt::rounding_modet rm) {
return ieee_floatt{op, rm}.round_to_integral();
};
const auto dp = ieee_float_spect::double_precision();
const auto NaN = ieee_float_valuet::NaN(dp);
const auto plus_inf = ieee_float_valuet::plus_infinity(dp);
const auto minus_inf = ieee_float_valuet::minus_infinity(dp);
const auto dmax = std::numeric_limits<double>::max();
const auto up = ieee_floatt::ROUND_TO_PLUS_INF;
REQUIRE(round_to_integral(NaN, up) == NaN);
REQUIRE(round_to_integral(plus_inf, up) == plus_inf);
REQUIRE(round_to_integral(minus_inf, up) == minus_inf);
REQUIRE(round_to_integral(from_double(0), up) == 0);
REQUIRE(round_to_integral(from_double(-0.0), up) == -0.0);
REQUIRE(round_to_integral(from_double(1), up) == 1);
REQUIRE(round_to_integral(from_double(0.1), up) == 1);
REQUIRE(round_to_integral(from_double(-0.1), up) == -0.0);
REQUIRE(round_to_integral(from_double(0.5), up) == 1);
REQUIRE(round_to_integral(from_double(0.49999999), up) == 1);
REQUIRE(round_to_integral(from_double(0.500000001), up) == 1);
REQUIRE(round_to_integral(from_double(10.1), up) == 11);
REQUIRE(round_to_integral(from_double(-10.1), up) == -10);
REQUIRE(round_to_integral(from_double(0x1.0p+52), up) == 0x1.0p+52);
REQUIRE(round_to_integral(from_double(dmax), up) == dmax);
const auto down = ieee_floatt::ROUND_TO_MINUS_INF;
REQUIRE(round_to_integral(NaN, down) == NaN);
REQUIRE(round_to_integral(plus_inf, down) == plus_inf);
REQUIRE(round_to_integral(minus_inf, down) == minus_inf);
REQUIRE(round_to_integral(from_double(0), down) == 0);
REQUIRE(round_to_integral(from_double(-0.0), down) == -0.0);
REQUIRE(round_to_integral(from_double(1), down) == 1);
REQUIRE(round_to_integral(from_double(0.1), down) == 0);
REQUIRE(round_to_integral(from_double(-0.1), down) == -1);
REQUIRE(round_to_integral(from_double(0.5), down) == 0);
REQUIRE(round_to_integral(from_double(0.49999999), down) == 0);
REQUIRE(round_to_integral(from_double(0.500000001), down) == 0);
REQUIRE(round_to_integral(from_double(10.1), down) == 10);
REQUIRE(round_to_integral(from_double(-10.1), down) == -11);
REQUIRE(round_to_integral(from_double(0x1.0p+52), down) == 0x1.0p+52);
REQUIRE(round_to_integral(from_double(dmax), down) == dmax);
const auto even = ieee_floatt::ROUND_TO_EVEN;
REQUIRE(round_to_integral(NaN, even) == NaN);
REQUIRE(round_to_integral(plus_inf, even) == plus_inf);
REQUIRE(round_to_integral(minus_inf, even) == minus_inf);
REQUIRE(round_to_integral(from_double(0), even) == 0);
REQUIRE(round_to_integral(from_double(-0.0), even) == -0.0);
REQUIRE(round_to_integral(from_double(1), even) == 1);
REQUIRE(round_to_integral(from_double(0.1), even) == 0);
REQUIRE(round_to_integral(from_double(-0.1), even) == -0.0);
REQUIRE(round_to_integral(from_double(0.5), even) == 0);
REQUIRE(round_to_integral(from_double(0.49999999), even) == 0);
REQUIRE(round_to_integral(from_double(0.500000001), even) == 1);
REQUIRE(round_to_integral(from_double(10.1), even) == 10);
REQUIRE(round_to_integral(from_double(-10.1), even) == -10);
REQUIRE(round_to_integral(from_double(0x1.0p+52), even) == 0x1.0p+52);
REQUIRE(round_to_integral(from_double(dmax), even) == dmax);
const auto zero = ieee_floatt::ROUND_TO_ZERO;
REQUIRE(round_to_integral(NaN, zero) == NaN);
REQUIRE(round_to_integral(plus_inf, zero) == plus_inf);
REQUIRE(round_to_integral(minus_inf, zero) == minus_inf);
REQUIRE(round_to_integral(from_double(0), zero) == 0);
REQUIRE(round_to_integral(from_double(-0.0), zero) == -0.0);
REQUIRE(round_to_integral(from_double(1), zero) == 1);
REQUIRE(round_to_integral(from_double(0.1), zero) == 0);
REQUIRE(round_to_integral(from_double(-0.1), zero) == -0.0);
REQUIRE(round_to_integral(from_double(0.5), zero) == 0);
REQUIRE(round_to_integral(from_double(0.49999999), zero) == 0);
REQUIRE(round_to_integral(from_double(0.500000001), zero) == 0);
REQUIRE(round_to_integral(from_double(10.1), zero) == 10);
REQUIRE(round_to_integral(from_double(-10.1), zero) == -10);
REQUIRE(round_to_integral(from_double(0x1.0p+52), zero) == 0x1.0p+52);
REQUIRE(round_to_integral(from_double(dmax), zero) == dmax);
const auto away = ieee_floatt::ROUND_TO_AWAY;
REQUIRE(round_to_integral(NaN, away) == NaN);
REQUIRE(round_to_integral(plus_inf, away) == plus_inf);
REQUIRE(round_to_integral(minus_inf, away) == minus_inf);
REQUIRE(round_to_integral(from_double(0), away) == 0);
REQUIRE(round_to_integral(from_double(-0.0), away) == -0.0);
REQUIRE(round_to_integral(from_double(1), away) == 1);
REQUIRE(round_to_integral(from_double(0.1), away) == 0);
REQUIRE(round_to_integral(from_double(-0.1), away) == -0.0);
REQUIRE(round_to_integral(from_double(0.5), away) == 1);
REQUIRE(round_to_integral(from_double(0.49999999), away) == 0);
REQUIRE(round_to_integral(from_double(0.500000001), away) == 1);
REQUIRE(round_to_integral(from_double(10.1), away) == 10);
REQUIRE(round_to_integral(from_double(-10.1), away) == -10);
REQUIRE(round_to_integral(from_double(0x1.0p+52), away) == 0x1.0p+52);
REQUIRE(round_to_integral(from_double(dmax), away) == dmax);
}
|