| 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 : Andrew Beach
|
|---|
| 12 | // Last Modified On : Tue Jun 2 16:46:00 2020
|
|---|
| 13 | // Update Count : 500
|
|---|
| 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 | { 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 | { void ^?{}( T & ); void delete( Params ); } )
|
|---|
| 89 | void delete( T * ptr, Params rest ) {
|
|---|
| 90 | delete( ptr );
|
|---|
| 91 | delete( rest );
|
|---|
| 92 | } // delete
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | // allocation/deallocation and constructor/destructor, array types
|
|---|
| 96 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
|
|---|
| 97 | T * anew( size_t dim, Params p ) {
|
|---|
| 98 | T * arr = alloc( dim );
|
|---|
| 99 | for ( unsigned int i = 0; i < dim; i += 1 ) {
|
|---|
| 100 | (arr[i]){ p }; // run constructor
|
|---|
| 101 | } // for
|
|---|
| 102 | return arr;
|
|---|
| 103 | } // anew
|
|---|
| 104 |
|
|---|
| 105 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
|
|---|
| 106 | void adelete( size_t dim, T arr[] ) {
|
|---|
| 107 | if ( arr ) { // ignore null
|
|---|
| 108 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
|---|
| 109 | ^(arr[i]){}; // run destructor
|
|---|
| 110 | } // for
|
|---|
| 111 | free( arr );
|
|---|
| 112 | } // if
|
|---|
| 113 | } // adelete
|
|---|
| 114 |
|
|---|
| 115 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
|
|---|
| 116 | void adelete( size_t dim, T arr[], Params rest ) {
|
|---|
| 117 | if ( arr ) { // ignore null
|
|---|
| 118 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
|---|
| 119 | ^(arr[i]){}; // run destructor
|
|---|
| 120 | } // for
|
|---|
| 121 | free( arr );
|
|---|
| 122 | } // if
|
|---|
| 123 | adelete( rest );
|
|---|
| 124 | } // adelete
|
|---|
| 125 |
|
|---|
| 126 | //---------------------------------------
|
|---|
| 127 |
|
|---|
| 128 | float _Complex strto( const char sptr[], char ** eptr ) {
|
|---|
| 129 | float re, im;
|
|---|
| 130 | char * eeptr;
|
|---|
| 131 | re = strtof( sptr, &eeptr );
|
|---|
| 132 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 133 | im = strtof( eeptr, &eeptr );
|
|---|
| 134 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 135 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 136 | return re + im * _Complex_I;
|
|---|
| 137 | } // strto
|
|---|
| 138 |
|
|---|
| 139 | double _Complex strto( const char sptr[], char ** eptr ) {
|
|---|
| 140 | double re, im;
|
|---|
| 141 | char * eeptr;
|
|---|
| 142 | re = strtod( sptr, &eeptr );
|
|---|
| 143 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 144 | im = strtod( eeptr, &eeptr );
|
|---|
| 145 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 146 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 147 | return re + im * _Complex_I;
|
|---|
| 148 | } // strto
|
|---|
| 149 |
|
|---|
| 150 | long double _Complex strto( const char sptr[], char ** eptr ) {
|
|---|
| 151 | long double re, im;
|
|---|
| 152 | char * eeptr;
|
|---|
| 153 | re = strtold( sptr, &eeptr );
|
|---|
| 154 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 155 | im = strtold( eeptr, &eeptr );
|
|---|
| 156 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 157 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 158 | return re + im * _Complex_I;
|
|---|
| 159 | } // strto
|
|---|
| 160 |
|
|---|
| 161 | //---------------------------------------
|
|---|
| 162 |
|
|---|
| 163 | forall( otype E | { int ?<?( E, E ); } ) {
|
|---|
| 164 | E * bsearch( E key, const E * vals, size_t dim ) {
|
|---|
| 165 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 166 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
|---|
| 167 | } // cmp
|
|---|
| 168 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
|---|
| 169 | } // bsearch
|
|---|
| 170 |
|
|---|
| 171 | size_t bsearch( E key, const E * vals, size_t dim ) {
|
|---|
| 172 | E * result = bsearch( key, vals, dim );
|
|---|
| 173 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
|---|
| 174 | } // bsearch
|
|---|
| 175 |
|
|---|
| 176 | size_t bsearchl( E key, const E * vals, size_t dim ) {
|
|---|
| 177 | size_t l = 0, m, h = dim;
|
|---|
| 178 | while ( l < h ) {
|
|---|
| 179 | m = (l + h) / 2;
|
|---|
| 180 | if ( (E &)(vals[m]) < key ) { // cast away const
|
|---|
| 181 | l = m + 1;
|
|---|
| 182 | } else {
|
|---|
| 183 | h = m;
|
|---|
| 184 | } // if
|
|---|
| 185 | } // while
|
|---|
| 186 | return l;
|
|---|
| 187 | } // bsearchl
|
|---|
| 188 |
|
|---|
| 189 | E * bsearchl( E key, const E * vals, size_t dim ) {
|
|---|
| 190 | size_t posn = bsearchl( key, vals, dim );
|
|---|
| 191 | return (E *)(&vals[posn]); // cast away const
|
|---|
| 192 | } // bsearchl
|
|---|
| 193 |
|
|---|
| 194 | size_t bsearchu( E key, const E * vals, size_t dim ) {
|
|---|
| 195 | size_t l = 0, m, h = dim;
|
|---|
| 196 | while ( l < h ) {
|
|---|
| 197 | m = (l + h) / 2;
|
|---|
| 198 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const
|
|---|
| 199 | l = m + 1;
|
|---|
| 200 | } else {
|
|---|
| 201 | h = m;
|
|---|
| 202 | } // if
|
|---|
| 203 | } // while
|
|---|
| 204 | return l;
|
|---|
| 205 | } // bsearchu
|
|---|
| 206 |
|
|---|
| 207 | E * bsearchu( E key, const E * vals, size_t dim ) {
|
|---|
| 208 | size_t posn = bsearchu( key, vals, dim );
|
|---|
| 209 | return (E *)(&vals[posn]);
|
|---|
| 210 | } // bsearchu
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | void qsort( E * vals, size_t dim ) {
|
|---|
| 214 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 215 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
|---|
| 216 | } // cmp
|
|---|
| 217 | qsort( vals, dim, sizeof(E), cmp );
|
|---|
| 218 | } // qsort
|
|---|
| 219 | } // distribution
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
|---|
| 223 | E * bsearch( K key, const E * vals, size_t dim ) {
|
|---|
| 224 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 225 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
|
|---|
| 226 | } // cmp
|
|---|
| 227 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
|---|
| 228 | } // bsearch
|
|---|
| 229 |
|
|---|
| 230 | size_t bsearch( K key, const E * vals, size_t dim ) {
|
|---|
| 231 | E * result = bsearch( key, vals, dim );
|
|---|
| 232 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
|---|
| 233 | } // bsearch
|
|---|
| 234 |
|
|---|
| 235 | size_t bsearchl( K key, const E * vals, size_t dim ) {
|
|---|
| 236 | size_t l = 0, m, h = dim;
|
|---|
| 237 | while ( l < h ) {
|
|---|
| 238 | m = (l + h) / 2;
|
|---|
| 239 | if ( getKey( vals[m] ) < key ) {
|
|---|
| 240 | l = m + 1;
|
|---|
| 241 | } else {
|
|---|
| 242 | h = m;
|
|---|
| 243 | } // if
|
|---|
| 244 | } // while
|
|---|
| 245 | return l;
|
|---|
| 246 | } // bsearchl
|
|---|
| 247 |
|
|---|
| 248 | E * bsearchl( K key, const E * vals, size_t dim ) {
|
|---|
| 249 | size_t posn = bsearchl( key, vals, dim );
|
|---|
| 250 | return (E *)(&vals[posn]); // cast away const
|
|---|
| 251 | } // bsearchl
|
|---|
| 252 |
|
|---|
| 253 | size_t bsearchu( K key, const E * vals, size_t dim ) {
|
|---|
| 254 | size_t l = 0, m, h = dim;
|
|---|
| 255 | while ( l < h ) {
|
|---|
| 256 | m = (l + h) / 2;
|
|---|
| 257 | if ( ! ( key < getKey( vals[m] ) ) ) {
|
|---|
| 258 | l = m + 1;
|
|---|
| 259 | } else {
|
|---|
| 260 | h = m;
|
|---|
| 261 | } // if
|
|---|
| 262 | } // while
|
|---|
| 263 | return l;
|
|---|
| 264 | } // bsearchu
|
|---|
| 265 |
|
|---|
| 266 | E * bsearchu( K key, const E * vals, size_t dim ) {
|
|---|
| 267 | size_t posn = bsearchu( key, vals, dim );
|
|---|
| 268 | return (E *)(&vals[posn]);
|
|---|
| 269 | } // bsearchu
|
|---|
| 270 | } // distribution
|
|---|
| 271 |
|
|---|
| 272 | //---------------------------------------
|
|---|
| 273 |
|
|---|
| 274 | extern "C" { // override C version
|
|---|
| 275 | void srandom( unsigned int seed ) { srand48( (long int)seed ); }
|
|---|
| 276 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES
|
|---|
| 277 | } // extern "C"
|
|---|
| 278 |
|
|---|
| 279 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
|---|
| 280 | double random( void ) { return drand48(); }
|
|---|
| 281 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
|---|
| 282 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
|---|
| 283 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
|---|
| 284 |
|
|---|
| 285 | //---------------------------------------
|
|---|
| 286 |
|
|---|
| 287 | bool threading_enabled(void) __attribute__((weak)) {
|
|---|
| 288 | return false;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | // Local Variables: //
|
|---|
| 292 | // tab-width: 4 //
|
|---|
| 293 | // End: //
|
|---|