Some coercions and an arithmetic kernel for Boost.Multiprecision.

This commit is contained in:
Marc Glisse 2017-06-16 13:14:11 +02:00
parent e82853dca6
commit 7857964f3b
2 changed files with 115 additions and 1 deletions

View File

@ -0,0 +1,45 @@
// Copyright (c) 2017 Inria.
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Author: Marc Glisse <marc.glisse@inria.fr>
#ifndef CGAL_GMPXX_ARITHMETIC_KERNEL_H
#define CGAL_GMPXX_ARITHMETIC_KERNEL_H
#include <CGAL/Arithmetic_kernel/Arithmetic_kernel_base.h>
#include <CGAL/Get_arithmetic_kernel.h>
#include <CGAL/boost_mp.h>
// FIXME: the could be several kernels based on Boost.Multiprecision.
namespace CGAL {
/** \ingroup CGAL_Arithmetic_kernel
* \brief The Boost.Multiprecision set of exact number types
*/
struct BOOST_MP_arithmetic_kernel : internal::Arithmetic_kernel_base {
typedef boost::multiprecision::cpp_int Integer;
typedef boost::multiprecision::cpp_rational Rational;
};
template <class B1, boost::multiprecision::expression_template_option E1>
struct Get_arithmetic_kernel<boost::multiprecision::number<B1, E1> > {
typedef BOOST_MP_arithmetic_kernel Arithmetic_kernel;
};
template <class T1, class T2, class T3, class T4, class T5>
struct Get_arithmetic_kernel<boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> > {
typedef BOOST_MP_arithmetic_kernel Arithmetic_kernel;
};
} //namespace CGAL
#endif

View File

@ -359,6 +359,21 @@ struct Coercion_traits<boost::multiprecision::number<B1, E1>, boost::multiprecis
} }
}; };
}; };
// Avoid ambiguity with the specialization for <A,A> ...
template <class B1, boost::multiprecision::expression_template_option E1>
struct Coercion_traits<boost::multiprecision::number<B1, E1>, boost::multiprecision::number<B1, E1> >
{
typedef boost::multiprecision::number<B1, E1> Type;
typedef Tag_true Are_implicit_interoperable;
typedef Tag_true Are_explicit_interoperable;
struct Cast{
typedef Type result_type;
template <class U>
Type operator()(const U& x) const {
return Type(x);
}
};
};
template <class T1, class T2, class T3, class T4, class T5, class U1, class U2, class U3, class U4, class U5> template <class T1, class T2, class T3, class T4, class T5, class U1, class U2, class U3, class U4, class U5>
struct Coercion_traits < struct Coercion_traits <
@ -368,6 +383,15 @@ boost::multiprecision::detail::expression<U1,U2,U3,U4,U5> >
typename boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>::result_type, typename boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>::result_type,
typename boost::multiprecision::detail::expression<U1,U2,U3,U4,U5>::result_type> typename boost::multiprecision::detail::expression<U1,U2,U3,U4,U5>::result_type>
{ }; { };
// Avoid ambiguity with the specialization for <A,A> ...
template <class T1, class T2, class T3, class T4, class T5>
struct Coercion_traits <
boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>,
boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> >
: Coercion_traits <
typename boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>::result_type,
typename boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>::result_type>
{ };
template <class B, boost::multiprecision::expression_template_option E, class T1, class T2, class T3, class T4, class T5> template <class B, boost::multiprecision::expression_template_option E, class T1, class T2, class T3, class T4, class T5>
struct Coercion_traits<boost::multiprecision::number<B, E>, boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> > struct Coercion_traits<boost::multiprecision::number<B, E>, boost::multiprecision::detail::expression<T1,T2,T3,T4,T5> >
@ -383,10 +407,55 @@ typename boost::multiprecision::detail::expression<T1,T2,T3,T4,T5>::result_type,
boost::multiprecision::number<B, E> > boost::multiprecision::number<B, E> >
{ }; { };
// TODO: coercion with int, long, double... // TODO: coercion with expressions, fix existing coercions
// (double -> rational is implicit only for 1.56+, see ticket #10082) // (double -> rational is implicit only for 1.56+, see ticket #10082)
// The real solution would be to avoid specializing Coercion_traits for all pairs of number types and let it auto-detect what works, so only broken types need an explicit specialization.
// Ignore types smaller than long
#define CGAL_COERCE_INT(int) \
template <class B1, boost::multiprecision::expression_template_option E1> \
struct Coercion_traits<boost::multiprecision::number<B1, E1>, int> { \
typedef boost::multiprecision::number<B1, E1> Type; \
typedef Tag_true Are_implicit_interoperable; \
typedef Tag_true Are_explicit_interoperable; \
struct Cast{ \
typedef Type result_type; \
template <class U> Type operator()(const U& x) const { return Type(x); } \
}; \
}; \
template <class B1, boost::multiprecision::expression_template_option E1> \
struct Coercion_traits<int, boost::multiprecision::number<B1, E1> > \
: Coercion_traits<boost::multiprecision::number<B1, E1>, int> {}
CGAL_COERCE_INT(short);
CGAL_COERCE_INT(int);
CGAL_COERCE_INT(long);
#undef CGAL_COERCE_INT
// Ignore bounded-precision rationals
#define CGAL_COERCE_FLOAT(float) \
template <class B1, boost::multiprecision::expression_template_option E1> \
struct Coercion_traits<boost::multiprecision::number<B1, E1>, float> { \
typedef boost::multiprecision::number<B1, E1> Type; \
typedef Boolean_tag<boost::multiprecision::number_category<Type>::value != boost::multiprecision::number_kind_integer> Are_implicit_interoperable; \
typedef Are_implicit_interoperable Are_explicit_interoperable; \
struct Cast{ \
typedef Type result_type; \
template <class U> Type operator()(const U& x) const { return Type(x); } \
}; \
}; \
template <class B1, boost::multiprecision::expression_template_option E1> \
struct Coercion_traits<float, boost::multiprecision::number<B1, E1> > \
: Coercion_traits<boost::multiprecision::number<B1, E1>, float> {}
CGAL_COERCE_FLOAT(float);
CGAL_COERCE_FLOAT(double);
#undef CGAL_COERCE_FLOAT
} //namespace CGAL } //namespace CGAL
#include <CGAL/BOOST_MP_arithmetic_kernel.h>
#endif // BOOST_VERSION #endif // BOOST_VERSION
#endif #endif