File: Random.xs

package info (click to toggle)
libmath-mpfr-perl 4.45-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,716 kB
  • sloc: perl: 1,508; ansic: 517; makefile: 9
file content (253 lines) | stat: -rwxr-xr-x 5,125 bytes parent folder | download | duplicates (2)
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

#ifdef  __MINGW32__
#ifndef __USE_MINGW_ANSI_STDIO
#define __USE_MINGW_ANSI_STDIO 1
#endif
#endif

#define PERL_NO_GET_CONTEXT 1

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"


#include "../math_mpfr_include.h"

/*
#ifdef _MSC_VER
#pragma warning(disable:4700 4715 4716)
#endif
*/

#ifdef OLDPERL
#define SvUOK SvIsUV
#endif

#ifndef Newx
#  define Newx(v,n,t) New(0,v,n,t)
#endif

#ifndef Newxz
#  define Newxz(v,n,t) Newz(0,v,n,t)
#endif

/* May one day be removed from mpfr.h */
#ifndef mp_rnd_t
# define mp_rnd_t  mpfr_rnd_t
#endif
#ifndef mp_prec_t
# define mp_prec_t mpfr_prec_t
#endif

#ifndef __gmpfr_default_rounding_mode
#define __gmpfr_default_rounding_mode mpfr_get_default_rounding_mode()
#endif

SV * Rmpfr_randinit_default(pTHX) {
  gmp_randstate_t * state;
  SV * obj_ref, * obj;

  Newx(state, 1, gmp_randstate_t);
  if(state == NULL) croak("Failed to allocate memory in Rmpfr_randinit_default function");
  obj_ref = newSV(0);
  obj = newSVrv(obj_ref, "Math::MPFR::Random");
  gmp_randinit_default(*state);

  sv_setiv(obj, INT2PTR(IV,state));
  SvREADONLY_on(obj);
  return obj_ref;
}

SV * Rmpfr_randinit_mt(pTHX) {
  gmp_randstate_t * rand_obj;
  SV * obj_ref, * obj;

  Newx(rand_obj, 1, gmp_randstate_t);
  if(rand_obj == NULL) croak("Failed to allocate memory in Math::MPFR::Random::Rmpfr_randinit_mt function");
  obj_ref = newSV(0);
  obj = newSVrv(obj_ref, "Math::MPFR::Random");
  gmp_randinit_mt(*rand_obj);

  sv_setiv(obj, INT2PTR(IV, rand_obj));
  SvREADONLY_on(obj);
  return obj_ref;
}

SV * Rmpfr_randinit_lc_2exp(pTHX_ SV * a, SV * c, SV * m2exp ) {
  gmp_randstate_t * state;
  mpz_t aa;
  SV * obj_ref, * obj;

  Newx(state, 1, gmp_randstate_t);
  if(state == NULL) croak("Failed to allocate memory in Rmpfr_randinit_lc_2exp function");
  obj_ref = newSV(0);
  obj = newSVrv(obj_ref, "Math::MPFR::Random");
  if(sv_isobject(a)) {
    const char* h = HvNAME(SvSTASH(SvRV(a)));

    if(strEQ(h, "Math::GMP") || strEQ(h, "GMP::Mpz") || strEQ(h, "Math::GMPz"))
      gmp_randinit_lc_2exp(*state, *(INT2PTR(mpz_t *, SvIVX(SvRV(a)))), (unsigned long)SvUV(c), (unsigned long)SvUV(m2exp));
    else croak("First arg to Rmpfr_randinit_lc_2exp is of invalid type");
  }
  else {
    if(!mpz_init_set_str(aa, SvPV_nolen(a), 0)) {
      gmp_randinit_lc_2exp(*state, aa, (unsigned long)SvUV(c), (unsigned long)SvUV(m2exp));
      mpz_clear(aa);
    }
    else croak("Seedstring supplied to Rmpfr_randinit_lc_2exp is not a valid number");
  }

  sv_setiv(obj, INT2PTR(IV,state));
  SvREADONLY_on(obj);
  return obj_ref;
}

