Use existing rtl_math_stringToDouble
[LibreOffice.git] / include / tools / bigint.hxx
blob14efb7e6924874993d8556a6a87704633fc35190
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 // we only use one of these two fields at a time
32 union {
33 sal_Int32 nVal;
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;
51 public:
52 BigInt()
53 : nVal(0)
54 , nLen(0)
55 , bIsNeg(false)
59 BigInt(sal_Int32 nValue)
60 : nVal(nValue)
61 , nLen(0)
62 , bIsNeg(false)
66 #if SAL_TYPES_SIZEOFLONG == 4
67 BigInt(int nValue)
68 : nVal(nValue)
69 , nLen(0)
70 , bIsNeg(false)
73 #endif
75 BigInt( double nVal );
76 BigInt( sal_uInt32 nVal );
77 BigInt( sal_Int64 nVal );
78 BigInt( const BigInt& rBigInt );
79 BigInt( const OUString& 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;
88 #endif
90 bool IsNeg() const;
91 bool IsZero() const;
92 bool IsLong() const { return nLen == 0; }
94 void Abs();
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");
129 return 0;
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");
137 return 0;
140 inline BigInt::operator sal_Int32() const
142 if (nLen == 0)
143 return nVal;
144 assert(false && "out of range");
145 return 0;
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");
153 return 0;
156 #if SAL_TYPES_SIZEOFPOINTER == 8
157 inline BigInt::operator tools::Long() const
159 // Clamp to int32 since long is int32 on Windows.
160 if (nLen == 0)
161 return nVal;
162 assert(false && "out of range");
163 return 0;
165 #endif
167 inline BigInt& BigInt::operator =( sal_Int32 nValue )
169 nLen = 0;
170 nVal = nValue;
172 return *this;
175 inline bool BigInt::IsNeg() const
177 if ( nLen == 0 )
178 return (nVal < 0);
179 else
180 return bIsNeg;
183 inline bool BigInt::IsZero() const
185 if ( nLen != 0 )
186 return false;
187 else
188 return (nVal == 0);
191 inline void BigInt::Abs()
193 if ( nLen != 0 )
194 bIsNeg = false;
195 else if ( nVal < 0 )
196 nVal = -nVal;
199 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
201 BigInt aErg( rVal1 );
202 aErg += rVal2;
203 return aErg;
206 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
208 BigInt aErg( rVal1 );
209 aErg -= rVal2;
210 return aErg;
213 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
215 BigInt aErg( rVal1 );
216 aErg *= rVal2;
217 return aErg;
220 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
222 BigInt aErg( rVal1 );
223 aErg /= rVal2;
224 return aErg;
227 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
229 BigInt aErg( rVal1 );
230 aErg %= rVal2;
231 return aErg;
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);
251 #endif
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */