source: libcfa/src/stdlib.hfa @ ff2a33e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ff2a33e was ffaedcd, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

clean up

  • Property mode set to 100644
File size: 9.5 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 Apr 24 17:35:43 2019
13// Update Count     : 352
14//
15
16#pragma once
17
18#include <stdlib.h>                                                                             // *alloc, strto*, ato*
19extern "C" {
20        void * memalign( size_t align, size_t size );           // malloc.h
21        void * memset( void * dest, int fill, size_t size ); // string.h
22        void * memcpy( void * dest, const void * src, size_t size ); // string.h
23    void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA
24} // extern "C"
25
26//---------------------------------------
27
28#ifndef EXIT_FAILURE
29#define EXIT_FAILURE    1                                                               // failing exit status
30#define EXIT_SUCCESS    0                                                               // successful exit status
31#endif // ! EXIT_FAILURE
32
33//---------------------------------------
34
35static inline forall( dtype T | sized(T) ) {
36        // C dynamic allocation
37
38        T * malloc( void ) {
39                return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
40        } // malloc
41
42        T * calloc( size_t dim ) {
43                return (T *)(void *)calloc( dim, sizeof(T) );   // C calloc
44        } // calloc
45
46        T * realloc( T * ptr, size_t size ) {
47                return (T *)(void *)realloc( (void *)ptr, size );
48        } // realloc
49
50        T * memalign( size_t align ) {
51                return (T *)memalign( align, sizeof(T) );
52        } // memalign
53
54        T * aligned_alloc( size_t align ) {
55                return (T *)aligned_alloc( align, sizeof(T) );
56        } // aligned_alloc
57
58        int posix_memalign( T ** ptr, size_t align ) {
59                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
60        } // posix_memalign
61
62
63        // Cforall dynamic allocation
64
65        T * alloc( void ) {
66                return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
67        } // alloc
68
69        T * alloc( char fill ) {
70                T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
71                return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value
72        } // alloc
73
74        T * alloc( size_t dim ) {
75                return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
76        } // alloc
77
78        T * alloc( size_t dim, char fill ) {
79                T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C calloc
80                return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initialize with fill value
81        } // alloc
82
83        T * alloc( T ptr[], size_t dim ) {
84                return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
85        } // alloc
86} // distribution
87
88
89forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
90
91
92static inline forall( dtype T | sized(T) ) {
93        T * align_alloc( size_t align ) {
94                return (T *)memalign( align, sizeof(T) );
95        } // align_alloc
96
97        T * align_alloc( size_t align, char fill ) {
98                T * ptr = (T *)memalign( align, sizeof(T) );
99                return (T *)memset( ptr, (int)fill, sizeof(T) );
100        } // align_alloc
101
102        T * align_alloc( size_t align, size_t dim ) {
103                return (T *)memalign( align, dim * sizeof(T) );
104        } // align_alloc
105
106        T * align_alloc( size_t align, size_t dim, char fill ) {
107                T * ptr;
108                if ( fill == '\0' ) {
109                        ptr = (T *)cmemalign( align, dim, sizeof(T) );
110                } else {
111                        ptr = (T *)memalign( align, dim * sizeof(T) );
112                        return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
113                } // if
114                return ptr;
115        } // align_alloc
116} // distribution
117
118
119static inline forall( dtype T | sized(T) ) {
120        // data, non-array types
121
122        T * memset( T * dest, char fill ) {
123                return (T *)memset( dest, fill, sizeof(T) );
124        } // memset
125
126        T * memcpy( T * dest, const T * src ) {
127                return (T *)memcpy( dest, src, sizeof(T) );
128        } // memcpy
129} // distribution
130
131static inline forall( dtype T | sized(T) ) {
132        // data, array types
133
134        T * amemset( T dest[], char fill, size_t dim ) {
135                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
136        } // amemset
137
138        T * amemcpy( T dest[], const T src[], size_t dim ) {
139                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
140        } // amemcpy
141} // distribution
142
143// allocation/deallocation and constructor/destructor, non-array types
144forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
145forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
146forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
147
148// allocation/deallocation and constructor/destructor, array types
149forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
150forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
151forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
152
153//---------------------------------------
154
155static inline {
156        int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
157        unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
158        long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
159        unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
160        long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
161        unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
162
163        float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
164        double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
165        long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
166} // distribution
167
168float _Complex strto( const char * sptr, char ** eptr );
169double _Complex strto( const char * sptr, char ** eptr );
170long double _Complex strto( const char * sptr, char ** eptr );
171
172static inline {
173        int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
174        unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
175        long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
176        unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
177        long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
178        unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
179
180        float ato( const char * sptr ) { return strtof( sptr, 0 ); }
181        double ato( const char * sptr ) { return strtod( sptr, 0 ); }
182        long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
183
184        float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
185        double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
186        long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
187} // distribution
188
189//---------------------------------------
190
191forall( otype E | { int ?<?( E, E ); } ) {
192        E * bsearch( E key, const E * vals, size_t dim );
193        size_t bsearch( E key, const E * vals, size_t dim );
194        E * bsearchl( E key, const E * vals, size_t dim );
195        size_t bsearchl( E key, const E * vals, size_t dim );
196        E * bsearchu( E key, const E * vals, size_t dim );
197        size_t bsearchu( E key, const E * vals, size_t dim );
198} // distribution
199
200forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
201        E * bsearch( K key, const E * vals, size_t dim );
202        size_t bsearch( K key, const E * vals, size_t dim );
203        E * bsearchl( K key, const E * vals, size_t dim );
204        size_t bsearchl( K key, const E * vals, size_t dim );
205        E * bsearchu( K key, const E * vals, size_t dim );
206        size_t bsearchu( K key, const E * vals, size_t dim );
207} // distribution
208
209forall( otype E | { int ?<?( E, E ); } ) {
210        void qsort( E * vals, size_t dim );
211} // distribution
212
213//---------------------------------------
214
215extern "C" {                                                                                    // override C version
216        void srandom( unsigned int seed );
217        long int random( void );
218} // extern "C"
219
220static inline {
221        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
222        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
223        unsigned long int random( void ) { return lrand48(); }
224        unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
225        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
226
227        char random( void ) { return (unsigned long int)random(); }
228        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
229        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
230        int random( void ) { return (long int)random(); }
231        int random( int u ) { return random( (long int)u ); } // [0,u]
232        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
233        unsigned int random( void ) { return (unsigned long int)random(); }
234        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
235        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
236} // distribution
237
238float random( void );                                                                   // [0.0, 1.0)
239double random( void );                                                                  // [0.0, 1.0)
240float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
241double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
242long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
243
244//---------------------------------------
245
246#include "common.hfa"
247
248// Local Variables: //
249// mode: c //
250// tab-width: 4 //
251// End: //
Note: See TracBrowser for help on using the repository browser.