Some compilers have support for extended arithmetic types such as 128-bit floats or integers. Let's take a quick glance at how to use them using Boost.
We'll be creating a method that accepts three parameters and returns the multiplied value of those methods. If compiler supports 128-bit integers, then we use them. If compiler supports long long
, then we use it; otherwise, we need to issue a compile-time error.
What do we need to work with 128-bit integers? Macros that show that they are available and a few typedefs
to have portable type names across platforms.
- Include a header:
#include <boost/config.hpp>
- Now, we need to detect int128 support:
#ifdef BOOST_HAS_INT128
- Add some
typedefs
and implement the method as follows:
typedef boost::int128_type int_t; typedef boost::uint128_type uint_t; inline int_t mul(int_t v1, int_t v2, int_t v3) { return v1 * v2 * v3; }
- For compilers that do not support...