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