1 /* Test mpz_set_f.
2
3 Copyright 2001 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library test suite.
6
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24
25
26 void
check_one(mpz_srcptr z)27 check_one (mpz_srcptr z)
28 {
29 static const int shift[] = {
30 0, 1, GMP_LIMB_BITS, 2*GMP_LIMB_BITS, 5*GMP_LIMB_BITS
31 };
32
33 int sh, shneg, neg;
34 mpf_t f;
35 mpz_t got, want;
36
37 mpf_init2 (f, mpz_sizeinbase(z,2));
38 mpz_init (got);
39 mpz_init (want);
40
41 for (sh = 0; sh < numberof(shift); sh++)
42 {
43 for (shneg = 0; shneg <= 1; shneg++)
44 {
45 for (neg = 0; neg <= 1; neg++)
46 {
47 mpf_set_z (f, z);
48 mpz_set (want, z);
49
50 if (neg)
51 {
52 mpf_neg (f, f);
53 mpz_neg (want, want);
54 }
55
56 if (shneg)
57 {
58 mpz_tdiv_q_2exp (want, want, shift[sh]);
59 mpf_div_2exp (f, f, shift[sh]);
60 }
61 else
62 {
63 mpz_mul_2exp (want, want, shift[sh]);
64 mpf_mul_2exp (f, f, shift[sh]);
65 }
66
67 mpz_set_f (got, f);
68 MPZ_CHECK_FORMAT (got);
69
70 if (mpz_cmp (got, want) != 0)
71 {
72 printf ("wrong result\n");
73 printf (" shift %d\n", shneg ? -shift[sh] : shift[sh]);
74 printf (" neg %d\n", neg);
75 mpf_trace (" f", f);
76 mpz_trace (" got", got);
77 mpz_trace (" want", want);
78 abort ();
79 }
80 }
81 }
82 }
83
84 mpf_clear (f);
85 mpz_clear (got);
86 mpz_clear (want);
87 }
88
89
90 void
check_various(void)91 check_various (void)
92 {
93 mpz_t z;
94
95 mpz_init (z);
96
97 mpz_set_ui (z, 0L);
98 check_one (z);
99
100 mpz_set_si (z, 123L);
101 check_one (z);
102
103 mpz_rrandomb (z, RANDS, 2*GMP_LIMB_BITS);
104 check_one (z);
105
106 mpz_rrandomb (z, RANDS, 5*GMP_LIMB_BITS);
107 check_one (z);
108
109 mpz_clear (z);
110 }
111
112
113 int
main(int argc,char * argv[])114 main (int argc, char *argv[])
115 {
116 #if GMP_NAIL_BITS == 0
117 tests_start ();
118 mp_trace_base = 16;
119
120 check_various ();
121
122 tests_end ();
123 #endif
124 exit (0);
125 }
126