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 #ifndef lint
00044 static const char RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbn/msr.c,v 14.13 2006/09/03 15:15:16 lbutler Exp $ (ARL)";
00045 #endif
00046
00047 #include "common.h"
00048
00049
00050
00051 #include <stdio.h>
00052 #include <math.h>
00053 #include "machine.h"
00054 #include "bu.h"
00055 #include "vmath.h"
00056 #include "bn.h"
00057
00058
00059
00060
00061 #define BN_MSR_MAXTBL 4096
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #define A 16807
00090 #define M 2147483647
00091 #define DM 2147483647.0
00092 #define Q 127773
00093 #define R 2836
00094 struct bn_unif *
00095 bn_unif_init(long int setseed, int method)
00096 {
00097 struct bn_unif *p;
00098 p = (struct bn_unif *) bu_malloc(sizeof(struct bn_unif),"bn_unif");
00099 p->msr_longs = (long *) bu_malloc(BN_MSR_MAXTBL*sizeof(long), "msr long table");
00100 p->msr_doubles=(double *) bu_malloc(BN_MSR_MAXTBL*sizeof(double),"msr double table");
00101 p->msr_seed = 1;
00102 p->msr_long_ptr = 0;
00103 p->msr_double_ptr = 0;
00104
00105 if (setseed&0x7fffffff) p->msr_seed=setseed&0x7fffffff;
00106 p->magic = BN_UNIF_MAGIC;
00107 return(p);
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 long
00139 bn_unif_long_fill(struct bn_unif *p)
00140 {
00141 register long test,work_seed;
00142 register int i;
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 if (!p || (p->magic != BN_UNIF_MAGIC &&
00153 p->magic != BN_GAUSS_MAGIC)) {
00154 BN_CK_UNIF(p);
00155 }
00156
00157 work_seed = p->msr_seed;
00158
00159 if ( p->msr_longs) {
00160 for (i=0; i < BN_MSR_MAXTBL; i++) {
00161 test = A*(work_seed % Q) - R*(work_seed / Q);
00162 p->msr_longs[i] = work_seed = (test < 0) ?
00163 test+M : test;
00164 }
00165 p->msr_long_ptr = BN_MSR_MAXTBL;
00166 }
00167 test = A*(work_seed % Q) - R*(work_seed / Q);
00168 p->msr_seed = (test < 0) ? test+M : test;
00169 return(p->msr_seed);
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 double
00201 bn_unif_double_fill(struct bn_unif *p)
00202 {
00203 register long test,work_seed;
00204 register int i;
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 if (!p || (p->magic != BN_UNIF_MAGIC &&
00215 p->magic != BN_GAUSS_MAGIC)) {
00216 BN_CK_UNIF(p);
00217 }
00218
00219 work_seed = p->msr_seed;
00220
00221 if (p->msr_doubles) {
00222 for (i=0; i < BN_MSR_MAXTBL; i++) {
00223 test = A*(work_seed % Q) - R*(work_seed / Q);
00224 work_seed = (test < 0) ? test+M : test;
00225 p->msr_doubles[i] = ( work_seed - M/2) * 1.0/DM;
00226 }
00227 p->msr_double_ptr = BN_MSR_MAXTBL;
00228 }
00229 test = A*(work_seed % Q) - R*(work_seed / Q);
00230 p->msr_seed = (test < 0) ? test+M : test;
00231
00232 return((p->msr_seed - M/2) * 1.0/DM);
00233 }
00234
00235
00236
00237
00238 void
00239 bn_unif_free(struct bn_unif *p)
00240 {
00241 bu_free(p->msr_doubles, "msr double table");
00242 bu_free(p->msr_longs, "msr long table");
00243 p->magic = 0;
00244 bu_free(p, "bn_unif");
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 struct bn_gauss *
00273 bn_gauss_init(long int setseed, int method)
00274 {
00275 struct bn_gauss *p;
00276 p = (struct bn_gauss *) bu_malloc(sizeof(struct bn_gauss),"bn_msr_guass");
00277 p->msr_gausses=(double *) bu_malloc(BN_MSR_MAXTBL*sizeof(double),"msr guass table");
00278 p->msr_gauss_doubles=(double *) bu_malloc(BN_MSR_MAXTBL*sizeof(double),"msr guass doubles");
00279 p->msr_gauss_seed = 1;
00280 p->msr_gauss_ptr = 0;
00281 p->msr_gauss_dbl_ptr = 0;
00282
00283 if (setseed&0x7fffffff) p->msr_gauss_seed=setseed&0x7fffffff;
00284 p->magic = BN_GAUSS_MAGIC;
00285 return(p);
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 double
00320 bn_gauss_fill(struct bn_gauss *p)
00321 {
00322 register int i;
00323 double v1,v2,r,fac;
00324
00325 BN_CK_GAUSS(p);
00326
00327 if (p->msr_gausses) {
00328 for (i=0; i< BN_MSR_MAXTBL-1; ) {
00329 BN_UNIF_CIRCLE((struct bn_unif *)p,v1,v2,r);
00330 if (r<0.00001) continue;
00331 fac = sqrt(-2.0*log(r)/r);
00332 p->msr_gausses[i++] = v1*fac;
00333 p->msr_gausses[i++] = v2*fac;
00334 }
00335 p->msr_gauss_ptr = BN_MSR_MAXTBL;
00336 }
00337
00338 do {
00339 BN_UNIF_CIRCLE((struct bn_unif *)p,v1,v2,r);
00340 } while (r < 0.00001);
00341 fac = sqrt(-2.0*log(r)/r);
00342 return(v1*fac);
00343 }
00344
00345
00346
00347 void
00348 bn_gauss_free(struct bn_gauss *p)
00349 {
00350 bu_free(p->msr_gauss_doubles, "msr guass doubles");
00351 bu_free(p->msr_gausses,"msr guass table");
00352 bu_free(p,"bn_msr_guass");
00353 }
00354
00355
00356 #undef A
00357 #undef M
00358 #undef DM
00359 #undef Q
00360 #undef R
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371