source: src/libcfa/stdlib.c@ 911348cd

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

remove const from parameters to remove warnings from incorrectly generated code

  • Property mode set to 100644
File size: 8.0 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 Jul 6 14:28:57 2016
13// Update Count : 169
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
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, (int)fill, sizeof(T) );
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
80//---------------------------------------
81
82int ato( const char * ptr ) {
83 int i;
84 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
85 return i;
86}
87unsigned int ato( const char * ptr ) {
88 unsigned int ui;
89 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
90 return ui;
91}
92long int ato( const char * ptr ) {
93 long int li;
94 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
95 return li;
96}
97unsigned long int ato( const char * ptr ) {
98 unsigned long int uli;
99 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
100 return uli;
101}
102long long int ato( const char * ptr ) {
103 long long int lli;
104 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
105 return lli;
106}
107unsigned long long int ato( const char * ptr ) {
108 unsigned long long int ulli;
109 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
110 return ulli;
111}
112
113float ato( const char * ptr ) {
114 float f;
115 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
116 return f;
117}
118double ato( const char * ptr ) {
119 double d;
120 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
121 return d;
122}
123long double ato( const char * ptr ) {
124 long double ld;
125 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
126 return ld;
127}
128
129float _Complex ato( const char * ptr ) {
130 float re, im;
131 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
132 return re + im * _Complex_I;
133}
134double _Complex ato( const char * ptr ) {
135 double re, im;
136 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
137 return re + im * _Complex_I;
138}
139long double _Complex ato( const char * ptr ) {
140 long double re, im;
141 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
142 return re + im * _Complex_I;
143}
144
145int strto( const char * sptr, char ** eptr, int base ) {
146 return (int)strtol( sptr, eptr, base );
147}
148unsigned int strto( const char * sptr, char ** eptr, int base ) {
149 return (unsigned int)strtoul( sptr, eptr, base );
150}
151long int strto( const char * sptr, char ** eptr, int base ) {
152 return strtol( sptr, eptr, base );
153}
154unsigned long int strto( const char * sptr, char ** eptr, int base ) {
155 return strtoul( sptr, eptr, base );
156}
157long long int strto( const char * sptr, char ** eptr, int base ) {
158 return strtoll( sptr, eptr, base );
159}
160unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
161 return strtoull( sptr, eptr, base );
162}
163
164float strto( const char * sptr, char ** eptr ) {
165 return strtof( sptr, eptr );
166}
167double strto( const char * sptr, char ** eptr ) {
168 return strtod( sptr, eptr );
169}
170long double strto( const char * sptr, char ** eptr ) {
171 return strtold( sptr, eptr );
172}
173
174float _Complex strto( const char * sptr, char ** eptr ) {
175 float re, im;
176 re = strtof( sptr, eptr );
177 if ( sptr == *eptr ) return 0.0;
178 im = strtof( sptr, eptr );
179 if ( sptr == *eptr ) return 0.0;
180 return re + im * _Complex_I;
181}
182double _Complex strto( const char * sptr, char ** eptr ) {
183 double re, im;
184 re = strtod( sptr, eptr );
185 if ( sptr == *eptr ) return 0.0;
186 im = strtod( sptr, eptr );
187 if ( sptr == *eptr ) return 0.0;
188 return re + im * _Complex_I;
189}
190long double _Complex strto( const char * sptr, char ** eptr ) {
191 long double re, im;
192 re = strtold( sptr, eptr );
193 if ( sptr == *eptr ) return 0.0;
194 im = strtold( sptr, eptr );
195 if ( sptr == *eptr ) return 0.0;
196 return re + im * _Complex_I;
197}
198
199//---------------------------------------
200
201forall( otype T | { int ?<?( T, T ); } )
202T * bsearch( T key, const T * arr, size_t dimension ) {
203 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
204 return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
205} // bsearch
206
207forall( otype T | { int ?<?( T, T ); } )
208void qsort( const T * arr, size_t dimension ) {
209 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
210 qsort( arr, dimension, sizeof(T), comp );
211} // qsort
212
213//---------------------------------------
214
215// forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
216// [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
217
218//---------------------------------------
219
220char abs( char v ) { return abs( (int)v ); }
221long int abs( long int v ) { return labs( v ); }
222long long int abs( long long int v ) { return llabs( v ); }
223float abs( float x ) { return fabsf( x ); }
224double abs( double x ) { return fabs( x ); }
225long double abs( long double x ) { return fabsl( x ); }
226float abs( float _Complex x ) { return cabsf( x ); }
227double abs( double _Complex x ) { return cabs( x ); }
228long double abs( long double _Complex x ) { return cabsl( x ); }
229
230//---------------------------------------
231
232void rand48seed( long int s ) { srand48( s ); }
233char rand48() { return mrand48(); }
234int rand48() { return mrand48(); }
235unsigned int rand48() { return lrand48(); }
236long int rand48() { return mrand48(); }
237unsigned long int rand48() { return lrand48(); }
238float rand48() { return (float)drand48(); } // otherwise float uses lrand48
239double rand48() { return drand48(); }
240float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
241double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
242long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
243
244//---------------------------------------
245
246forall( otype T | { int ?<?( T, T ); } )
247T min( T t1, T t2 ) {
248 return t1 < t2 ? t1 : t2;
249} // min
250
251forall( otype T | { int ?>?( T, T ); } )
252T max( T t1, T t2 ) {
253 return t1 > t2 ? t1 : t2;
254} // max
255
256forall( otype T | { T min( T, T ); T max( T, T ); } )
257T clamp( T value, T min_val, T max_val ) {
258 return max( min_val, min( value, max_val ) );
259} // clamp
260
261forall( otype T )
262void swap( T * t1, T * t2 ) {
263 T temp = *t1;
264 *t1 = *t2;
265 *t2 = temp;
266} // swap
267
268// Local Variables: //
269// tab-width: 4 //
270// End: //
Note: See TracBrowser for help on using the repository browser.