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
BOOST_INT128_HOST_DEVICE constexpr int128_t operator ""_i128(unsigned long long v) noexcept;
42
-
43
-
BOOST_INT128_HOST_DEVICE constexpr int128_t operator ""_I128(unsigned long long v) noexcept;
44
-
45
37
} // namespace literals
46
38
} // namespace int128
47
39
} // namespace boost
@@ -54,3 +46,22 @@ BOOST_INT128_HOST_DEVICE constexpr int128_t operator ""_I128(unsigned long long
54
46
The macros at the end allow you to write out a 128-bit number like you would with say `UINT64_C` without having to add quotes.
55
47
56
48
See the xref:examples.adoc#examples_construction[construction examples] for usage demonstrations of both literals and macros.
49
+
50
+
== Design Rationale
51
+
52
+
All of the user-defined literals provided by the library are string-form: the operator receives a `const char*` and parses the digit sequence via `from_chars`.
53
+
This holds even for raw numeric tokens like `12345_U128`, which the compiler forwards to the operator as the string `"12345"`.
54
+
The choice is intentional.
55
+
A 128-bit value cannot be represented by `unsigned long long`, so any literal whose magnitude exceeds 2^64 must go through a string-based parse.
56
+
Providing only the string form means there is a single overload that handles every magnitude uniformly, rather than a numeric form for small values and a string form for large ones with a hard cutoff at 2^64.
57
+
The API is the same regardless of how large the value is.
58
+
The trade-off is that every literal pays the cost of parsing, even when the value would fit in a builtin integer.
59
+
For values smaller than 2^64, prefer the constructor:
60
+
61
+
[source, c++]
62
+
----
63
+
constexpr uint128_t small {42U}; // direct conversion from a builtin
64
+
const auto small_literal {42_U128}; // parses "42" via from_chars
65
+
----
66
+
67
+
The two produce the same value, but the constructor avoids the parse and should be used in hot paths or in code where many small constants are constructed.
0 commit comments