1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_TOOLS_BIGINT_HXX
20 #define INCLUDED_TOOLS_BIGINT_HXX
22 #include <rtl/ustring.hxx>
23 #include <tools/toolsdllapi.h>
24 #include <tools/long.hxx>
28 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC BigInt
31 // we only use one of these two fields at a time
34 sal_uInt16 nNum
[MAX_DIGITS
];
36 sal_uInt8 nLen
: 5; // current length, if 0, data is in nVal, otherwise data is in nNum
37 bool bIsNeg
: 1; // Is Sign negative?
39 TOOLS_DLLPRIVATE
void MakeBigInt(BigInt
const &);
40 TOOLS_DLLPRIVATE
void Normalize();
41 TOOLS_DLLPRIVATE
void Mult(BigInt
const &, sal_uInt16
);
42 TOOLS_DLLPRIVATE
void Div(sal_uInt16
, sal_uInt16
&);
43 TOOLS_DLLPRIVATE
bool IsLess(BigInt
const &) const;
44 TOOLS_DLLPRIVATE
void AddLong(BigInt
&, BigInt
&);
45 TOOLS_DLLPRIVATE
void SubLong(BigInt
&, BigInt
&);
46 TOOLS_DLLPRIVATE
void MultLong(BigInt
const &, BigInt
&) const;
47 TOOLS_DLLPRIVATE
void DivLong(BigInt
const &, BigInt
&) const;
48 TOOLS_DLLPRIVATE
void ModLong(BigInt
const &, BigInt
&) const;
49 TOOLS_DLLPRIVATE
bool ABS_IsLess(BigInt
const &) const;
59 BigInt(sal_Int32 nValue
)
66 #if SAL_TYPES_SIZEOFLONG == 4
75 BigInt( double nVal
);
76 BigInt( sal_uInt32 nVal
);
77 BigInt( sal_Int64 nVal
);
78 BigInt( const BigInt
& rBigInt
);
79 BigInt( std::u16string_view rString
);
81 operator sal_Int16() const;
82 operator sal_uInt16() const;
83 operator sal_Int32() const;
84 operator sal_uInt32() const;
85 operator double() const;
86 #if SAL_TYPES_SIZEOFPOINTER == 8
87 operator tools::Long() const;
92 bool IsLong() const { return nLen
== 0; }
96 BigInt
& operator =( const BigInt
& rVal
);
97 BigInt
& operator +=( const BigInt
& rVal
);
98 BigInt
& operator -=( const BigInt
& rVal
);
99 BigInt
& operator *=( const BigInt
& rVal
);
100 BigInt
& operator /=( const BigInt
& rVal
);
101 BigInt
& operator %=( const BigInt
& rVal
);
103 BigInt
& operator =( sal_Int32 nValue
);
105 /* Scale and round value */
106 static tools::Long
Scale(tools::Long nVal
, tools::Long nMult
, tools::Long nDiv
);
108 friend inline BigInt
operator +( const BigInt
& rVal1
, const BigInt
& rVal2
);
109 friend inline BigInt
operator -( const BigInt
& rVal1
, const BigInt
& rVal2
);
110 friend inline BigInt
operator *( const BigInt
& rVal1
, const BigInt
& rVal2
);
111 friend inline BigInt
operator /( const BigInt
& rVal1
, const BigInt
& rVal2
);
112 friend inline BigInt
operator %( const BigInt
& rVal1
, const BigInt
& rVal2
);
114 TOOLS_DLLPUBLIC
friend bool operator==( const BigInt
& rVal1
, const BigInt
& rVal2
);
115 friend inline bool operator!=( const BigInt
& rVal1
, const BigInt
& rVal2
);
116 TOOLS_DLLPUBLIC
friend bool operator< ( const BigInt
& rVal1
, const BigInt
& rVal2
);
117 friend inline bool operator> ( const BigInt
& rVal1
, const BigInt
& rVal2
);
118 friend inline bool operator<=( const BigInt
& rVal1
, const BigInt
& rVal2
);
119 friend inline bool operator>=( const BigInt
& rVal1
, const BigInt
& rVal2
);
121 friend class Fraction
;
124 inline BigInt::operator sal_Int16() const
126 if ( nLen
== 0 && nVal
>= SAL_MIN_INT16
&& nVal
<= SAL_MAX_INT16
)
127 return static_cast<sal_Int16
>(nVal
);
128 assert(false && "out of range");
132 inline BigInt::operator sal_uInt16() const
134 if ( nLen
== 0 && nVal
>= 0 && nVal
<= SAL_MAX_UINT16
)
135 return static_cast<sal_uInt16
>(nVal
);
136 assert(false && "out of range");
140 inline BigInt::operator sal_Int32() const
144 assert(false && "out of range");
148 inline BigInt::operator sal_uInt32() const
150 if ( nLen
== 0 && nVal
>= 0 )
151 return static_cast<sal_uInt32
>(nVal
);
152 assert(false && "out of range");
156 #if SAL_TYPES_SIZEOFPOINTER == 8
157 inline BigInt::operator tools::Long() const
159 // Clamp to int32 since long is int32 on Windows.
162 assert(false && "out of range");
167 inline BigInt
& BigInt::operator =( sal_Int32 nValue
)
175 inline bool BigInt::IsNeg() const
183 inline bool BigInt::IsZero() const
191 inline void BigInt::Abs()
199 inline BigInt
operator+( const BigInt
&rVal1
, const BigInt
&rVal2
)
201 BigInt
aErg( rVal1
);
206 inline BigInt
operator-( const BigInt
&rVal1
, const BigInt
&rVal2
)
208 BigInt
aErg( rVal1
);
213 inline BigInt
operator*( const BigInt
&rVal1
, const BigInt
&rVal2
)
215 BigInt
aErg( rVal1
);
220 inline BigInt
operator/( const BigInt
&rVal1
, const BigInt
&rVal2
)
222 BigInt
aErg( rVal1
);
227 inline BigInt
operator%( const BigInt
&rVal1
, const BigInt
&rVal2
)
229 BigInt
aErg( rVal1
);
234 inline bool operator!=( const BigInt
& rVal1
, const BigInt
& rVal2
)
236 return !(rVal1
== rVal2
);
239 inline bool operator>(const BigInt
& rVal1
, const BigInt
& rVal2
) { return rVal2
< rVal1
; }
241 inline bool operator<=( const BigInt
& rVal1
, const BigInt
& rVal2
)
243 return !( rVal1
> rVal2
);
246 inline bool operator>=( const BigInt
& rVal1
, const BigInt
& rVal2
)
248 return !(rVal1
< rVal2
);
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */