2 * Copyright 2020 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 static const int p10s
[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
27 #define LIMB_DIGITS 9 /* each DWORD stores up to 9 digits */
28 #define LIMB_MAX 1000000000 /* 10^9 */
30 #define BNUM_PREC64 128 /* data size needed to store 64-bit double */
31 #define BNUM_PREC80 2048 /* data size needed to store 80-bit double */
33 /* bnum represents real number with fixed decimal point */
35 int b
; /* least significant digit position */
36 int e
; /* most significant digit position + 1 */
37 int size
; /* data buffer size in DWORDS (power of 2) */
38 DWORD data
[1]; /* circular buffer, base 10 number */
41 static inline int bnum_idx(struct bnum
*b
, int idx
)
43 return idx
& (b
->size
- 1);
46 /* Returns TRUE if new most significant limb was added */
47 static inline BOOL
bnum_lshift(struct bnum
*b
, int shift
)
53 /* The limbs number can change by up to 1 so shift <= 29 */
56 for(i
=b
->b
; i
<b
->e
; i
++) {
57 tmp
= ((ULONGLONG
)b
->data
[bnum_idx(b
, i
)] << shift
) + rest
;
58 rest
= tmp
/ LIMB_MAX
;
59 b
->data
[bnum_idx(b
, i
)] = tmp
% LIMB_MAX
;
61 if(i
== b
->b
&& !b
->data
[bnum_idx(b
, i
)])
66 b
->data
[bnum_idx(b
, b
->e
)] = rest
;
69 if(bnum_idx(b
, b
->b
) == bnum_idx(b
, b
->e
)) {
70 if(b
->data
[bnum_idx(b
, b
->b
)]) b
->data
[bnum_idx(b
, b
->b
+1)] |= 1;
78 /* Returns TRUE if most significant limb was removed */
79 static inline BOOL
bnum_rshift(struct bnum
*b
, int shift
)
85 /* Compute LIMB_MAX << shift without accuracy loss */
88 for(i
=b
->e
-1; i
>=b
->b
; i
--) {
89 tmp
= b
->data
[bnum_idx(b
, i
)] & ((1<<shift
)-1);
90 b
->data
[bnum_idx(b
, i
)] = (b
->data
[bnum_idx(b
, i
)] >> shift
) + rest
;
91 rest
= (LIMB_MAX
>> shift
) * tmp
;
92 if(i
==b
->e
-1 && !b
->data
[bnum_idx(b
, i
)]) {
99 if(bnum_idx(b
, b
->b
-1) == bnum_idx(b
, b
->e
)) {
100 if(rest
) b
->data
[bnum_idx(b
, b
->b
)] |= 1;
103 b
->data
[bnum_idx(b
, b
->b
)] = rest
;
109 static inline void bnum_mult(struct bnum
*b
, int mult
)
115 assert(mult
<= LIMB_MAX
);
117 for(i
=b
->b
; i
<b
->e
; i
++) {
118 tmp
= ((ULONGLONG
)b
->data
[bnum_idx(b
, i
)] * mult
) + rest
;
119 rest
= tmp
/ LIMB_MAX
;
120 b
->data
[bnum_idx(b
, i
)] = tmp
% LIMB_MAX
;
122 if(i
== b
->b
&& !b
->data
[bnum_idx(b
, i
)])
127 b
->data
[bnum_idx(b
, b
->e
)] = rest
;
130 if(bnum_idx(b
, b
->b
) == bnum_idx(b
, b
->e
)) {
131 if(b
->data
[bnum_idx(b
, b
->b
)]) b
->data
[bnum_idx(b
, b
->b
+1)] |= 1;
137 #endif /* __WINE_BNUM_H */