[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
|
---|
[3ce0d440] | 12 | // Last Modified On : Sat Jun 2 08:46:35 2018
|
---|
| 13 | // Update Count : 306
|
---|
[bd85400] | 14 | //
|
---|
| 15 |
|
---|
[53a6c2a] | 16 | #pragma once
|
---|
[17e5e2b] | 17 |
|
---|
[3ce0d440] | 18 | #include <stdlib.h> // allocation, strto*, *abs
|
---|
| 19 | extern "C" {
|
---|
| 20 | void * memalign( size_t align, size_t size );
|
---|
| 21 | void * aligned_alloc( size_t align, size_t size );
|
---|
| 22 | void * memset( void * dest, int c, size_t size );
|
---|
| 23 | } // extern "C"
|
---|
[e672372] | 24 |
|
---|
[bd85400] | 25 | //---------------------------------------
|
---|
| 26 |
|
---|
[45161b4d] | 27 | #ifndef EXIT_FAILURE
|
---|
| 28 | #define EXIT_FAILURE 1 // failing exit status
|
---|
| 29 | #define EXIT_SUCCESS 0 // successful exit status
|
---|
| 30 | #endif // ! EXIT_FAILURE
|
---|
| 31 |
|
---|
| 32 | //---------------------------------------
|
---|
| 33 |
|
---|
[74b19fb] | 34 | static inline forall( dtype T | sized(T) ) {
|
---|
[3ce0d440] | 35 | // C dynamic allocation
|
---|
| 36 |
|
---|
[74b19fb] | 37 | T * malloc( void ) {
|
---|
| 38 | // printf( "* malloc\n" );
|
---|
| 39 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 40 | } // malloc
|
---|
| 41 |
|
---|
| 42 | // T & malloc( void ) {
|
---|
| 43 | // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 44 | // printf( "& malloc %p\n", &p );
|
---|
| 45 | // return p;
|
---|
| 46 | // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 47 | // } // malloc
|
---|
| 48 |
|
---|
| 49 | T * calloc( size_t dim ) {
|
---|
| 50 | //printf( "X2\n" );
|
---|
| 51 | return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
|
---|
| 52 | } // calloc
|
---|
| 53 |
|
---|
| 54 | T * realloc( T * ptr, size_t size ) {
|
---|
| 55 | //printf( "X3\n" );
|
---|
| 56 | return (T *)(void *)realloc( (void *)ptr, size );
|
---|
| 57 | } // realloc
|
---|
| 58 |
|
---|
| 59 | T * memalign( size_t align ) {
|
---|
| 60 | //printf( "X4\n" );
|
---|
| 61 | return (T *)memalign( align, sizeof(T) );
|
---|
| 62 | } // memalign
|
---|
| 63 |
|
---|
| 64 | T * aligned_alloc( size_t align ) {
|
---|
| 65 | //printf( "X5\n" );
|
---|
| 66 | return (T *)aligned_alloc( align, sizeof(T) );
|
---|
| 67 | } // aligned_alloc
|
---|
| 68 |
|
---|
| 69 | int posix_memalign( T ** ptr, size_t align ) {
|
---|
| 70 | //printf( "X6\n" );
|
---|
| 71 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
|
---|
| 72 | } // posix_memalign
|
---|
| 73 |
|
---|
[3ce0d440] | 74 |
|
---|
| 75 | // Cforall dynamic allocation
|
---|
[74b19fb] | 76 |
|
---|
| 77 | T * alloc( void ) {
|
---|
| 78 | //printf( "X7\n" );
|
---|
| 79 | return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 80 | } // alloc
|
---|
| 81 |
|
---|
| 82 | T * alloc( char fill ) {
|
---|
| 83 | //printf( "X8\n" );
|
---|
| 84 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
|
---|
| 85 | return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value
|
---|
| 86 | } // alloc
|
---|
| 87 |
|
---|
| 88 | T * alloc( size_t dim ) {
|
---|
| 89 | //printf( "X9\n" );
|
---|
| 90 | return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
|
---|
| 91 | } // alloc
|
---|
| 92 |
|
---|
| 93 | T * alloc( size_t dim, char fill ) {
|
---|
| 94 | //printf( "X10\n" );
|
---|
| 95 | T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
|
---|
| 96 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initial with fill value
|
---|
| 97 | } // alloc
|
---|
| 98 |
|
---|
| 99 | T * alloc( T ptr[], size_t dim ) {
|
---|
| 100 | //printf( "X11\n" );
|
---|
| 101 | return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
|
---|
| 102 | } // alloc
|
---|
| 103 | } // distribution
|
---|
[6065b3aa] | 104 |
|
---|
[f3fc631f] | 105 |
|
---|
[6065b3aa] | 106 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
|
---|
| 107 |
|
---|
[3ce0d440] | 108 |
|
---|
| 109 | static inline forall( dtype T | sized(T) ) {
|
---|
| 110 | T * align_alloc( size_t align ) {
|
---|
| 111 | //printf( "X13\n" );
|
---|
| 112 | return (T *)memalign( align, sizeof(T) );
|
---|
| 113 | } // align_alloc
|
---|
| 114 |
|
---|
| 115 | T * align_alloc( size_t align, char fill ) {
|
---|
| 116 | //printf( "X14\n" );
|
---|
| 117 | T * ptr = (T *)memalign( align, sizeof(T) );
|
---|
| 118 | return (T *)memset( ptr, (int)fill, sizeof(T) );
|
---|
| 119 | } // align_alloc
|
---|
| 120 |
|
---|
| 121 | T * align_alloc( size_t align, size_t dim ) {
|
---|
| 122 | //printf( "X15\n" );
|
---|
| 123 | return (T *)memalign( align, dim * sizeof(T) );
|
---|
| 124 | } // align_alloc
|
---|
| 125 |
|
---|
| 126 | T * align_alloc( size_t align, size_t dim, char fill ) {
|
---|
| 127 | //printf( "X16\n" );
|
---|
| 128 | T * ptr = (T *)memalign( align, dim * sizeof(T) );
|
---|
| 129 | return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
|
---|
| 130 | } // align_alloc
|
---|
| 131 | } // distribution
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | static inline forall( dtype T | sized(T) ) {
|
---|
| 135 | // data, non-array types
|
---|
| 136 |
|
---|
| 137 | T * memset( T * dest, char c ) {
|
---|
| 138 | //printf( "X17\n" );
|
---|
| 139 | return (T *)memset( dest, c, sizeof(T) );
|
---|
| 140 | } // memset
|
---|
| 141 |
|
---|
| 142 | extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
|
---|
| 143 |
|
---|
| 144 | T * memcpy( T * dest, const T * src ) {
|
---|
| 145 | //printf( "X18\n" );
|
---|
| 146 | return (T *)memcpy( dest, src, sizeof(T) );
|
---|
| 147 | } // memcpy
|
---|
| 148 | } // distribution
|
---|
| 149 |
|
---|
| 150 | static inline forall( dtype T | sized(T) ) {
|
---|
| 151 | // data, array types
|
---|
| 152 |
|
---|
| 153 | T * memset( T dest[], size_t dim, char c ) {
|
---|
| 154 | //printf( "X19\n" );
|
---|
| 155 | return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
|
---|
| 156 | } // memset
|
---|
| 157 |
|
---|
| 158 | T * memcpy( T dest[], const T src[], size_t dim ) {
|
---|
| 159 | //printf( "X20\n" );
|
---|
| 160 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
|
---|
| 161 | } // memcpy
|
---|
| 162 | } // distribution
|
---|
[f3fc631f] | 163 |
|
---|
[6065b3aa] | 164 | // allocation/deallocation and constructor/destructor, non-array types
|
---|
[aca65621] | 165 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
|
---|
[83a071f9] | 166 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
|
---|
| 167 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
|
---|
[627f585] | 168 |
|
---|
[6065b3aa] | 169 | // allocation/deallocation and constructor/destructor, array types
|
---|
[aca65621] | 170 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
|
---|
| 171 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
|
---|
| 172 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
|
---|
[6065b3aa] | 173 |
|
---|
[bd85400] | 174 | //---------------------------------------
|
---|
| 175 |
|
---|
[e672372] | 176 | static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
|
---|
| 177 | static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
|
---|
| 178 | static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
|
---|
| 179 | static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
|
---|
| 180 | static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
|
---|
| 181 | static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
|
---|
| 182 |
|
---|
| 183 | static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
|
---|
| 184 | static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
|
---|
| 185 | static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
|
---|
| 186 |
|
---|
[bd85400] | 187 | float _Complex strto( const char * sptr, char ** eptr );
|
---|
| 188 | double _Complex strto( const char * sptr, char ** eptr );
|
---|
| 189 | long double _Complex strto( const char * sptr, char ** eptr );
|
---|
| 190 |
|
---|
[e672372] | 191 | static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
|
---|
| 192 | static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
|
---|
| 193 | static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
|
---|
| 194 | static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
|
---|
| 195 | static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
|
---|
| 196 | static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
|
---|
| 197 |
|
---|
| 198 | static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
|
---|
| 199 | static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
|
---|
| 200 | static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
|
---|
| 201 |
|
---|
| 202 | static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 203 | static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 204 | static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
|
---|
| 205 |
|
---|
[bd85400] | 206 | //---------------------------------------
|
---|
| 207 |
|
---|
[3ce0d440] | 208 | forall( otype E | { int ?<?( E, E ); } ) {
|
---|
| 209 | E * bsearch( E key, const E * vals, size_t dim );
|
---|
| 210 | size_t bsearch( E key, const E * vals, size_t dim );
|
---|
| 211 | E * bsearchl( E key, const E * vals, size_t dim );
|
---|
| 212 | size_t bsearchl( E key, const E * vals, size_t dim );
|
---|
| 213 | E * bsearchu( E key, const E * vals, size_t dim );
|
---|
| 214 | size_t bsearchu( E key, const E * vals, size_t dim );
|
---|
[f3ddc21] | 215 |
|
---|
[3ce0d440] | 216 | void qsort( E * vals, size_t dim );
|
---|
| 217 | } // distribution
|
---|
[9c47a47] | 218 |
|
---|
[3ce0d440] | 219 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
---|
| 220 | E * bsearch( K key, const E * vals, size_t dim );
|
---|
| 221 | size_t bsearch( K key, const E * vals, size_t dim );
|
---|
| 222 | E * bsearchl( K key, const E * vals, size_t dim );
|
---|
| 223 | size_t bsearchl( K key, const E * vals, size_t dim );
|
---|
| 224 | E * bsearchu( K key, const E * vals, size_t dim );
|
---|
| 225 | size_t bsearchu( K key, const E * vals, size_t dim );
|
---|
| 226 | } // distribution
|
---|
[bd85400] | 227 |
|
---|
| 228 | //---------------------------------------
|
---|
| 229 |
|
---|
[5ea26ed] | 230 | [ int, int ] div( int num, int denom );
|
---|
| 231 | [ long int, long int ] div( long int num, long int denom );
|
---|
| 232 | [ long long int, long long int ] div( long long int num, long long int denom );
|
---|
[4040425] | 233 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
[5ea26ed] | 234 | [ T, T ] div( T num, T demon );
|
---|
[bd85400] | 235 |
|
---|
| 236 | //---------------------------------------
|
---|
| 237 |
|
---|
[e672372] | 238 | static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
|
---|
[6e991d6] | 239 | extern "C" { int abs( int ); } // use default C routine for int
|
---|
[e672372] | 240 | static inline unsigned long int abs( long int v ) { return labs( v ); }
|
---|
| 241 | static inline unsigned long long int abs( long long int v ) { return llabs( v ); }
|
---|
| 242 |
|
---|
| 243 | extern "C" {
|
---|
| 244 | double fabs( double );
|
---|
| 245 | float fabsf( float );
|
---|
| 246 | long double fabsl( long double );
|
---|
| 247 | } // extern "C"
|
---|
| 248 | static inline float abs( float x ) { return fabsf( x ); }
|
---|
| 249 | static inline double abs( double x ) { return fabs( x ); }
|
---|
| 250 | static inline long double abs( long double x ) { return fabsl( x ); }
|
---|
| 251 |
|
---|
| 252 | extern "C" {
|
---|
| 253 | double cabs( double _Complex );
|
---|
| 254 | float cabsf( float _Complex );
|
---|
| 255 | long double cabsl( long double _Complex );
|
---|
| 256 | } // extern "C"
|
---|
| 257 | static inline float abs( float _Complex x ) { return cabsf( x ); }
|
---|
| 258 | static inline double abs( double _Complex x ) { return cabs( x ); }
|
---|
| 259 | static inline long double abs( long double _Complex x ) { return cabsl( x ); }
|
---|
| 260 |
|
---|
[aca65621] | 261 | forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
|
---|
[f3ddc21] | 262 | T abs( T );
|
---|
[53ba273] | 263 |
|
---|
| 264 | //---------------------------------------
|
---|
| 265 |
|
---|
[54aba8d] | 266 | extern "C" { void srandom( unsigned int seed ); } // override C version
|
---|
[70e4895d] | 267 | char random( void );
|
---|
[e672372] | 268 | char random( char u );
|
---|
[70e4895d] | 269 | char random( char l, char u );
|
---|
| 270 | int random( void );
|
---|
[e672372] | 271 | int random( int u );
|
---|
| 272 | int random( int l, int u );
|
---|
[70e4895d] | 273 | unsigned int random( void );
|
---|
| 274 | unsigned int random( unsigned int u );
|
---|
| 275 | unsigned int random( unsigned int l, unsigned int u );
|
---|
[54aba8d] | 276 | extern "C" { long int random( void ); } // override C version
|
---|
[e672372] | 277 | long int random( long int u );
|
---|
| 278 | long int random( long int l, long int u );
|
---|
[70e4895d] | 279 | unsigned long int random( void );
|
---|
| 280 | unsigned long int random( unsigned long int u );
|
---|
| 281 | unsigned long int random( unsigned long int l, unsigned long int u );
|
---|
| 282 | float random( void );
|
---|
| 283 | double random( void );
|
---|
| 284 | float _Complex random( void );
|
---|
| 285 | double _Complex random( void );
|
---|
| 286 | long double _Complex random( void );
|
---|
[bd85400] | 287 |
|
---|
| 288 | //---------------------------------------
|
---|
| 289 |
|
---|
[4040425] | 290 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[e672372] | 291 | static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
|
---|
[bd85400] | 292 |
|
---|
[4040425] | 293 | forall( otype T | { int ?>?( T, T ); } )
|
---|
[e672372] | 294 | static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
|
---|
[bd85400] | 295 |
|
---|
[a9f2c13] | 296 | forall( otype T | { T min( T, T ); T max( T, T ); } )
|
---|
[e672372] | 297 | static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
|
---|
[a9f2c13] | 298 |
|
---|
[4040425] | 299 | forall( otype T )
|
---|
[e672372] | 300 | static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
|
---|
[bd85400] | 301 |
|
---|
| 302 | // Local Variables: //
|
---|
| 303 | // mode: c //
|
---|
| 304 | // tab-width: 4 //
|
---|
| 305 | // End: //
|
---|