Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(57372)

Side by Side Diff: Modules/_decimal/libmpdec/basearith.h

Issue 7652: Merge C version of decimal into py3k.
Patch Set: Created 7 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/_decimal/libmpdec/basearith.c ('k') | Modules/_decimal/libmpdec/bits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28
29 #ifndef BASEARITH_H
30 #define BASEARITH_H
31
32
33 #include "mpdecimal.h"
34 #include <stdio.h>
35 #include "typearith.h"
36
37
38 mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
39 mpd_size_t m, mpd_size_t n);
40 void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
41 mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v);
42 mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v,
43 mpd_uint_t b);
44 mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n);
45 void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
46 mpd_size_t m, mpd_size_t n);
47 void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
48 void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
49 mpd_size_t m, mpd_size_t n);
50 void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
51 mpd_uint_t v);
52 void _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
53 mpd_uint_t v, mpd_uint_t b);
54 mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
55 mpd_uint_t v);
56 mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
57 mpd_uint_t v, mpd_uint_t b);
58 int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst,
59 const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n);
60 void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n,
61 mpd_size_t m, mpd_size_t shift);
62 mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen,
63 mpd_size_t shift);
64
65
66
67 #ifdef CONFIG_64
68 extern const mpd_uint_t mprime_rdx;
69
70 /*
71 * Algorithm from: Division by Invariant Integers using Multiplication,
72 * T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '94
73 * Conference on Programming Language Design and Implementation.
74 *
75 * http://gmplib.org/~tege/divcnst-pldi94.pdf
76 *
77 * Variables from the paper and their translations (See section 8):
78 *
79 * N := 64
80 * d := MPD_RADIX
81 * l := 64
82 * m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**64
83 *
84 * Since N-l == 0:
85 *
86 * dnorm := d
87 * n2 := hi
88 * n10 := lo
89 *
90 * ACL2 proof: mpd-div-words-r-correct
91 */
92 static inline void
93 _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
94 {
95 mpd_uint_t n_adj, h, l, t;
96 mpd_uint_t n1_neg;
97
98 /* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */
99 n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0;
100 /* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */
101 n_adj = lo + (n1_neg & MPD_RADIX);
102
103 /* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */
104 _mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg);
105 l = l + n_adj;
106 if (l < n_adj) h++;
107 t = h + hi;
108 /* At this point t == qest, with q == qest or q == qest+1:
109 * 1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX
110 */
111
112 /* t = 2**64-1 - qest = 2**64 - (qest+1) */
113 t = MPD_UINT_MAX - t;
114
115 /* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */
116 _mpd_mul_words(&h, &l, t, MPD_RADIX);
117 l = l + lo;
118 if (l < lo) h++;
119 h += hi;
120 h -= MPD_RADIX;
121 /* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128)
122 * Case q == qest+1:
123 * a) h == 0, l == r
124 * b) q := h - t == qest+1
125 * c) r := l
126 * Case q == qest:
127 * a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r)
128 * b) q := h - t == qest
129 * c) r := l + MPD_RADIX = r
130 */
131
132 *q = (h - t);
133 *r = l + (MPD_RADIX & h);
134 }
135 #else
136 static inline void
137 _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
138 {
139 _mpd_div_words(q, r, hi, lo, MPD_RADIX);
140 }
141 #endif
142
143
144 /* Multiply two single base MPD_RADIX words, store result in array w[2]. */
145 static inline void
146 _mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v)
147 {
148 mpd_uint_t hi, lo;
149
150 _mpd_mul_words(&hi, &lo, u, v);
151 _mpd_div_words_r(&w[1], &w[0], hi, lo);
152 }
153
154 /* Multiply u (len 2) and v (len m, 1 <= m <= 2). */
155 static inline void
156 _mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m)
157 {
158 mpd_uint_t hi, lo;
159
160 _mpd_mul_words(&hi, &lo, u[0], v[0]);
161 _mpd_div_words_r(&w[1], &w[0], hi, lo);
162
163 _mpd_mul_words(&hi, &lo, u[1], v[0]);
164 lo = w[1] + lo;
165 if (lo < w[1]) hi++;
166 _mpd_div_words_r(&w[2], &w[1], hi, lo);
167 if (m == 1) return;
168
169 _mpd_mul_words(&hi, &lo, u[0], v[1]);
170 lo = w[1] + lo;
171 if (lo < w[1]) hi++;
172 _mpd_div_words_r(&w[3], &w[1], hi, lo);
173
174 _mpd_mul_words(&hi, &lo, u[1], v[1]);
175 lo = w[2] + lo;
176 if (lo < w[2]) hi++;
177 lo = w[3] + lo;
178 if (lo < w[3]) hi++;
179 _mpd_div_words_r(&w[3], &w[2], hi, lo);
180 }
181
182
183 /*
184 * Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing
185 * is tested and the coefficient is regarded as "all zero".
186 */
187 static inline int
188 _mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len)
189 {
190 while (--len >= 0) {
191 if (data[len] != 0) return 0;
192 }
193 return 1;
194 }
195
196 /*
197 * Test if all full words from data[len-1] to data[0] are MPD_RADIX-1
198 * (all nines). Return true if len == 0.
199 */
200 static inline int
201 _mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len)
202 {
203 while (--len >= 0) {
204 if (data[len] != MPD_RADIX-1) return 0;
205 }
206 return 1;
207 }
208
209
210 #endif /* BASEARITH_H */
211
212
213
OLDNEW
« no previous file with comments | « Modules/_decimal/libmpdec/basearith.c ('k') | Modules/_decimal/libmpdec/bits.h » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+