[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 |
---|
[bbe1a87] | 12 | // Last Modified On : Tue Jul 3 08:17:30 2018 |
---|
| 13 | // Update Count : 457 |
---|
[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 | |
---|
[3ce0d440] | 132 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 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 ); |
---|
| 138 | } // bsearch |
---|
| 139 | |
---|
| 140 | size_t bsearch( E key, const E * vals, size_t dim ) { |
---|
| 141 | E * result = bsearch( key, vals, dim ); |
---|
| 142 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 143 | } // bsearch |
---|
| 144 | |
---|
| 145 | size_t bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 146 | size_t l = 0, m, h = dim; |
---|
| 147 | while ( l < h ) { |
---|
| 148 | m = (l + h) / 2; |
---|
| 149 | if ( (E &)(vals[m]) < key ) { // cast away const |
---|
| 150 | l = m + 1; |
---|
| 151 | } else { |
---|
| 152 | h = m; |
---|
| 153 | } // if |
---|
| 154 | } // while |
---|
| 155 | return l; |
---|
| 156 | } // bsearchl |
---|
| 157 | |
---|
| 158 | E * bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 159 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 160 | return (E *)(&vals[posn]); // cast away const |
---|
| 161 | } // bsearchl |
---|
| 162 | |
---|
| 163 | size_t bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 164 | size_t l = 0, m, h = dim; |
---|
| 165 | while ( l < h ) { |
---|
| 166 | m = (l + h) / 2; |
---|
| 167 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const |
---|
| 168 | l = m + 1; |
---|
| 169 | } else { |
---|
| 170 | h = m; |
---|
| 171 | } // if |
---|
| 172 | } // while |
---|
| 173 | return l; |
---|
| 174 | } // bsearchu |
---|
| 175 | |
---|
| 176 | E * bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 177 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 178 | return (E *)(&vals[posn]); |
---|
| 179 | } // bsearchu |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | void qsort( E * vals, size_t dim ) { |
---|
| 183 | int cmp( const void * t1, const void * t2 ) { |
---|
| 184 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
| 185 | } // cmp |
---|
| 186 | qsort( vals, dim, sizeof(E), cmp ); |
---|
| 187 | } // qsort |
---|
| 188 | } // distribution |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
| 192 | E * bsearch( K key, const E * vals, size_t dim ) { |
---|
| 193 | int cmp( const void * t1, const void * t2 ) { |
---|
| 194 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0; |
---|
| 195 | } // cmp |
---|
| 196 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
| 197 | } // bsearch |
---|
| 198 | |
---|
| 199 | size_t bsearch( K key, const E * vals, size_t dim ) { |
---|
| 200 | E * result = bsearch( key, vals, dim ); |
---|
| 201 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 202 | } // bsearch |
---|
| 203 | |
---|
| 204 | size_t bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 205 | size_t l = 0, m, h = dim; |
---|
| 206 | while ( l < h ) { |
---|
| 207 | m = (l + h) / 2; |
---|
| 208 | if ( getKey( vals[m] ) < key ) { |
---|
| 209 | l = m + 1; |
---|
| 210 | } else { |
---|
| 211 | h = m; |
---|
| 212 | } // if |
---|
| 213 | } // while |
---|
| 214 | return l; |
---|
| 215 | } // bsearchl |
---|
| 216 | |
---|
| 217 | E * bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 218 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 219 | return (E *)(&vals[posn]); // cast away const |
---|
| 220 | } // bsearchl |
---|
| 221 | |
---|
| 222 | size_t bsearchu( K key, const E * vals, size_t dim ) { |
---|
| 223 | size_t l = 0, m, h = dim; |
---|
| 224 | while ( l < h ) { |
---|
| 225 | m = (l + h) / 2; |
---|
| 226 | if ( ! ( key < getKey( vals[m] ) ) ) { |
---|
| 227 | l = m + 1; |
---|
| 228 | } else { |
---|
| 229 | h = m; |
---|
| 230 | } // if |
---|
| 231 | } // while |
---|
| 232 | return l; |
---|
| 233 | } // bsearchu |
---|
| 234 | |
---|
| 235 | E * bsearchu( K key, const E * vals, size_t dim ) { |
---|
| 236 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 237 | return (E *)(&vals[posn]); |
---|
| 238 | } // bsearchu |
---|
| 239 | } // distribution |
---|
[bd85400] | 240 | |
---|
| 241 | //--------------------------------------- |
---|
| 242 | |
---|
[5ea26ed] | 243 | [ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; } |
---|
| 244 | [ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; } |
---|
| 245 | [ 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] | 246 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
[5ea26ed] | 247 | [ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; } |
---|
[bd85400] | 248 | |
---|
| 249 | //--------------------------------------- |
---|
| 250 | |
---|
[bbe1a87] | 251 | extern "C" { // override C version |
---|
| 252 | void srandom( unsigned int seed ) { srand48( (long int)seed ); } |
---|
| 253 | long int random( void ) { return mrand48(); } |
---|
| 254 | } // extern "C" |
---|
| 255 | |
---|
[e672372] | 256 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 |
---|
[70e4895d] | 257 | double random( void ) { return drand48(); } |
---|
| 258 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 259 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
[e672372] | 260 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
[a9f2c13] | 261 | |
---|
[bd85400] | 262 | |
---|
| 263 | // Local Variables: // |
---|
| 264 | // tab-width: 4 // |
---|
| 265 | // End: // |
---|