// // 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 : Tue Feb 4 08:27:08 2020 // Update Count : 486 // #include "stdlib.hfa" //--------------------------------------- #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 #include // memcpy, memset #include // malloc_usable_size //#include // fabsf, fabs, fabsl #include // _Complex_I #include //--------------------------------------- forall( dtype T | sized(T) ) { T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill size_t olen = malloc_usable_size( ptr ); // current allocation void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc size_t nlen = malloc_usable_size( nptr ); // new allocation if ( nlen > olen ) { // larger ? memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage } // if return (T *)nptr; } // alloc_set T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill size_t olen = malloc_usable_size( ptr ); // current allocation void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc // char * nptr = alloc_align( ptr, align ); size_t nlen = malloc_usable_size( nptr ); // new allocation if ( nlen > olen ) { // larger ? memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage } // if return (T *)nptr; } // alloc_align_set } // distribution // allocation/deallocation and constructor/destructor, non-array types forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ) { return &(*malloc()){ p }; // run constructor } // new forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ) { if ( ptr ) { // ignore null ^(*ptr){}; // run destructor free( ptr ); } // if } // delete forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ) { if ( ptr ) { // ignore null ^(*ptr){}; // run destructor free( ptr ); } // if delete( rest ); } // delete // allocation/deallocation and constructor/destructor, array types forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ) { T * arr = alloc( dim ); for ( unsigned int i = 0; i < dim; i += 1 ) { (arr[i]){ p }; // run constructor } // for return arr; } // anew forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ) { if ( arr ) { // ignore null 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( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ) { if ( arr ) { // ignore null 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( otype E | { int ?