| 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 : Mon Aug 14 18:22:36 2023
 | 
|---|
| 13 | // Update Count     : 642
 | 
|---|
| 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 | 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
 | 
|---|
| 72 |         if ( errno == ERANGE ) throw ExceptionInst( out_of_range );
 | 
|---|
| 73 |         if ( eptr == sptr ||                                                            // conversion failed, no characters generated
 | 
|---|
| 74 |                  *eptr != '\0' ) throw ExceptionInst( invalid_argument ); // not at end of str ?
 | 
|---|
| 75 |         return val;
 | 
|---|
| 76 | } // convert
 | 
|---|
| 77 | 
 | 
|---|
| 78 | float _Complex strto( const char sptr[], char * eptr[] ) {
 | 
|---|
| 79 |         float re, im;
 | 
|---|
| 80 |         char * eeptr;
 | 
|---|
| 81 |         re = strtof( sptr, &eeptr );
 | 
|---|
| 82 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 83 |         im = strtof( eeptr, &eeptr );
 | 
|---|
| 84 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 85 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 86 |         return re + im * _Complex_I;
 | 
|---|
| 87 | } // strto
 | 
|---|
| 88 | 
 | 
|---|
| 89 | double _Complex strto( const char sptr[], char * eptr[] ) {
 | 
|---|
| 90 |         double re, im;
 | 
|---|
| 91 |         char * eeptr;
 | 
|---|
| 92 |         re = strtod( sptr, &eeptr );
 | 
|---|
| 93 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 94 |         im = strtod( eeptr, &eeptr );
 | 
|---|
| 95 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 96 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 97 |         return re + im * _Complex_I;
 | 
|---|
| 98 | } // strto
 | 
|---|
| 99 | 
 | 
|---|
| 100 | long double _Complex strto( const char sptr[], char * eptr[] ) {
 | 
|---|
| 101 |         long double re, im;
 | 
|---|
| 102 |         char * eeptr;
 | 
|---|
| 103 |         re = strtold( sptr, &eeptr );
 | 
|---|
| 104 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 105 |         im = strtold( eeptr, &eeptr );
 | 
|---|
| 106 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 107 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 108 |         return re + im * _Complex_I;
 | 
|---|
| 109 | } // strto
 | 
|---|
| 110 | 
 | 
|---|
| 111 | //---------------------------------------
 | 
|---|
| 112 | 
 | 
|---|
| 113 | forall( E | { int ?<?( E, E ); } ) {
 | 
|---|
| 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 | 
 | 
|---|
| 172 | forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
 | 
|---|
| 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
 | 
|---|
| 221 | 
 | 
|---|
| 222 | //---------------------------------------
 | 
|---|
| 223 | 
 | 
|---|
| 224 | extern "C" {                                                                                    // override C version
 | 
|---|
| 225 |         void srandom( unsigned int seed ) { srand48( (long int)seed ); }
 | 
|---|
| 226 |         long int random( void ) { return mrand48(); }           // GENERATES POSITIVE AND NEGATIVE VALUES
 | 
|---|
| 227 | } // extern "C"
 | 
|---|
| 228 | 
 | 
|---|
| 229 | float random( void ) { return (float)drand48(); }               // cast otherwise float uses lrand48
 | 
|---|
| 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); }
 | 
|---|
| 233 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | //---------------------------------------
 | 
|---|
| 236 | 
 | 
|---|
| 237 | // would be cool to make hidden but it's needed for libcfathread
 | 
|---|
| 238 | __attribute__((visibility("default"))) size_t __global_random_seed; // sequential/concurrent
 | 
|---|
| 239 | __attribute__((visibility("hidden"))) PRNG_STATE_T __global_random_state; // sequential only
 | 
|---|
| 240 | 
 | 
|---|
| 241 | void set_seed( size_t seed ) {
 | 
|---|
| 242 |         __global_random_seed = seed;
 | 
|---|
| 243 |         PRNG_SET_SEED( __global_random_state, seed );
 | 
|---|
| 244 | } // set_seed
 | 
|---|
| 245 | 
 | 
|---|
| 246 | size_t get_seed() { return __global_random_seed; }
 | 
|---|
| 247 | size_t prng( void ) { return PRNG_NAME( __global_random_state ); } // [0,UINT_MAX]
 | 
|---|
| 248 | 
 | 
|---|
| 249 | //---------------------------------------
 | 
|---|
| 250 | 
 | 
|---|
| 251 | bool threading_enabled( void ) __attribute__(( weak )) { return false; }
 | 
|---|
| 252 | 
 | 
|---|
| 253 | // Local Variables: //
 | 
|---|
| 254 | // tab-width: 4 //
 | 
|---|
| 255 | // End: //
 | 
|---|