[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 |
---|
[e310238] | 11 | // Last Modified By : Peter A. Buhr |
---|
[94429f8] | 12 | // Last Modified On : Thu Nov 12 07:46:09 2020 |
---|
| 13 | // Update Count : 503 |
---|
[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 | |
---|
[94429f8] | 28 | // Cforall allocation/deallocation and constructor/destructor, array types |
---|
[627f585] | 29 | |
---|
[94429f8] | 30 | forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } ) |
---|
| 31 | T * anew( size_t dim, TT p ) { |
---|
[6887a99] | 32 | T * arr = alloc( dim ); |
---|
[6065b3aa] | 33 | for ( unsigned int i = 0; i < dim; i += 1 ) { |
---|
[a493682] | 34 | (arr[i]){ p }; // run constructor |
---|
[6065b3aa] | 35 | } // for |
---|
| 36 | return arr; |
---|
| 37 | } // anew |
---|
| 38 | |
---|
[aca65621] | 39 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) |
---|
[45444c3] | 40 | void adelete( T arr[] ) { |
---|
[6065b3aa] | 41 | if ( arr ) { // ignore null |
---|
[45444c3] | 42 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[6065b3aa] | 43 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 44 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 45 | } // for |
---|
| 46 | free( arr ); |
---|
| 47 | } // if |
---|
| 48 | } // adelete |
---|
| 49 | |
---|
[94429f8] | 50 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } ) |
---|
| 51 | void adelete( T arr[], TT rest ) { |
---|
[6065b3aa] | 52 | if ( arr ) { // ignore null |
---|
[45444c3] | 53 | size_t dim = malloc_size( arr ) / sizeof( T ); |
---|
[6065b3aa] | 54 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned |
---|
[a493682] | 55 | ^(arr[i]){}; // run destructor |
---|
[6065b3aa] | 56 | } // for |
---|
| 57 | free( arr ); |
---|
| 58 | } // if |
---|
| 59 | adelete( rest ); |
---|
| 60 | } // adelete |
---|
| 61 | |
---|
[bd85400] | 62 | //--------------------------------------- |
---|
| 63 | |
---|
[e3fea42] | 64 | float _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 65 | float re, im; |
---|
[e672372] | 66 | char * eeptr; |
---|
| 67 | re = strtof( sptr, &eeptr ); |
---|
[bbf3fda] | 68 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 69 | im = strtof( eeptr, &eeptr ); |
---|
[bbf3fda] | 70 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[e672372] | 71 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } |
---|
[bd85400] | 72 | return re + im * _Complex_I; |
---|
[f3ddc21] | 73 | } // strto |
---|
| 74 | |
---|
[e3fea42] | 75 | double _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 76 | double re, im; |
---|
[e672372] | 77 | char * eeptr; |
---|
| 78 | re = strtod( sptr, &eeptr ); |
---|
[bbf3fda] | 79 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 80 | im = strtod( eeptr, &eeptr ); |
---|
[bbf3fda] | 81 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[e672372] | 82 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } |
---|
[bd85400] | 83 | return re + im * _Complex_I; |
---|
[f3ddc21] | 84 | } // strto |
---|
| 85 | |
---|
[e3fea42] | 86 | long double _Complex strto( const char sptr[], char ** eptr ) { |
---|
[bd85400] | 87 | long double re, im; |
---|
[e672372] | 88 | char * eeptr; |
---|
| 89 | re = strtold( sptr, &eeptr ); |
---|
[bbf3fda] | 90 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 91 | im = strtold( eeptr, &eeptr ); |
---|
[bbf3fda] | 92 | if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[e672372] | 93 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } |
---|
[bd85400] | 94 | return re + im * _Complex_I; |
---|
[f3ddc21] | 95 | } // strto |
---|
[bd85400] | 96 | |
---|
| 97 | //--------------------------------------- |
---|
| 98 | |
---|
[3ce0d440] | 99 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 100 | E * bsearch( E key, const E * vals, size_t dim ) { |
---|
| 101 | int cmp( const void * t1, const void * t2 ) { |
---|
| 102 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
| 103 | } // cmp |
---|
| 104 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
| 105 | } // bsearch |
---|
| 106 | |
---|
| 107 | size_t bsearch( E key, const E * vals, size_t dim ) { |
---|
| 108 | E * result = bsearch( key, vals, dim ); |
---|
| 109 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 110 | } // bsearch |
---|
| 111 | |
---|
| 112 | size_t bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 113 | size_t l = 0, m, h = dim; |
---|
| 114 | while ( l < h ) { |
---|
| 115 | m = (l + h) / 2; |
---|
| 116 | if ( (E &)(vals[m]) < key ) { // cast away const |
---|
| 117 | l = m + 1; |
---|
| 118 | } else { |
---|
| 119 | h = m; |
---|
| 120 | } // if |
---|
| 121 | } // while |
---|
| 122 | return l; |
---|
| 123 | } // bsearchl |
---|
| 124 | |
---|
| 125 | E * bsearchl( E key, const E * vals, size_t dim ) { |
---|
| 126 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 127 | return (E *)(&vals[posn]); // cast away const |
---|
| 128 | } // bsearchl |
---|
| 129 | |
---|
| 130 | size_t bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 131 | size_t l = 0, m, h = dim; |
---|
| 132 | while ( l < h ) { |
---|
| 133 | m = (l + h) / 2; |
---|
| 134 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const |
---|
| 135 | l = m + 1; |
---|
| 136 | } else { |
---|
| 137 | h = m; |
---|
| 138 | } // if |
---|
| 139 | } // while |
---|
| 140 | return l; |
---|
| 141 | } // bsearchu |
---|
| 142 | |
---|
| 143 | E * bsearchu( E key, const E * vals, size_t dim ) { |
---|
| 144 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 145 | return (E *)(&vals[posn]); |
---|
| 146 | } // bsearchu |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | void qsort( E * vals, size_t dim ) { |
---|
| 150 | int cmp( const void * t1, const void * t2 ) { |
---|
| 151 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0; |
---|
| 152 | } // cmp |
---|
| 153 | qsort( vals, dim, sizeof(E), cmp ); |
---|
| 154 | } // qsort |
---|
| 155 | } // distribution |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
| 159 | E * bsearch( K key, const E * vals, size_t dim ) { |
---|
| 160 | int cmp( const void * t1, const void * t2 ) { |
---|
| 161 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0; |
---|
| 162 | } // cmp |
---|
| 163 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp ); |
---|
| 164 | } // bsearch |
---|
| 165 | |
---|
| 166 | size_t bsearch( K key, const E * vals, size_t dim ) { |
---|
| 167 | E * result = bsearch( key, vals, dim ); |
---|
| 168 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E) |
---|
| 169 | } // bsearch |
---|
| 170 | |
---|
| 171 | size_t bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 172 | size_t l = 0, m, h = dim; |
---|
| 173 | while ( l < h ) { |
---|
| 174 | m = (l + h) / 2; |
---|
| 175 | if ( getKey( vals[m] ) < key ) { |
---|
| 176 | l = m + 1; |
---|
| 177 | } else { |
---|
| 178 | h = m; |
---|
| 179 | } // if |
---|
| 180 | } // while |
---|
| 181 | return l; |
---|
| 182 | } // bsearchl |
---|
| 183 | |
---|
| 184 | E * bsearchl( K key, const E * vals, size_t dim ) { |
---|
| 185 | size_t posn = bsearchl( key, vals, dim ); |
---|
| 186 | return (E *)(&vals[posn]); // cast away const |
---|
| 187 | } // bsearchl |
---|
| 188 | |
---|
| 189 | size_t bsearchu( K key, const E * vals, size_t dim ) { |
---|
| 190 | size_t l = 0, m, h = dim; |
---|
| 191 | while ( l < h ) { |
---|
| 192 | m = (l + h) / 2; |
---|
| 193 | if ( ! ( key < getKey( vals[m] ) ) ) { |
---|
| 194 | l = m + 1; |
---|
| 195 | } else { |
---|
| 196 | h = m; |
---|
| 197 | } // if |
---|
| 198 | } // while |
---|
| 199 | return l; |
---|
| 200 | } // bsearchu |
---|
| 201 | |
---|
| 202 | E * bsearchu( K key, const E * vals, size_t dim ) { |
---|
| 203 | size_t posn = bsearchu( key, vals, dim ); |
---|
| 204 | return (E *)(&vals[posn]); |
---|
| 205 | } // bsearchu |
---|
| 206 | } // distribution |
---|
[bd85400] | 207 | |
---|
| 208 | //--------------------------------------- |
---|
| 209 | |
---|
[bbe1a87] | 210 | extern "C" { // override C version |
---|
| 211 | void srandom( unsigned int seed ) { srand48( (long int)seed ); } |
---|
[4e7c0fc0] | 212 | long int random( void ) { return mrand48(); } // GENERATES POSITIVE AND NEGATIVE VALUES |
---|
[bbe1a87] | 213 | } // extern "C" |
---|
| 214 | |
---|
[e672372] | 215 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 |
---|
[70e4895d] | 216 | double random( void ) { return drand48(); } |
---|
| 217 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } |
---|
| 218 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); } |
---|
[e672372] | 219 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } |
---|
[a9f2c13] | 220 | |
---|
[2026bb6] | 221 | //--------------------------------------- |
---|
| 222 | |
---|
| 223 | bool threading_enabled(void) __attribute__((weak)) { |
---|
| 224 | return false; |
---|
| 225 | } |
---|
[bd85400] | 226 | |
---|
| 227 | // Local Variables: // |
---|
| 228 | // tab-width: 4 // |
---|
| 229 | // End: // |
---|