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