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