source: src/libcfa/stdlib @ 1dbc8590

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 1dbc8590 was 74b19fb, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add qualifier distribution

  • Property mode set to 100644
File size: 11.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// stdlib --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun May 13 23:22:23 2018
13// Update Count     : 299
14//
15
16#pragma once
17
18#define __USE_ISOC11                                                                    // aligned_alloc
19#include <stdlib.h>                                                                             // strto*, *abs
20
21//---------------------------------------
22
23#ifndef EXIT_FAILURE
24#define EXIT_FAILURE    1                                                               // failing exit status
25#define EXIT_SUCCESS    0                                                               // successful exit status
26#endif // ! EXIT_FAILURE
27
28//---------------------------------------
29
30// C dynamic allocation
31static inline forall( dtype T | sized(T) ) {
32        T * malloc( void ) {
33                // printf( "* malloc\n" );
34                return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
35        } // malloc
36
37        // T & malloc( void ) {
38        //      int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
39        //      printf( "& malloc %p\n", &p );
40        //      return p;
41        //      //      return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
42        // } // malloc
43
44        T * calloc( size_t dim ) {
45                //printf( "X2\n" );
46                return (T *)(void *)calloc( dim, sizeof(T) );   // C calloc
47        } // calloc
48
49        T * realloc( T * ptr, size_t size ) {
50                //printf( "X3\n" );
51                return (T *)(void *)realloc( (void *)ptr, size );
52        } // realloc
53
54        extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
55        T * memalign( size_t align ) {
56                //printf( "X4\n" );
57                return (T *)memalign( align, sizeof(T) );
58        } // memalign
59
60        extern "C" { void * aligned_alloc( size_t align, size_t size ); } // use default C routine for void *
61        T * aligned_alloc( size_t align ) {
62                //printf( "X5\n" );
63                return (T *)aligned_alloc( align, sizeof(T) );
64        } // aligned_alloc
65
66        int posix_memalign( T ** ptr, size_t align ) {
67                //printf( "X6\n" );
68                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
69        } // posix_memalign
70
71
72        // Cforall dynamic allocation
73        extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
74
75        T * alloc( void ) {
76                //printf( "X7\n" );
77                return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
78        } // alloc
79
80        T * alloc( char fill ) {
81                //printf( "X8\n" );
82                T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
83                return (T *)memset( ptr, (int)fill, sizeof(T) );        // initial with fill value
84        } // alloc
85
86        T * alloc( size_t dim ) {
87                //printf( "X9\n" );
88                return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
89        } // alloc
90
91        T * alloc( size_t dim, char fill ) {
92                //printf( "X10\n" );
93                T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
94                return (T *)memset( ptr, (int)fill, dim * sizeof(T) );    // initial with fill value
95        } // alloc
96
97        T * alloc( T ptr[], size_t dim ) {
98                //printf( "X11\n" );
99                return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
100        } // alloc
101} // distribution
102
103
104forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
105
106static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
107        //printf( "X13\n" );
108        return (T *)memalign( align, sizeof(T) );
109} // align_alloc
110static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
111        //printf( "X14\n" );
112    T * ptr = (T *)memalign( align, sizeof(T) );
113    return (T *)memset( ptr, (int)fill, sizeof(T) );
114} // align_alloc
115
116static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
117        //printf( "X15\n" );
118        return (T *)memalign( align, dim * sizeof(T) );
119} // align_alloc
120static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
121        //printf( "X16\n" );
122    T * ptr = (T *)memalign( align, dim * sizeof(T) );
123    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
124} // align_alloc
125
126
127// data, non-array types
128static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
129        //printf( "X17\n" );
130        return (T *)memset( dest, c, sizeof(T) );
131} // memset
132extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
133static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
134        //printf( "X18\n" );
135        return (T *)memcpy( dest, src, sizeof(T) );
136} // memcpy
137
138// data, array types
139static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
140        //printf( "X19\n" );
141        return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
142} // memset
143static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
144        //printf( "X20\n" );
145        return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
146} // memcpy
147
148// allocation/deallocation and constructor/destructor, non-array types
149forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
150forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
151forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
152
153// allocation/deallocation and constructor/destructor, array types
154forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
155forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
156forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
157
158//---------------------------------------
159
160static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
161static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
162static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
163static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
164static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
165static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
166
167static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
168static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
169static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
170
171float _Complex strto( const char * sptr, char ** eptr );
172double _Complex strto( const char * sptr, char ** eptr );
173long double _Complex strto( const char * sptr, char ** eptr );
174
175static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
176static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
177static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
178static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
179static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
180static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
181
182static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
183static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
184static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
185
186static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
187static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
188static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
189
190//---------------------------------------
191
192forall( otype E | { int ?<?( E, E ); } )
193E * bsearch( E key, const E * vals, size_t dim );
194
195forall( otype E | { int ?<?( E, E ); } )
196size_t bsearch( E key, const E * vals, size_t dim );
197
198forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
199E * bsearch( K key, const E * vals, size_t dim );
200
201forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
202size_t bsearch( K key, const E * vals, size_t dim );
203
204
205forall( otype E | { int ?<?( E, E ); } )
206E * bsearchl( E key, const E * vals, size_t dim );
207
208forall( otype E | { int ?<?( E, E ); } )
209size_t bsearchl( E key, const E * vals, size_t dim );
210
211forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
212E * bsearchl( K key, const E * vals, size_t dim );
213
214forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
215size_t bsearchl( K key, const E * vals, size_t dim );
216
217
218forall( otype E | { int ?<?( E, E ); } )
219E * bsearchu( E key, const E * vals, size_t dim );
220
221forall( otype E | { int ?<?( E, E ); } )
222size_t bsearchu( E key, const E * vals, size_t dim );
223
224forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
225E * bsearchu( K key, const E * vals, size_t dim );
226
227forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
228size_t bsearchu( K key, const E * vals, size_t dim );
229
230
231forall( otype E | { int ?<?( E, E ); } )
232void qsort( E * vals, size_t dim );
233
234//---------------------------------------
235
236[ int, int ] div( int num, int denom );
237[ long int, long int ] div( long int num, long int denom );
238[ long long int, long long int ] div( long long int num, long long int denom );
239forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
240[ T, T ] div( T num, T demon );
241
242//---------------------------------------
243
244static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
245extern "C" { int abs( int ); }                                                  // use default C routine for int
246static inline unsigned long int abs( long int v ) { return labs( v ); }
247static inline unsigned long long int abs( long long int v ) { return llabs( v ); }
248
249extern "C" {
250double fabs( double );
251float fabsf( float );
252long double fabsl( long double );
253} // extern "C"
254static inline float abs( float x ) { return fabsf( x ); }
255static inline double abs( double x ) { return fabs( x ); }
256static inline long double abs( long double x ) { return fabsl( x ); }
257
258extern "C" {
259double cabs( double _Complex );
260float cabsf( float _Complex );
261long double cabsl( long double _Complex );
262} // extern "C"
263static inline float abs( float _Complex x ) { return cabsf( x ); }
264static inline double abs( double _Complex x ) { return cabs( x ); }
265static inline long double abs( long double _Complex x ) { return cabsl( x ); }
266
267forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
268T abs( T );
269
270//---------------------------------------
271
272extern "C" { void srandom( unsigned int seed ); }               // override C version
273char random( void );
274char random( char u );
275char random( char l, char u );
276int random( void );
277int random( int u );
278int random( int l, int u );
279unsigned int random( void );
280unsigned int random( unsigned int u );
281unsigned int random( unsigned int l, unsigned int u );
282extern "C" { long int random( void ); }                                 // override C version
283long int random( long int u );
284long int random( long int l, long int u );
285unsigned long int random( void );
286unsigned long int random( unsigned long int u );
287unsigned long int random( unsigned long int l, unsigned long int u );
288float random( void );
289double random( void );
290float _Complex random( void );
291double _Complex random( void );
292long double _Complex random( void );
293
294//---------------------------------------
295
296forall( otype T | { int ?<?( T, T ); } )
297static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
298
299forall( otype T | { int ?>?( T, T ); } )
300static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
301
302forall( otype T | { T min( T, T ); T max( T, T ); } )
303static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
304
305forall( otype T )
306static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
307
308// Local Variables: //
309// mode: c //
310// tab-width: 4 //
311// End: //
Note: See TracBrowser for help on using the repository browser.