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