source: src/libcfa/stdlib.c @ f3fc631f

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

first attempt new storage management routines

  • Property mode set to 100644
File size: 7.9 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 : Tue May 30 09:07:56 2017
13// Update Count     : 237
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
23#include <string.h>                                                                             // memcpy, memset
24#include <malloc.h>                                                                             // malloc_usable_size
25#include <math.h>                                                                               // fabsf, fabs, fabsl
26#include <complex.h>                                                                    // _Complex_I
27} // extern "C"
28
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;
39} // realloc
40
41// allocation/deallocation and constructor/destructor
42forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
43T * new( Params p ) {
44        return ((T *)malloc()){ p };
45} // new
46
47forall( dtype T | { void ^?{}( T * ); } )
48void delete( T * ptr ) {
49        if ( ptr ) {
50                ^ptr{};                                                                                 // run destructor
51                free( ptr );
52        } // if
53} // delete
54
55forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
56void delete( T * ptr, Params rest ) {
57        if ( ptr ) {
58                ^ptr{};                                                                                 // run destructor
59                free( ptr );
60        } // if
61        delete( rest );
62} // delete
63
64//---------------------------------------
65
66int ato( const char * ptr ) {
67        int i;
68        if ( sscanf( ptr, "%d", &i ) == EOF ) {}
69        return i;
70} // ato
71
72unsigned int ato( const char * ptr ) {
73        unsigned int ui;
74        if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
75        return ui;
76} // ato
77
78long int ato( const char * ptr ) {
79        long int li;
80        if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
81        return li;
82} // ato
83
84unsigned long int ato( const char * ptr ) {
85        unsigned long int uli;
86        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
87        return uli;
88} // ato
89
90long long int ato( const char * ptr ) {
91        long long int lli;
92        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
93        return lli;
94} // ato
95
96unsigned long long int ato( const char * ptr ) {
97        unsigned long long int ulli;
98        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
99        return ulli;
100} // ato
101
102
103float ato( const char * ptr ) {
104        float f;
105        if ( sscanf( ptr, "%f", &f ) == EOF ) {}
106        return f;
107} // ato
108
109double ato( const char * ptr ) {
110        double d;
111        if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
112        return d;
113} // ato
114
115long double ato( const char * ptr ) {
116        long double ld;
117        if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
118        return ld;
119} // ato
120
121
122float _Complex ato( const char * ptr ) {
123        float re, im;
124        if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
125        return re + im * _Complex_I;
126} // ato
127
128double _Complex ato( const char * ptr ) {
129        double re, im;
130        if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
131        return re + im * _Complex_I;
132} // ato
133
134long double _Complex ato( const char * ptr ) {
135        long double re, im;
136        if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
137        return re + im * _Complex_I;
138} // ato
139
140
141int strto( const char * sptr, char ** eptr, int base ) {
142        return (int)strtol( sptr, eptr, base );
143} // strto
144
145unsigned int strto( const char * sptr, char ** eptr, int base ) {
146        return (unsigned int)strtoul( sptr, eptr, base );
147} // strto
148
149long int strto( const char * sptr, char ** eptr, int base ) {
150        return strtol( sptr, eptr, base );
151} // strto
152
153unsigned long int strto( const char * sptr, char ** eptr, int base ) {
154        return strtoul( sptr, eptr, base );
155} // strto
156
157long long int strto( const char * sptr, char ** eptr, int base ) {
158        return strtoll( sptr, eptr, base );
159} // strto
160
161unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
162        return strtoull( sptr, eptr, base );
163} // strto
164
165
166float strto( const char * sptr, char ** eptr ) {
167        return strtof( sptr, eptr );
168} // strto
169
170double strto( const char * sptr, char ** eptr ) {
171        return strtod( sptr, eptr );
172} // strto
173
174long double strto( const char * sptr, char ** eptr ) {
175        return strtold( sptr, eptr );
176} // strto
177
178
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;
186} // strto
187
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;
195} // strto
196
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;
204} // strto
205
206//---------------------------------------
207
208forall( otype T | { int ?<?( T, T ); } )
209T * bsearch( T key, const T * arr, size_t dim ) {
210        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
211        return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
212} // bsearch
213
214forall( otype T | { int ?<?( T, T ); } )
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)
218} // bsearch
219
220forall( otype T | { int ?<?( T, T ); } )
221void qsort( const T * arr, size_t dim ) {
222        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
223        qsort( arr, dim, sizeof(T), comp );
224} // qsort
225
226//---------------------------------------
227
228forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
229[ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
230
231//---------------------------------------
232
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 ); }
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 ); }
242
243//---------------------------------------
244
245void rand48seed( long int s ) { srand48( s ); }
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); }
256
257//---------------------------------------
258
259forall( otype T | { int ?<?( T, T ); } )
260T min( T t1, T t2 ) {
261        return t1 < t2 ? t1 : t2;
262} // min
263
264forall( otype T | { int ?>?( T, T ); } )
265T max( T t1, T t2 ) {
266        return t1 > t2 ? t1 : t2;
267} // max
268
269forall( otype T | { T min( T, T ); T max( T, T ); } )
270T clamp( T value, T min_val, T max_val ) {
271        return max( min_val, min( value, max_val ) );
272} // clamp
273
274forall( otype T )
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.