1 | n/a | /* |
---|
2 | n/a | * Copyright (c) 2008-2016 Stefan Krah. All rights reserved. |
---|
3 | n/a | * |
---|
4 | n/a | * Redistribution and use in source and binary forms, with or without |
---|
5 | n/a | * modification, are permitted provided that the following conditions |
---|
6 | n/a | * are met: |
---|
7 | n/a | * |
---|
8 | n/a | * 1. Redistributions of source code must retain the above copyright |
---|
9 | n/a | * notice, this list of conditions and the following disclaimer. |
---|
10 | n/a | * |
---|
11 | n/a | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | n/a | * notice, this list of conditions and the following disclaimer in the |
---|
13 | n/a | * documentation and/or other materials provided with the distribution. |
---|
14 | n/a | * |
---|
15 | n/a | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
---|
16 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | n/a | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | n/a | * SUCH DAMAGE. |
---|
26 | n/a | */ |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | #include "mpdecimal.h" |
---|
30 | n/a | #include <stdio.h> |
---|
31 | n/a | #include <stdlib.h> |
---|
32 | n/a | #include <string.h> |
---|
33 | n/a | #include <limits.h> |
---|
34 | n/a | #include <math.h> |
---|
35 | n/a | #include "basearith.h" |
---|
36 | n/a | #include "bits.h" |
---|
37 | n/a | #include "convolute.h" |
---|
38 | n/a | #include "crt.h" |
---|
39 | n/a | #include "mpalloc.h" |
---|
40 | n/a | #include "typearith.h" |
---|
41 | n/a | #include "umodarith.h" |
---|
42 | n/a | |
---|
43 | n/a | #ifdef PPRO |
---|
44 | n/a | #if defined(_MSC_VER) |
---|
45 | n/a | #include <float.h> |
---|
46 | n/a | #pragma float_control(precise, on) |
---|
47 | n/a | #pragma fenv_access(on) |
---|
48 | n/a | #elif !defined(__OpenBSD__) && !defined(__NetBSD__) |
---|
49 | n/a | /* C99 */ |
---|
50 | n/a | #include <fenv.h> |
---|
51 | n/a | #pragma STDC FENV_ACCESS ON |
---|
52 | n/a | #endif |
---|
53 | n/a | #endif |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | #if defined(_MSC_VER) |
---|
57 | n/a | #define ALWAYS_INLINE __forceinline |
---|
58 | n/a | #elif defined(LEGACY_COMPILER) |
---|
59 | n/a | #define ALWAYS_INLINE |
---|
60 | n/a | #undef inline |
---|
61 | n/a | #define inline |
---|
62 | n/a | #else |
---|
63 | n/a | #ifdef TEST_COVERAGE |
---|
64 | n/a | #define ALWAYS_INLINE |
---|
65 | n/a | #else |
---|
66 | n/a | #define ALWAYS_INLINE inline __attribute__ ((always_inline)) |
---|
67 | n/a | #endif |
---|
68 | n/a | #endif |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | #define MPD_NEWTONDIV_CUTOFF 1024L |
---|
72 | n/a | |
---|
73 | n/a | #define MPD_NEW_STATIC(name, flags, exp, digits, len) \ |
---|
74 | n/a | mpd_uint_t name##_data[MPD_MINALLOC_MAX]; \ |
---|
75 | n/a | mpd_t name = {flags|MPD_STATIC|MPD_STATIC_DATA, exp, digits, \ |
---|
76 | n/a | len, MPD_MINALLOC_MAX, name##_data} |
---|
77 | n/a | |
---|
78 | n/a | #define MPD_NEW_CONST(name, flags, exp, digits, len, alloc, initval) \ |
---|
79 | n/a | mpd_uint_t name##_data[alloc] = {initval}; \ |
---|
80 | n/a | mpd_t name = {flags|MPD_STATIC|MPD_CONST_DATA, exp, digits, \ |
---|
81 | n/a | len, alloc, name##_data} |
---|
82 | n/a | |
---|
83 | n/a | #define MPD_NEW_SHARED(name, a) \ |
---|
84 | n/a | mpd_t name = {(a->flags&~MPD_DATAFLAGS)|MPD_STATIC|MPD_SHARED_DATA, \ |
---|
85 | n/a | a->exp, a->digits, a->len, a->alloc, a->data} |
---|
86 | n/a | |
---|
87 | n/a | |
---|
88 | n/a | static mpd_uint_t data_one[1] = {1}; |
---|
89 | n/a | static mpd_uint_t data_zero[1] = {0}; |
---|
90 | n/a | static const mpd_t one = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_one}; |
---|
91 | n/a | static const mpd_t minus_one = {MPD_NEG|MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, |
---|
92 | n/a | data_one}; |
---|
93 | n/a | static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero}; |
---|
94 | n/a | |
---|
95 | n/a | static inline void _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx, |
---|
96 | n/a | uint32_t *status); |
---|
97 | n/a | static void _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a, |
---|
98 | n/a | mpd_ssize_t exp); |
---|
99 | n/a | static inline mpd_ssize_t _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size); |
---|
100 | n/a | |
---|
101 | n/a | static int _mpd_cmp_abs(const mpd_t *a, const mpd_t *b); |
---|
102 | n/a | |
---|
103 | n/a | static void _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
104 | n/a | const mpd_context_t *ctx, uint32_t *status); |
---|
105 | n/a | static inline void _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
106 | n/a | const mpd_context_t *ctx, uint32_t *status); |
---|
107 | n/a | static void _mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a, |
---|
108 | n/a | const mpd_t *b, uint32_t *status); |
---|
109 | n/a | static inline void _mpd_qpow_uint(mpd_t *result, const mpd_t *base, |
---|
110 | n/a | mpd_uint_t exp, uint8_t resultsign, |
---|
111 | n/a | const mpd_context_t *ctx, uint32_t *status); |
---|
112 | n/a | |
---|
113 | n/a | static mpd_uint_t mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n); |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | /******************************************************************************/ |
---|
117 | n/a | /* Version */ |
---|
118 | n/a | /******************************************************************************/ |
---|
119 | n/a | |
---|
120 | n/a | const char * |
---|
121 | n/a | mpd_version(void) |
---|
122 | n/a | { |
---|
123 | n/a | return MPD_VERSION; |
---|
124 | n/a | } |
---|
125 | n/a | |
---|
126 | n/a | |
---|
127 | n/a | /******************************************************************************/ |
---|
128 | n/a | /* Performance critical inline functions */ |
---|
129 | n/a | /******************************************************************************/ |
---|
130 | n/a | |
---|
131 | n/a | #ifdef CONFIG_64 |
---|
132 | n/a | /* Digits in a word, primarily useful for the most significant word. */ |
---|
133 | n/a | ALWAYS_INLINE int |
---|
134 | n/a | mpd_word_digits(mpd_uint_t word) |
---|
135 | n/a | { |
---|
136 | n/a | if (word < mpd_pow10[9]) { |
---|
137 | n/a | if (word < mpd_pow10[4]) { |
---|
138 | n/a | if (word < mpd_pow10[2]) { |
---|
139 | n/a | return (word < mpd_pow10[1]) ? 1 : 2; |
---|
140 | n/a | } |
---|
141 | n/a | return (word < mpd_pow10[3]) ? 3 : 4; |
---|
142 | n/a | } |
---|
143 | n/a | if (word < mpd_pow10[6]) { |
---|
144 | n/a | return (word < mpd_pow10[5]) ? 5 : 6; |
---|
145 | n/a | } |
---|
146 | n/a | if (word < mpd_pow10[8]) { |
---|
147 | n/a | return (word < mpd_pow10[7]) ? 7 : 8; |
---|
148 | n/a | } |
---|
149 | n/a | return 9; |
---|
150 | n/a | } |
---|
151 | n/a | if (word < mpd_pow10[14]) { |
---|
152 | n/a | if (word < mpd_pow10[11]) { |
---|
153 | n/a | return (word < mpd_pow10[10]) ? 10 : 11; |
---|
154 | n/a | } |
---|
155 | n/a | if (word < mpd_pow10[13]) { |
---|
156 | n/a | return (word < mpd_pow10[12]) ? 12 : 13; |
---|
157 | n/a | } |
---|
158 | n/a | return 14; |
---|
159 | n/a | } |
---|
160 | n/a | if (word < mpd_pow10[18]) { |
---|
161 | n/a | if (word < mpd_pow10[16]) { |
---|
162 | n/a | return (word < mpd_pow10[15]) ? 15 : 16; |
---|
163 | n/a | } |
---|
164 | n/a | return (word < mpd_pow10[17]) ? 17 : 18; |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | return (word < mpd_pow10[19]) ? 19 : 20; |
---|
168 | n/a | } |
---|
169 | n/a | #else |
---|
170 | n/a | ALWAYS_INLINE int |
---|
171 | n/a | mpd_word_digits(mpd_uint_t word) |
---|
172 | n/a | { |
---|
173 | n/a | if (word < mpd_pow10[4]) { |
---|
174 | n/a | if (word < mpd_pow10[2]) { |
---|
175 | n/a | return (word < mpd_pow10[1]) ? 1 : 2; |
---|
176 | n/a | } |
---|
177 | n/a | return (word < mpd_pow10[3]) ? 3 : 4; |
---|
178 | n/a | } |
---|
179 | n/a | if (word < mpd_pow10[6]) { |
---|
180 | n/a | return (word < mpd_pow10[5]) ? 5 : 6; |
---|
181 | n/a | } |
---|
182 | n/a | if (word < mpd_pow10[8]) { |
---|
183 | n/a | return (word < mpd_pow10[7]) ? 7 : 8; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | return (word < mpd_pow10[9]) ? 9 : 10; |
---|
187 | n/a | } |
---|
188 | n/a | #endif |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | /* Adjusted exponent */ |
---|
192 | n/a | ALWAYS_INLINE mpd_ssize_t |
---|
193 | n/a | mpd_adjexp(const mpd_t *dec) |
---|
194 | n/a | { |
---|
195 | n/a | return (dec->exp + dec->digits) - 1; |
---|
196 | n/a | } |
---|
197 | n/a | |
---|
198 | n/a | /* Etiny */ |
---|
199 | n/a | ALWAYS_INLINE mpd_ssize_t |
---|
200 | n/a | mpd_etiny(const mpd_context_t *ctx) |
---|
201 | n/a | { |
---|
202 | n/a | return ctx->emin - (ctx->prec - 1); |
---|
203 | n/a | } |
---|
204 | n/a | |
---|
205 | n/a | /* Etop: used for folding down in IEEE clamping */ |
---|
206 | n/a | ALWAYS_INLINE mpd_ssize_t |
---|
207 | n/a | mpd_etop(const mpd_context_t *ctx) |
---|
208 | n/a | { |
---|
209 | n/a | return ctx->emax - (ctx->prec - 1); |
---|
210 | n/a | } |
---|
211 | n/a | |
---|
212 | n/a | /* Most significant word */ |
---|
213 | n/a | ALWAYS_INLINE mpd_uint_t |
---|
214 | n/a | mpd_msword(const mpd_t *dec) |
---|
215 | n/a | { |
---|
216 | n/a | assert(dec->len > 0); |
---|
217 | n/a | return dec->data[dec->len-1]; |
---|
218 | n/a | } |
---|
219 | n/a | |
---|
220 | n/a | /* Most significant digit of a word */ |
---|
221 | n/a | inline mpd_uint_t |
---|
222 | n/a | mpd_msd(mpd_uint_t word) |
---|
223 | n/a | { |
---|
224 | n/a | int n; |
---|
225 | n/a | |
---|
226 | n/a | n = mpd_word_digits(word); |
---|
227 | n/a | return word / mpd_pow10[n-1]; |
---|
228 | n/a | } |
---|
229 | n/a | |
---|
230 | n/a | /* Least significant digit of a word */ |
---|
231 | n/a | ALWAYS_INLINE mpd_uint_t |
---|
232 | n/a | mpd_lsd(mpd_uint_t word) |
---|
233 | n/a | { |
---|
234 | n/a | return word % 10; |
---|
235 | n/a | } |
---|
236 | n/a | |
---|
237 | n/a | /* Coefficient size needed to store 'digits' */ |
---|
238 | n/a | ALWAYS_INLINE mpd_ssize_t |
---|
239 | n/a | mpd_digits_to_size(mpd_ssize_t digits) |
---|
240 | n/a | { |
---|
241 | n/a | mpd_ssize_t q, r; |
---|
242 | n/a | |
---|
243 | n/a | _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS); |
---|
244 | n/a | return (r == 0) ? q : q+1; |
---|
245 | n/a | } |
---|
246 | n/a | |
---|
247 | n/a | /* Number of digits in the exponent. Not defined for MPD_SSIZE_MIN. */ |
---|
248 | n/a | inline int |
---|
249 | n/a | mpd_exp_digits(mpd_ssize_t exp) |
---|
250 | n/a | { |
---|
251 | n/a | exp = (exp < 0) ? -exp : exp; |
---|
252 | n/a | return mpd_word_digits(exp); |
---|
253 | n/a | } |
---|
254 | n/a | |
---|
255 | n/a | /* Canonical */ |
---|
256 | n/a | ALWAYS_INLINE int |
---|
257 | n/a | mpd_iscanonical(const mpd_t *dec UNUSED) |
---|
258 | n/a | { |
---|
259 | n/a | return 1; |
---|
260 | n/a | } |
---|
261 | n/a | |
---|
262 | n/a | /* Finite */ |
---|
263 | n/a | ALWAYS_INLINE int |
---|
264 | n/a | mpd_isfinite(const mpd_t *dec) |
---|
265 | n/a | { |
---|
266 | n/a | return !(dec->flags & MPD_SPECIAL); |
---|
267 | n/a | } |
---|
268 | n/a | |
---|
269 | n/a | /* Infinite */ |
---|
270 | n/a | ALWAYS_INLINE int |
---|
271 | n/a | mpd_isinfinite(const mpd_t *dec) |
---|
272 | n/a | { |
---|
273 | n/a | return dec->flags & MPD_INF; |
---|
274 | n/a | } |
---|
275 | n/a | |
---|
276 | n/a | /* NaN */ |
---|
277 | n/a | ALWAYS_INLINE int |
---|
278 | n/a | mpd_isnan(const mpd_t *dec) |
---|
279 | n/a | { |
---|
280 | n/a | return dec->flags & (MPD_NAN|MPD_SNAN); |
---|
281 | n/a | } |
---|
282 | n/a | |
---|
283 | n/a | /* Negative */ |
---|
284 | n/a | ALWAYS_INLINE int |
---|
285 | n/a | mpd_isnegative(const mpd_t *dec) |
---|
286 | n/a | { |
---|
287 | n/a | return dec->flags & MPD_NEG; |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | /* Positive */ |
---|
291 | n/a | ALWAYS_INLINE int |
---|
292 | n/a | mpd_ispositive(const mpd_t *dec) |
---|
293 | n/a | { |
---|
294 | n/a | return !(dec->flags & MPD_NEG); |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | /* qNaN */ |
---|
298 | n/a | ALWAYS_INLINE int |
---|
299 | n/a | mpd_isqnan(const mpd_t *dec) |
---|
300 | n/a | { |
---|
301 | n/a | return dec->flags & MPD_NAN; |
---|
302 | n/a | } |
---|
303 | n/a | |
---|
304 | n/a | /* Signed */ |
---|
305 | n/a | ALWAYS_INLINE int |
---|
306 | n/a | mpd_issigned(const mpd_t *dec) |
---|
307 | n/a | { |
---|
308 | n/a | return dec->flags & MPD_NEG; |
---|
309 | n/a | } |
---|
310 | n/a | |
---|
311 | n/a | /* sNaN */ |
---|
312 | n/a | ALWAYS_INLINE int |
---|
313 | n/a | mpd_issnan(const mpd_t *dec) |
---|
314 | n/a | { |
---|
315 | n/a | return dec->flags & MPD_SNAN; |
---|
316 | n/a | } |
---|
317 | n/a | |
---|
318 | n/a | /* Special */ |
---|
319 | n/a | ALWAYS_INLINE int |
---|
320 | n/a | mpd_isspecial(const mpd_t *dec) |
---|
321 | n/a | { |
---|
322 | n/a | return dec->flags & MPD_SPECIAL; |
---|
323 | n/a | } |
---|
324 | n/a | |
---|
325 | n/a | /* Zero */ |
---|
326 | n/a | ALWAYS_INLINE int |
---|
327 | n/a | mpd_iszero(const mpd_t *dec) |
---|
328 | n/a | { |
---|
329 | n/a | return !mpd_isspecial(dec) && mpd_msword(dec) == 0; |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | /* Test for zero when specials have been ruled out already */ |
---|
333 | n/a | ALWAYS_INLINE int |
---|
334 | n/a | mpd_iszerocoeff(const mpd_t *dec) |
---|
335 | n/a | { |
---|
336 | n/a | return mpd_msword(dec) == 0; |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | /* Normal */ |
---|
340 | n/a | inline int |
---|
341 | n/a | mpd_isnormal(const mpd_t *dec, const mpd_context_t *ctx) |
---|
342 | n/a | { |
---|
343 | n/a | if (mpd_isspecial(dec)) return 0; |
---|
344 | n/a | if (mpd_iszerocoeff(dec)) return 0; |
---|
345 | n/a | |
---|
346 | n/a | return mpd_adjexp(dec) >= ctx->emin; |
---|
347 | n/a | } |
---|
348 | n/a | |
---|
349 | n/a | /* Subnormal */ |
---|
350 | n/a | inline int |
---|
351 | n/a | mpd_issubnormal(const mpd_t *dec, const mpd_context_t *ctx) |
---|
352 | n/a | { |
---|
353 | n/a | if (mpd_isspecial(dec)) return 0; |
---|
354 | n/a | if (mpd_iszerocoeff(dec)) return 0; |
---|
355 | n/a | |
---|
356 | n/a | return mpd_adjexp(dec) < ctx->emin; |
---|
357 | n/a | } |
---|
358 | n/a | |
---|
359 | n/a | /* Odd word */ |
---|
360 | n/a | ALWAYS_INLINE int |
---|
361 | n/a | mpd_isoddword(mpd_uint_t word) |
---|
362 | n/a | { |
---|
363 | n/a | return word & 1; |
---|
364 | n/a | } |
---|
365 | n/a | |
---|
366 | n/a | /* Odd coefficient */ |
---|
367 | n/a | ALWAYS_INLINE int |
---|
368 | n/a | mpd_isoddcoeff(const mpd_t *dec) |
---|
369 | n/a | { |
---|
370 | n/a | return mpd_isoddword(dec->data[0]); |
---|
371 | n/a | } |
---|
372 | n/a | |
---|
373 | n/a | /* 0 if dec is positive, 1 if dec is negative */ |
---|
374 | n/a | ALWAYS_INLINE uint8_t |
---|
375 | n/a | mpd_sign(const mpd_t *dec) |
---|
376 | n/a | { |
---|
377 | n/a | return dec->flags & MPD_NEG; |
---|
378 | n/a | } |
---|
379 | n/a | |
---|
380 | n/a | /* 1 if dec is positive, -1 if dec is negative */ |
---|
381 | n/a | ALWAYS_INLINE int |
---|
382 | n/a | mpd_arith_sign(const mpd_t *dec) |
---|
383 | n/a | { |
---|
384 | n/a | return 1 - 2 * mpd_isnegative(dec); |
---|
385 | n/a | } |
---|
386 | n/a | |
---|
387 | n/a | /* Radix */ |
---|
388 | n/a | ALWAYS_INLINE long |
---|
389 | n/a | mpd_radix(void) |
---|
390 | n/a | { |
---|
391 | n/a | return 10; |
---|
392 | n/a | } |
---|
393 | n/a | |
---|
394 | n/a | /* Dynamic decimal */ |
---|
395 | n/a | ALWAYS_INLINE int |
---|
396 | n/a | mpd_isdynamic(const mpd_t *dec) |
---|
397 | n/a | { |
---|
398 | n/a | return !(dec->flags & MPD_STATIC); |
---|
399 | n/a | } |
---|
400 | n/a | |
---|
401 | n/a | /* Static decimal */ |
---|
402 | n/a | ALWAYS_INLINE int |
---|
403 | n/a | mpd_isstatic(const mpd_t *dec) |
---|
404 | n/a | { |
---|
405 | n/a | return dec->flags & MPD_STATIC; |
---|
406 | n/a | } |
---|
407 | n/a | |
---|
408 | n/a | /* Data of decimal is dynamic */ |
---|
409 | n/a | ALWAYS_INLINE int |
---|
410 | n/a | mpd_isdynamic_data(const mpd_t *dec) |
---|
411 | n/a | { |
---|
412 | n/a | return !(dec->flags & MPD_DATAFLAGS); |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | /* Data of decimal is static */ |
---|
416 | n/a | ALWAYS_INLINE int |
---|
417 | n/a | mpd_isstatic_data(const mpd_t *dec) |
---|
418 | n/a | { |
---|
419 | n/a | return dec->flags & MPD_STATIC_DATA; |
---|
420 | n/a | } |
---|
421 | n/a | |
---|
422 | n/a | /* Data of decimal is shared */ |
---|
423 | n/a | ALWAYS_INLINE int |
---|
424 | n/a | mpd_isshared_data(const mpd_t *dec) |
---|
425 | n/a | { |
---|
426 | n/a | return dec->flags & MPD_SHARED_DATA; |
---|
427 | n/a | } |
---|
428 | n/a | |
---|
429 | n/a | /* Data of decimal is const */ |
---|
430 | n/a | ALWAYS_INLINE int |
---|
431 | n/a | mpd_isconst_data(const mpd_t *dec) |
---|
432 | n/a | { |
---|
433 | n/a | return dec->flags & MPD_CONST_DATA; |
---|
434 | n/a | } |
---|
435 | n/a | |
---|
436 | n/a | |
---|
437 | n/a | /******************************************************************************/ |
---|
438 | n/a | /* Inline memory handling */ |
---|
439 | n/a | /******************************************************************************/ |
---|
440 | n/a | |
---|
441 | n/a | /* Fill destination with zeros */ |
---|
442 | n/a | ALWAYS_INLINE void |
---|
443 | n/a | mpd_uint_zero(mpd_uint_t *dest, mpd_size_t len) |
---|
444 | n/a | { |
---|
445 | n/a | mpd_size_t i; |
---|
446 | n/a | |
---|
447 | n/a | for (i = 0; i < len; i++) { |
---|
448 | n/a | dest[i] = 0; |
---|
449 | n/a | } |
---|
450 | n/a | } |
---|
451 | n/a | |
---|
452 | n/a | /* Free a decimal */ |
---|
453 | n/a | ALWAYS_INLINE void |
---|
454 | n/a | mpd_del(mpd_t *dec) |
---|
455 | n/a | { |
---|
456 | n/a | if (mpd_isdynamic_data(dec)) { |
---|
457 | n/a | mpd_free(dec->data); |
---|
458 | n/a | } |
---|
459 | n/a | if (mpd_isdynamic(dec)) { |
---|
460 | n/a | mpd_free(dec); |
---|
461 | n/a | } |
---|
462 | n/a | } |
---|
463 | n/a | |
---|
464 | n/a | /* |
---|
465 | n/a | * Resize the coefficient. Existing data up to 'nwords' is left untouched. |
---|
466 | n/a | * Return 1 on success, 0 otherwise. |
---|
467 | n/a | * |
---|
468 | n/a | * Input invariant: MPD_MINALLOC <= result->alloc. |
---|
469 | n/a | * |
---|
470 | n/a | * Case nwords == result->alloc: |
---|
471 | n/a | * 'result' is unchanged. Return 1. |
---|
472 | n/a | * |
---|
473 | n/a | * Case nwords > result->alloc: |
---|
474 | n/a | * Case realloc success: |
---|
475 | n/a | * The value of 'result' does not change. Return 1. |
---|
476 | n/a | * Case realloc failure: |
---|
477 | n/a | * 'result' is NaN, status is updated with MPD_Malloc_error. Return 0. |
---|
478 | n/a | * |
---|
479 | n/a | * Case nwords < result->alloc: |
---|
480 | n/a | * Case is_static_data or realloc failure [1]: |
---|
481 | n/a | * 'result' is unchanged. Return 1. |
---|
482 | n/a | * Case realloc success: |
---|
483 | n/a | * The value of result is undefined (expected). Return 1. |
---|
484 | n/a | * |
---|
485 | n/a | * |
---|
486 | n/a | * [1] In that case the old (now oversized) area is still valid. |
---|
487 | n/a | */ |
---|
488 | n/a | ALWAYS_INLINE int |
---|
489 | n/a | mpd_qresize(mpd_t *result, mpd_ssize_t nwords, uint32_t *status) |
---|
490 | n/a | { |
---|
491 | n/a | assert(!mpd_isconst_data(result)); /* illegal operation for a const */ |
---|
492 | n/a | assert(!mpd_isshared_data(result)); /* illegal operation for a shared */ |
---|
493 | n/a | assert(MPD_MINALLOC <= result->alloc); |
---|
494 | n/a | |
---|
495 | n/a | nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords; |
---|
496 | n/a | if (nwords == result->alloc) { |
---|
497 | n/a | return 1; |
---|
498 | n/a | } |
---|
499 | n/a | if (mpd_isstatic_data(result)) { |
---|
500 | n/a | if (nwords > result->alloc) { |
---|
501 | n/a | return mpd_switch_to_dyn(result, nwords, status); |
---|
502 | n/a | } |
---|
503 | n/a | return 1; |
---|
504 | n/a | } |
---|
505 | n/a | |
---|
506 | n/a | return mpd_realloc_dyn(result, nwords, status); |
---|
507 | n/a | } |
---|
508 | n/a | |
---|
509 | n/a | /* Same as mpd_qresize, but the complete coefficient (including the old |
---|
510 | n/a | * memory area!) is initialized to zero. */ |
---|
511 | n/a | ALWAYS_INLINE int |
---|
512 | n/a | mpd_qresize_zero(mpd_t *result, mpd_ssize_t nwords, uint32_t *status) |
---|
513 | n/a | { |
---|
514 | n/a | assert(!mpd_isconst_data(result)); /* illegal operation for a const */ |
---|
515 | n/a | assert(!mpd_isshared_data(result)); /* illegal operation for a shared */ |
---|
516 | n/a | assert(MPD_MINALLOC <= result->alloc); |
---|
517 | n/a | |
---|
518 | n/a | nwords = (nwords <= MPD_MINALLOC) ? MPD_MINALLOC : nwords; |
---|
519 | n/a | if (nwords != result->alloc) { |
---|
520 | n/a | if (mpd_isstatic_data(result)) { |
---|
521 | n/a | if (nwords > result->alloc) { |
---|
522 | n/a | return mpd_switch_to_dyn_zero(result, nwords, status); |
---|
523 | n/a | } |
---|
524 | n/a | } |
---|
525 | n/a | else if (!mpd_realloc_dyn(result, nwords, status)) { |
---|
526 | n/a | return 0; |
---|
527 | n/a | } |
---|
528 | n/a | } |
---|
529 | n/a | |
---|
530 | n/a | mpd_uint_zero(result->data, nwords); |
---|
531 | n/a | return 1; |
---|
532 | n/a | } |
---|
533 | n/a | |
---|
534 | n/a | /* |
---|
535 | n/a | * Reduce memory size for the coefficient to MPD_MINALLOC. In theory, |
---|
536 | n/a | * realloc may fail even when reducing the memory size. But in that case |
---|
537 | n/a | * the old memory area is always big enough, so checking for MPD_Malloc_error |
---|
538 | n/a | * is not imperative. |
---|
539 | n/a | */ |
---|
540 | n/a | ALWAYS_INLINE void |
---|
541 | n/a | mpd_minalloc(mpd_t *result) |
---|
542 | n/a | { |
---|
543 | n/a | assert(!mpd_isconst_data(result)); /* illegal operation for a const */ |
---|
544 | n/a | assert(!mpd_isshared_data(result)); /* illegal operation for a shared */ |
---|
545 | n/a | |
---|
546 | n/a | if (!mpd_isstatic_data(result) && result->alloc > MPD_MINALLOC) { |
---|
547 | n/a | uint8_t err = 0; |
---|
548 | n/a | result->data = mpd_realloc(result->data, MPD_MINALLOC, |
---|
549 | n/a | sizeof *result->data, &err); |
---|
550 | n/a | if (!err) { |
---|
551 | n/a | result->alloc = MPD_MINALLOC; |
---|
552 | n/a | } |
---|
553 | n/a | } |
---|
554 | n/a | } |
---|
555 | n/a | |
---|
556 | n/a | int |
---|
557 | n/a | mpd_resize(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx) |
---|
558 | n/a | { |
---|
559 | n/a | uint32_t status = 0; |
---|
560 | n/a | if (!mpd_qresize(result, nwords, &status)) { |
---|
561 | n/a | mpd_addstatus_raise(ctx, status); |
---|
562 | n/a | return 0; |
---|
563 | n/a | } |
---|
564 | n/a | return 1; |
---|
565 | n/a | } |
---|
566 | n/a | |
---|
567 | n/a | int |
---|
568 | n/a | mpd_resize_zero(mpd_t *result, mpd_ssize_t nwords, mpd_context_t *ctx) |
---|
569 | n/a | { |
---|
570 | n/a | uint32_t status = 0; |
---|
571 | n/a | if (!mpd_qresize_zero(result, nwords, &status)) { |
---|
572 | n/a | mpd_addstatus_raise(ctx, status); |
---|
573 | n/a | return 0; |
---|
574 | n/a | } |
---|
575 | n/a | return 1; |
---|
576 | n/a | } |
---|
577 | n/a | |
---|
578 | n/a | |
---|
579 | n/a | /******************************************************************************/ |
---|
580 | n/a | /* Set attributes of a decimal */ |
---|
581 | n/a | /******************************************************************************/ |
---|
582 | n/a | |
---|
583 | n/a | /* Set digits. Assumption: result->len is initialized and > 0. */ |
---|
584 | n/a | inline void |
---|
585 | n/a | mpd_setdigits(mpd_t *result) |
---|
586 | n/a | { |
---|
587 | n/a | mpd_ssize_t wdigits = mpd_word_digits(mpd_msword(result)); |
---|
588 | n/a | result->digits = wdigits + (result->len-1) * MPD_RDIGITS; |
---|
589 | n/a | } |
---|
590 | n/a | |
---|
591 | n/a | /* Set sign */ |
---|
592 | n/a | ALWAYS_INLINE void |
---|
593 | n/a | mpd_set_sign(mpd_t *result, uint8_t sign) |
---|
594 | n/a | { |
---|
595 | n/a | result->flags &= ~MPD_NEG; |
---|
596 | n/a | result->flags |= sign; |
---|
597 | n/a | } |
---|
598 | n/a | |
---|
599 | n/a | /* Copy sign from another decimal */ |
---|
600 | n/a | ALWAYS_INLINE void |
---|
601 | n/a | mpd_signcpy(mpd_t *result, const mpd_t *a) |
---|
602 | n/a | { |
---|
603 | n/a | uint8_t sign = a->flags&MPD_NEG; |
---|
604 | n/a | |
---|
605 | n/a | result->flags &= ~MPD_NEG; |
---|
606 | n/a | result->flags |= sign; |
---|
607 | n/a | } |
---|
608 | n/a | |
---|
609 | n/a | /* Set infinity */ |
---|
610 | n/a | ALWAYS_INLINE void |
---|
611 | n/a | mpd_set_infinity(mpd_t *result) |
---|
612 | n/a | { |
---|
613 | n/a | result->flags &= ~MPD_SPECIAL; |
---|
614 | n/a | result->flags |= MPD_INF; |
---|
615 | n/a | } |
---|
616 | n/a | |
---|
617 | n/a | /* Set qNaN */ |
---|
618 | n/a | ALWAYS_INLINE void |
---|
619 | n/a | mpd_set_qnan(mpd_t *result) |
---|
620 | n/a | { |
---|
621 | n/a | result->flags &= ~MPD_SPECIAL; |
---|
622 | n/a | result->flags |= MPD_NAN; |
---|
623 | n/a | } |
---|
624 | n/a | |
---|
625 | n/a | /* Set sNaN */ |
---|
626 | n/a | ALWAYS_INLINE void |
---|
627 | n/a | mpd_set_snan(mpd_t *result) |
---|
628 | n/a | { |
---|
629 | n/a | result->flags &= ~MPD_SPECIAL; |
---|
630 | n/a | result->flags |= MPD_SNAN; |
---|
631 | n/a | } |
---|
632 | n/a | |
---|
633 | n/a | /* Set to negative */ |
---|
634 | n/a | ALWAYS_INLINE void |
---|
635 | n/a | mpd_set_negative(mpd_t *result) |
---|
636 | n/a | { |
---|
637 | n/a | result->flags |= MPD_NEG; |
---|
638 | n/a | } |
---|
639 | n/a | |
---|
640 | n/a | /* Set to positive */ |
---|
641 | n/a | ALWAYS_INLINE void |
---|
642 | n/a | mpd_set_positive(mpd_t *result) |
---|
643 | n/a | { |
---|
644 | n/a | result->flags &= ~MPD_NEG; |
---|
645 | n/a | } |
---|
646 | n/a | |
---|
647 | n/a | /* Set to dynamic */ |
---|
648 | n/a | ALWAYS_INLINE void |
---|
649 | n/a | mpd_set_dynamic(mpd_t *result) |
---|
650 | n/a | { |
---|
651 | n/a | result->flags &= ~MPD_STATIC; |
---|
652 | n/a | } |
---|
653 | n/a | |
---|
654 | n/a | /* Set to static */ |
---|
655 | n/a | ALWAYS_INLINE void |
---|
656 | n/a | mpd_set_static(mpd_t *result) |
---|
657 | n/a | { |
---|
658 | n/a | result->flags |= MPD_STATIC; |
---|
659 | n/a | } |
---|
660 | n/a | |
---|
661 | n/a | /* Set data to dynamic */ |
---|
662 | n/a | ALWAYS_INLINE void |
---|
663 | n/a | mpd_set_dynamic_data(mpd_t *result) |
---|
664 | n/a | { |
---|
665 | n/a | result->flags &= ~MPD_DATAFLAGS; |
---|
666 | n/a | } |
---|
667 | n/a | |
---|
668 | n/a | /* Set data to static */ |
---|
669 | n/a | ALWAYS_INLINE void |
---|
670 | n/a | mpd_set_static_data(mpd_t *result) |
---|
671 | n/a | { |
---|
672 | n/a | result->flags &= ~MPD_DATAFLAGS; |
---|
673 | n/a | result->flags |= MPD_STATIC_DATA; |
---|
674 | n/a | } |
---|
675 | n/a | |
---|
676 | n/a | /* Set data to shared */ |
---|
677 | n/a | ALWAYS_INLINE void |
---|
678 | n/a | mpd_set_shared_data(mpd_t *result) |
---|
679 | n/a | { |
---|
680 | n/a | result->flags &= ~MPD_DATAFLAGS; |
---|
681 | n/a | result->flags |= MPD_SHARED_DATA; |
---|
682 | n/a | } |
---|
683 | n/a | |
---|
684 | n/a | /* Set data to const */ |
---|
685 | n/a | ALWAYS_INLINE void |
---|
686 | n/a | mpd_set_const_data(mpd_t *result) |
---|
687 | n/a | { |
---|
688 | n/a | result->flags &= ~MPD_DATAFLAGS; |
---|
689 | n/a | result->flags |= MPD_CONST_DATA; |
---|
690 | n/a | } |
---|
691 | n/a | |
---|
692 | n/a | /* Clear flags, preserving memory attributes. */ |
---|
693 | n/a | ALWAYS_INLINE void |
---|
694 | n/a | mpd_clear_flags(mpd_t *result) |
---|
695 | n/a | { |
---|
696 | n/a | result->flags &= (MPD_STATIC|MPD_DATAFLAGS); |
---|
697 | n/a | } |
---|
698 | n/a | |
---|
699 | n/a | /* Set flags, preserving memory attributes. */ |
---|
700 | n/a | ALWAYS_INLINE void |
---|
701 | n/a | mpd_set_flags(mpd_t *result, uint8_t flags) |
---|
702 | n/a | { |
---|
703 | n/a | result->flags &= (MPD_STATIC|MPD_DATAFLAGS); |
---|
704 | n/a | result->flags |= flags; |
---|
705 | n/a | } |
---|
706 | n/a | |
---|
707 | n/a | /* Copy flags, preserving memory attributes of result. */ |
---|
708 | n/a | ALWAYS_INLINE void |
---|
709 | n/a | mpd_copy_flags(mpd_t *result, const mpd_t *a) |
---|
710 | n/a | { |
---|
711 | n/a | uint8_t aflags = a->flags; |
---|
712 | n/a | result->flags &= (MPD_STATIC|MPD_DATAFLAGS); |
---|
713 | n/a | result->flags |= (aflags & ~(MPD_STATIC|MPD_DATAFLAGS)); |
---|
714 | n/a | } |
---|
715 | n/a | |
---|
716 | n/a | /* Initialize a workcontext from ctx. Set traps, flags and newtrap to 0. */ |
---|
717 | n/a | static inline void |
---|
718 | n/a | mpd_workcontext(mpd_context_t *workctx, const mpd_context_t *ctx) |
---|
719 | n/a | { |
---|
720 | n/a | workctx->prec = ctx->prec; |
---|
721 | n/a | workctx->emax = ctx->emax; |
---|
722 | n/a | workctx->emin = ctx->emin; |
---|
723 | n/a | workctx->round = ctx->round; |
---|
724 | n/a | workctx->traps = 0; |
---|
725 | n/a | workctx->status = 0; |
---|
726 | n/a | workctx->newtrap = 0; |
---|
727 | n/a | workctx->clamp = ctx->clamp; |
---|
728 | n/a | workctx->allcr = ctx->allcr; |
---|
729 | n/a | } |
---|
730 | n/a | |
---|
731 | n/a | |
---|
732 | n/a | /******************************************************************************/ |
---|
733 | n/a | /* Getting and setting parts of decimals */ |
---|
734 | n/a | /******************************************************************************/ |
---|
735 | n/a | |
---|
736 | n/a | /* Flip the sign of a decimal */ |
---|
737 | n/a | static inline void |
---|
738 | n/a | _mpd_negate(mpd_t *dec) |
---|
739 | n/a | { |
---|
740 | n/a | dec->flags ^= MPD_NEG; |
---|
741 | n/a | } |
---|
742 | n/a | |
---|
743 | n/a | /* Set coefficient to zero */ |
---|
744 | n/a | void |
---|
745 | n/a | mpd_zerocoeff(mpd_t *result) |
---|
746 | n/a | { |
---|
747 | n/a | mpd_minalloc(result); |
---|
748 | n/a | result->digits = 1; |
---|
749 | n/a | result->len = 1; |
---|
750 | n/a | result->data[0] = 0; |
---|
751 | n/a | } |
---|
752 | n/a | |
---|
753 | n/a | /* Set the coefficient to all nines. */ |
---|
754 | n/a | void |
---|
755 | n/a | mpd_qmaxcoeff(mpd_t *result, const mpd_context_t *ctx, uint32_t *status) |
---|
756 | n/a | { |
---|
757 | n/a | mpd_ssize_t len, r; |
---|
758 | n/a | |
---|
759 | n/a | _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS); |
---|
760 | n/a | len = (r == 0) ? len : len+1; |
---|
761 | n/a | |
---|
762 | n/a | if (!mpd_qresize(result, len, status)) { |
---|
763 | n/a | return; |
---|
764 | n/a | } |
---|
765 | n/a | |
---|
766 | n/a | result->len = len; |
---|
767 | n/a | result->digits = ctx->prec; |
---|
768 | n/a | |
---|
769 | n/a | --len; |
---|
770 | n/a | if (r > 0) { |
---|
771 | n/a | result->data[len--] = mpd_pow10[r]-1; |
---|
772 | n/a | } |
---|
773 | n/a | for (; len >= 0; --len) { |
---|
774 | n/a | result->data[len] = MPD_RADIX-1; |
---|
775 | n/a | } |
---|
776 | n/a | } |
---|
777 | n/a | |
---|
778 | n/a | /* |
---|
779 | n/a | * Cut off the most significant digits so that the rest fits in ctx->prec. |
---|
780 | n/a | * Cannot fail. |
---|
781 | n/a | */ |
---|
782 | n/a | static void |
---|
783 | n/a | _mpd_cap(mpd_t *result, const mpd_context_t *ctx) |
---|
784 | n/a | { |
---|
785 | n/a | uint32_t dummy; |
---|
786 | n/a | mpd_ssize_t len, r; |
---|
787 | n/a | |
---|
788 | n/a | if (result->len > 0 && result->digits > ctx->prec) { |
---|
789 | n/a | _mpd_idiv_word(&len, &r, ctx->prec, MPD_RDIGITS); |
---|
790 | n/a | len = (r == 0) ? len : len+1; |
---|
791 | n/a | |
---|
792 | n/a | if (r != 0) { |
---|
793 | n/a | result->data[len-1] %= mpd_pow10[r]; |
---|
794 | n/a | } |
---|
795 | n/a | |
---|
796 | n/a | len = _mpd_real_size(result->data, len); |
---|
797 | n/a | /* resize to fewer words cannot fail */ |
---|
798 | n/a | mpd_qresize(result, len, &dummy); |
---|
799 | n/a | result->len = len; |
---|
800 | n/a | mpd_setdigits(result); |
---|
801 | n/a | } |
---|
802 | n/a | if (mpd_iszero(result)) { |
---|
803 | n/a | _settriple(result, mpd_sign(result), 0, result->exp); |
---|
804 | n/a | } |
---|
805 | n/a | } |
---|
806 | n/a | |
---|
807 | n/a | /* |
---|
808 | n/a | * Cut off the most significant digits of a NaN payload so that the rest |
---|
809 | n/a | * fits in ctx->prec - ctx->clamp. Cannot fail. |
---|
810 | n/a | */ |
---|
811 | n/a | static void |
---|
812 | n/a | _mpd_fix_nan(mpd_t *result, const mpd_context_t *ctx) |
---|
813 | n/a | { |
---|
814 | n/a | uint32_t dummy; |
---|
815 | n/a | mpd_ssize_t prec; |
---|
816 | n/a | mpd_ssize_t len, r; |
---|
817 | n/a | |
---|
818 | n/a | prec = ctx->prec - ctx->clamp; |
---|
819 | n/a | if (result->len > 0 && result->digits > prec) { |
---|
820 | n/a | if (prec == 0) { |
---|
821 | n/a | mpd_minalloc(result); |
---|
822 | n/a | result->len = result->digits = 0; |
---|
823 | n/a | } |
---|
824 | n/a | else { |
---|
825 | n/a | _mpd_idiv_word(&len, &r, prec, MPD_RDIGITS); |
---|
826 | n/a | len = (r == 0) ? len : len+1; |
---|
827 | n/a | |
---|
828 | n/a | if (r != 0) { |
---|
829 | n/a | result->data[len-1] %= mpd_pow10[r]; |
---|
830 | n/a | } |
---|
831 | n/a | |
---|
832 | n/a | len = _mpd_real_size(result->data, len); |
---|
833 | n/a | /* resize to fewer words cannot fail */ |
---|
834 | n/a | mpd_qresize(result, len, &dummy); |
---|
835 | n/a | result->len = len; |
---|
836 | n/a | mpd_setdigits(result); |
---|
837 | n/a | if (mpd_iszerocoeff(result)) { |
---|
838 | n/a | /* NaN0 is not a valid representation */ |
---|
839 | n/a | result->len = result->digits = 0; |
---|
840 | n/a | } |
---|
841 | n/a | } |
---|
842 | n/a | } |
---|
843 | n/a | } |
---|
844 | n/a | |
---|
845 | n/a | /* |
---|
846 | n/a | * Get n most significant digits from a decimal, where 0 < n <= MPD_UINT_DIGITS. |
---|
847 | n/a | * Assumes MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for 32 and 64 bit |
---|
848 | n/a | * machines. |
---|
849 | n/a | * |
---|
850 | n/a | * The result of the operation will be in lo. If the operation is impossible, |
---|
851 | n/a | * hi will be nonzero. This is used to indicate an error. |
---|
852 | n/a | */ |
---|
853 | n/a | static inline void |
---|
854 | n/a | _mpd_get_msdigits(mpd_uint_t *hi, mpd_uint_t *lo, const mpd_t *dec, |
---|
855 | n/a | unsigned int n) |
---|
856 | n/a | { |
---|
857 | n/a | mpd_uint_t r, tmp; |
---|
858 | n/a | |
---|
859 | n/a | assert(0 < n && n <= MPD_RDIGITS+1); |
---|
860 | n/a | |
---|
861 | n/a | _mpd_div_word(&tmp, &r, dec->digits, MPD_RDIGITS); |
---|
862 | n/a | r = (r == 0) ? MPD_RDIGITS : r; /* digits in the most significant word */ |
---|
863 | n/a | |
---|
864 | n/a | *hi = 0; |
---|
865 | n/a | *lo = dec->data[dec->len-1]; |
---|
866 | n/a | if (n <= r) { |
---|
867 | n/a | *lo /= mpd_pow10[r-n]; |
---|
868 | n/a | } |
---|
869 | n/a | else if (dec->len > 1) { |
---|
870 | n/a | /* at this point 1 <= r < n <= MPD_RDIGITS+1 */ |
---|
871 | n/a | _mpd_mul_words(hi, lo, *lo, mpd_pow10[n-r]); |
---|
872 | n/a | tmp = dec->data[dec->len-2] / mpd_pow10[MPD_RDIGITS-(n-r)]; |
---|
873 | n/a | *lo = *lo + tmp; |
---|
874 | n/a | if (*lo < tmp) (*hi)++; |
---|
875 | n/a | } |
---|
876 | n/a | } |
---|
877 | n/a | |
---|
878 | n/a | |
---|
879 | n/a | /******************************************************************************/ |
---|
880 | n/a | /* Gathering information about a decimal */ |
---|
881 | n/a | /******************************************************************************/ |
---|
882 | n/a | |
---|
883 | n/a | /* The real size of the coefficient without leading zero words. */ |
---|
884 | n/a | static inline mpd_ssize_t |
---|
885 | n/a | _mpd_real_size(mpd_uint_t *data, mpd_ssize_t size) |
---|
886 | n/a | { |
---|
887 | n/a | while (size > 1 && data[size-1] == 0) { |
---|
888 | n/a | size--; |
---|
889 | n/a | } |
---|
890 | n/a | |
---|
891 | n/a | return size; |
---|
892 | n/a | } |
---|
893 | n/a | |
---|
894 | n/a | /* Return number of trailing zeros. No errors are possible. */ |
---|
895 | n/a | mpd_ssize_t |
---|
896 | n/a | mpd_trail_zeros(const mpd_t *dec) |
---|
897 | n/a | { |
---|
898 | n/a | mpd_uint_t word; |
---|
899 | n/a | mpd_ssize_t i, tz = 0; |
---|
900 | n/a | |
---|
901 | n/a | for (i=0; i < dec->len; ++i) { |
---|
902 | n/a | if (dec->data[i] != 0) { |
---|
903 | n/a | word = dec->data[i]; |
---|
904 | n/a | tz = i * MPD_RDIGITS; |
---|
905 | n/a | while (word % 10 == 0) { |
---|
906 | n/a | word /= 10; |
---|
907 | n/a | tz++; |
---|
908 | n/a | } |
---|
909 | n/a | break; |
---|
910 | n/a | } |
---|
911 | n/a | } |
---|
912 | n/a | |
---|
913 | n/a | return tz; |
---|
914 | n/a | } |
---|
915 | n/a | |
---|
916 | n/a | /* Integer: Undefined for specials */ |
---|
917 | n/a | static int |
---|
918 | n/a | _mpd_isint(const mpd_t *dec) |
---|
919 | n/a | { |
---|
920 | n/a | mpd_ssize_t tz; |
---|
921 | n/a | |
---|
922 | n/a | if (mpd_iszerocoeff(dec)) { |
---|
923 | n/a | return 1; |
---|
924 | n/a | } |
---|
925 | n/a | |
---|
926 | n/a | tz = mpd_trail_zeros(dec); |
---|
927 | n/a | return (dec->exp + tz >= 0); |
---|
928 | n/a | } |
---|
929 | n/a | |
---|
930 | n/a | /* Integer */ |
---|
931 | n/a | int |
---|
932 | n/a | mpd_isinteger(const mpd_t *dec) |
---|
933 | n/a | { |
---|
934 | n/a | if (mpd_isspecial(dec)) { |
---|
935 | n/a | return 0; |
---|
936 | n/a | } |
---|
937 | n/a | return _mpd_isint(dec); |
---|
938 | n/a | } |
---|
939 | n/a | |
---|
940 | n/a | /* Word is a power of 10 */ |
---|
941 | n/a | static int |
---|
942 | n/a | mpd_word_ispow10(mpd_uint_t word) |
---|
943 | n/a | { |
---|
944 | n/a | int n; |
---|
945 | n/a | |
---|
946 | n/a | n = mpd_word_digits(word); |
---|
947 | n/a | if (word == mpd_pow10[n-1]) { |
---|
948 | n/a | return 1; |
---|
949 | n/a | } |
---|
950 | n/a | |
---|
951 | n/a | return 0; |
---|
952 | n/a | } |
---|
953 | n/a | |
---|
954 | n/a | /* Coefficient is a power of 10 */ |
---|
955 | n/a | static int |
---|
956 | n/a | mpd_coeff_ispow10(const mpd_t *dec) |
---|
957 | n/a | { |
---|
958 | n/a | if (mpd_word_ispow10(mpd_msword(dec))) { |
---|
959 | n/a | if (_mpd_isallzero(dec->data, dec->len-1)) { |
---|
960 | n/a | return 1; |
---|
961 | n/a | } |
---|
962 | n/a | } |
---|
963 | n/a | |
---|
964 | n/a | return 0; |
---|
965 | n/a | } |
---|
966 | n/a | |
---|
967 | n/a | /* All digits of a word are nines */ |
---|
968 | n/a | static int |
---|
969 | n/a | mpd_word_isallnine(mpd_uint_t word) |
---|
970 | n/a | { |
---|
971 | n/a | int n; |
---|
972 | n/a | |
---|
973 | n/a | n = mpd_word_digits(word); |
---|
974 | n/a | if (word == mpd_pow10[n]-1) { |
---|
975 | n/a | return 1; |
---|
976 | n/a | } |
---|
977 | n/a | |
---|
978 | n/a | return 0; |
---|
979 | n/a | } |
---|
980 | n/a | |
---|
981 | n/a | /* All digits of the coefficient are nines */ |
---|
982 | n/a | static int |
---|
983 | n/a | mpd_coeff_isallnine(const mpd_t *dec) |
---|
984 | n/a | { |
---|
985 | n/a | if (mpd_word_isallnine(mpd_msword(dec))) { |
---|
986 | n/a | if (_mpd_isallnine(dec->data, dec->len-1)) { |
---|
987 | n/a | return 1; |
---|
988 | n/a | } |
---|
989 | n/a | } |
---|
990 | n/a | |
---|
991 | n/a | return 0; |
---|
992 | n/a | } |
---|
993 | n/a | |
---|
994 | n/a | /* Odd decimal: Undefined for non-integers! */ |
---|
995 | n/a | int |
---|
996 | n/a | mpd_isodd(const mpd_t *dec) |
---|
997 | n/a | { |
---|
998 | n/a | mpd_uint_t q, r; |
---|
999 | n/a | assert(mpd_isinteger(dec)); |
---|
1000 | n/a | if (mpd_iszerocoeff(dec)) return 0; |
---|
1001 | n/a | if (dec->exp < 0) { |
---|
1002 | n/a | _mpd_div_word(&q, &r, -dec->exp, MPD_RDIGITS); |
---|
1003 | n/a | q = dec->data[q] / mpd_pow10[r]; |
---|
1004 | n/a | return mpd_isoddword(q); |
---|
1005 | n/a | } |
---|
1006 | n/a | return dec->exp == 0 && mpd_isoddword(dec->data[0]); |
---|
1007 | n/a | } |
---|
1008 | n/a | |
---|
1009 | n/a | /* Even: Undefined for non-integers! */ |
---|
1010 | n/a | int |
---|
1011 | n/a | mpd_iseven(const mpd_t *dec) |
---|
1012 | n/a | { |
---|
1013 | n/a | return !mpd_isodd(dec); |
---|
1014 | n/a | } |
---|
1015 | n/a | |
---|
1016 | n/a | /******************************************************************************/ |
---|
1017 | n/a | /* Getting and setting decimals */ |
---|
1018 | n/a | /******************************************************************************/ |
---|
1019 | n/a | |
---|
1020 | n/a | /* Internal function: Set a static decimal from a triple, no error checking. */ |
---|
1021 | n/a | static void |
---|
1022 | n/a | _ssettriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp) |
---|
1023 | n/a | { |
---|
1024 | n/a | mpd_set_flags(result, sign); |
---|
1025 | n/a | result->exp = exp; |
---|
1026 | n/a | _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX); |
---|
1027 | n/a | result->len = (result->data[1] == 0) ? 1 : 2; |
---|
1028 | n/a | mpd_setdigits(result); |
---|
1029 | n/a | } |
---|
1030 | n/a | |
---|
1031 | n/a | /* Internal function: Set a decimal from a triple, no error checking. */ |
---|
1032 | n/a | static void |
---|
1033 | n/a | _settriple(mpd_t *result, uint8_t sign, mpd_uint_t a, mpd_ssize_t exp) |
---|
1034 | n/a | { |
---|
1035 | n/a | mpd_minalloc(result); |
---|
1036 | n/a | mpd_set_flags(result, sign); |
---|
1037 | n/a | result->exp = exp; |
---|
1038 | n/a | _mpd_div_word(&result->data[1], &result->data[0], a, MPD_RADIX); |
---|
1039 | n/a | result->len = (result->data[1] == 0) ? 1 : 2; |
---|
1040 | n/a | mpd_setdigits(result); |
---|
1041 | n/a | } |
---|
1042 | n/a | |
---|
1043 | n/a | /* Set a special number from a triple */ |
---|
1044 | n/a | void |
---|
1045 | n/a | mpd_setspecial(mpd_t *result, uint8_t sign, uint8_t type) |
---|
1046 | n/a | { |
---|
1047 | n/a | mpd_minalloc(result); |
---|
1048 | n/a | result->flags &= ~(MPD_NEG|MPD_SPECIAL); |
---|
1049 | n/a | result->flags |= (sign|type); |
---|
1050 | n/a | result->exp = result->digits = result->len = 0; |
---|
1051 | n/a | } |
---|
1052 | n/a | |
---|
1053 | n/a | /* Set result of NaN with an error status */ |
---|
1054 | n/a | void |
---|
1055 | n/a | mpd_seterror(mpd_t *result, uint32_t flags, uint32_t *status) |
---|
1056 | n/a | { |
---|
1057 | n/a | mpd_minalloc(result); |
---|
1058 | n/a | mpd_set_qnan(result); |
---|
1059 | n/a | mpd_set_positive(result); |
---|
1060 | n/a | result->exp = result->digits = result->len = 0; |
---|
1061 | n/a | *status |= flags; |
---|
1062 | n/a | } |
---|
1063 | n/a | |
---|
1064 | n/a | /* quietly set a static decimal from an mpd_ssize_t */ |
---|
1065 | n/a | void |
---|
1066 | n/a | mpd_qsset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, |
---|
1067 | n/a | uint32_t *status) |
---|
1068 | n/a | { |
---|
1069 | n/a | mpd_uint_t u; |
---|
1070 | n/a | uint8_t sign = MPD_POS; |
---|
1071 | n/a | |
---|
1072 | n/a | if (a < 0) { |
---|
1073 | n/a | if (a == MPD_SSIZE_MIN) { |
---|
1074 | n/a | u = (mpd_uint_t)MPD_SSIZE_MAX + |
---|
1075 | n/a | (-(MPD_SSIZE_MIN+MPD_SSIZE_MAX)); |
---|
1076 | n/a | } |
---|
1077 | n/a | else { |
---|
1078 | n/a | u = -a; |
---|
1079 | n/a | } |
---|
1080 | n/a | sign = MPD_NEG; |
---|
1081 | n/a | } |
---|
1082 | n/a | else { |
---|
1083 | n/a | u = a; |
---|
1084 | n/a | } |
---|
1085 | n/a | _ssettriple(result, sign, u, 0); |
---|
1086 | n/a | mpd_qfinalize(result, ctx, status); |
---|
1087 | n/a | } |
---|
1088 | n/a | |
---|
1089 | n/a | /* quietly set a static decimal from an mpd_uint_t */ |
---|
1090 | n/a | void |
---|
1091 | n/a | mpd_qsset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, |
---|
1092 | n/a | uint32_t *status) |
---|
1093 | n/a | { |
---|
1094 | n/a | _ssettriple(result, MPD_POS, a, 0); |
---|
1095 | n/a | mpd_qfinalize(result, ctx, status); |
---|
1096 | n/a | } |
---|
1097 | n/a | |
---|
1098 | n/a | /* quietly set a static decimal from an int32_t */ |
---|
1099 | n/a | void |
---|
1100 | n/a | mpd_qsset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, |
---|
1101 | n/a | uint32_t *status) |
---|
1102 | n/a | { |
---|
1103 | n/a | mpd_qsset_ssize(result, a, ctx, status); |
---|
1104 | n/a | } |
---|
1105 | n/a | |
---|
1106 | n/a | /* quietly set a static decimal from a uint32_t */ |
---|
1107 | n/a | void |
---|
1108 | n/a | mpd_qsset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, |
---|
1109 | n/a | uint32_t *status) |
---|
1110 | n/a | { |
---|
1111 | n/a | mpd_qsset_uint(result, a, ctx, status); |
---|
1112 | n/a | } |
---|
1113 | n/a | |
---|
1114 | n/a | #ifdef CONFIG_64 |
---|
1115 | n/a | /* quietly set a static decimal from an int64_t */ |
---|
1116 | n/a | void |
---|
1117 | n/a | mpd_qsset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, |
---|
1118 | n/a | uint32_t *status) |
---|
1119 | n/a | { |
---|
1120 | n/a | mpd_qsset_ssize(result, a, ctx, status); |
---|
1121 | n/a | } |
---|
1122 | n/a | |
---|
1123 | n/a | /* quietly set a static decimal from a uint64_t */ |
---|
1124 | n/a | void |
---|
1125 | n/a | mpd_qsset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, |
---|
1126 | n/a | uint32_t *status) |
---|
1127 | n/a | { |
---|
1128 | n/a | mpd_qsset_uint(result, a, ctx, status); |
---|
1129 | n/a | } |
---|
1130 | n/a | #endif |
---|
1131 | n/a | |
---|
1132 | n/a | /* quietly set a decimal from an mpd_ssize_t */ |
---|
1133 | n/a | void |
---|
1134 | n/a | mpd_qset_ssize(mpd_t *result, mpd_ssize_t a, const mpd_context_t *ctx, |
---|
1135 | n/a | uint32_t *status) |
---|
1136 | n/a | { |
---|
1137 | n/a | mpd_minalloc(result); |
---|
1138 | n/a | mpd_qsset_ssize(result, a, ctx, status); |
---|
1139 | n/a | } |
---|
1140 | n/a | |
---|
1141 | n/a | /* quietly set a decimal from an mpd_uint_t */ |
---|
1142 | n/a | void |
---|
1143 | n/a | mpd_qset_uint(mpd_t *result, mpd_uint_t a, const mpd_context_t *ctx, |
---|
1144 | n/a | uint32_t *status) |
---|
1145 | n/a | { |
---|
1146 | n/a | _settriple(result, MPD_POS, a, 0); |
---|
1147 | n/a | mpd_qfinalize(result, ctx, status); |
---|
1148 | n/a | } |
---|
1149 | n/a | |
---|
1150 | n/a | /* quietly set a decimal from an int32_t */ |
---|
1151 | n/a | void |
---|
1152 | n/a | mpd_qset_i32(mpd_t *result, int32_t a, const mpd_context_t *ctx, |
---|
1153 | n/a | uint32_t *status) |
---|
1154 | n/a | { |
---|
1155 | n/a | mpd_qset_ssize(result, a, ctx, status); |
---|
1156 | n/a | } |
---|
1157 | n/a | |
---|
1158 | n/a | /* quietly set a decimal from a uint32_t */ |
---|
1159 | n/a | void |
---|
1160 | n/a | mpd_qset_u32(mpd_t *result, uint32_t a, const mpd_context_t *ctx, |
---|
1161 | n/a | uint32_t *status) |
---|
1162 | n/a | { |
---|
1163 | n/a | mpd_qset_uint(result, a, ctx, status); |
---|
1164 | n/a | } |
---|
1165 | n/a | |
---|
1166 | n/a | #if defined(CONFIG_32) && !defined(LEGACY_COMPILER) |
---|
1167 | n/a | /* set a decimal from a uint64_t */ |
---|
1168 | n/a | static void |
---|
1169 | n/a | _c32setu64(mpd_t *result, uint64_t u, uint8_t sign, uint32_t *status) |
---|
1170 | n/a | { |
---|
1171 | n/a | mpd_uint_t w[3]; |
---|
1172 | n/a | uint64_t q; |
---|
1173 | n/a | int i, len; |
---|
1174 | n/a | |
---|
1175 | n/a | len = 0; |
---|
1176 | n/a | do { |
---|
1177 | n/a | q = u / MPD_RADIX; |
---|
1178 | n/a | w[len] = (mpd_uint_t)(u - q * MPD_RADIX); |
---|
1179 | n/a | u = q; len++; |
---|
1180 | n/a | } while (u != 0); |
---|
1181 | n/a | |
---|
1182 | n/a | if (!mpd_qresize(result, len, status)) { |
---|
1183 | n/a | return; |
---|
1184 | n/a | } |
---|
1185 | n/a | for (i = 0; i < len; i++) { |
---|
1186 | n/a | result->data[i] = w[i]; |
---|
1187 | n/a | } |
---|
1188 | n/a | |
---|
1189 | n/a | mpd_set_sign(result, sign); |
---|
1190 | n/a | result->exp = 0; |
---|
1191 | n/a | result->len = len; |
---|
1192 | n/a | mpd_setdigits(result); |
---|
1193 | n/a | } |
---|
1194 | n/a | |
---|
1195 | n/a | static void |
---|
1196 | n/a | _c32_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, |
---|
1197 | n/a | uint32_t *status) |
---|
1198 | n/a | { |
---|
1199 | n/a | _c32setu64(result, a, MPD_POS, status); |
---|
1200 | n/a | mpd_qfinalize(result, ctx, status); |
---|
1201 | n/a | } |
---|
1202 | n/a | |
---|
1203 | n/a | /* set a decimal from an int64_t */ |
---|
1204 | n/a | static void |
---|
1205 | n/a | _c32_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, |
---|
1206 | n/a | uint32_t *status) |
---|
1207 | n/a | { |
---|
1208 | n/a | uint64_t u; |
---|
1209 | n/a | uint8_t sign = MPD_POS; |
---|
1210 | n/a | |
---|
1211 | n/a | if (a < 0) { |
---|
1212 | n/a | if (a == INT64_MIN) { |
---|
1213 | n/a | u = (uint64_t)INT64_MAX + (-(INT64_MIN+INT64_MAX)); |
---|
1214 | n/a | } |
---|
1215 | n/a | else { |
---|
1216 | n/a | u = -a; |
---|
1217 | n/a | } |
---|
1218 | n/a | sign = MPD_NEG; |
---|
1219 | n/a | } |
---|
1220 | n/a | else { |
---|
1221 | n/a | u = a; |
---|
1222 | n/a | } |
---|
1223 | n/a | _c32setu64(result, u, sign, status); |
---|
1224 | n/a | mpd_qfinalize(result, ctx, status); |
---|
1225 | n/a | } |
---|
1226 | n/a | #endif /* CONFIG_32 && !LEGACY_COMPILER */ |
---|
1227 | n/a | |
---|
1228 | n/a | #ifndef LEGACY_COMPILER |
---|
1229 | n/a | /* quietly set a decimal from an int64_t */ |
---|
1230 | n/a | void |
---|
1231 | n/a | mpd_qset_i64(mpd_t *result, int64_t a, const mpd_context_t *ctx, |
---|
1232 | n/a | uint32_t *status) |
---|
1233 | n/a | { |
---|
1234 | n/a | #ifdef CONFIG_64 |
---|
1235 | n/a | mpd_qset_ssize(result, a, ctx, status); |
---|
1236 | n/a | #else |
---|
1237 | n/a | _c32_qset_i64(result, a, ctx, status); |
---|
1238 | n/a | #endif |
---|
1239 | n/a | } |
---|
1240 | n/a | |
---|
1241 | n/a | /* quietly set a decimal from a uint64_t */ |
---|
1242 | n/a | void |
---|
1243 | n/a | mpd_qset_u64(mpd_t *result, uint64_t a, const mpd_context_t *ctx, |
---|
1244 | n/a | uint32_t *status) |
---|
1245 | n/a | { |
---|
1246 | n/a | #ifdef CONFIG_64 |
---|
1247 | n/a | mpd_qset_uint(result, a, ctx, status); |
---|
1248 | n/a | #else |
---|
1249 | n/a | _c32_qset_u64(result, a, ctx, status); |
---|
1250 | n/a | #endif |
---|
1251 | n/a | } |
---|
1252 | n/a | #endif /* !LEGACY_COMPILER */ |
---|
1253 | n/a | |
---|
1254 | n/a | |
---|
1255 | n/a | /* |
---|
1256 | n/a | * Quietly get an mpd_uint_t from a decimal. Assumes |
---|
1257 | n/a | * MPD_UINT_DIGITS == MPD_RDIGITS+1, which is true for |
---|
1258 | n/a | * 32 and 64 bit machines. |
---|
1259 | n/a | * |
---|
1260 | n/a | * If the operation is impossible, MPD_Invalid_operation is set. |
---|
1261 | n/a | */ |
---|
1262 | n/a | static mpd_uint_t |
---|
1263 | n/a | _mpd_qget_uint(int use_sign, const mpd_t *a, uint32_t *status) |
---|
1264 | n/a | { |
---|
1265 | n/a | mpd_t tmp; |
---|
1266 | n/a | mpd_uint_t tmp_data[2]; |
---|
1267 | n/a | mpd_uint_t lo, hi; |
---|
1268 | n/a | |
---|
1269 | n/a | if (mpd_isspecial(a)) { |
---|
1270 | n/a | *status |= MPD_Invalid_operation; |
---|
1271 | n/a | return MPD_UINT_MAX; |
---|
1272 | n/a | } |
---|
1273 | n/a | if (mpd_iszero(a)) { |
---|
1274 | n/a | return 0; |
---|
1275 | n/a | } |
---|
1276 | n/a | if (use_sign && mpd_isnegative(a)) { |
---|
1277 | n/a | *status |= MPD_Invalid_operation; |
---|
1278 | n/a | return MPD_UINT_MAX; |
---|
1279 | n/a | } |
---|
1280 | n/a | |
---|
1281 | n/a | if (a->digits+a->exp > MPD_RDIGITS+1) { |
---|
1282 | n/a | *status |= MPD_Invalid_operation; |
---|
1283 | n/a | return MPD_UINT_MAX; |
---|
1284 | n/a | } |
---|
1285 | n/a | |
---|
1286 | n/a | if (a->exp < 0) { |
---|
1287 | n/a | if (!_mpd_isint(a)) { |
---|
1288 | n/a | *status |= MPD_Invalid_operation; |
---|
1289 | n/a | return MPD_UINT_MAX; |
---|
1290 | n/a | } |
---|
1291 | n/a | /* At this point a->digits+a->exp <= MPD_RDIGITS+1, |
---|
1292 | n/a | * so the shift fits. */ |
---|
1293 | n/a | tmp.data = tmp_data; |
---|
1294 | n/a | tmp.flags = MPD_STATIC|MPD_STATIC_DATA; |
---|
1295 | n/a | tmp.alloc = 2; |
---|
1296 | n/a | mpd_qsshiftr(&tmp, a, -a->exp); |
---|
1297 | n/a | tmp.exp = 0; |
---|
1298 | n/a | a = &tmp; |
---|
1299 | n/a | } |
---|
1300 | n/a | |
---|
1301 | n/a | _mpd_get_msdigits(&hi, &lo, a, MPD_RDIGITS+1); |
---|
1302 | n/a | if (hi) { |
---|
1303 | n/a | *status |= MPD_Invalid_operation; |
---|
1304 | n/a | return MPD_UINT_MAX; |
---|
1305 | n/a | } |
---|
1306 | n/a | |
---|
1307 | n/a | if (a->exp > 0) { |
---|
1308 | n/a | _mpd_mul_words(&hi, &lo, lo, mpd_pow10[a->exp]); |
---|
1309 | n/a | if (hi) { |
---|
1310 | n/a | *status |= MPD_Invalid_operation; |
---|
1311 | n/a | return MPD_UINT_MAX; |
---|
1312 | n/a | } |
---|
1313 | n/a | } |
---|
1314 | n/a | |
---|
1315 | n/a | return lo; |
---|
1316 | n/a | } |
---|
1317 | n/a | |
---|
1318 | n/a | /* |
---|
1319 | n/a | * Sets Invalid_operation for: |
---|
1320 | n/a | * - specials |
---|
1321 | n/a | * - negative numbers (except negative zero) |
---|
1322 | n/a | * - non-integers |
---|
1323 | n/a | * - overflow |
---|
1324 | n/a | */ |
---|
1325 | n/a | mpd_uint_t |
---|
1326 | n/a | mpd_qget_uint(const mpd_t *a, uint32_t *status) |
---|
1327 | n/a | { |
---|
1328 | n/a | return _mpd_qget_uint(1, a, status); |
---|
1329 | n/a | } |
---|
1330 | n/a | |
---|
1331 | n/a | /* Same as above, but gets the absolute value, i.e. the sign is ignored. */ |
---|
1332 | n/a | mpd_uint_t |
---|
1333 | n/a | mpd_qabs_uint(const mpd_t *a, uint32_t *status) |
---|
1334 | n/a | { |
---|
1335 | n/a | return _mpd_qget_uint(0, a, status); |
---|
1336 | n/a | } |
---|
1337 | n/a | |
---|
1338 | n/a | /* quietly get an mpd_ssize_t from a decimal */ |
---|
1339 | n/a | mpd_ssize_t |
---|
1340 | n/a | mpd_qget_ssize(const mpd_t *a, uint32_t *status) |
---|
1341 | n/a | { |
---|
1342 | n/a | mpd_uint_t u; |
---|
1343 | n/a | int isneg; |
---|
1344 | n/a | |
---|
1345 | n/a | u = mpd_qabs_uint(a, status); |
---|
1346 | n/a | if (*status&MPD_Invalid_operation) { |
---|
1347 | n/a | return MPD_SSIZE_MAX; |
---|
1348 | n/a | } |
---|
1349 | n/a | |
---|
1350 | n/a | isneg = mpd_isnegative(a); |
---|
1351 | n/a | if (u <= MPD_SSIZE_MAX) { |
---|
1352 | n/a | return isneg ? -((mpd_ssize_t)u) : (mpd_ssize_t)u; |
---|
1353 | n/a | } |
---|
1354 | n/a | else if (isneg && u+(MPD_SSIZE_MIN+MPD_SSIZE_MAX) == MPD_SSIZE_MAX) { |
---|
1355 | n/a | return MPD_SSIZE_MIN; |
---|
1356 | n/a | } |
---|
1357 | n/a | |
---|
1358 | n/a | *status |= MPD_Invalid_operation; |
---|
1359 | n/a | return MPD_SSIZE_MAX; |
---|
1360 | n/a | } |
---|
1361 | n/a | |
---|
1362 | n/a | #if defined(CONFIG_32) && !defined(LEGACY_COMPILER) |
---|
1363 | n/a | /* |
---|
1364 | n/a | * Quietly get a uint64_t from a decimal. If the operation is impossible, |
---|
1365 | n/a | * MPD_Invalid_operation is set. |
---|
1366 | n/a | */ |
---|
1367 | n/a | static uint64_t |
---|
1368 | n/a | _c32_qget_u64(int use_sign, const mpd_t *a, uint32_t *status) |
---|
1369 | n/a | { |
---|
1370 | n/a | MPD_NEW_STATIC(tmp,0,0,20,3); |
---|
1371 | n/a | mpd_context_t maxcontext; |
---|
1372 | n/a | uint64_t ret; |
---|
1373 | n/a | |
---|
1374 | n/a | tmp_data[0] = 709551615; |
---|
1375 | n/a | tmp_data[1] = 446744073; |
---|
1376 | n/a | tmp_data[2] = 18; |
---|
1377 | n/a | |
---|
1378 | n/a | if (mpd_isspecial(a)) { |
---|
1379 | n/a | *status |= MPD_Invalid_operation; |
---|
1380 | n/a | return UINT64_MAX; |
---|
1381 | n/a | } |
---|
1382 | n/a | if (mpd_iszero(a)) { |
---|
1383 | n/a | return 0; |
---|
1384 | n/a | } |
---|
1385 | n/a | if (use_sign && mpd_isnegative(a)) { |
---|
1386 | n/a | *status |= MPD_Invalid_operation; |
---|
1387 | n/a | return UINT64_MAX; |
---|
1388 | n/a | } |
---|
1389 | n/a | if (!_mpd_isint(a)) { |
---|
1390 | n/a | *status |= MPD_Invalid_operation; |
---|
1391 | n/a | return UINT64_MAX; |
---|
1392 | n/a | } |
---|
1393 | n/a | |
---|
1394 | n/a | if (_mpd_cmp_abs(a, &tmp) > 0) { |
---|
1395 | n/a | *status |= MPD_Invalid_operation; |
---|
1396 | n/a | return UINT64_MAX; |
---|
1397 | n/a | } |
---|
1398 | n/a | |
---|
1399 | n/a | mpd_maxcontext(&maxcontext); |
---|
1400 | n/a | mpd_qrescale(&tmp, a, 0, &maxcontext, &maxcontext.status); |
---|
1401 | n/a | maxcontext.status &= ~MPD_Rounded; |
---|
1402 | n/a | if (maxcontext.status != 0) { |
---|
1403 | n/a | *status |= (maxcontext.status|MPD_Invalid_operation); /* GCOV_NOT_REACHED */ |
---|
1404 | n/a | return UINT64_MAX; /* GCOV_NOT_REACHED */ |
---|
1405 | n/a | } |
---|
1406 | n/a | |
---|
1407 | n/a | ret = 0; |
---|
1408 | n/a | switch (tmp.len) { |
---|
1409 | n/a | case 3: |
---|
1410 | n/a | ret += (uint64_t)tmp_data[2] * 1000000000000000000ULL; |
---|
1411 | n/a | case 2: |
---|
1412 | n/a | ret += (uint64_t)tmp_data[1] * 1000000000ULL; |
---|
1413 | n/a | case 1: |
---|
1414 | n/a | ret += tmp_data[0]; |
---|
1415 | n/a | break; |
---|
1416 | n/a | default: |
---|
1417 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
1418 | n/a | } |
---|
1419 | n/a | |
---|
1420 | n/a | return ret; |
---|
1421 | n/a | } |
---|
1422 | n/a | |
---|
1423 | n/a | static int64_t |
---|
1424 | n/a | _c32_qget_i64(const mpd_t *a, uint32_t *status) |
---|
1425 | n/a | { |
---|
1426 | n/a | uint64_t u; |
---|
1427 | n/a | int isneg; |
---|
1428 | n/a | |
---|
1429 | n/a | u = _c32_qget_u64(0, a, status); |
---|
1430 | n/a | if (*status&MPD_Invalid_operation) { |
---|
1431 | n/a | return INT64_MAX; |
---|
1432 | n/a | } |
---|
1433 | n/a | |
---|
1434 | n/a | isneg = mpd_isnegative(a); |
---|
1435 | n/a | if (u <= INT64_MAX) { |
---|
1436 | n/a | return isneg ? -((int64_t)u) : (int64_t)u; |
---|
1437 | n/a | } |
---|
1438 | n/a | else if (isneg && u+(INT64_MIN+INT64_MAX) == INT64_MAX) { |
---|
1439 | n/a | return INT64_MIN; |
---|
1440 | n/a | } |
---|
1441 | n/a | |
---|
1442 | n/a | *status |= MPD_Invalid_operation; |
---|
1443 | n/a | return INT64_MAX; |
---|
1444 | n/a | } |
---|
1445 | n/a | #endif /* CONFIG_32 && !LEGACY_COMPILER */ |
---|
1446 | n/a | |
---|
1447 | n/a | #ifdef CONFIG_64 |
---|
1448 | n/a | /* quietly get a uint64_t from a decimal */ |
---|
1449 | n/a | uint64_t |
---|
1450 | n/a | mpd_qget_u64(const mpd_t *a, uint32_t *status) |
---|
1451 | n/a | { |
---|
1452 | n/a | return mpd_qget_uint(a, status); |
---|
1453 | n/a | } |
---|
1454 | n/a | |
---|
1455 | n/a | /* quietly get an int64_t from a decimal */ |
---|
1456 | n/a | int64_t |
---|
1457 | n/a | mpd_qget_i64(const mpd_t *a, uint32_t *status) |
---|
1458 | n/a | { |
---|
1459 | n/a | return mpd_qget_ssize(a, status); |
---|
1460 | n/a | } |
---|
1461 | n/a | |
---|
1462 | n/a | /* quietly get a uint32_t from a decimal */ |
---|
1463 | n/a | uint32_t |
---|
1464 | n/a | mpd_qget_u32(const mpd_t *a, uint32_t *status) |
---|
1465 | n/a | { |
---|
1466 | n/a | uint64_t x = mpd_qget_uint(a, status); |
---|
1467 | n/a | |
---|
1468 | n/a | if (*status&MPD_Invalid_operation) { |
---|
1469 | n/a | return UINT32_MAX; |
---|
1470 | n/a | } |
---|
1471 | n/a | if (x > UINT32_MAX) { |
---|
1472 | n/a | *status |= MPD_Invalid_operation; |
---|
1473 | n/a | return UINT32_MAX; |
---|
1474 | n/a | } |
---|
1475 | n/a | |
---|
1476 | n/a | return (uint32_t)x; |
---|
1477 | n/a | } |
---|
1478 | n/a | |
---|
1479 | n/a | /* quietly get an int32_t from a decimal */ |
---|
1480 | n/a | int32_t |
---|
1481 | n/a | mpd_qget_i32(const mpd_t *a, uint32_t *status) |
---|
1482 | n/a | { |
---|
1483 | n/a | int64_t x = mpd_qget_ssize(a, status); |
---|
1484 | n/a | |
---|
1485 | n/a | if (*status&MPD_Invalid_operation) { |
---|
1486 | n/a | return INT32_MAX; |
---|
1487 | n/a | } |
---|
1488 | n/a | if (x < INT32_MIN || x > INT32_MAX) { |
---|
1489 | n/a | *status |= MPD_Invalid_operation; |
---|
1490 | n/a | return INT32_MAX; |
---|
1491 | n/a | } |
---|
1492 | n/a | |
---|
1493 | n/a | return (int32_t)x; |
---|
1494 | n/a | } |
---|
1495 | n/a | #else |
---|
1496 | n/a | #ifndef LEGACY_COMPILER |
---|
1497 | n/a | /* quietly get a uint64_t from a decimal */ |
---|
1498 | n/a | uint64_t |
---|
1499 | n/a | mpd_qget_u64(const mpd_t *a, uint32_t *status) |
---|
1500 | n/a | { |
---|
1501 | n/a | return _c32_qget_u64(1, a, status); |
---|
1502 | n/a | } |
---|
1503 | n/a | |
---|
1504 | n/a | /* quietly get an int64_t from a decimal */ |
---|
1505 | n/a | int64_t |
---|
1506 | n/a | mpd_qget_i64(const mpd_t *a, uint32_t *status) |
---|
1507 | n/a | { |
---|
1508 | n/a | return _c32_qget_i64(a, status); |
---|
1509 | n/a | } |
---|
1510 | n/a | #endif |
---|
1511 | n/a | |
---|
1512 | n/a | /* quietly get a uint32_t from a decimal */ |
---|
1513 | n/a | uint32_t |
---|
1514 | n/a | mpd_qget_u32(const mpd_t *a, uint32_t *status) |
---|
1515 | n/a | { |
---|
1516 | n/a | return mpd_qget_uint(a, status); |
---|
1517 | n/a | } |
---|
1518 | n/a | |
---|
1519 | n/a | /* quietly get an int32_t from a decimal */ |
---|
1520 | n/a | int32_t |
---|
1521 | n/a | mpd_qget_i32(const mpd_t *a, uint32_t *status) |
---|
1522 | n/a | { |
---|
1523 | n/a | return mpd_qget_ssize(a, status); |
---|
1524 | n/a | } |
---|
1525 | n/a | #endif |
---|
1526 | n/a | |
---|
1527 | n/a | |
---|
1528 | n/a | /******************************************************************************/ |
---|
1529 | n/a | /* Filtering input of functions, finalizing output of functions */ |
---|
1530 | n/a | /******************************************************************************/ |
---|
1531 | n/a | |
---|
1532 | n/a | /* |
---|
1533 | n/a | * Check if the operand is NaN, copy to result and return 1 if this is |
---|
1534 | n/a | * the case. Copying can fail since NaNs are allowed to have a payload that |
---|
1535 | n/a | * does not fit in MPD_MINALLOC. |
---|
1536 | n/a | */ |
---|
1537 | n/a | int |
---|
1538 | n/a | mpd_qcheck_nan(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
1539 | n/a | uint32_t *status) |
---|
1540 | n/a | { |
---|
1541 | n/a | if (mpd_isnan(a)) { |
---|
1542 | n/a | *status |= mpd_issnan(a) ? MPD_Invalid_operation : 0; |
---|
1543 | n/a | mpd_qcopy(result, a, status); |
---|
1544 | n/a | mpd_set_qnan(result); |
---|
1545 | n/a | _mpd_fix_nan(result, ctx); |
---|
1546 | n/a | return 1; |
---|
1547 | n/a | } |
---|
1548 | n/a | return 0; |
---|
1549 | n/a | } |
---|
1550 | n/a | |
---|
1551 | n/a | /* |
---|
1552 | n/a | * Check if either operand is NaN, copy to result and return 1 if this |
---|
1553 | n/a | * is the case. Copying can fail since NaNs are allowed to have a payload |
---|
1554 | n/a | * that does not fit in MPD_MINALLOC. |
---|
1555 | n/a | */ |
---|
1556 | n/a | int |
---|
1557 | n/a | mpd_qcheck_nans(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
1558 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
1559 | n/a | { |
---|
1560 | n/a | if ((a->flags|b->flags)&(MPD_NAN|MPD_SNAN)) { |
---|
1561 | n/a | const mpd_t *choice = b; |
---|
1562 | n/a | if (mpd_issnan(a)) { |
---|
1563 | n/a | choice = a; |
---|
1564 | n/a | *status |= MPD_Invalid_operation; |
---|
1565 | n/a | } |
---|
1566 | n/a | else if (mpd_issnan(b)) { |
---|
1567 | n/a | *status |= MPD_Invalid_operation; |
---|
1568 | n/a | } |
---|
1569 | n/a | else if (mpd_isqnan(a)) { |
---|
1570 | n/a | choice = a; |
---|
1571 | n/a | } |
---|
1572 | n/a | mpd_qcopy(result, choice, status); |
---|
1573 | n/a | mpd_set_qnan(result); |
---|
1574 | n/a | _mpd_fix_nan(result, ctx); |
---|
1575 | n/a | return 1; |
---|
1576 | n/a | } |
---|
1577 | n/a | return 0; |
---|
1578 | n/a | } |
---|
1579 | n/a | |
---|
1580 | n/a | /* |
---|
1581 | n/a | * Check if one of the operands is NaN, copy to result and return 1 if this |
---|
1582 | n/a | * is the case. Copying can fail since NaNs are allowed to have a payload |
---|
1583 | n/a | * that does not fit in MPD_MINALLOC. |
---|
1584 | n/a | */ |
---|
1585 | n/a | static int |
---|
1586 | n/a | mpd_qcheck_3nans(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, |
---|
1587 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
1588 | n/a | { |
---|
1589 | n/a | if ((a->flags|b->flags|c->flags)&(MPD_NAN|MPD_SNAN)) { |
---|
1590 | n/a | const mpd_t *choice = c; |
---|
1591 | n/a | if (mpd_issnan(a)) { |
---|
1592 | n/a | choice = a; |
---|
1593 | n/a | *status |= MPD_Invalid_operation; |
---|
1594 | n/a | } |
---|
1595 | n/a | else if (mpd_issnan(b)) { |
---|
1596 | n/a | choice = b; |
---|
1597 | n/a | *status |= MPD_Invalid_operation; |
---|
1598 | n/a | } |
---|
1599 | n/a | else if (mpd_issnan(c)) { |
---|
1600 | n/a | *status |= MPD_Invalid_operation; |
---|
1601 | n/a | } |
---|
1602 | n/a | else if (mpd_isqnan(a)) { |
---|
1603 | n/a | choice = a; |
---|
1604 | n/a | } |
---|
1605 | n/a | else if (mpd_isqnan(b)) { |
---|
1606 | n/a | choice = b; |
---|
1607 | n/a | } |
---|
1608 | n/a | mpd_qcopy(result, choice, status); |
---|
1609 | n/a | mpd_set_qnan(result); |
---|
1610 | n/a | _mpd_fix_nan(result, ctx); |
---|
1611 | n/a | return 1; |
---|
1612 | n/a | } |
---|
1613 | n/a | return 0; |
---|
1614 | n/a | } |
---|
1615 | n/a | |
---|
1616 | n/a | /* Check if rounding digit 'rnd' leads to an increment. */ |
---|
1617 | n/a | static inline int |
---|
1618 | n/a | _mpd_rnd_incr(const mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx) |
---|
1619 | n/a | { |
---|
1620 | n/a | int ld; |
---|
1621 | n/a | |
---|
1622 | n/a | switch (ctx->round) { |
---|
1623 | n/a | case MPD_ROUND_DOWN: case MPD_ROUND_TRUNC: |
---|
1624 | n/a | return 0; |
---|
1625 | n/a | case MPD_ROUND_HALF_UP: |
---|
1626 | n/a | return (rnd >= 5); |
---|
1627 | n/a | case MPD_ROUND_HALF_EVEN: |
---|
1628 | n/a | return (rnd > 5) || ((rnd == 5) && mpd_isoddcoeff(dec)); |
---|
1629 | n/a | case MPD_ROUND_CEILING: |
---|
1630 | n/a | return !(rnd == 0 || mpd_isnegative(dec)); |
---|
1631 | n/a | case MPD_ROUND_FLOOR: |
---|
1632 | n/a | return !(rnd == 0 || mpd_ispositive(dec)); |
---|
1633 | n/a | case MPD_ROUND_HALF_DOWN: |
---|
1634 | n/a | return (rnd > 5); |
---|
1635 | n/a | case MPD_ROUND_UP: |
---|
1636 | n/a | return !(rnd == 0); |
---|
1637 | n/a | case MPD_ROUND_05UP: |
---|
1638 | n/a | ld = (int)mpd_lsd(dec->data[0]); |
---|
1639 | n/a | return (!(rnd == 0) && (ld == 0 || ld == 5)); |
---|
1640 | n/a | default: |
---|
1641 | n/a | /* Without a valid context, further results will be undefined. */ |
---|
1642 | n/a | return 0; /* GCOV_NOT_REACHED */ |
---|
1643 | n/a | } |
---|
1644 | n/a | } |
---|
1645 | n/a | |
---|
1646 | n/a | /* |
---|
1647 | n/a | * Apply rounding to a decimal that has been right-shifted into a full |
---|
1648 | n/a | * precision decimal. If an increment leads to an overflow of the precision, |
---|
1649 | n/a | * adjust the coefficient and the exponent and check the new exponent for |
---|
1650 | n/a | * overflow. |
---|
1651 | n/a | */ |
---|
1652 | n/a | static inline void |
---|
1653 | n/a | _mpd_apply_round(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx, |
---|
1654 | n/a | uint32_t *status) |
---|
1655 | n/a | { |
---|
1656 | n/a | if (_mpd_rnd_incr(dec, rnd, ctx)) { |
---|
1657 | n/a | /* We have a number with exactly ctx->prec digits. The increment |
---|
1658 | n/a | * can only lead to an overflow if the decimal is all nines. In |
---|
1659 | n/a | * that case, the result is a power of ten with prec+1 digits. |
---|
1660 | n/a | * |
---|
1661 | n/a | * If the precision is a multiple of MPD_RDIGITS, this situation is |
---|
1662 | n/a | * detected by _mpd_baseincr returning a carry. |
---|
1663 | n/a | * If the precision is not a multiple of MPD_RDIGITS, we have to |
---|
1664 | n/a | * check if the result has one digit too many. |
---|
1665 | n/a | */ |
---|
1666 | n/a | mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len); |
---|
1667 | n/a | if (carry) { |
---|
1668 | n/a | dec->data[dec->len-1] = mpd_pow10[MPD_RDIGITS-1]; |
---|
1669 | n/a | dec->exp += 1; |
---|
1670 | n/a | _mpd_check_exp(dec, ctx, status); |
---|
1671 | n/a | return; |
---|
1672 | n/a | } |
---|
1673 | n/a | mpd_setdigits(dec); |
---|
1674 | n/a | if (dec->digits > ctx->prec) { |
---|
1675 | n/a | mpd_qshiftr_inplace(dec, 1); |
---|
1676 | n/a | dec->exp += 1; |
---|
1677 | n/a | dec->digits = ctx->prec; |
---|
1678 | n/a | _mpd_check_exp(dec, ctx, status); |
---|
1679 | n/a | } |
---|
1680 | n/a | } |
---|
1681 | n/a | } |
---|
1682 | n/a | |
---|
1683 | n/a | /* |
---|
1684 | n/a | * Apply rounding to a decimal. Allow overflow of the precision. |
---|
1685 | n/a | */ |
---|
1686 | n/a | static inline void |
---|
1687 | n/a | _mpd_apply_round_excess(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx, |
---|
1688 | n/a | uint32_t *status) |
---|
1689 | n/a | { |
---|
1690 | n/a | if (_mpd_rnd_incr(dec, rnd, ctx)) { |
---|
1691 | n/a | mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len); |
---|
1692 | n/a | if (carry) { |
---|
1693 | n/a | if (!mpd_qresize(dec, dec->len+1, status)) { |
---|
1694 | n/a | return; |
---|
1695 | n/a | } |
---|
1696 | n/a | dec->data[dec->len] = 1; |
---|
1697 | n/a | dec->len += 1; |
---|
1698 | n/a | } |
---|
1699 | n/a | mpd_setdigits(dec); |
---|
1700 | n/a | } |
---|
1701 | n/a | } |
---|
1702 | n/a | |
---|
1703 | n/a | /* |
---|
1704 | n/a | * Apply rounding to a decimal that has been right-shifted into a decimal |
---|
1705 | n/a | * with full precision or less. Return failure if an increment would |
---|
1706 | n/a | * overflow the precision. |
---|
1707 | n/a | */ |
---|
1708 | n/a | static inline int |
---|
1709 | n/a | _mpd_apply_round_fit(mpd_t *dec, mpd_uint_t rnd, const mpd_context_t *ctx, |
---|
1710 | n/a | uint32_t *status) |
---|
1711 | n/a | { |
---|
1712 | n/a | if (_mpd_rnd_incr(dec, rnd, ctx)) { |
---|
1713 | n/a | mpd_uint_t carry = _mpd_baseincr(dec->data, dec->len); |
---|
1714 | n/a | if (carry) { |
---|
1715 | n/a | if (!mpd_qresize(dec, dec->len+1, status)) { |
---|
1716 | n/a | return 0; |
---|
1717 | n/a | } |
---|
1718 | n/a | dec->data[dec->len] = 1; |
---|
1719 | n/a | dec->len += 1; |
---|
1720 | n/a | } |
---|
1721 | n/a | mpd_setdigits(dec); |
---|
1722 | n/a | if (dec->digits > ctx->prec) { |
---|
1723 | n/a | mpd_seterror(dec, MPD_Invalid_operation, status); |
---|
1724 | n/a | return 0; |
---|
1725 | n/a | } |
---|
1726 | n/a | } |
---|
1727 | n/a | return 1; |
---|
1728 | n/a | } |
---|
1729 | n/a | |
---|
1730 | n/a | /* Check a normal number for overflow, underflow, clamping. If the operand |
---|
1731 | n/a | is modified, it will be zero, special or (sub)normal with a coefficient |
---|
1732 | n/a | that fits into the current context precision. */ |
---|
1733 | n/a | static inline void |
---|
1734 | n/a | _mpd_check_exp(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status) |
---|
1735 | n/a | { |
---|
1736 | n/a | mpd_ssize_t adjexp, etiny, shift; |
---|
1737 | n/a | int rnd; |
---|
1738 | n/a | |
---|
1739 | n/a | adjexp = mpd_adjexp(dec); |
---|
1740 | n/a | if (adjexp > ctx->emax) { |
---|
1741 | n/a | |
---|
1742 | n/a | if (mpd_iszerocoeff(dec)) { |
---|
1743 | n/a | dec->exp = ctx->emax; |
---|
1744 | n/a | if (ctx->clamp) { |
---|
1745 | n/a | dec->exp -= (ctx->prec-1); |
---|
1746 | n/a | } |
---|
1747 | n/a | mpd_zerocoeff(dec); |
---|
1748 | n/a | *status |= MPD_Clamped; |
---|
1749 | n/a | return; |
---|
1750 | n/a | } |
---|
1751 | n/a | |
---|
1752 | n/a | switch (ctx->round) { |
---|
1753 | n/a | case MPD_ROUND_HALF_UP: case MPD_ROUND_HALF_EVEN: |
---|
1754 | n/a | case MPD_ROUND_HALF_DOWN: case MPD_ROUND_UP: |
---|
1755 | n/a | case MPD_ROUND_TRUNC: |
---|
1756 | n/a | mpd_setspecial(dec, mpd_sign(dec), MPD_INF); |
---|
1757 | n/a | break; |
---|
1758 | n/a | case MPD_ROUND_DOWN: case MPD_ROUND_05UP: |
---|
1759 | n/a | mpd_qmaxcoeff(dec, ctx, status); |
---|
1760 | n/a | dec->exp = ctx->emax - ctx->prec + 1; |
---|
1761 | n/a | break; |
---|
1762 | n/a | case MPD_ROUND_CEILING: |
---|
1763 | n/a | if (mpd_isnegative(dec)) { |
---|
1764 | n/a | mpd_qmaxcoeff(dec, ctx, status); |
---|
1765 | n/a | dec->exp = ctx->emax - ctx->prec + 1; |
---|
1766 | n/a | } |
---|
1767 | n/a | else { |
---|
1768 | n/a | mpd_setspecial(dec, MPD_POS, MPD_INF); |
---|
1769 | n/a | } |
---|
1770 | n/a | break; |
---|
1771 | n/a | case MPD_ROUND_FLOOR: |
---|
1772 | n/a | if (mpd_ispositive(dec)) { |
---|
1773 | n/a | mpd_qmaxcoeff(dec, ctx, status); |
---|
1774 | n/a | dec->exp = ctx->emax - ctx->prec + 1; |
---|
1775 | n/a | } |
---|
1776 | n/a | else { |
---|
1777 | n/a | mpd_setspecial(dec, MPD_NEG, MPD_INF); |
---|
1778 | n/a | } |
---|
1779 | n/a | break; |
---|
1780 | n/a | default: /* debug */ |
---|
1781 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
1782 | n/a | } |
---|
1783 | n/a | |
---|
1784 | n/a | *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; |
---|
1785 | n/a | |
---|
1786 | n/a | } /* fold down */ |
---|
1787 | n/a | else if (ctx->clamp && dec->exp > mpd_etop(ctx)) { |
---|
1788 | n/a | /* At this point adjexp=exp+digits-1 <= emax and exp > etop=emax-prec+1: |
---|
1789 | n/a | * (1) shift = exp -emax+prec-1 > 0 |
---|
1790 | n/a | * (2) digits+shift = exp+digits-1 - emax + prec <= prec */ |
---|
1791 | n/a | shift = dec->exp - mpd_etop(ctx); |
---|
1792 | n/a | if (!mpd_qshiftl(dec, dec, shift, status)) { |
---|
1793 | n/a | return; |
---|
1794 | n/a | } |
---|
1795 | n/a | dec->exp -= shift; |
---|
1796 | n/a | *status |= MPD_Clamped; |
---|
1797 | n/a | if (!mpd_iszerocoeff(dec) && adjexp < ctx->emin) { |
---|
1798 | n/a | /* Underflow is impossible, since exp < etiny=emin-prec+1 |
---|
1799 | n/a | * and exp > etop=emax-prec+1 would imply emax < emin. */ |
---|
1800 | n/a | *status |= MPD_Subnormal; |
---|
1801 | n/a | } |
---|
1802 | n/a | } |
---|
1803 | n/a | else if (adjexp < ctx->emin) { |
---|
1804 | n/a | |
---|
1805 | n/a | etiny = mpd_etiny(ctx); |
---|
1806 | n/a | |
---|
1807 | n/a | if (mpd_iszerocoeff(dec)) { |
---|
1808 | n/a | if (dec->exp < etiny) { |
---|
1809 | n/a | dec->exp = etiny; |
---|
1810 | n/a | mpd_zerocoeff(dec); |
---|
1811 | n/a | *status |= MPD_Clamped; |
---|
1812 | n/a | } |
---|
1813 | n/a | return; |
---|
1814 | n/a | } |
---|
1815 | n/a | |
---|
1816 | n/a | *status |= MPD_Subnormal; |
---|
1817 | n/a | if (dec->exp < etiny) { |
---|
1818 | n/a | /* At this point adjexp=exp+digits-1 < emin and exp < etiny=emin-prec+1: |
---|
1819 | n/a | * (1) shift = emin-prec+1 - exp > 0 |
---|
1820 | n/a | * (2) digits-shift = exp+digits-1 - emin + prec < prec */ |
---|
1821 | n/a | shift = etiny - dec->exp; |
---|
1822 | n/a | rnd = (int)mpd_qshiftr_inplace(dec, shift); |
---|
1823 | n/a | dec->exp = etiny; |
---|
1824 | n/a | /* We always have a spare digit in case of an increment. */ |
---|
1825 | n/a | _mpd_apply_round_excess(dec, rnd, ctx, status); |
---|
1826 | n/a | *status |= MPD_Rounded; |
---|
1827 | n/a | if (rnd) { |
---|
1828 | n/a | *status |= (MPD_Inexact|MPD_Underflow); |
---|
1829 | n/a | if (mpd_iszerocoeff(dec)) { |
---|
1830 | n/a | mpd_zerocoeff(dec); |
---|
1831 | n/a | *status |= MPD_Clamped; |
---|
1832 | n/a | } |
---|
1833 | n/a | } |
---|
1834 | n/a | } |
---|
1835 | n/a | /* Case exp >= etiny=emin-prec+1: |
---|
1836 | n/a | * (1) adjexp=exp+digits-1 < emin |
---|
1837 | n/a | * (2) digits < emin-exp+1 <= prec */ |
---|
1838 | n/a | } |
---|
1839 | n/a | } |
---|
1840 | n/a | |
---|
1841 | n/a | /* Transcendental functions do not always set Underflow reliably, |
---|
1842 | n/a | * since they only use as much precision as is necessary for correct |
---|
1843 | n/a | * rounding. If a result like 1.0000000000e-101 is finalized, there |
---|
1844 | n/a | * is no rounding digit that would trigger Underflow. But we can |
---|
1845 | n/a | * assume Inexact, so a short check suffices. */ |
---|
1846 | n/a | static inline void |
---|
1847 | n/a | mpd_check_underflow(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status) |
---|
1848 | n/a | { |
---|
1849 | n/a | if (mpd_adjexp(dec) < ctx->emin && !mpd_iszero(dec) && |
---|
1850 | n/a | dec->exp < mpd_etiny(ctx)) { |
---|
1851 | n/a | *status |= MPD_Underflow; |
---|
1852 | n/a | } |
---|
1853 | n/a | } |
---|
1854 | n/a | |
---|
1855 | n/a | /* Check if a normal number must be rounded after the exponent has been checked. */ |
---|
1856 | n/a | static inline void |
---|
1857 | n/a | _mpd_check_round(mpd_t *dec, const mpd_context_t *ctx, uint32_t *status) |
---|
1858 | n/a | { |
---|
1859 | n/a | mpd_uint_t rnd; |
---|
1860 | n/a | mpd_ssize_t shift; |
---|
1861 | n/a | |
---|
1862 | n/a | /* must handle specials: _mpd_check_exp() can produce infinities or NaNs */ |
---|
1863 | n/a | if (mpd_isspecial(dec)) { |
---|
1864 | n/a | return; |
---|
1865 | n/a | } |
---|
1866 | n/a | |
---|
1867 | n/a | if (dec->digits > ctx->prec) { |
---|
1868 | n/a | shift = dec->digits - ctx->prec; |
---|
1869 | n/a | rnd = mpd_qshiftr_inplace(dec, shift); |
---|
1870 | n/a | dec->exp += shift; |
---|
1871 | n/a | _mpd_apply_round(dec, rnd, ctx, status); |
---|
1872 | n/a | *status |= MPD_Rounded; |
---|
1873 | n/a | if (rnd) { |
---|
1874 | n/a | *status |= MPD_Inexact; |
---|
1875 | n/a | } |
---|
1876 | n/a | } |
---|
1877 | n/a | } |
---|
1878 | n/a | |
---|
1879 | n/a | /* Finalize all operations. */ |
---|
1880 | n/a | void |
---|
1881 | n/a | mpd_qfinalize(mpd_t *result, const mpd_context_t *ctx, uint32_t *status) |
---|
1882 | n/a | { |
---|
1883 | n/a | if (mpd_isspecial(result)) { |
---|
1884 | n/a | if (mpd_isnan(result)) { |
---|
1885 | n/a | _mpd_fix_nan(result, ctx); |
---|
1886 | n/a | } |
---|
1887 | n/a | return; |
---|
1888 | n/a | } |
---|
1889 | n/a | |
---|
1890 | n/a | _mpd_check_exp(result, ctx, status); |
---|
1891 | n/a | _mpd_check_round(result, ctx, status); |
---|
1892 | n/a | } |
---|
1893 | n/a | |
---|
1894 | n/a | |
---|
1895 | n/a | /******************************************************************************/ |
---|
1896 | n/a | /* Copying */ |
---|
1897 | n/a | /******************************************************************************/ |
---|
1898 | n/a | |
---|
1899 | n/a | /* Internal function: Copy a decimal, share data with src: USE WITH CARE! */ |
---|
1900 | n/a | static inline void |
---|
1901 | n/a | _mpd_copy_shared(mpd_t *dest, const mpd_t *src) |
---|
1902 | n/a | { |
---|
1903 | n/a | dest->flags = src->flags; |
---|
1904 | n/a | dest->exp = src->exp; |
---|
1905 | n/a | dest->digits = src->digits; |
---|
1906 | n/a | dest->len = src->len; |
---|
1907 | n/a | dest->alloc = src->alloc; |
---|
1908 | n/a | dest->data = src->data; |
---|
1909 | n/a | |
---|
1910 | n/a | mpd_set_shared_data(dest); |
---|
1911 | n/a | } |
---|
1912 | n/a | |
---|
1913 | n/a | /* |
---|
1914 | n/a | * Copy a decimal. In case of an error, status is set to MPD_Malloc_error. |
---|
1915 | n/a | */ |
---|
1916 | n/a | int |
---|
1917 | n/a | mpd_qcopy(mpd_t *result, const mpd_t *a, uint32_t *status) |
---|
1918 | n/a | { |
---|
1919 | n/a | if (result == a) return 1; |
---|
1920 | n/a | |
---|
1921 | n/a | if (!mpd_qresize(result, a->len, status)) { |
---|
1922 | n/a | return 0; |
---|
1923 | n/a | } |
---|
1924 | n/a | |
---|
1925 | n/a | mpd_copy_flags(result, a); |
---|
1926 | n/a | result->exp = a->exp; |
---|
1927 | n/a | result->digits = a->digits; |
---|
1928 | n/a | result->len = a->len; |
---|
1929 | n/a | memcpy(result->data, a->data, a->len * (sizeof *result->data)); |
---|
1930 | n/a | |
---|
1931 | n/a | return 1; |
---|
1932 | n/a | } |
---|
1933 | n/a | |
---|
1934 | n/a | /* |
---|
1935 | n/a | * Copy to a decimal with a static buffer. The caller has to make sure that |
---|
1936 | n/a | * the buffer is big enough. Cannot fail. |
---|
1937 | n/a | */ |
---|
1938 | n/a | static void |
---|
1939 | n/a | mpd_qcopy_static(mpd_t *result, const mpd_t *a) |
---|
1940 | n/a | { |
---|
1941 | n/a | if (result == a) return; |
---|
1942 | n/a | |
---|
1943 | n/a | memcpy(result->data, a->data, a->len * (sizeof *result->data)); |
---|
1944 | n/a | |
---|
1945 | n/a | mpd_copy_flags(result, a); |
---|
1946 | n/a | result->exp = a->exp; |
---|
1947 | n/a | result->digits = a->digits; |
---|
1948 | n/a | result->len = a->len; |
---|
1949 | n/a | } |
---|
1950 | n/a | |
---|
1951 | n/a | /* |
---|
1952 | n/a | * Return a newly allocated copy of the operand. In case of an error, |
---|
1953 | n/a | * status is set to MPD_Malloc_error and the return value is NULL. |
---|
1954 | n/a | */ |
---|
1955 | n/a | mpd_t * |
---|
1956 | n/a | mpd_qncopy(const mpd_t *a) |
---|
1957 | n/a | { |
---|
1958 | n/a | mpd_t *result; |
---|
1959 | n/a | |
---|
1960 | n/a | if ((result = mpd_qnew_size(a->len)) == NULL) { |
---|
1961 | n/a | return NULL; |
---|
1962 | n/a | } |
---|
1963 | n/a | memcpy(result->data, a->data, a->len * (sizeof *result->data)); |
---|
1964 | n/a | mpd_copy_flags(result, a); |
---|
1965 | n/a | result->exp = a->exp; |
---|
1966 | n/a | result->digits = a->digits; |
---|
1967 | n/a | result->len = a->len; |
---|
1968 | n/a | |
---|
1969 | n/a | return result; |
---|
1970 | n/a | } |
---|
1971 | n/a | |
---|
1972 | n/a | /* |
---|
1973 | n/a | * Copy a decimal and set the sign to positive. In case of an error, the |
---|
1974 | n/a | * status is set to MPD_Malloc_error. |
---|
1975 | n/a | */ |
---|
1976 | n/a | int |
---|
1977 | n/a | mpd_qcopy_abs(mpd_t *result, const mpd_t *a, uint32_t *status) |
---|
1978 | n/a | { |
---|
1979 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
1980 | n/a | return 0; |
---|
1981 | n/a | } |
---|
1982 | n/a | mpd_set_positive(result); |
---|
1983 | n/a | return 1; |
---|
1984 | n/a | } |
---|
1985 | n/a | |
---|
1986 | n/a | /* |
---|
1987 | n/a | * Copy a decimal and negate the sign. In case of an error, the |
---|
1988 | n/a | * status is set to MPD_Malloc_error. |
---|
1989 | n/a | */ |
---|
1990 | n/a | int |
---|
1991 | n/a | mpd_qcopy_negate(mpd_t *result, const mpd_t *a, uint32_t *status) |
---|
1992 | n/a | { |
---|
1993 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
1994 | n/a | return 0; |
---|
1995 | n/a | } |
---|
1996 | n/a | _mpd_negate(result); |
---|
1997 | n/a | return 1; |
---|
1998 | n/a | } |
---|
1999 | n/a | |
---|
2000 | n/a | /* |
---|
2001 | n/a | * Copy a decimal, setting the sign of the first operand to the sign of the |
---|
2002 | n/a | * second operand. In case of an error, the status is set to MPD_Malloc_error. |
---|
2003 | n/a | */ |
---|
2004 | n/a | int |
---|
2005 | n/a | mpd_qcopy_sign(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status) |
---|
2006 | n/a | { |
---|
2007 | n/a | uint8_t sign_b = mpd_sign(b); /* result may equal b! */ |
---|
2008 | n/a | |
---|
2009 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
2010 | n/a | return 0; |
---|
2011 | n/a | } |
---|
2012 | n/a | mpd_set_sign(result, sign_b); |
---|
2013 | n/a | return 1; |
---|
2014 | n/a | } |
---|
2015 | n/a | |
---|
2016 | n/a | |
---|
2017 | n/a | /******************************************************************************/ |
---|
2018 | n/a | /* Comparisons */ |
---|
2019 | n/a | /******************************************************************************/ |
---|
2020 | n/a | |
---|
2021 | n/a | /* |
---|
2022 | n/a | * For all functions that compare two operands and return an int the usual |
---|
2023 | n/a | * convention applies to the return value: |
---|
2024 | n/a | * |
---|
2025 | n/a | * -1 if op1 < op2 |
---|
2026 | n/a | * 0 if op1 == op2 |
---|
2027 | n/a | * 1 if op1 > op2 |
---|
2028 | n/a | * |
---|
2029 | n/a | * INT_MAX for error |
---|
2030 | n/a | */ |
---|
2031 | n/a | |
---|
2032 | n/a | |
---|
2033 | n/a | /* Convenience macro. If a and b are not equal, return from the calling |
---|
2034 | n/a | * function with the correct comparison value. */ |
---|
2035 | n/a | #define CMP_EQUAL_OR_RETURN(a, b) \ |
---|
2036 | n/a | if (a != b) { \ |
---|
2037 | n/a | if (a < b) { \ |
---|
2038 | n/a | return -1; \ |
---|
2039 | n/a | } \ |
---|
2040 | n/a | return 1; \ |
---|
2041 | n/a | } |
---|
2042 | n/a | |
---|
2043 | n/a | /* |
---|
2044 | n/a | * Compare the data of big and small. This function does the equivalent |
---|
2045 | n/a | * of first shifting small to the left and then comparing the data of |
---|
2046 | n/a | * big and small, except that no allocation for the left shift is needed. |
---|
2047 | n/a | */ |
---|
2048 | n/a | static int |
---|
2049 | n/a | _mpd_basecmp(mpd_uint_t *big, mpd_uint_t *small, mpd_size_t n, mpd_size_t m, |
---|
2050 | n/a | mpd_size_t shift) |
---|
2051 | n/a | { |
---|
2052 | n/a | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) |
---|
2053 | n/a | /* spurious uninitialized warnings */ |
---|
2054 | n/a | mpd_uint_t l=l, lprev=lprev, h=h; |
---|
2055 | n/a | #else |
---|
2056 | n/a | mpd_uint_t l, lprev, h; |
---|
2057 | n/a | #endif |
---|
2058 | n/a | mpd_uint_t q, r; |
---|
2059 | n/a | mpd_uint_t ph, x; |
---|
2060 | n/a | |
---|
2061 | n/a | assert(m > 0 && n >= m && shift > 0); |
---|
2062 | n/a | |
---|
2063 | n/a | _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS); |
---|
2064 | n/a | |
---|
2065 | n/a | if (r != 0) { |
---|
2066 | n/a | |
---|
2067 | n/a | ph = mpd_pow10[r]; |
---|
2068 | n/a | |
---|
2069 | n/a | --m; --n; |
---|
2070 | n/a | _mpd_divmod_pow10(&h, &lprev, small[m--], MPD_RDIGITS-r); |
---|
2071 | n/a | if (h != 0) { |
---|
2072 | n/a | CMP_EQUAL_OR_RETURN(big[n], h) |
---|
2073 | n/a | --n; |
---|
2074 | n/a | } |
---|
2075 | n/a | for (; m != MPD_SIZE_MAX; m--,n--) { |
---|
2076 | n/a | _mpd_divmod_pow10(&h, &l, small[m], MPD_RDIGITS-r); |
---|
2077 | n/a | x = ph * lprev + h; |
---|
2078 | n/a | CMP_EQUAL_OR_RETURN(big[n], x) |
---|
2079 | n/a | lprev = l; |
---|
2080 | n/a | } |
---|
2081 | n/a | x = ph * lprev; |
---|
2082 | n/a | CMP_EQUAL_OR_RETURN(big[q], x) |
---|
2083 | n/a | } |
---|
2084 | n/a | else { |
---|
2085 | n/a | while (--m != MPD_SIZE_MAX) { |
---|
2086 | n/a | CMP_EQUAL_OR_RETURN(big[m+q], small[m]) |
---|
2087 | n/a | } |
---|
2088 | n/a | } |
---|
2089 | n/a | |
---|
2090 | n/a | return !_mpd_isallzero(big, q); |
---|
2091 | n/a | } |
---|
2092 | n/a | |
---|
2093 | n/a | /* Compare two decimals with the same adjusted exponent. */ |
---|
2094 | n/a | static int |
---|
2095 | n/a | _mpd_cmp_same_adjexp(const mpd_t *a, const mpd_t *b) |
---|
2096 | n/a | { |
---|
2097 | n/a | mpd_ssize_t shift, i; |
---|
2098 | n/a | |
---|
2099 | n/a | if (a->exp != b->exp) { |
---|
2100 | n/a | /* Cannot wrap: a->exp + a->digits = b->exp + b->digits, so |
---|
2101 | n/a | * a->exp - b->exp = b->digits - a->digits. */ |
---|
2102 | n/a | shift = a->exp - b->exp; |
---|
2103 | n/a | if (shift > 0) { |
---|
2104 | n/a | return -1 * _mpd_basecmp(b->data, a->data, b->len, a->len, shift); |
---|
2105 | n/a | } |
---|
2106 | n/a | else { |
---|
2107 | n/a | return _mpd_basecmp(a->data, b->data, a->len, b->len, -shift); |
---|
2108 | n/a | } |
---|
2109 | n/a | } |
---|
2110 | n/a | |
---|
2111 | n/a | /* |
---|
2112 | n/a | * At this point adjexp(a) == adjexp(b) and a->exp == b->exp, |
---|
2113 | n/a | * so a->digits == b->digits, therefore a->len == b->len. |
---|
2114 | n/a | */ |
---|
2115 | n/a | for (i = a->len-1; i >= 0; --i) { |
---|
2116 | n/a | CMP_EQUAL_OR_RETURN(a->data[i], b->data[i]) |
---|
2117 | n/a | } |
---|
2118 | n/a | |
---|
2119 | n/a | return 0; |
---|
2120 | n/a | } |
---|
2121 | n/a | |
---|
2122 | n/a | /* Compare two numerical values. */ |
---|
2123 | n/a | static int |
---|
2124 | n/a | _mpd_cmp(const mpd_t *a, const mpd_t *b) |
---|
2125 | n/a | { |
---|
2126 | n/a | mpd_ssize_t adjexp_a, adjexp_b; |
---|
2127 | n/a | |
---|
2128 | n/a | /* equal pointers */ |
---|
2129 | n/a | if (a == b) { |
---|
2130 | n/a | return 0; |
---|
2131 | n/a | } |
---|
2132 | n/a | |
---|
2133 | n/a | /* infinities */ |
---|
2134 | n/a | if (mpd_isinfinite(a)) { |
---|
2135 | n/a | if (mpd_isinfinite(b)) { |
---|
2136 | n/a | return mpd_isnegative(b) - mpd_isnegative(a); |
---|
2137 | n/a | } |
---|
2138 | n/a | return mpd_arith_sign(a); |
---|
2139 | n/a | } |
---|
2140 | n/a | if (mpd_isinfinite(b)) { |
---|
2141 | n/a | return -mpd_arith_sign(b); |
---|
2142 | n/a | } |
---|
2143 | n/a | |
---|
2144 | n/a | /* zeros */ |
---|
2145 | n/a | if (mpd_iszerocoeff(a)) { |
---|
2146 | n/a | if (mpd_iszerocoeff(b)) { |
---|
2147 | n/a | return 0; |
---|
2148 | n/a | } |
---|
2149 | n/a | return -mpd_arith_sign(b); |
---|
2150 | n/a | } |
---|
2151 | n/a | if (mpd_iszerocoeff(b)) { |
---|
2152 | n/a | return mpd_arith_sign(a); |
---|
2153 | n/a | } |
---|
2154 | n/a | |
---|
2155 | n/a | /* different signs */ |
---|
2156 | n/a | if (mpd_sign(a) != mpd_sign(b)) { |
---|
2157 | n/a | return mpd_sign(b) - mpd_sign(a); |
---|
2158 | n/a | } |
---|
2159 | n/a | |
---|
2160 | n/a | /* different adjusted exponents */ |
---|
2161 | n/a | adjexp_a = mpd_adjexp(a); |
---|
2162 | n/a | adjexp_b = mpd_adjexp(b); |
---|
2163 | n/a | if (adjexp_a != adjexp_b) { |
---|
2164 | n/a | if (adjexp_a < adjexp_b) { |
---|
2165 | n/a | return -1 * mpd_arith_sign(a); |
---|
2166 | n/a | } |
---|
2167 | n/a | return mpd_arith_sign(a); |
---|
2168 | n/a | } |
---|
2169 | n/a | |
---|
2170 | n/a | /* same adjusted exponents */ |
---|
2171 | n/a | return _mpd_cmp_same_adjexp(a, b) * mpd_arith_sign(a); |
---|
2172 | n/a | } |
---|
2173 | n/a | |
---|
2174 | n/a | /* Compare the absolutes of two numerical values. */ |
---|
2175 | n/a | static int |
---|
2176 | n/a | _mpd_cmp_abs(const mpd_t *a, const mpd_t *b) |
---|
2177 | n/a | { |
---|
2178 | n/a | mpd_ssize_t adjexp_a, adjexp_b; |
---|
2179 | n/a | |
---|
2180 | n/a | /* equal pointers */ |
---|
2181 | n/a | if (a == b) { |
---|
2182 | n/a | return 0; |
---|
2183 | n/a | } |
---|
2184 | n/a | |
---|
2185 | n/a | /* infinities */ |
---|
2186 | n/a | if (mpd_isinfinite(a)) { |
---|
2187 | n/a | if (mpd_isinfinite(b)) { |
---|
2188 | n/a | return 0; |
---|
2189 | n/a | } |
---|
2190 | n/a | return 1; |
---|
2191 | n/a | } |
---|
2192 | n/a | if (mpd_isinfinite(b)) { |
---|
2193 | n/a | return -1; |
---|
2194 | n/a | } |
---|
2195 | n/a | |
---|
2196 | n/a | /* zeros */ |
---|
2197 | n/a | if (mpd_iszerocoeff(a)) { |
---|
2198 | n/a | if (mpd_iszerocoeff(b)) { |
---|
2199 | n/a | return 0; |
---|
2200 | n/a | } |
---|
2201 | n/a | return -1; |
---|
2202 | n/a | } |
---|
2203 | n/a | if (mpd_iszerocoeff(b)) { |
---|
2204 | n/a | return 1; |
---|
2205 | n/a | } |
---|
2206 | n/a | |
---|
2207 | n/a | /* different adjusted exponents */ |
---|
2208 | n/a | adjexp_a = mpd_adjexp(a); |
---|
2209 | n/a | adjexp_b = mpd_adjexp(b); |
---|
2210 | n/a | if (adjexp_a != adjexp_b) { |
---|
2211 | n/a | if (adjexp_a < adjexp_b) { |
---|
2212 | n/a | return -1; |
---|
2213 | n/a | } |
---|
2214 | n/a | return 1; |
---|
2215 | n/a | } |
---|
2216 | n/a | |
---|
2217 | n/a | /* same adjusted exponents */ |
---|
2218 | n/a | return _mpd_cmp_same_adjexp(a, b); |
---|
2219 | n/a | } |
---|
2220 | n/a | |
---|
2221 | n/a | /* Compare two values and return an integer result. */ |
---|
2222 | n/a | int |
---|
2223 | n/a | mpd_qcmp(const mpd_t *a, const mpd_t *b, uint32_t *status) |
---|
2224 | n/a | { |
---|
2225 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
2226 | n/a | if (mpd_isnan(a) || mpd_isnan(b)) { |
---|
2227 | n/a | *status |= MPD_Invalid_operation; |
---|
2228 | n/a | return INT_MAX; |
---|
2229 | n/a | } |
---|
2230 | n/a | } |
---|
2231 | n/a | |
---|
2232 | n/a | return _mpd_cmp(a, b); |
---|
2233 | n/a | } |
---|
2234 | n/a | |
---|
2235 | n/a | /* |
---|
2236 | n/a | * Compare a and b, convert the usual integer result to a decimal and |
---|
2237 | n/a | * store it in 'result'. For convenience, the integer result of the comparison |
---|
2238 | n/a | * is returned. Comparisons involving NaNs return NaN/INT_MAX. |
---|
2239 | n/a | */ |
---|
2240 | n/a | int |
---|
2241 | n/a | mpd_qcompare(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2242 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2243 | n/a | { |
---|
2244 | n/a | int c; |
---|
2245 | n/a | |
---|
2246 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
2247 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
2248 | n/a | return INT_MAX; |
---|
2249 | n/a | } |
---|
2250 | n/a | } |
---|
2251 | n/a | |
---|
2252 | n/a | c = _mpd_cmp(a, b); |
---|
2253 | n/a | _settriple(result, (c < 0), (c != 0), 0); |
---|
2254 | n/a | return c; |
---|
2255 | n/a | } |
---|
2256 | n/a | |
---|
2257 | n/a | /* Same as mpd_compare(), but signal for all NaNs, i.e. also for quiet NaNs. */ |
---|
2258 | n/a | int |
---|
2259 | n/a | mpd_qcompare_signal(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2260 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2261 | n/a | { |
---|
2262 | n/a | int c; |
---|
2263 | n/a | |
---|
2264 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
2265 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
2266 | n/a | *status |= MPD_Invalid_operation; |
---|
2267 | n/a | return INT_MAX; |
---|
2268 | n/a | } |
---|
2269 | n/a | } |
---|
2270 | n/a | |
---|
2271 | n/a | c = _mpd_cmp(a, b); |
---|
2272 | n/a | _settriple(result, (c < 0), (c != 0), 0); |
---|
2273 | n/a | return c; |
---|
2274 | n/a | } |
---|
2275 | n/a | |
---|
2276 | n/a | /* Compare the operands using a total order. */ |
---|
2277 | n/a | int |
---|
2278 | n/a | mpd_cmp_total(const mpd_t *a, const mpd_t *b) |
---|
2279 | n/a | { |
---|
2280 | n/a | mpd_t aa, bb; |
---|
2281 | n/a | int nan_a, nan_b; |
---|
2282 | n/a | int c; |
---|
2283 | n/a | |
---|
2284 | n/a | if (mpd_sign(a) != mpd_sign(b)) { |
---|
2285 | n/a | return mpd_sign(b) - mpd_sign(a); |
---|
2286 | n/a | } |
---|
2287 | n/a | |
---|
2288 | n/a | |
---|
2289 | n/a | if (mpd_isnan(a)) { |
---|
2290 | n/a | c = 1; |
---|
2291 | n/a | if (mpd_isnan(b)) { |
---|
2292 | n/a | nan_a = (mpd_isqnan(a)) ? 1 : 0; |
---|
2293 | n/a | nan_b = (mpd_isqnan(b)) ? 1 : 0; |
---|
2294 | n/a | if (nan_b == nan_a) { |
---|
2295 | n/a | if (a->len > 0 && b->len > 0) { |
---|
2296 | n/a | _mpd_copy_shared(&aa, a); |
---|
2297 | n/a | _mpd_copy_shared(&bb, b); |
---|
2298 | n/a | aa.exp = bb.exp = 0; |
---|
2299 | n/a | /* compare payload */ |
---|
2300 | n/a | c = _mpd_cmp_abs(&aa, &bb); |
---|
2301 | n/a | } |
---|
2302 | n/a | else { |
---|
2303 | n/a | c = (a->len > 0) - (b->len > 0); |
---|
2304 | n/a | } |
---|
2305 | n/a | } |
---|
2306 | n/a | else { |
---|
2307 | n/a | c = nan_a - nan_b; |
---|
2308 | n/a | } |
---|
2309 | n/a | } |
---|
2310 | n/a | } |
---|
2311 | n/a | else if (mpd_isnan(b)) { |
---|
2312 | n/a | c = -1; |
---|
2313 | n/a | } |
---|
2314 | n/a | else { |
---|
2315 | n/a | c = _mpd_cmp_abs(a, b); |
---|
2316 | n/a | if (c == 0 && a->exp != b->exp) { |
---|
2317 | n/a | c = (a->exp < b->exp) ? -1 : 1; |
---|
2318 | n/a | } |
---|
2319 | n/a | } |
---|
2320 | n/a | |
---|
2321 | n/a | return c * mpd_arith_sign(a); |
---|
2322 | n/a | } |
---|
2323 | n/a | |
---|
2324 | n/a | /* |
---|
2325 | n/a | * Compare a and b according to a total order, convert the usual integer result |
---|
2326 | n/a | * to a decimal and store it in 'result'. For convenience, the integer result |
---|
2327 | n/a | * of the comparison is returned. |
---|
2328 | n/a | */ |
---|
2329 | n/a | int |
---|
2330 | n/a | mpd_compare_total(mpd_t *result, const mpd_t *a, const mpd_t *b) |
---|
2331 | n/a | { |
---|
2332 | n/a | int c; |
---|
2333 | n/a | |
---|
2334 | n/a | c = mpd_cmp_total(a, b); |
---|
2335 | n/a | _settriple(result, (c < 0), (c != 0), 0); |
---|
2336 | n/a | return c; |
---|
2337 | n/a | } |
---|
2338 | n/a | |
---|
2339 | n/a | /* Compare the magnitude of the operands using a total order. */ |
---|
2340 | n/a | int |
---|
2341 | n/a | mpd_cmp_total_mag(const mpd_t *a, const mpd_t *b) |
---|
2342 | n/a | { |
---|
2343 | n/a | mpd_t aa, bb; |
---|
2344 | n/a | |
---|
2345 | n/a | _mpd_copy_shared(&aa, a); |
---|
2346 | n/a | _mpd_copy_shared(&bb, b); |
---|
2347 | n/a | |
---|
2348 | n/a | mpd_set_positive(&aa); |
---|
2349 | n/a | mpd_set_positive(&bb); |
---|
2350 | n/a | |
---|
2351 | n/a | return mpd_cmp_total(&aa, &bb); |
---|
2352 | n/a | } |
---|
2353 | n/a | |
---|
2354 | n/a | /* |
---|
2355 | n/a | * Compare the magnitude of a and b according to a total order, convert the |
---|
2356 | n/a | * the usual integer result to a decimal and store it in 'result'. |
---|
2357 | n/a | * For convenience, the integer result of the comparison is returned. |
---|
2358 | n/a | */ |
---|
2359 | n/a | int |
---|
2360 | n/a | mpd_compare_total_mag(mpd_t *result, const mpd_t *a, const mpd_t *b) |
---|
2361 | n/a | { |
---|
2362 | n/a | int c; |
---|
2363 | n/a | |
---|
2364 | n/a | c = mpd_cmp_total_mag(a, b); |
---|
2365 | n/a | _settriple(result, (c < 0), (c != 0), 0); |
---|
2366 | n/a | return c; |
---|
2367 | n/a | } |
---|
2368 | n/a | |
---|
2369 | n/a | /* Determine an ordering for operands that are numerically equal. */ |
---|
2370 | n/a | static inline int |
---|
2371 | n/a | _mpd_cmp_numequal(const mpd_t *a, const mpd_t *b) |
---|
2372 | n/a | { |
---|
2373 | n/a | int sign_a, sign_b; |
---|
2374 | n/a | int c; |
---|
2375 | n/a | |
---|
2376 | n/a | sign_a = mpd_sign(a); |
---|
2377 | n/a | sign_b = mpd_sign(b); |
---|
2378 | n/a | if (sign_a != sign_b) { |
---|
2379 | n/a | c = sign_b - sign_a; |
---|
2380 | n/a | } |
---|
2381 | n/a | else { |
---|
2382 | n/a | c = (a->exp < b->exp) ? -1 : 1; |
---|
2383 | n/a | c *= mpd_arith_sign(a); |
---|
2384 | n/a | } |
---|
2385 | n/a | |
---|
2386 | n/a | return c; |
---|
2387 | n/a | } |
---|
2388 | n/a | |
---|
2389 | n/a | |
---|
2390 | n/a | /******************************************************************************/ |
---|
2391 | n/a | /* Shifting the coefficient */ |
---|
2392 | n/a | /******************************************************************************/ |
---|
2393 | n/a | |
---|
2394 | n/a | /* |
---|
2395 | n/a | * Shift the coefficient of the operand to the left, no check for specials. |
---|
2396 | n/a | * Both operands may be the same pointer. If the result length has to be |
---|
2397 | n/a | * increased, mpd_qresize() might fail with MPD_Malloc_error. |
---|
2398 | n/a | */ |
---|
2399 | n/a | int |
---|
2400 | n/a | mpd_qshiftl(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status) |
---|
2401 | n/a | { |
---|
2402 | n/a | mpd_ssize_t size; |
---|
2403 | n/a | |
---|
2404 | n/a | assert(!mpd_isspecial(a)); |
---|
2405 | n/a | assert(n >= 0); |
---|
2406 | n/a | |
---|
2407 | n/a | if (mpd_iszerocoeff(a) || n == 0) { |
---|
2408 | n/a | return mpd_qcopy(result, a, status); |
---|
2409 | n/a | } |
---|
2410 | n/a | |
---|
2411 | n/a | size = mpd_digits_to_size(a->digits+n); |
---|
2412 | n/a | if (!mpd_qresize(result, size, status)) { |
---|
2413 | n/a | return 0; /* result is NaN */ |
---|
2414 | n/a | } |
---|
2415 | n/a | |
---|
2416 | n/a | _mpd_baseshiftl(result->data, a->data, size, a->len, n); |
---|
2417 | n/a | |
---|
2418 | n/a | mpd_copy_flags(result, a); |
---|
2419 | n/a | result->exp = a->exp; |
---|
2420 | n/a | result->digits = a->digits+n; |
---|
2421 | n/a | result->len = size; |
---|
2422 | n/a | |
---|
2423 | n/a | return 1; |
---|
2424 | n/a | } |
---|
2425 | n/a | |
---|
2426 | n/a | /* Determine the rounding indicator if all digits of the coefficient are shifted |
---|
2427 | n/a | * out of the picture. */ |
---|
2428 | n/a | static mpd_uint_t |
---|
2429 | n/a | _mpd_get_rnd(const mpd_uint_t *data, mpd_ssize_t len, int use_msd) |
---|
2430 | n/a | { |
---|
2431 | n/a | mpd_uint_t rnd = 0, rest = 0, word; |
---|
2432 | n/a | |
---|
2433 | n/a | word = data[len-1]; |
---|
2434 | n/a | /* special treatment for the most significant digit if shift == digits */ |
---|
2435 | n/a | if (use_msd) { |
---|
2436 | n/a | _mpd_divmod_pow10(&rnd, &rest, word, mpd_word_digits(word)-1); |
---|
2437 | n/a | if (len > 1 && rest == 0) { |
---|
2438 | n/a | rest = !_mpd_isallzero(data, len-1); |
---|
2439 | n/a | } |
---|
2440 | n/a | } |
---|
2441 | n/a | else { |
---|
2442 | n/a | rest = !_mpd_isallzero(data, len); |
---|
2443 | n/a | } |
---|
2444 | n/a | |
---|
2445 | n/a | return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd; |
---|
2446 | n/a | } |
---|
2447 | n/a | |
---|
2448 | n/a | /* |
---|
2449 | n/a | * Same as mpd_qshiftr(), but 'result' is an mpd_t with a static coefficient. |
---|
2450 | n/a | * It is the caller's responsibility to ensure that the coefficient is big |
---|
2451 | n/a | * enough. The function cannot fail. |
---|
2452 | n/a | */ |
---|
2453 | n/a | static mpd_uint_t |
---|
2454 | n/a | mpd_qsshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n) |
---|
2455 | n/a | { |
---|
2456 | n/a | mpd_uint_t rnd; |
---|
2457 | n/a | mpd_ssize_t size; |
---|
2458 | n/a | |
---|
2459 | n/a | assert(!mpd_isspecial(a)); |
---|
2460 | n/a | assert(n >= 0); |
---|
2461 | n/a | |
---|
2462 | n/a | if (mpd_iszerocoeff(a) || n == 0) { |
---|
2463 | n/a | mpd_qcopy_static(result, a); |
---|
2464 | n/a | return 0; |
---|
2465 | n/a | } |
---|
2466 | n/a | |
---|
2467 | n/a | if (n >= a->digits) { |
---|
2468 | n/a | rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits)); |
---|
2469 | n/a | mpd_zerocoeff(result); |
---|
2470 | n/a | } |
---|
2471 | n/a | else { |
---|
2472 | n/a | result->digits = a->digits-n; |
---|
2473 | n/a | size = mpd_digits_to_size(result->digits); |
---|
2474 | n/a | rnd = _mpd_baseshiftr(result->data, a->data, a->len, n); |
---|
2475 | n/a | result->len = size; |
---|
2476 | n/a | } |
---|
2477 | n/a | |
---|
2478 | n/a | mpd_copy_flags(result, a); |
---|
2479 | n/a | result->exp = a->exp; |
---|
2480 | n/a | |
---|
2481 | n/a | return rnd; |
---|
2482 | n/a | } |
---|
2483 | n/a | |
---|
2484 | n/a | /* |
---|
2485 | n/a | * Inplace shift of the coefficient to the right, no check for specials. |
---|
2486 | n/a | * Returns the rounding indicator for mpd_rnd_incr(). |
---|
2487 | n/a | * The function cannot fail. |
---|
2488 | n/a | */ |
---|
2489 | n/a | mpd_uint_t |
---|
2490 | n/a | mpd_qshiftr_inplace(mpd_t *result, mpd_ssize_t n) |
---|
2491 | n/a | { |
---|
2492 | n/a | uint32_t dummy; |
---|
2493 | n/a | mpd_uint_t rnd; |
---|
2494 | n/a | mpd_ssize_t size; |
---|
2495 | n/a | |
---|
2496 | n/a | assert(!mpd_isspecial(result)); |
---|
2497 | n/a | assert(n >= 0); |
---|
2498 | n/a | |
---|
2499 | n/a | if (mpd_iszerocoeff(result) || n == 0) { |
---|
2500 | n/a | return 0; |
---|
2501 | n/a | } |
---|
2502 | n/a | |
---|
2503 | n/a | if (n >= result->digits) { |
---|
2504 | n/a | rnd = _mpd_get_rnd(result->data, result->len, (n==result->digits)); |
---|
2505 | n/a | mpd_zerocoeff(result); |
---|
2506 | n/a | } |
---|
2507 | n/a | else { |
---|
2508 | n/a | rnd = _mpd_baseshiftr(result->data, result->data, result->len, n); |
---|
2509 | n/a | result->digits -= n; |
---|
2510 | n/a | size = mpd_digits_to_size(result->digits); |
---|
2511 | n/a | /* reducing the size cannot fail */ |
---|
2512 | n/a | mpd_qresize(result, size, &dummy); |
---|
2513 | n/a | result->len = size; |
---|
2514 | n/a | } |
---|
2515 | n/a | |
---|
2516 | n/a | return rnd; |
---|
2517 | n/a | } |
---|
2518 | n/a | |
---|
2519 | n/a | /* |
---|
2520 | n/a | * Shift the coefficient of the operand to the right, no check for specials. |
---|
2521 | n/a | * Both operands may be the same pointer. Returns the rounding indicator to |
---|
2522 | n/a | * be used by mpd_rnd_incr(). If the result length has to be increased, |
---|
2523 | n/a | * mpd_qcopy() or mpd_qresize() might fail with MPD_Malloc_error. In those |
---|
2524 | n/a | * cases, MPD_UINT_MAX is returned. |
---|
2525 | n/a | */ |
---|
2526 | n/a | mpd_uint_t |
---|
2527 | n/a | mpd_qshiftr(mpd_t *result, const mpd_t *a, mpd_ssize_t n, uint32_t *status) |
---|
2528 | n/a | { |
---|
2529 | n/a | mpd_uint_t rnd; |
---|
2530 | n/a | mpd_ssize_t size; |
---|
2531 | n/a | |
---|
2532 | n/a | assert(!mpd_isspecial(a)); |
---|
2533 | n/a | assert(n >= 0); |
---|
2534 | n/a | |
---|
2535 | n/a | if (mpd_iszerocoeff(a) || n == 0) { |
---|
2536 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
2537 | n/a | return MPD_UINT_MAX; |
---|
2538 | n/a | } |
---|
2539 | n/a | return 0; |
---|
2540 | n/a | } |
---|
2541 | n/a | |
---|
2542 | n/a | if (n >= a->digits) { |
---|
2543 | n/a | rnd = _mpd_get_rnd(a->data, a->len, (n==a->digits)); |
---|
2544 | n/a | mpd_zerocoeff(result); |
---|
2545 | n/a | } |
---|
2546 | n/a | else { |
---|
2547 | n/a | result->digits = a->digits-n; |
---|
2548 | n/a | size = mpd_digits_to_size(result->digits); |
---|
2549 | n/a | if (result == a) { |
---|
2550 | n/a | rnd = _mpd_baseshiftr(result->data, a->data, a->len, n); |
---|
2551 | n/a | /* reducing the size cannot fail */ |
---|
2552 | n/a | mpd_qresize(result, size, status); |
---|
2553 | n/a | } |
---|
2554 | n/a | else { |
---|
2555 | n/a | if (!mpd_qresize(result, size, status)) { |
---|
2556 | n/a | return MPD_UINT_MAX; |
---|
2557 | n/a | } |
---|
2558 | n/a | rnd = _mpd_baseshiftr(result->data, a->data, a->len, n); |
---|
2559 | n/a | } |
---|
2560 | n/a | result->len = size; |
---|
2561 | n/a | } |
---|
2562 | n/a | |
---|
2563 | n/a | mpd_copy_flags(result, a); |
---|
2564 | n/a | result->exp = a->exp; |
---|
2565 | n/a | |
---|
2566 | n/a | return rnd; |
---|
2567 | n/a | } |
---|
2568 | n/a | |
---|
2569 | n/a | |
---|
2570 | n/a | /******************************************************************************/ |
---|
2571 | n/a | /* Miscellaneous operations */ |
---|
2572 | n/a | /******************************************************************************/ |
---|
2573 | n/a | |
---|
2574 | n/a | /* Logical And */ |
---|
2575 | n/a | void |
---|
2576 | n/a | mpd_qand(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2577 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2578 | n/a | { |
---|
2579 | n/a | const mpd_t *big = a, *small = b; |
---|
2580 | n/a | mpd_uint_t x, y, z, xbit, ybit; |
---|
2581 | n/a | int k, mswdigits; |
---|
2582 | n/a | mpd_ssize_t i; |
---|
2583 | n/a | |
---|
2584 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b) || |
---|
2585 | n/a | mpd_isnegative(a) || mpd_isnegative(b) || |
---|
2586 | n/a | a->exp != 0 || b->exp != 0) { |
---|
2587 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2588 | n/a | return; |
---|
2589 | n/a | } |
---|
2590 | n/a | if (b->digits > a->digits) { |
---|
2591 | n/a | big = b; |
---|
2592 | n/a | small = a; |
---|
2593 | n/a | } |
---|
2594 | n/a | if (!mpd_qresize(result, big->len, status)) { |
---|
2595 | n/a | return; |
---|
2596 | n/a | } |
---|
2597 | n/a | |
---|
2598 | n/a | |
---|
2599 | n/a | /* full words */ |
---|
2600 | n/a | for (i = 0; i < small->len-1; i++) { |
---|
2601 | n/a | x = small->data[i]; |
---|
2602 | n/a | y = big->data[i]; |
---|
2603 | n/a | z = 0; |
---|
2604 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
2605 | n/a | xbit = x % 10; |
---|
2606 | n/a | x /= 10; |
---|
2607 | n/a | ybit = y % 10; |
---|
2608 | n/a | y /= 10; |
---|
2609 | n/a | if (xbit > 1 || ybit > 1) { |
---|
2610 | n/a | goto invalid_operation; |
---|
2611 | n/a | } |
---|
2612 | n/a | z += (xbit&ybit) ? mpd_pow10[k] : 0; |
---|
2613 | n/a | } |
---|
2614 | n/a | result->data[i] = z; |
---|
2615 | n/a | } |
---|
2616 | n/a | /* most significant word of small */ |
---|
2617 | n/a | x = small->data[i]; |
---|
2618 | n/a | y = big->data[i]; |
---|
2619 | n/a | z = 0; |
---|
2620 | n/a | mswdigits = mpd_word_digits(x); |
---|
2621 | n/a | for (k = 0; k < mswdigits; k++) { |
---|
2622 | n/a | xbit = x % 10; |
---|
2623 | n/a | x /= 10; |
---|
2624 | n/a | ybit = y % 10; |
---|
2625 | n/a | y /= 10; |
---|
2626 | n/a | if (xbit > 1 || ybit > 1) { |
---|
2627 | n/a | goto invalid_operation; |
---|
2628 | n/a | } |
---|
2629 | n/a | z += (xbit&ybit) ? mpd_pow10[k] : 0; |
---|
2630 | n/a | } |
---|
2631 | n/a | result->data[i++] = z; |
---|
2632 | n/a | |
---|
2633 | n/a | /* scan the rest of y for digits > 1 */ |
---|
2634 | n/a | for (; k < MPD_RDIGITS; k++) { |
---|
2635 | n/a | ybit = y % 10; |
---|
2636 | n/a | y /= 10; |
---|
2637 | n/a | if (ybit > 1) { |
---|
2638 | n/a | goto invalid_operation; |
---|
2639 | n/a | } |
---|
2640 | n/a | } |
---|
2641 | n/a | /* scan the rest of big for digits > 1 */ |
---|
2642 | n/a | for (; i < big->len; i++) { |
---|
2643 | n/a | y = big->data[i]; |
---|
2644 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
2645 | n/a | ybit = y % 10; |
---|
2646 | n/a | y /= 10; |
---|
2647 | n/a | if (ybit > 1) { |
---|
2648 | n/a | goto invalid_operation; |
---|
2649 | n/a | } |
---|
2650 | n/a | } |
---|
2651 | n/a | } |
---|
2652 | n/a | |
---|
2653 | n/a | mpd_clear_flags(result); |
---|
2654 | n/a | result->exp = 0; |
---|
2655 | n/a | result->len = _mpd_real_size(result->data, small->len); |
---|
2656 | n/a | mpd_qresize(result, result->len, status); |
---|
2657 | n/a | mpd_setdigits(result); |
---|
2658 | n/a | _mpd_cap(result, ctx); |
---|
2659 | n/a | return; |
---|
2660 | n/a | |
---|
2661 | n/a | invalid_operation: |
---|
2662 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2663 | n/a | } |
---|
2664 | n/a | |
---|
2665 | n/a | /* Class of an operand. Returns a pointer to the constant name. */ |
---|
2666 | n/a | const char * |
---|
2667 | n/a | mpd_class(const mpd_t *a, const mpd_context_t *ctx) |
---|
2668 | n/a | { |
---|
2669 | n/a | if (mpd_isnan(a)) { |
---|
2670 | n/a | if (mpd_isqnan(a)) |
---|
2671 | n/a | return "NaN"; |
---|
2672 | n/a | else |
---|
2673 | n/a | return "sNaN"; |
---|
2674 | n/a | } |
---|
2675 | n/a | else if (mpd_ispositive(a)) { |
---|
2676 | n/a | if (mpd_isinfinite(a)) |
---|
2677 | n/a | return "+Infinity"; |
---|
2678 | n/a | else if (mpd_iszero(a)) |
---|
2679 | n/a | return "+Zero"; |
---|
2680 | n/a | else if (mpd_isnormal(a, ctx)) |
---|
2681 | n/a | return "+Normal"; |
---|
2682 | n/a | else |
---|
2683 | n/a | return "+Subnormal"; |
---|
2684 | n/a | } |
---|
2685 | n/a | else { |
---|
2686 | n/a | if (mpd_isinfinite(a)) |
---|
2687 | n/a | return "-Infinity"; |
---|
2688 | n/a | else if (mpd_iszero(a)) |
---|
2689 | n/a | return "-Zero"; |
---|
2690 | n/a | else if (mpd_isnormal(a, ctx)) |
---|
2691 | n/a | return "-Normal"; |
---|
2692 | n/a | else |
---|
2693 | n/a | return "-Subnormal"; |
---|
2694 | n/a | } |
---|
2695 | n/a | } |
---|
2696 | n/a | |
---|
2697 | n/a | /* Logical Xor */ |
---|
2698 | n/a | void |
---|
2699 | n/a | mpd_qinvert(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
2700 | n/a | uint32_t *status) |
---|
2701 | n/a | { |
---|
2702 | n/a | mpd_uint_t x, z, xbit; |
---|
2703 | n/a | mpd_ssize_t i, digits, len; |
---|
2704 | n/a | mpd_ssize_t q, r; |
---|
2705 | n/a | int k; |
---|
2706 | n/a | |
---|
2707 | n/a | if (mpd_isspecial(a) || mpd_isnegative(a) || a->exp != 0) { |
---|
2708 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2709 | n/a | return; |
---|
2710 | n/a | } |
---|
2711 | n/a | |
---|
2712 | n/a | digits = (a->digits < ctx->prec) ? ctx->prec : a->digits; |
---|
2713 | n/a | _mpd_idiv_word(&q, &r, digits, MPD_RDIGITS); |
---|
2714 | n/a | len = (r == 0) ? q : q+1; |
---|
2715 | n/a | if (!mpd_qresize(result, len, status)) { |
---|
2716 | n/a | return; |
---|
2717 | n/a | } |
---|
2718 | n/a | |
---|
2719 | n/a | for (i = 0; i < len; i++) { |
---|
2720 | n/a | x = (i < a->len) ? a->data[i] : 0; |
---|
2721 | n/a | z = 0; |
---|
2722 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
2723 | n/a | xbit = x % 10; |
---|
2724 | n/a | x /= 10; |
---|
2725 | n/a | if (xbit > 1) { |
---|
2726 | n/a | goto invalid_operation; |
---|
2727 | n/a | } |
---|
2728 | n/a | z += !xbit ? mpd_pow10[k] : 0; |
---|
2729 | n/a | } |
---|
2730 | n/a | result->data[i] = z; |
---|
2731 | n/a | } |
---|
2732 | n/a | |
---|
2733 | n/a | mpd_clear_flags(result); |
---|
2734 | n/a | result->exp = 0; |
---|
2735 | n/a | result->len = _mpd_real_size(result->data, len); |
---|
2736 | n/a | mpd_qresize(result, result->len, status); |
---|
2737 | n/a | mpd_setdigits(result); |
---|
2738 | n/a | _mpd_cap(result, ctx); |
---|
2739 | n/a | return; |
---|
2740 | n/a | |
---|
2741 | n/a | invalid_operation: |
---|
2742 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2743 | n/a | } |
---|
2744 | n/a | |
---|
2745 | n/a | /* Exponent of the magnitude of the most significant digit of the operand. */ |
---|
2746 | n/a | void |
---|
2747 | n/a | mpd_qlogb(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
2748 | n/a | uint32_t *status) |
---|
2749 | n/a | { |
---|
2750 | n/a | if (mpd_isspecial(a)) { |
---|
2751 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
2752 | n/a | return; |
---|
2753 | n/a | } |
---|
2754 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
2755 | n/a | } |
---|
2756 | n/a | else if (mpd_iszerocoeff(a)) { |
---|
2757 | n/a | mpd_setspecial(result, MPD_NEG, MPD_INF); |
---|
2758 | n/a | *status |= MPD_Division_by_zero; |
---|
2759 | n/a | } |
---|
2760 | n/a | else { |
---|
2761 | n/a | mpd_qset_ssize(result, mpd_adjexp(a), ctx, status); |
---|
2762 | n/a | } |
---|
2763 | n/a | } |
---|
2764 | n/a | |
---|
2765 | n/a | /* Logical Or */ |
---|
2766 | n/a | void |
---|
2767 | n/a | mpd_qor(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2768 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2769 | n/a | { |
---|
2770 | n/a | const mpd_t *big = a, *small = b; |
---|
2771 | n/a | mpd_uint_t x, y, z, xbit, ybit; |
---|
2772 | n/a | int k, mswdigits; |
---|
2773 | n/a | mpd_ssize_t i; |
---|
2774 | n/a | |
---|
2775 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b) || |
---|
2776 | n/a | mpd_isnegative(a) || mpd_isnegative(b) || |
---|
2777 | n/a | a->exp != 0 || b->exp != 0) { |
---|
2778 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2779 | n/a | return; |
---|
2780 | n/a | } |
---|
2781 | n/a | if (b->digits > a->digits) { |
---|
2782 | n/a | big = b; |
---|
2783 | n/a | small = a; |
---|
2784 | n/a | } |
---|
2785 | n/a | if (!mpd_qresize(result, big->len, status)) { |
---|
2786 | n/a | return; |
---|
2787 | n/a | } |
---|
2788 | n/a | |
---|
2789 | n/a | |
---|
2790 | n/a | /* full words */ |
---|
2791 | n/a | for (i = 0; i < small->len-1; i++) { |
---|
2792 | n/a | x = small->data[i]; |
---|
2793 | n/a | y = big->data[i]; |
---|
2794 | n/a | z = 0; |
---|
2795 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
2796 | n/a | xbit = x % 10; |
---|
2797 | n/a | x /= 10; |
---|
2798 | n/a | ybit = y % 10; |
---|
2799 | n/a | y /= 10; |
---|
2800 | n/a | if (xbit > 1 || ybit > 1) { |
---|
2801 | n/a | goto invalid_operation; |
---|
2802 | n/a | } |
---|
2803 | n/a | z += (xbit|ybit) ? mpd_pow10[k] : 0; |
---|
2804 | n/a | } |
---|
2805 | n/a | result->data[i] = z; |
---|
2806 | n/a | } |
---|
2807 | n/a | /* most significant word of small */ |
---|
2808 | n/a | x = small->data[i]; |
---|
2809 | n/a | y = big->data[i]; |
---|
2810 | n/a | z = 0; |
---|
2811 | n/a | mswdigits = mpd_word_digits(x); |
---|
2812 | n/a | for (k = 0; k < mswdigits; k++) { |
---|
2813 | n/a | xbit = x % 10; |
---|
2814 | n/a | x /= 10; |
---|
2815 | n/a | ybit = y % 10; |
---|
2816 | n/a | y /= 10; |
---|
2817 | n/a | if (xbit > 1 || ybit > 1) { |
---|
2818 | n/a | goto invalid_operation; |
---|
2819 | n/a | } |
---|
2820 | n/a | z += (xbit|ybit) ? mpd_pow10[k] : 0; |
---|
2821 | n/a | } |
---|
2822 | n/a | |
---|
2823 | n/a | /* scan for digits > 1 and copy the rest of y */ |
---|
2824 | n/a | for (; k < MPD_RDIGITS; k++) { |
---|
2825 | n/a | ybit = y % 10; |
---|
2826 | n/a | y /= 10; |
---|
2827 | n/a | if (ybit > 1) { |
---|
2828 | n/a | goto invalid_operation; |
---|
2829 | n/a | } |
---|
2830 | n/a | z += ybit*mpd_pow10[k]; |
---|
2831 | n/a | } |
---|
2832 | n/a | result->data[i++] = z; |
---|
2833 | n/a | /* scan for digits > 1 and copy the rest of big */ |
---|
2834 | n/a | for (; i < big->len; i++) { |
---|
2835 | n/a | y = big->data[i]; |
---|
2836 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
2837 | n/a | ybit = y % 10; |
---|
2838 | n/a | y /= 10; |
---|
2839 | n/a | if (ybit > 1) { |
---|
2840 | n/a | goto invalid_operation; |
---|
2841 | n/a | } |
---|
2842 | n/a | } |
---|
2843 | n/a | result->data[i] = big->data[i]; |
---|
2844 | n/a | } |
---|
2845 | n/a | |
---|
2846 | n/a | mpd_clear_flags(result); |
---|
2847 | n/a | result->exp = 0; |
---|
2848 | n/a | result->len = _mpd_real_size(result->data, big->len); |
---|
2849 | n/a | mpd_qresize(result, result->len, status); |
---|
2850 | n/a | mpd_setdigits(result); |
---|
2851 | n/a | _mpd_cap(result, ctx); |
---|
2852 | n/a | return; |
---|
2853 | n/a | |
---|
2854 | n/a | invalid_operation: |
---|
2855 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2856 | n/a | } |
---|
2857 | n/a | |
---|
2858 | n/a | /* |
---|
2859 | n/a | * Rotate the coefficient of 'a' by 'b' digits. 'b' must be an integer with |
---|
2860 | n/a | * exponent 0. |
---|
2861 | n/a | */ |
---|
2862 | n/a | void |
---|
2863 | n/a | mpd_qrotate(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2864 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2865 | n/a | { |
---|
2866 | n/a | uint32_t workstatus = 0; |
---|
2867 | n/a | MPD_NEW_STATIC(tmp,0,0,0,0); |
---|
2868 | n/a | MPD_NEW_STATIC(big,0,0,0,0); |
---|
2869 | n/a | MPD_NEW_STATIC(small,0,0,0,0); |
---|
2870 | n/a | mpd_ssize_t n, lshift, rshift; |
---|
2871 | n/a | |
---|
2872 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
2873 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
2874 | n/a | return; |
---|
2875 | n/a | } |
---|
2876 | n/a | } |
---|
2877 | n/a | if (b->exp != 0 || mpd_isinfinite(b)) { |
---|
2878 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2879 | n/a | return; |
---|
2880 | n/a | } |
---|
2881 | n/a | |
---|
2882 | n/a | n = mpd_qget_ssize(b, &workstatus); |
---|
2883 | n/a | if (workstatus&MPD_Invalid_operation) { |
---|
2884 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2885 | n/a | return; |
---|
2886 | n/a | } |
---|
2887 | n/a | if (n > ctx->prec || n < -ctx->prec) { |
---|
2888 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2889 | n/a | return; |
---|
2890 | n/a | } |
---|
2891 | n/a | if (mpd_isinfinite(a)) { |
---|
2892 | n/a | mpd_qcopy(result, a, status); |
---|
2893 | n/a | return; |
---|
2894 | n/a | } |
---|
2895 | n/a | |
---|
2896 | n/a | if (n >= 0) { |
---|
2897 | n/a | lshift = n; |
---|
2898 | n/a | rshift = ctx->prec-n; |
---|
2899 | n/a | } |
---|
2900 | n/a | else { |
---|
2901 | n/a | lshift = ctx->prec+n; |
---|
2902 | n/a | rshift = -n; |
---|
2903 | n/a | } |
---|
2904 | n/a | |
---|
2905 | n/a | if (a->digits > ctx->prec) { |
---|
2906 | n/a | if (!mpd_qcopy(&tmp, a, status)) { |
---|
2907 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
2908 | n/a | goto finish; |
---|
2909 | n/a | } |
---|
2910 | n/a | _mpd_cap(&tmp, ctx); |
---|
2911 | n/a | a = &tmp; |
---|
2912 | n/a | } |
---|
2913 | n/a | |
---|
2914 | n/a | if (!mpd_qshiftl(&big, a, lshift, status)) { |
---|
2915 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
2916 | n/a | goto finish; |
---|
2917 | n/a | } |
---|
2918 | n/a | _mpd_cap(&big, ctx); |
---|
2919 | n/a | |
---|
2920 | n/a | if (mpd_qshiftr(&small, a, rshift, status) == MPD_UINT_MAX) { |
---|
2921 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
2922 | n/a | goto finish; |
---|
2923 | n/a | } |
---|
2924 | n/a | _mpd_qadd(result, &big, &small, ctx, status); |
---|
2925 | n/a | |
---|
2926 | n/a | |
---|
2927 | n/a | finish: |
---|
2928 | n/a | mpd_del(&tmp); |
---|
2929 | n/a | mpd_del(&big); |
---|
2930 | n/a | mpd_del(&small); |
---|
2931 | n/a | } |
---|
2932 | n/a | |
---|
2933 | n/a | /* |
---|
2934 | n/a | * b must be an integer with exponent 0 and in the range +-2*(emax + prec). |
---|
2935 | n/a | * XXX: In my opinion +-(2*emax + prec) would be more sensible. |
---|
2936 | n/a | * The result is a with the value of b added to its exponent. |
---|
2937 | n/a | */ |
---|
2938 | n/a | void |
---|
2939 | n/a | mpd_qscaleb(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
2940 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
2941 | n/a | { |
---|
2942 | n/a | uint32_t workstatus = 0; |
---|
2943 | n/a | mpd_uint_t n, maxjump; |
---|
2944 | n/a | #ifndef LEGACY_COMPILER |
---|
2945 | n/a | int64_t exp; |
---|
2946 | n/a | #else |
---|
2947 | n/a | mpd_uint_t x; |
---|
2948 | n/a | int x_sign, n_sign; |
---|
2949 | n/a | mpd_ssize_t exp; |
---|
2950 | n/a | #endif |
---|
2951 | n/a | |
---|
2952 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
2953 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
2954 | n/a | return; |
---|
2955 | n/a | } |
---|
2956 | n/a | } |
---|
2957 | n/a | if (b->exp != 0 || mpd_isinfinite(b)) { |
---|
2958 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2959 | n/a | return; |
---|
2960 | n/a | } |
---|
2961 | n/a | |
---|
2962 | n/a | n = mpd_qabs_uint(b, &workstatus); |
---|
2963 | n/a | /* the spec demands this */ |
---|
2964 | n/a | maxjump = 2 * (mpd_uint_t)(ctx->emax + ctx->prec); |
---|
2965 | n/a | |
---|
2966 | n/a | if (n > maxjump || workstatus&MPD_Invalid_operation) { |
---|
2967 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
2968 | n/a | return; |
---|
2969 | n/a | } |
---|
2970 | n/a | if (mpd_isinfinite(a)) { |
---|
2971 | n/a | mpd_qcopy(result, a, status); |
---|
2972 | n/a | return; |
---|
2973 | n/a | } |
---|
2974 | n/a | |
---|
2975 | n/a | #ifndef LEGACY_COMPILER |
---|
2976 | n/a | exp = a->exp + (int64_t)n * mpd_arith_sign(b); |
---|
2977 | n/a | exp = (exp > MPD_EXP_INF) ? MPD_EXP_INF : exp; |
---|
2978 | n/a | exp = (exp < MPD_EXP_CLAMP) ? MPD_EXP_CLAMP : exp; |
---|
2979 | n/a | #else |
---|
2980 | n/a | x = (a->exp < 0) ? -a->exp : a->exp; |
---|
2981 | n/a | x_sign = (a->exp < 0) ? 1 : 0; |
---|
2982 | n/a | n_sign = mpd_isnegative(b) ? 1 : 0; |
---|
2983 | n/a | |
---|
2984 | n/a | if (x_sign == n_sign) { |
---|
2985 | n/a | x = x + n; |
---|
2986 | n/a | if (x < n) x = MPD_UINT_MAX; |
---|
2987 | n/a | } |
---|
2988 | n/a | else { |
---|
2989 | n/a | x_sign = (x >= n) ? x_sign : n_sign; |
---|
2990 | n/a | x = (x >= n) ? x - n : n - x; |
---|
2991 | n/a | } |
---|
2992 | n/a | if (!x_sign && x > MPD_EXP_INF) x = MPD_EXP_INF; |
---|
2993 | n/a | if (x_sign && x > -MPD_EXP_CLAMP) x = -MPD_EXP_CLAMP; |
---|
2994 | n/a | exp = x_sign ? -((mpd_ssize_t)x) : (mpd_ssize_t)x; |
---|
2995 | n/a | #endif |
---|
2996 | n/a | |
---|
2997 | n/a | mpd_qcopy(result, a, status); |
---|
2998 | n/a | result->exp = (mpd_ssize_t)exp; |
---|
2999 | n/a | |
---|
3000 | n/a | mpd_qfinalize(result, ctx, status); |
---|
3001 | n/a | } |
---|
3002 | n/a | |
---|
3003 | n/a | /* |
---|
3004 | n/a | * Shift the coefficient by n digits, positive n is a left shift. In the case |
---|
3005 | n/a | * of a left shift, the result is decapitated to fit the context precision. If |
---|
3006 | n/a | * you don't want that, use mpd_shiftl(). |
---|
3007 | n/a | */ |
---|
3008 | n/a | void |
---|
3009 | n/a | mpd_qshiftn(mpd_t *result, const mpd_t *a, mpd_ssize_t n, const mpd_context_t *ctx, |
---|
3010 | n/a | uint32_t *status) |
---|
3011 | n/a | { |
---|
3012 | n/a | if (mpd_isspecial(a)) { |
---|
3013 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
3014 | n/a | return; |
---|
3015 | n/a | } |
---|
3016 | n/a | mpd_qcopy(result, a, status); |
---|
3017 | n/a | return; |
---|
3018 | n/a | } |
---|
3019 | n/a | |
---|
3020 | n/a | if (n >= 0 && n <= ctx->prec) { |
---|
3021 | n/a | mpd_qshiftl(result, a, n, status); |
---|
3022 | n/a | _mpd_cap(result, ctx); |
---|
3023 | n/a | } |
---|
3024 | n/a | else if (n < 0 && n >= -ctx->prec) { |
---|
3025 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
3026 | n/a | return; |
---|
3027 | n/a | } |
---|
3028 | n/a | _mpd_cap(result, ctx); |
---|
3029 | n/a | mpd_qshiftr_inplace(result, -n); |
---|
3030 | n/a | } |
---|
3031 | n/a | else { |
---|
3032 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3033 | n/a | } |
---|
3034 | n/a | } |
---|
3035 | n/a | |
---|
3036 | n/a | /* |
---|
3037 | n/a | * Same as mpd_shiftn(), but the shift is specified by the decimal b, which |
---|
3038 | n/a | * must be an integer with a zero exponent. Infinities remain infinities. |
---|
3039 | n/a | */ |
---|
3040 | n/a | void |
---|
3041 | n/a | mpd_qshift(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, |
---|
3042 | n/a | uint32_t *status) |
---|
3043 | n/a | { |
---|
3044 | n/a | uint32_t workstatus = 0; |
---|
3045 | n/a | mpd_ssize_t n; |
---|
3046 | n/a | |
---|
3047 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3048 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
3049 | n/a | return; |
---|
3050 | n/a | } |
---|
3051 | n/a | } |
---|
3052 | n/a | if (b->exp != 0 || mpd_isinfinite(b)) { |
---|
3053 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3054 | n/a | return; |
---|
3055 | n/a | } |
---|
3056 | n/a | |
---|
3057 | n/a | n = mpd_qget_ssize(b, &workstatus); |
---|
3058 | n/a | if (workstatus&MPD_Invalid_operation) { |
---|
3059 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3060 | n/a | return; |
---|
3061 | n/a | } |
---|
3062 | n/a | if (n > ctx->prec || n < -ctx->prec) { |
---|
3063 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3064 | n/a | return; |
---|
3065 | n/a | } |
---|
3066 | n/a | if (mpd_isinfinite(a)) { |
---|
3067 | n/a | mpd_qcopy(result, a, status); |
---|
3068 | n/a | return; |
---|
3069 | n/a | } |
---|
3070 | n/a | |
---|
3071 | n/a | if (n >= 0) { |
---|
3072 | n/a | mpd_qshiftl(result, a, n, status); |
---|
3073 | n/a | _mpd_cap(result, ctx); |
---|
3074 | n/a | } |
---|
3075 | n/a | else { |
---|
3076 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
3077 | n/a | return; |
---|
3078 | n/a | } |
---|
3079 | n/a | _mpd_cap(result, ctx); |
---|
3080 | n/a | mpd_qshiftr_inplace(result, -n); |
---|
3081 | n/a | } |
---|
3082 | n/a | } |
---|
3083 | n/a | |
---|
3084 | n/a | /* Logical Xor */ |
---|
3085 | n/a | void |
---|
3086 | n/a | mpd_qxor(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3087 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3088 | n/a | { |
---|
3089 | n/a | const mpd_t *big = a, *small = b; |
---|
3090 | n/a | mpd_uint_t x, y, z, xbit, ybit; |
---|
3091 | n/a | int k, mswdigits; |
---|
3092 | n/a | mpd_ssize_t i; |
---|
3093 | n/a | |
---|
3094 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b) || |
---|
3095 | n/a | mpd_isnegative(a) || mpd_isnegative(b) || |
---|
3096 | n/a | a->exp != 0 || b->exp != 0) { |
---|
3097 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3098 | n/a | return; |
---|
3099 | n/a | } |
---|
3100 | n/a | if (b->digits > a->digits) { |
---|
3101 | n/a | big = b; |
---|
3102 | n/a | small = a; |
---|
3103 | n/a | } |
---|
3104 | n/a | if (!mpd_qresize(result, big->len, status)) { |
---|
3105 | n/a | return; |
---|
3106 | n/a | } |
---|
3107 | n/a | |
---|
3108 | n/a | |
---|
3109 | n/a | /* full words */ |
---|
3110 | n/a | for (i = 0; i < small->len-1; i++) { |
---|
3111 | n/a | x = small->data[i]; |
---|
3112 | n/a | y = big->data[i]; |
---|
3113 | n/a | z = 0; |
---|
3114 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
3115 | n/a | xbit = x % 10; |
---|
3116 | n/a | x /= 10; |
---|
3117 | n/a | ybit = y % 10; |
---|
3118 | n/a | y /= 10; |
---|
3119 | n/a | if (xbit > 1 || ybit > 1) { |
---|
3120 | n/a | goto invalid_operation; |
---|
3121 | n/a | } |
---|
3122 | n/a | z += (xbit^ybit) ? mpd_pow10[k] : 0; |
---|
3123 | n/a | } |
---|
3124 | n/a | result->data[i] = z; |
---|
3125 | n/a | } |
---|
3126 | n/a | /* most significant word of small */ |
---|
3127 | n/a | x = small->data[i]; |
---|
3128 | n/a | y = big->data[i]; |
---|
3129 | n/a | z = 0; |
---|
3130 | n/a | mswdigits = mpd_word_digits(x); |
---|
3131 | n/a | for (k = 0; k < mswdigits; k++) { |
---|
3132 | n/a | xbit = x % 10; |
---|
3133 | n/a | x /= 10; |
---|
3134 | n/a | ybit = y % 10; |
---|
3135 | n/a | y /= 10; |
---|
3136 | n/a | if (xbit > 1 || ybit > 1) { |
---|
3137 | n/a | goto invalid_operation; |
---|
3138 | n/a | } |
---|
3139 | n/a | z += (xbit^ybit) ? mpd_pow10[k] : 0; |
---|
3140 | n/a | } |
---|
3141 | n/a | |
---|
3142 | n/a | /* scan for digits > 1 and copy the rest of y */ |
---|
3143 | n/a | for (; k < MPD_RDIGITS; k++) { |
---|
3144 | n/a | ybit = y % 10; |
---|
3145 | n/a | y /= 10; |
---|
3146 | n/a | if (ybit > 1) { |
---|
3147 | n/a | goto invalid_operation; |
---|
3148 | n/a | } |
---|
3149 | n/a | z += ybit*mpd_pow10[k]; |
---|
3150 | n/a | } |
---|
3151 | n/a | result->data[i++] = z; |
---|
3152 | n/a | /* scan for digits > 1 and copy the rest of big */ |
---|
3153 | n/a | for (; i < big->len; i++) { |
---|
3154 | n/a | y = big->data[i]; |
---|
3155 | n/a | for (k = 0; k < MPD_RDIGITS; k++) { |
---|
3156 | n/a | ybit = y % 10; |
---|
3157 | n/a | y /= 10; |
---|
3158 | n/a | if (ybit > 1) { |
---|
3159 | n/a | goto invalid_operation; |
---|
3160 | n/a | } |
---|
3161 | n/a | } |
---|
3162 | n/a | result->data[i] = big->data[i]; |
---|
3163 | n/a | } |
---|
3164 | n/a | |
---|
3165 | n/a | mpd_clear_flags(result); |
---|
3166 | n/a | result->exp = 0; |
---|
3167 | n/a | result->len = _mpd_real_size(result->data, big->len); |
---|
3168 | n/a | mpd_qresize(result, result->len, status); |
---|
3169 | n/a | mpd_setdigits(result); |
---|
3170 | n/a | _mpd_cap(result, ctx); |
---|
3171 | n/a | return; |
---|
3172 | n/a | |
---|
3173 | n/a | invalid_operation: |
---|
3174 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3175 | n/a | } |
---|
3176 | n/a | |
---|
3177 | n/a | |
---|
3178 | n/a | /******************************************************************************/ |
---|
3179 | n/a | /* Arithmetic operations */ |
---|
3180 | n/a | /******************************************************************************/ |
---|
3181 | n/a | |
---|
3182 | n/a | /* |
---|
3183 | n/a | * The absolute value of a. If a is negative, the result is the same |
---|
3184 | n/a | * as the result of the minus operation. Otherwise, the result is the |
---|
3185 | n/a | * result of the plus operation. |
---|
3186 | n/a | */ |
---|
3187 | n/a | void |
---|
3188 | n/a | mpd_qabs(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
3189 | n/a | uint32_t *status) |
---|
3190 | n/a | { |
---|
3191 | n/a | if (mpd_isspecial(a)) { |
---|
3192 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
3193 | n/a | return; |
---|
3194 | n/a | } |
---|
3195 | n/a | } |
---|
3196 | n/a | |
---|
3197 | n/a | if (mpd_isnegative(a)) { |
---|
3198 | n/a | mpd_qminus(result, a, ctx, status); |
---|
3199 | n/a | } |
---|
3200 | n/a | else { |
---|
3201 | n/a | mpd_qplus(result, a, ctx, status); |
---|
3202 | n/a | } |
---|
3203 | n/a | } |
---|
3204 | n/a | |
---|
3205 | n/a | static inline void |
---|
3206 | n/a | _mpd_ptrswap(const mpd_t **a, const mpd_t **b) |
---|
3207 | n/a | { |
---|
3208 | n/a | const mpd_t *t = *a; |
---|
3209 | n/a | *a = *b; |
---|
3210 | n/a | *b = t; |
---|
3211 | n/a | } |
---|
3212 | n/a | |
---|
3213 | n/a | /* Add or subtract infinities. */ |
---|
3214 | n/a | static void |
---|
3215 | n/a | _mpd_qaddsub_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b, |
---|
3216 | n/a | uint32_t *status) |
---|
3217 | n/a | { |
---|
3218 | n/a | if (mpd_isinfinite(a)) { |
---|
3219 | n/a | if (mpd_sign(a) != sign_b && mpd_isinfinite(b)) { |
---|
3220 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3221 | n/a | } |
---|
3222 | n/a | else { |
---|
3223 | n/a | mpd_setspecial(result, mpd_sign(a), MPD_INF); |
---|
3224 | n/a | } |
---|
3225 | n/a | return; |
---|
3226 | n/a | } |
---|
3227 | n/a | assert(mpd_isinfinite(b)); |
---|
3228 | n/a | mpd_setspecial(result, sign_b, MPD_INF); |
---|
3229 | n/a | } |
---|
3230 | n/a | |
---|
3231 | n/a | /* Add or subtract non-special numbers. */ |
---|
3232 | n/a | static void |
---|
3233 | n/a | _mpd_qaddsub(mpd_t *result, const mpd_t *a, const mpd_t *b, uint8_t sign_b, |
---|
3234 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3235 | n/a | { |
---|
3236 | n/a | const mpd_t *big, *small; |
---|
3237 | n/a | MPD_NEW_STATIC(big_aligned,0,0,0,0); |
---|
3238 | n/a | MPD_NEW_CONST(tiny,0,0,1,1,1,1); |
---|
3239 | n/a | mpd_uint_t carry; |
---|
3240 | n/a | mpd_ssize_t newsize, shift; |
---|
3241 | n/a | mpd_ssize_t exp, i; |
---|
3242 | n/a | int swap = 0; |
---|
3243 | n/a | |
---|
3244 | n/a | |
---|
3245 | n/a | /* compare exponents */ |
---|
3246 | n/a | big = a; small = b; |
---|
3247 | n/a | if (big->exp != small->exp) { |
---|
3248 | n/a | if (small->exp > big->exp) { |
---|
3249 | n/a | _mpd_ptrswap(&big, &small); |
---|
3250 | n/a | swap++; |
---|
3251 | n/a | } |
---|
3252 | n/a | /* align the coefficients */ |
---|
3253 | n/a | if (!mpd_iszerocoeff(big)) { |
---|
3254 | n/a | exp = big->exp - 1; |
---|
3255 | n/a | exp += (big->digits > ctx->prec) ? 0 : big->digits-ctx->prec-1; |
---|
3256 | n/a | if (mpd_adjexp(small) < exp) { |
---|
3257 | n/a | /* |
---|
3258 | n/a | * Avoid huge shifts by substituting a value for small that is |
---|
3259 | n/a | * guaranteed to produce the same results. |
---|
3260 | n/a | * |
---|
3261 | n/a | * adjexp(small) < exp if and only if: |
---|
3262 | n/a | * |
---|
3263 | n/a | * bdigits <= prec AND |
---|
3264 | n/a | * bdigits+shift >= prec+2+sdigits AND |
---|
3265 | n/a | * exp = bexp+bdigits-prec-2 |
---|
3266 | n/a | * |
---|
3267 | n/a | * 1234567000000000 -> bdigits + shift |
---|
3268 | n/a | * ----------XX1234 -> sdigits |
---|
3269 | n/a | * ----------X1 -> tiny-digits |
---|
3270 | n/a | * |- prec -| |
---|
3271 | n/a | * |
---|
3272 | n/a | * OR |
---|
3273 | n/a | * |
---|
3274 | n/a | * bdigits > prec AND |
---|
3275 | n/a | * shift > sdigits AND |
---|
3276 | n/a | * exp = bexp-1 |
---|
3277 | n/a | * |
---|
3278 | n/a | * 1234567892100000 -> bdigits + shift |
---|
3279 | n/a | * ----------XX1234 -> sdigits |
---|
3280 | n/a | * ----------X1 -> tiny-digits |
---|
3281 | n/a | * |- prec -| |
---|
3282 | n/a | * |
---|
3283 | n/a | * If tiny is zero, adding or subtracting is a no-op. |
---|
3284 | n/a | * Otherwise, adding tiny generates a non-zero digit either |
---|
3285 | n/a | * below the rounding digit or the least significant digit |
---|
3286 | n/a | * of big. When subtracting, tiny is in the same position as |
---|
3287 | n/a | * the carry that would be generated by subtracting sdigits. |
---|
3288 | n/a | */ |
---|
3289 | n/a | mpd_copy_flags(&tiny, small); |
---|
3290 | n/a | tiny.exp = exp; |
---|
3291 | n/a | tiny.digits = 1; |
---|
3292 | n/a | tiny.len = 1; |
---|
3293 | n/a | tiny.data[0] = mpd_iszerocoeff(small) ? 0 : 1; |
---|
3294 | n/a | small = &tiny; |
---|
3295 | n/a | } |
---|
3296 | n/a | /* This cannot wrap: the difference is positive and <= maxprec */ |
---|
3297 | n/a | shift = big->exp - small->exp; |
---|
3298 | n/a | if (!mpd_qshiftl(&big_aligned, big, shift, status)) { |
---|
3299 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
3300 | n/a | goto finish; |
---|
3301 | n/a | } |
---|
3302 | n/a | big = &big_aligned; |
---|
3303 | n/a | } |
---|
3304 | n/a | } |
---|
3305 | n/a | result->exp = small->exp; |
---|
3306 | n/a | |
---|
3307 | n/a | |
---|
3308 | n/a | /* compare length of coefficients */ |
---|
3309 | n/a | if (big->len < small->len) { |
---|
3310 | n/a | _mpd_ptrswap(&big, &small); |
---|
3311 | n/a | swap++; |
---|
3312 | n/a | } |
---|
3313 | n/a | |
---|
3314 | n/a | newsize = big->len; |
---|
3315 | n/a | if (!mpd_qresize(result, newsize, status)) { |
---|
3316 | n/a | goto finish; |
---|
3317 | n/a | } |
---|
3318 | n/a | |
---|
3319 | n/a | if (mpd_sign(a) == sign_b) { |
---|
3320 | n/a | |
---|
3321 | n/a | carry = _mpd_baseadd(result->data, big->data, small->data, |
---|
3322 | n/a | big->len, small->len); |
---|
3323 | n/a | |
---|
3324 | n/a | if (carry) { |
---|
3325 | n/a | newsize = big->len + 1; |
---|
3326 | n/a | if (!mpd_qresize(result, newsize, status)) { |
---|
3327 | n/a | goto finish; |
---|
3328 | n/a | } |
---|
3329 | n/a | result->data[newsize-1] = carry; |
---|
3330 | n/a | } |
---|
3331 | n/a | |
---|
3332 | n/a | result->len = newsize; |
---|
3333 | n/a | mpd_set_flags(result, sign_b); |
---|
3334 | n/a | } |
---|
3335 | n/a | else { |
---|
3336 | n/a | if (big->len == small->len) { |
---|
3337 | n/a | for (i=big->len-1; i >= 0; --i) { |
---|
3338 | n/a | if (big->data[i] != small->data[i]) { |
---|
3339 | n/a | if (big->data[i] < small->data[i]) { |
---|
3340 | n/a | _mpd_ptrswap(&big, &small); |
---|
3341 | n/a | swap++; |
---|
3342 | n/a | } |
---|
3343 | n/a | break; |
---|
3344 | n/a | } |
---|
3345 | n/a | } |
---|
3346 | n/a | } |
---|
3347 | n/a | |
---|
3348 | n/a | _mpd_basesub(result->data, big->data, small->data, |
---|
3349 | n/a | big->len, small->len); |
---|
3350 | n/a | newsize = _mpd_real_size(result->data, big->len); |
---|
3351 | n/a | /* resize to smaller cannot fail */ |
---|
3352 | n/a | (void)mpd_qresize(result, newsize, status); |
---|
3353 | n/a | |
---|
3354 | n/a | result->len = newsize; |
---|
3355 | n/a | sign_b = (swap & 1) ? sign_b : mpd_sign(a); |
---|
3356 | n/a | mpd_set_flags(result, sign_b); |
---|
3357 | n/a | |
---|
3358 | n/a | if (mpd_iszerocoeff(result)) { |
---|
3359 | n/a | mpd_set_positive(result); |
---|
3360 | n/a | if (ctx->round == MPD_ROUND_FLOOR) { |
---|
3361 | n/a | mpd_set_negative(result); |
---|
3362 | n/a | } |
---|
3363 | n/a | } |
---|
3364 | n/a | } |
---|
3365 | n/a | |
---|
3366 | n/a | mpd_setdigits(result); |
---|
3367 | n/a | |
---|
3368 | n/a | finish: |
---|
3369 | n/a | mpd_del(&big_aligned); |
---|
3370 | n/a | } |
---|
3371 | n/a | |
---|
3372 | n/a | /* Add a and b. No specials, no finalizing. */ |
---|
3373 | n/a | static void |
---|
3374 | n/a | _mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3375 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3376 | n/a | { |
---|
3377 | n/a | _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status); |
---|
3378 | n/a | } |
---|
3379 | n/a | |
---|
3380 | n/a | /* Subtract b from a. No specials, no finalizing. */ |
---|
3381 | n/a | static void |
---|
3382 | n/a | _mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3383 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3384 | n/a | { |
---|
3385 | n/a | _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status); |
---|
3386 | n/a | } |
---|
3387 | n/a | |
---|
3388 | n/a | /* Add a and b. */ |
---|
3389 | n/a | void |
---|
3390 | n/a | mpd_qadd(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3391 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3392 | n/a | { |
---|
3393 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3394 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
3395 | n/a | return; |
---|
3396 | n/a | } |
---|
3397 | n/a | _mpd_qaddsub_inf(result, a, b, mpd_sign(b), status); |
---|
3398 | n/a | return; |
---|
3399 | n/a | } |
---|
3400 | n/a | |
---|
3401 | n/a | _mpd_qaddsub(result, a, b, mpd_sign(b), ctx, status); |
---|
3402 | n/a | mpd_qfinalize(result, ctx, status); |
---|
3403 | n/a | } |
---|
3404 | n/a | |
---|
3405 | n/a | /* Add a and b. Set NaN/Invalid_operation if the result is inexact. */ |
---|
3406 | n/a | static void |
---|
3407 | n/a | _mpd_qadd_exact(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3408 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3409 | n/a | { |
---|
3410 | n/a | uint32_t workstatus = 0; |
---|
3411 | n/a | |
---|
3412 | n/a | mpd_qadd(result, a, b, ctx, &workstatus); |
---|
3413 | n/a | *status |= workstatus; |
---|
3414 | n/a | if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { |
---|
3415 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3416 | n/a | } |
---|
3417 | n/a | } |
---|
3418 | n/a | |
---|
3419 | n/a | /* Subtract b from a. */ |
---|
3420 | n/a | void |
---|
3421 | n/a | mpd_qsub(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3422 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3423 | n/a | { |
---|
3424 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3425 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
3426 | n/a | return; |
---|
3427 | n/a | } |
---|
3428 | n/a | _mpd_qaddsub_inf(result, a, b, !mpd_sign(b), status); |
---|
3429 | n/a | return; |
---|
3430 | n/a | } |
---|
3431 | n/a | |
---|
3432 | n/a | _mpd_qaddsub(result, a, b, !mpd_sign(b), ctx, status); |
---|
3433 | n/a | mpd_qfinalize(result, ctx, status); |
---|
3434 | n/a | } |
---|
3435 | n/a | |
---|
3436 | n/a | /* Subtract b from a. Set NaN/Invalid_operation if the result is inexact. */ |
---|
3437 | n/a | static void |
---|
3438 | n/a | _mpd_qsub_exact(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3439 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3440 | n/a | { |
---|
3441 | n/a | uint32_t workstatus = 0; |
---|
3442 | n/a | |
---|
3443 | n/a | mpd_qsub(result, a, b, ctx, &workstatus); |
---|
3444 | n/a | *status |= workstatus; |
---|
3445 | n/a | if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { |
---|
3446 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3447 | n/a | } |
---|
3448 | n/a | } |
---|
3449 | n/a | |
---|
3450 | n/a | /* Add decimal and mpd_ssize_t. */ |
---|
3451 | n/a | void |
---|
3452 | n/a | mpd_qadd_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, |
---|
3453 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3454 | n/a | { |
---|
3455 | n/a | mpd_context_t maxcontext; |
---|
3456 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3457 | n/a | |
---|
3458 | n/a | mpd_maxcontext(&maxcontext); |
---|
3459 | n/a | mpd_qsset_ssize(&bb, b, &maxcontext, status); |
---|
3460 | n/a | mpd_qadd(result, a, &bb, ctx, status); |
---|
3461 | n/a | mpd_del(&bb); |
---|
3462 | n/a | } |
---|
3463 | n/a | |
---|
3464 | n/a | /* Add decimal and mpd_uint_t. */ |
---|
3465 | n/a | void |
---|
3466 | n/a | mpd_qadd_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, |
---|
3467 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3468 | n/a | { |
---|
3469 | n/a | mpd_context_t maxcontext; |
---|
3470 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3471 | n/a | |
---|
3472 | n/a | mpd_maxcontext(&maxcontext); |
---|
3473 | n/a | mpd_qsset_uint(&bb, b, &maxcontext, status); |
---|
3474 | n/a | mpd_qadd(result, a, &bb, ctx, status); |
---|
3475 | n/a | mpd_del(&bb); |
---|
3476 | n/a | } |
---|
3477 | n/a | |
---|
3478 | n/a | /* Subtract mpd_ssize_t from decimal. */ |
---|
3479 | n/a | void |
---|
3480 | n/a | mpd_qsub_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, |
---|
3481 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3482 | n/a | { |
---|
3483 | n/a | mpd_context_t maxcontext; |
---|
3484 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3485 | n/a | |
---|
3486 | n/a | mpd_maxcontext(&maxcontext); |
---|
3487 | n/a | mpd_qsset_ssize(&bb, b, &maxcontext, status); |
---|
3488 | n/a | mpd_qsub(result, a, &bb, ctx, status); |
---|
3489 | n/a | mpd_del(&bb); |
---|
3490 | n/a | } |
---|
3491 | n/a | |
---|
3492 | n/a | /* Subtract mpd_uint_t from decimal. */ |
---|
3493 | n/a | void |
---|
3494 | n/a | mpd_qsub_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, |
---|
3495 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3496 | n/a | { |
---|
3497 | n/a | mpd_context_t maxcontext; |
---|
3498 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3499 | n/a | |
---|
3500 | n/a | mpd_maxcontext(&maxcontext); |
---|
3501 | n/a | mpd_qsset_uint(&bb, b, &maxcontext, status); |
---|
3502 | n/a | mpd_qsub(result, a, &bb, ctx, status); |
---|
3503 | n/a | mpd_del(&bb); |
---|
3504 | n/a | } |
---|
3505 | n/a | |
---|
3506 | n/a | /* Add decimal and int32_t. */ |
---|
3507 | n/a | void |
---|
3508 | n/a | mpd_qadd_i32(mpd_t *result, const mpd_t *a, int32_t b, |
---|
3509 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3510 | n/a | { |
---|
3511 | n/a | mpd_qadd_ssize(result, a, b, ctx, status); |
---|
3512 | n/a | } |
---|
3513 | n/a | |
---|
3514 | n/a | /* Add decimal and uint32_t. */ |
---|
3515 | n/a | void |
---|
3516 | n/a | mpd_qadd_u32(mpd_t *result, const mpd_t *a, uint32_t b, |
---|
3517 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3518 | n/a | { |
---|
3519 | n/a | mpd_qadd_uint(result, a, b, ctx, status); |
---|
3520 | n/a | } |
---|
3521 | n/a | |
---|
3522 | n/a | #ifdef CONFIG_64 |
---|
3523 | n/a | /* Add decimal and int64_t. */ |
---|
3524 | n/a | void |
---|
3525 | n/a | mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
3526 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3527 | n/a | { |
---|
3528 | n/a | mpd_qadd_ssize(result, a, b, ctx, status); |
---|
3529 | n/a | } |
---|
3530 | n/a | |
---|
3531 | n/a | /* Add decimal and uint64_t. */ |
---|
3532 | n/a | void |
---|
3533 | n/a | mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
3534 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3535 | n/a | { |
---|
3536 | n/a | mpd_qadd_uint(result, a, b, ctx, status); |
---|
3537 | n/a | } |
---|
3538 | n/a | #elif !defined(LEGACY_COMPILER) |
---|
3539 | n/a | /* Add decimal and int64_t. */ |
---|
3540 | n/a | void |
---|
3541 | n/a | mpd_qadd_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
3542 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3543 | n/a | { |
---|
3544 | n/a | mpd_context_t maxcontext; |
---|
3545 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3546 | n/a | |
---|
3547 | n/a | mpd_maxcontext(&maxcontext); |
---|
3548 | n/a | mpd_qset_i64(&bb, b, &maxcontext, status); |
---|
3549 | n/a | mpd_qadd(result, a, &bb, ctx, status); |
---|
3550 | n/a | mpd_del(&bb); |
---|
3551 | n/a | } |
---|
3552 | n/a | |
---|
3553 | n/a | /* Add decimal and uint64_t. */ |
---|
3554 | n/a | void |
---|
3555 | n/a | mpd_qadd_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
3556 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3557 | n/a | { |
---|
3558 | n/a | mpd_context_t maxcontext; |
---|
3559 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3560 | n/a | |
---|
3561 | n/a | mpd_maxcontext(&maxcontext); |
---|
3562 | n/a | mpd_qset_u64(&bb, b, &maxcontext, status); |
---|
3563 | n/a | mpd_qadd(result, a, &bb, ctx, status); |
---|
3564 | n/a | mpd_del(&bb); |
---|
3565 | n/a | } |
---|
3566 | n/a | #endif |
---|
3567 | n/a | |
---|
3568 | n/a | /* Subtract int32_t from decimal. */ |
---|
3569 | n/a | void |
---|
3570 | n/a | mpd_qsub_i32(mpd_t *result, const mpd_t *a, int32_t b, |
---|
3571 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3572 | n/a | { |
---|
3573 | n/a | mpd_qsub_ssize(result, a, b, ctx, status); |
---|
3574 | n/a | } |
---|
3575 | n/a | |
---|
3576 | n/a | /* Subtract uint32_t from decimal. */ |
---|
3577 | n/a | void |
---|
3578 | n/a | mpd_qsub_u32(mpd_t *result, const mpd_t *a, uint32_t b, |
---|
3579 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3580 | n/a | { |
---|
3581 | n/a | mpd_qsub_uint(result, a, b, ctx, status); |
---|
3582 | n/a | } |
---|
3583 | n/a | |
---|
3584 | n/a | #ifdef CONFIG_64 |
---|
3585 | n/a | /* Subtract int64_t from decimal. */ |
---|
3586 | n/a | void |
---|
3587 | n/a | mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
3588 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3589 | n/a | { |
---|
3590 | n/a | mpd_qsub_ssize(result, a, b, ctx, status); |
---|
3591 | n/a | } |
---|
3592 | n/a | |
---|
3593 | n/a | /* Subtract uint64_t from decimal. */ |
---|
3594 | n/a | void |
---|
3595 | n/a | mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
3596 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3597 | n/a | { |
---|
3598 | n/a | mpd_qsub_uint(result, a, b, ctx, status); |
---|
3599 | n/a | } |
---|
3600 | n/a | #elif !defined(LEGACY_COMPILER) |
---|
3601 | n/a | /* Subtract int64_t from decimal. */ |
---|
3602 | n/a | void |
---|
3603 | n/a | mpd_qsub_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
3604 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3605 | n/a | { |
---|
3606 | n/a | mpd_context_t maxcontext; |
---|
3607 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3608 | n/a | |
---|
3609 | n/a | mpd_maxcontext(&maxcontext); |
---|
3610 | n/a | mpd_qset_i64(&bb, b, &maxcontext, status); |
---|
3611 | n/a | mpd_qsub(result, a, &bb, ctx, status); |
---|
3612 | n/a | mpd_del(&bb); |
---|
3613 | n/a | } |
---|
3614 | n/a | |
---|
3615 | n/a | /* Subtract uint64_t from decimal. */ |
---|
3616 | n/a | void |
---|
3617 | n/a | mpd_qsub_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
3618 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3619 | n/a | { |
---|
3620 | n/a | mpd_context_t maxcontext; |
---|
3621 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
3622 | n/a | |
---|
3623 | n/a | mpd_maxcontext(&maxcontext); |
---|
3624 | n/a | mpd_qset_u64(&bb, b, &maxcontext, status); |
---|
3625 | n/a | mpd_qsub(result, a, &bb, ctx, status); |
---|
3626 | n/a | mpd_del(&bb); |
---|
3627 | n/a | } |
---|
3628 | n/a | #endif |
---|
3629 | n/a | |
---|
3630 | n/a | |
---|
3631 | n/a | /* Divide infinities. */ |
---|
3632 | n/a | static void |
---|
3633 | n/a | _mpd_qdiv_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
3634 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3635 | n/a | { |
---|
3636 | n/a | if (mpd_isinfinite(a)) { |
---|
3637 | n/a | if (mpd_isinfinite(b)) { |
---|
3638 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
3639 | n/a | return; |
---|
3640 | n/a | } |
---|
3641 | n/a | mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); |
---|
3642 | n/a | return; |
---|
3643 | n/a | } |
---|
3644 | n/a | assert(mpd_isinfinite(b)); |
---|
3645 | n/a | _settriple(result, mpd_sign(a)^mpd_sign(b), 0, mpd_etiny(ctx)); |
---|
3646 | n/a | *status |= MPD_Clamped; |
---|
3647 | n/a | } |
---|
3648 | n/a | |
---|
3649 | n/a | enum {NO_IDEAL_EXP, SET_IDEAL_EXP}; |
---|
3650 | n/a | /* Divide a by b. */ |
---|
3651 | n/a | static void |
---|
3652 | n/a | _mpd_qdiv(int action, mpd_t *q, const mpd_t *a, const mpd_t *b, |
---|
3653 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3654 | n/a | { |
---|
3655 | n/a | MPD_NEW_STATIC(aligned,0,0,0,0); |
---|
3656 | n/a | mpd_uint_t ld; |
---|
3657 | n/a | mpd_ssize_t shift, exp, tz; |
---|
3658 | n/a | mpd_ssize_t newsize; |
---|
3659 | n/a | mpd_ssize_t ideal_exp; |
---|
3660 | n/a | mpd_uint_t rem; |
---|
3661 | n/a | uint8_t sign_a = mpd_sign(a); |
---|
3662 | n/a | uint8_t sign_b = mpd_sign(b); |
---|
3663 | n/a | |
---|
3664 | n/a | |
---|
3665 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3666 | n/a | if (mpd_qcheck_nans(q, a, b, ctx, status)) { |
---|
3667 | n/a | return; |
---|
3668 | n/a | } |
---|
3669 | n/a | _mpd_qdiv_inf(q, a, b, ctx, status); |
---|
3670 | n/a | return; |
---|
3671 | n/a | } |
---|
3672 | n/a | if (mpd_iszerocoeff(b)) { |
---|
3673 | n/a | if (mpd_iszerocoeff(a)) { |
---|
3674 | n/a | mpd_seterror(q, MPD_Division_undefined, status); |
---|
3675 | n/a | } |
---|
3676 | n/a | else { |
---|
3677 | n/a | mpd_setspecial(q, sign_a^sign_b, MPD_INF); |
---|
3678 | n/a | *status |= MPD_Division_by_zero; |
---|
3679 | n/a | } |
---|
3680 | n/a | return; |
---|
3681 | n/a | } |
---|
3682 | n/a | if (mpd_iszerocoeff(a)) { |
---|
3683 | n/a | exp = a->exp - b->exp; |
---|
3684 | n/a | _settriple(q, sign_a^sign_b, 0, exp); |
---|
3685 | n/a | mpd_qfinalize(q, ctx, status); |
---|
3686 | n/a | return; |
---|
3687 | n/a | } |
---|
3688 | n/a | |
---|
3689 | n/a | shift = (b->digits - a->digits) + ctx->prec + 1; |
---|
3690 | n/a | ideal_exp = a->exp - b->exp; |
---|
3691 | n/a | exp = ideal_exp - shift; |
---|
3692 | n/a | if (shift > 0) { |
---|
3693 | n/a | if (!mpd_qshiftl(&aligned, a, shift, status)) { |
---|
3694 | n/a | mpd_seterror(q, MPD_Malloc_error, status); |
---|
3695 | n/a | goto finish; |
---|
3696 | n/a | } |
---|
3697 | n/a | a = &aligned; |
---|
3698 | n/a | } |
---|
3699 | n/a | else if (shift < 0) { |
---|
3700 | n/a | shift = -shift; |
---|
3701 | n/a | if (!mpd_qshiftl(&aligned, b, shift, status)) { |
---|
3702 | n/a | mpd_seterror(q, MPD_Malloc_error, status); |
---|
3703 | n/a | goto finish; |
---|
3704 | n/a | } |
---|
3705 | n/a | b = &aligned; |
---|
3706 | n/a | } |
---|
3707 | n/a | |
---|
3708 | n/a | |
---|
3709 | n/a | newsize = a->len - b->len + 1; |
---|
3710 | n/a | if ((q != b && q != a) || (q == b && newsize > b->len)) { |
---|
3711 | n/a | if (!mpd_qresize(q, newsize, status)) { |
---|
3712 | n/a | mpd_seterror(q, MPD_Malloc_error, status); |
---|
3713 | n/a | goto finish; |
---|
3714 | n/a | } |
---|
3715 | n/a | } |
---|
3716 | n/a | |
---|
3717 | n/a | |
---|
3718 | n/a | if (b->len == 1) { |
---|
3719 | n/a | rem = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]); |
---|
3720 | n/a | } |
---|
3721 | n/a | else if (b->len <= MPD_NEWTONDIV_CUTOFF) { |
---|
3722 | n/a | int ret = _mpd_basedivmod(q->data, NULL, a->data, b->data, |
---|
3723 | n/a | a->len, b->len); |
---|
3724 | n/a | if (ret < 0) { |
---|
3725 | n/a | mpd_seterror(q, MPD_Malloc_error, status); |
---|
3726 | n/a | goto finish; |
---|
3727 | n/a | } |
---|
3728 | n/a | rem = ret; |
---|
3729 | n/a | } |
---|
3730 | n/a | else { |
---|
3731 | n/a | MPD_NEW_STATIC(r,0,0,0,0); |
---|
3732 | n/a | _mpd_base_ndivmod(q, &r, a, b, status); |
---|
3733 | n/a | if (mpd_isspecial(q) || mpd_isspecial(&r)) { |
---|
3734 | n/a | mpd_setspecial(q, MPD_POS, MPD_NAN); |
---|
3735 | n/a | mpd_del(&r); |
---|
3736 | n/a | goto finish; |
---|
3737 | n/a | } |
---|
3738 | n/a | rem = !mpd_iszerocoeff(&r); |
---|
3739 | n/a | mpd_del(&r); |
---|
3740 | n/a | newsize = q->len; |
---|
3741 | n/a | } |
---|
3742 | n/a | |
---|
3743 | n/a | newsize = _mpd_real_size(q->data, newsize); |
---|
3744 | n/a | /* resize to smaller cannot fail */ |
---|
3745 | n/a | mpd_qresize(q, newsize, status); |
---|
3746 | n/a | mpd_set_flags(q, sign_a^sign_b); |
---|
3747 | n/a | q->len = newsize; |
---|
3748 | n/a | mpd_setdigits(q); |
---|
3749 | n/a | |
---|
3750 | n/a | shift = ideal_exp - exp; |
---|
3751 | n/a | if (rem) { |
---|
3752 | n/a | ld = mpd_lsd(q->data[0]); |
---|
3753 | n/a | if (ld == 0 || ld == 5) { |
---|
3754 | n/a | q->data[0] += 1; |
---|
3755 | n/a | } |
---|
3756 | n/a | } |
---|
3757 | n/a | else if (action == SET_IDEAL_EXP && shift > 0) { |
---|
3758 | n/a | tz = mpd_trail_zeros(q); |
---|
3759 | n/a | shift = (tz > shift) ? shift : tz; |
---|
3760 | n/a | mpd_qshiftr_inplace(q, shift); |
---|
3761 | n/a | exp += shift; |
---|
3762 | n/a | } |
---|
3763 | n/a | |
---|
3764 | n/a | q->exp = exp; |
---|
3765 | n/a | |
---|
3766 | n/a | |
---|
3767 | n/a | finish: |
---|
3768 | n/a | mpd_del(&aligned); |
---|
3769 | n/a | mpd_qfinalize(q, ctx, status); |
---|
3770 | n/a | } |
---|
3771 | n/a | |
---|
3772 | n/a | /* Divide a by b. */ |
---|
3773 | n/a | void |
---|
3774 | n/a | mpd_qdiv(mpd_t *q, const mpd_t *a, const mpd_t *b, |
---|
3775 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3776 | n/a | { |
---|
3777 | n/a | _mpd_qdiv(SET_IDEAL_EXP, q, a, b, ctx, status); |
---|
3778 | n/a | } |
---|
3779 | n/a | |
---|
3780 | n/a | /* Internal function. */ |
---|
3781 | n/a | static void |
---|
3782 | n/a | _mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, |
---|
3783 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3784 | n/a | { |
---|
3785 | n/a | MPD_NEW_STATIC(aligned,0,0,0,0); |
---|
3786 | n/a | mpd_ssize_t qsize, rsize; |
---|
3787 | n/a | mpd_ssize_t ideal_exp, expdiff, shift; |
---|
3788 | n/a | uint8_t sign_a = mpd_sign(a); |
---|
3789 | n/a | uint8_t sign_ab = mpd_sign(a)^mpd_sign(b); |
---|
3790 | n/a | |
---|
3791 | n/a | |
---|
3792 | n/a | ideal_exp = (a->exp > b->exp) ? b->exp : a->exp; |
---|
3793 | n/a | if (mpd_iszerocoeff(a)) { |
---|
3794 | n/a | if (!mpd_qcopy(r, a, status)) { |
---|
3795 | n/a | goto nanresult; /* GCOV_NOT_REACHED */ |
---|
3796 | n/a | } |
---|
3797 | n/a | r->exp = ideal_exp; |
---|
3798 | n/a | _settriple(q, sign_ab, 0, 0); |
---|
3799 | n/a | return; |
---|
3800 | n/a | } |
---|
3801 | n/a | |
---|
3802 | n/a | expdiff = mpd_adjexp(a) - mpd_adjexp(b); |
---|
3803 | n/a | if (expdiff < 0) { |
---|
3804 | n/a | if (a->exp > b->exp) { |
---|
3805 | n/a | /* positive and less than b->digits - a->digits */ |
---|
3806 | n/a | shift = a->exp - b->exp; |
---|
3807 | n/a | if (!mpd_qshiftl(r, a, shift, status)) { |
---|
3808 | n/a | goto nanresult; |
---|
3809 | n/a | } |
---|
3810 | n/a | r->exp = ideal_exp; |
---|
3811 | n/a | } |
---|
3812 | n/a | else { |
---|
3813 | n/a | if (!mpd_qcopy(r, a, status)) { |
---|
3814 | n/a | goto nanresult; |
---|
3815 | n/a | } |
---|
3816 | n/a | } |
---|
3817 | n/a | _settriple(q, sign_ab, 0, 0); |
---|
3818 | n/a | return; |
---|
3819 | n/a | } |
---|
3820 | n/a | if (expdiff > ctx->prec) { |
---|
3821 | n/a | *status |= MPD_Division_impossible; |
---|
3822 | n/a | goto nanresult; |
---|
3823 | n/a | } |
---|
3824 | n/a | |
---|
3825 | n/a | |
---|
3826 | n/a | /* |
---|
3827 | n/a | * At this point we have: |
---|
3828 | n/a | * (1) 0 <= a->exp + a->digits - b->exp - b->digits <= prec |
---|
3829 | n/a | * (2) a->exp - b->exp >= b->digits - a->digits |
---|
3830 | n/a | * (3) a->exp - b->exp <= prec + b->digits - a->digits |
---|
3831 | n/a | */ |
---|
3832 | n/a | if (a->exp != b->exp) { |
---|
3833 | n/a | shift = a->exp - b->exp; |
---|
3834 | n/a | if (shift > 0) { |
---|
3835 | n/a | /* by (3), after the shift a->digits <= prec + b->digits */ |
---|
3836 | n/a | if (!mpd_qshiftl(&aligned, a, shift, status)) { |
---|
3837 | n/a | goto nanresult; |
---|
3838 | n/a | } |
---|
3839 | n/a | a = &aligned; |
---|
3840 | n/a | } |
---|
3841 | n/a | else { |
---|
3842 | n/a | shift = -shift; |
---|
3843 | n/a | /* by (2), after the shift b->digits <= a->digits */ |
---|
3844 | n/a | if (!mpd_qshiftl(&aligned, b, shift, status)) { |
---|
3845 | n/a | goto nanresult; |
---|
3846 | n/a | } |
---|
3847 | n/a | b = &aligned; |
---|
3848 | n/a | } |
---|
3849 | n/a | } |
---|
3850 | n/a | |
---|
3851 | n/a | |
---|
3852 | n/a | qsize = a->len - b->len + 1; |
---|
3853 | n/a | if (!(q == a && qsize < a->len) && !(q == b && qsize < b->len)) { |
---|
3854 | n/a | if (!mpd_qresize(q, qsize, status)) { |
---|
3855 | n/a | goto nanresult; |
---|
3856 | n/a | } |
---|
3857 | n/a | } |
---|
3858 | n/a | |
---|
3859 | n/a | rsize = b->len; |
---|
3860 | n/a | if (!(r == a && rsize < a->len)) { |
---|
3861 | n/a | if (!mpd_qresize(r, rsize, status)) { |
---|
3862 | n/a | goto nanresult; |
---|
3863 | n/a | } |
---|
3864 | n/a | } |
---|
3865 | n/a | |
---|
3866 | n/a | if (b->len == 1) { |
---|
3867 | n/a | if (a->len == 1) { |
---|
3868 | n/a | _mpd_div_word(&q->data[0], &r->data[0], a->data[0], b->data[0]); |
---|
3869 | n/a | } |
---|
3870 | n/a | else { |
---|
3871 | n/a | r->data[0] = _mpd_shortdiv(q->data, a->data, a->len, b->data[0]); |
---|
3872 | n/a | } |
---|
3873 | n/a | } |
---|
3874 | n/a | else if (b->len <= MPD_NEWTONDIV_CUTOFF) { |
---|
3875 | n/a | int ret; |
---|
3876 | n/a | ret = _mpd_basedivmod(q->data, r->data, a->data, b->data, |
---|
3877 | n/a | a->len, b->len); |
---|
3878 | n/a | if (ret == -1) { |
---|
3879 | n/a | *status |= MPD_Malloc_error; |
---|
3880 | n/a | goto nanresult; |
---|
3881 | n/a | } |
---|
3882 | n/a | } |
---|
3883 | n/a | else { |
---|
3884 | n/a | _mpd_base_ndivmod(q, r, a, b, status); |
---|
3885 | n/a | if (mpd_isspecial(q) || mpd_isspecial(r)) { |
---|
3886 | n/a | goto nanresult; |
---|
3887 | n/a | } |
---|
3888 | n/a | qsize = q->len; |
---|
3889 | n/a | rsize = r->len; |
---|
3890 | n/a | } |
---|
3891 | n/a | |
---|
3892 | n/a | qsize = _mpd_real_size(q->data, qsize); |
---|
3893 | n/a | /* resize to smaller cannot fail */ |
---|
3894 | n/a | mpd_qresize(q, qsize, status); |
---|
3895 | n/a | q->len = qsize; |
---|
3896 | n/a | mpd_setdigits(q); |
---|
3897 | n/a | mpd_set_flags(q, sign_ab); |
---|
3898 | n/a | q->exp = 0; |
---|
3899 | n/a | if (q->digits > ctx->prec) { |
---|
3900 | n/a | *status |= MPD_Division_impossible; |
---|
3901 | n/a | goto nanresult; |
---|
3902 | n/a | } |
---|
3903 | n/a | |
---|
3904 | n/a | rsize = _mpd_real_size(r->data, rsize); |
---|
3905 | n/a | /* resize to smaller cannot fail */ |
---|
3906 | n/a | mpd_qresize(r, rsize, status); |
---|
3907 | n/a | r->len = rsize; |
---|
3908 | n/a | mpd_setdigits(r); |
---|
3909 | n/a | mpd_set_flags(r, sign_a); |
---|
3910 | n/a | r->exp = ideal_exp; |
---|
3911 | n/a | |
---|
3912 | n/a | out: |
---|
3913 | n/a | mpd_del(&aligned); |
---|
3914 | n/a | return; |
---|
3915 | n/a | |
---|
3916 | n/a | nanresult: |
---|
3917 | n/a | mpd_setspecial(q, MPD_POS, MPD_NAN); |
---|
3918 | n/a | mpd_setspecial(r, MPD_POS, MPD_NAN); |
---|
3919 | n/a | goto out; |
---|
3920 | n/a | } |
---|
3921 | n/a | |
---|
3922 | n/a | /* Integer division with remainder. */ |
---|
3923 | n/a | void |
---|
3924 | n/a | mpd_qdivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, |
---|
3925 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3926 | n/a | { |
---|
3927 | n/a | uint8_t sign = mpd_sign(a)^mpd_sign(b); |
---|
3928 | n/a | |
---|
3929 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3930 | n/a | if (mpd_qcheck_nans(q, a, b, ctx, status)) { |
---|
3931 | n/a | mpd_qcopy(r, q, status); |
---|
3932 | n/a | return; |
---|
3933 | n/a | } |
---|
3934 | n/a | if (mpd_isinfinite(a)) { |
---|
3935 | n/a | if (mpd_isinfinite(b)) { |
---|
3936 | n/a | mpd_setspecial(q, MPD_POS, MPD_NAN); |
---|
3937 | n/a | } |
---|
3938 | n/a | else { |
---|
3939 | n/a | mpd_setspecial(q, sign, MPD_INF); |
---|
3940 | n/a | } |
---|
3941 | n/a | mpd_setspecial(r, MPD_POS, MPD_NAN); |
---|
3942 | n/a | *status |= MPD_Invalid_operation; |
---|
3943 | n/a | return; |
---|
3944 | n/a | } |
---|
3945 | n/a | if (mpd_isinfinite(b)) { |
---|
3946 | n/a | if (!mpd_qcopy(r, a, status)) { |
---|
3947 | n/a | mpd_seterror(q, MPD_Malloc_error, status); |
---|
3948 | n/a | return; |
---|
3949 | n/a | } |
---|
3950 | n/a | mpd_qfinalize(r, ctx, status); |
---|
3951 | n/a | _settriple(q, sign, 0, 0); |
---|
3952 | n/a | return; |
---|
3953 | n/a | } |
---|
3954 | n/a | /* debug */ |
---|
3955 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
3956 | n/a | } |
---|
3957 | n/a | if (mpd_iszerocoeff(b)) { |
---|
3958 | n/a | if (mpd_iszerocoeff(a)) { |
---|
3959 | n/a | mpd_setspecial(q, MPD_POS, MPD_NAN); |
---|
3960 | n/a | mpd_setspecial(r, MPD_POS, MPD_NAN); |
---|
3961 | n/a | *status |= MPD_Division_undefined; |
---|
3962 | n/a | } |
---|
3963 | n/a | else { |
---|
3964 | n/a | mpd_setspecial(q, sign, MPD_INF); |
---|
3965 | n/a | mpd_setspecial(r, MPD_POS, MPD_NAN); |
---|
3966 | n/a | *status |= (MPD_Division_by_zero|MPD_Invalid_operation); |
---|
3967 | n/a | } |
---|
3968 | n/a | return; |
---|
3969 | n/a | } |
---|
3970 | n/a | |
---|
3971 | n/a | _mpd_qdivmod(q, r, a, b, ctx, status); |
---|
3972 | n/a | mpd_qfinalize(q, ctx, status); |
---|
3973 | n/a | mpd_qfinalize(r, ctx, status); |
---|
3974 | n/a | } |
---|
3975 | n/a | |
---|
3976 | n/a | void |
---|
3977 | n/a | mpd_qdivint(mpd_t *q, const mpd_t *a, const mpd_t *b, |
---|
3978 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
3979 | n/a | { |
---|
3980 | n/a | MPD_NEW_STATIC(r,0,0,0,0); |
---|
3981 | n/a | uint8_t sign = mpd_sign(a)^mpd_sign(b); |
---|
3982 | n/a | |
---|
3983 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
3984 | n/a | if (mpd_qcheck_nans(q, a, b, ctx, status)) { |
---|
3985 | n/a | return; |
---|
3986 | n/a | } |
---|
3987 | n/a | if (mpd_isinfinite(a) && mpd_isinfinite(b)) { |
---|
3988 | n/a | mpd_seterror(q, MPD_Invalid_operation, status); |
---|
3989 | n/a | return; |
---|
3990 | n/a | } |
---|
3991 | n/a | if (mpd_isinfinite(a)) { |
---|
3992 | n/a | mpd_setspecial(q, sign, MPD_INF); |
---|
3993 | n/a | return; |
---|
3994 | n/a | } |
---|
3995 | n/a | if (mpd_isinfinite(b)) { |
---|
3996 | n/a | _settriple(q, sign, 0, 0); |
---|
3997 | n/a | return; |
---|
3998 | n/a | } |
---|
3999 | n/a | /* debug */ |
---|
4000 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
4001 | n/a | } |
---|
4002 | n/a | if (mpd_iszerocoeff(b)) { |
---|
4003 | n/a | if (mpd_iszerocoeff(a)) { |
---|
4004 | n/a | mpd_seterror(q, MPD_Division_undefined, status); |
---|
4005 | n/a | } |
---|
4006 | n/a | else { |
---|
4007 | n/a | mpd_setspecial(q, sign, MPD_INF); |
---|
4008 | n/a | *status |= MPD_Division_by_zero; |
---|
4009 | n/a | } |
---|
4010 | n/a | return; |
---|
4011 | n/a | } |
---|
4012 | n/a | |
---|
4013 | n/a | |
---|
4014 | n/a | _mpd_qdivmod(q, &r, a, b, ctx, status); |
---|
4015 | n/a | mpd_del(&r); |
---|
4016 | n/a | mpd_qfinalize(q, ctx, status); |
---|
4017 | n/a | } |
---|
4018 | n/a | |
---|
4019 | n/a | /* Divide decimal by mpd_ssize_t. */ |
---|
4020 | n/a | void |
---|
4021 | n/a | mpd_qdiv_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, |
---|
4022 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4023 | n/a | { |
---|
4024 | n/a | mpd_context_t maxcontext; |
---|
4025 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
4026 | n/a | |
---|
4027 | n/a | mpd_maxcontext(&maxcontext); |
---|
4028 | n/a | mpd_qsset_ssize(&bb, b, &maxcontext, status); |
---|
4029 | n/a | mpd_qdiv(result, a, &bb, ctx, status); |
---|
4030 | n/a | mpd_del(&bb); |
---|
4031 | n/a | } |
---|
4032 | n/a | |
---|
4033 | n/a | /* Divide decimal by mpd_uint_t. */ |
---|
4034 | n/a | void |
---|
4035 | n/a | mpd_qdiv_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, |
---|
4036 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4037 | n/a | { |
---|
4038 | n/a | mpd_context_t maxcontext; |
---|
4039 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
4040 | n/a | |
---|
4041 | n/a | mpd_maxcontext(&maxcontext); |
---|
4042 | n/a | mpd_qsset_uint(&bb, b, &maxcontext, status); |
---|
4043 | n/a | mpd_qdiv(result, a, &bb, ctx, status); |
---|
4044 | n/a | mpd_del(&bb); |
---|
4045 | n/a | } |
---|
4046 | n/a | |
---|
4047 | n/a | /* Divide decimal by int32_t. */ |
---|
4048 | n/a | void |
---|
4049 | n/a | mpd_qdiv_i32(mpd_t *result, const mpd_t *a, int32_t b, |
---|
4050 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4051 | n/a | { |
---|
4052 | n/a | mpd_qdiv_ssize(result, a, b, ctx, status); |
---|
4053 | n/a | } |
---|
4054 | n/a | |
---|
4055 | n/a | /* Divide decimal by uint32_t. */ |
---|
4056 | n/a | void |
---|
4057 | n/a | mpd_qdiv_u32(mpd_t *result, const mpd_t *a, uint32_t b, |
---|
4058 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4059 | n/a | { |
---|
4060 | n/a | mpd_qdiv_uint(result, a, b, ctx, status); |
---|
4061 | n/a | } |
---|
4062 | n/a | |
---|
4063 | n/a | #ifdef CONFIG_64 |
---|
4064 | n/a | /* Divide decimal by int64_t. */ |
---|
4065 | n/a | void |
---|
4066 | n/a | mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
4067 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4068 | n/a | { |
---|
4069 | n/a | mpd_qdiv_ssize(result, a, b, ctx, status); |
---|
4070 | n/a | } |
---|
4071 | n/a | |
---|
4072 | n/a | /* Divide decimal by uint64_t. */ |
---|
4073 | n/a | void |
---|
4074 | n/a | mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
4075 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4076 | n/a | { |
---|
4077 | n/a | mpd_qdiv_uint(result, a, b, ctx, status); |
---|
4078 | n/a | } |
---|
4079 | n/a | #elif !defined(LEGACY_COMPILER) |
---|
4080 | n/a | /* Divide decimal by int64_t. */ |
---|
4081 | n/a | void |
---|
4082 | n/a | mpd_qdiv_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
4083 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4084 | n/a | { |
---|
4085 | n/a | mpd_context_t maxcontext; |
---|
4086 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
4087 | n/a | |
---|
4088 | n/a | mpd_maxcontext(&maxcontext); |
---|
4089 | n/a | mpd_qset_i64(&bb, b, &maxcontext, status); |
---|
4090 | n/a | mpd_qdiv(result, a, &bb, ctx, status); |
---|
4091 | n/a | mpd_del(&bb); |
---|
4092 | n/a | } |
---|
4093 | n/a | |
---|
4094 | n/a | /* Divide decimal by uint64_t. */ |
---|
4095 | n/a | void |
---|
4096 | n/a | mpd_qdiv_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
4097 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4098 | n/a | { |
---|
4099 | n/a | mpd_context_t maxcontext; |
---|
4100 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
4101 | n/a | |
---|
4102 | n/a | mpd_maxcontext(&maxcontext); |
---|
4103 | n/a | mpd_qset_u64(&bb, b, &maxcontext, status); |
---|
4104 | n/a | mpd_qdiv(result, a, &bb, ctx, status); |
---|
4105 | n/a | mpd_del(&bb); |
---|
4106 | n/a | } |
---|
4107 | n/a | #endif |
---|
4108 | n/a | |
---|
4109 | n/a | /* Pad the result with trailing zeros if it has fewer digits than prec. */ |
---|
4110 | n/a | static void |
---|
4111 | n/a | _mpd_zeropad(mpd_t *result, const mpd_context_t *ctx, uint32_t *status) |
---|
4112 | n/a | { |
---|
4113 | n/a | if (!mpd_isspecial(result) && !mpd_iszero(result) && |
---|
4114 | n/a | result->digits < ctx->prec) { |
---|
4115 | n/a | mpd_ssize_t shift = ctx->prec - result->digits; |
---|
4116 | n/a | mpd_qshiftl(result, result, shift, status); |
---|
4117 | n/a | result->exp -= shift; |
---|
4118 | n/a | } |
---|
4119 | n/a | } |
---|
4120 | n/a | |
---|
4121 | n/a | /* Check if the result is guaranteed to be one. */ |
---|
4122 | n/a | static int |
---|
4123 | n/a | _mpd_qexp_check_one(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4124 | n/a | uint32_t *status) |
---|
4125 | n/a | { |
---|
4126 | n/a | MPD_NEW_CONST(lim,0,-(ctx->prec+1),1,1,1,9); |
---|
4127 | n/a | MPD_NEW_SHARED(aa, a); |
---|
4128 | n/a | |
---|
4129 | n/a | mpd_set_positive(&aa); |
---|
4130 | n/a | |
---|
4131 | n/a | /* abs(a) <= 9 * 10**(-prec-1) */ |
---|
4132 | n/a | if (_mpd_cmp(&aa, &lim) <= 0) { |
---|
4133 | n/a | _settriple(result, 0, 1, 0); |
---|
4134 | n/a | *status |= MPD_Rounded|MPD_Inexact; |
---|
4135 | n/a | return 1; |
---|
4136 | n/a | } |
---|
4137 | n/a | |
---|
4138 | n/a | return 0; |
---|
4139 | n/a | } |
---|
4140 | n/a | |
---|
4141 | n/a | /* |
---|
4142 | n/a | * Get the number of iterations for the Horner scheme in _mpd_qexp(). |
---|
4143 | n/a | */ |
---|
4144 | n/a | static inline mpd_ssize_t |
---|
4145 | n/a | _mpd_get_exp_iterations(const mpd_t *r, mpd_ssize_t p) |
---|
4146 | n/a | { |
---|
4147 | n/a | mpd_ssize_t log10pbyr; /* lower bound for log10(p / abs(r)) */ |
---|
4148 | n/a | mpd_ssize_t n; |
---|
4149 | n/a | |
---|
4150 | n/a | assert(p >= 10); |
---|
4151 | n/a | assert(!mpd_iszero(r)); |
---|
4152 | n/a | assert(-p < mpd_adjexp(r) && mpd_adjexp(r) <= -1); |
---|
4153 | n/a | |
---|
4154 | n/a | #ifdef CONFIG_64 |
---|
4155 | n/a | if (p > (mpd_ssize_t)(1ULL<<52)) { |
---|
4156 | n/a | return MPD_SSIZE_MAX; |
---|
4157 | n/a | } |
---|
4158 | n/a | #endif |
---|
4159 | n/a | |
---|
4160 | n/a | /* |
---|
4161 | n/a | * Lower bound for log10(p / abs(r)): adjexp(p) - (adjexp(r) + 1) |
---|
4162 | n/a | * At this point (for CONFIG_64, CONFIG_32 is not problematic): |
---|
4163 | n/a | * 1) 10 <= p <= 2**52 |
---|
4164 | n/a | * 2) -p < adjexp(r) <= -1 |
---|
4165 | n/a | * 3) 1 <= log10pbyr <= 2**52 + 14 |
---|
4166 | n/a | */ |
---|
4167 | n/a | log10pbyr = (mpd_word_digits(p)-1) - (mpd_adjexp(r)+1); |
---|
4168 | n/a | |
---|
4169 | n/a | /* |
---|
4170 | n/a | * The numerator in the paper is 1.435 * p - 1.182, calculated |
---|
4171 | n/a | * exactly. We compensate for rounding errors by using 1.43503. |
---|
4172 | n/a | * ACL2 proofs: |
---|
4173 | n/a | * 1) exp-iter-approx-lower-bound: The term below evaluated |
---|
4174 | n/a | * in 53-bit floating point arithmetic is greater than or |
---|
4175 | n/a | * equal to the exact term used in the paper. |
---|
4176 | n/a | * 2) exp-iter-approx-upper-bound: The term below is less than |
---|
4177 | n/a | * or equal to 3/2 * p <= 3/2 * 2**52. |
---|
4178 | n/a | */ |
---|
4179 | n/a | n = (mpd_ssize_t)ceil((1.43503*(double)p - 1.182) / (double)log10pbyr); |
---|
4180 | n/a | return n >= 3 ? n : 3; |
---|
4181 | n/a | } |
---|
4182 | n/a | |
---|
4183 | n/a | /* |
---|
4184 | n/a | * Internal function, specials have been dealt with. Apart from Overflow |
---|
4185 | n/a | * and Underflow, two cases must be considered for the error of the result: |
---|
4186 | n/a | * |
---|
4187 | n/a | * 1) abs(a) <= 9 * 10**(-prec-1) ==> result == 1 |
---|
4188 | n/a | * |
---|
4189 | n/a | * Absolute error: abs(1 - e**x) < 10**(-prec) |
---|
4190 | n/a | * ------------------------------------------- |
---|
4191 | n/a | * |
---|
4192 | n/a | * 2) abs(a) > 9 * 10**(-prec-1) |
---|
4193 | n/a | * |
---|
4194 | n/a | * Relative error: abs(result - e**x) < 0.5 * 10**(-prec) * e**x |
---|
4195 | n/a | * ------------------------------------------------------------- |
---|
4196 | n/a | * |
---|
4197 | n/a | * The algorithm is from Hull&Abrham, Variable Precision Exponential Function, |
---|
4198 | n/a | * ACM Transactions on Mathematical Software, Vol. 12, No. 2, June 1986. |
---|
4199 | n/a | * |
---|
4200 | n/a | * Main differences: |
---|
4201 | n/a | * |
---|
4202 | n/a | * - The number of iterations for the Horner scheme is calculated using |
---|
4203 | n/a | * 53-bit floating point arithmetic. |
---|
4204 | n/a | * |
---|
4205 | n/a | * - In the error analysis for ER (relative error accumulated in the |
---|
4206 | n/a | * evaluation of the truncated series) the reduced operand r may |
---|
4207 | n/a | * have any number of digits. |
---|
4208 | n/a | * ACL2 proof: exponent-relative-error |
---|
4209 | n/a | * |
---|
4210 | n/a | * - The analysis for early abortion has been adapted for the mpd_t |
---|
4211 | n/a | * ranges. |
---|
4212 | n/a | */ |
---|
4213 | n/a | static void |
---|
4214 | n/a | _mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4215 | n/a | uint32_t *status) |
---|
4216 | n/a | { |
---|
4217 | n/a | mpd_context_t workctx; |
---|
4218 | n/a | MPD_NEW_STATIC(tmp,0,0,0,0); |
---|
4219 | n/a | MPD_NEW_STATIC(sum,0,0,0,0); |
---|
4220 | n/a | MPD_NEW_CONST(word,0,0,1,1,1,1); |
---|
4221 | n/a | mpd_ssize_t j, n, t; |
---|
4222 | n/a | |
---|
4223 | n/a | assert(!mpd_isspecial(a)); |
---|
4224 | n/a | |
---|
4225 | n/a | if (mpd_iszerocoeff(a)) { |
---|
4226 | n/a | _settriple(result, MPD_POS, 1, 0); |
---|
4227 | n/a | return; |
---|
4228 | n/a | } |
---|
4229 | n/a | |
---|
4230 | n/a | /* |
---|
4231 | n/a | * We are calculating e^x = e^(r*10^t) = (e^r)^(10^t), where abs(r) < 1 and t >= 0. |
---|
4232 | n/a | * |
---|
4233 | n/a | * If t > 0, we have: |
---|
4234 | n/a | * |
---|
4235 | n/a | * (1) 0.1 <= r < 1, so e^0.1 <= e^r. If t > MAX_T, overflow occurs: |
---|
4236 | n/a | * |
---|
4237 | n/a | * MAX-EMAX+1 < log10(e^(0.1*10*t)) <= log10(e^(r*10^t)) < adjexp(e^(r*10^t))+1 |
---|
4238 | n/a | * |
---|
4239 | n/a | * (2) -1 < r <= -0.1, so e^r <= e^-0.1. If t > MAX_T, underflow occurs: |
---|
4240 | n/a | * |
---|
4241 | n/a | * adjexp(e^(r*10^t)) <= log10(e^(r*10^t)) <= log10(e^(-0.1*10^t)) < MIN-ETINY |
---|
4242 | n/a | */ |
---|
4243 | n/a | #if defined(CONFIG_64) |
---|
4244 | n/a | #define MPD_EXP_MAX_T 19 |
---|
4245 | n/a | #elif defined(CONFIG_32) |
---|
4246 | n/a | #define MPD_EXP_MAX_T 10 |
---|
4247 | n/a | #endif |
---|
4248 | n/a | t = a->digits + a->exp; |
---|
4249 | n/a | t = (t > 0) ? t : 0; |
---|
4250 | n/a | if (t > MPD_EXP_MAX_T) { |
---|
4251 | n/a | if (mpd_ispositive(a)) { |
---|
4252 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
4253 | n/a | *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; |
---|
4254 | n/a | } |
---|
4255 | n/a | else { |
---|
4256 | n/a | _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); |
---|
4257 | n/a | *status |= (MPD_Inexact|MPD_Rounded|MPD_Subnormal| |
---|
4258 | n/a | MPD_Underflow|MPD_Clamped); |
---|
4259 | n/a | } |
---|
4260 | n/a | return; |
---|
4261 | n/a | } |
---|
4262 | n/a | |
---|
4263 | n/a | /* abs(a) <= 9 * 10**(-prec-1) */ |
---|
4264 | n/a | if (_mpd_qexp_check_one(result, a, ctx, status)) { |
---|
4265 | n/a | return; |
---|
4266 | n/a | } |
---|
4267 | n/a | |
---|
4268 | n/a | mpd_maxcontext(&workctx); |
---|
4269 | n/a | workctx.prec = ctx->prec + t + 2; |
---|
4270 | n/a | workctx.prec = (workctx.prec < 10) ? 10 : workctx.prec; |
---|
4271 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
4272 | n/a | |
---|
4273 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
4274 | n/a | return; |
---|
4275 | n/a | } |
---|
4276 | n/a | result->exp -= t; |
---|
4277 | n/a | |
---|
4278 | n/a | /* |
---|
4279 | n/a | * At this point: |
---|
4280 | n/a | * 1) 9 * 10**(-prec-1) < abs(a) |
---|
4281 | n/a | * 2) 9 * 10**(-prec-t-1) < abs(r) |
---|
4282 | n/a | * 3) log10(9) - prec - t - 1 < log10(abs(r)) < adjexp(abs(r)) + 1 |
---|
4283 | n/a | * 4) - prec - t - 2 < adjexp(abs(r)) <= -1 |
---|
4284 | n/a | */ |
---|
4285 | n/a | n = _mpd_get_exp_iterations(result, workctx.prec); |
---|
4286 | n/a | if (n == MPD_SSIZE_MAX) { |
---|
4287 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); /* GCOV_UNLIKELY */ |
---|
4288 | n/a | return; /* GCOV_UNLIKELY */ |
---|
4289 | n/a | } |
---|
4290 | n/a | |
---|
4291 | n/a | _settriple(&sum, MPD_POS, 1, 0); |
---|
4292 | n/a | |
---|
4293 | n/a | for (j = n-1; j >= 1; j--) { |
---|
4294 | n/a | word.data[0] = j; |
---|
4295 | n/a | mpd_setdigits(&word); |
---|
4296 | n/a | mpd_qdiv(&tmp, result, &word, &workctx, &workctx.status); |
---|
4297 | n/a | mpd_qfma(&sum, &sum, &tmp, &one, &workctx, &workctx.status); |
---|
4298 | n/a | } |
---|
4299 | n/a | |
---|
4300 | n/a | #ifdef CONFIG_64 |
---|
4301 | n/a | _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); |
---|
4302 | n/a | #else |
---|
4303 | n/a | if (t <= MPD_MAX_POW10) { |
---|
4304 | n/a | _mpd_qpow_uint(result, &sum, mpd_pow10[t], MPD_POS, &workctx, status); |
---|
4305 | n/a | } |
---|
4306 | n/a | else { |
---|
4307 | n/a | t -= MPD_MAX_POW10; |
---|
4308 | n/a | _mpd_qpow_uint(&tmp, &sum, mpd_pow10[MPD_MAX_POW10], MPD_POS, |
---|
4309 | n/a | &workctx, status); |
---|
4310 | n/a | _mpd_qpow_uint(result, &tmp, mpd_pow10[t], MPD_POS, &workctx, status); |
---|
4311 | n/a | } |
---|
4312 | n/a | #endif |
---|
4313 | n/a | |
---|
4314 | n/a | mpd_del(&tmp); |
---|
4315 | n/a | mpd_del(&sum); |
---|
4316 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
4317 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
4318 | n/a | } |
---|
4319 | n/a | |
---|
4320 | n/a | /* exp(a) */ |
---|
4321 | n/a | void |
---|
4322 | n/a | mpd_qexp(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4323 | n/a | uint32_t *status) |
---|
4324 | n/a | { |
---|
4325 | n/a | mpd_context_t workctx; |
---|
4326 | n/a | |
---|
4327 | n/a | if (mpd_isspecial(a)) { |
---|
4328 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
4329 | n/a | return; |
---|
4330 | n/a | } |
---|
4331 | n/a | if (mpd_isnegative(a)) { |
---|
4332 | n/a | _settriple(result, MPD_POS, 0, 0); |
---|
4333 | n/a | } |
---|
4334 | n/a | else { |
---|
4335 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
4336 | n/a | } |
---|
4337 | n/a | return; |
---|
4338 | n/a | } |
---|
4339 | n/a | if (mpd_iszerocoeff(a)) { |
---|
4340 | n/a | _settriple(result, MPD_POS, 1, 0); |
---|
4341 | n/a | return; |
---|
4342 | n/a | } |
---|
4343 | n/a | |
---|
4344 | n/a | workctx = *ctx; |
---|
4345 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
4346 | n/a | |
---|
4347 | n/a | if (ctx->allcr) { |
---|
4348 | n/a | MPD_NEW_STATIC(t1, 0,0,0,0); |
---|
4349 | n/a | MPD_NEW_STATIC(t2, 0,0,0,0); |
---|
4350 | n/a | MPD_NEW_STATIC(ulp, 0,0,0,0); |
---|
4351 | n/a | MPD_NEW_STATIC(aa, 0,0,0,0); |
---|
4352 | n/a | mpd_ssize_t prec; |
---|
4353 | n/a | mpd_ssize_t ulpexp; |
---|
4354 | n/a | uint32_t workstatus; |
---|
4355 | n/a | |
---|
4356 | n/a | if (result == a) { |
---|
4357 | n/a | if (!mpd_qcopy(&aa, a, status)) { |
---|
4358 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4359 | n/a | return; |
---|
4360 | n/a | } |
---|
4361 | n/a | a = &aa; |
---|
4362 | n/a | } |
---|
4363 | n/a | |
---|
4364 | n/a | workctx.clamp = 0; |
---|
4365 | n/a | prec = ctx->prec + 3; |
---|
4366 | n/a | while (1) { |
---|
4367 | n/a | workctx.prec = prec; |
---|
4368 | n/a | workstatus = 0; |
---|
4369 | n/a | |
---|
4370 | n/a | _mpd_qexp(result, a, &workctx, &workstatus); |
---|
4371 | n/a | *status |= workstatus; |
---|
4372 | n/a | |
---|
4373 | n/a | ulpexp = result->exp + result->digits - workctx.prec; |
---|
4374 | n/a | if (workstatus & MPD_Underflow) { |
---|
4375 | n/a | /* The effective work precision is result->digits. */ |
---|
4376 | n/a | ulpexp = result->exp; |
---|
4377 | n/a | } |
---|
4378 | n/a | _ssettriple(&ulp, MPD_POS, 1, ulpexp); |
---|
4379 | n/a | |
---|
4380 | n/a | /* |
---|
4381 | n/a | * At this point [1]: |
---|
4382 | n/a | * 1) abs(result - e**x) < 0.5 * 10**(-prec) * e**x |
---|
4383 | n/a | * 2) result - ulp < e**x < result + ulp |
---|
4384 | n/a | * 3) result - ulp < result < result + ulp |
---|
4385 | n/a | * |
---|
4386 | n/a | * If round(result-ulp)==round(result+ulp), then |
---|
4387 | n/a | * round(result)==round(e**x). Therefore the result |
---|
4388 | n/a | * is correctly rounded. |
---|
4389 | n/a | * |
---|
4390 | n/a | * [1] If abs(a) <= 9 * 10**(-prec-1), use the absolute |
---|
4391 | n/a | * error for a similar argument. |
---|
4392 | n/a | */ |
---|
4393 | n/a | workctx.prec = ctx->prec; |
---|
4394 | n/a | mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); |
---|
4395 | n/a | mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); |
---|
4396 | n/a | if (mpd_isspecial(result) || mpd_iszerocoeff(result) || |
---|
4397 | n/a | mpd_qcmp(&t1, &t2, status) == 0) { |
---|
4398 | n/a | workctx.clamp = ctx->clamp; |
---|
4399 | n/a | _mpd_zeropad(result, &workctx, status); |
---|
4400 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
4401 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
4402 | n/a | break; |
---|
4403 | n/a | } |
---|
4404 | n/a | prec += MPD_RDIGITS; |
---|
4405 | n/a | } |
---|
4406 | n/a | mpd_del(&t1); |
---|
4407 | n/a | mpd_del(&t2); |
---|
4408 | n/a | mpd_del(&ulp); |
---|
4409 | n/a | mpd_del(&aa); |
---|
4410 | n/a | } |
---|
4411 | n/a | else { |
---|
4412 | n/a | _mpd_qexp(result, a, &workctx, status); |
---|
4413 | n/a | _mpd_zeropad(result, &workctx, status); |
---|
4414 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
4415 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
4416 | n/a | } |
---|
4417 | n/a | } |
---|
4418 | n/a | |
---|
4419 | n/a | /* Fused multiply-add: (a * b) + c, with a single final rounding. */ |
---|
4420 | n/a | void |
---|
4421 | n/a | mpd_qfma(mpd_t *result, const mpd_t *a, const mpd_t *b, const mpd_t *c, |
---|
4422 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4423 | n/a | { |
---|
4424 | n/a | uint32_t workstatus = 0; |
---|
4425 | n/a | mpd_t *cc = NULL; |
---|
4426 | n/a | |
---|
4427 | n/a | if (result == c) { |
---|
4428 | n/a | if ((cc = mpd_qncopy(c)) == NULL) { |
---|
4429 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4430 | n/a | return; |
---|
4431 | n/a | } |
---|
4432 | n/a | c = cc; |
---|
4433 | n/a | } |
---|
4434 | n/a | |
---|
4435 | n/a | _mpd_qmul(result, a, b, ctx, &workstatus); |
---|
4436 | n/a | if (!(workstatus&MPD_Invalid_operation)) { |
---|
4437 | n/a | mpd_qadd(result, result, c, ctx, &workstatus); |
---|
4438 | n/a | } |
---|
4439 | n/a | |
---|
4440 | n/a | if (cc) mpd_del(cc); |
---|
4441 | n/a | *status |= workstatus; |
---|
4442 | n/a | } |
---|
4443 | n/a | |
---|
4444 | n/a | /* |
---|
4445 | n/a | * Schedule the optimal precision increase for the Newton iteration. |
---|
4446 | n/a | * v := input operand |
---|
4447 | n/a | * z_0 := initial approximation |
---|
4448 | n/a | * initprec := natural number such that abs(log(v) - z_0) < 10**-initprec |
---|
4449 | n/a | * maxprec := target precision |
---|
4450 | n/a | * |
---|
4451 | n/a | * For convenience the output klist contains the elements in reverse order: |
---|
4452 | n/a | * klist := [k_n-1, ..., k_0], where |
---|
4453 | n/a | * 1) k_0 <= initprec and |
---|
4454 | n/a | * 2) abs(log(v) - result) < 10**(-2*k_n-1 + 1) <= 10**-maxprec. |
---|
4455 | n/a | */ |
---|
4456 | n/a | static inline int |
---|
4457 | n/a | ln_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], mpd_ssize_t maxprec, |
---|
4458 | n/a | mpd_ssize_t initprec) |
---|
4459 | n/a | { |
---|
4460 | n/a | mpd_ssize_t k; |
---|
4461 | n/a | int i; |
---|
4462 | n/a | |
---|
4463 | n/a | assert(maxprec >= 2 && initprec >= 2); |
---|
4464 | n/a | if (maxprec <= initprec) return -1; |
---|
4465 | n/a | |
---|
4466 | n/a | i = 0; k = maxprec; |
---|
4467 | n/a | do { |
---|
4468 | n/a | k = (k+2) / 2; |
---|
4469 | n/a | klist[i++] = k; |
---|
4470 | n/a | } while (k > initprec); |
---|
4471 | n/a | |
---|
4472 | n/a | return i-1; |
---|
4473 | n/a | } |
---|
4474 | n/a | |
---|
4475 | n/a | /* The constants have been verified with both decimal.py and mpfr. */ |
---|
4476 | n/a | #ifdef CONFIG_64 |
---|
4477 | n/a | #if MPD_RDIGITS != 19 |
---|
4478 | n/a | #error "mpdecimal.c: MPD_RDIGITS must be 19." |
---|
4479 | n/a | #endif |
---|
4480 | n/a | static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { |
---|
4481 | n/a | 6983716328982174407ULL, 9089704281976336583ULL, 1515961135648465461ULL, |
---|
4482 | n/a | 4416816335727555703ULL, 2900988039194170265ULL, 2307925037472986509ULL, |
---|
4483 | n/a | 107598438319191292ULL, 3466624107184669231ULL, 4450099781311469159ULL, |
---|
4484 | n/a | 9807828059751193854ULL, 7713456862091670584ULL, 1492198849978748873ULL, |
---|
4485 | n/a | 6528728696511086257ULL, 2385392051446341972ULL, 8692180205189339507ULL, |
---|
4486 | n/a | 6518769751037497088ULL, 2375253577097505395ULL, 9095610299291824318ULL, |
---|
4487 | n/a | 982748238504564801ULL, 5438635917781170543ULL, 7547331541421808427ULL, |
---|
4488 | n/a | 752371033310119785ULL, 3171643095059950878ULL, 9785265383207606726ULL, |
---|
4489 | n/a | 2932258279850258550ULL, 5497347726624257094ULL, 2976979522110718264ULL, |
---|
4490 | n/a | 9221477656763693866ULL, 1979650047149510504ULL, 6674183485704422507ULL, |
---|
4491 | n/a | 9702766860595249671ULL, 9278096762712757753ULL, 9314848524948644871ULL, |
---|
4492 | n/a | 6826928280848118428ULL, 754403708474699401ULL, 230105703089634572ULL, |
---|
4493 | n/a | 1929203337658714166ULL, 7589402567763113569ULL, 4208241314695689016ULL, |
---|
4494 | n/a | 2922455440575892572ULL, 9356734206705811364ULL, 2684916746550586856ULL, |
---|
4495 | n/a | 644507064800027750ULL, 9476834636167921018ULL, 5659121373450747856ULL, |
---|
4496 | n/a | 2835522011480466371ULL, 6470806855677432162ULL, 7141748003688084012ULL, |
---|
4497 | n/a | 9619404400222105101ULL, 5504893431493939147ULL, 6674744042432743651ULL, |
---|
4498 | n/a | 2287698219886746543ULL, 7773262884616336622ULL, 1985283935053089653ULL, |
---|
4499 | n/a | 4680843799894826233ULL, 8168948290720832555ULL, 8067566662873690987ULL, |
---|
4500 | n/a | 6248633409525465082ULL, 9829834196778404228ULL, 3524802359972050895ULL, |
---|
4501 | n/a | 3327900967572609677ULL, 110148862877297603ULL, 179914546843642076ULL, |
---|
4502 | n/a | 2302585092994045684ULL |
---|
4503 | n/a | }; |
---|
4504 | n/a | #else |
---|
4505 | n/a | #if MPD_RDIGITS != 9 |
---|
4506 | n/a | #error "mpdecimal.c: MPD_RDIGITS must be 9." |
---|
4507 | n/a | #endif |
---|
4508 | n/a | static const mpd_uint_t mpd_ln10_data[MPD_MINALLOC_MAX] = { |
---|
4509 | n/a | 401682692UL, 708474699UL, 720754403UL, 30896345UL, 602301057UL, 765871416UL, |
---|
4510 | n/a | 192920333UL, 763113569UL, 589402567UL, 956890167UL, 82413146UL, 589257242UL, |
---|
4511 | n/a | 245544057UL, 811364292UL, 734206705UL, 868569356UL, 167465505UL, 775026849UL, |
---|
4512 | n/a | 706480002UL, 18064450UL, 636167921UL, 569476834UL, 734507478UL, 156591213UL, |
---|
4513 | n/a | 148046637UL, 283552201UL, 677432162UL, 470806855UL, 880840126UL, 417480036UL, |
---|
4514 | n/a | 210510171UL, 940440022UL, 939147961UL, 893431493UL, 436515504UL, 440424327UL, |
---|
4515 | n/a | 654366747UL, 821988674UL, 622228769UL, 884616336UL, 537773262UL, 350530896UL, |
---|
4516 | n/a | 319852839UL, 989482623UL, 468084379UL, 720832555UL, 168948290UL, 736909878UL, |
---|
4517 | n/a | 675666628UL, 546508280UL, 863340952UL, 404228624UL, 834196778UL, 508959829UL, |
---|
4518 | n/a | 23599720UL, 967735248UL, 96757260UL, 603332790UL, 862877297UL, 760110148UL, |
---|
4519 | n/a | 468436420UL, 401799145UL, 299404568UL, 230258509UL |
---|
4520 | n/a | }; |
---|
4521 | n/a | #endif |
---|
4522 | n/a | /* _mpd_ln10 is used directly for precisions smaller than MINALLOC_MAX*RDIGITS. |
---|
4523 | n/a | Otherwise, it serves as the initial approximation for calculating ln(10). */ |
---|
4524 | n/a | static const mpd_t _mpd_ln10 = { |
---|
4525 | n/a | MPD_STATIC|MPD_CONST_DATA, -(MPD_MINALLOC_MAX*MPD_RDIGITS-1), |
---|
4526 | n/a | MPD_MINALLOC_MAX*MPD_RDIGITS, MPD_MINALLOC_MAX, MPD_MINALLOC_MAX, |
---|
4527 | n/a | (mpd_uint_t *)mpd_ln10_data |
---|
4528 | n/a | }; |
---|
4529 | n/a | |
---|
4530 | n/a | /* |
---|
4531 | n/a | * Set 'result' to log(10). |
---|
4532 | n/a | * Ulp error: abs(result - log(10)) < ulp(log(10)) |
---|
4533 | n/a | * Relative error: abs(result - log(10)) < 5 * 10**-prec * log(10) |
---|
4534 | n/a | * |
---|
4535 | n/a | * NOTE: The relative error is not derived from the ulp error, but |
---|
4536 | n/a | * calculated separately using the fact that 23/10 < log(10) < 24/10. |
---|
4537 | n/a | */ |
---|
4538 | n/a | void |
---|
4539 | n/a | mpd_qln10(mpd_t *result, mpd_ssize_t prec, uint32_t *status) |
---|
4540 | n/a | { |
---|
4541 | n/a | mpd_context_t varcontext, maxcontext; |
---|
4542 | n/a | MPD_NEW_STATIC(tmp, 0,0,0,0); |
---|
4543 | n/a | MPD_NEW_CONST(static10, 0,0,2,1,1,10); |
---|
4544 | n/a | mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; |
---|
4545 | n/a | mpd_uint_t rnd; |
---|
4546 | n/a | mpd_ssize_t shift; |
---|
4547 | n/a | int i; |
---|
4548 | n/a | |
---|
4549 | n/a | assert(prec >= 1); |
---|
4550 | n/a | |
---|
4551 | n/a | shift = MPD_MINALLOC_MAX*MPD_RDIGITS-prec; |
---|
4552 | n/a | shift = shift < 0 ? 0 : shift; |
---|
4553 | n/a | |
---|
4554 | n/a | rnd = mpd_qshiftr(result, &_mpd_ln10, shift, status); |
---|
4555 | n/a | if (rnd == MPD_UINT_MAX) { |
---|
4556 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4557 | n/a | return; |
---|
4558 | n/a | } |
---|
4559 | n/a | result->exp = -(result->digits-1); |
---|
4560 | n/a | |
---|
4561 | n/a | mpd_maxcontext(&maxcontext); |
---|
4562 | n/a | if (prec < MPD_MINALLOC_MAX*MPD_RDIGITS) { |
---|
4563 | n/a | maxcontext.prec = prec; |
---|
4564 | n/a | _mpd_apply_round_excess(result, rnd, &maxcontext, status); |
---|
4565 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
4566 | n/a | return; |
---|
4567 | n/a | } |
---|
4568 | n/a | |
---|
4569 | n/a | mpd_maxcontext(&varcontext); |
---|
4570 | n/a | varcontext.round = MPD_ROUND_TRUNC; |
---|
4571 | n/a | |
---|
4572 | n/a | i = ln_schedule_prec(klist, prec+2, -result->exp); |
---|
4573 | n/a | for (; i >= 0; i--) { |
---|
4574 | n/a | varcontext.prec = 2*klist[i]+3; |
---|
4575 | n/a | result->flags ^= MPD_NEG; |
---|
4576 | n/a | _mpd_qexp(&tmp, result, &varcontext, status); |
---|
4577 | n/a | result->flags ^= MPD_NEG; |
---|
4578 | n/a | mpd_qmul(&tmp, &static10, &tmp, &varcontext, status); |
---|
4579 | n/a | mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); |
---|
4580 | n/a | mpd_qadd(result, result, &tmp, &maxcontext, status); |
---|
4581 | n/a | if (mpd_isspecial(result)) { |
---|
4582 | n/a | break; |
---|
4583 | n/a | } |
---|
4584 | n/a | } |
---|
4585 | n/a | |
---|
4586 | n/a | mpd_del(&tmp); |
---|
4587 | n/a | maxcontext.prec = prec; |
---|
4588 | n/a | mpd_qfinalize(result, &maxcontext, status); |
---|
4589 | n/a | } |
---|
4590 | n/a | |
---|
4591 | n/a | /* |
---|
4592 | n/a | * Initial approximations for the ln() iteration. The values have the |
---|
4593 | n/a | * following properties (established with both decimal.py and mpfr): |
---|
4594 | n/a | * |
---|
4595 | n/a | * Index 0 - 400, logarithms of x in [1.00, 5.00]: |
---|
4596 | n/a | * abs(lnapprox[i] * 10**-3 - log((i+100)/100)) < 10**-2 |
---|
4597 | n/a | * abs(lnapprox[i] * 10**-3 - log((i+1+100)/100)) < 10**-2 |
---|
4598 | n/a | * |
---|
4599 | n/a | * Index 401 - 899, logarithms of x in (0.500, 0.999]: |
---|
4600 | n/a | * abs(-lnapprox[i] * 10**-3 - log((i+100)/1000)) < 10**-2 |
---|
4601 | n/a | * abs(-lnapprox[i] * 10**-3 - log((i+1+100)/1000)) < 10**-2 |
---|
4602 | n/a | */ |
---|
4603 | n/a | static const uint16_t lnapprox[900] = { |
---|
4604 | n/a | /* index 0 - 400: log((i+100)/100) * 1000 */ |
---|
4605 | n/a | 0, 10, 20, 30, 39, 49, 58, 68, 77, 86, 95, 104, 113, 122, 131, 140, 148, 157, |
---|
4606 | n/a | 166, 174, 182, 191, 199, 207, 215, 223, 231, 239, 247, 255, 262, 270, 278, |
---|
4607 | n/a | 285, 293, 300, 308, 315, 322, 329, 336, 344, 351, 358, 365, 372, 378, 385, |
---|
4608 | n/a | 392, 399, 406, 412, 419, 425, 432, 438, 445, 451, 457, 464, 470, 476, 482, |
---|
4609 | n/a | 489, 495, 501, 507, 513, 519, 525, 531, 536, 542, 548, 554, 560, 565, 571, |
---|
4610 | n/a | 577, 582, 588, 593, 599, 604, 610, 615, 621, 626, 631, 637, 642, 647, 652, |
---|
4611 | n/a | 658, 663, 668, 673, 678, 683, 688, 693, 698, 703, 708, 713, 718, 723, 728, |
---|
4612 | n/a | 732, 737, 742, 747, 751, 756, 761, 766, 770, 775, 779, 784, 788, 793, 798, |
---|
4613 | n/a | 802, 806, 811, 815, 820, 824, 829, 833, 837, 842, 846, 850, 854, 859, 863, |
---|
4614 | n/a | 867, 871, 876, 880, 884, 888, 892, 896, 900, 904, 908, 912, 916, 920, 924, |
---|
4615 | n/a | 928, 932, 936, 940, 944, 948, 952, 956, 959, 963, 967, 971, 975, 978, 982, |
---|
4616 | n/a | 986, 990, 993, 997, 1001, 1004, 1008, 1012, 1015, 1019, 1022, 1026, 1030, |
---|
4617 | n/a | 1033, 1037, 1040, 1044, 1047, 1051, 1054, 1058, 1061, 1065, 1068, 1072, 1075, |
---|
4618 | n/a | 1078, 1082, 1085, 1089, 1092, 1095, 1099, 1102, 1105, 1109, 1112, 1115, 1118, |
---|
4619 | n/a | 1122, 1125, 1128, 1131, 1135, 1138, 1141, 1144, 1147, 1151, 1154, 1157, 1160, |
---|
4620 | n/a | 1163, 1166, 1169, 1172, 1176, 1179, 1182, 1185, 1188, 1191, 1194, 1197, 1200, |
---|
4621 | n/a | 1203, 1206, 1209, 1212, 1215, 1218, 1221, 1224, 1227, 1230, 1233, 1235, 1238, |
---|
4622 | n/a | 1241, 1244, 1247, 1250, 1253, 1256, 1258, 1261, 1264, 1267, 1270, 1273, 1275, |
---|
4623 | n/a | 1278, 1281, 1284, 1286, 1289, 1292, 1295, 1297, 1300, 1303, 1306, 1308, 1311, |
---|
4624 | n/a | 1314, 1316, 1319, 1322, 1324, 1327, 1330, 1332, 1335, 1338, 1340, 1343, 1345, |
---|
4625 | n/a | 1348, 1351, 1353, 1356, 1358, 1361, 1364, 1366, 1369, 1371, 1374, 1376, 1379, |
---|
4626 | n/a | 1381, 1384, 1386, 1389, 1391, 1394, 1396, 1399, 1401, 1404, 1406, 1409, 1411, |
---|
4627 | n/a | 1413, 1416, 1418, 1421, 1423, 1426, 1428, 1430, 1433, 1435, 1437, 1440, 1442, |
---|
4628 | n/a | 1445, 1447, 1449, 1452, 1454, 1456, 1459, 1461, 1463, 1466, 1468, 1470, 1472, |
---|
4629 | n/a | 1475, 1477, 1479, 1482, 1484, 1486, 1488, 1491, 1493, 1495, 1497, 1500, 1502, |
---|
4630 | n/a | 1504, 1506, 1509, 1511, 1513, 1515, 1517, 1520, 1522, 1524, 1526, 1528, 1530, |
---|
4631 | n/a | 1533, 1535, 1537, 1539, 1541, 1543, 1545, 1548, 1550, 1552, 1554, 1556, 1558, |
---|
4632 | n/a | 1560, 1562, 1564, 1567, 1569, 1571, 1573, 1575, 1577, 1579, 1581, 1583, 1585, |
---|
4633 | n/a | 1587, 1589, 1591, 1593, 1595, 1597, 1599, 1601, 1603, 1605, 1607, 1609, |
---|
4634 | n/a | /* index 401 - 899: -log((i+100)/1000) * 1000 */ |
---|
4635 | n/a | 691, 689, 687, 685, 683, 681, 679, 677, 675, 673, 671, 669, 668, 666, 664, |
---|
4636 | n/a | 662, 660, 658, 656, 654, 652, 650, 648, 646, 644, 642, 641, 639, 637, 635, |
---|
4637 | n/a | 633, 631, 629, 627, 626, 624, 622, 620, 618, 616, 614, 612, 611, 609, 607, |
---|
4638 | n/a | 605, 603, 602, 600, 598, 596, 594, 592, 591, 589, 587, 585, 583, 582, 580, |
---|
4639 | n/a | 578, 576, 574, 573, 571, 569, 567, 566, 564, 562, 560, 559, 557, 555, 553, |
---|
4640 | n/a | 552, 550, 548, 546, 545, 543, 541, 540, 538, 536, 534, 533, 531, 529, 528, |
---|
4641 | n/a | 526, 524, 523, 521, 519, 518, 516, 514, 512, 511, 509, 508, 506, 504, 502, |
---|
4642 | n/a | 501, 499, 498, 496, 494, 493, 491, 489, 488, 486, 484, 483, 481, 480, 478, |
---|
4643 | n/a | 476, 475, 473, 472, 470, 468, 467, 465, 464, 462, 460, 459, 457, 456, 454, |
---|
4644 | n/a | 453, 451, 449, 448, 446, 445, 443, 442, 440, 438, 437, 435, 434, 432, 431, |
---|
4645 | n/a | 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 410, 408, |
---|
4646 | n/a | 406, 405, 404, 402, 400, 399, 398, 396, 394, 393, 392, 390, 389, 387, 386, |
---|
4647 | n/a | 384, 383, 381, 380, 378, 377, 375, 374, 372, 371, 370, 368, 367, 365, 364, |
---|
4648 | n/a | 362, 361, 360, 358, 357, 355, 354, 352, 351, 350, 348, 347, 345, 344, 342, |
---|
4649 | n/a | 341, 340, 338, 337, 336, 334, 333, 331, 330, 328, 327, 326, 324, 323, 322, |
---|
4650 | n/a | 320, 319, 318, 316, 315, 313, 312, 311, 309, 308, 306, 305, 304, 302, 301, |
---|
4651 | n/a | 300, 298, 297, 296, 294, 293, 292, 290, 289, 288, 286, 285, 284, 282, 281, |
---|
4652 | n/a | 280, 278, 277, 276, 274, 273, 272, 270, 269, 268, 267, 265, 264, 263, 261, |
---|
4653 | n/a | 260, 259, 258, 256, 255, 254, 252, 251, 250, 248, 247, 246, 245, 243, 242, |
---|
4654 | n/a | 241, 240, 238, 237, 236, 234, 233, 232, 231, 229, 228, 227, 226, 224, 223, |
---|
4655 | n/a | 222, 221, 219, 218, 217, 216, 214, 213, 212, 211, 210, 208, 207, 206, 205, |
---|
4656 | n/a | 203, 202, 201, 200, 198, 197, 196, 195, 194, 192, 191, 190, 189, 188, 186, |
---|
4657 | n/a | 185, 184, 183, 182, 180, 179, 178, 177, 176, 174, 173, 172, 171, 170, 168, |
---|
4658 | n/a | 167, 166, 165, 164, 162, 161, 160, 159, 158, 157, 156, 154, 153, 152, 151, |
---|
4659 | n/a | 150, 148, 147, 146, 145, 144, 143, 142, 140, 139, 138, 137, 136, 135, 134, |
---|
4660 | n/a | 132, 131, 130, 129, 128, 127, 126, 124, 123, 122, 121, 120, 119, 118, 116, |
---|
4661 | n/a | 115, 114, 113, 112, 111, 110, 109, 108, 106, 105, 104, 103, 102, 101, 100, |
---|
4662 | n/a | 99, 98, 97, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79, |
---|
4663 | n/a | 78, 77, 76, 75, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, |
---|
4664 | n/a | 58, 57, 56, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, |
---|
4665 | n/a | 38, 37, 36, 35, 34, 33, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, |
---|
4666 | n/a | 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 |
---|
4667 | n/a | }; |
---|
4668 | n/a | |
---|
4669 | n/a | /* |
---|
4670 | n/a | * Internal ln() function that does not check for specials, zero or one. |
---|
4671 | n/a | * Relative error: abs(result - log(a)) < 0.1 * 10**-prec * abs(log(a)) |
---|
4672 | n/a | */ |
---|
4673 | n/a | static void |
---|
4674 | n/a | _mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4675 | n/a | uint32_t *status) |
---|
4676 | n/a | { |
---|
4677 | n/a | mpd_context_t varcontext, maxcontext; |
---|
4678 | n/a | mpd_t *z = (mpd_t *) result; |
---|
4679 | n/a | MPD_NEW_STATIC(v,0,0,0,0); |
---|
4680 | n/a | MPD_NEW_STATIC(vtmp,0,0,0,0); |
---|
4681 | n/a | MPD_NEW_STATIC(tmp,0,0,0,0); |
---|
4682 | n/a | mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; |
---|
4683 | n/a | mpd_ssize_t maxprec, shift, t; |
---|
4684 | n/a | mpd_ssize_t a_digits, a_exp; |
---|
4685 | n/a | mpd_uint_t dummy, x; |
---|
4686 | n/a | int i; |
---|
4687 | n/a | |
---|
4688 | n/a | assert(!mpd_isspecial(a) && !mpd_iszerocoeff(a)); |
---|
4689 | n/a | |
---|
4690 | n/a | /* |
---|
4691 | n/a | * We are calculating ln(a) = ln(v * 10^t) = ln(v) + t*ln(10), |
---|
4692 | n/a | * where 0.5 < v <= 5. |
---|
4693 | n/a | */ |
---|
4694 | n/a | if (!mpd_qcopy(&v, a, status)) { |
---|
4695 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4696 | n/a | goto finish; |
---|
4697 | n/a | } |
---|
4698 | n/a | |
---|
4699 | n/a | /* Initial approximation: we have at least one non-zero digit */ |
---|
4700 | n/a | _mpd_get_msdigits(&dummy, &x, &v, 3); |
---|
4701 | n/a | if (x < 10) x *= 10; |
---|
4702 | n/a | if (x < 100) x *= 10; |
---|
4703 | n/a | x -= 100; |
---|
4704 | n/a | |
---|
4705 | n/a | /* a may equal z */ |
---|
4706 | n/a | a_digits = a->digits; |
---|
4707 | n/a | a_exp = a->exp; |
---|
4708 | n/a | |
---|
4709 | n/a | mpd_minalloc(z); |
---|
4710 | n/a | mpd_clear_flags(z); |
---|
4711 | n/a | z->data[0] = lnapprox[x]; |
---|
4712 | n/a | z->len = 1; |
---|
4713 | n/a | z->exp = -3; |
---|
4714 | n/a | mpd_setdigits(z); |
---|
4715 | n/a | |
---|
4716 | n/a | if (x <= 400) { |
---|
4717 | n/a | /* Reduce the input operand to 1.00 <= v <= 5.00. Let y = x + 100, |
---|
4718 | n/a | * so 100 <= y <= 500. Since y contains the most significant digits |
---|
4719 | n/a | * of v, y/100 <= v < (y+1)/100 and abs(z - log(v)) < 10**-2. */ |
---|
4720 | n/a | v.exp = -(a_digits - 1); |
---|
4721 | n/a | t = a_exp + a_digits - 1; |
---|
4722 | n/a | } |
---|
4723 | n/a | else { |
---|
4724 | n/a | /* Reduce the input operand to 0.500 < v <= 0.999. Let y = x + 100, |
---|
4725 | n/a | * so 500 < y <= 999. Since y contains the most significant digits |
---|
4726 | n/a | * of v, y/1000 <= v < (y+1)/1000 and abs(z - log(v)) < 10**-2. */ |
---|
4727 | n/a | v.exp = -a_digits; |
---|
4728 | n/a | t = a_exp + a_digits; |
---|
4729 | n/a | mpd_set_negative(z); |
---|
4730 | n/a | } |
---|
4731 | n/a | |
---|
4732 | n/a | mpd_maxcontext(&maxcontext); |
---|
4733 | n/a | mpd_maxcontext(&varcontext); |
---|
4734 | n/a | varcontext.round = MPD_ROUND_TRUNC; |
---|
4735 | n/a | |
---|
4736 | n/a | maxprec = ctx->prec + 2; |
---|
4737 | n/a | if (t == 0 && (x <= 15 || x >= 800)) { |
---|
4738 | n/a | /* 0.900 <= v <= 1.15: Estimate the magnitude of the logarithm. |
---|
4739 | n/a | * If ln(v) will underflow, skip the loop. Otherwise, adjust the |
---|
4740 | n/a | * precision upwards in order to obtain a sufficient number of |
---|
4741 | n/a | * significant digits. |
---|
4742 | n/a | * |
---|
4743 | n/a | * Case v > 1: |
---|
4744 | n/a | * abs((v-1)/10) < abs((v-1)/v) < abs(ln(v)) < abs(v-1) |
---|
4745 | n/a | * Case v < 1: |
---|
4746 | n/a | * abs(v-1) < abs(ln(v)) < abs((v-1)/v) < abs((v-1)*10) |
---|
4747 | n/a | */ |
---|
4748 | n/a | int cmp = _mpd_cmp(&v, &one); |
---|
4749 | n/a | |
---|
4750 | n/a | /* Upper bound (assume v > 1): abs(v-1), unrounded */ |
---|
4751 | n/a | _mpd_qsub(&tmp, &v, &one, &maxcontext, &maxcontext.status); |
---|
4752 | n/a | if (maxcontext.status & MPD_Errors) { |
---|
4753 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4754 | n/a | goto finish; |
---|
4755 | n/a | } |
---|
4756 | n/a | |
---|
4757 | n/a | if (cmp < 0) { |
---|
4758 | n/a | /* v < 1: abs((v-1)*10) */ |
---|
4759 | n/a | tmp.exp += 1; |
---|
4760 | n/a | } |
---|
4761 | n/a | if (mpd_adjexp(&tmp) < mpd_etiny(ctx)) { |
---|
4762 | n/a | /* The upper bound is less than etiny: Underflow to zero */ |
---|
4763 | n/a | _settriple(result, (cmp<0), 1, mpd_etiny(ctx)-1); |
---|
4764 | n/a | goto finish; |
---|
4765 | n/a | } |
---|
4766 | n/a | /* Lower bound: abs((v-1)/10) or abs(v-1) */ |
---|
4767 | n/a | tmp.exp -= 1; |
---|
4768 | n/a | if (mpd_adjexp(&tmp) < 0) { |
---|
4769 | n/a | /* Absolute error of the loop: abs(z - log(v)) < 10**-p. If |
---|
4770 | n/a | * p = ctx->prec+2-adjexp(lower), then the relative error of |
---|
4771 | n/a | * the result is (using 10**adjexp(x) <= abs(x)): |
---|
4772 | n/a | * |
---|
4773 | n/a | * abs(z - log(v)) / abs(log(v)) < 10**-p / abs(log(v)) |
---|
4774 | n/a | * <= 10**(-ctx->prec-2) |
---|
4775 | n/a | */ |
---|
4776 | n/a | maxprec = maxprec - mpd_adjexp(&tmp); |
---|
4777 | n/a | } |
---|
4778 | n/a | } |
---|
4779 | n/a | |
---|
4780 | n/a | i = ln_schedule_prec(klist, maxprec, 2); |
---|
4781 | n/a | for (; i >= 0; i--) { |
---|
4782 | n/a | varcontext.prec = 2*klist[i]+3; |
---|
4783 | n/a | z->flags ^= MPD_NEG; |
---|
4784 | n/a | _mpd_qexp(&tmp, z, &varcontext, status); |
---|
4785 | n/a | z->flags ^= MPD_NEG; |
---|
4786 | n/a | |
---|
4787 | n/a | if (v.digits > varcontext.prec) { |
---|
4788 | n/a | shift = v.digits - varcontext.prec; |
---|
4789 | n/a | mpd_qshiftr(&vtmp, &v, shift, status); |
---|
4790 | n/a | vtmp.exp += shift; |
---|
4791 | n/a | mpd_qmul(&tmp, &vtmp, &tmp, &varcontext, status); |
---|
4792 | n/a | } |
---|
4793 | n/a | else { |
---|
4794 | n/a | mpd_qmul(&tmp, &v, &tmp, &varcontext, status); |
---|
4795 | n/a | } |
---|
4796 | n/a | |
---|
4797 | n/a | mpd_qsub(&tmp, &tmp, &one, &maxcontext, status); |
---|
4798 | n/a | mpd_qadd(z, z, &tmp, &maxcontext, status); |
---|
4799 | n/a | if (mpd_isspecial(z)) { |
---|
4800 | n/a | break; |
---|
4801 | n/a | } |
---|
4802 | n/a | } |
---|
4803 | n/a | |
---|
4804 | n/a | /* |
---|
4805 | n/a | * Case t == 0: |
---|
4806 | n/a | * t * log(10) == 0, the result does not change and the analysis |
---|
4807 | n/a | * above applies. If v < 0.900 or v > 1.15, the relative error is |
---|
4808 | n/a | * less than 10**(-ctx.prec-1). |
---|
4809 | n/a | * Case t != 0: |
---|
4810 | n/a | * z := approx(log(v)) |
---|
4811 | n/a | * y := approx(log(10)) |
---|
4812 | n/a | * p := maxprec = ctx->prec + 2 |
---|
4813 | n/a | * Absolute errors: |
---|
4814 | n/a | * 1) abs(z - log(v)) < 10**-p |
---|
4815 | n/a | * 2) abs(y - log(10)) < 10**-p |
---|
4816 | n/a | * The multiplication is exact, so: |
---|
4817 | n/a | * 3) abs(t*y - t*log(10)) < t*10**-p |
---|
4818 | n/a | * The sum is exact, so: |
---|
4819 | n/a | * 4) abs((z + t*y) - (log(v) + t*log(10))) < (abs(t) + 1) * 10**-p |
---|
4820 | n/a | * Bounds for log(v) and log(10): |
---|
4821 | n/a | * 5) -7/10 < log(v) < 17/10 |
---|
4822 | n/a | * 6) 23/10 < log(10) < 24/10 |
---|
4823 | n/a | * Using 4), 5), 6) and t != 0, the relative error is: |
---|
4824 | n/a | * |
---|
4825 | n/a | * 7) relerr < ((abs(t) + 1)*10**-p) / abs(log(v) + t*log(10)) |
---|
4826 | n/a | * < 0.5 * 10**(-p + 1) = 0.5 * 10**(-ctx->prec-1) |
---|
4827 | n/a | */ |
---|
4828 | n/a | mpd_qln10(&v, maxprec+1, status); |
---|
4829 | n/a | mpd_qmul_ssize(&tmp, &v, t, &maxcontext, status); |
---|
4830 | n/a | mpd_qadd(result, &tmp, z, &maxcontext, status); |
---|
4831 | n/a | |
---|
4832 | n/a | |
---|
4833 | n/a | finish: |
---|
4834 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
4835 | n/a | mpd_del(&v); |
---|
4836 | n/a | mpd_del(&vtmp); |
---|
4837 | n/a | mpd_del(&tmp); |
---|
4838 | n/a | } |
---|
4839 | n/a | |
---|
4840 | n/a | /* ln(a) */ |
---|
4841 | n/a | void |
---|
4842 | n/a | mpd_qln(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4843 | n/a | uint32_t *status) |
---|
4844 | n/a | { |
---|
4845 | n/a | mpd_context_t workctx; |
---|
4846 | n/a | mpd_ssize_t adjexp, t; |
---|
4847 | n/a | |
---|
4848 | n/a | if (mpd_isspecial(a)) { |
---|
4849 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
4850 | n/a | return; |
---|
4851 | n/a | } |
---|
4852 | n/a | if (mpd_isnegative(a)) { |
---|
4853 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
4854 | n/a | return; |
---|
4855 | n/a | } |
---|
4856 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
4857 | n/a | return; |
---|
4858 | n/a | } |
---|
4859 | n/a | if (mpd_iszerocoeff(a)) { |
---|
4860 | n/a | mpd_setspecial(result, MPD_NEG, MPD_INF); |
---|
4861 | n/a | return; |
---|
4862 | n/a | } |
---|
4863 | n/a | if (mpd_isnegative(a)) { |
---|
4864 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
4865 | n/a | return; |
---|
4866 | n/a | } |
---|
4867 | n/a | if (_mpd_cmp(a, &one) == 0) { |
---|
4868 | n/a | _settriple(result, MPD_POS, 0, 0); |
---|
4869 | n/a | return; |
---|
4870 | n/a | } |
---|
4871 | n/a | /* |
---|
4872 | n/a | * Check if the result will overflow (0 < x, x != 1): |
---|
4873 | n/a | * 1) log10(x) < 0 iff adjexp(x) < 0 |
---|
4874 | n/a | * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) |
---|
4875 | n/a | * 3) 0 < x /\ x != 1 ==> 2 * abs(log10(x)) < abs(log(x)) |
---|
4876 | n/a | * 4) adjexp(x) <= log10(x) < adjexp(x) + 1 |
---|
4877 | n/a | * |
---|
4878 | n/a | * Case adjexp(x) >= 0: |
---|
4879 | n/a | * 5) 2 * adjexp(x) < abs(log(x)) |
---|
4880 | n/a | * Case adjexp(x) > 0: |
---|
4881 | n/a | * 6) adjexp(2 * adjexp(x)) <= adjexp(abs(log(x))) |
---|
4882 | n/a | * Case adjexp(x) == 0: |
---|
4883 | n/a | * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) |
---|
4884 | n/a | * |
---|
4885 | n/a | * Case adjexp(x) < 0: |
---|
4886 | n/a | * 7) 2 * (-adjexp(x) - 1) < abs(log(x)) |
---|
4887 | n/a | * Case adjexp(x) < -1: |
---|
4888 | n/a | * 8) adjexp(2 * (-adjexp(x) - 1)) <= adjexp(abs(log(x))) |
---|
4889 | n/a | * Case adjexp(x) == -1: |
---|
4890 | n/a | * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) |
---|
4891 | n/a | */ |
---|
4892 | n/a | adjexp = mpd_adjexp(a); |
---|
4893 | n/a | t = (adjexp < 0) ? -adjexp-1 : adjexp; |
---|
4894 | n/a | t *= 2; |
---|
4895 | n/a | if (mpd_exp_digits(t)-1 > ctx->emax) { |
---|
4896 | n/a | *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; |
---|
4897 | n/a | mpd_setspecial(result, (adjexp<0), MPD_INF); |
---|
4898 | n/a | return; |
---|
4899 | n/a | } |
---|
4900 | n/a | |
---|
4901 | n/a | workctx = *ctx; |
---|
4902 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
4903 | n/a | |
---|
4904 | n/a | if (ctx->allcr) { |
---|
4905 | n/a | MPD_NEW_STATIC(t1, 0,0,0,0); |
---|
4906 | n/a | MPD_NEW_STATIC(t2, 0,0,0,0); |
---|
4907 | n/a | MPD_NEW_STATIC(ulp, 0,0,0,0); |
---|
4908 | n/a | MPD_NEW_STATIC(aa, 0,0,0,0); |
---|
4909 | n/a | mpd_ssize_t prec; |
---|
4910 | n/a | |
---|
4911 | n/a | if (result == a) { |
---|
4912 | n/a | if (!mpd_qcopy(&aa, a, status)) { |
---|
4913 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
4914 | n/a | return; |
---|
4915 | n/a | } |
---|
4916 | n/a | a = &aa; |
---|
4917 | n/a | } |
---|
4918 | n/a | |
---|
4919 | n/a | workctx.clamp = 0; |
---|
4920 | n/a | prec = ctx->prec + 3; |
---|
4921 | n/a | while (1) { |
---|
4922 | n/a | workctx.prec = prec; |
---|
4923 | n/a | _mpd_qln(result, a, &workctx, status); |
---|
4924 | n/a | _ssettriple(&ulp, MPD_POS, 1, |
---|
4925 | n/a | result->exp + result->digits-workctx.prec); |
---|
4926 | n/a | |
---|
4927 | n/a | workctx.prec = ctx->prec; |
---|
4928 | n/a | mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); |
---|
4929 | n/a | mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); |
---|
4930 | n/a | if (mpd_isspecial(result) || mpd_iszerocoeff(result) || |
---|
4931 | n/a | mpd_qcmp(&t1, &t2, status) == 0) { |
---|
4932 | n/a | workctx.clamp = ctx->clamp; |
---|
4933 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
4934 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
4935 | n/a | break; |
---|
4936 | n/a | } |
---|
4937 | n/a | prec += MPD_RDIGITS; |
---|
4938 | n/a | } |
---|
4939 | n/a | mpd_del(&t1); |
---|
4940 | n/a | mpd_del(&t2); |
---|
4941 | n/a | mpd_del(&ulp); |
---|
4942 | n/a | mpd_del(&aa); |
---|
4943 | n/a | } |
---|
4944 | n/a | else { |
---|
4945 | n/a | _mpd_qln(result, a, &workctx, status); |
---|
4946 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
4947 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
4948 | n/a | } |
---|
4949 | n/a | } |
---|
4950 | n/a | |
---|
4951 | n/a | /* |
---|
4952 | n/a | * Internal log10() function that does not check for specials, zero or one. |
---|
4953 | n/a | * Case SKIP_FINALIZE: |
---|
4954 | n/a | * Relative error: abs(result - log10(a)) < 0.1 * 10**-prec * abs(log10(a)) |
---|
4955 | n/a | * Case DO_FINALIZE: |
---|
4956 | n/a | * Ulp error: abs(result - log10(a)) < ulp(log10(a)) |
---|
4957 | n/a | */ |
---|
4958 | n/a | enum {SKIP_FINALIZE, DO_FINALIZE}; |
---|
4959 | n/a | static void |
---|
4960 | n/a | _mpd_qlog10(int action, mpd_t *result, const mpd_t *a, |
---|
4961 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
4962 | n/a | { |
---|
4963 | n/a | mpd_context_t workctx; |
---|
4964 | n/a | MPD_NEW_STATIC(ln10,0,0,0,0); |
---|
4965 | n/a | |
---|
4966 | n/a | mpd_maxcontext(&workctx); |
---|
4967 | n/a | workctx.prec = ctx->prec + 3; |
---|
4968 | n/a | /* relative error: 0.1 * 10**(-p-3). The specific underflow shortcut |
---|
4969 | n/a | * in _mpd_qln() does not change the final result. */ |
---|
4970 | n/a | _mpd_qln(result, a, &workctx, status); |
---|
4971 | n/a | /* relative error: 5 * 10**(-p-3) */ |
---|
4972 | n/a | mpd_qln10(&ln10, workctx.prec, status); |
---|
4973 | n/a | |
---|
4974 | n/a | if (action == DO_FINALIZE) { |
---|
4975 | n/a | workctx = *ctx; |
---|
4976 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
4977 | n/a | } |
---|
4978 | n/a | /* SKIP_FINALIZE: relative error: 5 * 10**(-p-3) */ |
---|
4979 | n/a | _mpd_qdiv(NO_IDEAL_EXP, result, result, &ln10, &workctx, status); |
---|
4980 | n/a | |
---|
4981 | n/a | mpd_del(&ln10); |
---|
4982 | n/a | } |
---|
4983 | n/a | |
---|
4984 | n/a | /* log10(a) */ |
---|
4985 | n/a | void |
---|
4986 | n/a | mpd_qlog10(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
4987 | n/a | uint32_t *status) |
---|
4988 | n/a | { |
---|
4989 | n/a | mpd_context_t workctx; |
---|
4990 | n/a | mpd_ssize_t adjexp, t; |
---|
4991 | n/a | |
---|
4992 | n/a | workctx = *ctx; |
---|
4993 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
4994 | n/a | |
---|
4995 | n/a | if (mpd_isspecial(a)) { |
---|
4996 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
4997 | n/a | return; |
---|
4998 | n/a | } |
---|
4999 | n/a | if (mpd_isnegative(a)) { |
---|
5000 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
5001 | n/a | return; |
---|
5002 | n/a | } |
---|
5003 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
5004 | n/a | return; |
---|
5005 | n/a | } |
---|
5006 | n/a | if (mpd_iszerocoeff(a)) { |
---|
5007 | n/a | mpd_setspecial(result, MPD_NEG, MPD_INF); |
---|
5008 | n/a | return; |
---|
5009 | n/a | } |
---|
5010 | n/a | if (mpd_isnegative(a)) { |
---|
5011 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
5012 | n/a | return; |
---|
5013 | n/a | } |
---|
5014 | n/a | if (mpd_coeff_ispow10(a)) { |
---|
5015 | n/a | uint8_t sign = 0; |
---|
5016 | n/a | adjexp = mpd_adjexp(a); |
---|
5017 | n/a | if (adjexp < 0) { |
---|
5018 | n/a | sign = 1; |
---|
5019 | n/a | adjexp = -adjexp; |
---|
5020 | n/a | } |
---|
5021 | n/a | _settriple(result, sign, adjexp, 0); |
---|
5022 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
5023 | n/a | return; |
---|
5024 | n/a | } |
---|
5025 | n/a | /* |
---|
5026 | n/a | * Check if the result will overflow (0 < x, x != 1): |
---|
5027 | n/a | * 1) log10(x) < 0 iff adjexp(x) < 0 |
---|
5028 | n/a | * 2) 0 < x /\ x <= y ==> adjexp(x) <= adjexp(y) |
---|
5029 | n/a | * 3) adjexp(x) <= log10(x) < adjexp(x) + 1 |
---|
5030 | n/a | * |
---|
5031 | n/a | * Case adjexp(x) >= 0: |
---|
5032 | n/a | * 4) adjexp(x) <= abs(log10(x)) |
---|
5033 | n/a | * Case adjexp(x) > 0: |
---|
5034 | n/a | * 5) adjexp(adjexp(x)) <= adjexp(abs(log10(x))) |
---|
5035 | n/a | * Case adjexp(x) == 0: |
---|
5036 | n/a | * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) |
---|
5037 | n/a | * |
---|
5038 | n/a | * Case adjexp(x) < 0: |
---|
5039 | n/a | * 6) -adjexp(x) - 1 < abs(log10(x)) |
---|
5040 | n/a | * Case adjexp(x) < -1: |
---|
5041 | n/a | * 7) adjexp(-adjexp(x) - 1) <= adjexp(abs(log(x))) |
---|
5042 | n/a | * Case adjexp(x) == -1: |
---|
5043 | n/a | * mpd_exp_digits(t)-1 == 0 <= emax (the shortcut is not triggered) |
---|
5044 | n/a | */ |
---|
5045 | n/a | adjexp = mpd_adjexp(a); |
---|
5046 | n/a | t = (adjexp < 0) ? -adjexp-1 : adjexp; |
---|
5047 | n/a | if (mpd_exp_digits(t)-1 > ctx->emax) { |
---|
5048 | n/a | *status |= MPD_Overflow|MPD_Inexact|MPD_Rounded; |
---|
5049 | n/a | mpd_setspecial(result, (adjexp<0), MPD_INF); |
---|
5050 | n/a | return; |
---|
5051 | n/a | } |
---|
5052 | n/a | |
---|
5053 | n/a | if (ctx->allcr) { |
---|
5054 | n/a | MPD_NEW_STATIC(t1, 0,0,0,0); |
---|
5055 | n/a | MPD_NEW_STATIC(t2, 0,0,0,0); |
---|
5056 | n/a | MPD_NEW_STATIC(ulp, 0,0,0,0); |
---|
5057 | n/a | MPD_NEW_STATIC(aa, 0,0,0,0); |
---|
5058 | n/a | mpd_ssize_t prec; |
---|
5059 | n/a | |
---|
5060 | n/a | if (result == a) { |
---|
5061 | n/a | if (!mpd_qcopy(&aa, a, status)) { |
---|
5062 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
5063 | n/a | return; |
---|
5064 | n/a | } |
---|
5065 | n/a | a = &aa; |
---|
5066 | n/a | } |
---|
5067 | n/a | |
---|
5068 | n/a | workctx.clamp = 0; |
---|
5069 | n/a | prec = ctx->prec + 3; |
---|
5070 | n/a | while (1) { |
---|
5071 | n/a | workctx.prec = prec; |
---|
5072 | n/a | _mpd_qlog10(SKIP_FINALIZE, result, a, &workctx, status); |
---|
5073 | n/a | _ssettriple(&ulp, MPD_POS, 1, |
---|
5074 | n/a | result->exp + result->digits-workctx.prec); |
---|
5075 | n/a | |
---|
5076 | n/a | workctx.prec = ctx->prec; |
---|
5077 | n/a | mpd_qadd(&t1, result, &ulp, &workctx, &workctx.status); |
---|
5078 | n/a | mpd_qsub(&t2, result, &ulp, &workctx, &workctx.status); |
---|
5079 | n/a | if (mpd_isspecial(result) || mpd_iszerocoeff(result) || |
---|
5080 | n/a | mpd_qcmp(&t1, &t2, status) == 0) { |
---|
5081 | n/a | workctx.clamp = ctx->clamp; |
---|
5082 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
5083 | n/a | mpd_qfinalize(result, &workctx, status); |
---|
5084 | n/a | break; |
---|
5085 | n/a | } |
---|
5086 | n/a | prec += MPD_RDIGITS; |
---|
5087 | n/a | } |
---|
5088 | n/a | mpd_del(&t1); |
---|
5089 | n/a | mpd_del(&t2); |
---|
5090 | n/a | mpd_del(&ulp); |
---|
5091 | n/a | mpd_del(&aa); |
---|
5092 | n/a | } |
---|
5093 | n/a | else { |
---|
5094 | n/a | _mpd_qlog10(DO_FINALIZE, result, a, &workctx, status); |
---|
5095 | n/a | mpd_check_underflow(result, &workctx, status); |
---|
5096 | n/a | } |
---|
5097 | n/a | } |
---|
5098 | n/a | |
---|
5099 | n/a | /* |
---|
5100 | n/a | * Maximum of the two operands. Attention: If one operand is a quiet NaN and the |
---|
5101 | n/a | * other is numeric, the numeric operand is returned. This may not be what one |
---|
5102 | n/a | * expects. |
---|
5103 | n/a | */ |
---|
5104 | n/a | void |
---|
5105 | n/a | mpd_qmax(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5106 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5107 | n/a | { |
---|
5108 | n/a | int c; |
---|
5109 | n/a | |
---|
5110 | n/a | if (mpd_isqnan(a) && !mpd_isnan(b)) { |
---|
5111 | n/a | mpd_qcopy(result, b, status); |
---|
5112 | n/a | } |
---|
5113 | n/a | else if (mpd_isqnan(b) && !mpd_isnan(a)) { |
---|
5114 | n/a | mpd_qcopy(result, a, status); |
---|
5115 | n/a | } |
---|
5116 | n/a | else if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
5117 | n/a | return; |
---|
5118 | n/a | } |
---|
5119 | n/a | else { |
---|
5120 | n/a | c = _mpd_cmp(a, b); |
---|
5121 | n/a | if (c == 0) { |
---|
5122 | n/a | c = _mpd_cmp_numequal(a, b); |
---|
5123 | n/a | } |
---|
5124 | n/a | |
---|
5125 | n/a | if (c < 0) { |
---|
5126 | n/a | mpd_qcopy(result, b, status); |
---|
5127 | n/a | } |
---|
5128 | n/a | else { |
---|
5129 | n/a | mpd_qcopy(result, a, status); |
---|
5130 | n/a | } |
---|
5131 | n/a | } |
---|
5132 | n/a | |
---|
5133 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5134 | n/a | } |
---|
5135 | n/a | |
---|
5136 | n/a | /* |
---|
5137 | n/a | * Maximum magnitude: Same as mpd_max(), but compares the operands with their |
---|
5138 | n/a | * sign ignored. |
---|
5139 | n/a | */ |
---|
5140 | n/a | void |
---|
5141 | n/a | mpd_qmax_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5142 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5143 | n/a | { |
---|
5144 | n/a | int c; |
---|
5145 | n/a | |
---|
5146 | n/a | if (mpd_isqnan(a) && !mpd_isnan(b)) { |
---|
5147 | n/a | mpd_qcopy(result, b, status); |
---|
5148 | n/a | } |
---|
5149 | n/a | else if (mpd_isqnan(b) && !mpd_isnan(a)) { |
---|
5150 | n/a | mpd_qcopy(result, a, status); |
---|
5151 | n/a | } |
---|
5152 | n/a | else if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
5153 | n/a | return; |
---|
5154 | n/a | } |
---|
5155 | n/a | else { |
---|
5156 | n/a | c = _mpd_cmp_abs(a, b); |
---|
5157 | n/a | if (c == 0) { |
---|
5158 | n/a | c = _mpd_cmp_numequal(a, b); |
---|
5159 | n/a | } |
---|
5160 | n/a | |
---|
5161 | n/a | if (c < 0) { |
---|
5162 | n/a | mpd_qcopy(result, b, status); |
---|
5163 | n/a | } |
---|
5164 | n/a | else { |
---|
5165 | n/a | mpd_qcopy(result, a, status); |
---|
5166 | n/a | } |
---|
5167 | n/a | } |
---|
5168 | n/a | |
---|
5169 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5170 | n/a | } |
---|
5171 | n/a | |
---|
5172 | n/a | /* |
---|
5173 | n/a | * Minimum of the two operands. Attention: If one operand is a quiet NaN and the |
---|
5174 | n/a | * other is numeric, the numeric operand is returned. This may not be what one |
---|
5175 | n/a | * expects. |
---|
5176 | n/a | */ |
---|
5177 | n/a | void |
---|
5178 | n/a | mpd_qmin(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5179 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5180 | n/a | { |
---|
5181 | n/a | int c; |
---|
5182 | n/a | |
---|
5183 | n/a | if (mpd_isqnan(a) && !mpd_isnan(b)) { |
---|
5184 | n/a | mpd_qcopy(result, b, status); |
---|
5185 | n/a | } |
---|
5186 | n/a | else if (mpd_isqnan(b) && !mpd_isnan(a)) { |
---|
5187 | n/a | mpd_qcopy(result, a, status); |
---|
5188 | n/a | } |
---|
5189 | n/a | else if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
5190 | n/a | return; |
---|
5191 | n/a | } |
---|
5192 | n/a | else { |
---|
5193 | n/a | c = _mpd_cmp(a, b); |
---|
5194 | n/a | if (c == 0) { |
---|
5195 | n/a | c = _mpd_cmp_numequal(a, b); |
---|
5196 | n/a | } |
---|
5197 | n/a | |
---|
5198 | n/a | if (c < 0) { |
---|
5199 | n/a | mpd_qcopy(result, a, status); |
---|
5200 | n/a | } |
---|
5201 | n/a | else { |
---|
5202 | n/a | mpd_qcopy(result, b, status); |
---|
5203 | n/a | } |
---|
5204 | n/a | } |
---|
5205 | n/a | |
---|
5206 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5207 | n/a | } |
---|
5208 | n/a | |
---|
5209 | n/a | /* |
---|
5210 | n/a | * Minimum magnitude: Same as mpd_min(), but compares the operands with their |
---|
5211 | n/a | * sign ignored. |
---|
5212 | n/a | */ |
---|
5213 | n/a | void |
---|
5214 | n/a | mpd_qmin_mag(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5215 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5216 | n/a | { |
---|
5217 | n/a | int c; |
---|
5218 | n/a | |
---|
5219 | n/a | if (mpd_isqnan(a) && !mpd_isnan(b)) { |
---|
5220 | n/a | mpd_qcopy(result, b, status); |
---|
5221 | n/a | } |
---|
5222 | n/a | else if (mpd_isqnan(b) && !mpd_isnan(a)) { |
---|
5223 | n/a | mpd_qcopy(result, a, status); |
---|
5224 | n/a | } |
---|
5225 | n/a | else if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
5226 | n/a | return; |
---|
5227 | n/a | } |
---|
5228 | n/a | else { |
---|
5229 | n/a | c = _mpd_cmp_abs(a, b); |
---|
5230 | n/a | if (c == 0) { |
---|
5231 | n/a | c = _mpd_cmp_numequal(a, b); |
---|
5232 | n/a | } |
---|
5233 | n/a | |
---|
5234 | n/a | if (c < 0) { |
---|
5235 | n/a | mpd_qcopy(result, a, status); |
---|
5236 | n/a | } |
---|
5237 | n/a | else { |
---|
5238 | n/a | mpd_qcopy(result, b, status); |
---|
5239 | n/a | } |
---|
5240 | n/a | } |
---|
5241 | n/a | |
---|
5242 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5243 | n/a | } |
---|
5244 | n/a | |
---|
5245 | n/a | /* Minimum space needed for the result array in _karatsuba_rec(). */ |
---|
5246 | n/a | static inline mpd_size_t |
---|
5247 | n/a | _kmul_resultsize(mpd_size_t la, mpd_size_t lb) |
---|
5248 | n/a | { |
---|
5249 | n/a | mpd_size_t n, m; |
---|
5250 | n/a | |
---|
5251 | n/a | n = add_size_t(la, lb); |
---|
5252 | n/a | n = add_size_t(n, 1); |
---|
5253 | n/a | |
---|
5254 | n/a | m = (la+1)/2 + 1; |
---|
5255 | n/a | m = mul_size_t(m, 3); |
---|
5256 | n/a | |
---|
5257 | n/a | return (m > n) ? m : n; |
---|
5258 | n/a | } |
---|
5259 | n/a | |
---|
5260 | n/a | /* Work space needed in _karatsuba_rec(). lim >= 4 */ |
---|
5261 | n/a | static inline mpd_size_t |
---|
5262 | n/a | _kmul_worksize(mpd_size_t n, mpd_size_t lim) |
---|
5263 | n/a | { |
---|
5264 | n/a | mpd_size_t m; |
---|
5265 | n/a | |
---|
5266 | n/a | if (n <= lim) { |
---|
5267 | n/a | return 0; |
---|
5268 | n/a | } |
---|
5269 | n/a | |
---|
5270 | n/a | m = (n+1)/2 + 1; |
---|
5271 | n/a | |
---|
5272 | n/a | return add_size_t(mul_size_t(m, 2), _kmul_worksize(m, lim)); |
---|
5273 | n/a | } |
---|
5274 | n/a | |
---|
5275 | n/a | |
---|
5276 | n/a | #define MPD_KARATSUBA_BASECASE 16 /* must be >= 4 */ |
---|
5277 | n/a | |
---|
5278 | n/a | /* |
---|
5279 | n/a | * Add the product of a and b to c. |
---|
5280 | n/a | * c must be _kmul_resultsize(la, lb) in size. |
---|
5281 | n/a | * w is used as a work array and must be _kmul_worksize(a, lim) in size. |
---|
5282 | n/a | * Roman E. Maeder, Storage Allocation for the Karatsuba Integer Multiplication |
---|
5283 | n/a | * Algorithm. In "Design and implementation of symbolic computation systems", |
---|
5284 | n/a | * Springer, 1993, ISBN 354057235X, 9783540572350. |
---|
5285 | n/a | */ |
---|
5286 | n/a | static void |
---|
5287 | n/a | _karatsuba_rec(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, |
---|
5288 | n/a | mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) |
---|
5289 | n/a | { |
---|
5290 | n/a | mpd_size_t m, lt; |
---|
5291 | n/a | |
---|
5292 | n/a | assert(la >= lb && lb > 0); |
---|
5293 | n/a | assert(la <= MPD_KARATSUBA_BASECASE || w != NULL); |
---|
5294 | n/a | |
---|
5295 | n/a | if (la <= MPD_KARATSUBA_BASECASE) { |
---|
5296 | n/a | _mpd_basemul(c, a, b, la, lb); |
---|
5297 | n/a | return; |
---|
5298 | n/a | } |
---|
5299 | n/a | |
---|
5300 | n/a | m = (la+1)/2; /* ceil(la/2) */ |
---|
5301 | n/a | |
---|
5302 | n/a | /* lb <= m < la */ |
---|
5303 | n/a | if (lb <= m) { |
---|
5304 | n/a | |
---|
5305 | n/a | /* lb can now be larger than la-m */ |
---|
5306 | n/a | if (lb > la-m) { |
---|
5307 | n/a | lt = lb + lb + 1; /* space needed for result array */ |
---|
5308 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5309 | n/a | _karatsuba_rec(w, b, a+m, w+lt, lb, la-m); /* b*ah */ |
---|
5310 | n/a | } |
---|
5311 | n/a | else { |
---|
5312 | n/a | lt = (la-m) + (la-m) + 1; /* space needed for result array */ |
---|
5313 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5314 | n/a | _karatsuba_rec(w, a+m, b, w+lt, la-m, lb); /* ah*b */ |
---|
5315 | n/a | } |
---|
5316 | n/a | _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ |
---|
5317 | n/a | |
---|
5318 | n/a | lt = m + m + 1; /* space needed for the result array */ |
---|
5319 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5320 | n/a | _karatsuba_rec(w, a, b, w+lt, m, lb); /* al*b */ |
---|
5321 | n/a | _mpd_baseaddto(c, w, m+lb); /* add al*b */ |
---|
5322 | n/a | |
---|
5323 | n/a | return; |
---|
5324 | n/a | } |
---|
5325 | n/a | |
---|
5326 | n/a | /* la >= lb > m */ |
---|
5327 | n/a | memcpy(w, a, m * sizeof *w); |
---|
5328 | n/a | w[m] = 0; |
---|
5329 | n/a | _mpd_baseaddto(w, a+m, la-m); |
---|
5330 | n/a | |
---|
5331 | n/a | memcpy(w+(m+1), b, m * sizeof *w); |
---|
5332 | n/a | w[m+1+m] = 0; |
---|
5333 | n/a | _mpd_baseaddto(w+(m+1), b+m, lb-m); |
---|
5334 | n/a | |
---|
5335 | n/a | _karatsuba_rec(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1); |
---|
5336 | n/a | |
---|
5337 | n/a | lt = (la-m) + (la-m) + 1; |
---|
5338 | n/a | mpd_uint_zero(w, lt); |
---|
5339 | n/a | |
---|
5340 | n/a | _karatsuba_rec(w, a+m, b+m, w+lt, la-m, lb-m); |
---|
5341 | n/a | |
---|
5342 | n/a | _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); |
---|
5343 | n/a | _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); |
---|
5344 | n/a | |
---|
5345 | n/a | lt = m + m + 1; |
---|
5346 | n/a | mpd_uint_zero(w, lt); |
---|
5347 | n/a | |
---|
5348 | n/a | _karatsuba_rec(w, a, b, w+lt, m, m); |
---|
5349 | n/a | _mpd_baseaddto(c, w, m+m); |
---|
5350 | n/a | _mpd_basesubfrom(c+m, w, m+m); |
---|
5351 | n/a | |
---|
5352 | n/a | return; |
---|
5353 | n/a | } |
---|
5354 | n/a | |
---|
5355 | n/a | /* |
---|
5356 | n/a | * Multiply u and v, using Karatsuba multiplication. Returns a pointer |
---|
5357 | n/a | * to the result or NULL in case of failure (malloc error). |
---|
5358 | n/a | * Conditions: ulen >= vlen, ulen >= 4 |
---|
5359 | n/a | */ |
---|
5360 | n/a | static mpd_uint_t * |
---|
5361 | n/a | _mpd_kmul(const mpd_uint_t *u, const mpd_uint_t *v, |
---|
5362 | n/a | mpd_size_t ulen, mpd_size_t vlen, |
---|
5363 | n/a | mpd_size_t *rsize) |
---|
5364 | n/a | { |
---|
5365 | n/a | mpd_uint_t *result = NULL, *w = NULL; |
---|
5366 | n/a | mpd_size_t m; |
---|
5367 | n/a | |
---|
5368 | n/a | assert(ulen >= 4); |
---|
5369 | n/a | assert(ulen >= vlen); |
---|
5370 | n/a | |
---|
5371 | n/a | *rsize = _kmul_resultsize(ulen, vlen); |
---|
5372 | n/a | if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { |
---|
5373 | n/a | return NULL; |
---|
5374 | n/a | } |
---|
5375 | n/a | |
---|
5376 | n/a | m = _kmul_worksize(ulen, MPD_KARATSUBA_BASECASE); |
---|
5377 | n/a | if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { |
---|
5378 | n/a | mpd_free(result); |
---|
5379 | n/a | return NULL; |
---|
5380 | n/a | } |
---|
5381 | n/a | |
---|
5382 | n/a | _karatsuba_rec(result, u, v, w, ulen, vlen); |
---|
5383 | n/a | |
---|
5384 | n/a | |
---|
5385 | n/a | if (w) mpd_free(w); |
---|
5386 | n/a | return result; |
---|
5387 | n/a | } |
---|
5388 | n/a | |
---|
5389 | n/a | |
---|
5390 | n/a | /* |
---|
5391 | n/a | * Determine the minimum length for the number theoretic transform. Valid |
---|
5392 | n/a | * transform lengths are 2**n or 3*2**n, where 2**n <= MPD_MAXTRANSFORM_2N. |
---|
5393 | n/a | * The function finds the shortest length m such that rsize <= m. |
---|
5394 | n/a | */ |
---|
5395 | n/a | static inline mpd_size_t |
---|
5396 | n/a | _mpd_get_transform_len(mpd_size_t rsize) |
---|
5397 | n/a | { |
---|
5398 | n/a | mpd_size_t log2rsize; |
---|
5399 | n/a | mpd_size_t x, step; |
---|
5400 | n/a | |
---|
5401 | n/a | assert(rsize >= 4); |
---|
5402 | n/a | log2rsize = mpd_bsr(rsize); |
---|
5403 | n/a | |
---|
5404 | n/a | if (rsize <= 1024) { |
---|
5405 | n/a | /* 2**n is faster in this range. */ |
---|
5406 | n/a | x = ((mpd_size_t)1)<<log2rsize; |
---|
5407 | n/a | return (rsize == x) ? x : x<<1; |
---|
5408 | n/a | } |
---|
5409 | n/a | else if (rsize <= MPD_MAXTRANSFORM_2N) { |
---|
5410 | n/a | x = ((mpd_size_t)1)<<log2rsize; |
---|
5411 | n/a | if (rsize == x) return x; |
---|
5412 | n/a | step = x>>1; |
---|
5413 | n/a | x += step; |
---|
5414 | n/a | return (rsize <= x) ? x : x + step; |
---|
5415 | n/a | } |
---|
5416 | n/a | else if (rsize <= MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2) { |
---|
5417 | n/a | return MPD_MAXTRANSFORM_2N+MPD_MAXTRANSFORM_2N/2; |
---|
5418 | n/a | } |
---|
5419 | n/a | else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { |
---|
5420 | n/a | return 3*MPD_MAXTRANSFORM_2N; |
---|
5421 | n/a | } |
---|
5422 | n/a | else { |
---|
5423 | n/a | return MPD_SIZE_MAX; |
---|
5424 | n/a | } |
---|
5425 | n/a | } |
---|
5426 | n/a | |
---|
5427 | n/a | #ifdef PPRO |
---|
5428 | n/a | #ifndef _MSC_VER |
---|
5429 | n/a | static inline unsigned short |
---|
5430 | n/a | _mpd_get_control87(void) |
---|
5431 | n/a | { |
---|
5432 | n/a | unsigned short cw; |
---|
5433 | n/a | |
---|
5434 | n/a | __asm__ __volatile__ ("fnstcw %0" : "=m" (cw)); |
---|
5435 | n/a | return cw; |
---|
5436 | n/a | } |
---|
5437 | n/a | |
---|
5438 | n/a | static inline void |
---|
5439 | n/a | _mpd_set_control87(unsigned short cw) |
---|
5440 | n/a | { |
---|
5441 | n/a | __asm__ __volatile__ ("fldcw %0" : : "m" (cw)); |
---|
5442 | n/a | } |
---|
5443 | n/a | #endif |
---|
5444 | n/a | |
---|
5445 | n/a | static unsigned int |
---|
5446 | n/a | mpd_set_fenv(void) |
---|
5447 | n/a | { |
---|
5448 | n/a | unsigned int cw; |
---|
5449 | n/a | #ifdef _MSC_VER |
---|
5450 | n/a | unsigned int flags = |
---|
5451 | n/a | _EM_INVALID|_EM_DENORMAL|_EM_ZERODIVIDE|_EM_OVERFLOW| |
---|
5452 | n/a | _EM_UNDERFLOW|_EM_INEXACT|_RC_CHOP|_PC_64; |
---|
5453 | n/a | unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; |
---|
5454 | n/a | unsigned int dummy; |
---|
5455 | n/a | |
---|
5456 | n/a | __control87_2(0, 0, &cw, NULL); |
---|
5457 | n/a | __control87_2(flags, mask, &dummy, NULL); |
---|
5458 | n/a | #else |
---|
5459 | n/a | cw = _mpd_get_control87(); |
---|
5460 | n/a | _mpd_set_control87(cw|0xF3F); |
---|
5461 | n/a | #endif |
---|
5462 | n/a | return cw; |
---|
5463 | n/a | } |
---|
5464 | n/a | |
---|
5465 | n/a | static void |
---|
5466 | n/a | mpd_restore_fenv(unsigned int cw) |
---|
5467 | n/a | { |
---|
5468 | n/a | #ifdef _MSC_VER |
---|
5469 | n/a | unsigned int mask = _MCW_EM|_MCW_RC|_MCW_PC; |
---|
5470 | n/a | unsigned int dummy; |
---|
5471 | n/a | |
---|
5472 | n/a | __control87_2(cw, mask, &dummy, NULL); |
---|
5473 | n/a | #else |
---|
5474 | n/a | _mpd_set_control87((unsigned short)cw); |
---|
5475 | n/a | #endif |
---|
5476 | n/a | } |
---|
5477 | n/a | #endif /* PPRO */ |
---|
5478 | n/a | |
---|
5479 | n/a | /* |
---|
5480 | n/a | * Multiply u and v, using the fast number theoretic transform. Returns |
---|
5481 | n/a | * a pointer to the result or NULL in case of failure (malloc error). |
---|
5482 | n/a | */ |
---|
5483 | n/a | static mpd_uint_t * |
---|
5484 | n/a | _mpd_fntmul(const mpd_uint_t *u, const mpd_uint_t *v, |
---|
5485 | n/a | mpd_size_t ulen, mpd_size_t vlen, |
---|
5486 | n/a | mpd_size_t *rsize) |
---|
5487 | n/a | { |
---|
5488 | n/a | mpd_uint_t *c1 = NULL, *c2 = NULL, *c3 = NULL, *vtmp = NULL; |
---|
5489 | n/a | mpd_size_t n; |
---|
5490 | n/a | |
---|
5491 | n/a | #ifdef PPRO |
---|
5492 | n/a | unsigned int cw; |
---|
5493 | n/a | cw = mpd_set_fenv(); |
---|
5494 | n/a | #endif |
---|
5495 | n/a | |
---|
5496 | n/a | *rsize = add_size_t(ulen, vlen); |
---|
5497 | n/a | if ((n = _mpd_get_transform_len(*rsize)) == MPD_SIZE_MAX) { |
---|
5498 | n/a | goto malloc_error; |
---|
5499 | n/a | } |
---|
5500 | n/a | |
---|
5501 | n/a | if ((c1 = mpd_calloc(n, sizeof *c1)) == NULL) { |
---|
5502 | n/a | goto malloc_error; |
---|
5503 | n/a | } |
---|
5504 | n/a | if ((c2 = mpd_calloc(n, sizeof *c2)) == NULL) { |
---|
5505 | n/a | goto malloc_error; |
---|
5506 | n/a | } |
---|
5507 | n/a | if ((c3 = mpd_calloc(n, sizeof *c3)) == NULL) { |
---|
5508 | n/a | goto malloc_error; |
---|
5509 | n/a | } |
---|
5510 | n/a | |
---|
5511 | n/a | memcpy(c1, u, ulen * (sizeof *c1)); |
---|
5512 | n/a | memcpy(c2, u, ulen * (sizeof *c2)); |
---|
5513 | n/a | memcpy(c3, u, ulen * (sizeof *c3)); |
---|
5514 | n/a | |
---|
5515 | n/a | if (u == v) { |
---|
5516 | n/a | if (!fnt_autoconvolute(c1, n, P1) || |
---|
5517 | n/a | !fnt_autoconvolute(c2, n, P2) || |
---|
5518 | n/a | !fnt_autoconvolute(c3, n, P3)) { |
---|
5519 | n/a | goto malloc_error; |
---|
5520 | n/a | } |
---|
5521 | n/a | } |
---|
5522 | n/a | else { |
---|
5523 | n/a | if ((vtmp = mpd_calloc(n, sizeof *vtmp)) == NULL) { |
---|
5524 | n/a | goto malloc_error; |
---|
5525 | n/a | } |
---|
5526 | n/a | |
---|
5527 | n/a | memcpy(vtmp, v, vlen * (sizeof *vtmp)); |
---|
5528 | n/a | if (!fnt_convolute(c1, vtmp, n, P1)) { |
---|
5529 | n/a | mpd_free(vtmp); |
---|
5530 | n/a | goto malloc_error; |
---|
5531 | n/a | } |
---|
5532 | n/a | |
---|
5533 | n/a | memcpy(vtmp, v, vlen * (sizeof *vtmp)); |
---|
5534 | n/a | mpd_uint_zero(vtmp+vlen, n-vlen); |
---|
5535 | n/a | if (!fnt_convolute(c2, vtmp, n, P2)) { |
---|
5536 | n/a | mpd_free(vtmp); |
---|
5537 | n/a | goto malloc_error; |
---|
5538 | n/a | } |
---|
5539 | n/a | |
---|
5540 | n/a | memcpy(vtmp, v, vlen * (sizeof *vtmp)); |
---|
5541 | n/a | mpd_uint_zero(vtmp+vlen, n-vlen); |
---|
5542 | n/a | if (!fnt_convolute(c3, vtmp, n, P3)) { |
---|
5543 | n/a | mpd_free(vtmp); |
---|
5544 | n/a | goto malloc_error; |
---|
5545 | n/a | } |
---|
5546 | n/a | |
---|
5547 | n/a | mpd_free(vtmp); |
---|
5548 | n/a | } |
---|
5549 | n/a | |
---|
5550 | n/a | crt3(c1, c2, c3, *rsize); |
---|
5551 | n/a | |
---|
5552 | n/a | out: |
---|
5553 | n/a | #ifdef PPRO |
---|
5554 | n/a | mpd_restore_fenv(cw); |
---|
5555 | n/a | #endif |
---|
5556 | n/a | if (c2) mpd_free(c2); |
---|
5557 | n/a | if (c3) mpd_free(c3); |
---|
5558 | n/a | return c1; |
---|
5559 | n/a | |
---|
5560 | n/a | malloc_error: |
---|
5561 | n/a | if (c1) mpd_free(c1); |
---|
5562 | n/a | c1 = NULL; |
---|
5563 | n/a | goto out; |
---|
5564 | n/a | } |
---|
5565 | n/a | |
---|
5566 | n/a | |
---|
5567 | n/a | /* |
---|
5568 | n/a | * Karatsuba multiplication with FNT/basemul as the base case. |
---|
5569 | n/a | */ |
---|
5570 | n/a | static int |
---|
5571 | n/a | _karatsuba_rec_fnt(mpd_uint_t *c, const mpd_uint_t *a, const mpd_uint_t *b, |
---|
5572 | n/a | mpd_uint_t *w, mpd_size_t la, mpd_size_t lb) |
---|
5573 | n/a | { |
---|
5574 | n/a | mpd_size_t m, lt; |
---|
5575 | n/a | |
---|
5576 | n/a | assert(la >= lb && lb > 0); |
---|
5577 | n/a | assert(la <= 3*(MPD_MAXTRANSFORM_2N/2) || w != NULL); |
---|
5578 | n/a | |
---|
5579 | n/a | if (la <= 3*(MPD_MAXTRANSFORM_2N/2)) { |
---|
5580 | n/a | |
---|
5581 | n/a | if (lb <= 192) { |
---|
5582 | n/a | _mpd_basemul(c, b, a, lb, la); |
---|
5583 | n/a | } |
---|
5584 | n/a | else { |
---|
5585 | n/a | mpd_uint_t *result; |
---|
5586 | n/a | mpd_size_t dummy; |
---|
5587 | n/a | |
---|
5588 | n/a | if ((result = _mpd_fntmul(a, b, la, lb, &dummy)) == NULL) { |
---|
5589 | n/a | return 0; |
---|
5590 | n/a | } |
---|
5591 | n/a | memcpy(c, result, (la+lb) * (sizeof *result)); |
---|
5592 | n/a | mpd_free(result); |
---|
5593 | n/a | } |
---|
5594 | n/a | return 1; |
---|
5595 | n/a | } |
---|
5596 | n/a | |
---|
5597 | n/a | m = (la+1)/2; /* ceil(la/2) */ |
---|
5598 | n/a | |
---|
5599 | n/a | /* lb <= m < la */ |
---|
5600 | n/a | if (lb <= m) { |
---|
5601 | n/a | |
---|
5602 | n/a | /* lb can now be larger than la-m */ |
---|
5603 | n/a | if (lb > la-m) { |
---|
5604 | n/a | lt = lb + lb + 1; /* space needed for result array */ |
---|
5605 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5606 | n/a | if (!_karatsuba_rec_fnt(w, b, a+m, w+lt, lb, la-m)) { /* b*ah */ |
---|
5607 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5608 | n/a | } |
---|
5609 | n/a | } |
---|
5610 | n/a | else { |
---|
5611 | n/a | lt = (la-m) + (la-m) + 1; /* space needed for result array */ |
---|
5612 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5613 | n/a | if (!_karatsuba_rec_fnt(w, a+m, b, w+lt, la-m, lb)) { /* ah*b */ |
---|
5614 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5615 | n/a | } |
---|
5616 | n/a | } |
---|
5617 | n/a | _mpd_baseaddto(c+m, w, (la-m)+lb); /* add ah*b*B**m */ |
---|
5618 | n/a | |
---|
5619 | n/a | lt = m + m + 1; /* space needed for the result array */ |
---|
5620 | n/a | mpd_uint_zero(w, lt); /* clear result array */ |
---|
5621 | n/a | if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, lb)) { /* al*b */ |
---|
5622 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5623 | n/a | } |
---|
5624 | n/a | _mpd_baseaddto(c, w, m+lb); /* add al*b */ |
---|
5625 | n/a | |
---|
5626 | n/a | return 1; |
---|
5627 | n/a | } |
---|
5628 | n/a | |
---|
5629 | n/a | /* la >= lb > m */ |
---|
5630 | n/a | memcpy(w, a, m * sizeof *w); |
---|
5631 | n/a | w[m] = 0; |
---|
5632 | n/a | _mpd_baseaddto(w, a+m, la-m); |
---|
5633 | n/a | |
---|
5634 | n/a | memcpy(w+(m+1), b, m * sizeof *w); |
---|
5635 | n/a | w[m+1+m] = 0; |
---|
5636 | n/a | _mpd_baseaddto(w+(m+1), b+m, lb-m); |
---|
5637 | n/a | |
---|
5638 | n/a | if (!_karatsuba_rec_fnt(c+m, w, w+(m+1), w+2*(m+1), m+1, m+1)) { |
---|
5639 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5640 | n/a | } |
---|
5641 | n/a | |
---|
5642 | n/a | lt = (la-m) + (la-m) + 1; |
---|
5643 | n/a | mpd_uint_zero(w, lt); |
---|
5644 | n/a | |
---|
5645 | n/a | if (!_karatsuba_rec_fnt(w, a+m, b+m, w+lt, la-m, lb-m)) { |
---|
5646 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5647 | n/a | } |
---|
5648 | n/a | |
---|
5649 | n/a | _mpd_baseaddto(c+2*m, w, (la-m) + (lb-m)); |
---|
5650 | n/a | _mpd_basesubfrom(c+m, w, (la-m) + (lb-m)); |
---|
5651 | n/a | |
---|
5652 | n/a | lt = m + m + 1; |
---|
5653 | n/a | mpd_uint_zero(w, lt); |
---|
5654 | n/a | |
---|
5655 | n/a | if (!_karatsuba_rec_fnt(w, a, b, w+lt, m, m)) { |
---|
5656 | n/a | return 0; /* GCOV_UNLIKELY */ |
---|
5657 | n/a | } |
---|
5658 | n/a | _mpd_baseaddto(c, w, m+m); |
---|
5659 | n/a | _mpd_basesubfrom(c+m, w, m+m); |
---|
5660 | n/a | |
---|
5661 | n/a | return 1; |
---|
5662 | n/a | } |
---|
5663 | n/a | |
---|
5664 | n/a | /* |
---|
5665 | n/a | * Multiply u and v, using Karatsuba multiplication with the FNT as the |
---|
5666 | n/a | * base case. Returns a pointer to the result or NULL in case of failure |
---|
5667 | n/a | * (malloc error). Conditions: ulen >= vlen, ulen >= 4. |
---|
5668 | n/a | */ |
---|
5669 | n/a | static mpd_uint_t * |
---|
5670 | n/a | _mpd_kmul_fnt(const mpd_uint_t *u, const mpd_uint_t *v, |
---|
5671 | n/a | mpd_size_t ulen, mpd_size_t vlen, |
---|
5672 | n/a | mpd_size_t *rsize) |
---|
5673 | n/a | { |
---|
5674 | n/a | mpd_uint_t *result = NULL, *w = NULL; |
---|
5675 | n/a | mpd_size_t m; |
---|
5676 | n/a | |
---|
5677 | n/a | assert(ulen >= 4); |
---|
5678 | n/a | assert(ulen >= vlen); |
---|
5679 | n/a | |
---|
5680 | n/a | *rsize = _kmul_resultsize(ulen, vlen); |
---|
5681 | n/a | if ((result = mpd_calloc(*rsize, sizeof *result)) == NULL) { |
---|
5682 | n/a | return NULL; |
---|
5683 | n/a | } |
---|
5684 | n/a | |
---|
5685 | n/a | m = _kmul_worksize(ulen, 3*(MPD_MAXTRANSFORM_2N/2)); |
---|
5686 | n/a | if (m && ((w = mpd_calloc(m, sizeof *w)) == NULL)) { |
---|
5687 | n/a | mpd_free(result); /* GCOV_UNLIKELY */ |
---|
5688 | n/a | return NULL; /* GCOV_UNLIKELY */ |
---|
5689 | n/a | } |
---|
5690 | n/a | |
---|
5691 | n/a | if (!_karatsuba_rec_fnt(result, u, v, w, ulen, vlen)) { |
---|
5692 | n/a | mpd_free(result); |
---|
5693 | n/a | result = NULL; |
---|
5694 | n/a | } |
---|
5695 | n/a | |
---|
5696 | n/a | |
---|
5697 | n/a | if (w) mpd_free(w); |
---|
5698 | n/a | return result; |
---|
5699 | n/a | } |
---|
5700 | n/a | |
---|
5701 | n/a | |
---|
5702 | n/a | /* Deal with the special cases of multiplying infinities. */ |
---|
5703 | n/a | static void |
---|
5704 | n/a | _mpd_qmul_inf(mpd_t *result, const mpd_t *a, const mpd_t *b, uint32_t *status) |
---|
5705 | n/a | { |
---|
5706 | n/a | if (mpd_isinfinite(a)) { |
---|
5707 | n/a | if (mpd_iszero(b)) { |
---|
5708 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
5709 | n/a | } |
---|
5710 | n/a | else { |
---|
5711 | n/a | mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); |
---|
5712 | n/a | } |
---|
5713 | n/a | return; |
---|
5714 | n/a | } |
---|
5715 | n/a | assert(mpd_isinfinite(b)); |
---|
5716 | n/a | if (mpd_iszero(a)) { |
---|
5717 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
5718 | n/a | } |
---|
5719 | n/a | else { |
---|
5720 | n/a | mpd_setspecial(result, mpd_sign(a)^mpd_sign(b), MPD_INF); |
---|
5721 | n/a | } |
---|
5722 | n/a | } |
---|
5723 | n/a | |
---|
5724 | n/a | /* |
---|
5725 | n/a | * Internal function: Multiply a and b. _mpd_qmul deals with specials but |
---|
5726 | n/a | * does NOT finalize the result. This is for use in mpd_fma(). |
---|
5727 | n/a | */ |
---|
5728 | n/a | static inline void |
---|
5729 | n/a | _mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5730 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5731 | n/a | { |
---|
5732 | n/a | const mpd_t *big = a, *small = b; |
---|
5733 | n/a | mpd_uint_t *rdata = NULL; |
---|
5734 | n/a | mpd_uint_t rbuf[MPD_MINALLOC_MAX]; |
---|
5735 | n/a | mpd_size_t rsize, i; |
---|
5736 | n/a | |
---|
5737 | n/a | |
---|
5738 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
5739 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
5740 | n/a | return; |
---|
5741 | n/a | } |
---|
5742 | n/a | _mpd_qmul_inf(result, a, b, status); |
---|
5743 | n/a | return; |
---|
5744 | n/a | } |
---|
5745 | n/a | |
---|
5746 | n/a | if (small->len > big->len) { |
---|
5747 | n/a | _mpd_ptrswap(&big, &small); |
---|
5748 | n/a | } |
---|
5749 | n/a | |
---|
5750 | n/a | rsize = big->len + small->len; |
---|
5751 | n/a | |
---|
5752 | n/a | if (big->len == 1) { |
---|
5753 | n/a | _mpd_singlemul(result->data, big->data[0], small->data[0]); |
---|
5754 | n/a | goto finish; |
---|
5755 | n/a | } |
---|
5756 | n/a | if (rsize <= (mpd_size_t)MPD_MINALLOC_MAX) { |
---|
5757 | n/a | if (big->len == 2) { |
---|
5758 | n/a | _mpd_mul_2_le2(rbuf, big->data, small->data, small->len); |
---|
5759 | n/a | } |
---|
5760 | n/a | else { |
---|
5761 | n/a | mpd_uint_zero(rbuf, rsize); |
---|
5762 | n/a | if (small->len == 1) { |
---|
5763 | n/a | _mpd_shortmul(rbuf, big->data, big->len, small->data[0]); |
---|
5764 | n/a | } |
---|
5765 | n/a | else { |
---|
5766 | n/a | _mpd_basemul(rbuf, small->data, big->data, small->len, big->len); |
---|
5767 | n/a | } |
---|
5768 | n/a | } |
---|
5769 | n/a | if (!mpd_qresize(result, rsize, status)) { |
---|
5770 | n/a | return; |
---|
5771 | n/a | } |
---|
5772 | n/a | for(i = 0; i < rsize; i++) { |
---|
5773 | n/a | result->data[i] = rbuf[i]; |
---|
5774 | n/a | } |
---|
5775 | n/a | goto finish; |
---|
5776 | n/a | } |
---|
5777 | n/a | |
---|
5778 | n/a | |
---|
5779 | n/a | if (small->len <= 256) { |
---|
5780 | n/a | rdata = mpd_calloc(rsize, sizeof *rdata); |
---|
5781 | n/a | if (rdata != NULL) { |
---|
5782 | n/a | if (small->len == 1) { |
---|
5783 | n/a | _mpd_shortmul(rdata, big->data, big->len, small->data[0]); |
---|
5784 | n/a | } |
---|
5785 | n/a | else { |
---|
5786 | n/a | _mpd_basemul(rdata, small->data, big->data, small->len, big->len); |
---|
5787 | n/a | } |
---|
5788 | n/a | } |
---|
5789 | n/a | } |
---|
5790 | n/a | else if (rsize <= 1024) { |
---|
5791 | n/a | rdata = _mpd_kmul(big->data, small->data, big->len, small->len, &rsize); |
---|
5792 | n/a | } |
---|
5793 | n/a | else if (rsize <= 3*MPD_MAXTRANSFORM_2N) { |
---|
5794 | n/a | rdata = _mpd_fntmul(big->data, small->data, big->len, small->len, &rsize); |
---|
5795 | n/a | } |
---|
5796 | n/a | else { |
---|
5797 | n/a | rdata = _mpd_kmul_fnt(big->data, small->data, big->len, small->len, &rsize); |
---|
5798 | n/a | } |
---|
5799 | n/a | |
---|
5800 | n/a | if (rdata == NULL) { |
---|
5801 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
5802 | n/a | return; |
---|
5803 | n/a | } |
---|
5804 | n/a | |
---|
5805 | n/a | if (mpd_isdynamic_data(result)) { |
---|
5806 | n/a | mpd_free(result->data); |
---|
5807 | n/a | } |
---|
5808 | n/a | result->data = rdata; |
---|
5809 | n/a | result->alloc = rsize; |
---|
5810 | n/a | mpd_set_dynamic_data(result); |
---|
5811 | n/a | |
---|
5812 | n/a | |
---|
5813 | n/a | finish: |
---|
5814 | n/a | mpd_set_flags(result, mpd_sign(a)^mpd_sign(b)); |
---|
5815 | n/a | result->exp = big->exp + small->exp; |
---|
5816 | n/a | result->len = _mpd_real_size(result->data, rsize); |
---|
5817 | n/a | /* resize to smaller cannot fail */ |
---|
5818 | n/a | mpd_qresize(result, result->len, status); |
---|
5819 | n/a | mpd_setdigits(result); |
---|
5820 | n/a | } |
---|
5821 | n/a | |
---|
5822 | n/a | /* Multiply a and b. */ |
---|
5823 | n/a | void |
---|
5824 | n/a | mpd_qmul(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5825 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5826 | n/a | { |
---|
5827 | n/a | _mpd_qmul(result, a, b, ctx, status); |
---|
5828 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5829 | n/a | } |
---|
5830 | n/a | |
---|
5831 | n/a | /* Multiply a and b. Set NaN/Invalid_operation if the result is inexact. */ |
---|
5832 | n/a | static void |
---|
5833 | n/a | _mpd_qmul_exact(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
5834 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5835 | n/a | { |
---|
5836 | n/a | uint32_t workstatus = 0; |
---|
5837 | n/a | |
---|
5838 | n/a | mpd_qmul(result, a, b, ctx, &workstatus); |
---|
5839 | n/a | *status |= workstatus; |
---|
5840 | n/a | if (workstatus & (MPD_Inexact|MPD_Rounded|MPD_Clamped)) { |
---|
5841 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
5842 | n/a | } |
---|
5843 | n/a | } |
---|
5844 | n/a | |
---|
5845 | n/a | /* Multiply decimal and mpd_ssize_t. */ |
---|
5846 | n/a | void |
---|
5847 | n/a | mpd_qmul_ssize(mpd_t *result, const mpd_t *a, mpd_ssize_t b, |
---|
5848 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5849 | n/a | { |
---|
5850 | n/a | mpd_context_t maxcontext; |
---|
5851 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
5852 | n/a | |
---|
5853 | n/a | mpd_maxcontext(&maxcontext); |
---|
5854 | n/a | mpd_qsset_ssize(&bb, b, &maxcontext, status); |
---|
5855 | n/a | mpd_qmul(result, a, &bb, ctx, status); |
---|
5856 | n/a | mpd_del(&bb); |
---|
5857 | n/a | } |
---|
5858 | n/a | |
---|
5859 | n/a | /* Multiply decimal and mpd_uint_t. */ |
---|
5860 | n/a | void |
---|
5861 | n/a | mpd_qmul_uint(mpd_t *result, const mpd_t *a, mpd_uint_t b, |
---|
5862 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5863 | n/a | { |
---|
5864 | n/a | mpd_context_t maxcontext; |
---|
5865 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
5866 | n/a | |
---|
5867 | n/a | mpd_maxcontext(&maxcontext); |
---|
5868 | n/a | mpd_qsset_uint(&bb, b, &maxcontext, status); |
---|
5869 | n/a | mpd_qmul(result, a, &bb, ctx, status); |
---|
5870 | n/a | mpd_del(&bb); |
---|
5871 | n/a | } |
---|
5872 | n/a | |
---|
5873 | n/a | void |
---|
5874 | n/a | mpd_qmul_i32(mpd_t *result, const mpd_t *a, int32_t b, |
---|
5875 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5876 | n/a | { |
---|
5877 | n/a | mpd_qmul_ssize(result, a, b, ctx, status); |
---|
5878 | n/a | } |
---|
5879 | n/a | |
---|
5880 | n/a | void |
---|
5881 | n/a | mpd_qmul_u32(mpd_t *result, const mpd_t *a, uint32_t b, |
---|
5882 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5883 | n/a | { |
---|
5884 | n/a | mpd_qmul_uint(result, a, b, ctx, status); |
---|
5885 | n/a | } |
---|
5886 | n/a | |
---|
5887 | n/a | #ifdef CONFIG_64 |
---|
5888 | n/a | void |
---|
5889 | n/a | mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
5890 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5891 | n/a | { |
---|
5892 | n/a | mpd_qmul_ssize(result, a, b, ctx, status); |
---|
5893 | n/a | } |
---|
5894 | n/a | |
---|
5895 | n/a | void |
---|
5896 | n/a | mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
5897 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5898 | n/a | { |
---|
5899 | n/a | mpd_qmul_uint(result, a, b, ctx, status); |
---|
5900 | n/a | } |
---|
5901 | n/a | #elif !defined(LEGACY_COMPILER) |
---|
5902 | n/a | /* Multiply decimal and int64_t. */ |
---|
5903 | n/a | void |
---|
5904 | n/a | mpd_qmul_i64(mpd_t *result, const mpd_t *a, int64_t b, |
---|
5905 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5906 | n/a | { |
---|
5907 | n/a | mpd_context_t maxcontext; |
---|
5908 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
5909 | n/a | |
---|
5910 | n/a | mpd_maxcontext(&maxcontext); |
---|
5911 | n/a | mpd_qset_i64(&bb, b, &maxcontext, status); |
---|
5912 | n/a | mpd_qmul(result, a, &bb, ctx, status); |
---|
5913 | n/a | mpd_del(&bb); |
---|
5914 | n/a | } |
---|
5915 | n/a | |
---|
5916 | n/a | /* Multiply decimal and uint64_t. */ |
---|
5917 | n/a | void |
---|
5918 | n/a | mpd_qmul_u64(mpd_t *result, const mpd_t *a, uint64_t b, |
---|
5919 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
5920 | n/a | { |
---|
5921 | n/a | mpd_context_t maxcontext; |
---|
5922 | n/a | MPD_NEW_STATIC(bb,0,0,0,0); |
---|
5923 | n/a | |
---|
5924 | n/a | mpd_maxcontext(&maxcontext); |
---|
5925 | n/a | mpd_qset_u64(&bb, b, &maxcontext, status); |
---|
5926 | n/a | mpd_qmul(result, a, &bb, ctx, status); |
---|
5927 | n/a | mpd_del(&bb); |
---|
5928 | n/a | } |
---|
5929 | n/a | #endif |
---|
5930 | n/a | |
---|
5931 | n/a | /* Like the minus operator. */ |
---|
5932 | n/a | void |
---|
5933 | n/a | mpd_qminus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
5934 | n/a | uint32_t *status) |
---|
5935 | n/a | { |
---|
5936 | n/a | if (mpd_isspecial(a)) { |
---|
5937 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
5938 | n/a | return; |
---|
5939 | n/a | } |
---|
5940 | n/a | } |
---|
5941 | n/a | |
---|
5942 | n/a | if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { |
---|
5943 | n/a | mpd_qcopy_abs(result, a, status); |
---|
5944 | n/a | } |
---|
5945 | n/a | else { |
---|
5946 | n/a | mpd_qcopy_negate(result, a, status); |
---|
5947 | n/a | } |
---|
5948 | n/a | |
---|
5949 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5950 | n/a | } |
---|
5951 | n/a | |
---|
5952 | n/a | /* Like the plus operator. */ |
---|
5953 | n/a | void |
---|
5954 | n/a | mpd_qplus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
5955 | n/a | uint32_t *status) |
---|
5956 | n/a | { |
---|
5957 | n/a | if (mpd_isspecial(a)) { |
---|
5958 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
5959 | n/a | return; |
---|
5960 | n/a | } |
---|
5961 | n/a | } |
---|
5962 | n/a | |
---|
5963 | n/a | if (mpd_iszero(a) && ctx->round != MPD_ROUND_FLOOR) { |
---|
5964 | n/a | mpd_qcopy_abs(result, a, status); |
---|
5965 | n/a | } |
---|
5966 | n/a | else { |
---|
5967 | n/a | mpd_qcopy(result, a, status); |
---|
5968 | n/a | } |
---|
5969 | n/a | |
---|
5970 | n/a | mpd_qfinalize(result, ctx, status); |
---|
5971 | n/a | } |
---|
5972 | n/a | |
---|
5973 | n/a | /* The largest representable number that is smaller than the operand. */ |
---|
5974 | n/a | void |
---|
5975 | n/a | mpd_qnext_minus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
5976 | n/a | uint32_t *status) |
---|
5977 | n/a | { |
---|
5978 | n/a | mpd_context_t workctx; |
---|
5979 | n/a | MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); |
---|
5980 | n/a | |
---|
5981 | n/a | if (mpd_isspecial(a)) { |
---|
5982 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
5983 | n/a | return; |
---|
5984 | n/a | } |
---|
5985 | n/a | |
---|
5986 | n/a | assert(mpd_isinfinite(a)); |
---|
5987 | n/a | if (mpd_isnegative(a)) { |
---|
5988 | n/a | mpd_qcopy(result, a, status); |
---|
5989 | n/a | return; |
---|
5990 | n/a | } |
---|
5991 | n/a | else { |
---|
5992 | n/a | mpd_clear_flags(result); |
---|
5993 | n/a | mpd_qmaxcoeff(result, ctx, status); |
---|
5994 | n/a | if (mpd_isnan(result)) { |
---|
5995 | n/a | return; |
---|
5996 | n/a | } |
---|
5997 | n/a | result->exp = mpd_etop(ctx); |
---|
5998 | n/a | return; |
---|
5999 | n/a | } |
---|
6000 | n/a | } |
---|
6001 | n/a | |
---|
6002 | n/a | mpd_workcontext(&workctx, ctx); |
---|
6003 | n/a | workctx.round = MPD_ROUND_FLOOR; |
---|
6004 | n/a | |
---|
6005 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
6006 | n/a | return; |
---|
6007 | n/a | } |
---|
6008 | n/a | |
---|
6009 | n/a | mpd_qfinalize(result, &workctx, &workctx.status); |
---|
6010 | n/a | if (workctx.status&(MPD_Inexact|MPD_Errors)) { |
---|
6011 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
6012 | n/a | return; |
---|
6013 | n/a | } |
---|
6014 | n/a | |
---|
6015 | n/a | workctx.status = 0; |
---|
6016 | n/a | mpd_qsub(result, a, &tiny, &workctx, &workctx.status); |
---|
6017 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
6018 | n/a | } |
---|
6019 | n/a | |
---|
6020 | n/a | /* The smallest representable number that is larger than the operand. */ |
---|
6021 | n/a | void |
---|
6022 | n/a | mpd_qnext_plus(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
6023 | n/a | uint32_t *status) |
---|
6024 | n/a | { |
---|
6025 | n/a | mpd_context_t workctx; |
---|
6026 | n/a | MPD_NEW_CONST(tiny,MPD_POS,mpd_etiny(ctx)-1,1,1,1,1); |
---|
6027 | n/a | |
---|
6028 | n/a | if (mpd_isspecial(a)) { |
---|
6029 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
6030 | n/a | return; |
---|
6031 | n/a | } |
---|
6032 | n/a | |
---|
6033 | n/a | assert(mpd_isinfinite(a)); |
---|
6034 | n/a | if (mpd_ispositive(a)) { |
---|
6035 | n/a | mpd_qcopy(result, a, status); |
---|
6036 | n/a | } |
---|
6037 | n/a | else { |
---|
6038 | n/a | mpd_clear_flags(result); |
---|
6039 | n/a | mpd_qmaxcoeff(result, ctx, status); |
---|
6040 | n/a | if (mpd_isnan(result)) { |
---|
6041 | n/a | return; |
---|
6042 | n/a | } |
---|
6043 | n/a | mpd_set_flags(result, MPD_NEG); |
---|
6044 | n/a | result->exp = mpd_etop(ctx); |
---|
6045 | n/a | } |
---|
6046 | n/a | return; |
---|
6047 | n/a | } |
---|
6048 | n/a | |
---|
6049 | n/a | mpd_workcontext(&workctx, ctx); |
---|
6050 | n/a | workctx.round = MPD_ROUND_CEILING; |
---|
6051 | n/a | |
---|
6052 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
6053 | n/a | return; |
---|
6054 | n/a | } |
---|
6055 | n/a | |
---|
6056 | n/a | mpd_qfinalize(result, &workctx, &workctx.status); |
---|
6057 | n/a | if (workctx.status & (MPD_Inexact|MPD_Errors)) { |
---|
6058 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
6059 | n/a | return; |
---|
6060 | n/a | } |
---|
6061 | n/a | |
---|
6062 | n/a | workctx.status = 0; |
---|
6063 | n/a | mpd_qadd(result, a, &tiny, &workctx, &workctx.status); |
---|
6064 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
6065 | n/a | } |
---|
6066 | n/a | |
---|
6067 | n/a | /* |
---|
6068 | n/a | * The number closest to the first operand that is in the direction towards |
---|
6069 | n/a | * the second operand. |
---|
6070 | n/a | */ |
---|
6071 | n/a | void |
---|
6072 | n/a | mpd_qnext_toward(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
6073 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6074 | n/a | { |
---|
6075 | n/a | int c; |
---|
6076 | n/a | |
---|
6077 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
6078 | n/a | return; |
---|
6079 | n/a | } |
---|
6080 | n/a | |
---|
6081 | n/a | c = _mpd_cmp(a, b); |
---|
6082 | n/a | if (c == 0) { |
---|
6083 | n/a | mpd_qcopy_sign(result, a, b, status); |
---|
6084 | n/a | return; |
---|
6085 | n/a | } |
---|
6086 | n/a | |
---|
6087 | n/a | if (c < 0) { |
---|
6088 | n/a | mpd_qnext_plus(result, a, ctx, status); |
---|
6089 | n/a | } |
---|
6090 | n/a | else { |
---|
6091 | n/a | mpd_qnext_minus(result, a, ctx, status); |
---|
6092 | n/a | } |
---|
6093 | n/a | |
---|
6094 | n/a | if (mpd_isinfinite(result)) { |
---|
6095 | n/a | *status |= (MPD_Overflow|MPD_Rounded|MPD_Inexact); |
---|
6096 | n/a | } |
---|
6097 | n/a | else if (mpd_adjexp(result) < ctx->emin) { |
---|
6098 | n/a | *status |= (MPD_Underflow|MPD_Subnormal|MPD_Rounded|MPD_Inexact); |
---|
6099 | n/a | if (mpd_iszero(result)) { |
---|
6100 | n/a | *status |= MPD_Clamped; |
---|
6101 | n/a | } |
---|
6102 | n/a | } |
---|
6103 | n/a | } |
---|
6104 | n/a | |
---|
6105 | n/a | /* |
---|
6106 | n/a | * Internal function: Integer power with mpd_uint_t exponent. The function |
---|
6107 | n/a | * can fail with MPD_Malloc_error. |
---|
6108 | n/a | * |
---|
6109 | n/a | * The error is equal to the error incurred in k-1 multiplications. Assuming |
---|
6110 | n/a | * the upper bound for the relative error in each operation: |
---|
6111 | n/a | * |
---|
6112 | n/a | * abs(err) = 5 * 10**-prec |
---|
6113 | n/a | * result = x**k * (1 + err)**(k-1) |
---|
6114 | n/a | */ |
---|
6115 | n/a | static inline void |
---|
6116 | n/a | _mpd_qpow_uint(mpd_t *result, const mpd_t *base, mpd_uint_t exp, |
---|
6117 | n/a | uint8_t resultsign, const mpd_context_t *ctx, uint32_t *status) |
---|
6118 | n/a | { |
---|
6119 | n/a | uint32_t workstatus = 0; |
---|
6120 | n/a | mpd_uint_t n; |
---|
6121 | n/a | |
---|
6122 | n/a | if (exp == 0) { |
---|
6123 | n/a | _settriple(result, resultsign, 1, 0); /* GCOV_NOT_REACHED */ |
---|
6124 | n/a | return; /* GCOV_NOT_REACHED */ |
---|
6125 | n/a | } |
---|
6126 | n/a | |
---|
6127 | n/a | if (!mpd_qcopy(result, base, status)) { |
---|
6128 | n/a | return; |
---|
6129 | n/a | } |
---|
6130 | n/a | |
---|
6131 | n/a | n = mpd_bits[mpd_bsr(exp)]; |
---|
6132 | n/a | while (n >>= 1) { |
---|
6133 | n/a | mpd_qmul(result, result, result, ctx, &workstatus); |
---|
6134 | n/a | if (exp & n) { |
---|
6135 | n/a | mpd_qmul(result, result, base, ctx, &workstatus); |
---|
6136 | n/a | } |
---|
6137 | n/a | if (mpd_isspecial(result) || |
---|
6138 | n/a | (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { |
---|
6139 | n/a | break; |
---|
6140 | n/a | } |
---|
6141 | n/a | } |
---|
6142 | n/a | |
---|
6143 | n/a | *status |= workstatus; |
---|
6144 | n/a | mpd_set_sign(result, resultsign); |
---|
6145 | n/a | } |
---|
6146 | n/a | |
---|
6147 | n/a | /* |
---|
6148 | n/a | * Internal function: Integer power with mpd_t exponent, tbase and texp |
---|
6149 | n/a | * are modified!! Function can fail with MPD_Malloc_error. |
---|
6150 | n/a | * |
---|
6151 | n/a | * The error is equal to the error incurred in k multiplications. Assuming |
---|
6152 | n/a | * the upper bound for the relative error in each operation: |
---|
6153 | n/a | * |
---|
6154 | n/a | * abs(err) = 5 * 10**-prec |
---|
6155 | n/a | * result = x**k * (1 + err)**k |
---|
6156 | n/a | */ |
---|
6157 | n/a | static inline void |
---|
6158 | n/a | _mpd_qpow_mpd(mpd_t *result, mpd_t *tbase, mpd_t *texp, uint8_t resultsign, |
---|
6159 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6160 | n/a | { |
---|
6161 | n/a | uint32_t workstatus = 0; |
---|
6162 | n/a | mpd_context_t maxctx; |
---|
6163 | n/a | MPD_NEW_CONST(two,0,0,1,1,1,2); |
---|
6164 | n/a | |
---|
6165 | n/a | |
---|
6166 | n/a | mpd_maxcontext(&maxctx); |
---|
6167 | n/a | |
---|
6168 | n/a | /* resize to smaller cannot fail */ |
---|
6169 | n/a | mpd_qcopy(result, &one, status); |
---|
6170 | n/a | |
---|
6171 | n/a | while (!mpd_iszero(texp)) { |
---|
6172 | n/a | if (mpd_isodd(texp)) { |
---|
6173 | n/a | mpd_qmul(result, result, tbase, ctx, &workstatus); |
---|
6174 | n/a | *status |= workstatus; |
---|
6175 | n/a | if (mpd_isspecial(result) || |
---|
6176 | n/a | (mpd_iszerocoeff(result) && (workstatus & MPD_Clamped))) { |
---|
6177 | n/a | break; |
---|
6178 | n/a | } |
---|
6179 | n/a | } |
---|
6180 | n/a | mpd_qmul(tbase, tbase, tbase, ctx, &workstatus); |
---|
6181 | n/a | mpd_qdivint(texp, texp, &two, &maxctx, &workstatus); |
---|
6182 | n/a | if (mpd_isnan(tbase) || mpd_isnan(texp)) { |
---|
6183 | n/a | mpd_seterror(result, workstatus&MPD_Errors, status); |
---|
6184 | n/a | return; |
---|
6185 | n/a | } |
---|
6186 | n/a | } |
---|
6187 | n/a | mpd_set_sign(result, resultsign); |
---|
6188 | n/a | } |
---|
6189 | n/a | |
---|
6190 | n/a | /* |
---|
6191 | n/a | * The power function for integer exponents. Relative error _before_ the |
---|
6192 | n/a | * final rounding to prec: |
---|
6193 | n/a | * abs(result - base**exp) < 0.1 * 10**-prec * abs(base**exp) |
---|
6194 | n/a | */ |
---|
6195 | n/a | static void |
---|
6196 | n/a | _mpd_qpow_int(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6197 | n/a | uint8_t resultsign, |
---|
6198 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6199 | n/a | { |
---|
6200 | n/a | mpd_context_t workctx; |
---|
6201 | n/a | MPD_NEW_STATIC(tbase,0,0,0,0); |
---|
6202 | n/a | MPD_NEW_STATIC(texp,0,0,0,0); |
---|
6203 | n/a | mpd_ssize_t n; |
---|
6204 | n/a | |
---|
6205 | n/a | |
---|
6206 | n/a | mpd_workcontext(&workctx, ctx); |
---|
6207 | n/a | workctx.prec += (exp->digits + exp->exp + 2); |
---|
6208 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
6209 | n/a | workctx.clamp = 0; |
---|
6210 | n/a | if (mpd_isnegative(exp)) { |
---|
6211 | n/a | workctx.prec += 1; |
---|
6212 | n/a | mpd_qdiv(&tbase, &one, base, &workctx, status); |
---|
6213 | n/a | if (*status&MPD_Errors) { |
---|
6214 | n/a | mpd_setspecial(result, MPD_POS, MPD_NAN); |
---|
6215 | n/a | goto finish; |
---|
6216 | n/a | } |
---|
6217 | n/a | } |
---|
6218 | n/a | else { |
---|
6219 | n/a | if (!mpd_qcopy(&tbase, base, status)) { |
---|
6220 | n/a | mpd_setspecial(result, MPD_POS, MPD_NAN); |
---|
6221 | n/a | goto finish; |
---|
6222 | n/a | } |
---|
6223 | n/a | } |
---|
6224 | n/a | |
---|
6225 | n/a | n = mpd_qabs_uint(exp, &workctx.status); |
---|
6226 | n/a | if (workctx.status&MPD_Invalid_operation) { |
---|
6227 | n/a | if (!mpd_qcopy(&texp, exp, status)) { |
---|
6228 | n/a | mpd_setspecial(result, MPD_POS, MPD_NAN); /* GCOV_UNLIKELY */ |
---|
6229 | n/a | goto finish; /* GCOV_UNLIKELY */ |
---|
6230 | n/a | } |
---|
6231 | n/a | _mpd_qpow_mpd(result, &tbase, &texp, resultsign, &workctx, status); |
---|
6232 | n/a | } |
---|
6233 | n/a | else { |
---|
6234 | n/a | _mpd_qpow_uint(result, &tbase, n, resultsign, &workctx, status); |
---|
6235 | n/a | } |
---|
6236 | n/a | |
---|
6237 | n/a | if (mpd_isinfinite(result)) { |
---|
6238 | n/a | /* for ROUND_DOWN, ROUND_FLOOR, etc. */ |
---|
6239 | n/a | _settriple(result, resultsign, 1, MPD_EXP_INF); |
---|
6240 | n/a | } |
---|
6241 | n/a | |
---|
6242 | n/a | finish: |
---|
6243 | n/a | mpd_del(&tbase); |
---|
6244 | n/a | mpd_del(&texp); |
---|
6245 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6246 | n/a | } |
---|
6247 | n/a | |
---|
6248 | n/a | /* |
---|
6249 | n/a | * If the exponent is infinite and base equals one, the result is one |
---|
6250 | n/a | * with a coefficient of length prec. Otherwise, result is undefined. |
---|
6251 | n/a | * Return the value of the comparison against one. |
---|
6252 | n/a | */ |
---|
6253 | n/a | static int |
---|
6254 | n/a | _qcheck_pow_one_inf(mpd_t *result, const mpd_t *base, uint8_t resultsign, |
---|
6255 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6256 | n/a | { |
---|
6257 | n/a | mpd_ssize_t shift; |
---|
6258 | n/a | int cmp; |
---|
6259 | n/a | |
---|
6260 | n/a | if ((cmp = _mpd_cmp(base, &one)) == 0) { |
---|
6261 | n/a | shift = ctx->prec-1; |
---|
6262 | n/a | mpd_qshiftl(result, &one, shift, status); |
---|
6263 | n/a | result->exp = -shift; |
---|
6264 | n/a | mpd_set_flags(result, resultsign); |
---|
6265 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
6266 | n/a | } |
---|
6267 | n/a | |
---|
6268 | n/a | return cmp; |
---|
6269 | n/a | } |
---|
6270 | n/a | |
---|
6271 | n/a | /* |
---|
6272 | n/a | * If abs(base) equals one, calculate the correct power of one result. |
---|
6273 | n/a | * Otherwise, result is undefined. Return the value of the comparison |
---|
6274 | n/a | * against 1. |
---|
6275 | n/a | * |
---|
6276 | n/a | * This is an internal function that does not check for specials. |
---|
6277 | n/a | */ |
---|
6278 | n/a | static int |
---|
6279 | n/a | _qcheck_pow_one(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6280 | n/a | uint8_t resultsign, |
---|
6281 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6282 | n/a | { |
---|
6283 | n/a | uint32_t workstatus = 0; |
---|
6284 | n/a | mpd_ssize_t shift; |
---|
6285 | n/a | int cmp; |
---|
6286 | n/a | |
---|
6287 | n/a | if ((cmp = _mpd_cmp_abs(base, &one)) == 0) { |
---|
6288 | n/a | if (_mpd_isint(exp)) { |
---|
6289 | n/a | if (mpd_isnegative(exp)) { |
---|
6290 | n/a | _settriple(result, resultsign, 1, 0); |
---|
6291 | n/a | return 0; |
---|
6292 | n/a | } |
---|
6293 | n/a | /* 1.000**3 = 1.000000000 */ |
---|
6294 | n/a | mpd_qmul_ssize(result, exp, -base->exp, ctx, &workstatus); |
---|
6295 | n/a | if (workstatus&MPD_Errors) { |
---|
6296 | n/a | *status |= (workstatus&MPD_Errors); |
---|
6297 | n/a | return 0; |
---|
6298 | n/a | } |
---|
6299 | n/a | /* digits-1 after exponentiation */ |
---|
6300 | n/a | shift = mpd_qget_ssize(result, &workstatus); |
---|
6301 | n/a | /* shift is MPD_SSIZE_MAX if result is too large */ |
---|
6302 | n/a | if (shift > ctx->prec-1) { |
---|
6303 | n/a | shift = ctx->prec-1; |
---|
6304 | n/a | *status |= MPD_Rounded; |
---|
6305 | n/a | } |
---|
6306 | n/a | } |
---|
6307 | n/a | else if (mpd_ispositive(base)) { |
---|
6308 | n/a | shift = ctx->prec-1; |
---|
6309 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
6310 | n/a | } |
---|
6311 | n/a | else { |
---|
6312 | n/a | return -2; /* GCOV_NOT_REACHED */ |
---|
6313 | n/a | } |
---|
6314 | n/a | if (!mpd_qshiftl(result, &one, shift, status)) { |
---|
6315 | n/a | return 0; |
---|
6316 | n/a | } |
---|
6317 | n/a | result->exp = -shift; |
---|
6318 | n/a | mpd_set_flags(result, resultsign); |
---|
6319 | n/a | } |
---|
6320 | n/a | |
---|
6321 | n/a | return cmp; |
---|
6322 | n/a | } |
---|
6323 | n/a | |
---|
6324 | n/a | /* |
---|
6325 | n/a | * Detect certain over/underflow of x**y. |
---|
6326 | n/a | * ACL2 proof: pow-bounds.lisp. |
---|
6327 | n/a | * |
---|
6328 | n/a | * Symbols: |
---|
6329 | n/a | * |
---|
6330 | n/a | * e: EXP_INF or EXP_CLAMP |
---|
6331 | n/a | * x: base |
---|
6332 | n/a | * y: exponent |
---|
6333 | n/a | * |
---|
6334 | n/a | * omega(e) = log10(abs(e)) |
---|
6335 | n/a | * zeta(x) = log10(abs(log10(x))) |
---|
6336 | n/a | * theta(y) = log10(abs(y)) |
---|
6337 | n/a | * |
---|
6338 | n/a | * Upper and lower bounds: |
---|
6339 | n/a | * |
---|
6340 | n/a | * ub_omega(e) = ceil(log10(abs(e))) |
---|
6341 | n/a | * lb_theta(y) = floor(log10(abs(y))) |
---|
6342 | n/a | * |
---|
6343 | n/a | * | floor(log10(floor(abs(log10(x))))) if x < 1/10 or x >= 10 |
---|
6344 | n/a | * lb_zeta(x) = | floor(log10(abs(x-1)/10)) if 1/10 <= x < 1 |
---|
6345 | n/a | * | floor(log10(abs((x-1)/100))) if 1 < x < 10 |
---|
6346 | n/a | * |
---|
6347 | n/a | * ub_omega(e) and lb_theta(y) are obviously upper and lower bounds |
---|
6348 | n/a | * for omega(e) and theta(y). |
---|
6349 | n/a | * |
---|
6350 | n/a | * lb_zeta is a lower bound for zeta(x): |
---|
6351 | n/a | * |
---|
6352 | n/a | * x < 1/10 or x >= 10: |
---|
6353 | n/a | * |
---|
6354 | n/a | * abs(log10(x)) >= 1, so the outer log10 is well defined. Since log10 |
---|
6355 | n/a | * is strictly increasing, the end result is a lower bound. |
---|
6356 | n/a | * |
---|
6357 | n/a | * 1/10 <= x < 1: |
---|
6358 | n/a | * |
---|
6359 | n/a | * We use: log10(x) <= (x-1)/log(10) |
---|
6360 | n/a | * abs(log10(x)) >= abs(x-1)/log(10) |
---|
6361 | n/a | * abs(log10(x)) >= abs(x-1)/10 |
---|
6362 | n/a | * |
---|
6363 | n/a | * 1 < x < 10: |
---|
6364 | n/a | * |
---|
6365 | n/a | * We use: (x-1)/(x*log(10)) < log10(x) |
---|
6366 | n/a | * abs((x-1)/100) < abs(log10(x)) |
---|
6367 | n/a | * |
---|
6368 | n/a | * XXX: abs((x-1)/10) would work, need ACL2 proof. |
---|
6369 | n/a | * |
---|
6370 | n/a | * |
---|
6371 | n/a | * Let (0 < x < 1 and y < 0) or (x > 1 and y > 0). (H1) |
---|
6372 | n/a | * Let ub_omega(exp_inf) < lb_zeta(x) + lb_theta(y) (H2) |
---|
6373 | n/a | * |
---|
6374 | n/a | * Then: |
---|
6375 | n/a | * log10(abs(exp_inf)) < log10(abs(log10(x))) + log10(abs(y)). (1) |
---|
6376 | n/a | * exp_inf < log10(x) * y (2) |
---|
6377 | n/a | * 10**exp_inf < x**y (3) |
---|
6378 | n/a | * |
---|
6379 | n/a | * Let (0 < x < 1 and y > 0) or (x > 1 and y < 0). (H3) |
---|
6380 | n/a | * Let ub_omega(exp_clamp) < lb_zeta(x) + lb_theta(y) (H4) |
---|
6381 | n/a | * |
---|
6382 | n/a | * Then: |
---|
6383 | n/a | * log10(abs(exp_clamp)) < log10(abs(log10(x))) + log10(abs(y)). (4) |
---|
6384 | n/a | * log10(x) * y < exp_clamp (5) |
---|
6385 | n/a | * x**y < 10**exp_clamp (6) |
---|
6386 | n/a | * |
---|
6387 | n/a | */ |
---|
6388 | n/a | static mpd_ssize_t |
---|
6389 | n/a | _lower_bound_zeta(const mpd_t *x, uint32_t *status) |
---|
6390 | n/a | { |
---|
6391 | n/a | mpd_context_t maxctx; |
---|
6392 | n/a | MPD_NEW_STATIC(scratch,0,0,0,0); |
---|
6393 | n/a | mpd_ssize_t t, u; |
---|
6394 | n/a | |
---|
6395 | n/a | t = mpd_adjexp(x); |
---|
6396 | n/a | if (t > 0) { |
---|
6397 | n/a | /* x >= 10 -> floor(log10(floor(abs(log10(x))))) */ |
---|
6398 | n/a | return mpd_exp_digits(t) - 1; |
---|
6399 | n/a | } |
---|
6400 | n/a | else if (t < -1) { |
---|
6401 | n/a | /* x < 1/10 -> floor(log10(floor(abs(log10(x))))) */ |
---|
6402 | n/a | return mpd_exp_digits(t+1) - 1; |
---|
6403 | n/a | } |
---|
6404 | n/a | else { |
---|
6405 | n/a | mpd_maxcontext(&maxctx); |
---|
6406 | n/a | mpd_qsub(&scratch, x, &one, &maxctx, status); |
---|
6407 | n/a | if (mpd_isspecial(&scratch)) { |
---|
6408 | n/a | mpd_del(&scratch); |
---|
6409 | n/a | return MPD_SSIZE_MAX; |
---|
6410 | n/a | } |
---|
6411 | n/a | u = mpd_adjexp(&scratch); |
---|
6412 | n/a | mpd_del(&scratch); |
---|
6413 | n/a | |
---|
6414 | n/a | /* t == -1, 1/10 <= x < 1 -> floor(log10(abs(x-1)/10)) |
---|
6415 | n/a | * t == 0, 1 < x < 10 -> floor(log10(abs(x-1)/100)) */ |
---|
6416 | n/a | return (t == 0) ? u-2 : u-1; |
---|
6417 | n/a | } |
---|
6418 | n/a | } |
---|
6419 | n/a | |
---|
6420 | n/a | /* |
---|
6421 | n/a | * Detect cases of certain overflow/underflow in the power function. |
---|
6422 | n/a | * Assumptions: x != 1, y != 0. The proof above is for positive x. |
---|
6423 | n/a | * If x is negative and y is an odd integer, x**y == -(abs(x)**y), |
---|
6424 | n/a | * so the analysis does not change. |
---|
6425 | n/a | */ |
---|
6426 | n/a | static int |
---|
6427 | n/a | _qcheck_pow_bounds(mpd_t *result, const mpd_t *x, const mpd_t *y, |
---|
6428 | n/a | uint8_t resultsign, |
---|
6429 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6430 | n/a | { |
---|
6431 | n/a | MPD_NEW_SHARED(abs_x, x); |
---|
6432 | n/a | mpd_ssize_t ub_omega, lb_zeta, lb_theta; |
---|
6433 | n/a | uint8_t sign; |
---|
6434 | n/a | |
---|
6435 | n/a | mpd_set_positive(&abs_x); |
---|
6436 | n/a | |
---|
6437 | n/a | lb_theta = mpd_adjexp(y); |
---|
6438 | n/a | lb_zeta = _lower_bound_zeta(&abs_x, status); |
---|
6439 | n/a | if (lb_zeta == MPD_SSIZE_MAX) { |
---|
6440 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
6441 | n/a | return 1; |
---|
6442 | n/a | } |
---|
6443 | n/a | |
---|
6444 | n/a | sign = (mpd_adjexp(&abs_x) < 0) ^ mpd_sign(y); |
---|
6445 | n/a | if (sign == 0) { |
---|
6446 | n/a | /* (0 < |x| < 1 and y < 0) or (|x| > 1 and y > 0) */ |
---|
6447 | n/a | ub_omega = mpd_exp_digits(ctx->emax); |
---|
6448 | n/a | if (ub_omega < lb_zeta + lb_theta) { |
---|
6449 | n/a | _settriple(result, resultsign, 1, MPD_EXP_INF); |
---|
6450 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6451 | n/a | return 1; |
---|
6452 | n/a | } |
---|
6453 | n/a | } |
---|
6454 | n/a | else { |
---|
6455 | n/a | /* (0 < |x| < 1 and y > 0) or (|x| > 1 and y < 0). */ |
---|
6456 | n/a | ub_omega = mpd_exp_digits(mpd_etiny(ctx)); |
---|
6457 | n/a | if (ub_omega < lb_zeta + lb_theta) { |
---|
6458 | n/a | _settriple(result, resultsign, 1, mpd_etiny(ctx)-1); |
---|
6459 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6460 | n/a | return 1; |
---|
6461 | n/a | } |
---|
6462 | n/a | } |
---|
6463 | n/a | |
---|
6464 | n/a | return 0; |
---|
6465 | n/a | } |
---|
6466 | n/a | |
---|
6467 | n/a | /* |
---|
6468 | n/a | * TODO: Implement algorithm for computing exact powers from decimal.py. |
---|
6469 | n/a | * In order to prevent infinite loops, this has to be called before |
---|
6470 | n/a | * using Ziv's strategy for correct rounding. |
---|
6471 | n/a | */ |
---|
6472 | n/a | /* |
---|
6473 | n/a | static int |
---|
6474 | n/a | _mpd_qpow_exact(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6475 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6476 | n/a | { |
---|
6477 | n/a | return 0; |
---|
6478 | n/a | } |
---|
6479 | n/a | */ |
---|
6480 | n/a | |
---|
6481 | n/a | /* |
---|
6482 | n/a | * The power function for real exponents. |
---|
6483 | n/a | * Relative error: abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) |
---|
6484 | n/a | */ |
---|
6485 | n/a | static void |
---|
6486 | n/a | _mpd_qpow_real(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6487 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6488 | n/a | { |
---|
6489 | n/a | mpd_context_t workctx; |
---|
6490 | n/a | MPD_NEW_STATIC(texp,0,0,0,0); |
---|
6491 | n/a | |
---|
6492 | n/a | if (!mpd_qcopy(&texp, exp, status)) { |
---|
6493 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
6494 | n/a | return; |
---|
6495 | n/a | } |
---|
6496 | n/a | |
---|
6497 | n/a | mpd_maxcontext(&workctx); |
---|
6498 | n/a | workctx.prec = (base->digits > ctx->prec) ? base->digits : ctx->prec; |
---|
6499 | n/a | workctx.prec += (4 + MPD_EXPDIGITS); |
---|
6500 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
6501 | n/a | workctx.allcr = ctx->allcr; |
---|
6502 | n/a | |
---|
6503 | n/a | /* |
---|
6504 | n/a | * extra := MPD_EXPDIGITS = MPD_EXP_MAX_T |
---|
6505 | n/a | * wp := prec + 4 + extra |
---|
6506 | n/a | * abs(err) < 5 * 10**-wp |
---|
6507 | n/a | * y := log(base) * exp |
---|
6508 | n/a | * Calculate: |
---|
6509 | n/a | * 1) e**(y * (1 + err)**2) * (1 + err) |
---|
6510 | n/a | * = e**y * e**(y * (2*err + err**2)) * (1 + err) |
---|
6511 | n/a | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
---|
6512 | n/a | * Relative error of the underlined term: |
---|
6513 | n/a | * 2) abs(e**(y * (2*err + err**2)) - 1) |
---|
6514 | n/a | * Case abs(y) >= 10**extra: |
---|
6515 | n/a | * 3) adjexp(y)+1 > log10(abs(y)) >= extra |
---|
6516 | n/a | * This triggers the Overflow/Underflow shortcut in _mpd_qexp(), |
---|
6517 | n/a | * so no further analysis is necessary. |
---|
6518 | n/a | * Case abs(y) < 10**extra: |
---|
6519 | n/a | * 4) abs(y * (2*err + err**2)) < 1/5 * 10**(-prec - 2) |
---|
6520 | n/a | * Use (see _mpd_qexp): |
---|
6521 | n/a | * 5) abs(x) <= 9/10 * 10**-p ==> abs(e**x - 1) < 10**-p |
---|
6522 | n/a | * With 2), 4) and 5): |
---|
6523 | n/a | * 6) abs(e**(y * (2*err + err**2)) - 1) < 10**(-prec - 2) |
---|
6524 | n/a | * The complete relative error of 1) is: |
---|
6525 | n/a | * 7) abs(result - e**y) < e**y * 1/5 * 10**(-prec - 1) |
---|
6526 | n/a | */ |
---|
6527 | n/a | mpd_qln(result, base, &workctx, &workctx.status); |
---|
6528 | n/a | mpd_qmul(result, result, &texp, &workctx, &workctx.status); |
---|
6529 | n/a | mpd_qexp(result, result, &workctx, status); |
---|
6530 | n/a | |
---|
6531 | n/a | mpd_del(&texp); |
---|
6532 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
6533 | n/a | *status |= (MPD_Inexact|MPD_Rounded); |
---|
6534 | n/a | } |
---|
6535 | n/a | |
---|
6536 | n/a | /* The power function: base**exp */ |
---|
6537 | n/a | void |
---|
6538 | n/a | mpd_qpow(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6539 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6540 | n/a | { |
---|
6541 | n/a | uint8_t resultsign = 0; |
---|
6542 | n/a | int intexp = 0; |
---|
6543 | n/a | int cmp; |
---|
6544 | n/a | |
---|
6545 | n/a | if (mpd_isspecial(base) || mpd_isspecial(exp)) { |
---|
6546 | n/a | if (mpd_qcheck_nans(result, base, exp, ctx, status)) { |
---|
6547 | n/a | return; |
---|
6548 | n/a | } |
---|
6549 | n/a | } |
---|
6550 | n/a | if (mpd_isinteger(exp)) { |
---|
6551 | n/a | intexp = 1; |
---|
6552 | n/a | resultsign = mpd_isnegative(base) && mpd_isodd(exp); |
---|
6553 | n/a | } |
---|
6554 | n/a | |
---|
6555 | n/a | if (mpd_iszero(base)) { |
---|
6556 | n/a | if (mpd_iszero(exp)) { |
---|
6557 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6558 | n/a | } |
---|
6559 | n/a | else if (mpd_isnegative(exp)) { |
---|
6560 | n/a | mpd_setspecial(result, resultsign, MPD_INF); |
---|
6561 | n/a | } |
---|
6562 | n/a | else { |
---|
6563 | n/a | _settriple(result, resultsign, 0, 0); |
---|
6564 | n/a | } |
---|
6565 | n/a | return; |
---|
6566 | n/a | } |
---|
6567 | n/a | if (mpd_isnegative(base)) { |
---|
6568 | n/a | if (!intexp || mpd_isinfinite(exp)) { |
---|
6569 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6570 | n/a | return; |
---|
6571 | n/a | } |
---|
6572 | n/a | } |
---|
6573 | n/a | if (mpd_isinfinite(exp)) { |
---|
6574 | n/a | /* power of one */ |
---|
6575 | n/a | cmp = _qcheck_pow_one_inf(result, base, resultsign, ctx, status); |
---|
6576 | n/a | if (cmp == 0) { |
---|
6577 | n/a | return; |
---|
6578 | n/a | } |
---|
6579 | n/a | else { |
---|
6580 | n/a | cmp *= mpd_arith_sign(exp); |
---|
6581 | n/a | if (cmp < 0) { |
---|
6582 | n/a | _settriple(result, resultsign, 0, 0); |
---|
6583 | n/a | } |
---|
6584 | n/a | else { |
---|
6585 | n/a | mpd_setspecial(result, resultsign, MPD_INF); |
---|
6586 | n/a | } |
---|
6587 | n/a | } |
---|
6588 | n/a | return; |
---|
6589 | n/a | } |
---|
6590 | n/a | if (mpd_isinfinite(base)) { |
---|
6591 | n/a | if (mpd_iszero(exp)) { |
---|
6592 | n/a | _settriple(result, resultsign, 1, 0); |
---|
6593 | n/a | } |
---|
6594 | n/a | else if (mpd_isnegative(exp)) { |
---|
6595 | n/a | _settriple(result, resultsign, 0, 0); |
---|
6596 | n/a | } |
---|
6597 | n/a | else { |
---|
6598 | n/a | mpd_setspecial(result, resultsign, MPD_INF); |
---|
6599 | n/a | } |
---|
6600 | n/a | return; |
---|
6601 | n/a | } |
---|
6602 | n/a | if (mpd_iszero(exp)) { |
---|
6603 | n/a | _settriple(result, resultsign, 1, 0); |
---|
6604 | n/a | return; |
---|
6605 | n/a | } |
---|
6606 | n/a | if (_qcheck_pow_one(result, base, exp, resultsign, ctx, status) == 0) { |
---|
6607 | n/a | return; |
---|
6608 | n/a | } |
---|
6609 | n/a | if (_qcheck_pow_bounds(result, base, exp, resultsign, ctx, status)) { |
---|
6610 | n/a | return; |
---|
6611 | n/a | } |
---|
6612 | n/a | |
---|
6613 | n/a | if (intexp) { |
---|
6614 | n/a | _mpd_qpow_int(result, base, exp, resultsign, ctx, status); |
---|
6615 | n/a | } |
---|
6616 | n/a | else { |
---|
6617 | n/a | _mpd_qpow_real(result, base, exp, ctx, status); |
---|
6618 | n/a | if (!mpd_isspecial(result) && _mpd_cmp(result, &one) == 0) { |
---|
6619 | n/a | mpd_ssize_t shift = ctx->prec-1; |
---|
6620 | n/a | mpd_qshiftl(result, &one, shift, status); |
---|
6621 | n/a | result->exp = -shift; |
---|
6622 | n/a | } |
---|
6623 | n/a | if (mpd_isinfinite(result)) { |
---|
6624 | n/a | /* for ROUND_DOWN, ROUND_FLOOR, etc. */ |
---|
6625 | n/a | _settriple(result, MPD_POS, 1, MPD_EXP_INF); |
---|
6626 | n/a | } |
---|
6627 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6628 | n/a | } |
---|
6629 | n/a | } |
---|
6630 | n/a | |
---|
6631 | n/a | /* |
---|
6632 | n/a | * Internal function: Integer powmod with mpd_uint_t exponent, base is modified! |
---|
6633 | n/a | * Function can fail with MPD_Malloc_error. |
---|
6634 | n/a | */ |
---|
6635 | n/a | static inline void |
---|
6636 | n/a | _mpd_qpowmod_uint(mpd_t *result, mpd_t *base, mpd_uint_t exp, |
---|
6637 | n/a | const mpd_t *mod, uint32_t *status) |
---|
6638 | n/a | { |
---|
6639 | n/a | mpd_context_t maxcontext; |
---|
6640 | n/a | |
---|
6641 | n/a | mpd_maxcontext(&maxcontext); |
---|
6642 | n/a | |
---|
6643 | n/a | /* resize to smaller cannot fail */ |
---|
6644 | n/a | mpd_qcopy(result, &one, status); |
---|
6645 | n/a | |
---|
6646 | n/a | while (exp > 0) { |
---|
6647 | n/a | if (exp & 1) { |
---|
6648 | n/a | _mpd_qmul_exact(result, result, base, &maxcontext, status); |
---|
6649 | n/a | mpd_qrem(result, result, mod, &maxcontext, status); |
---|
6650 | n/a | } |
---|
6651 | n/a | _mpd_qmul_exact(base, base, base, &maxcontext, status); |
---|
6652 | n/a | mpd_qrem(base, base, mod, &maxcontext, status); |
---|
6653 | n/a | exp >>= 1; |
---|
6654 | n/a | } |
---|
6655 | n/a | } |
---|
6656 | n/a | |
---|
6657 | n/a | /* The powmod function: (base**exp) % mod */ |
---|
6658 | n/a | void |
---|
6659 | n/a | mpd_qpowmod(mpd_t *result, const mpd_t *base, const mpd_t *exp, |
---|
6660 | n/a | const mpd_t *mod, |
---|
6661 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6662 | n/a | { |
---|
6663 | n/a | mpd_context_t maxcontext; |
---|
6664 | n/a | MPD_NEW_STATIC(tbase,0,0,0,0); |
---|
6665 | n/a | MPD_NEW_STATIC(texp,0,0,0,0); |
---|
6666 | n/a | MPD_NEW_STATIC(tmod,0,0,0,0); |
---|
6667 | n/a | MPD_NEW_STATIC(tmp,0,0,0,0); |
---|
6668 | n/a | MPD_NEW_CONST(two,0,0,1,1,1,2); |
---|
6669 | n/a | mpd_ssize_t tbase_exp, texp_exp; |
---|
6670 | n/a | mpd_ssize_t i; |
---|
6671 | n/a | mpd_t t; |
---|
6672 | n/a | mpd_uint_t r; |
---|
6673 | n/a | uint8_t sign; |
---|
6674 | n/a | |
---|
6675 | n/a | |
---|
6676 | n/a | if (mpd_isspecial(base) || mpd_isspecial(exp) || mpd_isspecial(mod)) { |
---|
6677 | n/a | if (mpd_qcheck_3nans(result, base, exp, mod, ctx, status)) { |
---|
6678 | n/a | return; |
---|
6679 | n/a | } |
---|
6680 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6681 | n/a | return; |
---|
6682 | n/a | } |
---|
6683 | n/a | |
---|
6684 | n/a | |
---|
6685 | n/a | if (!_mpd_isint(base) || !_mpd_isint(exp) || !_mpd_isint(mod)) { |
---|
6686 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6687 | n/a | return; |
---|
6688 | n/a | } |
---|
6689 | n/a | if (mpd_iszerocoeff(mod)) { |
---|
6690 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6691 | n/a | return; |
---|
6692 | n/a | } |
---|
6693 | n/a | if (mod->digits+mod->exp > ctx->prec) { |
---|
6694 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6695 | n/a | return; |
---|
6696 | n/a | } |
---|
6697 | n/a | |
---|
6698 | n/a | sign = (mpd_isnegative(base)) && (mpd_isodd(exp)); |
---|
6699 | n/a | if (mpd_iszerocoeff(exp)) { |
---|
6700 | n/a | if (mpd_iszerocoeff(base)) { |
---|
6701 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6702 | n/a | return; |
---|
6703 | n/a | } |
---|
6704 | n/a | r = (_mpd_cmp_abs(mod, &one)==0) ? 0 : 1; |
---|
6705 | n/a | _settriple(result, sign, r, 0); |
---|
6706 | n/a | return; |
---|
6707 | n/a | } |
---|
6708 | n/a | if (mpd_isnegative(exp)) { |
---|
6709 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6710 | n/a | return; |
---|
6711 | n/a | } |
---|
6712 | n/a | if (mpd_iszerocoeff(base)) { |
---|
6713 | n/a | _settriple(result, sign, 0, 0); |
---|
6714 | n/a | return; |
---|
6715 | n/a | } |
---|
6716 | n/a | |
---|
6717 | n/a | mpd_maxcontext(&maxcontext); |
---|
6718 | n/a | |
---|
6719 | n/a | mpd_qrescale(&tmod, mod, 0, &maxcontext, &maxcontext.status); |
---|
6720 | n/a | if (maxcontext.status&MPD_Errors) { |
---|
6721 | n/a | mpd_seterror(result, maxcontext.status&MPD_Errors, status); |
---|
6722 | n/a | goto out; |
---|
6723 | n/a | } |
---|
6724 | n/a | maxcontext.status = 0; |
---|
6725 | n/a | mpd_set_positive(&tmod); |
---|
6726 | n/a | |
---|
6727 | n/a | mpd_qround_to_int(&tbase, base, &maxcontext, status); |
---|
6728 | n/a | mpd_set_positive(&tbase); |
---|
6729 | n/a | tbase_exp = tbase.exp; |
---|
6730 | n/a | tbase.exp = 0; |
---|
6731 | n/a | |
---|
6732 | n/a | mpd_qround_to_int(&texp, exp, &maxcontext, status); |
---|
6733 | n/a | texp_exp = texp.exp; |
---|
6734 | n/a | texp.exp = 0; |
---|
6735 | n/a | |
---|
6736 | n/a | /* base = (base.int % modulo * pow(10, base.exp, modulo)) % modulo */ |
---|
6737 | n/a | mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); |
---|
6738 | n/a | mpd_qshiftl(result, &one, tbase_exp, status); |
---|
6739 | n/a | mpd_qrem(result, result, &tmod, &maxcontext, status); |
---|
6740 | n/a | _mpd_qmul_exact(&tbase, &tbase, result, &maxcontext, status); |
---|
6741 | n/a | mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); |
---|
6742 | n/a | if (mpd_isspecial(&tbase) || |
---|
6743 | n/a | mpd_isspecial(&texp) || |
---|
6744 | n/a | mpd_isspecial(&tmod)) { |
---|
6745 | n/a | goto mpd_errors; |
---|
6746 | n/a | } |
---|
6747 | n/a | |
---|
6748 | n/a | for (i = 0; i < texp_exp; i++) { |
---|
6749 | n/a | _mpd_qpowmod_uint(&tmp, &tbase, 10, &tmod, status); |
---|
6750 | n/a | t = tmp; |
---|
6751 | n/a | tmp = tbase; |
---|
6752 | n/a | tbase = t; |
---|
6753 | n/a | } |
---|
6754 | n/a | if (mpd_isspecial(&tbase)) { |
---|
6755 | n/a | goto mpd_errors; /* GCOV_UNLIKELY */ |
---|
6756 | n/a | } |
---|
6757 | n/a | |
---|
6758 | n/a | /* resize to smaller cannot fail */ |
---|
6759 | n/a | mpd_qcopy(result, &one, status); |
---|
6760 | n/a | while (mpd_isfinite(&texp) && !mpd_iszero(&texp)) { |
---|
6761 | n/a | if (mpd_isodd(&texp)) { |
---|
6762 | n/a | _mpd_qmul_exact(result, result, &tbase, &maxcontext, status); |
---|
6763 | n/a | mpd_qrem(result, result, &tmod, &maxcontext, status); |
---|
6764 | n/a | } |
---|
6765 | n/a | _mpd_qmul_exact(&tbase, &tbase, &tbase, &maxcontext, status); |
---|
6766 | n/a | mpd_qrem(&tbase, &tbase, &tmod, &maxcontext, status); |
---|
6767 | n/a | mpd_qdivint(&texp, &texp, &two, &maxcontext, status); |
---|
6768 | n/a | } |
---|
6769 | n/a | if (mpd_isspecial(&texp) || mpd_isspecial(&tbase) || |
---|
6770 | n/a | mpd_isspecial(&tmod) || mpd_isspecial(result)) { |
---|
6771 | n/a | /* MPD_Malloc_error */ |
---|
6772 | n/a | goto mpd_errors; |
---|
6773 | n/a | } |
---|
6774 | n/a | else { |
---|
6775 | n/a | mpd_set_sign(result, sign); |
---|
6776 | n/a | } |
---|
6777 | n/a | |
---|
6778 | n/a | out: |
---|
6779 | n/a | mpd_del(&tbase); |
---|
6780 | n/a | mpd_del(&texp); |
---|
6781 | n/a | mpd_del(&tmod); |
---|
6782 | n/a | mpd_del(&tmp); |
---|
6783 | n/a | return; |
---|
6784 | n/a | |
---|
6785 | n/a | mpd_errors: |
---|
6786 | n/a | mpd_setspecial(result, MPD_POS, MPD_NAN); |
---|
6787 | n/a | goto out; |
---|
6788 | n/a | } |
---|
6789 | n/a | |
---|
6790 | n/a | void |
---|
6791 | n/a | mpd_qquantize(mpd_t *result, const mpd_t *a, const mpd_t *b, |
---|
6792 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6793 | n/a | { |
---|
6794 | n/a | uint32_t workstatus = 0; |
---|
6795 | n/a | mpd_ssize_t b_exp = b->exp; |
---|
6796 | n/a | mpd_ssize_t expdiff, shift; |
---|
6797 | n/a | mpd_uint_t rnd; |
---|
6798 | n/a | |
---|
6799 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
6800 | n/a | if (mpd_qcheck_nans(result, a, b, ctx, status)) { |
---|
6801 | n/a | return; |
---|
6802 | n/a | } |
---|
6803 | n/a | if (mpd_isinfinite(a) && mpd_isinfinite(b)) { |
---|
6804 | n/a | mpd_qcopy(result, a, status); |
---|
6805 | n/a | return; |
---|
6806 | n/a | } |
---|
6807 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6808 | n/a | return; |
---|
6809 | n/a | } |
---|
6810 | n/a | |
---|
6811 | n/a | if (b->exp > ctx->emax || b->exp < mpd_etiny(ctx)) { |
---|
6812 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6813 | n/a | return; |
---|
6814 | n/a | } |
---|
6815 | n/a | |
---|
6816 | n/a | if (mpd_iszero(a)) { |
---|
6817 | n/a | _settriple(result, mpd_sign(a), 0, b->exp); |
---|
6818 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6819 | n/a | return; |
---|
6820 | n/a | } |
---|
6821 | n/a | |
---|
6822 | n/a | |
---|
6823 | n/a | expdiff = a->exp - b->exp; |
---|
6824 | n/a | if (a->digits + expdiff > ctx->prec) { |
---|
6825 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6826 | n/a | return; |
---|
6827 | n/a | } |
---|
6828 | n/a | |
---|
6829 | n/a | if (expdiff >= 0) { |
---|
6830 | n/a | shift = expdiff; |
---|
6831 | n/a | if (!mpd_qshiftl(result, a, shift, status)) { |
---|
6832 | n/a | return; |
---|
6833 | n/a | } |
---|
6834 | n/a | result->exp = b_exp; |
---|
6835 | n/a | } |
---|
6836 | n/a | else { |
---|
6837 | n/a | /* At this point expdiff < 0 and a->digits+expdiff <= prec, |
---|
6838 | n/a | * so the shift before an increment will fit in prec. */ |
---|
6839 | n/a | shift = -expdiff; |
---|
6840 | n/a | rnd = mpd_qshiftr(result, a, shift, status); |
---|
6841 | n/a | if (rnd == MPD_UINT_MAX) { |
---|
6842 | n/a | return; |
---|
6843 | n/a | } |
---|
6844 | n/a | result->exp = b_exp; |
---|
6845 | n/a | if (!_mpd_apply_round_fit(result, rnd, ctx, status)) { |
---|
6846 | n/a | return; |
---|
6847 | n/a | } |
---|
6848 | n/a | workstatus |= MPD_Rounded; |
---|
6849 | n/a | if (rnd) { |
---|
6850 | n/a | workstatus |= MPD_Inexact; |
---|
6851 | n/a | } |
---|
6852 | n/a | } |
---|
6853 | n/a | |
---|
6854 | n/a | if (mpd_adjexp(result) > ctx->emax || |
---|
6855 | n/a | mpd_adjexp(result) < mpd_etiny(ctx)) { |
---|
6856 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
6857 | n/a | return; |
---|
6858 | n/a | } |
---|
6859 | n/a | |
---|
6860 | n/a | *status |= workstatus; |
---|
6861 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6862 | n/a | } |
---|
6863 | n/a | |
---|
6864 | n/a | void |
---|
6865 | n/a | mpd_qreduce(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
6866 | n/a | uint32_t *status) |
---|
6867 | n/a | { |
---|
6868 | n/a | mpd_ssize_t shift, maxexp, maxshift; |
---|
6869 | n/a | uint8_t sign_a = mpd_sign(a); |
---|
6870 | n/a | |
---|
6871 | n/a | if (mpd_isspecial(a)) { |
---|
6872 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
6873 | n/a | return; |
---|
6874 | n/a | } |
---|
6875 | n/a | mpd_qcopy(result, a, status); |
---|
6876 | n/a | return; |
---|
6877 | n/a | } |
---|
6878 | n/a | |
---|
6879 | n/a | if (!mpd_qcopy(result, a, status)) { |
---|
6880 | n/a | return; |
---|
6881 | n/a | } |
---|
6882 | n/a | mpd_qfinalize(result, ctx, status); |
---|
6883 | n/a | if (mpd_isspecial(result)) { |
---|
6884 | n/a | return; |
---|
6885 | n/a | } |
---|
6886 | n/a | if (mpd_iszero(result)) { |
---|
6887 | n/a | _settriple(result, sign_a, 0, 0); |
---|
6888 | n/a | return; |
---|
6889 | n/a | } |
---|
6890 | n/a | |
---|
6891 | n/a | shift = mpd_trail_zeros(result); |
---|
6892 | n/a | maxexp = (ctx->clamp) ? mpd_etop(ctx) : ctx->emax; |
---|
6893 | n/a | /* After the finalizing above result->exp <= maxexp. */ |
---|
6894 | n/a | maxshift = maxexp - result->exp; |
---|
6895 | n/a | shift = (shift > maxshift) ? maxshift : shift; |
---|
6896 | n/a | |
---|
6897 | n/a | mpd_qshiftr_inplace(result, shift); |
---|
6898 | n/a | result->exp += shift; |
---|
6899 | n/a | } |
---|
6900 | n/a | |
---|
6901 | n/a | void |
---|
6902 | n/a | mpd_qrem(mpd_t *r, const mpd_t *a, const mpd_t *b, const mpd_context_t *ctx, |
---|
6903 | n/a | uint32_t *status) |
---|
6904 | n/a | { |
---|
6905 | n/a | MPD_NEW_STATIC(q,0,0,0,0); |
---|
6906 | n/a | |
---|
6907 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
6908 | n/a | if (mpd_qcheck_nans(r, a, b, ctx, status)) { |
---|
6909 | n/a | return; |
---|
6910 | n/a | } |
---|
6911 | n/a | if (mpd_isinfinite(a)) { |
---|
6912 | n/a | mpd_seterror(r, MPD_Invalid_operation, status); |
---|
6913 | n/a | return; |
---|
6914 | n/a | } |
---|
6915 | n/a | if (mpd_isinfinite(b)) { |
---|
6916 | n/a | mpd_qcopy(r, a, status); |
---|
6917 | n/a | mpd_qfinalize(r, ctx, status); |
---|
6918 | n/a | return; |
---|
6919 | n/a | } |
---|
6920 | n/a | /* debug */ |
---|
6921 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
6922 | n/a | } |
---|
6923 | n/a | if (mpd_iszerocoeff(b)) { |
---|
6924 | n/a | if (mpd_iszerocoeff(a)) { |
---|
6925 | n/a | mpd_seterror(r, MPD_Division_undefined, status); |
---|
6926 | n/a | } |
---|
6927 | n/a | else { |
---|
6928 | n/a | mpd_seterror(r, MPD_Invalid_operation, status); |
---|
6929 | n/a | } |
---|
6930 | n/a | return; |
---|
6931 | n/a | } |
---|
6932 | n/a | |
---|
6933 | n/a | _mpd_qdivmod(&q, r, a, b, ctx, status); |
---|
6934 | n/a | mpd_del(&q); |
---|
6935 | n/a | mpd_qfinalize(r, ctx, status); |
---|
6936 | n/a | } |
---|
6937 | n/a | |
---|
6938 | n/a | void |
---|
6939 | n/a | mpd_qrem_near(mpd_t *r, const mpd_t *a, const mpd_t *b, |
---|
6940 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
6941 | n/a | { |
---|
6942 | n/a | mpd_context_t workctx; |
---|
6943 | n/a | MPD_NEW_STATIC(btmp,0,0,0,0); |
---|
6944 | n/a | MPD_NEW_STATIC(q,0,0,0,0); |
---|
6945 | n/a | mpd_ssize_t expdiff, qdigits; |
---|
6946 | n/a | int cmp, isodd, allnine; |
---|
6947 | n/a | |
---|
6948 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
6949 | n/a | if (mpd_qcheck_nans(r, a, b, ctx, status)) { |
---|
6950 | n/a | return; |
---|
6951 | n/a | } |
---|
6952 | n/a | if (mpd_isinfinite(a)) { |
---|
6953 | n/a | mpd_seterror(r, MPD_Invalid_operation, status); |
---|
6954 | n/a | return; |
---|
6955 | n/a | } |
---|
6956 | n/a | if (mpd_isinfinite(b)) { |
---|
6957 | n/a | mpd_qcopy(r, a, status); |
---|
6958 | n/a | mpd_qfinalize(r, ctx, status); |
---|
6959 | n/a | return; |
---|
6960 | n/a | } |
---|
6961 | n/a | /* debug */ |
---|
6962 | n/a | abort(); /* GCOV_NOT_REACHED */ |
---|
6963 | n/a | } |
---|
6964 | n/a | if (mpd_iszerocoeff(b)) { |
---|
6965 | n/a | if (mpd_iszerocoeff(a)) { |
---|
6966 | n/a | mpd_seterror(r, MPD_Division_undefined, status); |
---|
6967 | n/a | } |
---|
6968 | n/a | else { |
---|
6969 | n/a | mpd_seterror(r, MPD_Invalid_operation, status); |
---|
6970 | n/a | } |
---|
6971 | n/a | return; |
---|
6972 | n/a | } |
---|
6973 | n/a | |
---|
6974 | n/a | if (r == b) { |
---|
6975 | n/a | if (!mpd_qcopy(&btmp, b, status)) { |
---|
6976 | n/a | mpd_seterror(r, MPD_Malloc_error, status); |
---|
6977 | n/a | return; |
---|
6978 | n/a | } |
---|
6979 | n/a | b = &btmp; |
---|
6980 | n/a | } |
---|
6981 | n/a | |
---|
6982 | n/a | _mpd_qdivmod(&q, r, a, b, ctx, status); |
---|
6983 | n/a | if (mpd_isnan(&q) || mpd_isnan(r)) { |
---|
6984 | n/a | goto finish; |
---|
6985 | n/a | } |
---|
6986 | n/a | if (mpd_iszerocoeff(r)) { |
---|
6987 | n/a | goto finish; |
---|
6988 | n/a | } |
---|
6989 | n/a | |
---|
6990 | n/a | expdiff = mpd_adjexp(b) - mpd_adjexp(r); |
---|
6991 | n/a | if (-1 <= expdiff && expdiff <= 1) { |
---|
6992 | n/a | |
---|
6993 | n/a | allnine = mpd_coeff_isallnine(&q); |
---|
6994 | n/a | qdigits = q.digits; |
---|
6995 | n/a | isodd = mpd_isodd(&q); |
---|
6996 | n/a | |
---|
6997 | n/a | mpd_maxcontext(&workctx); |
---|
6998 | n/a | if (mpd_sign(a) == mpd_sign(b)) { |
---|
6999 | n/a | /* sign(r) == sign(b) */ |
---|
7000 | n/a | _mpd_qsub(&q, r, b, &workctx, &workctx.status); |
---|
7001 | n/a | } |
---|
7002 | n/a | else { |
---|
7003 | n/a | /* sign(r) != sign(b) */ |
---|
7004 | n/a | _mpd_qadd(&q, r, b, &workctx, &workctx.status); |
---|
7005 | n/a | } |
---|
7006 | n/a | |
---|
7007 | n/a | if (workctx.status&MPD_Errors) { |
---|
7008 | n/a | mpd_seterror(r, workctx.status&MPD_Errors, status); |
---|
7009 | n/a | goto finish; |
---|
7010 | n/a | } |
---|
7011 | n/a | |
---|
7012 | n/a | cmp = _mpd_cmp_abs(&q, r); |
---|
7013 | n/a | if (cmp < 0 || (cmp == 0 && isodd)) { |
---|
7014 | n/a | /* abs(r) > abs(b)/2 or abs(r) == abs(b)/2 and isodd(quotient) */ |
---|
7015 | n/a | if (allnine && qdigits == ctx->prec) { |
---|
7016 | n/a | /* abs(quotient) + 1 == 10**prec */ |
---|
7017 | n/a | mpd_seterror(r, MPD_Division_impossible, status); |
---|
7018 | n/a | goto finish; |
---|
7019 | n/a | } |
---|
7020 | n/a | mpd_qcopy(r, &q, status); |
---|
7021 | n/a | } |
---|
7022 | n/a | } |
---|
7023 | n/a | |
---|
7024 | n/a | |
---|
7025 | n/a | finish: |
---|
7026 | n/a | mpd_del(&btmp); |
---|
7027 | n/a | mpd_del(&q); |
---|
7028 | n/a | mpd_qfinalize(r, ctx, status); |
---|
7029 | n/a | } |
---|
7030 | n/a | |
---|
7031 | n/a | static void |
---|
7032 | n/a | _mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, |
---|
7033 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
7034 | n/a | { |
---|
7035 | n/a | mpd_ssize_t expdiff, shift; |
---|
7036 | n/a | mpd_uint_t rnd; |
---|
7037 | n/a | |
---|
7038 | n/a | if (mpd_isspecial(a)) { |
---|
7039 | n/a | mpd_qcopy(result, a, status); |
---|
7040 | n/a | return; |
---|
7041 | n/a | } |
---|
7042 | n/a | |
---|
7043 | n/a | if (mpd_iszero(a)) { |
---|
7044 | n/a | _settriple(result, mpd_sign(a), 0, exp); |
---|
7045 | n/a | return; |
---|
7046 | n/a | } |
---|
7047 | n/a | |
---|
7048 | n/a | expdiff = a->exp - exp; |
---|
7049 | n/a | if (expdiff >= 0) { |
---|
7050 | n/a | shift = expdiff; |
---|
7051 | n/a | if (a->digits + shift > MPD_MAX_PREC+1) { |
---|
7052 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7053 | n/a | return; |
---|
7054 | n/a | } |
---|
7055 | n/a | if (!mpd_qshiftl(result, a, shift, status)) { |
---|
7056 | n/a | return; |
---|
7057 | n/a | } |
---|
7058 | n/a | result->exp = exp; |
---|
7059 | n/a | } |
---|
7060 | n/a | else { |
---|
7061 | n/a | shift = -expdiff; |
---|
7062 | n/a | rnd = mpd_qshiftr(result, a, shift, status); |
---|
7063 | n/a | if (rnd == MPD_UINT_MAX) { |
---|
7064 | n/a | return; |
---|
7065 | n/a | } |
---|
7066 | n/a | result->exp = exp; |
---|
7067 | n/a | _mpd_apply_round_excess(result, rnd, ctx, status); |
---|
7068 | n/a | *status |= MPD_Rounded; |
---|
7069 | n/a | if (rnd) { |
---|
7070 | n/a | *status |= MPD_Inexact; |
---|
7071 | n/a | } |
---|
7072 | n/a | } |
---|
7073 | n/a | |
---|
7074 | n/a | if (mpd_issubnormal(result, ctx)) { |
---|
7075 | n/a | *status |= MPD_Subnormal; |
---|
7076 | n/a | } |
---|
7077 | n/a | } |
---|
7078 | n/a | |
---|
7079 | n/a | /* |
---|
7080 | n/a | * Rescale a number so that it has exponent 'exp'. Does not regard context |
---|
7081 | n/a | * precision, emax, emin, but uses the rounding mode. Special numbers are |
---|
7082 | n/a | * quietly copied. Restrictions: |
---|
7083 | n/a | * |
---|
7084 | n/a | * MPD_MIN_ETINY <= exp <= MPD_MAX_EMAX+1 |
---|
7085 | n/a | * result->digits <= MPD_MAX_PREC+1 |
---|
7086 | n/a | */ |
---|
7087 | n/a | void |
---|
7088 | n/a | mpd_qrescale(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, |
---|
7089 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
7090 | n/a | { |
---|
7091 | n/a | if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY) { |
---|
7092 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7093 | n/a | return; |
---|
7094 | n/a | } |
---|
7095 | n/a | |
---|
7096 | n/a | _mpd_qrescale(result, a, exp, ctx, status); |
---|
7097 | n/a | } |
---|
7098 | n/a | |
---|
7099 | n/a | /* |
---|
7100 | n/a | * Same as mpd_qrescale, but with relaxed restrictions. The result of this |
---|
7101 | n/a | * function should only be used for formatting a number and never as input |
---|
7102 | n/a | * for other operations. |
---|
7103 | n/a | * |
---|
7104 | n/a | * MPD_MIN_ETINY-MPD_MAX_PREC <= exp <= MPD_MAX_EMAX+1 |
---|
7105 | n/a | * result->digits <= MPD_MAX_PREC+1 |
---|
7106 | n/a | */ |
---|
7107 | n/a | void |
---|
7108 | n/a | mpd_qrescale_fmt(mpd_t *result, const mpd_t *a, mpd_ssize_t exp, |
---|
7109 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
7110 | n/a | { |
---|
7111 | n/a | if (exp > MPD_MAX_EMAX+1 || exp < MPD_MIN_ETINY-MPD_MAX_PREC) { |
---|
7112 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7113 | n/a | return; |
---|
7114 | n/a | } |
---|
7115 | n/a | |
---|
7116 | n/a | _mpd_qrescale(result, a, exp, ctx, status); |
---|
7117 | n/a | } |
---|
7118 | n/a | |
---|
7119 | n/a | /* Round to an integer according to 'action' and ctx->round. */ |
---|
7120 | n/a | enum {TO_INT_EXACT, TO_INT_SILENT, TO_INT_TRUNC}; |
---|
7121 | n/a | static void |
---|
7122 | n/a | _mpd_qround_to_integral(int action, mpd_t *result, const mpd_t *a, |
---|
7123 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
7124 | n/a | { |
---|
7125 | n/a | mpd_uint_t rnd; |
---|
7126 | n/a | |
---|
7127 | n/a | if (mpd_isspecial(a)) { |
---|
7128 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
7129 | n/a | return; |
---|
7130 | n/a | } |
---|
7131 | n/a | mpd_qcopy(result, a, status); |
---|
7132 | n/a | return; |
---|
7133 | n/a | } |
---|
7134 | n/a | if (a->exp >= 0) { |
---|
7135 | n/a | mpd_qcopy(result, a, status); |
---|
7136 | n/a | return; |
---|
7137 | n/a | } |
---|
7138 | n/a | if (mpd_iszerocoeff(a)) { |
---|
7139 | n/a | _settriple(result, mpd_sign(a), 0, 0); |
---|
7140 | n/a | return; |
---|
7141 | n/a | } |
---|
7142 | n/a | |
---|
7143 | n/a | rnd = mpd_qshiftr(result, a, -a->exp, status); |
---|
7144 | n/a | if (rnd == MPD_UINT_MAX) { |
---|
7145 | n/a | return; |
---|
7146 | n/a | } |
---|
7147 | n/a | result->exp = 0; |
---|
7148 | n/a | |
---|
7149 | n/a | if (action == TO_INT_EXACT || action == TO_INT_SILENT) { |
---|
7150 | n/a | _mpd_apply_round_excess(result, rnd, ctx, status); |
---|
7151 | n/a | if (action == TO_INT_EXACT) { |
---|
7152 | n/a | *status |= MPD_Rounded; |
---|
7153 | n/a | if (rnd) { |
---|
7154 | n/a | *status |= MPD_Inexact; |
---|
7155 | n/a | } |
---|
7156 | n/a | } |
---|
7157 | n/a | } |
---|
7158 | n/a | } |
---|
7159 | n/a | |
---|
7160 | n/a | void |
---|
7161 | n/a | mpd_qround_to_intx(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7162 | n/a | uint32_t *status) |
---|
7163 | n/a | { |
---|
7164 | n/a | (void)_mpd_qround_to_integral(TO_INT_EXACT, result, a, ctx, status); |
---|
7165 | n/a | } |
---|
7166 | n/a | |
---|
7167 | n/a | void |
---|
7168 | n/a | mpd_qround_to_int(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7169 | n/a | uint32_t *status) |
---|
7170 | n/a | { |
---|
7171 | n/a | (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, ctx, status); |
---|
7172 | n/a | } |
---|
7173 | n/a | |
---|
7174 | n/a | void |
---|
7175 | n/a | mpd_qtrunc(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7176 | n/a | uint32_t *status) |
---|
7177 | n/a | { |
---|
7178 | n/a | (void)_mpd_qround_to_integral(TO_INT_TRUNC, result, a, ctx, status); |
---|
7179 | n/a | } |
---|
7180 | n/a | |
---|
7181 | n/a | void |
---|
7182 | n/a | mpd_qfloor(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7183 | n/a | uint32_t *status) |
---|
7184 | n/a | { |
---|
7185 | n/a | mpd_context_t workctx = *ctx; |
---|
7186 | n/a | workctx.round = MPD_ROUND_FLOOR; |
---|
7187 | n/a | (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, |
---|
7188 | n/a | &workctx, status); |
---|
7189 | n/a | } |
---|
7190 | n/a | |
---|
7191 | n/a | void |
---|
7192 | n/a | mpd_qceil(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7193 | n/a | uint32_t *status) |
---|
7194 | n/a | { |
---|
7195 | n/a | mpd_context_t workctx = *ctx; |
---|
7196 | n/a | workctx.round = MPD_ROUND_CEILING; |
---|
7197 | n/a | (void)_mpd_qround_to_integral(TO_INT_SILENT, result, a, |
---|
7198 | n/a | &workctx, status); |
---|
7199 | n/a | } |
---|
7200 | n/a | |
---|
7201 | n/a | int |
---|
7202 | n/a | mpd_same_quantum(const mpd_t *a, const mpd_t *b) |
---|
7203 | n/a | { |
---|
7204 | n/a | if (mpd_isspecial(a) || mpd_isspecial(b)) { |
---|
7205 | n/a | return ((mpd_isnan(a) && mpd_isnan(b)) || |
---|
7206 | n/a | (mpd_isinfinite(a) && mpd_isinfinite(b))); |
---|
7207 | n/a | } |
---|
7208 | n/a | |
---|
7209 | n/a | return a->exp == b->exp; |
---|
7210 | n/a | } |
---|
7211 | n/a | |
---|
7212 | n/a | /* Schedule the increase in precision for the Newton iteration. */ |
---|
7213 | n/a | static inline int |
---|
7214 | n/a | recpr_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], |
---|
7215 | n/a | mpd_ssize_t maxprec, mpd_ssize_t initprec) |
---|
7216 | n/a | { |
---|
7217 | n/a | mpd_ssize_t k; |
---|
7218 | n/a | int i; |
---|
7219 | n/a | |
---|
7220 | n/a | assert(maxprec > 0 && initprec > 0); |
---|
7221 | n/a | if (maxprec <= initprec) return -1; |
---|
7222 | n/a | |
---|
7223 | n/a | i = 0; k = maxprec; |
---|
7224 | n/a | do { |
---|
7225 | n/a | k = (k+1) / 2; |
---|
7226 | n/a | klist[i++] = k; |
---|
7227 | n/a | } while (k > initprec); |
---|
7228 | n/a | |
---|
7229 | n/a | return i-1; |
---|
7230 | n/a | } |
---|
7231 | n/a | |
---|
7232 | n/a | /* |
---|
7233 | n/a | * Initial approximation for the reciprocal: |
---|
7234 | n/a | * k_0 := MPD_RDIGITS-2 |
---|
7235 | n/a | * z_0 := 10**(-k_0) * floor(10**(2*k_0 + 2) / floor(v * 10**(k_0 + 2))) |
---|
7236 | n/a | * Absolute error: |
---|
7237 | n/a | * |1/v - z_0| < 10**(-k_0) |
---|
7238 | n/a | * ACL2 proof: maxerror-inverse-approx |
---|
7239 | n/a | */ |
---|
7240 | n/a | static void |
---|
7241 | n/a | _mpd_qreciprocal_approx(mpd_t *z, const mpd_t *v, uint32_t *status) |
---|
7242 | n/a | { |
---|
7243 | n/a | mpd_uint_t p10data[2] = {0, mpd_pow10[MPD_RDIGITS-2]}; |
---|
7244 | n/a | mpd_uint_t dummy, word; |
---|
7245 | n/a | int n; |
---|
7246 | n/a | |
---|
7247 | n/a | assert(v->exp == -v->digits); |
---|
7248 | n/a | |
---|
7249 | n/a | _mpd_get_msdigits(&dummy, &word, v, MPD_RDIGITS); |
---|
7250 | n/a | n = mpd_word_digits(word); |
---|
7251 | n/a | word *= mpd_pow10[MPD_RDIGITS-n]; |
---|
7252 | n/a | |
---|
7253 | n/a | mpd_qresize(z, 2, status); |
---|
7254 | n/a | (void)_mpd_shortdiv(z->data, p10data, 2, word); |
---|
7255 | n/a | |
---|
7256 | n/a | mpd_clear_flags(z); |
---|
7257 | n/a | z->exp = -(MPD_RDIGITS-2); |
---|
7258 | n/a | z->len = (z->data[1] == 0) ? 1 : 2; |
---|
7259 | n/a | mpd_setdigits(z); |
---|
7260 | n/a | } |
---|
7261 | n/a | |
---|
7262 | n/a | /* |
---|
7263 | n/a | * Reciprocal, calculated with Newton's Method. Assumption: result != a. |
---|
7264 | n/a | * NOTE: The comments in the function show that certain operations are |
---|
7265 | n/a | * exact. The proof for the maximum error is too long to fit in here. |
---|
7266 | n/a | * ACL2 proof: maxerror-inverse-complete |
---|
7267 | n/a | */ |
---|
7268 | n/a | static void |
---|
7269 | n/a | _mpd_qreciprocal(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7270 | n/a | uint32_t *status) |
---|
7271 | n/a | { |
---|
7272 | n/a | mpd_context_t varcontext, maxcontext; |
---|
7273 | n/a | mpd_t *z = result; /* current approximation */ |
---|
7274 | n/a | mpd_t *v; /* a, normalized to a number between 0.1 and 1 */ |
---|
7275 | n/a | MPD_NEW_SHARED(vtmp, a); /* v shares data with a */ |
---|
7276 | n/a | MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ |
---|
7277 | n/a | MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ |
---|
7278 | n/a | MPD_NEW_CONST(two,0,0,1,1,1,2); /* const 2 */ |
---|
7279 | n/a | mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; |
---|
7280 | n/a | mpd_ssize_t adj, maxprec, initprec; |
---|
7281 | n/a | uint8_t sign = mpd_sign(a); |
---|
7282 | n/a | int i; |
---|
7283 | n/a | |
---|
7284 | n/a | assert(result != a); |
---|
7285 | n/a | |
---|
7286 | n/a | v = &vtmp; |
---|
7287 | n/a | mpd_clear_flags(v); |
---|
7288 | n/a | adj = v->digits + v->exp; |
---|
7289 | n/a | v->exp = -v->digits; |
---|
7290 | n/a | |
---|
7291 | n/a | /* Initial approximation */ |
---|
7292 | n/a | _mpd_qreciprocal_approx(z, v, status); |
---|
7293 | n/a | |
---|
7294 | n/a | mpd_maxcontext(&varcontext); |
---|
7295 | n/a | mpd_maxcontext(&maxcontext); |
---|
7296 | n/a | varcontext.round = maxcontext.round = MPD_ROUND_TRUNC; |
---|
7297 | n/a | varcontext.emax = maxcontext.emax = MPD_MAX_EMAX + 100; |
---|
7298 | n/a | varcontext.emin = maxcontext.emin = MPD_MIN_EMIN - 100; |
---|
7299 | n/a | maxcontext.prec = MPD_MAX_PREC + 100; |
---|
7300 | n/a | |
---|
7301 | n/a | maxprec = ctx->prec; |
---|
7302 | n/a | maxprec += 2; |
---|
7303 | n/a | initprec = MPD_RDIGITS-3; |
---|
7304 | n/a | |
---|
7305 | n/a | i = recpr_schedule_prec(klist, maxprec, initprec); |
---|
7306 | n/a | for (; i >= 0; i--) { |
---|
7307 | n/a | /* Loop invariant: z->digits <= klist[i]+7 */ |
---|
7308 | n/a | /* Let s := z**2, exact result */ |
---|
7309 | n/a | _mpd_qmul_exact(&s, z, z, &maxcontext, status); |
---|
7310 | n/a | varcontext.prec = 2*klist[i] + 5; |
---|
7311 | n/a | if (v->digits > varcontext.prec) { |
---|
7312 | n/a | /* Let t := v, truncated to n >= 2*k+5 fraction digits */ |
---|
7313 | n/a | mpd_qshiftr(&t, v, v->digits-varcontext.prec, status); |
---|
7314 | n/a | t.exp = -varcontext.prec; |
---|
7315 | n/a | /* Let t := trunc(v)*s, truncated to n >= 2*k+1 fraction digits */ |
---|
7316 | n/a | mpd_qmul(&t, &t, &s, &varcontext, status); |
---|
7317 | n/a | } |
---|
7318 | n/a | else { /* v->digits <= 2*k+5 */ |
---|
7319 | n/a | /* Let t := v*s, truncated to n >= 2*k+1 fraction digits */ |
---|
7320 | n/a | mpd_qmul(&t, v, &s, &varcontext, status); |
---|
7321 | n/a | } |
---|
7322 | n/a | /* Let s := 2*z, exact result */ |
---|
7323 | n/a | _mpd_qmul_exact(&s, z, &two, &maxcontext, status); |
---|
7324 | n/a | /* s.digits < t.digits <= 2*k+5, |adjexp(s)-adjexp(t)| <= 1, |
---|
7325 | n/a | * so the subtraction generates at most 2*k+6 <= klist[i+1]+7 |
---|
7326 | n/a | * digits. The loop invariant is preserved. */ |
---|
7327 | n/a | _mpd_qsub_exact(z, &s, &t, &maxcontext, status); |
---|
7328 | n/a | } |
---|
7329 | n/a | |
---|
7330 | n/a | if (!mpd_isspecial(z)) { |
---|
7331 | n/a | z->exp -= adj; |
---|
7332 | n/a | mpd_set_flags(z, sign); |
---|
7333 | n/a | } |
---|
7334 | n/a | |
---|
7335 | n/a | mpd_del(&s); |
---|
7336 | n/a | mpd_del(&t); |
---|
7337 | n/a | mpd_qfinalize(z, ctx, status); |
---|
7338 | n/a | } |
---|
7339 | n/a | |
---|
7340 | n/a | /* |
---|
7341 | n/a | * Internal function for large numbers: |
---|
7342 | n/a | * |
---|
7343 | n/a | * q, r = divmod(coeff(a), coeff(b)) |
---|
7344 | n/a | * |
---|
7345 | n/a | * Strategy: Multiply the dividend by the reciprocal of the divisor. The |
---|
7346 | n/a | * inexact result is fixed by a small loop, using at most one iteration. |
---|
7347 | n/a | * |
---|
7348 | n/a | * ACL2 proofs: |
---|
7349 | n/a | * ------------ |
---|
7350 | n/a | * 1) q is a natural number. (ndivmod-quotient-natp) |
---|
7351 | n/a | * 2) r is a natural number. (ndivmod-remainder-natp) |
---|
7352 | n/a | * 3) a = q * b + r (ndivmod-q*b+r==a) |
---|
7353 | n/a | * 4) r < b (ndivmod-remainder-<-b) |
---|
7354 | n/a | */ |
---|
7355 | n/a | static void |
---|
7356 | n/a | _mpd_base_ndivmod(mpd_t *q, mpd_t *r, const mpd_t *a, const mpd_t *b, |
---|
7357 | n/a | uint32_t *status) |
---|
7358 | n/a | { |
---|
7359 | n/a | mpd_context_t workctx; |
---|
7360 | n/a | mpd_t *qq = q, *rr = r; |
---|
7361 | n/a | mpd_t aa, bb; |
---|
7362 | n/a | int k; |
---|
7363 | n/a | |
---|
7364 | n/a | _mpd_copy_shared(&aa, a); |
---|
7365 | n/a | _mpd_copy_shared(&bb, b); |
---|
7366 | n/a | |
---|
7367 | n/a | mpd_set_positive(&aa); |
---|
7368 | n/a | mpd_set_positive(&bb); |
---|
7369 | n/a | aa.exp = 0; |
---|
7370 | n/a | bb.exp = 0; |
---|
7371 | n/a | |
---|
7372 | n/a | if (q == a || q == b) { |
---|
7373 | n/a | if ((qq = mpd_qnew()) == NULL) { |
---|
7374 | n/a | *status |= MPD_Malloc_error; |
---|
7375 | n/a | goto nanresult; |
---|
7376 | n/a | } |
---|
7377 | n/a | } |
---|
7378 | n/a | if (r == a || r == b) { |
---|
7379 | n/a | if ((rr = mpd_qnew()) == NULL) { |
---|
7380 | n/a | *status |= MPD_Malloc_error; |
---|
7381 | n/a | goto nanresult; |
---|
7382 | n/a | } |
---|
7383 | n/a | } |
---|
7384 | n/a | |
---|
7385 | n/a | mpd_maxcontext(&workctx); |
---|
7386 | n/a | |
---|
7387 | n/a | /* Let prec := adigits - bdigits + 4 */ |
---|
7388 | n/a | workctx.prec = a->digits - b->digits + 1 + 3; |
---|
7389 | n/a | if (a->digits > MPD_MAX_PREC || workctx.prec > MPD_MAX_PREC) { |
---|
7390 | n/a | *status |= MPD_Division_impossible; |
---|
7391 | n/a | goto nanresult; |
---|
7392 | n/a | } |
---|
7393 | n/a | |
---|
7394 | n/a | /* Let x := _mpd_qreciprocal(b, prec) |
---|
7395 | n/a | * Then x is bounded by: |
---|
7396 | n/a | * 1) 1/b - 10**(-prec - bdigits) < x < 1/b + 10**(-prec - bdigits) |
---|
7397 | n/a | * 2) 1/b - 10**(-adigits - 4) < x < 1/b + 10**(-adigits - 4) |
---|
7398 | n/a | */ |
---|
7399 | n/a | _mpd_qreciprocal(rr, &bb, &workctx, &workctx.status); |
---|
7400 | n/a | |
---|
7401 | n/a | /* Get an estimate for the quotient. Let q := a * x |
---|
7402 | n/a | * Then q is bounded by: |
---|
7403 | n/a | * 3) a/b - 10**-4 < q < a/b + 10**-4 |
---|
7404 | n/a | */ |
---|
7405 | n/a | _mpd_qmul(qq, &aa, rr, &workctx, &workctx.status); |
---|
7406 | n/a | /* Truncate q to an integer: |
---|
7407 | n/a | * 4) a/b - 2 < trunc(q) < a/b + 1 |
---|
7408 | n/a | */ |
---|
7409 | n/a | mpd_qtrunc(qq, qq, &workctx, &workctx.status); |
---|
7410 | n/a | |
---|
7411 | n/a | workctx.prec = aa.digits + 3; |
---|
7412 | n/a | workctx.emax = MPD_MAX_EMAX + 3; |
---|
7413 | n/a | workctx.emin = MPD_MIN_EMIN - 3; |
---|
7414 | n/a | /* Multiply the estimate for q by b: |
---|
7415 | n/a | * 5) a - 2 * b < trunc(q) * b < a + b |
---|
7416 | n/a | */ |
---|
7417 | n/a | _mpd_qmul(rr, &bb, qq, &workctx, &workctx.status); |
---|
7418 | n/a | /* Get the estimate for r such that a = q * b + r. */ |
---|
7419 | n/a | _mpd_qsub_exact(rr, &aa, rr, &workctx, &workctx.status); |
---|
7420 | n/a | |
---|
7421 | n/a | /* Fix the result. At this point -b < r < 2*b, so the correction loop |
---|
7422 | n/a | takes at most one iteration. */ |
---|
7423 | n/a | for (k = 0;; k++) { |
---|
7424 | n/a | if (mpd_isspecial(qq) || mpd_isspecial(rr)) { |
---|
7425 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
7426 | n/a | goto nanresult; |
---|
7427 | n/a | } |
---|
7428 | n/a | if (k > 2) { /* Allow two iterations despite the proof. */ |
---|
7429 | n/a | mpd_err_warn("libmpdec: internal error in " /* GCOV_NOT_REACHED */ |
---|
7430 | n/a | "_mpd_base_ndivmod: please report"); /* GCOV_NOT_REACHED */ |
---|
7431 | n/a | *status |= MPD_Invalid_operation; /* GCOV_NOT_REACHED */ |
---|
7432 | n/a | goto nanresult; /* GCOV_NOT_REACHED */ |
---|
7433 | n/a | } |
---|
7434 | n/a | /* r < 0 */ |
---|
7435 | n/a | else if (_mpd_cmp(&zero, rr) == 1) { |
---|
7436 | n/a | _mpd_qadd_exact(rr, rr, &bb, &workctx, &workctx.status); |
---|
7437 | n/a | _mpd_qadd_exact(qq, qq, &minus_one, &workctx, &workctx.status); |
---|
7438 | n/a | } |
---|
7439 | n/a | /* 0 <= r < b */ |
---|
7440 | n/a | else if (_mpd_cmp(rr, &bb) == -1) { |
---|
7441 | n/a | break; |
---|
7442 | n/a | } |
---|
7443 | n/a | /* r >= b */ |
---|
7444 | n/a | else { |
---|
7445 | n/a | _mpd_qsub_exact(rr, rr, &bb, &workctx, &workctx.status); |
---|
7446 | n/a | _mpd_qadd_exact(qq, qq, &one, &workctx, &workctx.status); |
---|
7447 | n/a | } |
---|
7448 | n/a | } |
---|
7449 | n/a | |
---|
7450 | n/a | if (qq != q) { |
---|
7451 | n/a | if (!mpd_qcopy(q, qq, status)) { |
---|
7452 | n/a | goto nanresult; /* GCOV_UNLIKELY */ |
---|
7453 | n/a | } |
---|
7454 | n/a | mpd_del(qq); |
---|
7455 | n/a | } |
---|
7456 | n/a | if (rr != r) { |
---|
7457 | n/a | if (!mpd_qcopy(r, rr, status)) { |
---|
7458 | n/a | goto nanresult; /* GCOV_UNLIKELY */ |
---|
7459 | n/a | } |
---|
7460 | n/a | mpd_del(rr); |
---|
7461 | n/a | } |
---|
7462 | n/a | |
---|
7463 | n/a | *status |= (workctx.status&MPD_Errors); |
---|
7464 | n/a | return; |
---|
7465 | n/a | |
---|
7466 | n/a | |
---|
7467 | n/a | nanresult: |
---|
7468 | n/a | if (qq && qq != q) mpd_del(qq); |
---|
7469 | n/a | if (rr && rr != r) mpd_del(rr); |
---|
7470 | n/a | mpd_setspecial(q, MPD_POS, MPD_NAN); |
---|
7471 | n/a | mpd_setspecial(r, MPD_POS, MPD_NAN); |
---|
7472 | n/a | } |
---|
7473 | n/a | |
---|
7474 | n/a | /* LIBMPDEC_ONLY */ |
---|
7475 | n/a | /* |
---|
7476 | n/a | * Schedule the optimal precision increase for the Newton iteration. |
---|
7477 | n/a | * v := input operand |
---|
7478 | n/a | * z_0 := initial approximation |
---|
7479 | n/a | * initprec := natural number such that abs(sqrt(v) - z_0) < 10**-initprec |
---|
7480 | n/a | * maxprec := target precision |
---|
7481 | n/a | * |
---|
7482 | n/a | * For convenience the output klist contains the elements in reverse order: |
---|
7483 | n/a | * klist := [k_n-1, ..., k_0], where |
---|
7484 | n/a | * 1) k_0 <= initprec and |
---|
7485 | n/a | * 2) abs(sqrt(v) - result) < 10**(-2*k_n-1 + 2) <= 10**-maxprec. |
---|
7486 | n/a | */ |
---|
7487 | n/a | static inline int |
---|
7488 | n/a | invroot_schedule_prec(mpd_ssize_t klist[MPD_MAX_PREC_LOG2], |
---|
7489 | n/a | mpd_ssize_t maxprec, mpd_ssize_t initprec) |
---|
7490 | n/a | { |
---|
7491 | n/a | mpd_ssize_t k; |
---|
7492 | n/a | int i; |
---|
7493 | n/a | |
---|
7494 | n/a | assert(maxprec >= 3 && initprec >= 3); |
---|
7495 | n/a | if (maxprec <= initprec) return -1; |
---|
7496 | n/a | |
---|
7497 | n/a | i = 0; k = maxprec; |
---|
7498 | n/a | do { |
---|
7499 | n/a | k = (k+3) / 2; |
---|
7500 | n/a | klist[i++] = k; |
---|
7501 | n/a | } while (k > initprec); |
---|
7502 | n/a | |
---|
7503 | n/a | return i-1; |
---|
7504 | n/a | } |
---|
7505 | n/a | |
---|
7506 | n/a | /* |
---|
7507 | n/a | * Initial approximation for the inverse square root function. |
---|
7508 | n/a | * Input: |
---|
7509 | n/a | * v := rational number, with 1 <= v < 100 |
---|
7510 | n/a | * vhat := floor(v * 10**6) |
---|
7511 | n/a | * Output: |
---|
7512 | n/a | * z := approximation to 1/sqrt(v), such that abs(z - 1/sqrt(v)) < 10**-3. |
---|
7513 | n/a | */ |
---|
7514 | n/a | static inline void |
---|
7515 | n/a | _invroot_init_approx(mpd_t *z, mpd_uint_t vhat) |
---|
7516 | n/a | { |
---|
7517 | n/a | mpd_uint_t lo = 1000; |
---|
7518 | n/a | mpd_uint_t hi = 10000; |
---|
7519 | n/a | mpd_uint_t a, sq; |
---|
7520 | n/a | |
---|
7521 | n/a | assert(lo*lo <= vhat && vhat < (hi+1)*(hi+1)); |
---|
7522 | n/a | |
---|
7523 | n/a | for(;;) { |
---|
7524 | n/a | a = (lo + hi) / 2; |
---|
7525 | n/a | sq = a * a; |
---|
7526 | n/a | if (vhat >= sq) { |
---|
7527 | n/a | if (vhat < sq + 2*a + 1) { |
---|
7528 | n/a | break; |
---|
7529 | n/a | } |
---|
7530 | n/a | lo = a + 1; |
---|
7531 | n/a | } |
---|
7532 | n/a | else { |
---|
7533 | n/a | hi = a - 1; |
---|
7534 | n/a | } |
---|
7535 | n/a | } |
---|
7536 | n/a | |
---|
7537 | n/a | /* |
---|
7538 | n/a | * After the binary search we have: |
---|
7539 | n/a | * 1) a**2 <= floor(v * 10**6) < (a + 1)**2 |
---|
7540 | n/a | * This implies: |
---|
7541 | n/a | * 2) a**2 <= v * 10**6 < (a + 1)**2 |
---|
7542 | n/a | * 3) a <= sqrt(v) * 10**3 < a + 1 |
---|
7543 | n/a | * Since 10**3 <= a: |
---|
7544 | n/a | * 4) 0 <= 10**prec/a - 1/sqrt(v) < 10**-prec |
---|
7545 | n/a | * We have: |
---|
7546 | n/a | * 5) 10**3/a - 10**-3 < floor(10**9/a) * 10**-6 <= 10**3/a |
---|
7547 | n/a | * Merging 4) and 5): |
---|
7548 | n/a | * 6) abs(floor(10**9/a) * 10**-6 - 1/sqrt(v)) < 10**-3 |
---|
7549 | n/a | */ |
---|
7550 | n/a | mpd_minalloc(z); |
---|
7551 | n/a | mpd_clear_flags(z); |
---|
7552 | n/a | z->data[0] = 1000000000UL / a; |
---|
7553 | n/a | z->len = 1; |
---|
7554 | n/a | z->exp = -6; |
---|
7555 | n/a | mpd_setdigits(z); |
---|
7556 | n/a | } |
---|
7557 | n/a | |
---|
7558 | n/a | /* |
---|
7559 | n/a | * Set 'result' to 1/sqrt(a). |
---|
7560 | n/a | * Relative error: abs(result - 1/sqrt(a)) < 10**-prec * 1/sqrt(a) |
---|
7561 | n/a | */ |
---|
7562 | n/a | static void |
---|
7563 | n/a | _mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7564 | n/a | uint32_t *status) |
---|
7565 | n/a | { |
---|
7566 | n/a | uint32_t workstatus = 0; |
---|
7567 | n/a | mpd_context_t varcontext, maxcontext; |
---|
7568 | n/a | mpd_t *z = result; /* current approximation */ |
---|
7569 | n/a | mpd_t *v; /* a, normalized to a number between 1 and 100 */ |
---|
7570 | n/a | MPD_NEW_SHARED(vtmp, a); /* by default v will share data with a */ |
---|
7571 | n/a | MPD_NEW_STATIC(s,0,0,0,0); /* temporary variable */ |
---|
7572 | n/a | MPD_NEW_STATIC(t,0,0,0,0); /* temporary variable */ |
---|
7573 | n/a | MPD_NEW_CONST(one_half,0,-1,1,1,1,5); |
---|
7574 | n/a | MPD_NEW_CONST(three,0,0,1,1,1,3); |
---|
7575 | n/a | mpd_ssize_t klist[MPD_MAX_PREC_LOG2]; |
---|
7576 | n/a | mpd_ssize_t ideal_exp, shift; |
---|
7577 | n/a | mpd_ssize_t adj, tz; |
---|
7578 | n/a | mpd_ssize_t maxprec, fracdigits; |
---|
7579 | n/a | mpd_uint_t vhat, dummy; |
---|
7580 | n/a | int i, n; |
---|
7581 | n/a | |
---|
7582 | n/a | |
---|
7583 | n/a | ideal_exp = -(a->exp - (a->exp & 1)) / 2; |
---|
7584 | n/a | |
---|
7585 | n/a | v = &vtmp; |
---|
7586 | n/a | if (result == a) { |
---|
7587 | n/a | if ((v = mpd_qncopy(a)) == NULL) { |
---|
7588 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
7589 | n/a | return; |
---|
7590 | n/a | } |
---|
7591 | n/a | } |
---|
7592 | n/a | |
---|
7593 | n/a | /* normalize a to 1 <= v < 100 */ |
---|
7594 | n/a | if ((v->digits+v->exp) & 1) { |
---|
7595 | n/a | fracdigits = v->digits - 1; |
---|
7596 | n/a | v->exp = -fracdigits; |
---|
7597 | n/a | n = (v->digits > 7) ? 7 : (int)v->digits; |
---|
7598 | n/a | /* Let vhat := floor(v * 10**(2*initprec)) */ |
---|
7599 | n/a | _mpd_get_msdigits(&dummy, &vhat, v, n); |
---|
7600 | n/a | if (n < 7) { |
---|
7601 | n/a | vhat *= mpd_pow10[7-n]; |
---|
7602 | n/a | } |
---|
7603 | n/a | } |
---|
7604 | n/a | else { |
---|
7605 | n/a | fracdigits = v->digits - 2; |
---|
7606 | n/a | v->exp = -fracdigits; |
---|
7607 | n/a | n = (v->digits > 8) ? 8 : (int)v->digits; |
---|
7608 | n/a | /* Let vhat := floor(v * 10**(2*initprec)) */ |
---|
7609 | n/a | _mpd_get_msdigits(&dummy, &vhat, v, n); |
---|
7610 | n/a | if (n < 8) { |
---|
7611 | n/a | vhat *= mpd_pow10[8-n]; |
---|
7612 | n/a | } |
---|
7613 | n/a | } |
---|
7614 | n/a | adj = (a->exp-v->exp) / 2; |
---|
7615 | n/a | |
---|
7616 | n/a | /* initial approximation */ |
---|
7617 | n/a | _invroot_init_approx(z, vhat); |
---|
7618 | n/a | |
---|
7619 | n/a | mpd_maxcontext(&maxcontext); |
---|
7620 | n/a | mpd_maxcontext(&varcontext); |
---|
7621 | n/a | varcontext.round = MPD_ROUND_TRUNC; |
---|
7622 | n/a | maxprec = ctx->prec + 1; |
---|
7623 | n/a | |
---|
7624 | n/a | /* initprec == 3 */ |
---|
7625 | n/a | i = invroot_schedule_prec(klist, maxprec, 3); |
---|
7626 | n/a | for (; i >= 0; i--) { |
---|
7627 | n/a | varcontext.prec = 2*klist[i]+2; |
---|
7628 | n/a | mpd_qmul(&s, z, z, &maxcontext, &workstatus); |
---|
7629 | n/a | if (v->digits > varcontext.prec) { |
---|
7630 | n/a | shift = v->digits - varcontext.prec; |
---|
7631 | n/a | mpd_qshiftr(&t, v, shift, &workstatus); |
---|
7632 | n/a | t.exp += shift; |
---|
7633 | n/a | mpd_qmul(&t, &t, &s, &varcontext, &workstatus); |
---|
7634 | n/a | } |
---|
7635 | n/a | else { |
---|
7636 | n/a | mpd_qmul(&t, v, &s, &varcontext, &workstatus); |
---|
7637 | n/a | } |
---|
7638 | n/a | mpd_qsub(&t, &three, &t, &maxcontext, &workstatus); |
---|
7639 | n/a | mpd_qmul(z, z, &t, &varcontext, &workstatus); |
---|
7640 | n/a | mpd_qmul(z, z, &one_half, &maxcontext, &workstatus); |
---|
7641 | n/a | } |
---|
7642 | n/a | |
---|
7643 | n/a | z->exp -= adj; |
---|
7644 | n/a | |
---|
7645 | n/a | tz = mpd_trail_zeros(result); |
---|
7646 | n/a | shift = ideal_exp - result->exp; |
---|
7647 | n/a | shift = (tz > shift) ? shift : tz; |
---|
7648 | n/a | if (shift > 0) { |
---|
7649 | n/a | mpd_qshiftr_inplace(result, shift); |
---|
7650 | n/a | result->exp += shift; |
---|
7651 | n/a | } |
---|
7652 | n/a | |
---|
7653 | n/a | |
---|
7654 | n/a | mpd_del(&s); |
---|
7655 | n/a | mpd_del(&t); |
---|
7656 | n/a | if (v != &vtmp) mpd_del(v); |
---|
7657 | n/a | *status |= (workstatus&MPD_Errors); |
---|
7658 | n/a | *status |= (MPD_Rounded|MPD_Inexact); |
---|
7659 | n/a | } |
---|
7660 | n/a | |
---|
7661 | n/a | void |
---|
7662 | n/a | mpd_qinvroot(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7663 | n/a | uint32_t *status) |
---|
7664 | n/a | { |
---|
7665 | n/a | mpd_context_t workctx; |
---|
7666 | n/a | |
---|
7667 | n/a | if (mpd_isspecial(a)) { |
---|
7668 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
7669 | n/a | return; |
---|
7670 | n/a | } |
---|
7671 | n/a | if (mpd_isnegative(a)) { |
---|
7672 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7673 | n/a | return; |
---|
7674 | n/a | } |
---|
7675 | n/a | /* positive infinity */ |
---|
7676 | n/a | _settriple(result, MPD_POS, 0, mpd_etiny(ctx)); |
---|
7677 | n/a | *status |= MPD_Clamped; |
---|
7678 | n/a | return; |
---|
7679 | n/a | } |
---|
7680 | n/a | if (mpd_iszero(a)) { |
---|
7681 | n/a | mpd_setspecial(result, mpd_sign(a), MPD_INF); |
---|
7682 | n/a | *status |= MPD_Division_by_zero; |
---|
7683 | n/a | return; |
---|
7684 | n/a | } |
---|
7685 | n/a | if (mpd_isnegative(a)) { |
---|
7686 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7687 | n/a | return; |
---|
7688 | n/a | } |
---|
7689 | n/a | |
---|
7690 | n/a | workctx = *ctx; |
---|
7691 | n/a | workctx.prec += 2; |
---|
7692 | n/a | workctx.round = MPD_ROUND_HALF_EVEN; |
---|
7693 | n/a | _mpd_qinvroot(result, a, &workctx, status); |
---|
7694 | n/a | mpd_qfinalize(result, ctx, status); |
---|
7695 | n/a | } |
---|
7696 | n/a | /* END LIBMPDEC_ONLY */ |
---|
7697 | n/a | |
---|
7698 | n/a | /* Algorithm from decimal.py */ |
---|
7699 | n/a | void |
---|
7700 | n/a | mpd_qsqrt(mpd_t *result, const mpd_t *a, const mpd_context_t *ctx, |
---|
7701 | n/a | uint32_t *status) |
---|
7702 | n/a | { |
---|
7703 | n/a | mpd_context_t maxcontext; |
---|
7704 | n/a | MPD_NEW_STATIC(c,0,0,0,0); |
---|
7705 | n/a | MPD_NEW_STATIC(q,0,0,0,0); |
---|
7706 | n/a | MPD_NEW_STATIC(r,0,0,0,0); |
---|
7707 | n/a | MPD_NEW_CONST(two,0,0,1,1,1,2); |
---|
7708 | n/a | mpd_ssize_t prec, ideal_exp; |
---|
7709 | n/a | mpd_ssize_t l, shift; |
---|
7710 | n/a | int exact = 0; |
---|
7711 | n/a | |
---|
7712 | n/a | |
---|
7713 | n/a | ideal_exp = (a->exp - (a->exp & 1)) / 2; |
---|
7714 | n/a | |
---|
7715 | n/a | if (mpd_isspecial(a)) { |
---|
7716 | n/a | if (mpd_qcheck_nan(result, a, ctx, status)) { |
---|
7717 | n/a | return; |
---|
7718 | n/a | } |
---|
7719 | n/a | if (mpd_isnegative(a)) { |
---|
7720 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7721 | n/a | return; |
---|
7722 | n/a | } |
---|
7723 | n/a | mpd_setspecial(result, MPD_POS, MPD_INF); |
---|
7724 | n/a | return; |
---|
7725 | n/a | } |
---|
7726 | n/a | if (mpd_iszero(a)) { |
---|
7727 | n/a | _settriple(result, mpd_sign(a), 0, ideal_exp); |
---|
7728 | n/a | mpd_qfinalize(result, ctx, status); |
---|
7729 | n/a | return; |
---|
7730 | n/a | } |
---|
7731 | n/a | if (mpd_isnegative(a)) { |
---|
7732 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
7733 | n/a | return; |
---|
7734 | n/a | } |
---|
7735 | n/a | |
---|
7736 | n/a | mpd_maxcontext(&maxcontext); |
---|
7737 | n/a | prec = ctx->prec + 1; |
---|
7738 | n/a | |
---|
7739 | n/a | if (!mpd_qcopy(&c, a, status)) { |
---|
7740 | n/a | goto malloc_error; |
---|
7741 | n/a | } |
---|
7742 | n/a | c.exp = 0; |
---|
7743 | n/a | |
---|
7744 | n/a | if (a->exp & 1) { |
---|
7745 | n/a | if (!mpd_qshiftl(&c, &c, 1, status)) { |
---|
7746 | n/a | goto malloc_error; |
---|
7747 | n/a | } |
---|
7748 | n/a | l = (a->digits >> 1) + 1; |
---|
7749 | n/a | } |
---|
7750 | n/a | else { |
---|
7751 | n/a | l = (a->digits + 1) >> 1; |
---|
7752 | n/a | } |
---|
7753 | n/a | |
---|
7754 | n/a | shift = prec - l; |
---|
7755 | n/a | if (shift >= 0) { |
---|
7756 | n/a | if (!mpd_qshiftl(&c, &c, 2*shift, status)) { |
---|
7757 | n/a | goto malloc_error; |
---|
7758 | n/a | } |
---|
7759 | n/a | exact = 1; |
---|
7760 | n/a | } |
---|
7761 | n/a | else { |
---|
7762 | n/a | exact = !mpd_qshiftr_inplace(&c, -2*shift); |
---|
7763 | n/a | } |
---|
7764 | n/a | |
---|
7765 | n/a | ideal_exp -= shift; |
---|
7766 | n/a | |
---|
7767 | n/a | /* find result = floor(sqrt(c)) using Newton's method */ |
---|
7768 | n/a | if (!mpd_qshiftl(result, &one, prec, status)) { |
---|
7769 | n/a | goto malloc_error; |
---|
7770 | n/a | } |
---|
7771 | n/a | |
---|
7772 | n/a | while (1) { |
---|
7773 | n/a | _mpd_qdivmod(&q, &r, &c, result, &maxcontext, &maxcontext.status); |
---|
7774 | n/a | if (mpd_isspecial(result) || mpd_isspecial(&q)) { |
---|
7775 | n/a | mpd_seterror(result, maxcontext.status&MPD_Errors, status); |
---|
7776 | n/a | goto out; |
---|
7777 | n/a | } |
---|
7778 | n/a | if (_mpd_cmp(result, &q) <= 0) { |
---|
7779 | n/a | break; |
---|
7780 | n/a | } |
---|
7781 | n/a | _mpd_qadd_exact(result, result, &q, &maxcontext, &maxcontext.status); |
---|
7782 | n/a | if (mpd_isspecial(result)) { |
---|
7783 | n/a | mpd_seterror(result, maxcontext.status&MPD_Errors, status); |
---|
7784 | n/a | goto out; |
---|
7785 | n/a | } |
---|
7786 | n/a | _mpd_qdivmod(result, &r, result, &two, &maxcontext, &maxcontext.status); |
---|
7787 | n/a | } |
---|
7788 | n/a | |
---|
7789 | n/a | if (exact) { |
---|
7790 | n/a | _mpd_qmul_exact(&r, result, result, &maxcontext, &maxcontext.status); |
---|
7791 | n/a | if (mpd_isspecial(&r)) { |
---|
7792 | n/a | mpd_seterror(result, maxcontext.status&MPD_Errors, status); |
---|
7793 | n/a | goto out; |
---|
7794 | n/a | } |
---|
7795 | n/a | exact = (_mpd_cmp(&r, &c) == 0); |
---|
7796 | n/a | } |
---|
7797 | n/a | |
---|
7798 | n/a | if (exact) { |
---|
7799 | n/a | if (shift >= 0) { |
---|
7800 | n/a | mpd_qshiftr_inplace(result, shift); |
---|
7801 | n/a | } |
---|
7802 | n/a | else { |
---|
7803 | n/a | if (!mpd_qshiftl(result, result, -shift, status)) { |
---|
7804 | n/a | goto malloc_error; |
---|
7805 | n/a | } |
---|
7806 | n/a | } |
---|
7807 | n/a | ideal_exp += shift; |
---|
7808 | n/a | } |
---|
7809 | n/a | else { |
---|
7810 | n/a | int lsd = (int)mpd_lsd(result->data[0]); |
---|
7811 | n/a | if (lsd == 0 || lsd == 5) { |
---|
7812 | n/a | result->data[0] += 1; |
---|
7813 | n/a | } |
---|
7814 | n/a | } |
---|
7815 | n/a | |
---|
7816 | n/a | result->exp = ideal_exp; |
---|
7817 | n/a | |
---|
7818 | n/a | |
---|
7819 | n/a | out: |
---|
7820 | n/a | mpd_del(&c); |
---|
7821 | n/a | mpd_del(&q); |
---|
7822 | n/a | mpd_del(&r); |
---|
7823 | n/a | maxcontext = *ctx; |
---|
7824 | n/a | maxcontext.round = MPD_ROUND_HALF_EVEN; |
---|
7825 | n/a | mpd_qfinalize(result, &maxcontext, status); |
---|
7826 | n/a | return; |
---|
7827 | n/a | |
---|
7828 | n/a | malloc_error: |
---|
7829 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
7830 | n/a | goto out; |
---|
7831 | n/a | } |
---|
7832 | n/a | |
---|
7833 | n/a | |
---|
7834 | n/a | /******************************************************************************/ |
---|
7835 | n/a | /* Base conversions */ |
---|
7836 | n/a | /******************************************************************************/ |
---|
7837 | n/a | |
---|
7838 | n/a | /* Space needed to represent an integer mpd_t in base 'base'. */ |
---|
7839 | n/a | size_t |
---|
7840 | n/a | mpd_sizeinbase(const mpd_t *a, uint32_t base) |
---|
7841 | n/a | { |
---|
7842 | n/a | double x; |
---|
7843 | n/a | size_t digits; |
---|
7844 | n/a | |
---|
7845 | n/a | assert(mpd_isinteger(a)); |
---|
7846 | n/a | assert(base >= 2); |
---|
7847 | n/a | |
---|
7848 | n/a | if (mpd_iszero(a)) { |
---|
7849 | n/a | return 1; |
---|
7850 | n/a | } |
---|
7851 | n/a | |
---|
7852 | n/a | digits = a->digits+a->exp; |
---|
7853 | n/a | assert(digits > 0); |
---|
7854 | n/a | |
---|
7855 | n/a | #ifdef CONFIG_64 |
---|
7856 | n/a | /* ceil(2711437152599294 / log10(2)) + 4 == 2**53 */ |
---|
7857 | n/a | if (digits > 2711437152599294ULL) { |
---|
7858 | n/a | return SIZE_MAX; |
---|
7859 | n/a | } |
---|
7860 | n/a | #endif |
---|
7861 | n/a | |
---|
7862 | n/a | x = (double)digits / log10(base); |
---|
7863 | n/a | return (x > SIZE_MAX-1) ? SIZE_MAX : (size_t)x + 1; |
---|
7864 | n/a | } |
---|
7865 | n/a | |
---|
7866 | n/a | /* Space needed to import a base 'base' integer of length 'srclen'. */ |
---|
7867 | n/a | static mpd_ssize_t |
---|
7868 | n/a | _mpd_importsize(size_t srclen, uint32_t base) |
---|
7869 | n/a | { |
---|
7870 | n/a | double x; |
---|
7871 | n/a | |
---|
7872 | n/a | assert(srclen > 0); |
---|
7873 | n/a | assert(base >= 2); |
---|
7874 | n/a | |
---|
7875 | n/a | #if SIZE_MAX == UINT64_MAX |
---|
7876 | n/a | if (srclen > (1ULL<<53)) { |
---|
7877 | n/a | return MPD_SSIZE_MAX; |
---|
7878 | n/a | } |
---|
7879 | n/a | #endif |
---|
7880 | n/a | |
---|
7881 | n/a | x = (double)srclen * (log10(base)/MPD_RDIGITS); |
---|
7882 | n/a | return (x >= MPD_MAXIMPORT) ? MPD_SSIZE_MAX : (mpd_ssize_t)x + 1; |
---|
7883 | n/a | } |
---|
7884 | n/a | |
---|
7885 | n/a | static uint8_t |
---|
7886 | n/a | mpd_resize_u16(uint16_t **w, size_t nmemb) |
---|
7887 | n/a | { |
---|
7888 | n/a | uint8_t err = 0; |
---|
7889 | n/a | *w = mpd_realloc(*w, nmemb, sizeof **w, &err); |
---|
7890 | n/a | return !err; |
---|
7891 | n/a | } |
---|
7892 | n/a | |
---|
7893 | n/a | static uint8_t |
---|
7894 | n/a | mpd_resize_u32(uint32_t **w, size_t nmemb) |
---|
7895 | n/a | { |
---|
7896 | n/a | uint8_t err = 0; |
---|
7897 | n/a | *w = mpd_realloc(*w, nmemb, sizeof **w, &err); |
---|
7898 | n/a | return !err; |
---|
7899 | n/a | } |
---|
7900 | n/a | |
---|
7901 | n/a | static size_t |
---|
7902 | n/a | _baseconv_to_u16(uint16_t **w, size_t wlen, mpd_uint_t wbase, |
---|
7903 | n/a | mpd_uint_t *u, mpd_ssize_t ulen) |
---|
7904 | n/a | { |
---|
7905 | n/a | size_t n = 0; |
---|
7906 | n/a | |
---|
7907 | n/a | assert(wlen > 0 && ulen > 0); |
---|
7908 | n/a | assert(wbase <= (1U<<16)); |
---|
7909 | n/a | |
---|
7910 | n/a | do { |
---|
7911 | n/a | if (n >= wlen) { |
---|
7912 | n/a | if (!mpd_resize_u16(w, n+1)) { |
---|
7913 | n/a | return SIZE_MAX; |
---|
7914 | n/a | } |
---|
7915 | n/a | wlen = n+1; |
---|
7916 | n/a | } |
---|
7917 | n/a | (*w)[n++] = (uint16_t)_mpd_shortdiv(u, u, ulen, wbase); |
---|
7918 | n/a | /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ |
---|
7919 | n/a | ulen = _mpd_real_size(u, ulen); |
---|
7920 | n/a | |
---|
7921 | n/a | } while (u[ulen-1] != 0); |
---|
7922 | n/a | |
---|
7923 | n/a | return n; |
---|
7924 | n/a | } |
---|
7925 | n/a | |
---|
7926 | n/a | static size_t |
---|
7927 | n/a | _coeff_from_u16(mpd_t *w, mpd_ssize_t wlen, |
---|
7928 | n/a | const mpd_uint_t *u, size_t ulen, uint32_t ubase, |
---|
7929 | n/a | uint32_t *status) |
---|
7930 | n/a | { |
---|
7931 | n/a | mpd_ssize_t n = 0; |
---|
7932 | n/a | mpd_uint_t carry; |
---|
7933 | n/a | |
---|
7934 | n/a | assert(wlen > 0 && ulen > 0); |
---|
7935 | n/a | assert(ubase <= (1U<<16)); |
---|
7936 | n/a | |
---|
7937 | n/a | w->data[n++] = u[--ulen]; |
---|
7938 | n/a | while (--ulen != SIZE_MAX) { |
---|
7939 | n/a | carry = _mpd_shortmul_c(w->data, w->data, n, ubase); |
---|
7940 | n/a | if (carry) { |
---|
7941 | n/a | if (n >= wlen) { |
---|
7942 | n/a | if (!mpd_qresize(w, n+1, status)) { |
---|
7943 | n/a | return SIZE_MAX; |
---|
7944 | n/a | } |
---|
7945 | n/a | wlen = n+1; |
---|
7946 | n/a | } |
---|
7947 | n/a | w->data[n++] = carry; |
---|
7948 | n/a | } |
---|
7949 | n/a | carry = _mpd_shortadd(w->data, n, u[ulen]); |
---|
7950 | n/a | if (carry) { |
---|
7951 | n/a | if (n >= wlen) { |
---|
7952 | n/a | if (!mpd_qresize(w, n+1, status)) { |
---|
7953 | n/a | return SIZE_MAX; |
---|
7954 | n/a | } |
---|
7955 | n/a | wlen = n+1; |
---|
7956 | n/a | } |
---|
7957 | n/a | w->data[n++] = carry; |
---|
7958 | n/a | } |
---|
7959 | n/a | } |
---|
7960 | n/a | |
---|
7961 | n/a | return n; |
---|
7962 | n/a | } |
---|
7963 | n/a | |
---|
7964 | n/a | /* target base wbase < source base ubase */ |
---|
7965 | n/a | static size_t |
---|
7966 | n/a | _baseconv_to_smaller(uint32_t **w, size_t wlen, uint32_t wbase, |
---|
7967 | n/a | mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase) |
---|
7968 | n/a | { |
---|
7969 | n/a | size_t n = 0; |
---|
7970 | n/a | |
---|
7971 | n/a | assert(wlen > 0 && ulen > 0); |
---|
7972 | n/a | assert(wbase < ubase); |
---|
7973 | n/a | |
---|
7974 | n/a | do { |
---|
7975 | n/a | if (n >= wlen) { |
---|
7976 | n/a | if (!mpd_resize_u32(w, n+1)) { |
---|
7977 | n/a | return SIZE_MAX; |
---|
7978 | n/a | } |
---|
7979 | n/a | wlen = n+1; |
---|
7980 | n/a | } |
---|
7981 | n/a | (*w)[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); |
---|
7982 | n/a | /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ |
---|
7983 | n/a | ulen = _mpd_real_size(u, ulen); |
---|
7984 | n/a | |
---|
7985 | n/a | } while (u[ulen-1] != 0); |
---|
7986 | n/a | |
---|
7987 | n/a | return n; |
---|
7988 | n/a | } |
---|
7989 | n/a | |
---|
7990 | n/a | #ifdef CONFIG_32 |
---|
7991 | n/a | /* target base 'wbase' == source base 'ubase' */ |
---|
7992 | n/a | static size_t |
---|
7993 | n/a | _copy_equal_base(uint32_t **w, size_t wlen, |
---|
7994 | n/a | const uint32_t *u, size_t ulen) |
---|
7995 | n/a | { |
---|
7996 | n/a | if (wlen < ulen) { |
---|
7997 | n/a | if (!mpd_resize_u32(w, ulen)) { |
---|
7998 | n/a | return SIZE_MAX; |
---|
7999 | n/a | } |
---|
8000 | n/a | } |
---|
8001 | n/a | |
---|
8002 | n/a | memcpy(*w, u, ulen * (sizeof **w)); |
---|
8003 | n/a | return ulen; |
---|
8004 | n/a | } |
---|
8005 | n/a | |
---|
8006 | n/a | /* target base 'wbase' > source base 'ubase' */ |
---|
8007 | n/a | static size_t |
---|
8008 | n/a | _baseconv_to_larger(uint32_t **w, size_t wlen, mpd_uint_t wbase, |
---|
8009 | n/a | const mpd_uint_t *u, size_t ulen, mpd_uint_t ubase) |
---|
8010 | n/a | { |
---|
8011 | n/a | size_t n = 0; |
---|
8012 | n/a | mpd_uint_t carry; |
---|
8013 | n/a | |
---|
8014 | n/a | assert(wlen > 0 && ulen > 0); |
---|
8015 | n/a | assert(ubase < wbase); |
---|
8016 | n/a | |
---|
8017 | n/a | (*w)[n++] = u[--ulen]; |
---|
8018 | n/a | while (--ulen != SIZE_MAX) { |
---|
8019 | n/a | carry = _mpd_shortmul_b(*w, *w, n, ubase, wbase); |
---|
8020 | n/a | if (carry) { |
---|
8021 | n/a | if (n >= wlen) { |
---|
8022 | n/a | if (!mpd_resize_u32(w, n+1)) { |
---|
8023 | n/a | return SIZE_MAX; |
---|
8024 | n/a | } |
---|
8025 | n/a | wlen = n+1; |
---|
8026 | n/a | } |
---|
8027 | n/a | (*w)[n++] = carry; |
---|
8028 | n/a | } |
---|
8029 | n/a | carry = _mpd_shortadd_b(*w, n, u[ulen], wbase); |
---|
8030 | n/a | if (carry) { |
---|
8031 | n/a | if (n >= wlen) { |
---|
8032 | n/a | if (!mpd_resize_u32(w, n+1)) { |
---|
8033 | n/a | return SIZE_MAX; |
---|
8034 | n/a | } |
---|
8035 | n/a | wlen = n+1; |
---|
8036 | n/a | } |
---|
8037 | n/a | (*w)[n++] = carry; |
---|
8038 | n/a | } |
---|
8039 | n/a | } |
---|
8040 | n/a | |
---|
8041 | n/a | return n; |
---|
8042 | n/a | } |
---|
8043 | n/a | |
---|
8044 | n/a | /* target base wbase < source base ubase */ |
---|
8045 | n/a | static size_t |
---|
8046 | n/a | _coeff_from_larger_base(mpd_t *w, size_t wlen, mpd_uint_t wbase, |
---|
8047 | n/a | mpd_uint_t *u, mpd_ssize_t ulen, mpd_uint_t ubase, |
---|
8048 | n/a | uint32_t *status) |
---|
8049 | n/a | { |
---|
8050 | n/a | size_t n = 0; |
---|
8051 | n/a | |
---|
8052 | n/a | assert(wlen > 0 && ulen > 0); |
---|
8053 | n/a | assert(wbase < ubase); |
---|
8054 | n/a | |
---|
8055 | n/a | do { |
---|
8056 | n/a | if (n >= wlen) { |
---|
8057 | n/a | if (!mpd_qresize(w, n+1, status)) { |
---|
8058 | n/a | return SIZE_MAX; |
---|
8059 | n/a | } |
---|
8060 | n/a | wlen = n+1; |
---|
8061 | n/a | } |
---|
8062 | n/a | w->data[n++] = (uint32_t)_mpd_shortdiv_b(u, u, ulen, wbase, ubase); |
---|
8063 | n/a | /* ulen is at least 1. u[ulen-1] can only be zero if ulen == 1. */ |
---|
8064 | n/a | ulen = _mpd_real_size(u, ulen); |
---|
8065 | n/a | |
---|
8066 | n/a | } while (u[ulen-1] != 0); |
---|
8067 | n/a | |
---|
8068 | n/a | return n; |
---|
8069 | n/a | } |
---|
8070 | n/a | #endif |
---|
8071 | n/a | |
---|
8072 | n/a | /* target base 'wbase' > source base 'ubase' */ |
---|
8073 | n/a | static size_t |
---|
8074 | n/a | _coeff_from_smaller_base(mpd_t *w, mpd_ssize_t wlen, mpd_uint_t wbase, |
---|
8075 | n/a | const uint32_t *u, size_t ulen, mpd_uint_t ubase, |
---|
8076 | n/a | uint32_t *status) |
---|
8077 | n/a | { |
---|
8078 | n/a | mpd_ssize_t n = 0; |
---|
8079 | n/a | mpd_uint_t carry; |
---|
8080 | n/a | |
---|
8081 | n/a | assert(wlen > 0 && ulen > 0); |
---|
8082 | n/a | assert(wbase > ubase); |
---|
8083 | n/a | |
---|
8084 | n/a | w->data[n++] = u[--ulen]; |
---|
8085 | n/a | while (--ulen != SIZE_MAX) { |
---|
8086 | n/a | carry = _mpd_shortmul_b(w->data, w->data, n, ubase, wbase); |
---|
8087 | n/a | if (carry) { |
---|
8088 | n/a | if (n >= wlen) { |
---|
8089 | n/a | if (!mpd_qresize(w, n+1, status)) { |
---|
8090 | n/a | return SIZE_MAX; |
---|
8091 | n/a | } |
---|
8092 | n/a | wlen = n+1; |
---|
8093 | n/a | } |
---|
8094 | n/a | w->data[n++] = carry; |
---|
8095 | n/a | } |
---|
8096 | n/a | carry = _mpd_shortadd_b(w->data, n, u[ulen], wbase); |
---|
8097 | n/a | if (carry) { |
---|
8098 | n/a | if (n >= wlen) { |
---|
8099 | n/a | if (!mpd_qresize(w, n+1, status)) { |
---|
8100 | n/a | return SIZE_MAX; |
---|
8101 | n/a | } |
---|
8102 | n/a | wlen = n+1; |
---|
8103 | n/a | } |
---|
8104 | n/a | w->data[n++] = carry; |
---|
8105 | n/a | } |
---|
8106 | n/a | } |
---|
8107 | n/a | |
---|
8108 | n/a | return n; |
---|
8109 | n/a | } |
---|
8110 | n/a | |
---|
8111 | n/a | /* |
---|
8112 | n/a | * Convert an integer mpd_t to a multiprecision integer with base <= 2**16. |
---|
8113 | n/a | * The least significant word of the result is (*rdata)[0]. |
---|
8114 | n/a | * |
---|
8115 | n/a | * If rdata is NULL, space is allocated by the function and rlen is irrelevant. |
---|
8116 | n/a | * In case of an error any allocated storage is freed and rdata is set back to |
---|
8117 | n/a | * NULL. |
---|
8118 | n/a | * |
---|
8119 | n/a | * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation |
---|
8120 | n/a | * functions and rlen MUST be correct. If necessary, the function will resize |
---|
8121 | n/a | * rdata. In case of an error the caller must free rdata. |
---|
8122 | n/a | * |
---|
8123 | n/a | * Return value: In case of success, the exact length of rdata, SIZE_MAX |
---|
8124 | n/a | * otherwise. |
---|
8125 | n/a | */ |
---|
8126 | n/a | size_t |
---|
8127 | n/a | mpd_qexport_u16(uint16_t **rdata, size_t rlen, uint32_t rbase, |
---|
8128 | n/a | const mpd_t *src, uint32_t *status) |
---|
8129 | n/a | { |
---|
8130 | n/a | MPD_NEW_STATIC(tsrc,0,0,0,0); |
---|
8131 | n/a | int alloc = 0; /* rdata == NULL */ |
---|
8132 | n/a | size_t n; |
---|
8133 | n/a | |
---|
8134 | n/a | assert(rbase <= (1U<<16)); |
---|
8135 | n/a | |
---|
8136 | n/a | if (mpd_isspecial(src) || !_mpd_isint(src)) { |
---|
8137 | n/a | *status |= MPD_Invalid_operation; |
---|
8138 | n/a | return SIZE_MAX; |
---|
8139 | n/a | } |
---|
8140 | n/a | |
---|
8141 | n/a | if (*rdata == NULL) { |
---|
8142 | n/a | rlen = mpd_sizeinbase(src, rbase); |
---|
8143 | n/a | if (rlen == SIZE_MAX) { |
---|
8144 | n/a | *status |= MPD_Invalid_operation; |
---|
8145 | n/a | return SIZE_MAX; |
---|
8146 | n/a | } |
---|
8147 | n/a | *rdata = mpd_alloc(rlen, sizeof **rdata); |
---|
8148 | n/a | if (*rdata == NULL) { |
---|
8149 | n/a | goto malloc_error; |
---|
8150 | n/a | } |
---|
8151 | n/a | alloc = 1; |
---|
8152 | n/a | } |
---|
8153 | n/a | |
---|
8154 | n/a | if (mpd_iszero(src)) { |
---|
8155 | n/a | **rdata = 0; |
---|
8156 | n/a | return 1; |
---|
8157 | n/a | } |
---|
8158 | n/a | |
---|
8159 | n/a | if (src->exp >= 0) { |
---|
8160 | n/a | if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { |
---|
8161 | n/a | goto malloc_error; |
---|
8162 | n/a | } |
---|
8163 | n/a | } |
---|
8164 | n/a | else { |
---|
8165 | n/a | if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { |
---|
8166 | n/a | goto malloc_error; |
---|
8167 | n/a | } |
---|
8168 | n/a | } |
---|
8169 | n/a | |
---|
8170 | n/a | n = _baseconv_to_u16(rdata, rlen, rbase, tsrc.data, tsrc.len); |
---|
8171 | n/a | if (n == SIZE_MAX) { |
---|
8172 | n/a | goto malloc_error; |
---|
8173 | n/a | } |
---|
8174 | n/a | |
---|
8175 | n/a | |
---|
8176 | n/a | out: |
---|
8177 | n/a | mpd_del(&tsrc); |
---|
8178 | n/a | return n; |
---|
8179 | n/a | |
---|
8180 | n/a | malloc_error: |
---|
8181 | n/a | if (alloc) { |
---|
8182 | n/a | mpd_free(*rdata); |
---|
8183 | n/a | *rdata = NULL; |
---|
8184 | n/a | } |
---|
8185 | n/a | n = SIZE_MAX; |
---|
8186 | n/a | *status |= MPD_Malloc_error; |
---|
8187 | n/a | goto out; |
---|
8188 | n/a | } |
---|
8189 | n/a | |
---|
8190 | n/a | /* |
---|
8191 | n/a | * Convert an integer mpd_t to a multiprecision integer with base<=UINT32_MAX. |
---|
8192 | n/a | * The least significant word of the result is (*rdata)[0]. |
---|
8193 | n/a | * |
---|
8194 | n/a | * If rdata is NULL, space is allocated by the function and rlen is irrelevant. |
---|
8195 | n/a | * In case of an error any allocated storage is freed and rdata is set back to |
---|
8196 | n/a | * NULL. |
---|
8197 | n/a | * |
---|
8198 | n/a | * If rdata is non-NULL, it MUST be allocated by one of libmpdec's allocation |
---|
8199 | n/a | * functions and rlen MUST be correct. If necessary, the function will resize |
---|
8200 | n/a | * rdata. In case of an error the caller must free rdata. |
---|
8201 | n/a | * |
---|
8202 | n/a | * Return value: In case of success, the exact length of rdata, SIZE_MAX |
---|
8203 | n/a | * otherwise. |
---|
8204 | n/a | */ |
---|
8205 | n/a | size_t |
---|
8206 | n/a | mpd_qexport_u32(uint32_t **rdata, size_t rlen, uint32_t rbase, |
---|
8207 | n/a | const mpd_t *src, uint32_t *status) |
---|
8208 | n/a | { |
---|
8209 | n/a | MPD_NEW_STATIC(tsrc,0,0,0,0); |
---|
8210 | n/a | int alloc = 0; /* rdata == NULL */ |
---|
8211 | n/a | size_t n; |
---|
8212 | n/a | |
---|
8213 | n/a | if (mpd_isspecial(src) || !_mpd_isint(src)) { |
---|
8214 | n/a | *status |= MPD_Invalid_operation; |
---|
8215 | n/a | return SIZE_MAX; |
---|
8216 | n/a | } |
---|
8217 | n/a | |
---|
8218 | n/a | if (*rdata == NULL) { |
---|
8219 | n/a | rlen = mpd_sizeinbase(src, rbase); |
---|
8220 | n/a | if (rlen == SIZE_MAX) { |
---|
8221 | n/a | *status |= MPD_Invalid_operation; |
---|
8222 | n/a | return SIZE_MAX; |
---|
8223 | n/a | } |
---|
8224 | n/a | *rdata = mpd_alloc(rlen, sizeof **rdata); |
---|
8225 | n/a | if (*rdata == NULL) { |
---|
8226 | n/a | goto malloc_error; |
---|
8227 | n/a | } |
---|
8228 | n/a | alloc = 1; |
---|
8229 | n/a | } |
---|
8230 | n/a | |
---|
8231 | n/a | if (mpd_iszero(src)) { |
---|
8232 | n/a | **rdata = 0; |
---|
8233 | n/a | return 1; |
---|
8234 | n/a | } |
---|
8235 | n/a | |
---|
8236 | n/a | if (src->exp >= 0) { |
---|
8237 | n/a | if (!mpd_qshiftl(&tsrc, src, src->exp, status)) { |
---|
8238 | n/a | goto malloc_error; |
---|
8239 | n/a | } |
---|
8240 | n/a | } |
---|
8241 | n/a | else { |
---|
8242 | n/a | if (mpd_qshiftr(&tsrc, src, -src->exp, status) == MPD_UINT_MAX) { |
---|
8243 | n/a | goto malloc_error; |
---|
8244 | n/a | } |
---|
8245 | n/a | } |
---|
8246 | n/a | |
---|
8247 | n/a | #ifdef CONFIG_64 |
---|
8248 | n/a | n = _baseconv_to_smaller(rdata, rlen, rbase, |
---|
8249 | n/a | tsrc.data, tsrc.len, MPD_RADIX); |
---|
8250 | n/a | #else |
---|
8251 | n/a | if (rbase == MPD_RADIX) { |
---|
8252 | n/a | n = _copy_equal_base(rdata, rlen, tsrc.data, tsrc.len); |
---|
8253 | n/a | } |
---|
8254 | n/a | else if (rbase < MPD_RADIX) { |
---|
8255 | n/a | n = _baseconv_to_smaller(rdata, rlen, rbase, |
---|
8256 | n/a | tsrc.data, tsrc.len, MPD_RADIX); |
---|
8257 | n/a | } |
---|
8258 | n/a | else { |
---|
8259 | n/a | n = _baseconv_to_larger(rdata, rlen, rbase, |
---|
8260 | n/a | tsrc.data, tsrc.len, MPD_RADIX); |
---|
8261 | n/a | } |
---|
8262 | n/a | #endif |
---|
8263 | n/a | |
---|
8264 | n/a | if (n == SIZE_MAX) { |
---|
8265 | n/a | goto malloc_error; |
---|
8266 | n/a | } |
---|
8267 | n/a | |
---|
8268 | n/a | |
---|
8269 | n/a | out: |
---|
8270 | n/a | mpd_del(&tsrc); |
---|
8271 | n/a | return n; |
---|
8272 | n/a | |
---|
8273 | n/a | malloc_error: |
---|
8274 | n/a | if (alloc) { |
---|
8275 | n/a | mpd_free(*rdata); |
---|
8276 | n/a | *rdata = NULL; |
---|
8277 | n/a | } |
---|
8278 | n/a | n = SIZE_MAX; |
---|
8279 | n/a | *status |= MPD_Malloc_error; |
---|
8280 | n/a | goto out; |
---|
8281 | n/a | } |
---|
8282 | n/a | |
---|
8283 | n/a | |
---|
8284 | n/a | /* |
---|
8285 | n/a | * Converts a multiprecision integer with base <= UINT16_MAX+1 to an mpd_t. |
---|
8286 | n/a | * The least significant word of the source is srcdata[0]. |
---|
8287 | n/a | */ |
---|
8288 | n/a | void |
---|
8289 | n/a | mpd_qimport_u16(mpd_t *result, |
---|
8290 | n/a | const uint16_t *srcdata, size_t srclen, |
---|
8291 | n/a | uint8_t srcsign, uint32_t srcbase, |
---|
8292 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
8293 | n/a | { |
---|
8294 | n/a | mpd_uint_t *usrc; /* uint16_t src copied to an mpd_uint_t array */ |
---|
8295 | n/a | mpd_ssize_t rlen; /* length of the result */ |
---|
8296 | n/a | size_t n; |
---|
8297 | n/a | |
---|
8298 | n/a | assert(srclen > 0); |
---|
8299 | n/a | assert(srcbase <= (1U<<16)); |
---|
8300 | n/a | |
---|
8301 | n/a | rlen = _mpd_importsize(srclen, srcbase); |
---|
8302 | n/a | if (rlen == MPD_SSIZE_MAX) { |
---|
8303 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
8304 | n/a | return; |
---|
8305 | n/a | } |
---|
8306 | n/a | |
---|
8307 | n/a | usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); |
---|
8308 | n/a | if (usrc == NULL) { |
---|
8309 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
8310 | n/a | return; |
---|
8311 | n/a | } |
---|
8312 | n/a | for (n = 0; n < srclen; n++) { |
---|
8313 | n/a | usrc[n] = srcdata[n]; |
---|
8314 | n/a | } |
---|
8315 | n/a | |
---|
8316 | n/a | if (!mpd_qresize(result, rlen, status)) { |
---|
8317 | n/a | goto finish; |
---|
8318 | n/a | } |
---|
8319 | n/a | |
---|
8320 | n/a | n = _coeff_from_u16(result, rlen, usrc, srclen, srcbase, status); |
---|
8321 | n/a | if (n == SIZE_MAX) { |
---|
8322 | n/a | goto finish; |
---|
8323 | n/a | } |
---|
8324 | n/a | |
---|
8325 | n/a | mpd_set_flags(result, srcsign); |
---|
8326 | n/a | result->exp = 0; |
---|
8327 | n/a | result->len = n; |
---|
8328 | n/a | mpd_setdigits(result); |
---|
8329 | n/a | |
---|
8330 | n/a | mpd_qresize(result, result->len, status); |
---|
8331 | n/a | mpd_qfinalize(result, ctx, status); |
---|
8332 | n/a | |
---|
8333 | n/a | |
---|
8334 | n/a | finish: |
---|
8335 | n/a | mpd_free(usrc); |
---|
8336 | n/a | } |
---|
8337 | n/a | |
---|
8338 | n/a | /* |
---|
8339 | n/a | * Converts a multiprecision integer with base <= UINT32_MAX to an mpd_t. |
---|
8340 | n/a | * The least significant word of the source is srcdata[0]. |
---|
8341 | n/a | */ |
---|
8342 | n/a | void |
---|
8343 | n/a | mpd_qimport_u32(mpd_t *result, |
---|
8344 | n/a | const uint32_t *srcdata, size_t srclen, |
---|
8345 | n/a | uint8_t srcsign, uint32_t srcbase, |
---|
8346 | n/a | const mpd_context_t *ctx, uint32_t *status) |
---|
8347 | n/a | { |
---|
8348 | n/a | mpd_ssize_t rlen; /* length of the result */ |
---|
8349 | n/a | size_t n; |
---|
8350 | n/a | |
---|
8351 | n/a | assert(srclen > 0); |
---|
8352 | n/a | |
---|
8353 | n/a | rlen = _mpd_importsize(srclen, srcbase); |
---|
8354 | n/a | if (rlen == MPD_SSIZE_MAX) { |
---|
8355 | n/a | mpd_seterror(result, MPD_Invalid_operation, status); |
---|
8356 | n/a | return; |
---|
8357 | n/a | } |
---|
8358 | n/a | |
---|
8359 | n/a | if (!mpd_qresize(result, rlen, status)) { |
---|
8360 | n/a | return; |
---|
8361 | n/a | } |
---|
8362 | n/a | |
---|
8363 | n/a | #ifdef CONFIG_64 |
---|
8364 | n/a | n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, |
---|
8365 | n/a | srcdata, srclen, srcbase, |
---|
8366 | n/a | status); |
---|
8367 | n/a | #else |
---|
8368 | n/a | if (srcbase == MPD_RADIX) { |
---|
8369 | n/a | if (!mpd_qresize(result, srclen, status)) { |
---|
8370 | n/a | return; |
---|
8371 | n/a | } |
---|
8372 | n/a | memcpy(result->data, srcdata, srclen * (sizeof *srcdata)); |
---|
8373 | n/a | n = srclen; |
---|
8374 | n/a | } |
---|
8375 | n/a | else if (srcbase < MPD_RADIX) { |
---|
8376 | n/a | n = _coeff_from_smaller_base(result, rlen, MPD_RADIX, |
---|
8377 | n/a | srcdata, srclen, srcbase, |
---|
8378 | n/a | status); |
---|
8379 | n/a | } |
---|
8380 | n/a | else { |
---|
8381 | n/a | mpd_uint_t *usrc = mpd_alloc((mpd_size_t)srclen, sizeof *usrc); |
---|
8382 | n/a | if (usrc == NULL) { |
---|
8383 | n/a | mpd_seterror(result, MPD_Malloc_error, status); |
---|
8384 | n/a | return; |
---|
8385 | n/a | } |
---|
8386 | n/a | for (n = 0; n < srclen; n++) { |
---|
8387 | n/a | usrc[n] = srcdata[n]; |
---|
8388 | n/a | } |
---|
8389 | n/a | |
---|
8390 | n/a | n = _coeff_from_larger_base(result, rlen, MPD_RADIX, |
---|
8391 | n/a | usrc, (mpd_ssize_t)srclen, srcbase, |
---|
8392 | n/a | status); |
---|
8393 | n/a | mpd_free(usrc); |
---|
8394 | n/a | } |
---|
8395 | n/a | #endif |
---|
8396 | n/a | |
---|
8397 | n/a | if (n == SIZE_MAX) { |
---|
8398 | n/a | return; |
---|
8399 | n/a | } |
---|
8400 | n/a | |
---|
8401 | n/a | mpd_set_flags(result, srcsign); |
---|
8402 | n/a | result->exp = 0; |
---|
8403 | n/a | result->len = n; |
---|
8404 | n/a | mpd_setdigits(result); |
---|
8405 | n/a | |
---|
8406 | n/a | mpd_qresize(result, result->len, status); |
---|
8407 | n/a | mpd_qfinalize(result, ctx, status); |
---|
8408 | n/a | } |
---|
8409 | n/a | |
---|
8410 | n/a | |
---|
8411 | n/a | |
---|