Bump version to 21.06.18.1
[LibreOffice.git] / include / tools / bigint.hxx
bloba8d8575fb53bdbcf001b44f3a92590304f477746
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #define MAX_DIGITS 8
28 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC BigInt
30 private:
31 sal_Int32 nVal;
32 sal_uInt16 nNum[MAX_DIGITS];
33 sal_uInt8 nLen : 5; // current length
34 bool bIsNeg : 1, // Is Sign negative?
35 bIsBig : 1; // if true , value is in nNum array
37 TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
38 TOOLS_DLLPRIVATE void Normalize();
39 TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
40 TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
41 TOOLS_DLLPRIVATE bool IsLess(BigInt const &) const;
42 TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
43 TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
44 TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
45 TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
46 TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
47 TOOLS_DLLPRIVATE bool ABS_IsLess(BigInt const &) const;
49 public:
50 BigInt()
51 : nVal(0)
52 , nLen(0)
53 , bIsNeg(false)
54 , bIsBig(false)
58 BigInt(sal_Int32 nValue)
59 : nVal(nValue)
60 , nLen(0)
61 , bIsNeg(false)
62 , bIsBig(false)
66 #if SAL_TYPES_SIZEOFLONG == 4
67 BigInt(int nValue)
68 : nVal(nValue)
69 , nLen(0)
70 , bIsNeg(false)
71 , bIsBig(false)
74 #endif
76 BigInt( double nVal );
77 BigInt( sal_uInt32 nVal );
78 BigInt( sal_Int64 nVal );
79 BigInt( const BigInt& rBigInt );
80 BigInt( const OUString& rString );
82 operator sal_Int16() const;
83 operator sal_uInt16() const;
84 operator sal_Int32() const;
85 operator sal_uInt32() const;
86 operator double() const;
87 #if SAL_TYPES_SIZEOFPOINTER == 8
88 operator tools::Long() const;
89 #endif
91 bool IsNeg() const;
92 bool IsZero() const;
93 bool IsLong() const { return !bIsBig; }
95 void Abs();
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 );
102 BigInt& operator %=( const BigInt& rVal );
104 BigInt& operator =( sal_Int32 nValue );
106 friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
107 friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
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 );
112 TOOLS_DLLPUBLIC friend bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
113 friend inline bool 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 friend inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
117 friend inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
119 friend class Fraction;
122 inline BigInt::operator sal_Int16() const
124 if ( !bIsBig && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16 )
125 return static_cast<sal_Int16>(nVal);
126 assert(false && "out of range");
127 return 0;
130 inline BigInt::operator sal_uInt16() const
132 if ( !bIsBig && nVal >= 0 && nVal <= SAL_MAX_UINT16 )
133 return static_cast<sal_uInt16>(nVal);
134 assert(false && "out of range");
135 return 0;
138 inline BigInt::operator sal_Int32() const
140 if (!bIsBig)
141 return nVal;
142 assert(false && "out of range");
143 return 0;
146 inline BigInt::operator sal_uInt32() const
148 if ( !bIsBig && nVal >= 0 )
149 return static_cast<sal_uInt32>(nVal);
150 assert(false && "out of range");
151 return 0;
154 #if SAL_TYPES_SIZEOFPOINTER == 8
155 inline BigInt::operator tools::Long() const
157 // Clamp to int32 since long is int32 on Windows.
158 if (!bIsBig)
159 return nVal;
160 assert(false && "out of range");
161 return 0;
163 #endif
165 inline BigInt& BigInt::operator =( sal_Int32 nValue )
167 bIsBig = false;
168 nVal = nValue;
170 return *this;
173 inline bool BigInt::IsNeg() const
175 if ( !bIsBig )
176 return (nVal < 0);
177 else
178 return bIsNeg;
181 inline bool BigInt::IsZero() const
183 if ( bIsBig )
184 return false;
185 else
186 return (nVal == 0);
189 inline void BigInt::Abs()
191 if ( bIsBig )
192 bIsNeg = false;
193 else if ( nVal < 0 )
194 nVal = -nVal;
197 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
199 BigInt aErg( rVal1 );
200 aErg += rVal2;
201 return aErg;
204 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
206 BigInt aErg( rVal1 );
207 aErg -= rVal2;
208 return aErg;
211 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
213 BigInt aErg( rVal1 );
214 aErg *= rVal2;
215 return aErg;
218 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
220 BigInt aErg( rVal1 );
221 aErg /= rVal2;
222 return aErg;
225 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
227 BigInt aErg( rVal1 );
228 aErg %= rVal2;
229 return aErg;
232 inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
234 return !(rVal1 == rVal2);
237 inline bool operator>(const BigInt& rVal1, const BigInt& rVal2) { return rVal2 < rVal1; }
239 inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
241 return !( rVal1 > rVal2);
244 inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
246 return !(rVal1 < rVal2);
249 #endif
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */