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