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