| 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 : Thu Apr 16 22:43:33 2020
 | 
|---|
| 13 | // Update Count     : 498
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "stdlib.hfa"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | //---------------------------------------
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
 | 
|---|
| 21 | #include <string.h>                                                                             // memcpy, memset
 | 
|---|
| 22 | //#include <math.h>                                                                             // fabsf, fabs, fabsl
 | 
|---|
| 23 | #include <complex.h>                                                                    // _Complex_I
 | 
|---|
| 24 | #include <assert.h>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | //---------------------------------------
 | 
|---|
| 27 | 
 | 
|---|
| 28 | forall( dtype T | sized(T) ) {
 | 
|---|
| 29 |         T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
 | 
|---|
| 30 |                 size_t olen = malloc_usable_size( ptr );                // current allocation
 | 
|---|
| 31 |                 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
 | 
|---|
| 32 |                 size_t nlen = malloc_usable_size( nptr );               // new allocation
 | 
|---|
| 33 |                 if ( nlen > olen ) {                                                    // larger ?
 | 
|---|
| 34 |                         memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
 | 
|---|
| 35 |                 } // if
 | 
|---|
| 36 |                 return (T *)nptr;
 | 
|---|
| 37 |         } // alloc_set
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         T * alloc_set( T ptr[], size_t dim, T fill ) {          // realloc array with fill
 | 
|---|
| 40 |                 size_t olen = malloc_usable_size( ptr );                // current allocation
 | 
|---|
| 41 |                 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
 | 
|---|
| 42 |                 size_t nlen = malloc_usable_size( nptr );               // new allocation
 | 
|---|
| 43 |                 if ( nlen > olen ) {                                                    // larger ?
 | 
|---|
| 44 |                         for ( i; malloc_size( ptr ) / sizeof(T) ~ dim ) {
 | 
|---|
| 45 |                                 memcpy( &ptr[i], &fill, sizeof(T) );    // initialize with fill value
 | 
|---|
| 46 |                         } // for
 | 
|---|
| 47 |                 } // if
 | 
|---|
| 48 |                 return (T *)nptr;
 | 
|---|
| 49 |         } // alloc_align_set
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
 | 
|---|
| 52 |                 size_t olen = malloc_usable_size( ptr );                // current allocation
 | 
|---|
| 53 |                 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
 | 
|---|
| 54 |                 // char * nptr = alloc_align( ptr, align );
 | 
|---|
| 55 |                 size_t nlen = malloc_usable_size( nptr );               // new allocation
 | 
|---|
| 56 |                 if ( nlen > olen ) {                                                    // larger ?
 | 
|---|
| 57 |                         memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
 | 
|---|
| 58 |                 } // if
 | 
|---|
| 59 |                 return (T *)nptr;
 | 
|---|
| 60 |         } // alloc_align_set
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill
 | 
|---|
| 63 |                 size_t olen = malloc_usable_size( ptr );                // current allocation
 | 
|---|
| 64 |                 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
 | 
|---|
| 65 |                 // char * nptr = alloc_align( ptr, align );
 | 
|---|
| 66 |                 size_t nlen = malloc_usable_size( nptr );               // new allocation
 | 
|---|
| 67 |                 if ( nlen > olen ) {                                                    // larger ?
 | 
|---|
| 68 |                         for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value
 | 
|---|
| 69 |                 } // if
 | 
|---|
| 70 |                 return (T *)nptr;
 | 
|---|
| 71 |         } // alloc_align_set
 | 
|---|
| 72 | } // distribution
 | 
|---|
| 73 | 
 | 
|---|
| 74 | // allocation/deallocation and constructor/destructor, non-array types
 | 
|---|
| 75 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
 | 
|---|
| 76 | T * new( Params p ) {
 | 
|---|
| 77 |         return &(*malloc()){ p };                                                       // run constructor
 | 
|---|
| 78 | } // new
 | 
|---|
| 79 | 
 | 
|---|
| 80 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
 | 
|---|
| 81 | void delete( T * ptr ) {
 | 
|---|
| 82 |         if ( ptr ) {                                                                            // ignore null
 | 
|---|
| 83 |                 ^(*ptr){};                                                                              // run destructor
 | 
|---|
| 84 |                 free( ptr );
 | 
|---|
| 85 |         } // if
 | 
|---|
| 86 | } // delete
 | 
