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 | // algorithm.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 : Wed Mar 30 10:48:41 2016 |
---|
13 | // Update Count : 149 |
---|
14 | // |
---|
15 | |
---|
16 | #include "stdlib" |
---|
17 | |
---|
18 | //--------------------------------------- |
---|
19 | |
---|
20 | extern "C" { |
---|
21 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 |
---|
22 | #include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch |
---|
23 | #include <string.h> // memset |
---|
24 | #include <malloc.h> // malloc_usable_size |
---|
25 | #include <math.h> // fabsf, fabs, fabsl |
---|
26 | #include <complex.h> // _Complex_I, cabsf, cabs, cabsl |
---|
27 | } // extern "C" |
---|
28 | |
---|
29 | forall( otype T ) T * malloc( void ) { |
---|
30 | printf( "malloc1\n" ); |
---|
31 | return (T *)malloc( sizeof(T) ); |
---|
32 | } // malloc |
---|
33 | forall( otype T ) T * malloc( size_t size ) { |
---|
34 | printf( "malloc2\n" ); |
---|
35 | return (T *)(void *)malloc( size ); |
---|
36 | } // malloc |
---|
37 | forall( otype T ) T * malloc( char fill ) { |
---|
38 | printf( "malloc3\n" ); |
---|
39 | T * ptr = (T *)malloc( sizeof(T) ); |
---|
40 | return memset( ptr ); |
---|
41 | } // malloc |
---|
42 | |
---|
43 | forall( otype T ) T * calloc( size_t size ) { |
---|
44 | printf( "calloc\n" ); |
---|
45 | return (T *)calloc( size, sizeof(T) ); |
---|
46 | } // calloc |
---|
47 | |
---|
48 | forall( otype T ) T * realloc( T * ptr, size_t size ) { |
---|
49 | printf( "realloc1\n" ); |
---|
50 | return (T *)(void *)realloc( (void *)ptr, size ); |
---|
51 | } // realloc |
---|
52 | forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ) { |
---|
53 | printf( "realloc2\n" ); |
---|
54 | char * nptr = (T *)(void *)realloc( (void *)ptr, size ); |
---|
55 | size_t unused = malloc_usable_size( nptr ); |
---|
56 | memset( nptr + size - unused, (int)fill, unused ); // initialize any new storage |
---|
57 | return nptr; |
---|
58 | } // realloc |
---|
59 | |
---|
60 | forall( otype T ) T * malloc( T * ptr, size_t size ) { |
---|
61 | printf( "malloc4\n" ); |
---|
62 | return (T *)realloc( ptr, size ); |
---|
63 | } // malloc |
---|
64 | forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ) { |
---|
65 | printf( "malloc5\n" ); |
---|
66 | return (T *)realloc( ptr, size, fill ); |
---|
67 | } // malloc |
---|
68 | |
---|
69 | forall( otype T ) T * aligned_alloc( size_t alignment ) { |
---|
70 | printf( "aligned_alloc\n" ); |
---|
71 | return (T *)memalign( alignment, sizeof(T) ); |
---|
72 | } // aligned_alloc |
---|
73 | |
---|
74 | forall( otype T ) T * memalign( size_t alignment ) { |
---|
75 | printf( "memalign\n" ); |
---|
76 | return (T *)memalign( alignment, sizeof(T) ); |
---|
77 | } // memalign |
---|
78 | |
---|
79 | forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ) { |
---|
80 | printf( "posix_memalign\n" ); |
---|
81 | return posix_memalign( (void **)ptr, alignment, sizeof(T) ); |
---|
82 | } // posix_memalign |
---|
83 | |
---|
84 | forall( otype T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill |
---|
85 | printf( "memset1\n" ); |
---|
86 | return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) ); |
---|
87 | } // memset |
---|
88 | forall( otype T ) T * memset( T * ptr ) { // remove when default value available |
---|
89 | printf( "memset2\n" ); |
---|
90 | return (T *)memset( ptr, 0, malloc_usable_size( ptr ) ); |
---|
91 | } // memset |
---|
92 | |
---|
93 | //--------------------------------------- |
---|
94 | |
---|
95 | int ato( const char * ptr ) { |
---|
96 | int i; |
---|
97 | if ( sscanf( ptr, "%d", &i ) == EOF ) {} // check return code |
---|
98 | return i; |
---|
99 | } |
---|
100 | unsigned int ato( const char * ptr ) { |
---|
101 | unsigned int ui; |
---|
102 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {} // check return code |
---|
103 | return ui; |
---|
104 | } |
---|
105 | long int ato( const char * ptr ) { |
---|
106 | long int li; |
---|
107 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code |
---|
108 | return li; |
---|
109 | } |
---|
110 | unsigned long int ato( const char * ptr ) { |
---|
111 | unsigned long int uli; |
---|
112 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code |
---|
113 | return uli; |
---|
114 | } |
---|
115 | long long int ato( const char * ptr ) { |
---|
116 | long long int lli; |
---|
117 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code |
---|
118 | return lli; |
---|
119 | } |
---|
120 | unsigned long long int ato( const char * ptr ) { |
---|
121 | unsigned long long int ulli; |
---|
122 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code |
---|
123 | return ulli; |
---|
124 | } |
---|
125 | |
---|
126 | float ato( const char * ptr ) { |
---|
127 | float f; |
---|
128 | if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code |
---|
129 | return f; |
---|
130 | } |
---|
131 | double ato( const char * ptr ) { |
---|
132 | double d; |
---|
133 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code |
---|
134 | return d; |
---|
135 | } |
---|
136 | long double ato( const char * ptr ) { |
---|
137 | long double ld; |
---|
138 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code |
---|
139 | return ld; |
---|
140 | } |
---|
141 | |
---|
142 | float _Complex ato( const char * ptr ) { |
---|
143 | float re, im; |
---|
144 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} // check return code |
---|
145 | return re + im * _Complex_I; |
---|
146 | } |
---|
147 | double _Complex ato( const char * ptr ) { |
---|
148 | double re, im; |
---|
149 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code |
---|
150 | return re + im * _Complex_I; |
---|
151 | } |
---|
152 | long double _Complex ato( const char * ptr ) { |
---|
153 | long double re, im; |
---|
154 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} // check return code |
---|
155 | return re + im * _Complex_I; |
---|
156 | } |
---|
157 | |
---|
158 | int strto( const char * sptr, char ** eptr, int base ) { |
---|
159 | return (int)strtol( sptr, eptr, base ); |
---|
160 | } |
---|
161 | unsigned int strto( const char * sptr, char ** eptr, int base ) { |
---|
162 | return (unsigned int)strtoul( sptr, eptr, base ); |
---|
163 | } |
---|
164 | long int strto( const char * sptr, char ** eptr, int base ) { |
---|
165 | return strtol( sptr, eptr, base ); |
---|
166 | } |
---|
167 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { |
---|
168 | return strtoul( sptr, eptr, base ); |
---|
169 | } |
---|
170 | long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
171 | return strtoll( sptr, eptr, base ); |
---|
172 | } |
---|
173 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
174 | return strtoull( sptr, eptr, base ); |
---|
175 | } |
---|
176 | |
---|
177 | float strto( const char * sptr, char ** eptr ) { |
---|
178 | return strtof( sptr, eptr ); |
---|
179 | } |
---|
180 | double strto( const char * sptr, char ** eptr ) { |
---|
181 | return strtod( sptr, eptr ); |
---|
182 | } |
---|
183 | long double strto( const char * sptr, char ** eptr ) { |
---|
184 | return strtold( sptr, eptr ); |
---|
185 | } |
---|
186 | |
---|
187 | float _Complex strto( const char * sptr, char ** eptr ) { |
---|
188 | float re, im; |
---|
189 | re = strtof( sptr, eptr ); |
---|
190 | if ( sptr == *eptr ) return 0.0; |
---|
191 | im = strtof( sptr, eptr ); |
---|
192 | if ( sptr == *eptr ) return 0.0; |
---|
193 | return re + im * _Complex_I; |
---|
194 | } |
---|
195 | double _Complex strto( const char * sptr, char ** eptr ) { |
---|
196 | double re, im; |
---|
197 | re = strtod( sptr, eptr ); |
---|
198 | if ( sptr == *eptr ) return 0.0; |
---|
199 | im = strtod( sptr, eptr ); |
---|
200 | if ( sptr == *eptr ) return 0.0; |
---|
201 | return re + im * _Complex_I; |
---|
202 | } |
---|
203 | long double _Complex strto( const char * sptr, char ** eptr ) { |
---|
204 | long double re, im; |
---|
205 | re = strtold( sptr, eptr ); |
---|
206 | if ( sptr == *eptr ) return 0.0; |
---|
207 | im = strtold( sptr, eptr ); |
---|
208 | if ( sptr == *eptr ) return 0.0; |
---|
209 | return re + im * _Complex_I; |
---|
210 | } |
---|
211 | |
---|
212 | //--------------------------------------- |
---|
213 | |
---|
214 | forall( otype T | { int ?<?( T, T ); } ) |
---|
215 | T * bsearch( const T key, const T * arr, size_t dimension ) { |
---|
216 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
217 | return (T *)bsearch( &key, arr, dimension, sizeof(T), comp ); |
---|
218 | } // bsearch |
---|
219 | |
---|
220 | forall( otype T | { int ?<?( T, T ); } ) |
---|
221 | void qsort( const T * arr, size_t dimension ) { |
---|
222 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
223 | qsort( arr, dimension, sizeof(T), comp ); |
---|
224 | } // qsort |
---|
225 | |
---|
226 | //--------------------------------------- |
---|
227 | |
---|
228 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
229 | [ T, T ] div( T t1, T t2 ) { /* return [ t1 / t2, t1 % t2 ]; */ } |
---|
230 | |
---|
231 | //--------------------------------------- |
---|
232 | |
---|
233 | char abs( char v ) { return abs( (int)v ); } |
---|
234 | long int abs( long int v ) { return labs( v ); } |
---|
235 | long long int abs( long long int v ) { return llabs( v ); } |
---|
236 | float abs( float v ) { return fabsf( v ); } |
---|
237 | double abs( double v ) { return fabs( v ); } |
---|
238 | long double abs( long double v ) { return fabsl( v ); } |
---|
239 | float _Complex abs( float _Complex v ) { return cabsf( v ); } |
---|
240 | double _Complex abs( double _Complex v ) { return cabs( v ); } |
---|
241 | long double _Complex abs( long double _Complex v ) { return cabsl( v ); } |
---|
242 | |
---|
243 | //--------------------------------------- |
---|
244 | |
---|
245 | float floor( float v ) { return floorf( v ); } |
---|
246 | long double floor( long double v ) { return floorl( v ); } |
---|
247 | |
---|
248 | float ceil( float v ) { return ceilf( v ); } |
---|
249 | long double ceil( long double v ) { return ceill( v ); } |
---|
250 | |
---|
251 | //--------------------------------------- |
---|
252 | |
---|
253 | void rand48seed( long int s ) { srand48( s ); } |
---|
254 | char rand48() { return mrand48(); } |
---|
255 | int rand48() { return mrand48(); } |
---|
256 | unsigned int rand48() { return lrand48(); } |
---|
257 | long int rand48() { return mrand48(); } |
---|
258 | unsigned long int rand48() { return lrand48(); } |
---|
259 | float rand48() { return (float)drand48(); } // otherwise float uses lrand48 |
---|
260 | double rand48() { return drand48(); } |
---|
261 | float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
262 | double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
263 | long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
264 | |
---|
265 | //--------------------------------------- |
---|
266 | |
---|
267 | forall( otype T | { int ?<?( T, T ); } ) |
---|
268 | T min( const T t1, const T t2 ) { |
---|
269 | return t1 < t2 ? t1 : t2; |
---|
270 | } // min |
---|
271 | |
---|
272 | forall( otype T | { int ?>?( T, T ); } ) |
---|
273 | T max( const T t1, const T t2 ) { |
---|
274 | return t1 > t2 ? t1 : t2; |
---|
275 | } // max |
---|
276 | |
---|
277 | forall( otype T ) |
---|
278 | void swap( T * t1, T * t2 ) { |
---|
279 | T temp = *t1; |
---|
280 | *t1 = *t2; |
---|
281 | *t2 = temp; |
---|
282 | } // swap |
---|
283 | |
---|
284 | // Local Variables: // |
---|
285 | // tab-width: 4 // |
---|
286 | // End: // |
---|