// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // stdlib.c -- // // Author : Peter A. Buhr // Created On : Thu Jan 28 17:10:29 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Mon Jan 10 17:07:21 2022 // Update Count : 572 // #include "stdlib.hfa" //#include "concurrency/kernel/fwd.hfa" #include "concurrency/invoke.h" // random_state //--------------------------------------- #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 #include // memcpy, memset //#include // fabsf, fabs, fabsl #include // _Complex_I #include //--------------------------------------- // Cforall allocation/deallocation and constructor/destructor, array types forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p ) { T * arr = alloc( dim ); for ( unsigned int i = 0; i < dim; i += 1 ) { (arr[i]){ p }; // run constructor } // for return arr; } // anew forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] ) { if ( arr ) { // ignore null size_t dim = malloc_size( arr ) / sizeof( T ); for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned ^(arr[i]){}; // run destructor } // for free( arr ); } // if } // adelete forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest ) { if ( arr ) { // ignore null size_t dim = malloc_size( arr ) / sizeof( T ); for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned ^(arr[i]){}; // run destructor } // for free( arr ); } // if adelete( rest ); } // adelete //--------------------------------------- float _Complex strto( const char sptr[], char ** eptr ) { float re, im; char * eeptr; re = strtof( sptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } im = strtof( eeptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; } return re + im * _Complex_I; } // strto double _Complex strto( const char sptr[], char ** eptr ) { double re, im; char * eeptr; re = strtod( sptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } im = strtod( eeptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; } return re + im * _Complex_I; } // strto long double _Complex strto( const char sptr[], char ** eptr ) { long double re, im; char * eeptr; re = strtold( sptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } im = strtold( eeptr, &eeptr ); if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; } return re + im * _Complex_I; } // strto //--------------------------------------- forall( E | { int ?> 21; state ^= state << 7; return state; } // MarsagliaXor inline uint32_t LCG( uint32_t & state ) { // linear congruential generator return state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime! } // LCG uint32_t __thread_seed = rdtscl(); // global thread seed void set_seed( uint32_t seed ) { __thread_seed = seed; } uint32_t get_seed() { return __thread_seed; } uint32_t prng( PRNG & prng ) with( prng ) { callcnt += 1; return GENERATOR( state ); } uint32_t prng( void ) { return GENERATOR( __thread_seed ); } // [0,UINT_MAX] //--------------------------------------- bool threading_enabled( void ) __attribute__(( weak )) { return false; } // Local Variables: // // tab-width: 4 // // End: //