source: src/libcfa/stdlib.c @ bd85400

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since bd85400 was bd85400, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remove offsetof keyword, parser 0/1 names as structure fields, update examples to new stdlib, rename algorithms to stdlib, extend stdlib, use correct type for box parameters

  • Property mode set to 100644
File size: 8.6 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 : Fri Feb  5 15:41:24 2016
13// Update Count     : 128
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>                                                                             // memset
24#include <malloc.h>                                                                             // malloc_usable_size
25#include <stdio.h>
26#include <math.h>                                                                               // fabsf, fabs, fabsl
27#include <complex.h>                                                                    // _Complex_I, cabsf, cabs, cabsl
28} // extern "C"
29
30forall( type T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill
31        printf( "memset1\n" );
32    return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) );
33} // memset
34forall( type T ) T * memset( T * ptr ) {                                // remove when default value available
35        printf( "memset2\n" );
36    return (T *)memset( ptr, 0, malloc_usable_size( ptr ) );
37} // memset
38
39forall( type T ) T * malloc( void ) {
40        printf( "malloc1\n" );
41    return (T *)malloc( sizeof(T) );
42} // malloc
43forall( type T ) T * malloc( size_t size ) {
44        printf( "malloc2\n" );
45    return (T *)(void *)malloc( size );
46} // malloc
47forall( type T ) T * malloc( char fill ) {
48        printf( "malloc3\n" );
49        T * ptr = (T *)malloc( sizeof(T) );
50    return memset( ptr );
51} // malloc
52
53forall( type T ) T * calloc( size_t size ) {
54        printf( "calloc\n" );
55    return (T *)calloc( size, sizeof(T) );
56} // calloc
57
58forall( type T ) T * realloc( T * ptr, size_t size ) {
59        printf( "realloc1\n" );
60    return (T *)(void *)realloc( (void *)ptr, size );
61} // realloc
62forall( type T ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
63        printf( "realloc2\n" );
64    char * nptr = (T *)(void *)realloc( (void *)ptr, size );
65    size_t unused = malloc_usable_size( nptr );
66    memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
67    return nptr;
68} // realloc
69
70forall( type T ) T * malloc( T * ptr, size_t size ) {
71        printf( "malloc4\n" );
72    return (T *)realloc( ptr, size );
73} // malloc
74forall( type T ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
75        printf( "malloc5\n" );
76    return (T *)realloc( ptr, size, fill );
77} // malloc
78
79forall( type T ) T * aligned_alloc( size_t alignment ) {
80        printf( "aligned_alloc\n" );
81    return (T *)memalign( alignment, sizeof(T) );
82} // aligned_alloc
83
84forall( type T ) T * memalign( size_t alignment ) {
85        printf( "memalign\n" );
86    return (T *)memalign( alignment, sizeof(T) );
87} // memalign
88
89forall( type T ) int posix_memalign( T ** ptr, size_t alignment ) {
90        printf( "posix_memalign\n" );
91    return posix_memalign( (void **)ptr, alignment, sizeof(T) );
92} // posix_memalign
93
94//---------------------------------------
95
96int ato( const char * ptr ) {
97        int i;
98        if ( sscanf( ptr, "%d", &i ) == EOF ) {}                        // check return code
99        return i;
100}
101unsigned int ato( const char * ptr ) {
102        unsigned int ui;
103        if ( sscanf( ptr, "%u", &ui ) == EOF ) {}                       // check return code
104        return ui;
105}
106long int ato( const char * ptr ) {
107        long int li;
108        if ( sscanf( ptr, "%ld", &li ) == EOF ) {};                     // check return code
109        return li;
110}
111unsigned long int ato( const char * ptr ) {
112        unsigned long int uli;
113        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {};            // check return code
114        return uli;
115}
116long long int ato( const char * ptr ) {
117        long long int lli;
118        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {};           // check return code
119        return lli;
120}
121unsigned long long int ato( const char * ptr ) {
122        unsigned long long int ulli;
123        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {};          // check return code
124        return ulli;
125}
126float ato( const char * ptr ) {
127        float f;
128        if ( sscanf( ptr, "%f", &f ) == EOF ) {};                       // check return code
129        return f;
130}
131double ato( const char * ptr ) {
132        double d;
133        if ( sscanf( ptr, "%lf", &d ) == EOF ) {};                      // check return code
134        return d;
135}
136long double ato( const char * ptr ) {
137        long double ld;
138        printf( "FRED " );
139        if ( sscanf( ptr, "%.32Lf", &ld ) == EOF ) {};          // check return code
140        return ld;
141}
142float _Complex ato( const char * ptr ) {
143        float re, im;
144        if ( sscanf( ptr, "%g%g", &re, &im ) == EOF ) {};       // check return code
145        return re + im * _Complex_I;
146}
147double _Complex ato( const char * ptr ) {
148        double re, im;
149        if ( sscanf( ptr, "%.16lg%.16lg", &re, &im ) == EOF ) {}; // check return code
150        return re + im * _Complex_I;
151}
152long double _Complex ato( const char * ptr ) {
153        long double re, im;
154        if ( sscanf( ptr, "%.32Lg%.32Lg", &re, &im ) == EOF ) {}; // check return code
155        return re + im * _Complex_I;
156}       
157
158int strto( const char * sptr, char ** eptr, int base ) {
159        return (int)strtol( sptr, eptr, base );
160}
161unsigned int strto( const char * sptr, char ** eptr, int base ) {
162        return (unsigned int)strtoul( sptr, eptr, base );
163}
164long int strto( const char * sptr, char ** eptr, int base ) {
165        return strtol( sptr, eptr, base );
166}
167unsigned long int strto( const char * sptr, char ** eptr, int base ) {
168        return strtoul( sptr, eptr, base );
169}
170long long int strto( const char * sptr, char ** eptr, int base ) {
171        return strtoll( sptr, eptr, base );
172}
173unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
174        return strtoull( sptr, eptr, base );
175}
176float strto( const char * sptr, char ** eptr ) {
177        return strtof( sptr, eptr );
178}
179double strto( const char * sptr, char ** eptr ) {
180        return strtod( sptr, eptr );
181}
182long double strto( const char * sptr, char ** eptr ) {
183        return strtold( sptr, eptr );
184}
185float _Complex strto( const char * sptr, char ** eptr ) {
186        float re, im;
187        re = strtof( sptr, eptr );
188        if ( sptr == *eptr ) return 0.0;
189        im = strtof( sptr, eptr );
190        if ( sptr == *eptr ) return 0.0;
191        return re + im * _Complex_I;
192}
193double _Complex strto( const char * sptr, char ** eptr ) {
194        double re, im;
195        re = strtod( sptr, eptr );
196        if ( sptr == *eptr ) return 0.0;
197        im = strtod( sptr, eptr );
198        if ( sptr == *eptr ) return 0.0;
199        return re + im * _Complex_I;
200}
201long double _Complex strto( const char * sptr, char ** eptr ) {
202        long double re, im;
203        re = strtold( sptr, eptr );
204        if ( sptr == *eptr ) return 0.0;
205        im = strtold( sptr, eptr );
206        if ( sptr == *eptr ) return 0.0;
207        return re + im * _Complex_I;
208}
209
210//---------------------------------------
211
212forall( type T | { int ?<?( T, T ); } )
213T * bsearch( const T key, const T * arr, size_t dimension ) {
214        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
215        return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
216} // bsearch
217
218forall( type T | { int ?<?( T, T ); } )
219void qsort( const T * arr, size_t dimension ) {
220        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
221        qsort( arr, dimension, sizeof(T), comp );
222} // qsort
223
224//---------------------------------------
225
226forall( type T | { T ?/?( T, T ); T ?%?( T, T ); } )
227[ T, T ] div( T t1, T t2 ) { /* return [ t1 / t2, t1 % t2 ]; */ }
228
229//---------------------------------------
230
231char abs( char v ) { return abs( (int)v ); }
232long int abs( long int v ) { return labs( v ); }
233long long int abs( long long int v ) { return llabs( v ); }
234float abs( float v ) { return fabsf( v ); }
235double abs( double v ) { return fabs( v ); }
236long double abs( long double v ) { return fabsl( v ); }
237float _Complex abs( float _Complex v ) { return cabsf( v ); }
238double _Complex abs( double _Complex v ) { return cabs( v ); }
239long double _Complex abs( long double _Complex v ) { return cabsl( v ); }
240
241//---------------------------------------
242
243void randseed( long int s ) { srand48( s ); }
244char random() { return lrand48(); }
245int random() { return mrand48(); }
246unsigned int random() { return lrand48(); }
247long int random() { return mrand48(); }
248unsigned long int random() { return lrand48(); }
249float random() { return (float)drand48(); }                             // otherwise float uses lrand48
250double random() { return drand48(); }
251float _Complex random() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
252double _Complex random() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
253long double _Complex random() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
254
255//---------------------------------------
256
257forall( type T | { int ?<?( T, T ); } )
258T min( const T t1, const T t2 ) {
259        return t1 < t2 ? t1 : t2;
260} // min
261
262forall( type T | { int ?>?( T, T ); } )
263T max( const T t1, const T t2 ) {
264        return t1 > t2 ? t1 : t2;
265} // max
266
267forall( type T )
268void swap( T * t1, T * t2 ) {
269        T temp = *t1;
270        *t1 = *t2;
271        *t2 = temp;
272} // swap
273
274// Local Variables: //
275// tab-width: 4 //
276// End: //
Note: See TracBrowser for help on using the repository browser.