source: src/libcfa/stdlib.c@ dac593fd

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 dac593fd 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
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//
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
[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
80//---------------------------------------
81
82int ato( const char * ptr ) {
83 int i;
[6e991d6]84 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
[bd85400]85 return i;
86}
87unsigned int ato( const char * ptr ) {
88 unsigned int ui;
[6e991d6]89 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
[bd85400]90 return ui;
91}
92long int ato( const char * ptr ) {
93 long int li;
[6e991d6]94 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
[bd85400]95 return li;
96}
97unsigned long int ato( const char * ptr ) {
98 unsigned long int uli;
[6e991d6]99 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
[bd85400]100 return uli;
101}
102long long int ato( const char * ptr ) {
103 long long int lli;
[6e991d6]104 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
[bd85400]105 return lli;
106}
107unsigned long long int ato( const char * ptr ) {
108 unsigned long long int ulli;
[6e991d6]109 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
[bd85400]110 return ulli;
111}
[90c3b1c]112
[bd85400]113float ato( const char * ptr ) {
114 float f;
[6e991d6]115 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
[bd85400]116 return f;
117}
118double ato( const char * ptr ) {
119 double d;
[6e991d6]120 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
[bd85400]121 return d;
122}
123long double ato( const char * ptr ) {
124 long double ld;
[6e991d6]125 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
[bd85400]126 return ld;
127}
[90c3b1c]128
[bd85400]129float _Complex ato( const char * ptr ) {
130 float re, im;
[6e991d6]131 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
[bd85400]132 return re + im * _Complex_I;
133}
134double _Complex ato( const char * ptr ) {
135 double re, im;
[6e991d6]136 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
[bd85400]137 return re + im * _Complex_I;
138}
139long double _Complex ato( const char * ptr ) {
140 long double re, im;
[6e991d6]141 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
[bd85400]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}
[90c3b1c]163
[bd85400]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}
[90c3b1c]173
[bd85400]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
[4040425]201forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]202T * bsearch( T key, const T * arr, size_t dimension ) {
[bd85400]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
[4040425]207forall( otype T | { int ?<?( T, T ); } )
[bd85400]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
[8bc4ef8]215// forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
216// [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
[bd85400]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 ); }
[6e991d6]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 ); }
[53ba273]229
230//---------------------------------------
231
[0438091]232void rand48seed( long int s ) { srand48( s ); }
[3cfe27f]233char rand48() { return mrand48(); }
[0438091]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); }
[bd85400]243
244//---------------------------------------
245
[4040425]246forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]247T min( T t1, T t2 ) {
[bd85400]248 return t1 < t2 ? t1 : t2;
249} // min
250
[4040425]251forall( otype T | { int ?>?( T, T ); } )
[a797e2b1]252T max( T t1, T t2 ) {
[bd85400]253 return t1 > t2 ? t1 : t2;
254} // max
255
[a9f2c13]256forall( otype T | { T min( T, T ); T max( T, T ); } )
[a797e2b1]257T clamp( T value, T min_val, T max_val ) {
[a9f2c13]258 return max( min_val, min( value, max_val ) );
259} // clamp
260
[4040425]261forall( otype T )
[bd85400]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.