1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // stdlib.c -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu Jan 28 17:10:29 2016 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Jan 13 21:38:30 2022 |
---|
13 | // Update Count : 593 |
---|
14 | // |
---|
15 | |
---|
16 | #include "stdlib.hfa" |
---|
17 | #include "bits/random.hfa" |
---|
18 | #include "concurrency/invoke.h" // random_state |
---|
19 | |
---|
20 | //--------------------------------------- |
---|
21 | |
---|
22 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 |
---|
23 | #include <string.h> // memcpy, memset |
---|
24 | //#include <math.h> // fabsf, fabs, fabsl |
---|
25 | #include <complex.h> // _Complex_I |
---|
26 | #include <assert.h> |
---|
27 | |
---|
28 | //--------------------------------------- |
---|
29 | |
---|
30 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
31 | |
---|
32 | forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) |
---|
33 | T * anew( size_t dim, TT p ) { |
---|
34 | T * arr = alloc( dim ); |
---|
35 | for ( unsigned int i = 0; i < dim; i += 1 ) { |
---|
36 | (arr[i]){ p }; // run constructor |
---|
37 | } // for |
---|
38 | return arr; |
---|
39 | } // anew |
---|
40 | |
---|
41 | forall( T & | sized(T) | { void ^?{}( T & ); } ) |
---|
42 | void adelete( T arr[] ) { |
---|
43 | if ( arr ) { // ignore null |
---|
44 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
45 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
46 | ^(arr[i]){}; // run destructor |
---|
47 | } // for |
---|
48 | free( arr ); |
---|
49 | } // if |
---|
50 | } // adelete |
---|
51 | |
---|
52 | forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) |
---|
53 | void adelete( T arr[], TT rest ) { |
---|
54 | if ( arr ) { // ignore null |
---|
55 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
56 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
57 | ^(arr[i]){}; // run destructor |
---|
58 | } // for |
---|
59 | free( arr ); |
---|
60 | } // if |
---|
61 | adelete( rest ); |
---|
62 | } // adelete |
---|
63 | |
---|
64 | //--------------------------------------- |
---|
65 | |
---|
66 | float _Complex strto( const char sptr[], char ** eptr ) { |
---|
67 | float re, im; |
---|
68 | char * eeptr; |
---|
69 | re = strtof( sptr, &eeptr ); |
---|
70 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
71 | im = strtof( eeptr, &eeptr ); |
---|
72 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
73 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
74 | return re + im * _Complex_I; |
---|
75 | } // strto |
---|
76 | |
---|
77 | double _Complex strto( const char sptr[], char ** eptr ) { |
---|
78 | double re, im; |
---|
79 | char * eeptr; |
---|
80 | re = strtod( sptr, &eeptr ); |
---|
81 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
82 | im = strtod( eeptr, &eeptr ); |
---|
83 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
84 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
85 | return re + im * _Complex_I; |
---|
86 | } // strto |
---|
87 | |
---|
88 | long double _Complex strto( const char sptr[], char ** eptr ) { |
---|
89 | long double re, im; |
---|
90 | char * eeptr; |
---|
91 | re = strtold( sptr, &eeptr ); |
---|
92 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
93 | im = strtold( eeptr, &eeptr ); |
---|
94 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
95 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
96 | return re + im * _Complex_I; |
---|
97 | } // strto |
---|
98 | |
---|
99 | //--------------------------------------- |
---|
100 | |
---|
101 | forall( E | { int ?<?( E, E ); } ) { |
---|
102 | E * bsearch( E key, const E * vals, size_t dim ) { |
---|
103 | int cmp( const void * t1, const void * t2 ) { |
---|
104 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
105 | } // cmp |
---|
106 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
107 | } // bsearch |
---|
108 | |
---|
109 | size_t bsearch( E key, const E * vals, size_t dim ) { |
---|
110 | E * result = bsearch( key, vals, dim ); |
---|
111 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
112 | } // bsearch |
---|
113 | |
---|
114 | size_t bsearchl( E key, const E * vals, size_t dim ) { |
---|
115 | size_t l = 0, m, h = dim; |
---|
116 | while ( l < h ) { |
---|
117 | m = (l + h) / 2; |
---|
118 | if ( (E &)(vals[m]) < key ) { // cast away const |
---|
119 | l = m + 1; |
---|
120 | } else { |
---|
121 | h = m; |
---|
122 | } // if |
---|
123 | } // while |
---|
124 | return l; |
---|
125 | } // bsearchl |
---|
126 | |
---|
127 | E * bsearchl( E key, const E * vals, size_t dim ) { |
---|
128 | size_t posn = bsearchl( key, vals, dim ); |
---|
129 | return (E *)(&vals[posn]); // cast away const |
---|
130 | } // bsearchl |
---|
131 | |
---|
132 | size_t bsearchu( E key, const E * vals, size_t dim ) { |
---|
133 | size_t l = 0, m, h = dim; |
---|
134 | while ( l < h ) { |
---|
135 | m = (l + h) / 2; |
---|
136 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const |
---|
137 | l = m + 1; |
---|
138 | } else { |
---|
139 | h = m; |
---|
140 | } // if |
---|
141 | } // while |
---|
142 | return l; |
---|
143 | } // bsearchu |
---|
144 | |
---|
145 | E * bsearchu( E key, const E * vals, size_t dim ) { |
---|
146 | size_t posn = bsearchu( key, vals, dim ); |
---|
147 | return (E *)(&vals[posn]); |
---|
148 | } // bsearchu |
---|
149 | |
---|
150 | |
---|
151 | void qsort( E * vals, size_t dim ) { |
---|
152 | int cmp( const void * t1, const void * t2 ) { |
---|
153 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
154 | } // cmp |
---|
155 | qsort( vals, dim, sizeof(E), cmp ); |
---|
156 | } // qsort |
---|
157 | } // distribution |
---|
158 | |
---|
159 | |
---|
160 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
161 | E * bsearch( K key, const E * vals, size_t dim ) { |
---|
162 | int cmp( const void * t1, const void * t2 ) { |
---|
163 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0; |
---|
164 | } // cmp |
---|
165 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
166 | } // bsearch |
---|
167 | |
---|
168 | size_t bsearch( K key, const E * vals, size_t dim ) { |
---|
169 | E * result = bsearch( key, vals, dim ); |
---|
170 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
171 | } // bsearch |
---|
172 | |
---|
173 | size_t bsearchl( K key, const E * vals, size_t dim ) { |
---|
174 | size_t l = 0, m, h = dim; |
---|
175 | while ( l < h ) { |
---|
176 | m = (l + h) / 2; |
---|
177 | if ( getKey( vals[m] ) < key ) { |
---|
178 | l = m + 1; |
---|
179 | } else { |
---|
180 | h = m; |
---|
181 | } // if |
---|
182 | } // while |
---|
183 | return l; |
---|
184 | } // bsearchl |
---|
185 | |
---|
186 | E * bsearchl( K key, const E * vals, size_t dim ) { |
---|
187 | size_t posn = bsearchl( key, vals, dim ); |
---|
188 | return (E *)(&vals[posn]); // cast away const |
---|
189 | } // bsearchl |
---|
190 | |
---|
191 | size_t bsearchu( K key, const E * vals, size_t dim ) { |
---|
192 | size_t l = 0, m, h = dim; |
---|
193 | while ( l < h ) { |
---|
194 | m = (l + h) / 2; |
---|
195 | if ( ! ( key < getKey( vals[m] ) ) ) { |
---|
196 | l = m + 1; |
---|
197 | } else { |
---|
198 | h = m; |
---|
199 | } // if |
---|
200 | } // while |
---|
201 | return l; |
---|
202 | } // bsearchu |
---|
203 | |
---|
204 | E * bsearchu( K key, const E * vals, size_t dim ) { |
---|
205 | size_t posn = bsearchu( key, vals, dim ); |
---|
206 | return (E *)(&vals[posn]); |
---|
207 | } // bsearchu |
---|
208 | } // distribution |
---|
209 | |
---|
210 | //--------------------------------------- |
---|
211 | |
---|
212 | extern "C" { // override C version |
---|
213 | void srandom( unsigned int seed ) { srand48( (long int)seed ); } |
---|
214 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
215 | } // extern "C" |
---|
216 | |
---|
217 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 |
---|
218 | double random( void ) { return drand48(); } |
---|
219 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
220 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
221 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
222 | |
---|
223 | //--------------------------------------- |
---|
224 | |
---|
225 | #define GENERATOR LCG |
---|
226 | |
---|
227 | uint32_t __global_random_seed; // sequential/concurrent |
---|
228 | uint32_t __global_random_state; // sequential only |
---|
229 | |
---|
230 | void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; GENERATOR( state ); } // set seed |
---|
231 | uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); } |
---|
232 | |
---|
233 | void set_seed( uint32_t seed ) { __global_random_seed = seed; GENERATOR( __global_random_state ); } |
---|
234 | uint32_t get_seed() { return __global_random_seed; } |
---|
235 | uint32_t prng( void ) { return GENERATOR( __global_random_state ); } // [0,UINT_MAX] |
---|
236 | |
---|
237 | //--------------------------------------- |
---|
238 | |
---|
239 | bool threading_enabled( void ) __attribute__(( weak )) { return false; } |
---|
240 | |
---|
241 | // Local Variables: // |
---|
242 | // tab-width: 4 // |
---|
243 | // End: // |
---|