From 0dc041b8401f16debb5207bcdf5b1bdecbca45b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Moni=C3=A8re?= Date: Thu, 14 Apr 2022 17:18:52 +0200 Subject: [PATCH] Fix test method to really using test vectors - Grow kn_i to 4 bits to pass the new tests. --- .../CCordicRotateConstexpr.hpp | 8 +-- .../CCordicRotateRom/CCordicRotateRom.hpp.in | 8 +-- sources/tb/cordic_rom_tb.cpp.in | 12 ++--- sources/tb/cordic_tb.cpp | 53 ++++++++++--------- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/sources/CCordicRotateConstexpr/CCordicRotateConstexpr.hpp b/sources/CCordicRotateConstexpr/CCordicRotateConstexpr.hpp index 02ab6c1..ac65840 100644 --- a/sources/CCordicRotateConstexpr/CCordicRotateConstexpr.hpp +++ b/sources/CCordicRotateConstexpr/CCordicRotateConstexpr.hpp @@ -54,7 +54,7 @@ public: static constexpr unsigned Out_I = In_I + 2; static constexpr unsigned nb_stages = Tnb_stages; - static constexpr unsigned kn_i = unsigned(kn_values[nb_stages - 1] * double(1U << 3)); // 3 bits are enough + static constexpr unsigned kn_i = unsigned(kn_values[nb_stages - 1] * double(1U << 4)); // 4 bits are enough static constexpr unsigned in_scale_factor = unsigned(1U << (In_W - In_I)); static constexpr unsigned out_scale_factor = unsigned(1U << (Out_W - Out_I)); @@ -62,7 +62,7 @@ public: static constexpr unsigned addr_length = CRomGeneratorConst::addr_length; static constexpr int64_t scale_cordic(int64_t in) { - return in * kn_i / 8U; + return in * kn_i / 16U; } #if !defined(__SYNTHESIS__) && defined(SOFTWARE) @@ -108,8 +108,8 @@ public: #endif static ap_int scale_cordic(const ap_int & in) { - const ap_int tmp = in * ap_uint<3>(kn_i); - return ap_int(tmp >> 3); + const ap_int tmp = in * ap_uint<4>(kn_i); + return ap_int(tmp >> 4); } static void cordic(const ap_int & re_in, const ap_int & im_in, diff --git a/sources/CCordicRotateRom/CCordicRotateRom.hpp.in b/sources/CCordicRotateRom/CCordicRotateRom.hpp.in index b1a4dc0..059ab99 100644 --- a/sources/CCordicRotateRom/CCordicRotateRom.hpp.in +++ b/sources/CCordicRotateRom/CCordicRotateRom.hpp.in @@ -59,7 +59,7 @@ public: static constexpr unsigned nb_stages = @CORDIC_STAGES@; static constexpr unsigned q = @CORDIC_Q@; - static constexpr uint64_t kn_i = uint64_t(kn_values[nb_stages - 1] * double(1U << 3)); // 3 bits are enough + static constexpr uint64_t kn_i = uint64_t(kn_values[nb_stages - 1] * double(1U << 4)); // 4 bits are enough static constexpr uint64_t in_scale_factor = uint64_t(1U << (In_W - In_I)); static constexpr uint64_t out_scale_factor = uint64_t(1U << (Out_W - Out_I)); @@ -67,7 +67,7 @@ public: static constexpr unsigned addr_length = cordic_roms::@ROM_TYPE@_@CORDIC_W@_@CORDIC_STAGES@_@CORDIC_Q@_@CORDIC_DIVIDER@_size; static constexpr int64_t scale_cordic(int64_t in) { - return in * kn_i / 8U; + return in * kn_i / 16U; } #if !defined(__SYNTHESIS__) && defined(SOFTWARE) @@ -113,8 +113,8 @@ public: #endif static ap_int scale_cordic(const ap_int & in) { - const ap_int tmp = in * ap_uint<3>(kn_i); - return ap_int(tmp >> 3); + const ap_int tmp = in * ap_uint<4>(kn_i); + return ap_int(tmp >> 4); } static void cordic(const ap_int & re_in, const ap_int & im_in, diff --git a/sources/tb/cordic_rom_tb.cpp.in b/sources/tb/cordic_rom_tb.cpp.in index 5adb4c4..1ada926 100644 --- a/sources/tb/cordic_rom_tb.cpp.in +++ b/sources/tb/cordic_rom_tb.cpp.in @@ -101,12 +101,12 @@ TEST_CASE("ROM-based Cordic (TPL @ROM_TYPE@, @CORDIC_W@, @CORDIC_STAGES@, @CORDI double results_re[n_lines]; double results_im[n_lines]; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_re_in[i] = int64_t(a * double(cordic_rom::in_scale_factor)); @@ -117,7 +117,7 @@ TEST_CASE("ROM-based Cordic (TPL @ROM_TYPE@, @CORDIC_W@, @CORDIC_STAGES@, @CORDI results_im[i] = e.imag(); } - INPUT.close(); + fclose(INPUT); constexpr double abs_margin = double(1 << cordic.Out_I) * 2. / 100.; @@ -156,12 +156,12 @@ TEST_CASE("ROM-based Cordic (TPL @ROM_TYPE@, @CORDIC_W@, @CORDIC_STAGES@, @CORDI double results_re[n_lines]; double results_im[n_lines]; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_re_in[i] = int64_t(a * double(cordic_rom::in_scale_factor)); @@ -172,7 +172,7 @@ TEST_CASE("ROM-based Cordic (TPL @ROM_TYPE@, @CORDIC_W@, @CORDIC_STAGES@, @CORDI results_im[i] = e.imag(); } - INPUT.close(); + fclose(INPUT); constexpr double abs_margin = double(1 << cordic.Out_I) * 2. / 100.; diff --git a/sources/tb/cordic_tb.cpp b/sources/tb/cordic_tb.cpp index af35883..a1314b5 100644 --- a/sources/tb/cordic_tb.cpp +++ b/sources/tb/cordic_tb.cpp @@ -30,7 +30,7 @@ using Catch::Matchers::Floating::WithinAbsMatcher; typedef CCordicRotateSmart<8, 14, 4, 17, 5, 19, 7, 12> cordic_legacy; -TEST_CASE("Adaptive CORDIC work as intended", "[!hide][WIP]") { +TEST_CASE("Adaptive CORDIC work as intended", "[!mayfail][!hide][WIP]") { string input_fn = "../data/input.dat"; // _8_14_4_17_5_19_7_12 string output_fn = "../data/output.dat"; // _8_14_4_17_5_19_7_12 @@ -45,28 +45,29 @@ TEST_CASE("Adaptive CORDIC work as intended", "[!hide][WIP]") { double exp_re_out[n_lines]; double exp_im_out[n_lines]; - ofstream FILE; - - ifstream INPUT(input_fn); - ifstream RESULTS(output_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); + FILE * RESULTS = fopen(output_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, c; - INPUT >> a >> b >> c; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &c); values_re_in[i] = a; values_im_in[i] = b; angles_in[i] = c; - RESULTS >> a >> b; + fscanf(RESULTS, "%lf,%lf\n", &a, &b); exp_re_out[i] = a; exp_im_out[i] = b; } - INPUT.close(); - RESULTS.close(); + fclose(INPUT); + fclose(RESULTS); + + + constexpr double abs_margin = double(1 << 6) * 3. / 100.; // Save the results to a file - // FILE.open("results.dat"); + // ofstream FILE("results.dat"); // Executing the encoder for (unsigned iter = 0; iter < n_lines; iter++) { @@ -82,8 +83,8 @@ TEST_CASE("Adaptive CORDIC work as intended", "[!hide][WIP]") { // FILE << values_re_out[iter].to_float() << ", " << values_re_out[iter].to_float() << endl; - REQUIRE_THAT(values_re_out[iter].to_float(), WithinAbsMatcher(exp_re_out[iter], 0.079997558593750)); - REQUIRE_THAT(values_im_out[iter].to_float(), WithinAbsMatcher(exp_im_out[iter], 0.079997558593750)); + REQUIRE_THAT(values_re_out[iter].to_double(), WithinAbsMatcher(exp_re_out[iter], abs_margin)); + REQUIRE_THAT(values_im_out[iter].to_double(), WithinAbsMatcher(exp_im_out[iter], abs_margin)); } // FILE.close(); @@ -111,12 +112,12 @@ TEST_CASE("ROM-based Cordic works with C-Types", "[CORDIC]") { // ofstream FILE; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_in[i] = c; @@ -128,7 +129,7 @@ TEST_CASE("ROM-based Cordic works with C-Types", "[CORDIC]") { results[i] = c * e; } - INPUT.close(); + fclose(INPUT); // Save the results to a file // FILE.open("results.dat"); @@ -186,12 +187,12 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { // ofstream out_stream; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_re_in[i] = int64_t(a * double(cordic_rom::in_scale_factor)); @@ -202,7 +203,7 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { results_im[i] = e.imag(); } - INPUT.close(); + fclose(INPUT); // Save the results to a file // out_stream.open("results_ap.dat"); @@ -229,8 +230,8 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { // out_stream << values_re_out[iter].to_int64() << " " << values_im_out[iter].to_int64() << " " << results_re[iter] << " " << results_im[iter] << endl; - REQUIRE_THAT(values_re_out[iter].to_double() * 5. / 8. / cordic_rom::out_scale_factor, WithinAbsMatcher(results_re[iter], abs_margin)); - REQUIRE_THAT(values_im_out[iter].to_double() * 5. / 8. / cordic_rom::out_scale_factor, WithinAbsMatcher(results_im[iter], abs_margin)); + REQUIRE_THAT(cordic_rom::scale_cordic(values_re_out[iter].to_double()) / cordic_rom::out_scale_factor, WithinAbsMatcher(results_re[iter], abs_margin)); + REQUIRE_THAT(cordic_rom::scale_cordic(values_im_out[iter].to_double()) / cordic_rom::out_scale_factor, WithinAbsMatcher(results_im[iter], abs_margin)); } // out_stream.close(); // fclose(romf); @@ -263,12 +264,12 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { // ofstream out_stream; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_re_in[i] = int64_t(a * double(cordic_rom::in_scale_factor)); @@ -279,7 +280,7 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { results_im[i] = e.imag(); } - INPUT.close(); + fclose(INPUT); // Save the results to a file // out_stream.open("results_ap.dat"); @@ -345,12 +346,12 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { // ofstream out_stream; - ifstream INPUT(input_fn); + FILE * INPUT = fopen(input_fn.c_str(), "r"); // Init test vector for (unsigned i = 0; i < n_lines; i++) { double a, b, r; - INPUT >> a >> b >> r; + fscanf(INPUT, "%lf,%lf,%lf\n", &a, &b, &r); const complex c {a, b}; values_re_in[i] = int64_t(a * double(cordic_rom::in_scale_factor)); @@ -361,7 +362,7 @@ TEST_CASE("ROM-based Cordic works with AP-Types", "[CORDIC]") { results_im[i] = e.imag(); } - INPUT.close(); + fclose(INPUT); // Save the results to a file // out_stream.open("results_ap.dat");