source: src/libcfa/stdlib.c @ a9a259c

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

syslib updates and examples, formatting, make double 0 work

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