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