source: libcfa/src/stdlib.cfa@ e873838

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since e873838 was 45444c3, checked in by m3zulfiq <m3zulfiq@…>, 5 years ago

Removed dimension parameter from adelete.

  • Property mode set to 100644
File size: 7.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.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 : Sun Jul 19 15:05:28 2020
13// Update Count : 501
14//
15
16#include "stdlib.hfa"
17
18//---------------------------------------
19
20#define _XOPEN_SOURCE 600 // posix_memalign, *rand48
21#include <string.h> // memcpy, memset
22//#include <math.h> // fabsf, fabs, fabsl
23#include <complex.h> // _Complex_I
24#include <assert.h>
25
26//---------------------------------------
27
28// allocation/deallocation and constructor/destructor, non-array types
29forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
30T * new( Params p ) {
31 return &(*malloc()){ p }; // run constructor
32} // new
33
34forall( dtype T | { void ^?{}( T & ); } )
35void delete( T * ptr ) {
36 if ( ptr ) { // ignore null
37 ^(*ptr){}; // run destructor
38 free( ptr );
39 } // if
40} // delete
41
42forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } )
43void delete( T * ptr, Params rest ) {
44 delete( ptr );
45 delete( rest );
46} // delete
47
48
49// allocation/deallocation and constructor/destructor, array types
50forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
51T * anew( size_t dim, Params p ) {
52 T * arr = alloc( dim );
53 for ( unsigned int i = 0; i < dim; i += 1 ) {
54 (arr[i]){ p }; // run constructor
55 } // for
56 return arr;
57} // anew
58
59forall( dtype T | sized(T) | { void ^?{}( T & ); } )
60void adelete( T arr[] ) {
61 if ( arr ) { // ignore null
62 size_t dim = malloc_size( arr ) / sizeof( T );
63 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
64 ^(arr[i]){}; // run destructor
65 } // for
66 free( arr );
67 } // if
68} // adelete
69
70forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
71void adelete( T arr[], Params rest ) {
72 if ( arr ) { // ignore null
73 size_t dim = malloc_size( arr ) / sizeof( T );
74 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
75 ^(arr[i]){}; // run destructor
76 } // for
77 free( arr );
78 } // if
79 adelete( rest );
80} // adelete
81
82//---------------------------------------
83
84float _Complex strto( const char sptr[], char ** eptr ) {
85 float re, im;
86 char * eeptr;
87 re = strtof( sptr, &eeptr );
88 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
89 im = strtof( eeptr, &eeptr );
90 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
91 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
92 return re + im * _Complex_I;
93} // strto
94
95double _Complex strto( const char sptr[], char ** eptr ) {
96 double re, im;
97 char * eeptr;
98 re = strtod( sptr, &eeptr );
99 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
100 im = strtod( eeptr, &eeptr );
101 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
102 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
103 return re + im * _Complex_I;
104} // strto
105
106long double _Complex strto( const char sptr[], char ** eptr ) {
107 long double re, im;
108 char * eeptr;
109 re = strtold( sptr, &eeptr );
110 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
111 im = strtold( eeptr, &eeptr );
112 if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
113 if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
114 return re + im * _Complex_I;
115} // strto
116
117//---------------------------------------
118
119forall( otype E | { int ?<?( E, E ); } ) {
120 E * bsearch( E key, const E * vals, size_t dim ) {
121 int cmp( const void * t1, const void * t2 ) {
122 return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
123 } // cmp
124 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
125 } // bsearch
126
127 size_t bsearch( E key, const E * vals, size_t dim ) {
128 E * result = bsearch( key, vals, dim );
129 return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
130 } // bsearch
131
132 size_t bsearchl( E key, const E * vals, size_t dim ) {
133 size_t l = 0, m, h = dim;
134 while ( l < h ) {
135 m = (l + h) / 2;
136 if ( (E &)(vals[m]) < key ) { // cast away const
137 l = m + 1;
138 } else {
139 h = m;
140 } // if
141 } // while
142 return l;
143 } // bsearchl
144
145 E * bsearchl( E key, const E * vals, size_t dim ) {
146 size_t posn = bsearchl( key, vals, dim );
147 return (E *)(&vals[posn]); // cast away const
148 } // bsearchl
149
150 size_t bsearchu( E key, const E * vals, size_t dim ) {
151 size_t l = 0, m, h = dim;
152 while ( l < h ) {
153 m = (l + h) / 2;
154 if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const
155 l = m + 1;
156 } else {
157 h = m;
158 } // if
159 } // while
160 return l;
161 } // bsearchu
162
163 E * bsearchu( E key, const E * vals, size_t dim ) {
164 size_t posn = bsearchu( key, vals, dim );
165 return (E *)(&vals[posn]);
166 } // bsearchu
167
168
169 void qsort( E * vals, size_t dim ) {
170 int cmp( const void * t1, const void * t2 ) {
171 return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
172 } // cmp
173 qsort( vals, dim, sizeof(E), cmp );
174 } // qsort
175} // distribution
176
177
178forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
179 E * bsearch( K key, const E * vals, size_t dim ) {
180 int cmp( const void * t1, const void * t2 ) {
181 return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
182 } // cmp
183 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
184 } // bsearch
185
186 size_t bsearch( K key, const E * vals, size_t dim ) {
187 E * result = bsearch( key, vals, dim );
188 return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
189 } // bsearch
190
191 size_t bsearchl( K key, const E * vals, size_t dim ) {
192 size_t l = 0, m, h = dim;
193 while ( l < h ) {
194 m = (l + h) / 2;
195 if ( getKey( vals[m] ) < key ) {
196 l = m + 1;
197 } else {
198 h = m;
199 } // if
200 } // while
201 return l;
202 } // bsearchl
203
204 E * bsearchl( K key, const E * vals, size_t dim ) {
205 size_t posn = bsearchl( key, vals, dim );
206 return (E *)(&vals[posn]); // cast away const
207 } // bsearchl
208
209 size_t bsearchu( K key, const E * vals, size_t dim ) {
210 size_t l = 0, m, h = dim;
211 while ( l < h ) {
212 m = (l + h) / 2;
213 if ( ! ( key < getKey( vals[m] ) ) ) {
214 l = m + 1;
215 } else {
216 h = m;
217 } // if
218 } // while
219 return l;
220 } // bsearchu
221
222 E * bsearchu( K key, const E * vals, size_t dim ) {
223 size_t posn = bsearchu( key, vals, dim );
224 return (E *)(&vals[posn]);
225 } // bsearchu
226} // distribution
227
228//---------------------------------------
229
230extern "C" { // override C version
231 void srandom( unsigned int seed ) { srand48( (long int)seed ); }
232 long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES
233} // extern "C"
234
235float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
236double random( void ) { return drand48(); }
237float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
238double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
239long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
240
241//---------------------------------------
242
243bool threading_enabled(void) __attribute__((weak)) {
244 return false;
245}
246
247// Local Variables: //
248// tab-width: 4 //
249// End: //
Note: See TracBrowser for help on using the repository browser.