|---|
| 87 | 
 | 
|---|
| 88 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
 | 
|---|
| 89 | void delete( T * ptr, Params rest ) {
 | 
|---|
| 90 |         if ( ptr ) {                                                                            // ignore null
 | 
|---|
| 91 |                 ^(*ptr){};                                                                              // run destructor
 | 
|---|
| 92 |                 free( ptr );
 | 
|---|
| 93 |         } // if
 | 
|---|
| 94 |         delete( rest );
 | 
|---|
| 95 | } // delete
 | 
|---|
| 96 | 
 | 
|---|
| 97 | 
 | 
|---|
| 98 | // allocation/deallocation and constructor/destructor, array types
 | 
|---|
| 99 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
 | 
|---|
| 100 | T * anew( size_t dim, Params p ) {
 | 
|---|
| 101 |         T * arr = alloc( dim );
 | 
|---|
| 102 |         for ( unsigned int i = 0; i < dim; i += 1 ) {
 | 
|---|
| 103 |                 (arr[i]){ p };                                                                  // run constructor
 | 
|---|
| 104 |         } // for
 | 
|---|
| 105 |         return arr;
 | 
|---|
| 106 | } // anew
 | 
|---|
| 107 | 
 | 
|---|
| 108 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
 | 
|---|
| 109 | void adelete( size_t dim, T arr[] ) {
 | 
|---|
| 110 |         if ( arr ) {                                                                            // ignore null
 | 
|---|
| 111 |                 for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
 | 
|---|
| 112 |                         ^(arr[i]){};                                                            // run destructor
 | 
|---|
| 113 |                 } // for
 | 
|---|
| 114 |                 free( arr );
 | 
|---|
| 115 |         } // if
 | 
|---|
| 116 | } // adelete
 | 
|---|
| 117 | 
 | 
|---|
| 118 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
 | 
|---|
| 119 | void adelete( size_t dim, T arr[], Params rest ) {
 | 
|---|
| 120 |         if ( arr ) {                                                                            // ignore null
 | 
|---|
| 121 |                 for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
 | 
|---|
| 122 |                         ^(arr[i]){};                                                            // run destructor
 | 
|---|
| 123 |                 } // for
 | 
|---|
| 124 |                 free( arr );
 | 
|---|
| 125 |         } // if
 | 
|---|
| 126 |         adelete( rest );
 | 
|---|
| 127 | } // adelete
 | 
|---|
| 128 | 
 | 
|---|
| 129 | //---------------------------------------
 | 
|---|
| 130 | 
 | 
|---|
| 131 | float _Complex strto( const char sptr[], char ** eptr ) {
 | 
|---|
| 132 |         float re, im;
 | 
|---|
| 133 |         char * eeptr;
 | 
|---|
| 134 |         re = strtof( sptr, &eeptr );
 | 
|---|
| 135 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 136 |         im = strtof( eeptr, &eeptr );
 | 
|---|
| 137 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 138 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
 | 
|---|
| 139 |         return re + im * _Complex_I;
 | 
|---|
| 140 | } // strto
 | 
|---|
| 141 | 
 | 
|---|
| 142 | double _Complex strto( const char sptr[], char ** eptr ) {
 | 
|---|
| 143 |         double re, im;
 | 
|---|
| 144 |         char * eeptr;
 | 
|---|
| 145 |         re = strtod( sptr, &eeptr );
 | 
|---|
| 146 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 147 |         im = strtod( eeptr, &eeptr );
 | 
|---|
| 148 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 149 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
 | 
|---|
| 150 |         return re + im * _Complex_I;
 | 
|---|
| 151 | } // strto
 | 
|---|
| 152 | 
 | 
|---|
| 153 | long double _Complex strto( const char sptr[], char ** eptr ) {
 | 
|---|
| 154 |         long double re, im;
 | 
|---|
| 155 |         char * eeptr;
 | 
|---|
| 156 |         re = strtold( sptr, &eeptr );
 | 
|---|
| 157 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 158 |         im = strtold( eeptr, &eeptr );
 | 
|---|
| 159 |         if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 160 |         if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
 | 
|---|
| 161 |         return re + im * _Complex_I;
 | 
|---|
| 162 | } // strto
 | 
