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