summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/config/test/boost_has_int128.ipp
blob: b7e10a40ca5bcaf76730ef747570245125edf253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//  (C) Copyright John Maddock 2012. 
//  (C) Copyright Dynatrace 2017. 
//  Use, modification and distribution are subject to the 
//  Boost Software License, Version 1.0. (See accompanying file 
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/config for most recent version.

//  MACRO:         BOOST_HAS_INT128
//  TITLE:         __int128
//  DESCRIPTION:   The platform supports __int128.

#include <cstdlib>
#include <stdio.h>
#include <limits.h>

namespace boost_has_int128{

#ifdef __GNUC__
__extension__ typedef __int128 my_int128_t;
__extension__ typedef unsigned __int128 my_uint128_t;
#else
typedef __int128 my_int128_t;
typedef unsigned __int128 my_uint128_t;
#endif

my_uint128_t volatile g_ui128 = 0;
unsigned long volatile g_ul = 0;

int test()
{
   my_int128_t si128 = 0;
   (void)&si128;

   // 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...
   // #1: check __int128 size
   if (sizeof(my_uint128_t) < (128 / CHAR_BIT))
   {
      fputs("Type too small.", stderr);
      return 1;
   }

   // #2: check result of computation with __int128
   my_uint128_t p1 = 1;
   my_uint128_t p2 = 1;
   unsigned int i = 0;
   for (; i < 180; i++)
   {
      g_ui128 = p1 + p2;
      if (g_ui128 < p1)
      {
         fputs("Unexpected overflow.", stderr);
         return 1;
      }
      p2 = p1;
      p1 = g_ui128;
   }

   g_ul = static_cast<unsigned long>((g_ui128 >> 92) & 0xFFFFFFFFUL);
   g_ul -= 1216382273UL;
   if (g_ul != 0)
   {
      fputs("Incorrect computation result.", stderr);
      return 1;
   }

   return 0;
}

}