1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_KARMA_NUMERIC_UTILS_FEB_23_2007_0841PM)
7 #define BOOST_SPIRIT_KARMA_NUMERIC_UTILS_FEB_23_2007_0841PM
13 #include <boost/config.hpp>
14 #include <boost/config/no_tr1/cmath.hpp>
15 #include <boost/limits.hpp>
17 #include <boost/math/special_functions/fpclassify.hpp>
18 #include <boost/type_traits/is_integral.hpp>
19 #include <boost/spirit/home/support/char_class.hpp>
20 #include <boost/spirit/home/support/unused.hpp>
21 #include <boost/spirit/home/support/numeric_traits.hpp>
22 #include <boost/spirit/home/support/detail/pow10.hpp>
23 #include <boost/spirit/home/support/detail/sign.hpp>
24 #include <boost/spirit/home/karma/detail/generate_to.hpp>
25 #include <boost/spirit/home/karma/detail/string_generate.hpp>
27 ///////////////////////////////////////////////////////////////////////////////
29 // The value BOOST_KARMA_NUMERICS_LOOP_UNROLL specifies, how to unroll the
30 // integer string generation loop (see below).
32 // Set the value to some integer in between 0 (no unrolling) and the
33 // largest expected generated integer string length (complete unrolling).
34 // If not specified, this value defaults to 6.
36 ///////////////////////////////////////////////////////////////////////////////
37 #if !defined(BOOST_KARMA_NUMERICS_LOOP_UNROLL)
38 #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
41 #if BOOST_KARMA_NUMERICS_LOOP_UNROLL < 0
42 #error "Please set the BOOST_KARMA_NUMERICS_LOOP_UNROLL to a non-negative value!"
45 namespace boost { namespace spirit { namespace traits
47 ///////////////////////////////////////////////////////////////////////
49 // return the absolute value from a given number, avoiding over- and
52 ///////////////////////////////////////////////////////////////////////
53 template <typename T, typename Enable/* = void*/>
59 // allow for ADL to find the correct overloads for fabs
65 #define BOOST_SPIRIT_ABSOLUTE_VALUE(signedtype, unsignedtype) \
67 struct absolute_value<signedtype> \
69 typedef unsignedtype type; \
70 static type call(signedtype n) \
72 /* implementation is well-defined for one's complement, */ \
73 /* two's complement, and signed magnitude architectures */ \
74 /* by the C++ Standard. [conv.integral] [expr.unary.op] */ \
75 return (n >= 0) ? static_cast<type>(n) \
76 : -static_cast<type>(n); \
80 #define BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsignedtype) \
82 struct absolute_value<unsignedtype> \
84 typedef unsignedtype type; \
85 static type call(unsignedtype n) \
92 #if defined(BOOST_MSVC)
93 # pragma warning(push)
94 // unary minus operator applied to unsigned type, result still unsigned
95 # pragma warning(disable: 4146)
97 BOOST_SPIRIT_ABSOLUTE_VALUE(signed char, unsigned char);
98 BOOST_SPIRIT_ABSOLUTE_VALUE(char, unsigned char);
99 BOOST_SPIRIT_ABSOLUTE_VALUE(short, unsigned short);
100 BOOST_SPIRIT_ABSOLUTE_VALUE(int, unsigned int);
101 BOOST_SPIRIT_ABSOLUTE_VALUE(long, unsigned long);
102 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned char);
103 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned short);
104 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned int);
105 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned long);
106 #ifdef BOOST_HAS_LONG_LONG
107 BOOST_SPIRIT_ABSOLUTE_VALUE(boost::long_long_type, boost::ulong_long_type);
108 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(boost::ulong_long_type);
110 #if defined(BOOST_MSVC)
111 # pragma warning(pop)
114 #undef BOOST_SPIRIT_ABSOLUTE_VALUE
115 #undef BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED
118 struct absolute_value<float>
121 static type call(float n)
123 return (spirit::detail::signbit)(n) ? -n : n;
128 struct absolute_value<double>
131 static type call(double n)
133 return (spirit::detail::signbit)(n) ? -n : n;
138 struct absolute_value<long double>
140 typedef long double type;
141 static type call(long double n)
143 return (spirit::detail::signbit)(n) ? -n : n;
147 // specialization for pointers
148 template <typename T>
149 struct absolute_value<T*>
151 typedef std::size_t type;
152 static type call (T* p)
154 return std::size_t(p);
158 template <typename T>
159 inline typename absolute_value<T>::type
160 get_absolute_value(T n)
162 return absolute_value<T>::call(n);
165 ///////////////////////////////////////////////////////////////////////
166 template <typename T, typename Enable/* = void*/>
169 static bool call(T n)
171 return (n < 0) ? true : false;
176 struct is_negative<float>
178 static bool call(float n)
180 return (spirit::detail::signbit)(n) ? true : false;
185 struct is_negative<double>
187 static bool call(double n)
189 return (spirit::detail::signbit)(n) ? true : false;
194 struct is_negative<long double>
196 static bool call(long double n)
198 return (spirit::detail::signbit)(n) ? true : false;
202 template <typename T>
203 inline bool test_negative(T n)
205 return is_negative<T>::call(n);
208 ///////////////////////////////////////////////////////////////////////
209 template <typename T, typename Enable/* = void*/>
212 static bool call(T n)
214 return (n == 0) ? true : false;
219 struct is_zero<float>
221 static bool call(float n)
223 return (math::fpclassify)(n) == FP_ZERO;
228 struct is_zero<double>
230 static bool call(double n)
232 return (math::fpclassify)(n) == FP_ZERO;
237 struct is_zero<long double>
239 static bool call(long double n)
241 return (math::fpclassify)(n) == FP_ZERO;
245 template <typename T>
246 inline bool test_zero(T n)
248 return is_zero<T>::call(n);
251 ///////////////////////////////////////////////////////////////////////
252 template <typename T, typename Enable/* = void*/>
255 static bool call(T n)
257 // NaN numbers are not equal to anything
258 return (n != n) ? true : false;
265 static bool call(float n)
267 return (math::fpclassify)(n) == FP_NAN;
272 struct is_nan<double>
274 static bool call(double n)
276 return (math::fpclassify)(n) == FP_NAN;
281 struct is_nan<long double>
283 static bool call(long double n)
285 return (math::fpclassify)(n) == FP_NAN;
289 template <typename T>
290 inline bool test_nan(T n)
292 return is_nan<T>::call(n);
295 ///////////////////////////////////////////////////////////////////////
296 template <typename T, typename Enable/* = void*/>
299 static bool call(T n)
301 return std::numeric_limits<T>::has_infinity
302 && n == std::numeric_limits<T>::infinity();
307 struct is_infinite<float>
309 static bool call(float n)
311 return (math::fpclassify)(n) == FP_INFINITE;
316 struct is_infinite<double>
318 static bool call(double n)
320 return (math::fpclassify)(n) == FP_INFINITE;
325 struct is_infinite<long double>
327 static bool call(long double n)
329 return (math::fpclassify)(n) == FP_INFINITE;
333 template <typename T>
334 inline bool test_infinite(T n)
336 return is_infinite<T>::call(n);
339 ///////////////////////////////////////////////////////////////////////
342 static long call(float n, mpl::false_)
344 return static_cast<long>(std::floor(n));
347 static long call(double n, mpl::false_)
349 return static_cast<long>(std::floor(n));
352 static long call(long double n, mpl::false_)
354 return static_cast<long>(std::floor(n));
357 template <typename T>
358 static long call(T n, mpl::false_)
360 // allow for ADL to find the correct overload for floor and
363 return lround(floor(n));
366 template <typename T>
367 static long call(T n, mpl::true_)
369 return static_cast<long>(n);
372 template <typename T>
373 static long call(T n)
375 return call(n, mpl::bool_<is_integral<T>::value>());
379 ///////////////////////////////////////////////////////////////////////
380 struct truncate_to_long
382 static long call(float n, mpl::false_)
384 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
385 static_cast<long>(std::floor(n));
388 static long call(double n, mpl::false_)
390 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
391 static_cast<long>(std::floor(n));
394 static long call(long double n, mpl::false_)
396 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
397 static_cast<long>(std::floor(n));
400 template <typename T>
401 static long call(T n, mpl::false_)
403 // allow for ADL to find the correct overloads for ltrunc
408 template <typename T>
409 static long call(T n, mpl::true_)
411 return static_cast<long>(n);
414 template <typename T>
415 static long call(T n)
417 return call(n, mpl::bool_<is_integral<T>::value>());
421 ///////////////////////////////////////////////////////////////////////
423 // Traits class for radix specific number conversion
425 // Convert a digit from binary representation to character
428 // static int call(unsigned n);
430 ///////////////////////////////////////////////////////////////////////
433 template <typename CharEncoding, typename Tag, bool radix_less_than_10>
436 static int call(unsigned n)
441 using spirit::char_class::convert;
442 return convert<CharEncoding>::to(Tag(), n - 10 + 'a');
447 struct convert_digit<unused_type, unused_type, false>
449 static int call(unsigned n)
457 template <typename CharEncoding, typename Tag>
458 struct convert_digit<CharEncoding, Tag, true>
460 static int call(unsigned n)
467 template <unsigned Radix, typename CharEncoding, typename Tag>
469 : detail::convert_digit<CharEncoding, Tag, (Radix <= 10) ? true : false>
472 ///////////////////////////////////////////////////////////////////////
473 template <unsigned Radix>
476 template <typename T>
477 static T call(T& n, mpl::true_)
482 template <typename T>
483 static T call(T& n, mpl::false_)
485 // Allow ADL to find the correct overload for floor
487 return floor(n / Radix);
490 template <typename T>
491 static T call(T& n, T const&, int)
493 return call(n, mpl::bool_<is_integral<T>::value>());
496 template <typename T>
499 return call(n, mpl::bool_<is_integral<T>::value>());
503 // specialization for division by 10
507 template <typename T>
508 static T call(T& n, T, int, mpl::true_)
513 template <typename T>
514 static T call(T, T& num, int exp, mpl::false_)
516 // Allow ADL to find the correct overload for floor
518 return floor(num / spirit::traits::pow10<T>(exp));
521 template <typename T>
522 static T call(T& n, T& num, int exp)
524 return call(n, num, exp, mpl::bool_<is_integral<T>::value>());
527 template <typename T>
530 return call(n, n, 1, mpl::bool_<is_integral<T>::value>());
534 ///////////////////////////////////////////////////////////////////////
535 template <unsigned Radix>
538 template <typename T>
539 static long call(T n, mpl::true_)
541 // this cast is safe since we know the result is not larger
543 return static_cast<long>(n % Radix);
546 template <typename T>
547 static long call(T n, mpl::false_)
549 // Allow ADL to find the correct overload for fmod
551 return cast_to_long::call(fmod(n, T(Radix)));
554 template <typename T>
555 static long call(T n)
557 return call(n, mpl::bool_<is_integral<T>::value>());
562 namespace boost { namespace spirit { namespace karma
564 ///////////////////////////////////////////////////////////////////////////
566 // The int_inserter template takes care of the integer to string
567 // conversion. If specified, the loop is unrolled for better performance.
569 // Set the value BOOST_KARMA_NUMERICS_LOOP_UNROLL to some integer in
570 // between 0 (no unrolling) and the largest expected generated integer
571 // string length (complete unrolling).
572 // If not specified, this value defaults to 6.
574 ///////////////////////////////////////////////////////////////////////////
575 #define BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX(z, x, data) \
576 if (!traits::test_zero(n)) { \
577 int ch = radix_type::call(remainder_type::call(n)); \
578 n = divide_type::call(n, num, ++exp); \
581 #define BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX(z, x, data) \
588 unsigned Radix, typename CharEncoding = unused_type
589 , typename Tag = unused_type>
592 typedef traits::convert_digit<Radix, CharEncoding, Tag> radix_type;
593 typedef traits::divide<Radix> divide_type;
594 typedef traits::remainder<Radix> remainder_type;
596 template <typename OutputIterator, typename T>
598 call(OutputIterator& sink, T n, T& num, int exp)
600 // remainder_type::call returns n % Radix
601 int ch = radix_type::call(remainder_type::call(n));
602 n = divide_type::call(n, num, ++exp);
605 BOOST_KARMA_NUMERICS_LOOP_UNROLL,
606 BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX, _);
608 if (!traits::test_zero(n))
609 call(sink, n, num, exp);
612 BOOST_KARMA_NUMERICS_LOOP_UNROLL,
613 BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX, _);
620 // Common code for integer string representations
621 template <typename OutputIterator, typename T>
623 call(OutputIterator& sink, T n)
625 return call(sink, n, n, 0);
629 // helper function returning the biggest number representable either in
630 // a boost::long_long_type (if this does exist) or in a plain long
632 #if defined(BOOST_HAS_LONG_LONG)
633 typedef boost::long_long_type biggest_long_type;
635 typedef long biggest_long_type;
638 static biggest_long_type max_long()
640 return (std::numeric_limits<biggest_long_type>::max)();
644 // Specialization for doubles and floats, falling back to long integers
645 // for representable values. These specializations speed up formatting
646 // of floating point numbers considerably as all the required
647 // arithmetics will be executed using integral data types.
648 template <typename OutputIterator>
650 call(OutputIterator& sink, long double n)
652 if (std::fabs(n) < max_long())
654 biggest_long_type l((biggest_long_type)n);
655 return call(sink, l, l, 0);
657 return call(sink, n, n, 0);
659 template <typename OutputIterator>
661 call(OutputIterator& sink, double n)
663 if (std::fabs(n) < max_long())
665 biggest_long_type l((biggest_long_type)n);
666 return call(sink, l, l, 0);
668 return call(sink, n, n, 0);
670 template <typename OutputIterator>
672 call(OutputIterator& sink, float n)
674 if (std::fabs(n) < max_long())
676 biggest_long_type l((biggest_long_type)n);
677 return call(sink, l, l, 0);
679 return call(sink, n, n, 0);
683 #undef BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX
684 #undef BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX
686 ///////////////////////////////////////////////////////////////////////////
688 // The uint_inserter template takes care of the conversion of any integer
689 // to a string, while interpreting the number as an unsigned type.
691 ///////////////////////////////////////////////////////////////////////////
693 unsigned Radix, typename CharEncoding = unused_type
694 , typename Tag = unused_type>
695 struct uint_inserter : int_inserter<Radix, CharEncoding, Tag>
697 typedef int_inserter<Radix, CharEncoding, Tag> base_type;
699 // Common code for integer string representations
700 template <typename OutputIterator, typename T>
702 call(OutputIterator& sink, T const& n)
704 typedef typename traits::absolute_value<T>::type type;
706 return base_type::call(sink, un, un, 0);
710 ///////////////////////////////////////////////////////////////////////////
712 // The sign_inserter template generates a sign for a given numeric value.
714 // The parameter forcesign allows to generate a sign even for positive
717 ///////////////////////////////////////////////////////////////////////////
720 template <typename OutputIterator>
722 call_noforce(OutputIterator& sink, bool is_zero, bool is_negative,
725 // generate a sign for negative numbers only
726 if (is_negative || (is_zero && sign_if_zero)) {
733 template <typename OutputIterator>
735 call_force(OutputIterator& sink, bool is_zero, bool is_negative,
738 // generate a sign for all numbers except zero
739 if (!is_zero || sign_if_zero)
740 *sink = is_negative ? '-' : '+';
748 template <typename OutputIterator>
750 call(OutputIterator& sink, bool is_zero, bool is_negative
751 , bool forcesign, bool sign_if_zero = false)
754 call_force(sink, is_zero, is_negative, sign_if_zero) :
755 call_noforce(sink, is_zero, is_negative, sign_if_zero);
759 ///////////////////////////////////////////////////////////////////////////
760 // These are helper functions for the real policies allowing to generate
761 // a single character and a string
762 ///////////////////////////////////////////////////////////////////////////
763 template <typename CharEncoding = unused_type, typename Tag = unused_type>
766 template <typename OutputIterator, typename Char>
767 static bool call(OutputIterator& sink, Char c)
769 return detail::generate_to(sink, c, CharEncoding(), Tag());
773 template <typename CharEncoding = unused_type, typename Tag = unused_type>
774 struct string_inserter
776 template <typename OutputIterator, typename String>
777 static bool call(OutputIterator& sink, String str)
779 return detail::string_generate(sink, str, CharEncoding(), Tag());