/src/mpdecimal-4.0.0/libmpdec/crt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2008-2024 Stefan Krah. All rights reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions |
6 | | * are met: |
7 | | * |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | |
28 | | #include <assert.h> |
29 | | |
30 | | #include "constants.h" |
31 | | #include "crt.h" |
32 | | #include "numbertheory.h" |
33 | | #include "mpdecimal.h" |
34 | | #include "typearith.h" |
35 | | #include "umodarith.h" |
36 | | |
37 | | |
38 | | /* Bignum: Chinese Remainder Theorem, extends the maximum transform length. */ |
39 | | |
40 | | |
41 | | /* Multiply P1P2 by v, store result in w. */ |
42 | | static inline void |
43 | | _crt_mulP1P2_3(mpd_uint_t w[3], mpd_uint_t v) |
44 | 0 | { |
45 | 0 | mpd_uint_t hi1, hi2, lo; |
46 | |
|
47 | 0 | _mpd_mul_words(&hi1, &lo, LH_P1P2, v); |
48 | 0 | w[0] = lo; |
49 | |
|
50 | 0 | _mpd_mul_words(&hi2, &lo, UH_P1P2, v); |
51 | 0 | lo = hi1 + lo; |
52 | 0 | if (lo < hi1) hi2++; |
53 | |
|
54 | 0 | w[1] = lo; |
55 | 0 | w[2] = hi2; |
56 | 0 | } |
57 | | |
58 | | /* Add 3 words from v to w. The result is known to fit in w. */ |
59 | | static inline void |
60 | | _crt_add3(mpd_uint_t w[3], mpd_uint_t v[3]) |
61 | 0 | { |
62 | 0 | mpd_uint_t carry; |
63 | |
|
64 | 0 | w[0] = w[0] + v[0]; |
65 | 0 | carry = (w[0] < v[0]); |
66 | |
|
67 | 0 | w[1] = w[1] + v[1]; |
68 | 0 | if (w[1] < v[1]) w[2]++; |
69 | |
|
70 | 0 | w[1] = w[1] + carry; |
71 | 0 | if (w[1] < carry) w[2]++; |
72 | |
|
73 | 0 | w[2] += v[2]; |
74 | 0 | } |
75 | | |
76 | | /* Divide 3 words in u by v, store result in w, return remainder. */ |
77 | | static inline mpd_uint_t |
78 | | _crt_div3(mpd_uint_t *w, const mpd_uint_t *u, mpd_uint_t v) |
79 | 0 | { |
80 | 0 | mpd_uint_t r1 = u[2]; |
81 | 0 | mpd_uint_t r2; |
82 | |
|
83 | 0 | if (r1 < v) { |
84 | 0 | w[2] = 0; |
85 | 0 | } |
86 | 0 | else { |
87 | 0 | _mpd_div_word(&w[2], &r1, u[2], v); /* GCOV_NOT_REACHED */ |
88 | 0 | } |
89 | |
|
90 | 0 | _mpd_div_words(&w[1], &r2, r1, u[1], v); |
91 | 0 | _mpd_div_words(&w[0], &r1, r2, u[0], v); |
92 | |
|
93 | 0 | return r1; |
94 | 0 | } |
95 | | |
96 | | |
97 | | /* |
98 | | * Chinese Remainder Theorem: |
99 | | * Algorithm from Joerg Arndt, "Matters Computational", |
100 | | * Chapter 37.4.1 [http://www.jjj.de/fxt/] |
101 | | * |
102 | | * See also Knuth, TAOCP, Volume 2, 4.3.2, exercise 7. |
103 | | */ |
104 | | |
105 | | /* |
106 | | * CRT with carry: x1, x2, x3 contain numbers modulo p1, p2, p3. For each |
107 | | * triple of members of the arrays, find the unique z modulo p1*p2*p3, with |
108 | | * zmax = p1*p2*p3 - 1. |
109 | | * |
110 | | * In each iteration of the loop, split z into result[i] = z % MPD_RADIX |
111 | | * and carry = z / MPD_RADIX. Let N be the size of carry[] and cmax the |
112 | | * maximum carry. |
113 | | * |
114 | | * Limits for the 32-bit build: |
115 | | * |
116 | | * N = 2**96 |
117 | | * cmax = 7711435591312380274 |
118 | | * |
119 | | * Limits for the 64 bit build: |
120 | | * |
121 | | * N = 2**192 |
122 | | * cmax = 627710135393475385904124401220046371710 |
123 | | * |
124 | | * The following statements hold for both versions: |
125 | | * |
126 | | * 1) cmax + zmax < N, so the addition does not overflow. |
127 | | * |
128 | | * 2) (cmax + zmax) / MPD_RADIX == cmax. |
129 | | * |
130 | | * 3) If c <= cmax, then c_next = (c + zmax) / MPD_RADIX <= cmax. |
131 | | */ |
132 | | void |
133 | | crt3(mpd_uint_t *x1, mpd_uint_t *x2, mpd_uint_t *x3, mpd_size_t rsize) |
134 | 0 | { |
135 | 0 | mpd_uint_t p1 = mpd_moduli[P1]; |
136 | 0 | mpd_uint_t umod; |
137 | | #ifdef PPRO |
138 | | double dmod; |
139 | | uint32_t dinvmod[3]; |
140 | | #endif |
141 | 0 | mpd_uint_t a1, a2, a3; |
142 | 0 | mpd_uint_t s; |
143 | 0 | mpd_uint_t z[3], t[3]; |
144 | 0 | mpd_uint_t carry[3] = {0,0,0}; |
145 | 0 | mpd_uint_t hi, lo; |
146 | 0 | mpd_size_t i; |
147 | |
|
148 | 0 | for (i = 0; i < rsize; i++) { |
149 | |
|
150 | 0 | a1 = x1[i]; |
151 | 0 | a2 = x2[i]; |
152 | 0 | a3 = x3[i]; |
153 | |
|
154 | 0 | SETMODULUS(P2); |
155 | 0 | s = ext_submod(a2, a1, umod); |
156 | 0 | s = MULMOD(s, INV_P1_MOD_P2); |
157 | |
|
158 | 0 | _mpd_mul_words(&hi, &lo, s, p1); |
159 | 0 | lo = lo + a1; |
160 | 0 | if (lo < a1) hi++; |
161 | |
|
162 | 0 | SETMODULUS(P3); |
163 | 0 | s = dw_submod(a3, hi, lo, umod); |
164 | 0 | s = MULMOD(s, INV_P1P2_MOD_P3); |
165 | |
|
166 | 0 | z[0] = lo; |
167 | 0 | z[1] = hi; |
168 | 0 | z[2] = 0; |
169 | |
|
170 | 0 | _crt_mulP1P2_3(t, s); |
171 | 0 | _crt_add3(z, t); |
172 | 0 | _crt_add3(carry, z); |
173 | |
|
174 | 0 | x1[i] = _crt_div3(carry, carry, MPD_RADIX); |
175 | 0 | } |
176 | |
|
177 | 0 | assert(carry[0] == 0 && carry[1] == 0 && carry[2] == 0); |
178 | 0 | } |