]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // (C) Copyright John Maddock 2012. |
b32b8144 | 2 | // (C) Copyright Dynatrace 2017. |
7c673cae FG |
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> | |
b32b8144 FG |
14 | #include <stdio.h> |
15 | #include <limits.h> | |
7c673cae FG |
16 | |
17 | namespace boost_has_int128{ | |
18 | ||
7c673cae | 19 | #ifdef __GNUC__ |
b32b8144 FG |
20 | __extension__ typedef __int128 my_int128_t; |
21 | __extension__ typedef unsigned __int128 my_uint128_t; | |
7c673cae | 22 | #else |
b32b8144 FG |
23 | typedef __int128 my_int128_t; |
24 | typedef unsigned __int128 my_uint128_t; | |
7c673cae | 25 | #endif |
b32b8144 FG |
26 | |
27 | my_uint128_t volatile g_ui128 = 0; | |
28 | unsigned long volatile g_ul = 0; | |
29 | ||
30 | int 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 | ||
7c673cae FG |
67 | return 0; |
68 | } | |
69 | ||
70 | } | |
71 | ||
72 | ||
73 | ||
74 | ||
75 |