xref: /netbsd-src/external/lgpl3/mpfr/dist/src/random_deviate.h (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Declarations of random_deviate routines for mpfr_erandom and mpfr_nrandom.
2 
3 Copyright 2013-2023 Free Software Foundation, Inc.
4 Contributed by Charles Karney <charles@karney.com>, SRI International.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #if !defined(MPFR_RANDOM_DEVIATE_H)
24 #define MPFR_RANDOM_DEVIATE_H 1
25 
26 #include "mpfr-impl.h"
27 
28 /* This should be an unsigned type with a width of at least 32 and capable of
29  * representing at least 2*MPFR_PREC_MAX.  This is used to count the bits in
30  * the fraction of a mpfr_random_deviate_t.  See the checks made on this type
31  * in random_deviate_generate. */
32 typedef unsigned long mpfr_random_size_t;
33 
34 typedef struct {
35   mpfr_random_size_t e;       /* total number of bits in the fraction */
36   unsigned long h;            /* the high W bits of the fraction */
37   mpz_t f;                    /* the rest of the fraction */
38 } __mpfr_random_deviate_struct;
39 
40 typedef __mpfr_random_deviate_struct mpfr_random_deviate_t[1];
41 typedef __mpfr_random_deviate_struct *mpfr_random_deviate_ptr;
42 
43 #if defined(__cplusplus)
44 extern "C" {
45 #endif
46 
47 /* allocate and set to (0,1) */
48 __MPFR_DECLSPEC void
49   mpfr_random_deviate_init (mpfr_random_deviate_ptr);
50 
51 /* reset to (0,1) */
52 __MPFR_DECLSPEC void
53   mpfr_random_deviate_reset (mpfr_random_deviate_ptr);
54 
55 /* deallocate */
56 __MPFR_DECLSPEC void
57   mpfr_random_deviate_clear (mpfr_random_deviate_ptr);
58 
59 /* swap two random deviates */
60 __MPFR_DECLSPEC void
61   mpfr_random_deviate_swap (mpfr_random_deviate_ptr,
62                             mpfr_random_deviate_ptr);
63 
64 /* return kth bit of fraction, representing 2^-k */
65 __MPFR_DECLSPEC int
66   mpfr_random_deviate_tstbit (mpfr_random_deviate_ptr,
67                               mpfr_random_size_t,
68                               gmp_randstate_t);
69 
70 /* compare two random deviates, x < y */
71 __MPFR_DECLSPEC int
72   mpfr_random_deviate_less (mpfr_random_deviate_ptr,
73                             mpfr_random_deviate_ptr,
74                             gmp_randstate_t);
75 
76 /* set mpfr_t z = (neg ? -1 : 1) * (n + x) */
77 __MPFR_DECLSPEC int
78   mpfr_random_deviate_value (int, unsigned long,
79                              mpfr_random_deviate_ptr, mpfr_ptr,
80                              gmp_randstate_t, mpfr_rnd_t);
81 
82 #if defined(__cplusplus)
83 }
84 #endif
85 
86 #endif
87