|---|
| 163 | 
 | 
|---|
| 164 | //---------------------------------------
 | 
|---|
| 165 | 
 | 
|---|
| 166 | forall( otype E | { int ?<?( E, E ); } ) {
 | 
|---|
| 167 |         E * bsearch( E key, const E * vals, size_t dim ) {
 | 
|---|
| 168 |                 int cmp( const void * t1, const void * t2 ) {
 | 
|---|
| 169 |                         return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
 | 
|---|
| 170 |                 } // cmp
 | 
|---|
| 171 |                 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
 | 
|---|
| 172 |         } // bsearch
 | 
|---|
| 173 | 
 | 
|---|
| 174 |         size_t bsearch( E key, const E * vals, size_t dim ) {
 | 
|---|
| 175 |                 E * result = bsearch( key, vals, dim );
 | 
|---|
| 176 |                 return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
 | 
|---|
| 177 |         } // bsearch
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         size_t bsearchl( E key, const E * vals, size_t dim ) {
 | 
|---|
| 180 |                 size_t l = 0, m, h = dim;
 | 
|---|
| 181 |                 while ( l < h ) {
 | 
|---|
| 182 |                         m = (l + h) / 2;
 | 
|---|
| 183 |                         if ( (E &)(vals[m]) < key ) {                           // cast away const
 | 
|---|
| 184 |                                 l = m + 1;
 | 
|---|
| 185 |                         } else {
 | 
|---|
| 186 |                                 h = m;
 | 
|---|
| 187 |                         } // if
 | 
|---|
| 188 |                 } // while
 | 
|---|
| 189 |                 return l;
 | 
|---|
| 190 |         } // bsearchl
 | 
|---|
| 191 | 
 | 
|---|
| 192 |         E * bsearchl( E key, const E * vals, size_t dim ) {
 | 
|---|
| 193 |                 size_t posn = bsearchl( key, vals, dim );
 | 
|---|
| 194 |                 return (E *)(&vals[posn]);                                              // cast away const
 | 
|---|
| 195 |         } // bsearchl
 | 
|---|
| 196 | 
 | 
|---|
| 197 |         size_t bsearchu( E key, const E * vals, size_t dim ) {
 | 
|---|
| 198 |                 size_t l = 0, m, h = dim;
 | 
|---|
| 199 |                 while ( l < h ) {
 | 
|---|
| 200 |                         m = (l + h) / 2;
 | 
|---|
| 201 |                         if ( ! ( key < (E &)(vals[m]) ) ) {                     // cast away const
 | 
|---|
| 202 |                                 l = m + 1;
 | 
|---|
| 203 |                         } else {
 | 
|---|
| 204 |                                 h = m;
 | 
|---|
| 205 |                         } // if
 | 
|---|
| 206 |                 } // while
 | 
|---|
| 207 |                 return l;
 | 
|---|
| 208 |         } // bsearchu
 | 
|---|
| 209 | 
 | 
|---|
| 210 |         E * bsearchu( E key, const E * vals, size_t dim ) {
 | 
|---|
| 211 |                 size_t posn = bsearchu( key, vals, dim );
 | 
|---|
| 212 |                 return (E *)(&vals[posn]);
 | 
|---|
| 213 |         } // bsearchu
 | 
|---|
| 214 | 
 | 
|---|
| 215 | 
 | 
|---|
| 216 |         void qsort( E * vals, size_t dim ) {
 | 
|---|
| 217 |                 int cmp( const void * t1, const void * t2 ) {
 | 
|---|
| 218 |                         return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
 | 
|---|
| 219 |                 } // cmp
 | 
|---|
| 220 |                 qsort( vals, dim, sizeof(E), cmp );
 | 
|---|
| 221 |         } // qsort
 | 
|---|
| 222 | } // distribution
 | 
|---|
| 223 | 
 | 
|---|
| 224 | 
 | 
|---|
| 225 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
 | 
