Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
int128/example/construction.cpp at develop · boostorg/int128 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
boostorg
/
int128
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
19
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Files
Expand file tree
develop
Breadcrumbs
int128
/
example
/
construction.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
135 lines (105 loc) · 6.27 KB
develop
Breadcrumbs
int128
/
example
/
construction.cpp
Copy path
Top
File metadata and controls
Code
Blame
135 lines (105 loc) · 6.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
Copyright 2025 Matt Borland
//
Distributed under the Boost Software License, Version 1.0.
//
https://www.boost.org/LICENSE_1_0.txt
#
include
<
boost/int128/int128.hpp
>
#
include
<
boost/int128/literals.hpp
>
#
include
<
boost/int128/iostream.hpp
>
#
include
<
iostream
>
#
include
<
limits
>
#
include
<
sstream
>
int
main
()
{
using
boost::int128::uint128;
using
boost::int128::int128;
std::cout <<
"
=== uint128 Construction ===
"
<< std::endl;
//
1) From a builtin integer type
constexpr
uint128 from_builtin {
42U
};
std::cout <<
"
From builtin (42U):
"
<< from_builtin << std::endl;
//
2) From high and low 64-bit values (high, low)
constexpr
uint128 from_parts {
UINT64_C
(
0x1
),
UINT64_C
(
0x0
)};
//
2^64
std::cout <<
"
From parts (1, 0) = 2^64:
"
<< from_parts << std::endl;
constexpr
uint128 max_value {
UINT64_C
(
0xFFFFFFFFFFFFFFFF
),
UINT64_C
(
0xFFFFFFFFFFFFFFFF
)};
std::cout <<
"
From parts (max, max):
"
<< max_value << std::endl;
std::cout <<
"
Equals numeric_limits max?
"
<< std::boolalpha
<< (max_value == std::numeric_limits<uint128>::
max
()) << std::endl;
//
3) From user-defined literals.
//
The library provides only string-form UDLs
//
For small values like this a string is still parsed rather than direct construction
//
Using the constructors for values that fit in (unsigned) long long should be preferred for performance
using
namespace
boost
::int128::literals
;
const
auto
small_literal {12345_U128};
std::cout <<
"
From literal 12345_U128:
"
<< small_literal << std::endl;
//
4) From macro (like UINT64_C but for 128-bit), good for values that exceed unsigned long long
const
auto
from_macro {
BOOST_INT128_UINT128_C
(
340282366920938463463374607431768211455
)};
std::cout <<
"
From BOOST_INT128_UINT128_C(max):
"
<< from_macro << std::endl;
//
5) From input stream
std::stringstream ss;
ss.
str
(
"
12345678901234567890123456789
"
);
uint128 from_stream;
ss >> from_stream;
std::cout <<
"
From stringstream:
"
<< from_stream << std::endl;
std::cout <<
"
\n
=== int128 Construction ===
"
<< std::endl;
//
Signed from builtin
constexpr
int128 signed_builtin {-
42
};
std::cout <<
"
From builtin (-42):
"
<< signed_builtin << std::endl;
//
Signed from parts. Both words are stored unsigned, but the constructor takes
//
the high word signed; read it back with signed_high().
constexpr
int128 min_value {
INT64_MIN
,
0
};
std::cout <<
"
From parts (INT64_MIN, 0):
"
<< min_value << std::endl;
std::cout <<
"
Equals numeric_limits min?
"
<< (min_value == std::numeric_limits<int128>::
min
()) << std::endl;
//
Signed literals. Values that fit in unsigned long long can be written
//
directly; the leading minus is parsed as a unary operator on the
//
literal result (lowercase and uppercase suffixes both work):
const
auto
negative_literal {-12345_i128};
std::cout <<
"
From literal -12345_i128:
"
<< negative_literal << std::endl;
const
auto
positive_literal {12345_I128};
std::cout <<
"
From literal 12345_I128:
"
<< positive_literal << std::endl;
//
For magnitudes beyond unsigned long long you can use the macro or a string literal
const
auto
large_signed {
BOOST_INT128_INT128_C
(-
99999999999999999999
)};
std::cout <<
"
From BOOST_INT128_INT128_C(-99999999999999999999):
"
<< large_signed << std::endl;
const
auto
large_signed_string {
"
-99999999999999999999
"
_i128};
std::cout <<
"
From string literal:
"
<< large_signed_string << std::endl;
//
Signed macro
const
auto
from_signed_macro {
BOOST_INT128_INT128_C
(-
170141183460469231731687303715884105728
)};
std::cout <<
"
From BOOST_INT128_INT128_C(min):
"
<< from_signed_macro << std::endl;
std::cout <<
"
\n
=== Default and Copy Construction ===
"
<< std::endl;
//
Default construction (zero-initialized)
constexpr
uint128 default_constructed {};
std::cout <<
"
Default constructed:
"
<< default_constructed << std::endl;
//
Copy construction
const
uint128 copied {from_macro};
std::cout <<
"
Copy constructed:
"
<< copied << std::endl;
std::cout <<
"
\n
=== Floating-Point Construction ===
"
<< std::endl;
//
Floating-point construction truncates toward zero, matching the behavior of
//
a static_cast from a floating-point type to a built-in integer.
constexpr
uint128 from_double {
12345.9
};
std::cout <<
"
uint128 from 12345.9 (truncated):
"
<< from_double << std::endl;
constexpr
int128 from_negative_double {-
12345.9
};
std::cout <<
"
int128 from -12345.9 (truncated toward zero):
"
<< from_negative_double << std::endl;
//
Values that exceed the 64-bit range are routed through the full 128-bit decomposition.
const
double
two_to_the_100 {
1.2676506002282294e30
};
//
2^100
const
uint128 large_from_double {two_to_the_100};
std::cout <<
"
uint128 from 2^100:
"
<< large_from_double << std::endl;
std::cout <<
"
\n
=== Floating-Point Edge Cases ===
"
<< std::endl;
//
NaN yields zero for both signed and unsigned (mirrors libgcc's __fix(uns)Xfti).
const
double
nan_value {std::numeric_limits<
double
>::
quiet_NaN
()};
const
uint128 unsigned_from_nan {nan_value};
const
int128 signed_from_nan {nan_value};
std::cout <<
"
uint128 from NaN:
"
<< unsigned_from_nan << std::endl;
std::cout <<
"
int128 from NaN:
"
<< signed_from_nan << std::endl;
//
Negative values are clamped to zero when constructing uint128.
const
uint128 unsigned_from_negative {-
1.0
};
std::cout <<
"
uint128 from -1.0 (clamped to zero):
"
<< unsigned_from_negative << std::endl;
//
Positive overflow saturates: anything >= 2^128 (including +infinity) becomes UINT128_MAX.
const
double
infinity {std::numeric_limits<
double
>::
infinity
()};
const
uint128 saturated_unsigned {infinity};
std::cout <<
"
uint128 from +infinity (saturates to UINT128_MAX):
"
<< saturated_unsigned << std::endl;
//
For int128, values >= 2^127 saturate to INT128_MAX and values <= -2^127 saturate to INT128_MIN.
const
double
huge {
1e40
};
//
Well beyond 2^127 (~ 1.7e38)
const
int128 saturated_positive {huge};
const
int128 saturated_negative {-huge};
std::cout <<
"
int128 from 1e40 (saturates to INT128_MAX):
"
<< saturated_positive << std::endl;
std::cout <<
"
int128 from -1e40 (saturates to INT128_MIN):
"
<< saturated_negative << std::endl;
return
0
;
}
You can’t perform that action at this time.