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
23 #include <rtl/ustring.hxx>
24 #include <tools/toolsdllapi.h>
25 #include <tools/solar.h>
33 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED BigInt
37 unsigned short nNum
[MAX_DIGITS
];
38 sal_uInt8 nLen
: 5; // current length
39 bool bIsNeg
: 1, // Is Sign negative?
40 bIsBig
: 1, // sal_True == BigInt
41 bIsSet
: 1; // Not "Null" (not "not 0")
43 TOOLS_DLLPRIVATE
void MakeBigInt(BigInt
const &);
44 TOOLS_DLLPRIVATE
void Normalize();
45 TOOLS_DLLPRIVATE
void Mult(BigInt
const &, sal_uInt16
);
46 TOOLS_DLLPRIVATE
void Div(sal_uInt16
, sal_uInt16
&);
47 TOOLS_DLLPRIVATE
bool IsLess(BigInt
const &) const;
48 TOOLS_DLLPRIVATE
void AddLong(BigInt
&, BigInt
&);
49 TOOLS_DLLPRIVATE
void SubLong(BigInt
&, BigInt
&);
50 TOOLS_DLLPRIVATE
void MultLong(BigInt
const &, BigInt
&) const;
51 TOOLS_DLLPRIVATE
void DivLong(BigInt
const &, BigInt
&) const;
52 TOOLS_DLLPRIVATE
void ModLong(BigInt
const &, BigInt
&) const;
53 TOOLS_DLLPRIVATE
bool ABS_IsLess(BigInt
const &) const;
92 BigInt( double nVal
);
94 BigInt(sal_uInt16 nValue
)
103 BigInt( sal_uInt32 nVal
);
104 #if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG
105 BigInt( long long nVal
);
107 BigInt( const BigInt
& rBigInt
);
108 BigInt( const OUString
& rString
);
110 operator short() const;
111 operator long() const;
112 operator int() const;
113 operator double() const;
114 operator sal_uInt16() const;
115 operator sal_uIntPtr() const;
117 void Set( bool bSet
) { bIsSet
= bSet
; }
119 bool IsSet() const { return (bool)bIsSet
; }
123 bool IsLong() const { return !((bool)bIsBig
); }
126 BigInt
& operator =( const BigInt
& rVal
);
127 BigInt
& operator +=( const BigInt
& rVal
);
128 BigInt
& operator -=( const BigInt
& rVal
);
129 BigInt
& operator *=( const BigInt
& rVal
);
130 BigInt
& operator /=( const BigInt
& rVal
);
131 BigInt
& operator %=( const BigInt
& rVal
);
133 BigInt
& operator =( const short nValue
);
134 BigInt
& operator =( const long nValue
);
135 BigInt
& operator =( const int nValue
);
136 BigInt
& operator =( const sal_uInt16 nValue
);
138 friend inline BigInt
operator +( const BigInt
& rVal1
, const BigInt
& rVal2
);
139 friend inline BigInt
operator -( const BigInt
& rVal1
, const BigInt
& rVal2
);
140 friend inline BigInt
operator *( const BigInt
& rVal1
, const BigInt
& rVal2
);
141 friend inline BigInt
operator /( const BigInt
& rVal1
, const BigInt
& rVal2
);
142 friend inline BigInt
operator %( const BigInt
& rVal1
, const BigInt
& rVal2
);
144 TOOLS_DLLPUBLIC
friend bool operator==( const BigInt
& rVal1
, const BigInt
& rVal2
);
145 friend inline bool operator!=( const BigInt
& rVal1
, const BigInt
& rVal2
);
146 TOOLS_DLLPUBLIC
friend bool operator< ( const BigInt
& rVal1
, const BigInt
& rVal2
);
147 TOOLS_DLLPUBLIC
friend bool operator> ( const BigInt
& rVal1
, const BigInt
& rVal2
);
148 friend inline bool operator<=( const BigInt
& rVal1
, const BigInt
& rVal2
);
149 friend inline bool operator>=( const BigInt
& rVal1
, const BigInt
& rVal2
);
151 friend class Fraction
;
154 inline BigInt::operator short() const
156 if ( !bIsBig
&& nVal
>= SHRT_MIN
&& nVal
<= SHRT_MAX
)
162 inline BigInt::operator long() const
170 inline BigInt::operator int() const
172 if ( !bIsBig
&& (nVal
== (long)(int)nVal
) )
178 inline BigInt::operator sal_uInt16() const
180 if ( !bIsBig
&& nVal
>= 0 && nVal
<= (long)USHRT_MAX
)
181 return (sal_uInt16
)nVal
;
186 inline BigInt
& BigInt::operator =( const short nValue
)
195 inline BigInt
& BigInt::operator =( const long nValue
)
204 inline BigInt
& BigInt::operator =( const int nValue
)
213 inline BigInt
& BigInt::operator =( const sal_uInt16 nValue
)
222 inline bool BigInt::IsNeg() const
230 inline bool BigInt::IsZero() const
238 inline bool BigInt::IsOne() const
246 inline void BigInt::Abs()
254 inline BigInt
operator+( const BigInt
&rVal1
, const BigInt
&rVal2
)
256 BigInt
aErg( rVal1
);
261 inline BigInt
operator-( const BigInt
&rVal1
, const BigInt
&rVal2
)
263 BigInt
aErg( rVal1
);
268 inline BigInt
operator*( const BigInt
&rVal1
, const BigInt
&rVal2
)
270 BigInt
aErg( rVal1
);
275 inline BigInt
operator/( const BigInt
&rVal1
, const BigInt
&rVal2
)
277 BigInt
aErg( rVal1
);
282 inline BigInt
operator%( const BigInt
&rVal1
, const BigInt
&rVal2
)
284 BigInt
aErg( rVal1
);
289 inline bool operator!=( const BigInt
& rVal1
, const BigInt
& rVal2
)
291 return !(rVal1
== rVal2
);
294 inline bool operator<=( const BigInt
& rVal1
, const BigInt
& rVal2
)
296 return !( rVal1
> rVal2
);
299 inline bool operator>=( const BigInt
& rVal1
, const BigInt
& rVal2
)
301 return !(rVal1
< rVal2
);
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */