• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//  (C) Copyright John Maddock 2012.
2//  (C) Copyright Dynatrace 2017.
3//  Use, modification and distribution are subject to the
4//  Boost Software License, Version 1.0. (See accompanying file
5//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7//  See http://www.boost.org/libs/config for most recent version.
8
9//  MACRO:         BOOST_HAS_INT128
10//  TITLE:         __int128
11//  DESCRIPTION:   The platform supports __int128.
12
13#include <cstdlib>
14#include <stdio.h>
15#include <limits.h>
16
17namespace boost_has_int128{
18
19#ifdef __GNUC__
20__extension__ typedef __int128 my_int128_t;
21__extension__ typedef unsigned __int128 my_uint128_t;
22#else
23typedef __int128 my_int128_t;
24typedef unsigned __int128 my_uint128_t;
25#endif
26
27my_uint128_t volatile g_ui128 = 0;
28unsigned long volatile g_ul = 0;
29
30int test()
31{
32   my_int128_t si128 = 0;
33   (void)&si128;
34
35   // Some compilers have seriously broken __int128 implementations, so we need to do a little more than simply check if we can declare variables with __int128...
36   // #1: check __int128 size
37   if (sizeof(my_uint128_t) < (128 / CHAR_BIT))
38   {
39      fputs("Type too small.", stderr);
40      return 1;
41   }
42
43   // #2: check result of computation with __int128
44   my_uint128_t p1 = 1;
45   my_uint128_t p2 = 1;
46   unsigned int i = 0;
47   for (; i < 180; i++)
48   {
49      g_ui128 = p1 + p2;
50      if (g_ui128 < p1)
51      {
52         fputs("Unexpected overflow.", stderr);
53         return 1;
54      }
55      p2 = p1;
56      p1 = g_ui128;
57   }
58
59   g_ul = static_cast<unsigned long>((g_ui128 >> 92) & 0xFFFFFFFFUL);
60   g_ul -= 1216382273UL;
61   if (g_ul != 0)
62   {
63      fputs("Incorrect computation result.", stderr);
64      return 1;
65   }
66
67   my_uint128_t ii(2), jj(1), kk;
68   kk = ii / jj;
69
70   return 0;
71}
72
73}
74
75
76
77
78
79