Permalink
Browse files

fixed promote (reverted to boost), macros LVV_PROMOTE1,2, LVV_IS_INTE…

…GRAL
  • Loading branch information...
lvv committed Jan 8, 2009
1 parent 67056df commit 8e72a537bbdeddba3122282db6b195186f3f4d92
Showing with 180 additions and 154 deletions.
  1. +15 −16 float.h
  2. +6 −13 math.h
  3. +109 −79 meta.h
  4. +0 −19 t-equal.cc
  5. +0 −10 u-math.cc
  6. +50 −17 u-meta.cc
View
31 float.h
@@ -4,7 +4,7 @@
// TR floating point utils: http://www.codeproject.com/KB/cpp/floatutils.aspx
////////////////////////////////////////////////////////////////////////////////////////////// IEEE FLOATING POINT
////////////////////////////////////////////////////////////////////////////////////////////// IEEE FLOATING POINT
template<typename T, long M=0, long E=1> struct fp {
T const static value = M * (E > 0 ? ipow<10,E>::value : ( E < 0 ? T(1)/T(ipow<10,-E>::value) : 1) );
};
@@ -32,11 +32,11 @@
float static const one_ulp_magic = 6755399441055744.0; //2^52 * 1.5
};
//////////////////////////////////////////////////////////////////////////////////// ABS
//////////////////////////////////////////////////////////////////////////////////// TODO ABS
/* make x = abs(x) */
// TO TRY: *(((int *) &x) + 1) &= 0x7fffffff;
//
//////////////////////////////////////////////////////////////////////////////////// COMPARE
//////////////////////////////////////////////////////////////////////////////////// TODO COMPARE
// FROM http://aggregate.org/MAGIC/#Absolute%20Value%20of%20a%20Float
// #define FasI(f) (*((int *) &(f)))
// #define FasUI(f) (*((unsigned int *) &(f)))
@@ -47,20 +47,19 @@
// #define ge0(f) (FasUI(f) <= 0x80000000U)
//
//////////////////////////////////////////////////////////////////////////////////// RECIPROCAL
// // This is about 2.12 times faster than using 1.0f / n
//
// // r = 1/p
//
// #define FP_INV(r,p) { \
// int _i = 2 * 0x3F800000 - *(int *)&(p); \
// (r) = *(float *)&_i; \
// (r) = (r) * (2.0f - (p) * (r)); \
// }
//
//////////////////////////////////////////////////////////////////////////////////// TODO RECIPROCAL
// This is about 2.12 times faster than using 1.0f / n
//
// r = 1/p
/*
#define FP_INV(r,p) { \
int _i = 2 * 0x3F800000 - *(int *)&(p); \
(r) = *(float *)&_i; \
(r) = (r) * (2.0f - (p) * (r)); \
}
*/
//////////////////////////////////////////////////////////////////////////////////// FLOOR
//////////////////////////////////////////////////////////////////////////////////// FLOOR
// floor(float)
const float fm = 0x1.0p23f*1.5f+1; // 2^52 * 1.5, uses limited precisicion to floor
@@ -77,7 +76,7 @@
double static inline floor(double x) { return x - dme + dm - dm; }
//////////////////////////////////////////////////////////////////////////////////// INTEGER --> FP
//////////////////////////////////////////////////////////////////////////////////// INTEGER --> FP
/*
const double _double2fixmagic = 68719476736.0*1.5; //2^36 * 1.5, (52-_shiftamt=36) uses limited precisicion to floor
View
19 math.h
@@ -27,16 +27,6 @@
using std::ostream;
using std::setprecision;
#include <tr1/type_traits>
//#include <boost/type_traits.hpp>
//#include <boost/type_traits/integral_constant.hpp>
//#include <boost/type_traits/is_integral.hpp>
// using boost::true_type;
// using boost::false_type;
//using boost::is_integral;
#ifdef __GNUC__
#define PURE __attribute__((const))
#else
@@ -59,7 +49,9 @@
private:
long count;
//typename Type<T>::SuperiorType sum;
typename boost::promote<T>::type sum;
//typename std::tr1::promote<T>::type sum;
//__gnu_cxx::__promote<T>::__type sum;
typename LVV_PROMOTE1(T) sum;
string lable;
};
@@ -154,9 +146,10 @@ double powi (double x, int n) { // simplified http://dslinux.gits.kiev.ua/tr
return abs_diff < max_error;
}
template<typename T1, typename T2> static inline
bool eq (T1 n1,T2 n2, ulp_t ulps=100, typename promote_trait<T1,T2>::T_promote characteristic_value=0) {
typedef typename promote_trait<T1,T2>::T_promote T;
bool eq (T1 n1,T2 n2, ulp_t ulps=100, __gnu_cxx::__promote_2<T1,T2> characteristic_value=0) {
typedef __gnu_cxx::__promote_2<T1,T2> T;
typename boost::is_integral<T>::type integral_flag;
return eq_impl<T1,T2,T>(n1, n2, integral_flag, ulps, characteristic_value);
}
View
188 meta.h
@@ -1,36 +1,44 @@
// template metaprogramming and conveniance macros
#ifndef LVV_META_H
#define LVV_META_H
// template metaprogramming and conveniance macros
#ifndef LVV_META_H
#define LVV_META_H
// TODO: 2 check boost: typedef typename tools::promote_args<T>::type result_type;
// local copy of blitz promote-old.h
//#include <lvv/blitz-promote-old.h>
#include <boost/type_traits/integral_constant.hpp>
using boost::true_type;
using boost::false_type;
#include <boost/type_traits/is_integral.hpp>
//#include <boost/type_traits.hpp>
using boost::is_integral;
#include <iostream>
#ifdef __GNUC__
#define PURE __attribute__((const))
#else
#define PURE
#endif
//#include <boost/type_traits/integral_constant.hpp>
#define LVV_TRUE_TYPE boost::true_type;
#define LVV_FALSE_TYPE boost::false_type;
#include <iostream>
// for type_descriptor
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------------------ PROMOTE
#include <boost/type_traits.hpp>
#define LVV_PROMOTE1(T) boost::promote<T>::type
#define LVV_IS_INTEGRAL(T) boost::is_integral<T>::value
#include <boost/type_traits/promote.hpp>
namespace lvv {
#include <boost/numeric/ublas/traits.hpp>
#define LVV_PROMOTE2(T1,T2) boost::numeric::ublas::promote_traits<T1,T2>::promote_type
/* for promotion:
for single arg only:
#include <boost/type_traits/promote.hpp>
boost::promote
for 2 args
#include <boost/boost/numeric/ublas/traits.hpp>
boost::numeric:ublas
old blitz promte:
//#include <lvv/blitz-promote-old.h>
gcc builtin doesn't works with T from template?
__gnu_cxx::__promote_2<T1,T2>
my own, depricated:
/////////////////////////////////////////////////////////////////////////////////////////////// SUPERIOR TYPE type_trait
/* DEPRICATED, use boost::promote
DEPRICATED
// this is promote_trait class to provide summator type
// use:
// typename Type<T>::SuperiorType sum;
@@ -46,7 +54,54 @@
template <> struct Type<double> { typedef double SuperiorType; };
*/
////////////////////////////////////////////////////////////////////////////////////////////// META IF
namespace lvv {
//////////////////////////////////////////////////////////////////////////////////////////// TRAIT
typedef int64_t int_longest_t;
typedef uint64_t uint_longest_t;
typedef long double float_longest_t;
//------------------------------------------------------------------------------------------ TODO: FLOAT DEMOTION
// see after #else at /usr/include/boost-1_37/boost/type_traits/floating_point_promotion.hpp
//------------------------------------------------------------------------------------------ TYPE_DESCRIPTOR
// by Ariel Badichi, Irfan Zaidi and Leonid Volnitsky
// usage: cout << type_descriptor<const float>;
template<typename T> struct type_descriptor;
template<typename T> struct type_descriptor<T *> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " *"; }};
template<typename T> struct type_descriptor<T &> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " &"; }};
template<typename T, std::size_t N> struct type_descriptor<T[N]>{std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " [" << N << "]";}};
template<typename T> struct type_descriptor<const T> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " const"; }};
template<typename T> struct type_descriptor<volatile T> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " volatile"; }};
template<typename T> struct type_descriptor<const volatile T> {std::ostream & print(std::ostream& out) const {out << "const volatile " << type_descriptor<T>(); return out; } };
template<typename T> struct type_descriptor<T* const> {std::ostream & print(std::ostream& out) const {out << type_descriptor<T>() << "* const"; return out; } };
template<> struct type_descriptor<char> {std::ostream & print(std::ostream & out) const{return out << "char"; }}; // without this compile error
template<> struct type_descriptor<int8_t> {std::ostream & print(std::ostream & out) const{return out << "int8_t"; }};
template<> struct type_descriptor<uint8_t> {std::ostream & print(std::ostream & out) const{return out << "uint8_t"; }};
template<> struct type_descriptor<int16_t> {std::ostream & print(std::ostream & out) const{return out << "int16_t"; }};
template<> struct type_descriptor<uint16_t> {std::ostream & print(std::ostream & out) const{return out << "uint16_t"; }};
template<> struct type_descriptor<int32_t> {std::ostream & print(std::ostream & out) const{return out << "int32_t"; }};
template<> struct type_descriptor<uint32_t> {std::ostream & print(std::ostream & out) const{return out << "uint32_t"; }};
template<> struct type_descriptor<int64_t> {std::ostream & print(std::ostream & out) const{return out << "int64_t"; }};
template<> struct type_descriptor<uint64_t> {std::ostream & print(std::ostream & out) const{return out << "uint64_t"; }};
template<> struct type_descriptor<float> {std::ostream & print(std::ostream & out) const{return out << "float"; }};
template<> struct type_descriptor<double> {std::ostream & print(std::ostream & out) const{return out << "double"; }};
template<> struct type_descriptor<long double>{std::ostream & print(std::ostream & out) const{return out << "long double";}};
template<typename T> std::ostream & operator<< (std::ostream & out, const type_descriptor<T> & desc) { return desc.print(out); }
//////////////////////////////////////////////////////////////////////////////////////////// CONTROL STRUCTURE
//------------------------------------------------------------------------------------------ META IF
// also in /usr/include/boost/detail/select_type.hpp
template <bool CONDITION, class THEN, class ELSE> struct IF { typedef THEN type; };
@@ -58,67 +113,42 @@
// IF< sizeof(int)<sizeof(long), long, int>::type i;
///////////////////////////////////////////////////////////////////////////////////////////// TODO IS_POW_OF_TWO
// (x&(x-1)) == 0
///////////////////////////////////////////////////////////////////////////////////////////// IPOW
template<unsigned X, unsigned P> struct ipow { enum { value = ipow<X, P % 2>::value * ipow<X*X, P / 2>::value }; };
template<unsigned X> struct ipow<X, 0> { enum { value = 1 }; };
template<unsigned X> struct ipow<X, 1> { enum { value = X }; };
///////////////////////////////////////////////////////////////////////////////////////////// ILOG2
//////////////////////////////////////////////////////////////////////////////////////////// META MATH
//------------------------------------------------------------------------------------------ RATIO
template<long N, long D=1, typename FP=long double>
struct ratio_ {
//template<typename T> T value() { return T(N)/D;};
enum { numerator = N, denominator = D };
//template<typename TO> TO static convert() {return TO(N)/D; }
//FP const static value = FP(N)/D;
};
//------------------------------------------------------------------------------------------ INT_
template<long N> struct int_ { enum { value = N }; };
//------------------------------------------------------------------------------------------ TODO IS_POW_OF_TWO
// (x&(x-1)) == 0
//------------------------------------------------------------------------------------------ IPOW
template<unsigned X, unsigned P> struct ipow { enum { value = ipow<X, P % 2>::value * ipow<X*X, P / 2>::value }; };
template<unsigned X> struct ipow<X, 0> { enum { value = 1 }; };
template<unsigned X> struct ipow<X, 1> { enum { value = X }; };
//------------------------------------------------------------------------------------------ ILOG2
template<unsigned N> struct ilog2 { static_assert(N%2==0, "ilot2: N must be even number"); enum { value = 1 + ilog2<N/2>::value}; };
template<> struct ilog2<1> { enum { value = 0 }; };
template<> struct ilog2<2> { enum { value = 1 }; };
///////////////////////////////////////////////////////////////////////////////////////////// BINARY
//------------------------------------------------------------------------------------------ BINARY
// useage: const uint32_t bin_val2=binary<101010>::value;
template <uint32_t N> struct binary { static uint32_t const value = binary<N/10>::value <<1 | N%10; };
template <> struct binary<0> { static uint32_t const value = 0; };
///////////////////////////////////////////////////////////////////////////////////////////// RATIO
template<long N, long D=1, typename FP=long double>
struct ratio {
//template<typename T> T value() { return T(N)/D;};
enum { numerator = N, denominator = D };
FP const static value = FP(N)/D;
};
///////////////////////////////////////////////////////////////////////////////////////////// INT_
template<long N> struct int_ { enum { value = N }; };
///////////////////////////////////////////////////////////////////////////////////////////// TYPE_DESCRIPTOR
// by Ariel Badichi, Irfan Zaidi and Leonid Volnitsky
// #include <cstddef>
template<typename T> struct type_descriptor;
template<typename T> struct type_descriptor<T *> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " *"; }};
template<typename T> struct type_descriptor<T &> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " &"; }};
template<typename T, std::size_t N> struct type_descriptor<T[N]>{std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " [" << N << "]";}};
template<typename T> struct type_descriptor<const T> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " const"; }};
template<typename T> struct type_descriptor<volatile T> {std::ostream & print(std::ostream & out) const{return out << type_descriptor<T>() << " volatile"; }};
template<typename T> struct type_descriptor<const volatile T> {std::ostream & print(std::ostream& out) const {out << "const volatile " << type_descriptor<T>(); return out; } };
template<typename T> struct type_descriptor<T* const> {std::ostream & print(std::ostream& out) const {out << type_descriptor<T>() << "* const"; return out; } };
template<> struct type_descriptor<char> {std::ostream & print(std::ostream & out) const{return out << "char"; }}; // without this compile error
template<> struct type_descriptor<int8_t> {std::ostream & print(std::ostream & out) const{return out << "int8_t"; }};
template<> struct type_descriptor<uint8_t> {std::ostream & print(std::ostream & out) const{return out << "uint8_t"; }};
template<> struct type_descriptor<int16_t> {std::ostream & print(std::ostream & out) const{return out << "int16_t"; }};
template<> struct type_descriptor<uint16_t> {std::ostream & print(std::ostream & out) const{return out << "uint16_t"; }};
template<> struct type_descriptor<int32_t> {std::ostream & print(std::ostream & out) const{return out << "int32_t"; }};
template<> struct type_descriptor<uint32_t> {std::ostream & print(std::ostream & out) const{return out << "uint32_t"; }};
template<> struct type_descriptor<int64_t> {std::ostream & print(std::ostream & out) const{return out << "int64_t"; }};
template<> struct type_descriptor<uint64_t> {std::ostream & print(std::ostream & out) const{return out << "uint64_t"; }};
template<> struct type_descriptor<float> {std::ostream & print(std::ostream & out) const{return out << "float"; }};
template<> struct type_descriptor<double> {std::ostream & print(std::ostream & out) const{return out << "double"; }};
template<> struct type_descriptor<long double>{std::ostream & print(std::ostream & out) const{return out << "long double";}};
template<typename T> std::ostream & operator<< (std::ostream & out, const type_descriptor<T> & desc) { return desc.print(out); }
/////////////////////////////////////////////////////////////////////////////////////////////
}
#endif // LVV_META_H
}
#endif // LVV_META_H
View
@@ -27,25 +27,6 @@ main() {
cerr << setprecision(numeric_limits<double>::digits10+4) << boolalpha;
cout << setprecision(numeric_limits<double>::digits10+4) << boolalpha;
// is_integral
/*{
using std::tr1::is_integral;
cout << "\n\n *** Test is_integral ***\n";
PR1(is_integral<typeof(i0)>::value);
PR1(is_integral<typeof(f0)>::value);
PR1(is_integral<typeof(d2)>::value);
}*/
// promote_trait
cout << "\n\n *** Test promote trait ***\n";
PR1(typeid(promote_trait<int , int > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<int , float > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<int , float > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<float , float > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<int , int > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<char , unsigned > ::T_promote) .name() ) ;
PR1(typeid(promote_trait<long double, char > ::T_promote) .name() ) ;
cout << "\n\n *** Test promote trait ***\n";
PR1(eq(i0,i1))
PR1(eq(i0,i0))
View
@@ -7,16 +7,6 @@
int
main() {
cout << " ***** IPOW *******\n"; ///////////////////////////////////////////////
CHECKeq((lvv::ipow<3,0>::value), 1);
CHECKeq((lvv::ipow<3,1>::value), 3);
CHECKeq((lvv::ipow<3,2>::value), 9);
cout << " ***** ILOG2 *******\n"; ///////////////////////////////////////////////
CHECKeq((lvv::ilog2<1>::value), 0);
CHECKeq((lvv::ilog2<2>::value), 1);
CHECKeq((lvv::ilog2<4>::value), 2);
CHECKeq((lvv::ilog2<256>::value), 8);
CHECK_EXIT;
}
Oops, something went wrong.

0 comments on commit 8e72a53

Please sign in to comment.