00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <math.h>
00062 #include "others_generators.h"
00063 #include "machine.h"
00064 #include "sciprint.h"
00065
00066 #define N 37
00067 #define N2 24
00068
00069 static int is_init=0;
00070 static long swb_state[N];
00071 static int swb_index=N;
00072 static int swb_flag;
00073 static unsigned long cong_state;
00074
00075
00076
00077
00078
00079
00080
00081 static double DEFAULT_SEED1= 1234567.0, DEFAULT_SEED2=7654321.0;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 #define SWB(c,x,y,z) c = (y<0) ? (((z=x-y-c) < 0) || (x>=0)) : (((z=x-y-c) < 0) && (x>=0));
00104
00105 extern void sciprint __PARAMS((char *fmt,...));
00106
00107 void advance_state_swb()
00108 {
00109 int i;
00110
00111
00112
00113
00114
00115 for (i=0; i<N2; i++)
00116 SWB(swb_flag,swb_state[i+N-N2],swb_state[i],swb_state[i]);
00117 for (i=N2; i<N; i++)
00118 SWB(swb_flag,swb_state[i -N2],swb_state[i],swb_state[i]);
00119 swb_index = 0;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 int set_state_fsultra_simple(double s1, double s2)
00139 {
00140 unsigned long shrgx, tidbits=0;
00141 int i, j;
00142
00143 if ( (s1 == floor(s1) && 0.0 <= s1 && s1 <= 4294967295.0)
00144 && (s2 == floor(s2) && 0.0 <= s2 && s2 <= 4294967295.0) )
00145 {
00146 cong_state = ((unsigned long) s1)*2 + 1;
00147 shrgx = (unsigned long) s2;
00148 for ( i=0 ; i<N ; i++)
00149 {
00150 for ( j=32 ; j>0 ; j--)
00151 {
00152 cong_state = cong_state * 69069;
00153 shrgx = shrgx ^ (shrgx >> 15);
00154 shrgx = shrgx ^ (shrgx << 17);
00155 tidbits = (tidbits>>1) | (0x80000000 & (cong_state^shrgx));
00156 }
00157 swb_state[i] = tidbits;
00158 }
00159 swb_index = 0;
00160 swb_flag = 0;
00161 advance_state_swb();
00162 is_init = 1;
00163 return 1;
00164 }
00165 else
00166 {
00167 sciprint("\n\r bad seed for fsultra, must be integers in [0, 2^32-1] \n\r");
00168 return 0;
00169 }
00170 }
00171
00172 int set_state_fsultra(double *s)
00173 {
00174 double try;
00175 int i;
00176
00177 try = s[0];
00178 if ( floor(try) != try || try < 0.0 || try > (double) N)
00179 {
00180 sciprint("\n\r the first component of the fsultra state, must be an integer in [0, %d] \n\r",N);
00181 return 0;
00182 }
00183 swb_index = (int) try;
00184
00185 try = s[1];
00186 if ( try != 0.0 && try != 1.0)
00187 {
00188 sciprint("\n\r the second component of the fsultra state, must be 0 or 1 \n\r");
00189 return 0;
00190 }
00191 swb_flag = (int) try;
00192
00193 try = s[2];
00194 if ( floor(try) != try || try <= 0 || try > 4294967295.0 )
00195 {
00196 sciprint("\n\r the third component of the fsultra state, must be an integer in [1, 2^32-1] \n\r");
00197 return 0;
00198 }
00199 cong_state = (unsigned long) try;
00200
00201
00202 for (i = 0 ; i < N ; i++)
00203 swb_state[i] = (long) (((unsigned long) s[i+3]) & 0xffffffff);
00204
00205 is_init = 1;
00206 return 1;
00207 }
00208
00209
00210
00211 void get_state_fsultra(double s[])
00212 {
00213 int i;
00214
00215 if ( ! is_init )
00216 set_state_fsultra_simple(DEFAULT_SEED1, DEFAULT_SEED2);
00217
00218 s[0] = (double) swb_index;
00219 s[1] = (double) swb_flag;
00220 s[2] = (double) cong_state;
00221 for (i = 0 ; i < N ; i++)
00222 s[i+3] = (double) (unsigned long) swb_state[i];
00223 }
00224
00225 unsigned long fsultra()
00226 {
00227 if (swb_index >= N)
00228 {
00229 if ( ! is_init )
00230 set_state_fsultra_simple(DEFAULT_SEED1, DEFAULT_SEED2);
00231 else
00232 advance_state_swb();
00233 }
00234 return (swb_state[swb_index++] ^ (cong_state = cong_state * 69069));
00235 }