source: src/libcfa/stdlib @ 3d2ae8d

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

remove USE_ISOC11 as handled by -std=gnu11

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