source: src/libcfa/stdlib.c@ c315863

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 c315863 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
RevLine 
[bd85400]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//
[43385ca]7// algorithm.c --
[bd85400]8//
9// Author : Peter A. Buhr
10// Created On : Thu Jan 28 17:10:29 2016
11// Last Modified By : Peter A. Buhr
[cb811ac]12// Last Modified On : Wed Aug 23 20:30:44 2017
13// Update Count : 292
[bd85400]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
[f3fc631f]22#include <string.h> // memcpy, memset
[bd85400]23#include <malloc.h> // malloc_usable_size
24#include <math.h> // fabsf, fabs, fabsl
[6e991d6]25#include <complex.h> // _Complex_I
[bd85400]26
[f3fc631f]27// resize, non-array types
[6065b3aa]28forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
[f3fc631f]29 size_t olen = malloc_usable_size( ptr ); // current allocation
[6065b3aa]30 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
[f3fc631f]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
[aca65621]34 } //
[f3fc631f]35 return (T *)nptr;
[6065b3aa]36} // alloc
[f3ddc21]37
[6065b3aa]38// allocation/deallocation and constructor/destructor, non-array types
[aca65621]39forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[627f585]40T * new( Params p ) {
[a493682]41 return &(*malloc()){ p }; // run constructor
[f3ddc21]42} // new
[627f585]43
[83a071f9]44forall( dtype T | sized(T) | { void ^?{}( T & ); } )
[627f585]45void delete( T * ptr ) {
[6065b3aa]46 if ( ptr ) { // ignore null
[83a071f9]47 ^(*ptr){}; // run destructor
[f3ddc21]48 free( ptr );
[f3fc631f]49 } // if
[f3ddc21]50} // delete
[627f585]51
[83a071f9]52forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
[bf76eab]53void delete( T * ptr, Params rest ) {
[6065b3aa]54 if ( ptr ) { // ignore null
[83a071f9]55 ^(*ptr){}; // run destructor
[bf76eab]56 free( ptr );
[f3fc631f]57 } // if
[bf76eab]58 delete( rest );
[f3ddc21]59} // delete
[bf76eab]60
[6065b3aa]61
62// allocation/deallocation and constructor/destructor, array types
[aca65621]63forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[6065b3aa]64T * anew( size_t dim, Params p ) {
65 T *arr = alloc( dim );
66 for ( unsigned int i = 0; i < dim; i += 1 ) {
[a493682]67 (arr[i]){ p }; // run constructor
[6065b3aa]68 } // for
69 return arr;
70} // anew
71
[aca65621]72forall( dtype T | sized(T) | { void ^?{}( T & ); } )
[6065b3aa]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
[a493682]76 ^(arr[i]){}; // run destructor
[6065b3aa]77 } // for
78 free( arr );
79 } // if
80} // adelete
81
[aca65621]82forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
[6065b3aa]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
[a493682]86 ^(arr[i]){}; // run destructor
[6065b3aa]87 } // for
88 free( arr );
89 } // if
90 adelete( rest );
91} // adelete
92
[bd85400]93//---------------------------------------
94
95int ato( const char * ptr ) {
96 int i;
[6e991d6]97 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
[bd85400]98 return i;
[f3ddc21]99} // ato
100
[bd85400]101unsigned int ato( const char * ptr ) {
102 unsigned int ui;
[6e991d6]103 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
[bd85400]104 return ui;
[f3ddc21]105} // ato
106
[bd85400]107long int ato( const char * ptr ) {
108 long int li;
[6e991d6]109 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
[bd85400]110 return li;
[f3ddc21]111} // ato
112
[bd85400]113unsigned long int ato( const char * ptr ) {
114 unsigned long int uli;
[6e991d6]115 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
[bd85400]116 return uli;
[f3ddc21]117} // ato
118
[bd85400]119long long int ato( const char * ptr ) {
120 long long int lli;
[6e991d6]121 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
[bd85400]122 return lli;
[f3ddc21]123} // ato
124
[bd85400]125unsigned long long int ato( const char * ptr ) {
126 unsigned long long int ulli;
[6e991d6]127 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
[bd85400]128 return ulli;
[f3ddc21]129} // ato
130
[90c3b1c]131
[bd85400]132float ato( const char * ptr ) {
133 float f;
[6e991d6]134 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
[bd85400]135 return f;
[f3ddc21]136} // ato
137
[bd85400]138double ato( const char * ptr ) {
139 double d;
[6e991d6]140 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
[bd85400]141 return d;
[f3ddc21]142} // ato
143
[bd85400]144long double ato( const char * ptr ) {
145 long double ld;
[6e991d6]146 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
[bd85400]147 return ld;
[f3ddc21]148} // ato
149
[90c3b1c]150
[bd85400]151float _Complex ato( const char * ptr ) {
152 float re, im;
[6e991d6]153 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
[bd85400]154 return re + im * _Complex_I;
[f3ddc21]155} // ato
156
[bd85400]157double _Complex ato( const char * ptr ) {
158 double re, im;
[6e991d6]159 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
[bd85400]160 return re + im * _Complex_I;
[f3ddc21]161} // ato
162
[bd85400]163long double _Complex ato( const char * ptr ) {
164 long double re, im;
[6e991d6]165 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
[bd85400]166 return re + im * _Complex_I;
[f3ddc21]167} // ato
168
[bd85400]169
170int strto( const char * sptr, char ** eptr, int base ) {
171 return (int)strtol( sptr, eptr, base );
[f3ddc21]172} // strto
173
[bd85400]174unsigned int strto( const char * sptr, char ** eptr, int base ) {
175 return (unsigned int)strtoul( sptr, eptr, base );
[f3ddc21]176} // strto
177
[bd85400]178long int strto( const char * sptr, char ** eptr, int base ) {
179 return strtol( sptr, eptr, base );
[f3ddc21]180} // strto
181
[bd85400]182unsigned long int strto( const char * sptr, char ** eptr, int base ) {
183 return strtoul( sptr, eptr, base );
[f3ddc21]184} // strto
185
[bd85400]186long long int strto( const char * sptr, char ** eptr, int base ) {
187 return strtoll( sptr, eptr, base );
[f3ddc21]188} // strto
189
[bd85400]190unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
191 return strtoull( sptr, eptr, base );
[f3ddc21]192} // strto
193
[90c3b1c]194
[bd85400]195float strto( const char * sptr, char ** eptr ) {
196 return strtof( sptr, eptr );
[f3ddc21]197} // strto
198
[bd85400]199double strto( const char * sptr, char ** eptr ) {
200 return strtod( sptr, eptr );
[f3ddc21]201} // strto
202
[bd85400]203long double strto( const char * sptr, char ** eptr ) {
204 return strtold( sptr, eptr );
[f3ddc21]205} // strto
206
[90c3b1c]207
[bd85400]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;
[f3ddc21]215} // strto
216
[bd85400]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;
[f3ddc21]224} // strto
225
[bd85400]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;
[f3ddc21]233} // strto
[bd85400]234
235//---------------------------------------
236
[4040425]237forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]238T * bsearch( T key, const T * arr, size_t dim ) {
[bd85400]239 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
[f3fc631f]240 return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
[bd85400]241} // bsearch
242
[707446a]243forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]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)
[707446a]247} // bsearch
248
[4040425]249forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]250void qsort( const T * arr, size_t dim ) {
[bd85400]251 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
[f3fc631f]252 qsort( arr, dim, sizeof(T), comp );
[bd85400]253} // qsort
254
255//---------------------------------------
256
[5ea26ed]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 ]; }
[43385ca]260forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[5ea26ed]261[ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
[bd85400]262
263//---------------------------------------
264
[c3ebf37]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 ); }
[6e991d6]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 ); }
[53ba273]274
275//---------------------------------------
276
[0438091]277void rand48seed( long int s ) { srand48( s ); }
[8dc51c8]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); }
[bd85400]288
289//---------------------------------------
290
[4040425]291forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]292T min( T t1, T t2 ) {
[bd85400]293 return t1 < t2 ? t1 : t2;
294} // min
295
[4040425]296forall( otype T | { int ?>?( T, T ); } )
[a797e2b1]297T max( T t1, T t2 ) {
[bd85400]298 return t1 > t2 ? t1 : t2;
299} // max
300
[a9f2c13]301forall( otype T | { T min( T, T ); T max( T, T ); } )
[a797e2b1]302T clamp( T value, T min_val, T max_val ) {
[a9f2c13]303 return max( min_val, min( value, max_val ) );
304} // clamp
305
[4040425]306forall( otype T )
[cb811ac]307void swap( T & t1, T & t2 ) {
308 T temp = t1;
309 t1 = t2;
310 t2 = temp;
[bd85400]311} // swap
312
313// Local Variables: //
314// tab-width: 4 //
315// End: //
Note: See TracBrowser for help on using the repository browser.