| 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 : Sat Aug 5 11:53:43 2023
|
|---|
| 13 | // Update Count : 637
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "stdlib.hfa"
|
|---|
| 17 | #include "bits/random.hfa"
|
|---|
| 18 | #include "concurrency/invoke.h" // random_state
|
|---|
| 19 |
|
|---|
| 20 | //---------------------------------------
|
|---|
| 21 |
|
|---|
| 22 | #include <string.h> // memcpy, memset
|
|---|
| 23 | //#include <math.h> // fabsf, fabs, fabsl
|
|---|
| 24 | #include <complex.h> // _Complex_I
|
|---|
| 25 | #include <assert.h>
|
|---|
| 26 |
|
|---|
| 27 | #pragma GCC visibility push(default)
|
|---|
| 28 |
|
|---|
| 29 | //---------------------------------------
|
|---|
| 30 |
|
|---|
| 31 | // Cforall allocation/deallocation and constructor/destructor, array types
|
|---|
| 32 |
|
|---|
| 33 | forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
|
|---|
| 34 | T * anew( size_t dim, TT p ) {
|
|---|
| 35 | T * arr = alloc( dim );
|
|---|
| 36 | for ( i; dim ) {
|
|---|
| 37 | (arr[i]){ p }; // run constructor
|
|---|
| 38 | } // for
|
|---|
| 39 | return arr;
|
|---|
| 40 | } // anew
|
|---|
| 41 |
|
|---|
| 42 | forall( T & | sized(T) | { void ^?{}( T & ); } )
|
|---|
| 43 | void adelete( T arr[] ) {
|
|---|
| 44 | if ( arr ) { // ignore null
|
|---|
| 45 | size_t dim = malloc_size( arr ) / sizeof( T );
|
|---|
| 46 | for ( i; 0 -~= dim - 1 ) { // reverse allocation order, must be unsigned
|
|---|
| 47 | ^(arr[i]){}; // run destructor
|
|---|
| 48 | } // for
|
|---|
| 49 | free( arr );
|
|---|
| 50 | } // if
|
|---|
| 51 | } // adelete
|
|---|
| 52 |
|
|---|
| 53 | forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } )
|
|---|
| 54 | void adelete( T arr[], TT rest ) {
|
|---|
| 55 | if ( arr ) { // ignore null
|
|---|
| 56 | size_t dim = malloc_size( arr ) / sizeof( T );
|
|---|
| 57 | for ( i; 0 -~= dim - 1 ) { // reverse allocation order, must be unsigned
|
|---|
| 58 | ^(arr[i]){}; // run destructor
|
|---|
| 59 | } // for
|
|---|
| 60 | free( arr );
|
|---|
| 61 | } // if
|
|---|
| 62 | adelete( rest );
|
|---|
| 63 | } // adelete
|
|---|
| 64 |
|
|---|
| 65 | //---------------------------------------
|
|---|
| 66 |
|
|---|
| 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[] ) {
|
|---|
| 82 | float re, im;
|
|---|
| 83 | char * eeptr;
|
|---|
| 84 | re = strtof( sptr, &eeptr );
|
|---|
| 85 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 86 | im = strtof( eeptr, &eeptr );
|
|---|
| 87 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 88 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 89 | return re + im * _Complex_I;
|
|---|
| 90 | } // strto
|
|---|
| 91 |
|
|---|
| 92 | double _Complex strto( const char sptr[], char * eptr[] ) {
|
|---|
| 93 | double re, im;
|
|---|
| 94 | char * eeptr;
|
|---|
| 95 | re = strtod( sptr, &eeptr );
|
|---|
| 96 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 97 | im = strtod( eeptr, &eeptr );
|
|---|
| 98 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 99 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 100 | return re + im * _Complex_I;
|
|---|
| 101 | } // strto
|
|---|
| 102 |
|
|---|
| 103 | long double _Complex strto( const char sptr[], char * eptr[] ) {
|
|---|
| 104 | long double re, im;
|
|---|
| 105 | char * eeptr;
|
|---|
| 106 | re = strtold( sptr, &eeptr );
|
|---|
| 107 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 108 | im = strtold( eeptr, &eeptr );
|
|---|
| 109 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 110 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 111 | return re + im * _Complex_I;
|
|---|
| 112 | } // strto
|
|---|
| 113 |
|
|---|
| 114 | //---------------------------------------
|
|---|
| 115 |
|
|---|
| 116 | forall( E | { int ?<?( E, E ); } ) {
|
|---|
| 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 |
|
|---|
| 175 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
|---|
| 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
|
|---|
| 224 |
|
|---|
| 225 | //---------------------------------------
|
|---|
| 226 |
|
|---|
| 227 | extern "C" { // override C version
|
|---|
| 228 | void srandom( unsigned int seed ) { srand48( (long int)seed ); }
|
|---|
| 229 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES
|
|---|
| 230 | } // extern "C"
|
|---|
| 231 |
|
|---|
| 232 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
|---|
| 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); }
|
|---|
| 236 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
|---|
| 237 |
|
|---|
| 238 | //---------------------------------------
|
|---|
| 239 |
|
|---|
| 240 | // would be cool to make hidden but it's needed for libcfathread
|
|---|
| 241 | __attribute__((visibility("default"))) size_t __global_random_seed; // sequential/concurrent
|
|---|
| 242 | __attribute__((visibility("hidden"))) PRNG_STATE_T __global_random_state; // sequential only
|
|---|
| 243 |
|
|---|
| 244 | void set_seed( size_t seed ) {
|
|---|
| 245 | __global_random_seed = seed;
|
|---|
| 246 | PRNG_SET_SEED( __global_random_state, seed );
|
|---|
| 247 | } // set_seed
|
|---|
| 248 |
|
|---|
| 249 | size_t get_seed() { return __global_random_seed; }
|
|---|
| 250 | size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
|
|---|
| 251 |
|
|---|
| 252 | //---------------------------------------
|
|---|
| 253 |
|
|---|
| 254 | bool threading_enabled( void ) __attribute__(( weak )) { return false; }
|
|---|
| 255 |
|
|---|
| 256 | // Local Variables: //
|
|---|
| 257 | // tab-width: 4 //
|
|---|
| 258 | // End: //
|
|---|