diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt new file mode 100644 index 0000000..b41e1ce --- /dev/null +++ b/tools/CMakeLists.txt @@ -0,0 +1,8 @@ +#boost pre-processor +set(REQ_BOOST_LIBS ${REQ_BOOST_LIBS}) + +include_directories( + ${PROJECT_SOURCE_DIR}/include +) + +add_executable(fixed ${PROJECT_SOURCE_DIR}/tools/fixed.cpp) diff --git a/tools/fixed.cpp b/tools/fixed.cpp new file mode 100644 index 0000000..de7fd92 --- /dev/null +++ b/tools/fixed.cpp @@ -0,0 +1,120 @@ +/* + + *Copyright © 2013 Alexandre Raymond. + + *This file is part of the C++ Fixed-Point Library (LibFi). + + *LibFi is free software: you can redistribute it and/or modify + *it under the terms of the GNU General Public License as published by + *the Free Software Foundation, either version 3 of the License, or + *(at your option) any later version. + + *LibFi is distributed in the hope that it will be useful, + *but WITHOUT ANY WARRANTY; without even the implied warranty of + *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + *GNU General Public License for more details. + + *You should have received a copy of the GNU General Public License + *along with LibFi. If not, see . + + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "fi/Fixed.hpp" +#define OVERFLOW_TYPES (Wrap, (Saturate, (Throw, (Undefined, BOOST_PP_NIL)))) +#include "fi/overflow/Wrap.hpp" +#include "fi/overflow/Saturate.hpp" +#include "fi/overflow/Throw.hpp" +#include "fi/overflow/Undefined.hpp" + +#define ROUNDING_TYPES (Ceil, (Classic, (Fix, (Floor, BOOST_PP_NIL)))) +#include "fi/rounding/Ceil.hpp" +#include "fi/rounding/Classic.hpp" +#include "fi/rounding/Fix.hpp" +#include "fi/rounding/Floor.hpp" + +#define SIGNEDNESS_TYPES (UNSIGNED, (SIGNED, BOOST_PP_NIL)) +#define W 5 +#define F_VALS (1, (2, (3, (4, BOOST_PP_NIL)))) + + +#define MACRO1(r, p) { \ + typedef Fixed fi_t; \ + fi_t var; \ + std::string v_f = BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(4,0,p)); \ + boost::to_lower(v_f); \ + std::string v_signedness = BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(4,1,p)); \ + boost::to_lower(v_signedness); \ + std::string v_overflow = BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(4,2,p)); \ + boost::to_lower(v_overflow); \ + std::string v_rounding = BOOST_PP_STRINGIZE(BOOST_PP_TUPLE_ELEM(4,3,p)); \ + boost::to_lower(v_rounding); \ + if ( \ + (p_f == v_f) \ + && \ + (p_signedness == v_signedness) \ + && \ + (p_overflow == v_overflow) \ + && \ + (p_rounding == v_rounding) \ + ) \ + { \ + var = fi_t(p_val); \ + std::cout << var << std::endl; \ + return 0; \ + } \ +} + +using namespace Fi; + +void usage(char *argv0) { + std::cout << "LibFi test program with W=" << W << std::endl << std::endl + << "Usage: " << std::string(argv0) << " " + << std::endl + << " Where:" << std::endl + << " f : number of fractional bits (1-" << W << ")" << std::endl + << " signedness : overflow behavior " + << BOOST_PP_STRINGIZE(BOOST_PP_LIST_TO_TUPLE(SIGNEDNESS_TYPES)) << std::endl + << " overflow : overflow behavior " + << BOOST_PP_STRINGIZE(BOOST_PP_LIST_TO_TUPLE(OVERFLOW_TYPES)) << std::endl + << " rounding : rounding behavior " + << BOOST_PP_STRINGIZE(BOOST_PP_LIST_TO_TUPLE(ROUNDING_TYPES)) << std::endl + << " value : value to be converted to fixed point" + << std::endl; + exit(1); +} + +int main(int argc, char* argv[]) { + if (argc < 6) { + usage(argv[0]); + } + + //command-line parameters + std::string p_f = argv[1]; + std::string p_signedness = argv[2]; + std::string p_overflow = argv[3]; + std::string p_rounding = argv[4]; + std::string p_val = argv[5]; + + boost::to_lower(p_f); + boost::to_lower(p_signedness); + boost::to_lower(p_overflow); + boost::to_lower(p_rounding); + + BOOST_PP_LIST_FOR_EACH_PRODUCT(MACRO1, 4, (F_VALS,SIGNEDNESS_TYPES,OVERFLOW_TYPES,ROUNDING_TYPES)) + + std::cout << "ERROR: Invalid parameter combination" << std::endl; + usage(argv[0]); + + return 0; +} +