source: libcfa/src/stdlib.cfa@ 9b33337

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 9b33337 was 2210cfc, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

second attempt at specialized PRNG

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