[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 | // |
---|
[bb82c03] | 7 | // stdlib -- |
---|
[bd85400] | 8 | // |
---|
| 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Thu Jan 28 17:12:35 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[cafb687] | 12 | // Last Modified On : Sun Oct 20 22:57:33 2019 |
---|
| 13 | // Update Count : 390 |
---|
[bd85400] | 14 | // |
---|
| 15 | |
---|
[53a6c2a] | 16 | #pragma once |
---|
[17e5e2b] | 17 | |
---|
[2026bb6] | 18 | #include "bits/defs.hfa" |
---|
[d6b03b7] | 19 | #include "bits/align.hfa" |
---|
[2026bb6] | 20 | |
---|
[d46ed6e] | 21 | #include <stdlib.h> // *alloc, strto*, ato* |
---|
[d6b03b7] | 22 | |
---|
[3ce0d440] | 23 | extern "C" { |
---|
[57fc7d8] | 24 | void * memalign( size_t align, size_t size ); // malloc.h |
---|
[b9c04946] | 25 | void * memset( void * dest, int fill, size_t size ); // string.h |
---|
[57fc7d8] | 26 | void * memcpy( void * dest, const void * src, size_t size ); // string.h |
---|
[cafb687] | 27 | void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap |
---|
[3ce0d440] | 28 | } // extern "C" |
---|
[e672372] | 29 | |
---|
[bd85400] | 30 | //--------------------------------------- |
---|
| 31 | |
---|
[45161b4d] | 32 | #ifndef EXIT_FAILURE |
---|
| 33 | #define EXIT_FAILURE 1 // failing exit status |
---|
| 34 | #define EXIT_SUCCESS 0 // successful exit status |
---|
| 35 | #endif // ! EXIT_FAILURE |
---|
| 36 | |
---|
| 37 | //--------------------------------------- |
---|
| 38 | |
---|
[74b19fb] | 39 | static inline forall( dtype T | sized(T) ) { |
---|
[3ce0d440] | 40 | // C dynamic allocation |
---|
| 41 | |
---|
[74b19fb] | 42 | T * malloc( void ) { |
---|
[d6b03b7] | 43 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc |
---|
| 44 | else return (T *)memalign( _Alignof(T), sizeof(T) ); |
---|
[74b19fb] | 45 | } // malloc |
---|
| 46 | |
---|
| 47 | T * calloc( size_t dim ) { |
---|
[d6b03b7] | 48 | if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc |
---|
| 49 | else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); |
---|
[74b19fb] | 50 | } // calloc |
---|
| 51 | |
---|
| 52 | T * realloc( T * ptr, size_t size ) { |
---|
[d6b03b7] | 53 | if ( unlikely( ptr == 0 ) ) return malloc(); |
---|
[cafb687] | 54 | return (T *)(void *)realloc( (void *)ptr, size ); // C realloc |
---|
[74b19fb] | 55 | } // realloc |
---|
| 56 | |
---|
| 57 | T * memalign( size_t align ) { |
---|
[cafb687] | 58 | return (T *)memalign( align, sizeof(T) ); // C memalign |
---|
[74b19fb] | 59 | } // memalign |
---|
| 60 | |
---|
| 61 | T * aligned_alloc( size_t align ) { |
---|
[cafb687] | 62 | return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc |
---|
[74b19fb] | 63 | } // aligned_alloc |
---|
| 64 | |
---|
| 65 | int posix_memalign( T ** ptr, size_t align ) { |
---|
| 66 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign |
---|
| 67 | } // posix_memalign |
---|
| 68 | |
---|
[3ce0d440] | 69 | // Cforall dynamic allocation |
---|
[74b19fb] | 70 | |
---|
| 71 | T * alloc( void ) { |
---|
[d6b03b7] | 72 | return malloc(); |
---|
[74b19fb] | 73 | } // alloc |
---|
| 74 | |
---|
[cafb687] | 75 | T * alloc( size_t dim ) { |
---|
| 76 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); |
---|
| 77 | else return (T *)memalign( _Alignof(T), dim * sizeof(T) ); |
---|
[74b19fb] | 78 | } // alloc |
---|
| 79 | |
---|
[cafb687] | 80 | T * alloc( T ptr[], size_t dim ) { // realloc |
---|
| 81 | return realloc( ptr, dim * sizeof(T) ); |
---|
[7df201c] | 82 | } // alloc |
---|
| 83 | |
---|
[cafb687] | 84 | T * alloc_set( char fill ) { |
---|
| 85 | return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value |
---|
| 86 | } // alloc |
---|
| 87 | |
---|
| 88 | T * alloc_set( T fill ) { |
---|
| 89 | return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value |
---|
[74b19fb] | 90 | } // alloc |
---|
| 91 | |
---|
[cafb687] | 92 | T * alloc_set( size_t dim, char fill ) { |
---|
[d6b03b7] | 93 | return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value |
---|
[74b19fb] | 94 | } // alloc |
---|
| 95 | |
---|
[cafb687] | 96 | T * alloc_set( size_t dim, T fill ) { |
---|
[7df201c] | 97 | T * r = (T *)alloc( dim ); |
---|
| 98 | for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value |
---|
| 99 | return r; |
---|
| 100 | } // alloc |
---|
| 101 | |
---|
[cafb687] | 102 | T * alloc_set( size_t dim, const T fill[] ) { |
---|
[7df201c] | 103 | return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value |
---|
| 104 | } // alloc |
---|
[74b19fb] | 105 | } // distribution |
---|
[6065b3aa] | 106 | |
---|
[cafb687] | 107 | forall( dtype T | sized(T) ) { |
---|
| 108 | T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill |
---|
| 109 | } // distribution |
---|
[f3fc631f] | 110 | |
---|
[3ce0d440] | 111 | static inline forall( dtype T | sized(T) ) { |
---|
[cafb687] | 112 | T * alloc_align( size_t align ) { |
---|
[3ce0d440] | 113 | return (T *)memalign( align, sizeof(T) ); |
---|
[cafb687] | 114 | } // alloc_align |
---|
[3ce0d440] | 115 | |
---|
[cafb687] | 116 | T * alloc_align( size_t align, size_t dim ) { |
---|
[3ce0d440] | 117 | return (T *)memalign( align, dim * sizeof(T) ); |
---|
[cafb687] | 118 | } // alloc_align |
---|
| 119 | |
---|
| 120 | T * alloc_align_set( size_t align, char fill ) { |
---|
| 121 | return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value |
---|
| 122 | } // alloc_align |
---|
[3ce0d440] | 123 | |
---|
[cafb687] | 124 | T * alloc_align_set( size_t align, T fill ) { |
---|
| 125 | return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value |
---|
| 126 | } // alloc_align |
---|
[d6b03b7] | 127 | |
---|
[cafb687] | 128 | T * alloc_align_set( size_t align, size_t dim, char fill ) { |
---|
| 129 | return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value |
---|
| 130 | } // alloc_align |
---|
| 131 | |
---|
| 132 | T * alloc_align_set( size_t align, size_t dim, T fill ) { |
---|
| 133 | T * r = (T *)alloc_align( align, dim ); |
---|
| 134 | for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value |
---|
| 135 | return r; |
---|
| 136 | } // alloc_align |
---|
| 137 | |
---|
| 138 | T * alloc_align_set( size_t align, size_t dim, const T fill[] ) { |
---|
| 139 | return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); |
---|
| 140 | } // alloc_align |
---|
| 141 | } // distribution |
---|
| 142 | |
---|
| 143 | forall( dtype T | sized(T) ) { |
---|
| 144 | T * alloc_align( T ptr[], size_t align ); // realign |
---|
| 145 | T * alloc_align( T ptr[], size_t align, size_t dim ); // aligned realloc array |
---|
| 146 | T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill |
---|
| 147 | } // distribution |
---|
[3ce0d440] | 148 | |
---|
| 149 | static inline forall( dtype T | sized(T) ) { |
---|
| 150 | // data, non-array types |
---|
[b9c04946] | 151 | T * memset( T * dest, char fill ) { |
---|
| 152 | return (T *)memset( dest, fill, sizeof(T) ); |
---|
[3ce0d440] | 153 | } // memset |
---|
| 154 | |
---|
| 155 | T * memcpy( T * dest, const T * src ) { |
---|
| 156 | return (T *)memcpy( dest, src, sizeof(T) ); |
---|
| 157 | } // memcpy |
---|
| 158 | } // distribution |
---|
| 159 | |
---|
| 160 | static inline forall( dtype T | sized(T) ) { |
---|
| 161 | // data, array types |
---|
[b9c04946] | 162 | T * amemset( T dest[], char fill, size_t dim ) { |
---|
| 163 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset |
---|
| 164 | } // amemset |
---|
[3ce0d440] | 165 | |
---|
[b9c04946] | 166 | T * amemcpy( T dest[], const T src[], size_t dim ) { |
---|
[3ce0d440] | 167 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy |
---|
[b9c04946] | 168 | } // amemcpy |
---|
[3ce0d440] | 169 | } // distribution |
---|
[f3fc631f] | 170 | |
---|
[6065b3aa] | 171 | // allocation/deallocation and constructor/destructor, non-array types |
---|
[aca65621] | 172 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); |
---|
[83a071f9] | 173 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); |
---|
| 174 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); |
---|
[627f585] | 175 | |
---|
[6065b3aa] | 176 | // allocation/deallocation and constructor/destructor, array types |
---|
[aca65621] | 177 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); |
---|
| 178 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); |
---|
| 179 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); |
---|
[6065b3aa] | 180 | |
---|
[bd85400] | 181 | //--------------------------------------- |
---|
| 182 | |
---|
[57fc7d8] | 183 | static inline { |
---|
| 184 | int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } |
---|
| 185 | unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } |
---|
| 186 | long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } |
---|
| 187 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } |
---|
| 188 | long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } |
---|
| 189 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } |
---|
| 190 | |
---|
| 191 | float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } |
---|
| 192 | double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } |
---|
| 193 | long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } |
---|
| 194 | } // distribution |
---|
[e672372] | 195 | |
---|
[bd85400] | 196 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
| 197 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 198 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 199 | |
---|
[57fc7d8] | 200 | static inline { |
---|
[5ea5b28] | 201 | int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); } |
---|
[57fc7d8] | 202 | unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } |
---|
| 203 | long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } |
---|
| 204 | unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } |
---|
| 205 | long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } |
---|
| 206 | unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } |
---|
| 207 | |
---|
| 208 | float ato( const char * sptr ) { return strtof( sptr, 0 ); } |
---|
| 209 | double ato( const char * sptr ) { return strtod( sptr, 0 ); } |
---|
| 210 | long double ato( const char * sptr ) { return strtold( sptr, 0 ); } |
---|
| 211 | |
---|
| 212 | float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 213 | double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 214 | long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } |
---|
| 215 | } // distribution |
---|
[e672372] | 216 | |
---|
[bd85400] | 217 | //--------------------------------------- |
---|
| 218 | |
---|
[3ce0d440] | 219 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 220 | E * bsearch( E key, const E * vals, size_t dim ); |
---|
| 221 | size_t bsearch( E key, const E * vals, size_t dim ); |
---|
| 222 | E * bsearchl( E key, const E * vals, size_t dim ); |
---|
| 223 | size_t bsearchl( E key, const E * vals, size_t dim ); |
---|
| 224 | E * bsearchu( E key, const E * vals, size_t dim ); |
---|
| 225 | size_t bsearchu( E key, const E * vals, size_t dim ); |
---|
| 226 | } // distribution |
---|
[9c47a47] | 227 | |
---|
[3ce0d440] | 228 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) { |
---|
| 229 | E * bsearch( K key, const E * vals, size_t dim ); |
---|
| 230 | size_t bsearch( K key, const E * vals, size_t dim ); |
---|
| 231 | E * bsearchl( K key, const E * vals, size_t dim ); |
---|
| 232 | size_t bsearchl( K key, const E * vals, size_t dim ); |
---|
| 233 | E * bsearchu( K key, const E * vals, size_t dim ); |
---|
| 234 | size_t bsearchu( K key, const E * vals, size_t dim ); |
---|
| 235 | } // distribution |
---|
[bd85400] | 236 | |
---|
[b9c04946] | 237 | forall( otype E | { int ?<?( E, E ); } ) { |
---|
| 238 | void qsort( E * vals, size_t dim ); |
---|
| 239 | } // distribution |
---|
| 240 | |
---|
[bd85400] | 241 | //--------------------------------------- |
---|
| 242 | |
---|
[bbe1a87] | 243 | extern "C" { // override C version |
---|
| 244 | void srandom( unsigned int seed ); |
---|
| 245 | long int random( void ); |
---|
| 246 | } // extern "C" |
---|
| 247 | |
---|
| 248 | static inline { |
---|
| 249 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
| 250 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u) |
---|
| 251 | unsigned long int random( void ) { return lrand48(); } |
---|
| 252 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u) |
---|
| 253 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u) |
---|
| 254 | |
---|
| 255 | char random( void ) { return (unsigned long int)random(); } |
---|
| 256 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u) |
---|
| 257 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 258 | int random( void ) { return (long int)random(); } |
---|
| 259 | int random( int u ) { return random( (long int)u ); } // [0,u] |
---|
| 260 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u) |
---|
| 261 | unsigned int random( void ) { return (unsigned long int)random(); } |
---|
| 262 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u] |
---|
| 263 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u) |
---|
| 264 | } // distribution |
---|
| 265 | |
---|
| 266 | float random( void ); // [0.0, 1.0) |
---|
| 267 | double random( void ); // [0.0, 1.0) |
---|
| 268 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 269 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
| 270 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i |
---|
[bd85400] | 271 | |
---|
| 272 | //--------------------------------------- |
---|
| 273 | |
---|
[58b6d1b] | 274 | #include "common.hfa" |
---|
[bd85400] | 275 | |
---|
[2026bb6] | 276 | //--------------------------------------- |
---|
| 277 | |
---|
| 278 | extern bool threading_enabled(void) OPTIONAL_THREAD; |
---|
| 279 | |
---|
[bd85400] | 280 | // Local Variables: // |
---|
| 281 | // mode: c // |
---|
| 282 | // tab-width: 4 // |
---|
| 283 | // End: // |
---|