/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/Random.c

  • Committer: Gustav Hartvigsson
  • Date: 2017-12-20 00:24:28 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20171220002428-q2qakpj020fvtrvu
* Made SRandom compile

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2013-2017 Gustav Hartvigsson
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
of this software and associated documentation files (the "Software"), to deal
 
6
in the Software without restriction, including without limitation the rights
 
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
8
copies of the Software, and to permit persons to whom the Software is
 
9
furnished to do so, subject to the following conditions:
 
10
 
 
11
The above copyright notice and this permission notice shall be included in
 
12
all copies or substantial portions of the Software.
 
13
 
 
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
20
THE SOFTWARE.
 
21
 */
 
22
 
 
23
/*
 
24
Adaptation of
 
25
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c
 
26
with the following license:
 
27
 
 
28
Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
 
29
All rights reserved.                          
 
30
 
 
31
Redistribution and use in source and binary forms, with or without
 
32
modification, are permitted provided that the following conditions
 
33
are met:
 
34
 
 
35
 1. Redistributions of source code must retain the above copyright
 
36
    notice, this list of conditions and the following disclaimer.
 
37
 
 
38
 2. Redistributions in binary form must reproduce the above copyright
 
39
    notice, this list of conditions and the following disclaimer in the
 
40
    documentation and/or other materials provided with the distribution.
 
41
 
 
42
 3. The names of its contributors may not be used to endorse or promote 
 
43
    products derived from this software without specific prior written 
 
44
    permission.
 
45
 
 
46
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
47
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
48
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
49
A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
50
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
51
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
52
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
53
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
54
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
55
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
56
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
57
*/
 
58
 
 
59
#include "Random.h"
 
60
#include "primes.h"
 
61
#include <time.h>
 
62
 
 
63
 
 
64
#define NN 312
 
65
#define MM 156
 
66
#define MATRIX_A 0xB5026F5AA96619E9ULL
 
67
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
 
68
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
 
69
 
 
70
struct SRandom {
 
71
  sulong seed;
 
72
  sulong current;
 
73
  size_t index;
 
74
  
 
75
  size_t itt;
 
76
  
 
77
  sulong * list;
 
78
};
 
79
 
 
80
void
 
81
_s_internal_random_init (SRandom * self);
 
82
 
 
83
SRandom *
 
84
s_random_new () {
 
85
  return s_random_new_with_seed ((slong) time (NULL));
 
86
}
 
87
 
 
88
SRandom *
 
89
s_random_new_with_seed (sulong seed) {
 
90
  SRandom * self = s_malloc (sizeof (SRandom));
 
91
  
 
92
  self->seed = seed;
 
93
  
 
94
  self->list = s_calloc(NN, sizeof (sulong));
 
95
  
 
96
  self->itt = NN+1;
 
97
  
 
98
  return self;
 
99
}
 
100
 
 
101
void
 
102
s_random_reseed (SRandom * self, sulong seed) {
 
103
  /* We just have to re-set these things. */
 
104
  self->seed = seed;
 
105
  self->itt = NN+1;
 
106
}
 
107
 
 
108
sulong
 
109
s_random_next (SRandom * self) {
 
110
  
 
111
  size_t i;
 
112
  slong x;
 
113
  static slong mag01[2]={0ULL, MATRIX_A};
 
114
  
 
115
  if (self->itt >= NN) {
 
116
    if (self->itt == NN+1) {
 
117
      _s_internal_random_init (self);
 
118
    }
 
119
    
 
120
    for (i = 0; i < NN - MM; i++) {
 
121
      x = (self->list[i] & UM) | (self->list[i+1] & LM);
 
122
      self->list[i] = self->list[i + MM] ^
 
123
                                (x >> 1) ^
 
124
                mag01[(sint)(x & 1ULL)];
 
125
    }
 
126
    
 
127
    for (/* i */; i < NN - 1; i++) {
 
128
      x = (self->list[i] & UM) | (self->list[i + 1] & LM);
 
129
      self->list[i] = self->list[i + (MM - NN)] ^
 
130
                                       (x >> 1) ^
 
131
                        mag01[(sint)(x & 1ULL)];
 
132
    }
 
133
    
 
134
    x = (self->list[NN - 1] & UM) | (self->list[0] & LM);
 
135
    
 
136
    self->list[NN - 1] = self->list[MM - 1] ^
 
137
                                   (x >> 1) ^
 
138
                   mag01[(sint) (x & 1ULL)];
 
139
    
 
140
    self->itt = 0;
 
141
  }
 
142
  
 
143
  self->current = self->list[self->itt];
 
144
  
 
145
  x = self->list[self->itt++];
 
146
  
 
147
  x ^= (x >> 29) & 0x5555555555555555ULL;
 
148
  x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
 
149
  x ^= (x << 37) & 0xFFF7EEE000000000ULL;
 
150
  x ^= (x >> 43);
 
151
 
 
152
  return x;
 
153
}
 
154
 
 
155
sulong
 
156
s_random_current (SRandom * self) {
 
157
  return self->current;
 
158
}
 
159
 
 
160
slong
 
161
s_random_next_long (SRandom * self) {
 
162
  return (s_random_next (self) >> 1); 
 
163
}
 
164
 
 
165
sdouble
 
166
s_random_next_real (SRandom * self) {
 
167
  return (s_random_next (self) >> 11) * (1.0/9007199254740991.0);
 
168
}
 
169
 
 
170
slong
 
171
s_random_current_long (SRandom * self) {
 
172
  sulong current = self->current;
 
173
  return (current >> 1); 
 
174
}
 
175
 
 
176
sdouble
 
177
s_random_current_real (SRandom * self) {
 
178
  sulong current = self->current;
 
179
  return (current >> 11) * (1.0/9007199254740991.0);
 
180
}
 
181
 
 
182
void
 
183
_s_internal_random_init (SRandom * self) {
 
184
  self->list[0] = self->seed;
 
185
  
 
186
  for (self->itt = 0; self->itt < NN; self->itt++) {
 
187
    self->list[self->itt] =
 
188
       (6364136223846793005ULL * (self->list[self->itt - 1] ^
 
189
          (self->list[self->itt - 1] >> 62)) + self->itt);
 
190
  }
 
191
}
 
192
 
 
193
#if 0
 
194
slong
 
195
s_random_get_entropy () {
 
196
  #ifdef _WIN32
 
197
  
 
198
  #elif __unix__
 
199
  
 
200
  #else
 
201
    #error "We can't get at an entropy pool?"
 
202
  #endif
 
203
}
 
204
#endif
 
205
 
 
206
 
 
207