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/type_traits/is_integral.hpp>
18 #include <boost/spirit/home/support/char_class.hpp>
19 #include <boost/spirit/home/support/unused.hpp>
20 #include <boost/spirit/home/support/numeric_traits.hpp>
21 #include <boost/spirit/home/support/detail/pow10.hpp>
22 #include <boost/spirit/home/support/detail/sign.hpp>
23 #include <boost/spirit/home/karma/detail/generate_to.hpp>
24 #include <boost/spirit/home/karma/detail/string_generate.hpp>
26 ///////////////////////////////////////////////////////////////////////////////
28 // The value BOOST_KARMA_NUMERICS_LOOP_UNROLL specifies, how to unroll the
29 // integer string generation loop (see below).
31 // Set the value to some integer in between 0 (no unrolling) and the
32 // largest expected generated integer string length (complete unrolling).
33 // If not specified, this value defaults to 6.
35 ///////////////////////////////////////////////////////////////////////////////
36 #if !defined(BOOST_KARMA_NUMERICS_LOOP_UNROLL)
37 #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
40 #if BOOST_KARMA_NUMERICS_LOOP_UNROLL < 0
41 #error "Please set the BOOST_KARMA_NUMERICS_LOOP_UNROLL to a non-negative value!"
44 namespace boost { namespace spirit { namespace traits
46 ///////////////////////////////////////////////////////////////////////
48 // return the absolute value from a given number, avoiding over- and
51 ///////////////////////////////////////////////////////////////////////
52 template <typename T, typename Enable/* = void*/>
58 // allow for ADL to find the correct overloads for fabs
64 #define BOOST_SPIRIT_ABSOLUTE_VALUE(signedtype, unsignedtype) \
66 struct absolute_value<signedtype> \
68 typedef unsignedtype type; \
69 static type call(signedtype n) \
71 return static_cast<unsignedtype>((n >= 0) ? n : -n); \
75 #define BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsignedtype) \
77 struct absolute_value<unsignedtype> \
79 typedef unsignedtype type; \
80 static type call(unsignedtype n) \
87 BOOST_SPIRIT_ABSOLUTE_VALUE(signed char, unsigned char);
88 BOOST_SPIRIT_ABSOLUTE_VALUE(char, unsigned char);
89 BOOST_SPIRIT_ABSOLUTE_VALUE(short, unsigned short);
90 BOOST_SPIRIT_ABSOLUTE_VALUE(int, unsigned int);
91 BOOST_SPIRIT_ABSOLUTE_VALUE(long, unsigned long);
92 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned char);
93 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned short);
94 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned int);
95 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(unsigned long);
96 #ifdef BOOST_HAS_LONG_LONG
97 BOOST_SPIRIT_ABSOLUTE_VALUE(boost::long_long_type, boost::ulong_long_type);
98 BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED(boost::ulong_long_type);
101 #undef BOOST_SPIRIT_ABSOLUTE_VALUE
102 #undef BOOST_SPIRIT_ABSOLUTE_VALUE_UNSIGNED
105 struct absolute_value<float>
108 static type call(float n)
110 return (spirit::detail::signbit)(n) ? -n : n;
115 struct absolute_value<double>
118 static type call(double n)
120 return (spirit::detail::signbit)(n) ? -n : n;
125 struct absolute_value<long double>
127 typedef long double type;
128 static type call(long double n)
130 return (spirit::detail::signbit)(n) ? -n : n;
134 // specialization for pointers
135 template <typename T>
136 struct absolute_value<T*>
138 typedef std::size_t type;
139 static type call (T* p)
141 return std::size_t(p);
145 template <typename T>
146 inline typename absolute_value<T>::type
147 get_absolute_value(T n)
149 return absolute_value<T>::call(n);
152 ///////////////////////////////////////////////////////////////////////
153 template <typename T, typename Enable/* = void*/>
156 static bool call(T n)
158 return (n < 0) ? true : false;
163 struct is_negative<float>
165 static bool call(float n)
167 return (spirit::detail::signbit)(n) ? true : false;
172 struct is_negative<double>
174 static bool call(double n)
176 return (spirit::detail::signbit)(n) ? true : false;
181 struct is_negative<long double>
183 static bool call(long double n)
185 return (spirit::detail::signbit)(n) ? true : false;
189 template <typename T>
190 inline bool test_negative(T n)
192 return is_negative<T>::call(n);
195 ///////////////////////////////////////////////////////////////////////
196 template <typename T, typename Enable/* = void*/>
199 static bool call(T n)
201 return (n == 0) ? true : false;
206 struct is_zero<float>
208 static bool call(float n)
210 return (math::fpclassify)(n) == FP_ZERO;
215 struct is_zero<double>
217 static bool call(double n)
219 return (math::fpclassify)(n) == FP_ZERO;
224 struct is_zero<long double>
226 static bool call(long double n)
228 return (math::fpclassify)(n) == FP_ZERO;
232 template <typename T>
233 inline bool test_zero(T n)
235 return is_zero<T>::call(n);
238 ///////////////////////////////////////////////////////////////////////
239 template <typename T, typename Enable/* = void*/>
242 static bool call(T n)
244 // NaN numbers are not equal to anything
245 return (n != n) ? true : false;
252 static bool call(float n)
254 return (math::fpclassify)(n) == FP_NAN;
259 struct is_nan<double>
261 static bool call(double n)
263 return (math::fpclassify)(n) == FP_NAN;
268 struct is_nan<long double>
270 static bool call(long double n)
272 return (math::fpclassify)(n) == FP_NAN;
276 template <typename T>
277 inline bool test_nan(T n)
279 return is_nan<T>::call(n);
282 ///////////////////////////////////////////////////////////////////////
283 template <typename T, typename Enable/* = void*/>
286 static bool call(T n)
288 if (!std::numeric_limits<T>::has_infinity)
290 return (n == std::numeric_limits<T>::infinity()) ? true : false;
295 struct is_infinite<float>
297 static bool call(float n)
299 return (math::fpclassify)(n) == FP_INFINITE;
304 struct is_infinite<double>
306 static bool call(double n)
308 return (math::fpclassify)(n) == FP_INFINITE;
313 struct is_infinite<long double>
315 static bool call(long double n)
317 return (math::fpclassify)(n) == FP_INFINITE;
321 template <typename T>
322 inline bool test_infinite(T n)
324 return is_infinite<T>::call(n);
327 ///////////////////////////////////////////////////////////////////////
330 static long call(float n, mpl::false_)
332 return static_cast<long>(std::floor(n));
335 static long call(double n, mpl::false_)
337 return static_cast<long>(std::floor(n));
340 static long call(long double n, mpl::false_)
342 return static_cast<long>(std::floor(n));
345 template <typename T>
346 static long call(T n, mpl::false_)
348 // allow for ADL to find the correct overload for floor and
351 return lround(floor(n));
354 template <typename T>
355 static long call(T n, mpl::true_)
357 return static_cast<long>(n);
360 template <typename T>
361 static long call(T n)
363 return call(n, mpl::bool_<is_integral<T>::value>());
367 ///////////////////////////////////////////////////////////////////////
368 struct truncate_to_long
370 static long call(float n, mpl::false_)
372 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
373 static_cast<long>(std::floor(n));
376 static long call(double n, mpl::false_)
378 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
379 static_cast<long>(std::floor(n));
382 static long call(long double n, mpl::false_)
384 return test_negative(n) ? static_cast<long>(std::ceil(n)) :
385 static_cast<long>(std::floor(n));
388 template <typename T>
389 static long call(T n, mpl::false_)
391 // allow for ADL to find the correct overloads for ltrunc
396 template <typename T>
397 static long call(T n, mpl::true_)
399 return static_cast<long>(n);
402 template <typename T>
403 static long call(T n)
405 return call(n, mpl::bool_<is_integral<T>::value>());
409 ///////////////////////////////////////////////////////////////////////
411 // Traits class for radix specific number conversion
413 // Convert a digit from binary representation to character
416 // static int call(unsigned n);
418 ///////////////////////////////////////////////////////////////////////
421 template <typename CharEncoding, typename Tag, bool radix_less_than_10>
424 static int call(unsigned n)
429 using spirit::char_class::convert;
430 return convert<CharEncoding>::to(Tag(), n - 10 + 'a');
435 struct convert_digit<unused_type, unused_type, false>
437 static int call(unsigned n)
445 template <typename CharEncoding, typename Tag>
446 struct convert_digit<CharEncoding, Tag, true>
448 static int call(unsigned n)
455 template <unsigned Radix, typename CharEncoding, typename Tag>
457 : detail::convert_digit<CharEncoding, Tag, (Radix <= 10) ? true : false>
460 ///////////////////////////////////////////////////////////////////////
461 template <unsigned Radix>
464 template <typename T>
465 static T call(T& n, mpl::true_)
470 template <typename T>
471 static T call(T& n, mpl::false_)
473 // Allow ADL to find the correct overload for floor
475 return floor(n / Radix);
478 template <typename T>
479 static T call(T& n, T const&, int)
481 return call(n, mpl::bool_<is_integral<T>::value>());
484 template <typename T>
487 return call(n, mpl::bool_<is_integral<T>::value>());
491 // specialization for division by 10
495 template <typename T>
496 static T call(T& n, T, int, mpl::true_)
501 template <typename T>
502 static T call(T, T& num, int exp, mpl::false_)
504 // Allow ADL to find the correct overload for floor
506 return floor(num / spirit::traits::pow10<T>(exp));
509 template <typename T>
510 static T call(T& n, T& num, int exp)
512 return call(n, num, exp, mpl::bool_<is_integral<T>::value>());
515 template <typename T>
518 return call(n, n, 1, mpl::bool_<is_integral<T>::value>());
522 ///////////////////////////////////////////////////////////////////////
523 template <unsigned Radix>
526 template <typename T>
527 static long call(T n, mpl::true_)
529 // this cast is safe since we know the result is not larger
531 return static_cast<long>(n % Radix);
534 template <typename T>
535 static long call(T n, mpl::false_)
537 // Allow ADL to find the correct overload for fmod
539 return cast_to_long::call(fmod(n, T(Radix)));
542 template <typename T>
543 static long call(T n)
545 return call(n, mpl::bool_<is_integral<T>::value>());
550 namespace boost { namespace spirit { namespace karma
552 ///////////////////////////////////////////////////////////////////////////
554 // The int_inserter template takes care of the integer to string
555 // conversion. If specified, the loop is unrolled for better performance.
557 // Set the value BOOST_KARMA_NUMERICS_LOOP_UNROLL to some integer in
558 // between 0 (no unrolling) and the largest expected generated integer
559 // string length (complete unrolling).
560 // If not specified, this value defaults to 6.
562 ///////////////////////////////////////////////////////////////////////////
563 #define BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX(z, x, data) \
564 if (!traits::test_zero(n)) { \
565 int ch = radix_type::call(remainder_type::call(n)); \
566 n = divide_type::call(n, num, ++exp); \
569 #define BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX(z, x, data) \
576 unsigned Radix, typename CharEncoding = unused_type
577 , typename Tag = unused_type>
580 typedef traits::convert_digit<Radix, CharEncoding, Tag> radix_type;
581 typedef traits::divide<Radix> divide_type;
582 typedef traits::remainder<Radix> remainder_type;
584 template <typename OutputIterator, typename T>
586 call(OutputIterator& sink, T n, T& num, int exp)
588 // remainder_type::call returns n % Radix
589 int ch = radix_type::call(remainder_type::call(n));
590 n = divide_type::call(n, num, ++exp);
593 BOOST_KARMA_NUMERICS_LOOP_UNROLL,
594 BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX, _);
596 if (!traits::test_zero(n))
597 call(sink, n, num, exp);
600 BOOST_KARMA_NUMERICS_LOOP_UNROLL,
601 BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX, _);
608 // Common code for integer string representations
609 template <typename OutputIterator, typename T>
611 call(OutputIterator& sink, T n)
613 return call(sink, n, n, 0);
617 // helper function returning the biggest number representable either in
618 // a boost::long_long_type (if this does exist) or in a plain long
620 #if defined(BOOST_HAS_LONG_LONG)
621 typedef boost::long_long_type biggest_long_type;
623 typedef long biggest_long_type;
626 static biggest_long_type max_long()
628 return (std::numeric_limits<biggest_long_type>::max)();
632 // Specialization for doubles and floats, falling back to long integers
633 // for representable values. These specializations speed up formatting
634 // of floating point numbers considerably as all the required
635 // arithmetics will be executed using integral data types.
636 template <typename OutputIterator>
638 call(OutputIterator& sink, long double n)
640 if (std::fabs(n) < max_long())
642 biggest_long_type l((biggest_long_type)n);
643 return call(sink, l, l, 0);
645 return call(sink, n, n, 0);
647 template <typename OutputIterator>
649 call(OutputIterator& sink, double n)
651 if (std::fabs(n) < max_long())
653 biggest_long_type l((biggest_long_type)n);
654 return call(sink, l, l, 0);
656 return call(sink, n, n, 0);
658 template <typename OutputIterator>
660 call(OutputIterator& sink, float n)
662 if (std::fabs(n) < max_long())
664 biggest_long_type l((biggest_long_type)n);
665 return call(sink, l, l, 0);
667 return call(sink, n, n, 0);
671 #undef BOOST_KARMA_NUMERICS_INNER_LOOP_PREFIX
672 #undef BOOST_KARMA_NUMERICS_INNER_LOOP_SUFFIX
674 ///////////////////////////////////////////////////////////////////////////
676 // The uint_inserter template takes care of the conversion of any integer
677 // to a string, while interpreting the number as an unsigned type.
679 ///////////////////////////////////////////////////////////////////////////
681 unsigned Radix, typename CharEncoding = unused_type
682 , typename Tag = unused_type>
683 struct uint_inserter : int_inserter<Radix, CharEncoding, Tag>
685 typedef int_inserter<Radix, CharEncoding, Tag> base_type;
687 // Common code for integer string representations
688 template <typename OutputIterator, typename T>
690 call(OutputIterator& sink, T const& n)
692 typedef typename traits::absolute_value<T>::type type;
694 return base_type::call(sink, un, un, 0);
698 ///////////////////////////////////////////////////////////////////////////
700 // The sign_inserter template generates a sign for a given numeric value.
702 // The parameter forcesign allows to generate a sign even for positive
705 ///////////////////////////////////////////////////////////////////////////
708 template <typename OutputIterator>
710 call_noforce(OutputIterator& sink, bool is_zero, bool is_negative,
713 // generate a sign for negative numbers only
714 if (is_negative || (is_zero && sign_if_zero)) {
721 template <typename OutputIterator>
723 call_force(OutputIterator& sink, bool is_zero, bool is_negative,
726 // generate a sign for all numbers except zero
727 if (!is_zero || sign_if_zero)
728 *sink = is_negative ? '-' : '+';
736 template <typename OutputIterator>
738 call(OutputIterator& sink, bool is_zero, bool is_negative
739 , bool forcesign, bool sign_if_zero = false)
742 call_force(sink, is_zero, is_negative, sign_if_zero) :
743 call_noforce(sink, is_zero, is_negative, sign_if_zero);
747 ///////////////////////////////////////////////////////////////////////////
748 // These are helper functions for the real policies allowing to generate
749 // a single character and a string
750 ///////////////////////////////////////////////////////////////////////////
751 template <typename CharEncoding = unused_type, typename Tag = unused_type>
754 template <typename OutputIterator, typename Char>
755 static bool call(OutputIterator& sink, Char c)
757 return detail::generate_to(sink, c, CharEncoding(), Tag());
761 template <typename CharEncoding = unused_type, typename Tag = unused_type>
762 struct string_inserter
764 template <typename OutputIterator, typename String>
765 static bool call(OutputIterator& sink, String str)
767 return detail::string_generate(sink, str, CharEncoding(), Tag());