File: num_rand.c

package info (click to toggle)
gcl27 2.7.1-13
  • links: PTS
  • area: main
  • in suites: sid
  • size: 30,888 kB
  • sloc: lisp: 211,946; ansic: 52,944; sh: 9,347; makefile: 647; tcl: 53; awk: 52
file content (234 lines) | stat: -rw-r--r-- 5,354 bytes parent folder | download | duplicates (3)
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
/*
 Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
 Copyright (C) 2024 Camm Maguire

This file is part of GNU Common Lisp, herein referred to as GCL

GCL is free software; you can redistribute it and/or modify it under
the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCL 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 Library General Public 
License for more details.

You should have received a copy of the GNU Library General Public License 
along with GCL; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/*
	Random numbers
*/

#include <time.h>
#include <string.h>

#include "include.h"
#include "num_include.h"

#ifdef AOSVS

#endif

static object
rando(object x, object rs) {

  enum type tx;
  object base,out,z;
  fixnum fbase;
  double d;
  
  tx = type_of(x);
  if (number_compare(x, small_fixnum(0)) != 1)
    FEwrong_type_argument(TSpositive_number, x);
  
  if (tx==t_bignum) {
    out=new_bignum();
    base=x;
    fbase=-1;
  } else {
    out=big_fixnum1;
    fbase=tx==t_fixnum ? fix(x) : MOST_POSITIVE_FIX;
    mpz_set_si(MP(big_fixnum2),fbase);
    base=big_fixnum2;
  }
  
  mpz_urandomm(MP(out),&rs->rnd.rnd_state,MP(base));
  
  switch (tx) {
    
  case t_fixnum:
    return make_fixnum(mpz_get_si(MP(out)));
  case t_bignum:
    return normalize_big(out);
  case t_shortfloat: case t_longfloat:
    d=mpz_get_d(MP(out));
    d/=(double)fbase;
    z=alloc_object(tx);
    BLOCK_EXCEPTIONS(if (tx==t_shortfloat) sf(z)=sf(x)*d; else lf(z)=lf(x)*d);
    return z;
  default:
    FEerror("~S is not an integer nor a floating-point number.", 1, x);
    return(Cnil);
  }
}


#ifdef UNIX
#define RS_DEF_INIT time(0)
#else
#define RS_DEF_INIT 0
#endif

#if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
extern void * (*gcl_gmp_allocfun) (size_t);
static void * (*old_gcl_gmp_allocfun) (size_t);
static void * trap_result;
static size_t trap_size;

static void *
trap_gcl_gmp_allocfun(size_t size){

  size+=size%MP_LIMB_SIZE;
  if (trap_size)
    return old_gcl_gmp_allocfun(size);
  else {
    trap_size=size/MP_LIMB_SIZE;
    trap_result=old_gcl_gmp_allocfun(size);
    return trap_result;
  }

}
#endif

void
reinit_gmp() {

#if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
  Mersenne_Twister_Generator_Noseed.b=__gmp_randget_mt;
  Mersenne_Twister_Generator_Noseed.c=__gmp_randclear_mt;
  Mersenne_Twister_Generator_Noseed.d=__gmp_randiset_mt;
#endif

}

void
init_gmp_rnd_state(__gmp_randstate_struct *x) {

  static int n;

  bzero(x,sizeof(*x));
  
#if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
/*   if (!trap_size) { */
  old_gcl_gmp_allocfun=gcl_gmp_allocfun;
  gcl_gmp_allocfun=trap_gcl_gmp_allocfun;
/*   } */
#endif
  gmp_randinit_default(x);
#if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
  if (!n) {

    if (x->_mp_seed->_mp_d!=trap_result)
      FEerror("Unknown pointer in rnd_state!",0);
/* #ifndef __hppa__ /\*FIXME*\/ */
/*     if (((gmp_randfnptr_t *)x->_mp_algdata._mp_lc)->b!=Mersenne_Twister_Generator_Noseed.b || */
/* 	((gmp_randfnptr_t *)x->_mp_algdata._mp_lc)->c!=Mersenne_Twister_Generator_Noseed.c || */
/* 	((gmp_randfnptr_t *)x->_mp_algdata._mp_lc)->d!=Mersenne_Twister_Generator_Noseed.d) */
/*       FEerror("Unknown pointer data in rnd_state!",0); */
/* #endif */

    n=1;

  }
  gcl_gmp_allocfun=old_gcl_gmp_allocfun;
  x->_mp_seed->_mp_alloc=x->_mp_seed->_mp_size=trap_size;
#endif
    

}



static object
make_random_state(object rs) {

  object z;
  
  if (rs==Cnil)
    rs=symbol_value(Vrandom_state);
  
  if (rs!=Ct && type_of(rs) != t_random) {
    FEwrong_type_argument(sLrandom_state, rs);
    return(Cnil);
  }
  
  z = alloc_object(t_random);
  init_gmp_rnd_state(&z->rnd.rnd_state);

    
  if (rs == Ct) 
    gmp_randseed_ui(&z->rnd.rnd_state,RS_DEF_INIT);
  else
    memcpy(z->rnd.rnd_state._mp_seed->_mp_d,rs->rnd.rnd_state._mp_seed->_mp_d,
	   rs->rnd.rnd_state._mp_seed->_mp_alloc*sizeof(*z->rnd.rnd_state._mp_seed->_mp_d));
  
#if __GNU_MP_VERSION > 4 || (__GNU_MP_VERSION == 4 && __GNU_MP_VERSION_MINOR >= 2)
  z->rnd.rnd_state._mp_algdata._mp_lc=&Mersenne_Twister_Generator_Noseed;
#endif
  return(z);

}

LFD(Lrandom)(void)
{
	int j;
        object x;
	
	j = vs_top - vs_base;
	if (j == 1)
		vs_push(symbol_value(Vrandom_state));
	check_arg(2);
	check_type_random_state(&vs_base[1]);
	x = rando(vs_base[0], vs_base[1]);
	vs_top = vs_base;
	vs_push(x);
}

LFD(Lmake_random_state)(void)
{
	int j;
	object x;

	j = vs_top - vs_base;
	if (j == 0)
		vs_push(Cnil);
	check_arg(1);
	x = make_random_state(vs_head);
	vs_top = vs_base;
	vs_push(x);
}

LFD(Lrandom_state_p)(void)
{
	check_arg(1);
	if (type_of(vs_pop) == t_random)
		vs_push(Ct);
	else
		vs_push(Cnil);
}

void
gcl_init_num_rand(void)
{
        Vrandom_state = make_special("*RANDOM-STATE*",
				     make_random_state(Ct));

	make_function("RANDOM", Lrandom);
	make_function("MAKE-RANDOM-STATE", Lmake_random_state);
	make_function("RANDOM-STATE-P", Lrandom_state_p);
}