summaryrefslogtreecommitdiff
path: root/tests/mpf/t-ui_div.c
blob: 4b862152376caf0542653f94426e36f367505652 (plain)
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
/* Test mpf_ui_div.

Copyright 2004 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_one (const char *desc, mpf_ptr got, unsigned long u, mpf_srcptr v)
{
  mpf_t      uf;
  mp_limb_t  ulimbs[2];
  mp_size_t  usize;

  ulimbs[0] = u & GMP_NUMB_MASK;
  usize = (u != 0);
#if BITS_PER_ULONG > GMP_NUMB_BITS
  u >>= GMP_NUMB_BITS;
  ulimbs[1] = u;
  usize += (u != 0);
#endif
  PTR(uf) = ulimbs;
  SIZ(uf) = usize;
  EXP(uf) = usize;

  if (! refmpf_validate_division ("mpf_ui_div", got, uf, v))
    {
      mp_trace_base = -16;
      printf    ("  u 0x%lX  (%lu)\n", u, u);
      mpf_trace ("  v", v);
      printf    ("  %s\n", desc);
      abort ();
    }
}

void
check_rand (void)
{
  unsigned long  min_prec = __GMPF_BITS_TO_PREC (1);
  gmp_randstate_ptr  rands = RANDS;
  unsigned long  prec, u;
  mpf_t  got, v;
  int    i;

  mpf_init (got);
  mpf_init (v);

  for (i = 0; i < 200; i++)
    {
      /* got precision */
      prec = min_prec + gmp_urandomm_ui (rands, 15L);
      refmpf_set_prec_limbs (got, prec);

      /* u */
      prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1);
      u = gmp_urandomb_ui (rands, prec);

      /* v precision */
      prec = min_prec + gmp_urandomm_ui (rands, 15L);
      refmpf_set_prec_limbs (v, prec);

      /* v, non-zero */
      do {
        mpf_random2 (v, PREC(v), (mp_exp_t) 20);
      } while (SIZ(v) == 0);

      /* v possibly negative */
      if (gmp_urandomb_ui (rands, 1L))
        mpf_neg (v, v);

      if ((i % 2) == 0)
        {
          /* src != dst */
          mpf_ui_div (got, u, v);
          check_one ("separate", got, u, v);
        }
      else
        {
          /* src == dst */
          prec = refmpf_set_overlap (got, v);
          mpf_ui_div (got, u, got);
          check_one ("overlap src==dst", got, u, v);

          mpf_set_prec_raw (got, prec);
        }
    }

  mpf_clear (got);
  mpf_clear (v);
}

void
check_various (void)
{
  mpf_t got, v;

  mpf_init (got);
  mpf_init (v);

  /* 100/4 == 25 */
  mpf_set_prec (got, 20L);
  mpf_set_ui (v, 4L);
  mpf_ui_div (got, 100L, v);
  MPF_CHECK_FORMAT (got);
  ASSERT_ALWAYS (mpf_cmp_ui (got, 25L) == 0);

  {
    /* 1/(2^n+1), a case where truncating the divisor would be wrong */
    unsigned long  u = 1L;
    mpf_set_prec (got, 500L);
    mpf_set_prec (v, 900L);
    mpf_set_ui (v, 1L);
    mpf_mul_2exp (v, v, 800L);
    mpf_add_ui (v, v, 1L);
    mpf_ui_div (got, u, v);
    check_one ("1/2^n+1, separate", got, u, v);
  }

  mpf_clear (got);
  mpf_clear (v);
}

int
main (void)
{
  tests_start ();

  check_various ();
  check_rand ();

  tests_end ();
  exit (0);
}