1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/* Test mpf_trunc, mpf_ceil, mpf_floor.
Copyright 2001, 2002, 2020 Free Software Foundation, Inc.
This file is part of the GNU MP Library test suite.
The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
#include <stdio.h>
#include <stdlib.h>
#include "gmp-impl.h"
#include "tests.h"
void
check_print (mpf_srcptr src, mpf_srcptr got, mpf_srcptr want)
{
mp_trace_base = 16;
mpf_trace ("src ", src);
mpf_trace ("got ", got);
mpf_trace ("want", want);
printf ("got size=%d exp=%ld\n", SIZ(got), EXP(got));
mpn_trace (" limbs=", PTR(got), (mp_size_t) ABSIZ(got));
printf ("want size=%d exp=%ld\n", SIZ(want), EXP(want));
mpn_trace (" limbs=", PTR(want), (mp_size_t) ABSIZ(want));
}
void
check_one (mpf_srcptr src, mpf_srcptr trunc, mpf_srcptr ceil, mpf_srcptr floor)
{
mpf_t got;
mpf_init2 (got, mpf_get_prec (trunc));
ASSERT_ALWAYS (PREC(got) == PREC(trunc));
ASSERT_ALWAYS (PREC(got) == PREC(ceil));
ASSERT_ALWAYS (PREC(got) == PREC(floor));
#define CHECK_SEP(name, fun, want) \
mpf_set_ui (got, 54321L); /* initial junk */ \
fun (got, src); \
MPF_CHECK_FORMAT (got); \
if (mpf_cmp (got, want) != 0) \
{ \
printf ("%s wrong\n", name); \
check_print (src, got, want); \
abort (); \
}
CHECK_SEP ("mpf_trunc", mpf_trunc, trunc);
CHECK_SEP ("mpf_ceil", mpf_ceil, ceil);
CHECK_SEP ("mpf_floor", mpf_floor, floor);
#define CHECK_INPLACE(name, fun, want) \
mpf_set (got, src); \
fun (got, got); \
MPF_CHECK_FORMAT (got); \
if (mpf_cmp (got, want) != 0) \
{ \
printf ("%s wrong\n", name); \
check_print (src, got, want); \
abort (); \
}
CHECK_INPLACE ("mpf_trunc", mpf_trunc, trunc);
/* Can't do these unconditionally in case truncation by mpf_set strips
some low non-zero limbs which would have rounded the result. */
if (mpf_size (src) <= PREC(trunc)+1)
{
CHECK_INPLACE ("mpf_ceil", mpf_ceil, ceil);
CHECK_INPLACE ("mpf_floor", mpf_floor, floor);
}
mpf_clear (got);
}
void
check_all (mpf_ptr src, mpf_ptr trunc, mpf_ptr ceil, mpf_ptr floor)
{
/* some of these values are generated with direct field assignments */
MPF_CHECK_FORMAT (src);
MPF_CHECK_FORMAT (trunc);
MPF_CHECK_FORMAT (ceil);
MPF_CHECK_FORMAT (floor);
check_one (src, trunc, ceil, floor);
mpf_neg (src, src);
mpf_neg (trunc, trunc);
mpf_neg (ceil, ceil);
mpf_neg (floor, floor);
check_one (src, trunc, floor, ceil);
}
void
check_various (void)
{
mpf_t src, trunc, ceil, floor;
int n, i;
mpf_init2 (src, 512L);
mpf_init2 (trunc, 256L);
mpf_init2 (ceil, 256L);
mpf_init2 (floor, 256L);
/* 0 */
mpf_set_ui (src, 0L);
mpf_set_ui (trunc, 0L);
mpf_set_ui (ceil, 0L);
mpf_set_ui (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 1 */
mpf_set_ui (src, 1L);
mpf_set_ui (trunc, 1L);
mpf_set_ui (ceil, 1L);
mpf_set_ui (floor, 1L);
check_all (src, trunc, ceil, floor);
/* 2^1024 */
mpf_set_ui (src, 1L);
mpf_mul_2exp (src, src, 1024L);
mpf_set (trunc, src);
mpf_set (ceil, src);
mpf_set (floor, src);
check_all (src, trunc, ceil, floor);
/* 1/2^1024, fraction only */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 1024L);
mpf_set_si (trunc, 0L);
mpf_set_si (ceil, 1L);
mpf_set_si (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 1/2 */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 1L);
mpf_set_si (trunc, 0L);
mpf_set_si (ceil, 1L);
mpf_set_si (floor, 0L);
check_all (src, trunc, ceil, floor);
/* 123+1/2^64 */
mpf_set_ui (src, 1L);
mpf_div_2exp (src, src, 64L);
mpf_add_ui (src, src, 123L);
mpf_set_si (trunc, 123L);
mpf_set_si (ceil, 124L);
mpf_set_si (floor, 123L);
check_all (src, trunc, ceil, floor);
/* integer of full prec+1 limbs, unchanged */
n = PREC(trunc)+1;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+100;
mpf_set (trunc, src);
mpf_set (ceil, src);
mpf_set (floor, src);
check_all (src, trunc, ceil, floor);
/* full prec+1 limbs, 1 trimmed for integer */
n = PREC(trunc)+1;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n-1;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+200;
EXP(trunc) = n-1;
SIZ(trunc) = n-1;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+201;
mpf_set (floor, trunc);
mpf_add_ui (ceil, trunc, 1L);
check_all (src, trunc, ceil, floor);
/* prec+3 limbs, 2 trimmed for size */
n = PREC(trunc)+3;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+300;
EXP(trunc) = n;
SIZ(trunc) = n-2;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+302;
mpf_set (floor, trunc);
mpf_set (ceil, trunc);
PTR(ceil)[0]++;
check_all (src, trunc, ceil, floor);
/* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
n = PREC(trunc)+4;
ASSERT_ALWAYS (n <= PREC(src)+1);
EXP(src) = n-1;
SIZ(src) = n;
for (i = 0; i < SIZ(src); i++)
PTR(src)[i] = i+400;
EXP(trunc) = n-1;
SIZ(trunc) = n-3;
for (i = 0; i < SIZ(trunc); i++)
PTR(trunc)[i] = i+403;
mpf_set (floor, trunc);
mpf_set (ceil, trunc);
PTR(ceil)[0]++;
check_all (src, trunc, ceil, floor);
/* F.F, carry out of ceil */
EXP(src) = 1;
SIZ(src) = 2;
PTR(src)[0] = GMP_NUMB_MAX;
PTR(src)[1] = GMP_NUMB_MAX;
EXP(trunc) = 1;
SIZ(trunc) = 1;
PTR(trunc)[0] = GMP_NUMB_MAX;
mpf_set (floor, trunc);
EXP(ceil) = 2;
SIZ(ceil) = 1;
PTR(ceil)[0] = 1;
check_all (src, trunc, ceil, floor);
/* FF.F, carry out of ceil */
EXP(src) = 2;
SIZ(src) = 3;
PTR(src)[0] = GMP_NUMB_MAX;
PTR(src)[1] = GMP_NUMB_MAX;
PTR(src)[2] = GMP_NUMB_MAX;
EXP(trunc) = 2;
SIZ(trunc) = 2;
PTR(trunc)[0] = GMP_NUMB_MAX;
PTR(trunc)[1] = GMP_NUMB_MAX;
mpf_set (floor, trunc);
EXP(ceil) = 3;
SIZ(ceil) = 1;
PTR(ceil)[0] = 1;
check_all (src, trunc, ceil, floor);
mpf_clear (src);
mpf_clear (trunc);
mpf_clear (ceil);
mpf_clear (floor);
}
int
main (void)
{
tests_start ();
check_various ();
tests_end ();
exit (0);
}
|