Changeset e672372 for src/libcfa
- Timestamp:
- Dec 25, 2017, 11:43:00 AM (7 years ago)
- 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:
- 9c47a47, d9ff69a
- Parents:
- 1e6e08de
- Location:
- src/libcfa
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/stdlib
r1e6e08de re672372 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Oct 31 13:47:24201713 // Update Count : 2 4512 // Last Modified On : Sat Dec 23 18:21:42 2017 13 // Update Count : 267 14 14 // 15 15 16 16 #pragma once 17 18 //#define _XOPEN_SOURCE 600 // posix_memalign, *rand48 19 #include <stdlib.h> // strto*, *abs 17 20 18 21 //--------------------------------------- … … 77 80 //printf( "X8\n" ); 78 81 T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 79 return (T *)memset( ptr, (int)fill, sizeof(T) ); 82 return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value 80 83 } // alloc 81 84 … … 150 153 //--------------------------------------- 151 154 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 ); 155 static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } 156 static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } 157 static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } 158 static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } 159 static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } 160 static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } 161 162 static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } 163 static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } 164 static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } 165 174 166 float _Complex strto( const char * sptr, char ** eptr ); 175 167 double _Complex strto( const char * sptr, char ** eptr ); 176 168 long double _Complex strto( const char * sptr, char ** eptr ); 169 170 static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); } 171 static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } 172 static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } 173 static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } 174 static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } 175 static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } 176 177 static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); } 178 static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); } 179 static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); } 180 181 static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } 182 static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } 183 static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } 177 184 178 185 //--------------------------------------- … … 198 205 //--------------------------------------- 199 206 200 unsigned char abs( signed char ); 207 static inline unsigned char abs( signed char v ) { return abs( (int)v ); } 201 208 extern "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 ); 209 static inline unsigned long int abs( long int v ) { return labs( v ); } 210 static inline unsigned long long int abs( long long int v ) { return llabs( v ); } 211 212 extern "C" { 213 double fabs( double ); 214 float fabsf( float ); 215 long double fabsl( long double ); 216 } // extern "C" 217 static inline float abs( float x ) { return fabsf( x ); } 218 static inline double abs( double x ) { return fabs( x ); } 219 static inline long double abs( long double x ) { return fabsl( x ); } 220 221 extern "C" { 222 double cabs( double _Complex ); 223 float cabsf( float _Complex ); 224 long double cabsl( long double _Complex ); 225 } // extern "C" 226 static inline float abs( float _Complex x ) { return cabsf( x ); } 227 static inline double abs( double _Complex x ) { return cabs( x ); } 228 static inline long double abs( long double _Complex x ) { return cabsl( x ); } 229 210 230 forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } ) 211 231 T abs( T ); … … 215 235 void random_seed( long int s ); 216 236 char random( void ); 237 char random( char u ); 217 238 char random( char l, char u ); 218 239 int random( void ); 240 int random( int u ); 241 int random( int l, int u ); 219 242 unsigned int random( void ); 220 243 unsigned int random( unsigned int u ); 221 244 unsigned int random( unsigned int l, unsigned int u ); 222 //long int random( void ); 245 extern "C" { long int random( void ); } 246 long int random( long int u ); 247 long int random( long int l, long int u ); 223 248 unsigned long int random( void ); 224 249 unsigned long int random( unsigned long int u ); … … 233 258 234 259 forall( otype T | { int ?<?( T, T ); } ) 235 T min( T t1, T t2 ); 260 static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; } 236 261 237 262 forall( otype T | { int ?>?( T, T ); } ) 238 T max( T t1, T t2 ); 263 static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; } 239 264 240 265 forall( otype T | { T min( T, T ); T max( T, T ); } ) 241 T clamp( T value, T min_val, T max_val ); 266 static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); } 242 267 243 268 forall( otype T ) 244 void swap( T & t1, T & t2 ); 269 static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; } 245 270 246 271 // Local Variables: // -
src/libcfa/stdlib.c
r1e6e08de re672372 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Oct 30 22:43:02201713 // Update Count : 29712 // Last Modified On : Sun Dec 24 13:00:15 2017 13 // Update Count : 344 14 14 // 15 15 … … 19 19 20 20 #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 21 #include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch22 21 #include <string.h> // memcpy, memset 23 22 #include <malloc.h> // malloc_usable_size 24 23 #include <math.h> // fabsf, fabs, fabsl 25 24 #include <complex.h> // _Complex_I 25 #include <assert.h> 26 26 27 27 // resize, non-array types … … 93 93 //--------------------------------------- 94 94 95 int ato( const char * ptr ) {96 int i;97 if ( sscanf( ptr, "%d", &i ) == EOF ) {}98 return i;99 } // ato100 101 unsigned int ato( const char * ptr ) {102 unsigned int ui;103 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}104 return ui;105 } // ato106 107 long int ato( const char * ptr ) {108 long int li;109 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}110 return li;111 } // ato112 113 unsigned long int ato( const char * ptr ) {114 unsigned long int uli;115 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}116 return uli;117 } // ato118 119 long long int ato( const char * ptr ) {120 long long int lli;121 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}122 return lli;123 } // ato124 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 } // ato130 131 132 float ato( const char * ptr ) {133 float f;134 if ( sscanf( ptr, "%f", &f ) == EOF ) {}135 return f;136 } // ato137 138 double ato( const char * ptr ) {139 double d;140 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}141 return d;142 } // ato143 144 long double ato( const char * ptr ) {145 long double ld;146 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}147 return ld;148 } // ato149 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 } // ato156 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 } // ato162 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 } // ato168 169 170 int strto( const char * sptr, char ** eptr, int base ) {171 return (int)strtol( sptr, eptr, base );172 } // strto173 174 unsigned int strto( const char * sptr, char ** eptr, int base ) {175 return (unsigned int)strtoul( sptr, eptr, base );176 } // strto177 178 long int strto( const char * sptr, char ** eptr, int base ) {179 return strtol( sptr, eptr, base );180 } // strto181 182 unsigned long int strto( const char * sptr, char ** eptr, int base ) {183 return strtoul( sptr, eptr, base );184 } // strto185 186 long long int strto( const char * sptr, char ** eptr, int base ) {187 return strtoll( sptr, eptr, base );188 } // strto189 190 unsigned long long int strto( const char * sptr, char ** eptr, int base ) {191 return strtoull( sptr, eptr, base );192 } // strto193 194 195 float strto( const char * sptr, char ** eptr ) {196 return strtof( sptr, eptr );197 } // strto198 199 double strto( const char * sptr, char ** eptr ) {200 return strtod( sptr, eptr );201 } // strto202 203 long double strto( const char * sptr, char ** eptr ) {204 return strtold( sptr, eptr );205 } // strto206 207 208 95 float _Complex strto( const char * sptr, char ** eptr ) { 209 96 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; } 214 103 return re + im * _Complex_I; 215 104 } // strto … … 217 106 double _Complex strto( const char * sptr, char ** eptr ) { 218 107 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; } 223 114 return re + im * _Complex_I; 224 115 } // strto … … 226 117 long double _Complex strto( const char * sptr, char ** eptr ) { 227 118 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; } 232 125 return re + im * _Complex_I; 233 126 } // strto … … 243 136 forall( otype T | { int ?<?( T, T ); } ) 244 137 unsigned int bsearch( T key, const T * arr, size_t dim ) { 245 T * result = bsearch( key, arr, dim );138 T * result = bsearch( key, arr, dim ); 246 139 return result ? result - arr : dim; // pointer subtraction includes sizeof(T) 247 140 } // bsearch … … 263 156 //--------------------------------------- 264 157 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(); } 158 void random_seed( long int s ) { srand48( s ); srandom( s ); } // call srandom to harmonize with C-lib random 159 char random( void ) { return (unsigned long int)random(); } 160 char random( char u ) { return random( (unsigned long int)u ); } 161 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } 162 int random( void ) { return (long int)random(); } 163 int random( int u ) { return random( (long int)u ); } 164 int random( int l, int u ) { return random( (long int)l, (long int)u ); } 165 unsigned int random( void ) { return (unsigned long int)random(); } 166 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } 167 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } 168 //extern "C" { long int random() { return mrand48(); } } 169 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } 170 long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; } 285 171 unsigned long int random( void ) { return lrand48(); } 286 172 unsigned 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 lrand48173 unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; } 174 float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48 289 175 double random( void ) { return drand48(); } 290 176 float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } 291 177 double _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); }178 long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } 293 179 294 //---------------------------------------295 296 forall( otype T | { int ?<?( T, T ); } )297 T min( T t1, T t2 ) {298 return t1 < t2 ? t1 : t2;299 } // min300 301 forall( otype T | { int ?>?( T, T ); } )302 T max( T t1, T t2 ) {303 return t1 > t2 ? t1 : t2;304 } // max305 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 } // clamp310 311 forall( otype T )312 void swap( T & t1, T & t2 ) {313 T temp = t1;314 t1 = t2;315 t2 = temp;316 } // swap317 180 318 181 // Local Variables: //
Note: See TracChangeset
for help on using the changeset viewer.