SV * Rmpfr_randinit_lc_2exp_size(pTHX_ SV * size) {
  gmp_randstate_t * state;
  SV * obj_ref, * obj;

  if(SvUV(size) > 128) croak("The argument supplied to Rmpfr_randinit_lc_2exp_size function is too large - ie greater than 128");

  Newx(state, 1, gmp_randstate_t);
  if(state == NULL) croak("Failed to allocate memory in Rmpfr_randinit_lc_2exp_size function");
  obj_ref = newSV(0);
  obj = newSVrv(obj_ref, "Math::MPFR::Random");

  if(gmp_randinit_lc_2exp_size(*state, (unsigned long)SvUV(size))) {
    sv_setiv(obj, INT2PTR(IV,state));
    SvREADONLY_on(obj);
    return obj_ref;
  }

  croak("Rmpfr_randinit_lc_2exp_size function failed");
}

/* Provide a duplicate of Math::MPFR::_MPFR_VERSION. *
 * This allows MPFR.pm to determine the value of     *
 * MPFR_VERSION at compile time.                     */

SV * _MPFR_VERSION(pTHX) {
#if defined(MPFR_VERSION)
  return newSVuv(MPFR_VERSION);
#else
  return &PL_sv_undef;
#endif
}

/* Provide a duplicate of Math::MPFR::_has_pv_nv_bug. *
 * This allows MPFR.pm to determine the value of      *
 * the constant MPFR_PV_NV_BUG at compile time.       */

int _has_pv_nv_bug(void) {
#if defined(MPFR_PV_NV_BUG)
  return 1;
#else
  return 0;
#endif
}

void DESTROY(gmp_randstate_t * p) {
  gmp_randclear(*p);
  Safefree(p);
}

int _is_NOK_and_POK(SV * in) {
  if(SvNOK(in) && SvPOK(in)) return 1;
  return 0;
}

int _win32_fmt_bug_ignore(void) {
#if defined(WIN32_FMT_BUG_IGNORE)
  return 1;
#else
  return 0;
#endif
}

SV * _gmp_cflags(pTHX) {
#if defined(__GMP_CFLAGS)
  return newSVpv(__GMP_CFLAGS, 0);
#else
  return &PL_sv_undef;
#endif
}

SV * _gmp_cc(pTHX) {
#if defined(__GMP_CC)
  return newSVpv(__GMP_CC, 0);
#else
  return &PL_sv_undef;
#endif
}



MODULE = Math::MPFR::Random  PACKAGE = Math::MPFR::Random

PROTOTYPES: DISABLE


SV *
Rmpfr_randinit_default ()
CODE:
  RETVAL = Rmpfr_randinit_default (aTHX);
OUTPUT:  RETVAL


SV *
Rmpfr_randinit_mt ()
CODE:
  RETVAL = Rmpfr_randinit_mt (aTHX);
OUTPUT:  RETVAL


SV *
Rmpfr_randinit_lc_2exp (a, c, m2exp)
	SV *	a
	SV *	c
	SV *	m2exp
CODE:
  RETVAL = Rmpfr_randinit_lc_2exp (aTHX_ a, c, m2exp);
OUTPUT:  RETVAL

SV *
Rmpfr_randinit_lc_2exp_size (size)
	SV *	size
CODE:
  RETVAL = Rmpfr_randinit_lc_2exp_size (aTHX_ size);
OUTPUT:  RETVAL

SV *
_MPFR_VERSION ()
CODE:
  RETVAL = _MPFR_VERSION (aTHX);
OUTPUT:  RETVAL


int
_has_pv_nv_bug ()


int
_is_NOK_and_POK (in)
	SV *	in

int
_win32_fmt_bug_ignore ()


SV *
_gmp_cflags ()
CODE:
  RETVAL = _gmp_cflags (aTHX);
OUTPUT:  RETVAL


SV *
_gmp_cc ()
CODE:
  RETVAL = _gmp_cc (aTHX);
OUTPUT:  RETVAL