sparrow 1.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
int128_t.hpp
Go to the documentation of this file.
1
2// SPARROW: THIS HAS BEEN COPY-PASTED FROM https://github.com/kimwalisch/primesum/blob/master/include/int128_t.hpp
3// PLEASE UPDATE THIS COMMENT IF YOU REPLACE THIS
4// SEE README.md for rational
6// Modification from the original:
7// * disabled io operators
18
19#ifndef INT128_T_HPP
20#define INT128_T_HPP
21
22#include <sstream>
23#include <stdint.h>
24#include <limits>
25#include <type_traits>
26
33#if !defined(INT128_MAX) && \
34 defined(__SIZEOF_INT128__)
35
36#include <ostream>
37#include <string>
38
39namespace primesum {
40
41using int128_t = __int128_t;
42using uint128_t = __uint128_t;
43
44} // namespace
45
46#endif
47
48inline std::ostream& operator<<(std::ostream& stream, primesum::uint128_t n)
49{
50 std::string str;
51
52 while (n > 0)
53 {
54 str += '0' + char(n % 10);
55 n /= 10;
56 }
57 if (str.empty())
58 {
59 str = "0";
60 }
61
62 stream << std::string(str.rbegin(), str.rend());
63 return stream;
64}
65
66inline std::ostream& operator<<(std::ostream& stream, primesum::int128_t n)
67{
68 if (n < 0)
69 {
70 stream << "-";
71 n = -n;
72 }
73 stream << (primesum::uint128_t) n;
74 return stream;
75}
76
77namespace primesum {
78
79inline std::string to_string(const uint128_t& n)
80{
81 std::ostringstream oss;
82 oss << n;
83 return oss.str();
84}
85
86inline std::string to_string(const int128_t& n)
87{
88 std::ostringstream oss;
89 oss << n;
90 return oss.str();
91}
92
97namespace prt {
98
99template <typename T>
101{
102 static constexpr T max()
103 {
104 return std::numeric_limits<T>::max();
105 }
106};
107
108template <>
109struct numeric_limits<int128_t>
110{
111 static constexpr int128_t min() { return ((int128_t) 1) << 127; }
112 static constexpr int128_t max() { return ~min(); }
113};
114
115template <>
116struct numeric_limits<uint128_t>
117{
118 static constexpr uint128_t min() { return 0; }
119 static constexpr uint128_t max() { return ~min(); }
120};
121
122template <typename T>
124{
125 enum
126 {
127 value = std::is_integral<T>::value ||
128 std::is_same<T, int128_t>::value ||
129 std::is_same<T, uint128_t>::value
130 };
131};
132
133template <typename T>
135{
136 enum
137 {
138 value = std::is_signed<T>::value ||
139 std::is_same<T, int128_t>::value
140 };
141};
142
143template <typename T>
145{
146 enum
147 {
148 value = std::is_unsigned<T>::value ||
149 std::is_same<T, uint128_t>::value
150 };
151};
152
153} // namespace prt
154} // namespace primesum
155
156#endif
std::ostream & operator<<(std::ostream &stream, primesum::uint128_t n)
The __int128_t type (GCC/Clang) is not well supported by the C++ standard library (in 2016) so we hav...
Definition int128_t.hpp:48
Portable namespace, includes functions which (unlike the versions form the C++ standard library) work...
Definition int128_t.hpp:97
std::string to_string(const uint128_t &n)
Definition int128_t.hpp:79
primesum::int128_t int128_t
Definition large_int.hpp:84
static constexpr int128_t max()
Definition int128_t.hpp:112
static constexpr int128_t min()
Definition int128_t.hpp:111
static constexpr uint128_t max()
Definition int128_t.hpp:119
static constexpr uint128_t min()
Definition int128_t.hpp:118
static constexpr T max()
Definition int128_t.hpp:102