source: src/libcfa/stdlib @ 3ce0d440

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 3ce0d440 was 3ce0d440, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

more push/pop updates

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