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