sparrow 0.3.0
Loading...
Searching...
No Matches
large_int.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#if defined(__cpp_lib_format)
18# include <format>
19#endif
20
21#ifndef SPARROW_USE_LARGE_INT_PLACEHOLDERS
22
23// disabe warnings -Wold-style-cast sign-conversion for clang and gcc
24# if defined(__clang__) || defined(__GNUC__)
25# pragma GCC diagnostic push
26# pragma GCC diagnostic ignored "-Wold-style-cast"
27# pragma GCC diagnostic ignored "-Wsign-conversion"
28# pragma GCC diagnostic ignored "-Wshadow"
29# pragma GCC diagnostic ignored "-Wsign-conversion"
30# endif
31# include <sparrow/details/3rdparty/large_integers/int128_t.hpp>
32# include <sparrow/details/3rdparty/large_integers/int256_t.hpp>
33
34# if defined(__clang__) || defined(__GNUC__)
35# pragma GCC diagnostic pop
36# endif
37
38#endif
39
40#include <cstdint>
41
42namespace sparrow
43{
44#ifdef SPARROW_USE_LARGE_INT_PLACEHOLDERS
45 constexpr bool large_int_placeholders = true;
46
47 struct int128_t
48 {
49 int128_t() = default;
50
51 std::uint64_t words[2];
52
53 bool operator==(const int128_t& other) const
54 {
55 return words[0] == other.words[0] && words[1] == other.words[1];
56 }
57
58 bool operator!=(const int128_t& other) const
59 {
60 return !(*this == other);
61 }
62 };
63
64 struct int256_t
65 {
66 int256_t() = default;
67 std::uint64_t words[4];
68
69 bool operator==(const int256_t& other) const
70 {
71 return words[0] == other.words[0] && words[1] == other.words[1] && words[2] == other.words[2]
72 && words[3] == other.words[3];
73 }
74
75 bool operator!=(const int256_t& other) const
76 {
77 return !(*this == other);
78 }
79 };
80 template <class T>
81 constexpr bool is_int_placeholder_v = std::is_same_v<T, int128_t> || std::is_same_v<T, int256_t>;
82
83#else
84
85 template <class T>
86 constexpr bool is_int_placeholder_v = false;
87 constexpr bool large_int_placeholders = false;
88 using int128_t = primesum::int128_t;
89 using int256_t = primesum::int256_t;
90
91 template <class T>
92 requires(std::is_same_v<T, int128_t> || std::is_same_v<T, int256_t>)
93 inline std::ostream& operator<<(std::ostream& stream, T n)
94 {
95 std::string str;
96
97 if (n < 0)
98 {
99 stream << "-";
100 n = -n;
101 }
102 while (n > 0)
103 {
104 str.push_back(static_cast<char>('0' + std::int8_t(n % 10)));
105 n /= 10;
106 }
107
108 if (str.empty())
109 {
110 str = "0";
111 }
112
113 stream << std::string(str.rbegin(), str.rend());
114
115 return stream;
116 }
117#endif
118} // namespace sparrow
119
120#if defined(__cpp_lib_format)
121
122# if defined(SPARROW_USE_LARGE_INT_PLACEHOLDERS)
123
124template <>
125struct std::formatter<sparrow::int128_t>
126{
127 constexpr auto parse(std::format_parse_context& ctx)
128 {
129 return ctx.begin(); // Simple implementation
130 }
131
132 auto format(const sparrow::int128_t&, std::format_context& ctx) const
133 {
134 return std::format_to(ctx.out(), "{}", "Integer int128_t TODO");
135 }
136};
137
138# endif
139
140template <>
141struct std::formatter<sparrow::int256_t>
142{
143 constexpr auto parse(std::format_parse_context& ctx)
144 {
145 return ctx.begin(); // Simple implementation
146 }
147
148 auto format(const sparrow::int256_t&, std::format_context& ctx) const
149 {
150 return std::format_to(ctx.out(), "{}", "Integer int256_t TODO");
151 }
152};
153
154#endif
primesum::int128_t int128_t
Definition large_int.hpp:88
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr bool is_int_placeholder_v
Definition large_int.hpp:86
constexpr bool large_int_placeholders
Definition large_int.hpp:87
primesum::int256_t int256_t
Definition large_int.hpp:89