Changeset f3ddc21


Ignore:
Timestamp:
May 13, 2017, 11:56:45 AM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
07846d8
Parents:
b65a9fc
Message:

add polymorphic abs routine and code formatting

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib

    rb65a9fc rf3ddc21  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr  1 17:35:24 2017
    13 // Update Count     : 104
     12// Last Modified On : Tue May  9 08:42:44 2017
     13// Update Count     : 107
    1414//
    1515
     
    8484forall( otype T | { int ?<?( T, T ); } )
    8585T * bsearch( T key, const T * arr, size_t dimension );
     86
    8687forall( otype T | { int ?<?( T, T ); } )
    8788unsigned int bsearch( T key, const T * arr, size_t dimension );
     89
    8890
    8991forall( otype T | { int ?<?( T, T ); } )
     
    107109double abs( double _Complex );
    108110long double abs( long double _Complex );
     111forall ( otype T | { void ?{}( T *, zero_t ); int ?<?( T, T ); T -?( T ); } )
     112T abs( T );
    109113
    110114//---------------------------------------
  • src/libcfa/stdlib.c

    rb65a9fc rf3ddc21  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Apr 16 10:41:05 2017
    13 // Update Count     : 189
     12// Last Modified On : Tue May  9 08:43:00 2017
     13// Update Count     : 191
    1414//
    1515
     
    2727} // extern "C"
    2828
    29 forall( dtype T | sized(T) ) T * malloc( void ) {
    30         //printf( "malloc1\n" );
    31     return (T *)(void*)malloc( (size_t)sizeof(T) );
     29forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe
     30    return (T *)(void *)malloc( (size_t)sizeof(T) );
    3231} // malloc
    33 forall( dtype T | sized(T) ) T * malloc( char fill ) {
    34         //printf( "malloc3\n" );
    35         T * ptr = (T *)(void*)malloc( (size_t)sizeof(T) );
     32
     33forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc)
     34        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
    3635    return memset( ptr, (int)fill, sizeof(T) );
    3736} // malloc
    3837
    39 forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) {
    40         //printf( "calloc\n" );
     38forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
     39    return (T *)realloc( ptr, size );
     40} // malloc
     41
     42forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
     43    return (T *)realloc( ptr, size, fill );
     44} // malloc
     45
     46
     47forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
    4148    return (T *)calloc( nmemb, sizeof(T) );
    4249} // calloc
    4350
    44 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
    45         //printf( "realloc1\n" );
     51
     52forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
    4653    return (T *)(void *)realloc( (void *)ptr, size );
    4754} // realloc
    48 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
    49         //printf( "realloc2\n" );
     55
     56forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
    5057    char * nptr = (T *)(void *)realloc( (void *)ptr, size );
    5158    size_t unused = malloc_usable_size( nptr );
     
    5461} // realloc
    5562
    56 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) {
    57         //printf( "malloc4\n" );
    58     return (T *)realloc( ptr, size );
    59 } // malloc
    60 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
    61         //printf( "malloc5\n" );
    62     return (T *)realloc( ptr, size, fill );
    63 } // malloc
    64 
    65 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) {
    66         //printf( "aligned_alloc\n" );
     63
     64forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
    6765    return (T *)memalign( alignment, sizeof(T) );
    6866} // aligned_alloc
    6967
    7068forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
    71         //printf( "memalign\n" );
    7269    return (T *)memalign( alignment, sizeof(T) );
    7370} // memalign
    7471
    7572forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
    76         //printf( "posix_memalign\n" );
    7773    return posix_memalign( (void **)ptr, alignment, sizeof(T) );
    7874} // posix_memalign
    7975
    80 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
     76
     77forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new
    8178T * new( Params p ) {
    8279        return ((T *)malloc()){ p };
    83 }
    84 
    85 forall( dtype T | { void ^?{}(T *); } )
     80} // new
     81
     82forall( dtype T | { void ^?{}(T *); } )                                 // delete
    8683void delete( T * ptr ) {
    87   if ( ptr ) {
    88     ^ptr{};
    89     free( ptr );
    90   }
    91 }
     84        if ( ptr ) {
     85                ^ptr{};
     86                free( ptr );
     87        }
     88} // delete
    9289
    9390forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
     
    9895        }
    9996        delete( rest );
    100 }
     97} // delete
    10198
    10299//---------------------------------------
     
    106103        if ( sscanf( ptr, "%d", &i ) == EOF ) {}
    107104        return i;
    108 }
     105} // ato
     106
    109107unsigned int ato( const char * ptr ) {
    110108        unsigned int ui;
    111109        if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
    112110        return ui;
    113 }
     111} // ato
     112
    114113long int ato( const char * ptr ) {
    115114        long int li;
    116115        if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
    117116        return li;
    118 }
     117} // ato
     118
    119119unsigned long int ato( const char * ptr ) {
    120120        unsigned long int uli;
    121121        if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
    122122        return uli;
    123 }
     123} // ato
     124
    124125long long int ato( const char * ptr ) {
    125126        long long int lli;
    126127        if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
    127128        return lli;
    128 }
     129} // ato
     130
    129131unsigned long long int ato( const char * ptr ) {
    130132        unsigned long long int ulli;
    131133        if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
    132134        return ulli;
    133 }
     135} // ato
     136
    134137
    135138float ato( const char * ptr ) {
     
    137140        if ( sscanf( ptr, "%f", &f ) == EOF ) {}
    138141        return f;
    139 }
     142} // ato
     143
    140144double ato( const char * ptr ) {
    141145        double d;
    142146        if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
    143147        return d;
    144 }
     148} // ato
     149
    145150long double ato( const char * ptr ) {
    146151        long double ld;
    147152        if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
    148153        return ld;
    149 }
     154} // ato
     155
    150156
    151157float _Complex ato( const char * ptr ) {
     
    153159        if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
    154160        return re + im * _Complex_I;
    155 }
     161} // ato
     162
    156163double _Complex ato( const char * ptr ) {
    157164        double re, im;
    158165        if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
    159166        return re + im * _Complex_I;
    160 }
     167} // ato
     168
    161169long double _Complex ato( const char * ptr ) {
    162170        long double re, im;
    163171        if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
    164172        return re + im * _Complex_I;
    165 }
     173} // ato
     174
    166175
    167176int strto( const char * sptr, char ** eptr, int base ) {
    168177        return (int)strtol( sptr, eptr, base );
    169 }
     178} // strto
     179
    170180unsigned int strto( const char * sptr, char ** eptr, int base ) {
    171181        return (unsigned int)strtoul( sptr, eptr, base );
    172 }
     182} // strto
     183
    173184long int strto( const char * sptr, char ** eptr, int base ) {
    174185        return strtol( sptr, eptr, base );
    175 }
     186} // strto
     187
    176188unsigned long int strto( const char * sptr, char ** eptr, int base ) {
    177189        return strtoul( sptr, eptr, base );
    178 }
     190} // strto
     191
    179192long long int strto( const char * sptr, char ** eptr, int base ) {
    180193        return strtoll( sptr, eptr, base );
    181 }
     194} // strto
     195
    182196unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
    183197        return strtoull( sptr, eptr, base );
    184 }
     198} // strto
     199
    185200
    186201float strto( const char * sptr, char ** eptr ) {
    187202        return strtof( sptr, eptr );
    188 }
     203} // strto
     204
    189205double strto( const char * sptr, char ** eptr ) {
    190206        return strtod( sptr, eptr );
    191 }
     207} // strto
     208
    192209long double strto( const char * sptr, char ** eptr ) {
    193210        return strtold( sptr, eptr );
    194 }
     211} // strto
     212
    195213
    196214float _Complex strto( const char * sptr, char ** eptr ) {
     
    201219        if ( sptr == *eptr ) return 0.0;
    202220        return re + im * _Complex_I;
    203 }
     221} // strto
     222
    204223double _Complex strto( const char * sptr, char ** eptr ) {
    205224        double re, im;
     
    209228        if ( sptr == *eptr ) return 0.0;
    210229        return re + im * _Complex_I;
    211 }
     230} // strto
     231
    212232long double _Complex strto( const char * sptr, char ** eptr ) {
    213233        long double re, im;
     
    217237        if ( sptr == *eptr ) return 0.0;
    218238        return re + im * _Complex_I;
    219 }
     239} // strto
    220240
    221241//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.