[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 |
---|
[b5e725a] | 12 | // Last Modified On : Mon Aug 14 18:22:36 2023 |
---|
| 13 | // Update Count : 642 |
---|
[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 | |
---|
[f3fc631f] | 22 | #include <string.h> // memcpy, memset |
---|
[89124ff] | 23 | //#include <math.h> // fabsf, fabs, fabsl |
---|
[6e991d6] | 24 | #include <complex.h> // _Complex_I |
---|
[e672372] | 25 | #include <assert.h> |
---|
[bd85400] | 26 | |
---|
[0aa4beb] | 27 | #pragma GCC visibility push(default) |
---|
| 28 | |
---|
[f4a6101] | 29 | //--------------------------------------- |
---|
| 30 | |
---|
[94429f8] | 31 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
[627f585] | 32 | |
---|
[fd54fef] | 33 | forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) |
---|
[94429f8] | 34 | T * anew( size_t dim, TT p ) { |
---|
[6887a99] | 35 | T * arr = alloc( dim ); |
---|
[f6a4917] | 36 | for ( i; dim ) { |
---|
[a493682] | 37 | (arr[i]){ p }; // run constructor |
---|
[6065b3aa] | 38 | } // for |
---|
| 39 | return arr; |
---|
| 40 | } // anew |
---|
| 41 | |
---|
[fd54fef] | 42 | forall( T & | sized(T) | { void ^?{}( T & ); } ) |
---|
[45444c3] | 43 | void adelete( T arr[] ) { |
---|
[6065b3aa] | 44 | if ( arr ) { // ignore null |
---|
[45444c3] | 45 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[f6a4917] | 46 | for ( i; 0 -~= dim - 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 47 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 48 | } // for |
---|
| 49 | free( arr ); |
---|
| 50 | } // if |
---|
| 51 | } // adelete |
---|
| 52 | |
---|
[fd54fef] | 53 | forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) |
---|
[94429f8] | 54 | void adelete( T arr[], TT rest ) { |
---|
[6065b3aa] | 55 | if ( arr ) { // ignore null |
---|
[45444c3] | 56 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[f6a4917] | 57 | for ( i; 0 -~= dim - 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 58 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 59 | } // for |
---|
| 60 | free( arr ); |
---|
| 61 | } // if |
---|
| 62 | adelete( rest ); |
---|
| 63 | } // adelete |
---|
| 64 | |
---|
[bd85400] | 65 | //--------------------------------------- |
---|
| 66 | |
---|
[76acb60] | 67 | forall( T | { T strto( const char sptr[], char * eptr[], int ); } ) |
---|
| 68 | T convert( const char sptr[] ) { |
---|
| 69 | char * eptr; |
---|
| 70 | errno = 0; // reset |
---|
| 71 | T val = strto( sptr, &eptr, 10 ); // attempt conversion |
---|
[b5e725a] | 72 | if ( errno == ERANGE ) throw ExceptionInst( out_of_range ); |
---|
[76acb60] | 73 | if ( eptr == sptr || // conversion failed, no characters generated |
---|
[b5e725a] | 74 | *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ? |
---|
[76acb60] | 75 | return val; |
---|
| 76 | } // convert |
---|
| 77 | |
---|
| 78 | float _Complex strto( const char sptr[], char * eptr[] ) { |
---|
[bd85400] | 79 | float re, im; |
---|
[e672372] | 80 | char * eeptr; |
---|
| 81 | re = strtof( sptr, &eeptr ); |
---|
[bbf3fda] | 82 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 83 | im = strtof( eeptr, &eeptr ); |
---|
[bbf3fda] | 84 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 85 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[bd85400] | 86 | return re + im * _Complex_I; |
---|
[f3ddc21] | 87 | } // strto |
---|
| 88 | |
---|
[76acb60] | 89 | double _Complex strto( const char sptr[], char * eptr[] ) { |
---|
[bd85400] | 90 | double re, im; |
---|
[e672372] | 91 | char * eeptr; |
---|
| 92 | re = strtod( sptr, &eeptr ); |
---|
[bbf3fda] | 93 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 94 | im = strtod( eeptr, &eeptr ); |
---|
[bbf3fda] | 95 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 96 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[bd85400] | 97 | return re + im * _Complex_I; |
---|
[f3ddc21] | 98 | } // strto |
---|
| 99 | |
---|
[76acb60] | 100 | long double _Complex strto( const char sptr[], char * eptr[] ) { |
---|
[bd85400] | 101 | long double re, im; |
---|
[e672372] | 102 | char * eeptr; |
---|
| 103 | re = strtold( sptr, &eeptr ); |
---|
[bbf3fda] | 104 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 105 | im = strtold( eeptr, &eeptr ); |
---|
[bbf3fda] | 106 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 107 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[bd85400] | 108 | return re + im * _Complex_I; |
---|
[f3ddc21] | 109 | } // strto |
---|
[bd85400] | 110 | |
---|
| 111 | //--------------------------------------- |
---|
| 112 | |
---|
[fd54fef] | 113 | forall( E | { int ?<?( E, E ); } ) { |
---|
[3ce0d440] | 114 | E * bsearch( E key, const E * vals, size_t dim ) { |
---|
| 115 | int cmp( const void * t1, const void * t2 ) { |
---|
| 116 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
| 117 | } // cmp |
---|
| 118 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
| 119 | } // bsearch |
---|
| 120 | |
---|
| 121 | size_t bsearch( E key, const E * vals, size_t dim ) { |
---|
| 122 | E * result = bsearch( key, vals, dim ); |
---|
| 123 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 124 | } // bsearch |
---|
| 125 | |
---|
| 126 | size_t bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 127 | size_t l = 0, m, h = dim; |
---|
| 128 | while ( l < h ) { |
---|
| 129 | m = (l + h) / 2; |
---|
| 130 | if ( (E &)(vals[m]) < key ) { // cast away const |
---|
| 131 | l = m + 1; |
---|
| 132 | } else { |
---|
| 133 | h = m; |
---|
| 134 | } // if |
---|
| 135 | } // while |
---|
| 136 | return l; |
---|
| 137 | } // bsearchl |
---|
| 138 | |
---|
| 139 | E * bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 140 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 141 | return (E *)(&vals[posn]); // cast away const |
---|
| 142 | } // bsearchl |
---|
| 143 | |
---|
| 144 | size_t bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 145 | size_t l = 0, m, h = dim; |
---|
| 146 | while ( l < h ) { |
---|
| 147 | m = (l + h) / 2; |
---|
| 148 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const |
---|
| 149 | l = m + 1; |
---|
| 150 | } else { |
---|
| 151 | h = m; |
---|
| 152 | } // if |
---|
| 153 | } // while |
---|
| 154 | return l; |
---|
| 155 | } // bsearchu |
---|
| 156 | |
---|
| 157 | E * bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 158 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 159 | return (E *)(&vals[posn]); |
---|
| 160 | } // bsearchu |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | void qsort( E * vals, size_t dim ) { |
---|
| 164 | int cmp( const void * t1, const void * t2 ) { |
---|
| 165 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
| 166 | } // cmp |
---|
| 167 | qsort( vals, dim, sizeof(E), cmp ); |
---|
| 168 | } // qsort |
---|
| 169 | } // distribution |
---|
| 170 | |
---|
| 171 | |
---|
[fd54fef] | 172 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
[3ce0d440] | 173 | E * bsearch( K key, const E * vals, size_t dim ) { |
---|
| 174 | int cmp( const void * t1, const void * t2 ) { |
---|
| 175 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0; |
---|
| 176 | } // cmp |
---|
| 177 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
| 178 | } // bsearch |
---|
| 179 | |
---|
| 180 | size_t bsearch( K key, const E * vals, size_t dim ) { |
---|
| 181 | E * result = bsearch( key, vals, dim ); |
---|
| 182 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 183 | } // bsearch |
---|
| 184 | |
---|
| 185 | size_t bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 186 | size_t l = 0, m, h = dim; |
---|
| 187 | while ( l < h ) { |
---|
| 188 | m = (l + h) / 2; |
---|
| 189 | if ( getKey( vals[m] ) < key ) { |
---|
| 190 | l = m + 1; |
---|
| 191 | } else { |
---|
| 192 | h = m; |
---|
| 193 | } // if |
---|
| 194 | } // while |
---|
| 195 | return l; |
---|
| 196 | } // bsearchl |
---|
| 197 | |
---|
| 198 | E * bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 199 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 200 | return (E *)(&vals[posn]); // cast away const |
---|
| 201 | } // bsearchl |
---|
| 202 | |
---|
| 203 | size_t bsearchu( K 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 < getKey( vals[m] ) ) ) { |
---|
| 208 | l = m + 1; |
---|
| 209 | } else { |
---|
| 210 | h = m; |
---|
| 211 | } // if |
---|
| 212 | } // while |
---|
| 213 | return l; |
---|
| 214 | } // bsearchu |
---|
| 215 | |
---|
| 216 | E * bsearchu( K key, const E * vals, size_t dim ) { |
---|
| 217 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 218 | return (E *)(&vals[posn]); |
---|
| 219 | } // bsearchu |
---|
| 220 | } // distribution |
---|
[bd85400] | 221 | |
---|
| 222 | //--------------------------------------- |
---|
| 223 | |
---|
[bbe1a87] | 224 | extern "C" { // override C version |
---|
| 225 | void srandom( unsigned int seed ) { srand48( (long int)seed ); } |
---|
[4e7c0fc0] | 226 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
[bbe1a87] | 227 | } // extern "C" |
---|
| 228 | |
---|
[e672372] | 229 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 |
---|
[70e4895d] | 230 | double random( void ) { return drand48(); } |
---|
| 231 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 232 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
[e672372] | 233 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
[a9f2c13] | 234 | |
---|
[2026bb6] | 235 | //--------------------------------------- |
---|
| 236 | |
---|
[0aa4beb] | 237 | // would be cool to make hidden but it's needed for libcfathread |
---|
[dd46fd3] | 238 | __attribute__((visibility("default"))) size_t __global_random_seed; // sequential/concurrent |
---|
| 239 | __attribute__((visibility("hidden"))) PRNG_STATE_T __global_random_state; // sequential only |
---|
[aa8e24c3] | 240 | |
---|
[8a3d5e7] | 241 | void set_seed( size_t seed ) { |
---|
| 242 | __global_random_seed = seed; |
---|
| 243 | PRNG_SET_SEED( __global_random_state, seed ); |
---|
[4020f09] | 244 | } // set_seed |
---|
| 245 | |
---|
[20cf96d] | 246 | size_t get_seed() { return __global_random_seed; } |
---|
| 247 | size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX] |
---|
[aa8e24c3] | 248 | |
---|
| 249 | //--------------------------------------- |
---|
| 250 | |
---|
| 251 | bool threading_enabled( void ) __attribute__(( weak )) { return false; } |
---|
[bd85400] | 252 | |
---|
| 253 | // Local Variables: // |
---|
| 254 | // tab-width: 4 // |
---|
| 255 | // End: // |
---|