source: src/libcfa/stdlib.c @ 1267fb1

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 1267fb1 was 627f585, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

moved new and delete to stdlib

  • Property mode set to 100644
File size: 8.2 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
[a797e2b1]12// Last Modified On : Wed Jul  6 14:28:57 2016
13// Update Count     : 169
[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
23#include <string.h>                                                                             // memset
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
[4040425]29forall( otype T ) T * malloc( void ) {
[3849857]30        //printf( "malloc1\n" );
[bd85400]31    return (T *)malloc( sizeof(T) );
32} // malloc
[4040425]33forall( otype T ) T * malloc( char fill ) {
[3849857]34        //printf( "malloc3\n" );
[bd85400]35        T * ptr = (T *)malloc( sizeof(T) );
[6b6597c]36    return memset( ptr, (int)fill, sizeof(T) );
[bd85400]37} // malloc
38
[45161b4d]39forall( otype T ) T * calloc( size_t nmemb ) {
[3849857]40        //printf( "calloc\n" );
[45161b4d]41    return (T *)calloc( nmemb, sizeof(T) );
[bd85400]42} // calloc
43
[4040425]44forall( otype T ) T * realloc( T * ptr, size_t size ) {
[3849857]45        //printf( "realloc1\n" );
[bd85400]46    return (T *)(void *)realloc( (void *)ptr, size );
47} // realloc
[4040425]48forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
[3849857]49        //printf( "realloc2\n" );
[bd85400]50    char * nptr = (T *)(void *)realloc( (void *)ptr, size );
51    size_t unused = malloc_usable_size( nptr );
52    memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
53    return nptr;
54} // realloc
55
[4040425]56forall( otype T ) T * malloc( T * ptr, size_t size ) {
[3849857]57        //printf( "malloc4\n" );
[bd85400]58    return (T *)realloc( ptr, size );
59} // malloc
[4040425]60forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
[3849857]61        //printf( "malloc5\n" );
[bd85400]62    return (T *)realloc( ptr, size, fill );
63} // malloc
64
[4040425]65forall( otype T ) T * aligned_alloc( size_t alignment ) {
[3849857]66        //printf( "aligned_alloc\n" );
[bd85400]67    return (T *)memalign( alignment, sizeof(T) );
68} // aligned_alloc
69
[4040425]70forall( otype T ) T * memalign( size_t alignment ) {
[3849857]71        //printf( "memalign\n" );
[bd85400]72    return (T *)memalign( alignment, sizeof(T) );
73} // memalign
74
[4040425]75forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ) {
[3849857]76        //printf( "posix_memalign\n" );
[bd85400]77    return posix_memalign( (void **)ptr, alignment, sizeof(T) );
78} // posix_memalign
79
[627f585]80forall( otype T, ttype Params | { void ?{}(T *, Params); } )
81T * new( Params p ) {
82        return ((T*)malloc()){ p };
83}
84
85forall( dtype T | { void ^?{}(T *); } )
86void delete( T * ptr ) {
87  if ( ptr ) {
88    ^ptr{};
89    free( ptr );
90  }
91}
92
[bd85400]93//---------------------------------------
94
95int ato( const char * ptr ) {
96        int i;
[6e991d6]97        if ( sscanf( ptr, "%d", &i ) == EOF ) {}
[bd85400]98        return i;
99}
100unsigned int ato( const char * ptr ) {
101        unsigned int ui;
[6e991d6]102        if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
[bd85400]103        return ui;
104}
105long int ato( const char * ptr ) {
106        long int li;
[6e991d6]107        if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
[bd85400]108        return li;
109}
110unsigned long int ato( const char * ptr ) {
111        unsigned long int uli;
[6e991d6]112        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
[bd85400]113        return uli;
114}
115long long int ato( const char * ptr ) {
116        long long int lli;
[6e991d6]117        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
[bd85400]118        return lli;
119}
120unsigned long long int ato( const char * ptr ) {
121        unsigned long long int ulli;
[6e991d6]122        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
[bd85400]123        return ulli;
124}
[90c3b1c]125
[bd85400]126float ato( const char * ptr ) {
127        float f;
[6e991d6]128        if ( sscanf( ptr, "%f", &f ) == EOF ) {}
[bd85400]129        return f;
130}
131double ato( const char * ptr ) {
132        double d;
[6e991d6]133        if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
[bd85400]134        return d;
135}
136long double ato( const char * ptr ) {
137        long double ld;
[6e991d6]138        if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
[bd85400]139        return ld;
140}
[90c3b1c]141
[bd85400]142float _Complex ato( const char * ptr ) {
143        float re, im;
[6e991d6]144        if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
[bd85400]145        return re + im * _Complex_I;
146}
147double _Complex ato( const char * ptr ) {
148        double re, im;
[6e991d6]149        if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
[bd85400]150        return re + im * _Complex_I;
151}
152long double _Complex ato( const char * ptr ) {
153        long double re, im;
[6e991d6]154        if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
[bd85400]155        return re + im * _Complex_I;
[43385ca]156}
[bd85400]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}
[90c3b1c]176
[bd85400]177float strto( const char * sptr, char ** eptr ) {
178        return strtof( sptr, eptr );
179}
180double strto( const char * sptr, char ** eptr ) {
181        return strtod( sptr, eptr );
182}
183long double strto( const char * sptr, char ** eptr ) {
184        return strtold( sptr, eptr );
185}
[90c3b1c]186
[bd85400]187float _Complex strto( const char * sptr, char ** eptr ) {
188        float re, im;
189        re = strtof( sptr, eptr );
190        if ( sptr == *eptr ) return 0.0;
191        im = strtof( sptr, eptr );
192        if ( sptr == *eptr ) return 0.0;
193        return re + im * _Complex_I;
194}
195double _Complex strto( const char * sptr, char ** eptr ) {
196        double re, im;
197        re = strtod( sptr, eptr );
198        if ( sptr == *eptr ) return 0.0;
199        im = strtod( sptr, eptr );
200        if ( sptr == *eptr ) return 0.0;
201        return re + im * _Complex_I;
202}
203long double _Complex strto( const char * sptr, char ** eptr ) {
204        long double re, im;
205        re = strtold( sptr, eptr );
206        if ( sptr == *eptr ) return 0.0;
207        im = strtold( sptr, eptr );
208        if ( sptr == *eptr ) return 0.0;
209        return re + im * _Complex_I;
210}
211
212//---------------------------------------
213
[4040425]214forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]215T * bsearch( T key, const T * arr, size_t dimension ) {
[bd85400]216        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
217        return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
218} // bsearch
219
[4040425]220forall( otype T | { int ?<?( T, T ); } )
[bd85400]221void qsort( const T * arr, size_t dimension ) {
222        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
223        qsort( arr, dimension, sizeof(T), comp );
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
233char abs( char v ) { return abs( (int)v ); }
234long int abs( long int v ) { return labs( v ); }
235long 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 ); }
[3cfe27f]246char rand48() { return mrand48(); }
[0438091]247int rand48() { return mrand48(); }
248unsigned int rand48() { return lrand48(); }
249long int rand48() { return mrand48(); }
250unsigned long int rand48() { return lrand48(); }
251float rand48() { return (float)drand48(); }                             // otherwise float uses lrand48
252double rand48() { return drand48(); }
253float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
254double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
255long double _Complex rand48() { 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.