1 /* Test mpn_add_1 and mpn_sub_1.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
28 #define M GMP_NUMB_MAX
34 refmpn_random (got, data[i].size); \
35 got[data[i].size] = MAGIC; \
38 #define SETUP_INPLACE() \
40 refmpn_copyi (got, data[i].src, data[i].size); \
41 got[data[i].size] = MAGIC; \
44 #define VERIFY(name) \
46 verify (name, i, data[i].src, data[i].n, \
47 got_c, data[i].want_c, \
48 got, data[i].want, data[i].size); \
51 typedef mp_limb_t (*mpn_aors_1_t)
52 __GMP_PROTO ((mp_ptr, mp_srcptr, mp_size_t, mp_limb_t));
53 mpn_aors_1_t fudge __GMP_PROTO ((mpn_aors_1_t));
57 verify (const char *name, int i,
58 mp_srcptr src, mp_limb_t n,
59 mp_limb_t got_c, mp_limb_t want_c,
60 mp_srcptr got, mp_srcptr want, mp_size_t size)
62 if (got[size] != MAGIC)
64 printf ("Overwrite at %s i=%d\n", name, i);
68 if (got_c != want_c || ! refmpn_equal_anynail (got, want, size))
70 printf ("Wrong at %s i=%d size=%ld\n", name, i, size);
71 mpn_trace (" src", src, size);
72 mpn_trace (" n", &n, (mp_size_t) 1);
73 mpn_trace (" got", got, size);
74 mpn_trace (" want", want, size);
75 mpn_trace (" got c", &got_c, (mp_size_t) 1);
76 mpn_trace ("want c", &want_c, (mp_size_t) 1);
88 const mp_limb_t src[ASIZE];
90 const mp_limb_t want[ASIZE];
92 { 1, 0, { 0 }, 0, { 0 } },
93 { 1, 0, { 1 }, 0, { 1 } },
94 { 1, 1, { 0 }, 0, { 1 } },
95 { 1, 0, { M }, 0, { M } },
96 { 1, M, { 0 }, 0, { M } },
97 { 1, 1, { 123 }, 0, { 124 } },
99 { 1, 1, { M }, 1, { 0 } },
100 { 1, M, { 1 }, 1, { 0 } },
101 { 1, M, { M }, 1, { M-1 } },
103 { 2, 0, { 0, 0 }, 0, { 0, 0 } },
104 { 2, 0, { 1, 0 }, 0, { 1, 0 } },
105 { 2, 1, { 0, 0 }, 0, { 1, 0 } },
106 { 2, 0, { M, 0 }, 0, { M, 0 } },
107 { 2, M, { 0, 0 }, 0, { M, 0 } },
108 { 2, 1, { M, 0 }, 0, { 0, 1 } },
109 { 2, M, { 1, 0 }, 0, { 0, 1 } },
110 { 2, M, { M, 0 }, 0, { M-1, 1 } },
111 { 2, M, { M, 0 }, 0, { M-1, 1 } },
113 { 2, 1, { M, M }, 1, { 0, 0 } },
114 { 2, M, { 1, M }, 1, { 0, 0 } },
115 { 2, M, { M, M }, 1, { M-1, 0 } },
116 { 2, M, { M, M }, 1, { M-1, 0 } },
118 { 3, 1, { M, M, M }, 1, { 0, 0, 0 } },
119 { 3, M, { 1, M, M }, 1, { 0, 0, 0 } },
120 { 3, M, { M, M, M }, 1, { M-1, 0, 0 } },
121 { 3, M, { M, M, M }, 1, { M-1, 0, 0 } },
123 { 4, 1, { M, M, M, M }, 1, { 0, 0, 0, 0 } },
124 { 4, M, { 1, M, M, M }, 1, { 0, 0, 0, 0 } },
125 { 4, M, { M, M, M, M }, 1, { M-1, 0, 0, 0 } },
126 { 4, M, { M, M, M, M }, 1, { M-1, 0, 0, 0 } },
128 { 4, M, { M, 0, M, M }, 0, { M-1, 1, M, M } },
129 { 4, M, { M, M-1, M, M }, 0, { M-1, M, M, M } },
131 { 4, M, { M, M, 0, M }, 0, { M-1, 0, 1, M } },
132 { 4, M, { M, M, M-1, M }, 0, { M-1, 0, M, M } },
135 mp_limb_t got[ASIZE];
139 for (i = 0; i < numberof (data); i++)
142 got_c = mpn_add_1 (got, data[i].src, data[i].size, data[i].n);
143 VERIFY ("check_add_1 (separate)");
146 got_c = mpn_add_1 (got, got, data[i].size, data[i].n);
147 VERIFY ("check_add_1 (in-place)");
152 got_c = mpn_add_1 (got, data[i].src, data[i].size, CNST_LIMB(1));
153 VERIFY ("check_add_1 (separate, const 1)");
156 got_c = mpn_add_1 (got, got, data[i].size, CNST_LIMB(1));
157 VERIFY ("check_add_1 (in-place, const 1)");
160 /* Same again on functions, not inlines. */
162 got_c = (*fudge(mpn_add_1)) (got, data[i].src, data[i].size, data[i].n);
163 VERIFY ("check_add_1 (function, separate)");
166 got_c = (*fudge(mpn_add_1)) (got, got, data[i].size, data[i].n);
167 VERIFY ("check_add_1 (function, in-place)");
174 static const struct {
177 const mp_limb_t src[ASIZE];
179 const mp_limb_t want[ASIZE];
181 { 1, 0, { 0 }, 0, { 0 } },
182 { 1, 0, { 1 }, 0, { 1 } },
183 { 1, 1, { 1 }, 0, { 0 } },
184 { 1, 0, { M }, 0, { M } },
185 { 1, 1, { M }, 0, { M-1 } },
186 { 1, 1, { 123 }, 0, { 122 } },
188 { 1, 1, { 0 }, 1, { M } },
189 { 1, M, { 0 }, 1, { 1 } },
191 { 2, 0, { 0, 0 }, 0, { 0, 0 } },
192 { 2, 0, { 1, 0 }, 0, { 1, 0 } },
193 { 2, 1, { 1, 0 }, 0, { 0, 0 } },
194 { 2, 0, { M, 0 }, 0, { M, 0 } },
195 { 2, 1, { M, 0 }, 0, { M-1, 0 } },
196 { 2, 1, { 123, 0 }, 0, { 122, 0 } },
198 { 2, 1, { 0, 0 }, 1, { M, M } },
199 { 2, M, { 0, 0 }, 1, { 1, M } },
201 { 3, 0, { 0, 0, 0 }, 0, { 0, 0, 0 } },
202 { 3, 0, { 123, 0, 0 }, 0, { 123, 0, 0 } },
204 { 3, 1, { 0, 0, 0 }, 1, { M, M, M } },
205 { 3, M, { 0, 0, 0 }, 1, { 1, M, M } },
207 { 4, 1, { 0, 0, 0, 0 }, 1, { M, M, M, M } },
208 { 4, M, { 0, 0, 0, 0 }, 1, { 1, M, M, M } },
210 { 4, 1, { 0, 0, 1, 42 }, 0, { M, M, 0, 42 } },
211 { 4, M, { 0, 0, 123, 24 }, 0, { 1, M, 122, 24 } },
214 mp_limb_t got[ASIZE];
218 for (i = 0; i < numberof (data); i++)
221 got_c = mpn_sub_1 (got, data[i].src, data[i].size, data[i].n);
222 VERIFY ("check_sub_1 (separate)");
225 got_c = mpn_sub_1 (got, got, data[i].size, data[i].n);
226 VERIFY ("check_sub_1 (in-place)");
231 got_c = mpn_sub_1 (got, data[i].src, data[i].size, CNST_LIMB(1));
232 VERIFY ("check_sub_1 (separate, const 1)");
235 got_c = mpn_sub_1 (got, got, data[i].size, CNST_LIMB(1));
236 VERIFY ("check_sub_1 (in-place, const 1)");
239 /* Same again on functions, not inlines. */
241 got_c = (*fudge(mpn_sub_1)) (got, data[i].src, data[i].size, data[i].n);
242 VERIFY ("check_sub_1 (function, separate)");
245 got_c = (*fudge(mpn_sub_1)) (got, got, data[i].size, data[i].n);
246 VERIFY ("check_sub_1 (function, in-place)");
250 /* Try to prevent the optimizer inlining. */
252 fudge (mpn_aors_1_t f)