Changeset 5e8d7327 for src


Ignore:
Timestamp:
May 30, 2017, 9:53:15 AM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
d6fb3c7
Parents:
a029714 (diff), bff607e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src
Files:
2 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/gmp

    ra029714 r5e8d7327  
    1010// Created On       : Tue Apr 19 08:43:43 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon May 22 08:32:39 2017
    13 // Update Count     : 13
     12// Last Modified On : Sat May 27 09:55:51 2017
     13// Update Count     : 14
    1414//
    1515
     
    2222
    2323// constructor
    24 void ?{}( Int * this ) { mpz_init( this->mpz ); }
    25 void ?{}( Int * this, Int init ) { mpz_init_set( this->mpz, init.mpz ); }
    26 void ?{}( Int * this, zero_t ) { mpz_init_set_si( this->mpz, 0 ); }
    27 void ?{}( Int * this, one_t ) { mpz_init_set_si( this->mpz, 1 ); }
    28 void ?{}( Int * this, signed long int init ) { mpz_init_set_si( this->mpz, init ); }
    29 void ?{}( Int * this, unsigned long int init ) { mpz_init_set_ui( this->mpz, init ); }
    30 void ?{}( Int * this, const char * val ) { if ( mpz_init_set_str( this->mpz, val, 0 ) ) abort(); }
    31 void ^?{}( Int * this ) { mpz_clear( this->mpz ); }
     24static inline void ?{}( Int * this ) { mpz_init( this->mpz ); }
     25static inline void ?{}( Int * this, Int init ) { mpz_init_set( this->mpz, init.mpz ); }
     26static inline void ?{}( Int * this, zero_t ) { mpz_init_set_si( this->mpz, 0 ); }
     27static inline void ?{}( Int * this, one_t ) { mpz_init_set_si( this->mpz, 1 ); }
     28static inline void ?{}( Int * this, signed long int init ) { mpz_init_set_si( this->mpz, init ); }
     29static inline void ?{}( Int * this, unsigned long int init ) { mpz_init_set_ui( this->mpz, init ); }
     30static inline void ?{}( Int * this, const char * val ) { if ( mpz_init_set_str( this->mpz, val, 0 ) ) abort(); }
     31static inline void ^?{}( Int * this ) { mpz_clear( this->mpz ); }
    3232
    3333// assignment
    34 Int ?=?( Int * lhs, Int rhs ) { mpz_set( lhs->mpz, rhs.mpz ); return *lhs; }
    35 Int ?=?( Int * lhs, long int rhs ) { mpz_set_si( lhs->mpz, rhs ); return *lhs; }
    36 Int ?=?( Int * lhs, unsigned long int rhs ) { mpz_set_ui( lhs->mpz, rhs ); return *lhs; }
    37 Int ?=?( Int * lhs, const char * rhs ) { if ( mpz_set_str( lhs->mpz, rhs, 0 ) ) { printf( "invalid string conversion\n" ); abort(); } return *lhs; }
    38 
    39 char ?=?( char * lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
    40 short int ?=?( short int * lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
    41 int ?=?( int * lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
    42 long int ?=?( long int * lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
    43 unsigned char ?=?( unsigned char * lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
    44 unsigned short int ?=?( unsigned short int * lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
    45 unsigned int ?=?( unsigned int * lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
    46 unsigned long int ?=?( unsigned long int * lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
     34static inline Int ?=?( Int * lhs, Int rhs ) { mpz_set( lhs->mpz, rhs.mpz ); return *lhs; }
     35static inline Int ?=?( Int * lhs, long int rhs ) { mpz_set_si( lhs->mpz, rhs ); return *lhs; }
     36static inline Int ?=?( Int * lhs, unsigned long int rhs ) { mpz_set_ui( lhs->mpz, rhs ); return *lhs; }
     37static inline Int ?=?( Int * lhs, const char * rhs ) { if ( mpz_set_str( lhs->mpz, rhs, 0 ) ) { printf( "invalid string conversion\n" ); abort(); } return *lhs; }
     38
     39static inline char ?=?( char * lhs, Int rhs ) { char val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
     40static inline short int ?=?( short int * lhs, Int rhs ) { short int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
     41static inline int ?=?( int * lhs, Int rhs ) { int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
     42static inline long int ?=?( long int * lhs, Int rhs ) { long int val = mpz_get_si( rhs.mpz ); *lhs = val; return val; }
     43static inline unsigned char ?=?( unsigned char * lhs, Int rhs ) { unsigned char val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
     44static inline unsigned short int ?=?( unsigned short int * lhs, Int rhs ) { unsigned short int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
     45static inline unsigned int ?=?( unsigned int * lhs, Int rhs ) { unsigned int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
     46static inline unsigned long int ?=?( unsigned long int * lhs, Int rhs ) { unsigned long int val = mpz_get_ui( rhs.mpz ); *lhs = val; return val; }
    4747
    4848// conversions
    49 long int narrow( Int val ) { return mpz_get_si( val.mpz ); }
    50 unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); }
     49static inline long int narrow( Int val ) { return mpz_get_si( val.mpz ); }
     50static inline unsigned long int narrow( Int val ) { return mpz_get_ui( val.mpz ); }
    5151
    5252// comparison
    53 int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; }
    54 int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
    55 int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
    56 int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
    57 int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
    58 
    59 int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    60 int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); }
    61 int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    62 int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); }
    63 int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
    64 
    65 int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; }
    66 int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
    67 int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
    68 int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
    69 int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
    70 
    71 int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; }
    72 int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
    73 int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
    74 int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
    75 int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
    76 
    77 int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    78 int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); }
    79 int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    80 int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); }
    81 int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
    82 
    83 int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    84 int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); }
    85 int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    86 int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); }
    87 int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     53static inline int ?==?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) == 0; }
     54static inline int ?==?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
     55static inline int ?==?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) == 0; }
     56static inline int ?==?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
     57static inline int ?==?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) == 0; }
     58
     59static inline int ?!=?( Int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     60static inline int ?!=?( Int oper1, long int oper2 ) { return ! ( oper1 == oper2 ); }
     61static inline int ?!=?( long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     62static inline int ?!=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 == oper2 ); }
     63static inline int ?!=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 == oper2 ); }
     64
     65static inline int ?<?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) < 0; }
     66static inline int ?<?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
     67static inline int ?<?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) < 0; }
     68static inline int ?<?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
     69static inline int ?<?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) < 0; }
     70
     71static inline int ?<=?( Int oper1, Int oper2 ) { return mpz_cmp( oper1.mpz, oper2.mpz ) <= 0; }
     72static inline int ?<=?( Int oper1, long int oper2 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
     73static inline int ?<=?( long int oper2, Int oper1 ) { return mpz_cmp_si( oper1.mpz, oper2 ) <= 0; }
     74static inline int ?<=?( Int oper1, unsigned long int oper2 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
     75static inline int ?<=?( unsigned long int oper2, Int oper1 ) { return mpz_cmp_ui( oper1.mpz, oper2 ) <= 0; }
     76
     77static inline int ?>?( Int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     78static inline int ?>?( Int oper1, long int oper2 ) { return ! ( oper1 <= oper2 ); }
     79static inline int ?>?( long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     80static inline int ?>?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 <= oper2 ); }
     81static inline int ?>?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 <= oper2 ); }
     82
     83static inline int ?>=?( Int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     84static inline int ?>=?( Int oper1, long int oper2 ) { return ! ( oper1 < oper2 ); }
     85static inline int ?>=?( long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
     86static inline int ?>=?( Int oper1, unsigned long int oper2 ) { return ! ( oper1 < oper2 ); }
     87static inline int ?>=?( unsigned long int oper1, Int oper2 ) { return ! ( oper1 < oper2 ); }
    8888
    8989// arithmetic
    90 Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; }
    91 Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; }
    92 Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; }
    93 
    94 Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; }
    95 Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
    96 Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
    97 Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
    98 Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
    99 Int ?&=?( Int * lhs, Int rhs ) { return *lhs = *lhs & rhs; }
    100 
    101 Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
    102 Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    103 Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    104 Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    105 Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    106 Int ?|=?( Int * lhs, Int rhs ) { return *lhs = *lhs | rhs; }
    107 
    108 Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
    109 Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    110 Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    111 Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
    112 Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
    113 Int ?^=?( Int * lhs, Int rhs ) { return *lhs = *lhs ^ rhs; }
    114 
    115 Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; }
    116 Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
    117 Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
    118 Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
    119 Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
    120 Int ?+=?( Int * lhs, Int rhs ) { return *lhs = *lhs + rhs; }
    121 Int ?+=?( Int * lhs, long int rhs ) { return *lhs = *lhs + rhs; }
    122 Int ?+=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs + rhs; }
    123 Int ++?( Int * lhs ) { return *lhs += 1; }
    124 Int ?++( Int * lhs ) { Int ret = *lhs; *lhs += 1; return ret; }
    125 
    126 Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; }
    127 Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; }
    128 Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; }
    129 Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; }
    130 Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; }
    131 Int ?-=?( Int * lhs, Int rhs ) { return *lhs = *lhs - rhs; }
    132 Int ?-=?( Int * lhs, long int rhs ) { return *lhs = *lhs - rhs; }
    133 Int ?-=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs - rhs; }
    134 Int --?( Int * lhs ) { return *lhs -= 1; }
    135 Int ?--( Int * lhs ) { Int ret = *lhs; *lhs -= 1; return ret; }
    136 
    137 Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; }
    138 Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    139 Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    140 Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    141 Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
    142 Int ?*=?( Int * lhs, Int rhs ) { return *lhs = *lhs * rhs; }
    143 Int ?*=?( Int * lhs, long int rhs ) { return *lhs = *lhs * rhs; }
    144 Int ?*=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs * rhs; }
     90static inline Int +?( Int oper ) { Int pos; mpz_set( pos.mpz, oper.mpz ); return pos; }
     91static inline Int -?( Int oper ) { Int neg; mpz_neg( neg.mpz, oper.mpz ); return neg; }
     92static inline Int ~?( Int oper ) { Int comp; mpz_com( comp.mpz, oper.mpz ); return comp; }
     93
     94static inline Int ?&?( Int oper1, Int oper2 ) { Int conjunction; mpz_and( conjunction.mpz, oper1.mpz, oper2.mpz ); return conjunction; }
     95static inline Int ?&?( Int oper1, long int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
     96static inline Int ?&?( long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
     97static inline Int ?&?( Int oper1, unsigned long int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_and( conjunction.mpz, oper1.mpz, temp.mpz ); return conjunction; }
     98static inline Int ?&?( unsigned long int oper1, Int oper2 ) { Int conjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_and( conjunction.mpz, temp.mpz, oper2.mpz ); return conjunction; }
     99static inline Int ?&=?( Int * lhs, Int rhs ) { return *lhs = *lhs & rhs; }
     100
     101static inline Int ?|?( Int oper1, Int oper2 ) { Int disjunction; mpz_ior( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
     102static inline Int ?|?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     103static inline Int ?|?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     104static inline Int ?|?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     105static inline Int ?|?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     106static inline Int ?|=?( Int * lhs, Int rhs ) { return *lhs = *lhs | rhs; }
     107
     108static inline Int ?^?( Int oper1, Int oper2 ) { Int disjunction; mpz_xor( disjunction.mpz, oper1.mpz, oper2.mpz ); return disjunction; }
     109static inline Int ?^?( Int oper1, long int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     110static inline Int ?^?( long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_si( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     111static inline Int ?^?( Int oper1, unsigned long int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper2 ); mpz_ior( disjunction.mpz, oper1.mpz, temp.mpz ); return disjunction; }
     112static inline Int ?^?( unsigned long int oper1, Int oper2 ) { Int disjunction, temp; mpz_set_ui( temp.mpz, oper1 ); mpz_ior( disjunction.mpz, temp.mpz, oper2.mpz ); return disjunction; }
     113static inline Int ?^=?( Int * lhs, Int rhs ) { return *lhs = *lhs ^ rhs; }
     114
     115static inline Int ?+?( Int addend1, Int addend2 ) { Int sum; mpz_add( sum.mpz, addend1.mpz, addend2.mpz ); return sum; }
     116static inline Int ?+?( Int addend1, long int addend2 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
     117static inline Int ?+?( long int addend2, Int addend1 ) { Int sum; if ( addend2 >= 0 ) mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); else mpz_sub_ui( sum.mpz, addend1.mpz, -addend2 ); return sum; }
     118static inline Int ?+?( Int addend1, unsigned long int addend2 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
     119static inline Int ?+?( unsigned long int addend2, Int addend1 ) { Int sum; mpz_add_ui( sum.mpz, addend1.mpz, addend2 ); return sum; }
     120static inline Int ?+=?( Int * lhs, Int rhs ) { return *lhs = *lhs + rhs; }
     121static inline Int ?+=?( Int * lhs, long int rhs ) { return *lhs = *lhs + rhs; }
     122static inline Int ?+=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs + rhs; }
     123static inline Int ++?( Int * lhs ) { return *lhs += 1; }
     124static inline Int ?++( Int * lhs ) { Int ret = *lhs; *lhs += 1; return ret; }
     125
     126static inline Int ?-?( Int minuend, Int subtrahend ) { Int diff; mpz_sub( diff.mpz, minuend.mpz, subtrahend.mpz ); return diff; }
     127static inline Int ?-?( Int minuend, long int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); else mpz_add_ui( diff.mpz, minuend.mpz, -subtrahend ); return diff; }
     128static inline Int ?-?( long int minuend, Int subtrahend ) { Int diff; if ( subtrahend >= 0 ) mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); else { mpz_add_ui( diff.mpz, subtrahend.mpz, -minuend ); mpz_neg( diff.mpz, diff.mpz ); } return diff; }
     129static inline Int ?-?( Int minuend, unsigned long int subtrahend ) { Int diff; mpz_sub_ui( diff.mpz, minuend.mpz, subtrahend ); return diff; }
     130static inline Int ?-?( unsigned long int minuend, Int subtrahend ) { Int diff; mpz_ui_sub( diff.mpz, minuend, subtrahend.mpz ); return diff; }
     131static inline Int ?-=?( Int * lhs, Int rhs ) { return *lhs = *lhs - rhs; }
     132static inline Int ?-=?( Int * lhs, long int rhs ) { return *lhs = *lhs - rhs; }
     133static inline Int ?-=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs - rhs; }
     134static inline Int --?( Int * lhs ) { return *lhs -= 1; }
     135static inline Int ?--( Int * lhs ) { Int ret = *lhs; *lhs -= 1; return ret; }
     136
     137static inline Int ?*?( Int multiplicator, Int multiplicand ) { Int product; mpz_mul( product.mpz, multiplicator.mpz, multiplicand.mpz ); return product; }
     138static inline Int ?*?( Int multiplicator, long int multiplicand ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     139static inline Int ?*?( long int multiplicand, Int multiplicator ) { Int product; mpz_mul_si( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     140static inline Int ?*?( Int multiplicator, unsigned long int multiplicand ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     141static inline Int ?*?( unsigned long int multiplicand, Int multiplicator ) { Int product; mpz_mul_ui( product.mpz, multiplicator.mpz, multiplicand ); return product; }
     142static inline Int ?*=?( Int * lhs, Int rhs ) { return *lhs = *lhs * rhs; }
     143static inline Int ?*=?( Int * lhs, long int rhs ) { return *lhs = *lhs * rhs; }
     144static inline Int ?*=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs * rhs; }
    145145
    146146// some code for operators "/" and "%" taken from g++ gmpxx.h
    147 Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; }
    148 Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; }
    149 Int ?/?( unsigned long int dividend, Int divisor ) {
     147static inline Int ?/?( Int dividend, Int divisor ) { Int quotient; mpz_tdiv_q( quotient.mpz, dividend.mpz, divisor.mpz ); return quotient; }
     148static inline Int ?/?( Int dividend, unsigned long int divisor ) { Int quotient; mpz_tdiv_q_ui( quotient.mpz, dividend.mpz, divisor ); return quotient; }
     149static inline Int ?/?( unsigned long int dividend, Int divisor ) {
    150150        Int quotient;
    151151    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
     
    164164        return quotient;
    165165} // ?/?
    166 Int ?/?( Int dividend, long int divisor ) {
     166static inline Int ?/?( Int dividend, long int divisor ) {
    167167        Int quotient;
    168168    if ( divisor >= 0 )
     
    174174        return quotient;
    175175} // ?/?
    176 Int ?/?( long int dividend, Int divisor ) {
     176static inline Int ?/?( long int dividend, Int divisor ) {
    177177        Int quotient;
    178178    if ( mpz_fits_slong_p( divisor.mpz ) )
     
    185185        return quotient;
    186186} // ?/?
    187 Int ?/=?( Int * lhs, Int rhs ) { return *lhs = *lhs / rhs; }
    188 Int ?/=?( Int * lhs, long int rhs ) { return *lhs = *lhs / rhs; }
    189 Int ?/=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs / rhs; }
    190 
    191 [ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; }
    192 [ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; }
    193 
    194 Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; }
    195 Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; }
    196 Int ?%?( unsigned long int dividend, Int divisor ) {
     187static inline Int ?/=?( Int * lhs, Int rhs ) { return *lhs = *lhs / rhs; }
     188static inline Int ?/=?( Int * lhs, long int rhs ) { return *lhs = *lhs / rhs; }
     189static inline Int ?/=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs / rhs; }
     190
     191static inline [ Int, Int ] div( Int dividend, Int divisor ) { Int quotient, remainder; mpz_fdiv_qr( quotient.mpz, remainder.mpz, dividend.mpz, divisor.mpz ); return [ quotient, remainder ]; }
     192static inline [ Int, Int ] div( Int dividend, unsigned long int divisor ) { Int quotient, remainder; mpz_fdiv_qr_ui( quotient.mpz, remainder.mpz, dividend.mpz, divisor ); return [ quotient, remainder ]; }
     193
     194static inline Int ?%?( Int dividend, Int divisor ) { Int remainder; mpz_tdiv_r( remainder.mpz, dividend.mpz, divisor.mpz ); return remainder; }
     195static inline Int ?%?( Int dividend, unsigned long int divisor ) { Int remainder; mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, divisor ); return remainder; }
     196static inline Int ?%?( unsigned long int dividend, Int divisor ) {
    197197        Int remainder;
    198198    if ( mpz_sgn( divisor.mpz ) >= 0 ) {
     
    210210        return remainder;
    211211} // ?%?
    212 Int ?%?( Int dividend, long int divisor ) {
     212static inline Int ?%?( Int dividend, long int divisor ) {
    213213        Int remainder;
    214214    mpz_tdiv_r_ui( remainder.mpz, dividend.mpz, (divisor >= 0 ? divisor : -divisor));
    215215        return remainder;
    216216} // ?%?
    217 Int ?%?( long int dividend, Int divisor ) {
     217static inline Int ?%?( long int dividend, Int divisor ) {
    218218        Int remainder;
    219219    if ( mpz_fits_slong_p( divisor.mpz ) )
     
    226226        return remainder;
    227227} // ?%?
    228 Int ?%=?( Int * lhs, Int rhs ) { return *lhs = *lhs % rhs; }
    229 Int ?%=?( Int * lhs, long int rhs ) { return *lhs = *lhs % rhs; }
    230 Int ?%=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs % rhs; }
    231 
    232 Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
    233 Int ?<<=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs << shift; }
    234 Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
    235 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs >> shift; }
     228static inline Int ?%=?( Int * lhs, Int rhs ) { return *lhs = *lhs % rhs; }
     229static inline Int ?%=?( Int * lhs, long int rhs ) { return *lhs = *lhs % rhs; }
     230static inline Int ?%=?( Int * lhs, unsigned long int rhs ) { return *lhs = *lhs % rhs; }
     231
     232static inline Int ?<<?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_mul_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
     233static inline Int ?<<=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs << shift; }
     234static inline Int ?>>?( Int shiften, mp_bitcnt_t shift ) { Int shifted; mpz_fdiv_q_2exp( shifted.mpz, shiften.mpz, shift ); return shifted; }
     235static inline Int ?>>=?( Int * lhs, mp_bitcnt_t shift ) { return *lhs = *lhs >> shift; }
    236236
    237237// number functions
    238 Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; }
    239 Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; }
    240 Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; }
    241 Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; }
    242 Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; }
    243 void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); }
    244 Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; }
    245 Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; }
    246 Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; }
    247 int sgn( Int oper ) { return mpz_sgn( oper.mpz ); }
    248 Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; }
     238static inline Int abs( Int oper ) { Int positive; mpz_abs( positive.mpz, oper.mpz ); return positive; }
     239static inline Int fact( unsigned long int N ) { Int factorial; mpz_fac_ui( factorial.mpz, N ); return factorial; }
     240static inline Int gcd( Int oper1, Int oper2 ) { Int gcdret; mpz_gcd( gcdret.mpz, oper1.mpz, oper2.mpz ); return gcdret; }
     241static inline Int pow( Int base, unsigned long int exponent ) { Int power; mpz_pow_ui( power.mpz, base.mpz, exponent ); return power; }
     242static inline Int pow( unsigned long int base, unsigned long int exponent ) { Int power; mpz_ui_pow_ui( power.mpz, base, exponent ); return power; }
     243static inline void srandom( gmp_randstate_t state ) { gmp_randinit_default( state ); }
     244static inline Int random( gmp_randstate_t state, mp_bitcnt_t n ) { Int rand; mpz_urandomb( rand.mpz, state, n ); return rand; }
     245static inline Int random( gmp_randstate_t state, Int n ) { Int rand; mpz_urandomm( rand.mpz, state, n.mpz ); return rand; }
     246static inline Int random( gmp_randstate_t state, mp_size_t max_size ) { Int rand; mpz_random( rand.mpz, max_size ); return rand; }
     247static inline int sgn( Int oper ) { return mpz_sgn( oper.mpz ); }
     248static inline Int sqrt( Int oper ) { Int root; mpz_sqrt( root.mpz, oper.mpz ); return root; }
    249249
    250250// I/O
    251 forall( dtype istype | istream( istype ) )
     251static inline forall( dtype istype | istream( istype ) )
    252252istype * ?|?( istype * is, Int * mp ) {
    253253        gmp_scanf( "%Zd", mp );
     
    255255} // ?|?
    256256
    257 forall( dtype ostype | ostream( ostype ) )
     257static inline forall( dtype ostype | ostream( ostype ) )
    258258ostype * ?|?( ostype * os, Int mp ) {
    259259        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
  • src/libcfa/stdlib

    ra029714 r5e8d7327  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 24 18:06:27 2017
    13 // Update Count     : 115
     12// Last Modified On : Tue May 30 09:07:35 2017
     13// Update Count     : 164
    1414//
    1515
     
    2828//---------------------------------------
    2929
    30 forall( dtype T | sized(T) ) T * malloc( void );
    31 forall( dtype T | sized(T) ) T * malloc( char fill );
    32 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size );
    33 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill );
    34 extern "C" { void * calloc( size_t nmemb, size_t size ); } // use default C routine for void *
    35 forall( dtype T | sized(T) ) T * calloc( size_t nmemb );
     30extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
     31
     32// allocation, non-array types
     33static inline forall( dtype T | sized(T) ) T * malloc( void ) {
     34        //printf( "X1\n" );
     35        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
     36} // malloc
     37static inline forall( dtype T | sized(T) ) T * malloc( char fill ) {
     38        //printf( "X2\n" );
     39        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
     40    return memset( ptr, (int)fill, sizeof(T) );                 // initial with fill value
     41} // malloc
     42
     43// allocation, array types
     44extern "C" { void * calloc( size_t dim, size_t size ); } // use default C routine for void *
     45static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
     46        //printf( "X3\n" );
     47        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
     48}
     49static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim ) { // alternative name
     50        //printf( "X4\n" );
     51        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     52} // amalloc
     53static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim, char fill ) { // alternative name
     54        //printf( "X5\n" );
     55        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     56    return memset( ptr, (int)fill, dim * sizeof(T) );
     57} // amalloc
     58
     59// resize, non-array types
    3660extern "C" { void * realloc( void * ptr, size_t size ); } // use default C routine for void *
    37 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size );
    38 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill );
    39 
    40 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment );
    41 forall( dtype T | sized(T) ) T * memalign( size_t alignment );          // deprecated
    42 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment );
    43 
     61static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
     62        //printf( "X5.5\n" );
     63        return (T *)(void *)realloc( (void *)ptr, size );
     64}
     65forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill );
     66static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative name
     67        //printf( "X7\n" );
     68        return realloc( ptr, size );
     69} // malloc
     70static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, char fill ) { // alternative name
     71        //printf( "X8\n" );
     72        return realloc( ptr, size, fill );
     73} // malloc
     74
     75// resize, array types
     76static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim ) {
     77        //printf( "X9\n" );
     78        return malloc( ptr, dim * (size_t)sizeof(T) );
     79} // amalloc
     80static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim, char fill ) {
     81        //printf( "X10\n" );
     82        return malloc( ptr, dim * (size_t)sizeof(T), fill );
     83} // amalloc
     84
     85// alignment, non-array types
     86extern "C" { void * memalign( size_t alignment, size_t size ); } // use default C routine for void *
     87static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
     88        //printf( "X11\n" );
     89        return (T *)memalign( alignment, sizeof(T) );
     90} // memalign
     91static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment, char fill ) {
     92        //printf( "X12\n" );
     93    T * ptr = (T *)memalign( alignment, sizeof(T) );
     94    return memset( ptr, (int)fill, sizeof(T) );
     95} // memalign
     96static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) {
     97        //printf( "X13\n" );
     98        return (T *)memalign( alignment, sizeof(T) );
     99} // aligned_alloc
     100extern "C" { int posix_memalign( void ** ptr, size_t alignment, size_t size ); } // use default C routine for void *
     101static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
     102        //printf( "X14\n" );
     103        return posix_memalign( (void **)ptr, alignment, sizeof(T) );
     104} // posix_memalign
     105
     106// alignment, array types
     107static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim ) {
     108        //printf( "X15\n" );
     109        return (T *)memalign( alignment, dim * sizeof(T) );
     110} // amemalign
     111static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim, char fill ) {
     112        //printf( "X16\n" );
     113    T * ptr = (T *)memalign( alignment, dim * sizeof(T) );
     114    return memset( ptr, (int)fill, dim * sizeof(T) );
     115} // amemalign
     116
     117// data, non-array types
     118static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
     119        //printf( "X17\n" );
     120        return memset( dest, c, sizeof(T) );
     121} // memset
     122extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
     123static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
     124        //printf( "X18\n" );
     125        return memcpy( dest, src, sizeof(T) );
     126} // memcpy
     127
     128// data, array types
     129static inline forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c ) {
     130        //printf( "X19\n" );
     131        return memset( dest, c, dim * sizeof(T) );
     132} // amemset
     133static inline forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim ) {
     134        //printf( "X20\n" );
     135        return memcpy( dest, src, dim * sizeof(T) );
     136} // amemcpy
     137
     138// allocation/deallocation and constructor/destructor
    44139forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p );
    45 forall( dtype T | { void ^?{}(T *); } ) void delete( T * ptr );
    46 forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } ) void delete( T * ptr, Params rest );
     140forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );
     141forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    47142
    48143//---------------------------------------
     
    77172
    78173forall( otype T | { int ?<?( T, T ); } )
    79 T * bsearch( T key, const T * arr, size_t dimension );
    80 
    81 forall( otype T | { int ?<?( T, T ); } )
    82 unsigned int bsearch( T key, const T * arr, size_t dimension );
    83 
    84 
    85 forall( otype T | { int ?<?( T, T ); } )
    86 void qsort( const T * arr, size_t dimension );
     174T * bsearch( T key, const T * arr, size_t dim );
     175
     176forall( otype T | { int ?<?( T, T ); } )
     177unsigned int bsearch( T key, const T * arr, size_t dim );
     178
     179
     180forall( otype T | { int ?<?( T, T ); } )
     181void qsort( const T * arr, size_t dim );
    87182
    88183//---------------------------------------
  • src/libcfa/stdlib.c

    ra029714 r5e8d7327  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 24 18:13:15 2017
    13 // Update Count     : 198
     12// Last Modified On : Tue May 30 09:07:56 2017
     13// Update Count     : 237
    1414//
    1515
     
    2121#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2222#include <stdlib.h>                                                                             // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
    23 #include <string.h>                                                                             // memset
     23#include <string.h>                                                                             // memcpy, memset
    2424#include <malloc.h>                                                                             // malloc_usable_size
    2525#include <math.h>                                                                               // fabsf, fabs, fabsl
     
    2727} // extern "C"
    2828
    29 forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe
    30     return (T *)(void *)malloc( (size_t)sizeof(T) );
    31 } // malloc
    32 
    33 forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc)
    34         T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
    35     return memset( ptr, (int)fill, sizeof(T) );
    36 } // malloc
    37 
    38 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
    39     return (T *)realloc( ptr, size );
    40 } // malloc
    41 
    42 forall( 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 
    47 forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
    48     return (T *)calloc( nmemb, sizeof(T) );
    49 } // calloc
    50 
    51 
    52 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
    53     return (T *)(void *)realloc( (void *)ptr, size );
     29// resize, non-array types
     30forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
     31        //printf( "X6\n" );
     32        size_t olen = malloc_usable_size( ptr );                        // current allocation
     33    char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
     34        size_t nlen = malloc_usable_size( nptr );                       // new allocation
     35        if ( nlen > olen ) {                                                            // larger ?
     36                memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
     37        } //
     38    return (T *)nptr;
    5439} // realloc
    5540
    56 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
    57     char * nptr = (T *)(void *)realloc( (void *)ptr, size );
    58     size_t unused = malloc_usable_size( nptr );
    59     memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
    60     return nptr;
    61 } // realloc
    62 
    63 
    64 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
    65     return (T *)memalign( alignment, sizeof(T) );
    66 } // aligned_alloc
    67 
    68 forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
    69     return (T *)memalign( alignment, sizeof(T) );
    70 } // memalign
    71 
    72 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
    73     return posix_memalign( (void **)ptr, alignment, sizeof(T) );
    74 } // posix_memalign
    75 
    76 
    77 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new
     41// allocation/deallocation and constructor/destructor
     42forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
    7843T * new( Params p ) {
    7944        return ((T *)malloc()){ p };
    8045} // new
    8146
    82 forall( dtype T | { void ^?{}(T *); } )                                 // delete
     47forall( dtype T | { void ^?{}( T * ); } )
    8348void delete( T * ptr ) {
    8449        if ( ptr ) {
    85                 ^ptr{};
     50                ^ptr{};                                                                                 // run destructor
    8651                free( ptr );
    87         }
     52        } // if
    8853} // delete
    8954
    90 forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
     55forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    9156void delete( T * ptr, Params rest ) {
    9257        if ( ptr ) {
    93                 ^ptr{};
     58                ^ptr{};                                                                                 // run destructor
    9459                free( ptr );
    95         }
     60        } // if
    9661        delete( rest );
    9762} // delete
     
    242207
    243208forall( otype T | { int ?<?( T, T ); } )
    244 T * bsearch( T key, const T * arr, size_t dimension ) {
     209T * bsearch( T key, const T * arr, size_t dim ) {
    245210        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    246         return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
     211        return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
    247212} // bsearch
    248213
    249214forall( otype T | { int ?<?( T, T ); } )
    250 unsigned int bsearch( T key, const T * arr, size_t dimension ) {
    251         T *result = bsearch( key, arr, dimension );
    252         return result ? result - arr : dimension;                       // pointer subtraction includes sizeof(T)
     215unsigned int bsearch( T key, const T * arr, size_t dim ) {
     216        T *result = bsearch( key, arr, dim );
     217        return result ? result - arr : dim;                                     // pointer subtraction includes sizeof(T)
    253218} // bsearch
    254219
    255220forall( otype T | { int ?<?( T, T ); } )
    256 void qsort( const T * arr, size_t dimension ) {
     221void qsort( const T * arr, size_t dim ) {
    257222        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    258         qsort( arr, dimension, sizeof(T), comp );
     223        qsort( arr, dim, sizeof(T), comp );
    259224} // qsort
    260225
Note: See TracChangeset for help on using the changeset viewer.