source: src/libcfa/stdlib.c@ 37f0da8

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 37f0da8 was 45161b4d, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

generate implicit typedef right after sue name appears, further fixes storage allocation routines and comments

  • Property mode set to 100644
File size: 8.8 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 Apr 13 14:49:58 2016
13// Update Count : 155
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( otype T ) T * malloc( void ) {
30 //printf( "malloc1\n" );
31 return (T *)malloc( sizeof(T) );
32} // malloc
33forall( otype T ) T * malloc( char fill ) {
34 //printf( "malloc3\n" );
35 T * ptr = (T *)malloc( sizeof(T) );
36 return memset( ptr );
37} // malloc
38
39forall( otype T ) T * calloc( size_t nmemb ) {
40 //printf( "calloc\n" );
41 return (T *)calloc( nmemb, sizeof(T) );
42} // calloc
43
44forall( otype T ) T * realloc( T * ptr, size_t size ) {
45 //printf( "realloc1\n" );
46 return (T *)(void *)realloc( (void *)ptr, size );
47} // realloc
48forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
49 //printf( "realloc2\n" );
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
56forall( otype T ) T * malloc( T * ptr, size_t size ) {
57 //printf( "malloc4\n" );
58 return (T *)realloc( ptr, size );
59} // malloc
60forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
61 //printf( "malloc5\n" );
62 return (T *)realloc( ptr, size, fill );
63} // malloc
64
65forall( otype T ) T * aligned_alloc( size_t alignment ) {
66 //printf( "aligned_alloc\n" );
67 return (T *)memalign( alignment, sizeof(T) );
68} // aligned_alloc
69
70forall( otype T ) T * memalign( size_t alignment ) {
71 //printf( "memalign\n" );
72 return (T *)memalign( alignment, sizeof(T) );
73} // memalign
74
75forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ) {
76 //printf( "posix_memalign\n" );
77 return posix_memalign( (void **)ptr, alignment, sizeof(T) );
78} // posix_memalign
79
80forall( otype T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill
81 //printf( "memset1\n" );
82 return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) );
83} // memset
84forall( otype T ) T * memset( T * ptr ) { // remove when default value available
85 //printf( "memset2\n" );
86 return (T *)memset( ptr, 0, malloc_usable_size( ptr ) );
87} // memset
88
89//---------------------------------------
90
91int ato( const char * ptr ) {
92 int i;
93 if ( sscanf( ptr, "%d", &i ) == EOF ) {} // check return code
94 return i;
95}
96unsigned int ato( const char * ptr ) {
97 unsigned int ui;
98 if ( sscanf( ptr, "%u", &ui ) == EOF ) {} // check return code
99 return ui;
100}
101long int ato( const char * ptr ) {
102 long int li;
103 if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code
104 return li;
105}
106unsigned long int ato( const char * ptr ) {
107 unsigned long int uli;
108 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code
109 return uli;
110}
111long long int ato( const char * ptr ) {
112 long long int lli;
113 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code
114 return lli;
115}
116unsigned long long int ato( const char * ptr ) {
117 unsigned long long int ulli;
118 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code
119 return ulli;
120}
121
122float ato( const char * ptr ) {
123 float f;
124 if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code
125 return f;
126}
127double ato( const char * ptr ) {
128 double d;
129 if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code
130 return d;
131}
132long double ato( const char * ptr ) {
133 long double ld;
134 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code
135 return ld;
136}
137
138float _Complex ato( const char * ptr ) {
139 float re, im;
140 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} // check return code
141 return re + im * _Complex_I;
142}
143double _Complex ato( const char * ptr ) {
144 double re, im;
145 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code
146 return re + im * _Complex_I;
147}
148long double _Complex ato( const char * ptr ) {
149 long double re, im;
150 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} // check return code
151 return re + im * _Complex_I;
152}
153
154int strto( const char * sptr, char ** eptr, int base ) {
155 return (int)strtol( sptr, eptr, base );
156}
157unsigned int strto( const char * sptr, char ** eptr, int base ) {
158 return (unsigned int)strtoul( sptr, eptr, base );
159}
160long int strto( const char * sptr, char ** eptr, int base ) {
161 return strtol( sptr, eptr, base );
162}
163unsigned long int strto( const char * sptr, char ** eptr, int base ) {
164 return strtoul( sptr, eptr, base );
165}
166long long int strto( const char * sptr, char ** eptr, int base ) {
167 return strtoll( sptr, eptr, base );
168}
169unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
170 return strtoull( sptr, eptr, base );
171}
172
173float strto( const char * sptr, char ** eptr ) {
174 return strtof( sptr, eptr );
175}
176double strto( const char * sptr, char ** eptr ) {
177 return strtod( sptr, eptr );
178}
179long double strto( const char * sptr, char ** eptr ) {
180 return strtold( sptr, eptr );
181}
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( otype 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( otype 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( otype 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
241float floor( float v ) { return floorf( v ); }
242long double floor( long double v ) { return floorl( v ); }
243
244float ceil( float v ) { return ceilf( v ); }
245long double ceil( long double v ) { return ceill( v ); }
246
247//---------------------------------------
248
249void rand48seed( long int s ) { srand48( s ); }
250char rand48() { return mrand48(); }
251int rand48() { return mrand48(); }
252unsigned int rand48() { return lrand48(); }
253long int rand48() { return mrand48(); }
254unsigned long int rand48() { return lrand48(); }
255float rand48() { return (float)drand48(); } // otherwise float uses lrand48
256double rand48() { return drand48(); }
257float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
258double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
259long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
260
261//---------------------------------------
262
263forall( otype T | { int ?<?( T, T ); } )
264T min( const T t1, const T t2 ) {
265 return t1 < t2 ? t1 : t2;
266} // min
267
268forall( otype T | { int ?>?( T, T ); } )
269T max( const T t1, const T t2 ) {
270 return t1 > t2 ? t1 : t2;
271} // max
272
273forall( otype T )
274void swap( T * t1, T * t2 ) {
275 T temp = *t1;
276 *t1 = *t2;
277 *t2 = temp;
278} // swap
279
280// Local Variables: //
281// tab-width: 4 //
282// End: //
Note: See TracBrowser for help on using the repository browser.