ReactOS 0.4.16-dev-2104-gb84fa49
bnum.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Piotr Caban for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef __WINE_BNUM_H
20#define __WINE_BNUM_H
21
22#define EXP_BITS 11
23#define MANT_BITS 53
24
25static const int p10s[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
26
27#define LIMB_DIGITS 9 /* each DWORD stores up to 9 digits */
28#define LIMB_MAX 1000000000 /* 10^9 */
29
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 */
32
33/* bnum represents real number with fixed decimal point */
34struct 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 */
39};
40
41static inline int bnum_idx(struct bnum *b, int idx)
42{
43 return idx & (b->size - 1);
44}
45
46/* Returns TRUE if new most significant limb was added */
47static inline BOOL bnum_lshift(struct bnum *b, int shift)
48{
49 DWORD rest = 0;
50 ULONGLONG tmp;
51 int i;
52
53 /* The limbs number can change by up to 1 so shift <= 29 */
54 assert(shift <= 29);
55
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;
60
61 if(i == b->b && !b->data[bnum_idx(b, i)])
62 b->b++;
63 }
64
65 if(rest) {
66 b->data[bnum_idx(b, b->e)] = rest;
67 b->e++;
68
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++;
72 }
73 return TRUE;
74 }
75 return FALSE;
76}
77
78/* Returns TRUE if most significant limb was removed */
79static inline BOOL bnum_rshift(struct bnum *b, int shift)
80{
81 DWORD tmp, rest = 0;
82 BOOL ret = FALSE;
83 int i;
84
85 /* Compute LIMB_MAX << shift without accuracy loss */
86 assert(shift <= 9);
87
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;
95 }
96 }
97
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;
104 }
105 }
106 return ret;
107}
108
109static inline void bnum_mult(struct bnum *b, int mult)
110{
111 DWORD rest = 0;
112 ULONGLONG tmp;
113 int i;
114
115 assert(mult <= LIMB_MAX);
116
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;
121
122 if(i == b->b && !b->data[bnum_idx(b, i)])
123 b->b++;
124 }
125
126 if(rest) {
127 b->data[bnum_idx(b, b->e)] = rest;
128 b->e++;
129
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++;
133 }
134 }
135}
136
137#endif /* __WINE_BNUM_H */
static const int p10s[]
Definition: bnum.h:25
static BOOL bnum_lshift(struct bnum *b, int shift)
Definition: bnum.h:47
static void bnum_mult(struct bnum *b, int mult)
Definition: bnum.h:109
#define LIMB_MAX
Definition: bnum.h:28
static int bnum_idx(struct bnum *b, int idx)
Definition: bnum.h:41
static BOOL bnum_rshift(struct bnum *b, int shift)
Definition: bnum.h:79
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int idx
Definition: utils.c:41
#define assert(_expr)
Definition: assert.h:32
return ret
Definition: mutex.c:146
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define e
Definition: ke_i.h:82
#define shift
Definition: input.c:1755
Definition: bnum.h:34
int size
Definition: bnum.h:37
int e
Definition: bnum.h:36
int b
Definition: bnum.h:35
uint64_t ULONGLONG
Definition: typedefs.h:67