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 | | |
| | | | | | | #include "global.h" | #include "stuff.h" | #include "bitvector.h" | | | PMOD_EXPORT const char Pike_is8bitalnum_vector[] = | "0000000000000000" | "0000000000000000" | "0000000000000000" | "1111111111000000" | "0111111111111111" | "1111111111100001" | "0111111111111111" | "1111111111100000" | "0000000000000000" | "0000000000000000" | "1011110101100010" | "1011011001101110" | "1111111111111111" | "1111111011111111" | "1111111111111111" | "1111111011111111"; | | | const INT32 hashprimes[32] = | { | 31, | 31, | 31, | 31, | 31, | 31, | 61, | 127, | 251, | 541, | 1151, | 2111, | 4327, | 8803, | 17903, | 32321, | 65599, | 133153, | 270001, | 547453, | 1109891, | 2000143, | 4561877, | 9248339, | 16777215, | 33554431, | 67108863, | 134217727, | 268435455, | 536870911, | 1073741823, | 2147483647, | }; | | | | | | | | | | | PMOD_EXPORT int my_log2(size_t x) | { | if( x == 0 ) return 0; | return log2_u32(x); | } | | | PMOD_EXPORT double my_strtod(char *nptr, char **endptr) | { | double tmp=strtod(nptr,endptr); | if(*endptr>nptr) | { | if(endptr[0][-1]=='.') | endptr[0]--; | } | return tmp; | } | | | | | | unsigned INT32 find_next_power(unsigned INT32 x) | { | if( x == 0 ) return 1; | return 1<<(my_log2(x-1)+1); | } | | static unsigned INT32 RandSeed1 = 0x5c2582a4; | static unsigned INT32 RandSeed2 = 0x64dff8ca; | | static unsigned INT32 slow_rand(void) | { | RandSeed1 = ((RandSeed1 * 13 + 1) ^ (RandSeed1 >> 9)) + RandSeed2; | RandSeed2 = (RandSeed2 * RandSeed1 + 13) ^ (RandSeed2 >> 13); | return RandSeed1; | } | | static void slow_srand(INT32 seed) | { | RandSeed1 = (seed - 1) ^ 0xA5B96384UL; | RandSeed2 = (seed + 1) ^ 0x56F04021UL; | } | | #define RNDBUF 250 | #define RNDSTEP 7 | #define RNDJUMP 103 | | static unsigned INT32 rndbuf[ RNDBUF ]; | static unsigned int rnd_index; | | #if HAS___BUILTIN_IA32_RDRAND32_STEP | static int use_rdrnd; | static unsigned long long rnd_index64; | #endif | #define bit_RDRND_2 (1<<30) | | #if SIZEOF_CHAR_P == 4 | #define __cpuid(level, a, b, c, d) \ | __asm__ ("pushl %%ebx \n\t" \ | "cpuid \n\t" \ | "movl %%ebx, %1 \n\t" \ | "popl %%ebx \n\t" \ | : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \ | : "a" (level) \ | : "cc") | #else | #define __cpuid(level, a, b, c, d) \ | __asm__ ("push %%rbx \n\t" \ | "cpuid \n\t" \ | "movl %%ebx, %1 \n\t" \ | "pop %%rbx \n\t" \ | : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \ | : "a" (level) \ | : "cc") | #endif | | PMOD_EXPORT void my_srand(INT32 seed) | { | #if HAS___BUILTIN_IA32_RDRAND32_STEP | unsigned int ignore, cpuid_ecx; | if( !use_rdrnd ) | { | __cpuid( 0x1, ignore, ignore, cpuid_ecx, ignore ); | if( cpuid_ecx & bit_RDRND_2 ) | use_rdrnd = 1; | } | | | | | | | | | | | | | #endif | { | int e; | unsigned INT32 mask; | | slow_srand(seed); | | rnd_index = 0; | for (e=0;e < RNDBUF; e++) rndbuf[e]=slow_rand(); | | mask = (unsigned INT32) -1; | | for (e=0;e< (int)sizeof(INT32)*8 ;e++) | { | int d = RNDSTEP * e + 3; | rndbuf[d % RNDBUF] &= mask; | mask>>=1; | rndbuf[d % RNDBUF] |= (mask+1); | } | } | } | | PMOD_EXPORT unsigned INT32 my_rand(void) | { | #if HAS___BUILTIN_IA32_RDRAND32_STEP | if( use_rdrnd ) | { | unsigned int cnt = 0; | do{ | if( __builtin_ia32_rdrand32_step( &rnd_index ) ) | return rnd_index; | } while(cnt++ < 100); | | | | rnd_index = 0; | use_rdrnd = 0; | } | #endif | if(++rnd_index == RNDBUF) rnd_index=0; | return rndbuf[rnd_index] += rndbuf[rnd_index+RNDJUMP-(rnd_index<RNDBUF-RNDJUMP?0:RNDBUF)]; | } | | PMOD_EXPORT unsigned INT64 my_rand64(void) | { | #if HAS___BUILTIN_IA32_RDRAND32_STEP | if( use_rdrnd ) | { | unsigned int cnt = 0; | do{ | | | if( __builtin_ia32_rdrand64_step( &rnd_index64 ) ) | return rnd_index64; | } while(cnt++ < 100); | | | | rnd_index = 0; | use_rdrnd = 0; | } | #endif | return ((INT64)my_rand()<<32) | my_rand(); | } | | |
|