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