LibreOffice Module tools (master) 1
bigint.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#ifndef INCLUDED_TOOLS_BIGINT_HXX
20#define INCLUDED_TOOLS_BIGINT_HXX
21
22#include <rtl/ustring.hxx>
23#include <tools/toolsdllapi.h>
24#include <tools/long.hxx>
25
26#define MAX_DIGITS 8
27
29{
30private:
31 // we only use one of these two fields at a time
32 union {
33 sal_Int32 nVal;
34 sal_uInt16 nNum[MAX_DIGITS];
35 };
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?
38
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;
50
51public:
53 : nVal(0)
54 , nLen(0)
55 , bIsNeg(false)
56 {
57 }
58
59 BigInt(sal_Int32 nValue)
60 : nVal(nValue)
61 , nLen(0)
62 , bIsNeg(false)
63 {
64 }
65
66#if SAL_TYPES_SIZEOFLONG == 4
67 BigInt(int nValue)
68 : nVal(nValue)
69 , nLen(0)
70 , bIsNeg(false)
71 {
72 }
73#endif
74
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 );
80
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
89
90 bool IsNeg() const;
91 bool IsZero() const;
92 bool IsLong() const { return nLen == 0; }
93
94 void Abs();
95
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 );
102
103 BigInt& operator =( sal_Int32 nValue );
104
105 /* Scale and round value */
106 static tools::Long Scale(tools::Long nVal, tools::Long nMult, tools::Long nDiv);
107
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 );
113
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 );
120
121 friend class Fraction;
122};
123
124inline BigInt::operator sal_Int16() const
125{
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;
130}
131
132inline BigInt::operator sal_uInt16() const
133{
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;
138}
139
140inline BigInt::operator sal_Int32() const
141{
142 if (nLen == 0)
143 return nVal;
144 assert(false && "out of range");
145 return 0;
146}
147
148inline BigInt::operator sal_uInt32() const
149{
150 if ( nLen == 0 && nVal >= 0 )
151 return static_cast<sal_uInt32>(nVal);
152 assert(false && "out of range");
153 return 0;
154}
155
156#if SAL_TYPES_SIZEOFPOINTER == 8
157inline BigInt::operator tools::Long() const
158{
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;
164}
165#endif
166
167inline BigInt& BigInt::operator =( sal_Int32 nValue )
168{
169 nLen = 0;
170 nVal = nValue;
171
172 return *this;
173}
174
175inline bool BigInt::IsNeg() const
176{
177 if ( nLen == 0 )
178 return (nVal < 0);
179 else
180 return bIsNeg;
181}
182
183inline bool BigInt::IsZero() const
184{
185 if ( nLen != 0 )
186 return false;
187 else
188 return (nVal == 0);
189}
190
191inline void BigInt::Abs()
192{
193 if ( nLen != 0 )
194 bIsNeg = false;
195 else if ( nVal < 0 )
196 nVal = -nVal;
197}
198
199inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
200{
201 BigInt aErg( rVal1 );
202 aErg += rVal2;
203 return aErg;
204}
205
206inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
207{
208 BigInt aErg( rVal1 );
209 aErg -= rVal2;
210 return aErg;
211}
212
213inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
214{
215 BigInt aErg( rVal1 );
216 aErg *= rVal2;
217 return aErg;
218}
219
220inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
221{
222 BigInt aErg( rVal1 );
223 aErg /= rVal2;
224 return aErg;
225}
226
227inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
228{
229 BigInt aErg( rVal1 );
230 aErg %= rVal2;
231 return aErg;
232}
233
234inline bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
235{
236 return !(rVal1 == rVal2);
237}
238
239inline bool operator>(const BigInt& rVal1, const BigInt& rVal2) { return rVal2 < rVal1; }
240
241inline bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
242{
243 return !( rVal1 > rVal2);
244}
245
246inline bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
247{
248 return !(rVal1 < rVal2);
249}
250
251#endif
252
253/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:806
bool operator<(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:818
#define MAX_DIGITS
Definition: bigint.hxx:26
BigInt operator+(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:199
BigInt operator*(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:213
BigInt operator-(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:206
BigInt operator/(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:220
bool operator>(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:239
BigInt operator%(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:227
bool operator<=(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:241
bool operator!=(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:234
bool operator>=(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.hxx:246
bool bIsNeg
Definition: bigint.hxx:37
bool IsLong() const
Definition: bigint.hxx:92
sal_uInt8 nLen
Definition: bigint.hxx:36
BigInt(sal_Int32 nValue)
Definition: bigint.hxx:59
bool IsZero() const
Definition: bigint.hxx:183
void Abs()
Definition: bigint.hxx:191
bool IsNeg() const
Definition: bigint.hxx:175
sal_Int32 nVal
Definition: bigint.hxx:33
BigInt & operator=(const BigInt &rVal)
Definition: bigint.cxx:610
BigInt()
Definition: bigint.hxx:52
sal_Int16 nValue
long Long
Definition: long.hxx:34
tools::Rectangle & operator+=(tools::Rectangle &rRect, const SvBorder &rBorder)
Definition: svborder.cxx:22
#define TOOLS_DLLPRIVATE
Definition: toolsdllapi.h:30
#define TOOLS_DLLPUBLIC
Definition: toolsdllapi.h:28
unsigned char sal_uInt8
#define SAL_MAX_UINT16
#define SAL_MIN_INT16
#define SAL_MAX_INT16
#define SAL_WARN_UNUSED