[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
|
---|
[76acb60] | 12 | // Last Modified On : Sat Aug 5 11:53:43 2023
|
---|
| 13 | // Update Count : 637
|
---|
[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 | static vtable(out_of_range) out_of_range_vt;
|
---|
| 68 | static vtable(invalid_argument) invalid_argument_vt;
|
---|
| 69 |
|
---|
| 70 | forall( T | { T strto( const char sptr[], char * eptr[], int ); } )
|
---|
| 71 | T convert( const char sptr[] ) {
|
---|
| 72 | char * eptr;
|
---|
| 73 | errno = 0; // reset
|
---|
| 74 | T val = strto( sptr, &eptr, 10 ); // attempt conversion
|
---|
| 75 | if ( errno == ERANGE ) Throw( out_of_range );
|
---|
| 76 | if ( eptr == sptr || // conversion failed, no characters generated
|
---|
| 77 | *eptr != '\0' ) Throw( invalid_argument ); // not at end of str ?
|
---|
| 78 | return val;
|
---|
| 79 | } // convert
|
---|
| 80 |
|
---|
| 81 | float _Complex strto( const char sptr[], char * eptr[] ) {
|
---|
[bd85400] | 82 | float re, im;
|
---|
[e672372] | 83 | char * eeptr;
|
---|
| 84 | re = strtof( sptr, &eeptr );
|
---|
[bbf3fda] | 85 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
[e672372] | 86 | im = strtof( eeptr, &eeptr );
|
---|
[bbf3fda] | 87 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
[e672372] | 88 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
---|
[bd85400] | 89 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 90 | } // strto
|
---|
| 91 |
|
---|
[76acb60] | 92 | double _Complex strto( const char sptr[], char * eptr[] ) {
|
---|
[bd85400] | 93 | double re, im;
|
---|
[e672372] | 94 | char * eeptr;
|
---|
| 95 | re = strtod( sptr, &eeptr );
|
---|
[bbf3fda] | 96 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
[e672372] | 97 | im = strtod( eeptr, &eeptr );
|
---|
[bbf3fda] | 98 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
[e672372] | 99 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
---|
[bd85400] | 100 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 101 | } // strto
|
---|
| 102 |
|
---|
[76acb60] | 103 | long double _Complex strto( const char sptr[], char * eptr[] ) {
|
---|
[bd85400] | 104 | long double re, im;
|
---|
[e672372] | 105 | char * eeptr;
|
---|
| 106 | re = strtold( sptr, &eeptr );
|
---|
[bbf3fda] | 107 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
[e672372] | 108 | im = strtold( eeptr, &eeptr );
|
---|
[bbf3fda] | 109 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
[e672372] | 110 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
---|
[bd85400] | 111 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 112 | } // strto
|
---|
[bd85400] | 113 |
|
---|
| 114 | //---------------------------------------
|
---|
| 115 |
|
---|
[fd54fef] | 116 | forall( E | { int ?<?( E, E ); } ) {
|
---|
[3ce0d440] | 117 | E * bsearch( E key, const E * vals, size_t dim ) {
|
---|
| 118 | int cmp( const void * t1, const void * t2 ) {
|
---|
| 119 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
---|
| 120 | } // cmp
|
---|
| 121 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
---|
| 122 | } // bsearch
|
---|
| 123 |
|
---|
| 124 | size_t bsearch( E key, const E * vals, size_t dim ) {
|
---|
| 125 | E * result = bsearch( key, vals, dim );
|
---|
| 126 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
---|
| 127 | } // bsearch
|
---|
| 128 |
|
---|
| 129 | size_t bsearchl( E key, const E * vals, size_t dim ) {
|
---|
| 130 | size_t l = 0, m, h = dim;
|
---|
| 131 | while ( l < h ) {
|
---|
| 132 | m = (l + h) / 2;
|
---|
| 133 | if ( (E &)(vals[m]) < key ) { // cast away const
|
---|
| 134 | l = m + 1;
|
---|
| 135 | } else {
|
---|
| 136 | h = m;
|
---|
| 137 | } // if
|
---|
| 138 | } // while
|
---|
| 139 | return l;
|
---|
| 140 | } // bsearchl
|
---|
| 141 |
|
---|
| 142 | E * bsearchl( E key, const E * vals, size_t dim ) {
|
---|
| 143 | size_t posn = bsearchl( key, vals, dim );
|
---|
| 144 | return (E *)(&vals[posn]); // cast away const
|
---|
| 145 | } // bsearchl
|
---|
| 146 |
|
---|
| 147 | size_t bsearchu( E key, const E * vals, size_t dim ) {
|
---|
| 148 | size_t l = 0, m, h = dim;
|
---|
| 149 | while ( l < h ) {
|
---|
| 150 | m = (l + h) / 2;
|
---|
| 151 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const
|
---|
| 152 | l = m + 1;
|
---|
| 153 | } else {
|
---|
| 154 | h = m;
|
---|
| 155 | } // if
|
---|
| 156 | } // while
|
---|
| 157 | return l;
|
---|
| 158 | } // bsearchu
|
---|
| 159 |
|
---|
| 160 | E * bsearchu( E key, const E * vals, size_t dim ) {
|
---|
| 161 | size_t posn = bsearchu( key, vals, dim );
|
---|
| 162 | return (E *)(&vals[posn]);
|
---|
| 163 | } // bsearchu
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | void qsort( E * vals, size_t dim ) {
|
---|
| 167 | int cmp( const void * t1, const void * t2 ) {
|
---|
| 168 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
---|
| 169 | } // cmp
|
---|
| 170 | qsort( vals, dim, sizeof(E), cmp );
|
---|
| 171 | } // qsort
|
---|
| 172 | } // distribution
|
---|
| 173 |
|
---|
| 174 |
|
---|
[fd54fef] | 175 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
---|
[3ce0d440] | 176 | E * bsearch( K key, const E * vals, size_t dim ) {
|
---|
| 177 | int cmp( const void * t1, const void * t2 ) {
|
---|
| 178 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
|
---|
| 179 | } // cmp
|
---|
| 180 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
---|
| 181 | } // bsearch
|
---|
| 182 |
|
---|
| 183 | size_t bsearch( K key, const E * vals, size_t dim ) {
|
---|
| 184 | E * result = bsearch( key, vals, dim );
|
---|
| 185 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
---|
| 186 | } // bsearch
|
---|
| 187 |
|
---|
| 188 | size_t bsearchl( K key, const E * vals, size_t dim ) {
|
---|
| 189 | size_t l = 0, m, h = dim;
|
---|
| 190 | while ( l < h ) {
|
---|
| 191 | m = (l + h) / 2;
|
---|
| 192 | if ( getKey( vals[m] ) < key ) {
|
---|
| 193 | l = m + 1;
|
---|
| 194 | } else {
|
---|
| 195 | h = m;
|
---|
| 196 | } // if
|
---|
| 197 | } // while
|
---|
| 198 | return l;
|
---|
| 199 | } // bsearchl
|
---|
| 200 |
|
---|
| 201 | E * bsearchl( K key, const E * vals, size_t dim ) {
|
---|
| 202 | size_t posn = bsearchl( key, vals, dim );
|
---|
| 203 | return (E *)(&vals[posn]); // cast away const
|
---|
| 204 | } // bsearchl
|
---|
| 205 |
|
---|
| 206 | size_t bsearchu( K key, const E * vals, size_t dim ) {
|
---|
| 207 | size_t l = 0, m, h = dim;
|
---|
| 208 | while ( l < h ) {
|
---|
| 209 | m = (l + h) / 2;
|
---|
| 210 | if ( ! ( key < getKey( vals[m] ) ) ) {
|
---|
| 211 | l = m + 1;
|
---|
| 212 | } else {
|
---|
| 213 | h = m;
|
---|
| 214 | } // if
|
---|
| 215 | } // while
|
---|
| 216 | return l;
|
---|
| 217 | } // bsearchu
|
---|
| 218 |
|
---|
| 219 | E * bsearchu( K key, const E * vals, size_t dim ) {
|
---|
| 220 | size_t posn = bsearchu( key, vals, dim );
|
---|
| 221 | return (E *)(&vals[posn]);
|
---|
| 222 | } // bsearchu
|
---|
| 223 | } // distribution
|
---|
[bd85400] | 224 |
|
---|
| 225 | //---------------------------------------
|
---|
| 226 |
|
---|
[bbe1a87] | 227 | extern "C" { // override C version
|
---|
| 228 | void srandom( unsigned int seed ) { srand48( (long int)seed ); }
|
---|
[4e7c0fc0] | 229 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES
|
---|
[bbe1a87] | 230 | } // extern "C"
|
---|
| 231 |
|
---|
[e672372] | 232 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
---|
[70e4895d] | 233 | double random( void ) { return drand48(); }
|
---|
| 234 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
| 235 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
[e672372] | 236 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
[a9f2c13] | 237 |
|
---|
[2026bb6] | 238 | //---------------------------------------
|
---|
| 239 |
|
---|
[0aa4beb] | 240 | // would be cool to make hidden but it's needed for libcfathread
|
---|
[dd46fd3] | 241 | __attribute__((visibility("default"))) size_t __global_random_seed; // sequential/concurrent
|
---|
| 242 | __attribute__((visibility("hidden"))) PRNG_STATE_T __global_random_state; // sequential only
|
---|
[aa8e24c3] | 243 |
|
---|
[8a3d5e7] | 244 | void set_seed( size_t seed ) {
|
---|
| 245 | __global_random_seed = seed;
|
---|
| 246 | PRNG_SET_SEED( __global_random_state, seed );
|
---|
[4020f09] | 247 | } // set_seed
|
---|
| 248 |
|
---|
[20cf96d] | 249 | size_t get_seed() { return __global_random_seed; }
|
---|
| 250 | size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
|
---|
[aa8e24c3] | 251 |
|
---|
| 252 | //---------------------------------------
|
---|
| 253 |
|
---|
| 254 | bool threading_enabled( void ) __attribute__(( weak )) { return false; }
|
---|
[bd85400] | 255 |
|
---|
| 256 | // Local Variables: //
|
---|
| 257 | // tab-width: 4 //
|
---|
| 258 | // End: //
|
---|