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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_TOOLS_BIGINT_HXX
#define INCLUDED_TOOLS_BIGINT_HXX
#include <tools/toolsdllapi.h>
#include <tools/long.hxx>
#include <cassert>
#include <compare>
#include <limits>
#include <string_view>
#define MAX_DIGITS 4
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC BigInt
{
private:
// we only use one of these two fields at a time
union {
sal_Int32 nVal;
sal_uInt32 nNum[MAX_DIGITS];
};
sal_uInt8 nLen; // current length, if 0, data is in nVal, otherwise data is in nNum
bool bIsNeg; // Is Sign negative?
TOOLS_DLLPRIVATE BigInt MakeBig() const; // Create a BigInt with minimal non-0 nLen
TOOLS_DLLPRIVATE void Normalize(); // Use nVal if possible, otherwise use minimal nLen
TOOLS_DLLPRIVATE bool ABS_IsLessBig(BigInt const& rVal) const;
TOOLS_DLLPRIVATE void AddBig(BigInt& rB, BigInt& rRes);
TOOLS_DLLPRIVATE void SubBig(BigInt& rB, BigInt& rRes);
TOOLS_DLLPRIVATE void MultBig(BigInt const& rB, BigInt& rRes) const;
TOOLS_DLLPRIVATE void DivModBig(BigInt const& rB, BigInt* pDiv, BigInt* pMod) const;
public:
BigInt()
: nVal(0)
, nLen(0)
, bIsNeg(false)
{
}
BigInt(sal_Int32 nValue)
: nVal(nValue)
, nLen(0)
, bIsNeg(false)
{
}
BigInt( double nVal );
BigInt( sal_uInt32 nVal );
BigInt( sal_Int64 nVal );
BigInt(const BigInt& rBigInt) = default;
BigInt( std::u16string_view rString );
template <typename N>
requires(std::is_integral_v<N> && std::is_signed_v<N> && sizeof(N) <= sizeof(sal_Int32))
BigInt(N val)
: BigInt(sal_Int32(val))
{
}
template <typename N>
requires(std::is_integral_v<N> && std::is_unsigned_v<N> && sizeof(N) <= sizeof(sal_uInt32))
BigInt(N val)
: BigInt(sal_uInt32(val))
{
}
template <typename N>
requires(std::is_integral_v<N> && std::is_signed_v<N> && sizeof(N) == sizeof(sal_Int64))
BigInt(N val)
: BigInt(sal_Int64(val))
{
}
operator sal_Int16() const;
operator sal_uInt16() const;
operator sal_Int32() const;
operator sal_uInt32() const;
operator double() const;
operator sal_Int64() const;
bool IsNeg() const { return IsBig() ? bIsNeg : nVal < 0; }
bool IsZero() const { return !IsBig() && nVal == 0; }
bool IsBig() const { return nLen != 0; }
BigInt Abs() const;
BigInt& operator =( const BigInt& rVal );
BigInt& operator +=( const BigInt& rVal );
BigInt& operator -=( const BigInt& rVal );
BigInt& operator *=( const BigInt& rVal );
BigInt& operator /=( const BigInt& rVal );
BigInt& operator %=( const BigInt& rVal );
BigInt& operator =( sal_Int32 nValue );
void DivMod(BigInt const& rVal, BigInt* pDiv, BigInt* pMod) const;
/* Scale and round value */
static tools::Long Scale(tools::Long nVal, tools::Long nMult, tools::Long nDiv);
TOOLS_DLLPUBLIC friend bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
TOOLS_DLLPUBLIC friend std::strong_ordering operator<=> ( const BigInt& rVal1, const BigInt& rVal2 );
};
inline BigInt::operator sal_Int16() const
{
if (!IsBig() && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16)
return static_cast<sal_Int16>(nVal);
assert(false && "out of range");
return 0;
}
inline BigInt::operator sal_uInt16() const
{
if (!IsBig() && nVal >= 0 && nVal <= SAL_MAX_UINT16)
return static_cast<sal_uInt16>(nVal);
assert(false && "out of range");
return 0;
}
inline BigInt::operator sal_Int32() const
{
if (!IsBig())
return nVal;
assert(false && "out of range");
return 0;
}
inline BigInt::operator sal_uInt32() const
{
if (!IsBig() && nVal >= 0)
return static_cast<sal_uInt32>(nVal);
assert(false && "out of range");
return 0;
}
inline BigInt::operator sal_Int64() const
{
constexpr sal_uInt64 maxForPosInt64 = std::numeric_limits<sal_Int64>::max();
constexpr sal_uInt64 maxForNegInt64 = std::numeric_limits<sal_Int64>::min();
switch (nLen)
{
case 0:
return nVal;
case 1:
return bIsNeg ? -sal_Int64(nNum[0]) : nNum[0];
case 2:
if (sal_uInt64 n = (sal_uInt64(nNum[1]) << 32) + nNum[0]; bIsNeg && n <= maxForNegInt64)
return -sal_Int64(n); // maxForNegInt64 will convert correctly
else if (!bIsNeg && n <= maxForPosInt64)
return n;
}
assert(false && "out of range");
return 0;
}
inline BigInt& BigInt::operator =( sal_Int32 nValue )
{
nLen = 0;
nVal = nValue;
return *this;
}
inline BigInt BigInt::Abs() const
{
BigInt aRes(*this);
if (IsBig())
aRes.bIsNeg = false;
else if ( nVal < 0 )
{
if (nVal == std::numeric_limits<sal_Int32>::min())
{
aRes.nNum[0] = -sal_Int64(std::numeric_limits<sal_Int32>::min());
aRes.nLen = 1;
aRes.bIsNeg = false;
}
else
aRes.nVal = -nVal;
}
return aRes;
}
inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
{
BigInt aRes( rVal1 );
aRes += rVal2;
return aRes;
}
inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
{
BigInt aRes( rVal1 );
aRes -= rVal2;
return aRes;
}
inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
{
BigInt aRes( rVal1 );
aRes *= rVal2;
return aRes;
}
inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
{
BigInt aRes( rVal1 );
aRes /= rVal2;
return aRes;
}
inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
{
BigInt aRes( rVal1 );
aRes %= rVal2;
return aRes;
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|