source: src/libcfa/stdlib.c@ 13099105

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 13099105 was f3ddc21, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add polymorphic abs routine and code formatting

  • Property mode set to 100644
File size: 9.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 : Tue May 9 08:43:00 2017
13// Update Count : 191
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( dtype T | sized(T) ) T * malloc( void ) { // type-safe
30 return (T *)(void *)malloc( (size_t)sizeof(T) );
31} // malloc
32
33forall( dtype T | sized(T) ) T * malloc( char fill ) { // initial with fill value (like calloc)
34 T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
35 return memset( ptr, (int)fill, sizeof(T) );
36} // malloc
37
38forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
39 return (T *)realloc( ptr, size );
40} // malloc
41
42forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
43 return (T *)realloc( ptr, size, fill );
44} // malloc
45
46
47forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
48 return (T *)calloc( nmemb, sizeof(T) );
49} // calloc
50
51
52forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
53 return (T *)(void *)realloc( (void *)ptr, size );
54} // realloc
55
56forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
57 char * nptr = (T *)(void *)realloc( (void *)ptr, size );
58 size_t unused = malloc_usable_size( nptr );
59 memset( nptr + size - unused, (int)fill, unused ); // initialize any new storage
60 return nptr;
61} // realloc
62
63
64forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
65 return (T *)memalign( alignment, sizeof(T) );
66} // aligned_alloc
67
68forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
69 return (T *)memalign( alignment, sizeof(T) );
70} // memalign
71
72forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
73 return posix_memalign( (void **)ptr, alignment, sizeof(T) );
74} // posix_memalign
75
76
77forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) // new
78T * new( Params p ) {
79 return ((T *)malloc()){ p };
80} // new
81
82forall( dtype T | { void ^?{}(T *); } ) // delete
83void delete( T * ptr ) {
84 if ( ptr ) {
85 ^ptr{};
86 free( ptr );
87 }
88} // delete
89
90forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
91void delete( T * ptr, Params rest ) {
92 if ( ptr ) {
93 ^ptr{};
94 free( ptr );
95 }
96 delete( rest );
97} // delete
98
99//---------------------------------------
100
101int ato( const char * ptr ) {
102 int i;
103 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
104 return i;
105} // ato
106
107unsigned int ato( const char * ptr ) {
108 unsigned int ui;
109 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
110 return ui;
111} // ato
112
113long int ato( const char * ptr ) {
114 long int li;
115 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
116 return li;
117} // ato
118
119unsigned long int ato( const char * ptr ) {
120 unsigned long int uli;
121 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
122 return uli;
123} // ato
124
125long long int ato( const char * ptr ) {
126 long long int lli;
127 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
128 return lli;
129} // ato
130
131unsigned long long int ato( const char * ptr ) {
132 unsigned long long int ulli;
133 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
134 return ulli;
135} // ato
136
137
138float ato( const char * ptr ) {
139 float f;
140 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
141 return f;
142} // ato
143
144double ato( const char * ptr ) {
145 double d;
146 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
147 return d;
148} // ato
149
150long double ato( const char * ptr ) {
151 long double ld;
152 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
153 return ld;
154} // ato
155
156
157float _Complex ato( const char * ptr ) {
158 float re, im;
159 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
160 return re + im * _Complex_I;
161} // ato
162
163double _Complex ato( const char * ptr ) {
164 double re, im;
165 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
166 return re + im * _Complex_I;
167} // ato
168
169long double _Complex ato( const char * ptr ) {
170 long double re, im;
171 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
172 return re + im * _Complex_I;
173} // ato
174
175
176int strto( const char * sptr, char ** eptr, int base ) {
177 return (int)strtol( sptr, eptr, base );
178} // strto
179
180unsigned int strto( const char * sptr, char ** eptr, int base ) {
181 return (unsigned int)strtoul( sptr, eptr, base );
182} // strto
183
184long int strto( const char * sptr, char ** eptr, int base ) {
185 return strtol( sptr, eptr, base );
186} // strto
187
188unsigned long int strto( const char * sptr, char ** eptr, int base ) {
189 return strtoul( sptr, eptr, base );
190} // strto
191
192long long int strto( const char * sptr, char ** eptr, int base ) {
193 return strtoll( sptr, eptr, base );
194} // strto
195
196unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
197 return strtoull( sptr, eptr, base );
198} // strto
199
200
201float strto( const char * sptr, char ** eptr ) {
202 return strtof( sptr, eptr );
203} // strto
204
205double strto( const char * sptr, char ** eptr ) {
206 return strtod( sptr, eptr );
207} // strto
208
209long double strto( const char * sptr, char ** eptr ) {
210 return strtold( sptr, eptr );
211} // strto
212
213
214float _Complex strto( const char * sptr, char ** eptr ) {
215 float re, im;
216 re = strtof( sptr, eptr );
217 if ( sptr == *eptr ) return 0.0;
218 im = strtof( sptr, eptr );
219 if ( sptr == *eptr ) return 0.0;
220 return re + im * _Complex_I;
221} // strto
222
223double _Complex strto( const char * sptr, char ** eptr ) {
224 double re, im;
225 re = strtod( sptr, eptr );
226 if ( sptr == *eptr ) return 0.0;
227 im = strtod( sptr, eptr );
228 if ( sptr == *eptr ) return 0.0;
229 return re + im * _Complex_I;
230} // strto
231
232long double _Complex strto( const char * sptr, char ** eptr ) {
233 long double re, im;
234 re = strtold( sptr, eptr );
235 if ( sptr == *eptr ) return 0.0;
236 im = strtold( sptr, eptr );
237 if ( sptr == *eptr ) return 0.0;
238 return re + im * _Complex_I;
239} // strto
240
241//---------------------------------------
242
243forall( otype T | { int ?<?( T, T ); } )
244T * bsearch( T key, const T * arr, size_t dimension ) {
245 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
246 return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
247} // bsearch
248
249forall( otype T | { int ?<?( T, T ); } )
250unsigned int bsearch( T key, const T * arr, size_t dimension ) {
251 T *result = bsearch( key, arr, dimension );
252 return result ? result - arr : dimension; // pointer subtraction includes sizeof(T)
253} // bsearch
254
255forall( otype T | { int ?<?( T, T ); } )
256void qsort( const T * arr, size_t dimension ) {
257 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
258 qsort( arr, dimension, sizeof(T), comp );
259} // qsort
260
261//---------------------------------------
262
263forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
264[ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
265
266//---------------------------------------
267
268unsigned char abs( signed char v ) { return abs( (int)v ); }
269unsigned long int abs( long int v ) { return labs( v ); }
270unsigned long long int abs( long long int v ) { return llabs( v ); }
271float abs( float x ) { return fabsf( x ); }
272double abs( double x ) { return fabs( x ); }
273long double abs( long double x ) { return fabsl( x ); }
274float abs( float _Complex x ) { return cabsf( x ); }
275double abs( double _Complex x ) { return cabs( x ); }
276long double abs( long double _Complex x ) { return cabsl( x ); }
277
278//---------------------------------------
279
280void rand48seed( long int s ) { srand48( s ); }
281char rand48() { return mrand48(); }
282int rand48() { return mrand48(); }
283unsigned int rand48() { return lrand48(); }
284long int rand48() { return mrand48(); }
285unsigned long int rand48() { return lrand48(); }
286float rand48() { return (float)drand48(); } // otherwise float uses lrand48
287double rand48() { return drand48(); }
288float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
289double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
290long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
291
292//---------------------------------------
293
294forall( otype T | { int ?<?( T, T ); } )
295T min( T t1, T t2 ) {
296 return t1 < t2 ? t1 : t2;
297} // min
298
299forall( otype T | { int ?>?( T, T ); } )
300T max( T t1, T t2 ) {
301 return t1 > t2 ? t1 : t2;
302} // max
303
304forall( otype T | { T min( T, T ); T max( T, T ); } )
305T clamp( T value, T min_val, T max_val ) {
306 return max( min_val, min( value, max_val ) );
307} // clamp
308
309forall( otype T )
310void swap( T * t1, T * t2 ) {
311 T temp = *t1;
312 *t1 = *t2;
313 *t2 = temp;
314} // swap
315
316// Local Variables: //
317// tab-width: 4 //
318// End: //
Note: See TracBrowser for help on using the repository browser.