Skip to content

Commit

Permalink
Follow cnl an test rounding modes (hbe72#6)
Browse files Browse the repository at this point in the history
Follow CNL development head and test rounding modes
  • Loading branch information
hbe72 authored Nov 13, 2018
1 parent 2f808d6 commit 18b2f90
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ExternalProject_Add(
Cnl
PREFIX ${CMAKE_BINARY_DIR}/Cnl
GIT_REPOSITORY "https://github.com/johnmcfarlane/cnl.git"
GIT_TAG "d5ae23f62075c74cd453723819ff6e2174ff83cb"
GIT_TAG "68b13242269380027de72849b0eb4393a5a61e43"
GIT_SHALLOW 1
GIT_PROGRESS 1
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_INSTALL_PREFIX=${Cnl_DESTDIR} -DINT128=${INT128} -DSTD=${STD} -DEXCEPTIONS=${EXCEPTIONS}
Expand Down
6 changes: 3 additions & 3 deletions include/dsp/dsp_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ inline T1 divides<T1, T2>::operator()(const T1& a, const T2& b) const
template <>
inline q4_20 divides<q4_20,q4_20>::operator()(const q4_20& a, const q4_20& b) const
{
return static_cast<q4_20>(cnl::make_fixed_point(cnl::make_fractional(a, b)));
return static_cast<q4_20>(cnl::make_fixed_point(cnl::make_fraction(a, b)));
}

#if defined(CNL_INT128_ENABLED)
template <>
inline q8_40 divides<q8_40,q8_40>::operator()(const q8_40& a, const q8_40& b) const
{
return static_cast<q8_40>(cnl::make_fixed_point(cnl::make_fractional(a, b)));
return static_cast<q8_40>(cnl::make_fixed_point(cnl::make_fraction(a, b)));
}

template <>
inline q8_40 divides<q8_40,q4_20>::operator()(const q8_40& a, const q4_20& b) const
{
return static_cast<q8_40>(cnl::make_fixed_point(cnl::make_fractional(a, b)));
return static_cast<q8_40>(cnl::make_fixed_point(cnl::make_fraction(a, b)));
}
#endif

Expand Down
73 changes: 34 additions & 39 deletions test/basic_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include <dsp/dsp_types.h>
#include <dsp/dsp_math.h>

namespace {
#include <cnl/_impl/type_traits/assert_same.h>
#include <cnl/_impl/type_traits/identical.h>
using cnl::_impl::assert_same;
using cnl::_impl::identical;

TEST(basic_math, floor)
{
Expand Down Expand Up @@ -73,24 +76,11 @@ TEST(basic_math, rounding_fixed_point)
EXPECT_EQ(b, 0.5);
}

template<
int IntegerDigits,
class RoundingTag = cnl::nearest_rounding_tag,
class Narrowest = int>
using rounding_elastic_integer = cnl::rounding_integer<
cnl::elastic_integer<
IntegerDigits,
Narrowest>,
RoundingTag>;

template<int Digits, int Exponent = 0, class RoundingTag = cnl::nearest_rounding_tag, class Narrowest = signed>
using rounding_elastic_number = cnl::fixed_point<rounding_elastic_integer<Digits, RoundingTag, Narrowest>, Exponent>;

TEST(basic_math, rounding_elastic_number)
{

using q4_4 = rounding_elastic_number<8, -4>;
using q4_1 = rounding_elastic_number<4, -1>;
using q4_4 = cdsp::rounding_elastic_number<8, -4>;
using q4_1 = cdsp::rounding_elastic_number<4, -1>;

q4_4 a(0.4375);
q4_1 b = a;
Expand All @@ -101,39 +91,44 @@ TEST(basic_math, rounding_elastic_number)
/// Same as above but as compile time test
namespace test_rounding_elastic_number
{
static constexpr auto a = rounding_elastic_number<8, -4>{0.4375};
static constexpr auto b = static_cast<rounding_elastic_number<4, -1>>(a);
static_assert(identical(rounding_elastic_number<4, -1>{0.5}, b),
static constexpr auto a = cdsp::rounding_elastic_number<8, -4>{0.4375};
static constexpr auto b = static_cast<cdsp::rounding_elastic_number<4, -1>>(a);
static_assert(identical(cdsp::rounding_elastic_number<4, -1>{0.5}, b),
"rounding_elastic_number");
static_assert(identical(rounding_elastic_integer<8>{7}, cnl::to_rep(a)),
static_assert(identical(cdsp::rounding_elastic_integer<8>{7}, to_rep(a)),
"rounding_elastic_number");
static_assert(identical(rounding_elastic_integer<4>{1}, cnl::to_rep(b)),
static_assert(identical(cdsp::rounding_elastic_integer<4>{1}, to_rep(b)),
"rounding_elastic_number");
}

namespace test_rounding_should_stay_under_64_bits
{
static constexpr auto c = static_cast<rounding_elastic_number<24, -20>>
(rounding_elastic_number<48, -40>{0.21875});
static_assert(identical(rounding_elastic_number<24, -20>{0.21875}, c),
static constexpr auto c = static_cast<cdsp::rounding_elastic_number<24, -20>>
(cdsp::rounding_elastic_number<48, -40>{0.21875});
static_assert(identical(cdsp::rounding_elastic_number<24, -20>{0.21875}, c),
"rounding_elastic_number assignment");
}

namespace test_rounding_from_elastic_number
namespace test_convert_nearest_rounding_elastic_number
{
static constexpr auto a = cnl::elastic_number<8, -4>{0.4375};
static constexpr auto b = static_cast<rounding_elastic_number<4, -1>>(a);
static constexpr auto c = static_cast<cnl::elastic_number<4, -1>>(a);
static_assert(identical(rounding_elastic_number<4, -1>{0.5}, b),
"conversion from elastic number to smaller rounding elastic number");
static_assert(identical(cnl::elastic_number<4, -1>{0.0}, c),
"conversion from elastic number to smaller elastic number");
static_assert(identical(cnl::elastic_integer<8>{7}, cnl::to_rep(a)),
"conversion from elastic number to smaller rounding elastic number, checking the underlying value");
static_assert(identical(rounding_elastic_integer<4>{1}, cnl::to_rep(b)),
"conversion from elastic number to rounding elastic number, checking the underlying value");
static_assert(identical(cnl::elastic_integer<4>{0}, cnl::to_rep(c)),
"conversion from elastic number to smaller elastic number, checking underlying value");
static constexpr auto a = cnl::elastic_number<8, -4>{0.3125};
static constexpr auto b = cnl::convert<cnl::nearest_rounding_tag, cnl::elastic_number<4, -1>, cnl::elastic_number<8, -4>>{}(a);
static_assert(identical(cnl::elastic_number<4, -1>{0.5}, b),
"cnl::convert<nearest_rounding_tag, elastic_number, elastic_number>");

static constexpr auto c = cnl::convert<cnl::nearest_rounding_tag, cnl::elastic_number<4, -2>, cnl::elastic_number<8, -4>>{}(a);
static_assert(identical(cnl::elastic_number<4, -2>{0.25}, c),
"cnl::convert<nearest_rounding_tag, elastic_number, elastic_number>");
}

}
namespace test_convert_nearest_rounding_fixed_point
{
static constexpr auto a = cnl::fixed_point<int, -4>{0.3125};
static constexpr auto b = cnl::convert<cnl::nearest_rounding_tag, cnl::fixed_point<int, -1>, cnl::fixed_point<int, -4>>{}(a);
static_assert(identical(cnl::fixed_point<int, -1>{0.5}, b),
"cnl::convert<nearest_rounding_tag, fixed_point, fixed_point>");

static constexpr auto c = cnl::convert<cnl::nearest_rounding_tag, cnl::fixed_point<int, -2>, cnl::fixed_point<int, -4>>{}(a);
static_assert(identical(cnl::fixed_point<int, -2>{0.25}, c),
"cnl::convert<nearest_rounding_tag, fixed_point, fixed_point>");
}

0 comments on commit 18b2f90

Please sign in to comment.