Skip to content

Commit

Permalink
Rounding integer (hbe72#4)
Browse files Browse the repository at this point in the history
Using rounding_elastic_number
  • Loading branch information
hbe72 authored Jul 23, 2018
1 parent 4d7085f commit d69fe2d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 31 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ include("cmake/common.cmake")
set(Cnl_DESTDIR ${CMAKE_BINARY_DIR}/install)
file(MAKE_DIRECTORY ${Cnl_DESTDIR})

message("CXX COMPILER = ${CMAKE_CXX_COMPILER}")
message("C COMPILER = ${CMAKE_C_COMPILER}")
message("INT128 = ${INT128}")
message("STD = ${STD}")
message("EXCEPTIONS = ${EXCEPTIONS}")

ExternalProject_Add(
Cnl
PREFIX ${CMAKE_BINARY_DIR}/Cnl
GIT_REPOSITORY "https://github.com/johnmcfarlane/cnl.git"
GIT_REPOSITORY "https://github.com/hbe72/cnl.git"
GIT_TAG "develop"
GIT_SHALLOW 1
GIT_PROGRESS 1
UPDATE_COMMAND ""
CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_INSTALL_PREFIX=${Cnl_DESTDIR}
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}
)

# runs a suite of compile-time tests using `static_assert`
Expand Down
2 changes: 1 addition & 1 deletion include/dsp/complex_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class complex_vector
/// Returns the size of vector
std::size_t size() const;
/// resizes the vector
void resize(std::size_t size, complex<T> init = complex<T>(0, 0));
void resize(std::size_t size, complex<T> init = complex<T>(0., 0.));
/// reserves space for the vector
void reserve(std::size_t size);
/// Pushes value back to the vector
Expand Down
2 changes: 1 addition & 1 deletion include/dsp/dsp_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ T floor(const T a)
template<class Rep, int Exponent>
inline cnl::fixed_point<Rep, Exponent> floor(const cnl::fixed_point<Rep, Exponent>& x)
{
return cnl::fixed_point<Rep, Exponent>((cnl::to_rep(x)) >> -Exponent);
return cnl::floor(x);
}

template<typename T>
Expand Down
24 changes: 21 additions & 3 deletions include/dsp/dsp_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
#if !defined(CDSP_TYPES)
#define CDSP_TYPES

#include <cnl.h>
#include <cnl/all.h>

namespace cdsp
{
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>;
}

// Elastic fixed point types equivalent to Cirrus ADSP2
typedef cnl::elastic_number<24, -20> q4_20;
typedef cnl::elastic_number<48, -40> q8_40;
typedef cdsp::rounding_elastic_number<24, -20> q4_20;
typedef cdsp::rounding_elastic_number<48, -40> q8_40;
//typedef cnl::elastic_number<24, -20> q4_20;
//typedef cnl::elastic_number<48, -40> q8_40;

#endif //CDSP_TYPES
2 changes: 1 addition & 1 deletion include/dsp/fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void real_fft_postprocess(complex_vector<T>& out)
complex<T> x2(xPlus.imag(), xMinus.real());

complex<T> xa = x1 + conj(exp[k * stride] * x2);
complex<T> xb(out[k].real() - out[k].imag(), T(0));
complex<T> xb(out[k].real() - out[k].imag(), T(0.));
out.set_at(k, xa);
out.set_at(r, xb);

Expand Down
27 changes: 14 additions & 13 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
include("../cmake/common.cmake")

# DSP tests
set(sample_cdsp_sources
basic_math.cpp
biquad.cpp
biquad_cascade.cpp
complex.cpp
complex_vector.cpp
fft.cpp
stft.cpp
trig.cpp
virtual_float.cpp
)

######################################################################
# add_cnl_dependency
function(add_cnl_dependency target)
Expand Down Expand Up @@ -46,19 +59,6 @@ function(add_gtest_dependency target)
add_dependencies("${target}" GoogleTest)
endfunction(add_gtest_dependency)


# DSP tests
set(sample_cdsp_sources
basic_math.cpp
biquad.cpp
biquad_cascade.cpp
complex.cpp
complex_vector.cpp
fft.cpp
stft.cpp
trig.cpp
virtual_float.cpp)

include(CTest)
add_custom_target(Tests)

Expand All @@ -67,6 +67,7 @@ foreach(source ${sample_cdsp_sources})
string(REPLACE "/" "-" target "Test-${stripped}")
add_executable(${target} ${source})
add_test("${target}" "${target}")
set_target_properties("${target}" PROPERTIES COMPILE_FLAGS "${COMMON_CXX_FLAGS}")

add_gtest_dependency("${target}")
add_cnl_dependency("${target}")
Expand Down
85 changes: 75 additions & 10 deletions test/basic_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <dsp/dsp_types.h>
#include <dsp/dsp_math.h>

namespace {

TEST(basic_math, floor)
{
std::vector<float> nbrs {
Expand Down Expand Up @@ -42,33 +44,96 @@ TEST(basic_math, floor)
<< "index: " << index << std::endl;
}
}


}

TEST(basic_math, div)
{

q4_20 a(1.0);
q4_20 b(2.0);
q4_20 c1 = cnl::make_fixed_point(cnl::make_fractional(b,a));
q4_20 c2 = cnl::make_fixed_point(cnl::make_fractional(a,b));
q4_20 c1 = cdsp::math::divides<q4_20,q4_20>()(b,a);
q4_20 c2 = cdsp::math::divides<q4_20,q4_20>()(a,b);

EXPECT_EQ(static_cast<float>(c1), 2.0);
EXPECT_EQ(static_cast<float>(c2), 0.5);
}

template<class Rep = int, int Exponent = 0, class RoundingTag = cnl::nearest_rounding_tag>
using rounding_fixed_point = cnl::fixed_point<cnl::rounding_integer<Rep, RoundingTag>, Exponent>;

/*
TEST(basic_math, round)
TEST(basic_math, rounding_fixed_point)
{

using q4_4 = rsfp<4, 4>;
using q4_1 = rsfp<4, 1>;
q4_4 a = 0.4375;
using q28_4 = rounding_fixed_point<std::int32_t, -4>;
using q31_1 = rounding_fixed_point<std::int32_t, -1>;

q28_4 a(0.4375);
q31_1 b = a;
EXPECT_EQ(a, 0.4375);
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>;

q4_4 a(0.4375);
q4_1 b = a;
EXPECT_EQ(a, 0.4375);
EXPECT_EQ(b, 0.5);
}

/// 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),
"rounding_elastic_number");
static_assert(identical(rounding_elastic_integer<8>{7}, cnl::to_rep(a)),
"rounding_elastic_number");
static_assert(identical(rounding_elastic_integer<4>{1}, cnl::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),
"rounding_elastic_number assignment");
}
*/

namespace test_rounding_from_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");
}

}

0 comments on commit d69fe2d

Please sign in to comment.