source: libcfa/src/stdlib.hfa @ 58b6d1b

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 58b6d1b was 58b6d1b, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed tests after headers change

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