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