source: src/libcfa/stdlib.c@ 676cc8c

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 676cc8c was f3fc631f, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt new storage management routines

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