Changeset b6838214 for src/libcfa


Ignore:
Timestamp:
Jan 23, 2018, 5:46:43 PM (6 years ago)
Author:
Alan Kennedy <afakenne@…>
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:
258e6ad5
Parents:
b158d8f (diff), 15d248e (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:

add context switch for ARM

Location:
src/libcfa
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/bits/defs.h

    rb158d8f rb6838214  
    1 //
     1// 
    22// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
    55// file "LICENCE" distributed with Cforall.
    6 //
    7 // bits/defs.h --
    8 //
     6// 
     7// defs.h --
     8// 
    99// Author           : Thierry Delisle
    10 // Created On       : Thu Nov 09 13:24:10 2017
    11 // Last Modified By :
    12 // Last Modified On :
    13 // Update Count     :
    14 //
     10// Created On       : Thu Nov  9 13:24:10 2017
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Tue Jan  2 09:17:06 2018
     13// Update Count     : 2
     14// 
    1515
    1616#pragma once
     
    2020#include <stdint.h>
    2121
    22 #define unlikely(x)    __builtin_expect(!!(x), 0)
    23 #define likely  (x)    __builtin_expect(!!(x), 1)
     22#define likely(x)   __builtin_expect(!!(x), 1)
     23#define unlikely(x) __builtin_expect(!!(x), 0)
    2424#define thread_local _Thread_local
    2525
  • src/libcfa/concurrency/monitor.c

    rb158d8f rb6838214  
    534534        __lock_size_t actual_count = aggregate( mon_storage, mask );
    535535
    536         __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);
     536        __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max);
    537537
    538538        if(actual_count == 0) return;
     
    575575                                monitor_save;
    576576
    577                                 __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %d monitors : ", count );
     577                                __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %"PRIdFAST16" monitors : ", count );
    578578                                #ifdef __CFA_DEBUG_PRINT__
    579579                                        for( int i = 0; i < count; i++) {
  • src/libcfa/concurrency/preemption.c

    rb158d8f rb6838214  
    303303        // Check if it is safe to preempt here
    304304        if( !preemption_ready() ) { return; }
     305
     306        // __cfaabi_dbg_print_buffer_decl(" KERNEL: preempting core %p (%p).\n", this_processor, this_thread);
    305307
    306308        preemption_in_progress = true;                      // Sync flag : prevent recursive calls to the signal handler
  • src/libcfa/fstream

    rb158d8f rb6838214  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec  7 08:06:11 2017
    13 // Update Count     : 129
     12// Last Modified On : Thu Dec  7 15:17:26 2017
     13// Update Count     : 130
    1414//
    1515
     
    5252int flush( ofstream & );
    5353void open( ofstream &, const char * name, const char * mode );
     54void open( ofstream &, const char * name );
    5455void close( ofstream & );
    5556ofstream & write( ofstream &, const char * data, unsigned long int size );
     
    5859void ?{}( ofstream & os );
    5960void ?{}( ofstream & os, const char * name, const char * mode );
     61void ?{}( ofstream & os, const char * name );
    6062
    6163extern ofstream & sout, & serr;
     
    6971int fail( ifstream & is );
    7072int eof( ifstream & is );
     73void open( ifstream & is, const char * name, const char * mode );
    7174void open( ifstream & is, const char * name );
    7275void close( ifstream & is );
     
    7679
    7780void ?{}( ifstream & is );
     81void ?{}( ifstream & is, const char * name, const char * mode );
    7882void ?{}( ifstream & is, const char * name );
    7983
  • src/libcfa/fstream.c

    rb158d8f rb6838214  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec  7 08:35:01 2017
    13 // Update Count     : 270
     12// Last Modified On : Sat Dec  9 09:31:23 2017
     13// Update Count     : 275
    1414//
    1515
     
    4646
    4747// public
    48 void ?{}( ofstream & os ) {}
     48void ?{}( ofstream & os ) { os.file = 0; }
    4949
    5050void ?{}( ofstream & os, const char * name, const char * mode ) {
    5151        open( os, name, mode );
     52}
     53void ?{}( ofstream & os, const char * name ) {
     54        open( os, name, "w" );
    5255}
    5356
     
    8487
    8588int fail( ofstream & os ) {
    86         return ferror( (FILE *)(os.file) );
     89        return os.file == 0 || ferror( (FILE *)(os.file) );
    8790} // fail
    8891
     
    9396void open( ofstream & os, const char * name, const char * mode ) {
    9497        FILE *file = fopen( name, mode );
    95         if ( file == 0 ) {                                                                      // do not change unless successful
    96                 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
    97                 perror( 0 );
    98                 exit( EXIT_FAILURE );
    99         } // if
     98        // if ( file == 0 ) {                                                                   // do not change unless successful
     99        //      fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
     100        //      perror( 0 );
     101        //      exit( EXIT_FAILURE );
     102        // } // if
    100103        (os){ file, true, false, " ", ", " };
     104} // open
     105
     106void open( ofstream & os, const char * name ) {
     107        open( os, name, "w" );
    101108} // open
    102109
     
    152159
    153160// public
    154 void ?{}( ifstream & is ) {}
    155 
     161void ?{}( ifstream & is ) {     is.file = 0; }
     162
     163void ?{}( ifstream & is, const char * name, const char * mode ) {
     164        open( is, name, mode );
     165}
    156166void ?{}( ifstream & is, const char * name ) {
    157         open( is, name );
     167        open( is, name, "r" );
    158168}
    159169
    160170int fail( ifstream & is ) {
    161         return ferror( (FILE *)(is.file) );
     171        return is.file == 0 || ferror( (FILE *)(is.file) );
    162172} // fail
    163173
     
    166176} // eof
    167177
     178void open( ifstream & is, const char * name, const char * mode ) {
     179        FILE *file = fopen( name, mode );
     180        // if ( file == 0 ) {                                                                   // do not change unless successful
     181        //      fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
     182        //      perror( 0 );
     183        //      exit( EXIT_FAILURE );
     184        // } // if
     185        is.file = file;
     186} // open
     187
    168188void open( ifstream & is, const char * name ) {
    169         FILE *file = fopen( name, "r" );
    170         if ( file == 0 ) {                                                                      // do not change unless successful
    171                 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
    172                 perror( 0 );
    173                 exit( EXIT_FAILURE );
    174         } // if
    175         is.file = file;
     189        open( is, name, "r" );
    176190} // open
    177191
  • src/libcfa/iostream

    rb158d8f rb6838214  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Dec  6 23:03:30 2017
    13 // Update Count     : 144
     12// Last Modified On : Thu Dec 21 13:55:41 2017
     13// Update Count     : 145
    1414//
    1515
     
    5555
    5656// implement writable for intrinsic types
     57
     58forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, _Bool );
    5759
    5860forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype &, char );
  • src/libcfa/iostream.c

    rb158d8f rb6838214  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Dec  6 23:03:43 2017
    13 // Update Count     : 426
     12// Last Modified On : Thu Dec 21 13:55:09 2017
     13// Update Count     : 427
    1414//
    1515
     
    2323#include <complex.h>                                                                    // creal, cimag
    2424}
     25
     26forall( dtype ostype | ostream( ostype ) )
     27ostype & ?|?( ostype & os, _Bool b ) {
     28        if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     29        fmt( os, "%s", b ? "true" : "false" );
     30        return os;
     31} // ?|?
    2532
    2633forall( dtype ostype | ostream( ostype ) )
  • src/libcfa/stdlib

    rb158d8f rb6838214  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Oct 31 13:47:24 2017
    13 // Update Count     : 245
     12// Last Modified On : Tue Jan  2 12:21:04 2018
     13// Update Count     : 292
    1414//
    1515
    1616#pragma once
     17
     18//#define _XOPEN_SOURCE 600                                                             // posix_memalign, *rand48
     19#include <stdlib.h>                                                                             // strto*, *abs
    1720
    1821//---------------------------------------
     
    7780        //printf( "X8\n" );
    7881        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
    79     return (T *)memset( ptr, (int)fill, sizeof(T) );                    // initial with fill value
     82    return (T *)memset( ptr, (int)fill, sizeof(T) );    // initial with fill value
    8083} // alloc
    8184
     
    150153//---------------------------------------
    151154
    152 int ato( const char * ptr );
    153 unsigned int ato( const char * ptr );
    154 long int ato( const char * ptr );
    155 unsigned long int ato( const char * ptr );
    156 long long int ato( const char * ptr );
    157 unsigned long long int ato( const char * ptr );
    158 float ato( const char * ptr );
    159 double ato( const char * ptr );
    160 long double ato( const char * ptr );
    161 float _Complex ato( const char * ptr );
    162 double _Complex ato( const char * ptr );
    163 long double _Complex ato( const char * ptr );
    164 
    165 int strto( const char * sptr, char ** eptr, int base );
    166 unsigned int strto( const char * sptr, char ** eptr, int base );
    167 long int strto( const char * sptr, char ** eptr, int base );
    168 unsigned long int strto( const char * sptr, char ** eptr, int base );
    169 long long int strto( const char * sptr, char ** eptr, int base );
    170 unsigned long long int strto( const char * sptr, char ** eptr, int base );
    171 float strto( const char * sptr, char ** eptr );
    172 double strto( const char * sptr, char ** eptr );
    173 long double strto( const char * sptr, char ** eptr );
     155static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
     156static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
     157static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
     158static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
     159static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
     160static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
     161
     162static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
     163static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
     164static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
     165
    174166float _Complex strto( const char * sptr, char ** eptr );
    175167double _Complex strto( const char * sptr, char ** eptr );
    176168long double _Complex strto( const char * sptr, char ** eptr );
    177169
    178 //---------------------------------------
    179 
    180 forall( otype T | { int ?<?( T, T ); } )
    181 T * bsearch( T key, const T * arr, size_t dim );
    182 
    183 forall( otype T | { int ?<?( T, T ); } )
    184 unsigned int bsearch( T key, const T * arr, size_t dim );
    185 
    186 
    187 forall( otype T | { int ?<?( T, T ); } )
    188 void qsort( const T * arr, size_t dim );
     170static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
     171static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
     172static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
     173static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
     174static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
     175static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
     176
     177static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
     178static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
     179static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
     180
     181static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     182static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     183static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     184
     185//---------------------------------------
     186
     187forall( otype E | { int ?<?( E, E ); } )
     188E * bsearch( E key, const E * vals, size_t dim );
     189
     190forall( otype E | { int ?<?( E, E ); } )
     191size_t bsearch( E key, const E * vals, size_t dim );
     192
     193forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     194E * bsearch( K key, const E * vals, size_t dim );
     195
     196forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     197size_t bsearch( K key, const E * vals, size_t dim );
     198
     199
     200forall( otype E | { int ?<?( E, E ); } )
     201E * bsearchl( E key, const E * vals, size_t dim );
     202
     203forall( otype E | { int ?<?( E, E ); } )
     204size_t bsearchl( E key, const E * vals, size_t dim );
     205
     206forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     207E * bsearchl( K key, const E * vals, size_t dim );
     208
     209forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     210size_t bsearchl( K key, const E * vals, size_t dim );
     211
     212
     213forall( otype E | { int ?<?( E, E ); } )
     214E * bsearchu( E key, const E * vals, size_t dim );
     215
     216forall( otype E | { int ?<?( E, E ); } )
     217size_t bsearchu( E key, const E * vals, size_t dim );
     218
     219forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     220E * bsearchu( K key, const E * vals, size_t dim );
     221
     222forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     223size_t bsearchu( K key, const E * vals, size_t dim );
     224
     225
     226forall( otype E | { int ?<?( E, E ); } )
     227void qsort( E * vals, size_t dim );
    189228
    190229//---------------------------------------
     
    198237//---------------------------------------
    199238
    200 unsigned char abs( signed char );
     239static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
    201240extern "C" { int abs( int ); }                                                  // use default C routine for int
    202 unsigned long int abs( long int );
    203 unsigned long long int abs( long long int );
    204 float abs( float );
    205 double abs( double );
    206 long double abs( long double );
    207 float abs( float _Complex );
    208 double abs( double _Complex );
    209 long double abs( long double _Complex );
     241static inline unsigned long int abs( long int v ) { return labs( v ); }
     242static inline unsigned long long int abs( long long int v ) { return llabs( v ); }
     243
     244extern "C" {
     245double fabs( double );
     246float fabsf( float );
     247long double fabsl( long double );
     248} // extern "C"
     249static inline float abs( float x ) { return fabsf( x ); }
     250static inline double abs( double x ) { return fabs( x ); }
     251static inline long double abs( long double x ) { return fabsl( x ); }
     252
     253extern "C" {
     254double cabs( double _Complex );
     255float cabsf( float _Complex );
     256long double cabsl( long double _Complex );
     257} // extern "C"
     258static inline float abs( float _Complex x ) { return cabsf( x ); }
     259static inline double abs( double _Complex x ) { return cabs( x ); }
     260static inline long double abs( long double _Complex x ) { return cabsl( x ); }
     261
    210262forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
    211263T abs( T );
     
    213265//---------------------------------------
    214266
    215 void random_seed( long int s );
     267extern "C" { void srandom( unsigned int seed ); }               // override C version
    216268char random( void );
     269char random( char u );
    217270char random( char l, char u );
    218271int random( void );
     272int random( int u );
     273int random( int l, int u );
    219274unsigned int random( void );
    220275unsigned int random( unsigned int u );
    221276unsigned int random( unsigned int l, unsigned int u );
    222 //long int random( void );
     277extern "C" { long int random( void ); }                                 // override C version
     278long int random( long int u );
     279long int random( long int l, long int u );
    223280unsigned long int random( void );
    224281unsigned long int random( unsigned long int u );
     
    233290
    234291forall( otype T | { int ?<?( T, T ); } )
    235 T min( T t1, T t2 );
     292static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }
    236293
    237294forall( otype T | { int ?>?( T, T ); } )
    238 T max( T t1, T t2 );
     295static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }
    239296
    240297forall( otype T | { T min( T, T ); T max( T, T ); } )
    241 T clamp( T value, T min_val, T max_val );
     298static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }
    242299
    243300forall( otype T )
    244 void swap( T & t1, T & t2 );
     301static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }
    245302
    246303// Local Variables: //
  • src/libcfa/stdlib.c

    rb158d8f rb6838214  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Oct 30 22:43:02 2017
    13 // Update Count     : 297
     12// Last Modified On : Tue Jan  2 12:20:32 2018
     13// Update Count     : 441
    1414//
    1515
     
    1919
    2020#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    21 #include <stdlib.h>                                                                             // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
    2221#include <string.h>                                                                             // memcpy, memset
    2322#include <malloc.h>                                                                             // malloc_usable_size
    2423#include <math.h>                                                                               // fabsf, fabs, fabsl
    2524#include <complex.h>                                                                    // _Complex_I
     25#include <assert.h>
    2626
    2727// resize, non-array types
     
    9393//---------------------------------------
    9494
    95 int ato( const char * ptr ) {
    96         int i;
    97         if ( sscanf( ptr, "%d", &i ) == EOF ) {}
    98         return i;
    99 } // ato
    100 
    101 unsigned int ato( const char * ptr ) {
    102         unsigned int ui;
    103         if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
    104         return ui;
    105 } // ato
    106 
    107 long int ato( const char * ptr ) {
    108         long int li;
    109         if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
    110         return li;
    111 } // ato
    112 
    113 unsigned long int ato( const char * ptr ) {
    114         unsigned long int uli;
    115         if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
    116         return uli;
    117 } // ato
    118 
    119 long long int ato( const char * ptr ) {
    120         long long int lli;
    121         if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
    122         return lli;
    123 } // ato
    124 
    125 unsigned long long int ato( const char * ptr ) {
    126         unsigned long long int ulli;
    127         if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
    128         return ulli;
    129 } // ato
    130 
    131 
    132 float ato( const char * ptr ) {
    133         float f;
    134         if ( sscanf( ptr, "%f", &f ) == EOF ) {}
    135         return f;
    136 } // ato
    137 
    138 double ato( const char * ptr ) {
    139         double d;
    140         if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
    141         return d;
    142 } // ato
    143 
    144 long double ato( const char * ptr ) {
    145         long double ld;
    146         if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
    147         return ld;
    148 } // ato
    149 
    150 
    151 float _Complex ato( const char * ptr ) {
    152         float re, im;
    153         if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
    154         return re + im * _Complex_I;
    155 } // ato
    156 
    157 double _Complex ato( const char * ptr ) {
    158         double re, im;
    159         if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
    160         return re + im * _Complex_I;
    161 } // ato
    162 
    163 long double _Complex ato( const char * ptr ) {
    164         long double re, im;
    165         if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
    166         return re + im * _Complex_I;
    167 } // ato
    168 
    169 
    170 int strto( const char * sptr, char ** eptr, int base ) {
    171         return (int)strtol( sptr, eptr, base );
    172 } // strto
    173 
    174 unsigned int strto( const char * sptr, char ** eptr, int base ) {
    175         return (unsigned int)strtoul( sptr, eptr, base );
    176 } // strto
    177 
    178 long int strto( const char * sptr, char ** eptr, int base ) {
    179         return strtol( sptr, eptr, base );
    180 } // strto
    181 
    182 unsigned long int strto( const char * sptr, char ** eptr, int base ) {
    183         return strtoul( sptr, eptr, base );
    184 } // strto
    185 
    186 long long int strto( const char * sptr, char ** eptr, int base ) {
    187         return strtoll( sptr, eptr, base );
    188 } // strto
    189 
    190 unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
    191         return strtoull( sptr, eptr, base );
    192 } // strto
    193 
    194 
    195 float strto( const char * sptr, char ** eptr ) {
    196         return strtof( sptr, eptr );
    197 } // strto
    198 
    199 double strto( const char * sptr, char ** eptr ) {
    200         return strtod( sptr, eptr );
    201 } // strto
    202 
    203 long double strto( const char * sptr, char ** eptr ) {
    204         return strtold( sptr, eptr );
    205 } // strto
    206 
    207 
    20895float _Complex strto( const char * sptr, char ** eptr ) {
    20996        float re, im;
    210         re = strtof( sptr, eptr );
    211         if ( sptr == *eptr ) return 0.0;
    212         im = strtof( sptr, eptr );
    213         if ( sptr == *eptr ) return 0.0;
     97        char * eeptr;
     98        re = strtof( sptr, &eeptr );
     99        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
     100        im = strtof( eeptr, &eeptr );
     101        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
     102        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
    214103        return re + im * _Complex_I;
    215104} // strto
     
    217106double _Complex strto( const char * sptr, char ** eptr ) {
    218107        double re, im;
    219         re = strtod( sptr, eptr );
    220         if ( sptr == *eptr ) return 0.0;
    221         im = strtod( sptr, eptr );
    222         if ( sptr == *eptr ) return 0.0;
     108        char * eeptr;
     109        re = strtod( sptr, &eeptr );
     110        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
     111        im = strtod( eeptr, &eeptr );
     112        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
     113        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
    223114        return re + im * _Complex_I;
    224115} // strto
     
    226117long double _Complex strto( const char * sptr, char ** eptr ) {
    227118        long double re, im;
    228         re = strtold( sptr, eptr );
    229         if ( sptr == *eptr ) return 0.0;
    230         im = strtold( sptr, eptr );
    231         if ( sptr == *eptr ) return 0.0;
     119        char * eeptr;
     120        re = strtold( sptr, &eeptr );
     121        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
     122        im = strtold( eeptr, &eeptr );
     123        if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
     124        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
    232125        return re + im * _Complex_I;
    233126} // strto
     
    235128//---------------------------------------
    236129
    237 forall( otype T | { int ?<?( T, T ); } )
    238 T * bsearch( T key, const T * arr, size_t dim ) {
    239         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    240         return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
    241 } // bsearch
    242 
    243 forall( otype T | { int ?<?( T, T ); } )
    244 unsigned int bsearch( T key, const T * arr, size_t dim ) {
    245         T *result = bsearch( key, arr, dim );
    246         return result ? result - arr : dim;                                     // pointer subtraction includes sizeof(T)
    247 } // bsearch
    248 
    249 forall( otype T | { int ?<?( T, T ); } )
    250 void qsort( const T * arr, size_t dim ) {
    251         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    252         qsort( arr, dim, sizeof(T), comp );
     130forall( otype E | { int ?<?( E, E ); } )
     131E * bsearch( E key, const E * vals, size_t dim ) {
     132        int cmp( const void * t1, const void * t2 ) {
     133                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     134        } // cmp
     135        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     136} // bsearch
     137
     138forall( otype E | { int ?<?( E, E ); } )
     139size_t bsearch( E key, const E * vals, size_t dim ) {
     140        E * result = bsearch( key, vals, dim );
     141        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
     142} // bsearch
     143
     144forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     145E * bsearch( K key, const E * vals, size_t dim ) {
     146        int cmp( const void * t1, const void * t2 ) {
     147                return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
     148        } // cmp
     149        return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
     150} // bsearch
     151
     152forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     153size_t bsearch( K key, const E * vals, size_t dim ) {
     154        E * result = bsearch( key, vals, dim );
     155        return result ? result - vals : dim;                            // pointer subtraction includes sizeof(E)
     156} // bsearch
     157
     158
     159forall( otype E | { int ?<?( E, E ); } )
     160size_t bsearchl( E key, const E * vals, size_t dim ) {
     161        size_t l = 0, m, h = dim;
     162        while ( l < h ) {
     163                m = (l + h) / 2;
     164                if ( (E &)(vals[m]) < key ) {                                   // cast away const
     165                        l = m + 1;
     166                } else {
     167                        h = m;
     168                } // if
     169        } // while
     170        return l;
     171} // bsearchl
     172
     173forall( otype E | { int ?<?( E, E ); } )
     174E * bsearchl( E key, const E * vals, size_t dim ) {
     175        size_t posn = bsearchl( key, vals, dim );
     176        return (E *)(&vals[posn]);                                                      // cast away const
     177} // bsearchl
     178
     179forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     180size_t bsearchl( K key, const E * vals, size_t dim ) {
     181        size_t l = 0, m, h = dim;
     182        while ( l < h ) {
     183                m = (l + h) / 2;
     184                if ( getKey( vals[m] ) < key ) {
     185                        l = m + 1;
     186                } else {
     187                        h = m;
     188                } // if
     189        } // while
     190        return l;
     191} // bsearchl
     192
     193forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     194E * bsearchl( K key, const E * vals, size_t dim ) {
     195        size_t posn = bsearchl( key, vals, dim );
     196        return (E *)(&vals[posn]);                                                      // cast away const
     197} // bsearchl
     198
     199
     200forall( otype E | { int ?<?( E, E ); } )
     201size_t bsearchu( E key, const E * vals, size_t dim ) {
     202        size_t l = 0, m, h = dim;
     203        while ( l < h ) {
     204                m = (l + h) / 2;
     205                if ( ! ( key < (E &)(vals[m]) ) ) {                             // cast away const
     206                        l = m + 1;
     207                } else {
     208                        h = m;
     209                } // if
     210        } // while
     211        return l;
     212} // bsearchu
     213
     214forall( otype E | { int ?<?( E, E ); } )
     215E * bsearchu( E key, const E * vals, size_t dim ) {
     216        size_t posn = bsearchu( key, vals, dim );
     217        return (E *)(&vals[posn]);
     218} // bsearchu
     219
     220forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     221size_t bsearchu( K key, const E * vals, size_t dim ) {
     222        size_t l = 0, m, h = dim;
     223        while ( l < h ) {
     224                m = (l + h) / 2;
     225                if ( ! ( key < getKey( vals[m] ) ) ) {
     226                        l = m + 1;
     227                } else {
     228                        h = m;
     229                } // if
     230        } // while
     231        return l;
     232} // bsearchu
     233
     234forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
     235E * bsearchu( K key, const E * vals, size_t dim ) {
     236        size_t posn = bsearchu( key, vals, dim );
     237        return (E *)(&vals[posn]);
     238} // bsearchu
     239
     240
     241forall( otype E | { int ?<?( E, E ); } )
     242void qsort( E * vals, size_t dim ) {
     243        int cmp( const void * t1, const void * t2 ) {
     244                return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
     245        } // cmp
     246        qsort( vals, dim, sizeof(E), cmp );
    253247} // qsort
    254248
     
    263257//---------------------------------------
    264258
    265 unsigned char abs( signed char v ) { return abs( (int)v ); }
    266 unsigned long int abs( long int v ) { return labs( v ); }
    267 unsigned long long int abs( long long int v ) { return llabs( v ); }
    268 float abs( float x ) { return fabsf( x ); }
    269 double abs( double x ) { return fabs( x ); }
    270 long double abs( long double x ) { return fabsl( x ); }
    271 float abs( float _Complex x ) { return cabsf( x ); }
    272 double abs( double _Complex x ) { return cabs( x ); }
    273 long double abs( long double _Complex x ) { return cabsl( x ); }
    274 
    275 //---------------------------------------
    276 
    277 void random_seed( long int s ) { srand48( s ); }
    278 char random( void ) { return mrand48(); }
    279 char random( char l, char u ) { return lrand48() % (u - l) + l; }
    280 int random( void ) { return mrand48(); }
    281 unsigned int random( void ) { return lrand48(); }
    282 unsigned int random( unsigned int u ) { return lrand48() % u; }
    283 unsigned int random( unsigned int l, unsigned int u ) { return lrand48() % (u - l) + l; }
    284 //long int random( void ) { return mrand48(); }
     259extern "C" { void srandom( unsigned int seed ) { srand48( seed ); } } // override C version
     260char random( void ) { return (unsigned long int)random(); }
     261char random( char u ) { return random( (unsigned long int)u ); }
     262char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
     263int random( void ) { return (long int)random(); }
     264int random( int u ) { return random( (long int)u ); }
     265int random( int l, int u ) { return random( (long int)l, (long int)u ); }
     266unsigned int random( void ) { return (unsigned long int)random(); }
     267unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); }
     268unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
     269extern "C" { long int random( void ) { return mrand48(); } } // override C version
     270long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); }
     271long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
    285272unsigned long int random( void ) { return lrand48(); }
    286273unsigned long int random( unsigned long int u ) { return lrand48() % u; }
    287 unsigned long int random( unsigned long int l, unsigned long int u ) { return lrand48() % (u - l) + l; }
    288 float random( void ) { return (float)drand48(); }               // otherwise float uses lrand48
     274unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
     275float random( void ) { return (float)drand48(); }               // cast otherwise float uses lrand48
    289276double random( void ) { return drand48(); }
    290277float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
    291278double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
    292 long double _Complex random( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
    293 
    294 //---------------------------------------
    295 
    296 forall( otype T | { int ?<?( T, T ); } )
    297 T min( T t1, T t2 ) {
    298         return t1 < t2 ? t1 : t2;
    299 } // min
    300 
    301 forall( otype T | { int ?>?( T, T ); } )
    302 T max( T t1, T t2 ) {
    303         return t1 > t2 ? t1 : t2;
    304 } // max
    305 
    306 forall( otype T | { T min( T, T ); T max( T, T ); } )
    307 T clamp( T value, T min_val, T max_val ) {
    308         return max( min_val, min( value, max_val ) );
    309 } // clamp
    310 
    311 forall( otype T )
    312 void swap( T & t1, T & t2 ) {
    313         T temp = t1;
    314         t1 = t2;
    315         t2 = temp;
    316 } // swap
     279long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
     280
    317281
    318282// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.