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