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