[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 | //
|
---|
| 7 | // algorithm.c --
|
---|
| 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Thu Jan 28 17:10:29 2016
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[45161b4d] | 12 | // Last Modified On : Wed Apr 13 14:49:58 2016
|
---|
| 13 | // Update Count : 155
|
---|
[bd85400] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "stdlib"
|
---|
| 17 |
|
---|
| 18 | //---------------------------------------
|
---|
| 19 |
|
---|
| 20 | extern "C" {
|
---|
| 21 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
---|
| 22 | #include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
|
---|
| 23 | #include <string.h> // memset
|
---|
| 24 | #include <malloc.h> // malloc_usable_size
|
---|
| 25 | #include <math.h> // fabsf, fabs, fabsl
|
---|
| 26 | #include <complex.h> // _Complex_I, cabsf, cabs, cabsl
|
---|
| 27 | } // extern "C"
|
---|
| 28 |
|
---|
[4040425] | 29 | forall( otype T ) T * malloc( void ) {
|
---|
[3849857] | 30 | //printf( "malloc1\n" );
|
---|
[bd85400] | 31 | return (T *)malloc( sizeof(T) );
|
---|
| 32 | } // malloc
|
---|
[4040425] | 33 | forall( otype T ) T * malloc( char fill ) {
|
---|
[3849857] | 34 | //printf( "malloc3\n" );
|
---|
[bd85400] | 35 | T * ptr = (T *)malloc( sizeof(T) );
|
---|
| 36 | return memset( ptr );
|
---|
| 37 | } // malloc
|
---|
| 38 |
|
---|
[45161b4d] | 39 | forall( otype T ) T * calloc( size_t nmemb ) {
|
---|
[3849857] | 40 | //printf( "calloc\n" );
|
---|
[45161b4d] | 41 | return (T *)calloc( nmemb, sizeof(T) );
|
---|
[bd85400] | 42 | } // calloc
|
---|
| 43 |
|
---|
[4040425] | 44 | forall( otype T ) T * realloc( T * ptr, size_t size ) {
|
---|
[3849857] | 45 | //printf( "realloc1\n" );
|
---|
[bd85400] | 46 | return (T *)(void *)realloc( (void *)ptr, size );
|
---|
| 47 | } // realloc
|
---|
[4040425] | 48 | forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
|
---|
[3849857] | 49 | //printf( "realloc2\n" );
|
---|
[bd85400] | 50 | char * nptr = (T *)(void *)realloc( (void *)ptr, size );
|
---|
| 51 | size_t unused = malloc_usable_size( nptr );
|
---|
| 52 | memset( nptr + size - unused, (int)fill, unused ); // initialize any new storage
|
---|
| 53 | return nptr;
|
---|
| 54 | } // realloc
|
---|
| 55 |
|
---|
[4040425] | 56 | forall( otype T ) T * malloc( T * ptr, size_t size ) {
|
---|
[3849857] | 57 | //printf( "malloc4\n" );
|
---|
[bd85400] | 58 | return (T *)realloc( ptr, size );
|
---|
| 59 | } // malloc
|
---|
[4040425] | 60 | forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
|
---|
[3849857] | 61 | //printf( "malloc5\n" );
|
---|
[bd85400] | 62 | return (T *)realloc( ptr, size, fill );
|
---|
| 63 | } // malloc
|
---|
| 64 |
|
---|
[4040425] | 65 | forall( otype T ) T * aligned_alloc( size_t alignment ) {
|
---|
[3849857] | 66 | //printf( "aligned_alloc\n" );
|
---|
[bd85400] | 67 | return (T *)memalign( alignment, sizeof(T) );
|
---|
| 68 | } // aligned_alloc
|
---|
| 69 |
|
---|
[4040425] | 70 | forall( otype T ) T * memalign( size_t alignment ) {
|
---|
[3849857] | 71 | //printf( "memalign\n" );
|
---|
[bd85400] | 72 | return (T *)memalign( alignment, sizeof(T) );
|
---|
| 73 | } // memalign
|
---|
| 74 |
|
---|
[4040425] | 75 | forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ) {
|
---|
[3849857] | 76 | //printf( "posix_memalign\n" );
|
---|
[bd85400] | 77 | return posix_memalign( (void **)ptr, alignment, sizeof(T) );
|
---|
| 78 | } // posix_memalign
|
---|
| 79 |
|
---|
[0438091] | 80 | forall( otype T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill
|
---|
[3849857] | 81 | //printf( "memset1\n" );
|
---|
[0438091] | 82 | return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) );
|
---|
| 83 | } // memset
|
---|
| 84 | forall( otype T ) T * memset( T * ptr ) { // remove when default value available
|
---|
[3849857] | 85 | //printf( "memset2\n" );
|
---|
[0438091] | 86 | return (T *)memset( ptr, 0, malloc_usable_size( ptr ) );
|
---|
| 87 | } // memset
|
---|
| 88 |
|
---|
[bd85400] | 89 | //---------------------------------------
|
---|
| 90 |
|
---|
| 91 | int ato( const char * ptr ) {
|
---|
| 92 | int i;
|
---|
| 93 | if ( sscanf( ptr, "%d", &i ) == EOF ) {} // check return code
|
---|
| 94 | return i;
|
---|
| 95 | }
|
---|
| 96 | unsigned int ato( const char * ptr ) {
|
---|
| 97 | unsigned int ui;
|
---|
| 98 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {} // check return code
|
---|
| 99 | return ui;
|
---|
| 100 | }
|
---|
| 101 | long int ato( const char * ptr ) {
|
---|
| 102 | long int li;
|
---|
[52f85e0] | 103 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {} // check return code
|
---|
[bd85400] | 104 | return li;
|
---|
| 105 | }
|
---|
| 106 | unsigned long int ato( const char * ptr ) {
|
---|
| 107 | unsigned long int uli;
|
---|
[52f85e0] | 108 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} // check return code
|
---|
[bd85400] | 109 | return uli;
|
---|
| 110 | }
|
---|
| 111 | long long int ato( const char * ptr ) {
|
---|
| 112 | long long int lli;
|
---|
[52f85e0] | 113 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} // check return code
|
---|
[bd85400] | 114 | return lli;
|
---|
| 115 | }
|
---|
| 116 | unsigned long long int ato( const char * ptr ) {
|
---|
| 117 | unsigned long long int ulli;
|
---|
[52f85e0] | 118 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} // check return code
|
---|
[bd85400] | 119 | return ulli;
|
---|
| 120 | }
|
---|
[90c3b1c] | 121 |
|
---|
[bd85400] | 122 | float ato( const char * ptr ) {
|
---|
| 123 | float f;
|
---|
[52f85e0] | 124 | if ( sscanf( ptr, "%f", &f ) == EOF ) {} // check return code
|
---|
[bd85400] | 125 | return f;
|
---|
| 126 | }
|
---|
| 127 | double ato( const char * ptr ) {
|
---|
| 128 | double d;
|
---|
[52f85e0] | 129 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {} // check return code
|
---|
[bd85400] | 130 | return d;
|
---|
| 131 | }
|
---|
| 132 | long double ato( const char * ptr ) {
|
---|
| 133 | long double ld;
|
---|
[52f85e0] | 134 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} // check return code
|
---|
[bd85400] | 135 | return ld;
|
---|
| 136 | }
|
---|
[90c3b1c] | 137 |
|
---|
[bd85400] | 138 | float _Complex ato( const char * ptr ) {
|
---|
| 139 | float re, im;
|
---|
[52f85e0] | 140 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} // check return code
|
---|
[bd85400] | 141 | return re + im * _Complex_I;
|
---|
| 142 | }
|
---|
| 143 | double _Complex ato( const char * ptr ) {
|
---|
| 144 | double re, im;
|
---|
[52f85e0] | 145 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} // check return code
|
---|
[bd85400] | 146 | return re + im * _Complex_I;
|
---|
| 147 | }
|
---|
| 148 | long double _Complex ato( const char * ptr ) {
|
---|
| 149 | long double re, im;
|
---|
[52f85e0] | 150 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} // check return code
|
---|
[bd85400] | 151 | return re + im * _Complex_I;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 155 | return (int)strtol( sptr, eptr, base );
|
---|
| 156 | }
|
---|
| 157 | unsigned int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 158 | return (unsigned int)strtoul( sptr, eptr, base );
|
---|
| 159 | }
|
---|
| 160 | long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 161 | return strtol( sptr, eptr, base );
|
---|
| 162 | }
|
---|
| 163 | unsigned long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 164 | return strtoul( sptr, eptr, base );
|
---|
| 165 | }
|
---|
| 166 | long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 167 | return strtoll( sptr, eptr, base );
|
---|
| 168 | }
|
---|
| 169 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
| 170 | return strtoull( sptr, eptr, base );
|
---|
| 171 | }
|
---|
[90c3b1c] | 172 |
|
---|
[bd85400] | 173 | float strto( const char * sptr, char ** eptr ) {
|
---|
| 174 | return strtof( sptr, eptr );
|
---|
| 175 | }
|
---|
| 176 | double strto( const char * sptr, char ** eptr ) {
|
---|
| 177 | return strtod( sptr, eptr );
|
---|
| 178 | }
|
---|
| 179 | long double strto( const char * sptr, char ** eptr ) {
|
---|
| 180 | return strtold( sptr, eptr );
|
---|
| 181 | }
|
---|
[90c3b1c] | 182 |
|
---|
[bd85400] | 183 | float _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 184 | float re, im;
|
---|
| 185 | re = strtof( sptr, eptr );
|
---|
| 186 | if ( sptr == *eptr ) return 0.0;
|
---|
| 187 | im = strtof( sptr, eptr );
|
---|
| 188 | if ( sptr == *eptr ) return 0.0;
|
---|
| 189 | return re + im * _Complex_I;
|
---|
| 190 | }
|
---|
| 191 | double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 192 | double re, im;
|
---|
| 193 | re = strtod( sptr, eptr );
|
---|
| 194 | if ( sptr == *eptr ) return 0.0;
|
---|
| 195 | im = strtod( sptr, eptr );
|
---|
| 196 | if ( sptr == *eptr ) return 0.0;
|
---|
| 197 | return re + im * _Complex_I;
|
---|
| 198 | }
|
---|
| 199 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 200 | long double re, im;
|
---|
| 201 | re = strtold( sptr, eptr );
|
---|
| 202 | if ( sptr == *eptr ) return 0.0;
|
---|
| 203 | im = strtold( sptr, eptr );
|
---|
| 204 | if ( sptr == *eptr ) return 0.0;
|
---|
| 205 | return re + im * _Complex_I;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | //---------------------------------------
|
---|
| 209 |
|
---|
[4040425] | 210 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[bd85400] | 211 | T * bsearch( const T key, const T * arr, size_t dimension ) {
|
---|
| 212 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
| 213 | return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
|
---|
| 214 | } // bsearch
|
---|
| 215 |
|
---|
[4040425] | 216 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[bd85400] | 217 | void qsort( const T * arr, size_t dimension ) {
|
---|
| 218 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
| 219 | qsort( arr, dimension, sizeof(T), comp );
|
---|
| 220 | } // qsort
|
---|
| 221 |
|
---|
| 222 | //---------------------------------------
|
---|
| 223 |
|
---|
[4040425] | 224 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
[bd85400] | 225 | [ T, T ] div( T t1, T t2 ) { /* return [ t1 / t2, t1 % t2 ]; */ }
|
---|
| 226 |
|
---|
| 227 | //---------------------------------------
|
---|
| 228 |
|
---|
| 229 | char abs( char v ) { return abs( (int)v ); }
|
---|
| 230 | long int abs( long int v ) { return labs( v ); }
|
---|
| 231 | long long int abs( long long int v ) { return llabs( v ); }
|
---|
| 232 | float abs( float v ) { return fabsf( v ); }
|
---|
| 233 | double abs( double v ) { return fabs( v ); }
|
---|
| 234 | long double abs( long double v ) { return fabsl( v ); }
|
---|
| 235 | float _Complex abs( float _Complex v ) { return cabsf( v ); }
|
---|
| 236 | double _Complex abs( double _Complex v ) { return cabs( v ); }
|
---|
| 237 | long double _Complex abs( long double _Complex v ) { return cabsl( v ); }
|
---|
| 238 |
|
---|
| 239 | //---------------------------------------
|
---|
| 240 |
|
---|
[53ba273] | 241 | float floor( float v ) { return floorf( v ); }
|
---|
| 242 | long double floor( long double v ) { return floorl( v ); }
|
---|
| 243 |
|
---|
| 244 | float ceil( float v ) { return ceilf( v ); }
|
---|
| 245 | long double ceil( long double v ) { return ceill( v ); }
|
---|
| 246 |
|
---|
| 247 | //---------------------------------------
|
---|
| 248 |
|
---|
[0438091] | 249 | void rand48seed( long int s ) { srand48( s ); }
|
---|
[3cfe27f] | 250 | char rand48() { return mrand48(); }
|
---|
[0438091] | 251 | int rand48() { return mrand48(); }
|
---|
| 252 | unsigned int rand48() { return lrand48(); }
|
---|
| 253 | long int rand48() { return mrand48(); }
|
---|
| 254 | unsigned long int rand48() { return lrand48(); }
|
---|
| 255 | float rand48() { return (float)drand48(); } // otherwise float uses lrand48
|
---|
| 256 | double rand48() { return drand48(); }
|
---|
| 257 | float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
| 258 | double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
| 259 | long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
[bd85400] | 260 |
|
---|
| 261 | //---------------------------------------
|
---|
| 262 |
|
---|
[4040425] | 263 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[bd85400] | 264 | T min( const T t1, const T t2 ) {
|
---|
| 265 | return t1 < t2 ? t1 : t2;
|
---|
| 266 | } // min
|
---|
| 267 |
|
---|
[4040425] | 268 | forall( otype T | { int ?>?( T, T ); } )
|
---|
[bd85400] | 269 | T max( const T t1, const T t2 ) {
|
---|
| 270 | return t1 > t2 ? t1 : t2;
|
---|
| 271 | } // max
|
---|
| 272 |
|
---|
[4040425] | 273 | forall( otype T )
|
---|
[bd85400] | 274 | void swap( T * t1, T * t2 ) {
|
---|
| 275 | T temp = *t1;
|
---|
| 276 | *t1 = *t2;
|
---|
| 277 | *t2 = temp;
|
---|
| 278 | } // swap
|
---|
| 279 |
|
---|
| 280 | // Local Variables: //
|
---|
| 281 | // tab-width: 4 //
|
---|
| 282 | // End: //
|
---|