File: tmul_ui.c

package info (click to toggle)
mpfr4 4.0.2-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 11,632 kB
  • sloc: ansic: 94,960; sh: 4,549; makefile: 322; perl: 74
file content (289 lines) | stat: -rw-r--r-- 8,150 bytes parent folder | download
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* Test file for mpfr_mul_ui.

Copyright 1999-2019 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 MPFR Library 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 Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#include "mpfr-test.h"

static void
check_inexact (mpfr_prec_t p)
{
  mpfr_t x, y, z;
  unsigned long u;
  mpfr_prec_t q;
  int inexact, cmp;
  int rnd;

  mpfr_init2 (x, p);
  mpfr_init (y);
  mpfr_init2 (z, p + mp_bits_per_limb);
  mpfr_urandomb (x, RANDS);
  u = randlimb ();
  if (mpfr_mul_ui (z, x, u, MPFR_RNDN))
    {
      printf ("Error: result should be exact\n");
      exit (1);
    }

  for (q = MPFR_PREC_MIN; q <= p; q++)
    for (rnd = 0; rnd < MPFR_RND_MAX; rnd++)
      {
        if (rnd == MPFR_RNDF)
          continue; /* inexact is undefined */

        mpfr_set_prec (y, q);
        inexact = mpfr_mul_ui (y, x, u, (mpfr_rnd_t) rnd);
        cmp = mpfr_cmp (y, z);
        if (((inexact == 0) && (cmp != 0)) ||
            ((inexact < 0) && (cmp >= 0)) ||
            ((inexact > 0) && (cmp <= 0)))
          {
            printf ("Wrong inexact flag for p=%u, q=%u, rnd=%s\n",
                    (unsigned int) p, (unsigned int) q,
                    mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
            exit (1);
          }
      }

  mpfr_set_prec (x, 2);
  mpfr_set_ui (x, 1, MPFR_RNDN);
  if (mpfr_mul_ui (x, x, 5, MPFR_RNDZ) == 0)
    {
      printf ("mul_ui(1, 5) cannot be exact with prec=2\n");
      exit (1);
    }

  mpfr_clear (x);
  mpfr_clear (y);
  mpfr_clear (z);
}

#define TEST_FUNCTION mpfr_mul_ui
#define ULONG_ARG2
#define RAND_FUNCTION(x) mpfr_random2(x, MPFR_LIMB_SIZE (x), 1, RANDS)
#include "tgeneric.c"

int
main (int argc, char *argv[])
{
  mpfr_t x, y;
  unsigned int xprec, yprec, i;
  mpfr_prec_t p;
  mpfr_exp_t emax;

  tests_start_mpfr ();

  for (p=2; p<100; p++)
    for (i=1; i<50; i++)
      check_inexact (p);

  mpfr_init2 (x, 53);
  mpfr_init2 (y, 53);

  /* checks that result is normalized */
  mpfr_set_str (y, "6.93147180559945286227e-01", 10, MPFR_RNDZ);
  mpfr_mul_ui (x, y, 1, MPFR_RNDZ);
  if (MPFR_MANT(x)[MPFR_PREC(x)/mp_bits_per_limb] >> (mp_bits_per_limb-1) == 0)
    {
      printf ("Error in mpfr_mul_ui: result not normalized\n");
      exit (1);
    }
  if (mpfr_cmp (x, y))
    {
      printf ("Error in mpfr_mul_ui: 1*y != y\n");
      printf ("y=  "); mpfr_dump (y);
      printf ("1*y="); mpfr_dump (x);
      exit (1);
    }

  mpfr_set_inf (x, 1);
  mpfr_mul_ui (x, x, 3, MPFR_RNDU);
  if (!mpfr_inf_p (x) || (mpfr_sgn (x) <= 0))
    {
      printf ("Error in mpfr_mul_ui: +Inf*3 does not give +Inf\n");
      exit (1);
    }

  mpfr_set_inf (x, -1);
  mpfr_mul_ui (x, x, 3, MPFR_RNDU);
  if (!mpfr_inf_p (x) || (mpfr_sgn (x) >= 0))
    {
      printf ("Error in mpfr_mul_ui: -Inf*3 does not give -Inf\n");
      exit (1);
    }

  mpfr_set_nan (x);
  mpfr_mul_ui (x, x, 3, MPFR_RNDU);
  if (!mpfr_nan_p(x))
    {
      printf ("Error in mpfr_mul_ui: NaN*3 does not give NaN\n");
      exit (1);
    }

  mpfr_set_inf (x, 1);
  mpfr_mul_ui (x, x, 0, MPFR_RNDU);
  MPFR_ASSERTN(mpfr_nan_p (x));

  mpfr_set_ui (x, 1, MPFR_RNDU);
  mpfr_mul_ui (x, x, 0, MPFR_RNDU);
  MPFR_ASSERTN(mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS(x));

  emax = mpfr_get_emax ();
  set_emax (0);
  mpfr_set_str_binary (x, "0.1E0");
  (mpfr_mul_ui) (x, x, 2, MPFR_RNDN);
  MPFR_ASSERTN(mpfr_inf_p (x) && MPFR_IS_POS(x));
  set_emax (emax);

  /* To check the macros call */
  mpfr_set_ui (x, 1, MPFR_RNDN);
  mpfr_mul_ui (x, x, 2, MPFR_RNDN);
  MPFR_ASSERTN(mpfr_cmp_ui(x, 2) == 0);
  mpfr_mul_si (x, x, 4, MPFR_RNDN);
  MPFR_ASSERTN(mpfr_cmp_ui(x, 8) == 0);

  mpfr_set_str (x, /*1.0/3.0*/
                "0.333333333333333333333333333333333", 10, MPFR_RNDZ);
  mpfr_mul_ui (x, x, 3, MPFR_RNDU);
  if (mpfr_cmp_ui (x, 1))
    {
      printf ("Error in mpfr_mul_ui: U(Z(1/3)*3) does not give 1\n");
      exit (1);
    }

  /* checks sign is correct */
  mpfr_set_si (x, -2, MPFR_RNDZ);
  mpfr_set_si (y, 3, MPFR_RNDZ);
  mpfr_mul_ui(x, y, 4, MPFR_RNDZ);
  if (mpfr_cmp_ui(x, 0) <= 0)
    {
      printf("Error in mpfr_mul_ui: 4*3.0 does not give a positive result:\n");
      mpfr_dump (x);
      printf("mpfr_cmp_ui(x, 0) = %d\n", mpfr_cmp_ui(x, 0));
      exit(1);
    }

  mpfr_set_prec (x, 9);
  mpfr_set_prec (y, 9);
  mpfr_set_str_binary (y, "0.100001111E9"); /* 271 */
  mpfr_mul_ui (x, y, 1335, MPFR_RNDN);
  mpfr_set_str_binary (y, "0.101100001E19"); /* 361472 */
  if (mpfr_cmp (x, y))
    {
      printf ("Error in mul_ui for 1335*(0.100001111E9)\n");
      printf ("got "); mpfr_dump (x);
      exit(1);
    }

  mpfr_set_prec(y, 100);
  mpfr_set_prec(x, 100);
  /* y = 1199781142214086656 */
  mpfr_set_str_binary(y, "0.1000010100110011110101001011110010101111000100001E61");
  mpfr_mul_ui(x, y, 121, MPFR_RNDD);
  /* 121*y = 145173518207904485376, representable exactly */
  mpfr_set_str_binary(y, "0.1111101111010101111111100011010010111010111110110011001E67");
  if (mpfr_cmp(x, y))
    {
      printf("Error for 121*y: expected result is:\n");
      mpfr_dump (y);
    }

  mpfr_set_prec (x, 32);
  mpfr_set_str_binary (x, "0.10000000000000000000000000000000E1");
  mpfr_set_prec (y, 93);
  mpfr_mul_ui (y, x, 1, MPFR_RNDN);

  mpfr_set_prec (x, 287);
  mpfr_set_str_binary (x, "0.1111E7");
  mpfr_set_prec (y, 289);
  mpfr_mul_ui (y, x, 6, MPFR_RNDN);
  mpfr_set_str_binary (x, "0.101101E10");
  if (mpfr_cmp (x, y))
    {
      printf ("Error for 6 * 120\n");
      exit (1);
    }

  mpfr_set_prec (x, 68);
  mpfr_set_prec (y, 64);
  mpfr_set_str (x, "2143861251406875.0", 10, MPFR_RNDN);
  mpfr_mul_ui (y, x, 23, MPFR_RNDN);
  mpfr_set_str_binary (x, "10101111001011100001100110101111110001010010011001101101.0");
  if (mpfr_cmp (x, y))
    {
      printf ("Error for 23 * 2143861251406875.0\n");
      printf ("expected "); mpfr_dump (x);
      printf ("got      "); mpfr_dump (y);
      exit (1);
    }


  for (xprec = 53; xprec <= 128; xprec++)
    {
      mpfr_set_prec (x, xprec);
      mpfr_set_str_binary (x, "0.1100100100001111110011111000000011011100001100110111E2");
      for (yprec = 53; yprec <= 128; yprec++)
        {
          mpfr_set_prec (y, yprec);
          mpfr_mul_ui (y, x, 1, MPFR_RNDN);
          if (mpfr_cmp(x,y))
            {
              printf ("multiplication by 1.0 fails for xprec=%u, yprec=%u\n",
                      xprec, yprec);
              printf ("expected "); mpfr_dump (x);
              printf ("got      "); mpfr_dump (y);
              exit (1);
            }
        }
    }

  mpfr_set_prec (x, 128);
  mpfr_set_ui (x, 17, MPFR_RNDN);
  mpfr_mul_ui (x, x, ULONG_HIGHBIT, MPFR_RNDN);
  mpfr_set_prec (y, 128);
  mpfr_set_ui (y, ULONG_HIGHBIT, MPFR_RNDN);
  mpfr_mul_ui (y, y, 17, MPFR_RNDN);
  if (mpfr_cmp (x, y))
    {
      printf ("Error for 17 * ULONG_HIGHBIT\n");
      exit (1);
    }

  /* Check regression */
  mpfr_set_prec (x, 32);
  mpfr_set_prec (y, 96);
  mpfr_set_ui (x, 1742175942, MPFR_RNDN);
  mpfr_mul_ui (y, x, 59, MPFR_RNDN);
  if (mpfr_cmp_str (y, "0.10111111011101010101000110111101000100000000000000"
                    "0000000000000000000000000000000000000000000000E37",
                    2, MPFR_RNDN))
    {
      printf ("Regression tested failed for x=1742175942 * 59\n");
      exit (1);
    }

  mpfr_clear(x);
  mpfr_clear(y);

  test_generic (MPFR_PREC_MIN, 500, 100);

  tests_end_mpfr ();
  return 0;
}