source: src/libcfa/stdlib.c @ aca65621

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since aca65621 was aca65621, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Convert several library files to use references

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