summaryrefslogtreecommitdiff
path: root/src/random_deviate.h
blob: 8c115ad95b753284ce88d5765fcca7c33aa2d4d9 (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
/* Declarations of random_deviate routines for mpfr_erandom and mpfr_nrandom.

Copyright 2013-2015 Free Software Foundation, Inc.
Contributed by Charles Karney <charles@karney.com>, SRI International.

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
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#if !defined(MPFR_RANDOM_DEVIATE_H)
#define MPFR_RANDOM_DEVIATE_H 1

#include "mpfr-impl.h"

/* This should be an unsigned type with a width of at least 32 and capable of
 * representing at least 2*MPFR_PREC_MAX.  This is used to count the bits in
 * the fraction of a mpfr_random_deviate_t.  See the checks made on this type
 * in random_deviate_generate. */
typedef unsigned long mpfr_random_size_t;

typedef struct {
  mpfr_random_size_t e;       /* total number of bits in the fraction */
  unsigned long h;            /* the high W bits of the fraction */
  mpz_t f;                    /* the rest of the fraction */
} __mpfr_random_deviate_struct;

typedef __mpfr_random_deviate_struct mpfr_random_deviate_t[1];
typedef __mpfr_random_deviate_struct *mpfr_random_deviate_ptr;

#if defined(__cplusplus)
extern "C" {
#endif

/* allocate and set to (0,1) */
__MPFR_DECLSPEC void
  mpfr_random_deviate_init _MPFR_PROTO((mpfr_random_deviate_ptr));

/* reset to (0,1) */
__MPFR_DECLSPEC void
  mpfr_random_deviate_reset _MPFR_PROTO((mpfr_random_deviate_ptr));

/* deallocate */
__MPFR_DECLSPEC void
  mpfr_random_deviate_clear _MPFR_PROTO((mpfr_random_deviate_ptr));

/* swap two random deviates */
__MPFR_DECLSPEC void
  mpfr_random_deviate_swap _MPFR_PROTO((mpfr_random_deviate_ptr,
                                        mpfr_random_deviate_ptr));

/* return kth bit of fraction, representing 2^-k */
__MPFR_DECLSPEC int
  mpfr_random_deviate_tstbit _MPFR_PROTO((mpfr_random_deviate_ptr,
                                          mpfr_random_size_t,
                                          gmp_randstate_t));

/* compare two random deviates, x < y */
__MPFR_DECLSPEC int
  mpfr_random_deviate_less _MPFR_PROTO((mpfr_random_deviate_ptr,
                                        mpfr_random_deviate_ptr,
                                        gmp_randstate_t));

/* set mpfr_t z = (neg ? -1 : 1) * (n + x) */
__MPFR_DECLSPEC int
  mpfr_random_deviate_value _MPFR_PROTO((int, unsigned long,
                                         mpfr_random_deviate_ptr, mpfr_t,
                                         gmp_randstate_t, mpfr_rnd_t));

#if defined(__cplusplus)
}
#endif

#endif