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