mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / msvcrt / bnum.h
bloba1d3133c3e64e55093b0cc2ffdeeac02968651ab
1 /*
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
19 #ifndef __WINE_BNUM_H
20 #define __WINE_BNUM_H
22 #define EXP_BITS 11
23 #define MANT_BITS 53
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 */
34 struct bnum {
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)
49 DWORD rest = 0;
50 ULONGLONG tmp;
51 int i;
53 /* The limbs number can change by up to 1 so shift <= 29 */
54 assert(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)])
62 b->b++;
65 if(rest) {
66 b->data[bnum_idx(b, b->e)] = rest;
67 b->e++;
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;
71 b->b++;
73 return TRUE;
75 return FALSE;
78 /* Returns TRUE if most significant limb was removed */
79 static inline BOOL bnum_rshift(struct bnum *b, int shift)
81 DWORD tmp, rest = 0;
82 BOOL ret = FALSE;
83 int i;
85 /* Compute LIMB_MAX << shift without accuracy loss */
86 assert(shift <= 9);
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)]) {
93 b->e--;
94 ret = TRUE;
98 if(rest) {
99 if(bnum_idx(b, b->b-1) == bnum_idx(b, b->e)) {
100 if(rest) b->data[bnum_idx(b, b->b)] |= 1;
101 } else {
102 b->b--;
103 b->data[bnum_idx(b, b->b)] = rest;
106 return ret;
109 static inline void bnum_mult(struct bnum *b, int mult)
111 DWORD rest = 0;
112 ULONGLONG tmp;
113 int i;
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)])
123 b->b++;
126 if(rest) {
127 b->data[bnum_idx(b, b->e)] = rest;
128 b->e++;
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;
132 b->b++;
137 #endif /* __WINE_BNUM_H */