|---|
| 226 |         E * bsearch( K key, const E * vals, size_t dim ) {
 | 
|---|
| 227 |                 int cmp( const void * t1, const void * t2 ) {
 | 
|---|
| 228 |                         return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
 | 
|---|
| 229 |                 } // cmp
 | 
|---|
| 230 |                 return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
 | 
|---|
| 231 |         } // bsearch
 | 
|---|
| 232 | 
 | 
|---|
| 233 |         size_t bsearch( K key, const E * vals, size_t dim ) {
 | 
|---|
| 234 |                 E * result = bsearch( key, vals, dim );
 | 
|---|
| 235 |                 return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
 | 
|---|
| 236 |         } // bsearch
 | 
|---|
| 237 | 
 | 
|---|
| 238 |         size_t bsearchl( K key, const E * vals, size_t dim ) {
 | 
|---|
| 239 |                 size_t l = 0, m, h = dim;
 | 
|---|
| 240 |                 while ( l < h ) {
 | 
|---|
| 241 |                         m = (l + h) / 2;
 | 
|---|
| 242 |                         if ( getKey( vals[m] ) < key ) {
 | 
|---|
| 243 |                                 l = m + 1;
 | 
|---|
| 244 |                         } else {
 | 
|---|
| 245 |                                 h = m;
 | 
|---|
| 246 |                         } // if
 | 
|---|
| 247 |                 } // while
 | 
|---|
| 248 |                 return l;
 | 
|---|
| 249 |         } // bsearchl
 | 
|---|
| 250 | 
 | 
|---|
| 251 |         E * bsearchl( K key, const E * vals, size_t dim ) {
 | 
|---|
| 252 |                 size_t posn = bsearchl( key, vals, dim );
 | 
|---|
| 253 |                 return (E *)(&vals[posn]);                                              // cast away const
 | 
|---|
| 254 |         } // bsearchl
 | 
|---|
| 255 | 
 | 
|---|
| 256 |         size_t bsearchu( K key, const E * vals, size_t dim ) {
 | 
|---|
| 257 |                 size_t l = 0, m, h = dim;
 | 
|---|
| 258 |                 while ( l < h ) {
 | 
|---|
| 259 |                         m = (l + h) / 2;
 | 
|---|
| 260 |                         if ( ! ( key < getKey( vals[m] ) ) ) {
 | 
|---|
| 261 |                                 l = m + 1;
 | 
|---|
| 262 |                         } else {
 | 
|---|
| 263 |                                 h = m;
 | 
|---|
| 264 |                         } // if
 | 
|---|
| 265 |                 } // while
 | 
|---|
| 266 |                 return l;
 | 
|---|
| 267 |         } // bsearchu
 | 
|---|
| 268 | 
 | 
|---|
| 269 |         E * bsearchu( K key, const E * vals, size_t dim ) {
 | 
|---|
| 270 |                 size_t posn = bsearchu( key, vals, dim );
 | 
|---|
| 271 |                 return (E *)(&vals[posn]);
 | 
|---|
| 272 |         } // bsearchu
 | 
|---|
| 273 | } // distribution
 | 
|---|
| 274 | 
 | 
|---|
| 275 | //---------------------------------------
 | 
|---|
| 276 | 
 | 
|---|
| 277 | extern "C" {                                                                                    // override C version
 | 
|---|
| 278 |         void srandom( unsigned int seed ) { srand48( (long int)seed ); }
 | 
|---|
| 279 |         long int random( void ) { return mrand48(); }
 | 
|---|
| 280 | } // extern "C"
 | 
|---|
| 281 | 
 | 
|---|
| 282 | float random( void ) { return (float)drand48(); }               // cast otherwise float uses lrand48
 | 
|---|
| 283 | double random( void ) { return drand48(); }
 | 
|---|
| 284 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 285 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 286 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 287 | 
 | 
|---|
| 288 | //---------------------------------------
 | 
|---|
| 289 | 
 | 
|---|
| 290 | bool threading_enabled(void) __attribute__((weak)) {
 | 
|---|
| 291 |         return false;
 | 
|---|
| 292 | }
 | 
|---|
| 293 | 
 | 
|---|
| 294 | // Local Variables: //
 | 
|---|
| 295 | // tab-width: 4 //
 | 
|---|
| 296 | // End: //
 | 
|---|