Bump version to 6.4-15
[LibreOffice.git] / include / tools / bigint.hxx
blob66ce1dd3b1b8855ab8d3861430e5ce28a2dbffc6
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>
25 #define MAX_DIGITS 8
27 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC BigInt
29 private:
30 sal_Int32 nVal;
31 sal_uInt16 nNum[MAX_DIGITS];
32 sal_uInt8 nLen : 5; // current length
33 bool bIsNeg : 1, // Is Sign negative?
34 bIsBig : 1, // sal_True == BigInt
35 bIsSet : 1; // Not "Null" (not "not 0")
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)
55 , bIsSet(false)
59 BigInt(sal_Int32 nValue)
60 : nVal(nValue)
61 , nLen(0)
62 , bIsNeg(false)
63 , bIsBig(false)
64 , bIsSet(true)
68 #if SAL_TYPES_SIZEOFLONG == 4
69 BigInt(int nValue)
70 : nVal(nValue)
71 , nLen(0)
72 , bIsNeg(false)
73 , bIsBig(false)
74 , bIsSet(true)
77 #endif
79 BigInt( double nVal );
80 BigInt( sal_uInt32 nVal );
81 BigInt( sal_Int64 nVal );
82 BigInt( const BigInt& rBigInt );
83 BigInt( const OUString& rString );
85 operator sal_Int16() const;
86 operator sal_uInt16() const;
87 operator sal_Int32() const;
88 operator sal_uInt32() const;
89 operator double() const;
90 #if SAL_TYPES_SIZEOFLONG == 8
91 operator long() const;
92 #endif
94 bool IsSet() const { return bIsSet; }
95 bool IsNeg() const;
96 bool IsZero() const;
97 bool IsLong() const { return !bIsBig; }
99 void Abs();
101 BigInt& operator =( const BigInt& rVal );
102 BigInt& operator +=( const BigInt& rVal );
103 BigInt& operator -=( const BigInt& rVal );
104 BigInt& operator *=( const BigInt& rVal );
105 BigInt& operator /=( const BigInt& rVal );
106 BigInt& operator %=( const BigInt& rVal );
108 BigInt& operator =( sal_Int32 nValue );
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 );
113 friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
114 friend inline BigInt 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 TOOLS_DLLPUBLIC friend bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
119 TOOLS_DLLPUBLIC friend bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
120 friend inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
121 friend inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
123 friend class Fraction;
126 inline BigInt::operator sal_Int16() const
128 if ( !bIsBig && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16 )
129 return static_cast<sal_Int16>(nVal);
130 assert(false && "out of range");
131 return 0;
134 inline BigInt::operator sal_uInt16() const
136 if ( !bIsBig && nVal >= 0 && nVal <= SAL_MAX_UINT16 )
137 return static_cast<sal_uInt16>(nVal);
138 assert(false && "out of range");
139 return 0;
142 inline BigInt::operator sal_Int32() const
144 if (!bIsBig)
145 return nVal;
146 assert(false && "out of range");
147 return 0;
150 inline BigInt::operator sal_uInt32() const
152 if ( !bIsBig && nVal >= 0 )
153 return static_cast<sal_uInt32>(nVal);
154 assert(false && "out of range");
155 return 0;
158 #if SAL_TYPES_SIZEOFLONG == 8
159 inline BigInt::operator long() const
161 // Clamp to int32 since long is int32 on Windows.
162 if (!bIsBig)
163 return nVal;
164 assert(false && "out of range");
165 return 0;
167 #endif
169 inline BigInt& BigInt::operator =( sal_Int32 nValue )
171 bIsSet = true;
172 bIsBig = false;
173 nVal = nValue;
175 return *this;
178 inline bool BigInt::IsNeg() const
180 if ( !bIsBig )
181 return (nVal < 0);
182 else
183 return bIsNeg;
186 inline bool BigInt::IsZero() const
188 if ( bIsBig )
189 return false;
190 else
191 return (nVal == 0);
194 inline void BigInt::Abs()
196 if ( bIsBig )
197 bIsNeg = false;
198 else if ( nVal < 0 )
199 nVal = -nVal;
202 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
204 BigInt aErg( rVal1 );
205 aErg += rVal2;
206 return aErg;
209 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
211 BigInt aErg( rVal1 );
212 aErg -= rVal2;
213 return aErg;
216 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
218 BigInt aErg( rVal1 );
219 aErg *= rVal2;
220 return aErg;
223 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
225 BigInt aErg( rVal1 );
226 aErg /= rVal2;
227 return aErg;
230 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
232 BigInt aErg( rVal1 );
233 aErg %= rVal2;
234 return aErg;
237 inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
239 return !(rVal1 == rVal2);
242 inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
244 return !( rVal1 > rVal2);
247 inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
249 return !(rVal1 < rVal2);
252 #endif
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */