[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 | // |
---|
[f4a6101] | 7 | // stdlib.c -- |
---|
[bd85400] | 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Jan 28 17:10:29 2016 |
---|
[e310238] | 11 | // Last Modified By : Peter A. Buhr |
---|
[6a823241] | 12 | // Last Modified On : Thu Feb 10 22:41:39 2022 |
---|
| 13 | // Update Count : 602 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[58b6d1b] | 16 | #include "stdlib.hfa" |
---|
[5d1ebb9] | 17 | #include "bits/random.hfa" |
---|
[2210cfc] | 18 | #include "concurrency/invoke.h" // random_state |
---|
[bd85400] | 19 | |
---|
| 20 | //--------------------------------------- |
---|
| 21 | |
---|
| 22 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 |
---|
[f3fc631f] | 23 | #include <string.h> // memcpy, memset |
---|
[89124ff] | 24 | //#include <math.h> // fabsf, fabs, fabsl |
---|
[6e991d6] | 25 | #include <complex.h> // _Complex_I |
---|
[e672372] | 26 | #include <assert.h> |
---|
[bd85400] | 27 | |
---|
[f4a6101] | 28 | //--------------------------------------- |
---|
| 29 | |
---|
[94429f8] | 30 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
[627f585] | 31 | |
---|
[fd54fef] | 32 | forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) |
---|
[94429f8] | 33 | T * anew( size_t dim, TT p ) { |
---|
[6887a99] | 34 | T * arr = alloc( dim ); |
---|
[6065b3aa] | 35 | for ( unsigned int i = 0; i < dim; i += 1 ) { |
---|
[a493682] | 36 | (arr[i]){ p }; // run constructor |
---|
[6065b3aa] | 37 | } // for |
---|
| 38 | return arr; |
---|
| 39 | } // anew |
---|
| 40 | |
---|
[fd54fef] | 41 | forall( T & | sized(T) | { void ^?{}( T & ); } ) |
---|
[45444c3] | 42 | void adelete( T arr[] ) { |
---|
[6065b3aa] | 43 | if ( arr ) { // ignore null |
---|
[45444c3] | 44 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[6065b3aa] | 45 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 46 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 47 | } // for |
---|
| 48 | free( arr ); |
---|
| 49 | } // if |
---|
| 50 | } // adelete |
---|
| 51 | |
---|
[fd54fef] | 52 | forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) |
---|
[94429f8] | 53 | void adelete( T arr[], TT rest ) { |
---|
[6065b3aa] | 54 | if ( arr ) { // ignore null |
---|
[45444c3] | 55 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[6065b3aa] | 56 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 57 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 58 | } // for |
---|
| 59 | free( arr ); |
---|
| 60 | } // if |
---|
| 61 | adelete( rest ); |
---|
| 62 | } // adelete |
---|
| 63 | |
---|
[bd85400] | 64 | //--------------------------------------- |
---|
| 65 | |
---|
[e3fea42] | 66 | float _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 67 | float re, im; |
---|
[e672372] | 68 | char * eeptr; |
---|
| 69 | re = strtof( sptr, &eeptr ); |
---|
[bbf3fda] | 70 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 71 | im = strtof( eeptr, &eeptr ); |
---|
[bbf3fda] | 72 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 73 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[bd85400] | 74 | return re + im * _Complex_I; |
---|
[f3ddc21] | 75 | } // strto |
---|
| 76 | |
---|
[e3fea42] | 77 | double _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 78 | double re, im; |
---|
[e672372] | 79 | char * eeptr; |
---|
| 80 | re = strtod( sptr, &eeptr ); |
---|
[bbf3fda] | 81 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 82 | im = strtod( eeptr, &eeptr ); |
---|
[bbf3fda] | 83 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 84 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[bd85400] | 85 | return re + im * _Complex_I; |
---|
[f3ddc21] | 86 | } // strto |
---|
| 87 | |
---|
[e3fea42] | 88 | long double _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 89 | long double re, im; |
---|
[e672372] | 90 | char * eeptr; |
---|
| 91 | re = strtold( sptr, &eeptr ); |
---|
[bbf3fda] | 92 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 93 | im = strtold( eeptr, &eeptr ); |
---|
[bbf3fda] | 94 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 95 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[bd85400] | 96 | return re + im * _Complex_I; |
---|
[f3ddc21] | 97 | } // strto |
---|
[bd85400] | 98 | |
---|
| 99 | //--------------------------------------- |
---|
| 100 | |
---|
[fd54fef] | 101 | forall( E | { int ?<?( E, E ); } ) { |
---|
[3ce0d440] | 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 | |
---|
[fd54fef] | 160 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
[3ce0d440] | 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 |
---|
[bd85400] | 209 | |
---|
| 210 | //--------------------------------------- |
---|
| 211 | |
---|
[bbe1a87] | 212 | extern "C" { // override C version |
---|
| 213 | void srandom( unsigned int seed ) { srand48( (long int)seed ); } |
---|
[4e7c0fc0] | 214 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
[bbe1a87] | 215 | } // extern "C" |
---|
| 216 | |
---|
[e672372] | 217 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 |
---|
[70e4895d] | 218 | double random( void ) { return drand48(); } |
---|
| 219 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 220 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
[e672372] | 221 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
[a9f2c13] | 222 | |
---|
[2026bb6] | 223 | //--------------------------------------- |
---|
| 224 | |
---|
[aa8e24c3] | 225 | #define GENERATOR LCG |
---|
| 226 | |
---|
[00f5fde] | 227 | uint32_t __global_random_seed; // sequential/concurrent |
---|
[12b5e94a] | 228 | uint32_t __global_random_state; // sequential only |
---|
[2210cfc] | 229 | |
---|
[1959528] | 230 | void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; GENERATOR( state ); } // set seed |
---|
[aa8e24c3] | 231 | |
---|
[6a823241] | 232 | void set_seed( uint32_t seed ) { __global_random_state = __global_random_seed = seed; GENERATOR( __global_random_state ); } |
---|
[1959528] | 233 | uint32_t get_seed() { return __global_random_seed; } |
---|
[00f5fde] | 234 | uint32_t prng( void ) { return GENERATOR( __global_random_state ); } // [0,UINT_MAX] |
---|
[aa8e24c3] | 235 | |
---|
| 236 | //--------------------------------------- |
---|
| 237 | |
---|
| 238 | bool threading_enabled( void ) __attribute__(( weak )) { return false; } |
---|
[bd85400] | 239 | |
---|
| 240 | // Local Variables: // |
---|
| 241 | // tab-width: 4 // |
---|
| 242 | // End: // |
---|