Skip to content

Commit

Permalink
Merge pull request #20 from gsarkis/tools
Browse files Browse the repository at this point in the history
Tools
  • Loading branch information
ajraymond committed Sep 25, 2013
2 parents 18e62be + 42144c5 commit e17a358
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 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)
120 changes: 120 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
*/

#include <boost/preprocessor/list/for_each_product.hpp>
#include <boost/preprocessor/tuple/to_seq.hpp>
#include <boost/preprocessor/seq/cat.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <cstdlib>

#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<W, BOOST_PP_TUPLE_ELEM(4,0,p), BOOST_PP_TUPLE_ELEM(4,1,p), \
BOOST_PP_TUPLE_ELEM(4,2,p), BOOST_PP_TUPLE_ELEM(4,3,p)> 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) << " <f> <signedness> <overflow> <rounding> <value>"
<< 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;
}

0 comments on commit e17a358

Please sign in to comment.