source: src/libcfa/stdlib.c@ 21a5dde1

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 21a5dde1 was b2551ce, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remove unnecessary extern "C"

  • Property mode set to 100644
File size: 8.9 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
[b2551ce]12// Last Modified On : Thu Jul 20 16:01:40 2017
13// Update Count : 282
[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
34 } //
35 return (T *)nptr;
[6065b3aa]36} // alloc
[f3ddc21]37
[6065b3aa]38// allocation/deallocation and constructor/destructor, non-array types
39forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
[627f585]40T * new( Params p ) {
[6065b3aa]41 return (malloc()){ p }; // run constructor
[f3ddc21]42} // new
[627f585]43
[f3fc631f]44forall( dtype T | { void ^?{}( T * ); } )
[627f585]45void delete( T * ptr ) {
[6065b3aa]46 if ( ptr ) { // ignore null
[f3fc631f]47 ^ptr{}; // run destructor
[f3ddc21]48 free( ptr );
[f3fc631f]49 } // if
[f3ddc21]50} // delete
[627f585]51
[f3fc631f]52forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
[bf76eab]53void delete( T * ptr, Params rest ) {
[6065b3aa]54 if ( ptr ) { // ignore null
[f3fc631f]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
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
[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
[43385ca]257forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
258[ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
[bd85400]259
260//---------------------------------------
261
[c3ebf37]262unsigned char abs( signed char v ) { return abs( (int)v ); }
263unsigned long int abs( long int v ) { return labs( v ); }
264unsigned long long int abs( long long int v ) { return llabs( v ); }
[6e991d6]265float abs( float x ) { return fabsf( x ); }
266double abs( double x ) { return fabs( x ); }
267long double abs( long double x ) { return fabsl( x ); }
268float abs( float _Complex x ) { return cabsf( x ); }
269double abs( double _Complex x ) { return cabs( x ); }
270long double abs( long double _Complex x ) { return cabsl( x ); }
[53ba273]271
272//---------------------------------------
273
[0438091]274void rand48seed( long int s ) { srand48( s ); }
[8dc51c8]275char rand48( void ) { return mrand48(); }
276int rand48( void ) { return mrand48(); }
277unsigned int rand48( void ) { return lrand48(); }
278long int rand48( void ) { return mrand48(); }
279unsigned long int rand48( void ) { return lrand48(); }
280float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48
281double rand48( void ) { return drand48(); }
282float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
283double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
284long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
[bd85400]285
286//---------------------------------------
287
[4040425]288forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]289T min( T t1, T t2 ) {
[bd85400]290 return t1 < t2 ? t1 : t2;
291} // min
292
[4040425]293forall( otype T | { int ?>?( T, T ); } )
[a797e2b1]294T max( T t1, T t2 ) {
[bd85400]295 return t1 > t2 ? t1 : t2;
296} // max
297
[a9f2c13]298forall( otype T | { T min( T, T ); T max( T, T ); } )
[a797e2b1]299T clamp( T value, T min_val, T max_val ) {
[a9f2c13]300 return max( min_val, min( value, max_val ) );
301} // clamp
302
[4040425]303forall( otype T )
[bd85400]304void swap( T * t1, T * t2 ) {
305 T temp = *t1;
306 *t1 = *t2;
307 *t2 = temp;
308} // swap
309
310// Local Variables: //
311// tab-width: 4 //
312// End: //
Note: See TracBrowser for help on using the repository browser.