• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008-2016 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 /* Internal header file: all symbols have local scope in the DSO */
39 MPD_PRAGMA(MPD_HIDE_SYMBOLS_START)
40 
41 
42 mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
43                         mpd_size_t m, mpd_size_t n);
44 void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
45 mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v);
46 mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v,
47                            mpd_uint_t b);
48 mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n);
49 void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
50                   mpd_size_t m, mpd_size_t n);
51 void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);
52 void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
53                   mpd_size_t m, mpd_size_t n);
54 void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
55                    mpd_uint_t v);
56 mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
57                            mpd_uint_t v);
58 mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
59                            mpd_uint_t v, mpd_uint_t b);
60 mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
61                          mpd_uint_t v);
62 mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
63                            mpd_uint_t v, mpd_uint_t b);
64 int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst,
65                     const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n);
66 void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n,
67                      mpd_size_t m, mpd_size_t shift);
68 mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen,
69                            mpd_size_t shift);
70 
71 
72 
73 #ifdef CONFIG_64
74 extern const mpd_uint_t mprime_rdx;
75 
76 /*
77  * Algorithm from: Division by Invariant Integers using Multiplication,
78  * T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '94
79  * Conference on Programming Language Design and Implementation.
80  *
81  * http://gmplib.org/~tege/divcnst-pldi94.pdf
82  *
83  * Variables from the paper and their translations (See section 8):
84  *
85  *  N := 64
86  *  d := MPD_RADIX
87  *  l := 64
88  *  m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**64
89  *
90  * Since N-l == 0:
91  *
92  *  dnorm := d
93  *  n2 := hi
94  *  n10 := lo
95  *
96  * ACL2 proof: mpd-div-words-r-correct
97  */
98 static inline void
_mpd_div_words_r(mpd_uint_t * q,mpd_uint_t * r,mpd_uint_t hi,mpd_uint_t lo)99 _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
100 {
101     mpd_uint_t n_adj, h, l, t;
102     mpd_uint_t n1_neg;
103 
104     /* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */
105     n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0;
106     /* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */
107     n_adj = lo + (n1_neg & MPD_RADIX);
108 
109     /* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */
110     _mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg);
111     l = l + n_adj;
112     if (l < n_adj) h++;
113     t = h + hi;
114     /* At this point t == qest, with q == qest or q == qest+1:
115      *   1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX
116      */
117 
118     /* t = 2**64-1 - qest = 2**64 - (qest+1) */
119     t = MPD_UINT_MAX - t;
120 
121     /* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */
122     _mpd_mul_words(&h, &l, t, MPD_RADIX);
123     l = l + lo;
124     if (l < lo) h++;
125     h += hi;
126     h -= MPD_RADIX;
127     /* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128)
128      * Case q == qest+1:
129      *     a) h == 0, l == r
130      *     b) q := h - t == qest+1
131      *     c) r := l
132      * Case q == qest:
133      *     a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r)
134      *     b) q := h - t == qest
135      *     c) r := l + MPD_RADIX = r
136      */
137 
138     *q = (h - t);
139     *r = l + (MPD_RADIX & h);
140 }
141 #else
142 static inline void
_mpd_div_words_r(mpd_uint_t * q,mpd_uint_t * r,mpd_uint_t hi,mpd_uint_t lo)143 _mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)
144 {
145     _mpd_div_words(q, r, hi, lo, MPD_RADIX);
146 }
147 #endif
148 
149 
150 /* Multiply two single base MPD_RADIX words, store result in array w[2]. */
151 static inline void
_mpd_singlemul(mpd_uint_t w[2],mpd_uint_t u,mpd_uint_t v)152 _mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v)
153 {
154     mpd_uint_t hi, lo;
155 
156     _mpd_mul_words(&hi, &lo, u, v);
157     _mpd_div_words_r(&w[1], &w[0], hi, lo);
158 }
159 
160 /* Multiply u (len 2) and v (len m, 1 <= m <= 2). */
161 static inline void
_mpd_mul_2_le2(mpd_uint_t w[4],mpd_uint_t u[2],mpd_uint_t v[2],mpd_ssize_t m)162 _mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m)
163 {
164     mpd_uint_t hi, lo;
165 
166     _mpd_mul_words(&hi, &lo, u[0], v[0]);
167     _mpd_div_words_r(&w[1], &w[0], hi, lo);
168 
169     _mpd_mul_words(&hi, &lo, u[1], v[0]);
170     lo = w[1] + lo;
171     if (lo < w[1]) hi++;
172     _mpd_div_words_r(&w[2], &w[1], hi, lo);
173     if (m == 1) return;
174 
175     _mpd_mul_words(&hi, &lo, u[0], v[1]);
176     lo = w[1] + lo;
177     if (lo < w[1]) hi++;
178     _mpd_div_words_r(&w[3], &w[1], hi, lo);
179 
180     _mpd_mul_words(&hi, &lo, u[1], v[1]);
181     lo = w[2] + lo;
182     if (lo < w[2]) hi++;
183     lo = w[3] + lo;
184     if (lo < w[3]) hi++;
185     _mpd_div_words_r(&w[3], &w[2], hi, lo);
186 }
187 
188 
189 /*
190  * Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing
191  * is tested and the coefficient is regarded as "all zero".
192  */
193 static inline int
_mpd_isallzero(const mpd_uint_t * data,mpd_ssize_t len)194 _mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len)
195 {
196     while (--len >= 0) {
197         if (data[len] != 0) return 0;
198     }
199     return 1;
200 }
201 
202 /*
203  * Test if all full words from data[len-1] to data[0] are MPD_RADIX-1
204  * (all nines). Return true if len == 0.
205  */
206 static inline int
_mpd_isallnine(const mpd_uint_t * data,mpd_ssize_t len)207 _mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len)
208 {
209     while (--len >= 0) {
210         if (data[len] != MPD_RADIX-1) return 0;
211     }
212     return 1;
213 }
214 
215 
216 MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */
217 
218 
219 #endif /* BASEARITH_H */
220 
221 
222 
223