source: src/libcfa/stdlib.c @ bc03be3

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

inserted cast for random seed

  • Property mode set to 100644
File size: 10.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.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:10:29 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Jan  3 08:29:29 2018
13// Update Count     : 444
14//
15
16#include "stdlib"
17
18//---------------------------------------
19
20#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
21#include <string.h>                                                                             // memcpy, memset
22#include <malloc.h>                                                                             // malloc_usable_size
23#include <math.h>                                                                               // fabsf, fabs, fabsl
24#include <complex.h>                                                                    // _Complex_I
25#include <assert.h>
26
27//---------------------------------------
28
29// resize, non-array types
30forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
31        size_t olen = malloc_usable_size( ptr );                        // current allocation
32    char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
33        size_t nlen = malloc_usable_size( nptr );                       // new allocation
34        if ( nlen > olen ) {                                                            // larger ?
35                memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
36        } //
37    return (T *)nptr;
38} // alloc
39
40// allocation/deallocation and constructor/destructor, non-array types
41forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
42T * new( Params p ) {
43        return &(*malloc()){ p };                                                               // run constructor
44} // new
45
46forall( dtype T | sized(T) | { void ^?{}( T & ); } )
47void delete( T * ptr ) {
48        if ( ptr ) {                                                                            // ignore null
49                ^(*ptr){};                                                                                      // run destructor
50                free( ptr );
51        } // if
52} // delete
53
54forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
55void delete( T * ptr, Params rest ) {
56        if ( ptr ) {                                                                            // ignore null
57                ^(*ptr){};                                                                                      // run destructor
58                free( ptr );
59        } // if
60        delete( rest );
61} // delete
62
63
64// allocation/deallocation and constructor/destructor, array types
65forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
66T * anew( size_t dim, Params p ) {
67        T *arr = alloc( dim );
68        for ( unsigned int i = 0; i < dim; i += 1 ) {
69                (arr[i]){ p };                                                                  // run constructor
70        } // for
71        return arr;
72} // anew
73
74forall( dtype T | sized(T) | { void ^?{}( T & ); } )
75void adelete( size_t dim, T arr[] ) {
76        if ( arr ) {                                                                            // ignore null
77                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
78                        ^(arr[i]){};                                                            // run destructor
79                } // for
80                free( arr );
81        } // if
82} // adelete
83
84forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
85void adelete( size_t dim, T arr[], Params rest ) {
86        if ( arr ) {                                                                            // ignore null
87                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
88                        ^(arr[i]){};                                                            // run destructor
89                } // for
90                free( arr );
91        } // if
92        adelete( rest );
93} // adelete
94
95//---------------------------------------
96
97float _Complex strto( const char * sptr, char ** eptr ) {
98        float re, im;
99        char * eeptr;
100        re = strtof( sptr, &eeptr );
101        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
102        im = strtof( eeptr, &eeptr );
103        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
104        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
105        return re + im * _Complex_I;
106} // strto
107
108double _Complex strto( const char * sptr, char ** eptr ) {
109        double re, im;
110        char * eeptr;
111        re = strtod( sptr, &eeptr );
112        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
113        im = strtod( eeptr, &eeptr );
114        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
115        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
116        return re + im * _Complex_I;
117} // strto
118
119long double _Complex strto( const char * sptr, char ** eptr ) {
120        long double re, im;
121        char * eeptr;
122        re = strtold( sptr, &eeptr );
123        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
124        im = strtold( eeptr, &eeptr );
125        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
126        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
127        return re + im * _Complex_I;
128} // strto
129
130//---------------------------------------
131
132forall( otype E | { int ?<?( E, E ); } )
133E * bsearch( E key, const E * vals, size_t dim ) {
134        int cmp( const void * t1, const void * t2 ) {
135                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
136        } // cmp
137        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
138} // bsearch
139
140forall( otype E | { int ?<?( E, E ); } )
141size_t bsearch( E key, const E * vals, size_t dim ) {
142        E * result = bsearch( key, vals, dim );
143        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
144} // bsearch
145
146forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
147E * bsearch( K key, const E * vals, size_t dim ) {
148        int cmp( const void * t1, const void * t2 ) {
149                return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
150        } // cmp
151        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
152} // bsearch
153
154forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
155size_t bsearch( K key, const E * vals, size_t dim ) {
156        E * result = bsearch( key, vals, dim );
157        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
158} // bsearch
159
160
161forall( otype E | { int ?<?( E, E ); } )
162size_t bsearchl( E key, const E * vals, size_t dim ) {
163        size_t l = 0, m, h = dim;
164        while ( l < h ) {
165                m = (l + h) / 2;
166                if ( (E &)(vals[m]) < key ) {                                   // cast away const
167                        l = m + 1;
168                } else {
169                        h = m;
170                } // if
171        } // while
172        return l;
173} // bsearchl
174
175forall( otype E | { int ?<?( E, E ); } )
176E * bsearchl( E key, const E * vals, size_t dim ) {
177        size_t posn = bsearchl( key, vals, dim );
178        return (E *)(&vals[posn]);                                                      // cast away const
179} // bsearchl
180
181forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
182size_t bsearchl( K key, const E * vals, size_t dim ) {
183        size_t l = 0, m, h = dim;
184        while ( l < h ) {
185                m = (l + h) / 2;
186                if ( getKey( vals[m] ) < key ) {
187                        l = m + 1;
188                } else {
189                        h = m;
190                } // if
191        } // while
192        return l;
193} // bsearchl
194
195forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
196E * bsearchl( K key, const E * vals, size_t dim ) {
197        size_t posn = bsearchl( key, vals, dim );
198        return (E *)(&vals[posn]);                                                      // cast away const
199} // bsearchl
200
201
202forall( otype E | { int ?<?( E, E ); } )
203size_t bsearchu( E key, const E * vals, size_t dim ) {
204        size_t l = 0, m, h = dim;
205        while ( l < h ) {
206                m = (l + h) / 2;
207                if ( ! ( key < (E &)(vals[m]) ) ) {                             // cast away const
208                        l = m + 1;
209                } else {
210                        h = m;
211                } // if
212        } // while
213        return l;
214} // bsearchu
215
216forall( otype E | { int ?<?( E, E ); } )
217E * bsearchu( E key, const E * vals, size_t dim ) {
218        size_t posn = bsearchu( key, vals, dim );
219        return (E *)(&vals[posn]);
220} // bsearchu
221
222forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
223size_t bsearchu( K key, const E * vals, size_t dim ) {
224        size_t l = 0, m, h = dim;
225        while ( l < h ) {
226                m = (l + h) / 2;
227                if ( ! ( key < getKey( vals[m] ) ) ) {
228                        l = m + 1;
229                } else {
230                        h = m;
231                } // if
232        } // while
233        return l;
234} // bsearchu
235
236forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
237E * bsearchu( K key, const E * vals, size_t dim ) {
238        size_t posn = bsearchu( key, vals, dim );
239        return (E *)(&vals[posn]);
240} // bsearchu
241
242
243forall( otype E | { int ?<?( E, E ); } )
244void qsort( E * vals, size_t dim ) {
245        int cmp( const void * t1, const void * t2 ) {
246                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
247        } // cmp
248        qsort( vals, dim, sizeof(E), cmp );
249} // qsort
250
251//---------------------------------------
252
253[ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; }
254[ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
255[ long long int, long long int ] div( long long int num, long long int denom ) { lldiv_t qr = lldiv( num, denom ); return [ qr.quot, qr.rem ]; }
256forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
257[ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
258
259//---------------------------------------
260
261extern "C" { void srandom( unsigned int seed ) { srand48( (long int)seed ); } } // override C version
262char random( void ) { return (unsigned long int)random(); }
263char random( char u ) { return random( (unsigned long int)u ); }
264char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
265int random( void ) { return (long int)random(); }
266int random( int u ) { return random( (long int)u ); }
267int random( int l, int u ) { return random( (long int)l, (long int)u ); }
268unsigned int random( void ) { return (unsigned long int)random(); }
269unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); }
270unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
271extern "C" { long int random( void ) { return mrand48(); } } // override C version
272long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); }
273long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
274unsigned long int random( void ) { return lrand48(); }
275unsigned long int random( unsigned long int u ) { return lrand48() % u; }
276unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
277float random( void ) { return (float)drand48(); }               // cast otherwise float uses lrand48
278double random( void ) { return drand48(); }
279float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
280double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
281long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
282
283
284// Local Variables: //
285// tab-width: 4 //
286// End: //
Note: See TracBrowser for help on using the repository browser.