source: src/libcfa/stdlib.c@ 6ae8c92

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6ae8c92 was cb811ac, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change stdlib swap to use references, and update swap tests

  • Property mode set to 100644
File size: 9.3 KB
Line 
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 Aug 23 20:30:44 2017
13// Update Count : 292
14//
15
16#include "stdlib"
17
18//---------------------------------------
19
20#define _XOPEN_SOURCE 600 // posix_memalign, *rand48
21#include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
22#include <string.h> // memcpy, memset
23#include <malloc.h> // malloc_usable_size
24#include <math.h> // fabsf, fabs, fabsl
25#include <complex.h> // _Complex_I
26
27// resize, non-array types
28forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
29 size_t olen = malloc_usable_size( ptr ); // current allocation
30 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
31 size_t nlen = malloc_usable_size( nptr ); // new allocation
32 if ( nlen > olen ) { // larger ?
33 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
34 } //
35 return (T *)nptr;
36} // alloc
37
38// allocation/deallocation and constructor/destructor, non-array types
39forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
40T * new( Params p ) {
41 return &(*malloc()){ p }; // run constructor
42} // new
43
44forall( dtype T | sized(T) | { void ^?{}( T & ); } )
45void delete( T * ptr ) {
46 if ( ptr ) { // ignore null
47 ^(*ptr){}; // run destructor
48 free( ptr );
49 } // if
50} // delete
51
52forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
53void delete( T * ptr, Params rest ) {
54 if ( ptr ) { // ignore null
55 ^(*ptr){}; // run destructor
56 free( ptr );
57 } // if
58 delete( rest );
59} // delete
60
61
62// allocation/deallocation and constructor/destructor, array types
63forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
64T * anew( size_t dim, Params p ) {
65 T *arr = alloc( dim );
66 for ( unsigned int i = 0; i < dim; i += 1 ) {
67 (arr[i]){ p }; // run constructor
68 } // for
69 return arr;
70} // anew
71
72forall( dtype T | sized(T) | { void ^?{}( T & ); } )
73void adelete( size_t dim, T arr[] ) {
74 if ( arr ) { // ignore null
75 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
76 ^(arr[i]){}; // run destructor
77 } // for
78 free( arr );
79 } // if
80} // adelete
81
82forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
83void adelete( size_t dim, T arr[], Params rest ) {
84 if ( arr ) { // ignore null
85 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
86 ^(arr[i]){}; // run destructor
87 } // for
88 free( arr );
89 } // if
90 adelete( rest );
91} // adelete
92
93//---------------------------------------
94
95int ato( const char * ptr ) {
96 int i;
97 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
98 return i;
99} // ato
100
101unsigned int ato( const char * ptr ) {
102 unsigned int ui;
103 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
104 return ui;
105} // ato
106
107long int ato( const char * ptr ) {
108 long int li;
109 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
110 return li;
111} // ato
112
113unsigned long int ato( const char * ptr ) {
114 unsigned long int uli;
115 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
116 return uli;
117} // ato
118
119long long int ato( const char * ptr ) {
120 long long int lli;
121 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
122 return lli;
123} // ato
124
125unsigned long long int ato( const char * ptr ) {
126 unsigned long long int ulli;
127 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
128 return ulli;
129} // ato
130
131
132float ato( const char * ptr ) {
133 float f;
134 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
135 return f;
136} // ato
137
138double ato( const char * ptr ) {
139 double d;
140 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
141 return d;
142} // ato
143
144long double ato( const char * ptr ) {
145 long double ld;
146 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
147 return ld;
148} // ato
149
150
151float _Complex ato( const char * ptr ) {
152 float re, im;
153 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
154 return re + im * _Complex_I;
155} // ato
156
157double _Complex ato( const char * ptr ) {
158 double re, im;
159 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
160 return re + im * _Complex_I;
161} // ato
162
163long double _Complex ato( const char * ptr ) {
164 long double re, im;
165 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
166 return re + im * _Complex_I;
167} // ato
168
169
170int strto( const char * sptr, char ** eptr, int base ) {
171 return (int)strtol( sptr, eptr, base );
172} // strto
173
174unsigned int strto( const char * sptr, char ** eptr, int base ) {
175 return (unsigned int)strtoul( sptr, eptr, base );
176} // strto
177
178long int strto( const char * sptr, char ** eptr, int base ) {
179 return strtol( sptr, eptr, base );
180} // strto
181
182unsigned long int strto( const char * sptr, char ** eptr, int base ) {
183 return strtoul( sptr, eptr, base );
184} // strto
185
186long long int strto( const char * sptr, char ** eptr, int base ) {
187 return strtoll( sptr, eptr, base );
188} // strto
189
190unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
191 return strtoull( sptr, eptr, base );
192} // strto
193
194
195float strto( const char * sptr, char ** eptr ) {
196 return strtof( sptr, eptr );
197} // strto
198
199double strto( const char * sptr, char ** eptr ) {
200 return strtod( sptr, eptr );
201} // strto
202
203long double strto( const char * sptr, char ** eptr ) {
204 return strtold( sptr, eptr );
205} // strto
206
207
208float _Complex strto( const char * sptr, char ** eptr ) {
209 float re, im;
210 re = strtof( sptr, eptr );
211 if ( sptr == *eptr ) return 0.0;
212 im = strtof( sptr, eptr );
213 if ( sptr == *eptr ) return 0.0;
214 return re + im * _Complex_I;
215} // strto
216
217double _Complex strto( const char * sptr, char ** eptr ) {
218 double re, im;
219 re = strtod( sptr, eptr );
220 if ( sptr == *eptr ) return 0.0;
221 im = strtod( sptr, eptr );
222 if ( sptr == *eptr ) return 0.0;
223 return re + im * _Complex_I;
224} // strto
225
226long double _Complex strto( const char * sptr, char ** eptr ) {
227 long double re, im;
228 re = strtold( sptr, eptr );
229 if ( sptr == *eptr ) return 0.0;
230 im = strtold( sptr, eptr );
231 if ( sptr == *eptr ) return 0.0;
232 return re + im * _Complex_I;
233} // strto
234
235//---------------------------------------
236
237forall( otype T | { int ?<?( T, T ); } )
238T * bsearch( T key, const T * arr, size_t dim ) {
239 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
240 return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
241} // bsearch
242
243forall( otype T | { int ?<?( T, T ); } )
244unsigned int bsearch( T key, const T * arr, size_t dim ) {
245 T *result = bsearch( key, arr, dim );
246 return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
247} // bsearch
248
249forall( otype T | { int ?<?( T, T ); } )
250void qsort( const T * arr, size_t dim ) {
251 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
252 qsort( arr, dim, sizeof(T), comp );
253} // qsort
254
255//---------------------------------------
256
257[ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; }
258[ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
259[ long long int, long long int ] div( long long int num, long long int denom ) { lldiv_t qr = lldiv( num, denom ); return [ qr.quot, qr.rem ]; }
260forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
261[ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
262
263//---------------------------------------
264
265unsigned char abs( signed char v ) { return abs( (int)v ); }
266unsigned long int abs( long int v ) { return labs( v ); }
267unsigned long long int abs( long long int v ) { return llabs( v ); }
268float abs( float x ) { return fabsf( x ); }
269double abs( double x ) { return fabs( x ); }
270long double abs( long double x ) { return fabsl( x ); }
271float abs( float _Complex x ) { return cabsf( x ); }
272double abs( double _Complex x ) { return cabs( x ); }
273long double abs( long double _Complex x ) { return cabsl( x ); }
274
275//---------------------------------------
276
277void rand48seed( long int s ) { srand48( s ); }
278char rand48( void ) { return mrand48(); }
279int rand48( void ) { return mrand48(); }
280unsigned int rand48( void ) { return lrand48(); }
281long int rand48( void ) { return mrand48(); }
282unsigned long int rand48( void ) { return lrand48(); }
283float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48
284double rand48( void ) { return drand48(); }
285float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
286double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
287long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
288
289//---------------------------------------
290
291forall( otype T | { int ?<?( T, T ); } )
292T min( T t1, T t2 ) {
293 return t1 < t2 ? t1 : t2;
294} // min
295
296forall( otype T | { int ?>?( T, T ); } )
297T max( T t1, T t2 ) {
298 return t1 > t2 ? t1 : t2;
299} // max
300
301forall( otype T | { T min( T, T ); T max( T, T ); } )
302T clamp( T value, T min_val, T max_val ) {
303 return max( min_val, min( value, max_val ) );
304} // clamp
305
306forall( otype T )
307void swap( T & t1, T & t2 ) {
308 T temp = t1;
309 t1 = t2;
310 t2 = temp;
311} // swap
312
313// Local Variables: //
314// tab-width: 4 //
315// End: //
Note: See TracBrowser for help on using the repository browser.